summaryrefslogtreecommitdiffstats
path: root/store/sql_reaction_store.go
blob: 0d94a500fa9a20672d58c51ac2c8da184d1c40f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

package store

import (
	"github.com/mattermost/platform/einterfaces"
	"github.com/mattermost/platform/model"
	"github.com/mattermost/platform/utils"

	l4g "github.com/alecthomas/log4go"
	"github.com/go-gorp/gorp"
)

const (
	REACTION_CACHE_SIZE = 20000
	REACTION_CACHE_SEC  = 1800 // 30 minutes
)

var reactionCache *utils.Cache = utils.NewLru(REACTION_CACHE_SIZE)

type SqlReactionStore struct {
	*SqlStore
}

func NewSqlReactionStore(sqlStore *SqlStore) ReactionStore {
	s := &SqlReactionStore{sqlStore}

	for _, db := range sqlStore.GetAllConns() {
		table := db.AddTableWithName(model.Reaction{}, "Reactions").SetKeys(false, "UserId", "PostId", "EmojiName")
		table.ColMap("UserId").SetMaxSize(26)
		table.ColMap("PostId").SetMaxSize(26)
		table.ColMap("EmojiName").SetMaxSize(64)
	}

	return s
}

func (s SqlReactionStore) CreateIndexesIfNotExists() {
	s.CreateIndexIfNotExists("idx_reactions_post_id", "Reactions", "PostId")
	s.CreateIndexIfNotExists("idx_reactions_user_id", "Reactions", "UserId")
	s.CreateIndexIfNotExists("idx_reactions_emoji_name", "Reactions", "EmojiName")
}

func (s SqlReactionStore) Save(reaction *model.Reaction) StoreChannel {
	storeChannel := make(StoreChannel)

	go func() {
		result := StoreResult{}

		reaction.PreSave()
		if result.Err = reaction.IsValid(); result.Err != nil {
			storeChannel <- result
			close(storeChannel)
			return
		}

		if transaction, err := s.GetMaster().Begin(); err != nil {
			result.Err = model.NewLocAppError("SqlReactionStore.Save", "store.sql_reaction.save.begin.app_error", nil, err.Error())
		} else {
			err := saveReactionAndUpdatePost(transaction, reaction)

			if err != nil {
				transaction.Rollback()

				// We don't consider duplicated save calls as an error
				if !IsUniqueConstraintError(err.Error(), []string{"reactions_pkey", "PRIMARY"}) {
					result.Err = model.NewLocAppError("SqlPreferenceStore.Save", "store.sql_reaction.save.save.app_error", nil, err.Error())
				}
			} else {
				if err := transaction.Commit(); err != nil {
					// don't need to rollback here since the transaction is already closed
					result.Err = model.NewLocAppError("SqlPreferenceStore.Save", "store.sql_reaction.save.commit.app_error", nil, err.Error())
				}
			}

			if result.Err == nil {
				result.Data = reaction
			}
		}

		storeChannel <- result
		close(storeChannel)
	}()

	return storeChannel
}

func (s SqlReactionStore) Delete(reaction *model.Reaction) StoreChannel {
	storeChannel := make(StoreChannel)

	go func() {
		result := StoreResult{}

		if transaction, err := s.GetMaster().Begin(); err != nil {
			result.Err = model.NewLocAppError("SqlReactionStore.Delete", "store.sql_reaction.delete.begin.app_error", nil, err.Error())
		} else {
			err := deleteReactionAndUpdatePost(transaction, reaction)

			if err != nil {
				transaction.Rollback()

				result.Err = model.NewLocAppError("SqlPreferenceStore.Delete", "store.sql_reaction.delete.app_error", nil, err.Error())
			} else if err := transaction.Commit(); err != nil {
				// don't need to rollback here since the transaction is already closed
				result.Err = model.NewLocAppError("SqlPreferenceStore.Delete", "store.sql_reaction.delete.commit.app_error", nil, err.Error())
			} else {
				result.Data = reaction
			}
		}

		storeChannel <- result
		close(storeChannel)
	}()

	return storeChannel
}

