Fix notifications in activity center when chat is inactive
This commit is contained in:
parent
f1066857f4
commit
45a8de8e2b
|
@ -41,7 +41,7 @@ func (db sqlitePersistence) SaveActivityCenterNotification(notification *Activit
|
|||
|
||||
if notification.Type == ActivityCenterNotificationTypeNewOneToOne {
|
||||
// Delete other notifications so it pop us again if not currently dismissed
|
||||
_, err = tx.Exec(`DELETE FROM activity_center_notifications WHERE id = ? AND dismissed`, notification.ID)
|
||||
_, err = tx.Exec(`DELETE FROM activity_center_notifications WHERE id = ? AND (dismissed OR accepted)`, notification.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -0,0 +1,161 @@
|
|||
package protocol
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/ecdsa"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/suite"
|
||||
"go.uber.org/zap"
|
||||
|
||||
gethbridge "github.com/status-im/status-go/eth-node/bridge/geth"
|
||||
"github.com/status-im/status-go/eth-node/crypto"
|
||||
"github.com/status-im/status-go/eth-node/types"
|
||||
"github.com/status-im/status-go/protocol/requests"
|
||||
"github.com/status-im/status-go/protocol/tt"
|
||||
"github.com/status-im/status-go/waku"
|
||||
)
|
||||
|
||||
func TestMessengerActivityCenterMessageSuite(t *testing.T) {
|
||||
suite.Run(t, new(MessengerActivityCenterMessageSuite))
|
||||
}
|
||||
|
||||
type MessengerActivityCenterMessageSuite struct {
|
||||
suite.Suite
|
||||
m *Messenger // main instance of Messenger
|
||||
privateKey *ecdsa.PrivateKey // private key for the main instance of Messenger
|
||||
// If one wants to send messages between different instances of Messenger,
|
||||
// a single waku service should be shared.
|
||||
shh types.Waku
|
||||
logger *zap.Logger
|
||||
}
|
||||
|
||||
func (s *MessengerActivityCenterMessageSuite) SetupTest() {
|
||||
s.logger = tt.MustCreateTestLogger()
|
||||
|
||||
config := waku.DefaultConfig
|
||||
config.MinimumAcceptedPoW = 0
|
||||
shh := waku.New(&config, s.logger)
|
||||
s.shh = gethbridge.NewGethWakuWrapper(shh)
|
||||
s.Require().NoError(shh.Start(nil))
|
||||
|
||||
s.m = s.newMessenger()
|
||||
s.privateKey = s.m.identity
|
||||
_, err := s.m.Start()
|
||||
s.Require().NoError(err)
|
||||
}
|
||||
|
||||
func (s *MessengerActivityCenterMessageSuite) TearDownTest() {
|
||||
s.Require().NoError(s.m.Shutdown())
|
||||
}
|
||||
|
||||
func (s *MessengerActivityCenterMessageSuite) newMessenger() *Messenger {
|
||||
privateKey, err := crypto.GenerateKey()
|
||||
s.Require().NoError(err)
|
||||
|
||||
messenger, err := newMessengerWithKey(s.shh, privateKey, s.logger, nil)
|
||||
s.Require().NoError(err)
|
||||
return messenger
|
||||
}
|
||||
|
||||
func (s *MessengerActivityCenterMessageSuite) TestDismissOneToOneMessage() {
|
||||
theirMessenger := s.newMessenger()
|
||||
_, err := theirMessenger.Start()
|
||||
s.Require().NoError(err)
|
||||
|
||||
theirChat := CreateOneToOneChat("Their 1TO1", &s.privateKey.PublicKey, s.m.transport)
|
||||
err = theirMessenger.SaveChat(theirChat)
|
||||
s.Require().NoError(err)
|
||||
|
||||
inputMessage := buildTestMessage(*theirChat)
|
||||
sendResponse, err := theirMessenger.SendChatMessage(context.Background(), inputMessage)
|
||||
s.NoError(err)
|
||||
s.Require().Len(sendResponse.Messages(), 1)
|
||||
|
||||
response, err := WaitOnMessengerResponse(
|
||||
s.m,
|
||||
func(r *MessengerResponse) bool { return len(r.messages) > 0 },
|
||||
"no messages",
|
||||
)
|
||||
s.Require().NoError(err)
|
||||
s.Require().Len(response.Chats(), 1)
|
||||
s.Require().Len(response.Messages(), 1)
|
||||
s.Require().Len(response.ActivityCenterNotifications(), 1)
|
||||
|
||||
// Dismiss all
|
||||
s.Require().NoError(s.m.DismissAllActivityCenterNotifications())
|
||||
|
||||
// Send another message
|
||||
inputMessage = buildTestMessage(*theirChat)
|
||||
sendResponse, err = theirMessenger.SendChatMessage(context.Background(), inputMessage)
|
||||
s.NoError(err)
|
||||
s.Require().Len(sendResponse.Messages(), 1)
|
||||
|
||||
response, err = WaitOnMessengerResponse(
|
||||
s.m,
|
||||
func(r *MessengerResponse) bool { return len(r.messages) > 0 },
|
||||
"no messages",
|
||||
)
|
||||
s.Require().NoError(err)
|
||||
s.Require().Len(response.Chats(), 1)
|
||||
s.Require().Len(response.Messages(), 1)
|
||||
s.Require().Len(response.ActivityCenterNotifications(), 1)
|
||||
}
|
||||
|
||||
func (s *MessengerActivityCenterMessageSuite) TestDeleteOneToOneChat() {
|
||||
theirMessenger := s.newMessenger()
|
||||
_, err := theirMessenger.Start()
|
||||
s.Require().NoError(err)
|
||||
|
||||
theirChat := CreateOneToOneChat("Their 1TO1", &s.privateKey.PublicKey, s.m.transport)
|
||||
err = theirMessenger.SaveChat(theirChat)
|
||||
s.Require().NoError(err)
|
||||
|
||||
inputMessage := buildTestMessage(*theirChat)
|
||||
sendResponse, err := theirMessenger.SendChatMessage(context.Background(), inputMessage)
|
||||
s.NoError(err)
|
||||
s.Require().Len(sendResponse.Messages(), 1)
|
||||
|
||||
response, err := WaitOnMessengerResponse(
|
||||
s.m,
|
||||
func(r *MessengerResponse) bool { return len(r.messages) > 0 },
|
||||
"no messages",
|
||||
)
|
||||
s.Require().NoError(err)
|
||||
s.Require().Len(response.Chats(), 1)
|
||||
s.Require().Len(response.Messages(), 1)
|
||||
s.Require().Len(response.ActivityCenterNotifications(), 1)
|
||||
|
||||
// accept notification
|
||||
_, err = s.m.AcceptAllActivityCenterNotifications()
|
||||
s.Require().NoError(err)
|
||||
|
||||
request := &requests.DeactivateChat{ID: response.Chats()[0].ID}
|
||||
response, err = s.m.DeactivateChat(request)
|
||||
s.Require().NoError(err)
|
||||
s.Require().Len(response.Chats(), 1)
|
||||
deletedChat := response.Chats()[0]
|
||||
s.Require().NotEmpty(deletedChat.DeletedAtClockValue)
|
||||
|
||||
// Make sure deleted at clock value is greater
|
||||
theirChat.LastClockValue = deletedChat.DeletedAtClockValue + 1
|
||||
err = theirMessenger.SaveChat(theirChat)
|
||||
s.Require().NoError(err)
|
||||
|
||||
// Send another message
|
||||
inputMessage = buildTestMessage(*theirChat)
|
||||
sendResponse, err = theirMessenger.SendChatMessage(context.Background(), inputMessage)
|
||||
s.NoError(err)
|
||||
s.Require().Len(sendResponse.Messages(), 1)
|
||||
|
||||
response, err = WaitOnMessengerResponse(
|
||||
s.m,
|
||||
func(r *MessengerResponse) bool { return len(r.ActivityCenterNotifications()) > 0 },
|
||||
"no messages",
|
||||
)
|
||||
s.Require().NoError(err)
|
||||
s.Require().Len(response.Chats(), 1)
|
||||
s.Require().Len(response.Messages(), 1)
|
||||
s.Require().Len(response.ActivityCenterNotifications(), 1)
|
||||
|
||||
}
|
|
@ -4,7 +4,6 @@ import (
|
|||
"context"
|
||||
"errors"
|
||||
|
||||
"github.com/status-im/status-go/eth-node/types"
|
||||
"github.com/status-im/status-go/protocol/common"
|
||||
"github.com/status-im/status-go/protocol/requests"
|
||||
"github.com/status-im/status-go/protocol/transport"
|
||||
|
@ -293,13 +292,6 @@ func (m *Messenger) saveChats(chats []*Chat) error {
|
|||
func (m *Messenger) saveChat(chat *Chat) error {
|
||||
previousChat, ok := m.allChats.Load(chat.ID)
|
||||
if chat.OneToOne() {
|
||||
// We clear all notifications so it pops up again
|
||||
if !chat.Active {
|
||||
err := m.persistence.DeleteActivityCenterNotification(types.FromHex(chat.ID))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
name, identicon, err := generateAliasAndIdenticon(chat.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
|
@ -130,6 +130,83 @@ func (s *MessengerEditMessageSuite) TestEditMessage() {
|
|||
s.Require().Equal(ErrInvalidEditAuthor, err)
|
||||
}
|
||||
|
||||
func (s *MessengerEditMessageSuite) TestEditMessageActivityCenter() {
|
||||
theirMessenger := s.newMessenger()
|
||||
_, err := theirMessenger.Start()
|
||||
s.Require().NoError(err)
|
||||
|
||||
theirChat := CreateOneToOneChat("Their 1TO1", &s.privateKey.PublicKey, s.m.transport)
|
||||
err = theirMessenger.SaveChat(theirChat)
|
||||
s.Require().NoError(err)
|
||||
|
||||
inputMessage := buildTestMessage(*theirChat)
|
||||
sendResponse, err := theirMessenger.SendChatMessage(context.Background(), inputMessage)
|
||||
s.NoError(err)
|
||||
s.Require().Len(sendResponse.Messages(), 1)
|
||||
|
||||
response, err := WaitOnMessengerResponse(
|
||||
s.m,
|
||||
func(r *MessengerResponse) bool { return len(r.messages) > 0 },
|
||||
"no messages",
|
||||
)
|
||||
s.Require().NoError(err)
|
||||
s.Require().Len(response.Chats(), 1)
|
||||
s.Require().Len(response.Messages(), 1)
|
||||
s.Require().Len(response.ActivityCenterNotifications(), 1)
|
||||
|
||||
// Check we can fetch the notification
|
||||
paginationResponse, err := s.m.ActivityCenterNotifications("", 10)
|
||||
s.Require().NoError(err)
|
||||
s.Require().Len(paginationResponse.Notifications, 1)
|
||||
|
||||
ogMessage := sendResponse.Messages()[0]
|
||||
|
||||
messageID, err := types.DecodeHex(ogMessage.ID)
|
||||
s.Require().NoError(err)
|
||||
|
||||
editedText := "edited text"
|
||||
editedMessage := &requests.EditMessage{
|
||||
ID: messageID,
|
||||
Text: editedText,
|
||||
}
|
||||
|
||||
sendResponse, err = theirMessenger.EditMessage(context.Background(), editedMessage)
|
||||
|
||||
s.Require().NoError(err)
|
||||
s.Require().Len(sendResponse.Messages(), 1)
|
||||
s.Require().NotEmpty(sendResponse.Messages()[0].EditedAt)
|
||||
s.Require().Equal(sendResponse.Messages()[0].Text, editedText)
|
||||
s.Require().Len(sendResponse.Chats(), 1)
|
||||
s.Require().NotNil(sendResponse.Chats()[0].LastMessage)
|
||||
s.Require().NotEmpty(sendResponse.Chats()[0].LastMessage.EditedAt)
|
||||
|
||||
response, err = WaitOnMessengerResponse(
|
||||
s.m,
|
||||
func(r *MessengerResponse) bool { return len(r.messages) > 0 },
|
||||
"no messages",
|
||||
)
|
||||
s.Require().NoError(err)
|
||||
|
||||
s.Require().Len(response.Chats(), 1)
|
||||
s.Require().Len(response.Messages(), 1)
|
||||
s.Require().NotEmpty(response.Messages()[0].EditedAt)
|
||||
s.Require().False(response.Messages()[0].New)
|
||||
|
||||
// Check we can fetch the notification
|
||||
paginationResponse, err = s.m.ActivityCenterNotifications("", 10)
|
||||
s.Require().NoError(err)
|
||||
s.Require().Len(paginationResponse.Notifications, 1)
|
||||
|
||||
// Main instance user attempts to edit the message it received from theirMessenger
|
||||
editedMessage = &requests.EditMessage{
|
||||
ID: messageID,
|
||||
Text: "edited-again text",
|
||||
}
|
||||
_, err = s.m.EditMessage(context.Background(), editedMessage)
|
||||
|
||||
s.Require().Equal(ErrInvalidEditAuthor, err)
|
||||
}
|
||||
|
||||
func (s *MessengerEditMessageSuite) TestEditMessageEdgeCases() {
|
||||
theirMessenger := s.newMessenger()
|
||||
_, err := theirMessenger.Start()
|
||||
|
@ -190,10 +267,11 @@ func (s *MessengerEditMessageSuite) TestEditMessageEdgeCases() {
|
|||
|
||||
editMessage = EditMessage{
|
||||
EditMessage: protobuf.EditMessage{
|
||||
Clock: editedMessage.Clock + 2,
|
||||
Text: "some text",
|
||||
MessageId: editedMessage.ID,
|
||||
ChatId: chat.ID,
|
||||
Clock: editedMessage.Clock + 2,
|
||||
Text: "some text",
|
||||
MessageType: protobuf.MessageType_ONE_TO_ONE,
|
||||
MessageId: editedMessage.ID,
|
||||
ChatId: chat.ID,
|
||||
},
|
||||
From: contact.ID,
|
||||
}
|
||||
|
@ -211,10 +289,11 @@ func (s *MessengerEditMessageSuite) TestEditMessageEdgeCases() {
|
|||
// In-between edit
|
||||
editMessage = EditMessage{
|
||||
EditMessage: protobuf.EditMessage{
|
||||
Clock: editedMessage.Clock + 1,
|
||||
Text: "some other text",
|
||||
MessageId: editedMessage.ID,
|
||||
ChatId: chat.ID,
|
||||
Clock: editedMessage.Clock + 1,
|
||||
Text: "some other text",
|
||||
MessageType: protobuf.MessageType_ONE_TO_ONE,
|
||||
MessageId: editedMessage.ID,
|
||||
ChatId: chat.ID,
|
||||
},
|
||||
From: contact.ID,
|
||||
}
|
||||
|
@ -249,10 +328,11 @@ func (s *MessengerEditMessageSuite) TestEditMessageFirstEditsThenMessage() {
|
|||
inputMessage.Clock = 1
|
||||
editMessage := EditMessage{
|
||||
EditMessage: protobuf.EditMessage{
|
||||
Clock: 2,
|
||||
Text: "some text",
|
||||
MessageId: messageID,
|
||||
ChatId: theirChat.ID,
|
||||
Clock: 2,
|
||||
Text: "some text",
|
||||
MessageType: protobuf.MessageType_ONE_TO_ONE,
|
||||
MessageId: messageID,
|
||||
ChatId: theirChat.ID,
|
||||
},
|
||||
From: common.PubkeyToHex(&theirMessenger.identity.PublicKey),
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue