diff --git a/store/sqlstore/post_store.go b/store/sqlstore/post_store.go index 5f05eb0d59..19176ebf99 100644 --- a/store/sqlstore/post_store.go +++ b/store/sqlstore/post_store.go @@ -6,7 +6,6 @@ package sqlstore import ( "context" "database/sql" - "encoding/json" "fmt" "reflect" "regexp" @@ -2411,27 +2410,15 @@ func (s *SqlPostStore) cleanupThreads(postId, rootId string, permanent bool, use updateQuery := s.getQueryBuilder().Update("Threads") if count == 0 { - var participants model.StringArray - err = s.getQueryBuilder(). - Select("Participants"). - From("Threads"). - Where(sq.Eq{"PostId": rootId}). - RunWith(s.GetReplica()). - QueryRow(). - Scan(&participants) - - if err != nil { - return errors.Wrap(err, "failed getting thread participants") - } - - if participants.Contains(userId) { - participants = participants.Remove(userId) - var participantsJSON []byte - participantsJSON, err = json.Marshal(participants) - if err != nil { - return errors.Wrap(err, "failed marshalling thread participants") - } - updateQuery = updateQuery.Set("Participants", string(participantsJSON)) + if s.DriverName() == model.DatabaseDriverPostgres { + updateQuery = updateQuery.Set("Participants", sq.Expr("Participants - ?", userId)) + } else { + // The .Where is because JSON_REMOVE returns null if the element to remove wasn't present + updateQuery = updateQuery. + Set("Participants", sq.Expr( + `JSON_REMOVE(Participants, JSON_UNQUOTE(JSON_SEARCH(Participants, 'one', ?)))`, userId, + )). + Where(sq.Expr(`JSON_CONTAINS(Participants, ?)`, strconv.Quote(userId))) } }