diff options
Diffstat (limited to 'internal/processing/admin')
20 files changed, 723 insertions, 1099 deletions
diff --git a/internal/processing/admin/accountaction.go b/internal/processing/admin/account.go index e4e6bbd97..d23d1fbfe 100644 --- a/internal/processing/admin/accountaction.go +++ b/internal/processing/admin/account.go @@ -1,3 +1,21 @@ +/* + GoToSocial + Copyright (C) 2021-2023 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 admin import ( @@ -12,7 +30,7 @@ import ( "github.com/superseriousbusiness/gotosocial/internal/messages" ) -func (p *processor) AccountAction(ctx context.Context, account *gtsmodel.Account, form *apimodel.AdminAccountActionRequest) gtserror.WithCode { +func (p *Processor) AccountAction(ctx context.Context, account *gtsmodel.Account, form *apimodel.AdminAccountActionRequest) gtserror.WithCode { targetAccount, err := p.db.GetAccountByID(ctx, form.TargetAccountID) if err != nil { return gtserror.NewErrorInternalError(err) diff --git a/internal/processing/admin/admin.go b/internal/processing/admin/admin.go index b08b589bb..54827b8fd 100644 --- a/internal/processing/admin/admin.go +++ b/internal/processing/admin/admin.go @@ -19,14 +19,8 @@ package admin import ( - "context" - "mime/multipart" - - apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" "github.com/superseriousbusiness/gotosocial/internal/concurrency" "github.com/superseriousbusiness/gotosocial/internal/db" - "github.com/superseriousbusiness/gotosocial/internal/gtserror" - "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" "github.com/superseriousbusiness/gotosocial/internal/media" "github.com/superseriousbusiness/gotosocial/internal/messages" "github.com/superseriousbusiness/gotosocial/internal/storage" @@ -34,28 +28,7 @@ import ( "github.com/superseriousbusiness/gotosocial/internal/typeutils" ) -// Processor wraps a bunch of functions for processing admin actions. -type Processor interface { - DomainBlockCreate(ctx context.Context, account *gtsmodel.Account, domain string, obfuscate bool, publicComment string, privateComment string, subscriptionID string) (*apimodel.DomainBlock, gtserror.WithCode) - DomainBlocksImport(ctx context.Context, account *gtsmodel.Account, domains *multipart.FileHeader) ([]*apimodel.DomainBlock, gtserror.WithCode) - DomainBlocksGet(ctx context.Context, account *gtsmodel.Account, export bool) ([]*apimodel.DomainBlock, gtserror.WithCode) - DomainBlockGet(ctx context.Context, account *gtsmodel.Account, id string, export bool) (*apimodel.DomainBlock, gtserror.WithCode) - DomainBlockDelete(ctx context.Context, account *gtsmodel.Account, id string) (*apimodel.DomainBlock, gtserror.WithCode) - AccountAction(ctx context.Context, account *gtsmodel.Account, form *apimodel.AdminAccountActionRequest) gtserror.WithCode - EmojiCreate(ctx context.Context, account *gtsmodel.Account, user *gtsmodel.User, form *apimodel.EmojiCreateRequest) (*apimodel.Emoji, gtserror.WithCode) - EmojisGet(ctx context.Context, account *gtsmodel.Account, user *gtsmodel.User, domain string, includeDisabled bool, includeEnabled bool, shortcode string, maxShortcodeDomain string, minShortcodeDomain string, limit int) (*apimodel.PageableResponse, gtserror.WithCode) - EmojiGet(ctx context.Context, account *gtsmodel.Account, user *gtsmodel.User, id string) (*apimodel.AdminEmoji, gtserror.WithCode) - EmojiDelete(ctx context.Context, id string) (*apimodel.AdminEmoji, gtserror.WithCode) - EmojiUpdate(ctx context.Context, id string, form *apimodel.EmojiUpdateRequest) (*apimodel.AdminEmoji, gtserror.WithCode) - EmojiCategoriesGet(ctx context.Context) ([]*apimodel.EmojiCategory, gtserror.WithCode) - MediaPrune(ctx context.Context, mediaRemoteCacheDays int) gtserror.WithCode - MediaRefetch(ctx context.Context, requestingAccount *gtsmodel.Account, domain string) gtserror.WithCode - ReportsGet(ctx context.Context, account *gtsmodel.Account, resolved *bool, accountID string, targetAccountID string, maxID string, sinceID string, minID string, limit int) (*apimodel.PageableResponse, gtserror.WithCode) - ReportGet(ctx context.Context, account *gtsmodel.Account, id string) (*apimodel.AdminReport, gtserror.WithCode) - ReportResolve(ctx context.Context, account *gtsmodel.Account, id string, actionTakenComment *string) (*apimodel.AdminReport, gtserror.WithCode) -} - -type processor struct { +type Processor struct { tc typeutils.TypeConverter mediaManager media.Manager transportController transport.Controller @@ -66,7 +39,7 @@ type processor struct { // New returns a new admin processor. func New(db db.DB, tc typeutils.TypeConverter, mediaManager media.Manager, transportController transport.Controller, storage *storage.Driver, clientWorker *concurrency.WorkerPool[messages.FromClientAPI]) Processor { - return &processor{ + return Processor{ tc: tc, mediaManager: mediaManager, transportController: transportController, diff --git a/internal/processing/admin/createemoji.go b/internal/processing/admin/createemoji.go deleted file mode 100644 index b2a7bfc86..000000000 --- a/internal/processing/admin/createemoji.go +++ /dev/null @@ -1,89 +0,0 @@ -/* - GoToSocial - Copyright (C) 2021-2023 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 admin - -import ( - "context" - "fmt" - "io" - - apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" - "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/uris" -) - -func (p *processor) EmojiCreate(ctx context.Context, account *gtsmodel.Account, user *gtsmodel.User, form *apimodel.EmojiCreateRequest) (*apimodel.Emoji, gtserror.WithCode) { - if !*user.Admin { - return nil, gtserror.NewErrorUnauthorized(fmt.Errorf("user %s not an admin", user.ID), "user is not an admin") - } - - maybeExisting, err := p.db.GetEmojiByShortcodeDomain(ctx, form.Shortcode, "") - if maybeExisting != nil { - return nil, gtserror.NewErrorConflict(fmt.Errorf("emoji with shortcode %s already exists", form.Shortcode), fmt.Sprintf("emoji with shortcode %s already exists", form.Shortcode)) - } - - if err != nil && err != db.ErrNoEntries { - return nil, gtserror.NewErrorInternalError(fmt.Errorf("error checking existence of emoji with shortcode %s: %s", form.Shortcode, err)) - } - - emojiID, err := id.NewRandomULID() - if err != nil { - return nil, gtserror.NewErrorInternalError(fmt.Errorf("error creating id for new emoji: %s", err), "error creating emoji ID") - } - - emojiURI := uris.GenerateURIForEmoji(emojiID) - - data := func(innerCtx context.Context) (io.ReadCloser, int64, error) { - f, err := form.Image.Open() - return f, form.Image.Size, err - } - - var ai *media.AdditionalEmojiInfo - if form.CategoryName != "" { - category, err := p.GetOrCreateEmojiCategory(ctx, form.CategoryName) - if err != nil { - return nil, gtserror.NewErrorInternalError(fmt.Errorf("error putting id in category: %s", err), "error putting id in category") - } - - ai = &media.AdditionalEmojiInfo{ - CategoryID: &category.ID, - } - } - - processingEmoji, err := p.mediaManager.PreProcessEmoji(ctx, data, nil, form.Shortcode, emojiID, emojiURI, ai, false) - if err != nil { - return nil, gtserror.NewErrorInternalError(fmt.Errorf("error processing emoji: %s", err), "error processing emoji") - } - - emoji, err := processingEmoji.LoadEmoji(ctx) - if err != nil { - return nil, gtserror.NewErrorInternalError(fmt.Errorf("error loading emoji: %s", err), "error loading emoji") - } - - apiEmoji, err := p.tc.EmojiToAPIEmoji(ctx, emoji) - if err != nil { - return nil, gtserror.NewErrorInternalError(fmt.Errorf("error converting emoji: %s", err), "error converting emoji to api representation") - } - - return &apiEmoji, nil -} diff --git a/internal/processing/admin/deletedomainblock.go b/internal/processing/admin/deletedomainblock.go deleted file mode 100644 index 412a01b8b..000000000 --- a/internal/processing/admin/deletedomainblock.go +++ /dev/null @@ -1,86 +0,0 @@ -/* - GoToSocial - Copyright (C) 2021-2023 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 admin - -import ( - "context" - "fmt" - "time" - - apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" - "github.com/superseriousbusiness/gotosocial/internal/db" - "github.com/superseriousbusiness/gotosocial/internal/gtserror" - "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" -) - -func (p *processor) DomainBlockDelete(ctx context.Context, account *gtsmodel.Account, id string) (*apimodel.DomainBlock, gtserror.WithCode) { - domainBlock := >smodel.DomainBlock{} - - if err := p.db.GetByID(ctx, id, domainBlock); err != nil { - if err != db.ErrNoEntries { - // something has gone really wrong - return nil, gtserror.NewErrorInternalError(err) - } - // there are no entries for this ID - return nil, gtserror.NewErrorNotFound(fmt.Errorf("no entry for ID %s", id)) - } - - // prepare the domain block to return - apiDomainBlock, err := p.tc.DomainBlockToAPIDomainBlock(ctx, domainBlock, false) - if err != nil { - return nil, gtserror.NewErrorInternalError(err) - } - - // Delete the domain block - if err := p.db.DeleteDomainBlock(ctx, domainBlock.Domain); err != nil { - return nil, gtserror.NewErrorInternalError(err) - } - - // remove the domain block reference from the instance, if we have an entry for it - i := >smodel.Instance{} - if err := p.db.GetWhere(ctx, []db.Where{ - {Key: "domain", Value: domainBlock.Domain}, - {Key: "domain_block_id", Value: id}, - }, i); err == nil { - updatingColumns := []string{"suspended_at", "domain_block_id", "updated_at"} - i.SuspendedAt = time.Time{} - i.DomainBlockID = "" - i.UpdatedAt = time.Now() - if err := p.db.UpdateByID(ctx, i, i.ID, updatingColumns...); err != nil { - return nil, gtserror.NewErrorInternalError(fmt.Errorf("couldn't update database entry for instance %s: %s", domainBlock.Domain, err)) - } - } - - // unsuspend all accounts whose suspension origin was this domain block - // 1. remove the 'suspended_at' entry from their accounts - if err := p.db.UpdateWhere(ctx, []db.Where{ - {Key: "suspension_origin", Value: domainBlock.ID}, - }, "suspended_at", nil, &[]*gtsmodel.Account{}); err != nil { - return nil, gtserror.NewErrorInternalError(fmt.Errorf("database error removing suspended_at from accounts: %s", err)) - } - - // 2. remove the 'suspension_origin' entry from their accounts - if err := p.db.UpdateWhere(ctx, []db.Where{ - {Key: "suspension_origin", Value: domainBlock.ID}, - }, "suspension_origin", nil, &[]*gtsmodel.Account{}); err != nil { - return nil, gtserror.NewErrorInternalError(fmt.Errorf("database error removing suspension_origin from accounts: %s", err)) - } - - return apiDomainBlock, nil -} diff --git a/internal/processing/admin/deleteemoji.go b/internal/processing/admin/deleteemoji.go deleted file mode 100644 index 17c3a0ca0..000000000 --- a/internal/processing/admin/deleteemoji.go +++ /dev/null @@ -1,59 +0,0 @@ -/* - GoToSocial - Copyright (C) 2021-2023 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 admin - -import ( - "context" - "errors" - "fmt" - - apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" - "github.com/superseriousbusiness/gotosocial/internal/db" - "github.com/superseriousbusiness/gotosocial/internal/gtserror" -) - -func (p *processor) EmojiDelete(ctx context.Context, id string) (*apimodel.AdminEmoji, gtserror.WithCode) { - emoji, err := p.db.GetEmojiByID(ctx, id) - if err != nil { - if errors.Is(err, db.ErrNoEntries) { - err = fmt.Errorf("EmojiDelete: no emoji with id %s found in the db", id) - return nil, gtserror.NewErrorNotFound(err) - } - err := fmt.Errorf("EmojiDelete: db error: %s", err) - return nil, gtserror.NewErrorInternalError(err) - } - - if emoji.Domain != "" { - err = fmt.Errorf("EmojiDelete: emoji with id %s was not a local emoji, will not delete", id) - return nil, gtserror.NewErrorBadRequest(err, err.Error()) - } - - adminEmoji, err := p.tc.EmojiToAdminAPIEmoji(ctx, emoji) - if err != nil { - err = fmt.Errorf("EmojiDelete: error converting emoji to admin api emoji: %s", err) - return nil, gtserror.NewErrorInternalError(err) - } - - if err := p.db.DeleteEmojiByID(ctx, id); err != nil { - err := fmt.Errorf("EmojiDelete: db error: %s", err) - return nil, gtserror.NewErrorInternalError(err) - } - - return adminEmoji, nil -} diff --git a/internal/processing/admin/createdomainblock.go b/internal/processing/admin/domainblock.go index 5c3542f79..415ac610f 100644 --- a/internal/processing/admin/createdomainblock.go +++ b/internal/processing/admin/domainblock.go @@ -1,27 +1,13 @@ -/* - GoToSocial - Copyright (C) 2021-2023 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 admin import ( + "bytes" "context" + "encoding/json" "errors" "fmt" + "io" + "mime/multipart" "strings" "time" @@ -37,7 +23,7 @@ import ( "github.com/superseriousbusiness/gotosocial/internal/text" ) -func (p *processor) DomainBlockCreate(ctx context.Context, account *gtsmodel.Account, domain string, obfuscate bool, publicComment string, privateComment string, subscriptionID string) (*apimodel.DomainBlock, gtserror.WithCode) { +func (p *Processor) DomainBlockCreate(ctx context.Context, account *gtsmodel.Account, domain string, obfuscate bool, publicComment string, privateComment string, subscriptionID string) (*apimodel.DomainBlock, gtserror.WithCode) { // domain blocks will always be lowercase domain = strings.ToLower(domain) @@ -88,12 +74,8 @@ func (p *processor) DomainBlockCreate(ctx context.Context, account *gtsmodel.Acc // 1. Strip most info away from the instance entry for the domain. // 2. Delete the instance account for that instance if it exists. // 3. Select all accounts from this instance and pass them through the delete functionality of the processor. -func (p *processor) initiateDomainBlockSideEffects(ctx context.Context, account *gtsmodel.Account, block *gtsmodel.DomainBlock) { - l := log.WithContext(ctx). - WithFields(kv.Fields{ - {"domain", block.Domain}, - }...) - +func (p *Processor) initiateDomainBlockSideEffects(ctx context.Context, account *gtsmodel.Account, block *gtsmodel.DomainBlock) { + l := log.WithContext(ctx).WithFields(kv.Fields{{"domain", block.Domain}}...) l.Debug("processing domain block side effects") // if we have an instance entry for this domain, update it with the new block ID and clear all fields @@ -174,3 +156,139 @@ selectAccountsLoop: } } } + +// DomainBlocksImport handles the import of a bunch of domain blocks at once, by calling the DomainBlockCreate function for each domain in the provided file. +func (p *Processor) DomainBlocksImport(ctx context.Context, account *gtsmodel.Account, domains *multipart.FileHeader) ([]*apimodel.DomainBlock, gtserror.WithCode) { + f, err := domains.Open() + if err != nil { + return nil, gtserror.NewErrorBadRequest(fmt.Errorf("DomainBlocksImport: error opening attachment: %s", err)) + } + buf := new(bytes.Buffer) + size, err := io.Copy(buf, f) + if err != nil { + return nil, gtserror.NewErrorBadRequest(fmt.Errorf("DomainBlocksImport: error reading attachment: %s", err)) + } + if size == 0 { + return nil, gtserror.NewErrorBadRequest(errors.New("DomainBlocksImport: could not read provided attachment: size 0 bytes")) + } + + d := []apimodel.DomainBlock{} + if err := json.Unmarshal(buf.Bytes(), &d); err != nil { + return nil, gtserror.NewErrorBadRequest(fmt.Errorf("DomainBlocksImport: could not read provided attachment: %s", err)) + } + + blocks := []*apimodel.DomainBlock{} + for _, d := range d { + block, err := p.DomainBlockCreate(ctx, account, d.Domain.Domain, false, d.PublicComment, "", "") + if err != nil { + return nil, err + } + + blocks = append(blocks, block) + } + + return blocks, nil +} + +// DomainBlocksGet returns all existing domain blocks. +// If export is true, the format will be suitable for writing out to an export. +func (p *Processor) DomainBlocksGet(ctx context.Context, account *gtsmodel.Account, export bool) ([]*apimodel.DomainBlock, gtserror.WithCode) { + domainBlocks := []*gtsmodel.DomainBlock{} + + if err := p.db.GetAll(ctx, &domainBlocks); err != nil { + if !errors.Is(err, db.ErrNoEntries) { + // something has gone really wrong + return nil, gtserror.NewErrorInternalError(err) + } + } + + apiDomainBlocks := []*apimodel.DomainBlock{} + for _, b := range domainBlocks { + apiDomainBlock, err := p.tc.DomainBlockToAPIDomainBlock(ctx, b, export) + if err != nil { + return nil, gtserror.NewErrorInternalError(err) + } + apiDomainBlocks = append(apiDomainBlocks, apiDomainBlock) + } + + return apiDomainBlocks, nil +} + +// DomainBlockGet returns one domain block with the given id. +// If export is true, the format will be suitable for writing out to an export. +func (p *Processor) DomainBlockGet(ctx context.Context, account *gtsmodel.Account, id string, export bool) (*apimodel.DomainBlock, gtserror.WithCode) { + domainBlock := >smodel.DomainBlock{} + + if err := p.db.GetByID(ctx, id, domainBlock); err != nil { + if !errors.Is(err, db.ErrNoEntries) { + // something has gone really wrong + return nil, gtserror.NewErrorInternalError(err) + } + // there are no entries for this ID + return nil, gtserror.NewErrorNotFound(fmt.Errorf("no entry for ID %s", id)) + } + + apiDomainBlock, err := p.tc.DomainBlockToAPIDomainBlock(ctx, domainBlock, export) + if err != nil { + return nil, gtserror.NewErrorInternalError(err) + } + + return apiDomainBlock, nil +} + +// DomainBlockDelete removes one domain block with the given ID. +func (p *Processor) DomainBlockDelete(ctx context.Context, account *gtsmodel.Account, id string) (*apimodel.DomainBlock, gtserror.WithCode) { + domainBlock := >smodel.DomainBlock{} + + if err := p.db.GetByID(ctx, id, domainBlock); err != nil { + if !errors.Is(err, db.ErrNoEntries) { + // something has gone really wrong + return nil, gtserror.NewErrorInternalError(err) + } + // there are no entries for this ID + return nil, gtserror.NewErrorNotFound(fmt.Errorf("no entry for ID %s", id)) + } + + // prepare the domain block to return + apiDomainBlock, err := p.tc.DomainBlockToAPIDomainBlock(ctx, domainBlock, false) + if err != nil { + return nil, gtserror.NewErrorInternalError(err) + } + + // Delete the domain block + if err := p.db.DeleteDomainBlock(ctx, domainBlock.Domain); err != nil { + return nil, gtserror.NewErrorInternalError(err) + } + + // remove the domain block reference from the instance, if we have an entry for it + i := >smodel.Instance{} + if err := p.db.GetWhere(ctx, []db.Where{ + {Key: "domain", Value: domainBlock.Domain}, + {Key: "domain_block_id", Value: id}, + }, i); err == nil { + updatingColumns := []string{"suspended_at", "domain_block_id", "updated_at"} + i.SuspendedAt = time.Time{} + i.DomainBlockID = "" + i.UpdatedAt = time.Now() + if err := p.db.UpdateByID(ctx, i, i.ID, updatingColumns...); err != nil { + return nil, gtserror.NewErrorInternalError(fmt.Errorf("couldn't update database entry for instance %s: %s", domainBlock.Domain, err)) + } + } + + // unsuspend all accounts whose suspension origin was this domain block + // 1. remove the 'suspended_at' entry from their accounts + if err := p.db.UpdateWhere(ctx, []db.Where{ + {Key: "suspension_origin", Value: domainBlock.ID}, + }, "suspended_at", nil, &[]*gtsmodel.Account{}); err != nil { + return nil, gtserror.NewErrorInternalError(fmt.Errorf("database error removing suspended_at from accounts: %s", err)) + } + + // 2. remove the 'suspension_origin' entry from their accounts + if err := p.db.UpdateWhere(ctx, []db.Where{ + {Key: "suspension_origin", Value: domainBlock.ID}, + }, "suspension_origin", nil, &[]*gtsmodel.Account{}); err != nil { + return nil, gtserror.NewErrorInternalError(fmt.Errorf("database error removing suspension_origin from accounts: %s", err)) + } + + return apiDomainBlock, nil +} diff --git a/internal/processing/admin/emoji.go b/internal/processing/admin/emoji.go new file mode 100644 index 000000000..391d18525 --- /dev/null +++ b/internal/processing/admin/emoji.go @@ -0,0 +1,485 @@ +/* + GoToSocial + Copyright (C) 2021-2023 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 admin + +import ( + "context" + "errors" + "fmt" + "io" + "mime/multipart" + "strings" + + apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" + "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/uris" + "github.com/superseriousbusiness/gotosocial/internal/util" +) + +// EmojiCreate creates a custom emoji on this instance. +func (p *Processor) EmojiCreate(ctx context.Context, account *gtsmodel.Account, user *gtsmodel.User, form *apimodel.EmojiCreateRequest) (*apimodel.Emoji, gtserror.WithCode) { + if !*user.Admin { + return nil, gtserror.NewErrorUnauthorized(fmt.Errorf("user %s not an admin", user.ID), "user is not an admin") + } + + maybeExisting, err := p.db.GetEmojiByShortcodeDomain(ctx, form.Shortcode, "") + if maybeExisting != nil { + return nil, gtserror.NewErrorConflict(fmt.Errorf("emoji with shortcode %s already exists", form.Shortcode), fmt.Sprintf("emoji with shortcode %s already exists", form.Shortcode)) + } + + if err != nil && err != db.ErrNoEntries { + return nil, gtserror.NewErrorInternalError(fmt.Errorf("error checking existence of emoji with shortcode %s: %s", form.Shortcode, err)) + } + + emojiID, err := id.NewRandomULID() + if err != nil { + return nil, gtserror.NewErrorInternalError(fmt.Errorf("error creating id for new emoji: %s", err), "error creating emoji ID") + } + + emojiURI := uris.GenerateURIForEmoji(emojiID) + + data := func(innerCtx context.Context) (io.ReadCloser, int64, error) { + f, err := form.Image.Open() + return f, form.Image.Size, err + } + + var ai *media.AdditionalEmojiInfo + if form.CategoryName != "" { + category, err := p.getOrCreateEmojiCategory(ctx, form.CategoryName) + if err != nil { + return nil, gtserror.NewErrorInternalError(fmt.Errorf("error putting id in category: %s", err), "error putting id in category") + } + + ai = &media.AdditionalEmojiInfo{ + CategoryID: &category.ID, + } + } + + processingEmoji, err := p.mediaManager.PreProcessEmoji(ctx, data, nil, form.Shortcode, emojiID, emojiURI, ai, false) + if err != nil { + return nil, gtserror.NewErrorInternalError(fmt.Errorf("error processing emoji: %s", err), "error processing emoji") + } + + emoji, err := processingEmoji.LoadEmoji(ctx) + if err != nil { + return nil, gtserror.NewErrorInternalError(fmt.Errorf("error loading emoji: %s", err), "error loading emoji") + } + + apiEmoji, err := p.tc.EmojiToAPIEmoji(ctx, emoji) + if err != nil { + return nil, gtserror.NewErrorInternalError(fmt.Errorf("error converting emoji: %s", err), "error converting emoji to api representation") + } + + 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, + user *gtsmodel.User, + domain string, + includeDisabled bool, + includeEnabled bool, + shortcode string, + maxShortcodeDomain string, + minShortcodeDomain string, + limit int, +) (*apimodel.PageableResponse, gtserror.WithCode) { + if !*user.Admin { + return nil, gtserror.NewErrorUnauthorized(fmt.Errorf("user %s not an admin", user.ID), "user is not an admin") + } + + emojis, err := p.db.GetEmojis(ctx, domain, includeDisabled, includeEnabled, shortcode, maxShortcodeDomain, minShortcodeDomain, limit) + if err != nil && !errors.Is(err, db.ErrNoEntries) { + err := fmt.Errorf("EmojisGet: db error: %s", 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.tc.EmojiToAdminAPIEmoji(ctx, emoji) + if err != nil { + err := fmt.Errorf("EmojisGet: error converting emoji to admin model emoji: %s", err) + return nil, gtserror.NewErrorInternalError(err) + } + items = append(items, adminEmoji) + } + + filterBuilder := strings.Builder{} + filterBuilder.WriteString("filter=") + + switch domain { + case "", "local": + filterBuilder.WriteString("domain:local") + case db.EmojiAllDomains: + filterBuilder.WriteString("domain:all") + default: + filterBuilder.WriteString("domain:") + filterBuilder.WriteString(domain) + } + + if includeDisabled != includeEnabled { + if includeDisabled { + filterBuilder.WriteString(",disabled") + } + if includeEnabled { + filterBuilder.WriteString(",enabled") + } + } + + if shortcode != "" { + filterBuilder.WriteString(",shortcode:") + filterBuilder.WriteString(shortcode) + } + + return util.PackagePageableResponse(util.PageableResponseParams{ + Items: items, + Path: "api/v1/admin/custom_emojis", + NextMaxIDKey: "max_shortcode_domain", + NextMaxIDValue: util.ShortcodeDomain(emojis[count-1]), + PrevMinIDKey: "min_shortcode_domain", + PrevMinIDValue: util.ShortcodeDomain(emojis[0]), + Limit: limit, + ExtraQueryParams: []string{filterBuilder.String()}, + }) +} + +// EmojiGet returns the admin view of one custom emoji with the given id. +func (p *Processor) EmojiGet(ctx context.Context, account *gtsmodel.Account, user *gtsmodel.User, id string) (*apimodel.AdminEmoji, gtserror.WithCode) { + if !*user.Admin { + return nil, gtserror.NewErrorUnauthorized(fmt.Errorf("user %s not an admin", user.ID), "user is not an admin") + } + + emoji, err := p.db.GetEmojiByID(ctx, id) + if err != nil { + if errors.Is(err, db.ErrNoEntries) { + err = fmt.Errorf("EmojiGet: no emoji with id %s found in the db", id) + return nil, gtserror.NewErrorNotFound(err) + } + err := fmt.Errorf("EmojiGet: db error: %s", err) + return nil, gtserror.NewErrorInternalError(err) + } + + adminEmoji, err := p.tc.EmojiToAdminAPIEmoji(ctx, emoji) + if err != nil { + err = fmt.Errorf("EmojiGet: error converting emoji to admin api emoji: %s", err) + return nil, gtserror.NewErrorInternalError(err) + } + + return adminEmoji, nil +} + +// EmojiDelete deletes one emoji from the database, with the given id. +func (p *Processor) EmojiDelete(ctx context.Context, id string) (*apimodel.AdminEmoji, gtserror.WithCode) { + emoji, err := p.db.GetEmojiByID(ctx, id) + if err != nil { + if errors.Is(err, db.ErrNoEntries) { + err = fmt.Errorf("EmojiDelete: no emoji with id %s found in the db", id) + return nil, gtserror.NewErrorNotFound(err) + } + err := fmt.Errorf("EmojiDelete: db error: %s", err) + return nil, gtserror.NewErrorInternalError(err) + } + + if emoji.Domain != "" { + err = fmt.Errorf("EmojiDelete: emoji with id %s was not a local emoji, will not delete", id) + return nil, gtserror.NewErrorBadRequest(err, err.Error()) + } + + adminEmoji, err := p.tc.EmojiToAdminAPIEmoji(ctx, emoji) + if err != nil { + err = fmt.Errorf("EmojiDelete: error converting emoji to admin api emoji: %s", err) + return nil, gtserror.NewErrorInternalError(err) + } + + if err := p.db.DeleteEmojiByID(ctx, id); err != nil { + err := fmt.Errorf("EmojiDelete: db error: %s", 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, id string, form *apimodel.EmojiUpdateRequest) (*apimodel.AdminEmoji, gtserror.WithCode) { + emoji, err := p.db.GetEmojiByID(ctx, id) + if err != nil { + if errors.Is(err, db.ErrNoEntries) { + err = fmt.Errorf("EmojiUpdate: no emoji with id %s found in the db", id) + return nil, gtserror.NewErrorNotFound(err) + } + err := fmt.Errorf("EmojiUpdate: db error: %s", err) + return nil, gtserror.NewErrorInternalError(err) + } + + 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: + err := errors.New("unrecognized emoji action type") + return nil, gtserror.NewErrorBadRequest(err, err.Error()) + } +} + +// 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.db.GetEmojiCategories(ctx) + if err != nil { + err := fmt.Errorf("EmojiCategoriesGet: db error: %s", err) + return nil, gtserror.NewErrorInternalError(err) + } + + apiCategories := make([]*apimodel.EmojiCategory, 0, len(categories)) + for _, category := range categories { + apiCategory, err := p.tc.EmojiCategoryToAPIEmojiCategory(ctx, category) + if err != nil { + err := fmt.Errorf("EmojiCategoriesGet: error converting emoji category to api emoji category: %s", err) + return nil, gtserror.NewErrorInternalError(err) + } + apiCategories = append(apiCategories, apiCategory) + } + + return apiCategories, nil +} + +/* + UTIL FUNCTIONS +*/ + +func (p *Processor) getOrCreateEmojiCategory(ctx context.Context, name string) (*gtsmodel.EmojiCategory, error) { + category, err := p.db.GetEmojiCategoryByName(ctx, name) + if err == nil { + return category, nil + } + + if err != nil && !errors.Is(err, db.ErrNoEntries) { + err = fmt.Errorf("GetOrCreateEmojiCategory: database error trying get emoji category by name: %s", err) + return nil, err + } + + // we don't have the category yet, just create it with the given name + categoryID, err := id.NewRandomULID() + if err != nil { + err = fmt.Errorf("GetOrCreateEmojiCategory: error generating id for new emoji category: %s", err) + return nil, err + } + + category = >smodel.EmojiCategory{ + ID: categoryID, + Name: name, + } + + if err := p.db.PutEmojiCategory(ctx, category); err != nil { + err = fmt.Errorf("GetOrCreateEmojiCategory: error putting new emoji category in the database: %s", err) + return nil, err + } + + return category, nil +} + +// copy an emoji from remote to local +func (p *Processor) emojiUpdateCopy(ctx context.Context, emoji *gtsmodel.Emoji, shortcode *string, categoryName *string) (*apimodel.AdminEmoji, gtserror.WithCode) { + if emoji.Domain == "" { + err := fmt.Errorf("emojiUpdateCopy: emoji %s is not a remote emoji, cannot copy it to local", emoji.ID) + return nil, gtserror.NewErrorBadRequest(err, err.Error()) + } + + if shortcode == nil { + err := fmt.Errorf("emojiUpdateCopy: emoji %s could not be copied, no shortcode provided", emoji.ID) + return nil, gtserror.NewErrorBadRequest(err, err.Error()) + } + + maybeExisting, err := p.db.GetEmojiByShortcodeDomain(ctx, *shortcode, "") + if maybeExisting != nil { + err := fmt.Errorf("emojiUpdateCopy: emoji %s could not be copied, emoji with shortcode %s already exists on this instance", emoji.ID, *shortcode) + return nil, gtserror.NewErrorConflict(err, err.Error()) + } + + if err != nil && err != db.ErrNoEntries { + err := fmt.Errorf("emojiUpdateCopy: emoji %s could not be copied, error checking existence of emoji with shortcode %s: %s", emoji.ID, *shortcode, err) + return nil, gtserror.NewErrorInternalError(err) + } + + newEmojiID, err := id.NewRandomULID() + if err != nil { + err := fmt.Errorf("emojiUpdateCopy: emoji %s could not be copied, error creating id for new emoji: %s", emoji.ID, err) + return nil, gtserror.NewErrorInternalError(err) + } + + newEmojiURI := uris.GenerateURIForEmoji(newEmojiID) + + data := func(ctx context.Context) (reader io.ReadCloser, fileSize int64, err error) { + rc, err := p.storage.GetStream(ctx, emoji.ImagePath) + return rc, int64(emoji.ImageFileSize), err + } + + var ai *media.AdditionalEmojiInfo + if categoryName != nil { + category, err := p.getOrCreateEmojiCategory(ctx, *categoryName) + if err != nil { + err = fmt.Errorf("emojiUpdateCopy: error getting or creating category: %s", err) + return nil, gtserror.NewErrorInternalError(err) + } + + ai = &media.AdditionalEmojiInfo{ + CategoryID: &category.ID, + } + } + + processingEmoji, err := p.mediaManager.PreProcessEmoji(ctx, data, nil, *shortcode, newEmojiID, newEmojiURI, ai, false) + if err != nil { + err = fmt.Errorf("emojiUpdateCopy: error processing emoji %s: %s", emoji.ID, err) + return nil, gtserror.NewErrorInternalError(err) + } + + newEmoji, err := processingEmoji.LoadEmoji(ctx) + if err != nil { + err = fmt.Errorf("emojiUpdateCopy: error loading processed emoji %s: %s", emoji.ID, err) + return nil, gtserror.NewErrorInternalError(err) + } + + adminEmoji, err := p.tc.EmojiToAdminAPIEmoji(ctx, newEmoji) + if err != nil { + err = fmt.Errorf("emojiUpdateCopy: error converting updated emoji %s to admin emoji: %s", emoji.ID, err) + return nil, gtserror.NewErrorInternalError(err) + } + + return adminEmoji, nil +} + +// disable a remote emoji +func (p *Processor) emojiUpdateDisable(ctx context.Context, emoji *gtsmodel.Emoji) (*apimodel.AdminEmoji, gtserror.WithCode) { + if emoji.Domain == "" { + err := fmt.Errorf("emojiUpdateDisable: emoji %s is not a remote emoji, cannot disable it via this endpoint", emoji.ID) + return nil, gtserror.NewErrorBadRequest(err, err.Error()) + } + + emojiDisabled := true + emoji.Disabled = &emojiDisabled + updatedEmoji, err := p.db.UpdateEmoji(ctx, emoji, "updated_at", "disabled") + if err != nil { + err = fmt.Errorf("emojiUpdateDisable: error updating emoji %s: %s", emoji.ID, err) + return nil, gtserror.NewErrorInternalError(err) + } + + adminEmoji, err := p.tc.EmojiToAdminAPIEmoji(ctx, updatedEmoji) + if err != nil { + err = fmt.Errorf("emojiUpdateDisable: error converting updated emoji %s to admin emoji: %s", emoji.ID, err) + return nil, gtserror.NewErrorInternalError(err) + } + + return adminEmoji, nil +} + +// modify a local emoji +func (p *Processor) emojiUpdateModify(ctx context.Context, emoji *gtsmodel.Emoji, image *multipart.FileHeader, categoryName *string) (*apimodel.AdminEmoji, gtserror.WithCode) { + if emoji.Domain != "" { + err := fmt.Errorf("emojiUpdateModify: emoji %s is not a local emoji, cannot do a modify action on it", emoji.ID) + return nil, gtserror.NewErrorBadRequest(err, err.Error()) + } + + var updatedEmoji *gtsmodel.Emoji + + // keep existing categoryID unless a new one is defined + var ( + updatedCategoryID = emoji.CategoryID + updateCategoryID bool + ) + if categoryName != nil { + category, err := p.getOrCreateEmojiCategory(ctx, *categoryName) + if err != nil { + err = fmt.Errorf("emojiUpdateModify: error getting or creating category: %s", err) + return nil, gtserror.NewErrorInternalError(err) + } + + updatedCategoryID = category.ID + updateCategoryID = true + } + + // only update image if provided with one + var updateImage bool + if image != nil && image.Size != 0 { + updateImage = true + } + + if !updateImage { + // only updating fields, we only need + // to do a database update for this + columns := []string{"updated_at"} + + if updateCategoryID { + emoji.CategoryID = updatedCategoryID + columns = append(columns, "category_id") + } + + var err error + updatedEmoji, err = p.db.UpdateEmoji(ctx, emoji, columns...) + if err != nil { + err = fmt.Errorf("emojiUpdateModify: error updating emoji %s: %s", emoji.ID, err) + return nil, gtserror.NewErrorInternalError(err) + } + } else { + // new image, so we need to reprocess the emoji + data := func(ctx context.Context) (reader io.ReadCloser, fileSize int64, err error) { + i, err := image.Open() + return i, image.Size, err + } + + var ai *media.AdditionalEmojiInfo + if updateCategoryID { + ai = &media.AdditionalEmojiInfo{ + CategoryID: &updatedCategoryID, + } + } + + processingEmoji, err := p.mediaManager.PreProcessEmoji(ctx, data, nil, emoji.Shortcode, emoji.ID, emoji.URI, ai, true) + if err != nil { + err = fmt.Errorf("emojiUpdateModify: error processing emoji %s: %s", emoji.ID, err) + return nil, gtserror.NewErrorInternalError(err) + } + + updatedEmoji, err = processingEmoji.LoadEmoji(ctx) + if err != nil { + err = fmt.Errorf("emojiUpdateModify: error loading processed emoji %s: %s", emoji.ID, err) + return nil, gtserror.NewErrorInternalError(err) + } + } + + adminEmoji, err := p.tc.EmojiToAdminAPIEmoji(ctx, updatedEmoji) + if err != nil { + err = fmt.Errorf("emojiUpdateModify: error converting updated emoji %s to admin emoji: %s", emoji.ID, err) + return nil, gtserror.NewErrorInternalError(err) + } + + return adminEmoji, nil +} diff --git a/internal/processing/admin/emojicategory.go b/internal/processing/admin/emojicategory.go deleted file mode 100644 index 67bfece20..000000000 --- a/internal/processing/admin/emojicategory.go +++ /dev/null @@ -1,60 +0,0 @@ -/* - GoToSocial - Copyright (C) 2021-2023 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 admin - -import ( - "context" - "errors" - "fmt" - - "github.com/superseriousbusiness/gotosocial/internal/db" - "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" - "github.com/superseriousbusiness/gotosocial/internal/id" -) - -func (p *processor) GetOrCreateEmojiCategory(ctx context.Context, name string) (*gtsmodel.EmojiCategory, error) { - category, err := p.db.GetEmojiCategoryByName(ctx, name) - if err == nil { - return category, nil - } - - if err != nil && !errors.Is(err, db.ErrNoEntries) { - err = fmt.Errorf("GetOrCreateEmojiCategory: database error trying get emoji category by name: %s", err) - return nil, err - } - - // we don't have the category yet, just create it with the given name - categoryID, err := id.NewRandomULID() - if err != nil { - err = fmt.Errorf("GetOrCreateEmojiCategory: error generating id for new emoji category: %s", err) - return nil, err - } - - category = >smodel.EmojiCategory{ - ID: categoryID, - Name: name, - } - - if err := p.db.PutEmojiCategory(ctx, category); err != nil { - err = fmt.Errorf("GetOrCreateEmojiCategory: error putting new emoji category in the database: %s", err) - return nil, err - } - - return category, nil -} diff --git a/internal/processing/admin/getcategories.go b/internal/processing/admin/getcategories.go deleted file mode 100644 index 999898827..000000000 --- a/internal/processing/admin/getcategories.go +++ /dev/null @@ -1,47 +0,0 @@ -/* - GoToSocial - Copyright (C) 2021-2023 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 admin - -import ( - "context" - "fmt" - - apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" - "github.com/superseriousbusiness/gotosocial/internal/gtserror" -) - -func (p *processor) EmojiCategoriesGet(ctx context.Context) ([]*apimodel.EmojiCategory, gtserror.WithCode) { - categories, err := p.db.GetEmojiCategories(ctx) - if err != nil { - err := fmt.Errorf("EmojiCategoriesGet: db error: %s", err) - return nil, gtserror.NewErrorInternalError(err) - } - - apiCategories := make([]*apimodel.EmojiCategory, 0, len(categories)) - for _, category := range categories { - apiCategory, err := p.tc.EmojiCategoryToAPIEmojiCategory(ctx, category) - if err != nil { - err := fmt.Errorf("EmojiCategoriesGet: error converting emoji category to api emoji category: %s", err) - return nil, gtserror.NewErrorInternalError(err) - } - apiCategories = append(apiCategories, apiCategory) - } - - return apiCategories, nil -} diff --git a/internal/processing/admin/getdomainblock.go b/internal/processing/admin/getdomainblock.go deleted file mode 100644 index 073fd87ac..000000000 --- a/internal/processing/admin/getdomainblock.go +++ /dev/null @@ -1,49 +0,0 @@ -/* - GoToSocial - Copyright (C) 2021-2023 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 admin - -import ( - "context" - "fmt" - - apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" - "github.com/superseriousbusiness/gotosocial/internal/db" - "github.com/superseriousbusiness/gotosocial/internal/gtserror" - "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" -) - -func (p *processor) DomainBlockGet(ctx context.Context, account *gtsmodel.Account, id string, export bool) (*apimodel.DomainBlock, gtserror.WithCode) { - domainBlock := >smodel.DomainBlock{} - - if err := p.db.GetByID(ctx, id, domainBlock); err != nil { - if err != db.ErrNoEntries { - // something has gone really wrong - return nil, gtserror.NewErrorInternalError(err) - } - // there are no entries for this ID - return nil, gtserror.NewErrorNotFound(fmt.Errorf("no entry for ID %s", id)) - } - - apiDomainBlock, err := p.tc.DomainBlockToAPIDomainBlock(ctx, domainBlock, export) - if err != nil { - return nil, gtserror.NewErrorInternalError(err) - } - - return apiDomainBlock, nil -} diff --git a/internal/processing/admin/getdomainblocks.go b/internal/processing/admin/getdomainblocks.go deleted file mode 100644 index 2e8dcf881..000000000 --- a/internal/processing/admin/getdomainblocks.go +++ /dev/null @@ -1,50 +0,0 @@ -/* - GoToSocial - Copyright (C) 2021-2023 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 admin - -import ( - "context" - - apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" - "github.com/superseriousbusiness/gotosocial/internal/db" - "github.com/superseriousbusiness/gotosocial/internal/gtserror" - "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" -) - -func (p *processor) DomainBlocksGet(ctx context.Context, account *gtsmodel.Account, export bool) ([]*apimodel.DomainBlock, gtserror.WithCode) { - domainBlocks := []*gtsmodel.DomainBlock{} - - if err := p.db.GetAll(ctx, &domainBlocks); err != nil { - if err != db.ErrNoEntries { - // something has gone really wrong - return nil, gtserror.NewErrorInternalError(err) - } - } - - apiDomainBlocks := []*apimodel.DomainBlock{} - for _, b := range domainBlocks { - apiDomainBlock, err := p.tc.DomainBlockToAPIDomainBlock(ctx, b, export) - if err != nil { - return nil, gtserror.NewErrorInternalError(err) - } - apiDomainBlocks = append(apiDomainBlocks, apiDomainBlock) - } - - return apiDomainBlocks, nil -} diff --git a/internal/processing/admin/getemoji.go b/internal/processing/admin/getemoji.go deleted file mode 100644 index b37cc807f..000000000 --- a/internal/processing/admin/getemoji.go +++ /dev/null @@ -1,54 +0,0 @@ -/* - GoToSocial - Copyright (C) 2021-2023 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 admin - -import ( - "context" - "errors" - "fmt" - - apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" - "github.com/superseriousbusiness/gotosocial/internal/db" - "github.com/superseriousbusiness/gotosocial/internal/gtserror" - "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" -) - -func (p *processor) EmojiGet(ctx context.Context, account *gtsmodel.Account, user *gtsmodel.User, id string) (*apimodel.AdminEmoji, gtserror.WithCode) { - if !*user.Admin { - return nil, gtserror.NewErrorUnauthorized(fmt.Errorf("user %s not an admin", user.ID), "user is not an admin") - } - - emoji, err := p.db.GetEmojiByID(ctx, id) - if err != nil { - if errors.Is(err, db.ErrNoEntries) { - err = fmt.Errorf("EmojiGet: no emoji with id %s found in the db", id) - return nil, gtserror.NewErrorNotFound(err) - } - err := fmt.Errorf("EmojiGet: db error: %s", err) - return nil, gtserror.NewErrorInternalError(err) - } - - adminEmoji, err := p.tc.EmojiToAdminAPIEmoji(ctx, emoji) - if err != nil { - err = fmt.Errorf("EmojiGet: error converting emoji to admin api emoji: %s", err) - return nil, gtserror.NewErrorInternalError(err) - } - - return adminEmoji, nil -} diff --git a/internal/processing/admin/getemojis.go b/internal/processing/admin/getemojis.go deleted file mode 100644 index 7d3470dae..000000000 --- a/internal/processing/admin/getemojis.go +++ /dev/null @@ -1,97 +0,0 @@ -/* - GoToSocial - Copyright (C) 2021-2023 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 admin - -import ( - "context" - "errors" - "fmt" - "strings" - - apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" - "github.com/superseriousbusiness/gotosocial/internal/db" - "github.com/superseriousbusiness/gotosocial/internal/gtserror" - "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" - "github.com/superseriousbusiness/gotosocial/internal/util" -) - -func (p *processor) EmojisGet(ctx context.Context, account *gtsmodel.Account, user *gtsmodel.User, domain string, includeDisabled bool, includeEnabled bool, shortcode string, maxShortcodeDomain string, minShortcodeDomain string, limit int) (*apimodel.PageableResponse, gtserror.WithCode) { - if !*user.Admin { - return nil, gtserror.NewErrorUnauthorized(fmt.Errorf("user %s not an admin", user.ID), "user is not an admin") - } - - emojis, err := p.db.GetEmojis(ctx, domain, includeDisabled, includeEnabled, shortcode, maxShortcodeDomain, minShortcodeDomain, limit) - if err != nil && !errors.Is(err, db.ErrNoEntries) { - err := fmt.Errorf("EmojisGet: db error: %s", 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.tc.EmojiToAdminAPIEmoji(ctx, emoji) - if err != nil { - err := fmt.Errorf("EmojisGet: error converting emoji to admin model emoji: %s", err) - return nil, gtserror.NewErrorInternalError(err) - } - items = append(items, adminEmoji) - } - - filterBuilder := strings.Builder{} - filterBuilder.WriteString("filter=") - - switch domain { - case "", "local": - filterBuilder.WriteString("domain:local") - case db.EmojiAllDomains: - filterBuilder.WriteString("domain:all") - default: - filterBuilder.WriteString("domain:") - filterBuilder.WriteString(domain) - } - - if includeDisabled != includeEnabled { - if includeDisabled { - filterBuilder.WriteString(",disabled") - } - if includeEnabled { - filterBuilder.WriteString(",enabled") - } - } - - if shortcode != "" { - filterBuilder.WriteString(",shortcode:") - filterBuilder.WriteString(shortcode) - } - - return util.PackagePageableResponse(util.PageableResponseParams{ - Items: items, - Path: "api/v1/admin/custom_emojis", - NextMaxIDKey: "max_shortcode_domain", - NextMaxIDValue: util.ShortcodeDomain(emojis[count-1]), - PrevMinIDKey: "min_shortcode_domain", - PrevMinIDValue: util.ShortcodeDomain(emojis[0]), - Limit: limit, - ExtraQueryParams: []string{filterBuilder.String()}, - }) -} diff --git a/internal/processing/admin/getreport.go b/internal/processing/admin/getreport.go deleted file mode 100644 index 6c2f93935..000000000 --- a/internal/processing/admin/getreport.go +++ /dev/null @@ -1,45 +0,0 @@ -/* - GoToSocial - Copyright (C) 2021-2023 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 admin - -import ( - "context" - - apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" - "github.com/superseriousbusiness/gotosocial/internal/db" - "github.com/superseriousbusiness/gotosocial/internal/gtserror" - "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" -) - -func (p *processor) ReportGet(ctx context.Context, account *gtsmodel.Account, id string) (*apimodel.AdminReport, gtserror.WithCode) { - report, err := p.db.GetReportByID(ctx, id) - if err != nil { - if err == db.ErrNoEntries { - return nil, gtserror.NewErrorNotFound(err) - } - return nil, gtserror.NewErrorInternalError(err) - } - - apimodelReport, err := p.tc.ReportToAdminAPIReport(ctx, report, account) - if err != nil { - return nil, gtserror.NewErrorInternalError(err) - } - - return apimodelReport, nil -} diff --git a/internal/processing/admin/importdomainblocks.go b/internal/processing/admin/importdomainblocks.go deleted file mode 100644 index 5118b4826..000000000 --- a/internal/processing/admin/importdomainblocks.go +++ /dev/null @@ -1,66 +0,0 @@ -/* - GoToSocial - Copyright (C) 2021-2023 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 admin - -import ( - "bytes" - "context" - "encoding/json" - "errors" - "fmt" - "io" - "mime/multipart" - - apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" - "github.com/superseriousbusiness/gotosocial/internal/gtserror" - "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" -) - -// DomainBlocksImport handles the import of a bunch of domain blocks at once, by calling the DomainBlockCreate function for each domain in the provided file. -func (p *processor) DomainBlocksImport(ctx context.Context, account *gtsmodel.Account, domains *multipart.FileHeader) ([]*apimodel.DomainBlock, gtserror.WithCode) { - f, err := domains.Open() - if err != nil { - return nil, gtserror.NewErrorBadRequest(fmt.Errorf("DomainBlocksImport: error opening attachment: %s", err)) - } - buf := new(bytes.Buffer) - size, err := io.Copy(buf, f) - if err != nil { - return nil, gtserror.NewErrorBadRequest(fmt.Errorf("DomainBlocksImport: error reading attachment: %s", err)) - } - if size == 0 { - return nil, gtserror.NewErrorBadRequest(errors.New("DomainBlocksImport: could not read provided attachment: size 0 bytes")) - } - - d := []apimodel.DomainBlock{} - if err := json.Unmarshal(buf.Bytes(), &d); err != nil { - return nil, gtserror.NewErrorBadRequest(fmt.Errorf("DomainBlocksImport: could not read provided attachment: %s", err)) - } - - blocks := []*apimodel.DomainBlock{} - for _, d := range d { - block, err := p.DomainBlockCreate(ctx, account, d.Domain.Domain, false, d.PublicComment, "", "") - if err != nil { - return nil, err - } - - blocks = append(blocks, block) - } - - return blocks, nil -} diff --git a/internal/processing/admin/mediarefetch.go b/internal/processing/admin/media.go index a73580d98..6064e4300 100644 --- a/internal/processing/admin/mediarefetch.go +++ b/internal/processing/admin/media.go @@ -27,7 +27,8 @@ import ( "github.com/superseriousbusiness/gotosocial/internal/log" ) -func (p *processor) MediaRefetch(ctx context.Context, requestingAccount *gtsmodel.Account, domain string) gtserror.WithCode { +// MediaRefetch forces a refetch of remote emojis. +func (p *Processor) MediaRefetch(ctx context.Context, requestingAccount *gtsmodel.Account, domain string) gtserror.WithCode { transport, err := p.transportController.NewTransportForUsername(ctx, requestingAccount.Username) if err != nil { err = fmt.Errorf("error getting transport for user %s during media refetch request: %w", requestingAccount.Username, err) @@ -46,3 +47,18 @@ func (p *processor) MediaRefetch(ctx context.Context, requestingAccount *gtsmode return nil } + +// MediaPrune triggers a non-blocking prune of remote media, local unused media, etc. +func (p *Processor) MediaPrune(ctx context.Context, mediaRemoteCacheDays int) gtserror.WithCode { + if mediaRemoteCacheDays < 0 { + err := fmt.Errorf("MediaPrune: invalid value for mediaRemoteCacheDays prune: value was %d, cannot be less than 0", mediaRemoteCacheDays) + return gtserror.NewErrorBadRequest(err, err.Error()) + } + + if err := p.mediaManager.PruneAll(ctx, mediaRemoteCacheDays, false); err != nil { + err = fmt.Errorf("MediaPrune: %w", err) + return gtserror.NewErrorInternalError(err) + } + + return nil +} diff --git a/internal/processing/admin/mediaprune.go b/internal/processing/admin/mediaprune.go deleted file mode 100644 index c8157d576..000000000 --- a/internal/processing/admin/mediaprune.go +++ /dev/null @@ -1,40 +0,0 @@ -/* - GoToSocial - Copyright (C) 2021-2023 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 admin - -import ( - "context" - "fmt" - - "github.com/superseriousbusiness/gotosocial/internal/gtserror" -) - -func (p *processor) MediaPrune(ctx context.Context, mediaRemoteCacheDays int) gtserror.WithCode { - if mediaRemoteCacheDays < 0 { - err := fmt.Errorf("MediaPrune: invalid value for mediaRemoteCacheDays prune: value was %d, cannot be less than 0", mediaRemoteCacheDays) - return gtserror.NewErrorBadRequest(err, err.Error()) - } - - if err := p.mediaManager.PruneAll(ctx, mediaRemoteCacheDays, false); err != nil { - err = fmt.Errorf("MediaPrune: %w", err) - return gtserror.NewErrorInternalError(err) - } - - return nil -} diff --git a/internal/processing/admin/getreports.go b/internal/processing/admin/report.go index fbc4b45b2..3a6028bca 100644 --- a/internal/processing/admin/getreports.go +++ b/internal/processing/admin/report.go @@ -22,6 +22,7 @@ import ( "context" "fmt" "strconv" + "time" apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" "github.com/superseriousbusiness/gotosocial/internal/db" @@ -30,7 +31,8 @@ import ( "github.com/superseriousbusiness/gotosocial/internal/util" ) -func (p *processor) ReportsGet( +// ReportsGet returns all reports stored on this instance, with the given parameters. +func (p *Processor) ReportsGet( ctx context.Context, account *gtsmodel.Account, resolved *bool, @@ -90,3 +92,57 @@ func (p *processor) ReportsGet( ExtraQueryParams: extraQueryParams, }) } + +// ReportGet returns one report, with the given ID. +func (p *Processor) ReportGet(ctx context.Context, account *gtsmodel.Account, id string) (*apimodel.AdminReport, gtserror.WithCode) { + report, err := p.db.GetReportByID(ctx, id) + if err != nil { + if err == db.ErrNoEntries { + return nil, gtserror.NewErrorNotFound(err) + } + return nil, gtserror.NewErrorInternalError(err) + } + + apimodelReport, err := p.tc.ReportToAdminAPIReport(ctx, report, account) + if err != nil { + return nil, gtserror.NewErrorInternalError(err) + } + + return apimodelReport, nil +} + +// ReportResolve marks a report with the given id as resolved, and stores the provided actionTakenComment (if not null). +func (p *Processor) ReportResolve(ctx context.Context, account *gtsmodel.Account, id string, actionTakenComment *string) (*apimodel.AdminReport, gtserror.WithCode) { + report, err := p.db.GetReportByID(ctx, id) + if err != nil { + if err == db.ErrNoEntries { + return nil, gtserror.NewErrorNotFound(err) + } + return nil, gtserror.NewErrorInternalError(err) + } + + columns := []string{ + "action_taken_at", + "action_taken_by_account_id", + } + + report.ActionTakenAt = time.Now() + report.ActionTakenByAccountID = account.ID + + if actionTakenComment != nil { + report.ActionTaken = *actionTakenComment + columns = append(columns, "action_taken") + } + + updatedReport, err := p.db.UpdateReport(ctx, report, columns...) + if err != nil { + return nil, gtserror.NewErrorInternalError(err) + } + + apimodelReport, err := p.tc.ReportToAdminAPIReport(ctx, updatedReport, account) + if err != nil { + return nil, gtserror.NewErrorInternalError(err) + } + + return apimodelReport, nil +} diff --git a/internal/processing/admin/resolvereport.go b/internal/processing/admin/resolvereport.go deleted file mode 100644 index 5c1dca1b0..000000000 --- a/internal/processing/admin/resolvereport.go +++ /dev/null @@ -1,64 +0,0 @@ -/* - GoToSocial - Copyright (C) 2021-2023 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 admin - -import ( - "context" - "time" - - apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" - "github.com/superseriousbusiness/gotosocial/internal/db" - "github.com/superseriousbusiness/gotosocial/internal/gtserror" - "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" -) - -func (p *processor) ReportResolve(ctx context.Context, account *gtsmodel.Account, id string, actionTakenComment *string) (*apimodel.AdminReport, gtserror.WithCode) { - report, err := p.db.GetReportByID(ctx, id) - if err != nil { - if err == db.ErrNoEntries { - return nil, gtserror.NewErrorNotFound(err) - } - return nil, gtserror.NewErrorInternalError(err) - } - - columns := []string{ - "action_taken_at", - "action_taken_by_account_id", - } - - report.ActionTakenAt = time.Now() - report.ActionTakenByAccountID = account.ID - - if actionTakenComment != nil { - report.ActionTaken = *actionTakenComment - columns = append(columns, "action_taken") - } - - updatedReport, err := p.db.UpdateReport(ctx, report, columns...) - if err != nil { - return nil, gtserror.NewErrorInternalError(err) - } - - apimodelReport, err := p.tc.ReportToAdminAPIReport(ctx, updatedReport, account) - if err != nil { - return nil, gtserror.NewErrorInternalError(err) - } - - return apimodelReport, nil -} diff --git a/internal/processing/admin/updateemoji.go b/internal/processing/admin/updateemoji.go deleted file mode 100644 index 41ccd609c..000000000 --- a/internal/processing/admin/updateemoji.go +++ /dev/null @@ -1,236 +0,0 @@ -/* - GoToSocial - Copyright (C) 2021-2023 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 admin - -import ( - "context" - "errors" - "fmt" - "io" - "mime/multipart" - - apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" - "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/uris" -) - -func (p *processor) EmojiUpdate(ctx context.Context, id string, form *apimodel.EmojiUpdateRequest) (*apimodel.AdminEmoji, gtserror.WithCode) { - emoji, err := p.db.GetEmojiByID(ctx, id) - if err != nil { - if errors.Is(err, db.ErrNoEntries) { - err = fmt.Errorf("EmojiUpdate: no emoji with id %s found in the db", id) - return nil, gtserror.NewErrorNotFound(err) - } - err := fmt.Errorf("EmojiUpdate: db error: %s", err) - return nil, gtserror.NewErrorInternalError(err) - } - - 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: - err := errors.New("unrecognized emoji action type") - return nil, gtserror.NewErrorBadRequest(err, err.Error()) - } -} - -// copy an emoji from remote to local -func (p *processor) emojiUpdateCopy(ctx context.Context, emoji *gtsmodel.Emoji, shortcode *string, categoryName *string) (*apimodel.AdminEmoji, gtserror.WithCode) { - if emoji.Domain == "" { - err := fmt.Errorf("emojiUpdateCopy: emoji %s is not a remote emoji, cannot copy it to local", emoji.ID) - return nil, gtserror.NewErrorBadRequest(err, err.Error()) - } - - if shortcode == nil { - err := fmt.Errorf("emojiUpdateCopy: emoji %s could not be copied, no shortcode provided", emoji.ID) - return nil, gtserror.NewErrorBadRequest(err, err.Error()) - } - - maybeExisting, err := p.db.GetEmojiByShortcodeDomain(ctx, *shortcode, "") - if maybeExisting != nil { - err := fmt.Errorf("emojiUpdateCopy: emoji %s could not be copied, emoji with shortcode %s already exists on this instance", emoji.ID, *shortcode) - return nil, gtserror.NewErrorConflict(err, err.Error()) - } - - if err != nil && err != db.ErrNoEntries { - err := fmt.Errorf("emojiUpdateCopy: emoji %s could not be copied, error checking existence of emoji with shortcode %s: %s", emoji.ID, *shortcode, err) - return nil, gtserror.NewErrorInternalError(err) - } - - newEmojiID, err := id.NewRandomULID() - if err != nil { - err := fmt.Errorf("emojiUpdateCopy: emoji %s could not be copied, error creating id for new emoji: %s", emoji.ID, err) - return nil, gtserror.NewErrorInternalError(err) - } - - newEmojiURI := uris.GenerateURIForEmoji(newEmojiID) - - data := func(ctx context.Context) (reader io.ReadCloser, fileSize int64, err error) { - rc, err := p.storage.GetStream(ctx, emoji.ImagePath) - return rc, int64(emoji.ImageFileSize), err - } - - var ai *media.AdditionalEmojiInfo - if categoryName != nil { - category, err := p.GetOrCreateEmojiCategory(ctx, *categoryName) - if err != nil { - err = fmt.Errorf("emojiUpdateCopy: error getting or creating category: %s", err) - return nil, gtserror.NewErrorInternalError(err) - } - - ai = &media.AdditionalEmojiInfo{ - CategoryID: &category.ID, - } - } - - processingEmoji, err := p.mediaManager.PreProcessEmoji(ctx, data, nil, *shortcode, newEmojiID, newEmojiURI, ai, false) - if err != nil { - err = fmt.Errorf("emojiUpdateCopy: error processing emoji %s: %s", emoji.ID, err) - return nil, gtserror.NewErrorInternalError(err) - } - - newEmoji, err := processingEmoji.LoadEmoji(ctx) - if err != nil { - err = fmt.Errorf("emojiUpdateCopy: error loading processed emoji %s: %s", emoji.ID, err) - return nil, gtserror.NewErrorInternalError(err) - } - - adminEmoji, err := p.tc.EmojiToAdminAPIEmoji(ctx, newEmoji) - if err != nil { - err = fmt.Errorf("emojiUpdateCopy: error converting updated emoji %s to admin emoji: %s", emoji.ID, err) - return nil, gtserror.NewErrorInternalError(err) - } - - return adminEmoji, nil -} - -// disable a remote emoji -func (p *processor) emojiUpdateDisable(ctx context.Context, emoji *gtsmodel.Emoji) (*apimodel.AdminEmoji, gtserror.WithCode) { - if emoji.Domain == "" { - err := fmt.Errorf("emojiUpdateDisable: emoji %s is not a remote emoji, cannot disable it via this endpoint", emoji.ID) - return nil, gtserror.NewErrorBadRequest(err, err.Error()) - } - - emojiDisabled := true - emoji.Disabled = &emojiDisabled - updatedEmoji, err := p.db.UpdateEmoji(ctx, emoji, "updated_at", "disabled") - if err != nil { - err = fmt.Errorf("emojiUpdateDisable: error updating emoji %s: %s", emoji.ID, err) - return nil, gtserror.NewErrorInternalError(err) - } - - adminEmoji, err := p.tc.EmojiToAdminAPIEmoji(ctx, updatedEmoji) - if err != nil { - err = fmt.Errorf("emojiUpdateDisable: error converting updated emoji %s to admin emoji: %s", emoji.ID, err) - return nil, gtserror.NewErrorInternalError(err) - } - - return adminEmoji, nil -} - -// modify a local emoji -func (p *processor) emojiUpdateModify(ctx context.Context, emoji *gtsmodel.Emoji, image *multipart.FileHeader, categoryName *string) (*apimodel.AdminEmoji, gtserror.WithCode) { - if emoji.Domain != "" { - err := fmt.Errorf("emojiUpdateModify: emoji %s is not a local emoji, cannot do a modify action on it", emoji.ID) - return nil, gtserror.NewErrorBadRequest(err, err.Error()) - } - - var updatedEmoji *gtsmodel.Emoji - - // keep existing categoryID unless a new one is defined - var ( - updatedCategoryID = emoji.CategoryID - updateCategoryID bool - ) - if categoryName != nil { - category, err := p.GetOrCreateEmojiCategory(ctx, *categoryName) - if err != nil { - err = fmt.Errorf("emojiUpdateModify: error getting or creating category: %s", err) - return nil, gtserror.NewErrorInternalError(err) - } - - updatedCategoryID = category.ID - updateCategoryID = true - } - - // only update image if provided with one - var updateImage bool - if image != nil && image.Size != 0 { - updateImage = true - } - - if !updateImage { - // only updating fields, we only need - // to do a database update for this - columns := []string{"updated_at"} - - if updateCategoryID { - emoji.CategoryID = updatedCategoryID - columns = append(columns, "category_id") - } - - var err error - updatedEmoji, err = p.db.UpdateEmoji(ctx, emoji, columns...) - if err != nil { - err = fmt.Errorf("emojiUpdateModify: error updating emoji %s: %s", emoji.ID, err) - return nil, gtserror.NewErrorInternalError(err) - } - } else { - // new image, so we need to reprocess the emoji - data := func(ctx context.Context) (reader io.ReadCloser, fileSize int64, err error) { - i, err := image.Open() - return i, image.Size, err - } - - var ai *media.AdditionalEmojiInfo - if updateCategoryID { - ai = &media.AdditionalEmojiInfo{ - CategoryID: &updatedCategoryID, - } - } - - processingEmoji, err := p.mediaManager.PreProcessEmoji(ctx, data, nil, emoji.Shortcode, emoji.ID, emoji.URI, ai, true) - if err != nil { - err = fmt.Errorf("emojiUpdateModify: error processing emoji %s: %s", emoji.ID, err) - return nil, gtserror.NewErrorInternalError(err) - } - - updatedEmoji, err = processingEmoji.LoadEmoji(ctx) - if err != nil { - err = fmt.Errorf("emojiUpdateModify: error loading processed emoji %s: %s", emoji.ID, err) - return nil, gtserror.NewErrorInternalError(err) - } - } - - adminEmoji, err := p.tc.EmojiToAdminAPIEmoji(ctx, updatedEmoji) - if err != nil { - err = fmt.Errorf("emojiUpdateModify: error converting updated emoji %s to admin emoji: %s", emoji.ID, err) - return nil, gtserror.NewErrorInternalError(err) - } - - return adminEmoji, nil -} |