Skip to content

Commit

Permalink
fix(pins)_: remove pins when the original message is deleted
Browse files Browse the repository at this point in the history
Fixes #6246
  • Loading branch information
jrainville committed Jan 13, 2025
1 parent 956721f commit a0a0075
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions protocol/message_persistence.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"database/sql"
"encoding/json"
"errors"
"fmt"
"sort"
"strings"
Expand Down Expand Up @@ -1768,17 +1769,41 @@ func (db sqlitePersistence) SavePinMessage(message *common.PinMessage) (inserted

func (db sqlitePersistence) DeleteMessage(id string) error {
_, err := db.db.Exec(`DELETE FROM user_messages WHERE id = ?`, id)

if err != nil {
return err
}

_, err = db.db.Exec("DELETE FROM pin_messages WHERE message_id = ?", id)

return err
}

func (db sqlitePersistence) DeleteMessages(ids []string) error {
func (db sqlitePersistence) DeleteMessages(ids []string) (err error) {
idsArgs := make([]interface{}, 0, len(ids))
for _, id := range ids {
idsArgs = append(idsArgs, id)
}
inVector := strings.Repeat("?, ", len(ids)-1) + "?"

_, err := db.db.Exec("DELETE FROM user_messages WHERE id IN ("+inVector+")", idsArgs...) // nolint: gosec
tx, err := db.db.BeginTx(context.Background(), &sql.TxOptions{})
if err != nil {
return err
}
defer func() {
if err == nil {
err = tx.Commit()
return
}
err = errors.Join(err, tx.Rollback())
}()

_, err = tx.Exec("DELETE FROM user_messages WHERE id IN ("+inVector+")", idsArgs...) // nolint: gosec
if err != nil {
return err
}

_, err = tx.Exec("DELETE FROM pin_messages WHERE message_id IN ("+inVector+")", idsArgs...) // nolint: gosec

return err
}
Expand Down

0 comments on commit a0a0075

Please sign in to comment.