mirror of
https://github.com/mattermost/mattermost.git
synced 2026-04-26 00:33:23 -04:00
* Adding initial retry layer version * Some simplification around the generated code * Generating retry layer again * Improving naming generation in store generated layers * Address PR review comments * Updating store layers * Addressing PR review comments * fixing lint errors * Updating store layers * Adding license header * Applying the retry layer to the reaction_store * Regenerating retry layer
221 lines
6.1 KiB
Go
221 lines
6.1 KiB
Go
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
package sqlstore
|
|
|
|
import (
|
|
"github.com/mattermost/mattermost-server/v5/mlog"
|
|
"github.com/mattermost/mattermost-server/v5/model"
|
|
"github.com/mattermost/mattermost-server/v5/store"
|
|
|
|
"github.com/mattermost/gorp"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
type SqlReactionStore struct {
|
|
SqlStore
|
|
}
|
|
|
|
func newSqlReactionStore(sqlStore SqlStore) store.ReactionStore {
|
|
s := &SqlReactionStore{sqlStore}
|
|
|
|
for _, db := range sqlStore.GetAllConns() {
|
|
table := db.AddTableWithName(model.Reaction{}, "Reactions").SetKeys(false, "PostId", "UserId", "EmojiName")
|
|
table.ColMap("UserId").SetMaxSize(26)
|
|
table.ColMap("PostId").SetMaxSize(26)
|
|
table.ColMap("EmojiName").SetMaxSize(64)
|
|
}
|
|
|
|
return s
|
|
}
|
|
|
|
func (s *SqlReactionStore) Save(reaction *model.Reaction) (*model.Reaction, error) {
|
|
reaction.PreSave()
|
|
if err := reaction.IsValid(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
transaction, err := s.GetMaster().Begin()
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "begin_transaction")
|
|
}
|
|
defer finalizeTransaction(transaction)
|
|
err = saveReactionAndUpdatePost(transaction, reaction)
|
|
if err != nil {
|
|
// We don't consider duplicated save calls as an error
|
|
if !IsUniqueConstraintError(err, []string{"reactions_pkey", "PRIMARY"}) {
|
|
return nil, errors.Wrap(err, "failed while saving reaction or updating post")
|
|
}
|
|
} else {
|
|
if err := transaction.Commit(); err != nil {
|
|
return nil, errors.Wrap(err, "commit_transaction")
|
|
}
|
|
}
|
|
|
|
return reaction, nil
|
|
}
|
|
|
|
func (s *SqlReactionStore) Delete(reaction *model.Reaction) (*model.Reaction, error) {
|
|
transaction, err := s.GetMaster().Begin()
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "begin_transaction")
|
|
}
|
|
defer finalizeTransaction(transaction)
|
|
|
|
if err := deleteReactionAndUpdatePost(transaction, reaction); err != nil {
|
|
return nil, errors.Wrap(err, "deleteReactionAndUpdatePost")
|
|
}
|
|
|
|
if err := transaction.Commit(); err != nil {
|
|
return nil, errors.Wrap(err, "commit_transaction")
|
|
}
|
|
|
|
return reaction, nil
|
|
}
|
|
|
|
func (s *SqlReactionStore) GetForPost(postId string, allowFromCache bool) ([]*model.Reaction, error) {
|
|
var reactions []*model.Reaction
|
|
|
|
if _, err := s.GetReplica().Select(&reactions,
|
|
`SELECT
|
|
*
|
|
FROM
|
|
Reactions
|
|
WHERE
|
|
PostId = :PostId
|
|
ORDER BY
|
|
CreateAt`, map[string]interface{}{"PostId": postId}); err != nil {
|
|
return nil, errors.Wrapf(err, "failed to get Reactions with postId=%s", postId)
|
|
}
|
|
|
|
return reactions, nil
|
|
}
|
|
|
|
func (s *SqlReactionStore) BulkGetForPosts(postIds []string) ([]*model.Reaction, error) {
|
|
keys, params := MapStringsToQueryParams(postIds, "postId")
|
|
var reactions []*model.Reaction
|
|
|
|
if _, err := s.GetReplica().Select(&reactions, `SELECT
|
|
*
|
|
FROM
|
|
Reactions
|
|
WHERE
|
|
PostId IN `+keys+`
|
|
ORDER BY
|
|
CreateAt`, params); err != nil {
|
|
return nil, errors.Wrap(err, "failed to get Reactions")
|
|
}
|
|
return reactions, nil
|
|
}
|
|
|
|
func (s *SqlReactionStore) DeleteAllWithEmojiName(emojiName string) error {
|
|
var reactions []*model.Reaction
|
|
|
|
if _, err := s.GetReplica().Select(&reactions,
|
|
`SELECT
|
|
*
|
|
FROM
|
|
Reactions
|
|
WHERE
|
|
EmojiName = :EmojiName`, map[string]interface{}{"EmojiName": emojiName}); err != nil {
|
|
return errors.Wrapf(err, "failed to get Reactions with emojiName=%s", emojiName)
|
|
}
|
|
|
|
_, err := s.GetMaster().Exec(
|
|
`DELETE FROM
|
|
Reactions
|
|
WHERE
|
|
EmojiName = :EmojiName`, map[string]interface{}{"EmojiName": emojiName})
|
|
if err != nil {
|
|
return errors.Wrapf(err, "failed to delete Reactions with emojiName=%s", emojiName)
|
|
}
|
|
|
|
for _, reaction := range reactions {
|
|
reaction := reaction
|
|
_, err := s.GetMaster().Exec(UPDATE_POST_HAS_REACTIONS_ON_DELETE_QUERY,
|
|
map[string]interface{}{
|
|
"PostId": reaction.PostId,
|
|
"UpdateAt": model.GetMillis(),
|
|
})
|
|
if err != nil {
|
|
mlog.Warn("Unable to update Post.HasReactions while removing reactions",
|
|
mlog.String("post_id", reaction.PostId),
|
|
mlog.Err(err))
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *SqlReactionStore) PermanentDeleteBatch(endTime int64, limit int64) (int64, error) {
|
|
var query string
|
|
if s.DriverName() == "postgres" {
|
|
query = "DELETE from Reactions WHERE CreateAt = any (array (SELECT CreateAt FROM Reactions WHERE CreateAt < :EndTime LIMIT :Limit))"
|
|
} else {
|
|
query = "DELETE from Reactions WHERE CreateAt < :EndTime LIMIT :Limit"
|
|
}
|
|
|
|
sqlResult, err := s.GetMaster().Exec(query, map[string]interface{}{"EndTime": endTime, "Limit": limit})
|
|
if err != nil {
|
|
return 0, errors.Wrap(err, "failed to delete Reactions")
|
|
}
|
|
|
|
rowsAffected, err := sqlResult.RowsAffected()
|
|
if err != nil {
|
|
return 0, errors.Wrap(err, "unable to get rows affected for deleted Reactions")
|
|
}
|
|
return rowsAffected, nil
|
|
}
|
|
|
|
func saveReactionAndUpdatePost(transaction *gorp.Transaction, reaction *model.Reaction) error {
|
|
if err := transaction.Insert(reaction); err != nil {
|
|
return err
|
|
}
|
|
|
|
return updatePostForReactionsOnInsert(transaction, reaction.PostId)
|
|
}
|
|
|
|
func deleteReactionAndUpdatePost(transaction *gorp.Transaction, reaction *model.Reaction) error {
|
|
if _, err := transaction.Exec(
|
|
`DELETE FROM
|
|
Reactions
|
|
WHERE
|
|
PostId = :PostId AND
|
|
UserId = :UserId AND
|
|
EmojiName = :EmojiName`,
|
|
map[string]interface{}{"PostId": reaction.PostId, "UserId": reaction.UserId, "EmojiName": reaction.EmojiName}); err != nil {
|
|
return err
|
|
}
|
|
|
|
return updatePostForReactionsOnDelete(transaction, reaction.PostId)
|
|
}
|
|
|
|
const (
|
|
UPDATE_POST_HAS_REACTIONS_ON_DELETE_QUERY = `UPDATE
|
|
Posts
|
|
SET
|
|
UpdateAt = :UpdateAt,
|
|
HasReactions = (SELECT count(0) > 0 FROM Reactions WHERE PostId = :PostId)
|
|
WHERE
|
|
Id = :PostId`
|
|
)
|
|
|
|
func updatePostForReactionsOnDelete(transaction *gorp.Transaction, postId string) error {
|
|
updateAt := model.GetMillis()
|
|
_, err := transaction.Exec(UPDATE_POST_HAS_REACTIONS_ON_DELETE_QUERY, map[string]interface{}{"PostId": postId, "UpdateAt": updateAt})
|
|
return err
|
|
}
|
|
|
|
func updatePostForReactionsOnInsert(transaction *gorp.Transaction, postId string) error {
|
|
_, err := transaction.Exec(
|
|
`UPDATE
|
|
Posts
|
|
SET
|
|
HasReactions = True,
|
|
UpdateAt = :UpdateAt
|
|
WHERE
|
|
Id = :PostId`,
|
|
map[string]interface{}{"PostId": postId, "UpdateAt": model.GetMillis()})
|
|
|
|
return err
|
|
}
|