diff --git a/protocol/activity_center.go b/protocol/activity_center.go index cc98858ef..001eda50b 100644 --- a/protocol/activity_center.go +++ b/protocol/activity_center.go @@ -76,6 +76,16 @@ type ActivityCenterNotification struct { AlbumMessages []*common.Message `json:"albumMessages"` } +func (n *ActivityCenterNotification) IncrementUpdatedAt(timesource common.TimeSource) { + tNow := timesource.GetCurrentTime() + // If updatead at is greater or equal than time now, we bump it + if n.UpdatedAt >= tNow { + n.UpdatedAt++ + } else { + n.UpdatedAt = tNow + } +} + type ActivityCenterNotificationsRequest struct { Cursor string `json:"cursor"` Limit uint64 `json:"limit"` diff --git a/protocol/communities_messenger_test.go b/protocol/communities_messenger_test.go index 2cf0515ad..2932cae4c 100644 --- a/protocol/communities_messenger_test.go +++ b/protocol/communities_messenger_test.go @@ -1053,25 +1053,26 @@ func (s *MessengerCommunitiesSuite) TestDeletePendingRequestAccess() { s.Require().NotNil(response) s.Require().Len(response.RequestsToJoinCommunity, 1) + aliceRequestToJoin := response.RequestsToJoinCommunity[0] + // Retrieve request to join and Check activity center notification for Bob err = tt.RetryWithBackOff(func() error { response, err = bobRetrieveAll() if err != nil { return err } + // NOTE: we might receive multiple requests to join in case of re-transmissions + // because request to join are hard deleted from the database, we can't check + // whether that's an old one or a new one. So here we test for the specific id - if len(response.RequestsToJoinCommunity) == 0 { - return errors.New("request to join community not received") + for _, r := range response.RequestsToJoinCommunity { + if bytes.Equal(r.ID, aliceRequestToJoin.ID) { + return nil + } } - - if len(response.ActivityCenterNotifications()) == 0 { - return errors.New("request to join community notification not added in activity center") - } - - return nil + return errors.New("request to join not found") }) s.Require().NoError(err) - s.Require().Len(response.RequestsToJoinCommunity, 1) // Check activity center notification for Bob notifications, err = fetchActivityCenterNotificationsForAdmin() diff --git a/protocol/messenger_communities.go b/protocol/messenger_communities.go index c826ca0fe..646f95318 100644 --- a/protocol/messenger_communities.go +++ b/protocol/messenger_communities.go @@ -1530,8 +1530,7 @@ func (m *Messenger) CancelRequestToJoinCommunity(ctx context.Context, request *r } if notification != nil { - updatedAt := m.GetCurrentTimeInMillis() - notification.UpdatedAt = updatedAt + notification.IncrementUpdatedAt(m.getTimesource()) err = m.persistence.DeleteActivityCenterNotificationByID(types.FromHex(requestToJoin.ID.String()), notification.UpdatedAt) if err != nil { m.logger.Error("failed to delete notification from Activity Center", zap.Error(err)) @@ -1540,7 +1539,7 @@ func (m *Messenger) CancelRequestToJoinCommunity(ctx context.Context, request *r // set notification as deleted, so that the client will remove the activity center notification from UI notification.Deleted = true - err = m.syncActivityCenterDeletedByIDs(ctx, []types.HexBytes{notification.ID}, updatedAt) + err = m.syncActivityCenterDeletedByIDs(ctx, []types.HexBytes{notification.ID}, notification.UpdatedAt) if err != nil { m.logger.Error("CancelRequestToJoinCommunity, failed to sync activity center notification as deleted", zap.Error(err)) return nil, err @@ -1631,7 +1630,7 @@ func (m *Messenger) acceptRequestToJoinCommunity(requestToJoin *communities.Requ } notification.Read = true notification.Accepted = true - notification.UpdatedAt = m.GetCurrentTimeInMillis() + notification.IncrementUpdatedAt(m.getTimesource()) err = m.addActivityCenterNotification(response, notification, m.syncActivityCenterCommunityRequestDecisionAdapter) if err != nil { @@ -1715,7 +1714,7 @@ func (m *Messenger) declineRequestToJoinCommunity(requestToJoin *communities.Req } notification.Read = true notification.Dismissed = true - notification.UpdatedAt = m.GetCurrentTimeInMillis() + notification.IncrementUpdatedAt(m.getTimesource()) err = m.addActivityCenterNotification(response, notification, m.syncActivityCenterCommunityRequestDecisionAdapter) if err != nil { @@ -1898,7 +1897,7 @@ func (m *Messenger) CheckAndDeletePendingRequestToJoinCommunity(ctx context.Cont notification.MembershipStatus = ActivityCenterMembershipStatusIdle notification.Read = false notification.Deleted = false - notification.UpdatedAt = m.GetCurrentTimeInMillis() + notification.IncrementUpdatedAt(m.getTimesource()) err = m.addActivityCenterNotification(response, notification, m.syncActivityCenterUnreadByIDs) if err != nil { m.logger.Error("failed to update notification in activity center", zap.Error(err)) @@ -3019,7 +3018,7 @@ func (m *Messenger) handleCommunityResponse(state *ReceivedMessageState, communi notification.Read = true notification.Accepted = true - notification.UpdatedAt = m.GetCurrentTimeInMillis() + notification.IncrementUpdatedAt(m.getTimesource()) err = m.addActivityCenterNotification(state.Response, notification, nil) if err != nil {