mirror of
https://github.com/superseriousbusiness/gotosocial.git
synced 2025-11-10 14:17:29 -06:00
start refactoring return codes from fedi endpoints, remove some cruft
This commit is contained in:
parent
c6044d0142
commit
47051a26d6
28 changed files with 346 additions and 291 deletions
|
|
@ -19,38 +19,69 @@ package fedi
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"errors"
|
||||
|
||||
"code.superseriousbusiness.org/gotosocial/internal/ap"
|
||||
"code.superseriousbusiness.org/gotosocial/internal/config"
|
||||
"code.superseriousbusiness.org/gotosocial/internal/db"
|
||||
"code.superseriousbusiness.org/gotosocial/internal/gtserror"
|
||||
)
|
||||
|
||||
// EmojiGet handles the GET for a federated emoji originating from this instance.
|
||||
func (p *Processor) EmojiGet(ctx context.Context, requestedEmojiID string) (interface{}, gtserror.WithCode) {
|
||||
if _, errWithCode := p.federator.AuthenticateFederatedRequest(ctx, ""); errWithCode != nil {
|
||||
// EmojiGet handles the GET for an emoji originating from this instance.
|
||||
func (p *Processor) EmojiGet(ctx context.Context, emojiID string) (any, gtserror.WithCode) {
|
||||
// Authenticate incoming request.
|
||||
//
|
||||
// Pass hostname string to this function to indicate
|
||||
// it's the instance account being requested, as
|
||||
// emojis are always owned by the instance account.
|
||||
auth, errWithCode := p.authenticate(ctx, config.GetHost())
|
||||
if errWithCode != nil {
|
||||
return nil, errWithCode
|
||||
}
|
||||
|
||||
requestedEmoji, err := p.state.DB.GetEmojiByID(ctx, requestedEmojiID)
|
||||
if auth.handshakingURI != nil {
|
||||
// We're currently handshaking, which means
|
||||
// we don't know this account yet. This should
|
||||
// be a very rare race condition.
|
||||
err := gtserror.Newf("network race handshaking %s", auth.handshakingURI)
|
||||
return nil, gtserror.NewErrorInternalError(err)
|
||||
}
|
||||
|
||||
// Get the requested emoji.
|
||||
emoji, err := p.state.DB.GetEmojiByID(ctx, emojiID)
|
||||
if err != nil && !errors.Is(err, db.ErrNoEntries) {
|
||||
err := gtserror.Newf("db error getting emoji %s: %w", emojiID, err)
|
||||
return nil, gtserror.NewErrorNotFound(err)
|
||||
}
|
||||
|
||||
if emoji == nil {
|
||||
err := gtserror.Newf("emoji %s not found in the db", emojiID)
|
||||
return nil, gtserror.NewErrorNotFound(err)
|
||||
}
|
||||
|
||||
// Only serve *our*
|
||||
// emojis on this path.
|
||||
if !emoji.IsLocal() {
|
||||
err := gtserror.Newf("emoji %s doesn't belong to this instance (domain is %s)", emojiID, emoji.Domain)
|
||||
return nil, gtserror.NewErrorNotFound(err)
|
||||
}
|
||||
|
||||
// Don't serve emojis that have
|
||||
// been disabled by an admin.
|
||||
if *emoji.Disabled {
|
||||
err := gtserror.Newf("emoji with id %s has been disabled by an admin", emojiID)
|
||||
return nil, gtserror.NewErrorNotFound(err)
|
||||
}
|
||||
|
||||
apEmoji, err := p.converter.EmojiToAS(ctx, emoji)
|
||||
if err != nil {
|
||||
return nil, gtserror.NewErrorNotFound(fmt.Errorf("database error getting emoji with id %s: %s", requestedEmojiID, err))
|
||||
}
|
||||
|
||||
if !requestedEmoji.IsLocal() {
|
||||
return nil, gtserror.NewErrorNotFound(fmt.Errorf("emoji with id %s doesn't belong to this instance (domain %s)", requestedEmojiID, requestedEmoji.Domain))
|
||||
}
|
||||
|
||||
if *requestedEmoji.Disabled {
|
||||
return nil, gtserror.NewErrorNotFound(fmt.Errorf("emoji with id %s has been disabled", requestedEmojiID))
|
||||
}
|
||||
|
||||
apEmoji, err := p.converter.EmojiToAS(ctx, requestedEmoji)
|
||||
if err != nil {
|
||||
return nil, gtserror.NewErrorInternalError(fmt.Errorf("error converting gtsmodel emoji with id %s to ap emoji: %s", requestedEmojiID, err))
|
||||
err := gtserror.Newf("error converting emoji %s to ap: %s", emojiID, err)
|
||||
return nil, gtserror.NewErrorInternalError(err)
|
||||
}
|
||||
|
||||
data, err := ap.Serialize(apEmoji)
|
||||
if err != nil {
|
||||
err := gtserror.Newf("error serializing emoji %s: %w", emojiID, err)
|
||||
return nil, gtserror.NewErrorInternalError(err)
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue