[Fixes: #4088] Fix pending notification test

There were 2 issues:

1) We hard delete requests, that means that on retransmission they will
be recreated, the test has been changed to accommodate this behavior
2) We always used time.now when updating timestamp in notification,
   sometimes time is the same so the notification is not updated, we
   changed to use what essentially is a clock value
This commit is contained in:
Andrea Maria Piana 2023-11-02 10:38:45 +00:00
parent 05530f57bd
commit 071c431606
3 changed files with 26 additions and 16 deletions

View File

@ -76,6 +76,16 @@ type ActivityCenterNotification struct {
AlbumMessages []*common.Message `json:"albumMessages"` 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 { type ActivityCenterNotificationsRequest struct {
Cursor string `json:"cursor"` Cursor string `json:"cursor"`
Limit uint64 `json:"limit"` Limit uint64 `json:"limit"`

View File

@ -1053,25 +1053,26 @@ func (s *MessengerCommunitiesSuite) TestDeletePendingRequestAccess() {
s.Require().NotNil(response) s.Require().NotNil(response)
s.Require().Len(response.RequestsToJoinCommunity, 1) s.Require().Len(response.RequestsToJoinCommunity, 1)
aliceRequestToJoin := response.RequestsToJoinCommunity[0]
// Retrieve request to join and Check activity center notification for Bob // Retrieve request to join and Check activity center notification for Bob
err = tt.RetryWithBackOff(func() error { err = tt.RetryWithBackOff(func() error {
response, err = bobRetrieveAll() response, err = bobRetrieveAll()
if err != nil { if err != nil {
return err 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 { for _, r := range response.RequestsToJoinCommunity {
return errors.New("request to join community not received") if bytes.Equal(r.ID, aliceRequestToJoin.ID) {
return nil
}
} }
return errors.New("request to join not found")
if len(response.ActivityCenterNotifications()) == 0 {
return errors.New("request to join community notification not added in activity center")
}
return nil
}) })
s.Require().NoError(err) s.Require().NoError(err)
s.Require().Len(response.RequestsToJoinCommunity, 1)
// Check activity center notification for Bob // Check activity center notification for Bob
notifications, err = fetchActivityCenterNotificationsForAdmin() notifications, err = fetchActivityCenterNotificationsForAdmin()

View File

@ -1530,8 +1530,7 @@ func (m *Messenger) CancelRequestToJoinCommunity(ctx context.Context, request *r
} }
if notification != nil { if notification != nil {
updatedAt := m.GetCurrentTimeInMillis() notification.IncrementUpdatedAt(m.getTimesource())
notification.UpdatedAt = updatedAt
err = m.persistence.DeleteActivityCenterNotificationByID(types.FromHex(requestToJoin.ID.String()), notification.UpdatedAt) err = m.persistence.DeleteActivityCenterNotificationByID(types.FromHex(requestToJoin.ID.String()), notification.UpdatedAt)
if err != nil { if err != nil {
m.logger.Error("failed to delete notification from Activity Center", zap.Error(err)) 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 // set notification as deleted, so that the client will remove the activity center notification from UI
notification.Deleted = true 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 { if err != nil {
m.logger.Error("CancelRequestToJoinCommunity, failed to sync activity center notification as deleted", zap.Error(err)) m.logger.Error("CancelRequestToJoinCommunity, failed to sync activity center notification as deleted", zap.Error(err))
return nil, err return nil, err
@ -1631,7 +1630,7 @@ func (m *Messenger) acceptRequestToJoinCommunity(requestToJoin *communities.Requ
} }
notification.Read = true notification.Read = true
notification.Accepted = true notification.Accepted = true
notification.UpdatedAt = m.GetCurrentTimeInMillis() notification.IncrementUpdatedAt(m.getTimesource())
err = m.addActivityCenterNotification(response, notification, m.syncActivityCenterCommunityRequestDecisionAdapter) err = m.addActivityCenterNotification(response, notification, m.syncActivityCenterCommunityRequestDecisionAdapter)
if err != nil { if err != nil {
@ -1715,7 +1714,7 @@ func (m *Messenger) declineRequestToJoinCommunity(requestToJoin *communities.Req
} }
notification.Read = true notification.Read = true
notification.Dismissed = true notification.Dismissed = true
notification.UpdatedAt = m.GetCurrentTimeInMillis() notification.IncrementUpdatedAt(m.getTimesource())
err = m.addActivityCenterNotification(response, notification, m.syncActivityCenterCommunityRequestDecisionAdapter) err = m.addActivityCenterNotification(response, notification, m.syncActivityCenterCommunityRequestDecisionAdapter)
if err != nil { if err != nil {
@ -1898,7 +1897,7 @@ func (m *Messenger) CheckAndDeletePendingRequestToJoinCommunity(ctx context.Cont
notification.MembershipStatus = ActivityCenterMembershipStatusIdle notification.MembershipStatus = ActivityCenterMembershipStatusIdle
notification.Read = false notification.Read = false
notification.Deleted = false notification.Deleted = false
notification.UpdatedAt = m.GetCurrentTimeInMillis() notification.IncrementUpdatedAt(m.getTimesource())
err = m.addActivityCenterNotification(response, notification, m.syncActivityCenterUnreadByIDs) err = m.addActivityCenterNotification(response, notification, m.syncActivityCenterUnreadByIDs)
if err != nil { if err != nil {
m.logger.Error("failed to update notification in activity center", zap.Error(err)) 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.Read = true
notification.Accepted = true notification.Accepted = true
notification.UpdatedAt = m.GetCurrentTimeInMillis() notification.IncrementUpdatedAt(m.getTimesource())
err = m.addActivityCenterNotification(state.Response, notification, nil) err = m.addActivityCenterNotification(state.Response, notification, nil)
if err != nil { if err != nil {