|
| 1 | +package memory |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "sort" |
| 7 | + "sync" |
| 8 | + |
| 9 | + activitypb "github.com/code-payments/flipchat-protobuf-api/generated/go/activity/v1" |
| 10 | + commonpb "github.com/code-payments/flipchat-protobuf-api/generated/go/common/v1" |
| 11 | + "google.golang.org/protobuf/proto" |
| 12 | + |
| 13 | + "github.com/code-payments/flipchat-server/activity" |
| 14 | + "github.com/code-payments/flipchat-server/protoutil" |
| 15 | +) |
| 16 | + |
| 17 | +type NotificationsByTimestamp []*activitypb.Notification |
| 18 | + |
| 19 | +func (a NotificationsByTimestamp) Len() int { return len(a) } |
| 20 | +func (a NotificationsByTimestamp) Swap(i, j int) { a[i], a[j] = a[j], a[i] } |
| 21 | +func (a NotificationsByTimestamp) Less(i, j int) bool { |
| 22 | + return a[i].Ts.AsTime().Before(a[j].Ts.AsTime()) |
| 23 | +} |
| 24 | + |
| 25 | +type InMemoryStore struct { |
| 26 | + mu sync.RWMutex |
| 27 | + notifications map[string][]*activitypb.Notification |
| 28 | +} |
| 29 | + |
| 30 | +func NewInMemory() activity.Store { |
| 31 | + return &InMemoryStore{ |
| 32 | + notifications: map[string][]*activitypb.Notification{}, |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func (m *InMemoryStore) SaveNotification(ctx context.Context, activityFeedType activitypb.ActivityFeedType, userID *commonpb.UserId, notification *activitypb.Notification) (*activitypb.Notification, error) { |
| 37 | + if activityFeedType != activitypb.ActivityFeedType_TRANSACTION_HISTORY { |
| 38 | + return nil, activity.ErrInvalidActivityFeedType |
| 39 | + } |
| 40 | + |
| 41 | + m.mu.Lock() |
| 42 | + defer m.mu.Unlock() |
| 43 | + |
| 44 | + var existing *activitypb.Notification |
| 45 | + for _, n := range m.notifications[string(userID.Value)] { |
| 46 | + if bytes.Equal(notification.Id.Value, n.Id.Value) { |
| 47 | + existing = n |
| 48 | + break |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + switch typed := notification.AdditionalMetadata.(type) { |
| 53 | + case |
| 54 | + *activitypb.Notification_WelcomeBonus, |
| 55 | + *activitypb.Notification_WeeklyBonus, |
| 56 | + *activitypb.Notification_CreateGroup, |
| 57 | + *activitypb.Notification_SendListenerMessage: |
| 58 | + case *activitypb.Notification_SendTip: |
| 59 | + if existing != nil { |
| 60 | + existing.GetSendTip().TotalQuarksSent += typed.SendTip.TotalQuarksSent |
| 61 | + } |
| 62 | + case *activitypb.Notification_ReceivedTip: |
| 63 | + if existing != nil { |
| 64 | + existing.GetReceivedTip().TotalQuarksReceived += typed.ReceivedTip.TotalQuarksReceived |
| 65 | + } |
| 66 | + default: |
| 67 | + return nil, activity.ErrInvalidNotificationType |
| 68 | + } |
| 69 | + |
| 70 | + if existing == nil { |
| 71 | + existing = proto.Clone(notification).(*activitypb.Notification) |
| 72 | + m.notifications[string(userID.Value)] = append(m.notifications[string(userID.Value)], proto.Clone(notification).(*activitypb.Notification)) |
| 73 | + } |
| 74 | + return proto.Clone(existing).(*activitypb.Notification), nil |
| 75 | +} |
| 76 | + |
| 77 | +func (m *InMemoryStore) GetLatestNotifications(ctx context.Context, activityFeedType activitypb.ActivityFeedType, userID *commonpb.UserId, limit int) ([]*activitypb.Notification, error) { |
| 78 | + if activityFeedType != activitypb.ActivityFeedType_TRANSACTION_HISTORY { |
| 79 | + return nil, activity.ErrInvalidActivityFeedType |
| 80 | + } |
| 81 | + |
| 82 | + m.mu.RLock() |
| 83 | + defer m.mu.RUnlock() |
| 84 | + |
| 85 | + res := protoutil.SliceClone(m.notifications[string(userID.Value)]) |
| 86 | + |
| 87 | + sorted := NotificationsByTimestamp(res) |
| 88 | + sort.Sort(sort.Reverse(sorted)) |
| 89 | + |
| 90 | + if len(sorted) > limit { |
| 91 | + sorted = sorted[:limit] |
| 92 | + } |
| 93 | + |
| 94 | + return sorted, nil |
| 95 | +} |
| 96 | + |
| 97 | +func (m *InMemoryStore) reset() { |
| 98 | + m.mu.Lock() |
| 99 | + m.notifications = map[string][]*activitypb.Notification{} |
| 100 | + m.mu.Unlock() |
| 101 | +} |
0 commit comments