From e04b187702acb0c9908237a35b3a9857e2167b3f Mon Sep 17 00:00:00 2001 From: tobi <31960611+tsmethurst@users.noreply.github.com> Date: Mon, 4 Oct 2021 15:24:19 +0200 Subject: [PATCH] Refactor/tidy (#261) * tidy up streaming * cut down code duplication * test get followers/following * test streaming processor * fix some test models * add TimeMustParse * fix uri / url typo * make trace logging less verbose * make logging more consistent * disable quote on logging * remove context.Background * remove many extraneous mastodon references * regenerate swagger * don't log query on no rows result * log latency first for easier reading --- docs/api/swagger.yaml | 7 +- internal/api/client/account/accountupdate.go | 2 +- internal/api/client/admin/emojicreate.go | 4 +- internal/api/client/app/appcreate.go | 5 +- internal/api/client/auth/auth_test.go | 2 +- internal/api/client/auth/authorize.go | 3 +- internal/api/client/auth/token.go | 1 - internal/api/client/auth/util.go | 22 ++- internal/api/client/media/mediacreate.go | 4 +- internal/api/client/status/statusboost.go | 4 +- internal/api/client/status/statusboostedby.go | 4 +- internal/api/client/status/statuscreate.go | 4 +- .../api/client/status/statuscreate_test.go | 16 +- internal/api/client/status/statusdelete.go | 6 +- internal/api/client/status/statusfave.go | 4 +- internal/api/client/status/statusfavedby.go | 4 +- internal/api/client/status/statusget.go | 4 +- internal/api/client/status/statusget_test.go | 55 ------ internal/api/client/status/statusunboost.go | 4 +- internal/api/client/status/statusunfave.go | 4 +- internal/api/model/conversation.go | 2 +- internal/api/model/featuredtag.go | 2 +- internal/api/model/filter.go | 2 +- internal/api/model/history.go | 2 +- internal/api/model/list.go | 2 +- internal/api/model/marker.go | 4 +- internal/api/model/notification.go | 2 +- internal/api/model/oauth.go | 1 - internal/api/model/preferences.go | 2 +- internal/api/model/pushsubscription.go | 2 +- internal/api/model/results.go | 2 +- internal/api/model/scheduledstatus.go | 4 +- internal/api/model/source.go | 1 - internal/api/model/status.go | 2 +- internal/api/model/timeline.go | 18 ++ internal/cliactions/server/server.go | 4 +- internal/cliactions/testrig/testrig.go | 2 +- internal/db/bundb/trace.go | 14 +- internal/federation/authenticate.go | 4 +- internal/federation/dereferencing/account.go | 4 +- .../dereferencing/collectionpage.go | 4 +- internal/federation/dereferencing/instance.go | 2 +- internal/federation/dereferencing/status.go | 4 +- internal/federation/federatingdb/accept.go | 50 ++---- internal/federation/federatingdb/announce.go | 46 ++--- internal/federation/federatingdb/create.go | 49 ++--- internal/federation/federatingdb/delete.go | 32 +--- internal/federation/federatingdb/exists.go | 7 +- .../federatingdb/federatingdb_test.go | 53 +++++- internal/federation/federatingdb/followers.go | 47 ++--- .../federation/federatingdb/followers_test.go | 53 ++++++ internal/federation/federatingdb/following.go | 63 ++----- .../federation/federatingdb/following_test.go | 53 ++++++ internal/federation/federatingdb/get.go | 33 +--- internal/federation/federatingdb/inbox.go | 45 +---- internal/federation/federatingdb/liked.go | 13 +- internal/federation/federatingdb/outbox.go | 43 +---- internal/federation/federatingdb/owns.go | 4 +- internal/federation/federatingdb/undo.go | 40 ++--- internal/federation/federatingdb/update.go | 46 ++--- internal/federation/federatingdb/util.go | 167 +++++++++++++----- internal/federation/finger.go | 2 +- internal/gtsmodel/account.go | 2 +- internal/log/log.go | 2 +- internal/media/handler.go | 2 +- internal/oauth/server.go | 12 +- internal/oidc/idp.go | 4 +- internal/processing/account/create.go | 2 +- internal/processing/account/get.go | 12 +- internal/processing/account/getfollowers.go | 2 +- internal/processing/account/getfollowing.go | 2 +- .../processing/account/getrelationship.go | 2 +- internal/processing/account/getstatuses.go | 4 +- internal/processing/account/update.go | 6 +- .../processing/admin/createdomainblock.go | 6 +- .../processing/admin/deletedomainblock.go | 4 +- internal/processing/admin/emoji.go | 6 +- internal/processing/admin/getdomainblock.go | 4 +- internal/processing/admin/getdomainblocks.go | 8 +- internal/processing/app.go | 6 +- internal/processing/blocks.go | 2 +- internal/processing/federation.go | 4 +- internal/processing/followrequest.go | 6 +- internal/processing/fromcommon.go | 38 ++-- internal/processing/instance.go | 4 +- internal/processing/media/create.go | 4 +- internal/processing/media/getmedia.go | 2 +- internal/processing/media/update.go | 2 +- internal/processing/notification.go | 10 +- internal/processing/processor.go | 2 +- internal/processing/search.go | 8 +- internal/processing/status/boost.go | 4 +- internal/processing/status/boostedby.go | 10 +- internal/processing/status/context.go | 8 +- internal/processing/status/create.go | 4 +- internal/processing/status/delete.go | 4 +- internal/processing/status/fave.go | 6 +- internal/processing/status/favedby.go | 10 +- internal/processing/status/get.go | 4 +- internal/processing/status/unboost.go | 4 +- internal/processing/status/unfave.go | 4 +- internal/processing/status/util.go | 2 +- internal/processing/streaming/authorize.go | 20 ++- .../processing/streaming/authorize_test.go | 48 +++++ .../streaming/notification.go} | 30 ++-- .../processing/streaming/notification_test.go | 60 +++++++ internal/processing/streaming/openstream.go | 18 ++ .../streaming/openstream_test.go} | 29 +-- internal/processing/streaming/streamdelete.go | 59 +++---- internal/processing/streaming/streaming.go | 33 ++-- .../processing/streaming/streaming_test.go | 55 ++++++ .../streaming/streamnotification.go | 51 ------ internal/processing/streaming/streamstatus.go | 51 ------ .../processing/streaming/streamtoaccount.go | 55 ++++++ internal/processing/streaming/update.go | 37 ++++ internal/processing/timeline.go | 8 +- internal/router/logger.go | 4 +- internal/stream/stream.go | 12 ++ internal/timeline/prepare.go | 2 +- internal/typeutils/converter.go | 68 +++---- internal/typeutils/frontendtointernal.go | 3 +- internal/typeutils/internaltoas.go | 6 +- internal/typeutils/internaltofrontend.go | 158 ++++++++--------- testrig/oauthserver.go | 4 +- testrig/testmodels.go | 6 +- testrig/util.go | 11 ++ 126 files changed, 1192 insertions(+), 955 deletions(-) create mode 100644 internal/federation/federatingdb/followers_test.go create mode 100644 internal/federation/federatingdb/following_test.go create mode 100644 internal/processing/streaming/authorize_test.go rename internal/{api/model/identityproof.go => processing/streaming/notification.go} (52%) create mode 100644 internal/processing/streaming/notification_test.go rename internal/{api/model/error.go => processing/streaming/openstream_test.go} (61%) create mode 100644 internal/processing/streaming/streaming_test.go delete mode 100644 internal/processing/streaming/streamnotification.go delete mode 100644 internal/processing/streaming/streamstatus.go create mode 100644 internal/processing/streaming/streamtoaccount.go create mode 100644 internal/processing/streaming/update.go diff --git a/docs/api/swagger.yaml b/docs/api/swagger.yaml index 17846686b..77a150a9a 100644 --- a/docs/api/swagger.yaml +++ b/docs/api/swagger.yaml @@ -50,9 +50,8 @@ definitions: type: object x-go-package: github.com/superseriousbusiness/gotosocial/internal/api/model Source: - description: |- - Returned as an additional entity when verifying and updated credentials, as an attribute of Account. - See https://docs.joinmastodon.org/entities/source/ + description: Returned as an additional entity when verifying and updated credentials, + as an attribute of Account. properties: fields: description: Metadata about the account. @@ -330,7 +329,7 @@ definitions: x-go-package: github.com/superseriousbusiness/gotosocial/internal/api/model advancedStatusCreateForm: description: |- - AdvancedStatusCreateForm wraps the mastodon status create form along with the GTS advanced + AdvancedStatusCreateForm wraps the mastodon-compatible status create form along with the GTS advanced visibility settings. properties: boostable: diff --git a/internal/api/client/account/accountupdate.go b/internal/api/client/account/accountupdate.go index 9a377f3b8..35eaaad6f 100644 --- a/internal/api/client/account/accountupdate.go +++ b/internal/api/client/account/accountupdate.go @@ -139,7 +139,7 @@ func (m *Module) AccountUpdateCredentialsPATCHHandler(c *gin.Context) { return } - l.Tracef("conversion successful, returning OK and mastosensitive account %+v", acctSensitive) + l.Tracef("conversion successful, returning OK and apisensitive account %+v", acctSensitive) c.JSON(http.StatusOK, acctSensitive) } diff --git a/internal/api/client/admin/emojicreate.go b/internal/api/client/admin/emojicreate.go index 019298976..38928a9d8 100644 --- a/internal/api/client/admin/emojicreate.go +++ b/internal/api/client/admin/emojicreate.go @@ -111,14 +111,14 @@ func (m *Module) emojiCreatePOSTHandler(c *gin.Context) { return } - mastoEmoji, err := m.processor.AdminEmojiCreate(c.Request.Context(), authed, form) + apiEmoji, err := m.processor.AdminEmojiCreate(c.Request.Context(), authed, form) if err != nil { l.Debugf("error creating emoji: %s", err) c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } - c.JSON(http.StatusOK, mastoEmoji) + c.JSON(http.StatusOK, apiEmoji) } func validateCreateEmoji(form *model.EmojiCreateRequest) error { diff --git a/internal/api/client/app/appcreate.go b/internal/api/client/app/appcreate.go index d233841b0..e4cbc32d0 100644 --- a/internal/api/client/app/appcreate.go +++ b/internal/api/client/app/appcreate.go @@ -101,12 +101,11 @@ func (m *Module) AppsPOSTHandler(c *gin.Context) { return } - mastoApp, err := m.processor.AppCreate(c.Request.Context(), authed, form) + apiApp, err := m.processor.AppCreate(c.Request.Context(), authed, form) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } - // done, return the new app information per the spec here: https://docs.joinmastodon.org/methods/apps/ - c.JSON(http.StatusOK, mastoApp) + c.JSON(http.StatusOK, apiApp) } diff --git a/internal/api/client/auth/auth_test.go b/internal/api/client/auth/auth_test.go index 295c0e964..bd4ff27fd 100644 --- a/internal/api/client/auth/auth_test.go +++ b/internal/api/client/auth/auth_test.go @@ -124,7 +124,7 @@ func (suite *AuthTestSuite) SetupTest() { } } - suite.oauthServer = oauth.New(suite.db, log) + suite.oauthServer = oauth.New(context.Background(), suite.db, log) if err := suite.db.Put(context.Background(), suite.testAccount); err != nil { logrus.Panicf("could not insert test account into db: %s", err) diff --git a/internal/api/client/auth/authorize.go b/internal/api/client/auth/authorize.go index d7ea65cca..972853687 100644 --- a/internal/api/client/auth/authorize.go +++ b/internal/api/client/auth/authorize.go @@ -35,7 +35,7 @@ // AuthorizeGETHandler should be served as GET at https://example.org/oauth/authorize // The idea here is to present an oauth authorize page to the user, with a button -// that they have to click to accept. See here: https://docs.joinmastodon.org/methods/apps/oauth/#authorize-a-user +// that they have to click to accept. func (m *Module) AuthorizeGETHandler(c *gin.Context) { l := m.log.WithField("func", "AuthorizeGETHandler") s := sessions.Default(c) @@ -122,7 +122,6 @@ func (m *Module) AuthorizeGETHandler(c *gin.Context) { // AuthorizePOSTHandler should be served as POST at https://example.org/oauth/authorize // At this point we assume that the user has A) logged in and B) accepted that the app should act for them, // so we should proceed with the authentication flow and generate an oauth token for them if we can. -// See here: https://docs.joinmastodon.org/methods/apps/oauth/#authorize-a-user func (m *Module) AuthorizePOSTHandler(c *gin.Context) { l := m.log.WithField("func", "AuthorizePOSTHandler") s := sessions.Default(c) diff --git a/internal/api/client/auth/token.go b/internal/api/client/auth/token.go index 7e590aa1f..f9009767e 100644 --- a/internal/api/client/auth/token.go +++ b/internal/api/client/auth/token.go @@ -36,7 +36,6 @@ type tokenBody struct { // TokenPOSTHandler should be served as a POST at https://example.org/oauth/token // The idea here is to serve an oauth access token to a user, which can be used for authorizing against non-public APIs. -// See https://docs.joinmastodon.org/methods/apps/oauth/#obtain-a-token func (m *Module) TokenPOSTHandler(c *gin.Context) { l := m.log.WithField("func", "TokenPOSTHandler") l.Trace("entered TokenPOSTHandler") diff --git a/internal/api/client/auth/util.go b/internal/api/client/auth/util.go index 48fe4748a..14c38e667 100644 --- a/internal/api/client/auth/util.go +++ b/internal/api/client/auth/util.go @@ -1,3 +1,21 @@ +/* + GoToSocial + Copyright (C) 2021 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 . +*/ + package auth import ( @@ -7,10 +25,6 @@ func (m *Module) clearSession(s sessions.Session) { s.Clear() - // newOptions := router.SessionOptions(m.config) - // newOptions.MaxAge = -1 // instruct browser to delete cookie immediately - // s.Options(newOptions) - if err := s.Save(); err != nil { panic(err) } diff --git a/internal/api/client/media/mediacreate.go b/internal/api/client/media/mediacreate.go index 58d076ea6..68b25039c 100644 --- a/internal/api/client/media/mediacreate.go +++ b/internal/api/client/media/mediacreate.go @@ -108,14 +108,14 @@ func (m *Module) MediaCreatePOSTHandler(c *gin.Context) { } l.Debug("calling processor media create func") - mastoAttachment, err := m.processor.MediaCreate(c.Request.Context(), authed, form) + apiAttachment, err := m.processor.MediaCreate(c.Request.Context(), authed, form) if err != nil { l.Debugf("error creating attachment: %s", err) c.JSON(http.StatusUnprocessableEntity, gin.H{"error": err.Error()}) return } - c.JSON(http.StatusOK, mastoAttachment) + c.JSON(http.StatusOK, apiAttachment) } func validateCreateMedia(form *model.AttachmentRequest, config *config.MediaConfig) error { diff --git a/internal/api/client/status/statusboost.go b/internal/api/client/status/statusboost.go index 094e56ac0..6a1ccde28 100644 --- a/internal/api/client/status/statusboost.go +++ b/internal/api/client/status/statusboost.go @@ -87,12 +87,12 @@ func (m *Module) StatusBoostPOSTHandler(c *gin.Context) { return } - mastoStatus, errWithCode := m.processor.StatusBoost(c.Request.Context(), authed, targetStatusID) + apiStatus, errWithCode := m.processor.StatusBoost(c.Request.Context(), authed, targetStatusID) if errWithCode != nil { l.Debugf("error processing status boost: %s", errWithCode.Error()) c.JSON(errWithCode.Code(), gin.H{"error": errWithCode.Safe()}) return } - c.JSON(http.StatusOK, mastoStatus) + c.JSON(http.StatusOK, apiStatus) } diff --git a/internal/api/client/status/statusboostedby.go b/internal/api/client/status/statusboostedby.go index 908c3ff10..a1446aef7 100644 --- a/internal/api/client/status/statusboostedby.go +++ b/internal/api/client/status/statusboostedby.go @@ -84,12 +84,12 @@ func (m *Module) StatusBoostedByGETHandler(c *gin.Context) { return } - mastoAccounts, err := m.processor.StatusBoostedBy(c.Request.Context(), authed, targetStatusID) + apiAccounts, err := m.processor.StatusBoostedBy(c.Request.Context(), authed, targetStatusID) if err != nil { l.Debugf("error processing status boosted by request: %s", err) c.JSON(http.StatusBadRequest, gin.H{"error": "bad request"}) return } - c.JSON(http.StatusOK, mastoAccounts) + c.JSON(http.StatusOK, apiAccounts) } diff --git a/internal/api/client/status/statuscreate.go b/internal/api/client/status/statuscreate.go index c8ea019d2..cae11ebdb 100644 --- a/internal/api/client/status/statuscreate.go +++ b/internal/api/client/status/statuscreate.go @@ -101,14 +101,14 @@ func (m *Module) StatusCreatePOSTHandler(c *gin.Context) { return } - mastoStatus, err := m.processor.StatusCreate(c.Request.Context(), authed, form) + apiStatus, err := m.processor.StatusCreate(c.Request.Context(), authed, form) if err != nil { l.Debugf("error processing status create: %s", err) c.JSON(http.StatusBadRequest, gin.H{"error": "bad request"}) return } - c.JSON(http.StatusOK, mastoStatus) + c.JSON(http.StatusOK, apiStatus) } func validateCreateStatus(form *model.AdvancedStatusCreateForm, config *config.StatusesConfig) error { diff --git a/internal/api/client/status/statuscreate_test.go b/internal/api/client/status/statuscreate_test.go index 060d96dad..084f1a6f4 100644 --- a/internal/api/client/status/statuscreate_test.go +++ b/internal/api/client/status/statuscreate_test.go @@ -121,7 +121,7 @@ func (suite *StatusCreateTestSuite) TestPostNewStatus() { assert.Equal(suite.T(), "hello hello", statusReply.SpoilerText) assert.Equal(suite.T(), "

this is a brand new status! #helloworld

", statusReply.Content) assert.True(suite.T(), statusReply.Sensitive) - assert.Equal(suite.T(), model.VisibilityPrivate, statusReply.Visibility) // even though we set this status to mutuals only, it should serialize to private, because masto has no idea about mutuals_only + assert.Equal(suite.T(), model.VisibilityPrivate, statusReply.Visibility) // even though we set this status to mutuals only, it should serialize to private, because the mastodon api has no idea about mutuals_only assert.Len(suite.T(), statusReply.Tags, 1) assert.Equal(suite.T(), model.Tag{ Name: "helloworld", @@ -202,12 +202,12 @@ func (suite *StatusCreateTestSuite) TestPostNewStatusWithEmoji() { assert.Equal(suite.T(), "

here is a rainbow emoji a few times! :rainbow: :rainbow: :rainbow:
here's an emoji that isn't in the db: :test_emoji:

", statusReply.Content) assert.Len(suite.T(), statusReply.Emojis, 1) - mastoEmoji := statusReply.Emojis[0] + apiEmoji := statusReply.Emojis[0] gtsEmoji := testrig.NewTestEmojis()["rainbow"] - assert.Equal(suite.T(), gtsEmoji.Shortcode, mastoEmoji.Shortcode) - assert.Equal(suite.T(), gtsEmoji.ImageURL, mastoEmoji.URL) - assert.Equal(suite.T(), gtsEmoji.ImageStaticURL, mastoEmoji.StaticURL) + assert.Equal(suite.T(), gtsEmoji.Shortcode, apiEmoji.Shortcode) + assert.Equal(suite.T(), gtsEmoji.ImageURL, apiEmoji.URL) + assert.Equal(suite.T(), gtsEmoji.ImageStaticURL, apiEmoji.StaticURL) } // Try to reply to a status that doesn't exist @@ -326,12 +326,12 @@ func (suite *StatusCreateTestSuite) TestAttachNewMediaSuccess() { gtsAttachment, err := suite.db.GetAttachmentByID(context.Background(), statusResponse.MediaAttachments[0].ID) assert.NoError(suite.T(), err) - // convert it to a masto attachment - gtsAttachmentAsMasto, err := suite.tc.AttachmentToMasto(context.Background(), gtsAttachment) + // convert it to a api attachment + gtsAttachmentAsapi, err := suite.tc.AttachmentToAPIAttachment(context.Background(), gtsAttachment) assert.NoError(suite.T(), err) // compare it with what we have now - assert.EqualValues(suite.T(), statusResponse.MediaAttachments[0], gtsAttachmentAsMasto) + assert.EqualValues(suite.T(), statusResponse.MediaAttachments[0], gtsAttachmentAsapi) // the status id of the attachment should now be set to the id of the status we just created assert.Equal(suite.T(), statusResponse.ID, gtsAttachment.StatusID) diff --git a/internal/api/client/status/statusdelete.go b/internal/api/client/status/statusdelete.go index 9a67c45ba..1914a1a07 100644 --- a/internal/api/client/status/statusdelete.go +++ b/internal/api/client/status/statusdelete.go @@ -86,7 +86,7 @@ func (m *Module) StatusDELETEHandler(c *gin.Context) { return } - mastoStatus, err := m.processor.StatusDelete(c.Request.Context(), authed, targetStatusID) + apiStatus, err := m.processor.StatusDelete(c.Request.Context(), authed, targetStatusID) if err != nil { l.Debugf("error processing status delete: %s", err) c.JSON(http.StatusBadRequest, gin.H{"error": "bad request"}) @@ -94,10 +94,10 @@ func (m *Module) StatusDELETEHandler(c *gin.Context) { } // the status was already gone/never existed - if mastoStatus == nil { + if apiStatus == nil { c.JSON(http.StatusNotFound, gin.H{"error": "Record not found"}) return } - c.JSON(http.StatusOK, mastoStatus) + c.JSON(http.StatusOK, apiStatus) } diff --git a/internal/api/client/status/statusfave.go b/internal/api/client/status/statusfave.go index 94a8f9380..94d930989 100644 --- a/internal/api/client/status/statusfave.go +++ b/internal/api/client/status/statusfave.go @@ -83,12 +83,12 @@ func (m *Module) StatusFavePOSTHandler(c *gin.Context) { return } - mastoStatus, err := m.processor.StatusFave(c.Request.Context(), authed, targetStatusID) + apiStatus, err := m.processor.StatusFave(c.Request.Context(), authed, targetStatusID) if err != nil { l.Debugf("error processing status fave: %s", err) c.JSON(http.StatusBadRequest, gin.H{"error": "bad request"}) return } - c.JSON(http.StatusOK, mastoStatus) + c.JSON(http.StatusOK, apiStatus) } diff --git a/internal/api/client/status/statusfavedby.go b/internal/api/client/status/statusfavedby.go index 7b8e19e20..4ed6e74ed 100644 --- a/internal/api/client/status/statusfavedby.go +++ b/internal/api/client/status/statusfavedby.go @@ -84,12 +84,12 @@ func (m *Module) StatusFavedByGETHandler(c *gin.Context) { return } - mastoAccounts, err := m.processor.StatusFavedBy(c.Request.Context(), authed, targetStatusID) + apiAccounts, err := m.processor.StatusFavedBy(c.Request.Context(), authed, targetStatusID) if err != nil { l.Debugf("error processing status faved by request: %s", err) c.JSON(http.StatusBadRequest, gin.H{"error": "bad request"}) return } - c.JSON(http.StatusOK, mastoAccounts) + c.JSON(http.StatusOK, apiAccounts) } diff --git a/internal/api/client/status/statusget.go b/internal/api/client/status/statusget.go index 39668288f..3c74b53c8 100644 --- a/internal/api/client/status/statusget.go +++ b/internal/api/client/status/statusget.go @@ -83,12 +83,12 @@ func (m *Module) StatusGETHandler(c *gin.Context) { return } - mastoStatus, err := m.processor.StatusGet(c.Request.Context(), authed, targetStatusID) + apiStatus, err := m.processor.StatusGet(c.Request.Context(), authed, targetStatusID) if err != nil { l.Debugf("error processing status get: %s", err) c.JSON(http.StatusBadRequest, gin.H{"error": "bad request"}) return } - c.JSON(http.StatusOK, mastoStatus) + c.JSON(http.StatusOK, apiStatus) } diff --git a/internal/api/client/status/statusget_test.go b/internal/api/client/status/statusget_test.go index 1c700aaa5..fa5f786cb 100644 --- a/internal/api/client/status/statusget_test.go +++ b/internal/api/client/status/statusget_test.go @@ -57,61 +57,6 @@ func (suite *StatusGetTestSuite) TearDownTest() { testrig.StandardStorageTeardown(suite.storage) } -// Post a new status with some custom visibility settings -func (suite *StatusGetTestSuite) TestPostNewStatus() { - - // t := suite.testTokens["local_account_1"] - // oauthToken := oauth.PGTokenToOauthToken(t) - - // // setup - // recorder := httptest.NewRecorder() - // ctx, _ := gin.CreateTestContext(recorder) - // ctx.Set(oauth.SessionAuthorizedApplication, suite.testApplications["application_1"]) - // ctx.Set(oauth.SessionAuthorizedToken, oauthToken) - // ctx.Set(oauth.SessionAuthorizedUser, suite.testUsers["local_account_1"]) - // ctx.Set(oauth.SessionAuthorizedAccount, suite.testAccounts["local_account_1"]) - // ctx.Request = httptest.NewRequest(http.MethodPost, fmt.Sprintf("http://localhost:8080/%s", basePath), nil) // the endpoint we're hitting - // ctx.Request.Form = url.Values{ - // "status": {"this is a brand new status! #helloworld"}, - // "spoiler_text": {"hello hello"}, - // "sensitive": {"true"}, - // "visibility_advanced": {"mutuals_only"}, - // "likeable": {"false"}, - // "replyable": {"false"}, - // "federated": {"false"}, - // } - // suite.statusModule.statusGETHandler(ctx) - - // // check response - - // // 1. we should have OK from our call to the function - // suite.EqualValues(http.StatusOK, recorder.Code) - - // result := recorder.Result() - // defer result.Body.Close() - // b, err := ioutil.ReadAll(result.Body) - // assert.NoError(suite.T(), err) - - // statusReply := &mastotypes.Status{} - // err = json.Unmarshal(b, statusReply) - // assert.NoError(suite.T(), err) - - // assert.Equal(suite.T(), "hello hello", statusReply.SpoilerText) - // assert.Equal(suite.T(), "this is a brand new status! #helloworld", statusReply.Content) - // assert.True(suite.T(), statusReply.Sensitive) - // assert.Equal(suite.T(), mastotypes.VisibilityPrivate, statusReply.Visibility) - // assert.Len(suite.T(), statusReply.Tags, 1) - // assert.Equal(suite.T(), mastotypes.Tag{ - // Name: "helloworld", - // URL: "http://localhost:8080/tags/helloworld", - // }, statusReply.Tags[0]) - - // gtsTag := >smodel.Tag{} - // err = suite.db.GetWhere("name", "helloworld", gtsTag) - // assert.NoError(suite.T(), err) - // assert.Equal(suite.T(), statusReply.Account.ID, gtsTag.FirstSeenFromAccountID) -} - func TestStatusGetTestSuite(t *testing.T) { suite.Run(t, new(StatusGetTestSuite)) } diff --git a/internal/api/client/status/statusunboost.go b/internal/api/client/status/statusunboost.go index 8bb28fa50..f114dbd0c 100644 --- a/internal/api/client/status/statusunboost.go +++ b/internal/api/client/status/statusunboost.go @@ -84,12 +84,12 @@ func (m *Module) StatusUnboostPOSTHandler(c *gin.Context) { return } - mastoStatus, errWithCode := m.processor.StatusUnboost(c.Request.Context(), authed, targetStatusID) + apiStatus, errWithCode := m.processor.StatusUnboost(c.Request.Context(), authed, targetStatusID) if errWithCode != nil { l.Debugf("error processing status unboost: %s", errWithCode.Error()) c.JSON(errWithCode.Code(), gin.H{"error": errWithCode.Safe()}) return } - c.JSON(http.StatusOK, mastoStatus) + c.JSON(http.StatusOK, apiStatus) } diff --git a/internal/api/client/status/statusunfave.go b/internal/api/client/status/statusunfave.go index 5a1074ca2..3689e1899 100644 --- a/internal/api/client/status/statusunfave.go +++ b/internal/api/client/status/statusunfave.go @@ -83,12 +83,12 @@ func (m *Module) StatusUnfavePOSTHandler(c *gin.Context) { return } - mastoStatus, err := m.processor.StatusUnfave(c.Request.Context(), authed, targetStatusID) + apiStatus, err := m.processor.StatusUnfave(c.Request.Context(), authed, targetStatusID) if err != nil { l.Debugf("error processing status unfave: %s", err) c.JSON(http.StatusBadRequest, gin.H{"error": "bad request"}) return } - c.JSON(http.StatusOK, mastoStatus) + c.JSON(http.StatusOK, apiStatus) } diff --git a/internal/api/model/conversation.go b/internal/api/model/conversation.go index b0568c17e..36da6aab5 100644 --- a/internal/api/model/conversation.go +++ b/internal/api/model/conversation.go @@ -18,7 +18,7 @@ package model -// Conversation represents a conversation with "direct message" visibility. See https://docs.joinmastodon.org/entities/conversation/ +// Conversation represents a conversation with "direct message" visibility. type Conversation struct { // REQUIRED diff --git a/internal/api/model/featuredtag.go b/internal/api/model/featuredtag.go index 3df3fe4c9..a0ea536f9 100644 --- a/internal/api/model/featuredtag.go +++ b/internal/api/model/featuredtag.go @@ -18,7 +18,7 @@ package model -// FeaturedTag represents a hashtag that is featured on a profile. See https://docs.joinmastodon.org/entities/featuredtag/ +// FeaturedTag represents a hashtag that is featured on a profile. type FeaturedTag struct { // The internal ID of the featured tag in the database. ID string `json:"id"` diff --git a/internal/api/model/filter.go b/internal/api/model/filter.go index 519922ba3..2b0b05e5c 100644 --- a/internal/api/model/filter.go +++ b/internal/api/model/filter.go @@ -18,7 +18,7 @@ package model -// Filter represents a user-defined filter for determining which statuses should not be shown to the user. See https://docs.joinmastodon.org/entities/filter/ +// Filter represents a user-defined filter for determining which statuses should not be shown to the user. // If whole_word is true , client app should do: // Define ‘word constituent character’ for your app. In the official implementation, it’s [A-Za-z0-9_] in JavaScript, and [[:word:]] in Ruby. // Ruby uses the POSIX character class (Letter | Mark | Decimal_Number | Connector_Punctuation). diff --git a/internal/api/model/history.go b/internal/api/model/history.go index d8b4d6b4f..92b7d4d77 100644 --- a/internal/api/model/history.go +++ b/internal/api/model/history.go @@ -18,7 +18,7 @@ package model -// History represents daily usage history of a hashtag. See https://docs.joinmastodon.org/entities/history/ +// History represents daily usage history of a hashtag. type History struct { // UNIX timestamp on midnight of the given day (string cast from integer). Day string `json:"day"` diff --git a/internal/api/model/list.go b/internal/api/model/list.go index 220cde59e..61dd199d0 100644 --- a/internal/api/model/list.go +++ b/internal/api/model/list.go @@ -18,7 +18,7 @@ package model -// List represents a list of some users that the authenticated user follows. See https://docs.joinmastodon.org/entities/list/ +// List represents a list of some users that the authenticated user follows. type List struct { // The internal database ID of the list. ID string `json:"id"` diff --git a/internal/api/model/marker.go b/internal/api/model/marker.go index 1e39f1516..7705f499b 100644 --- a/internal/api/model/marker.go +++ b/internal/api/model/marker.go @@ -18,7 +18,7 @@ package model -// Marker represents the last read position within a user's timelines. See https://docs.joinmastodon.org/entities/marker/ +// Marker represents the last read position within a user's timelines. type Marker struct { // Information about the user's position in the home timeline. Home *TimelineMarker `json:"home"` @@ -26,7 +26,7 @@ type Marker struct { Notifications *TimelineMarker `json:"notifications"` } -// TimelineMarker contains information about a user's progress through a specific timeline. See https://docs.joinmastodon.org/entities/marker/ +// TimelineMarker contains information about a user's progress through a specific timeline. type TimelineMarker struct { // The ID of the most recently viewed entity. LastReadID string `json:"last_read_id"` diff --git a/internal/api/model/notification.go b/internal/api/model/notification.go index 2163251b4..2f6d8e595 100644 --- a/internal/api/model/notification.go +++ b/internal/api/model/notification.go @@ -18,7 +18,7 @@ package model -// Notification represents a notification of an event relevant to the user. See https://docs.joinmastodon.org/entities/notification/ +// Notification represents a notification of an event relevant to the user. type Notification struct { // REQUIRED diff --git a/internal/api/model/oauth.go b/internal/api/model/oauth.go index 10c150474..7ff58daef 100644 --- a/internal/api/model/oauth.go +++ b/internal/api/model/oauth.go @@ -19,7 +19,6 @@ package model // OAuthAuthorize represents a request sent to https://example.org/oauth/authorize -// See here: https://docs.joinmastodon.org/methods/apps/oauth/ type OAuthAuthorize struct { // Forces the user to re-login, which is necessary for authorizing with multiple accounts from the same instance. ForceLogin string `form:"force_login" json:"force_login"` diff --git a/internal/api/model/preferences.go b/internal/api/model/preferences.go index 9e410091e..0ff50bbd7 100644 --- a/internal/api/model/preferences.go +++ b/internal/api/model/preferences.go @@ -18,7 +18,7 @@ package model -// Preferences represents a user's preferences. See https://docs.joinmastodon.org/entities/preferences/ +// Preferences represents a user's preferences. type Preferences struct { // Default visibility for new posts. // public = Public post diff --git a/internal/api/model/pushsubscription.go b/internal/api/model/pushsubscription.go index f34c63374..a8dd69b97 100644 --- a/internal/api/model/pushsubscription.go +++ b/internal/api/model/pushsubscription.go @@ -18,7 +18,7 @@ package model -// PushSubscription represents a subscription to the push streaming server. See https://docs.joinmastodon.org/entities/pushsubscription/ +// PushSubscription represents a subscription to the push streaming server. type PushSubscription struct { // The id of the push subscription in the database. ID string `json:"id"` diff --git a/internal/api/model/results.go b/internal/api/model/results.go index 1b2625a0d..266d2083b 100644 --- a/internal/api/model/results.go +++ b/internal/api/model/results.go @@ -18,7 +18,7 @@ package model -// Results represents the results of a search. See https://docs.joinmastodon.org/entities/results/ +// Results represents the results of a search. type Results struct { // Accounts which match the given query Accounts []Account `json:"accounts"` diff --git a/internal/api/model/scheduledstatus.go b/internal/api/model/scheduledstatus.go index deafd22aa..38f70d96a 100644 --- a/internal/api/model/scheduledstatus.go +++ b/internal/api/model/scheduledstatus.go @@ -18,7 +18,7 @@ package model -// ScheduledStatus represents a status that will be published at a future scheduled date. See https://docs.joinmastodon.org/entities/scheduledstatus/ +// ScheduledStatus represents a status that will be published at a future scheduled date. type ScheduledStatus struct { ID string `json:"id"` ScheduledAt string `json:"scheduled_at"` @@ -26,7 +26,7 @@ type ScheduledStatus struct { MediaAttachments []Attachment `json:"media_attachments"` } -// StatusParams represents parameters for a scheduled status. See https://docs.joinmastodon.org/entities/scheduledstatus/ +// StatusParams represents parameters for a scheduled status. type StatusParams struct { Text string `json:"text"` InReplyToID string `json:"in_reply_to_id,omitempty"` diff --git a/internal/api/model/source.go b/internal/api/model/source.go index 441af71de..9e429a3ca 100644 --- a/internal/api/model/source.go +++ b/internal/api/model/source.go @@ -20,7 +20,6 @@ // Source represents display or publishing preferences of user's own account. // Returned as an additional entity when verifying and updated credentials, as an attribute of Account. -// See https://docs.joinmastodon.org/entities/source/ type Source struct { // The default post privacy to be used for new statuses. // public = Public post diff --git a/internal/api/model/status.go b/internal/api/model/status.go index 8be1a4870..ff0cd2e80 100644 --- a/internal/api/model/status.go +++ b/internal/api/model/status.go @@ -177,7 +177,7 @@ type StatusCreateRequest struct { VisibilityDirect Visibility = "direct" ) -// AdvancedStatusCreateForm wraps the mastodon status create form along with the GTS advanced +// AdvancedStatusCreateForm wraps the mastodon-compatible status create form along with the GTS advanced // visibility settings. // // swagger:model advancedStatusCreateForm diff --git a/internal/api/model/timeline.go b/internal/api/model/timeline.go index 52d920879..de50c16cb 100644 --- a/internal/api/model/timeline.go +++ b/internal/api/model/timeline.go @@ -1,3 +1,21 @@ +/* + GoToSocial + Copyright (C) 2021 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 . +*/ + package model // StatusTimelineResponse wraps a slice of statuses, ready to be serialized, along with the Link diff --git a/internal/cliactions/server/server.go b/internal/cliactions/server/server.go index e2f6a24b4..e75eff7c5 100644 --- a/internal/cliactions/server/server.go +++ b/internal/cliactions/server/server.go @@ -88,7 +88,7 @@ // build backend handlers mediaHandler := media.New(c, dbService, storage, log) - oauthServer := oauth.New(dbService, log) + oauthServer := oauth.New(ctx, dbService, log) transportController := transport.NewController(c, dbService, &federation.Clock{}, http.DefaultClient, log) federator := federation.NewFederator(dbService, federatingDB, transportController, c, log, typeConverter, mediaHandler) processor := processing.NewProcessor(c, typeConverter, federator, oauthServer, mediaHandler, storage, timelineManager, dbService, log) @@ -96,7 +96,7 @@ return fmt.Errorf("error starting processor: %s", err) } - idp, err := oidc.NewIDP(c, log) + idp, err := oidc.NewIDP(ctx, c, log) if err != nil { return fmt.Errorf("error creating oidc idp: %s", err) } diff --git a/internal/cliactions/testrig/testrig.go b/internal/cliactions/testrig/testrig.go index 7badca556..482ec34f8 100644 --- a/internal/cliactions/testrig/testrig.go +++ b/internal/cliactions/testrig/testrig.go @@ -67,7 +67,7 @@ return fmt.Errorf("error starting processor: %s", err) } - idp, err := oidc.NewIDP(c, log) + idp, err := oidc.NewIDP(ctx, c, log) if err != nil { return fmt.Errorf("error creating oidc idp: %s", err) } diff --git a/internal/db/bundb/trace.go b/internal/db/bundb/trace.go index e62e8c01f..82b50c058 100644 --- a/internal/db/bundb/trace.go +++ b/internal/db/bundb/trace.go @@ -20,6 +20,7 @@ import ( "context" + "database/sql" "time" "github.com/sirupsen/logrus" @@ -46,8 +47,17 @@ func (q *debugQueryHook) BeforeQuery(ctx context.Context, event *bun.QueryEvent) func (q *debugQueryHook) AfterQuery(ctx context.Context, event *bun.QueryEvent) { dur := time.Since(event.StartTime).Round(time.Microsecond) l := q.log.WithFields(logrus.Fields{ - "queryTime": dur, + "duration": dur, "operation": event.Operation(), }) - l.Trace(event.Query) + + if event.Err != nil && event.Err != sql.ErrNoRows { + // if there's an error the it'll be handled in the application logic, + // but we can still debug log it here alongside the query + l = l.WithField("query", event.Query) + l.Debug(event.Err) + return + } + + l.Tracef("[%s] %s", dur, event.Operation()) } diff --git a/internal/federation/authenticate.go b/internal/federation/authenticate.go index 1e359aa28..da5d2b93d 100644 --- a/internal/federation/authenticate.go +++ b/internal/federation/authenticate.go @@ -185,13 +185,13 @@ func (f *federator) AuthenticateFederatedRequest(ctx context.Context, requestedU } // The actual http call to the remote server is made right here in the Dereference function. - b, err := transport.Dereference(context.Background(), requestingPublicKeyID) + b, err := transport.Dereference(ctx, requestingPublicKeyID) if err != nil { return nil, false, fmt.Errorf("error deferencing key %s: %s", requestingPublicKeyID.String(), err) } // if the key isn't in the response, we can't authenticate the request - requestingPublicKey, err := getPublicKeyFromResponse(context.Background(), b, requestingPublicKeyID) + requestingPublicKey, err := getPublicKeyFromResponse(ctx, b, requestingPublicKeyID) if err != nil { return nil, false, fmt.Errorf("error getting key %s from response %s: %s", requestingPublicKeyID.String(), string(b), err) } diff --git a/internal/federation/dereferencing/account.go b/internal/federation/dereferencing/account.go index 35ea2507d..6acb3b06d 100644 --- a/internal/federation/dereferencing/account.go +++ b/internal/federation/dereferencing/account.go @@ -149,7 +149,7 @@ func (d *deref) dereferenceAccountable(ctx context.Context, username string, rem return nil, fmt.Errorf("DereferenceAccountable: transport err: %s", err) } - b, err := transport.Dereference(context.Background(), remoteAccountID) + b, err := transport.Dereference(ctx, remoteAccountID) if err != nil { return nil, fmt.Errorf("DereferenceAccountable: error deferencing %s: %s", remoteAccountID.String(), err) } @@ -159,7 +159,7 @@ func (d *deref) dereferenceAccountable(ctx context.Context, username string, rem return nil, fmt.Errorf("DereferenceAccountable: error unmarshalling bytes into json: %s", err) } - t, err := streams.ToType(context.Background(), m) + t, err := streams.ToType(ctx, m) if err != nil { return nil, fmt.Errorf("DereferenceAccountable: error resolving json into ap vocab type: %s", err) } diff --git a/internal/federation/dereferencing/collectionpage.go b/internal/federation/dereferencing/collectionpage.go index c5a54402c..cf97327a5 100644 --- a/internal/federation/dereferencing/collectionpage.go +++ b/internal/federation/dereferencing/collectionpage.go @@ -41,7 +41,7 @@ func (d *deref) DereferenceCollectionPage(ctx context.Context, username string, return nil, fmt.Errorf("DereferenceCollectionPage: error creating transport: %s", err) } - b, err := transport.Dereference(context.Background(), pageIRI) + b, err := transport.Dereference(ctx, pageIRI) if err != nil { return nil, fmt.Errorf("DereferenceCollectionPage: error deferencing %s: %s", pageIRI.String(), err) } @@ -51,7 +51,7 @@ func (d *deref) DereferenceCollectionPage(ctx context.Context, username string, return nil, fmt.Errorf("DereferenceCollectionPage: error unmarshalling bytes into json: %s", err) } - t, err := streams.ToType(context.Background(), m) + t, err := streams.ToType(ctx, m) if err != nil { return nil, fmt.Errorf("DereferenceCollectionPage: error resolving json into ap vocab type: %s", err) } diff --git a/internal/federation/dereferencing/instance.go b/internal/federation/dereferencing/instance.go index ec3c3f13d..775475749 100644 --- a/internal/federation/dereferencing/instance.go +++ b/internal/federation/dereferencing/instance.go @@ -36,5 +36,5 @@ func (d *deref) GetRemoteInstance(ctx context.Context, username string, remoteIn return nil, fmt.Errorf("transport err: %s", err) } - return transport.DereferenceInstance(context.Background(), remoteInstanceURI) + return transport.DereferenceInstance(ctx, remoteInstanceURI) } diff --git a/internal/federation/dereferencing/status.go b/internal/federation/dereferencing/status.go index edf1f0e41..435bc9217 100644 --- a/internal/federation/dereferencing/status.go +++ b/internal/federation/dereferencing/status.go @@ -137,7 +137,7 @@ func (d *deref) dereferenceStatusable(ctx context.Context, username string, remo return nil, fmt.Errorf("DereferenceStatusable: transport err: %s", err) } - b, err := transport.Dereference(context.Background(), remoteStatusID) + b, err := transport.Dereference(ctx, remoteStatusID) if err != nil { return nil, fmt.Errorf("DereferenceStatusable: error deferencing %s: %s", remoteStatusID.String(), err) } @@ -147,7 +147,7 @@ func (d *deref) dereferenceStatusable(ctx context.Context, username string, remo return nil, fmt.Errorf("DereferenceStatusable: error unmarshalling bytes into json: %s", err) } - t, err := streams.ToType(context.Background(), m) + t, err := streams.ToType(ctx, m) if err != nil { return nil, fmt.Errorf("DereferenceStatusable: error resolving json into ap vocab type: %s", err) } diff --git a/internal/federation/federatingdb/accept.go b/internal/federation/federatingdb/accept.go index 477c5e8b9..3bd84849d 100644 --- a/internal/federation/federatingdb/accept.go +++ b/internal/federation/federatingdb/accept.go @@ -20,11 +20,9 @@ import ( "context" - "encoding/json" "errors" "fmt" - "github.com/go-fed/activity/streams" "github.com/go-fed/activity/streams/vocab" "github.com/sirupsen/logrus" "github.com/superseriousbusiness/gotosocial/internal/ap" @@ -37,43 +35,29 @@ func (f *federatingDB) Accept(ctx context.Context, accept vocab.ActivityStreamsAccept) error { l := f.log.WithFields( logrus.Fields{ - "func": "Accept", - "asType": accept.GetTypeName(), + "func": "Accept", }, ) - m, err := streams.Serialize(accept) - if err != nil { - return err - } - b, err := json.Marshal(m) - if err != nil { - return err - } - l.Debugf("received ACCEPT asType %s", string(b)) - targetAcctI := ctx.Value(util.APAccount) - if targetAcctI == nil { - // If the target account wasn't set on the context, that means this request didn't pass through the - // API, but came from inside GtS as the result of another activity on this instance. That being so, + if l.Level >= logrus.DebugLevel { + i, err := marshalItem(accept) + if err != nil { + return err + } + l = l.WithField("accept", i) + l.Debug("entering Accept") + } + + targetAcct, fromFederatorChan, err := extractFromCtx(ctx) + if err != nil { + return err + } + if targetAcct == nil || fromFederatorChan == nil { + // If the target account or federator channel wasn't set on the context, that means this request didn't pass + // through the API, but came from inside GtS as the result of another activity on this instance. That being so, // we can safely just ignore this activity, since we know we've already processed it elsewhere. return nil } - targetAcct, ok := targetAcctI.(*gtsmodel.Account) - if !ok { - l.Error("ACCEPT: target account was set on context but couldn't be parsed") - return nil - } - - fromFederatorChanI := ctx.Value(util.APFromFederatorChanKey) - if fromFederatorChanI == nil { - l.Error("ACCEPT: from federator channel wasn't set on context") - return nil - } - fromFederatorChan, ok := fromFederatorChanI.(chan messages.FromFederator) - if !ok { - l.Error("ACCEPT: from federator channel was set on context but couldn't be parsed") - return nil - } acceptObject := accept.GetActivityStreamsObject() if acceptObject == nil { diff --git a/internal/federation/federatingdb/announce.go b/internal/federation/federatingdb/announce.go index 7d7b12cbc..e089f7e42 100644 --- a/internal/federation/federatingdb/announce.go +++ b/internal/federation/federatingdb/announce.go @@ -20,16 +20,12 @@ import ( "context" - "encoding/json" "fmt" - "github.com/go-fed/activity/streams" "github.com/go-fed/activity/streams/vocab" "github.com/sirupsen/logrus" "github.com/superseriousbusiness/gotosocial/internal/ap" - "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" "github.com/superseriousbusiness/gotosocial/internal/messages" - "github.com/superseriousbusiness/gotosocial/internal/util" ) func (f *federatingDB) Announce(ctx context.Context, announce vocab.ActivityStreamsAnnounce) error { @@ -38,40 +34,26 @@ func (f *federatingDB) Announce(ctx context.Context, announce vocab.ActivityStre "func": "Announce", }, ) - m, err := streams.Serialize(announce) + + if l.Level >= logrus.DebugLevel { + i, err := marshalItem(announce) + if err != nil { + return err + } + l = l.WithField("announce", i) + l.Debug("entering Announce") + } + + targetAcct, fromFederatorChan, err := extractFromCtx(ctx) if err != nil { return err } - b, err := json.Marshal(m) - if err != nil { - return err - } - - l.Debugf("received ANNOUNCE %s", string(b)) - - targetAcctI := ctx.Value(util.APAccount) - if targetAcctI == nil { - // If the target account wasn't set on the context, that means this request didn't pass through the - // API, but came from inside GtS as the result of another activity on this instance. That being so, + if targetAcct == nil || fromFederatorChan == nil { + // If the target account or federator channel wasn't set on the context, that means this request didn't pass + // through the API, but came from inside GtS as the result of another activity on this instance. That being so, // we can safely just ignore this activity, since we know we've already processed it elsewhere. return nil } - targetAcct, ok := targetAcctI.(*gtsmodel.Account) - if !ok { - l.Error("ANNOUNCE: target account was set on context but couldn't be parsed") - return nil - } - - fromFederatorChanI := ctx.Value(util.APFromFederatorChanKey) - if fromFederatorChanI == nil { - l.Error("ANNOUNCE: from federator channel wasn't set on context") - return nil - } - fromFederatorChan, ok := fromFederatorChanI.(chan messages.FromFederator) - if !ok { - l.Error("ANNOUNCE: from federator channel was set on context but couldn't be parsed") - return nil - } boost, isNew, err := f.typeConverter.ASAnnounceToStatus(ctx, announce) if err != nil { diff --git a/internal/federation/federatingdb/create.go b/internal/federation/federatingdb/create.go index 88b0d1e8b..474890c34 100644 --- a/internal/federation/federatingdb/create.go +++ b/internal/federation/federatingdb/create.go @@ -20,19 +20,15 @@ import ( "context" - "encoding/json" "errors" "fmt" - "github.com/go-fed/activity/streams" "github.com/go-fed/activity/streams/vocab" "github.com/sirupsen/logrus" "github.com/superseriousbusiness/gotosocial/internal/ap" "github.com/superseriousbusiness/gotosocial/internal/db" - "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" "github.com/superseriousbusiness/gotosocial/internal/id" "github.com/superseriousbusiness/gotosocial/internal/messages" - "github.com/superseriousbusiness/gotosocial/internal/util" ) // Create adds a new entry to the database which must be able to be @@ -50,44 +46,29 @@ func (f *federatingDB) Create(ctx context.Context, asType vocab.Type) error { l := f.log.WithFields( logrus.Fields{ - "func": "Create", - "asType": asType.GetTypeName(), + "func": "Create", }, ) - m, err := streams.Serialize(asType) + + if l.Level >= logrus.DebugLevel { + i, err := marshalItem(asType) + if err != nil { + return err + } + l = l.WithField("create", i) + l.Debug("entering Create") + } + + targetAcct, fromFederatorChan, err := extractFromCtx(ctx) if err != nil { return err } - b, err := json.Marshal(m) - if err != nil { - return err - } - - l.Debugf("received CREATE asType %s", string(b)) - - targetAcctI := ctx.Value(util.APAccount) - if targetAcctI == nil { - // If the target account wasn't set on the context, that means this request didn't pass through the - // API, but came from inside GtS as the result of another activity on this instance. That being so, + if targetAcct == nil || fromFederatorChan == nil { + // If the target account or federator channel wasn't set on the context, that means this request didn't pass + // through the API, but came from inside GtS as the result of another activity on this instance. That being so, // we can safely just ignore this activity, since we know we've already processed it elsewhere. return nil } - targetAcct, ok := targetAcctI.(*gtsmodel.Account) - if !ok { - l.Error("CREATE: target account was set on context but couldn't be parsed") - return nil - } - - fromFederatorChanI := ctx.Value(util.APFromFederatorChanKey) - if fromFederatorChanI == nil { - l.Error("CREATE: from federator channel wasn't set on context") - return nil - } - fromFederatorChan, ok := fromFederatorChanI.(chan messages.FromFederator) - if !ok { - l.Error("CREATE: from federator channel was set on context but couldn't be parsed") - return nil - } switch asType.GetTypeName() { case ap.ActivityCreate: diff --git a/internal/federation/federatingdb/delete.go b/internal/federation/federatingdb/delete.go index 9aa36ee90..fc77f8025 100644 --- a/internal/federation/federatingdb/delete.go +++ b/internal/federation/federatingdb/delete.go @@ -27,7 +27,6 @@ "github.com/superseriousbusiness/gotosocial/internal/ap" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" "github.com/superseriousbusiness/gotosocial/internal/messages" - "github.com/superseriousbusiness/gotosocial/internal/util" ) // Delete removes the entry with the given id. @@ -40,34 +39,21 @@ func (f *federatingDB) Delete(ctx context.Context, id *url.URL) error { l := f.log.WithFields( logrus.Fields{ "func": "Delete", - "id": id.String(), + "id": id, }, ) - l.Debugf("received DELETE id %s", id.String()) + l.Debug("entering Delete") - targetAcctI := ctx.Value(util.APAccount) - if targetAcctI == nil { - // If the target account wasn't set on the context, that means this request didn't pass through the - // API, but came from inside GtS as the result of another activity on this instance. That being so, + targetAcct, fromFederatorChan, err := extractFromCtx(ctx) + if err != nil { + return err + } + if targetAcct == nil || fromFederatorChan == nil { + // If the target account or federator channel wasn't set on the context, that means this request didn't pass + // through the API, but came from inside GtS as the result of another activity on this instance. That being so, // we can safely just ignore this activity, since we know we've already processed it elsewhere. return nil } - targetAcct, ok := targetAcctI.(*gtsmodel.Account) - if !ok { - l.Error("DELETE: target account was set on context but couldn't be parsed") - return nil - } - - fromFederatorChanI := ctx.Value(util.APFromFederatorChanKey) - if fromFederatorChanI == nil { - l.Error("DELETE: from federator channel wasn't set on context") - return nil - } - fromFederatorChan, ok := fromFederatorChanI.(chan messages.FromFederator) - if !ok { - l.Error("DELETE: from federator channel was set on context but couldn't be parsed") - return nil - } // in a delete we only get the URI, we can't know if we have a status or a profile or something else, // so we have to try a few different things... diff --git a/internal/federation/federatingdb/exists.go b/internal/federation/federatingdb/exists.go index 0e13c1196..ec5599c24 100644 --- a/internal/federation/federatingdb/exists.go +++ b/internal/federation/federatingdb/exists.go @@ -29,14 +29,15 @@ // id. It may not be owned by this application instance. // // The library makes this call only after acquiring a lock first. +// +// Implementation note: this just straight up isn't implemented, and doesn't *really* need to be either. func (f *federatingDB) Exists(c context.Context, id *url.URL) (exists bool, err error) { l := f.log.WithFields( logrus.Fields{ "func": "Exists", - "id": id.String(), + "id": id, }, ) - l.Debugf("entering EXISTS function with id %s", id.String()) - + l.Debug("entering Exists") return false, nil } diff --git a/internal/federation/federatingdb/federatingdb_test.go b/internal/federation/federatingdb/federatingdb_test.go index f32314b10..fc78540f2 100644 --- a/internal/federation/federatingdb/federatingdb_test.go +++ b/internal/federation/federatingdb/federatingdb_test.go @@ -18,4 +18,55 @@ package federatingdb_test -// TODO: write tests for pgfed +import ( + "github.com/sirupsen/logrus" + "github.com/stretchr/testify/suite" + "github.com/superseriousbusiness/gotosocial/internal/config" + "github.com/superseriousbusiness/gotosocial/internal/db" + "github.com/superseriousbusiness/gotosocial/internal/federation/federatingdb" + "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" + "github.com/superseriousbusiness/gotosocial/internal/typeutils" + "github.com/superseriousbusiness/gotosocial/testrig" +) + +type FederatingDBTestSuite struct { + suite.Suite + config *config.Config + db db.DB + log *logrus.Logger + tc typeutils.TypeConverter + federatingDB federatingdb.DB + + testTokens map[string]*gtsmodel.Token + testClients map[string]*gtsmodel.Client + testApplications map[string]*gtsmodel.Application + testUsers map[string]*gtsmodel.User + testAccounts map[string]*gtsmodel.Account + testAttachments map[string]*gtsmodel.MediaAttachment + testStatuses map[string]*gtsmodel.Status + testBlocks map[string]*gtsmodel.Block +} + +func (suite *FederatingDBTestSuite) SetupSuite() { + suite.testTokens = testrig.NewTestTokens() + suite.testClients = testrig.NewTestClients() + suite.testApplications = testrig.NewTestApplications() + suite.testUsers = testrig.NewTestUsers() + suite.testAccounts = testrig.NewTestAccounts() + suite.testAttachments = testrig.NewTestAttachments() + suite.testStatuses = testrig.NewTestStatuses() + suite.testBlocks = testrig.NewTestBlocks() +} + +func (suite *FederatingDBTestSuite) SetupTest() { + suite.config = testrig.NewTestConfig() + suite.db = testrig.NewTestDB() + suite.tc = testrig.NewTestTypeConverter(suite.db) + suite.log = testrig.NewTestLog() + suite.federatingDB = testrig.NewTestFederatingDB(suite.db) + testrig.StandardDBSetup(suite.db, suite.testAccounts) +} + +func (suite *FederatingDBTestSuite) TearDownTest() { + testrig.StandardDBTeardown(suite.db) +} diff --git a/internal/federation/federatingdb/followers.go b/internal/federation/federatingdb/followers.go index 69c68b8b9..61c5d4287 100644 --- a/internal/federation/federatingdb/followers.go +++ b/internal/federation/federatingdb/followers.go @@ -5,12 +5,9 @@ "fmt" "net/url" - "github.com/go-fed/activity/streams" "github.com/go-fed/activity/streams/vocab" "github.com/sirupsen/logrus" "github.com/superseriousbusiness/gotosocial/internal/db" - "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" - "github.com/superseriousbusiness/gotosocial/internal/util" ) // Followers obtains the Followers Collection for an actor with the @@ -22,39 +19,28 @@ func (f *federatingDB) Followers(ctx context.Context, actorIRI *url.URL) (followers vocab.ActivityStreamsCollection, err error) { l := f.log.WithFields( logrus.Fields{ - "func": "Followers", - "actorIRI": actorIRI.String(), + "func": "Followers", + "id": actorIRI, }, ) - l.Debugf("entering FOLLOWERS function with actorIRI %s", actorIRI.String()) + l.Debug("entering Followers") - acct := >smodel.Account{} - - if util.IsUserPath(actorIRI) { - acct, err = f.db.GetAccountByURI(ctx, actorIRI.String()) - if err != nil { - return nil, fmt.Errorf("FOLLOWERS: db error getting account with uri %s: %s", actorIRI.String(), err) - } - } else if util.IsFollowersPath(actorIRI) { - if err := f.db.GetWhere(ctx, []db.Where{{Key: "followers_uri", Value: actorIRI.String()}}, acct); err != nil { - return nil, fmt.Errorf("FOLLOWERS: db error getting account with followers uri %s: %s", actorIRI.String(), err) - } - } else { - return nil, fmt.Errorf("FOLLOWERS: could not parse actor IRI %s as users or followers path", actorIRI.String()) + acct, err := f.getAccountForIRI(ctx, actorIRI) + if err != nil { + return nil, err } acctFollowers, err := f.db.GetAccountFollowedBy(ctx, acct.ID, false) if err != nil { - return nil, fmt.Errorf("FOLLOWERS: db error getting followers for account id %s: %s", acct.ID, err) + return nil, fmt.Errorf("Followers: db error getting followers for account id %s: %s", acct.ID, err) } - followers = streams.NewActivityStreamsCollection() - items := streams.NewActivityStreamsItemsProperty() + iris := []*url.URL{} for _, follow := range acctFollowers { if follow.Account == nil { - followAccount, err := f.db.GetAccountByID(ctx, follow.AccountID) + a, err := f.db.GetAccountByID(ctx, follow.AccountID) if err != nil { - errWrapped := fmt.Errorf("FOLLOWERS: db error getting account id %s: %s", follow.AccountID, err) + errWrapped := fmt.Errorf("Followers: db error getting account id %s: %s", follow.AccountID, err) if err == db.ErrNoEntries { // no entry for this account id so it's probably been deleted and we haven't caught up yet l.Error(errWrapped) @@ -64,15 +50,14 @@ func (f *federatingDB) Followers(ctx context.Context, actorIRI *url.URL) (follow return nil, errWrapped } } - follow.Account = followAccount + follow.Account = a } - - uri, err := url.Parse(follow.Account.URI) + u, err := url.Parse(follow.Account.URI) if err != nil { - return nil, fmt.Errorf("FOLLOWERS: error parsing %s as url: %s", follow.Account.URI, err) + return nil, err } - items.AppendIRI(uri) + iris = append(iris, u) } - followers.SetActivityStreamsItems(items) - return + + return f.collectIRIs(ctx, iris) } diff --git a/internal/federation/federatingdb/followers_test.go b/internal/federation/federatingdb/followers_test.go new file mode 100644 index 000000000..d993a7201 --- /dev/null +++ b/internal/federation/federatingdb/followers_test.go @@ -0,0 +1,53 @@ +/* + GoToSocial + Copyright (C) 2021 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 . +*/ + +package federatingdb_test + +import ( + "context" + "encoding/json" + "testing" + + "github.com/go-fed/activity/streams" + "github.com/stretchr/testify/suite" + "github.com/superseriousbusiness/gotosocial/testrig" +) + +type FollowersTestSuite struct { + FederatingDBTestSuite +} + +func (suite *FollowersTestSuite) TestGetFollowers() { + testAccount := suite.testAccounts["local_account_2"] + + f, err := suite.federatingDB.Followers(context.Background(), testrig.URLMustParse(testAccount.URI)) + suite.NoError(err) + + fi, err := streams.Serialize(f) + suite.NoError(err) + + fJson, err := json.Marshal(fi) + suite.NoError(err) + + // zork follows local_account_2 so this should be reflected in the response + suite.Equal(`{"@context":"https://www.w3.org/ns/activitystreams","items":"http://localhost:8080/users/the_mighty_zork","type":"Collection"}`, string(fJson)) +} + +func TestFollowersTestSuite(t *testing.T) { + suite.Run(t, &FollowersTestSuite{}) +} diff --git a/internal/federation/federatingdb/following.go b/internal/federation/federatingdb/following.go index a36f4e203..2cc024832 100644 --- a/internal/federation/federatingdb/following.go +++ b/internal/federation/federatingdb/following.go @@ -5,12 +5,9 @@ "fmt" "net/url" - "github.com/go-fed/activity/streams" "github.com/go-fed/activity/streams/vocab" "github.com/sirupsen/logrus" "github.com/superseriousbusiness/gotosocial/internal/db" - "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" - "github.com/superseriousbusiness/gotosocial/internal/util" ) // Following obtains the Following Collection for an actor with the @@ -22,53 +19,28 @@ func (f *federatingDB) Following(ctx context.Context, actorIRI *url.URL) (following vocab.ActivityStreamsCollection, err error) { l := f.log.WithFields( logrus.Fields{ - "func": "Following", - "actorIRI": actorIRI.String(), + "func": "Following", + "id": actorIRI, }, ) - l.Debugf("entering FOLLOWING function with actorIRI %s", actorIRI.String()) + l.Debug("entering Following") - var acct *gtsmodel.Account - if util.IsUserPath(actorIRI) { - username, err := util.ParseUserPath(actorIRI) - if err != nil { - return nil, fmt.Errorf("FOLLOWING: error parsing user path: %s", err) - } - - a, err := f.db.GetLocalAccountByUsername(ctx, username) - if err != nil { - return nil, fmt.Errorf("FOLLOWING: db error getting account with uri %s: %s", actorIRI.String(), err) - } - - acct = a - } else if util.IsFollowingPath(actorIRI) { - username, err := util.ParseFollowingPath(actorIRI) - if err != nil { - return nil, fmt.Errorf("FOLLOWING: error parsing following path: %s", err) - } - - a, err := f.db.GetLocalAccountByUsername(ctx, username) - if err != nil { - return nil, fmt.Errorf("FOLLOWING: db error getting account with following uri %s: %s", actorIRI.String(), err) - } - - acct = a - } else { - return nil, fmt.Errorf("FOLLOWING: could not parse actor IRI %s as users or following path", actorIRI.String()) + acct, err := f.getAccountForIRI(ctx, actorIRI) + if err != nil { + return nil, err } acctFollowing, err := f.db.GetAccountFollows(ctx, acct.ID) if err != nil { - return nil, fmt.Errorf("FOLLOWING: db error getting following for account id %s: %s", acct.ID, err) + return nil, fmt.Errorf("Following: db error getting following for account id %s: %s", acct.ID, err) } - following = streams.NewActivityStreamsCollection() - items := streams.NewActivityStreamsItemsProperty() + iris := []*url.URL{} for _, follow := range acctFollowing { - if follow.Account == nil { - followAccount, err := f.db.GetAccountByID(ctx, follow.AccountID) + if follow.TargetAccount == nil { + a, err := f.db.GetAccountByID(ctx, follow.TargetAccountID) if err != nil { - errWrapped := fmt.Errorf("FOLLOWING: db error getting account id %s: %s", follow.AccountID, err) + errWrapped := fmt.Errorf("Following: db error getting account id %s: %s", follow.TargetAccountID, err) if err == db.ErrNoEntries { // no entry for this account id so it's probably been deleted and we haven't caught up yet l.Error(errWrapped) @@ -78,15 +50,14 @@ func (f *federatingDB) Following(ctx context.Context, actorIRI *url.URL) (follow return nil, errWrapped } } - follow.Account = followAccount + follow.TargetAccount = a } - - uri, err := url.Parse(follow.Account.URI) + u, err := url.Parse(follow.TargetAccount.URI) if err != nil { - return nil, fmt.Errorf("FOLLOWING: error parsing %s as url: %s", follow.Account.URI, err) + return nil, err } - items.AppendIRI(uri) + iris = append(iris, u) } - following.SetActivityStreamsItems(items) - return + + return f.collectIRIs(ctx, iris) } diff --git a/internal/federation/federatingdb/following_test.go b/internal/federation/federatingdb/following_test.go new file mode 100644 index 000000000..7788840d7 --- /dev/null +++ b/internal/federation/federatingdb/following_test.go @@ -0,0 +1,53 @@ +/* + GoToSocial + Copyright (C) 2021 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 . +*/ + +package federatingdb_test + +import ( + "context" + "encoding/json" + "testing" + + "github.com/go-fed/activity/streams" + "github.com/stretchr/testify/suite" + "github.com/superseriousbusiness/gotosocial/testrig" +) + +type FollowingTestSuite struct { + FederatingDBTestSuite +} + +func (suite *FollowingTestSuite) TestGetFollowing() { + testAccount := suite.testAccounts["local_account_1"] + + f, err := suite.federatingDB.Following(context.Background(), testrig.URLMustParse(testAccount.URI)) + suite.NoError(err) + + fi, err := streams.Serialize(f) + suite.NoError(err) + + fJson, err := json.Marshal(fi) + suite.NoError(err) + + // zork follows admin account and local_account_1 + suite.Equal(`{"@context":"https://www.w3.org/ns/activitystreams","items":["http://localhost:8080/users/admin","http://localhost:8080/users/1happyturtle"],"type":"Collection"}`, string(fJson)) +} + +func TestFollowingTestSuite(t *testing.T) { + suite.Run(t, &FollowingTestSuite{}) +} diff --git a/internal/federation/federatingdb/get.go b/internal/federation/federatingdb/get.go index cc04dd851..505891ca4 100644 --- a/internal/federation/federatingdb/get.go +++ b/internal/federation/federatingdb/get.go @@ -25,8 +25,6 @@ "github.com/go-fed/activity/streams/vocab" "github.com/sirupsen/logrus" - "github.com/superseriousbusiness/gotosocial/internal/db" - "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" "github.com/superseriousbusiness/gotosocial/internal/util" ) @@ -37,46 +35,33 @@ func (f *federatingDB) Get(ctx context.Context, id *url.URL) (value vocab.Type, l := f.log.WithFields( logrus.Fields{ "func": "Get", - "id": id.String(), + "id": id, }, ) - l.Debug("entering GET function") + l.Debug("entering Get") if util.IsUserPath(id) { acct, err := f.db.GetAccountByURI(ctx, id.String()) if err != nil { return nil, err } - l.Debug("is user path! returning account") return f.typeConverter.AccountToAS(ctx, acct) } - if util.IsFollowersPath(id) { - acct := >smodel.Account{} - if err := f.db.GetWhere(ctx, []db.Where{{Key: "followers_uri", Value: id.String()}}, acct); err != nil { - return nil, err - } - - followersURI, err := url.Parse(acct.FollowersURI) + if util.IsStatusesPath(id) { + status, err := f.db.GetStatusByURI(ctx, id.String()) if err != nil { return nil, err } + return f.typeConverter.StatusToAS(ctx, status) + } - return f.Followers(ctx, followersURI) + if util.IsFollowersPath(id) { + return f.Followers(ctx, id) } if util.IsFollowingPath(id) { - acct := >smodel.Account{} - if err := f.db.GetWhere(ctx, []db.Where{{Key: "following_uri", Value: id.String()}}, acct); err != nil { - return nil, err - } - - followingURI, err := url.Parse(acct.FollowingURI) - if err != nil { - return nil, err - } - - return f.Following(ctx, followingURI) + return f.Following(ctx, id) } return nil, errors.New("could not get") diff --git a/internal/federation/federatingdb/inbox.go b/internal/federation/federatingdb/inbox.go index 4390a8b4b..95886b571 100644 --- a/internal/federation/federatingdb/inbox.go +++ b/internal/federation/federatingdb/inbox.go @@ -20,44 +20,19 @@ import ( "context" - "fmt" "net/url" - "github.com/go-fed/activity/pub" "github.com/go-fed/activity/streams" "github.com/go-fed/activity/streams/vocab" - "github.com/sirupsen/logrus" - "github.com/superseriousbusiness/gotosocial/internal/util" ) // InboxContains returns true if the OrderedCollection at 'inbox' // contains the specified 'id'. // // The library makes this call only after acquiring a lock first. +// +// Implementation note: we have our own logic for inboxes so always return false here. func (f *federatingDB) InboxContains(c context.Context, inbox, id *url.URL) (contains bool, err error) { - l := f.log.WithFields( - logrus.Fields{ - "func": "InboxContains", - "id": id.String(), - }, - ) - l.Debugf("entering INBOXCONTAINS function with for inbox %s and id %s", inbox.String(), id.String()) - - if !util.IsInboxPath(inbox) { - return false, fmt.Errorf("%s is not an inbox URI", inbox.String()) - } - - activityI := c.Value(util.APActivity) - if activityI == nil { - return false, fmt.Errorf("no activity was set for id %s", id.String()) - } - activity, ok := activityI.(pub.Activity) - if !ok || activity == nil { - return false, fmt.Errorf("could not parse contextual activity for id %s", id.String()) - } - - l.Debugf("activity type %s for id %s", activity.GetTypeName(), id.String()) - return false, nil } @@ -65,13 +40,9 @@ func (f *federatingDB) InboxContains(c context.Context, inbox, id *url.URL) (con // the specified IRI, for prepending new items. // // The library makes this call only after acquiring a lock first. +// +// Implementation note: we don't (yet) serve inboxes, so just return empty and nil here. func (f *federatingDB) GetInbox(c context.Context, inboxIRI *url.URL) (inbox vocab.ActivityStreamsOrderedCollectionPage, err error) { - l := f.log.WithFields( - logrus.Fields{ - "func": "GetInbox", - }, - ) - l.Debugf("entering GETINBOX function with inboxIRI %s", inboxIRI.String()) return streams.NewActivityStreamsOrderedCollectionPage(), nil } @@ -80,12 +51,8 @@ func (f *federatingDB) GetInbox(c context.Context, inboxIRI *url.URL) (inbox voc // database entries. Separate calls to Create will do that. // // The library makes this call only after acquiring a lock first. +// +// Implementation note: we don't allow inbox setting so just return nil here. func (f *federatingDB) SetInbox(c context.Context, inbox vocab.ActivityStreamsOrderedCollectionPage) error { - l := f.log.WithFields( - logrus.Fields{ - "func": "SetInbox", - }, - ) - l.Debug("entering SETINBOX function") return nil } diff --git a/internal/federation/federatingdb/liked.go b/internal/federation/federatingdb/liked.go index b85398fef..93b3d2c88 100644 --- a/internal/federation/federatingdb/liked.go +++ b/internal/federation/federatingdb/liked.go @@ -22,8 +22,8 @@ "context" "net/url" + "github.com/go-fed/activity/streams" "github.com/go-fed/activity/streams/vocab" - "github.com/sirupsen/logrus" ) // Liked obtains the Liked Collection for an actor with the @@ -32,13 +32,8 @@ // If modified, the library will then call Update. // // The library makes this call only after acquiring a lock first. +// +// Implementation note: we don't serve a Liked collection *yet* so just return an empty collection for now. func (f *federatingDB) Liked(c context.Context, actorIRI *url.URL) (liked vocab.ActivityStreamsCollection, err error) { - l := f.log.WithFields( - logrus.Fields{ - "func": "Liked", - "actorIRI": actorIRI.String(), - }, - ) - l.Debugf("entering LIKED function with actorIRI %s", actorIRI.String()) - return nil, nil + return streams.NewActivityStreamsCollection(), nil } diff --git a/internal/federation/federatingdb/outbox.go b/internal/federation/federatingdb/outbox.go index 81b90aae2..07caf999e 100644 --- a/internal/federation/federatingdb/outbox.go +++ b/internal/federation/federatingdb/outbox.go @@ -20,29 +20,19 @@ import ( "context" - "fmt" "net/url" "github.com/go-fed/activity/streams" "github.com/go-fed/activity/streams/vocab" - "github.com/sirupsen/logrus" - "github.com/superseriousbusiness/gotosocial/internal/db" - "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" - "github.com/superseriousbusiness/gotosocial/internal/util" ) // GetOutbox returns the first ordered collection page of the outbox // at the specified IRI, for prepending new items. // // The library makes this call only after acquiring a lock first. +// +// Implementation note: we don't (yet) serve outboxes, so just return empty and nil here. func (f *federatingDB) GetOutbox(ctx context.Context, outboxIRI *url.URL) (inbox vocab.ActivityStreamsOrderedCollectionPage, err error) { - l := f.log.WithFields( - logrus.Fields{ - "func": "GetOutbox", - }, - ) - l.Debug("entering GETOUTBOX function") - return streams.NewActivityStreamsOrderedCollectionPage(), nil } @@ -51,14 +41,9 @@ func (f *federatingDB) GetOutbox(ctx context.Context, outboxIRI *url.URL) (inbox // database entries. Separate calls to Create will do that. // // The library makes this call only after acquiring a lock first. +// +// Implementation note: we don't allow outbox setting so just return nil here. func (f *federatingDB) SetOutbox(ctx context.Context, outbox vocab.ActivityStreamsOrderedCollectionPage) error { - l := f.log.WithFields( - logrus.Fields{ - "func": "SetOutbox", - }, - ) - l.Debug("entering SETOUTBOX function") - return nil } @@ -67,23 +52,9 @@ func (f *federatingDB) SetOutbox(ctx context.Context, outbox vocab.ActivityStrea // // The library makes this call only after acquiring a lock first. func (f *federatingDB) OutboxForInbox(ctx context.Context, inboxIRI *url.URL) (outboxIRI *url.URL, err error) { - l := f.log.WithFields( - logrus.Fields{ - "func": "OutboxForInbox", - "inboxIRI": inboxIRI.String(), - }, - ) - l.Debugf("entering OUTBOXFORINBOX function with inboxIRI %s", inboxIRI.String()) - - if !util.IsInboxPath(inboxIRI) { - return nil, fmt.Errorf("%s is not an inbox URI", inboxIRI.String()) - } - acct := >smodel.Account{} - if err := f.db.GetWhere(ctx, []db.Where{{Key: "inbox_uri", Value: inboxIRI.String()}}, acct); err != nil { - if err == db.ErrNoEntries { - return nil, fmt.Errorf("no actor found that corresponds to inbox %s", inboxIRI.String()) - } - return nil, fmt.Errorf("db error searching for actor with inbox %s", inboxIRI.String()) + acct, err := f.getAccountForIRI(ctx, inboxIRI) + if err != nil { + return nil, err } return url.Parse(acct.OutboxURI) } diff --git a/internal/federation/federatingdb/owns.go b/internal/federation/federatingdb/owns.go index 1c1f2512d..04a417490 100644 --- a/internal/federation/federatingdb/owns.go +++ b/internal/federation/federatingdb/owns.go @@ -36,10 +36,10 @@ func (f *federatingDB) Owns(ctx context.Context, id *url.URL) (bool, error) { l := f.log.WithFields( logrus.Fields{ "func": "Owns", - "id": id.String(), + "id": id, }, ) - l.Tracef("entering OWNS function with id %s", id.String()) + l.Debug("entering Owns") // if the id host isn't this instance host, we don't own this IRI if id.Host != f.config.Host { diff --git a/internal/federation/federatingdb/undo.go b/internal/federation/federatingdb/undo.go index 481c2d787..9fcb6ce1a 100644 --- a/internal/federation/federatingdb/undo.go +++ b/internal/federation/federatingdb/undo.go @@ -20,46 +20,40 @@ import ( "context" - "encoding/json" "errors" "fmt" - "github.com/go-fed/activity/streams" "github.com/go-fed/activity/streams/vocab" "github.com/sirupsen/logrus" "github.com/superseriousbusiness/gotosocial/internal/ap" "github.com/superseriousbusiness/gotosocial/internal/db" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" - "github.com/superseriousbusiness/gotosocial/internal/util" ) func (f *federatingDB) Undo(ctx context.Context, undo vocab.ActivityStreamsUndo) error { l := f.log.WithFields( logrus.Fields{ - "func": "Undo", - "asType": undo.GetTypeName(), + "func": "Undo", }, ) - m, err := streams.Serialize(undo) - if err != nil { - return err - } - b, err := json.Marshal(m) - if err != nil { - return err - } - l.Debugf("received UNDO asType %s", string(b)) - targetAcctI := ctx.Value(util.APAccount) - if targetAcctI == nil { - // If the target account wasn't set on the context, that means this request didn't pass through the - // API, but came from inside GtS as the result of another activity on this instance. That being so, - // we can safely just ignore this activity, since we know we've already processed it elsewhere. - return nil + if l.Level >= logrus.DebugLevel { + i, err := marshalItem(undo) + if err != nil { + return err + } + l = l.WithField("undo", i) + l.Debug("entering Undo") } - targetAcct, ok := targetAcctI.(*gtsmodel.Account) - if !ok { - l.Error("UNDO: target account was set on context but couldn't be parsed") + + targetAcct, fromFederatorChan, err := extractFromCtx(ctx) + if err != nil { + return err + } + if targetAcct == nil || fromFederatorChan == nil { + // If the target account or federator channel wasn't set on the context, that means this request didn't pass + // through the API, but came from inside GtS as the result of another activity on this instance. That being so, + // we can safely just ignore this activity, since we know we've already processed it elsewhere. return nil } diff --git a/internal/federation/federatingdb/update.go b/internal/federation/federatingdb/update.go index 5dec2bd69..9e7bd3d5b 100644 --- a/internal/federation/federatingdb/update.go +++ b/internal/federation/federatingdb/update.go @@ -20,11 +20,9 @@ import ( "context" - "encoding/json" "errors" "fmt" - "github.com/go-fed/activity/streams" "github.com/go-fed/activity/streams/vocab" "github.com/sirupsen/logrus" "github.com/superseriousbusiness/gotosocial/internal/ap" @@ -45,35 +43,32 @@ func (f *federatingDB) Update(ctx context.Context, asType vocab.Type) error { l := f.log.WithFields( logrus.Fields{ - "func": "Update", - "asType": asType.GetTypeName(), + "func": "Update", }, ) - m, err := streams.Serialize(asType) + + if l.Level >= logrus.DebugLevel { + i, err := marshalItem(asType) + if err != nil { + return err + } + l = l.WithField("update", i) + l.Debug("entering Update") + } + + targetAcct, fromFederatorChan, err := extractFromCtx(ctx) if err != nil { return err } - b, err := json.Marshal(m) - if err != nil { - return err - } - - l.Debugf("received UPDATE asType %s", string(b)) - - targetAcctI := ctx.Value(util.APAccount) - if targetAcctI == nil { - // If the target account wasn't set on the context, that means this request didn't pass through the - // API, but came from inside GtS as the result of another activity on this instance. That being so, + if targetAcct == nil || fromFederatorChan == nil { + // If the target account or federator channel wasn't set on the context, that means this request didn't pass + // through the API, but came from inside GtS as the result of another activity on this instance. That being so, // we can safely just ignore this activity, since we know we've already processed it elsewhere. return nil } - targetAcct, ok := targetAcctI.(*gtsmodel.Account) - if !ok { - l.Error("UPDATE: target account was set on context but couldn't be parsed") - } requestingAcctI := ctx.Value(util.APRequestingAccount) - if targetAcctI == nil { + if requestingAcctI == nil { l.Error("UPDATE: requesting account wasn't set on context") } requestingAcct, ok := requestingAcctI.(*gtsmodel.Account) @@ -81,15 +76,6 @@ func (f *federatingDB) Update(ctx context.Context, asType vocab.Type) error { l.Error("UPDATE: requesting account was set on context but couldn't be parsed") } - fromFederatorChanI := ctx.Value(util.APFromFederatorChanKey) - if fromFederatorChanI == nil { - l.Error("UPDATE: from federator channel wasn't set on context") - } - fromFederatorChan, ok := fromFederatorChanI.(chan messages.FromFederator) - if !ok { - l.Error("UPDATE: from federator channel was set on context but couldn't be parsed") - } - typeName := asType.GetTypeName() if typeName == ap.ActorApplication || typeName == ap.ActorGroup || diff --git a/internal/federation/federatingdb/util.go b/internal/federation/federatingdb/util.go index d8c7d8e8a..d719bf16d 100644 --- a/internal/federation/federatingdb/util.go +++ b/internal/federation/federatingdb/util.go @@ -32,6 +32,7 @@ "github.com/superseriousbusiness/gotosocial/internal/db" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" "github.com/superseriousbusiness/gotosocial/internal/id" + "github.com/superseriousbusiness/gotosocial/internal/messages" "github.com/superseriousbusiness/gotosocial/internal/util" ) @@ -64,19 +65,18 @@ func sameActor(activityActor vocab.ActivityStreamsActorProperty, followActor voc func (f *federatingDB) NewID(ctx context.Context, t vocab.Type) (idURL *url.URL, err error) { l := f.log.WithFields( logrus.Fields{ - "func": "NewID", - "asType": t.GetTypeName(), + "func": "NewID", }, ) - m, err := streams.Serialize(t) - if err != nil { - return nil, err + + if l.Level >= logrus.DebugLevel { + i, err := marshalItem(t) + if err != nil { + return nil, err + } + l = l.WithField("newID", i) + l.Debug("entering NewID") } - b, err := json.Marshal(m) - if err != nil { - return nil, err - } - l.Debugf("received NEWID request for asType %s", string(b)) switch t.GetTypeName() { case ap.ActivityFollow: @@ -201,23 +201,9 @@ func (f *federatingDB) NewID(ctx context.Context, t vocab.Type) (idURL *url.URL, // // The library makes this call only after acquiring a lock first. func (f *federatingDB) ActorForOutbox(ctx context.Context, outboxIRI *url.URL) (actorIRI *url.URL, err error) { - l := f.log.WithFields( - logrus.Fields{ - "func": "ActorForOutbox", - "inboxIRI": outboxIRI.String(), - }, - ) - l.Debugf("entering ACTORFOROUTBOX function with outboxIRI %s", outboxIRI.String()) - - if !util.IsOutboxPath(outboxIRI) { - return nil, fmt.Errorf("%s is not an outbox URI", outboxIRI.String()) - } - acct := >smodel.Account{} - if err := f.db.GetWhere(ctx, []db.Where{{Key: "outbox_uri", Value: outboxIRI.String()}}, acct); err != nil { - if err == db.ErrNoEntries { - return nil, fmt.Errorf("no actor found that corresponds to outbox %s", outboxIRI.String()) - } - return nil, fmt.Errorf("db error searching for actor with outbox %s", outboxIRI.String()) + acct, err := f.getAccountForIRI(ctx, outboxIRI) + if err != nil { + return nil, err } return url.Parse(acct.URI) } @@ -226,23 +212,116 @@ func (f *federatingDB) ActorForOutbox(ctx context.Context, outboxIRI *url.URL) ( // // The library makes this call only after acquiring a lock first. func (f *federatingDB) ActorForInbox(ctx context.Context, inboxIRI *url.URL) (actorIRI *url.URL, err error) { - l := f.log.WithFields( - logrus.Fields{ - "func": "ActorForInbox", - "inboxIRI": inboxIRI.String(), - }, - ) - l.Debugf("entering ACTORFORINBOX function with inboxIRI %s", inboxIRI.String()) - - if !util.IsInboxPath(inboxIRI) { - return nil, fmt.Errorf("%s is not an inbox URI", inboxIRI.String()) - } - acct := >smodel.Account{} - if err := f.db.GetWhere(ctx, []db.Where{{Key: "inbox_uri", Value: inboxIRI.String()}}, acct); err != nil { - if err == db.ErrNoEntries { - return nil, fmt.Errorf("no actor found that corresponds to inbox %s", inboxIRI.String()) - } - return nil, fmt.Errorf("db error searching for actor with inbox %s", inboxIRI.String()) + acct, err := f.getAccountForIRI(ctx, inboxIRI) + if err != nil { + return nil, err } return url.Parse(acct.URI) } + +// getAccountForIRI returns the account that corresponds to or owns the given IRI. +func (f *federatingDB) getAccountForIRI(ctx context.Context, iri *url.URL) (account *gtsmodel.Account, err error) { + acct := >smodel.Account{} + + if util.IsInboxPath(iri) { + if err := f.db.GetWhere(ctx, []db.Where{{Key: "inbox_uri", Value: iri.String()}}, acct); err != nil { + if err == db.ErrNoEntries { + return nil, fmt.Errorf("no actor found that corresponds to inbox %s", iri.String()) + } + return nil, fmt.Errorf("db error searching for actor with inbox %s", iri.String()) + } + return acct, nil + } + + if util.IsOutboxPath(iri) { + if err := f.db.GetWhere(ctx, []db.Where{{Key: "outbox_uri", Value: iri.String()}}, acct); err != nil { + if err == db.ErrNoEntries { + return nil, fmt.Errorf("no actor found that corresponds to outbox %s", iri.String()) + } + return nil, fmt.Errorf("db error searching for actor with outbox %s", iri.String()) + } + return acct, nil + } + + if util.IsUserPath(iri) { + if err := f.db.GetWhere(ctx, []db.Where{{Key: "uri", Value: iri.String()}}, acct); err != nil { + if err == db.ErrNoEntries { + return nil, fmt.Errorf("no actor found that corresponds to uri %s", iri.String()) + } + return nil, fmt.Errorf("db error searching for actor with uri %s", iri.String()) + } + return acct, nil + } + + if util.IsFollowersPath(iri) { + if err := f.db.GetWhere(ctx, []db.Where{{Key: "followers_uri", Value: iri.String()}}, acct); err != nil { + if err == db.ErrNoEntries { + return nil, fmt.Errorf("no actor found that corresponds to followers_uri %s", iri.String()) + } + return nil, fmt.Errorf("db error searching for actor with followers_uri %s", iri.String()) + } + return acct, nil + } + + if util.IsFollowingPath(iri) { + if err := f.db.GetWhere(ctx, []db.Where{{Key: "following_uri", Value: iri.String()}}, acct); err != nil { + if err == db.ErrNoEntries { + return nil, fmt.Errorf("no actor found that corresponds to following_uri %s", iri.String()) + } + return nil, fmt.Errorf("db error searching for actor with following_uri %s", iri.String()) + } + return acct, nil + } + + return nil, fmt.Errorf("getActorForIRI: iri %s not recognised", iri) +} + +// collectFollows takes a slice of iris and converts them into ActivityStreamsCollection of IRIs. +func (f *federatingDB) collectIRIs(ctx context.Context, iris []*url.URL) (vocab.ActivityStreamsCollection, error) { + collection := streams.NewActivityStreamsCollection() + items := streams.NewActivityStreamsItemsProperty() + for _, i := range iris { + items.AppendIRI(i) + } + collection.SetActivityStreamsItems(items) + return collection, nil +} + +// extractFromCtx extracts some useful values from a context passed into the federatingDB via the API: +// - The target account that owns the inbox or URI being interacted with. +// - A channel that messages for the processor can be placed into. +func extractFromCtx(ctx context.Context) (*gtsmodel.Account, chan messages.FromFederator, error) { + var targetAcct *gtsmodel.Account + targetAcctI := ctx.Value(util.APAccount) + if targetAcctI != nil { + var ok bool + targetAcct, ok = targetAcctI.(*gtsmodel.Account) + if !ok { + return nil, nil, errors.New("extractFromCtx: account value in context not parseable") + } + } + + var fromFederatorChan chan messages.FromFederator + fromFederatorChanI := ctx.Value(util.APFromFederatorChanKey) + if fromFederatorChanI != nil { + var ok bool + fromFederatorChan, ok = fromFederatorChanI.(chan messages.FromFederator) + if !ok { + return nil, nil, errors.New("extractFromCtx: fromFederatorChan value in context not parseable") + } + } + + return targetAcct, fromFederatorChan, nil +} + +func marshalItem(item vocab.Type) (string, error) { + m, err := streams.Serialize(item) + if err != nil { + return "", err + } + b, err := json.Marshal(m) + if err != nil { + return "", err + } + return string(b), nil +} diff --git a/internal/federation/finger.go b/internal/federation/finger.go index 5cdd4c04d..ef7896b86 100644 --- a/internal/federation/finger.go +++ b/internal/federation/finger.go @@ -39,7 +39,7 @@ func (f *federator) FingerRemoteAccount(ctx context.Context, requestingUsername return nil, fmt.Errorf("FingerRemoteAccount: error getting transport for username %s while dereferencing @%s@%s: %s", requestingUsername, targetUsername, targetDomain, err) } - b, err := t.Finger(context.Background(), targetUsername, targetDomain) + b, err := t.Finger(ctx, targetUsername, targetDomain) if err != nil { return nil, fmt.Errorf("FingerRemoteAccount: error doing request on behalf of username %s while dereferencing @%s@%s: %s", requestingUsername, targetUsername, targetDomain, err) } diff --git a/internal/gtsmodel/account.go b/internal/gtsmodel/account.go index 40f1ee726..67a7e435a 100644 --- a/internal/gtsmodel/account.go +++ b/internal/gtsmodel/account.go @@ -33,7 +33,7 @@ type Account struct { CreatedAt time.Time `validate:"-" bun:"type:timestamptz,nullzero,notnull,default:current_timestamp"` // when was item created UpdatedAt time.Time `validate:"-" bun:"type:timestamptz,nullzero,notnull,default:current_timestamp"` // when was item last updated Username string `validate:"required" bun:",nullzero,notnull,unique:userdomain"` // Username of the account, should just be a string of [a-zA-Z0-9_]. Can be added to domain to create the full username in the form ``[username]@[domain]`` eg., ``user_96@example.org``. Username and domain should be unique *with* each other - Domain string `validate:"omitempty,fqdn" bun:",nullzero,unique:userdomain"` // Domain of the account, will be null if this is a local account, otherwise something like ``example.org`` or ``mastodon.social``. Should be unique with username. + Domain string `validate:"omitempty,fqdn" bun:",nullzero,unique:userdomain"` // Domain of the account, will be null if this is a local account, otherwise something like ``example.org``. Should be unique with username. AvatarMediaAttachmentID string `validate:"omitempty,ulid" bun:"type:CHAR(26),nullzero"` // Database ID of the media attachment, if present AvatarMediaAttachment *MediaAttachment `validate:"-" bun:"rel:belongs-to"` // MediaAttachment corresponding to avatarMediaAttachmentID AvatarRemoteURL string `validate:"omitempty,url" bun:",nullzero"` // For a non-local account, where can the header be fetched? diff --git a/internal/log/log.go b/internal/log/log.go index 2e1afdb52..b71e62e14 100644 --- a/internal/log/log.go +++ b/internal/log/log.go @@ -46,7 +46,7 @@ func New(level string) (*logrus.Logger, error) { log.SetFormatter(&logrus.TextFormatter{ DisableColors: true, - ForceQuote: true, + DisableQuote: true, FullTimestamp: true, }) diff --git a/internal/media/handler.go b/internal/media/handler.go index 70c5c0826..429dee88a 100644 --- a/internal/media/handler.go +++ b/internal/media/handler.go @@ -323,7 +323,7 @@ func (mh *mediaHandler) ProcessRemoteHeaderOrAvatar(ctx context.Context, t trans expectedContentType = currentAttachment.File.ContentType } - attachmentBytes, err := t.DereferenceMedia(context.Background(), remoteIRI, expectedContentType) + attachmentBytes, err := t.DereferenceMedia(ctx, remoteIRI, expectedContentType) if err != nil { return nil, fmt.Errorf("dereferencing remote media with url %s: %s", remoteIRI.String(), err) } diff --git a/internal/oauth/server.go b/internal/oauth/server.go index 6d8f50064..0386ade40 100644 --- a/internal/oauth/server.go +++ b/internal/oauth/server.go @@ -55,7 +55,7 @@ type Server interface { HandleTokenRequest(w http.ResponseWriter, r *http.Request) error HandleAuthorizeRequest(w http.ResponseWriter, r *http.Request) error ValidationBearerToken(r *http.Request) (oauth2.TokenInfo, error) - GenerateUserAccessToken(ti oauth2.TokenInfo, clientSecret string, userID string) (accessToken oauth2.TokenInfo, err error) + GenerateUserAccessToken(ctx context.Context, ti oauth2.TokenInfo, clientSecret string, userID string) (accessToken oauth2.TokenInfo, err error) LoadAccessToken(ctx context.Context, access string) (accessToken oauth2.TokenInfo, err error) } @@ -66,8 +66,8 @@ type s struct { } // New returns a new oauth server that implements the Server interface -func New(database db.Basic, log *logrus.Logger) Server { - ts := newTokenStore(context.Background(), database, log) +func New(ctx context.Context, database db.Basic, log *logrus.Logger) Server { + ts := newTokenStore(ctx, database, log) cs := NewClientStore(database) manager := manage.NewDefaultManager() @@ -138,9 +138,9 @@ func (s *s) ValidationBearerToken(r *http.Request) (oauth2.TokenInfo, error) { // // The ti parameter refers to an existing Application token that was used to make the upstream // request. This token needs to be validated and exist in database in order to create a new token. -func (s *s) GenerateUserAccessToken(ti oauth2.TokenInfo, clientSecret string, userID string) (oauth2.TokenInfo, error) { +func (s *s) GenerateUserAccessToken(ctx context.Context, ti oauth2.TokenInfo, clientSecret string, userID string) (oauth2.TokenInfo, error) { - authToken, err := s.server.Manager.GenerateAuthToken(context.Background(), oauth2.Code, &oauth2.TokenGenerateRequest{ + authToken, err := s.server.Manager.GenerateAuthToken(ctx, oauth2.Code, &oauth2.TokenGenerateRequest{ ClientID: ti.GetClientID(), ClientSecret: clientSecret, UserID: userID, @@ -155,7 +155,7 @@ func (s *s) GenerateUserAccessToken(ti oauth2.TokenInfo, clientSecret string, us } s.log.Tracef("obtained auth token: %+v", authToken) - accessToken, err := s.server.Manager.GenerateAccessToken(context.Background(), oauth2.AuthorizationCode, &oauth2.TokenGenerateRequest{ + accessToken, err := s.server.Manager.GenerateAccessToken(ctx, oauth2.AuthorizationCode, &oauth2.TokenGenerateRequest{ ClientID: authToken.GetClientID(), ClientSecret: clientSecret, RedirectURI: authToken.GetRedirectURI(), diff --git a/internal/oidc/idp.go b/internal/oidc/idp.go index 8758f6aad..9a615997c 100644 --- a/internal/oidc/idp.go +++ b/internal/oidc/idp.go @@ -56,7 +56,7 @@ type idp struct { // If the passed config contains a nil value for the OIDCConfig, or OIDCConfig.Enabled // is set to false, then nil, nil will be returned. If OIDCConfig.Enabled is true, // then the other OIDC config fields must also be set. -func NewIDP(config *config.Config, log *logrus.Logger) (IDP, error) { +func NewIDP(ctx context.Context, config *config.Config, log *logrus.Logger) (IDP, error) { // oidc isn't enabled so we don't need to do anything if config.OIDCConfig == nil || !config.OIDCConfig.Enabled { @@ -80,7 +80,7 @@ func NewIDP(config *config.Config, log *logrus.Logger) (IDP, error) { return nil, fmt.Errorf("not set: Scopes") } - provider, err := oidc.NewProvider(context.Background(), config.OIDCConfig.Issuer) + provider, err := oidc.NewProvider(ctx, config.OIDCConfig.Issuer) if err != nil { return nil, err } diff --git a/internal/processing/account/create.go b/internal/processing/account/create.go index 37c742b45..9eb618994 100644 --- a/internal/processing/account/create.go +++ b/internal/processing/account/create.go @@ -60,7 +60,7 @@ func (p *processor) Create(ctx context.Context, applicationToken oauth2.TokenInf } l.Tracef("generating a token for user %s with account %s and application %s", user.ID, user.AccountID, application.ID) - accessToken, err := p.oauthServer.GenerateUserAccessToken(applicationToken, application.ClientSecret, user.ID) + accessToken, err := p.oauthServer.GenerateUserAccessToken(ctx, applicationToken, application.ClientSecret, user.ID) if err != nil { return nil, fmt.Errorf("error creating new access token for user %s: %s", user.ID, err) } diff --git a/internal/processing/account/get.go b/internal/processing/account/get.go index 5f039127c..dd56df356 100644 --- a/internal/processing/account/get.go +++ b/internal/processing/account/get.go @@ -45,13 +45,13 @@ func (p *processor) Get(ctx context.Context, requestingAccount *gtsmodel.Account } } - var mastoAccount *apimodel.Account + var apiAccount *apimodel.Account if blocked { - mastoAccount, err = p.tc.AccountToMastoBlocked(ctx, targetAccount) + apiAccount, err = p.tc.AccountToAPIAccountBlocked(ctx, targetAccount) if err != nil { return nil, fmt.Errorf("error converting account: %s", err) } - return mastoAccount, nil + return apiAccount, nil } // last-minute check to make sure we have remote account header/avi cached @@ -63,12 +63,12 @@ func (p *processor) Get(ctx context.Context, requestingAccount *gtsmodel.Account } if requestingAccount != nil && targetAccount.ID == requestingAccount.ID { - mastoAccount, err = p.tc.AccountToMastoSensitive(ctx, targetAccount) + apiAccount, err = p.tc.AccountToAPIAccountSensitive(ctx, targetAccount) } else { - mastoAccount, err = p.tc.AccountToMastoPublic(ctx, targetAccount) + apiAccount, err = p.tc.AccountToAPIAccountPublic(ctx, targetAccount) } if err != nil { return nil, fmt.Errorf("error converting account: %s", err) } - return mastoAccount, nil + return apiAccount, nil } diff --git a/internal/processing/account/getfollowers.go b/internal/processing/account/getfollowers.go index 517467085..bcc607290 100644 --- a/internal/processing/account/getfollowers.go +++ b/internal/processing/account/getfollowers.go @@ -64,7 +64,7 @@ func (p *processor) FollowersGet(ctx context.Context, requestingAccount *gtsmode f.Account = a } - account, err := p.tc.AccountToMastoPublic(ctx, f.Account) + account, err := p.tc.AccountToAPIAccountPublic(ctx, f.Account) if err != nil { return nil, gtserror.NewErrorInternalError(err) } diff --git a/internal/processing/account/getfollowing.go b/internal/processing/account/getfollowing.go index 543213f90..d7e9d5f63 100644 --- a/internal/processing/account/getfollowing.go +++ b/internal/processing/account/getfollowing.go @@ -64,7 +64,7 @@ func (p *processor) FollowingGet(ctx context.Context, requestingAccount *gtsmode f.TargetAccount = a } - account, err := p.tc.AccountToMastoPublic(ctx, f.TargetAccount) + account, err := p.tc.AccountToAPIAccountPublic(ctx, f.TargetAccount) if err != nil { return nil, gtserror.NewErrorInternalError(err) } diff --git a/internal/processing/account/getrelationship.go b/internal/processing/account/getrelationship.go index ebfd9b479..9f23cd070 100644 --- a/internal/processing/account/getrelationship.go +++ b/internal/processing/account/getrelationship.go @@ -38,7 +38,7 @@ func (p *processor) RelationshipGet(ctx context.Context, requestingAccount *gtsm return nil, gtserror.NewErrorInternalError(fmt.Errorf("error getting relationship: %s", err)) } - r, err := p.tc.RelationshipToMasto(ctx, gtsR) + r, err := p.tc.RelationshipToAPIRelationship(ctx, gtsR) if err != nil { return nil, gtserror.NewErrorInternalError(fmt.Errorf("error converting relationship: %s", err)) } diff --git a/internal/processing/account/getstatuses.go b/internal/processing/account/getstatuses.go index dc157e43c..56b5b0eae 100644 --- a/internal/processing/account/getstatuses.go +++ b/internal/processing/account/getstatuses.go @@ -51,9 +51,9 @@ func (p *processor) StatusesGet(ctx context.Context, requestingAccount *gtsmodel continue } - apiStatus, err := p.tc.StatusToMasto(ctx, s, requestingAccount) + apiStatus, err := p.tc.StatusToAPIStatus(ctx, s, requestingAccount) if err != nil { - return nil, gtserror.NewErrorInternalError(fmt.Errorf("error converting status to masto: %s", err)) + return nil, gtserror.NewErrorInternalError(fmt.Errorf("error converting status to api: %s", err)) } apiStatuses = append(apiStatuses, *apiStatus) diff --git a/internal/processing/account/update.go b/internal/processing/account/update.go index 6dc288849..1ab25787f 100644 --- a/internal/processing/account/update.go +++ b/internal/processing/account/update.go @@ -105,7 +105,7 @@ func (p *processor) Update(ctx context.Context, account *gtsmodel.Account, form if err := validate.Privacy(*form.Source.Privacy); err != nil { return nil, err } - privacy := p.tc.MastoVisToVis(apimodel.Visibility(*form.Source.Privacy)) + privacy := p.tc.APIVisToVis(apimodel.Visibility(*form.Source.Privacy)) account.Privacy = privacy } } @@ -122,9 +122,9 @@ func (p *processor) Update(ctx context.Context, account *gtsmodel.Account, form OriginAccount: updatedAccount, } - acctSensitive, err := p.tc.AccountToMastoSensitive(ctx, updatedAccount) + acctSensitive, err := p.tc.AccountToAPIAccountSensitive(ctx, updatedAccount) if err != nil { - return nil, fmt.Errorf("could not convert account into mastosensitive account: %s", err) + return nil, fmt.Errorf("could not convert account into apisensitive account: %s", err) } return acctSensitive, nil } diff --git a/internal/processing/admin/createdomainblock.go b/internal/processing/admin/createdomainblock.go index 50df056e5..399007fe0 100644 --- a/internal/processing/admin/createdomainblock.go +++ b/internal/processing/admin/createdomainblock.go @@ -73,12 +73,12 @@ func (p *processor) DomainBlockCreate(ctx context.Context, account *gtsmodel.Acc go p.initiateDomainBlockSideEffects(ctx, account, domainBlock) // TODO: add this to a queuing system so it can retry/resume } - mastoDomainBlock, err := p.tc.DomainBlockToMasto(ctx, domainBlock, false) + apiDomainBlock, err := p.tc.DomainBlockToAPIDomainBlock(ctx, domainBlock, false) if err != nil { - return nil, gtserror.NewErrorInternalError(fmt.Errorf("DomainBlockCreate: error converting domain block to frontend/masto representation %s: %s", domain, err)) + return nil, gtserror.NewErrorInternalError(fmt.Errorf("DomainBlockCreate: error converting domain block to frontend/api representation %s: %s", domain, err)) } - return mastoDomainBlock, nil + return apiDomainBlock, nil } // initiateDomainBlockSideEffects should be called asynchronously, to process the side effects of a domain block: diff --git a/internal/processing/admin/deletedomainblock.go b/internal/processing/admin/deletedomainblock.go index d11374c78..156c156ec 100644 --- a/internal/processing/admin/deletedomainblock.go +++ b/internal/processing/admin/deletedomainblock.go @@ -42,7 +42,7 @@ func (p *processor) DomainBlockDelete(ctx context.Context, account *gtsmodel.Acc } // prepare the domain block to return - mastoDomainBlock, err := p.tc.DomainBlockToMasto(ctx, domainBlock, false) + apiDomainBlock, err := p.tc.DomainBlockToAPIDomainBlock(ctx, domainBlock, false) if err != nil { return nil, gtserror.NewErrorInternalError(err) } @@ -80,5 +80,5 @@ func (p *processor) DomainBlockDelete(ctx context.Context, account *gtsmodel.Acc return nil, gtserror.NewErrorInternalError(fmt.Errorf("database error removing suspension_origin from accounts: %s", err)) } - return mastoDomainBlock, nil + return apiDomainBlock, nil } diff --git a/internal/processing/admin/emoji.go b/internal/processing/admin/emoji.go index f56bde8e0..de36b3e50 100644 --- a/internal/processing/admin/emoji.go +++ b/internal/processing/admin/emoji.go @@ -61,14 +61,14 @@ func (p *processor) EmojiCreate(ctx context.Context, account *gtsmodel.Account, } emoji.ID = emojiID - mastoEmoji, err := p.tc.EmojiToMasto(ctx, emoji) + apiEmoji, err := p.tc.EmojiToAPIEmoji(ctx, emoji) if err != nil { - return nil, fmt.Errorf("error converting emoji to mastotype: %s", err) + return nil, fmt.Errorf("error converting emoji to apitype: %s", err) } if err := p.db.Put(ctx, emoji); err != nil { return nil, fmt.Errorf("database error while processing emoji: %s", err) } - return &mastoEmoji, nil + return &apiEmoji, nil } diff --git a/internal/processing/admin/getdomainblock.go b/internal/processing/admin/getdomainblock.go index 19bc9fe09..4cb626a4e 100644 --- a/internal/processing/admin/getdomainblock.go +++ b/internal/processing/admin/getdomainblock.go @@ -40,10 +40,10 @@ func (p *processor) DomainBlockGet(ctx context.Context, account *gtsmodel.Accoun return nil, gtserror.NewErrorNotFound(fmt.Errorf("no entry for ID %s", id)) } - mastoDomainBlock, err := p.tc.DomainBlockToMasto(ctx, domainBlock, export) + apiDomainBlock, err := p.tc.DomainBlockToAPIDomainBlock(ctx, domainBlock, export) if err != nil { return nil, gtserror.NewErrorInternalError(err) } - return mastoDomainBlock, nil + return apiDomainBlock, nil } diff --git a/internal/processing/admin/getdomainblocks.go b/internal/processing/admin/getdomainblocks.go index 0ec33cfff..13cc2ed19 100644 --- a/internal/processing/admin/getdomainblocks.go +++ b/internal/processing/admin/getdomainblocks.go @@ -37,14 +37,14 @@ func (p *processor) DomainBlocksGet(ctx context.Context, account *gtsmodel.Accou } } - mastoDomainBlocks := []*apimodel.DomainBlock{} + apiDomainBlocks := []*apimodel.DomainBlock{} for _, b := range domainBlocks { - mastoDomainBlock, err := p.tc.DomainBlockToMasto(ctx, b, export) + apiDomainBlock, err := p.tc.DomainBlockToAPIDomainBlock(ctx, b, export) if err != nil { return nil, gtserror.NewErrorInternalError(err) } - mastoDomainBlocks = append(mastoDomainBlocks, mastoDomainBlock) + apiDomainBlocks = append(apiDomainBlocks, apiDomainBlock) } - return mastoDomainBlocks, nil + return apiDomainBlocks, nil } diff --git a/internal/processing/app.go b/internal/processing/app.go index d6ded6efa..d0aba3636 100644 --- a/internal/processing/app.go +++ b/internal/processing/app.go @@ -29,7 +29,7 @@ ) func (p *processor) AppCreate(ctx context.Context, authed *oauth.Auth, form *apimodel.ApplicationCreateRequest) (*apimodel.Application, error) { - // set default 'read' for scopes if it's not set, this follows the default of the mastodon api https://docs.joinmastodon.org/methods/apps/ + // set default 'read' for scopes if it's not set var scopes string if form.Scopes == "" { scopes = "read" @@ -78,10 +78,10 @@ func (p *processor) AppCreate(ctx context.Context, authed *oauth.Auth, form *api return nil, err } - mastoApp, err := p.tc.AppToMastoSensitive(ctx, app) + apiApp, err := p.tc.AppToAPIAppSensitive(ctx, app) if err != nil { return nil, err } - return mastoApp, nil + return apiApp, nil } diff --git a/internal/processing/blocks.go b/internal/processing/blocks.go index 7c8371989..1144579a4 100644 --- a/internal/processing/blocks.go +++ b/internal/processing/blocks.go @@ -44,7 +44,7 @@ func (p *processor) BlocksGet(ctx context.Context, authed *oauth.Auth, maxID str apiAccounts := []*apimodel.Account{} for _, a := range accounts { - apiAccount, err := p.tc.AccountToMastoBlocked(ctx, a) + apiAccount, err := p.tc.AccountToAPIAccountBlocked(ctx, a) if err != nil { continue } diff --git a/internal/processing/federation.go b/internal/processing/federation.go index 1fccfa36e..1336a6e46 100644 --- a/internal/processing/federation.go +++ b/internal/processing/federation.go @@ -120,7 +120,7 @@ func (p *processor) GetFediFollowers(ctx context.Context, requestedUsername stri return nil, gtserror.NewErrorInternalError(fmt.Errorf("error parsing url %s: %s", requestedAccount.URI, err)) } - requestedFollowers, err := p.federator.FederatingDB().Followers(context.Background(), requestedAccountURI) + requestedFollowers, err := p.federator.FederatingDB().Followers(ctx, requestedAccountURI) if err != nil { return nil, gtserror.NewErrorInternalError(fmt.Errorf("error fetching followers for uri %s: %s", requestedAccountURI.String(), err)) } @@ -165,7 +165,7 @@ func (p *processor) GetFediFollowing(ctx context.Context, requestedUsername stri return nil, gtserror.NewErrorInternalError(fmt.Errorf("error parsing url %s: %s", requestedAccount.URI, err)) } - requestedFollowing, err := p.federator.FederatingDB().Following(context.Background(), requestedAccountURI) + requestedFollowing, err := p.federator.FederatingDB().Following(ctx, requestedAccountURI) if err != nil { return nil, gtserror.NewErrorInternalError(fmt.Errorf("error fetching following for uri %s: %s", requestedAccountURI.String(), err)) } diff --git a/internal/processing/followrequest.go b/internal/processing/followrequest.go index b313e42f8..74bffd693 100644 --- a/internal/processing/followrequest.go +++ b/internal/processing/followrequest.go @@ -47,11 +47,11 @@ func (p *processor) FollowRequestsGet(ctx context.Context, auth *oauth.Auth) ([] fr.Account = frAcct } - mastoAcct, err := p.tc.AccountToMastoPublic(ctx, fr.Account) + apiAcct, err := p.tc.AccountToAPIAccountPublic(ctx, fr.Account) if err != nil { return nil, gtserror.NewErrorInternalError(err) } - accts = append(accts, *mastoAcct) + accts = append(accts, *apiAcct) } return accts, nil } @@ -91,7 +91,7 @@ func (p *processor) FollowRequestAccept(ctx context.Context, auth *oauth.Auth, a return nil, gtserror.NewErrorInternalError(err) } - r, err := p.tc.RelationshipToMasto(ctx, gtsR) + r, err := p.tc.RelationshipToAPIRelationship(ctx, gtsR) if err != nil { return nil, gtserror.NewErrorInternalError(err) } diff --git a/internal/processing/fromcommon.go b/internal/processing/fromcommon.go index e14c36fd4..88f9994d4 100644 --- a/internal/processing/fromcommon.go +++ b/internal/processing/fromcommon.go @@ -96,12 +96,12 @@ func (p *processor) notifyStatus(ctx context.Context, status *gtsmodel.Status) e } // now stream the notification to the user - mastoNotif, err := p.tc.NotificationToMasto(ctx, notif) + apiNotif, err := p.tc.NotificationToAPINotification(ctx, notif) if err != nil { - return fmt.Errorf("notifyStatus: error converting notification to masto representation: %s", err) + return fmt.Errorf("notifyStatus: error converting notification to api representation: %s", err) } - if err := p.streamingProcessor.StreamNotificationToAccount(mastoNotif, m.TargetAccount); err != nil { + if err := p.streamingProcessor.StreamNotificationToAccount(apiNotif, m.TargetAccount); err != nil { return fmt.Errorf("notifyStatus: error streaming notification to account: %s", err) } } @@ -143,12 +143,12 @@ func (p *processor) notifyFollowRequest(ctx context.Context, followRequest *gtsm } // now stream the notification to the user - mastoNotif, err := p.tc.NotificationToMasto(ctx, notif) + apiNotif, err := p.tc.NotificationToAPINotification(ctx, notif) if err != nil { - return fmt.Errorf("notifyStatus: error converting notification to masto representation: %s", err) + return fmt.Errorf("notifyStatus: error converting notification to api representation: %s", err) } - if err := p.streamingProcessor.StreamNotificationToAccount(mastoNotif, targetAccount); err != nil { + if err := p.streamingProcessor.StreamNotificationToAccount(apiNotif, targetAccount); err != nil { return fmt.Errorf("notifyStatus: error streaming notification to account: %s", err) } @@ -189,12 +189,12 @@ func (p *processor) notifyFollow(ctx context.Context, follow *gtsmodel.Follow, t } // now stream the notification to the user - mastoNotif, err := p.tc.NotificationToMasto(ctx, notif) + apiNotif, err := p.tc.NotificationToAPINotification(ctx, notif) if err != nil { - return fmt.Errorf("notifyStatus: error converting notification to masto representation: %s", err) + return fmt.Errorf("notifyStatus: error converting notification to api representation: %s", err) } - if err := p.streamingProcessor.StreamNotificationToAccount(mastoNotif, targetAccount); err != nil { + if err := p.streamingProcessor.StreamNotificationToAccount(apiNotif, targetAccount); err != nil { return fmt.Errorf("notifyStatus: error streaming notification to account: %s", err) } @@ -237,12 +237,12 @@ func (p *processor) notifyFave(ctx context.Context, fave *gtsmodel.StatusFave) e } // now stream the notification to the user - mastoNotif, err := p.tc.NotificationToMasto(ctx, notif) + apiNotif, err := p.tc.NotificationToAPINotification(ctx, notif) if err != nil { - return fmt.Errorf("notifyStatus: error converting notification to masto representation: %s", err) + return fmt.Errorf("notifyStatus: error converting notification to api representation: %s", err) } - if err := p.streamingProcessor.StreamNotificationToAccount(mastoNotif, targetAccount); err != nil { + if err := p.streamingProcessor.StreamNotificationToAccount(apiNotif, targetAccount); err != nil { return fmt.Errorf("notifyStatus: error streaming notification to account: %s", err) } @@ -316,12 +316,12 @@ func (p *processor) notifyAnnounce(ctx context.Context, status *gtsmodel.Status) } // now stream the notification to the user - mastoNotif, err := p.tc.NotificationToMasto(ctx, notif) + apiNotif, err := p.tc.NotificationToAPINotification(ctx, notif) if err != nil { - return fmt.Errorf("notifyStatus: error converting notification to masto representation: %s", err) + return fmt.Errorf("notifyStatus: error converting notification to api representation: %s", err) } - if err := p.streamingProcessor.StreamNotificationToAccount(mastoNotif, status.BoostOfAccount); err != nil { + if err := p.streamingProcessor.StreamNotificationToAccount(apiNotif, status.BoostOfAccount); err != nil { return fmt.Errorf("notifyStatus: error streaming notification to account: %s", err) } @@ -414,21 +414,21 @@ func (p *processor) timelineStatusForAccount(ctx context.Context, status *gtsmod // the status was inserted to stream it to the user if inserted { - mastoStatus, err := p.tc.StatusToMasto(ctx, status, timelineAccount) + apiStatus, err := p.tc.StatusToAPIStatus(ctx, status, timelineAccount) if err != nil { errors <- fmt.Errorf("timelineStatusForAccount: error converting status %s to frontend representation: %s", status.ID, err) } else { - if err := p.streamingProcessor.StreamStatusToAccount(mastoStatus, timelineAccount); err != nil { + if err := p.streamingProcessor.StreamUpdateToAccount(apiStatus, timelineAccount); err != nil { errors <- fmt.Errorf("timelineStatusForAccount: error streaming status %s: %s", status.ID, err) } } } - mastoStatus, err := p.tc.StatusToMasto(ctx, status, timelineAccount) + apiStatus, err := p.tc.StatusToAPIStatus(ctx, status, timelineAccount) if err != nil { errors <- fmt.Errorf("timelineStatusForAccount: error converting status %s to frontend representation: %s", status.ID, err) } else { - if err := p.streamingProcessor.StreamStatusToAccount(mastoStatus, timelineAccount); err != nil { + if err := p.streamingProcessor.StreamUpdateToAccount(apiStatus, timelineAccount); err != nil { errors <- fmt.Errorf("timelineStatusForAccount: error streaming status %s: %s", status.ID, err) } } diff --git a/internal/processing/instance.go b/internal/processing/instance.go index 41139c491..75d17d13a 100644 --- a/internal/processing/instance.go +++ b/internal/processing/instance.go @@ -36,7 +36,7 @@ func (p *processor) InstanceGet(ctx context.Context, domain string) (*apimodel.I return nil, gtserror.NewErrorInternalError(fmt.Errorf("db error fetching instance %s: %s", p.config.Host, err)) } - ai, err := p.tc.InstanceToMasto(ctx, i) + ai, err := p.tc.InstanceToAPIInstance(ctx, i) if err != nil { return nil, gtserror.NewErrorInternalError(fmt.Errorf("error converting instance to api representation: %s", err)) } @@ -151,7 +151,7 @@ func (p *processor) InstancePatch(ctx context.Context, form *apimodel.InstanceSe return nil, gtserror.NewErrorInternalError(fmt.Errorf("db error updating instance %s: %s", p.config.Host, err)) } - ai, err := p.tc.InstanceToMasto(ctx, i) + ai, err := p.tc.InstanceToAPIInstance(ctx, i) if err != nil { return nil, gtserror.NewErrorInternalError(fmt.Errorf("error converting instance to api representation: %s", err)) } diff --git a/internal/processing/media/create.go b/internal/processing/media/create.go index 43162f3f6..0783bfae8 100644 --- a/internal/processing/media/create.go +++ b/internal/processing/media/create.go @@ -73,7 +73,7 @@ func (p *processor) Create(ctx context.Context, account *gtsmodel.Account, form // prepare the frontend representation now -- if there are any errors here at least we can bail without // having already put something in the database and then having to clean it up again (eugh) - mastoAttachment, err := p.tc.AttachmentToMasto(ctx, attachment) + apiAttachment, err := p.tc.AttachmentToAPIAttachment(ctx, attachment) if err != nil { return nil, fmt.Errorf("error parsing media attachment to frontend type: %s", err) } @@ -83,5 +83,5 @@ func (p *processor) Create(ctx context.Context, account *gtsmodel.Account, form return nil, fmt.Errorf("error storing media attachment in db: %s", err) } - return &mastoAttachment, nil + return &apiAttachment, nil } diff --git a/internal/processing/media/getmedia.go b/internal/processing/media/getmedia.go index 91608e90d..763aaf8f6 100644 --- a/internal/processing/media/getmedia.go +++ b/internal/processing/media/getmedia.go @@ -43,7 +43,7 @@ func (p *processor) GetMedia(ctx context.Context, account *gtsmodel.Account, med return nil, gtserror.NewErrorNotFound(errors.New("attachment not owned by requesting account")) } - a, err := p.tc.AttachmentToMasto(ctx, attachment) + a, err := p.tc.AttachmentToAPIAttachment(ctx, attachment) if err != nil { return nil, gtserror.NewErrorNotFound(fmt.Errorf("error converting attachment: %s", err)) } diff --git a/internal/processing/media/update.go b/internal/processing/media/update.go index e6c78563d..b3455bc91 100644 --- a/internal/processing/media/update.go +++ b/internal/processing/media/update.go @@ -63,7 +63,7 @@ func (p *processor) Update(ctx context.Context, account *gtsmodel.Account, media } } - a, err := p.tc.AttachmentToMasto(ctx, attachment) + a, err := p.tc.AttachmentToAPIAttachment(ctx, attachment) if err != nil { return nil, gtserror.NewErrorNotFound(fmt.Errorf("error converting attachment: %s", err)) } diff --git a/internal/processing/notification.go b/internal/processing/notification.go index f91d2f2cd..27280a973 100644 --- a/internal/processing/notification.go +++ b/internal/processing/notification.go @@ -34,15 +34,15 @@ func (p *processor) NotificationsGet(ctx context.Context, authed *oauth.Auth, li return nil, gtserror.NewErrorInternalError(err) } - mastoNotifs := []*apimodel.Notification{} + apiNotifs := []*apimodel.Notification{} for _, n := range notifs { - mastoNotif, err := p.tc.NotificationToMasto(ctx, n) + apiNotif, err := p.tc.NotificationToAPINotification(ctx, n) if err != nil { - l.Debugf("got an error converting a notification to masto, will skip it: %s", err) + l.Debugf("got an error converting a notification to api, will skip it: %s", err) continue } - mastoNotifs = append(mastoNotifs, mastoNotif) + apiNotifs = append(apiNotifs, apiNotif) } - return mastoNotifs, nil + return apiNotifs, nil } diff --git a/internal/processing/processor.go b/internal/processing/processor.go index 6f36f6d21..c643738c2 100644 --- a/internal/processing/processor.go +++ b/internal/processing/processor.go @@ -256,7 +256,7 @@ func NewProcessor(config *config.Config, tc typeutils.TypeConverter, federator f fromFederator := make(chan messages.FromFederator, 1000) statusProcessor := status.New(db, tc, config, fromClientAPI, log) - streamingProcessor := streaming.New(db, tc, oauthServer, config, log) + streamingProcessor := streaming.New(db, oauthServer, log) accountProcessor := account.New(db, tc, mediaHandler, oauthServer, fromClientAPI, federator, config, log) adminProcessor := admin.New(db, tc, mediaHandler, fromClientAPI, config, log) mediaProcessor := mediaProcessor.New(db, tc, mediaHandler, storage, config, log) diff --git a/internal/processing/search.go b/internal/processing/search.go index 2fb1f6062..c5bfd722b 100644 --- a/internal/processing/search.go +++ b/internal/processing/search.go @@ -93,8 +93,8 @@ func (p *processor) SearchGet(ctx context.Context, authed *oauth.Auth, searchQue // make sure there's no block in either direction between the account and the requester if blocked, err := p.db.IsBlocked(ctx, authed.Account.ID, foundAccount.ID, true); err == nil && !blocked { // all good, convert it and add it to the results - if acctMasto, err := p.tc.AccountToMastoPublic(ctx, foundAccount); err == nil && acctMasto != nil { - results.Accounts = append(results.Accounts, *acctMasto) + if apiAcct, err := p.tc.AccountToAPIAccountPublic(ctx, foundAccount); err == nil && apiAcct != nil { + results.Accounts = append(results.Accounts, *apiAcct) } } } @@ -104,12 +104,12 @@ func (p *processor) SearchGet(ctx context.Context, authed *oauth.Auth, searchQue continue } - statusMasto, err := p.tc.StatusToMasto(ctx, foundStatus, authed.Account) + apiStatus, err := p.tc.StatusToAPIStatus(ctx, foundStatus, authed.Account) if err != nil { continue } - results.Statuses = append(results.Statuses, *statusMasto) + results.Statuses = append(results.Statuses, *apiStatus) } return results, nil diff --git a/internal/processing/status/boost.go b/internal/processing/status/boost.go index 4276ca9fa..2ee6acd55 100644 --- a/internal/processing/status/boost.go +++ b/internal/processing/status/boost.go @@ -74,10 +74,10 @@ func (p *processor) Boost(ctx context.Context, requestingAccount *gtsmodel.Accou } // return the frontend representation of the new status to the submitter - mastoStatus, err := p.tc.StatusToMasto(ctx, boostWrapperStatus, requestingAccount) + apiStatus, err := p.tc.StatusToAPIStatus(ctx, boostWrapperStatus, requestingAccount) if err != nil { return nil, gtserror.NewErrorInternalError(fmt.Errorf("error converting status %s to frontend representation: %s", targetStatus.ID, err)) } - return mastoStatus, nil + return apiStatus, nil } diff --git a/internal/processing/status/boostedby.go b/internal/processing/status/boostedby.go index 46f41039f..00256d2dd 100644 --- a/internal/processing/status/boostedby.go +++ b/internal/processing/status/boostedby.go @@ -64,15 +64,15 @@ func (p *processor) BoostedBy(ctx context.Context, requestingAccount *gtsmodel.A // TODO: filter other things here? suspended? muted? silenced? - // now we can return the masto representation of those accounts - mastoAccounts := []*apimodel.Account{} + // now we can return the api representation of those accounts + apiAccounts := []*apimodel.Account{} for _, acc := range filteredAccounts { - mastoAccount, err := p.tc.AccountToMastoPublic(ctx, acc) + apiAccount, err := p.tc.AccountToAPIAccountPublic(ctx, acc) if err != nil { return nil, gtserror.NewErrorNotFound(fmt.Errorf("StatusFavedBy: error converting account to api model: %s", err)) } - mastoAccounts = append(mastoAccounts, mastoAccount) + apiAccounts = append(apiAccounts, apiAccount) } - return mastoAccounts, nil + return apiAccounts, nil } diff --git a/internal/processing/status/context.go b/internal/processing/status/context.go index 3e8e93d09..75c756cfe 100644 --- a/internal/processing/status/context.go +++ b/internal/processing/status/context.go @@ -58,9 +58,9 @@ func (p *processor) Context(ctx context.Context, requestingAccount *gtsmodel.Acc for _, status := range parents { if v, err := p.filter.StatusVisible(ctx, status, requestingAccount); err == nil && v { - mastoStatus, err := p.tc.StatusToMasto(ctx, status, requestingAccount) + apiStatus, err := p.tc.StatusToAPIStatus(ctx, status, requestingAccount) if err == nil { - context.Ancestors = append(context.Ancestors, *mastoStatus) + context.Ancestors = append(context.Ancestors, *apiStatus) } } } @@ -76,9 +76,9 @@ func (p *processor) Context(ctx context.Context, requestingAccount *gtsmodel.Acc for _, status := range children { if v, err := p.filter.StatusVisible(ctx, status, requestingAccount); err == nil && v { - mastoStatus, err := p.tc.StatusToMasto(ctx, status, requestingAccount) + apiStatus, err := p.tc.StatusToAPIStatus(ctx, status, requestingAccount) if err == nil { - context.Descendants = append(context.Descendants, *mastoStatus) + context.Descendants = append(context.Descendants, *apiStatus) } } } diff --git a/internal/processing/status/create.go b/internal/processing/status/create.go index a87dbc7fe..655be5b17 100644 --- a/internal/processing/status/create.go +++ b/internal/processing/status/create.go @@ -105,10 +105,10 @@ func (p *processor) Create(ctx context.Context, account *gtsmodel.Account, appli } // return the frontend representation of the new status to the submitter - mastoStatus, err := p.tc.StatusToMasto(ctx, newStatus, account) + apiStatus, err := p.tc.StatusToAPIStatus(ctx, newStatus, account) if err != nil { return nil, gtserror.NewErrorInternalError(fmt.Errorf("error converting status %s to frontend representation: %s", newStatus.ID, err)) } - return mastoStatus, nil + return apiStatus, nil } diff --git a/internal/processing/status/delete.go b/internal/processing/status/delete.go index dfb2c3626..822e1559c 100644 --- a/internal/processing/status/delete.go +++ b/internal/processing/status/delete.go @@ -43,7 +43,7 @@ func (p *processor) Delete(ctx context.Context, requestingAccount *gtsmodel.Acco return nil, gtserror.NewErrorForbidden(errors.New("status doesn't belong to requesting account")) } - mastoStatus, err := p.tc.StatusToMasto(ctx, targetStatus, requestingAccount) + apiStatus, err := p.tc.StatusToAPIStatus(ctx, targetStatus, requestingAccount) if err != nil { return nil, gtserror.NewErrorInternalError(fmt.Errorf("error converting status %s to frontend representation: %s", targetStatus.ID, err)) } @@ -61,5 +61,5 @@ func (p *processor) Delete(ctx context.Context, requestingAccount *gtsmodel.Acco TargetAccount: requestingAccount, } - return mastoStatus, nil + return apiStatus, nil } diff --git a/internal/processing/status/fave.go b/internal/processing/status/fave.go index f3f10c43c..571e0715c 100644 --- a/internal/processing/status/fave.go +++ b/internal/processing/status/fave.go @@ -93,11 +93,11 @@ func (p *processor) Fave(ctx context.Context, requestingAccount *gtsmodel.Accoun } } - // return the mastodon representation of the target status - mastoStatus, err := p.tc.StatusToMasto(ctx, targetStatus, requestingAccount) + // return the apidon representation of the target status + apiStatus, err := p.tc.StatusToAPIStatus(ctx, targetStatus, requestingAccount) if err != nil { return nil, gtserror.NewErrorInternalError(fmt.Errorf("error converting status %s to frontend representation: %s", targetStatus.ID, err)) } - return mastoStatus, nil + return apiStatus, nil } diff --git a/internal/processing/status/favedby.go b/internal/processing/status/favedby.go index 227fb669d..e8681e379 100644 --- a/internal/processing/status/favedby.go +++ b/internal/processing/status/favedby.go @@ -62,15 +62,15 @@ func (p *processor) FavedBy(ctx context.Context, requestingAccount *gtsmodel.Acc } } - // now we can return the masto representation of those accounts - mastoAccounts := []*apimodel.Account{} + // now we can return the api representation of those accounts + apiAccounts := []*apimodel.Account{} for _, acc := range filteredAccounts { - mastoAccount, err := p.tc.AccountToMastoPublic(ctx, acc) + apiAccount, err := p.tc.AccountToAPIAccountPublic(ctx, acc) if err != nil { return nil, gtserror.NewErrorInternalError(fmt.Errorf("error converting status %s to frontend representation: %s", targetStatus.ID, err)) } - mastoAccounts = append(mastoAccounts, mastoAccount) + apiAccounts = append(apiAccounts, apiAccount) } - return mastoAccounts, nil + return apiAccounts, nil } diff --git a/internal/processing/status/get.go b/internal/processing/status/get.go index 258210faf..fb9a3ea77 100644 --- a/internal/processing/status/get.go +++ b/internal/processing/status/get.go @@ -45,10 +45,10 @@ func (p *processor) Get(ctx context.Context, requestingAccount *gtsmodel.Account return nil, gtserror.NewErrorNotFound(errors.New("status is not visible")) } - mastoStatus, err := p.tc.StatusToMasto(ctx, targetStatus, requestingAccount) + apiStatus, err := p.tc.StatusToAPIStatus(ctx, targetStatus, requestingAccount) if err != nil { return nil, gtserror.NewErrorInternalError(fmt.Errorf("error converting status %s to frontend representation: %s", targetStatus.ID, err)) } - return mastoStatus, nil + return apiStatus, nil } diff --git a/internal/processing/status/unboost.go b/internal/processing/status/unboost.go index 13c24d638..94e74f8e0 100644 --- a/internal/processing/status/unboost.go +++ b/internal/processing/status/unboost.go @@ -100,10 +100,10 @@ func (p *processor) Unboost(ctx context.Context, requestingAccount *gtsmodel.Acc } } - mastoStatus, err := p.tc.StatusToMasto(ctx, targetStatus, requestingAccount) + apiStatus, err := p.tc.StatusToAPIStatus(ctx, targetStatus, requestingAccount) if err != nil { return nil, gtserror.NewErrorInternalError(fmt.Errorf("error converting status %s to frontend representation: %s", targetStatus.ID, err)) } - return mastoStatus, nil + return apiStatus, nil } diff --git a/internal/processing/status/unfave.go b/internal/processing/status/unfave.go index 27ce9b156..a8ddea39d 100644 --- a/internal/processing/status/unfave.go +++ b/internal/processing/status/unfave.go @@ -82,10 +82,10 @@ func (p *processor) Unfave(ctx context.Context, requestingAccount *gtsmodel.Acco } } - mastoStatus, err := p.tc.StatusToMasto(ctx, targetStatus, requestingAccount) + apiStatus, err := p.tc.StatusToAPIStatus(ctx, targetStatus, requestingAccount) if err != nil { return nil, gtserror.NewErrorInternalError(fmt.Errorf("error converting status %s to frontend representation: %s", targetStatus.ID, err)) } - return mastoStatus, nil + return apiStatus, nil } diff --git a/internal/processing/status/util.go b/internal/processing/status/util.go index edbb9a31a..e655632db 100644 --- a/internal/processing/status/util.go +++ b/internal/processing/status/util.go @@ -42,7 +42,7 @@ func (p *processor) ProcessVisibility(ctx context.Context, form *apimodel.Advanc // If visibility isn't set on the form, then just take the account default. // If that's also not set, take the default for the whole instance. if form.Visibility != "" { - vis = p.tc.MastoVisToVis(form.Visibility) + vis = p.tc.APIVisToVis(form.Visibility) } else if accountDefaultVis != "" { vis = accountDefaultVis } else { diff --git a/internal/processing/streaming/authorize.go b/internal/processing/streaming/authorize.go index f938a0c0c..1a5724f51 100644 --- a/internal/processing/streaming/authorize.go +++ b/internal/processing/streaming/authorize.go @@ -1,3 +1,21 @@ +/* + GoToSocial + Copyright (C) 2021 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 . +*/ + package streaming import ( @@ -8,7 +26,7 @@ ) func (p *processor) AuthorizeStreamingRequest(ctx context.Context, accessToken string) (*gtsmodel.Account, error) { - ti, err := p.oauthServer.LoadAccessToken(context.Background(), accessToken) + ti, err := p.oauthServer.LoadAccessToken(ctx, accessToken) if err != nil { return nil, fmt.Errorf("AuthorizeStreamingRequest: error loading access token: %s", err) } diff --git a/internal/processing/streaming/authorize_test.go b/internal/processing/streaming/authorize_test.go new file mode 100644 index 000000000..f52396250 --- /dev/null +++ b/internal/processing/streaming/authorize_test.go @@ -0,0 +1,48 @@ +/* + GoToSocial + Copyright (C) 2021 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 . +*/ + +package streaming_test + +import ( + "context" + "testing" + + "github.com/stretchr/testify/suite" +) + +type AuthorizeTestSuite struct { + StreamingTestSuite +} + +func (suite *AuthorizeTestSuite) TestAuthorize() { + account1, err := suite.streamingProcessor.AuthorizeStreamingRequest(context.Background(), suite.testTokens["local_account_1"].Access) + suite.NoError(err) + suite.Equal(suite.testAccounts["local_account_1"].ID, account1.ID) + + account2, err := suite.streamingProcessor.AuthorizeStreamingRequest(context.Background(), suite.testTokens["local_account_2"].Access) + suite.NoError(err) + suite.Equal(suite.testAccounts["local_account_2"].ID, account2.ID) + + noAccount, err := suite.streamingProcessor.AuthorizeStreamingRequest(context.Background(), "aaaaaaaaaaaaaaaaaaaaa!!") + suite.EqualError(err, "AuthorizeStreamingRequest: error loading access token: no entries") + suite.Nil(noAccount) +} + +func TestAuthorizeTestSuite(t *testing.T) { + suite.Run(t, &AuthorizeTestSuite{}) +} diff --git a/internal/api/model/identityproof.go b/internal/processing/streaming/notification.go similarity index 52% rename from internal/api/model/identityproof.go rename to internal/processing/streaming/notification.go index 400835fca..870490be4 100644 --- a/internal/api/model/identityproof.go +++ b/internal/processing/streaming/notification.go @@ -16,18 +16,22 @@ along with this program. If not, see . */ -package model +package streaming -// IdentityProof represents a proof from an external identity provider. See https://docs.joinmastodon.org/entities/identityproof/ -type IdentityProof struct { - // The name of the identity provider. - Provider string `json:"provider"` - // The account owner's username on the identity provider's service. - ProviderUsername string `json:"provider_username"` - // The account owner's profile URL on the identity provider. - ProfileURL string `json:"profile_url"` - // A link to a statement of identity proof, hosted by the identity provider. - ProofURL string `json:"proof_url"` - // When the identity proof was last updated. - UpdatedAt string `json:"updated_at"` +import ( + "encoding/json" + "fmt" + + apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" + "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" + "github.com/superseriousbusiness/gotosocial/internal/stream" +) + +func (p *processor) StreamNotificationToAccount(n *apimodel.Notification, account *gtsmodel.Account) error { + bytes, err := json.Marshal(n) + if err != nil { + return fmt.Errorf("error marshalling notification to json: %s", err) + } + + return p.streamToAccount(string(bytes), stream.EventTypeNotification, account.ID) } diff --git a/internal/processing/streaming/notification_test.go b/internal/processing/streaming/notification_test.go new file mode 100644 index 000000000..fa77a8f92 --- /dev/null +++ b/internal/processing/streaming/notification_test.go @@ -0,0 +1,60 @@ +/* + GoToSocial + Copyright (C) 2021 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 . +*/ + +package streaming_test + +import ( + "context" + "testing" + + "github.com/stretchr/testify/suite" + apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" + "github.com/superseriousbusiness/gotosocial/testrig" +) + +type NotificationTestSuite struct { + StreamingTestSuite +} + +func (suite *NotificationTestSuite) TestStreamNotification() { + account := suite.testAccounts["local_account_1"] + + openStream, errWithCode := suite.streamingProcessor.OpenStreamForAccount(context.Background(), account, "user") + suite.NoError(errWithCode) + + followAccount := suite.testAccounts["remote_account_1"] + followAccountAPIModel, err := testrig.NewTestTypeConverter(suite.db).AccountToAPIAccountPublic(context.Background(), followAccount) + suite.NoError(err) + + notification := &apimodel.Notification{ + ID: "01FH57SJCMDWQGEAJ0X08CE3WV", + Type: "follow", + CreatedAt: "2021-10-04T10:52:36+02:00", + Account: followAccountAPIModel, + } + + err = suite.streamingProcessor.StreamNotificationToAccount(notification, account) + suite.NoError(err) + + msg := <-openStream.Messages + suite.Equal(`{"id":"01FH57SJCMDWQGEAJ0X08CE3WV","type":"follow","created_at":"2021-10-04T10:52:36+02:00","account":{"id":"01F8MH5ZK5VRH73AKHQM6Y9VNX","username":"foss_satan","acct":"foss_satan@fossbros-anonymous.io","display_name":"big gerald","locked":false,"bot":false,"created_at":"2021-09-26T12:52:36+02:00","note":"i post about like, i dunno, stuff, or whatever!!!!","url":"http://fossbros-anonymous.io/@foss_satan","avatar":"","avatar_static":"","header":"","header_static":"","followers_count":0,"following_count":0,"statuses_count":0,"last_status_at":"","emojis":[],"fields":[]}}`, msg.Payload) +} + +func TestNotificationTestSuite(t *testing.T) { + suite.Run(t, &NotificationTestSuite{}) +} diff --git a/internal/processing/streaming/openstream.go b/internal/processing/streaming/openstream.go index d4e4eef9f..74b6486f5 100644 --- a/internal/processing/streaming/openstream.go +++ b/internal/processing/streaming/openstream.go @@ -1,3 +1,21 @@ +/* + GoToSocial + Copyright (C) 2021 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 . +*/ + package streaming import ( diff --git a/internal/api/model/error.go b/internal/processing/streaming/openstream_test.go similarity index 61% rename from internal/api/model/error.go rename to internal/processing/streaming/openstream_test.go index f145d69f2..6c4134997 100644 --- a/internal/api/model/error.go +++ b/internal/processing/streaming/openstream_test.go @@ -16,17 +16,26 @@ along with this program. If not, see . */ -package model +package streaming_test -// Error represents an error message returned from the API. See https://docs.joinmastodon.org/entities/error/ -type Error struct { - // REQUIRED +import ( + "context" + "testing" - // The error message. - Error string `json:"error"` + "github.com/stretchr/testify/suite" +) - // OPTIONAL - - // A longer description of the error, mainly provided with the OAuth API. - ErrorDescription string `json:"error_description"` +type OpenStreamTestSuite struct { + StreamingTestSuite +} + +func (suite *OpenStreamTestSuite) TestOpenStream() { + account := suite.testAccounts["local_account_1"] + + _, errWithCode := suite.streamingProcessor.OpenStreamForAccount(context.Background(), account, "user") + suite.NoError(errWithCode) +} + +func TestOpenStreamTestSuite(t *testing.T) { + suite.Run(t, &OpenStreamTestSuite{}) } diff --git a/internal/processing/streaming/streamdelete.go b/internal/processing/streaming/streamdelete.go index cd541bc57..8332c37dc 100644 --- a/internal/processing/streaming/streamdelete.go +++ b/internal/processing/streaming/streamdelete.go @@ -1,3 +1,21 @@ +/* + GoToSocial + Copyright (C) 2021 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 . +*/ + package streaming import ( @@ -10,39 +28,20 @@ func (p *processor) StreamDelete(statusID string) error { errs := []string{} - // we want to range through ALL streams for ALL accounts here to make sure it's very clear to everyone that the status has been deleted - p.streamMap.Range(func(k interface{}, v interface{}) bool { - // the key of this map should be an accountID (string) - accountID, ok := k.(string) - if !ok { - errs = append(errs, "key in streamMap was not a string!") - return false - } - - // the value of the map should be a buncha streams - streamsForAccount, ok := v.(*stream.StreamsForAccount) - if !ok { - errs = append(errs, fmt.Sprintf("stream map error for account stream %s", accountID)) - } - - // lock the streams while we work on them - streamsForAccount.Lock() - defer streamsForAccount.Unlock() - for _, s := range streamsForAccount.Streams { - // lock each individual stream as we work on it - s.Lock() - defer s.Unlock() - if s.Connected { - s.Messages <- &stream.Message{ - Stream: []string{s.Type}, - Event: "delete", - Payload: statusID, - } - } - } + // get all account IDs with open streams + accountIDs := []string{} + p.streamMap.Range(func(k interface{}, _ interface{}) bool { + accountIDs = append(accountIDs, k.(string)) return true }) + // stream the delete to every account + for _, accountID := range accountIDs { + if err := p.streamToAccount(statusID, stream.EventTypeDelete, accountID); err != nil { + errs = append(errs, err.Error()) + } + } + if len(errs) != 0 { return fmt.Errorf("one or more errors streaming status delete: %s", strings.Join(errs, ";")) } diff --git a/internal/processing/streaming/streaming.go b/internal/processing/streaming/streaming.go index 610d4a9d2..abce30cd1 100644 --- a/internal/processing/streaming/streaming.go +++ b/internal/processing/streaming/streaming.go @@ -1,3 +1,21 @@ +/* + GoToSocial + Copyright (C) 2021 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 . +*/ + package streaming import ( @@ -6,14 +24,11 @@ "github.com/sirupsen/logrus" apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" - "github.com/superseriousbusiness/gotosocial/internal/config" "github.com/superseriousbusiness/gotosocial/internal/db" "github.com/superseriousbusiness/gotosocial/internal/gtserror" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" "github.com/superseriousbusiness/gotosocial/internal/oauth" "github.com/superseriousbusiness/gotosocial/internal/stream" - "github.com/superseriousbusiness/gotosocial/internal/typeutils" - "github.com/superseriousbusiness/gotosocial/internal/visibility" ) // Processor wraps a bunch of functions for processing streaming. @@ -22,8 +37,8 @@ type Processor interface { AuthorizeStreamingRequest(ctx context.Context, accessToken string) (*gtsmodel.Account, error) // OpenStreamForAccount returns a new Stream for the given account, which will contain a channel for passing messages back to the caller. OpenStreamForAccount(ctx context.Context, account *gtsmodel.Account, streamType string) (*stream.Stream, gtserror.WithCode) - // StreamStatusToAccount streams the given status to any open, appropriate streams belonging to the given account. - StreamStatusToAccount(s *apimodel.Status, account *gtsmodel.Account) error + // StreamUpdateToAccount streams the given update to any open, appropriate streams belonging to the given account. + StreamUpdateToAccount(s *apimodel.Status, account *gtsmodel.Account) error // StreamNotificationToAccount streams the given notification to any open, appropriate streams belonging to the given account. StreamNotificationToAccount(n *apimodel.Notification, account *gtsmodel.Account) error // StreamDelete streams the delete of the given statusID to *ALL* open streams. @@ -31,22 +46,16 @@ type Processor interface { } type processor struct { - tc typeutils.TypeConverter - config *config.Config db db.DB - filter visibility.Filter log *logrus.Logger oauthServer oauth.Server streamMap *sync.Map } // New returns a new status processor. -func New(db db.DB, tc typeutils.TypeConverter, oauthServer oauth.Server, config *config.Config, log *logrus.Logger) Processor { +func New(db db.DB, oauthServer oauth.Server, log *logrus.Logger) Processor { return &processor{ - tc: tc, - config: config, db: db, - filter: visibility.NewFilter(db, log), log: log, oauthServer: oauthServer, streamMap: &sync.Map{}, diff --git a/internal/processing/streaming/streaming_test.go b/internal/processing/streaming/streaming_test.go new file mode 100644 index 000000000..acc090b06 --- /dev/null +++ b/internal/processing/streaming/streaming_test.go @@ -0,0 +1,55 @@ +/* + GoToSocial + Copyright (C) 2021 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 . +*/ + +package streaming_test + +import ( + "github.com/sirupsen/logrus" + "github.com/stretchr/testify/suite" + "github.com/superseriousbusiness/gotosocial/internal/db" + "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" + "github.com/superseriousbusiness/gotosocial/internal/oauth" + "github.com/superseriousbusiness/gotosocial/internal/processing/streaming" + "github.com/superseriousbusiness/gotosocial/testrig" +) + +type StreamingTestSuite struct { + suite.Suite + testAccounts map[string]*gtsmodel.Account + testTokens map[string]*gtsmodel.Token + db db.DB + oauthServer oauth.Server + log *logrus.Logger + + streamingProcessor streaming.Processor +} + +func (suite *StreamingTestSuite) SetupTest() { + suite.testAccounts = testrig.NewTestAccounts() + suite.testTokens = testrig.NewTestTokens() + suite.db = testrig.NewTestDB() + suite.oauthServer = testrig.NewTestOauthServer(suite.db) + suite.log = testrig.NewTestLog() + suite.streamingProcessor = streaming.New(suite.db, suite.oauthServer, suite.log) + + testrig.StandardDBSetup(suite.db, suite.testAccounts) +} + +func (suite *StreamingTestSuite) TearDownTest() { + testrig.StandardDBTeardown(suite.db) +} diff --git a/internal/processing/streaming/streamnotification.go b/internal/processing/streaming/streamnotification.go deleted file mode 100644 index d8460874f..000000000 --- a/internal/processing/streaming/streamnotification.go +++ /dev/null @@ -1,51 +0,0 @@ -package streaming - -import ( - "encoding/json" - "errors" - "fmt" - - "github.com/sirupsen/logrus" - apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" - "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" - "github.com/superseriousbusiness/gotosocial/internal/stream" -) - -func (p *processor) StreamNotificationToAccount(n *apimodel.Notification, account *gtsmodel.Account) error { - l := p.log.WithFields(logrus.Fields{ - "func": "StreamNotificationToAccount", - "account": account.ID, - }) - v, ok := p.streamMap.Load(account.ID) - if !ok { - // no open connections so nothing to stream - return nil - } - - streamsForAccount, ok := v.(*stream.StreamsForAccount) - if !ok { - return errors.New("stream map error") - } - - notificationBytes, err := json.Marshal(n) - if err != nil { - return fmt.Errorf("error marshalling notification to json: %s", err) - } - - streamsForAccount.Lock() - defer streamsForAccount.Unlock() - for _, s := range streamsForAccount.Streams { - s.Lock() - defer s.Unlock() - if s.Connected { - l.Debugf("streaming notification to stream id %s", s.ID) - s.Messages <- &stream.Message{ - Stream: []string{s.Type}, - Event: "notification", - Payload: string(notificationBytes), - } - } - } - - return nil -} diff --git a/internal/processing/streaming/streamstatus.go b/internal/processing/streaming/streamstatus.go deleted file mode 100644 index f4d6b2629..000000000 --- a/internal/processing/streaming/streamstatus.go +++ /dev/null @@ -1,51 +0,0 @@ -package streaming - -import ( - "encoding/json" - "errors" - "fmt" - - "github.com/sirupsen/logrus" - apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" - "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" - "github.com/superseriousbusiness/gotosocial/internal/stream" -) - -func (p *processor) StreamStatusToAccount(s *apimodel.Status, account *gtsmodel.Account) error { - l := p.log.WithFields(logrus.Fields{ - "func": "StreamStatusForAccount", - "account": account.ID, - }) - v, ok := p.streamMap.Load(account.ID) - if !ok { - // no open connections so nothing to stream - return nil - } - - streamsForAccount, ok := v.(*stream.StreamsForAccount) - if !ok { - return errors.New("stream map error") - } - - statusBytes, err := json.Marshal(s) - if err != nil { - return fmt.Errorf("error marshalling status to json: %s", err) - } - - streamsForAccount.Lock() - defer streamsForAccount.Unlock() - for _, s := range streamsForAccount.Streams { - s.Lock() - defer s.Unlock() - if s.Connected { - l.Debugf("streaming status to stream id %s", s.ID) - s.Messages <- &stream.Message{ - Stream: []string{s.Type}, - Event: "update", - Payload: string(statusBytes), - } - } - } - - return nil -} diff --git a/internal/processing/streaming/streamtoaccount.go b/internal/processing/streaming/streamtoaccount.go new file mode 100644 index 000000000..140910ab7 --- /dev/null +++ b/internal/processing/streaming/streamtoaccount.go @@ -0,0 +1,55 @@ +/* + GoToSocial + Copyright (C) 2021 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 . +*/ + +package streaming + +import ( + "errors" + + "github.com/superseriousbusiness/gotosocial/internal/stream" +) + +// streamToAccount streams the given payload with the given event type to any streams currently open for the given account ID. +func (p *processor) streamToAccount(payload string, event stream.EventType, accountID string) error { + v, ok := p.streamMap.Load(accountID) + if !ok { + // no open connections so nothing to stream + return nil + } + + streamsForAccount, ok := v.(*stream.StreamsForAccount) + if !ok { + return errors.New("stream map error") + } + + streamsForAccount.Lock() + defer streamsForAccount.Unlock() + for _, s := range streamsForAccount.Streams { + s.Lock() + defer s.Unlock() + if s.Connected { + s.Messages <- &stream.Message{ + Stream: []string{s.Type}, + Event: string(event), + Payload: payload, + } + } + } + + return nil +} diff --git a/internal/processing/streaming/update.go b/internal/processing/streaming/update.go new file mode 100644 index 000000000..da7dcb6ce --- /dev/null +++ b/internal/processing/streaming/update.go @@ -0,0 +1,37 @@ +/* + GoToSocial + Copyright (C) 2021 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 . +*/ + +package streaming + +import ( + "encoding/json" + "fmt" + + apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" + "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" + "github.com/superseriousbusiness/gotosocial/internal/stream" +) + +func (p *processor) StreamUpdateToAccount(s *apimodel.Status, account *gtsmodel.Account) error { + bytes, err := json.Marshal(s) + if err != nil { + return fmt.Errorf("error marshalling status to json: %s", err) + } + + return p.streamToAccount(string(bytes), stream.EventTypeUpdate, account.ID) +} diff --git a/internal/processing/timeline.go b/internal/processing/timeline.go index 6a409a6cc..249ef542d 100644 --- a/internal/processing/timeline.go +++ b/internal/processing/timeline.go @@ -151,9 +151,9 @@ func (p *processor) filterPublicStatuses(ctx context.Context, authed *oauth.Auth continue } - apiStatus, err := p.tc.StatusToMasto(ctx, s, authed.Account) + apiStatus, err := p.tc.StatusToAPIStatus(ctx, s, authed.Account) if err != nil { - l.Debugf("filterPublicStatuses: skipping status %s because it couldn't be converted to its mastodon representation: %s", s.ID, err) + l.Debugf("filterPublicStatuses: skipping status %s because it couldn't be converted to its api representation: %s", s.ID, err) continue } @@ -186,9 +186,9 @@ func (p *processor) filterFavedStatuses(ctx context.Context, authed *oauth.Auth, continue } - apiStatus, err := p.tc.StatusToMasto(ctx, s, authed.Account) + apiStatus, err := p.tc.StatusToAPIStatus(ctx, s, authed.Account) if err != nil { - l.Debugf("filterFavedStatuses: skipping status %s because it couldn't be converted to its mastodon representation: %s", s.ID, err) + l.Debugf("filterFavedStatuses: skipping status %s because it couldn't be converted to its api representation: %s", s.ID, err) continue } diff --git a/internal/router/logger.go b/internal/router/logger.go index 0daafba22..154f5c684 100644 --- a/internal/router/logger.go +++ b/internal/router/logger.go @@ -62,9 +62,9 @@ func loggerWithConfig(log *logrus.Logger) gin.HandlerFunc { }) if errorMessage == "" { - l.Infof("%s: wrote %d bytes in %v", http.StatusText(statusCode), bodySize, latency) + l.Infof("[%s] %s: wrote %d bytes", latency, http.StatusText(statusCode), bodySize) } else { - l.Errorf("%s: %s", http.StatusText(statusCode), errorMessage) + l.Errorf("[%s] %s: %s", latency, http.StatusText(statusCode), errorMessage) } } } diff --git a/internal/stream/stream.go b/internal/stream/stream.go index 9d1d27d72..011b7dbe1 100644 --- a/internal/stream/stream.go +++ b/internal/stream/stream.go @@ -2,6 +2,18 @@ import "sync" +// EventType models a type of stream event. +type EventType string + +const ( + // EventTypeNotification -- a user should be shown a notification + EventTypeNotification EventType = "notification" + // EventTypeUpdate -- a user should be shown an update in their timeline + EventTypeUpdate EventType = "update" + // EventTypeDelete -- something should be deleted from a user + EventTypeDelete EventType = "delete" +) + // StreamsForAccount is a wrapper for the multiple streams that one account can have running at the same time. // TODO: put a limit on this type StreamsForAccount struct { diff --git a/internal/timeline/prepare.go b/internal/timeline/prepare.go index d57222ee8..ee8175a5f 100644 --- a/internal/timeline/prepare.go +++ b/internal/timeline/prepare.go @@ -244,7 +244,7 @@ func (t *timeline) prepare(ctx context.Context, statusID string) error { } // serialize the status (or, at least, convert it to a form that's ready to be serialized) - apiModelStatus, err := t.tc.StatusToMasto(ctx, gtsStatus, t.account) + apiModelStatus, err := t.tc.StatusToAPIStatus(ctx, gtsStatus, t.account) if err != nil { return err } diff --git a/internal/typeutils/converter.go b/internal/typeutils/converter.go index 40cb5b969..4515fcbf0 100644 --- a/internal/typeutils/converter.go +++ b/internal/typeutils/converter.go @@ -43,58 +43,58 @@ // That said, it *absolutely should not* manipulate database entries in any way, only examine them. type TypeConverter interface { /* - INTERNAL (gts) MODEL TO FRONTEND (mastodon) MODEL + INTERNAL (gts) MODEL TO FRONTEND (api) MODEL */ - // AccountToMastoSensitive takes a db model account as a param, and returns a populated mastotype account, or an error + // AccountToAPIAccountSensitive takes a db model account as a param, and returns a populated apitype account, or an error // if something goes wrong. The returned account should be ready to serialize on an API level, and may have sensitive fields, // so serve it only to an authorized user who should have permission to see it. - AccountToMastoSensitive(ctx context.Context, account *gtsmodel.Account) (*model.Account, error) - // AccountToMastoPublic takes a db model account as a param, and returns a populated mastotype account, or an error + AccountToAPIAccountSensitive(ctx context.Context, account *gtsmodel.Account) (*model.Account, error) + // AccountToAPIAccountPublic takes a db model account as a param, and returns a populated apitype account, or an error // if something goes wrong. The returned account should be ready to serialize on an API level, and may NOT have sensitive fields. // In other words, this is the public record that the server has of an account. - AccountToMastoPublic(ctx context.Context, account *gtsmodel.Account) (*model.Account, error) - // AccountToMastoBlocked takes a db model account as a param, and returns a mastotype account, or an error if + AccountToAPIAccountPublic(ctx context.Context, account *gtsmodel.Account) (*model.Account, error) + // AccountToAPIAccountBlocked takes a db model account as a param, and returns a apitype account, or an error if // something goes wrong. The returned account will be a bare minimum representation of the account. This function should be used // when someone wants to view an account they've blocked. - AccountToMastoBlocked(ctx context.Context, account *gtsmodel.Account) (*model.Account, error) - // AppToMastoSensitive takes a db model application as a param, and returns a populated mastotype application, or an error + AccountToAPIAccountBlocked(ctx context.Context, account *gtsmodel.Account) (*model.Account, error) + // AppToAPIAppSensitive takes a db model application as a param, and returns a populated apitype application, or an error // if something goes wrong. The returned application should be ready to serialize on an API level, and may have sensitive fields // (such as client id and client secret), so serve it only to an authorized user who should have permission to see it. - AppToMastoSensitive(ctx context.Context, application *gtsmodel.Application) (*model.Application, error) - // AppToMastoPublic takes a db model application as a param, and returns a populated mastotype application, or an error + AppToAPIAppSensitive(ctx context.Context, application *gtsmodel.Application) (*model.Application, error) + // AppToAPIAppPublic takes a db model application as a param, and returns a populated apitype application, or an error // if something goes wrong. The returned application should be ready to serialize on an API level, and has sensitive // fields sanitized so that it can be served to non-authorized accounts without revealing any private information. - AppToMastoPublic(ctx context.Context, application *gtsmodel.Application) (*model.Application, error) - // AttachmentToMasto converts a gts model media attacahment into its mastodon representation for serialization on the API. - AttachmentToMasto(ctx context.Context, attachment *gtsmodel.MediaAttachment) (model.Attachment, error) - // MentionToMasto converts a gts model mention into its mastodon (frontend) representation for serialization on the API. - MentionToMasto(ctx context.Context, m *gtsmodel.Mention) (model.Mention, error) - // EmojiToMasto converts a gts model emoji into its mastodon (frontend) representation for serialization on the API. - EmojiToMasto(ctx context.Context, e *gtsmodel.Emoji) (model.Emoji, error) - // TagToMasto converts a gts model tag into its mastodon (frontend) representation for serialization on the API. - TagToMasto(ctx context.Context, t *gtsmodel.Tag) (model.Tag, error) - // StatusToMasto converts a gts model status into its mastodon (frontend) representation for serialization on the API. + AppToAPIAppPublic(ctx context.Context, application *gtsmodel.Application) (*model.Application, error) + // AttachmentToAPIAttachment converts a gts model media attacahment into its api representation for serialization on the API. + AttachmentToAPIAttachment(ctx context.Context, attachment *gtsmodel.MediaAttachment) (model.Attachment, error) + // MentionToAPIMention converts a gts model mention into its api (frontend) representation for serialization on the API. + MentionToAPIMention(ctx context.Context, m *gtsmodel.Mention) (model.Mention, error) + // EmojiToAPIEmoji converts a gts model emoji into its api (frontend) representation for serialization on the API. + EmojiToAPIEmoji(ctx context.Context, e *gtsmodel.Emoji) (model.Emoji, error) + // TagToAPITag converts a gts model tag into its api (frontend) representation for serialization on the API. + TagToAPITag(ctx context.Context, t *gtsmodel.Tag) (model.Tag, error) + // StatusToAPIStatus converts a gts model status into its api (frontend) representation for serialization on the API. // // Requesting account can be nil. - StatusToMasto(ctx context.Context, s *gtsmodel.Status, requestingAccount *gtsmodel.Account) (*model.Status, error) - // VisToMasto converts a gts visibility into its mastodon equivalent - VisToMasto(ctx context.Context, m gtsmodel.Visibility) model.Visibility - // InstanceToMasto converts a gts instance into its mastodon equivalent for serving at /api/v1/instance - InstanceToMasto(ctx context.Context, i *gtsmodel.Instance) (*model.Instance, error) - // RelationshipToMasto converts a gts relationship into its mastodon equivalent for serving in various places - RelationshipToMasto(ctx context.Context, r *gtsmodel.Relationship) (*model.Relationship, error) - // NotificationToMasto converts a gts notification into a mastodon notification - NotificationToMasto(ctx context.Context, n *gtsmodel.Notification) (*model.Notification, error) - // DomainBlockTomasto converts a gts model domin block into a mastodon domain block, for serving at /api/v1/admin/domain_blocks - DomainBlockToMasto(ctx context.Context, b *gtsmodel.DomainBlock, export bool) (*model.DomainBlock, error) + StatusToAPIStatus(ctx context.Context, s *gtsmodel.Status, requestingAccount *gtsmodel.Account) (*model.Status, error) + // VisToAPIVis converts a gts visibility into its api equivalent + VisToAPIVis(ctx context.Context, m gtsmodel.Visibility) model.Visibility + // InstanceToAPIInstance converts a gts instance into its api equivalent for serving at /api/v1/instance + InstanceToAPIInstance(ctx context.Context, i *gtsmodel.Instance) (*model.Instance, error) + // RelationshipToAPIRelationship converts a gts relationship into its api equivalent for serving in various places + RelationshipToAPIRelationship(ctx context.Context, r *gtsmodel.Relationship) (*model.Relationship, error) + // NotificationToAPINotification converts a gts notification into a api notification + NotificationToAPINotification(ctx context.Context, n *gtsmodel.Notification) (*model.Notification, error) + // DomainBlockToAPIDomainBlock converts a gts model domin block into a api domain block, for serving at /api/v1/admin/domain_blocks + DomainBlockToAPIDomainBlock(ctx context.Context, b *gtsmodel.DomainBlock, export bool) (*model.DomainBlock, error) /* - FRONTEND (mastodon) MODEL TO INTERNAL (gts) MODEL + FRONTEND (api) MODEL TO INTERNAL (gts) MODEL */ - // MastoVisToVis converts a mastodon visibility into its gts equivalent. - MastoVisToVis(m model.Visibility) gtsmodel.Visibility + // APIVisToVis converts an API model visibility into its internal gts equivalent. + APIVisToVis(m model.Visibility) gtsmodel.Visibility /* ACTIVITYSTREAMS MODEL TO INTERNAL (gts) MODEL diff --git a/internal/typeutils/frontendtointernal.go b/internal/typeutils/frontendtointernal.go index 75c5168aa..371bff99a 100644 --- a/internal/typeutils/frontendtointernal.go +++ b/internal/typeutils/frontendtointernal.go @@ -23,8 +23,7 @@ "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" ) -// MastoVisToVis converts a mastodon visibility into its gts equivalent. -func (c *converter) MastoVisToVis(m model.Visibility) gtsmodel.Visibility { +func (c *converter) APIVisToVis(m model.Visibility) gtsmodel.Visibility { switch m { case model.VisibilityPublic: return gtsmodel.VisibilityPublic diff --git a/internal/typeutils/internaltoas.go b/internal/typeutils/internaltoas.go index 819b78267..1689db496 100644 --- a/internal/typeutils/internaltoas.go +++ b/internal/typeutils/internaltoas.go @@ -31,8 +31,7 @@ "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" ) -// Converts a gts model account into an Activity Streams person type, following -// the spec laid out for mastodon here: https://docs.joinmastodon.org/spec/activitypub/ +// Converts a gts model account into an Activity Streams person type. func (c *converter) AccountToAS(ctx context.Context, a *gtsmodel.Account) (vocab.ActivityStreamsPerson, error) { person := streams.NewActivityStreamsPerson() @@ -267,8 +266,7 @@ func (c *converter) AccountToAS(ctx context.Context, a *gtsmodel.Account) (vocab return person, nil } -// Converts a gts model account into a VERY MINIMAL Activity Streams person type, following -// the spec laid out for mastodon here: https://docs.joinmastodon.org/spec/activitypub/ +// Converts a gts model account into a VERY MINIMAL Activity Streams person type. // // The returned account will just have the Type, Username, PublicKey, and ID properties set. func (c *converter) AccountToASMinimal(ctx context.Context, a *gtsmodel.Account) (vocab.ActivityStreamsPerson, error) { diff --git a/internal/typeutils/internaltofrontend.go b/internal/typeutils/internaltofrontend.go index 67d6cef94..70899d2d8 100644 --- a/internal/typeutils/internaltofrontend.go +++ b/internal/typeutils/internaltofrontend.go @@ -29,9 +29,9 @@ "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" ) -func (c *converter) AccountToMastoSensitive(ctx context.Context, a *gtsmodel.Account) (*model.Account, error) { +func (c *converter) AccountToAPIAccountSensitive(ctx context.Context, a *gtsmodel.Account) (*model.Account, error) { // we can build this sensitive account easily by first getting the public account.... - mastoAccount, err := c.AccountToMastoPublic(ctx, a) + apiAccount, err := c.AccountToAPIAccountPublic(ctx, a) if err != nil { return nil, err } @@ -50,19 +50,19 @@ func (c *converter) AccountToMastoSensitive(ctx context.Context, a *gtsmodel.Acc frc = len(frs) } - mastoAccount.Source = &model.Source{ - Privacy: c.VisToMasto(ctx, a.Privacy), + apiAccount.Source = &model.Source{ + Privacy: c.VisToAPIVis(ctx, a.Privacy), Sensitive: a.Sensitive, Language: a.Language, Note: a.Note, - Fields: mastoAccount.Fields, + Fields: apiAccount.Fields, FollowRequestsCount: frc, } - return mastoAccount, nil + return apiAccount, nil } -func (c *converter) AccountToMastoPublic(ctx context.Context, a *gtsmodel.Account) (*model.Account, error) { +func (c *converter) AccountToAPIAccountPublic(ctx context.Context, a *gtsmodel.Account) (*model.Account, error) { if a == nil { return nil, fmt.Errorf("given account was nil") } @@ -179,7 +179,7 @@ func (c *converter) AccountToMastoPublic(ctx context.Context, a *gtsmodel.Accoun return accountFrontend, nil } -func (c *converter) AccountToMastoBlocked(ctx context.Context, a *gtsmodel.Account) (*model.Account, error) { +func (c *converter) AccountToAPIAccountBlocked(ctx context.Context, a *gtsmodel.Account) (*model.Account, error) { var acct string if a.Domain != "" { // this is a remote user @@ -206,7 +206,7 @@ func (c *converter) AccountToMastoBlocked(ctx context.Context, a *gtsmodel.Accou }, nil } -func (c *converter) AppToMastoSensitive(ctx context.Context, a *gtsmodel.Application) (*model.Application, error) { +func (c *converter) AppToAPIAppSensitive(ctx context.Context, a *gtsmodel.Application) (*model.Application, error) { return &model.Application{ ID: a.ID, Name: a.Name, @@ -217,14 +217,14 @@ func (c *converter) AppToMastoSensitive(ctx context.Context, a *gtsmodel.Applica }, nil } -func (c *converter) AppToMastoPublic(ctx context.Context, a *gtsmodel.Application) (*model.Application, error) { +func (c *converter) AppToAPIAppPublic(ctx context.Context, a *gtsmodel.Application) (*model.Application, error) { return &model.Application{ Name: a.Name, Website: a.Website, }, nil } -func (c *converter) AttachmentToMasto(ctx context.Context, a *gtsmodel.MediaAttachment) (model.Attachment, error) { +func (c *converter) AttachmentToAPIAttachment(ctx context.Context, a *gtsmodel.MediaAttachment) (model.Attachment, error) { return model.Attachment{ ID: a.ID, Type: strings.ToLower(string(a.Type)), @@ -255,7 +255,7 @@ func (c *converter) AttachmentToMasto(ctx context.Context, a *gtsmodel.MediaAtta }, nil } -func (c *converter) MentionToMasto(ctx context.Context, m *gtsmodel.Mention) (model.Mention, error) { +func (c *converter) MentionToAPIMention(ctx context.Context, m *gtsmodel.Mention) (model.Mention, error) { if m.TargetAccount == nil { targetAccount, err := c.db.GetAccountByID(ctx, m.TargetAccountID) if err != nil { @@ -284,7 +284,7 @@ func (c *converter) MentionToMasto(ctx context.Context, m *gtsmodel.Mention) (mo }, nil } -func (c *converter) EmojiToMasto(ctx context.Context, e *gtsmodel.Emoji) (model.Emoji, error) { +func (c *converter) EmojiToAPIEmoji(ctx context.Context, e *gtsmodel.Emoji) (model.Emoji, error) { return model.Emoji{ Shortcode: e.Shortcode, URL: e.ImageURL, @@ -294,14 +294,14 @@ func (c *converter) EmojiToMasto(ctx context.Context, e *gtsmodel.Emoji) (model. }, nil } -func (c *converter) TagToMasto(ctx context.Context, t *gtsmodel.Tag) (model.Tag, error) { +func (c *converter) TagToAPITag(ctx context.Context, t *gtsmodel.Tag) (model.Tag, error) { return model.Tag{ Name: t.Name, URL: t.URL, }, nil } -func (c *converter) StatusToMasto(ctx context.Context, s *gtsmodel.Status, requestingAccount *gtsmodel.Account) (*model.Status, error) { +func (c *converter) StatusToAPIStatus(ctx context.Context, s *gtsmodel.Status, requestingAccount *gtsmodel.Account) (*model.Status, error) { l := c.log repliesCount, err := c.db.CountStatusReplies(ctx, s) @@ -319,7 +319,7 @@ func (c *converter) StatusToMasto(ctx context.Context, s *gtsmodel.Status, reque return nil, fmt.Errorf("error counting faves: %s", err) } - var mastoRebloggedStatus *model.Status + var apiRebloggedStatus *model.Status if s.BoostOfID != "" { // the boosted status might have been set on this struct already so check first before doing db calls if s.BoostOf == nil { @@ -342,19 +342,19 @@ func (c *converter) StatusToMasto(ctx context.Context, s *gtsmodel.Status, reque s.BoostOf.Account = ba } - mastoRebloggedStatus, err = c.StatusToMasto(ctx, s.BoostOf, requestingAccount) + apiRebloggedStatus, err = c.StatusToAPIStatus(ctx, s.BoostOf, requestingAccount) if err != nil { - return nil, fmt.Errorf("error converting boosted status to mastotype: %s", err) + return nil, fmt.Errorf("error converting boosted status to apitype: %s", err) } } - var mastoApplication *model.Application + var apiApplication *model.Application if s.CreatedWithApplicationID != "" { gtsApplication := >smodel.Application{} if err := c.db.GetByID(ctx, s.CreatedWithApplicationID, gtsApplication); err != nil { return nil, fmt.Errorf("error fetching application used to create status: %s", err) } - mastoApplication, err = c.AppToMastoPublic(ctx, gtsApplication) + apiApplication, err = c.AppToAPIAppPublic(ctx, gtsApplication) if err != nil { return nil, fmt.Errorf("error parsing application used to create status: %s", err) } @@ -368,25 +368,25 @@ func (c *converter) StatusToMasto(ctx context.Context, s *gtsmodel.Status, reque s.Account = a } - mastoAuthorAccount, err := c.AccountToMastoPublic(ctx, s.Account) + apiAuthorAccount, err := c.AccountToAPIAccountPublic(ctx, s.Account) if err != nil { return nil, fmt.Errorf("error parsing account of status author: %s", err) } - mastoAttachments := []model.Attachment{} + apiAttachments := []model.Attachment{} // the status might already have some gts attachments on it if it's not been pulled directly from the database - // if so, we can directly convert the gts attachments into masto ones + // if so, we can directly convert the gts attachments into api ones if s.Attachments != nil { for _, gtsAttachment := range s.Attachments { - mastoAttachment, err := c.AttachmentToMasto(ctx, gtsAttachment) + apiAttachment, err := c.AttachmentToAPIAttachment(ctx, gtsAttachment) if err != nil { l.Errorf("error converting attachment with id %s: %s", gtsAttachment.ID, err) continue } - mastoAttachments = append(mastoAttachments, mastoAttachment) + apiAttachments = append(apiAttachments, apiAttachment) } // the status doesn't have gts attachments on it, but it does have attachment IDs - // in this case, we need to pull the gts attachments from the db to convert them into masto ones + // in this case, we need to pull the gts attachments from the db to convert them into api ones } else { for _, aID := range s.AttachmentIDs { gtsAttachment, err := c.db.GetAttachmentByID(ctx, aID) @@ -394,29 +394,29 @@ func (c *converter) StatusToMasto(ctx context.Context, s *gtsmodel.Status, reque l.Errorf("error getting attachment with id %s: %s", aID, err) continue } - mastoAttachment, err := c.AttachmentToMasto(ctx, gtsAttachment) + apiAttachment, err := c.AttachmentToAPIAttachment(ctx, gtsAttachment) if err != nil { l.Errorf("error converting attachment with id %s: %s", aID, err) continue } - mastoAttachments = append(mastoAttachments, mastoAttachment) + apiAttachments = append(apiAttachments, apiAttachment) } } - mastoMentions := []model.Mention{} + apiMentions := []model.Mention{} // the status might already have some gts mentions on it if it's not been pulled directly from the database - // if so, we can directly convert the gts mentions into masto ones + // if so, we can directly convert the gts mentions into api ones if s.Mentions != nil { for _, gtsMention := range s.Mentions { - mastoMention, err := c.MentionToMasto(ctx, gtsMention) + apiMention, err := c.MentionToAPIMention(ctx, gtsMention) if err != nil { l.Errorf("error converting mention with id %s: %s", gtsMention.ID, err) continue } - mastoMentions = append(mastoMentions, mastoMention) + apiMentions = append(apiMentions, apiMention) } // the status doesn't have gts mentions on it, but it does have mention IDs - // in this case, we need to pull the gts mentions from the db to convert them into masto ones + // in this case, we need to pull the gts mentions from the db to convert them into api ones } else { for _, mID := range s.MentionIDs { gtsMention, err := c.db.GetMention(ctx, mID) @@ -424,29 +424,29 @@ func (c *converter) StatusToMasto(ctx context.Context, s *gtsmodel.Status, reque l.Errorf("error getting mention with id %s: %s", mID, err) continue } - mastoMention, err := c.MentionToMasto(ctx, gtsMention) + apiMention, err := c.MentionToAPIMention(ctx, gtsMention) if err != nil { l.Errorf("error converting mention with id %s: %s", gtsMention.ID, err) continue } - mastoMentions = append(mastoMentions, mastoMention) + apiMentions = append(apiMentions, apiMention) } } - mastoTags := []model.Tag{} + apiTags := []model.Tag{} // the status might already have some gts tags on it if it's not been pulled directly from the database - // if so, we can directly convert the gts tags into masto ones + // if so, we can directly convert the gts tags into api ones if s.Tags != nil { for _, gtsTag := range s.Tags { - mastoTag, err := c.TagToMasto(ctx, gtsTag) + apiTag, err := c.TagToAPITag(ctx, gtsTag) if err != nil { l.Errorf("error converting tag with id %s: %s", gtsTag.ID, err) continue } - mastoTags = append(mastoTags, mastoTag) + apiTags = append(apiTags, apiTag) } // the status doesn't have gts tags on it, but it does have tag IDs - // in this case, we need to pull the gts tags from the db to convert them into masto ones + // in this case, we need to pull the gts tags from the db to convert them into api ones } else { for _, t := range s.TagIDs { gtsTag := >smodel.Tag{} @@ -454,29 +454,29 @@ func (c *converter) StatusToMasto(ctx context.Context, s *gtsmodel.Status, reque l.Errorf("error getting tag with id %s: %s", t, err) continue } - mastoTag, err := c.TagToMasto(ctx, gtsTag) + apiTag, err := c.TagToAPITag(ctx, gtsTag) if err != nil { l.Errorf("error converting tag with id %s: %s", gtsTag.ID, err) continue } - mastoTags = append(mastoTags, mastoTag) + apiTags = append(apiTags, apiTag) } } - mastoEmojis := []model.Emoji{} + apiEmojis := []model.Emoji{} // the status might already have some gts emojis on it if it's not been pulled directly from the database - // if so, we can directly convert the gts emojis into masto ones + // if so, we can directly convert the gts emojis into api ones if s.Emojis != nil { for _, gtsEmoji := range s.Emojis { - mastoEmoji, err := c.EmojiToMasto(ctx, gtsEmoji) + apiEmoji, err := c.EmojiToAPIEmoji(ctx, gtsEmoji) if err != nil { l.Errorf("error converting emoji with id %s: %s", gtsEmoji.ID, err) continue } - mastoEmojis = append(mastoEmojis, mastoEmoji) + apiEmojis = append(apiEmojis, apiEmoji) } // the status doesn't have gts emojis on it, but it does have emoji IDs - // in this case, we need to pull the gts emojis from the db to convert them into masto ones + // in this case, we need to pull the gts emojis from the db to convert them into api ones } else { for _, e := range s.EmojiIDs { gtsEmoji := >smodel.Emoji{} @@ -484,17 +484,17 @@ func (c *converter) StatusToMasto(ctx context.Context, s *gtsmodel.Status, reque l.Errorf("error getting emoji with id %s: %s", e, err) continue } - mastoEmoji, err := c.EmojiToMasto(ctx, gtsEmoji) + apiEmoji, err := c.EmojiToAPIEmoji(ctx, gtsEmoji) if err != nil { l.Errorf("error converting emoji with id %s: %s", gtsEmoji.ID, err) continue } - mastoEmojis = append(mastoEmojis, mastoEmoji) + apiEmojis = append(apiEmojis, apiEmoji) } } - var mastoCard *model.Card - var mastoPoll *model.Poll + var apiCard *model.Card + var apiPoll *model.Poll statusInteractions := &statusInteractions{} si, err := c.interactionsWithStatusForAccount(ctx, s, requestingAccount) @@ -509,7 +509,7 @@ func (c *converter) StatusToMasto(ctx context.Context, s *gtsmodel.Status, reque InReplyToAccountID: s.InReplyToAccountID, Sensitive: s.Sensitive, SpoilerText: s.ContentWarning, - Visibility: c.VisToMasto(ctx, s.Visibility), + Visibility: c.VisToAPIVis(ctx, s.Visibility), Language: s.Language, URI: s.URI, URL: s.URL, @@ -522,26 +522,26 @@ func (c *converter) StatusToMasto(ctx context.Context, s *gtsmodel.Status, reque Reblogged: statusInteractions.Reblogged, Pinned: s.Pinned, Content: s.Content, - Application: mastoApplication, - Account: mastoAuthorAccount, - MediaAttachments: mastoAttachments, - Mentions: mastoMentions, - Tags: mastoTags, - Emojis: mastoEmojis, - Card: mastoCard, // TODO: implement cards - Poll: mastoPoll, // TODO: implement polls + Application: apiApplication, + Account: apiAuthorAccount, + MediaAttachments: apiAttachments, + Mentions: apiMentions, + Tags: apiTags, + Emojis: apiEmojis, + Card: apiCard, // TODO: implement cards + Poll: apiPoll, // TODO: implement polls Text: s.Text, } - if mastoRebloggedStatus != nil { - apiStatus.Reblog = &model.StatusReblogged{Status: mastoRebloggedStatus} + if apiRebloggedStatus != nil { + apiStatus.Reblog = &model.StatusReblogged{Status: apiRebloggedStatus} } return apiStatus, nil } -// VisToMasto converts a gts visibility into its mastodon equivalent -func (c *converter) VisToMasto(ctx context.Context, m gtsmodel.Visibility) model.Visibility { +// VisToapi converts a gts visibility into its api equivalent +func (c *converter) VisToAPIVis(ctx context.Context, m gtsmodel.Visibility) model.Visibility { switch m { case gtsmodel.VisibilityPublic: return model.VisibilityPublic @@ -555,7 +555,7 @@ func (c *converter) VisToMasto(ctx context.Context, m gtsmodel.Visibility) model return "" } -func (c *converter) InstanceToMasto(ctx context.Context, i *gtsmodel.Instance) (*model.Instance, error) { +func (c *converter) InstanceToAPIInstance(ctx context.Context, i *gtsmodel.Instance) (*model.Instance, error) { mi := &model.Instance{ URI: i.URI, Title: i.Title, @@ -614,7 +614,7 @@ func (c *converter) InstanceToMasto(ctx context.Context, i *gtsmodel.Instance) ( i.ContactAccount = contactAccount } } - ma, err := c.AccountToMastoPublic(ctx, i.ContactAccount) + ma, err := c.AccountToAPIAccountPublic(ctx, i.ContactAccount) if err == nil { mi.ContactAccount = ma } @@ -623,7 +623,7 @@ func (c *converter) InstanceToMasto(ctx context.Context, i *gtsmodel.Instance) ( return mi, nil } -func (c *converter) RelationshipToMasto(ctx context.Context, r *gtsmodel.Relationship) (*model.Relationship, error) { +func (c *converter) RelationshipToAPIRelationship(ctx context.Context, r *gtsmodel.Relationship) (*model.Relationship, error) { return &model.Relationship{ ID: r.ID, Following: r.Following, @@ -641,11 +641,11 @@ func (c *converter) RelationshipToMasto(ctx context.Context, r *gtsmodel.Relatio }, nil } -func (c *converter) NotificationToMasto(ctx context.Context, n *gtsmodel.Notification) (*model.Notification, error) { +func (c *converter) NotificationToAPINotification(ctx context.Context, n *gtsmodel.Notification) (*model.Notification, error) { if n.TargetAccount == nil { tAccount, err := c.db.GetAccountByID(ctx, n.TargetAccountID) if err != nil { - return nil, fmt.Errorf("NotificationToMasto: error getting target account with id %s from the db: %s", n.TargetAccountID, err) + return nil, fmt.Errorf("NotificationToapi: error getting target account with id %s from the db: %s", n.TargetAccountID, err) } n.TargetAccount = tAccount } @@ -653,22 +653,22 @@ func (c *converter) NotificationToMasto(ctx context.Context, n *gtsmodel.Notific if n.OriginAccount == nil { ogAccount, err := c.db.GetAccountByID(ctx, n.OriginAccountID) if err != nil { - return nil, fmt.Errorf("NotificationToMasto: error getting origin account with id %s from the db: %s", n.OriginAccountID, err) + return nil, fmt.Errorf("NotificationToapi: error getting origin account with id %s from the db: %s", n.OriginAccountID, err) } n.OriginAccount = ogAccount } - mastoAccount, err := c.AccountToMastoPublic(ctx, n.OriginAccount) + apiAccount, err := c.AccountToAPIAccountPublic(ctx, n.OriginAccount) if err != nil { - return nil, fmt.Errorf("NotificationToMasto: error converting account to masto: %s", err) + return nil, fmt.Errorf("NotificationToapi: error converting account to api: %s", err) } - var mastoStatus *model.Status + var apiStatus *model.Status if n.StatusID != "" { if n.Status == nil { status, err := c.db.GetStatusByID(ctx, n.StatusID) if err != nil { - return nil, fmt.Errorf("NotificationToMasto: error getting status with id %s from the db: %s", n.StatusID, err) + return nil, fmt.Errorf("NotificationToapi: error getting status with id %s from the db: %s", n.StatusID, err) } n.Status = status } @@ -682,9 +682,9 @@ func (c *converter) NotificationToMasto(ctx context.Context, n *gtsmodel.Notific } var err error - mastoStatus, err = c.StatusToMasto(ctx, n.Status, nil) + apiStatus, err = c.StatusToAPIStatus(ctx, n.Status, nil) if err != nil { - return nil, fmt.Errorf("NotificationToMasto: error converting status to masto: %s", err) + return nil, fmt.Errorf("NotificationToapi: error converting status to api: %s", err) } } @@ -692,12 +692,12 @@ func (c *converter) NotificationToMasto(ctx context.Context, n *gtsmodel.Notific ID: n.ID, Type: string(n.NotificationType), CreatedAt: n.CreatedAt.Format(time.RFC3339), - Account: mastoAccount, - Status: mastoStatus, + Account: apiAccount, + Status: apiStatus, }, nil } -func (c *converter) DomainBlockToMasto(ctx context.Context, b *gtsmodel.DomainBlock, export bool) (*model.DomainBlock, error) { +func (c *converter) DomainBlockToAPIDomainBlock(ctx context.Context, b *gtsmodel.DomainBlock, export bool) (*model.DomainBlock, error) { domainBlock := &model.DomainBlock{ Domain: b.Domain, diff --git a/testrig/oauthserver.go b/testrig/oauthserver.go index 49615cadc..7ed230c49 100644 --- a/testrig/oauthserver.go +++ b/testrig/oauthserver.go @@ -19,11 +19,13 @@ package testrig import ( + "context" + "github.com/superseriousbusiness/gotosocial/internal/db" "github.com/superseriousbusiness/gotosocial/internal/oauth" ) // NewTestOauthServer returns an oauth server with the given db, and the default test logger. func NewTestOauthServer(db db.DB) oauth.Server { - return oauth.New(db, NewTestLog()) + return oauth.New(context.Background(), db, NewTestLog()) } diff --git a/testrig/testmodels.go b/testrig/testmodels.go index 23762707c..3f32b588d 100644 --- a/testrig/testmodels.go +++ b/testrig/testmodels.go @@ -58,7 +58,7 @@ func NewTestTokens() map[string]*gtsmodel.Token { "local_account_2": { ID: "01F8MGVVM1EDVYET710J27XY5R", ClientID: "01F8MGW47HN8ZXNHNZ7E47CDMQ", - UserID: "01F8MGWAPB4GJ42M4N0TCZSQ7K", + UserID: "01F8MH1VYJAE00TVVGMM5JNJ8X", RedirectURI: "http://localhost:8080", Scope: "read write follow push", Access: "PIPINALKNNNFNF98717NAMNAMNFKIJKJ881818KJKJAKJJJA", @@ -88,7 +88,7 @@ func NewTestClients() map[string]*gtsmodel.Client { ID: "01F8MGW47HN8ZXNHNZ7E47CDMQ", Secret: "8f5603a5-c721-46cd-8f1b-2e368f51379f", Domain: "http://localhost:8080", - UserID: "01F8MGWAPB4GJ42M4N0TCZSQ7K", // local_account_2 + UserID: "01F8MH1VYJAE00TVVGMM5JNJ8X", // local_account_2 }, } return clients @@ -420,7 +420,7 @@ func NewTestAccounts() map[string]*gtsmodel.Account { Note: "i post about like, i dunno, stuff, or whatever!!!!", Memorial: false, MovedToAccountID: "", - CreatedAt: time.Now().Add(-190 * time.Hour), + CreatedAt: TimeMustParse("2021-09-26T12:52:36+02:00"), UpdatedAt: time.Now().Add(-36 * time.Hour), Bot: false, Locked: false, diff --git a/testrig/util.go b/testrig/util.go index 0410366e3..6cadb90d2 100644 --- a/testrig/util.go +++ b/testrig/util.go @@ -24,6 +24,7 @@ "mime/multipart" "net/url" "os" + "time" ) // CreateMultipartFormData is a handy function for taking a fieldname and a filename, and creating a multipart form bytes buffer @@ -76,3 +77,13 @@ func URLMustParse(stringURL string) *url.URL { } return u } + +// TimeMustParse tries to parse the given time as RFC3339, and panics if it can't. +// Should only be used in tests. +func TimeMustParse(timeString string) time.Time { + t, err := time.Parse(time.RFC3339, timeString) + if err != nil { + panic(err) + } + return t +}