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
272
273
|
/*
GoToSocial
Copyright (C) 2021-2022 GoToSocial Authors admin@gotosocial.org
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package bundb
import (
"context"
"strings"
"github.com/superseriousbusiness/gotosocial/internal/cache"
"github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/internal/log"
"github.com/uptrace/bun"
"github.com/uptrace/bun/dialect"
)
type emojiDB struct {
conn *DBConn
cache *cache.EmojiCache
}
func (e *emojiDB) newEmojiQ(emoji *gtsmodel.Emoji) *bun.SelectQuery {
return e.conn.
NewSelect().
Model(emoji)
}
func (e *emojiDB) PutEmoji(ctx context.Context, emoji *gtsmodel.Emoji) db.Error {
if _, err := e.conn.NewInsert().Model(emoji).Exec(ctx); err != nil {
return e.conn.ProcessError(err)
}
e.cache.Put(emoji)
return nil
}
func (e *emojiDB) GetEmojis(ctx context.Context, domain string, includeDisabled bool, includeEnabled bool, shortcode string, maxShortcodeDomain string, minShortcodeDomain string, limit int) ([]*gtsmodel.Emoji, db.Error) {
emojiIDs := []string{}
subQuery := e.conn.
NewSelect().
ColumnExpr("? AS ?", bun.Ident("emoji.id"), bun.Ident("emoji_ids"))
// To ensure consistent ordering and make paging possible, we sort not by shortcode
// but by [shortcode]@[domain]. Because sqlite and postgres have different syntax
// for concatenation, that means we need to switch here. Depending on which driver
// is in use, query will look something like this (sqlite):
//
// SELECT
// "emoji"."id" AS "emoji_ids",
// lower("emoji"."shortcode" || '@' || COALESCE("emoji"."domain", '')) AS "shortcode_domain"
// FROM
// "emojis" AS "emoji"
// ORDER BY
// "shortcode_domain" ASC
//
// Or like this (postgres):
//
// SELECT
// "emoji"."id" AS "emoji_ids",
// LOWER(CONCAT("emoji"."shortcode", '@', COALESCE("emoji"."domain", ''))) AS "shortcode_domain"
// FROM
// "emojis" AS "emoji"
// ORDER BY
// "shortcode_domain" ASC
switch e.conn.Dialect().Name() {
case dialect.SQLite:
subQuery = subQuery.ColumnExpr("LOWER(? || ? || COALESCE(?, ?)) AS ?", bun.Ident("emoji.shortcode"), "@", bun.Ident("emoji.domain"), "", bun.Ident("shortcode_domain"))
case dialect.PG:
subQuery = subQuery.ColumnExpr("LOWER(CONCAT(?, ?, COALESCE(?, ?))) AS ?", bun.Ident("emoji.shortcode"), "@", bun.Ident("emoji.domain"), "", bun.Ident("shortcode_domain"))
default:
panic("db conn was neither pg not sqlite")
}
subQuery = subQuery.TableExpr("? AS ?", bun.Ident("emojis"), bun.Ident("emoji"))
if domain == "" {
subQuery = subQuery.Where("? IS NULL", bun.Ident("emoji.domain"))
} else if domain != db.EmojiAllDomains {
subQuery = subQuery.Where("? = ?", bun.Ident("emoji.domain"), domain)
}
switch {
case includeDisabled && !includeEnabled:
// show only disabled emojis
subQuery = subQuery.Where("? = ?", bun.Ident("emoji.disabled"), true)
case includeEnabled && !includeDisabled:
// show only enabled emojis
subQuery = subQuery.Where("? = ?", bun.Ident("emoji.disabled"), false)
default:
// show emojis regardless of emoji.disabled value
}
if shortcode != "" {
subQuery = subQuery.Where("LOWER(?) = LOWER(?)", bun.Ident("emoji.shortcode"), shortcode)
}
// assume we want to sort ASC (a-z) unless informed otherwise
order := "ASC"
if maxShortcodeDomain != "" {
subQuery = subQuery.Where("? > LOWER(?)", bun.Ident("shortcode_domain"), maxShortcodeDomain)
}
if minShortcodeDomain != "" {
subQuery = subQuery.Where("? < LOWER(?)", bun.Ident("shortcode_domain"), minShortcodeDomain)
// if we have a minShortcodeDomain we're paging upwards/backwards
order = "DESC"
}
subQuery = subQuery.Order("shortcode_domain " + order)
if limit > 0 {
subQuery = subQuery.Limit(limit)
}
// Wrap the subQuery in a query, since we don't need to select the shortcode_domain column.
//
// The final query will come out looking something like...
//
// SELECT
// "subquery"."emoji_ids"
// FROM (
// SELECT
// "emoji"."id" AS "emoji_ids",
// LOWER("emoji"."shortcode" || '@' || COALESCE("emoji"."domain", '')) AS "shortcode_domain"
// FROM
// "emojis" AS "emoji"
// ORDER BY
// "shortcode_domain" ASC
// ) AS "subquery"
if err := e.conn.
NewSelect().
Column("subquery.emoji_ids").
TableExpr("(?) AS ?", subQuery, bun.Ident("subquery")).
Scan(ctx, &emojiIDs); err != nil {
return nil, e.conn.ProcessError(err)
}
if order == "DESC" {
// Reverse the slice order so the caller still
// gets emojis in expected a-z alphabetical order.
//
// See https://github.com/golang/go/wiki/SliceTricks#reversing
for i := len(emojiIDs)/2 - 1; i >= 0; i-- {
opp := len(emojiIDs) - 1 - i
emojiIDs[i], emojiIDs[opp] = emojiIDs[opp], emojiIDs[i]
}
}
return e.emojisFromIDs(ctx, emojiIDs)
}
func (e *emojiDB) GetUseableEmojis(ctx context.Context) ([]*gtsmodel.Emoji, db.Error) {
emojiIDs := []string{}
q := e.conn.
NewSelect().
TableExpr("? AS ?", bun.Ident("emojis"), bun.Ident("emoji")).
Column("emoji.id").
Where("? = ?", bun.Ident("emoji.visible_in_picker"), true).
Where("? = ?", bun.Ident("emoji.disabled"), false).
Where("? IS NULL", bun.Ident("emoji.domain")).
Order("emoji.shortcode ASC")
if err := q.Scan(ctx, &emojiIDs); err != nil {
return nil, e.conn.ProcessError(err)
}
return e.emojisFromIDs(ctx, emojiIDs)
}
func (e *emojiDB) GetEmojiByID(ctx context.Context, id string) (*gtsmodel.Emoji, db.Error) {
return e.getEmoji(
ctx,
func() (*gtsmodel.Emoji, bool) {
return e.cache.GetByID(id)
},
func(emoji *gtsmodel.Emoji) error {
return e.newEmojiQ(emoji).Where("? = ?", bun.Ident("emoji.id"), id).Scan(ctx)
},
)
}
func (e *emojiDB) GetEmojiByURI(ctx context.Context, uri string) (*gtsmodel.Emoji, db.Error) {
return e.getEmoji(
ctx,
func() (*gtsmodel.Emoji, bool) {
return e.cache.GetByURI(uri)
},
func(emoji *gtsmodel.Emoji) error {
return e.newEmojiQ(emoji).Where("? = ?", bun.Ident("emoji.uri"), uri).Scan(ctx)
},
)
}
func (e *emojiDB) GetEmojiByShortcodeDomain(ctx context.Context, shortcode string, domain string) (*gtsmodel.Emoji, db.Error) {
return e.getEmoji(
ctx,
func() (*gtsmodel.Emoji, bool) {
return e.cache.GetByShortcodeDomain(shortcode, domain)
},
func(emoji *gtsmodel.Emoji) error {
q := e.newEmojiQ(emoji)
if domain != "" {
q = q.Where("? = ?", bun.Ident("emoji.shortcode"), shortcode)
q = q.Where("? = ?", bun.Ident("emoji.domain"), domain)
} else {
q = q.Where("? = ?", bun.Ident("emoji.shortcode"), strings.ToLower(shortcode))
q = q.Where("? IS NULL", bun.Ident("emoji.domain"))
}
return q.Scan(ctx)
},
)
}
func (e *emojiDB) getEmoji(ctx context.Context, cacheGet func() (*gtsmodel.Emoji, bool), dbQuery func(*gtsmodel.Emoji) error) (*gtsmodel.Emoji, db.Error) {
// Attempt to fetch cached emoji
emoji, cached := cacheGet()
if !cached {
emoji = >smodel.Emoji{}
// Not cached! Perform database query
err := dbQuery(emoji)
if err != nil {
return nil, e.conn.ProcessError(err)
}
// Place in the cache
e.cache.Put(emoji)
}
return emoji, nil
}
func (e *emojiDB) emojisFromIDs(ctx context.Context, emojiIDs []string) ([]*gtsmodel.Emoji, db.Error) {
// Catch case of no emojis early
if len(emojiIDs) == 0 {
return nil, db.ErrNoEntries
}
emojis := make([]*gtsmodel.Emoji, 0, len(emojiIDs))
for _, id := range emojiIDs {
emoji, err := e.GetEmojiByID(ctx, id)
if err != nil {
log.Errorf("emojisFromIDs: error getting emoji %q: %v", id, err)
}
emojis = append(emojis, emoji)
}
return emojis, nil
}
|