From 29deacc2df7269894f833ff5ad76d736873ddaa1 Mon Sep 17 00:00:00 2001 From: Andrea Maria Piana Date: Fri, 25 Jun 2021 10:30:18 +0200 Subject: [PATCH] address feedback --- go.sum | 12 ++- protocol/edit_message.go | 3 +- protocol/messenger.go | 89 +++++-------------- protocol/messenger_edit_message_test.go | 1 + ...essage_handler.go => messenger_handler.go} | 2 + ...dler_test.go => messenger_handler_test.go} | 0 protocol/messenger_response.go | 1 - protocol/messenger_response_test.go | 11 ++- 8 files changed, 42 insertions(+), 77 deletions(-) rename protocol/{message_handler.go => messenger_handler.go} (99%) rename protocol/{message_handler_test.go => messenger_handler_test.go} (100%) diff --git a/go.sum b/go.sum index 9da191c99..098c0bd87 100644 --- a/go.sum +++ b/go.sum @@ -1021,8 +1021,6 @@ golang.org/x/mobile v0.0.0-20190719004257-d2bd2a29d028/go.mod h1:E/iHnbuqvinMTCc golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc= golang.org/x/mod v0.1.0/go.mod h1:0QHyrYULN0/3qlju5TqG8bIK38QM8yzMo5ekMj3DlcY= golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= -golang.org/x/mod v0.1.1-0.20191209134235-331c550502dd h1:ePuNC7PZ6O5BzgPn9bZayERXBdfZjUYoXEf5BTfDfh8= -golang.org/x/mod v0.1.1-0.20191209134235-331c550502dd/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= @@ -1060,8 +1058,9 @@ golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a h1:WXEvlFVvvGxCJLG6REjsT03iWnKLEWinaScsxF2Vm2o= golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ= +golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= @@ -1136,14 +1135,13 @@ golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5/go.mod h1:b+2E5dAYhXwXZwtn golang.org/x/tools v0.0.0-20191108193012-7d206e10da11/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20191109212701-97ad0ed33101/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20191112195655-aa38f8e97acc/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20191216052735-49a3e744a425 h1:VvQyQJN0tSuecqgcIxMWnnfG5kSmgy9KZR9sW3W5QeA= golang.org/x/tools v0.0.0-20191216052735-49a3e744a425/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= -golang.org/x/tools v0.0.0-20200211045251-2de505fc5306 h1:5gd/+xxg4X7hx+44aG6Sdh17vBwwRFacMaSfqF4wkWk= -golang.org/x/tools v0.0.0-20200211045251-2de505fc5306/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/api v0.3.1/go.mod h1:6wY9I6uQWHQ8EM57III9mq/AjF+i8G65rmVagqKMtkk= google.golang.org/api v0.3.2/go.mod h1:6wY9I6uQWHQ8EM57III9mq/AjF+i8G65rmVagqKMtkk= google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE= diff --git a/protocol/edit_message.go b/protocol/edit_message.go index b58667c68..737fb7a74 100644 --- a/protocol/edit_message.go +++ b/protocol/edit_message.go @@ -13,7 +13,8 @@ import ( type EditMessage struct { protobuf.EditMessage - ID string `json:"id",omitempty"` + // ID is the ID of the message that has been edited + ID string `json:"id,omitempty"` // From is a public key of the author of the edit reaction. From string `json:"from,omitempty"` diff --git a/protocol/messenger.go b/protocol/messenger.go index e86979f47..c9aabebd2 100644 --- a/protocol/messenger.go +++ b/protocol/messenger.go @@ -1216,14 +1216,18 @@ func (m *Messenger) CreateGroupChatWithMembers(ctx context.Context, name string, chat.updateChatFromGroupMembershipChanges(group) - response.AddChat(&chat) - response.AddMessages(buildSystemMessages(chat.MembershipUpdates, m.systemMessagesTranslations)) - err = m.persistence.SaveMessages(response.Messages()) + return m.addMessagesAndChat(&chat, buildSystemMessages(chat.MembershipUpdates, m.systemMessagesTranslations), &response) +} + +func (m *Messenger) addMessagesAndChat(chat *Chat, messages []*common.Message, response *MessengerResponse) (*MessengerResponse, error) { + response.AddChat(chat) + response.AddMessages(messages) + err := m.persistence.SaveMessages(response.Messages()) if err != nil { return nil, err } - return &response, m.saveChat(&chat) + return response, m.saveChat(chat) } func (m *Messenger) CreateGroupChatFromInvitation(name string, chatID string, adminPK string) (*MessengerResponse, error) { @@ -1291,14 +1295,7 @@ func (m *Messenger) RemoveMemberFromGroupChat(ctx context.Context, chatID string chat.updateChatFromGroupMembershipChanges(group) - response.AddChat(chat) - response.AddMessages(buildSystemMessages(chat.MembershipUpdates, m.systemMessagesTranslations)) - err = m.persistence.SaveMessages(response.Messages()) - if err != nil { - return nil, err - } - - return &response, m.saveChat(chat) + return m.addMessagesAndChat(chat, buildSystemMessages(chat.MembershipUpdates, m.systemMessagesTranslations), &response) } func (m *Messenger) AddMembersToGroupChat(ctx context.Context, chatID string, members []string) (*MessengerResponse, error) { @@ -1377,14 +1374,7 @@ func (m *Messenger) AddMembersToGroupChat(ctx context.Context, chatID string, me chat.updateChatFromGroupMembershipChanges(group) - response.AddChat(chat) - response.AddMessages(buildSystemMessages([]v1protocol.MembershipUpdateEvent{event}, m.systemMessagesTranslations)) - err = m.persistence.SaveMessages(response.Messages()) - if err != nil { - return nil, err - } - - return &response, m.saveChat(chat) + return m.addMessagesAndChat(chat, buildSystemMessages([]v1protocol.MembershipUpdateEvent{event}, m.systemMessagesTranslations), &response) } func (m *Messenger) ChangeGroupChatName(ctx context.Context, chatID string, name string) (*MessengerResponse, error) { @@ -1439,14 +1429,8 @@ func (m *Messenger) ChangeGroupChatName(ctx context.Context, chatID string, name chat.updateChatFromGroupMembershipChanges(group) var response MessengerResponse - response.AddChat(chat) - response.AddMessages(buildSystemMessages([]v1protocol.MembershipUpdateEvent{event}, m.systemMessagesTranslations)) - err = m.persistence.SaveMessages(response.Messages()) - if err != nil { - return nil, err - } - return &response, m.saveChat(chat) + return m.addMessagesAndChat(chat, buildSystemMessages([]v1protocol.MembershipUpdateEvent{event}, m.systemMessagesTranslations), &response) } func (m *Messenger) SendGroupChatInvitationRequest(ctx context.Context, chatID string, adminPK string, @@ -1638,15 +1622,7 @@ func (m *Messenger) AddAdminsToGroupChat(ctx context.Context, chatID string, mem } chat.updateChatFromGroupMembershipChanges(group) - - response.AddChat(chat) - response.AddMessages(buildSystemMessages([]v1protocol.MembershipUpdateEvent{event}, m.systemMessagesTranslations)) - err = m.persistence.SaveMessages(response.Messages()) - if err != nil { - return nil, err - } - - return &response, m.saveChat(chat) + return m.addMessagesAndChat(chat, buildSystemMessages([]v1protocol.MembershipUpdateEvent{event}, m.systemMessagesTranslations), &response) } func (m *Messenger) ConfirmJoiningGroup(ctx context.Context, chatID string) (*MessengerResponse, error) { @@ -1703,14 +1679,7 @@ func (m *Messenger) ConfirmJoiningGroup(ctx context.Context, chatID string) (*Me chat.updateChatFromGroupMembershipChanges(group) chat.Joined = int64(m.getTimesource().GetCurrentTime()) - response.AddChat(chat) - response.AddMessages(buildSystemMessages([]v1protocol.MembershipUpdateEvent{event}, m.systemMessagesTranslations)) - err = m.persistence.SaveMessages(response.Messages()) - if err != nil { - return nil, err - } - - return &response, m.saveChat(chat) + return m.addMessagesAndChat(chat, buildSystemMessages([]v1protocol.MembershipUpdateEvent{event}, m.systemMessagesTranslations), &response) } func (m *Messenger) LeaveGroupChat(ctx context.Context, chatID string, remove bool) (*MessengerResponse, error) { @@ -3013,7 +2982,9 @@ func (m *Messenger) handleRetrievedMessages(chatWithMessages map[transport.Filte newMessagesIds := map[string]struct{}{} for _, message := range messagesToSave { - newMessagesIds[message.ID] = struct{}{} + if message.New { + newMessagesIds[message.ID] = struct{}{} + } } messagesWithResponses, err := m.pullMessagesAndResponsesFromDB(messagesToSave) @@ -3286,9 +3257,7 @@ func (m *Messenger) RequestTransaction(ctx context.Context, chatID, value, contr return nil, err } - response.AddChat(chat) - response.AddMessages([]*common.Message{message}) - return &response, m.saveChat(chat) + return m.addMessagesAndChat(chat, []*common.Message{message}, &response) } func (m *Messenger) RequestAddressForTransaction(ctx context.Context, chatID, from, value, contract string) (*MessengerResponse, error) { @@ -3360,9 +3329,7 @@ func (m *Messenger) RequestAddressForTransaction(ctx context.Context, chatID, fr return nil, err } - response.AddChat(chat) - response.AddMessages([]*common.Message{message}) - return &response, m.saveChat(chat) + return m.addMessagesAndChat(chat, []*common.Message{message}, &response) } func (m *Messenger) AcceptRequestAddressForTransaction(ctx context.Context, messageID, address string) (*MessengerResponse, error) { @@ -3453,9 +3420,7 @@ func (m *Messenger) AcceptRequestAddressForTransaction(ctx context.Context, mess return nil, err } - response.AddChat(chat) - response.AddMessages([]*common.Message{message}) - return &response, m.saveChat(chat) + return m.addMessagesAndChat(chat, []*common.Message{message}, &response) } func (m *Messenger) DeclineRequestTransaction(ctx context.Context, messageID string) (*MessengerResponse, error) { @@ -3533,9 +3498,7 @@ func (m *Messenger) DeclineRequestTransaction(ctx context.Context, messageID str return nil, err } - response.AddChat(chat) - response.AddMessages([]*common.Message{message}) - return &response, m.saveChat(chat) + return m.addMessagesAndChat(chat, []*common.Message{message}, &response) } func (m *Messenger) DeclineRequestAddressForTransaction(ctx context.Context, messageID string) (*MessengerResponse, error) { @@ -3613,9 +3576,7 @@ func (m *Messenger) DeclineRequestAddressForTransaction(ctx context.Context, mes return nil, err } - response.AddChat(chat) - response.AddMessages([]*common.Message{message}) - return &response, m.saveChat(chat) + return m.addMessagesAndChat(chat, []*common.Message{message}, &response) } func (m *Messenger) AcceptRequestTransaction(ctx context.Context, transactionHash, messageID string, signature []byte) (*MessengerResponse, error) { @@ -3710,9 +3671,7 @@ func (m *Messenger) AcceptRequestTransaction(ctx context.Context, transactionHas return nil, err } - response.AddChat(chat) - response.AddMessages([]*common.Message{message}) - return &response, m.saveChat(chat) + return m.addMessagesAndChat(chat, []*common.Message{message}, &response) } func (m *Messenger) SendTransaction(ctx context.Context, chatID, value, contract, transactionHash string, signature []byte) (*MessengerResponse, error) { @@ -3789,9 +3748,7 @@ func (m *Messenger) SendTransaction(ctx context.Context, chatID, value, contract return nil, err } - response.AddChat(chat) - response.AddMessages([]*common.Message{message}) - return &response, m.saveChat(chat) + return m.addMessagesAndChat(chat, []*common.Message{message}, &response) } func (m *Messenger) ValidateTransactions(ctx context.Context, addresses []types.Address) (*MessengerResponse, error) { diff --git a/protocol/messenger_edit_message_test.go b/protocol/messenger_edit_message_test.go index 3c8b73a1c..6b2686127 100644 --- a/protocol/messenger_edit_message_test.go +++ b/protocol/messenger_edit_message_test.go @@ -118,6 +118,7 @@ func (s *MessengerEditMessageSuite) TestEditMessage() { 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) // Main instance user attempts to edit the message it received from theirMessenger editedMessage = &requests.EditMessage{ diff --git a/protocol/message_handler.go b/protocol/messenger_handler.go similarity index 99% rename from protocol/message_handler.go rename to protocol/messenger_handler.go index 8da8128be..52a1d7b84 100644 --- a/protocol/message_handler.go +++ b/protocol/messenger_handler.go @@ -239,6 +239,7 @@ func (m *Messenger) handleCommandMessage(state *ReceivedMessageState, message *c // Add to response state.Response.AddChat(chat) if message != nil { + message.New = true state.Response.AddMessage(message) } @@ -657,6 +658,7 @@ func (m *Messenger) HandleChatMessage(state *ReceivedMessageState) error { state.Response.CommunityChanges = append(state.Response.CommunityChanges, communityResponse.Changes) } + receivedMessage.New = true state.Response.AddMessage(receivedMessage) return nil diff --git a/protocol/message_handler_test.go b/protocol/messenger_handler_test.go similarity index 100% rename from protocol/message_handler_test.go rename to protocol/messenger_handler_test.go diff --git a/protocol/messenger_response.go b/protocol/messenger_response.go index d6fa4f42f..bca1e43f0 100644 --- a/protocol/messenger_response.go +++ b/protocol/messenger_response.go @@ -18,7 +18,6 @@ type MessengerResponse struct { CommunityChanges []*communities.CommunityChanges RequestsToJoinCommunity []*communities.RequestToJoin Mailservers []mailservers.Mailserver - pendingEdits []*common.Message // notifications a list of notifications derived from messenger events // that are useful to notify the user about diff --git a/protocol/messenger_response_test.go b/protocol/messenger_response_test.go index faabad0d6..a0c2c00c3 100644 --- a/protocol/messenger_response_test.go +++ b/protocol/messenger_response_test.go @@ -40,8 +40,15 @@ func TestMessengerResponseMergeMessages(t *testing.T) { require.NoError(t, response1.Merge(response2)) require.Len(t, response1.Messages(), 2) - require.Equal(t, modifiedMessage1, response1.Messages()[0]) - require.Equal(t, message2, response1.Messages()[1]) + messages := response1.Messages() + if messages[0].ID == modifiedMessage1.ID { + require.Equal(t, modifiedMessage1, messages[0]) + require.Equal(t, message2, messages[1]) + } else { + require.Equal(t, modifiedMessage1, messages[1]) + require.Equal(t, message2, messages[0]) + } + } func TestMessengerResponseMergeNotImplemented(t *testing.T) {