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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
|
// GoToSocial
// Copyright (C) GoToSocial Authors admin@gotosocial.org
// SPDX-License-Identifier: AGPL-3.0-or-later
//
// 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 admin
import (
"context"
"errors"
"fmt"
"io"
"mime/multipart"
"strings"
"codeberg.org/gruf/go-iotools"
apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
"github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/internal/id"
"github.com/superseriousbusiness/gotosocial/internal/media"
"github.com/superseriousbusiness/gotosocial/internal/util"
)
// EmojiCreate creates a custom emoji on this instance.
func (p *Processor) EmojiCreate(
ctx context.Context,
account *gtsmodel.Account,
form *apimodel.EmojiCreateRequest,
) (*apimodel.Emoji, gtserror.WithCode) {
// Get maximum supported local emoji size.
maxsz := config.GetMediaEmojiLocalMaxSize()
maxszInt64 := int64(maxsz) // #nosec G115 -- Already validated.
// Ensure media within size bounds.
if form.Image.Size > maxszInt64 {
text := fmt.Sprintf("emoji exceeds configured max size: %s", maxsz)
return nil, gtserror.NewErrorBadRequest(errors.New(text), text)
}
// Open multipart file reader.
mpfile, err := form.Image.Open()
if err != nil {
err := gtserror.Newf("error opening multipart file: %w", err)
return nil, gtserror.NewErrorInternalError(err)
}
// Wrap the multipart file reader to ensure is limited to max.
rc, _, _ := iotools.UpdateReadCloserLimit(mpfile, maxszInt64)
data := func(context.Context) (io.ReadCloser, error) {
return rc, nil
}
// Attempt to create the new local emoji.
emoji, errWithCode := p.createEmoji(ctx,
form.Shortcode,
form.CategoryName,
data,
)
if errWithCode != nil {
return nil, errWithCode
}
apiEmoji, err := p.converter.EmojiToAPIEmoji(ctx, emoji)
if err != nil {
err := gtserror.Newf("error converting emoji: %w", err)
return nil, gtserror.NewErrorInternalError(err)
}
return &apiEmoji, nil
}
// EmojisGet returns an admin view of custom
// emojis, filtered with the given parameters.
func (p *Processor) EmojisGet(
ctx context.Context,
account *gtsmodel.Account,
domain string,
includeDisabled bool,
includeEnabled bool,
shortcode string,
maxShortcodeDomain string,
minShortcodeDomain string,
limit int,
) (*apimodel.PageableResponse, gtserror.WithCode) {
emojis, err := p.state.DB.GetEmojisBy(ctx,
domain,
includeDisabled,
includeEnabled,
shortcode,
maxShortcodeDomain,
minShortcodeDomain,
limit,
)
if err != nil && !errors.Is(err, db.ErrNoEntries) {
err := gtserror.Newf("db error: %w", err)
return nil, gtserror.NewErrorInternalError(err)
}
count := len(emojis)
if count == 0 {
return util.EmptyPageableResponse(), nil
}
items := make([]interface{}, 0, count)
for _, emoji := range emojis {
adminEmoji, err := p.converter.EmojiToAdminAPIEmoji(ctx, emoji)
if err != nil {
err := gtserror.Newf("error converting emoji to admin model emoji: %w", err)
return nil, gtserror.NewErrorInternalError(err)
}
items = append(items, adminEmoji)
}
return util.PackagePageableResponse(util.PageableResponseParams{
Items: items,
Path: "api/v1/admin/custom_emojis",
NextMaxIDKey: "max_shortcode_domain",
NextMaxIDValue: emojis[count-1].ShortcodeDomain(),
PrevMinIDKey: "min_shortcode_domain",
PrevMinIDValue: emojis[0].ShortcodeDomain(),
Limit: limit,
ExtraQueryParams: []string{
emojisGetFilterParams(
shortcode,
domain,
includeDisabled,
includeEnabled,
),
},
})
}
// EmojiGet returns the admin view of
// one custom emoji with the given id.
func (p *Processor) EmojiGet(
ctx context.Context,
account *gtsmodel.Account,
id string,
) (*apimodel.AdminEmoji, gtserror.WithCode) {
emoji, err := p.state.DB.GetEmojiByID(ctx, id)
if err != nil && !errors.Is(err, db.ErrNoEntries) {
err := gtserror.Newf("db error: %w", err)
return nil, gtserror.NewErrorInternalError(err)
}
if emoji == nil {
err := gtserror.Newf("no emoji with id %s found in the db", id)
return nil, gtserror.NewErrorNotFound(err)
}
adminEmoji, err := p.converter.EmojiToAdminAPIEmoji(ctx, emoji)
if err != nil {
err := gtserror.Newf("error converting emoji to admin api emoji: %w", err)
return nil, gtserror.NewErrorInternalError(err)
}
return adminEmoji, nil
}
// EmojiDelete deletes one *local* emoji
// from the database, with the given id.
func (p *Processor) EmojiDelete(
ctx context.Context,
id string,
) (*apimodel.AdminEmoji, gtserror.WithCode) {
emoji, err := p.state.DB.GetEmojiByID(ctx, id)
if err != nil && !errors.Is(err, db.ErrNoEntries) {
err := gtserror.Newf("db error: %w", err)
return nil, gtserror.NewErrorInternalError(err)
}
if emoji == nil {
err := gtserror.Newf("no emoji with id %s found in the db", id)
return nil, gtserror.NewErrorNotFound(err)
}
if !emoji.IsLocal() {
err := fmt.Errorf("emoji with id %s was not a local emoji, will not delete", id)
return nil, gtserror.NewErrorBadRequest(err, err.Error())
}
// Convert to admin emoji before deletion,
// so we can return the deleted emoji.
adminEmoji, err := p.converter.EmojiToAdminAPIEmoji(ctx, emoji)
if err != nil {
err := gtserror.Newf("error converting emoji to admin api emoji: %w", err)
return nil, gtserror.NewErrorInternalError(err)
}
if err := p.state.DB.DeleteEmojiByID(ctx, id); err != nil {
err := gtserror.Newf("db error deleting emoji %s: %w", id, err)
return nil, gtserror.NewErrorInternalError(err)
}
return adminEmoji, nil
}
// EmojiUpdate updates one emoji with the
// given id, using the provided form parameters.
func (p *Processor) EmojiUpdate(
ctx context.Context,
emojiID string,
form *apimodel.EmojiUpdateRequest,
) (*apimodel.AdminEmoji, gtserror.WithCode) {
// Get the emoji with given ID from the database.
emoji, err := p.state.DB.GetEmojiByID(ctx, emojiID)
if err != nil && !errors.Is(err, db.ErrNoEntries) {
err := gtserror.Newf("error fetching emoji from db: %w", err)
return nil, gtserror.NewErrorInternalError(err)
}
// Check found.
if emoji == nil {
const text = "emoji not found"
return nil, gtserror.NewErrorNotFound(errors.New(text), text)
}
switch form.Type {
case apimodel.EmojiUpdateCopy:
return p.emojiUpdateCopy(ctx, emoji, form.Shortcode, form.CategoryName)
case apimodel.EmojiUpdateDisable:
return p.emojiUpdateDisable(ctx, emoji)
case apimodel.EmojiUpdateModify:
return p.emojiUpdateModify(ctx, emoji, form.Image, form.CategoryName)
default:
const text = "unrecognized emoji update action type"
return nil, gtserror.NewErrorBadRequest(errors.New(text), text)
}
}
// EmojiCategoriesGet returns all custom emoji
// categories that exist on this instance.
func (p *Processor) EmojiCategoriesGet(
ctx context.Context,
) ([]*apimodel.EmojiCategory, gtserror.WithCode) {
categories, err := p.state.DB.GetEmojiCategories(ctx)
if err != nil {
err := gtserror.Newf("db error getting emoji categories: %w", err)
return nil, gtserror.NewErrorInternalError(err)
}
apiCategories := make([]*apimodel.EmojiCategory, 0, len(categories))
for _, category := range categories {
apiCategory, err := p.converter.EmojiCategoryToAPIEmojiCategory(ctx, category)
if err != nil {
err := gtserror.Newf("error converting emoji category to api emoji category: %w", err)
return nil, gtserror.NewErrorInternalError(err)
}
apiCategories = append(apiCategories, apiCategory)
}
return apiCategories, nil
}
// emojiUpdateCopy copies and stores the given
// *remote* emoji as a *local* emoji, preserving
// the same image, and using the provided shortcode.
//
// The provided emoji model must correspond to an
// emoji already stored in the database + storage.
func (p *Processor) emojiUpdateCopy(
ctx context.Context,
target *gtsmodel.Emoji,
shortcode *string,
categoryName *string,
) (*apimodel.AdminEmoji, gtserror.WithCode) {
if target.IsLocal() {
const text = "target emoji is not remote; cannot copy to local"
return nil, gtserror.NewErrorBadRequest(errors.New(text), text)
}
// Ensure target emoji is locally cached.
target, err := p.federator.RecacheEmoji(ctx,
target,
)
if err != nil {
err := gtserror.Newf("error recaching emoji %s: %w", target.ImageRemoteURL, err)
return nil, gtserror.NewErrorNotFound(err)
}
// Get maximum supported local emoji size.
maxsz := config.GetMediaEmojiLocalMaxSize()
maxszInt := int(maxsz) // #nosec G115 -- Already validated.
// Ensure target emoji image within size bounds.
if target.ImageFileSize > maxszInt {
text := fmt.Sprintf("emoji exceeds configured max size: %s", maxsz)
return nil, gtserror.NewErrorBadRequest(errors.New(text), text)
}
// Data function for copying just streams media
// out of storage into an additional location.
//
// This means that data for the copy persists even
// if the remote copied emoji gets deleted at some point.
data := func(ctx context.Context) (io.ReadCloser, error) {
rc, err := p.state.Storage.GetStream(ctx, target.ImagePath)
return rc, err
}
// Attempt to create the new local emoji.
emoji, errWithCode := p.createEmoji(ctx,
util.PtrOrZero(shortcode),
util.PtrOrZero(categoryName),
data,
)
if errWithCode != nil {
return nil, errWithCode
}
apiEmoji, err := p.converter.EmojiToAdminAPIEmoji(ctx, emoji)
if err != nil {
err := gtserror.Newf("error converting emoji: %w", err)
return nil, gtserror.NewErrorInternalError(err)
}
return apiEmoji, nil
}
// emojiUpdateDisable marks the given *remote*
// emoji as disabled by setting disabled = true.
//
// The provided emoji model must correspond to an
// emoji already stored in the database + storage.
func (p *Processor) emojiUpdateDisable(
ctx context.Context,
emoji *gtsmodel.Emoji,
) (*apimodel.AdminEmoji, gtserror.WithCode) {
if emoji.IsLocal() {
err := fmt.Errorf("emoji %s is not a remote emoji, cannot disable it via this endpoint", emoji.ID)
return nil, gtserror.NewErrorBadRequest(err, err.Error())
}
// Only bother with a db call
// if emoji not already disabled.
if !*emoji.Disabled {
emoji.Disabled = util.Ptr(true)
if err := p.state.DB.UpdateEmoji(ctx, emoji, "disabled"); err != nil {
err := gtserror.Newf("db error updating emoji %s: %w", emoji.ID, err)
return nil, gtserror.NewErrorInternalError(err)
}
}
adminEmoji, err := p.converter.EmojiToAdminAPIEmoji(ctx, emoji)
if err != nil {
err := gtserror.Newf("error converting emoji: %w", err)
return nil, gtserror.NewErrorInternalError(err)
}
return adminEmoji, nil
}
// emojiUpdateModify modifies the given *local* emoji.
//
// Either one of image or category must be non-nil,
// otherwise there's nothing to modify. If category
// is non-nil and dereferences to an empty string,
// category will be cleared.
//
// The provided emoji model must correspond to an
// emoji already stored in the database + storage.
func (p *Processor) emojiUpdateModify(
ctx context.Context,
emoji *gtsmodel.Emoji,
image *multipart.FileHeader,
categoryName *string,
) (*apimodel.AdminEmoji, gtserror.WithCode) {
if !emoji.IsLocal() {
const text = "cannot modify remote emoji"
return nil, gtserror.NewErrorBadRequest(errors.New(text), text)
}
// Ensure there's actually something to update.
if image == nil && categoryName == nil {
const text = "no changes were provided"
return nil, gtserror.NewErrorBadRequest(errors.New(text), text)
}
// Check if we need to
// set a new category ID.
var newCategoryID *string
switch {
case categoryName == nil:
// No changes.
case *categoryName == "":
// Emoji category was unset.
newCategoryID = util.Ptr("")
emoji.CategoryID = ""
emoji.Category = nil
case *categoryName != "":
// A category was provided, get or create relevant emoji category.
category, errWithCode := p.mustGetEmojiCategory(ctx, *categoryName)
if errWithCode != nil {
return nil, errWithCode
}
// Update emoji category if
// it's different from before.
if category.ID != emoji.CategoryID {
newCategoryID = &category.ID
emoji.CategoryID = category.ID
emoji.Category = category
}
}
// Check whether any image changes were requested.
imageUpdated := (image != nil && image.Size > 0)
if !imageUpdated && newCategoryID != nil {
// Only updating category; only a single database update required.
if err := p.state.DB.UpdateEmoji(ctx, emoji, "category_id"); err != nil {
err := gtserror.Newf("error updating emoji in db: %w", err)
return nil, gtserror.NewErrorInternalError(err)
}
} else if imageUpdated {
var err error
// Updating image and maybe categoryID.
// We can do both at the same time :)
// Get maximum supported local emoji size.
maxsz := config.GetMediaEmojiLocalMaxSize()
maxszInt64 := int64(maxsz) // #nosec G115 -- Already validated.
// Ensure media within size bounds.
if image.Size > maxszInt64 {
text := fmt.Sprintf("emoji exceeds configured max size: %s", maxsz)
return nil, gtserror.NewErrorBadRequest(errors.New(text), text)
}
// Open multipart file reader.
mpfile, err := image.Open()
if err != nil {
err := gtserror.Newf("error opening multipart file: %w", err)
return nil, gtserror.NewErrorInternalError(err)
}
// Wrap the multipart file reader to ensure is limited to max.
rc, _, _ := iotools.UpdateReadCloserLimit(mpfile, int64(maxsz)) // #nosec G115 -- Already validated.
data := func(context.Context) (io.ReadCloser, error) {
return rc, nil
}
// Include category ID
// update if necessary.
ai := media.AdditionalEmojiInfo{}
ai.CategoryID = newCategoryID
// Prepare emoji model for update+recache from new data.
processing, err := p.media.UpdateEmoji(ctx, emoji, data, ai)
if err != nil {
err := gtserror.Newf("error preparing recache: %w", err)
return nil, gtserror.NewErrorInternalError(err)
}
// Load to trigger update + write.
emoji, err = processing.Load(ctx)
if err != nil {
err := gtserror.Newf("error processing emoji %s: %w", emoji.Shortcode, err)
return nil, gtserror.NewErrorInternalError(err)
}
}
adminEmoji, err := p.converter.EmojiToAdminAPIEmoji(ctx, emoji)
if err != nil {
err := gtserror.Newf("error converting emoji: %w", err)
return nil, gtserror.NewErrorInternalError(err)
}
return adminEmoji, nil
}
// createEmoji will create a new local emoji
// with the given shortcode, attached category
// name (if any) and data source function.
func (p *Processor) createEmoji(
ctx context.Context,
shortcode string,
categoryName string,
data media.DataFunc,
) (
*gtsmodel.Emoji,
gtserror.WithCode,
) {
// Validate shortcode.
if shortcode == "" {
const text = "empty shortcode name"
return nil, gtserror.NewErrorBadRequest(errors.New(text), text)
}
// Look for an existing local emoji with shortcode to ensure this is new.
existing, err := p.state.DB.GetEmojiByShortcodeDomain(ctx, shortcode, "")
if err != nil && !errors.Is(err, db.ErrNoEntries) {
err := gtserror.Newf("error fetching emoji from db: %w", err)
return nil, gtserror.NewErrorInternalError(err)
} else if existing != nil {
const text = "emoji with shortcode already exists"
return nil, gtserror.NewErrorConflict(errors.New(text), text)
}
var categoryID *string
if categoryName != "" {
// A category was provided, get / create relevant emoji category.
category, errWithCode := p.mustGetEmojiCategory(ctx, categoryName)
if errWithCode != nil {
return nil, errWithCode
}
// Set category ID for emoji.
categoryID = &category.ID
}
// Store to instance storage.
return p.c.StoreLocalEmoji(
ctx,
shortcode,
data,
media.AdditionalEmojiInfo{
CategoryID: categoryID,
},
)
}
// mustGetEmojiCategory either gets an existing
// category with the given name from the database,
// or, if the category doesn't yet exist, it creates
// the category and then returns it.
func (p *Processor) mustGetEmojiCategory(
ctx context.Context,
name string,
) (
*gtsmodel.EmojiCategory,
gtserror.WithCode,
) {
// Look for an existing emoji category with name.
category, err := p.state.DB.GetEmojiCategoryByName(ctx, name)
if err != nil && !errors.Is(err, db.ErrNoEntries) {
err := gtserror.Newf("error fetching emoji category from db: %w", err)
return nil, gtserror.NewErrorInternalError(err)
}
if category != nil {
// We had it already.
return category, nil
}
// Create new ID.
id := id.NewULID()
// Prepare new category for insertion.
category = >smodel.EmojiCategory{
ID: id,
Name: name,
}
// Insert new category into the database.
err = p.state.DB.PutEmojiCategory(ctx, category)
if err != nil {
err := gtserror.Newf("error inserting emoji category into db: %w", err)
return nil, gtserror.NewErrorInternalError(err)
}
return category, nil
}
// emojisGetFilterParams builds extra
// query parameters to return as part
// of an Emojis pageable response.
//
// The returned string will look like:
//
// "filter=domain:all,enabled,shortcode:example"
func emojisGetFilterParams(
shortcode string,
domain string,
includeDisabled bool,
includeEnabled bool,
) string {
var filterBuilder strings.Builder
filterBuilder.WriteString("filter=")
switch domain {
case "", "local":
// Local emojis only.
filterBuilder.WriteString("domain:local")
case db.EmojiAllDomains:
// Local or remote.
filterBuilder.WriteString("domain:all")
default:
// Specific domain only.
filterBuilder.WriteString("domain:" + domain)
}
if includeDisabled != includeEnabled {
if includeDisabled {
filterBuilder.WriteString(",disabled")
}
if includeEnabled {
filterBuilder.WriteString(",enabled")
}
}
if shortcode != "" {
// Specific shortcode only.
filterBuilder.WriteString(",shortcode:" + shortcode)
}
return filterBuilder.String()
}
|