/
module.go
116 lines (96 loc) · 2.74 KB
/
module.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package notifications
import (
"context"
firebase "firebase.google.com/go/v4"
"firebase.google.com/go/v4/messaging"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/forbole/juno/v5/modules"
"github.com/forbole/juno/v5/types/config"
"google.golang.org/api/option"
notificationsbuilder "github.com/desmos-labs/athena/x/notifications/builder"
notificationssender "github.com/desmos-labs/athena/x/notifications/sender"
)
var (
_ modules.Module = &Module{}
_ modules.TransactionModule = &Module{}
_ modules.MessageModule = &Module{}
)
type Module struct {
cdc codec.Codec
db Database
cfg *Config
app *firebase.App
client *messaging.Client
postsModule PostsModule
reactionsModule ReactionsModule
notificationsBuilder notificationsbuilder.NotificationsBuilder
buildMessage notificationsbuilder.MessagesBuilder
notificationSender notificationssender.NotificationSender
}
// NewModule returns a new Module instance
func NewModule(
junoCfg config.Config,
postsModule PostsModule, reactionsModule ReactionsModule,
cdc codec.Codec, db Database,
) *Module {
bz, err := junoCfg.GetBytes()
if err != nil {
panic(err)
}
cfg, err := ParseConfig(bz)
if err != nil {
panic(err)
}
if cfg == nil {
return nil
}
// Build the firebase app
app, err := firebase.NewApp(context.Background(), nil, option.WithCredentialsFile(cfg.FirebaseCredentialsFilePath))
if err != nil {
panic(err)
}
// Build the FCM client
client, err := app.Messaging(context.Background())
if err != nil {
panic(err)
}
// Build the module
module := &Module{
cdc: cdc,
db: db,
cfg: cfg,
app: app,
client: client,
postsModule: postsModule,
reactionsModule: reactionsModule,
}
// Set the default messages builder and sender
module = module.WithMessagesBuilder(module.BuildMessage)
module = module.WithNotificationSender(module.sendNotification)
return module
}
// Name implements modules.Module
func (m *Module) Name() string {
return "notifications"
}
// WithNotificationsBuilder sets the given builder as the notifications builder
func (m *Module) WithNotificationsBuilder(builder notificationsbuilder.NotificationsBuilder) *Module {
if builder != nil {
m.notificationsBuilder = builder
}
return m
}
// WithMessagesBuilder sets the given builder as the messages builder
func (m *Module) WithMessagesBuilder(builder notificationsbuilder.MessagesBuilder) *Module {
if builder != nil {
m.buildMessage = builder
}
return m
}
// WithNotificationSender sets the given sender as the notification sender
func (m *Module) WithNotificationSender(sender notificationssender.NotificationSender) *Module {
if sender != nil {
m.notificationSender = sender
}
return m
}