Delete messages by timestamp range instead of message ID

This commit is contained in:
Tulir Asokan 2022-04-18 20:12:01 +03:00
parent 7a77b1976c
commit 5b9fe8e08a

View file

@ -20,7 +20,6 @@ import (
"database/sql" "database/sql"
"errors" "errors"
"fmt" "fmt"
"strings"
"time" "time"
waProto "go.mau.fi/whatsmeow/binary/proto" waProto "go.mau.fi/whatsmeow/binary/proto"
@ -209,17 +208,15 @@ func (hsq *HistorySyncQuery) DeleteAllConversations(userID id.UserID) error {
const ( const (
getMessagesBetween = ` getMessagesBetween = `
SELECT data SELECT data FROM history_sync_message
FROM history_sync_message WHERE user_mxid=$1 AND conversation_id=$2
WHERE user_mxid=$1 %s
AND conversation_id=$2 ORDER BY timestamp DESC
%s %s
ORDER BY timestamp DESC
%s
` `
deleteMessages = ` deleteMessagesBetweenExclusive = `
DELETE FROM history_sync_message DELETE FROM history_sync_message
WHERE %s WHERE user_mxid=$1 AND conversation_id=$2 AND timestamp<$3 AND timestamp>$4
` `
) )
@ -305,14 +302,11 @@ func (hsq *HistorySyncQuery) GetMessagesBetween(userID id.UserID, conversationID
} }
func (hsq *HistorySyncQuery) DeleteMessages(userID id.UserID, conversationID string, messages []*waProto.WebMessageInfo) error { func (hsq *HistorySyncQuery) DeleteMessages(userID id.UserID, conversationID string, messages []*waProto.WebMessageInfo) error {
whereClauses := []string{} newest := messages[0]
preparedStatementArgs := []interface{}{userID, conversationID} beforeTS := time.Unix(int64(newest.GetMessageTimestamp())+1, 0)
for i, msg := range messages { oldest := messages[len(messages)-1]
whereClauses = append(whereClauses, fmt.Sprintf("(user_mxid=$1 AND conversation_id=$2 AND message_id=$%d)", i+3)) afterTS := time.Unix(int64(oldest.GetMessageTimestamp())-1, 0)
preparedStatementArgs = append(preparedStatementArgs, msg.GetKey().GetId()) _, err := hsq.db.Exec(deleteMessagesBetweenExclusive, userID, conversationID, beforeTS, afterTS)
}
_, err := hsq.db.Exec(fmt.Sprintf(deleteMessages, strings.Join(whereClauses, " OR ")), preparedStatementArgs...)
return err return err
} }