-
Notifications
You must be signed in to change notification settings - Fork 0
/
notificator.go
494 lines (419 loc) · 11.8 KB
/
notificator.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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
package notificator
import (
"context"
"database/sql"
"errors"
"fmt"
"time"
"github.com/galexrt/fivenet/gen/go/proto/resources/common/database"
"github.com/galexrt/fivenet/gen/go/proto/resources/notifications"
timestamp "github.com/galexrt/fivenet/gen/go/proto/resources/timestamp"
"github.com/galexrt/fivenet/gen/go/proto/resources/users"
"github.com/galexrt/fivenet/pkg/events"
"github.com/galexrt/fivenet/pkg/grpc/auth"
"github.com/galexrt/fivenet/pkg/grpc/auth/userinfo"
"github.com/galexrt/fivenet/pkg/grpc/errswrap"
"github.com/galexrt/fivenet/pkg/mstlystcdata"
"github.com/galexrt/fivenet/pkg/notifi"
"github.com/galexrt/fivenet/pkg/perms"
"github.com/galexrt/fivenet/query/fivenet/table"
jet "github.com/go-jet/jet/v2/mysql"
"github.com/go-jet/jet/v2/qrm"
"github.com/nats-io/nats.go/jetstream"
"go.uber.org/fx"
"go.uber.org/zap"
grpc "google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"
)
var (
tNotifications = table.FivenetNotifications
tUsers = table.Users.AS("user")
tJobs = table.Jobs
tJobGrades = table.JobGrades
tJobProps = table.FivenetJobProps.AS("jobprops")
)
var (
ErrFailedRequest = status.Error(codes.InvalidArgument, "errors.NotificatorService.ErrFailedRequest")
ErrFailedStream = status.Error(codes.InvalidArgument, "errors.NotificatorService.ErrFailedStream")
)
type Server struct {
NotificatorServiceServer
logger *zap.Logger
db *sql.DB
p perms.Permissions
tm *auth.TokenMgr
ui userinfo.UserInfoRetriever
js *events.JSWrapper
enricher *mstlystcdata.Enricher
}
type Params struct {
fx.In
LC fx.Lifecycle
Logger *zap.Logger
DB *sql.DB
Perms perms.Permissions
TM *auth.TokenMgr
UI userinfo.UserInfoRetriever
JS *events.JSWrapper
Enricher *mstlystcdata.Enricher
}
func NewServer(p Params) *Server {
s := &Server{
logger: p.Logger,
db: p.DB,
p: p.Perms,
tm: p.TM,
ui: p.UI,
js: p.JS,
enricher: p.Enricher,
}
return s
}
func (s *Server) RegisterServer(srv *grpc.Server) {
RegisterNotificatorServiceServer(srv, s)
}
func (s *Server) GetNotifications(ctx context.Context, req *GetNotificationsRequest) (*GetNotificationsResponse, error) {
userInfo := auth.MustGetUserInfoFromContext(ctx)
tNotifications := tNotifications.AS("notification")
condition := tNotifications.UserID.EQ(jet.Int32(userInfo.UserId))
if req.IncludeRead != nil && !*req.IncludeRead {
condition = jet.AND(
condition,
tNotifications.ReadAt.IS_NULL(),
)
}
countStmt := tNotifications.
SELECT(
jet.COUNT(tNotifications.ID).AS("datacount.totalcount"),
).
FROM(tNotifications).
WHERE(condition)
var count database.DataCount
if err := countStmt.QueryContext(ctx, s.db, &count); err != nil {
return nil, errswrap.NewError(err, ErrFailedRequest)
}
pag, limit := req.Pagination.GetResponse(count.TotalCount)
resp := &GetNotificationsResponse{
Pagination: pag,
}
if count.TotalCount <= 0 {
return resp, nil
}
stmt := tNotifications.
SELECT(
tNotifications.ID,
tNotifications.CreatedAt,
tNotifications.ReadAt,
tNotifications.UserID,
tNotifications.Title,
tNotifications.Type,
tNotifications.Content,
tNotifications.Category,
tNotifications.Data,
).
FROM(tNotifications).
WHERE(
condition,
).
OFFSET(req.Pagination.Offset).
LIMIT(limit)
if err := stmt.QueryContext(ctx, s.db, &resp.Notifications); err != nil {
if !errors.Is(err, qrm.ErrNoRows) {
return nil, errswrap.NewError(err, ErrFailedRequest)
}
}
resp.Pagination.Update(len(resp.Notifications))
return resp, nil
}
func (s *Server) MarkNotifications(ctx context.Context, req *MarkNotificationsRequest) (*MarkNotificationsResponse, error) {
userInfo := auth.MustGetUserInfoFromContext(ctx)
condition := tNotifications.UserID.EQ(
jet.Int32(userInfo.UserId)).AND(
tNotifications.ReadAt.IS_NULL(),
)
// If not all
if len(req.Ids) > 0 {
ids := make([]jet.Expression, len(req.Ids))
for i := 0; i < len(req.Ids); i++ {
ids[i] = jet.Uint64(req.Ids[i])
}
condition = condition.AND(tNotifications.ID.IN(ids...))
} else if req.All == nil || !*req.All {
return &MarkNotificationsResponse{}, nil
}
stmt := tNotifications.
UPDATE(
tNotifications.ReadAt,
).
SET(
tNotifications.ReadAt.SET(jet.CURRENT_TIMESTAMP()),
).
WHERE(condition)
res, err := stmt.ExecContext(ctx, s.db)
if err != nil {
return nil, errswrap.NewError(err, ErrFailedRequest)
}
rows, err := res.RowsAffected()
if err != nil {
return nil, errswrap.NewError(err, ErrFailedRequest)
}
return &MarkNotificationsResponse{
Updated: uint64(rows),
}, nil
}
func (s *Server) getNotificationCount(ctx context.Context, userId int32) (int32, error) {
stmt := tNotifications.
SELECT(
jet.COUNT(tNotifications.ID).AS("count"),
).
FROM(tNotifications).
WHERE(jet.AND(
tNotifications.UserID.EQ(jet.Int32(userId)),
tNotifications.ReadAt.IS_NULL(),
)).
ORDER_BY(tNotifications.ID.DESC())
var dest struct {
Count int32
}
if err := stmt.QueryContext(ctx, s.db, &dest); err != nil {
if !errors.Is(err, qrm.ErrNoRows) {
return 0, errswrap.NewError(err, ErrFailedStream)
}
}
return dest.Count, nil
}
func (s *Server) Stream(req *StreamRequest, srv NotificatorService_StreamServer) error {
userInfo, ok := auth.GetUserInfoFromContext(srv.Context())
if !ok {
return ErrFailedStream
}
// Track changes to user info, so we can send an updated user info to the user
currentUserInfo := userInfo.Clone()
// Setup consumer
c, err := s.js.CreateConsumer(srv.Context(), notifi.StreamName, jetstream.ConsumerConfig{
FilterSubject: fmt.Sprintf("%s.%s.%d", notifi.BaseSubject, notifi.UserNotification, currentUserInfo.UserId),
DeliverPolicy: jetstream.DeliverNewPolicy,
})
if err != nil {
return errswrap.NewError(err, ErrFailedStream)
}
cons, err := c.Messages()
if err != nil {
return errswrap.NewError(err, ErrFailedStream)
}
defer cons.Stop()
msgCh := make(chan jetstream.Msg, 8)
go func() {
for {
msg, err := cons.Next()
if err != nil {
close(msgCh)
break
}
msgCh <- msg
}
}()
// Update Ticker
updateTicker := time.NewTicker(45 * time.Second)
defer updateTicker.Stop()
// Check user token validity and update if necessary
data, stop, err := s.checkUser(srv.Context(), currentUserInfo)
if err != nil {
return errswrap.NewError(err, ErrFailedStream)
}
notsCount, err := s.getNotificationCount(srv.Context(), userInfo.UserId)
if err != nil {
return errswrap.NewError(err, ErrFailedStream)
}
if err := srv.Send(&StreamResponse{
NotificationCount: notsCount,
Data: data,
Restart: &stop,
}); err != nil {
return errswrap.NewError(err, ErrFailedStream)
}
for {
resp := &StreamResponse{
NotificationCount: notsCount,
}
select {
case <-srv.Context().Done():
return nil
case <-updateTicker.C:
// Check user token validity
data, stop, err := s.checkUser(srv.Context(), currentUserInfo)
if err != nil {
return errswrap.NewError(err, ErrFailedStream)
}
if data != nil {
resp.Data = data
if err := srv.Send(resp); err != nil {
return errswrap.NewError(err, ErrFailedStream)
}
}
if stop {
// End stream if we should "stop"
return nil
}
// Make sure the notification is in sync (again)
resp.NotificationCount, err = s.getNotificationCount(srv.Context(), userInfo.UserId)
if err != nil {
return errswrap.NewError(err, ErrFailedStream)
}
case msg := <-msgCh:
// Publish notifications sent directly to user via the message queue
if msg == nil {
s.logger.Warn("nil notification message received via message queue", zap.Int32("user_id", currentUserInfo.UserId))
return nil
}
if err := msg.Ack(); err != nil {
s.logger.Error("failed to ack notification message", zap.Error(err))
}
var dest notifications.Notification
if err := proto.Unmarshal(msg.Data(), &dest); err != nil {
return errswrap.NewError(err, ErrFailedStream)
}
resp.Data = &StreamResponse_Notification{
Notification: &dest,
}
resp.NotificationCount++
if err := srv.Send(resp); err != nil {
return errswrap.NewError(err, ErrFailedStream)
}
}
}
}
func (s *Server) checkUser(ctx context.Context, userInfo userinfo.UserInfo) (isStreamResponse_Data, bool, error) {
claims, restart, tu, err := s.checkAndUpdateToken(ctx)
if err != nil {
return nil, false, err
}
if tu != nil && claims.CharID > 0 {
if err := s.checkAndUpdateUserInfo(ctx, tu, userInfo); err != nil {
return nil, false, err
}
return &StreamResponse_Token{
Token: tu,
}, true, nil
}
return nil, restart, nil
}
func (s *Server) checkAndUpdateToken(ctx context.Context) (*auth.CitizenInfoClaims, bool, *TokenUpdate, error) {
token, err := auth.GetTokenFromGRPCContext(ctx)
if err != nil {
return nil, true, nil, auth.ErrInvalidToken
}
claims, err := s.tm.ParseWithClaims(token)
if err != nil {
return nil, true, nil, auth.ErrInvalidToken
}
if time.Until(claims.ExpiresAt.Time) <= auth.TokenRenewalTime {
if claims.RenewedCount >= auth.TokenMaxRenews {
return nil, true, nil, auth.ErrInvalidToken
}
// Increase re-newed count
claims.RenewedCount++
auth.SetTokenClaimsTimes(claims)
newToken, err := s.tm.NewWithClaims(claims)
if err != nil {
return nil, true, nil, auth.ErrCheckToken
}
tu := &TokenUpdate{
NewToken: &newToken,
Expires: timestamp.New(claims.ExpiresAt.Time),
}
return claims, true, tu, nil
}
return claims, false, nil, nil
}
func (s *Server) checkAndUpdateUserInfo(ctx context.Context, tu *TokenUpdate, currentUserInfo userinfo.UserInfo) error {
userInfo, err := s.ui.GetUserInfo(ctx, currentUserInfo.UserId, currentUserInfo.AccountId)
if err != nil {
return err
}
// If the user is logged into a character, update user info and load permissions of user
if currentUserInfo.Equal(userInfo) {
return nil
}
char, jobProps, group, err := s.getCharacter(ctx, userInfo.UserId)
if err != nil {
return err
}
tu.UserInfo = char
tu.JobProps = jobProps
// Update current user info with new data from database
currentUserInfo.UserId = char.UserId
currentUserInfo.Job = char.Job
currentUserInfo.JobGrade = char.JobGrade
currentUserInfo.Group = group
ps, err := s.p.GetPermissionsOfUser(&userinfo.UserInfo{
UserId: userInfo.UserId,
Job: userInfo.Job,
JobGrade: userInfo.JobGrade,
})
if err != nil {
return auth.ErrUserNoPerms
}
tu.Permissions = ps.GuardNames()
if userInfo.SuperUser {
tu.Permissions = append(tu.Permissions, auth.PermSuperUserKey)
}
attrs, err := s.p.FlattenRoleAttributes(userInfo.Job, userInfo.JobGrade)
if err != nil {
return auth.ErrUserNoPerms
}
tu.Permissions = append(tu.Permissions, attrs...)
return nil
}
func (s *Server) getCharacter(ctx context.Context, charId int32) (*users.User, *users.JobProps, string, error) {
stmt := tUsers.
SELECT(
tUsers.ID,
tUsers.Identifier,
tUsers.Job,
tUsers.JobGrade,
tUsers.Firstname,
tUsers.Lastname,
tUsers.Dateofbirth,
tUsers.Group.AS("group"),
tJobs.Label.AS("user.job_label"),
tJobGrades.Label.AS("user.job_grade_label"),
tJobProps.Theme,
tJobProps.RadioFrequency,
tJobProps.QuickButtons,
tJobProps.LogoURL,
).
FROM(
tUsers.
LEFT_JOIN(tJobs,
tJobs.Name.EQ(tUsers.Job),
).
LEFT_JOIN(tJobGrades,
jet.AND(
tJobGrades.Grade.EQ(tUsers.JobGrade),
tJobGrades.JobName.EQ(tUsers.Job),
),
).
LEFT_JOIN(tJobProps,
tJobProps.Job.EQ(tJobs.Name),
),
).
WHERE(
tUsers.ID.EQ(jet.Int32(charId)),
).
LIMIT(1)
var dest struct {
users.User
Group string
JobProps *users.JobProps
}
if err := stmt.QueryContext(ctx, s.db, &dest); err != nil {
return nil, nil, "", err
}
if dest.JobProps != nil {
s.enricher.EnrichJobName(dest.JobProps)
}
return &dest.User, dest.JobProps, dest.Group, nil
}