func saveReactionAndUpdatePost(transaction *gorp.Transaction, reaction *model.Reaction) error {
	if err := transaction.Insert(reaction); err != nil {
		return err
	}

	return updatePostForReactions(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 updatePostForReactions(transaction, reaction.PostId)
}

const (
	// Set HasReactions = true if and only if the post has reactions, update UpdateAt only if HasReactions changes
	UPDATE_POST_HAS_REACTIONS_QUERY = `UPDATE
			Posts
		SET
			UpdateAt = (CASE
				WHEN HasReactions != (SELECT count(0) > 0 FROM Reactions WHERE PostId = :PostId) THEN :UpdateAt
				ELSE UpdateAt
			END),
			HasReactions = (SELECT count(0) > 0 FROM Reactions WHERE PostId = :PostId)
		WHERE
			Id = :PostId`
)

func updatePostForReactions(transaction *gorp.Transaction, postId string) error {
	_, err := transaction.Exec(UPDATE_POST_HAS_REACTIONS_QUERY, map[string]interface{}{"PostId": postId, "UpdateAt": model.GetMillis()})

	return err
}

func (s SqlReactionStore) InvalidateCacheForPost(postId string) {
	reactionCache.Remove(postId)
}

func (s SqlReactionStore) InvalidateCache() {
	reactionCache.Purge()
}

func (s SqlReactionStore) GetForPost(postId string, allowFromCache bool) StoreChannel {
	storeChannel := make(StoreChannel)

	go func() {
		result := StoreResult{}
		metrics := einterfaces.GetMetricsInterface()

		if allowFromCache {
			if cacheItem, ok := reactionCache.Get(postId); ok {
				if metrics != nil {
					metrics.IncrementMemCacheHitCounter("Reactions")
				}
				result.Data = cacheItem.([]*model.Reaction)
				storeChannel <- result
				close(storeChannel)
				return
			} else {
				if metrics != nil {
					metrics.IncrementMemCacheMissCounter("Reactions")
				}
			}
		} else {
			if metrics != nil {
				metrics.IncrementMemCacheMissCounter("Reactions")
			}
		}

		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 {
			result.Err = model.NewLocAppError("SqlReactionStore.GetForPost", "store.sql_reaction.get_for_post.app_error", nil, "")
		} else {
			result.Data = reactions

			reactionCache.AddWithExpiresInSecs(postId, reactions, REACTION_CACHE_SEC)
		}

		storeChannel <- result
		close(storeChannel)
	}()

	return storeChannel
}

func (s SqlReactionStore) DeleteAllWithEmojiName(emojiName string) StoreChannel {
	storeChannel := make(StoreChannel)

	go func() {
		result := StoreResult{}

		// doesn't use a transaction since it's better for this to half-finish than to not commit anything
		var reactions []*model.Reaction

		if _, err := s.GetReplica().Select(&reactions,
			`SELECT
				*
			FROM
				Reactions
			WHERE
				EmojiName = :EmojiName`, map[string]interface{}{"EmojiName": emojiName}); err != nil {
			result.Err = model.NewLocAppError("SqlReactionStore.DeleteAllWithEmojiName",
				"store.sql_reaction.delete_all_with_emoji_name.get_reactions.app_error", nil,
				"emoji_name="+emojiName+", error="+err.Error())
			storeChannel <- result
			close(storeChannel)
			return
		}

		if _, err := s.GetMaster().Exec(
			`DELETE FROM
				Reactions
			WHERE
				EmojiName = :EmojiName`, map[string]interface{}{"EmojiName": emojiName}); err != nil {
			result.Err = model.NewLocAppError("SqlReactionStore.DeleteAllWithEmojiName",
				"store.sql_reaction.delete_all_with_emoji_name.delete_reactions.app_error", nil,
				"emoji_name="+emojiName+", error="+err.Error())
			storeChannel <- result
			close(storeChannel)
			return
		}

		for _, reaction := range reactions {
			if _, err := s.GetMaster().Exec(UPDATE_POST_HAS_REACTIONS_QUERY,
				map[string]interface{}{"PostId": reaction.PostId, "UpdateAt": model.GetMillis()}); err != nil {
				l4g.Warn(utils.T("store.sql_reaction.delete_all_with_emoji_name.update_post.warn"), reaction.PostId, err.Error())
			}
		}

		storeChannel <- result
		close(storeChannel)
	}()

	return storeChannel
}