fix(pins)_: delete pins when the og message is deleted (#6173)

Found when fixing https://github.com/status-im/status-desktop/issues/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
This commit is contained in:
Jonathan Rainville 2024-12-12 13:18:40 -05:00 committed by GitHub
parent 137698e022
commit 5a8310d5a7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 64 additions and 0 deletions

View File

@ -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),

View File

@ -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;

View File

@ -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()