Skip to content

Commit

Permalink
fix(pins)_: delete pins when the og message is deleted (#6173)
Browse files Browse the repository at this point in the history
Found when fixing status-im/status-desktop#16639

When a message is deleted, we need to delete the pins too as they are no longer available. This was done using an ON DELETE clause (thanks @osmaczko)
I also made sure the SELECT query for the pins doesn't return deleted messages
  • Loading branch information
jrainville authored Dec 12, 2024
1 parent 137698e commit 5a8310d
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
1 change: 1 addition & 0 deletions protocol/message_persistence.go
Original file line number Diff line number Diff line change
Expand Up @@ -1250,6 +1250,7 @@ func (db sqlitePersistence) PinnedMessageByChatIDs(chatIDs []string, currCursor
WHERE
pm.pinned = 1
AND NOT(m1.hide) AND m1.local_chat_id IN %s %s
AND m1.deleted = 0
ORDER BY cursor DESC
%s
`, allFields, cursorField, "(?"+strings.Repeat(",?", len(chatIDs)-1)+")", cursorWhere, limitStr),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
CREATE TABLE pin_messages_new (
id VARCHAR PRIMARY KEY NOT NULL,
message_id VARCHAR NOT NULL,
whisper_timestamp INTEGER NOT NULL,
chat_id VARCHAR NOT NULL,
local_chat_id VARCHAR NOT NULL,
clock_value INT NOT NULL,
pinned BOOLEAN NOT NULL,
pinned_by TEXT,
FOREIGN KEY (message_id) REFERENCES user_messages(id) ON DELETE CASCADE
);

INSERT INTO pin_messages_new (id, message_id, whisper_timestamp, chat_id, local_chat_id, clock_value, pinned, pinned_by)
SELECT id, message_id, whisper_timestamp, chat_id, local_chat_id, clock_value, pinned, pinned_by
FROM pin_messages;

DROP TABLE pin_messages;

ALTER TABLE pin_messages_new RENAME TO pin_messages;
44 changes: 44 additions & 0 deletions protocol/persistence_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,50 @@ func TestDeleteMessagesByChatID(t *testing.T) {

}

func TestDeletePinnedMessageByID(t *testing.T) {
db, err := openTestDB()
require.NoError(t, err)
p := newSQLitePersistence(db)
id := "1"

err = insertMinimalMessage(p, id)
require.NoError(t, err)

m, err := p.MessageByID(id)
require.NoError(t, err)
require.Equal(t, id, m.ID)

pinMessageProto := protobuf.PinMessage{
ChatId: testPublicChatID,
MessageId: m.ID,
Pinned: true,
Clock: 2,
MessageType: protobuf.MessageType_PUBLIC_GROUP,
}

pinMessage := &common.PinMessage{
PinMessage: &pinMessageProto,
}

inserted, err := p.SavePinMessage(pinMessage)
require.NoError(t, err)
require.True(t, inserted)

pinnedMsgs, _, err := p.PinnedMessageByChatID(testPublicChatID, "", 10)
require.NoError(t, err)
require.Len(t, pinnedMsgs, 1)

err = p.DeleteMessage(m.ID)
require.NoError(t, err)

_, err = p.MessageByID(id)
require.EqualError(t, err, "record not found")

pinnedMsgs, _, err = p.PinnedMessageByChatID(testPublicChatID, "", 10)
require.NoError(t, err)
require.Len(t, pinnedMsgs, 0)
}

func TestMarkMessageSeen(t *testing.T) {
chatID := "test-chat"
db, err := openTestDB()
Expand Down

0 comments on commit 5a8310d

Please sign in to comment.