/
notification_service.go
71 lines (62 loc) · 1.89 KB
/
notification_service.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
package services
import (
"context"
"github.com/mreider/koto/backend/common"
"github.com/mreider/koto/backend/messagehub/rpc"
)
type notificationService struct {
*BaseService
}
func NewNotification(base *BaseService) rpc.NotificationService {
return ¬ificationService{
BaseService: base,
}
}
func (s *notificationService) Count(ctx context.Context, _ *rpc.Empty) (*rpc.NotificationCountResponse, error) {
me := s.getMe(ctx)
total, unread, err := s.repos.Notification.Counts(me.ID)
if err != nil {
return nil, err
}
return &rpc.NotificationCountResponse{
Total: int32(total),
Unread: int32(unread),
}, nil
}
func (s *notificationService) Notifications(ctx context.Context, _ *rpc.Empty) (*rpc.NotificationNotificationsResponse, error) {
me := s.getMe(ctx)
notifications, err := s.repos.Notification.Notifications(me.ID)
if err != nil {
return nil, err
}
rpcNotifications := make([]*rpc.Notification, len(notifications))
for i, notification := range notifications {
rpcNotifications[i] = &rpc.Notification{
Id: notification.ID,
Text: notification.Text,
Type: notification.Type,
Data: notification.Data.String(),
CreatedAt: common.TimeToRPCString(notification.CreatedAt),
ReadAt: common.NullTimeToRPCString(notification.ReadAt),
}
}
return &rpc.NotificationNotificationsResponse{
Notifications: rpcNotifications,
}, nil
}
func (s *notificationService) Clean(ctx context.Context, r *rpc.NotificationCleanRequest) (*rpc.Empty, error) {
me := s.getMe(ctx)
err := s.repos.Notification.Clean(me.ID, r.LastKnownId)
if err != nil {
return nil, err
}
return &rpc.Empty{}, nil
}
func (s *notificationService) MarkRead(ctx context.Context, r *rpc.NotificationMarkReadRequest) (*rpc.Empty, error) {
me := s.getMe(ctx)
err := s.repos.Notification.MarkRead(me.ID, r.LastKnownId)
if err != nil {
return nil, err
}
return &rpc.Empty{}, nil
}