start refactoring return codes from fedi endpoints, remove some cruft

This commit is contained in:
tobi 2025-10-08 13:14:06 +02:00
commit 47051a26d6
28 changed files with 346 additions and 291 deletions

View file

@ -20,16 +20,12 @@ package emoji
import (
"net/http"
apiutil "code.superseriousbusiness.org/gotosocial/internal/api/util"
"code.superseriousbusiness.org/gotosocial/internal/processing"
"github.com/gin-gonic/gin"
)
const (
// EmojiIDKey is for emoji IDs
EmojiIDKey = "id"
// EmojiBasePath is the base path for serving AP Emojis, minus the "emoji" prefix
EmojiWithIDPath = "/:" + EmojiIDKey
)
const EmojiWithIDPath = "/:" + apiutil.IDKey
type Module struct {
processor *processing.Processor

View file

@ -28,8 +28,8 @@ import (
)
func (m *Module) EmojiGetHandler(c *gin.Context) {
requestedEmojiID := strings.ToUpper(c.Param(EmojiIDKey))
if requestedEmojiID == "" {
emojiID := strings.ToUpper(c.Param(apiutil.IDKey))
if emojiID == "" {
err := errors.New("no emoji id specified in request")
apiutil.ErrorHandler(c, gtserror.NewErrorBadRequest(err, err.Error()), m.processor.InstanceGetV1)
return
@ -41,7 +41,7 @@ func (m *Module) EmojiGetHandler(c *gin.Context) {
return
}
resp, errWithCode := m.processor.Fedi().EmojiGet(c.Request.Context(), requestedEmojiID)
resp, errWithCode := m.processor.Fedi().EmojiGet(c.Request.Context(), emojiID)
if errWithCode != nil {
apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1)
return

View file

@ -25,6 +25,7 @@ import (
"code.superseriousbusiness.org/gotosocial/internal/admin"
"code.superseriousbusiness.org/gotosocial/internal/api/activitypub/emoji"
apiutil "code.superseriousbusiness.org/gotosocial/internal/api/util"
"code.superseriousbusiness.org/gotosocial/internal/db"
"code.superseriousbusiness.org/gotosocial/internal/email"
"code.superseriousbusiness.org/gotosocial/internal/federation"
@ -122,7 +123,7 @@ func (suite *EmojiGetTestSuite) TestGetEmoji() {
// but because we're calling the function directly, we need to set them manually.
ctx.Params = gin.Params{
gin.Param{
Key: emoji.EmojiIDKey,
Key: apiutil.IDKey,
Value: targetEmoji.ID,
},
}

View file

@ -20,17 +20,14 @@ package publickey
import (
"net/http"
apiutil "code.superseriousbusiness.org/gotosocial/internal/api/util"
"code.superseriousbusiness.org/gotosocial/internal/processing"
"code.superseriousbusiness.org/gotosocial/internal/uris"
"github.com/gin-gonic/gin"
)
const (
// UsernameKey is for account usernames.
UsernameKey = "username"
// PublicKeyPath is a path to a user's public key, for serving bare minimum AP representations.
PublicKeyPath = "users/:" + UsernameKey + "/" + uris.PublicKeyPath
)
const PublicKeyPath = "users/:" + apiutil.UsernameKey + "/" + uris.PublicKeyPath
type Module struct {
processor *processing.Processor

View file

@ -34,7 +34,7 @@ import (
// public key, username, and type of the account.
func (m *Module) PublicKeyGETHandler(c *gin.Context) {
// usernames on our instance are always lowercase
requestedUsername := strings.ToLower(c.Param(UsernameKey))
requestedUsername := strings.ToLower(c.Param(apiutil.UsernameKey))
if requestedUsername == "" {
err := errors.New("no username specified in request")
apiutil.ErrorHandler(c, gtserror.NewErrorBadRequest(err, err.Error()), m.processor.InstanceGetV1)
@ -47,13 +47,17 @@ func (m *Module) PublicKeyGETHandler(c *gin.Context) {
return
}
// If HTML is requested, redirect
// to user's profile instead.
if contentType == string(apiutil.TextHTML) {
// redirect to the user's profile
c.Redirect(http.StatusSeeOther, "/@"+requestedUsername)
return
}
resp, errWithCode := m.processor.Fedi().UserGet(c.Request.Context(), requestedUsername, c.Request.URL)
resp, errWithCode := m.processor.Fedi().UserGetMinimal(
c.Request.Context(),
requestedUsername,
)
if errWithCode != nil {
apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1)
return

View file

@ -24,7 +24,7 @@ type SwaggerCollection struct {
// A string or an array of strings, or more
// complex nested items.
// example: https://www.w3.org/ns/activitystreams
Context interface{} `json:"@context"`
Context any `json:"@context"`
// ActivityStreams ID.
// example: https://example.org/users/some_user/statuses/106717595988259568/replies
ID string `json:"id"`
@ -64,7 +64,7 @@ type SwaggerFeaturedCollection struct {
// A string or an array of strings, or more
// complex nested items.
// example: https://www.w3.org/ns/activitystreams
Context interface{} `json:"@context"`
Context any `json:"@context"`
// ActivityStreams ID.
// example: https://example.org/users/some_user/collections/featured
ID string `json:"id"`

View file

@ -67,7 +67,7 @@ import (
// description: not found
func (m *Module) FeaturedCollectionGETHandler(c *gin.Context) {
// usernames on our instance are always lowercase
requestedUsername := strings.ToLower(c.Param(UsernameKey))
requestedUsername := strings.ToLower(c.Param(apiutil.UsernameKey))
if requestedUsername == "" {
err := errors.New("no username specified in request")
apiutil.ErrorHandler(c, gtserror.NewErrorBadRequest(err, err.Error()), m.processor.InstanceGetV1)

View file

@ -31,7 +31,7 @@ import (
// FollowersGETHandler returns a collection of URIs for followers of the target user, formatted so that other AP servers can understand it.
func (m *Module) FollowersGETHandler(c *gin.Context) {
// usernames on our instance are always lowercase
requestedUsername := strings.ToLower(c.Param(UsernameKey))
requestedUsername := strings.ToLower(c.Param(apiutil.UsernameKey))
if requestedUsername == "" {
err := errors.New("no username specified in request")
apiutil.ErrorHandler(c, gtserror.NewErrorBadRequest(err, err.Error()), m.processor.InstanceGetV1)

View file

@ -31,7 +31,7 @@ import (
// FollowingGETHandler returns a collection of URIs for accounts that the target user follows, formatted so that other AP servers can understand it.
func (m *Module) FollowingGETHandler(c *gin.Context) {
// usernames on our instance are always lowercase
requestedUsername := strings.ToLower(c.Param(UsernameKey))
requestedUsername := strings.ToLower(c.Param(apiutil.UsernameKey))
if requestedUsername == "" {
err := errors.New("no username specified in request")
apiutil.ErrorHandler(c, gtserror.NewErrorBadRequest(err, err.Error()), m.processor.InstanceGetV1)

View file

@ -31,7 +31,7 @@ import (
"code.superseriousbusiness.org/activity/streams"
"code.superseriousbusiness.org/activity/streams/vocab"
"code.superseriousbusiness.org/gotosocial/internal/ap"
"code.superseriousbusiness.org/gotosocial/internal/api/activitypub/users"
apiutil "code.superseriousbusiness.org/gotosocial/internal/api/util"
"code.superseriousbusiness.org/gotosocial/internal/db"
"code.superseriousbusiness.org/gotosocial/internal/gtserror"
"code.superseriousbusiness.org/gotosocial/internal/gtsmodel"
@ -79,7 +79,7 @@ func (suite *InboxPostTestSuite) inboxPost(
)
// Put the request together.
ctx.AddParam(users.UsernameKey, targetAccount.Username)
ctx.AddParam(apiutil.UsernameKey, targetAccount.Username)
ctx.Request = httptest.NewRequest(http.MethodPost, targetAccount.InboxURI, bytes.NewReader(b))
ctx.Request.Header.Set("Signature", signature)
ctx.Request.Header.Set("Date", dateHeader)

View file

@ -84,7 +84,7 @@ import (
// description: not found
func (m *Module) OutboxGETHandler(c *gin.Context) {
// usernames on our instance are always lowercase
requestedUsername := strings.ToLower(c.Param(UsernameKey))
requestedUsername := strings.ToLower(c.Param(apiutil.UsernameKey))
if requestedUsername == "" {
err := errors.New("no username specified in request")
apiutil.ErrorHandler(c, gtserror.NewErrorBadRequest(err, err.Error()), m.processor.InstanceGetV1)

View file

@ -28,7 +28,7 @@ import (
"code.superseriousbusiness.org/activity/streams"
"code.superseriousbusiness.org/activity/streams/vocab"
"code.superseriousbusiness.org/gotosocial/internal/api/activitypub/users"
apiutil "code.superseriousbusiness.org/gotosocial/internal/api/util"
"code.superseriousbusiness.org/gotosocial/testrig"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/suite"
@ -59,7 +59,7 @@ func (suite *OutboxGetTestSuite) TestGetOutbox() {
// but because we're calling the function directly, we need to set them manually.
ctx.Params = gin.Params{
gin.Param{
Key: users.UsernameKey,
Key: apiutil.UsernameKey,
Value: targetAccount.Username,
},
}
@ -85,7 +85,7 @@ func (suite *OutboxGetTestSuite) TestGetOutbox() {
"type": "OrderedCollection"
}`, dst.String())
m := make(map[string]interface{})
m := make(map[string]any)
err = json.Unmarshal(b, &m)
suite.NoError(err)
@ -117,7 +117,7 @@ func (suite *OutboxGetTestSuite) TestGetOutboxFirstPage() {
// but because we're calling the function directly, we need to set them manually.
ctx.Params = gin.Params{
gin.Param{
Key: users.UsernameKey,
Key: apiutil.UsernameKey,
Value: targetAccount.Username,
},
}
@ -172,7 +172,7 @@ func (suite *OutboxGetTestSuite) TestGetOutboxFirstPage() {
"type": "OrderedCollectionPage"
}`, dst.String())
m := make(map[string]interface{})
m := make(map[string]any)
err = json.Unmarshal(b, &m)
suite.NoError(err)
@ -204,11 +204,11 @@ func (suite *OutboxGetTestSuite) TestGetOutboxNextPage() {
// but because we're calling the function directly, we need to set them manually.
ctx.Params = gin.Params{
gin.Param{
Key: users.UsernameKey,
Key: apiutil.UsernameKey,
Value: targetAccount.Username,
},
gin.Param{
Key: users.MaxIDKey,
Key: apiutil.MaxIDKey,
Value: "01F8MHAMCHF6Y650WCRSCP4WMY",
},
}
@ -235,7 +235,7 @@ func (suite *OutboxGetTestSuite) TestGetOutboxNextPage() {
"type": "OrderedCollectionPage"
}`, dst.String())
m := make(map[string]interface{})
m := make(map[string]any)
err = json.Unmarshal(b, &m)
suite.NoError(err)
@ -261,7 +261,7 @@ func checkDropPublished(t *testing.T, b []byte, at ...string) []byte {
entries := make([]map[string]any, 0)
for _, key := range at {
switch vt := m[key].(type) {
case []interface{}:
case []any:
for _, t := range vt {
if entry, ok := t.(map[string]any); ok {
entries = append(entries, entry)

View file

@ -91,7 +91,7 @@ import (
// description: not found
func (m *Module) StatusRepliesGETHandler(c *gin.Context) {
// usernames on our instance are always lowercase
requestedUsername := strings.ToLower(c.Param(UsernameKey))
requestedUsername := strings.ToLower(c.Param(apiutil.UsernameKey))
if requestedUsername == "" {
err := errors.New("no username specified in request")
apiutil.ErrorHandler(c, gtserror.NewErrorBadRequest(err, err.Error()), m.processor.InstanceGetV1)
@ -99,7 +99,7 @@ func (m *Module) StatusRepliesGETHandler(c *gin.Context) {
}
// status IDs on our instance are always uppercase
requestedStatusID := strings.ToUpper(c.Param(StatusIDKey))
requestedStatusID := strings.ToUpper(c.Param(apiutil.IDKey))
if requestedStatusID == "" {
err := errors.New("no status id specified in request")
apiutil.ErrorHandler(c, gtserror.NewErrorBadRequest(err, err.Error()), m.processor.InstanceGetV1)

View file

@ -28,7 +28,7 @@ import (
"code.superseriousbusiness.org/activity/streams"
"code.superseriousbusiness.org/activity/streams/vocab"
"code.superseriousbusiness.org/gotosocial/internal/ap"
"code.superseriousbusiness.org/gotosocial/internal/api/activitypub/users"
apiutil "code.superseriousbusiness.org/gotosocial/internal/api/util"
"code.superseriousbusiness.org/gotosocial/testrig"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
@ -61,11 +61,11 @@ func (suite *RepliesGetTestSuite) TestGetReplies() {
// but because we're calling the function directly, we need to set them manually.
ctx.Params = gin.Params{
gin.Param{
Key: users.UsernameKey,
Key: apiutil.UsernameKey,
Value: targetAccount.Username,
},
gin.Param{
Key: users.StatusIDKey,
Key: apiutil.IDKey,
Value: targetStatus.ID,
},
}
@ -96,7 +96,7 @@ func (suite *RepliesGetTestSuite) TestGetReplies() {
})
assert.Equal(suite.T(), expect, string(b))
m := make(map[string]interface{})
m := make(map[string]any)
err = json.Unmarshal(b, &m)
assert.NoError(suite.T(), err)
@ -129,11 +129,11 @@ func (suite *RepliesGetTestSuite) TestGetRepliesNext() {
// but because we're calling the function directly, we need to set them manually.
ctx.Params = gin.Params{
gin.Param{
Key: users.UsernameKey,
Key: apiutil.UsernameKey,
Value: targetAccount.Username,
},
gin.Param{
Key: users.StatusIDKey,
Key: apiutil.IDKey,
Value: targetStatus.ID,
},
}
@ -167,7 +167,7 @@ func (suite *RepliesGetTestSuite) TestGetRepliesNext() {
})
assert.Equal(suite.T(), expect, string(b))
m := make(map[string]interface{})
m := make(map[string]any)
err = json.Unmarshal(b, &m)
assert.NoError(suite.T(), err)
@ -202,11 +202,11 @@ func (suite *RepliesGetTestSuite) TestGetRepliesLast() {
// but because we're calling the function directly, we need to set them manually.
ctx.Params = gin.Params{
gin.Param{
Key: users.UsernameKey,
Key: apiutil.UsernameKey,
Value: targetAccount.Username,
},
gin.Param{
Key: users.StatusIDKey,
Key: apiutil.IDKey,
Value: targetStatus.ID,
},
}
@ -238,7 +238,7 @@ func (suite *RepliesGetTestSuite) TestGetRepliesLast() {
})
assert.Equal(suite.T(), expect, string(b))
m := make(map[string]interface{})
m := make(map[string]any)
err = json.Unmarshal(b, &m)
assert.NoError(suite.T(), err)

View file

@ -30,7 +30,7 @@ import (
// StatusGETHandler serves the target status as an activitystreams NOTE so that other AP servers can parse it.
func (m *Module) StatusGETHandler(c *gin.Context) {
// usernames on our instance are always lowercase
requestedUsername := strings.ToLower(c.Param(UsernameKey))
requestedUsername := strings.ToLower(c.Param(apiutil.UsernameKey))
if requestedUsername == "" {
err := errors.New("no username specified in request")
apiutil.ErrorHandler(c, gtserror.NewErrorBadRequest(err, err.Error()), m.processor.InstanceGetV1)
@ -38,7 +38,7 @@ func (m *Module) StatusGETHandler(c *gin.Context) {
}
// status IDs on our instance are always uppercase
requestedStatusID := strings.ToUpper(c.Param(StatusIDKey))
requestedStatusID := strings.ToUpper(c.Param(apiutil.IDKey))
if requestedStatusID == "" {
err := errors.New("no status id specified in request")
apiutil.ErrorHandler(c, gtserror.NewErrorBadRequest(err, err.Error()), m.processor.InstanceGetV1)

View file

@ -27,7 +27,7 @@ import (
"code.superseriousbusiness.org/activity/streams"
"code.superseriousbusiness.org/activity/streams/vocab"
"code.superseriousbusiness.org/gotosocial/internal/api/activitypub/users"
apiutil "code.superseriousbusiness.org/gotosocial/internal/api/util"
"code.superseriousbusiness.org/gotosocial/testrig"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/suite"
@ -59,11 +59,11 @@ func (suite *StatusGetTestSuite) TestGetStatus() {
// but because we're calling the function directly, we need to set them manually.
ctx.Params = gin.Params{
gin.Param{
Key: users.UsernameKey,
Key: apiutil.UsernameKey,
Value: targetAccount.Username,
},
gin.Param{
Key: users.StatusIDKey,
Key: apiutil.IDKey,
Value: targetStatus.ID,
},
}
@ -80,7 +80,7 @@ func (suite *StatusGetTestSuite) TestGetStatus() {
suite.NoError(err)
// should be a Note
m := make(map[string]interface{})
m := make(map[string]any)
err = json.Unmarshal(b, &m)
suite.NoError(err)
@ -118,11 +118,11 @@ func (suite *StatusGetTestSuite) TestGetStatusLowercase() {
// but because we're calling the function directly, we need to set them manually.
ctx.Params = gin.Params{
gin.Param{
Key: users.UsernameKey,
Key: apiutil.UsernameKey,
Value: strings.ToLower(targetAccount.Username),
},
gin.Param{
Key: users.StatusIDKey,
Key: apiutil.IDKey,
Value: strings.ToLower(targetStatus.ID),
},
}
@ -139,7 +139,7 @@ func (suite *StatusGetTestSuite) TestGetStatusLowercase() {
suite.NoError(err)
// should be a Note
m := make(map[string]interface{})
m := make(map[string]any)
err = json.Unmarshal(b, &m)
suite.NoError(err)

View file

@ -27,39 +27,17 @@ import (
)
const (
// UsernameKey is for account usernames.
UsernameKey = "username"
// StatusIDKey is for status IDs
StatusIDKey = "status"
// OnlyOtherAccountsKey is for filtering status responses.
OnlyOtherAccountsKey = "only_other_accounts"
// MinIDKey is for filtering status responses.
MinIDKey = "min_id"
// MaxIDKey is for filtering status responses.
MaxIDKey = "max_id"
// PageKey is for filtering status responses.
PageKey = "page"
// BasePath is the base path for serving AP 'users' requests, minus the 'users' prefix.
BasePath = "/:" + UsernameKey
// InboxPath is for serving POST requests to a user's inbox with the given username key.
InboxPath = BasePath + "/" + uris.InboxPath
// OutboxPath is for serving GET requests to a user's outbox with the given username key.
OutboxPath = BasePath + "/" + uris.OutboxPath
// FollowersPath is for serving GET request's to a user's followers list, with the given username key.
FollowersPath = BasePath + "/" + uris.FollowersPath
// FollowingPath is for serving GET request's to a user's following list, with the given username key.
FollowingPath = BasePath + "/" + uris.FollowingPath
// FeaturedCollectionPath is for serving GET requests to a user's list of featured (pinned) statuses.
OnlyOtherAccountsKey = "only_other_accounts"
BasePath = "/:" + apiutil.UsernameKey
InboxPath = BasePath + "/" + uris.InboxPath
OutboxPath = BasePath + "/" + uris.OutboxPath
FollowersPath = BasePath + "/" + uris.FollowersPath
FollowingPath = BasePath + "/" + uris.FollowingPath
FeaturedCollectionPath = BasePath + "/" + uris.CollectionsPath + "/" + uris.FeaturedPath
// StatusPath is for serving GET requests to a particular status by a user, with the given username key and status ID
StatusPath = BasePath + "/" + uris.StatusesPath + "/:" + StatusIDKey
// StatusRepliesPath is for serving the replies collection of a status.
StatusRepliesPath = StatusPath + "/replies"
// AcceptPath is for serving accepts of a status.
AcceptPath = BasePath + "/" + uris.AcceptsPath + "/:" + apiutil.IDKey
// AuthorizationsPath is for serving authorizations of an interaction.
AuthorizationsPath = BasePath + "/" + uris.AuthorizationsPath + "/:" + apiutil.IDKey
StatusPath = BasePath + "/" + uris.StatusesPath + "/:" + apiutil.IDKey
StatusRepliesPath = StatusPath + "/replies"
AcceptPath = BasePath + "/" + uris.AcceptsPath + "/:" + apiutil.IDKey
AuthorizationsPath = BasePath + "/" + uris.AuthorizationsPath + "/:" + apiutil.IDKey
)
type Module struct {

View file

@ -38,7 +38,7 @@ import (
// request is blocked.
func (m *Module) UsersGETHandler(c *gin.Context) {
// usernames on our instance are always lowercase
requestedUsername := strings.ToLower(c.Param(UsernameKey))
requestedUsername := strings.ToLower(c.Param(apiutil.UsernameKey))
if requestedUsername == "" {
err := errors.New("no username specified in request")
apiutil.ErrorHandler(c, gtserror.NewErrorBadRequest(err, err.Error()), m.processor.InstanceGetV1)
@ -51,13 +51,17 @@ func (m *Module) UsersGETHandler(c *gin.Context) {
return
}
// If HTML is requested, redirect
// to user's profile instead.
if contentType == string(apiutil.TextHTML) {
// redirect to the user's profile
c.Redirect(http.StatusSeeOther, "/@"+requestedUsername)
return
}
resp, errWithCode := m.processor.Fedi().UserGet(c.Request.Context(), requestedUsername, c.Request.URL)
resp, errWithCode := m.processor.Fedi().UserGet(
c.Request.Context(),
requestedUsername,
)
if errWithCode != nil {
apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1)
return

View file

@ -27,6 +27,7 @@ import (
"code.superseriousbusiness.org/activity/streams"
"code.superseriousbusiness.org/activity/streams/vocab"
"code.superseriousbusiness.org/gotosocial/internal/api/activitypub/users"
apiutil "code.superseriousbusiness.org/gotosocial/internal/api/util"
"code.superseriousbusiness.org/gotosocial/testrig"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/suite"
@ -57,7 +58,7 @@ func (suite *UserGetTestSuite) TestGetUser() {
// but because we're calling the function directly, we need to set them manually.
ctx.Params = gin.Params{
gin.Param{
Key: users.UsernameKey,
Key: apiutil.UsernameKey,
Value: targetAccount.Username,
},
}
@ -74,7 +75,7 @@ func (suite *UserGetTestSuite) TestGetUser() {
suite.NoError(err)
// should be a Person
m := make(map[string]interface{})
m := make(map[string]any)
err = json.Unmarshal(b, &m)
suite.NoError(err)
@ -125,7 +126,7 @@ func (suite *UserGetTestSuite) TestGetUserPublicKeyDeleted() {
// but because we're calling the function directly, we need to set them manually.
ctx.Params = gin.Params{
gin.Param{
Key: users.UsernameKey,
Key: apiutil.UsernameKey,
Value: targetAccount.Username,
},
}
@ -142,7 +143,7 @@ func (suite *UserGetTestSuite) TestGetUserPublicKeyDeleted() {
suite.NoError(err)
// should be a Person
m := make(map[string]interface{})
m := make(map[string]any)
err = json.Unmarshal(b, &m)
suite.NoError(err)