github.com/status-im/status-go@v1.1.0/protocol/activity_center_persistence_test.go (about)

     1  package protocol
     2  
     3  import (
     4  	"strconv"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/stretchr/testify/suite"
     9  
    10  	"github.com/status-im/status-go/eth-node/types"
    11  	"github.com/status-im/status-go/protocol/common"
    12  	"github.com/status-im/status-go/protocol/protobuf"
    13  )
    14  
    15  func TestActivityCenterPersistence(t *testing.T) {
    16  	suite.Run(t, new(ActivityCenterPersistenceTestSuite))
    17  }
    18  
    19  type ActivityCenterPersistenceTestSuite struct {
    20  	suite.Suite
    21  	idCounter int
    22  }
    23  
    24  func (s *ActivityCenterPersistenceTestSuite) SetupTest() {
    25  	s.idCounter = 0
    26  }
    27  
    28  func currentMilliseconds() uint64 {
    29  	c := time.Now().UnixMilli()
    30  	return uint64(c)
    31  }
    32  
    33  func (s *ActivityCenterPersistenceTestSuite) createNotifications(p *sqlitePersistence, notifications []*ActivityCenterNotification) []*ActivityCenterNotification {
    34  	now := currentMilliseconds()
    35  	for index, notif := range notifications {
    36  		if notif.Timestamp == 0 {
    37  			notif.Timestamp = now
    38  		}
    39  		if len(notif.ID) == 0 {
    40  			s.idCounter++
    41  			notif.ID = types.HexBytes(strconv.Itoa(s.idCounter + index))
    42  		}
    43  		if notif.UpdatedAt == 0 {
    44  			notif.UpdatedAt = now
    45  		}
    46  		_, err := p.SaveActivityCenterNotification(notif, true)
    47  		s.Require().NoError(err, notif.ID)
    48  	}
    49  
    50  	// Fetches notifications to get an up-to-date slice.
    51  	var createdNotifications []*ActivityCenterNotification
    52  	for _, notif := range notifications {
    53  		n, err := p.GetActivityCenterNotificationByID(notif.ID)
    54  		s.Require().NoError(err, notif.ID)
    55  		createdNotifications = append(createdNotifications, n)
    56  	}
    57  
    58  	return createdNotifications
    59  }
    60  
    61  func (s *ActivityCenterPersistenceTestSuite) Test_DeleteActivityCenterNotificationsWhenEmpty() {
    62  	db, err := openTestDB()
    63  	s.Require().NoError(err)
    64  	p := newSQLitePersistence(db)
    65  
    66  	chat := CreatePublicChat("test-chat", &testTimeSource{})
    67  	message := common.NewMessage()
    68  	message.Text = "sample text"
    69  	chat.LastMessage = message
    70  	err = p.SaveChat(*chat)
    71  	s.Require().NoError(err)
    72  
    73  	s.createNotifications(p, []*ActivityCenterNotification{
    74  		{
    75  			Type: ActivityCenterNotificationTypeMention,
    76  		},
    77  	})
    78  
    79  	var count uint64
    80  	count, _ = p.ActivityCenterNotificationsCount([]ActivityCenterType{}, ActivityCenterQueryParamsReadUnread, false)
    81  	s.Require().Equal(uint64(1), count)
    82  
    83  	_, err = p.MarkActivityCenterNotificationsDeleted([]types.HexBytes{}, currentMilliseconds())
    84  	s.Require().NoError(err)
    85  
    86  	count, _ = p.ActivityCenterNotificationsCount([]ActivityCenterType{}, ActivityCenterQueryParamsReadUnread, false)
    87  	s.Require().Equal(uint64(1), count)
    88  }
    89  
    90  func (s *ActivityCenterPersistenceTestSuite) Test_DeleteActivityCenterNotificationsWithMultipleIds() {
    91  	db, err := openTestDB()
    92  	s.Require().NoError(err)
    93  	p := newSQLitePersistence(db)
    94  
    95  	chat := CreatePublicChat("test-chat", &testTimeSource{})
    96  	message := common.NewMessage()
    97  	message.Text = "sample text"
    98  	chat.LastMessage = message
    99  	err = p.SaveChat(*chat)
   100  	s.Require().NoError(err)
   101  
   102  	notifications := s.createNotifications(p, []*ActivityCenterNotification{
   103  		{Type: ActivityCenterNotificationTypeMention},
   104  		{Type: ActivityCenterNotificationTypeNewOneToOne},
   105  		{Type: ActivityCenterNotificationTypeNewOneToOne},
   106  	})
   107  
   108  	var count uint64
   109  	count, _ = p.ActivityCenterNotificationsCount([]ActivityCenterType{}, ActivityCenterQueryParamsReadUnread, false)
   110  	s.Require().Equal(uint64(3), count)
   111  
   112  	_, err = p.MarkActivityCenterNotificationsDeleted([]types.HexBytes{notifications[1].ID, notifications[2].ID}, currentMilliseconds())
   113  	s.Require().NoError(err)
   114  
   115  	count, _ = p.ActivityCenterNotificationsCount([]ActivityCenterType{}, ActivityCenterQueryParamsReadUnread, false)
   116  	s.Require().Equal(uint64(1), count)
   117  }
   118  
   119  func (s *ActivityCenterPersistenceTestSuite) Test_DeleteActivityCenterNotificationsForMessage() {
   120  	db, err := openTestDB()
   121  	s.Require().NoError(err)
   122  	p := newSQLitePersistence(db)
   123  
   124  	chat := CreatePublicChat("test-chat", &testTimeSource{})
   125  	err = p.SaveChat(*chat)
   126  	s.Require().NoError(err)
   127  
   128  	chat2 := CreatePublicChat("test-chat", &testTimeSource{})
   129  	err = p.SaveChat(*chat2)
   130  	s.Require().NoError(err)
   131  
   132  	messages := []*common.Message{
   133  		{
   134  			ID:          "0x1",
   135  			ChatMessage: &protobuf.ChatMessage{},
   136  			LocalChatID: chat.ID,
   137  		},
   138  		{
   139  			ID:          "0x2",
   140  			ChatMessage: &protobuf.ChatMessage{},
   141  			LocalChatID: chat.ID,
   142  		},
   143  		{
   144  			ChatMessage: &protobuf.ChatMessage{},
   145  			ID:          "0x3",
   146  		},
   147  	}
   148  	err = p.SaveMessages(messages)
   149  	s.Require().NoError(err)
   150  
   151  	chat.LastMessage = messages[1]
   152  	err = p.SaveChat(*chat)
   153  	s.Require().NoError(err)
   154  
   155  	chatMessages, _, err := p.MessageByChatID(chat.ID, "", 2)
   156  	s.Require().NoError(err)
   157  	s.Require().Len(chatMessages, 2)
   158  
   159  	nID1 := types.HexBytes("1")
   160  	nID2 := types.HexBytes("2")
   161  	nID3 := types.HexBytes("3")
   162  	nID4 := types.HexBytes("4")
   163  
   164  	s.createNotifications(p, []*ActivityCenterNotification{
   165  		{
   166  			ID:      nID1,
   167  			ChatID:  chat.ID,
   168  			Type:    ActivityCenterNotificationTypeMention,
   169  			Message: messages[0],
   170  		},
   171  		{
   172  			ID:      nID2,
   173  			ChatID:  chat.ID,
   174  			Type:    ActivityCenterNotificationTypeMention,
   175  			Message: messages[1],
   176  		},
   177  		{
   178  			ID:     nID3,
   179  			ChatID: chat.ID,
   180  			Type:   ActivityCenterNotificationTypeMention,
   181  		},
   182  		{
   183  			ID:   nID4,
   184  			Type: ActivityCenterNotificationTypeMention,
   185  		},
   186  	})
   187  
   188  	// Test: soft delete only the notifications that have Message.ID == messages[0].ID.
   189  	_, err = p.DeleteActivityCenterNotificationForMessage(chat.ID, messages[0].ID, currentMilliseconds())
   190  	s.Require().NoError(err)
   191  
   192  	notif, err := p.GetActivityCenterNotificationByID(nID1)
   193  	s.Require().NoError(err)
   194  	s.Require().NotNil(notif)
   195  	s.Require().True(notif.Deleted)
   196  	s.Require().True(notif.Dismissed)
   197  	s.Require().True(notif.Read)
   198  
   199  	// Other notifications are not affected.
   200  	for _, id := range []types.HexBytes{nID2, nID3, nID4} {
   201  		notif, err = p.GetActivityCenterNotificationByID(id)
   202  		s.Require().NoError(err)
   203  		s.Require().NotNil(notif)
   204  		s.Require().False(notif.Deleted, notif.ID)
   205  		s.Require().False(notif.Dismissed, notif.ID)
   206  		s.Require().False(notif.Read, notif.ID)
   207  	}
   208  
   209  	// Test: soft delete the notifications that have Message.ID == messages[1].ID
   210  	_, err = p.DeleteActivityCenterNotificationForMessage(chat.ID, messages[1].ID, currentMilliseconds())
   211  	s.Require().NoError(err)
   212  
   213  	notif, err = p.GetActivityCenterNotificationByID(nID2)
   214  	s.Require().NoError(err)
   215  	s.Require().NotNil(notif)
   216  	s.Require().True(notif.Deleted, notif.ID)
   217  	s.Require().True(notif.Dismissed, notif.ID)
   218  	s.Require().True(notif.Read, notif.ID)
   219  
   220  	// Check that notifications with LastMessage.ID == messages[1].ID will remain.
   221  	notif, err = p.GetActivityCenterNotificationByID(nID3)
   222  	s.Require().NoError(err)
   223  	s.Require().NotNil(notif)
   224  	s.Require().False(notif.Deleted, notif.ID)
   225  	s.Require().False(notif.Dismissed, notif.ID)
   226  	s.Require().False(notif.Read, notif.ID)
   227  
   228  	notif, err = p.GetActivityCenterNotificationByID(nID4)
   229  	s.Require().NoError(err)
   230  	s.Require().NotNil(notif)
   231  	s.Require().False(notif.Deleted)
   232  	s.Require().False(notif.Dismissed)
   233  	s.Require().False(notif.Read)
   234  
   235  	// Test: don't do anything if passed a chat and message without notifications.
   236  	_, err = p.DeleteActivityCenterNotificationForMessage(chat2.ID, messages[2].ID, currentMilliseconds())
   237  	s.Require().NoError(err)
   238  }
   239  
   240  func (s *ActivityCenterPersistenceTestSuite) Test_DeleteActivityCenterNotificationsForMessage_LastMessage() {
   241  	// Create the temporary test-database that will be used to store chats, messages, and notifications
   242  	db, err := openTestDB()
   243  	s.Require().NoError(err)
   244  	p := newSQLitePersistence(db)
   245  
   246  	// Create and save the public chat that will be used to group our test messages
   247  	chat := CreatePublicChat("test-chat", &testTimeSource{})
   248  	err = p.SaveChat(*chat)
   249  	s.Require().NoError(err)
   250  
   251  	// Define multiple test messages for our chat so we can emulate a chat with a latest message.
   252  	messages := []*common.Message{
   253  		{
   254  			ID:          "0x1",
   255  			ChatMessage: &protobuf.ChatMessage{},
   256  			LocalChatID: chat.ID,
   257  		},
   258  		{
   259  			ID:          "0x2",
   260  			ChatMessage: &protobuf.ChatMessage{},
   261  			LocalChatID: chat.ID,
   262  		},
   263  	}
   264  	err = p.SaveMessages(messages)
   265  	s.Require().NoError(err)
   266  
   267  	chat.LastMessage = messages[1]
   268  	err = p.SaveChat(*chat)
   269  	s.Require().NoError(err)
   270  
   271  	chatMessages, _, err := p.MessageByChatID(chat.ID, "", 2)
   272  	s.Require().NoError(err)
   273  	s.Require().Len(chatMessages, 2)
   274  
   275  	// Define multiple notifications of different types to emulate the removal of notifications when deleting a message
   276  	nID1 := types.HexBytes("1")
   277  	nID2 := types.HexBytes("2")
   278  	nID3 := types.HexBytes("3")
   279  	nID4 := types.HexBytes("4")
   280  
   281  	s.createNotifications(p, []*ActivityCenterNotification{
   282  		{
   283  			ID:          nID1,
   284  			ChatID:      chat.ID,
   285  			Type:        ActivityCenterNotificationTypeMention,
   286  			Message:     messages[0],
   287  			LastMessage: messages[1],
   288  		},
   289  		{
   290  			ID:      nID2,
   291  			ChatID:  chat.ID,
   292  			Type:    ActivityCenterNotificationTypeMention,
   293  			Message: messages[1],
   294  		},
   295  		{
   296  			ID:          nID3,
   297  			ChatID:      chat.ID,
   298  			Type:        ActivityCenterNotificationTypeNewOneToOne,
   299  			LastMessage: messages[1],
   300  		},
   301  		{
   302  			ID:          nID4,
   303  			ChatID:      chat.ID,
   304  			Type:        ActivityCenterNotificationTypeNewPrivateGroupChat,
   305  			LastMessage: messages[1],
   306  		},
   307  	})
   308  
   309  	// Action: soft delete notifications related to a chat and message
   310  	_, err = p.DeleteActivityCenterNotificationForMessage(chat.ID, messages[1].ID, currentMilliseconds())
   311  	s.Require().NoError(err)
   312  
   313  	// Test: check that notifications unrelated to the message are not affected.
   314  	notif, err := p.GetActivityCenterNotificationByID(nID1)
   315  	s.Require().NoError(err)
   316  	s.Require().NotNil(notif)
   317  	s.Require().False(notif.Deleted, notif.ID)
   318  	s.Require().False(notif.Dismissed, notif.ID)
   319  	s.Require().False(notif.Read, notif.ID)
   320  
   321  	// Test: check notifications directly related to the message are soft deleted
   322  	notif, err = p.GetActivityCenterNotificationByID(nID2)
   323  	s.Require().NoError(err)
   324  	s.Require().NotNil(notif)
   325  	s.Require().True(notif.Deleted)
   326  	s.Require().True(notif.Dismissed)
   327  	s.Require().True(notif.Read)
   328  
   329  	// Test: check NewOneToOne or NewPrivateGroupChat notifications that are indirectly related to the message are soft deleted
   330  	for _, id := range []types.HexBytes{nID3, nID4} {
   331  		notif, err = p.GetActivityCenterNotificationByID(id)
   332  		s.Require().NoError(err)
   333  		s.Require().NotNil(notif)
   334  		s.Require().False(notif.Deleted)
   335  		s.Require().False(notif.Dismissed)
   336  		s.Require().False(notif.Read)
   337  	}
   338  }
   339  
   340  func (s *ActivityCenterPersistenceTestSuite) Test_AcceptActivityCenterNotificationsForInvitesFromUser() {
   341  	db, err := openTestDB()
   342  	s.Require().NoError(err)
   343  	p := newSQLitePersistence(db)
   344  
   345  	nID1 := types.HexBytes("1")
   346  	nID2 := types.HexBytes("2")
   347  	nID3 := types.HexBytes("3")
   348  	nID4 := types.HexBytes("4")
   349  
   350  	userPublicKey := "zQ3sh"
   351  
   352  	notifications := []*ActivityCenterNotification{
   353  		{
   354  			ID:        nID1,
   355  			Type:      ActivityCenterNotificationTypeNewPrivateGroupChat,
   356  			Timestamp: 1,
   357  		},
   358  		{
   359  			ID:        nID2,
   360  			Type:      ActivityCenterNotificationTypeNewPrivateGroupChat,
   361  			Timestamp: 1,
   362  			Author:    userPublicKey,
   363  		},
   364  		{
   365  			ID:        nID3,
   366  			Type:      ActivityCenterNotificationTypeMention,
   367  			Timestamp: 1,
   368  			Author:    userPublicKey,
   369  		},
   370  		{
   371  			ID:        nID4,
   372  			Timestamp: 1,
   373  			Type:      ActivityCenterNotificationTypeMention,
   374  		},
   375  	}
   376  
   377  	var notif *ActivityCenterNotification
   378  	for _, notif = range notifications {
   379  		_, err = p.SaveActivityCenterNotification(notif, true)
   380  		s.Require().NoError(err, notif.ID)
   381  	}
   382  
   383  	// Only notifications of type new private group chat and with Author equal to
   384  	// userPublicKey should be marked as accepted & read.
   385  	_, err = p.GetActivityCenterNotificationByID(nID2)
   386  	s.Require().NoError(err)
   387  	s.Require().False(notifications[0].Accepted)
   388  	s.Require().False(notifications[0].Read)
   389  
   390  	notifications, err = p.AcceptActivityCenterNotificationsForInvitesFromUser(userPublicKey, 1)
   391  	s.Require().NoError(err)
   392  	s.Require().Len(notifications, 1)
   393  	s.Require().Equal(nID2, notifications[0].ID)
   394  
   395  	notif, err = p.GetActivityCenterNotificationByID(nID2)
   396  	s.Require().NoError(err)
   397  	s.Require().True(notif.Accepted)
   398  	s.Require().True(notif.Read)
   399  
   400  	// Deleted notifications are ignored.
   401  	notif = &ActivityCenterNotification{
   402  		ID:        types.HexBytes("99"),
   403  		Type:      ActivityCenterNotificationTypeNewPrivateGroupChat,
   404  		Timestamp: 1,
   405  		Author:    userPublicKey,
   406  		Deleted:   true,
   407  	}
   408  	_, err = p.SaveActivityCenterNotification(notif, true)
   409  	s.Require().NoError(err)
   410  	_, err = p.AcceptActivityCenterNotificationsForInvitesFromUser(userPublicKey, currentMilliseconds())
   411  	s.Require().NoError(err)
   412  	notif, err = p.GetActivityCenterNotificationByID(notif.ID)
   413  	s.Require().NoError(err)
   414  	s.Require().False(notif.Accepted)
   415  	s.Require().False(notif.Read)
   416  	s.Require().True(notif.Deleted)
   417  
   418  	// Dismissed notifications are ignored.
   419  	notif = &ActivityCenterNotification{
   420  		ID:        types.HexBytes("100"),
   421  		Type:      ActivityCenterNotificationTypeNewPrivateGroupChat,
   422  		Timestamp: 1,
   423  		Author:    userPublicKey,
   424  		Dismissed: true,
   425  	}
   426  	_, err = p.SaveActivityCenterNotification(notif, true)
   427  	s.Require().NoError(err)
   428  	_, err = p.AcceptActivityCenterNotificationsForInvitesFromUser(userPublicKey, currentMilliseconds())
   429  	s.Require().NoError(err)
   430  	notif, err = p.GetActivityCenterNotificationByID(notif.ID)
   431  	s.Require().NoError(err)
   432  	s.Require().False(notif.Accepted)
   433  	s.Require().False(notif.Read)
   434  	s.Require().True(notif.Dismissed)
   435  }
   436  
   437  func (s *ActivityCenterPersistenceTestSuite) Test_GetToProcessActivityCenterNotificationIds() {
   438  	db, err := openTestDB()
   439  	s.Require().NoError(err)
   440  	p := newSQLitePersistence(db)
   441  
   442  	notifications := s.createNotifications(p, []*ActivityCenterNotification{
   443  		{
   444  			Type:    ActivityCenterNotificationTypeNewPrivateGroupChat,
   445  			Deleted: true,
   446  		},
   447  		{
   448  			Type:      ActivityCenterNotificationTypeNewPrivateGroupChat,
   449  			Dismissed: true,
   450  		},
   451  		{
   452  			Type:     ActivityCenterNotificationTypeMention,
   453  			Accepted: true,
   454  		},
   455  		{
   456  			Type: ActivityCenterNotificationTypeMention,
   457  		},
   458  	})
   459  
   460  	ids, err := p.GetToProcessActivityCenterNotificationIds()
   461  	s.Require().NoError(err)
   462  	s.Require().Len(ids, 1)
   463  	s.Require().Equal(notifications[3].ID, types.HexBytes(ids[0]))
   464  }
   465  
   466  func (s *ActivityCenterPersistenceTestSuite) Test_HasPendingNotificationsForChat() {
   467  	db, err := openTestDB()
   468  	s.Require().NoError(err)
   469  	p := newSQLitePersistence(db)
   470  
   471  	chat := CreatePublicChat("test-chat", &testTimeSource{})
   472  	err = p.SaveChat(*chat)
   473  	s.Require().NoError(err)
   474  
   475  	// Test: there are no notifications.
   476  	result, err := p.HasPendingNotificationsForChat(chat.ID)
   477  	s.Require().NoError(err)
   478  	s.Require().False(result)
   479  
   480  	// Test: there are only deleted, dismissed or accepted notifications,
   481  	// therefore, no pending notifications.
   482  	s.createNotifications(p, []*ActivityCenterNotification{
   483  		{
   484  			ChatID:  chat.ID,
   485  			Type:    ActivityCenterNotificationTypeNewPrivateGroupChat,
   486  			Deleted: true,
   487  		},
   488  		{
   489  			ChatID:    chat.ID,
   490  			Type:      ActivityCenterNotificationTypeNewPrivateGroupChat,
   491  			Dismissed: true,
   492  		},
   493  		{
   494  			ChatID:   chat.ID,
   495  			Type:     ActivityCenterNotificationTypeMention,
   496  			Accepted: true,
   497  		},
   498  	})
   499  
   500  	result, err = p.HasPendingNotificationsForChat(chat.ID)
   501  	s.Require().NoError(err)
   502  	s.Require().False(result)
   503  
   504  	// Test: there's one pending notification.
   505  	notif := &ActivityCenterNotification{
   506  		ID:        types.HexBytes("99"),
   507  		ChatID:    chat.ID,
   508  		Type:      ActivityCenterNotificationTypeCommunityRequest,
   509  		Timestamp: 1,
   510  	}
   511  	_, err = p.SaveActivityCenterNotification(notif, true)
   512  	s.Require().NoError(err)
   513  
   514  	result, err = p.HasPendingNotificationsForChat(chat.ID)
   515  	s.Require().NoError(err)
   516  	s.Require().True(result)
   517  }
   518  
   519  func (s *ActivityCenterPersistenceTestSuite) Test_DismissAllActivityCenterNotificationsFromUser() {
   520  	db, err := openTestDB()
   521  	s.Require().NoError(err)
   522  	p := newSQLitePersistence(db)
   523  
   524  	publicKey := "0x04"
   525  
   526  	notifications := s.createNotifications(p, []*ActivityCenterNotification{
   527  		{
   528  			Type:    ActivityCenterNotificationTypeNewPrivateGroupChat,
   529  			Author:  publicKey,
   530  			Deleted: true,
   531  		},
   532  		{
   533  			Type:      ActivityCenterNotificationTypeNewPrivateGroupChat,
   534  			Author:    publicKey,
   535  			Dismissed: true,
   536  		},
   537  		{
   538  			Type:     ActivityCenterNotificationTypeMention,
   539  			Author:   publicKey,
   540  			Accepted: true,
   541  		},
   542  		{
   543  			Type:   ActivityCenterNotificationTypeMention,
   544  			Author: "0x09",
   545  		},
   546  		{
   547  			Type:   ActivityCenterNotificationTypeMention,
   548  			Author: publicKey,
   549  		},
   550  	})
   551  
   552  	_, err = p.DismissAllActivityCenterNotificationsFromUser(publicKey, 1)
   553  	s.Require().NoError(err)
   554  
   555  	// Ignores already soft deleted.
   556  	notif, err := p.GetActivityCenterNotificationByID(notifications[0].ID)
   557  	s.Require().NoError(err)
   558  	s.Require().False(notif.Accepted)
   559  	s.Require().False(notif.Read)
   560  	s.Require().False(notif.Dismissed)
   561  	s.Require().True(notif.Deleted)
   562  
   563  	// Ignores already dismissed.
   564  	notif, err = p.GetActivityCenterNotificationByID(notifications[1].ID)
   565  	s.Require().NoError(err)
   566  	s.Require().False(notif.Accepted)
   567  	s.Require().False(notif.Read)
   568  	s.Require().True(notif.Dismissed)
   569  	s.Require().False(notif.Deleted)
   570  
   571  	// Ignores already accepted.
   572  	notif, err = p.GetActivityCenterNotificationByID(notifications[2].ID)
   573  	s.Require().NoError(err)
   574  	s.Require().True(notif.Accepted)
   575  	s.Require().False(notif.Read)
   576  	s.Require().False(notif.Dismissed)
   577  	s.Require().False(notif.Deleted)
   578  
   579  	// Ignores notification from different author.
   580  	notif, err = p.GetActivityCenterNotificationByID(notifications[3].ID)
   581  	s.Require().NoError(err)
   582  	s.Require().False(notif.Accepted)
   583  	s.Require().False(notif.Read)
   584  	s.Require().False(notif.Dismissed)
   585  	s.Require().False(notif.Deleted)
   586  
   587  	// Finally, dismiss and mark as read this one notification.
   588  	notif, err = p.GetActivityCenterNotificationByID(notifications[4].ID)
   589  	s.Require().NoError(err)
   590  	s.Require().False(notif.Accepted)
   591  	s.Require().True(notif.Read)
   592  	s.Require().True(notif.Dismissed)
   593  	s.Require().False(notif.Deleted)
   594  }
   595  
   596  func (s *ActivityCenterPersistenceTestSuite) Test_DismissAllActivityCenterNotificationsFromChatID() {
   597  	db, err := openTestDB()
   598  	s.Require().NoError(err)
   599  	p := newSQLitePersistence(db)
   600  
   601  	chatID := "0x99"
   602  
   603  	notifications := s.createNotifications(p, []*ActivityCenterNotification{
   604  		{
   605  			ChatID:  chatID,
   606  			Type:    ActivityCenterNotificationTypeNewPrivateGroupChat,
   607  			Deleted: true,
   608  		},
   609  		{
   610  			ChatID:    chatID,
   611  			Type:      ActivityCenterNotificationTypeNewPrivateGroupChat,
   612  			Dismissed: true,
   613  		},
   614  		{
   615  			ChatID:   chatID,
   616  			Type:     ActivityCenterNotificationTypeMention,
   617  			Accepted: true,
   618  		},
   619  		{
   620  			Type: ActivityCenterNotificationTypeMention,
   621  		},
   622  		{
   623  			ChatID: chatID,
   624  			Type:   ActivityCenterNotificationTypeContactRequest,
   625  		},
   626  		{
   627  			ChatID: chatID,
   628  			Type:   ActivityCenterNotificationTypeMention,
   629  		},
   630  	})
   631  
   632  	_, err = p.DismissAllActivityCenterNotificationsFromChatID(chatID, currentMilliseconds())
   633  	s.Require().NoError(err)
   634  
   635  	// Ignores already soft deleted.
   636  	notif, err := p.GetActivityCenterNotificationByID(notifications[0].ID)
   637  	s.Require().NoError(err)
   638  	s.Require().False(notif.Accepted)
   639  	s.Require().False(notif.Read)
   640  	s.Require().False(notif.Dismissed)
   641  	s.Require().True(notif.Deleted)
   642  
   643  	// Do not ignore already dismissed, because notifications can become
   644  	// read/unread AND dismissed, and the method should still update the Read
   645  	// column.
   646  	notif, err = p.GetActivityCenterNotificationByID(notifications[1].ID)
   647  	s.Require().NoError(err)
   648  	s.Require().False(notif.Accepted)
   649  	s.Require().True(notif.Read)
   650  	s.Require().True(notif.Dismissed)
   651  	s.Require().False(notif.Deleted)
   652  
   653  	// Ignores already accepted.
   654  	notif, err = p.GetActivityCenterNotificationByID(notifications[2].ID)
   655  	s.Require().NoError(err)
   656  	s.Require().True(notif.Accepted)
   657  	s.Require().False(notif.Read)
   658  	s.Require().False(notif.Dismissed)
   659  	s.Require().False(notif.Deleted)
   660  
   661  	// Ignores notification from different chat.
   662  	notif, err = p.GetActivityCenterNotificationByID(notifications[3].ID)
   663  	s.Require().NoError(err)
   664  	s.Require().False(notif.Accepted)
   665  	s.Require().False(notif.Read)
   666  	s.Require().False(notif.Dismissed)
   667  	s.Require().False(notif.Deleted)
   668  
   669  	// Ignores contact request notifications.
   670  	notif, err = p.GetActivityCenterNotificationByID(notifications[4].ID)
   671  	s.Require().NoError(err)
   672  	s.Require().False(notif.Accepted)
   673  	s.Require().False(notif.Read)
   674  	s.Require().False(notif.Dismissed)
   675  	s.Require().False(notif.Deleted)
   676  
   677  	// Finally, dismiss and mark as read this one notification.
   678  	notif, err = p.GetActivityCenterNotificationByID(notifications[5].ID)
   679  	s.Require().NoError(err)
   680  	s.Require().False(notif.Accepted)
   681  	s.Require().True(notif.Read)
   682  	s.Require().True(notif.Dismissed)
   683  	s.Require().False(notif.Deleted)
   684  }
   685  
   686  func (s *ActivityCenterPersistenceTestSuite) Test_ActiveContactRequestNotification() {
   687  	db, err := openTestDB()
   688  	s.Require().NoError(err)
   689  	p := newSQLitePersistence(db)
   690  
   691  	chat := CreatePublicChat("test-chat", &testTimeSource{})
   692  	err = p.SaveChat(*chat)
   693  	s.Require().NoError(err)
   694  
   695  	contactID := "0x99"
   696  
   697  	// Test: ignores deleted/dismissed/accepted notifications, as well as
   698  	// notifications not associated to any chat.
   699  	s.createNotifications(p, []*ActivityCenterNotification{
   700  		{
   701  			ChatID:  chat.ID,
   702  			Author:  contactID,
   703  			Type:    ActivityCenterNotificationTypeContactRequest,
   704  			Deleted: true,
   705  		},
   706  		{
   707  			ChatID:    chat.ID,
   708  			Author:    contactID,
   709  			Type:      ActivityCenterNotificationTypeContactRequest,
   710  			Dismissed: true,
   711  		},
   712  		{
   713  			ChatID:   chat.ID,
   714  			Author:   contactID,
   715  			Type:     ActivityCenterNotificationTypeContactRequest,
   716  			Accepted: true,
   717  		},
   718  	})
   719  
   720  	notif, err := p.ActiveContactRequestNotification(contactID)
   721  	s.Require().NoError(err)
   722  	s.Require().Nil(notif)
   723  
   724  	// Test: Ignores notifications that are not contact requests.
   725  	s.createNotifications(p, []*ActivityCenterNotification{
   726  		{ChatID: chat.ID, Author: contactID, Type: ActivityCenterNotificationTypeCommunityInvitation},
   727  		{ChatID: chat.ID, Author: contactID, Type: ActivityCenterNotificationTypeCommunityKicked},
   728  		{ChatID: chat.ID, Author: contactID, Type: ActivityCenterNotificationTypeCommunityMembershipRequest},
   729  		{ChatID: chat.ID, Author: contactID, Type: ActivityCenterNotificationTypeCommunityRequest},
   730  		{ChatID: chat.ID, Author: contactID, Type: ActivityCenterNotificationTypeContactVerification},
   731  		{ChatID: chat.ID, Author: contactID, Type: ActivityCenterNotificationTypeMention},
   732  		{ChatID: chat.ID, Author: contactID, Type: ActivityCenterNotificationTypeNewOneToOne},
   733  		{ChatID: chat.ID, Author: contactID, Type: ActivityCenterNotificationTypeNewPrivateGroupChat},
   734  		{ChatID: chat.ID, Author: contactID, Type: ActivityCenterNotificationTypeReply},
   735  	})
   736  
   737  	notif, err = p.ActiveContactRequestNotification(contactID)
   738  	s.Require().NoError(err)
   739  	s.Require().Nil(notif)
   740  
   741  	// Test: Returns one, and only one contact request notification for the
   742  	// contact under test.
   743  	s.createNotifications(p, []*ActivityCenterNotification{
   744  		{ChatID: chat.ID, Author: contactID, Type: ActivityCenterNotificationTypeContactRequest},
   745  	})
   746  
   747  	notif, err = p.ActiveContactRequestNotification(contactID)
   748  	s.Require().NoError(err)
   749  	s.Require().NotNil(notif)
   750  	s.Require().Equal(ActivityCenterNotificationTypeContactRequest, notif.Type)
   751  
   752  	// Test: In case there's more than one notification, return the most recent
   753  	// one according to the notification's timestamp.
   754  	expectedID := types.HexBytes("667")
   755  
   756  	t1 := currentMilliseconds()
   757  	t2 := currentMilliseconds()
   758  	s.createNotifications(p, []*ActivityCenterNotification{
   759  		{
   760  			ID:        expectedID,
   761  			Timestamp: t2 + 1,
   762  			ChatID:    chat.ID,
   763  			Author:    contactID,
   764  			Type:      ActivityCenterNotificationTypeContactRequest,
   765  		},
   766  		{
   767  			ID:        types.HexBytes("666"),
   768  			Timestamp: t1,
   769  			ChatID:    chat.ID,
   770  			Author:    contactID,
   771  			Type:      ActivityCenterNotificationTypeContactRequest,
   772  		},
   773  	})
   774  
   775  	notif, err = p.ActiveContactRequestNotification(contactID)
   776  	s.Require().NoError(err)
   777  	s.Require().Equal(expectedID, notif.ID)
   778  }
   779  
   780  func (s *ActivityCenterPersistenceTestSuite) Test_UnreadActivityCenterNotificationsCount() {
   781  	db, err := openTestDB()
   782  	s.Require().NoError(err)
   783  	p := newSQLitePersistence(db)
   784  
   785  	s.createNotifications(p, []*ActivityCenterNotification{
   786  		{Type: ActivityCenterNotificationTypeMention, Read: true},
   787  		{Type: ActivityCenterNotificationTypeNewOneToOne, Deleted: true},
   788  		{Type: ActivityCenterNotificationTypeMention, Dismissed: true},
   789  		{Type: ActivityCenterNotificationTypeCommunityRequest, Accepted: true},
   790  		{Type: ActivityCenterNotificationTypeContactRequest},
   791  	})
   792  
   793  	// Test: Ignore soft deleted and accepted.
   794  	count, err := p.ActivityCenterNotificationsCount([]ActivityCenterType{}, ActivityCenterQueryParamsReadUnread, true)
   795  	s.Require().NoError(err)
   796  	s.Require().Equal(uint64(3), count)
   797  }
   798  
   799  func (s *ActivityCenterPersistenceTestSuite) Test_UnreadAndAcceptedActivityCenterNotificationsCount() {
   800  	db, err := openTestDB()
   801  	s.Require().NoError(err)
   802  	p := newSQLitePersistence(db)
   803  
   804  	s.createNotifications(p, []*ActivityCenterNotification{
   805  		{Type: ActivityCenterNotificationTypeMention, Read: true},
   806  		{Type: ActivityCenterNotificationTypeNewOneToOne, Deleted: true},
   807  		{Type: ActivityCenterNotificationTypeMention, Dismissed: true},
   808  		{Type: ActivityCenterNotificationTypeCommunityRequest, Accepted: true},
   809  		{Type: ActivityCenterNotificationTypeContactRequest},
   810  	})
   811  
   812  	// Test: counts everything, except soft deleted notifications.
   813  	count, err := p.ActivityCenterNotificationsCount([]ActivityCenterType{}, ActivityCenterQueryParamsReadUnread, true)
   814  	s.Require().NoError(err)
   815  	s.Require().Equal(uint64(3), count)
   816  
   817  	// Test: counts everything, except soft deleted ones and limit by type.
   818  	count, err = p.ActivityCenterNotificationsCount([]ActivityCenterType{
   819  		ActivityCenterNotificationTypeContactRequest,
   820  	}, ActivityCenterQueryParamsReadUnread, true)
   821  	s.Require().NoError(err)
   822  	s.Require().Equal(uint64(1), count)
   823  }
   824  
   825  func (s *ActivityCenterPersistenceTestSuite) Test_ActivityCenterPersistence() {
   826  	nID1 := types.HexBytes([]byte("1"))
   827  	nID2 := types.HexBytes([]byte("2"))
   828  	nID3 := types.HexBytes([]byte("3"))
   829  	nID4 := types.HexBytes([]byte("4"))
   830  
   831  	db, err := openTestDB()
   832  	s.Require().NoError(err)
   833  	p := newSQLitePersistence(db)
   834  
   835  	chat := CreatePublicChat("test-chat", &testTimeSource{})
   836  	message := common.NewMessage()
   837  	message.Text = "sample text"
   838  	chat.LastMessage = message
   839  	err = p.SaveChat(*chat)
   840  	s.Require().NoError(err)
   841  
   842  	notification := &ActivityCenterNotification{
   843  		ID:        nID1,
   844  		Type:      ActivityCenterNotificationTypeNewOneToOne,
   845  		ChatID:    chat.ID,
   846  		Timestamp: 1,
   847  	}
   848  	_, err = p.SaveActivityCenterNotification(notification, true)
   849  	s.Require().NoError(err)
   850  
   851  	cursor, notifications, err := p.ActivityCenterNotifications("", 2, []ActivityCenterType{}, ActivityCenterQueryParamsReadAll, false)
   852  	s.Require().NoError(err)
   853  	s.Require().Empty(cursor)
   854  	s.Require().Len(notifications, 1)
   855  	s.Require().Equal(chat.ID, notifications[0].ChatID)
   856  	s.Require().Equal(message, notifications[0].LastMessage)
   857  
   858  	// Add another notification
   859  
   860  	notification = &ActivityCenterNotification{
   861  		ID:        nID2,
   862  		Type:      ActivityCenterNotificationTypeNewOneToOne,
   863  		Timestamp: 2,
   864  	}
   865  	_, err = p.SaveActivityCenterNotification(notification, true)
   866  	s.Require().NoError(err)
   867  
   868  	cursor, notifications, err = p.ActivityCenterNotifications("", 1, []ActivityCenterType{}, ActivityCenterQueryParamsReadAll, false)
   869  	s.Require().NoError(err)
   870  	s.Require().Len(notifications, 1)
   871  	s.Require().NotEmpty(cursor)
   872  	s.Require().Equal(nID2, notifications[0].ID)
   873  
   874  	// fetch next pagination
   875  
   876  	cursor, notifications, err = p.ActivityCenterNotifications(cursor, 1, []ActivityCenterType{}, ActivityCenterQueryParamsReadAll, false)
   877  	s.Require().NoError(err)
   878  	s.Require().Len(notifications, 1)
   879  	s.Require().Empty(cursor)
   880  	s.Require().False(notifications[0].Read)
   881  	s.Require().Equal(nID1, notifications[0].ID)
   882  
   883  	// Check count
   884  	count, err := p.ActivityCenterNotificationsCount([]ActivityCenterType{}, ActivityCenterQueryParamsReadUnread, false)
   885  	s.Require().NoError(err)
   886  	s.Require().Equal(uint64(2), count)
   887  
   888  	var updatedAt uint64 = 1
   889  	// Mark first one as read
   890  	s.Require().NoError(p.MarkActivityCenterNotificationsRead([]types.HexBytes{nID1}, updatedAt))
   891  	count, err = p.ActivityCenterNotificationsCount([]ActivityCenterType{}, ActivityCenterQueryParamsReadUnread, false)
   892  	s.Require().NoError(err)
   893  	s.Require().Equal(uint64(1), count)
   894  
   895  	// Mark first one as unread
   896  	updatedAt++
   897  	_, err = p.MarkActivityCenterNotificationsUnread([]types.HexBytes{nID1}, updatedAt)
   898  	s.Require().NoError(err)
   899  	count, err = p.ActivityCenterNotificationsCount([]ActivityCenterType{}, ActivityCenterQueryParamsReadUnread, false)
   900  	s.Require().NoError(err)
   901  	s.Require().Equal(uint64(2), count)
   902  
   903  	// Mark all read
   904  	updatedAt++
   905  	s.Require().NoError(p.MarkAllActivityCenterNotificationsRead(updatedAt))
   906  	_, notifications, err = p.ActivityCenterNotifications(cursor, 2, []ActivityCenterType{}, ActivityCenterQueryParamsReadAll, false)
   907  	s.Require().NoError(err)
   908  	s.Require().Len(notifications, 2)
   909  	s.Require().Empty(cursor)
   910  	s.Require().True(notifications[0].Read)
   911  	s.Require().True(notifications[1].Read)
   912  
   913  	// Check count
   914  	count, err = p.ActivityCenterNotificationsCount([]ActivityCenterType{}, ActivityCenterQueryParamsReadUnread, false)
   915  	s.Require().NoError(err)
   916  	s.Require().Equal(uint64(0), count)
   917  
   918  	// Mark first one as accepted
   919  	updatedAt++
   920  	notifications, err = p.AcceptActivityCenterNotifications([]types.HexBytes{nID1}, updatedAt)
   921  	s.Require().NoError(err)
   922  	s.Require().Len(notifications, 1)
   923  	_, notifications, err = p.ActivityCenterNotifications("", 2, []ActivityCenterType{}, ActivityCenterQueryParamsReadAll, false)
   924  	s.Require().NoError(err)
   925  	// It should not be returned anymore
   926  	s.Require().Len(notifications, 1)
   927  
   928  	// Mark last one as dismissed
   929  	updatedAt++
   930  	s.Require().NoError(p.DismissActivityCenterNotifications([]types.HexBytes{nID2}, updatedAt))
   931  	_, notifications, err = p.ActivityCenterNotifications("", 2, []ActivityCenterType{}, ActivityCenterQueryParamsReadAll, false)
   932  	s.Require().NoError(err)
   933  
   934  	s.Require().Len(notifications, 1)
   935  	s.Require().True(notifications[0].Dismissed)
   936  
   937  	// Insert new notification
   938  	notification = &ActivityCenterNotification{
   939  		ID:        nID3,
   940  		Type:      ActivityCenterNotificationTypeNewOneToOne,
   941  		Timestamp: 3,
   942  	}
   943  	_, err = p.SaveActivityCenterNotification(notification, true)
   944  	s.Require().NoError(err)
   945  
   946  	// Mark all as accepted
   947  	updatedAt++
   948  	notifications, err = p.AcceptAllActivityCenterNotifications(updatedAt)
   949  	s.Require().NoError(err)
   950  	s.Require().Len(notifications, 2)
   951  
   952  	_, notifications, err = p.ActivityCenterNotifications("", 2, []ActivityCenterType{}, ActivityCenterQueryParamsReadAll, false)
   953  	s.Require().NoError(err)
   954  
   955  	s.Require().Len(notifications, 1)
   956  
   957  	// Insert new notification
   958  	notification = &ActivityCenterNotification{
   959  		ID:        nID4,
   960  		Type:      ActivityCenterNotificationTypeNewOneToOne,
   961  		Timestamp: 4,
   962  	}
   963  	_, err = p.SaveActivityCenterNotification(notification, true)
   964  	s.Require().NoError(err)
   965  
   966  	// Mark all as dismissed
   967  	updatedAt++
   968  	s.Require().NoError(p.DismissAllActivityCenterNotifications(updatedAt))
   969  	_, notifications, err = p.ActivityCenterNotifications("", 2, []ActivityCenterType{}, ActivityCenterQueryParamsReadAll, false)
   970  	s.Require().NoError(err)
   971  
   972  	s.Require().Len(notifications, 2)
   973  	s.Require().True(notifications[0].Dismissed)
   974  	s.Require().True(notifications[1].Dismissed)
   975  }
   976  
   977  func (s *ActivityCenterPersistenceTestSuite) Test_ActivityCenterReadUnreadPagination() {
   978  	db, err := openTestDB()
   979  	s.Require().NoError(err)
   980  	p := newSQLitePersistence(db)
   981  
   982  	initialOrFinalCursor := ""
   983  
   984  	chat := CreatePublicChat("test-chat", &testTimeSource{})
   985  	message := common.NewMessage()
   986  	message.Text = "sample text"
   987  	chat.LastMessage = message
   988  	err = p.SaveChat(*chat)
   989  	s.Require().NoError(err)
   990  
   991  	nID1 := types.HexBytes("1")
   992  	nID2 := types.HexBytes("2")
   993  	nID3 := types.HexBytes("3")
   994  	nID4 := types.HexBytes("4")
   995  	nID5 := types.HexBytes("5")
   996  
   997  	allNotifications := []*ActivityCenterNotification{
   998  		{
   999  			ID:        nID1,
  1000  			Type:      ActivityCenterNotificationTypeNewOneToOne,
  1001  			ChatID:    chat.ID,
  1002  			Timestamp: 1,
  1003  		},
  1004  		{
  1005  			ID:        nID2,
  1006  			Type:      ActivityCenterNotificationTypeNewOneToOne,
  1007  			ChatID:    chat.ID,
  1008  			Timestamp: 1,
  1009  		},
  1010  		{
  1011  			ID:        nID3,
  1012  			Type:      ActivityCenterNotificationTypeNewOneToOne,
  1013  			ChatID:    chat.ID,
  1014  			Timestamp: 1,
  1015  		},
  1016  		{
  1017  			ID:        nID4,
  1018  			Type:      ActivityCenterNotificationTypeNewOneToOne,
  1019  			ChatID:    chat.ID,
  1020  			Timestamp: 1,
  1021  		},
  1022  		{
  1023  			ID:        nID5,
  1024  			Type:      ActivityCenterNotificationTypeNewOneToOne,
  1025  			ChatID:    chat.ID,
  1026  			Timestamp: 1,
  1027  		},
  1028  	}
  1029  
  1030  	for _, notification := range allNotifications {
  1031  		_, err = p.SaveActivityCenterNotification(notification, true)
  1032  		s.Require().NoError(err)
  1033  	}
  1034  
  1035  	// Mark the notification as read
  1036  	err = p.MarkActivityCenterNotificationsRead([]types.HexBytes{nID2}, currentMilliseconds())
  1037  	s.Require().NoError(err)
  1038  	err = p.MarkActivityCenterNotificationsRead([]types.HexBytes{nID4}, currentMilliseconds())
  1039  	s.Require().NoError(err)
  1040  
  1041  	// Fetch UNREAD notifications, first page.
  1042  	cursor, notifications, err := p.ActivityCenterNotifications(
  1043  		initialOrFinalCursor,
  1044  		1,
  1045  		[]ActivityCenterType{ActivityCenterNotificationTypeNewOneToOne},
  1046  		ActivityCenterQueryParamsReadUnread,
  1047  		false,
  1048  	)
  1049  	s.Require().NoError(err)
  1050  	s.Require().Len(notifications, 1)
  1051  	s.Require().Equal(nID5, notifications[0].ID)
  1052  	s.Require().NotEmpty(cursor)
  1053  
  1054  	// Fetch next pages.
  1055  	cursor, notifications, err = p.ActivityCenterNotifications(
  1056  		cursor,
  1057  		1,
  1058  		[]ActivityCenterType{ActivityCenterNotificationTypeNewOneToOne},
  1059  		ActivityCenterQueryParamsReadUnread,
  1060  		false,
  1061  	)
  1062  	s.Require().NoError(err)
  1063  	s.Require().Len(notifications, 1)
  1064  	s.Require().Equal(nID3, notifications[0].ID)
  1065  	s.Require().NotEmpty(cursor)
  1066  
  1067  	cursor, notifications, err = p.ActivityCenterNotifications(
  1068  		cursor,
  1069  		1,
  1070  		[]ActivityCenterType{ActivityCenterNotificationTypeNewOneToOne},
  1071  		ActivityCenterQueryParamsReadUnread,
  1072  		false,
  1073  	)
  1074  	s.Require().NoError(err)
  1075  	s.Require().Len(notifications, 1)
  1076  	s.Require().Equal(nID1, notifications[0].ID)
  1077  	s.Require().Empty(cursor)
  1078  
  1079  	// Fetch READ notifications, first page.
  1080  	cursor, notifications, err = p.ActivityCenterNotifications(
  1081  		initialOrFinalCursor,
  1082  		1,
  1083  		[]ActivityCenterType{ActivityCenterNotificationTypeNewOneToOne},
  1084  		ActivityCenterQueryParamsReadRead,
  1085  		false,
  1086  	)
  1087  	s.Require().NoError(err)
  1088  	s.Require().Len(notifications, 1)
  1089  	s.Require().Equal(nID4, notifications[0].ID)
  1090  	s.Require().NotEmpty(cursor)
  1091  
  1092  	// Fetch next page.
  1093  	cursor, notifications, err = p.ActivityCenterNotifications(
  1094  		cursor,
  1095  		1,
  1096  		[]ActivityCenterType{ActivityCenterNotificationTypeNewOneToOne},
  1097  		ActivityCenterQueryParamsReadRead,
  1098  		false,
  1099  	)
  1100  	s.Require().NoError(err)
  1101  	s.Require().Len(notifications, 1)
  1102  	s.Require().Equal(nID2, notifications[0].ID)
  1103  	s.Require().Empty(cursor)
  1104  }
  1105  
  1106  func (s *ActivityCenterPersistenceTestSuite) Test_ActivityCenterReadUnreadFilterByTypes() {
  1107  	db, err := openTestDB()
  1108  	s.Require().NoError(err)
  1109  	p := newSQLitePersistence(db)
  1110  
  1111  	chat := CreatePublicChat("test-chat", &testTimeSource{})
  1112  	message := common.NewMessage()
  1113  	message.Text = "sample text"
  1114  	chat.LastMessage = message
  1115  	err = p.SaveChat(*chat)
  1116  	s.Require().NoError(err)
  1117  
  1118  	initialCursor := ""
  1119  	limit := uint64(3)
  1120  
  1121  	nID1 := types.HexBytes("1")
  1122  	nID2 := types.HexBytes("2")
  1123  	nID3 := types.HexBytes("3")
  1124  
  1125  	allNotifications := []*ActivityCenterNotification{
  1126  		{
  1127  			ID:        nID1,
  1128  			Type:      ActivityCenterNotificationTypeMention,
  1129  			ChatID:    chat.ID,
  1130  			Timestamp: 1,
  1131  		},
  1132  		{
  1133  			ID:        nID2,
  1134  			Type:      ActivityCenterNotificationTypeNewOneToOne,
  1135  			ChatID:    chat.ID,
  1136  			Timestamp: 1,
  1137  		},
  1138  		{
  1139  			ID:        nID3,
  1140  			Type:      ActivityCenterNotificationTypeMention,
  1141  			ChatID:    chat.ID,
  1142  			Timestamp: 1,
  1143  		},
  1144  	}
  1145  
  1146  	for _, notification := range allNotifications {
  1147  		_, err = p.SaveActivityCenterNotification(notification, true)
  1148  		s.Require().NoError(err)
  1149  	}
  1150  
  1151  	// Don't filter by type if the array of types is empty.
  1152  	_, notifications, err := p.ActivityCenterNotifications(
  1153  		initialCursor,
  1154  		limit,
  1155  		[]ActivityCenterType{},
  1156  		ActivityCenterQueryParamsReadUnread,
  1157  		false,
  1158  	)
  1159  	s.Require().NoError(err)
  1160  	s.Require().Len(notifications, 3)
  1161  	s.Require().Equal(nID3, notifications[0].ID)
  1162  	s.Require().Equal(nID2, notifications[1].ID)
  1163  	s.Require().Equal(nID1, notifications[2].ID)
  1164  
  1165  	_, notifications, err = p.ActivityCenterNotifications(
  1166  		initialCursor,
  1167  		limit,
  1168  		[]ActivityCenterType{ActivityCenterNotificationTypeNewOneToOne},
  1169  		ActivityCenterQueryParamsReadUnread,
  1170  		false,
  1171  	)
  1172  	s.Require().NoError(err)
  1173  	s.Require().Len(notifications, 1)
  1174  	s.Require().Equal(nID2, notifications[0].ID)
  1175  
  1176  	_, notifications, err = p.ActivityCenterNotifications(
  1177  		initialCursor,
  1178  		limit,
  1179  		[]ActivityCenterType{ActivityCenterNotificationTypeMention},
  1180  		ActivityCenterQueryParamsReadUnread,
  1181  		false,
  1182  	)
  1183  	s.Require().NoError(err)
  1184  	s.Require().Len(notifications, 2)
  1185  	s.Require().Equal(nID3, notifications[0].ID)
  1186  	s.Require().Equal(nID1, notifications[1].ID)
  1187  
  1188  	_, notifications, err = p.ActivityCenterNotifications(
  1189  		initialCursor,
  1190  		limit,
  1191  		[]ActivityCenterType{ActivityCenterNotificationTypeMention, ActivityCenterNotificationTypeNewOneToOne},
  1192  		ActivityCenterQueryParamsReadUnread,
  1193  		false,
  1194  	)
  1195  	s.Require().NoError(err)
  1196  	s.Require().Len(notifications, 3)
  1197  	s.Require().Equal(nID3, notifications[0].ID)
  1198  	s.Require().Equal(nID2, notifications[1].ID)
  1199  	s.Require().Equal(nID1, notifications[2].ID)
  1200  
  1201  	// Mark all notifications as read.
  1202  	for _, notification := range allNotifications {
  1203  		err = p.MarkActivityCenterNotificationsRead([]types.HexBytes{notification.ID}, currentMilliseconds())
  1204  		s.Require().NoError(err)
  1205  	}
  1206  
  1207  	_, notifications, err = p.ActivityCenterNotifications(
  1208  		initialCursor,
  1209  		limit,
  1210  		[]ActivityCenterType{ActivityCenterNotificationTypeNewOneToOne},
  1211  		ActivityCenterQueryParamsReadRead,
  1212  		false,
  1213  	)
  1214  	s.Require().NoError(err)
  1215  	s.Require().Len(notifications, 1)
  1216  	s.Require().Equal(nID2, notifications[0].ID)
  1217  
  1218  	_, notifications, err = p.ActivityCenterNotifications(
  1219  		initialCursor,
  1220  		limit,
  1221  		[]ActivityCenterType{ActivityCenterNotificationTypeMention},
  1222  		ActivityCenterQueryParamsReadRead,
  1223  		false,
  1224  	)
  1225  	s.Require().NoError(err)
  1226  	s.Require().Len(notifications, 2)
  1227  	s.Require().Equal(nID3, notifications[0].ID)
  1228  	s.Require().Equal(nID1, notifications[1].ID)
  1229  }
  1230  
  1231  func (s *ActivityCenterPersistenceTestSuite) Test_ActivityCenterReadUnread() {
  1232  	nID1 := types.HexBytes("1")
  1233  	nID2 := types.HexBytes("2")
  1234  
  1235  	db, err := openTestDB()
  1236  	s.Require().NoError(err)
  1237  	p := newSQLitePersistence(db)
  1238  
  1239  	chat := CreatePublicChat("test-chat", &testTimeSource{})
  1240  	message := common.NewMessage()
  1241  	message.Text = "sample text"
  1242  	chat.LastMessage = message
  1243  	err = p.SaveChat(*chat)
  1244  	s.Require().NoError(err)
  1245  
  1246  	notification := &ActivityCenterNotification{
  1247  		ID:        nID1,
  1248  		Type:      ActivityCenterNotificationTypeNewOneToOne,
  1249  		ChatID:    chat.ID,
  1250  		Timestamp: 1,
  1251  	}
  1252  
  1253  	_, err = p.SaveActivityCenterNotification(notification, true)
  1254  	s.Require().NoError(err)
  1255  
  1256  	notification = &ActivityCenterNotification{
  1257  		ID:        nID2,
  1258  		Type:      ActivityCenterNotificationTypeNewOneToOne,
  1259  		ChatID:    chat.ID,
  1260  		Timestamp: 1,
  1261  	}
  1262  
  1263  	_, err = p.SaveActivityCenterNotification(notification, true)
  1264  	s.Require().NoError(err)
  1265  
  1266  	// Mark the notification as read
  1267  	err = p.MarkActivityCenterNotificationsRead([]types.HexBytes{nID2}, currentMilliseconds())
  1268  	s.Require().NoError(err)
  1269  
  1270  	cursor, notifications, err := p.ActivityCenterNotifications(
  1271  		"",
  1272  		2,
  1273  		[]ActivityCenterType{ActivityCenterNotificationTypeNewOneToOne},
  1274  		ActivityCenterQueryParamsReadUnread,
  1275  		false,
  1276  	)
  1277  	s.Require().NoError(err)
  1278  	s.Require().Empty(cursor)
  1279  	s.Require().Len(notifications, 1)
  1280  	s.Require().Equal(nID1, notifications[0].ID)
  1281  
  1282  	cursor, notifications, err = p.ActivityCenterNotifications(
  1283  		"",
  1284  		2,
  1285  		[]ActivityCenterType{ActivityCenterNotificationTypeNewOneToOne},
  1286  		ActivityCenterQueryParamsReadRead,
  1287  		false,
  1288  	)
  1289  	s.Require().NoError(err)
  1290  	s.Require().Empty(cursor)
  1291  	s.Require().Len(notifications, 1)
  1292  	s.Require().Equal(nID2, notifications[0].ID)
  1293  }