-
Notifications
You must be signed in to change notification settings - Fork 1
/
interfaceImplementation.go
176 lines (151 loc) · 6.4 KB
/
interfaceImplementation.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package notificationSender
import (
"fmt"
"path"
"github.com/olahol/melody"
"github.com/pixlise/core/v4/api/ws"
"github.com/pixlise/core/v4/api/ws/wsHelpers"
"github.com/pixlise/core/v4/core/idgen"
"github.com/pixlise/core/v4/core/logger"
"github.com/pixlise/core/v4/core/timestamper"
protos "github.com/pixlise/core/v4/generated-protos"
"go.mongodb.org/mongo-driver/mongo"
)
type NotificationSender struct {
instanceId string
db *mongo.Database
timestamper timestamper.ITimeStamper // So we can mock time.Now()
log logger.ILogger
envRootURL string
ws *ws.WSHandler
melody *melody.Melody
idgen idgen.IDGenerator
}
func MakeNotificationSender(instanceId string, db *mongo.Database, idgen idgen.IDGenerator, timestamper timestamper.ITimeStamper, log logger.ILogger, envRootURL string, ws *ws.WSHandler, melody *melody.Melody) *NotificationSender {
return &NotificationSender{
instanceId: instanceId,
db: db,
timestamper: timestamper,
log: log,
ws: ws,
melody: melody,
idgen: idgen,
}
}
func (n *NotificationSender) NotifyNewScan(scanName string, scanId string) {
notifMsg := &protos.NotificationUpd{
Notification: &protos.Notification{
NotificationType: protos.NotificationType_NT_USER_MESSAGE,
Subject: fmt.Sprintf("New scan imported: %v", scanName),
Contents: fmt.Sprintf("A new scan named %v was just imported. Scan ID is: %v", scanName, scanId),
From: "Data Importer",
ActionLink: path.Join(n.envRootURL, "?q="+scanId),
},
}
n.sendNotificationToObjectUsers(NOTIF_TOPIC_SCAN_NEW, notifMsg, scanId)
}
func (n *NotificationSender) NotifyUpdatedScan(scanName string, scanId string) {
notifMsg := &protos.NotificationUpd{
Notification: &protos.Notification{
NotificationType: protos.NotificationType_NT_USER_MESSAGE,
Subject: fmt.Sprintf("Updated scan: %v", scanName),
Contents: fmt.Sprintf("The scan named %v, which you have access to, was just updated. Scan ID is: %v", scanName, scanId),
From: "Data Importer",
ActionLink: path.Join(n.envRootURL, "?q="+scanId),
},
}
n.sendNotificationToObjectUsers(NOTIF_TOPIC_SCAN_UPDATED, notifMsg, scanId)
}
func (n *NotificationSender) SysNotifyScanChanged(scanId string) {
wsSysNotify := &protos.NotificationUpd{
Notification: &protos.Notification{
NotificationType: protos.NotificationType_NT_SYS_DATA_CHANGED,
ScanIds: []string{scanId},
},
}
n.sendSysNotification(wsSysNotify)
}
func (n *NotificationSender) NotifyNewScanImage(scanName string, scanId string, imageName string) {
notifMsg := &protos.NotificationUpd{
Notification: &protos.Notification{
NotificationType: protos.NotificationType_NT_USER_MESSAGE,
Subject: fmt.Sprintf("New image added to scan: %v", scanName),
Contents: fmt.Sprintf("A new image named %v was added to scan: %v (id: %v)", path.Base(imageName), scanName, scanId),
From: "Data Importer",
ActionLink: path.Join(n.envRootURL, "?q="+scanId+"&image="+imageName),
},
}
n.sendNotificationToObjectUsers(NOTIF_TOPIC_IMAGE_NEW, notifMsg, scanId)
}
func (n *NotificationSender) SysNotifyScanImagesChanged(scanIds []string) {
wsSysNotify := &protos.NotificationUpd{
Notification: &protos.Notification{
NotificationType: protos.NotificationType_NT_SYS_DATA_CHANGED,
ScanIds: scanIds,
},
}
n.sendSysNotification(wsSysNotify)
}
func (n *NotificationSender) NotifyNewQuant(uploaded bool, quantId string, quantName string, status string, scanName string, scanId string) {
notifMsg := &protos.NotificationUpd{
Notification: &protos.Notification{
NotificationType: protos.NotificationType_NT_USER_MESSAGE,
Subject: fmt.Sprintf("Quantification %v has completed with status: %v", quantName, status),
Contents: fmt.Sprintf("A quantification named %v (id: %v) has completed with status %v. This quantification is for the scan named: %v", quantName, quantId, status, scanName),
From: "Data Importer",
ActionLink: path.Join(n.envRootURL, "?q="+scanId+"&quant="+quantId),
},
}
n.sendNotificationToObjectUsers(NOTIF_TOPIC_QUANT_COMPLETE, notifMsg, quantId)
}
func (n *NotificationSender) SysNotifyQuantChanged(quantId string) {
wsSysNotify := &protos.NotificationUpd{
Notification: &protos.Notification{
NotificationType: protos.NotificationType_NT_SYS_DATA_CHANGED,
QuantId: quantId,
},
}
n.sendSysNotification(wsSysNotify)
}
func (n *NotificationSender) NotifyObjectShared(objectType string, objectId string, objectName, sharerName string) {
notifMsg := &protos.NotificationUpd{
Notification: &protos.Notification{
NotificationType: protos.NotificationType_NT_USER_MESSAGE,
Subject: fmt.Sprintf("%v was just shared", objectType),
Contents: fmt.Sprintf("An object of type %v named %v was just shared by %v", objectType, objectName, sharerName),
From: "PIXLISE back-end",
ActionLink: "",
},
}
n.sendNotificationToObjectUsers(NOTIF_TOPIC_OBJECT_SHARED, notifMsg, objectId)
}
func (n *NotificationSender) NotifyUserGroupMessage(subject string, message string, notificationType protos.NotificationType, actionLink string, groupId string, groupName string, sender string) {
notifMsg := &protos.NotificationUpd{
Notification: &protos.Notification{
NotificationType: notificationType,
Subject: subject,
Contents: fmt.Sprintf("%v\nThis message was sent by %v to group %v", message, sender, groupName),
From: sender,
ActionLink: actionLink,
},
}
userIds, err := wsHelpers.GetUserIdsForGroup([]string{groupId}, n.db)
if err != nil {
n.log.Errorf("Failed to get user ids for group: %v. Error: %v", groupId, err)
return
}
n.sendNotification(subject, "", notifMsg, userIds)
}
func (n *NotificationSender) NotifyUserMessage(subject string, message string, notificationType protos.NotificationType, actionLink string, requestorUserId string, destUserIds []string, sender string) {
notifMsg := &protos.NotificationUpd{
Notification: &protos.Notification{
NotificationType: notificationType,
Subject: subject,
Contents: fmt.Sprintf("%v\nThis message was sent by %v", message, sender),
From: sender,
ActionLink: actionLink,
RequestorUserId: requestorUserId,
},
}
n.sendNotification(subject, "", notifMsg, destUserIds)
}