Delete activity center notifications for deleted messages (#2319)

This commit is contained in:
Brian Sztamfater 2021-08-23 11:23:55 -03:00 committed by GitHub
parent 8f9c644dae
commit 0e54d04e67
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 66 additions and 5 deletions

View File

@ -17,6 +17,52 @@ func (db sqlitePersistence) DeleteActivityCenterNotification(id []byte) error {
return err
}
func (db sqlitePersistence) DeleteActivityCenterNotificationForMessage(chatID string, messageID string) error {
var tx *sql.Tx
var err error
tx, err = db.db.BeginTx(context.Background(), &sql.TxOptions{})
if err != nil {
return err
}
defer func() {
if err == nil {
err = tx.Commit()
return
}
// don't shadow original error
_ = tx.Rollback()
}()
_, notifications, err := db.buildActivityCenterQuery(tx, "", 0, nil, chatID)
if err != nil {
return err
}
var ids []types.HexBytes
for _, notification := range notifications {
if notification.Message != nil && notification.Message.ID == messageID {
ids = append(ids, notification.ID)
}
}
if len(ids) > 0 {
idsArgs := make([]interface{}, 0, len(ids))
for _, id := range ids {
idsArgs = append(idsArgs, id)
}
inVector := strings.Repeat("?, ", len(ids)-1) + "?"
query := "UPDATE activity_center_notifications SET dismissed = 1 WHERE id IN (" + inVector + ")" // nolint: gosec
_, err = tx.Exec(query, idsArgs...)
return err
}
return nil
}
func (db sqlitePersistence) SaveActivityCenterNotification(notification *ActivityCenterNotification) error {
var tx *sql.Tx
var err error
@ -143,10 +189,11 @@ func (db sqlitePersistence) unmarshalActivityCenterNotificationRows(rows *sql.Ro
return latestCursor, notifications, nil
}
func (db sqlitePersistence) buildActivityCenterQuery(tx *sql.Tx, cursor string, limit int, ids []types.HexBytes) (string, []*ActivityCenterNotification, error) {
func (db sqlitePersistence) buildActivityCenterQuery(tx *sql.Tx, cursor string, limit int, ids []types.HexBytes, chatID string) (string, []*ActivityCenterNotification, error) {
var args []interface{}
cursorWhere := ""
inQueryWhere := ""
inChatWhere := ""
if cursor != "" {
cursorWhere = "AND cursor <= ?" //nolint: goconst
args = append(args, cursor)
@ -162,6 +209,11 @@ func (db sqlitePersistence) buildActivityCenterQuery(tx *sql.Tx, cursor string,
}
if chatID != "" {
inChatWhere = "AND a.chat_id = ?" //nolint: goconst
args = append(args, chatID)
}
query := fmt.Sprintf( // nolint: gosec
`
SELECT
@ -185,7 +237,8 @@ func (db sqlitePersistence) buildActivityCenterQuery(tx *sql.Tx, cursor string,
WHERE NOT a.dismissed AND NOT a.accepted
%s
%s
ORDER BY cursor DESC`, cursorWhere, inQueryWhere)
%s
ORDER BY cursor DESC`, cursorWhere, inQueryWhere, inChatWhere)
if limit != 0 {
args = append(args, limit)
query += ` LIMIT ?`
@ -216,7 +269,7 @@ func (db sqlitePersistence) ActivityCenterNotifications(currCursor string, limit
_ = tx.Rollback()
}()
latestCursor, notifications, err := db.buildActivityCenterQuery(tx, currCursor, incrementedLimit, nil)
latestCursor, notifications, err := db.buildActivityCenterQuery(tx, currCursor, incrementedLimit, nil, "")
if err != nil {
return "", nil, err
}
@ -266,7 +319,7 @@ func (db sqlitePersistence) AcceptAllActivityCenterNotifications() ([]*ActivityC
_ = tx.Rollback()
}()
_, notifications, err := db.buildActivityCenterQuery(tx, "", 0, nil)
_, notifications, err := db.buildActivityCenterQuery(tx, "", 0, nil, "")
_, err = tx.Exec(`UPDATE activity_center_notifications SET accepted = 1 WHERE NOT accepted AND NOT dismissed`)
if err != nil {
@ -293,7 +346,7 @@ func (db sqlitePersistence) AcceptActivityCenterNotifications(ids []types.HexByt
_ = tx.Rollback()
}()
_, notifications, err := db.buildActivityCenterQuery(tx, "", 0, ids)
_, notifications, err := db.buildActivityCenterQuery(tx, "", 0, ids, "")
if err != nil {
return nil, err

View File

@ -598,6 +598,14 @@ func (m *Messenger) HandleDeleteMessage(state *ReceivedMessageState, deleteMessa
return err
}
m.logger.Debug("deleting activity center notification for message", zap.String("chatID", chat.ID), zap.String("messageID", deleteMessage.MessageId))
err = m.persistence.DeleteActivityCenterNotificationForMessage(chat.ID, deleteMessage.MessageId)
if err != nil {
m.logger.Warn("failed to delete notifications for deleted message", zap.Error(err))
return err
}
if chat.LastMessage != nil && chat.LastMessage.ID == originalMessage.ID {
if err := m.updateLastMessage(chat); err != nil {
return err