-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotifications.go
More file actions
201 lines (176 loc) · 6.87 KB
/
notifications.go
File metadata and controls
201 lines (176 loc) · 6.87 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package api
import (
"api-server/customerrors"
"api-server/db"
"api-server/models"
"api-server/utils"
"encoding/json"
"net/http"
"net/url"
"os"
"github.com/gin-gonic/gin"
"go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/v2/mongo"
"go.uber.org/zap"
)
type OnlineNotificationsResponse struct {
Notifications []OnlineNotification `json:"notifications"`
}
type OnlineNotification struct {
ID string `json:"id"`
SentAt uint64 `json:"sentAt"`
Title string `json:"title"`
Body string `json:"body"`
DeviceCount uint64 `json:"deviceCount"`
Devices []OnlineNotificationDevice `json:"devices"`
Provider string `json:"provider"`
ProviderMessageID string `json:"providerMessageId"`
}
type OnlineNotificationDevice struct {
DeviceUUID string `json:"deviceUuid"`
}
type NotificationsResponse struct {
Notifications []Notification `json:"notifications"`
}
type Notification struct {
ID string `json:"id"`
SentAt uint64 `json:"sentAt"`
Title string `json:"title"`
Body string `json:"body"`
DeviceCount uint64 `json:"deviceCount"`
Devices []models.Device `json:"devices"`
Provider string `json:"provider"`
ProviderMessageID string `json:"providerMessageId"`
}
// Notifications handles notification-history lookups via the external online service.
type Notifications struct {
client *mongo.Client
collDevices *mongo.Collection
collProfiles *mongo.Collection
logger *zap.SugaredLogger
onlineNotificationsURL string
}
// NewNotifications constructs a Notifications handler with the given dependencies.
func NewNotifications(logger *zap.SugaredLogger, client *mongo.Client) *Notifications {
onlineServerURL := os.Getenv("HTTP_ONLINE_SERVER") + ":" + os.Getenv("HTTP_ONLINE_PORT")
onlineNotificationsAPI := os.Getenv("HTTP_ONLINE_NOTIFICATIONS_API")
return &Notifications{
client: client,
collDevices: db.GetCollections(client).Devices,
collProfiles: db.GetCollections(client).Profiles,
logger: logger,
onlineNotificationsURL: onlineServerURL + onlineNotificationsAPI,
}
}
// GetNotifications returns notification history for the authenticated profile.
func (n *Notifications) GetNotifications(c *gin.Context) {
n.logger.Info("REST - GET - GetNotifications called")
profile, err := utils.GetLoggedProfileFromContext(c, n.collProfiles)
if err != nil {
n.logger.Error("REST - GET - GetNotifications - cannot find profile")
c.JSON(http.StatusUnauthorized, gin.H{"error": "cannot find profile"})
return
}
apiToken, err := decryptProfileAPIToken(&profile)
if err != nil {
n.logger.Error("REST - GET - GetNotifications - cannot load profile api token")
c.JSON(http.StatusInternalServerError, gin.H{"error": "Cannot get notifications"})
return
}
if !utils.IsValidUUID(apiToken) {
n.logger.Error("REST - GET - GetNotifications - invalid profile api token format")
c.JSON(http.StatusInternalServerError, gin.H{"error": "Cannot get notifications"})
return
}
path := n.onlineNotificationsURL + url.PathEscape(apiToken)
n.logger.Debug("REST - GET - GetNotifications - calling external 'online' notifications service using apiToken")
_, result, err := n.notificationsService(path)
if err != nil {
n.logger.Errorf("REST - GET - GetNotifications - cannot get notifications from remote service = %#v", err)
if re, ok := err.(*customerrors.ErrorWrapper); ok {
n.logger.Errorf("REST - GET - GetNotifications - remote status = %d, message = %s\n", re.Code, re.Message)
}
c.JSON(http.StatusInternalServerError, gin.H{"error": "Cannot get notifications"})
return
}
var onlineResponse OnlineNotificationsResponse
if err = json.Unmarshal([]byte(result), &onlineResponse); err != nil {
n.logger.Errorf("REST - GET - GetNotifications - cannot unmarshal JSON response from online remote service = %#v", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Cannot get notifications response"})
return
}
response, err := n.enrichNotifications(c, profile, onlineResponse)
if err != nil {
n.logger.Errorf("REST - GET - GetNotifications - cannot enrich notification devices = %#v", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Cannot get notifications response"})
return
}
c.JSON(http.StatusOK, response)
}
func (n *Notifications) enrichNotifications(c *gin.Context, profile models.Profile, onlineResponse OnlineNotificationsResponse) (NotificationsResponse, error) {
deviceUUIDs := make([]string, 0)
for _, item := range onlineResponse.Notifications {
for _, device := range item.Devices {
if device.DeviceUUID != "" {
deviceUUIDs = append(deviceUUIDs, device.DeviceUUID)
}
}
}
devicesByUUID, err := n.getProfileDevicesByUUID(c, profile, deviceUUIDs)
if err != nil {
return NotificationsResponse{}, err
}
response := NotificationsResponse{
Notifications: make([]Notification, 0, len(onlineResponse.Notifications)),
}
for _, item := range onlineResponse.Notifications {
devices := make([]models.Device, 0, len(item.Devices))
for _, onlineDevice := range item.Devices {
device, ok := devicesByUUID[onlineDevice.DeviceUUID]
if ok {
devices = append(devices, device)
}
}
response.Notifications = append(response.Notifications, Notification{
ID: item.ID,
SentAt: item.SentAt,
Title: item.Title,
Body: item.Body,
DeviceCount: item.DeviceCount,
Devices: devices,
Provider: item.Provider,
ProviderMessageID: item.ProviderMessageID,
})
}
return response, nil
}
func (n *Notifications) getProfileDevicesByUUID(c *gin.Context, profile models.Profile, deviceUUIDs []string) (map[string]models.Device, error) {
devicesByUUID := make(map[string]models.Device)
if len(deviceUUIDs) == 0 || len(profile.Devices) == 0 {
return devicesByUUID, nil
}
cur, err := n.collDevices.Find(c.Request.Context(), bson.M{
// resolve the device references returned by the online service notification payload
"_id": bson.M{"$in": profile.Devices},
// ensure we only return devices owned by the authenticated profile
"uuid": bson.M{"$in": deviceUUIDs},
})
if err != nil {
return nil, err
}
defer cur.Close(c.Request.Context())
for cur.Next(c.Request.Context()) {
var device models.Device
if err = cur.Decode(&device); err != nil {
return nil, err
}
devicesByUUID[device.UUID] = device
}
if err = cur.Err(); err != nil {
return nil, err
}
return devicesByUUID, nil
}
func (n *Notifications) notificationsService(urlNotifications string) (int, string, error) {
return utils.Get(urlNotifications)
}