[#17288] fix: Mention is displayed as public key in PNs (#4079)

This commit is contained in:
Mohsen 2023-10-03 19:29:27 +03:00 committed by GitHub
parent 53423e58ba
commit c88b6e53af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 71 additions and 15 deletions

View File

@ -1 +1 @@
0.167.4
0.167.5

View File

@ -45,14 +45,14 @@ func (n NotificationBody) MarshalJSON() ([]byte, error) {
return json.Marshal(item)
}
func NewMessageNotification(id string, message *common.Message, chat *Chat, contact *Contact, contacts *contactMap, profilePicturesVisibility int) (*localnotifications.Notification, error) {
func NewMessageNotification(id string, message *common.Message, chat *Chat, contact *Contact, resolvePrimaryName func(string) (string, error), profilePicturesVisibility int) (*localnotifications.Notification, error) {
body := &NotificationBody{
Message: message,
Chat: chat,
Contact: contact,
}
return body.toMessageNotification(id, contacts, profilePicturesVisibility)
return body.toMessageNotification(id, resolvePrimaryName, profilePicturesVisibility)
}
func DeletedMessageNotification(id string, chat *Chat) *localnotifications.Notification {
@ -84,7 +84,7 @@ func NewPrivateGroupInviteNotification(id string, chat *Chat, contact *Contact,
return body.toPrivateGroupInviteNotification(id, profilePicturesVisibility)
}
func (n NotificationBody) toMessageNotification(id string, contacts *contactMap, profilePicturesVisibility int) (*localnotifications.Notification, error) {
func (n NotificationBody) toMessageNotification(id string, resolvePrimaryName func(string) (string, error), profilePicturesVisibility int) (*localnotifications.Notification, error) {
var title string
if n.Chat.PrivateGroupChat() || n.Chat.Public() || n.Chat.CommunityChat() {
title = n.Chat.Name
@ -95,16 +95,13 @@ func (n NotificationBody) toMessageNotification(id string, contacts *contactMap,
canonicalNames := make(map[string]string)
for _, mentionID := range n.Message.Mentions {
contact, ok := contacts.Load(mentionID)
if !ok {
var err error
contact, err = buildContactFromPkString(mentionID)
name, err := resolvePrimaryName(mentionID)
if err != nil {
return nil, err
canonicalNames[mentionID] = mentionID
} else {
canonicalNames[mentionID] = name
}
}
canonicalNames[mentionID] = contact.PrimaryName()
}
// We don't pass idenity as only interested in the simplified text
simplifiedText, err := n.Message.GetSimplifiedText("", canonicalNames)

View File

@ -204,6 +204,29 @@ type EnvelopeEventsInterceptor struct {
Messenger *Messenger
}
func (m *Messenger) GetOwnPrimaryName() (string, error) {
ensName, err := m.settings.ENSName()
if err != nil {
return ensName, nil
}
return m.settings.DisplayName()
}
func (m *Messenger) ResolvePrimaryName(mentionID string) (string, error) {
if mentionID == m.myHexIdentity() {
return m.GetOwnPrimaryName()
}
contact, ok := m.allContacts.Load(mentionID)
if !ok {
var err error
contact, err = buildContactFromPkString(mentionID)
if err != nil {
return mentionID, err
}
}
return contact.PrimaryName(), nil
}
// EnvelopeSent triggered when envelope delivered at least to 1 peer.
func (interceptor EnvelopeEventsInterceptor) EnvelopeSent(identifiers [][]byte) {
if interceptor.Messenger != nil {
@ -3201,6 +3224,7 @@ type ReceivedMessageState struct {
GroupChatInvitations map[string]*GroupChatInvitation
// Response to the client
Response *MessengerResponse
ResolvePrimaryName func(string) (string, error)
// Timesource is a time source for clock values/timestamps.
Timesource common.TimeSource
AllBookmarks map[string]*browsers.Bookmark
@ -3263,7 +3287,7 @@ func (r *ReceivedMessageState) addNewMessageNotification(publicKey ecdsa.PublicK
if !chat.Muted {
if showMessageNotification(publicKey, m, chat, responseTo) {
notification, err := NewMessageNotification(m.ID, m, chat, contact, r.AllContacts, profilePicturesVisibility)
notification, err := NewMessageNotification(m.ID, m, chat, contact, r.ResolvePrimaryName, profilePicturesVisibility)
if err != nil {
return err
}
@ -3366,6 +3390,7 @@ func (m *Messenger) buildMessageState() *ReceivedMessageState {
GroupChatInvitations: make(map[string]*GroupChatInvitation),
Response: &MessengerResponse{},
Timesource: m.getTimesource(),
ResolvePrimaryName: m.ResolvePrimaryName,
AllBookmarks: make(map[string]*browsers.Bookmark),
AllTrustStatus: make(map[string]verification.TrustStatus),
}
@ -5013,7 +5038,7 @@ func (m *Messenger) ValidateTransactions(ctx context.Context, addresses []types.
}
if notificationsEnabled {
notification, err := NewMessageNotification(message.ID, message, chat, contact, m.allContacts, profilePicturesVisibility)
notification, err := NewMessageNotification(message.ID, message, chat, contact, m.ResolvePrimaryName, profilePicturesVisibility)
if err != nil {
return nil, err
}

View File

@ -5,6 +5,7 @@ import (
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"math/big"
"os"
@ -22,6 +23,7 @@ import (
"github.com/status-im/status-go/eth-node/types"
enstypes "github.com/status-im/status-go/eth-node/types/ens"
"github.com/status-im/status-go/images"
"github.com/status-im/status-go/multiaccounts/settings"
"github.com/status-im/status-go/protocol/common"
"github.com/status-im/status-go/protocol/protobuf"
"github.com/status-im/status-go/protocol/requests"
@ -2447,3 +2449,35 @@ type testTimeSource struct{}
func (t *testTimeSource) GetCurrentTime() uint64 {
return uint64(time.Now().Unix())
}
func (s *MessengerSuite) TestSendMessageMention() {
// Initialize Alice and Bob's messengers
alice, bob := s.m, s.newMessenger()
_, err := bob.Start()
s.Require().NoError(err)
defer bob.Shutdown() // nolint: errcheck
// Set display names for Bob and Alice
s.Require().NoError(bob.settings.SaveSettingField(settings.DisplayName, "bobby"))
s.Require().NoError(alice.settings.SaveSettingField(settings.DisplayName, "Alice"))
s.Require().NoError(alice.settings.SaveSettingField(settings.NotificationsEnabled, true))
// Create one-to-one chats
chat, chat2 := CreateOneToOneChat(common.PubkeyToHex(&alice.identity.PublicKey), &alice.identity.PublicKey, bob.transport),
CreateOneToOneChat(common.PubkeyToHex(&bob.identity.PublicKey), &bob.identity.PublicKey, alice.transport)
s.Require().NoError(bob.SaveChat(chat))
s.Require().NoError(alice.SaveChat(chat2))
// Prepare the message and Send the message from Bob to Alice
inputMessage := common.NewMessage()
inputMessage.ChatId = chat.ID
inputMessage.Text = fmt.Sprintf("@%s talk to @%s", alice.myHexIdentity(), bob.myHexIdentity())
inputMessage.ContentType = protobuf.ChatMessage_TEXT_PLAIN
_, err = bob.SendChatMessage(context.Background(), inputMessage)
s.Require().NoError(err)
// Wait for Alice to receive the message and make sure it's properly formatted
response, err := WaitOnMessengerResponse(alice, func(r *MessengerResponse) bool { return len(r.Notifications()) >= 1 }, "no messages")
s.Require().NoError(err)
s.Require().Equal("Alice talk to bobby", response.Notifications()[0].Message)
}