| 
									
										
										
										
											2023-03-12 16:00:57 +01:00
										 |  |  | // 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/>. | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | package admin | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"context" | 
					
						
							|  |  |  | 	"errors" | 
					
						
							|  |  |  | 	"fmt" | 
					
						
							|  |  |  | 	"io" | 
					
						
							|  |  |  | 	"mime/multipart" | 
					
						
							|  |  |  | 	"strings" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-12 09:39:47 +00:00
										 |  |  | 	"codeberg.org/gruf/go-bytesize" | 
					
						
							|  |  |  | 	"codeberg.org/gruf/go-iotools" | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 	apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" | 
					
						
							| 
									
										
										
										
											2024-07-12 09:39:47 +00:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/config" | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 	"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. | 
					
						
							| 
									
										
										
										
											2024-01-29 15:57:22 +01:00
										 |  |  | func (p *Processor) EmojiCreate( | 
					
						
							|  |  |  | 	ctx context.Context, | 
					
						
							|  |  |  | 	account *gtsmodel.Account, | 
					
						
							|  |  |  | 	form *apimodel.EmojiCreateRequest, | 
					
						
							|  |  |  | ) (*apimodel.Emoji, gtserror.WithCode) { | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-12 09:39:47 +00:00
										 |  |  | 	// Get maximum supported local emoji size. | 
					
						
							|  |  |  | 	maxsz := config.GetMediaEmojiLocalMaxSize() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Ensure media within size bounds. | 
					
						
							|  |  |  | 	if form.Image.Size > int64(maxsz) { | 
					
						
							|  |  |  | 		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, int64(maxsz)) | 
					
						
							|  |  |  | 	data := func(context.Context) (io.ReadCloser, error) { | 
					
						
							|  |  |  | 		return rc, nil | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-26 15:01:16 +00:00
										 |  |  | 	// Attempt to create the new local emoji. | 
					
						
							|  |  |  | 	emoji, errWithCode := p.createEmoji(ctx, | 
					
						
							|  |  |  | 		form.Shortcode, | 
					
						
							|  |  |  | 		form.CategoryName, | 
					
						
							|  |  |  | 		data, | 
					
						
							| 
									
										
										
										
											2024-01-29 15:57:22 +01:00
										 |  |  | 	) | 
					
						
							| 
									
										
										
										
											2024-06-26 15:01:16 +00:00
										 |  |  | 	if errWithCode != nil { | 
					
						
							|  |  |  | 		return nil, errWithCode | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-23 17:44:11 +01:00
										 |  |  | 	apiEmoji, err := p.converter.EmojiToAPIEmoji(ctx, emoji) | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2024-01-29 15:57:22 +01:00
										 |  |  | 		err := gtserror.Newf("error converting emoji: %w", err) | 
					
						
							|  |  |  | 		return nil, gtserror.NewErrorInternalError(err) | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return &apiEmoji, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-29 15:57:22 +01:00
										 |  |  | // EmojisGet returns an admin view of custom | 
					
						
							|  |  |  | // emojis, filtered with the given parameters. | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 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) { | 
					
						
							| 
									
										
										
										
											2024-01-29 15:57:22 +01:00
										 |  |  | 	emojis, err := p.state.DB.GetEmojisBy(ctx, | 
					
						
							|  |  |  | 		domain, | 
					
						
							|  |  |  | 		includeDisabled, | 
					
						
							|  |  |  | 		includeEnabled, | 
					
						
							|  |  |  | 		shortcode, | 
					
						
							|  |  |  | 		maxShortcodeDomain, | 
					
						
							|  |  |  | 		minShortcodeDomain, | 
					
						
							|  |  |  | 		limit, | 
					
						
							|  |  |  | 	) | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 	if err != nil && !errors.Is(err, db.ErrNoEntries) { | 
					
						
							| 
									
										
										
										
											2024-01-29 15:57:22 +01:00
										 |  |  | 		err := gtserror.Newf("db error: %w", err) | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 		return nil, gtserror.NewErrorInternalError(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	count := len(emojis) | 
					
						
							|  |  |  | 	if count == 0 { | 
					
						
							|  |  |  | 		return util.EmptyPageableResponse(), nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	items := make([]interface{}, 0, count) | 
					
						
							|  |  |  | 	for _, emoji := range emojis { | 
					
						
							| 
									
										
										
										
											2023-09-23 17:44:11 +01:00
										 |  |  | 		adminEmoji, err := p.converter.EmojiToAdminAPIEmoji(ctx, emoji) | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2024-01-29 15:57:22 +01:00
										 |  |  | 			err := gtserror.Newf("error converting emoji to admin model emoji: %w", err) | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 			return nil, gtserror.NewErrorInternalError(err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		items = append(items, adminEmoji) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return util.PackagePageableResponse(util.PageableResponseParams{ | 
					
						
							| 
									
										
										
										
											2024-01-29 15:57:22 +01:00
										 |  |  | 		Items:          items, | 
					
						
							|  |  |  | 		Path:           "api/v1/admin/custom_emojis", | 
					
						
							|  |  |  | 		NextMaxIDKey:   "max_shortcode_domain", | 
					
						
							| 
									
										
										
										
											2024-07-03 15:53:54 -07:00
										 |  |  | 		NextMaxIDValue: emojis[count-1].ShortcodeDomain(), | 
					
						
							| 
									
										
										
										
											2024-01-29 15:57:22 +01:00
										 |  |  | 		PrevMinIDKey:   "min_shortcode_domain", | 
					
						
							| 
									
										
										
										
											2024-07-03 15:53:54 -07:00
										 |  |  | 		PrevMinIDValue: emojis[0].ShortcodeDomain(), | 
					
						
							| 
									
										
										
										
											2024-01-29 15:57:22 +01:00
										 |  |  | 		Limit:          limit, | 
					
						
							|  |  |  | 		ExtraQueryParams: []string{ | 
					
						
							|  |  |  | 			emojisGetFilterParams( | 
					
						
							|  |  |  | 				shortcode, | 
					
						
							|  |  |  | 				domain, | 
					
						
							|  |  |  | 				includeDisabled, | 
					
						
							|  |  |  | 				includeEnabled, | 
					
						
							|  |  |  | 			), | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 	}) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-29 15:57:22 +01:00
										 |  |  | // 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) { | 
					
						
							| 
									
										
										
										
											2023-03-01 18:26:53 +00:00
										 |  |  | 	emoji, err := p.state.DB.GetEmojiByID(ctx, id) | 
					
						
							| 
									
										
										
										
											2024-01-29 15:57:22 +01:00
										 |  |  | 	if err != nil && !errors.Is(err, db.ErrNoEntries) { | 
					
						
							|  |  |  | 		err := gtserror.Newf("db error: %w", err) | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 		return nil, gtserror.NewErrorInternalError(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-29 15:57:22 +01:00
										 |  |  | 	if emoji == nil { | 
					
						
							|  |  |  | 		err := gtserror.Newf("no emoji with id %s found in the db", id) | 
					
						
							|  |  |  | 		return nil, gtserror.NewErrorNotFound(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-23 17:44:11 +01:00
										 |  |  | 	adminEmoji, err := p.converter.EmojiToAdminAPIEmoji(ctx, emoji) | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2024-01-29 15:57:22 +01:00
										 |  |  | 		err := gtserror.Newf("error converting emoji to admin api emoji: %w", err) | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 		return nil, gtserror.NewErrorInternalError(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return adminEmoji, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-29 15:57:22 +01:00
										 |  |  | // 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) { | 
					
						
							| 
									
										
										
										
											2023-03-01 18:26:53 +00:00
										 |  |  | 	emoji, err := p.state.DB.GetEmojiByID(ctx, id) | 
					
						
							| 
									
										
										
										
											2024-01-29 15:57:22 +01:00
										 |  |  | 	if err != nil && !errors.Is(err, db.ErrNoEntries) { | 
					
						
							|  |  |  | 		err := gtserror.Newf("db error: %w", err) | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 		return nil, gtserror.NewErrorInternalError(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-29 15:57:22 +01:00
										 |  |  | 	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) | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 		return nil, gtserror.NewErrorBadRequest(err, err.Error()) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-29 15:57:22 +01:00
										 |  |  | 	// Convert to admin emoji before deletion, | 
					
						
							|  |  |  | 	// so we can return the deleted emoji. | 
					
						
							| 
									
										
										
										
											2023-09-23 17:44:11 +01:00
										 |  |  | 	adminEmoji, err := p.converter.EmojiToAdminAPIEmoji(ctx, emoji) | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2024-01-29 15:57:22 +01:00
										 |  |  | 		err := gtserror.Newf("error converting emoji to admin api emoji: %w", err) | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 		return nil, gtserror.NewErrorInternalError(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-01 18:26:53 +00:00
										 |  |  | 	if err := p.state.DB.DeleteEmojiByID(ctx, id); err != nil { | 
					
						
							| 
									
										
										
										
											2024-01-29 15:57:22 +01:00
										 |  |  | 		err := gtserror.Newf("db error deleting emoji %s: %w", id, err) | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 		return nil, gtserror.NewErrorInternalError(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return adminEmoji, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-29 15:57:22 +01:00
										 |  |  | // EmojiUpdate updates one emoji with the | 
					
						
							|  |  |  | // given id, using the provided form parameters. | 
					
						
							|  |  |  | func (p *Processor) EmojiUpdate( | 
					
						
							|  |  |  | 	ctx context.Context, | 
					
						
							| 
									
										
										
										
											2024-06-26 15:01:16 +00:00
										 |  |  | 	emojiID string, | 
					
						
							| 
									
										
										
										
											2024-01-29 15:57:22 +01:00
										 |  |  | 	form *apimodel.EmojiUpdateRequest, | 
					
						
							|  |  |  | ) (*apimodel.AdminEmoji, gtserror.WithCode) { | 
					
						
							| 
									
										
										
										
											2024-06-26 15:01:16 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Get the emoji with given ID from the database. | 
					
						
							|  |  |  | 	emoji, err := p.state.DB.GetEmojiByID(ctx, emojiID) | 
					
						
							| 
									
										
										
										
											2024-01-29 15:57:22 +01:00
										 |  |  | 	if err != nil && !errors.Is(err, db.ErrNoEntries) { | 
					
						
							| 
									
										
										
										
											2024-06-26 15:01:16 +00:00
										 |  |  | 		err := gtserror.Newf("error fetching emoji from db: %w", err) | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 		return nil, gtserror.NewErrorInternalError(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-26 15:01:16 +00:00
										 |  |  | 	// Check found. | 
					
						
							| 
									
										
										
										
											2024-01-29 15:57:22 +01:00
										 |  |  | 	if emoji == nil { | 
					
						
							| 
									
										
										
										
											2024-06-26 15:01:16 +00:00
										 |  |  | 		const text = "emoji not found" | 
					
						
							|  |  |  | 		return nil, gtserror.NewErrorNotFound(errors.New(text), text) | 
					
						
							| 
									
										
										
										
											2024-01-29 15:57:22 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-26 15:01:16 +00:00
										 |  |  | 	switch form.Type { | 
					
						
							| 
									
										
										
										
											2024-01-29 15:57:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 	case apimodel.EmojiUpdateCopy: | 
					
						
							|  |  |  | 		return p.emojiUpdateCopy(ctx, emoji, form.Shortcode, form.CategoryName) | 
					
						
							| 
									
										
										
										
											2024-01-29 15:57:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 	case apimodel.EmojiUpdateDisable: | 
					
						
							|  |  |  | 		return p.emojiUpdateDisable(ctx, emoji) | 
					
						
							| 
									
										
										
										
											2024-01-29 15:57:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 	case apimodel.EmojiUpdateModify: | 
					
						
							|  |  |  | 		return p.emojiUpdateModify(ctx, emoji, form.Image, form.CategoryName) | 
					
						
							| 
									
										
										
										
											2024-01-29 15:57:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 	default: | 
					
						
							| 
									
										
										
										
											2024-06-26 15:01:16 +00:00
										 |  |  | 		const text = "unrecognized emoji update action type" | 
					
						
							|  |  |  | 		return nil, gtserror.NewErrorBadRequest(errors.New(text), text) | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-29 15:57:22 +01:00
										 |  |  | // EmojiCategoriesGet returns all custom emoji | 
					
						
							|  |  |  | // categories that exist on this instance. | 
					
						
							|  |  |  | func (p *Processor) EmojiCategoriesGet( | 
					
						
							|  |  |  | 	ctx context.Context, | 
					
						
							|  |  |  | ) ([]*apimodel.EmojiCategory, gtserror.WithCode) { | 
					
						
							| 
									
										
										
										
											2023-03-01 18:26:53 +00:00
										 |  |  | 	categories, err := p.state.DB.GetEmojiCategories(ctx) | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2024-01-29 15:57:22 +01:00
										 |  |  | 		err := gtserror.Newf("db error getting emoji categories: %w", err) | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 		return nil, gtserror.NewErrorInternalError(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	apiCategories := make([]*apimodel.EmojiCategory, 0, len(categories)) | 
					
						
							|  |  |  | 	for _, category := range categories { | 
					
						
							| 
									
										
										
										
											2023-09-23 17:44:11 +01:00
										 |  |  | 		apiCategory, err := p.converter.EmojiCategoryToAPIEmojiCategory(ctx, category) | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2024-01-29 15:57:22 +01:00
										 |  |  | 			err := gtserror.Newf("error converting emoji category to api emoji category: %w", err) | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 			return nil, gtserror.NewErrorInternalError(err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		apiCategories = append(apiCategories, apiCategory) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return apiCategories, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-29 15:57:22 +01:00
										 |  |  | // 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, | 
					
						
							| 
									
										
										
										
											2024-06-26 15:01:16 +00:00
										 |  |  | 	target *gtsmodel.Emoji, | 
					
						
							| 
									
										
										
										
											2024-01-29 15:57:22 +01:00
										 |  |  | 	shortcode *string, | 
					
						
							| 
									
										
										
										
											2024-06-26 15:01:16 +00:00
										 |  |  | 	categoryName *string, | 
					
						
							| 
									
										
										
										
											2024-01-29 15:57:22 +01:00
										 |  |  | ) (*apimodel.AdminEmoji, gtserror.WithCode) { | 
					
						
							| 
									
										
										
										
											2024-06-26 15:01:16 +00:00
										 |  |  | 	if target.IsLocal() { | 
					
						
							|  |  |  | 		const text = "target emoji is not remote; cannot copy to local" | 
					
						
							|  |  |  | 		return nil, gtserror.NewErrorBadRequest(errors.New(text), text) | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-26 15:01:16 +00:00
										 |  |  | 	// Ensure target emoji is locally cached. | 
					
						
							|  |  |  | 	target, err := p.federator.RefreshEmoji( | 
					
						
							|  |  |  | 		ctx, | 
					
						
							|  |  |  | 		target, | 
					
						
							| 
									
										
										
										
											2024-01-29 15:57:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-26 15:01:16 +00:00
										 |  |  | 		// no changes we want to make. | 
					
						
							|  |  |  | 		media.AdditionalEmojiInfo{}, | 
					
						
							|  |  |  | 		false, | 
					
						
							|  |  |  | 	) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		err := gtserror.Newf("error recaching emoji %s: %w", target.ImageRemoteURL, err) | 
					
						
							|  |  |  | 		return nil, gtserror.NewErrorNotFound(err) | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-12 09:39:47 +00:00
										 |  |  | 	// Get maximum supported local emoji size. | 
					
						
							|  |  |  | 	maxsz := config.GetMediaEmojiLocalMaxSize() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Ensure target emoji image within size bounds. | 
					
						
							|  |  |  | 	if bytesize.Size(target.ImageFileSize) > maxsz { | 
					
						
							|  |  |  | 		text := fmt.Sprintf("emoji exceeds configured max size: %s", maxsz) | 
					
						
							|  |  |  | 		return nil, gtserror.NewErrorBadRequest(errors.New(text), text) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-29 15:57:22 +01:00
										 |  |  | 	// 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. | 
					
						
							| 
									
										
										
										
											2024-07-12 09:39:47 +00:00
										 |  |  | 	data := func(ctx context.Context) (io.ReadCloser, error) { | 
					
						
							| 
									
										
										
										
											2024-06-26 15:01:16 +00:00
										 |  |  | 		rc, err := p.state.Storage.GetStream(ctx, target.ImagePath) | 
					
						
							| 
									
										
										
										
											2024-07-12 09:39:47 +00:00
										 |  |  | 		return rc, err | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-26 15:01:16 +00:00
										 |  |  | 	// Attempt to create the new local emoji. | 
					
						
							|  |  |  | 	emoji, errWithCode := p.createEmoji(ctx, | 
					
						
							| 
									
										
										
										
											2024-07-17 15:26:33 +00:00
										 |  |  | 		util.PtrOrValue(shortcode, ""), | 
					
						
							|  |  |  | 		util.PtrOrValue(categoryName, ""), | 
					
						
							| 
									
										
										
										
											2024-06-26 15:01:16 +00:00
										 |  |  | 		data, | 
					
						
							| 
									
										
										
										
											2024-01-29 15:57:22 +01:00
										 |  |  | 	) | 
					
						
							| 
									
										
										
										
											2024-06-26 15:01:16 +00:00
										 |  |  | 	if errWithCode != nil { | 
					
						
							|  |  |  | 		return nil, errWithCode | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-26 15:01:16 +00:00
										 |  |  | 	apiEmoji, err := p.converter.EmojiToAdminAPIEmoji(ctx, emoji) | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2024-06-26 15:01:16 +00:00
										 |  |  | 		err := gtserror.Newf("error converting emoji: %w", err) | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 		return nil, gtserror.NewErrorInternalError(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-26 15:01:16 +00:00
										 |  |  | 	return apiEmoji, nil | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-29 15:57:22 +01:00
										 |  |  | // 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) | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 		return nil, gtserror.NewErrorBadRequest(err, err.Error()) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-29 15:57:22 +01:00
										 |  |  | 	// 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) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-23 17:44:11 +01:00
										 |  |  | 	adminEmoji, err := p.converter.EmojiToAdminAPIEmoji(ctx, emoji) | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2024-06-26 15:01:16 +00:00
										 |  |  | 		err := gtserror.Newf("error converting emoji: %w", err) | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 		return nil, gtserror.NewErrorInternalError(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return adminEmoji, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-29 15:57:22 +01:00
										 |  |  | // 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, | 
					
						
							| 
									
										
										
										
											2024-06-26 15:01:16 +00:00
										 |  |  | 	categoryName *string, | 
					
						
							| 
									
										
										
										
											2024-01-29 15:57:22 +01:00
										 |  |  | ) (*apimodel.AdminEmoji, gtserror.WithCode) { | 
					
						
							|  |  |  | 	if !emoji.IsLocal() { | 
					
						
							| 
									
										
										
										
											2024-06-26 15:01:16 +00:00
										 |  |  | 		const text = "cannot modify remote emoji" | 
					
						
							|  |  |  | 		return nil, gtserror.NewErrorBadRequest(errors.New(text), text) | 
					
						
							| 
									
										
										
										
											2024-01-29 15:57:22 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Ensure there's actually something to update. | 
					
						
							| 
									
										
										
										
											2024-06-26 15:01:16 +00:00
										 |  |  | 	if image == nil && categoryName == nil { | 
					
						
							|  |  |  | 		const text = "no changes were provided" | 
					
						
							|  |  |  | 		return nil, gtserror.NewErrorBadRequest(errors.New(text), text) | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-26 15:01:16 +00:00
										 |  |  | 	if categoryName != nil { | 
					
						
							|  |  |  | 		if *categoryName != "" { | 
					
						
							|  |  |  | 			// A category was provided, get / create relevant emoji category. | 
					
						
							|  |  |  | 			category, errWithCode := p.mustGetEmojiCategory(ctx, *categoryName) | 
					
						
							|  |  |  | 			if errWithCode != nil { | 
					
						
							|  |  |  | 				return nil, errWithCode | 
					
						
							| 
									
										
										
										
											2024-01-29 15:57:22 +01:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-26 15:01:16 +00:00
										 |  |  | 			if category.ID == emoji.CategoryID { | 
					
						
							|  |  |  | 				// There was no change, | 
					
						
							|  |  |  | 				// indicate this by unsetting | 
					
						
							|  |  |  | 				// the category name pointer. | 
					
						
							|  |  |  | 				categoryName = nil | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				// Update emoji category. | 
					
						
							|  |  |  | 				emoji.CategoryID = category.ID | 
					
						
							|  |  |  | 				emoji.Category = category | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2024-01-29 15:57:22 +01:00
										 |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2024-06-26 15:01:16 +00:00
										 |  |  | 			// Emoji category was unset. | 
					
						
							|  |  |  | 			emoji.CategoryID = "" | 
					
						
							|  |  |  | 			emoji.Category = nil | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-26 15:01:16 +00:00
										 |  |  | 	// Check whether any image changes were requested. | 
					
						
							|  |  |  | 	imageUpdated := (image != nil && image.Size > 0) | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-26 15:01:16 +00:00
										 |  |  | 	if !imageUpdated && categoryName != nil { | 
					
						
							|  |  |  | 		// Only updating category; only a single database update required. | 
					
						
							| 
									
										
										
										
											2024-01-29 15:57:22 +01:00
										 |  |  | 		if err := p.state.DB.UpdateEmoji(ctx, emoji, "category_id"); err != nil { | 
					
						
							| 
									
										
										
										
											2024-06-26 15:01:16 +00:00
										 |  |  | 			err := gtserror.Newf("error updating emoji in db: %w", err) | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 			return nil, gtserror.NewErrorInternalError(err) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2024-06-26 15:01:16 +00:00
										 |  |  | 	} else if imageUpdated { | 
					
						
							|  |  |  | 		var err error | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-29 15:57:22 +01:00
										 |  |  | 		// Updating image and maybe categoryID. | 
					
						
							|  |  |  | 		// We can do both at the same time :) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-12 09:39:47 +00:00
										 |  |  | 		// Get maximum supported local emoji size. | 
					
						
							|  |  |  | 		maxsz := config.GetMediaEmojiLocalMaxSize() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// Ensure media within size bounds. | 
					
						
							|  |  |  | 		if image.Size > int64(maxsz) { | 
					
						
							|  |  |  | 			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)) | 
					
						
							|  |  |  | 		data := func(context.Context) (io.ReadCloser, error) { | 
					
						
							|  |  |  | 			return rc, nil | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-26 15:01:16 +00:00
										 |  |  | 		// Prepare emoji model for recache from new data. | 
					
						
							|  |  |  | 		processing := p.media.RecacheEmoji(emoji, data) | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-26 15:01:16 +00:00
										 |  |  | 		// Load to trigger update + write. | 
					
						
							|  |  |  | 		emoji, err = processing.Load(ctx) | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2024-06-26 15:01:16 +00:00
										 |  |  | 			err := gtserror.Newf("error processing emoji %s: %w", emoji.Shortcode, err) | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 			return nil, gtserror.NewErrorInternalError(err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-23 17:44:11 +01:00
										 |  |  | 	adminEmoji, err := p.converter.EmojiToAdminAPIEmoji(ctx, emoji) | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2024-06-26 15:01:16 +00:00
										 |  |  | 		err := gtserror.Newf("error converting emoji: %w", err) | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 		return nil, gtserror.NewErrorInternalError(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return adminEmoji, nil | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2024-06-26 15:01:16 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | // 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() | 
					
						
							|  |  |  | } |