fix(bridge)_: fix bridge replies not working when they come from Discord (#5830)
Fixes https://github.com/status-im/status-desktop/issues/16323 The problem was that the code expected to receive the Discord message ID, but the bridge is smart enough to return the Status message ID already, so there is no need to try and convert it. There were also a couple issues in the bridge code itself.
This commit is contained in:
parent
fc36a7e980
commit
2bbcce6e25
|
@ -1580,11 +1580,6 @@ func (db sqlitePersistence) SaveMessages(messages []*common.Message) (err error)
|
|||
if err != nil {
|
||||
return
|
||||
}
|
||||
// handle replies
|
||||
err = db.findAndUpdateReplies(tx, msg.GetBridgeMessage().MessageID, msg.ID)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
parentMessageID := msg.GetBridgeMessage().ParentMessageID
|
||||
if parentMessageID != "" {
|
||||
err = db.findAndUpdateRepliedTo(tx, parentMessageID, msg.ID)
|
||||
|
@ -2972,26 +2967,6 @@ func (db sqlitePersistence) GetCommunityMemberMessagesToDelete(member string, co
|
|||
return result, nil
|
||||
}
|
||||
|
||||
// Finds status messages id which are replies for bridgeMessageID
|
||||
func (db sqlitePersistence) findStatusMessageIdsReplies(tx *sql.Tx, bridgeMessageID string) ([]string, error) {
|
||||
rows, err := tx.Query(`SELECT user_messages_id FROM bridge_messages WHERE parent_message_id = ?`, bridgeMessageID)
|
||||
if err != nil {
|
||||
return []string{}, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var statusMessageIDs []string
|
||||
for rows.Next() {
|
||||
var statusMessageID string
|
||||
err = rows.Scan(&statusMessageID)
|
||||
if err != nil {
|
||||
return []string{}, err
|
||||
}
|
||||
statusMessageIDs = append(statusMessageIDs, statusMessageID)
|
||||
}
|
||||
return statusMessageIDs, nil
|
||||
}
|
||||
|
||||
func (db sqlitePersistence) FindStatusMessageIDForBridgeMessageID(messageID string) (string, error) {
|
||||
rows, err := db.db.Query(`SELECT user_messages_id FROM bridge_messages WHERE message_id = ?`, messageID)
|
||||
if err != nil {
|
||||
|
@ -3010,24 +2985,6 @@ func (db sqlitePersistence) FindStatusMessageIDForBridgeMessageID(messageID stri
|
|||
return "", nil
|
||||
}
|
||||
|
||||
func (db sqlitePersistence) findStatusMessageIDForBridgeMessageID(tx *sql.Tx, messageID string) (string, error) {
|
||||
rows, err := tx.Query(`SELECT user_messages_id FROM bridge_messages WHERE message_id = ?`, messageID)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
if rows.Next() {
|
||||
var statusMessageID string
|
||||
err = rows.Scan(&statusMessageID)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return statusMessageID, nil
|
||||
}
|
||||
return "", nil
|
||||
}
|
||||
|
||||
func (db sqlitePersistence) updateStatusMessagesWithResponse(tx *sql.Tx, statusMessagesToUpdate []string, responseValue string) error {
|
||||
sql := "UPDATE user_messages SET response_to = ? WHERE id IN (?" + strings.Repeat(",?", len(statusMessagesToUpdate)-1) + ")" //nolint: gosec
|
||||
stmt, err := tx.Prepare(sql)
|
||||
|
@ -3062,28 +3019,8 @@ func (db sqlitePersistence) updateBridgeMessageContent(tx *sql.Tx, bridgeMessage
|
|||
return err
|
||||
}
|
||||
|
||||
// Finds if there are any messages that are replies to that message (in case replies were received earlier)
|
||||
func (db sqlitePersistence) findAndUpdateReplies(tx *sql.Tx, bridgeMessageID string, statusMessageID string) error {
|
||||
replyMessageIds, err := db.findStatusMessageIdsReplies(tx, bridgeMessageID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(replyMessageIds) == 0 {
|
||||
return nil
|
||||
}
|
||||
return db.updateStatusMessagesWithResponse(tx, replyMessageIds, statusMessageID)
|
||||
}
|
||||
|
||||
func (db sqlitePersistence) findAndUpdateRepliedTo(tx *sql.Tx, discordParentMessageID string, statusMessageID string) error {
|
||||
// Finds status messages id which are replies for bridgeMessageID
|
||||
repliedMessageID, err := db.findStatusMessageIDForBridgeMessageID(tx, discordParentMessageID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if repliedMessageID == "" {
|
||||
return nil
|
||||
}
|
||||
return db.updateStatusMessagesWithResponse(tx, []string{statusMessageID}, repliedMessageID)
|
||||
return db.updateStatusMessagesWithResponse(tx, []string{statusMessageID}, discordParentMessageID)
|
||||
}
|
||||
|
||||
func (db sqlitePersistence) GetCommunityMemberAllMessages(member string, communityID string) ([]*common.Message, error) {
|
||||
|
|
|
@ -1983,43 +1983,43 @@ func TestBridgeMessageReplies(t *testing.T) {
|
|||
|
||||
require.NoError(t, err)
|
||||
|
||||
err = insertMinimalBridgeMessage(p, "111", "1", "")
|
||||
err = insertMinimalBridgeMessage(p, "message1", "discordId1", "")
|
||||
require.NoError(t, err)
|
||||
|
||||
err = insertMinimalBridgeMessage(p, "222", "2", "1")
|
||||
err = insertMinimalBridgeMessage(p, "message2", "discordId2", "message1")
|
||||
require.NoError(t, err)
|
||||
|
||||
// "333 is not delivered yet"
|
||||
// "message3 is not delivered yet"
|
||||
|
||||
// this is a reply to a message which was not delivered yet
|
||||
err = insertMinimalBridgeMessage(p, "444", "4", "3")
|
||||
err = insertMinimalBridgeMessage(p, "message4", "discordId4", "message3")
|
||||
require.NoError(t, err)
|
||||
|
||||
// status message "222" should have reply_to = "111"
|
||||
responseTo, err := messageResponseTo(p, "222")
|
||||
// status message "message2" should have reply_to ="message1" because it's a discord message to another discord message
|
||||
responseTo, err := messageResponseTo(p, "message2")
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "111", responseTo)
|
||||
require.Equal(t, "message1", responseTo)
|
||||
|
||||
responseTo, err = messageResponseTo(p, "111")
|
||||
responseTo, err = messageResponseTo(p, "message1")
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "", responseTo)
|
||||
|
||||
responseTo, err = messageResponseTo(p, "444")
|
||||
responseTo, err = messageResponseTo(p, "message4")
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "message3", responseTo)
|
||||
|
||||
// receiving message for which "message4" is replied to
|
||||
err = insertMinimalBridgeMessage(p, "message3", "discordId3", "")
|
||||
require.NoError(t, err)
|
||||
|
||||
responseTo, err = messageResponseTo(p, "message3")
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "", responseTo)
|
||||
|
||||
// receiving message for which "444" is replied to
|
||||
err = insertMinimalBridgeMessage(p, "333", "3", "")
|
||||
// message4 is still replied to message3
|
||||
responseTo, err = messageResponseTo(p, "message4")
|
||||
require.NoError(t, err)
|
||||
|
||||
responseTo, err = messageResponseTo(p, "333")
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "", responseTo)
|
||||
|
||||
// now 444 is replied to 333
|
||||
responseTo, err = messageResponseTo(p, "444")
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "333", responseTo)
|
||||
require.Equal(t, "message3", responseTo)
|
||||
}
|
||||
|
||||
func createAndSaveMessage(p *sqlitePersistence, id string, from string, deleted bool, communityID string) error {
|
||||
|
|
Loading…
Reference in New Issue