diff --git a/internal/api/client/account/accountcreate.go b/internal/api/client/account/accountcreate.go index 50e72655e..a9d672f80 100644 --- a/internal/api/client/account/accountcreate.go +++ b/internal/api/client/account/accountcreate.go @@ -101,7 +101,7 @@ func (m *Module) AccountCreatePOSTHandler(c *gin.Context) { form.IP = signUpIP - ti, err := m.processor.AccountCreate(authed, form) + ti, err := m.processor.AccountCreate(c.Request.Context(), authed, form) if err != nil { l.Errorf("internal server error while creating new account: %s", err) c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) diff --git a/internal/api/client/account/accountget.go b/internal/api/client/account/accountget.go index a7f9d8c70..8bac1360b 100644 --- a/internal/api/client/account/accountget.go +++ b/internal/api/client/account/accountget.go @@ -70,7 +70,7 @@ func (m *Module) AccountGETHandler(c *gin.Context) { return } - acctInfo, err := m.processor.AccountGet(authed, targetAcctID) + acctInfo, err := m.processor.AccountGet(c.Request.Context(), authed, targetAcctID) if err != nil { c.JSON(http.StatusNotFound, gin.H{"error": "not found"}) return diff --git a/internal/api/client/account/accountupdate.go b/internal/api/client/account/accountupdate.go index f55f45f59..282d172ed 100644 --- a/internal/api/client/account/accountupdate.go +++ b/internal/api/client/account/accountupdate.go @@ -122,7 +122,7 @@ func (m *Module) AccountUpdateCredentialsPATCHHandler(c *gin.Context) { return } - acctSensitive, err := m.processor.AccountUpdate(authed, form) + acctSensitive, err := m.processor.AccountUpdate(c.Request.Context(), authed, form) if err != nil { l.Debugf("could not update account: %s", err) c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) diff --git a/internal/api/client/account/accountverify.go b/internal/api/client/account/accountverify.go index 4c77f3fa6..c5c40a03d 100644 --- a/internal/api/client/account/accountverify.go +++ b/internal/api/client/account/accountverify.go @@ -59,7 +59,7 @@ func (m *Module) AccountVerifyGETHandler(c *gin.Context) { return } - acctSensitive, err := m.processor.AccountGet(authed, authed.Account.ID) + acctSensitive, err := m.processor.AccountGet(c.Request.Context(), authed, authed.Account.ID) if err != nil { l.Debugf("error getting account from processor: %s", err) c.JSON(http.StatusInternalServerError, gin.H{"error": "internal server error"}) diff --git a/internal/api/client/account/block.go b/internal/api/client/account/block.go index 0d9d6c51b..243f90c5e 100644 --- a/internal/api/client/account/block.go +++ b/internal/api/client/account/block.go @@ -72,7 +72,7 @@ func (m *Module) AccountBlockPOSTHandler(c *gin.Context) { return } - relationship, errWithCode := m.processor.AccountBlockCreate(authed, targetAcctID) + relationship, errWithCode := m.processor.AccountBlockCreate(c.Request.Context(), authed, targetAcctID) if errWithCode != nil { c.JSON(errWithCode.Code(), gin.H{"error": errWithCode.Safe()}) return diff --git a/internal/api/client/account/follow.go b/internal/api/client/account/follow.go index 985a5f821..8f3f7ad0a 100644 --- a/internal/api/client/account/follow.go +++ b/internal/api/client/account/follow.go @@ -99,7 +99,7 @@ func (m *Module) AccountFollowPOSTHandler(c *gin.Context) { } form.ID = targetAcctID - relationship, errWithCode := m.processor.AccountFollowCreate(authed, form) + relationship, errWithCode := m.processor.AccountFollowCreate(c.Request.Context(), authed, form) if errWithCode != nil { c.JSON(errWithCode.Code(), gin.H{"error": errWithCode.Safe()}) return diff --git a/internal/api/client/account/followers.go b/internal/api/client/account/followers.go index 7e93544b8..4f30e9939 100644 --- a/internal/api/client/account/followers.go +++ b/internal/api/client/account/followers.go @@ -74,7 +74,7 @@ func (m *Module) AccountFollowersGETHandler(c *gin.Context) { return } - followers, errWithCode := m.processor.AccountFollowersGet(authed, targetAcctID) + followers, errWithCode := m.processor.AccountFollowersGet(c.Request.Context(), authed, targetAcctID) if errWithCode != nil { c.JSON(errWithCode.Code(), gin.H{"error": errWithCode.Safe()}) return diff --git a/internal/api/client/account/following.go b/internal/api/client/account/following.go index e70265eb5..baac2c9d3 100644 --- a/internal/api/client/account/following.go +++ b/internal/api/client/account/following.go @@ -74,7 +74,7 @@ func (m *Module) AccountFollowingGETHandler(c *gin.Context) { return } - following, errWithCode := m.processor.AccountFollowingGet(authed, targetAcctID) + following, errWithCode := m.processor.AccountFollowingGet(c.Request.Context(), authed, targetAcctID) if errWithCode != nil { c.JSON(errWithCode.Code(), gin.H{"error": errWithCode.Safe()}) return diff --git a/internal/api/client/account/relationships.go b/internal/api/client/account/relationships.go index 9dbc8c4bb..d350209af 100644 --- a/internal/api/client/account/relationships.go +++ b/internal/api/client/account/relationships.go @@ -71,7 +71,7 @@ func (m *Module) AccountRelationshipsGETHandler(c *gin.Context) { relationships := []model.Relationship{} for _, targetAccountID := range targetAccountIDs { - r, errWithCode := m.processor.AccountRelationshipGet(authed, targetAccountID) + r, errWithCode := m.processor.AccountRelationshipGet(c.Request.Context(), authed, targetAccountID) if err != nil { c.JSON(errWithCode.Code(), gin.H{"error": errWithCode.Safe()}) return diff --git a/internal/api/client/account/statuses.go b/internal/api/client/account/statuses.go index 097ccc3cc..4841d86df 100644 --- a/internal/api/client/account/statuses.go +++ b/internal/api/client/account/statuses.go @@ -166,7 +166,7 @@ func (m *Module) AccountStatusesGETHandler(c *gin.Context) { mediaOnly = i } - statuses, errWithCode := m.processor.AccountStatusesGet(authed, targetAcctID, limit, excludeReplies, maxID, pinnedOnly, mediaOnly) + statuses, errWithCode := m.processor.AccountStatusesGet(c.Request.Context(), authed, targetAcctID, limit, excludeReplies, maxID, pinnedOnly, mediaOnly) if errWithCode != nil { l.Debugf("error from processor account statuses get: %s", errWithCode) c.JSON(errWithCode.Code(), gin.H{"error": errWithCode.Safe()}) diff --git a/internal/api/client/account/unblock.go b/internal/api/client/account/unblock.go index d9a2f2881..7b16ac887 100644 --- a/internal/api/client/account/unblock.go +++ b/internal/api/client/account/unblock.go @@ -72,7 +72,7 @@ func (m *Module) AccountUnblockPOSTHandler(c *gin.Context) { return } - relationship, errWithCode := m.processor.AccountBlockRemove(authed, targetAcctID) + relationship, errWithCode := m.processor.AccountBlockRemove(c.Request.Context(), authed, targetAcctID) if errWithCode != nil { c.JSON(errWithCode.Code(), gin.H{"error": errWithCode.Safe()}) return diff --git a/internal/api/client/account/unfollow.go b/internal/api/client/account/unfollow.go index 84a558c65..7ac9697d5 100644 --- a/internal/api/client/account/unfollow.go +++ b/internal/api/client/account/unfollow.go @@ -75,7 +75,7 @@ func (m *Module) AccountUnfollowPOSTHandler(c *gin.Context) { return } - relationship, errWithCode := m.processor.AccountFollowRemove(authed, targetAcctID) + relationship, errWithCode := m.processor.AccountFollowRemove(c.Request.Context(), authed, targetAcctID) if errWithCode != nil { l.Debug(errWithCode.Error()) c.JSON(errWithCode.Code(), gin.H{"error": errWithCode.Safe()}) diff --git a/internal/api/client/admin/domainblockcreate.go b/internal/api/client/admin/domainblockcreate.go index d48c70408..9ef4c6f92 100644 --- a/internal/api/client/admin/domainblockcreate.go +++ b/internal/api/client/admin/domainblockcreate.go @@ -141,7 +141,7 @@ func (m *Module) DomainBlocksPOSTHandler(c *gin.Context) { if imp { // we're importing multiple blocks - domainBlocks, err := m.processor.AdminDomainBlocksImport(authed, form) + domainBlocks, err := m.processor.AdminDomainBlocksImport(c.Request.Context(), authed, form) if err != nil { l.Debugf("error importing domain blocks: %s", err) c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) @@ -150,7 +150,7 @@ func (m *Module) DomainBlocksPOSTHandler(c *gin.Context) { c.JSON(http.StatusOK, domainBlocks) } else { // we're just creating one block - domainBlock, err := m.processor.AdminDomainBlockCreate(authed, form) + domainBlock, err := m.processor.AdminDomainBlockCreate(c.Request.Context(), authed, form) if err != nil { l.Debugf("error creating domain block: %s", err) c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) diff --git a/internal/api/client/admin/domainblockdelete.go b/internal/api/client/admin/domainblockdelete.go index 9cd2fd711..64e4ef6de 100644 --- a/internal/api/client/admin/domainblockdelete.go +++ b/internal/api/client/admin/domainblockdelete.go @@ -68,7 +68,7 @@ func (m *Module) DomainBlockDELETEHandler(c *gin.Context) { return } - domainBlock, errWithCode := m.processor.AdminDomainBlockDelete(authed, domainBlockID) + domainBlock, errWithCode := m.processor.AdminDomainBlockDelete(c.Request.Context(), authed, domainBlockID) if errWithCode != nil { l.Debugf("error deleting domain block: %s", errWithCode.Error()) c.JSON(errWithCode.Code(), gin.H{"error": errWithCode.Safe()}) diff --git a/internal/api/client/admin/domainblockget.go b/internal/api/client/admin/domainblockget.go index 86923d705..d23f99a8c 100644 --- a/internal/api/client/admin/domainblockget.go +++ b/internal/api/client/admin/domainblockget.go @@ -81,7 +81,7 @@ func (m *Module) DomainBlockGETHandler(c *gin.Context) { export = i } - domainBlock, err := m.processor.AdminDomainBlockGet(authed, domainBlockID, export) + domainBlock, err := m.processor.AdminDomainBlockGet(c.Request.Context(), authed, domainBlockID, export) if err != nil { l.Debugf("error getting domain block: %s", err) c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) diff --git a/internal/api/client/admin/domainblocksget.go b/internal/api/client/admin/domainblocksget.go index 77a287387..dad8250e0 100644 --- a/internal/api/client/admin/domainblocksget.go +++ b/internal/api/client/admin/domainblocksget.go @@ -81,7 +81,7 @@ func (m *Module) DomainBlocksGETHandler(c *gin.Context) { export = i } - domainBlocks, err := m.processor.AdminDomainBlocksGet(authed, export) + domainBlocks, err := m.processor.AdminDomainBlocksGet(c.Request.Context(), authed, export) if err != nil { l.Debugf("error getting domain blocks: %s", err) c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) diff --git a/internal/api/client/admin/emojicreate.go b/internal/api/client/admin/emojicreate.go index bfdf28249..859933b16 100644 --- a/internal/api/client/admin/emojicreate.go +++ b/internal/api/client/admin/emojicreate.go @@ -111,7 +111,7 @@ func (m *Module) emojiCreatePOSTHandler(c *gin.Context) { return } - mastoEmoji, err := m.processor.AdminEmojiCreate(authed, form) + mastoEmoji, 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()}) diff --git a/internal/api/client/app/appcreate.go b/internal/api/client/app/appcreate.go index 31072f9c8..d233841b0 100644 --- a/internal/api/client/app/appcreate.go +++ b/internal/api/client/app/appcreate.go @@ -101,7 +101,7 @@ func (m *Module) AppsPOSTHandler(c *gin.Context) { return } - mastoApp, err := m.processor.AppCreate(authed, form) + mastoApp, err := m.processor.AppCreate(c.Request.Context(), authed, form) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return diff --git a/internal/api/client/auth/auth_test.go b/internal/api/client/auth/auth_test.go index 48d2a2508..436b01247 100644 --- a/internal/api/client/auth/auth_test.go +++ b/internal/api/client/auth/auth_test.go @@ -120,23 +120,23 @@ func (suite *AuthTestSuite) SetupTest() { } for _, m := range models { - if err := suite.db.CreateTable(m); err != nil { + if err := suite.db.CreateTable(context.Background(),m); err != nil { logrus.Panicf("db connection error: %s", err) } } suite.oauthServer = oauth.New(suite.db, log) - if err := suite.db.Put(suite.testAccount); err != nil { + if err := suite.db.Put(context.Background(),suite.testAccount); err != nil { logrus.Panicf("could not insert test account into db: %s", err) } - if err := suite.db.Put(suite.testUser); err != nil { + if err := suite.db.Put(context.Background(),suite.testUser); err != nil { logrus.Panicf("could not insert test user into db: %s", err) } - if err := suite.db.Put(suite.testClient); err != nil { + if err := suite.db.Put(context.Background(),suite.testClient); err != nil { logrus.Panicf("could not insert test client into db: %s", err) } - if err := suite.db.Put(suite.testApplication); err != nil { + if err := suite.db.Put(context.Background(),suite.testApplication); err != nil { logrus.Panicf("could not insert test application into db: %s", err) } @@ -152,7 +152,7 @@ func (suite *AuthTestSuite) TearDownTest() { >smodel.Application{}, } for _, m := range models { - if err := suite.db.DropTable(m); err != nil { + if err := suite.db.DropTable(context.Background(),m); err != nil { logrus.Panicf("error dropping table: %s", err) } } diff --git a/internal/api/client/auth/authorize.go b/internal/api/client/auth/authorize.go index a10408723..34e53f5ae 100644 --- a/internal/api/client/auth/authorize.go +++ b/internal/api/client/auth/authorize.go @@ -73,7 +73,7 @@ func (m *Module) AuthorizeGETHandler(c *gin.Context) { app := >smodel.Application{ ClientID: clientID, } - if err := m.db.GetWhere([]db.Where{{Key: sessionClientID, Value: app.ClientID}}, app); err != nil { + if err := m.db.GetWhere(c.Request.Context(), []db.Where{{Key: sessionClientID, Value: app.ClientID}}, app); err != nil { m.clearSession(s) c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("no application found for client id %s", clientID)}) return @@ -83,7 +83,7 @@ func (m *Module) AuthorizeGETHandler(c *gin.Context) { user := >smodel.User{ ID: userID, } - if err := m.db.GetByID(user.ID, user); err != nil { + if err := m.db.GetByID(c.Request.Context(), user.ID, user); err != nil { m.clearSession(s) c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return @@ -93,7 +93,7 @@ func (m *Module) AuthorizeGETHandler(c *gin.Context) { ID: user.AccountID, } - if err := m.db.GetByID(acct.ID, acct); err != nil { + if err := m.db.GetByID(c.Request.Context(), acct.ID, acct); err != nil { m.clearSession(s) c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return diff --git a/internal/api/client/auth/callback.go b/internal/api/client/auth/callback.go index a26838aa3..309a8a741 100644 --- a/internal/api/client/auth/callback.go +++ b/internal/api/client/auth/callback.go @@ -19,6 +19,7 @@ package auth import ( + "context" "errors" "fmt" "net" @@ -80,13 +81,13 @@ func (m *Module) CallbackGETHandler(c *gin.Context) { app := >smodel.Application{ ClientID: clientID, } - if err := m.db.GetWhere([]db.Where{{Key: sessionClientID, Value: app.ClientID}}, app); err != nil { + if err := m.db.GetWhere(c.Request.Context(), []db.Where{{Key: sessionClientID, Value: app.ClientID}}, app); err != nil { m.clearSession(s) c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("no application found for client id %s", clientID)}) return } - user, err := m.parseUserFromClaims(claims, net.IP(c.ClientIP()), app.ID) + user, err := m.parseUserFromClaims(c.Request.Context(), claims, net.IP(c.ClientIP()), app.ID) if err != nil { m.clearSession(s) c.JSON(http.StatusForbidden, gin.H{"error": err.Error()}) @@ -103,14 +104,14 @@ func (m *Module) CallbackGETHandler(c *gin.Context) { c.Redirect(http.StatusFound, OauthAuthorizePath) } -func (m *Module) parseUserFromClaims(claims *oidc.Claims, ip net.IP, appID string) (*gtsmodel.User, error) { +func (m *Module) parseUserFromClaims(ctx context.Context, claims *oidc.Claims, ip net.IP, appID string) (*gtsmodel.User, error) { if claims.Email == "" { return nil, errors.New("no email returned in claims") } // see if we already have a user for this email address user := >smodel.User{} - err := m.db.GetWhere([]db.Where{{Key: "email", Value: claims.Email}}, user) + err := m.db.GetWhere(ctx, []db.Where{{Key: "email", Value: claims.Email}}, user) if err == nil { // we do! so we can just return it return user, nil @@ -122,7 +123,7 @@ func (m *Module) parseUserFromClaims(claims *oidc.Claims, ip net.IP, appID strin } // maybe we have an unconfirmed user - err = m.db.GetWhere([]db.Where{{Key: "unconfirmed_email", Value: claims.Email}}, user) + err = m.db.GetWhere(ctx, []db.Where{{Key: "unconfirmed_email", Value: claims.Email}}, user) if err == nil { // user is unconfirmed so return an error return nil, fmt.Errorf("user with email address %s is unconfirmed", claims.Email) @@ -137,7 +138,7 @@ func (m *Module) parseUserFromClaims(claims *oidc.Claims, ip net.IP, appID strin // however, because we trust the OIDC provider, we should now create a user + account with the provided claims // check if the email address is available for use; if it's not there's nothing we can so - if err := m.db.IsEmailAvailable(claims.Email); err != nil { + if err := m.db.IsEmailAvailable(ctx, claims.Email); err != nil { return nil, fmt.Errorf("email %s not available: %s", claims.Email, err) } @@ -180,7 +181,7 @@ func (m *Module) parseUserFromClaims(claims *oidc.Claims, ip net.IP, appID strin // note that for the first iteration, iString is still "" when the check is made, so our first choice // is still the raw username with no integer stuck on the end for i := 1; !found; i = i + 1 { - if err := m.db.IsUsernameAvailable(username + iString); err != nil { + if err := m.db.IsUsernameAvailable(ctx, username + iString); err != nil { if strings.Contains(err.Error(), "db error") { // if there's an actual db error we should return return nil, fmt.Errorf("error checking username availability: %s", err) @@ -209,7 +210,7 @@ func (m *Module) parseUserFromClaims(claims *oidc.Claims, ip net.IP, appID strin password := uuid.NewString() + uuid.NewString() // create the user! this will also create an account and store it in the database so we don't need to do that here - user, err = m.db.NewSignup(username, "", m.config.AccountsConfig.RequireApproval, claims.Email, password, ip, "", appID, claims.EmailVerified, admin) + user, err = m.db.NewSignup(ctx, username, "", m.config.AccountsConfig.RequireApproval, claims.Email, password, ip, "", appID, claims.EmailVerified, admin) if err != nil { return nil, fmt.Errorf("error creating user: %s", err) } diff --git a/internal/api/client/auth/middleware.go b/internal/api/client/auth/middleware.go index a734b2ceb..c1995ca92 100644 --- a/internal/api/client/auth/middleware.go +++ b/internal/api/client/auth/middleware.go @@ -49,7 +49,7 @@ func (m *Module) OauthTokenMiddleware(c *gin.Context) { // fetch user's and account for this user id user := >smodel.User{} - if err := m.db.GetByID(uid, user); err != nil || user == nil { + if err := m.db.GetByID(c.Request.Context(), uid, user); err != nil || user == nil { l.Warnf("no user found for validated uid %s", uid) return } @@ -57,7 +57,7 @@ func (m *Module) OauthTokenMiddleware(c *gin.Context) { l.Tracef("set gin context %s to %+v", oauth.SessionAuthorizedUser, user) acct := >smodel.Account{} - if err := m.db.GetByID(user.AccountID, acct); err != nil || acct == nil { + if err := m.db.GetByID(c.Request.Context(), user.AccountID, acct); err != nil || acct == nil { l.Warnf("no account found for validated user %s", uid) return } @@ -69,7 +69,7 @@ func (m *Module) OauthTokenMiddleware(c *gin.Context) { if cid := ti.GetClientID(); cid != "" { l.Tracef("authenticated client %s with bearer token, scope is %s", cid, ti.GetScope()) app := >smodel.Application{} - if err := m.db.GetWhere([]db.Where{{Key: "client_id", Value: cid}}, app); err != nil { + if err := m.db.GetWhere(c.Request.Context(), []db.Where{{Key: "client_id", Value: cid}}, app); err != nil { l.Tracef("no app found for client %s", cid) } c.Set(oauth.SessionAuthorizedApplication, app) diff --git a/internal/api/client/auth/signin.go b/internal/api/client/auth/signin.go index 543505cbd..6b8bb93db 100644 --- a/internal/api/client/auth/signin.go +++ b/internal/api/client/auth/signin.go @@ -19,6 +19,7 @@ package auth import ( + "context" "errors" "net/http" @@ -74,7 +75,7 @@ func (m *Module) SignInPOSTHandler(c *gin.Context) { } l.Tracef("parsed form: %+v", form) - userid, err := m.ValidatePassword(form.Email, form.Password) + userid, err := m.ValidatePassword(c.Request.Context(), form.Email, form.Password) if err != nil { c.String(http.StatusForbidden, err.Error()) m.clearSession(s) @@ -96,7 +97,7 @@ func (m *Module) SignInPOSTHandler(c *gin.Context) { // The goal is to authenticate the password against the one for that email // address stored in the database. If OK, we return the userid (a ulid) for that user, // so that it can be used in further Oauth flows to generate a token/retreieve an oauth client from the db. -func (m *Module) ValidatePassword(email string, password string) (userid string, err error) { +func (m *Module) ValidatePassword(ctx context.Context, email string, password string) (userid string, err error) { l := m.log.WithField("func", "ValidatePassword") // make sure an email/password was provided and bail if not @@ -108,7 +109,7 @@ func (m *Module) ValidatePassword(email string, password string) (userid string, // first we select the user from the database based on email address, bail if no user found for that email gtsUser := >smodel.User{} - if err := m.db.GetWhere([]db.Where{{Key: "email", Value: email}}, gtsUser); err != nil { + if err := m.db.GetWhere(ctx, []db.Where{{Key: "email", Value: email}}, gtsUser); err != nil { l.Debugf("user %s was not retrievable from db during oauth authorization attempt: %s", email, err) return incorrectPassword() } diff --git a/internal/api/client/blocks/blocksget.go b/internal/api/client/blocks/blocksget.go index 65c11ea1a..b6c9c39e1 100644 --- a/internal/api/client/blocks/blocksget.go +++ b/internal/api/client/blocks/blocksget.go @@ -117,7 +117,7 @@ func (m *Module) BlocksGETHandler(c *gin.Context) { limit = int(i) } - resp, errWithCode := m.processor.BlocksGet(authed, maxID, sinceID, limit) + resp, errWithCode := m.processor.BlocksGet(c.Request.Context(), authed, maxID, sinceID, limit) if errWithCode != nil { l.Debugf("error from processor BlocksGet: %s", errWithCode) c.JSON(errWithCode.Code(), gin.H{"error": errWithCode.Safe()}) diff --git a/internal/api/client/favourites/favouritesget.go b/internal/api/client/favourites/favouritesget.go index 76eb921e0..6984ea754 100644 --- a/internal/api/client/favourites/favouritesget.go +++ b/internal/api/client/favourites/favouritesget.go @@ -43,7 +43,7 @@ func (m *Module) FavouritesGETHandler(c *gin.Context) { limit = int(i) } - resp, errWithCode := m.processor.FavedTimelineGet(authed, maxID, minID, limit) + resp, errWithCode := m.processor.FavedTimelineGet(c.Request.Context(), authed, maxID, minID, limit) if errWithCode != nil { l.Debugf("error from processor FavedTimelineGet: %s", errWithCode) c.JSON(errWithCode.Code(), gin.H{"error": errWithCode.Safe()}) diff --git a/internal/api/client/fileserver/fileserver.go b/internal/api/client/fileserver/fileserver.go index 08e6abb62..61286c17a 100644 --- a/internal/api/client/fileserver/fileserver.go +++ b/internal/api/client/fileserver/fileserver.go @@ -25,8 +25,6 @@ import ( "github.com/sirupsen/logrus" "github.com/superseriousbusiness/gotosocial/internal/api" "github.com/superseriousbusiness/gotosocial/internal/config" - "github.com/superseriousbusiness/gotosocial/internal/db" - "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" "github.com/superseriousbusiness/gotosocial/internal/processing" "github.com/superseriousbusiness/gotosocial/internal/router" ) @@ -66,17 +64,3 @@ func (m *FileServer) Route(s router.Router) error { s.AttachHandler(http.MethodGet, fmt.Sprintf("%s/:%s/:%s/:%s/:%s", m.storageBase, AccountIDKey, MediaTypeKey, MediaSizeKey, FileNameKey), m.ServeFile) return nil } - -// CreateTables populates necessary tables in the given DB -func (m *FileServer) CreateTables(db db.DB) error { - models := []interface{}{ - >smodel.MediaAttachment{}, - } - - for _, m := range models { - if err := db.CreateTable(m); err != nil { - return fmt.Errorf("error creating table: %s", err) - } - } - return nil -} diff --git a/internal/api/client/fileserver/servefile.go b/internal/api/client/fileserver/servefile.go index 1339fbac3..130a16c4f 100644 --- a/internal/api/client/fileserver/servefile.go +++ b/internal/api/client/fileserver/servefile.go @@ -78,7 +78,7 @@ func (m *FileServer) ServeFile(c *gin.Context) { return } - content, err := m.processor.FileGet(authed, &model.GetContentRequestForm{ + content, err := m.processor.FileGet(c.Request.Context(), authed, &model.GetContentRequestForm{ AccountID: accountID, MediaType: mediaType, MediaSize: mediaSize, diff --git a/internal/api/client/followrequest/accept.go b/internal/api/client/followrequest/accept.go index bb2910c8f..3dba7673f 100644 --- a/internal/api/client/followrequest/accept.go +++ b/internal/api/client/followrequest/accept.go @@ -48,7 +48,7 @@ func (m *Module) FollowRequestAcceptPOSTHandler(c *gin.Context) { return } - r, errWithCode := m.processor.FollowRequestAccept(authed, originAccountID) + r, errWithCode := m.processor.FollowRequestAccept(c.Request.Context(), authed, originAccountID) if errWithCode != nil { l.Debug(errWithCode.Error()) c.JSON(errWithCode.Code(), gin.H{"error": errWithCode.Safe()}) diff --git a/internal/api/client/followrequest/get.go b/internal/api/client/followrequest/get.go index 3f02ee02a..47e1d74ba 100644 --- a/internal/api/client/followrequest/get.go +++ b/internal/api/client/followrequest/get.go @@ -41,7 +41,7 @@ func (m *Module) FollowRequestGETHandler(c *gin.Context) { return } - accts, errWithCode := m.processor.FollowRequestsGet(authed) + accts, errWithCode := m.processor.FollowRequestsGet(c.Request.Context(), authed) if errWithCode != nil { c.JSON(errWithCode.Code(), gin.H{"error": errWithCode.Safe()}) return diff --git a/internal/api/client/instance/instanceget.go b/internal/api/client/instance/instanceget.go index 0d53edadb..0a6d17153 100644 --- a/internal/api/client/instance/instanceget.go +++ b/internal/api/client/instance/instanceget.go @@ -31,7 +31,7 @@ import ( func (m *Module) InstanceInformationGETHandler(c *gin.Context) { l := m.log.WithField("func", "InstanceInformationGETHandler") - instance, err := m.processor.InstanceGet(m.config.Host) + instance, err := m.processor.InstanceGet(c.Request.Context(), m.config.Host) if err != nil { l.Debugf("error getting instance from processor: %s", err) c.JSON(http.StatusInternalServerError, gin.H{"error": "internal server error"}) diff --git a/internal/api/client/instance/instancepatch.go b/internal/api/client/instance/instancepatch.go index 3620f6044..fa37ccd8e 100644 --- a/internal/api/client/instance/instancepatch.go +++ b/internal/api/client/instance/instancepatch.go @@ -116,7 +116,7 @@ func (m *Module) InstanceUpdatePATCHHandler(c *gin.Context) { return } - i, errWithCode := m.processor.InstancePatch(form) + i, errWithCode := m.processor.InstancePatch(c.Request.Context(), form) if errWithCode != nil { l.Debugf("error with instance patch request: %s", errWithCode.Error()) c.JSON(errWithCode.Code(), gin.H{"error": errWithCode.Safe()}) diff --git a/internal/api/client/media/media.go b/internal/api/client/media/media.go index 05058e2e2..1e9e8fdaa 100644 --- a/internal/api/client/media/media.go +++ b/internal/api/client/media/media.go @@ -19,14 +19,11 @@ package media import ( - "fmt" "net/http" "github.com/sirupsen/logrus" "github.com/superseriousbusiness/gotosocial/internal/api" "github.com/superseriousbusiness/gotosocial/internal/config" - "github.com/superseriousbusiness/gotosocial/internal/db" - "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" "github.com/superseriousbusiness/gotosocial/internal/processing" "github.com/superseriousbusiness/gotosocial/internal/router" ) @@ -63,17 +60,3 @@ func (m *Module) Route(s router.Router) error { s.AttachHandler(http.MethodPut, BasePathWithID, m.MediaPUTHandler) return nil } - -// CreateTables populates necessary tables in the given DB -func (m *Module) CreateTables(db db.DB) error { - models := []interface{}{ - >smodel.MediaAttachment{}, - } - - for _, m := range models { - if err := db.CreateTable(m); err != nil { - return fmt.Errorf("error creating table: %s", err) - } - } - return nil -} diff --git a/internal/api/client/media/mediacreate.go b/internal/api/client/media/mediacreate.go index f41d4568f..58d076ea6 100644 --- a/internal/api/client/media/mediacreate.go +++ b/internal/api/client/media/mediacreate.go @@ -108,7 +108,7 @@ func (m *Module) MediaCreatePOSTHandler(c *gin.Context) { } l.Debug("calling processor media create func") - mastoAttachment, err := m.processor.MediaCreate(authed, form) + mastoAttachment, 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()}) diff --git a/internal/api/client/media/mediaget.go b/internal/api/client/media/mediaget.go index 17c5a090b..5fd7856e9 100644 --- a/internal/api/client/media/mediaget.go +++ b/internal/api/client/media/mediaget.go @@ -75,7 +75,7 @@ func (m *Module) MediaGETHandler(c *gin.Context) { return } - attachment, errWithCode := m.processor.MediaGet(authed, attachmentID) + attachment, errWithCode := m.processor.MediaGet(c.Request.Context(), authed, attachmentID) if errWithCode != nil { c.JSON(errWithCode.Code(), gin.H{"error": errWithCode.Safe()}) return diff --git a/internal/api/client/media/mediaupdate.go b/internal/api/client/media/mediaupdate.go index 0ceb01f82..3af19297f 100644 --- a/internal/api/client/media/mediaupdate.go +++ b/internal/api/client/media/mediaupdate.go @@ -122,7 +122,7 @@ func (m *Module) MediaPUTHandler(c *gin.Context) { return } - attachment, errWithCode := m.processor.MediaUpdate(authed, attachmentID, &form) + attachment, errWithCode := m.processor.MediaUpdate(c.Request.Context(), authed, attachmentID, &form) if errWithCode != nil { c.JSON(errWithCode.Code(), gin.H{"error": errWithCode.Safe()}) return diff --git a/internal/api/client/notification/notificationsget.go b/internal/api/client/notification/notificationsget.go index a30674750..81e8a6890 100644 --- a/internal/api/client/notification/notificationsget.go +++ b/internal/api/client/notification/notificationsget.go @@ -68,7 +68,7 @@ func (m *Module) NotificationsGETHandler(c *gin.Context) { sinceID = sinceIDString } - notifs, errWithCode := m.processor.NotificationsGet(authed, limit, maxID, sinceID) + notifs, errWithCode := m.processor.NotificationsGet(c.Request.Context(), authed, limit, maxID, sinceID) if errWithCode != nil { l.Debugf("error processing notifications get: %s", errWithCode.Error()) c.JSON(errWithCode.Code(), gin.H{"error": errWithCode.Safe()}) diff --git a/internal/api/client/search/searchget.go b/internal/api/client/search/searchget.go index faa227719..848915274 100644 --- a/internal/api/client/search/searchget.go +++ b/internal/api/client/search/searchget.go @@ -164,7 +164,7 @@ func (m *Module) SearchGETHandler(c *gin.Context) { Following: following, } - results, errWithCode := m.processor.SearchGet(authed, searchQuery) + results, errWithCode := m.processor.SearchGet(c.Request.Context(), authed, searchQuery) if errWithCode != nil { l.Debugf("error searching: %s", errWithCode.Error()) c.JSON(errWithCode.Code(), gin.H{"error": errWithCode.Safe()}) diff --git a/internal/api/client/status/statusboost.go b/internal/api/client/status/statusboost.go index 5aa7989bc..094e56ac0 100644 --- a/internal/api/client/status/statusboost.go +++ b/internal/api/client/status/statusboost.go @@ -87,7 +87,7 @@ func (m *Module) StatusBoostPOSTHandler(c *gin.Context) { return } - mastoStatus, errWithCode := m.processor.StatusBoost(authed, targetStatusID) + mastoStatus, 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()}) diff --git a/internal/api/client/status/statusboostedby.go b/internal/api/client/status/statusboostedby.go index 260e21642..908c3ff10 100644 --- a/internal/api/client/status/statusboostedby.go +++ b/internal/api/client/status/statusboostedby.go @@ -84,7 +84,7 @@ func (m *Module) StatusBoostedByGETHandler(c *gin.Context) { return } - mastoAccounts, err := m.processor.StatusBoostedBy(authed, targetStatusID) + mastoAccounts, 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"}) diff --git a/internal/api/client/status/statuscontext.go b/internal/api/client/status/statuscontext.go index 6e28b004e..90fcb9608 100644 --- a/internal/api/client/status/statuscontext.go +++ b/internal/api/client/status/statuscontext.go @@ -86,7 +86,7 @@ func (m *Module) StatusContextGETHandler(c *gin.Context) { return } - statusContext, errWithCode := m.processor.StatusGetContext(authed, targetStatusID) + statusContext, errWithCode := m.processor.StatusGetContext(c.Request.Context(), authed, targetStatusID) if errWithCode != nil { l.Debugf("error getting status context: %s", errWithCode.Error()) c.JSON(errWithCode.Code(), gin.H{"error": errWithCode.Safe()}) diff --git a/internal/api/client/status/statuscreate.go b/internal/api/client/status/statuscreate.go index 2007ba80f..09fc47b5b 100644 --- a/internal/api/client/status/statuscreate.go +++ b/internal/api/client/status/statuscreate.go @@ -101,7 +101,7 @@ func (m *Module) StatusCreatePOSTHandler(c *gin.Context) { return } - mastoStatus, err := m.processor.StatusCreate(authed, form) + mastoStatus, 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"}) diff --git a/internal/api/client/status/statuscreate_test.go b/internal/api/client/status/statuscreate_test.go index 33912397e..097f268f2 100644 --- a/internal/api/client/status/statuscreate_test.go +++ b/internal/api/client/status/statuscreate_test.go @@ -19,6 +19,7 @@ package status_test import ( + "context" "encoding/json" "fmt" "io/ioutil" @@ -128,7 +129,7 @@ func (suite *StatusCreateTestSuite) TestPostNewStatus() { }, statusReply.Tags[0]) gtsTag := >smodel.Tag{} - err = suite.db.GetWhere([]db.Where{{Key: "name", Value: "helloworld"}}, gtsTag) + err = suite.db.GetWhere(context.Background(), []db.Where{{Key: "name", Value: "helloworld"}}, gtsTag) assert.NoError(suite.T(), err) assert.Equal(suite.T(), statusReply.Account.ID, gtsTag.FirstSeenFromAccountID) } @@ -323,11 +324,11 @@ func (suite *StatusCreateTestSuite) TestAttachNewMediaSuccess() { // get the updated media attachment from the database gtsAttachment := >smodel.MediaAttachment{} - err = suite.db.GetByID(statusResponse.MediaAttachments[0].ID, gtsAttachment) + err = suite.db.GetByID(context.Background(), statusResponse.MediaAttachments[0].ID, gtsAttachment) assert.NoError(suite.T(), err) // convert it to a masto attachment - gtsAttachmentAsMasto, err := suite.tc.AttachmentToMasto(gtsAttachment) + gtsAttachmentAsMasto, err := suite.tc.AttachmentToMasto(context.Background(), gtsAttachment) assert.NoError(suite.T(), err) // compare it with what we have now diff --git a/internal/api/client/status/statusdelete.go b/internal/api/client/status/statusdelete.go index 257280ce0..9a67c45ba 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(authed, targetStatusID) + mastoStatus, 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"}) diff --git a/internal/api/client/status/statusfave.go b/internal/api/client/status/statusfave.go index a76acf3d9..94a8f9380 100644 --- a/internal/api/client/status/statusfave.go +++ b/internal/api/client/status/statusfave.go @@ -83,7 +83,7 @@ func (m *Module) StatusFavePOSTHandler(c *gin.Context) { return } - mastoStatus, err := m.processor.StatusFave(authed, targetStatusID) + mastoStatus, 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"}) diff --git a/internal/api/client/status/statusfavedby.go b/internal/api/client/status/statusfavedby.go index a5d6e7c58..7b8e19e20 100644 --- a/internal/api/client/status/statusfavedby.go +++ b/internal/api/client/status/statusfavedby.go @@ -84,7 +84,7 @@ func (m *Module) StatusFavedByGETHandler(c *gin.Context) { return } - mastoAccounts, err := m.processor.StatusFavedBy(authed, targetStatusID) + mastoAccounts, 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"}) diff --git a/internal/api/client/status/statusget.go b/internal/api/client/status/statusget.go index bcca010f5..39668288f 100644 --- a/internal/api/client/status/statusget.go +++ b/internal/api/client/status/statusget.go @@ -83,7 +83,7 @@ func (m *Module) StatusGETHandler(c *gin.Context) { return } - mastoStatus, err := m.processor.StatusGet(authed, targetStatusID) + mastoStatus, 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"}) diff --git a/internal/api/client/status/statusunboost.go b/internal/api/client/status/statusunboost.go index dc42e3b62..8bb28fa50 100644 --- a/internal/api/client/status/statusunboost.go +++ b/internal/api/client/status/statusunboost.go @@ -84,7 +84,7 @@ func (m *Module) StatusUnboostPOSTHandler(c *gin.Context) { return } - mastoStatus, errWithCode := m.processor.StatusUnboost(authed, targetStatusID) + mastoStatus, 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()}) diff --git a/internal/api/client/status/statusunfave.go b/internal/api/client/status/statusunfave.go index 80eb87acf..5a1074ca2 100644 --- a/internal/api/client/status/statusunfave.go +++ b/internal/api/client/status/statusunfave.go @@ -83,7 +83,7 @@ func (m *Module) StatusUnfavePOSTHandler(c *gin.Context) { return } - mastoStatus, err := m.processor.StatusUnfave(authed, targetStatusID) + mastoStatus, 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"}) diff --git a/internal/api/client/streaming/stream.go b/internal/api/client/streaming/stream.go index 626f1ff41..fa210e8d8 100644 --- a/internal/api/client/streaming/stream.go +++ b/internal/api/client/streaming/stream.go @@ -122,7 +122,7 @@ func (m *Module) StreamGETHandler(c *gin.Context) { } // make sure a valid token has been provided and obtain the associated account - account, err := m.processor.AuthorizeStreamingRequest(accessToken) + account, err := m.processor.AuthorizeStreamingRequest(c.Request.Context(), accessToken) if err != nil { c.JSON(http.StatusUnauthorized, gin.H{"error": "could not authorize with given token"}) return @@ -147,7 +147,7 @@ func (m *Module) StreamGETHandler(c *gin.Context) { defer conn.Close() // whatever happens, when we leave this function we want to close the websocket connection // inform the processor that we have a new connection and want a stream for it - stream, errWithCode := m.processor.OpenStreamForAccount(account, streamType) + stream, errWithCode := m.processor.OpenStreamForAccount(c.Request.Context(), account, streamType) if errWithCode != nil { c.JSON(errWithCode.Code(), errWithCode.Safe()) return diff --git a/internal/api/client/timeline/home.go b/internal/api/client/timeline/home.go index a6e64f384..6df4b29d0 100644 --- a/internal/api/client/timeline/home.go +++ b/internal/api/client/timeline/home.go @@ -153,7 +153,7 @@ func (m *Module) HomeTimelineGETHandler(c *gin.Context) { local = i } - resp, errWithCode := m.processor.HomeTimelineGet(authed, maxID, sinceID, minID, limit, local) + resp, errWithCode := m.processor.HomeTimelineGet(c.Request.Context(), authed, maxID, sinceID, minID, limit, local) if errWithCode != nil { l.Debugf("error from processor HomeTimelineGet: %s", errWithCode) c.JSON(errWithCode.Code(), gin.H{"error": errWithCode.Safe()}) diff --git a/internal/api/client/timeline/public.go b/internal/api/client/timeline/public.go index 178fcd7f1..8c8c9f120 100644 --- a/internal/api/client/timeline/public.go +++ b/internal/api/client/timeline/public.go @@ -153,7 +153,7 @@ func (m *Module) PublicTimelineGETHandler(c *gin.Context) { local = i } - resp, errWithCode := m.processor.PublicTimelineGet(authed, maxID, sinceID, minID, limit, local) + resp, errWithCode := m.processor.PublicTimelineGet(c.Request.Context(), authed, maxID, sinceID, minID, limit, local) if errWithCode != nil { l.Debugf("error from processor PublicTimelineGet: %s", errWithCode) c.JSON(errWithCode.Code(), gin.H{"error": errWithCode.Safe()}) diff --git a/internal/api/s2s/nodeinfo/nodeinfoget.go b/internal/api/s2s/nodeinfo/nodeinfoget.go index a54c8b190..c362e1d2e 100644 --- a/internal/api/s2s/nodeinfo/nodeinfoget.go +++ b/internal/api/s2s/nodeinfo/nodeinfoget.go @@ -33,7 +33,7 @@ func (m *Module) NodeInfoGETHandler(c *gin.Context) { "user-agent": c.Request.UserAgent(), }) - ni, err := m.processor.GetNodeInfo(c.Request) + ni, err := m.processor.GetNodeInfo(c.Request.Context(), c.Request) if err != nil { l.Debugf("error with get node info request: %s", err) c.JSON(err.Code(), err.Safe()) diff --git a/internal/api/s2s/nodeinfo/wellknownget.go b/internal/api/s2s/nodeinfo/wellknownget.go index 614d2a9c6..fd2c84408 100644 --- a/internal/api/s2s/nodeinfo/wellknownget.go +++ b/internal/api/s2s/nodeinfo/wellknownget.go @@ -33,7 +33,7 @@ func (m *Module) NodeInfoWellKnownGETHandler(c *gin.Context) { "user-agent": c.Request.UserAgent(), }) - niRel, err := m.processor.GetNodeInfoRel(c.Request) + niRel, err := m.processor.GetNodeInfoRel(c.Request.Context(), c.Request) if err != nil { l.Debugf("error with get node info rel request: %s", err) c.JSON(err.Code(), err.Safe()) diff --git a/internal/api/s2s/user/userget_test.go b/internal/api/s2s/user/userget_test.go index ab0015c57..29cc0e0d8 100644 --- a/internal/api/s2s/user/userget_test.go +++ b/internal/api/s2s/user/userget_test.go @@ -105,7 +105,7 @@ func (suite *UserGetTestSuite) TestGetUser() { // convert person to account // since this account is already known, we should get a pretty full model of it from the conversion - a, err := suite.tc.ASRepresentationToAccount(person, false) + a, err := suite.tc.ASRepresentationToAccount(context.Background(), person, false) assert.NoError(suite.T(), err) assert.EqualValues(suite.T(), targetAccount.Username, a.Username) } diff --git a/internal/api/security/signaturecheck.go b/internal/api/security/signaturecheck.go index 88b0b4dff..71e539e96 100644 --- a/internal/api/security/signaturecheck.go +++ b/internal/api/security/signaturecheck.go @@ -31,7 +31,7 @@ func (m *Module) SignatureCheck(c *gin.Context) { // we managed to parse the url! // if the domain is blocked we want to bail as early as possible - blocked, err := m.db.IsURIBlocked(requestingPublicKeyID) + blocked, err := m.db.IsURIBlocked(c.Request.Context(), requestingPublicKeyID) if err != nil { l.Errorf("could not tell if domain %s was blocked or not: %s", requestingPublicKeyID.Host, err) c.AbortWithStatus(http.StatusInternalServerError) diff --git a/internal/cliactions/admin/account/account.go b/internal/cliactions/admin/account/account.go index 0ae7f32de..82058fe25 100644 --- a/internal/cliactions/admin/account/account.go +++ b/internal/cliactions/admin/account/account.go @@ -65,7 +65,7 @@ var Create cliactions.GTSAction = func(ctx context.Context, c *config.Config, lo return err } - _, err = dbConn.NewSignup(username, "", false, email, password, nil, "", "", false, false) + _, err = dbConn.NewSignup(ctx, username, "", false, email, password, nil, "", "", false, false) if err != nil { return err } @@ -88,20 +88,20 @@ var Confirm cliactions.GTSAction = func(ctx context.Context, c *config.Config, l return err } - a, err := dbConn.GetLocalAccountByUsername(username) + a, err := dbConn.GetLocalAccountByUsername(ctx, username) if err != nil { return err } u := >smodel.User{} - if err := dbConn.GetWhere([]db.Where{{Key: "account_id", Value: a.ID}}, u); err != nil { + if err := dbConn.GetWhere(ctx, []db.Where{{Key: "account_id", Value: a.ID}}, u); err != nil { return err } u.Approved = true u.Email = u.UnconfirmedEmail u.ConfirmedAt = time.Now() - if err := dbConn.UpdateByID(u.ID, u); err != nil { + if err := dbConn.UpdateByID(ctx, u.ID, u); err != nil { return err } @@ -123,17 +123,17 @@ var Promote cliactions.GTSAction = func(ctx context.Context, c *config.Config, l return err } - a, err := dbConn.GetLocalAccountByUsername(username) + a, err := dbConn.GetLocalAccountByUsername(ctx, username) if err != nil { return err } u := >smodel.User{} - if err := dbConn.GetWhere([]db.Where{{Key: "account_id", Value: a.ID}}, u); err != nil { + if err := dbConn.GetWhere(ctx, []db.Where{{Key: "account_id", Value: a.ID}}, u); err != nil { return err } u.Admin = true - if err := dbConn.UpdateByID(u.ID, u); err != nil { + if err := dbConn.UpdateByID(ctx, u.ID, u); err != nil { return err } @@ -155,17 +155,17 @@ var Demote cliactions.GTSAction = func(ctx context.Context, c *config.Config, lo return err } - a, err := dbConn.GetLocalAccountByUsername(username) + a, err := dbConn.GetLocalAccountByUsername(ctx, username) if err != nil { return err } u := >smodel.User{} - if err := dbConn.GetWhere([]db.Where{{Key: "account_id", Value: a.ID}}, u); err != nil { + if err := dbConn.GetWhere(ctx, []db.Where{{Key: "account_id", Value: a.ID}}, u); err != nil { return err } u.Admin = false - if err := dbConn.UpdateByID(u.ID, u); err != nil { + if err := dbConn.UpdateByID(ctx, u.ID, u); err != nil { return err } @@ -187,17 +187,17 @@ var Disable cliactions.GTSAction = func(ctx context.Context, c *config.Config, l return err } - a, err := dbConn.GetLocalAccountByUsername(username) + a, err := dbConn.GetLocalAccountByUsername(ctx, username) if err != nil { return err } u := >smodel.User{} - if err := dbConn.GetWhere([]db.Where{{Key: "account_id", Value: a.ID}}, u); err != nil { + if err := dbConn.GetWhere(ctx, []db.Where{{Key: "account_id", Value: a.ID}}, u); err != nil { return err } u.Disabled = true - if err := dbConn.UpdateByID(u.ID, u); err != nil { + if err := dbConn.UpdateByID(ctx, u.ID, u); err != nil { return err } @@ -233,13 +233,13 @@ var Password cliactions.GTSAction = func(ctx context.Context, c *config.Config, return err } - a, err := dbConn.GetLocalAccountByUsername(username) + a, err := dbConn.GetLocalAccountByUsername(ctx, username) if err != nil { return err } u := >smodel.User{} - if err := dbConn.GetWhere([]db.Where{{Key: "account_id", Value: a.ID}}, u); err != nil { + if err := dbConn.GetWhere(ctx, []db.Where{{Key: "account_id", Value: a.ID}}, u); err != nil { return err } @@ -250,7 +250,7 @@ var Password cliactions.GTSAction = func(ctx context.Context, c *config.Config, u.EncryptedPassword = string(pw) - if err := dbConn.UpdateByID(u.ID, u); err != nil { + if err := dbConn.UpdateByID(ctx, u.ID, u); err != nil { return err } diff --git a/internal/cliactions/server/server.go b/internal/cliactions/server/server.go index 72c6cfadf..90f2badae 100644 --- a/internal/cliactions/server/server.go +++ b/internal/cliactions/server/server.go @@ -85,22 +85,22 @@ var Start cliactions.GTSAction = func(ctx context.Context, c *config.Config, log } for _, m := range models { - if err := dbService.CreateTable(m); err != nil { + if err := dbService.CreateTable(ctx, m); err != nil { return fmt.Errorf("table creation error: %s", err) } } - if err := dbService.CreateInstanceAccount(); err != nil { + if err := dbService.CreateInstanceAccount(ctx); err != nil { return fmt.Errorf("error creating instance account: %s", err) } - if err := dbService.CreateInstanceInstance(); err != nil { + if err := dbService.CreateInstanceInstance(ctx); err != nil { return fmt.Errorf("error creating instance instance: %s", err) } federatingDB := federatingdb.New(dbService, c, log) - router, err := router.New(c, dbService, log) + router, err := router.New(ctx, c, dbService, log) if err != nil { return fmt.Errorf("error creating router: %s", err) } diff --git a/internal/db/basic.go b/internal/db/basic.go index d48ffb018..97023a493 100644 --- a/internal/db/basic.go +++ b/internal/db/basic.go @@ -30,10 +30,6 @@ type Basic interface { // For implementations that don't use tables, this can just return nil. DropTable(ctx context.Context, i interface{}) Error - // RegisterTable registers a table for use in many2many relations. - // For implementations that don't use tables, or many2many relations, this can just return nil. - RegisterTable(ctx context.Context, i interface{}) Error - // Stop should stop and close the database connection cleanly, returning an error if this is not possible. // If the database implementation doesn't need to be stopped, this can just return nil. Stop(ctx context.Context) Error diff --git a/internal/db/pg/admin.go b/internal/db/pg/admin.go index 6319ba273..590b89894 100644 --- a/internal/db/pg/admin.go +++ b/internal/db/pg/admin.go @@ -22,13 +22,13 @@ import ( "context" "crypto/rand" "crypto/rsa" + "database/sql" "fmt" "net" "net/mail" "strings" "time" - "github.com/go-pg/pg/v10" "github.com/sirupsen/logrus" "github.com/superseriousbusiness/gotosocial/internal/config" "github.com/superseriousbusiness/gotosocial/internal/db" @@ -48,22 +48,31 @@ type adminDB struct { func (a *adminDB) IsUsernameAvailable(ctx context.Context, username string) db.Error { // if no error we fail because it means we found something - // if error but it's not pg.ErrNoRows then we fail // if err is pg.ErrNoRows we're good, we found nothing so continue - if err := a.conn. + // if error but it's not sql.ErrNoRows then we fail + q := a.conn. NewSelect(). Model(>smodel.Account{}). Where("username = ?", username). - Where("domain = ?", nil). - Scan(ctx); err == nil { + Where("domain = ?", nil) + + err := q.Scan(ctx) + + if err == nil { + // we got something, not good return fmt.Errorf("username %s already in use", username) - } else if err != pg.ErrNoRows { - return fmt.Errorf("db error: %s", err) } - return nil + + if err == sql.ErrNoRows { + // no entries, we're happy + return nil + } + + // another type of error occurred + return processErrorResponse(err) } -func (a *adminDB) IsEmailAvailable(email string) db.Error { +func (a *adminDB) IsEmailAvailable(ctx context.Context, email string) db.Error { // parse the domain from the email m, err := mail.ParseAddress(email) if err != nil { @@ -72,26 +81,34 @@ func (a *adminDB) IsEmailAvailable(email string) db.Error { domain := strings.Split(m.Address, "@")[1] // domain will always be the second part after @ // check if the email domain is blocked - if err := a.conn.Model(>smodel.EmailDomainBlock{}).Where("domain = ?", domain).Select(); err == nil { + if err := a.conn. + NewSelect(). + Model(>smodel.EmailDomainBlock{}). + Where("domain = ?", domain). + Scan(ctx); err == nil { // fail because we found something return fmt.Errorf("email domain %s is blocked", domain) - } else if err != pg.ErrNoRows { - // fail because we got an unexpected error - return fmt.Errorf("db error: %s", err) + } else if err != sql.ErrNoRows { + return processErrorResponse(err) } // check if this email is associated with a user already - if err := a.conn.Model(>smodel.User{}).Where("email = ?", email).WhereOr("unconfirmed_email = ?", email).Select(); err == nil { + if err := a.conn. + NewSelect(). + Model(>smodel.User{}). + Where("email = ?", email). + WhereOr("unconfirmed_email = ?", email). + Scan(ctx); err == nil { // fail because we found something return fmt.Errorf("email %s already in use", email) - } else if err != pg.ErrNoRows { - // fail because we got an unexpected error - return fmt.Errorf("db error: %s", err) + } else if err != sql.ErrNoRows { + return processErrorResponse(err) } + return nil } -func (a *adminDB) NewSignup(username string, reason string, requireApproval bool, email string, password string, signUpIP net.IP, locale string, appID string, emailVerified bool, admin bool) (*gtsmodel.User, db.Error) { +func (a *adminDB) NewSignup(ctx context.Context, username string, reason string, requireApproval bool, email string, password string, signUpIP net.IP, locale string, appID string, emailVerified bool, admin bool) (*gtsmodel.User, db.Error) { key, err := rsa.GenerateKey(rand.Reader, 2048) if err != nil { a.log.Errorf("error creating new rsa key: %s", err) @@ -100,13 +117,12 @@ func (a *adminDB) NewSignup(username string, reason string, requireApproval bool // if something went wrong while creating a user, we might already have an account, so check here first... acct := >smodel.Account{} - err = a.conn.Model(acct).Where("username = ?", username).Where("? IS NULL", pg.Ident("domain")).Select() + err = a.conn.NewSelect(). + Model(acct). + Where("username = ?", username). + Where("? IS NULL", bun.Ident("domain")). + Scan(ctx) if err != nil { - // there's been an actual error - if err != pg.ErrNoRows { - return nil, fmt.Errorf("db error checking existence of account: %s", err) - } - // we just don't have an account yet create one newAccountURIs := util.GenerateURIsForAccount(username, a.config.Protocol, a.config.Host) newAccountID, err := id.NewRandomULID() @@ -131,7 +147,10 @@ func (a *adminDB) NewSignup(username string, reason string, requireApproval bool FollowingURI: newAccountURIs.FollowingURI, FeaturedCollectionURI: newAccountURIs.CollectionURI, } - if _, err = a.conn.Model(acct).Insert(); err != nil { + if _, err = a.conn. + NewInsert(). + Model(acct). + Exec(ctx); err != nil { return nil, err } } @@ -167,15 +186,33 @@ func (a *adminDB) NewSignup(username string, reason string, requireApproval bool u.Moderator = true } - if _, err = a.conn.Model(u).Insert(); err != nil { + if _, err = a.conn. + NewInsert(). + Model(u). + Exec(ctx); err != nil { return nil, err } return u, nil } -func (a *adminDB) CreateInstanceAccount() db.Error { +func (a *adminDB) CreateInstanceAccount(ctx context.Context) db.Error { username := a.config.Host + + // check if instance account already exists + existsQ := a.conn. + NewSelect(). + Model(>smodel.Account{}). + Where("username = ?", username). + Where("? IS NULL", bun.Ident("domain")) + count, err := existsQ.Count(ctx) + if err != nil && count == 1 { + a.log.Infof("instance account %s already exists", username) + return nil + } else if err != sql.ErrNoRows { + return processErrorResponse(err) + } + key, err := rsa.GenerateKey(rand.Reader, 2048) if err != nil { a.log.Errorf("error creating new rsa key: %s", err) @@ -204,19 +241,36 @@ func (a *adminDB) CreateInstanceAccount() db.Error { FollowingURI: newAccountURIs.FollowingURI, FeaturedCollectionURI: newAccountURIs.CollectionURI, } - inserted, err := a.conn.Model(acct).Where("username = ?", username).SelectOrInsert() - if err != nil { + + insertQ := a.conn. + NewInsert(). + Model(acct) + + if _, err := insertQ.Exec(ctx); err != nil { return err } - if inserted { - a.log.Infof("created instance account %s with id %s", username, acct.ID) - } else { - a.log.Infof("instance account %s already exists with id %s", username, acct.ID) - } + + a.log.Infof("instance account CREATED with id %s", username, acct.ID) return nil } -func (a *adminDB) CreateInstanceInstance() db.Error { +func (a *adminDB) CreateInstanceInstance(ctx context.Context) db.Error { + domain := a.config.Host + + // check if instance entry already exists + existsQ := a.conn. + NewSelect(). + Model(>smodel.Instance{}). + Where("domain = ?", domain) + + count, err := existsQ.Count(ctx) + if err != nil && count == 1 { + a.log.Infof("instance instance %s already exists", domain) + return nil + } else if err != sql.ErrNoRows { + return processErrorResponse(err) + } + iID, err := id.NewRandomULID() if err != nil { return err @@ -224,18 +278,18 @@ func (a *adminDB) CreateInstanceInstance() db.Error { i := >smodel.Instance{ ID: iID, - Domain: a.config.Host, - Title: a.config.Host, + Domain: domain, + Title: domain, URI: fmt.Sprintf("%s://%s", a.config.Protocol, a.config.Host), } - inserted, err := a.conn.Model(i).Where("domain = ?", a.config.Host).SelectOrInsert() - if err != nil { + + insertQ := a.conn. + NewInsert(). + Model(i) + + if _, err := insertQ.Exec(ctx); err != nil { return err } - if inserted { - a.log.Infof("created instance instance %s with id %s", a.config.Host, i.ID) - } else { - a.log.Infof("instance instance %s already exists with id %s", a.config.Host, i.ID) - } + a.log.Infof("created instance instance %s with id %s", domain, i.ID) return nil } diff --git a/internal/db/pg/basic.go b/internal/db/pg/basic.go index f43b80afe..ace9b3d80 100644 --- a/internal/db/pg/basic.go +++ b/internal/db/pg/basic.go @@ -24,8 +24,6 @@ import ( "fmt" "strings" - "github.com/go-pg/pg/v10" - "github.com/go-pg/pg/v10/orm" "github.com/sirupsen/logrus" "github.com/superseriousbusiness/gotosocial/internal/config" "github.com/superseriousbusiness/gotosocial/internal/db" @@ -39,160 +37,150 @@ type basicDB struct { cancel context.CancelFunc } -func (b *basicDB) Put(i interface{}) db.Error { - _, err := b.conn.Model(i).Insert(i) +func (b *basicDB) Put(ctx context.Context, i interface{}) db.Error { + _, err := b.conn.NewInsert().Model(i).Exec(ctx) if err != nil && strings.Contains(err.Error(), "duplicate key value violates unique constraint") { return db.ErrAlreadyExists } return err } -func (b *basicDB) GetByID(id string, i interface{}) db.Error { - if err := b.conn.Model(i).Where("id = ?", id).Select(); err != nil { - if err == pg.ErrNoRows { - return db.ErrNoEntries - } - return err +func (b *basicDB) GetByID(ctx context.Context, id string, i interface{}) db.Error { + q := b.conn. + NewSelect(). + Model(i). + Where("id = ?", id) - } - return nil + return processErrorResponse(q.Scan(ctx)) } -func (b *basicDB) GetWhere(where []db.Where, i interface{}) db.Error { +func (b *basicDB) GetWhere(ctx context.Context, where []db.Where, i interface{}) db.Error { if len(where) == 0 { return errors.New("no queries provided") } - q := b.conn.Model(i) + q := b.conn.NewSelect().Model(i) for _, w := range where { if w.Value == nil { - q = q.Where("? IS NULL", pg.Ident(w.Key)) + q = q.Where("? IS NULL", bun.Ident(w.Key)) } else { if w.CaseInsensitive { - q = q.Where("LOWER(?) = LOWER(?)", pg.Safe(w.Key), w.Value) + q = q.Where("LOWER(?) = LOWER(?)", bun.Safe(w.Key), w.Value) } else { - q = q.Where("? = ?", pg.Safe(w.Key), w.Value) + q = q.Where("? = ?", bun.Safe(w.Key), w.Value) } } } - if err := q.Select(); err != nil { - if err == pg.ErrNoRows { - return db.ErrNoEntries - } - return err - } - return nil + return processErrorResponse(q.Scan(ctx)) } -func (b *basicDB) GetAll(i interface{}) db.Error { - if err := b.conn.Model(i).Select(); err != nil { - if err == pg.ErrNoRows { - return db.ErrNoEntries - } - return err - } - return nil +func (b *basicDB) GetAll(ctx context.Context, i interface{}) db.Error { + q := b.conn. + NewSelect(). + Model(i) + + return processErrorResponse(q.Scan(ctx)) } -func (b *basicDB) DeleteByID(id string, i interface{}) db.Error { - if _, err := b.conn.Model(i).Where("id = ?", id).Delete(); err != nil { - // if there are no rows *anyway* then that's fine - // just return err if there's an actual error - if err != pg.ErrNoRows { - return err - } - } - return nil +func (b *basicDB) DeleteByID(ctx context.Context, id string, i interface{}) db.Error { + q := b.conn. + NewDelete(). + Model(i). + Where("id = ?", id) + + _, err := q.Exec(ctx) + + return processErrorResponse(err) } -func (b *basicDB) DeleteWhere(where []db.Where, i interface{}) db.Error { +func (b *basicDB) DeleteWhere(ctx context.Context, where []db.Where, i interface{}) db.Error { if len(where) == 0 { return errors.New("no queries provided") } - q := b.conn.Model(i) + q := b.conn. + NewDelete(). + Model(i) + for _, w := range where { - q = q.Where("? = ?", pg.Safe(w.Key), w.Value) + q = q.Where("? = ?", bun.Safe(w.Key), w.Value) } - if _, err := q.Delete(); err != nil { - // if there are no rows *anyway* then that's fine - // just return err if there's an actual error - if err != pg.ErrNoRows { - return err - } - } - return nil + _, err := q.Exec(ctx) + + return processErrorResponse(err) } -func (b *basicDB) Upsert(i interface{}, conflictColumn string) db.Error { - if _, err := b.conn.Model(i).OnConflict(fmt.Sprintf("(%s) DO UPDATE", conflictColumn)).Insert(); err != nil { - if err == pg.ErrNoRows { - return db.ErrNoEntries - } - return err - } - return nil +func (b *basicDB) Upsert(ctx context.Context, i interface{}, conflictColumn string) db.Error { + q := b.conn. + NewInsert(). + Model(i). + On(fmt.Sprintf("CONFLICT (%s) DO UPDATE", conflictColumn)) + + _, err := q.Exec(ctx) + + return processErrorResponse(err) } -func (b *basicDB) UpdateByID(id string, i interface{}) db.Error { - if _, err := b.conn.Model(i).Where("id = ?", id).OnConflict("(id) DO UPDATE").Insert(); err != nil { - if err == pg.ErrNoRows { - return db.ErrNoEntries - } - return err - } - return nil +func (b *basicDB) UpdateByID(ctx context.Context, id string, i interface{}) db.Error { + q := b.conn. + NewInsert(). + Model(i). + Where("id = ?", id). + On("CONFLICT (id) DO UPDATE") + + _, err := q.Exec(ctx) + + return processErrorResponse(err) } -func (b *basicDB) UpdateOneByID(id string, key string, value interface{}, i interface{}) db.Error { - _, err := b.conn.Model(i).Set("? = ?", pg.Safe(key), value).Where("id = ?", id).Update() - return err +func (b *basicDB) UpdateOneByID(ctx context.Context, id string, key string, value interface{}, i interface{}) db.Error { + q := b.conn.NewUpdate(). + Model(i). + Set("? = ?", bun.Safe(key), value). + Where("id = ?", id) + + _, err := q.Exec(ctx) + + return processErrorResponse(err) } -func (b *basicDB) UpdateWhere(where []db.Where, key string, value interface{}, i interface{}) db.Error { - q := b.conn.Model(i) +func (b *basicDB) UpdateWhere(ctx context.Context, where []db.Where, key string, value interface{}, i interface{}) db.Error { + q := b.conn.NewUpdate().Model(i) for _, w := range where { if w.Value == nil { - q = q.Where("? IS NULL", pg.Ident(w.Key)) + q = q.Where("? IS NULL", bun.Ident(w.Key)) } else { if w.CaseInsensitive { - q = q.Where("LOWER(?) = LOWER(?)", pg.Safe(w.Key), w.Value) + q = q.Where("LOWER(?) = LOWER(?)", bun.Safe(w.Key), w.Value) } else { - q = q.Where("? = ?", pg.Safe(w.Key), w.Value) + q = q.Where("? = ?", bun.Safe(w.Key), w.Value) } } } - q = q.Set("? = ?", pg.Safe(key), value) + q = q.Set("? = ?", bun.Safe(key), value) - _, err := q.Update() + _, err := q.Exec(ctx) + return processErrorResponse(err) +} + +func (b *basicDB) CreateTable(ctx context.Context, i interface{}) db.Error { + _, err := b.conn.NewCreateTable().IfNotExists().Model(i).Exec(ctx) return err } -func (b *basicDB) CreateTable(i interface{}) db.Error { - return b.conn.Model(i).CreateTable(&orm.CreateTableOptions{ - IfNotExists: true, - }) -} - -func (b *basicDB) DropTable(i interface{}) db.Error { - return b.conn.Model(i).DropTable(&orm.DropTableOptions{ - IfExists: true, - }) -} - -func (b *basicDB) RegisterTable(i interface{}) db.Error { - orm.RegisterTable(i) - return nil +func (b *basicDB) DropTable(ctx context.Context, i interface{}) db.Error { + _, err := b.conn.NewDropTable().Model(i).Exec(ctx) + return processErrorResponse(err) } func (b *basicDB) IsHealthy(ctx context.Context) db.Error { - return b.conn.Ping(ctx) + return b.conn.Ping() } func (b *basicDB) Stop(ctx context.Context) db.Error { diff --git a/internal/db/pg/domain.go b/internal/db/pg/domain.go index 1666d8b11..83145e083 100644 --- a/internal/db/pg/domain.go +++ b/internal/db/pg/domain.go @@ -37,27 +37,34 @@ type domainDB struct { cancel context.CancelFunc } -func (d *domainDB) IsDomainBlocked(domain string) (bool, db.Error) { +func (d *domainDB) IsDomainBlocked(ctx context.Context, domain string) (bool, db.Error) { if domain == "" { return false, nil } - blocked, err := d.conn. + count, err := d.conn. + NewSelect(). Model(>smodel.DomainBlock{}). Where("LOWER(domain) = LOWER(?)", domain). - Exists() + Limit(1). + Count(ctx) + blocked := count != 0 err = processErrorResponse(err) - return blocked, err + if err != db.ErrNoEntries { + return false, err + } + + return blocked, nil } -func (d *domainDB) AreDomainsBlocked(domains []string) (bool, db.Error) { +func (d *domainDB) AreDomainsBlocked(ctx context.Context, domains []string) (bool, db.Error) { // filter out any doubles uniqueDomains := util.UniqueStrings(domains) for _, domain := range uniqueDomains { - if blocked, err := d.IsDomainBlocked(domain); err != nil { + if blocked, err := d.IsDomainBlocked(ctx, domain); err != nil { return false, err } else if blocked { return blocked, nil @@ -68,16 +75,16 @@ func (d *domainDB) AreDomainsBlocked(domains []string) (bool, db.Error) { return false, nil } -func (d *domainDB) IsURIBlocked(uri *url.URL) (bool, db.Error) { +func (d *domainDB) IsURIBlocked(ctx context.Context, uri *url.URL) (bool, db.Error) { domain := uri.Hostname() - return d.IsDomainBlocked(domain) + return d.IsDomainBlocked(ctx, domain) } -func (d *domainDB) AreURIsBlocked(uris []*url.URL) (bool, db.Error) { +func (d *domainDB) AreURIsBlocked(ctx context.Context, uris []*url.URL) (bool, db.Error) { domains := []string{} for _, uri := range uris { domains = append(domains, uri.Hostname()) } - return d.AreDomainsBlocked(domains) + return d.AreDomainsBlocked(ctx, domains) } diff --git a/internal/db/pg/instance.go b/internal/db/pg/instance.go index 2f0c326ca..946c4c441 100644 --- a/internal/db/pg/instance.go +++ b/internal/db/pg/instance.go @@ -21,7 +21,6 @@ package pg import ( "context" - "github.com/go-pg/pg/v10" "github.com/sirupsen/logrus" "github.com/superseriousbusiness/gotosocial/internal/config" "github.com/superseriousbusiness/gotosocial/internal/db" @@ -36,24 +35,32 @@ type instanceDB struct { cancel context.CancelFunc } -func (i *instanceDB) CountInstanceUsers(domain string) (int, db.Error) { - q := i.conn.Model(&[]*gtsmodel.Account{}) +func (i *instanceDB) CountInstanceUsers(ctx context.Context, domain string) (int, db.Error) { + q := i.conn. + NewSelect(). + Model(&[]*gtsmodel.Account{}) if domain == i.config.Host { // if the domain is *this* domain, just count where the domain field is null - q = q.Where("? IS NULL", pg.Ident("domain")) + q = q.Where("? IS NULL", bun.Ident("domain")) } else { q = q.Where("domain = ?", domain) } // don't count the instance account or suspended users - q = q.Where("username != ?", domain).Where("? IS NULL", pg.Ident("suspended_at")) + q = q. + Where("username != ?", domain). + Where("? IS NULL", bun.Ident("suspended_at")) - return q.Count() + count, err := q.Count(ctx) + + return count, processErrorResponse(err) } -func (i *instanceDB) CountInstanceStatuses(domain string) (int, db.Error) { - q := i.conn.Model(&[]*gtsmodel.Status{}) +func (i *instanceDB) CountInstanceStatuses(ctx context.Context, domain string) (int, db.Error) { + q := i.conn. + NewSelect(). + Model(&[]*gtsmodel.Status{}) if domain == i.config.Host { // if the domain is *this* domain, just count where local is true @@ -64,30 +71,39 @@ func (i *instanceDB) CountInstanceStatuses(domain string) (int, db.Error) { Where("account.domain = ?", domain) } - return q.Count() + count, err := q.Count(ctx) + + return count, processErrorResponse(err) } -func (i *instanceDB) CountInstanceDomains(domain string) (int, db.Error) { - q := i.conn.Model(&[]*gtsmodel.Instance{}) +func (i *instanceDB) CountInstanceDomains(ctx context.Context, domain string) (int, db.Error) { + q := i.conn. + NewSelect(). + Model(&[]*gtsmodel.Instance{}) if domain == i.config.Host { // if the domain is *this* domain, just count other instances it knows about // exclude domains that are blocked - q = q.Where("domain != ?", domain).Where("? IS NULL", pg.Ident("suspended_at")) + q = q.Where("domain != ?", domain).Where("? IS NULL", bun.Ident("suspended_at")) } else { // TODO: implement federated domain counting properly for remote domains return 0, nil } - return q.Count() + count, err := q.Count(ctx) + + return count, processErrorResponse(err) } -func (i *instanceDB) GetInstanceAccounts(domain string, maxID string, limit int) ([]*gtsmodel.Account, db.Error) { +func (i *instanceDB) GetInstanceAccounts(ctx context.Context, domain string, maxID string, limit int) ([]*gtsmodel.Account, db.Error) { i.log.Debug("GetAccountsForInstance") accounts := []*gtsmodel.Account{} - q := i.conn.Model(&accounts).Where("domain = ?", domain).Order("id DESC") + q := i.conn.NewSelect(). + Model(&accounts). + Where("domain = ?", domain). + Order("id DESC") if maxID != "" { q = q.Where("id < ?", maxID) @@ -97,17 +113,7 @@ func (i *instanceDB) GetInstanceAccounts(domain string, maxID string, limit int) q = q.Limit(limit) } - err := q.Select() - if err != nil { - if err == pg.ErrNoRows { - return nil, db.ErrNoEntries - } - return nil, err - } + err := processErrorResponse(q.Scan(ctx)) - if len(accounts) == 0 { - return nil, db.ErrNoEntries - } - - return accounts, nil + return accounts, err } diff --git a/internal/db/pg/media.go b/internal/db/pg/media.go index 4b421cca4..dea26b8de 100644 --- a/internal/db/pg/media.go +++ b/internal/db/pg/media.go @@ -21,7 +21,6 @@ package pg import ( "context" - "github.com/go-pg/pg/v10/orm" "github.com/sirupsen/logrus" "github.com/superseriousbusiness/gotosocial/internal/config" "github.com/superseriousbusiness/gotosocial/internal/db" @@ -36,18 +35,20 @@ type mediaDB struct { cancel context.CancelFunc } -func (m *mediaDB) newMediaQ(i interface{}) *orm.Query { - return m.conn.Model(i). +func (m *mediaDB) newMediaQ(i interface{}) *bun.SelectQuery { + return m.conn. + NewSelect(). + Model(i). Relation("Account") } -func (m *mediaDB) GetAttachmentByID(id string) (*gtsmodel.MediaAttachment, db.Error) { +func (m *mediaDB) GetAttachmentByID(ctx context.Context, id string) (*gtsmodel.MediaAttachment, db.Error) { attachment := >smodel.MediaAttachment{} q := m.newMediaQ(attachment). Where("media_attachment.id = ?", id) - err := processErrorResponse(q.Select()) + err := processErrorResponse(q.Scan(ctx)) return attachment, err } diff --git a/internal/db/pg/mention.go b/internal/db/pg/mention.go index fac7ca5ad..5f61b93ec 100644 --- a/internal/db/pg/mention.go +++ b/internal/db/pg/mention.go @@ -21,7 +21,6 @@ package pg import ( "context" - "github.com/go-pg/pg/v10/orm" "github.com/sirupsen/logrus" "github.com/superseriousbusiness/gotosocial/internal/cache" "github.com/superseriousbusiness/gotosocial/internal/config" @@ -67,14 +66,16 @@ func (m *mentionDB) mentionCached(id string) (*gtsmodel.Mention, bool) { return mention, true } -func (m *mentionDB) newMentionQ(i interface{}) *orm.Query { - return m.conn.Model(i). +func (m *mentionDB) newMentionQ(i interface{}) *bun.SelectQuery { + return m.conn. + NewSelect(). + Model(i). Relation("Status"). Relation("OriginAccount"). Relation("TargetAccount") } -func (m *mentionDB) GetMention(id string) (*gtsmodel.Mention, db.Error) { +func (m *mentionDB) GetMention(ctx context.Context, id string) (*gtsmodel.Mention, db.Error) { if mention, cached := m.mentionCached(id); cached { return mention, nil } @@ -84,7 +85,7 @@ func (m *mentionDB) GetMention(id string) (*gtsmodel.Mention, db.Error) { q := m.newMentionQ(mention). Where("mention.id = ?", id) - err := processErrorResponse(q.Select()) + err := processErrorResponse(q.Scan(ctx)) if err == nil && mention != nil { m.cacheMention(id, mention) @@ -93,11 +94,11 @@ func (m *mentionDB) GetMention(id string) (*gtsmodel.Mention, db.Error) { return mention, err } -func (m *mentionDB) GetMentions(ids []string) ([]*gtsmodel.Mention, db.Error) { +func (m *mentionDB) GetMentions(ctx context.Context, ids []string) ([]*gtsmodel.Mention, db.Error) { mentions := []*gtsmodel.Mention{} for _, i := range ids { - mention, err := m.GetMention(i) + mention, err := m.GetMention(ctx, i) if err != nil { return nil, processErrorResponse(err) } diff --git a/internal/db/pg/notification.go b/internal/db/pg/notification.go index 27fe00b1e..497bfb056 100644 --- a/internal/db/pg/notification.go +++ b/internal/db/pg/notification.go @@ -21,7 +21,6 @@ package pg import ( "context" - "github.com/go-pg/pg/v10/orm" "github.com/sirupsen/logrus" "github.com/superseriousbusiness/gotosocial/internal/cache" "github.com/superseriousbusiness/gotosocial/internal/config" @@ -67,14 +66,16 @@ func (n *notificationDB) notificationCached(id string) (*gtsmodel.Notification, return notification, true } -func (n *notificationDB) newNotificationQ(i interface{}) *orm.Query { - return n.conn.Model(i). +func (n *notificationDB) newNotificationQ(i interface{}) *bun.SelectQuery { + return n.conn. + NewSelect(). + Model(i). Relation("OriginAccount"). Relation("TargetAccount"). Relation("Status") } -func (n *notificationDB) GetNotification(id string) (*gtsmodel.Notification, db.Error) { +func (n *notificationDB) GetNotification(ctx context.Context, id string) (*gtsmodel.Notification, db.Error) { if notification, cached := n.notificationCached(id); cached { return notification, nil } @@ -84,7 +85,7 @@ func (n *notificationDB) GetNotification(id string) (*gtsmodel.Notification, db. q := n.newNotificationQ(notification). Where("notification.id = ?", id) - err := processErrorResponse(q.Select()) + err := processErrorResponse(q.Scan(ctx)) if err == nil && notification != nil { n.cacheNotification(id, notification) @@ -93,10 +94,11 @@ func (n *notificationDB) GetNotification(id string) (*gtsmodel.Notification, db. return notification, err } -func (n *notificationDB) GetNotifications(accountID string, limit int, maxID string, sinceID string) ([]*gtsmodel.Notification, db.Error) { +func (n *notificationDB) GetNotifications(ctx context.Context, accountID string, limit int, maxID string, sinceID string) ([]*gtsmodel.Notification, db.Error) { // begin by selecting just the IDs notifIDs := []*gtsmodel.Notification{} q := n.conn. + NewSelect(). Model(¬ifIDs). Column("id"). Where("target_account_id = ?", accountID). @@ -114,7 +116,7 @@ func (n *notificationDB) GetNotifications(accountID string, limit int, maxID str q = q.Limit(limit) } - err := processErrorResponse(q.Select()) + err := processErrorResponse(q.Scan(ctx)) if err != nil { return nil, err } @@ -123,7 +125,7 @@ func (n *notificationDB) GetNotifications(accountID string, limit int, maxID str // reason for this is that for each notif, we can instead get it from our cache if it's cached notifications := []*gtsmodel.Notification{} for _, notifID := range notifIDs { - notif, err := n.GetNotification(notifID.ID) + notif, err := n.GetNotification(ctx, notifID.ID) errP := processErrorResponse(err) if errP != nil { return nil, errP diff --git a/internal/db/pg/pg.go b/internal/db/pg/pg.go index 7626386ee..0a29cb0bf 100644 --- a/internal/db/pg/pg.go +++ b/internal/db/pg/pg.go @@ -30,8 +30,6 @@ import ( "strings" "time" - "github.com/go-pg/pg/v10" - "github.com/go-pg/pg/v10/orm" "github.com/sirupsen/logrus" "github.com/superseriousbusiness/gotosocial/internal/config" "github.com/superseriousbusiness/gotosocial/internal/db" @@ -61,7 +59,7 @@ type postgresService struct { db.Status db.Timeline config *config.Config - conn *pg.DB + conn *bun.DB log *logrus.Logger cancel context.CancelFunc } @@ -71,7 +69,7 @@ type postgresService struct { func NewPostgresService(ctx context.Context, c *config.Config, log *logrus.Logger) (db.DB, error) { for _, t := range registerTables { // https://pg.uptrace.dev/orm/many-to-many-relation/ - orm.RegisterTable(t) + bun.RegisterModel(t) } opts, err := derivePGOptions(c) diff --git a/internal/db/pg/relationship.go b/internal/db/pg/relationship.go index 35f5a7eab..49456fe7a 100644 --- a/internal/db/pg/relationship.go +++ b/internal/db/pg/relationship.go @@ -20,10 +20,9 @@ package pg import ( "context" + "database/sql" "fmt" - "github.com/go-pg/pg/v10" - "github.com/go-pg/pg/v10/orm" "github.com/sirupsen/logrus" "github.com/superseriousbusiness/gotosocial/internal/config" "github.com/superseriousbusiness/gotosocial/internal/db" @@ -38,23 +37,29 @@ type relationshipDB struct { cancel context.CancelFunc } -func (r *relationshipDB) newBlockQ(block *gtsmodel.Block) *orm.Query { - return r.conn.Model(block). +func (r *relationshipDB) newBlockQ(block *gtsmodel.Block) *bun.SelectQuery { + return r.conn. + NewSelect(). + Model(block). Relation("Account"). Relation("TargetAccount") } -func (r *relationshipDB) newFollowQ(follow interface{}) *orm.Query { - return r.conn.Model(follow). +func (r *relationshipDB) newFollowQ(follow interface{}) *bun.SelectQuery { + return r.conn. + NewSelect(). + Model(follow). Relation("Account"). Relation("TargetAccount") } -func (r *relationshipDB) IsBlocked(account1 string, account2 string, eitherDirection bool) (bool, db.Error) { +func (r *relationshipDB) IsBlocked(ctx context.Context, account1 string, account2 string, eitherDirection bool) (bool, db.Error) { q := r.conn. + NewSelect(). Model(>smodel.Block{}). Where("account_id = ?", account1). - Where("target_account_id = ?", account2) + Where("target_account_id = ?", account2). + Limit(1) if eitherDirection { q = q. @@ -62,30 +67,45 @@ func (r *relationshipDB) IsBlocked(account1 string, account2 string, eitherDirec Where("account_id = ?", account2) } - return q.Exists() + count, err := q.Count(ctx) + + blocked := count != 0 + err = processErrorResponse(err) + + if err != db.ErrNoEntries { + return false, err + } + + return blocked, nil } -func (r *relationshipDB) GetBlock(account1 string, account2 string) (*gtsmodel.Block, db.Error) { +func (r *relationshipDB) GetBlock(ctx context.Context, account1 string, account2 string) (*gtsmodel.Block, db.Error) { block := >smodel.Block{} q := r.newBlockQ(block). Where("block.account_id = ?", account1). Where("block.target_account_id = ?", account2) - err := processErrorResponse(q.Select()) + err := processErrorResponse(q.Scan(ctx)) return block, err } -func (r *relationshipDB) GetRelationship(requestingAccount string, targetAccount string) (*gtsmodel.Relationship, db.Error) { +func (r *relationshipDB) GetRelationship(ctx context.Context, requestingAccount string, targetAccount string) (*gtsmodel.Relationship, db.Error) { rel := >smodel.Relationship{ ID: targetAccount, } // check if the requesting account follows the target account follow := >smodel.Follow{} - if err := r.conn.Model(follow).Where("account_id = ?", requestingAccount).Where("target_account_id = ?", targetAccount).Select(); err != nil { - if err != pg.ErrNoRows { + if err := r.conn. + NewSelect(). + Model(follow). + Where("account_id = ?", requestingAccount). + Where("target_account_id = ?", targetAccount). + Limit(1). + Scan(ctx); err != nil { + if err != sql.ErrNoRows { // a proper error return nil, fmt.Errorf("getrelationship: error checking follow existence: %s", err) } @@ -101,75 +121,119 @@ func (r *relationshipDB) GetRelationship(requestingAccount string, targetAccount } // check if the target account follows the requesting account - followedBy, err := r.conn.Model(>smodel.Follow{}).Where("account_id = ?", targetAccount).Where("target_account_id = ?", requestingAccount).Exists() + count, err := r.conn. + NewSelect(). + Model(>smodel.Follow{}). + Where("account_id = ?", targetAccount). + Where("target_account_id = ?", requestingAccount). + Limit(1). + Count(ctx) if err != nil { return nil, fmt.Errorf("getrelationship: error checking followed_by existence: %s", err) } - rel.FollowedBy = followedBy + rel.FollowedBy = count > 0 // check if the requesting account blocks the target account - blocking, err := r.conn.Model(>smodel.Block{}).Where("account_id = ?", requestingAccount).Where("target_account_id = ?", targetAccount).Exists() + count, err = r.conn.NewSelect(). + Model(>smodel.Block{}). + Where("account_id = ?", requestingAccount). + Where("target_account_id = ?", targetAccount). + Limit(1). + Count(ctx) if err != nil { return nil, fmt.Errorf("getrelationship: error checking blocking existence: %s", err) } - rel.Blocking = blocking + rel.Blocking = count > 0 // check if the target account blocks the requesting account - blockedBy, err := r.conn.Model(>smodel.Block{}).Where("account_id = ?", targetAccount).Where("target_account_id = ?", requestingAccount).Exists() + count, err = r.conn. + NewSelect(). + Model(>smodel.Block{}). + Where("account_id = ?", targetAccount). + Where("target_account_id = ?", requestingAccount). + Limit(1). + Count(ctx) if err != nil { return nil, fmt.Errorf("getrelationship: error checking blocked existence: %s", err) } - rel.BlockedBy = blockedBy + rel.BlockedBy = count > 0 // check if there's a pending following request from requesting account to target account - requested, err := r.conn.Model(>smodel.FollowRequest{}).Where("account_id = ?", requestingAccount).Where("target_account_id = ?", targetAccount).Exists() + count, err = r.conn. + NewSelect(). + Model(>smodel.FollowRequest{}). + Where("account_id = ?", requestingAccount). + Where("target_account_id = ?", targetAccount). + Limit(1). + Count(ctx) if err != nil { return nil, fmt.Errorf("getrelationship: error checking blocked existence: %s", err) } - rel.Requested = requested + rel.Requested = count > 0 return rel, nil } -func (r *relationshipDB) IsFollowing(sourceAccount *gtsmodel.Account, targetAccount *gtsmodel.Account) (bool, db.Error) { +func (r *relationshipDB) IsFollowing(ctx context.Context, sourceAccount *gtsmodel.Account, targetAccount *gtsmodel.Account) (bool, db.Error) { if sourceAccount == nil || targetAccount == nil { return false, nil } q := r.conn. + NewSelect(). Model(>smodel.Follow{}). Where("account_id = ?", sourceAccount.ID). - Where("target_account_id = ?", targetAccount.ID) + Where("target_account_id = ?", targetAccount.ID). + Limit(1) - return q.Exists() + count, err := q.Count(ctx) + + following := count != 0 + err = processErrorResponse(err) + + if err != db.ErrNoEntries { + return false, err + } + + return following, nil } -func (r *relationshipDB) IsFollowRequested(sourceAccount *gtsmodel.Account, targetAccount *gtsmodel.Account) (bool, db.Error) { +func (r *relationshipDB) IsFollowRequested(ctx context.Context, sourceAccount *gtsmodel.Account, targetAccount *gtsmodel.Account) (bool, db.Error) { if sourceAccount == nil || targetAccount == nil { return false, nil } q := r.conn. + NewSelect(). Model(>smodel.FollowRequest{}). Where("account_id = ?", sourceAccount.ID). Where("target_account_id = ?", targetAccount.ID) - return q.Exists() + count, err := q.Count(ctx) + + followRequested := count != 0 + err = processErrorResponse(err) + + if err != db.ErrNoEntries { + return false, err + } + + return followRequested, nil } -func (r *relationshipDB) IsMutualFollowing(account1 *gtsmodel.Account, account2 *gtsmodel.Account) (bool, db.Error) { +func (r *relationshipDB) IsMutualFollowing(ctx context.Context, account1 *gtsmodel.Account, account2 *gtsmodel.Account) (bool, db.Error) { if account1 == nil || account2 == nil { return false, nil } // make sure account 1 follows account 2 - f1, err := r.IsFollowing(account1, account2) + f1, err := r.IsFollowing(ctx, account1, account2) if err != nil { return false, processErrorResponse(err) } // make sure account 2 follows account 1 - f2, err := r.IsFollowing(account2, account1) + f2, err := r.IsFollowing(ctx, account2, account1) if err != nil { return false, processErrorResponse(err) } @@ -177,14 +241,16 @@ func (r *relationshipDB) IsMutualFollowing(account1 *gtsmodel.Account, account2 return f1 && f2, nil } -func (r *relationshipDB) AcceptFollowRequest(originAccountID string, targetAccountID string) (*gtsmodel.Follow, db.Error) { +func (r *relationshipDB) AcceptFollowRequest(ctx context.Context, originAccountID string, targetAccountID string) (*gtsmodel.Follow, db.Error) { // make sure the original follow request exists fr := >smodel.FollowRequest{} - if err := r.conn.Model(fr).Where("account_id = ?", originAccountID).Where("target_account_id = ?", targetAccountID).Select(); err != nil { - if err == pg.ErrMultiRows { - return nil, db.ErrNoEntries - } - return nil, err + if err := r.conn. + NewSelect(). + Model(fr). + Where("account_id = ?", originAccountID). + Where("target_account_id = ?", targetAccountID). + Scan(ctx); err != nil { + return nil, processErrorResponse(err) } // create a new follow to 'replace' the request with @@ -196,82 +262,95 @@ func (r *relationshipDB) AcceptFollowRequest(originAccountID string, targetAccou } // if the follow already exists, just update the URI -- we don't need to do anything else - if _, err := r.conn.Model(follow).OnConflict("ON CONSTRAINT follows_account_id_target_account_id_key DO UPDATE set uri = ?", follow.URI).Insert(); err != nil { - return nil, err + if _, err := r.conn. + NewInsert(). + Model(follow). + On("CONFLICT CONSTRAINT follows_account_id_target_account_id_key DO UPDATE set uri = ?", follow.URI). + Exec(ctx); err != nil { + return nil, processErrorResponse(err) } // now remove the follow request - if _, err := r.conn.Model(>smodel.FollowRequest{}).Where("account_id = ?", originAccountID).Where("target_account_id = ?", targetAccountID).Delete(); err != nil { - return nil, err + if _, err := r.conn. + NewDelete(). + Model(>smodel.FollowRequest{}). + Where("account_id = ?", originAccountID). + Where("target_account_id = ?", targetAccountID). + Exec(ctx); err != nil { + return nil, processErrorResponse(err) } return follow, nil } -func (r *relationshipDB) GetAccountFollowRequests(accountID string) ([]*gtsmodel.FollowRequest, db.Error) { +func (r *relationshipDB) GetAccountFollowRequests(ctx context.Context, accountID string) ([]*gtsmodel.FollowRequest, db.Error) { followRequests := []*gtsmodel.FollowRequest{} q := r.newFollowQ(&followRequests). Where("target_account_id = ?", accountID) - err := processErrorResponse(q.Select()) + err := processErrorResponse(q.Scan(ctx)) return followRequests, err } -func (r *relationshipDB) GetAccountFollows(accountID string) ([]*gtsmodel.Follow, db.Error) { +func (r *relationshipDB) GetAccountFollows(ctx context.Context, accountID string) ([]*gtsmodel.Follow, db.Error) { follows := []*gtsmodel.Follow{} q := r.newFollowQ(&follows). Where("account_id = ?", accountID) - err := processErrorResponse(q.Select()) + err := processErrorResponse(q.Scan(ctx)) return follows, err } -func (r *relationshipDB) CountAccountFollows(accountID string, localOnly bool) (int, db.Error) { +func (r *relationshipDB) CountAccountFollows(ctx context.Context, accountID string, localOnly bool) (int, db.Error) { return r.conn. + NewSelect(). Model(&[]*gtsmodel.Follow{}). Where("account_id = ?", accountID). - Count() + Count(ctx) } -func (r *relationshipDB) GetAccountFollowedBy(accountID string, localOnly bool) ([]*gtsmodel.Follow, db.Error) { +func (r *relationshipDB) GetAccountFollowedBy(ctx context.Context, accountID string, localOnly bool) ([]*gtsmodel.Follow, db.Error) { follows := []*gtsmodel.Follow{} - q := r.conn.Model(&follows) + q := r.conn. + NewSelect(). + Model(&follows) if localOnly { // for local accounts let's get where domain is null OR where domain is an empty string, just to be safe - whereGroup := func(q *pg.Query) (*pg.Query, error) { + whereGroup := func(q *bun.SelectQuery) *bun.SelectQuery { q = q. - WhereOr("? IS NULL", pg.Ident("a.domain")). + WhereOr("? IS NULL", bun.Ident("a.domain")). WhereOr("a.domain = ?", "") - return q, nil + return q } q = q.ColumnExpr("follow.*"). Join("JOIN accounts AS a ON follow.account_id = TEXT(a.id)"). Where("follow.target_account_id = ?", accountID). - WhereGroup(whereGroup) + WhereGroup(" AND ", whereGroup) } else { q = q.Where("target_account_id = ?", accountID) } - if err := q.Select(); err != nil { - if err == pg.ErrNoRows { + if err := q.Scan(ctx); err != nil { + if err == sql.ErrNoRows { return follows, nil } - return nil, err + return nil, processErrorResponse(err) } return follows, nil } -func (r *relationshipDB) CountAccountFollowedBy(accountID string, localOnly bool) (int, db.Error) { +func (r *relationshipDB) CountAccountFollowedBy(ctx context.Context, accountID string, localOnly bool) (int, db.Error) { return r.conn. + NewSelect(). Model(&[]*gtsmodel.Follow{}). Where("target_account_id = ?", accountID). - Count() + Count(ctx) } diff --git a/internal/db/pg/util.go b/internal/db/pg/util.go index e6d31780a..80f967648 100644 --- a/internal/db/pg/util.go +++ b/internal/db/pg/util.go @@ -3,6 +3,8 @@ package pg import ( "strings" + "database/sql" + "github.com/superseriousbusiness/gotosocial/internal/db" ) @@ -11,10 +13,8 @@ func processErrorResponse(err error) db.Error { switch err { case nil: return nil - case bun.ErrNoRows: + case sql.ErrNoRows: return db.ErrNoEntries - case bun.ErrMultiRows: - return db.ErrMultipleEntries default: if strings.Contains(err.Error(), "duplicate key value violates unique constraint") { return db.ErrAlreadyExists diff --git a/internal/federation/authenticate.go b/internal/federation/authenticate.go index 699691ca6..81ac84544 100644 --- a/internal/federation/authenticate.go +++ b/internal/federation/authenticate.go @@ -148,7 +148,7 @@ func (f *federator) AuthenticateFederatedRequest(ctx context.Context, requestedU // LOCAL ACCOUNT REQUEST // the request is coming from INSIDE THE HOUSE so skip the remote dereferencing l.Tracef("proceeding without dereference for local public key %s", requestingPublicKeyID) - if err := f.db.GetWhere([]db.Where{{Key: "public_key_uri", Value: requestingPublicKeyID.String()}}, requestingLocalAccount); err != nil { + if err := f.db.GetWhere(ctx, []db.Where{{Key: "public_key_uri", Value: requestingPublicKeyID.String()}}, requestingLocalAccount); err != nil { return nil, false, fmt.Errorf("couldn't get local account with public key uri %s from the database: %s", requestingPublicKeyID.String(), err) } publicKey = requestingLocalAccount.PublicKey @@ -156,7 +156,7 @@ func (f *federator) AuthenticateFederatedRequest(ctx context.Context, requestedU if err != nil { return nil, false, fmt.Errorf("error parsing url %s: %s", requestingLocalAccount.URI, err) } - } else if err := f.db.GetWhere([]db.Where{{Key: "public_key_uri", Value: requestingPublicKeyID.String()}}, requestingRemoteAccount); err == nil { + } else if err := f.db.GetWhere(ctx, []db.Where{{Key: "public_key_uri", Value: requestingPublicKeyID.String()}}, requestingRemoteAccount); err == nil { // REMOTE ACCOUNT REQUEST WITH KEY CACHED LOCALLY // this is a remote account and we already have the public key for it so use that l.Tracef("proceeding without dereference for cached public key %s", requestingPublicKeyID) @@ -170,7 +170,7 @@ func (f *federator) AuthenticateFederatedRequest(ctx context.Context, requestedU // the request is remote and we don't have the public key yet, // so we need to authenticate the request properly by dereferencing the remote key l.Tracef("proceeding with dereference for uncached public key %s", requestingPublicKeyID) - transport, err := f.transportController.NewTransportForUsername(requestedUsername) + transport, err := f.transportController.NewTransportForUsername(ctx, requestedUsername) if err != nil { return nil, false, fmt.Errorf("transport err: %s", err) } diff --git a/internal/federation/dereference.go b/internal/federation/dereference.go index 96a662e32..a09f0f84b 100644 --- a/internal/federation/dereference.go +++ b/internal/federation/dereference.go @@ -19,36 +19,37 @@ package federation import ( + "context" "net/url" "github.com/superseriousbusiness/gotosocial/internal/ap" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" ) -func (f *federator) GetRemoteAccount(username string, remoteAccountID *url.URL, refresh bool) (*gtsmodel.Account, bool, error) { - return f.dereferencer.GetRemoteAccount(username, remoteAccountID, refresh) +func (f *federator) GetRemoteAccount(ctx context.Context, username string, remoteAccountID *url.URL, refresh bool) (*gtsmodel.Account, bool, error) { + return f.dereferencer.GetRemoteAccount(ctx, username, remoteAccountID, refresh) } -func (f *federator) EnrichRemoteAccount(username string, account *gtsmodel.Account) (*gtsmodel.Account, error) { - return f.dereferencer.EnrichRemoteAccount(username, account) +func (f *federator) EnrichRemoteAccount(ctx context.Context, username string, account *gtsmodel.Account) (*gtsmodel.Account, error) { + return f.dereferencer.EnrichRemoteAccount(ctx, username, account) } -func (f *federator) GetRemoteStatus(username string, remoteStatusID *url.URL, refresh bool) (*gtsmodel.Status, ap.Statusable, bool, error) { - return f.dereferencer.GetRemoteStatus(username, remoteStatusID, refresh) +func (f *federator) GetRemoteStatus(ctx context.Context, username string, remoteStatusID *url.URL, refresh bool) (*gtsmodel.Status, ap.Statusable, bool, error) { + return f.dereferencer.GetRemoteStatus(ctx, username, remoteStatusID, refresh) } -func (f *federator) EnrichRemoteStatus(username string, status *gtsmodel.Status) (*gtsmodel.Status, error) { - return f.dereferencer.EnrichRemoteStatus(username, status) +func (f *federator) EnrichRemoteStatus(ctx context.Context, username string, status *gtsmodel.Status) (*gtsmodel.Status, error) { + return f.dereferencer.EnrichRemoteStatus(ctx, username, status) } -func (f *federator) DereferenceRemoteThread(username string, statusIRI *url.URL) error { - return f.dereferencer.DereferenceThread(username, statusIRI) +func (f *federator) DereferenceRemoteThread(ctx context.Context, username string, statusIRI *url.URL) error { + return f.dereferencer.DereferenceThread(ctx, username, statusIRI) } -func (f *federator) GetRemoteInstance(username string, remoteInstanceURI *url.URL) (*gtsmodel.Instance, error) { - return f.dereferencer.GetRemoteInstance(username, remoteInstanceURI) +func (f *federator) GetRemoteInstance(ctx context.Context, username string, remoteInstanceURI *url.URL) (*gtsmodel.Instance, error) { + return f.dereferencer.GetRemoteInstance(ctx, username, remoteInstanceURI) } -func (f *federator) DereferenceAnnounce(announce *gtsmodel.Status, requestingUsername string) error { - return f.dereferencer.DereferenceAnnounce(announce, requestingUsername) +func (f *federator) DereferenceAnnounce(ctx context.Context, announce *gtsmodel.Status, requestingUsername string) error { + return f.dereferencer.DereferenceAnnounce(ctx, announce, requestingUsername) } diff --git a/internal/federation/dereferencing/account.go b/internal/federation/dereferencing/account.go index 50ec04f51..dbecd49f6 100644 --- a/internal/federation/dereferencing/account.go +++ b/internal/federation/dereferencing/account.go @@ -80,7 +80,7 @@ func (d *deref) GetRemoteAccount(ctx context.Context, username string, remoteAcc return nil, new, fmt.Errorf("FullyDereferenceAccount: error dereferencing accountable: %s", err) } - gtsAccount, err := d.typeConverter.ASRepresentationToAccount(accountable, refresh) + gtsAccount, err := d.typeConverter.ASRepresentationToAccount(ctx, accountable, refresh) if err != nil { return nil, new, fmt.Errorf("FullyDereferenceAccount: error converting accountable to account: %s", err) } @@ -218,7 +218,7 @@ func (d *deref) fetchHeaderAndAviForAccount(ctx context.Context, targetAccount * } if targetAccount.AvatarRemoteURL != "" && (targetAccount.AvatarMediaAttachmentID == "" || refresh) { - a, err := d.mediaHandler.ProcessRemoteHeaderOrAvatar(t, >smodel.MediaAttachment{ + a, err := d.mediaHandler.ProcessRemoteHeaderOrAvatar(ctx, t, >smodel.MediaAttachment{ RemoteURL: targetAccount.AvatarRemoteURL, Avatar: true, }, targetAccount.ID) @@ -229,7 +229,7 @@ func (d *deref) fetchHeaderAndAviForAccount(ctx context.Context, targetAccount * } if targetAccount.HeaderRemoteURL != "" && (targetAccount.HeaderMediaAttachmentID == "" || refresh) { - a, err := d.mediaHandler.ProcessRemoteHeaderOrAvatar(t, >smodel.MediaAttachment{ + a, err := d.mediaHandler.ProcessRemoteHeaderOrAvatar(ctx, t, >smodel.MediaAttachment{ RemoteURL: targetAccount.HeaderRemoteURL, Header: true, }, targetAccount.ID) diff --git a/internal/federation/dereferencing/status.go b/internal/federation/dereferencing/status.go index ac42be14e..93ead6523 100644 --- a/internal/federation/dereferencing/status.go +++ b/internal/federation/dereferencing/status.go @@ -93,7 +93,7 @@ func (d *deref) GetRemoteStatus(ctx context.Context, username string, remoteStat return nil, statusable, new, fmt.Errorf("GetRemoteStatus: couldn't derive status author: %s", err) } - gtsStatus, err := d.typeConverter.ASStatusToStatus(statusable) + gtsStatus, err := d.typeConverter.ASStatusToStatus(ctx, statusable) if err != nil { return nil, statusable, new, fmt.Errorf("GetRemoteStatus: error converting statusable to status: %s", err) } @@ -294,7 +294,7 @@ func (d *deref) populateStatusFields(ctx context.Context, status *gtsmodel.Statu } // it just doesn't exist yet so carry on l.Debug("attachment doesn't exist yet, calling ProcessRemoteAttachment", a) - deferencedAttachment, err := d.mediaHandler.ProcessRemoteAttachment(t, a, status.AccountID) + deferencedAttachment, err := d.mediaHandler.ProcessRemoteAttachment(ctx, t, a, status.AccountID) if err != nil { l.Errorf("error dereferencing status attachment: %s", err) continue diff --git a/internal/federation/federatingdb/accept.go b/internal/federation/federatingdb/accept.go index 91d9df86f..0b14e8a6a 100644 --- a/internal/federation/federatingdb/accept.go +++ b/internal/federation/federatingdb/accept.go @@ -86,7 +86,7 @@ func (f *federatingDB) Accept(ctx context.Context, accept vocab.ActivityStreamsA if util.IsFollowPath(acceptedObjectIRI) { // ACCEPT FOLLOW gtsFollowRequest := >smodel.FollowRequest{} - if err := f.db.GetWhere([]db.Where{{Key: "uri", Value: acceptedObjectIRI.String()}}, gtsFollowRequest); err != nil { + if err := f.db.GetWhere(ctx, []db.Where{{Key: "uri", Value: acceptedObjectIRI.String()}}, gtsFollowRequest); err != nil { return fmt.Errorf("ACCEPT: couldn't get follow request with id %s from the database: %s", acceptedObjectIRI.String(), err) } @@ -94,7 +94,7 @@ func (f *federatingDB) Accept(ctx context.Context, accept vocab.ActivityStreamsA if gtsFollowRequest.AccountID != targetAcct.ID { return errors.New("ACCEPT: follow object account and inbox account were not the same") } - follow, err := f.db.AcceptFollowRequest(gtsFollowRequest.AccountID, gtsFollowRequest.TargetAccountID) + follow, err := f.db.AcceptFollowRequest(ctx, gtsFollowRequest.AccountID, gtsFollowRequest.TargetAccountID) if err != nil { return err } @@ -123,7 +123,7 @@ func (f *federatingDB) Accept(ctx context.Context, accept vocab.ActivityStreamsA return errors.New("ACCEPT: couldn't parse follow into vocab.ActivityStreamsFollow") } // convert the follow to something we can understand - gtsFollow, err := f.typeConverter.ASFollowToFollow(asFollow) + gtsFollow, err := f.typeConverter.ASFollowToFollow(ctx, asFollow) if err != nil { return fmt.Errorf("ACCEPT: error converting asfollow to gtsfollow: %s", err) } @@ -131,7 +131,7 @@ func (f *federatingDB) Accept(ctx context.Context, accept vocab.ActivityStreamsA if gtsFollow.AccountID != targetAcct.ID { return errors.New("ACCEPT: follow object account and inbox account were not the same") } - follow, err := f.db.AcceptFollowRequest(gtsFollow.AccountID, gtsFollow.TargetAccountID) + follow, err := f.db.AcceptFollowRequest(ctx, gtsFollow.AccountID, gtsFollow.TargetAccountID) if err != nil { return err } diff --git a/internal/federation/federatingdb/announce.go b/internal/federation/federatingdb/announce.go index 981eaf1ef..5cd34285e 100644 --- a/internal/federation/federatingdb/announce.go +++ b/internal/federation/federatingdb/announce.go @@ -71,7 +71,7 @@ func (f *federatingDB) Announce(ctx context.Context, announce vocab.ActivityStre return nil } - boost, isNew, err := f.typeConverter.ASAnnounceToStatus(announce) + boost, isNew, err := f.typeConverter.ASAnnounceToStatus(ctx, announce) if err != nil { return fmt.Errorf("ANNOUNCE: error converting announce to boost: %s", err) } diff --git a/internal/federation/federatingdb/create.go b/internal/federation/federatingdb/create.go index fb4353cd4..8ea549c5a 100644 --- a/internal/federation/federatingdb/create.go +++ b/internal/federation/federatingdb/create.go @@ -100,7 +100,7 @@ func (f *federatingDB) Create(ctx context.Context, asType vocab.Type) error { case gtsmodel.ActivityStreamsNote: // CREATE A NOTE note := objectIter.GetActivityStreamsNote() - status, err := f.typeConverter.ASStatusToStatus(note) + status, err := f.typeConverter.ASStatusToStatus(ctx, note) if err != nil { return fmt.Errorf("CREATE: error converting note to status: %s", err) } @@ -112,7 +112,7 @@ func (f *federatingDB) Create(ctx context.Context, asType vocab.Type) error { } status.ID = statusID - if err := f.db.PutStatus(status); err != nil { + if err := f.db.PutStatus(ctx, status); err != nil { if err == db.ErrAlreadyExists { // the status already exists in the database, which means we've already handled everything else, // so we can just return nil here and be done with it. @@ -137,7 +137,7 @@ func (f *federatingDB) Create(ctx context.Context, asType vocab.Type) error { return errors.New("CREATE: could not convert type to follow") } - followRequest, err := f.typeConverter.ASFollowToFollowRequest(follow) + followRequest, err := f.typeConverter.ASFollowToFollowRequest(ctx, follow) if err != nil { return fmt.Errorf("CREATE: could not convert Follow to follow request: %s", err) } @@ -148,7 +148,7 @@ func (f *federatingDB) Create(ctx context.Context, asType vocab.Type) error { } followRequest.ID = newID - if err := f.db.Put(followRequest); err != nil { + if err := f.db.Put(ctx, followRequest); err != nil { return fmt.Errorf("CREATE: database error inserting follow request: %s", err) } @@ -165,7 +165,7 @@ func (f *federatingDB) Create(ctx context.Context, asType vocab.Type) error { return errors.New("CREATE: could not convert type to like") } - fave, err := f.typeConverter.ASLikeToFave(like) + fave, err := f.typeConverter.ASLikeToFave(ctx, like) if err != nil { return fmt.Errorf("CREATE: could not convert Like to fave: %s", err) } @@ -176,7 +176,7 @@ func (f *federatingDB) Create(ctx context.Context, asType vocab.Type) error { } fave.ID = newID - if err := f.db.Put(fave); err != nil { + if err := f.db.Put(ctx, fave); err != nil { return fmt.Errorf("CREATE: database error inserting fave: %s", err) } @@ -193,7 +193,7 @@ func (f *federatingDB) Create(ctx context.Context, asType vocab.Type) error { return errors.New("CREATE: could not convert type to block") } - block, err := f.typeConverter.ASBlockToBlock(blockable) + block, err := f.typeConverter.ASBlockToBlock(ctx, blockable) if err != nil { return fmt.Errorf("CREATE: could not convert Block to gts model block") } @@ -204,7 +204,7 @@ func (f *federatingDB) Create(ctx context.Context, asType vocab.Type) error { } block.ID = newID - if err := f.db.Put(block); err != nil { + if err := f.db.Put(ctx, block); err != nil { return fmt.Errorf("CREATE: database error inserting block: %s", err) } diff --git a/internal/federation/federatingdb/delete.go b/internal/federation/federatingdb/delete.go index ee9310789..94dfdf71f 100644 --- a/internal/federation/federatingdb/delete.go +++ b/internal/federation/federatingdb/delete.go @@ -69,11 +69,11 @@ func (f *federatingDB) Delete(ctx context.Context, id *url.URL) error { // 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... - s, err := f.db.GetStatusByURI(id.String()) + s, err := f.db.GetStatusByURI(ctx, id.String()) if err == nil { // it's a status l.Debugf("uri is for status with id: %s", s.ID) - if err := f.db.DeleteByID(s.ID, >smodel.Status{}); err != nil { + if err := f.db.DeleteByID(ctx, s.ID, >smodel.Status{}); err != nil { return fmt.Errorf("DELETE: err deleting status: %s", err) } fromFederatorChan <- gtsmodel.FromFederator{ @@ -84,11 +84,11 @@ func (f *federatingDB) Delete(ctx context.Context, id *url.URL) error { } } - a, err := f.db.GetAccountByURI(id.String()) + a, err := f.db.GetAccountByURI(ctx, id.String()) if err == nil { // it's an account l.Debugf("uri is for an account with id: %s", s.ID) - if err := f.db.DeleteByID(a.ID, >smodel.Account{}); err != nil { + if err := f.db.DeleteByID(ctx, a.ID, >smodel.Account{}); err != nil { return fmt.Errorf("DELETE: err deleting account: %s", err) } fromFederatorChan <- gtsmodel.FromFederator{ diff --git a/internal/federation/federatingdb/followers.go b/internal/federation/federatingdb/followers.go index 241362fc1..e0923453f 100644 --- a/internal/federation/federatingdb/followers.go +++ b/internal/federation/federatingdb/followers.go @@ -19,7 +19,7 @@ import ( // If modified, the library will then call Update. // // The library makes this call only after acquiring a lock first. -func (f *federatingDB) Followers(c context.Context, actorIRI *url.URL) (followers vocab.ActivityStreamsCollection, err error) { +func (f *federatingDB) Followers(ctx context.Context, actorIRI *url.URL) (followers vocab.ActivityStreamsCollection, err error) { l := f.log.WithFields( logrus.Fields{ "func": "Followers", @@ -31,19 +31,19 @@ func (f *federatingDB) Followers(c context.Context, actorIRI *url.URL) (follower acct := >smodel.Account{} if util.IsUserPath(actorIRI) { - acct, err = f.db.GetAccountByURI(actorIRI.String()) + 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([]db.Where{{Key: "followers_uri", Value: actorIRI.String()}}, acct); err != nil { + 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()) } - acctFollowers, err := f.db.GetAccountFollowedBy(acct.ID, false) + 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) } @@ -52,7 +52,7 @@ func (f *federatingDB) Followers(c context.Context, actorIRI *url.URL) (follower items := streams.NewActivityStreamsItemsProperty() for _, follow := range acctFollowers { gtsFollower := >smodel.Account{} - if err := f.db.GetByID(follow.AccountID, gtsFollower); err != nil { + if err := f.db.GetByID(ctx, follow.AccountID, gtsFollower); err != nil { return nil, fmt.Errorf("FOLLOWERS: db error getting account id %s: %s", follow.AccountID, err) } uri, err := url.Parse(gtsFollower.URI) diff --git a/internal/federation/federatingdb/following.go b/internal/federation/federatingdb/following.go index 45785c671..963ba63e4 100644 --- a/internal/federation/federatingdb/following.go +++ b/internal/federation/federatingdb/following.go @@ -18,7 +18,7 @@ import ( // If modified, the library will then call Update. // // The library makes this call only after acquiring a lock first. -func (f *federatingDB) Following(c context.Context, actorIRI *url.URL) (following vocab.ActivityStreamsCollection, err error) { +func (f *federatingDB) Following(ctx context.Context, actorIRI *url.URL) (following vocab.ActivityStreamsCollection, err error) { l := f.log.WithFields( logrus.Fields{ "func": "Following", @@ -34,7 +34,7 @@ func (f *federatingDB) Following(c context.Context, actorIRI *url.URL) (followin return nil, fmt.Errorf("FOLLOWING: error parsing user path: %s", err) } - a, err := f.db.GetLocalAccountByUsername(username) + 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) } @@ -46,7 +46,7 @@ func (f *federatingDB) Following(c context.Context, actorIRI *url.URL) (followin return nil, fmt.Errorf("FOLLOWING: error parsing following path: %s", err) } - a, err := f.db.GetLocalAccountByUsername(username) + 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) } @@ -56,7 +56,7 @@ func (f *federatingDB) Following(c context.Context, actorIRI *url.URL) (followin return nil, fmt.Errorf("FOLLOWING: could not parse actor IRI %s as users or following path", actorIRI.String()) } - acctFollowing, err := f.db.GetAccountFollows(acct.ID) + 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) } @@ -65,7 +65,7 @@ func (f *federatingDB) Following(c context.Context, actorIRI *url.URL) (followin items := streams.NewActivityStreamsItemsProperty() for _, follow := range acctFollowing { gtsFollowing := >smodel.Account{} - if err := f.db.GetByID(follow.AccountID, gtsFollowing); err != nil { + if err := f.db.GetByID(ctx, follow.AccountID, gtsFollowing); err != nil { return nil, fmt.Errorf("FOLLOWING: db error getting account id %s: %s", follow.AccountID, err) } uri, err := url.Parse(gtsFollowing.URI) diff --git a/internal/federation/federatingdb/get.go b/internal/federation/federatingdb/get.go index 0265080f9..cc04dd851 100644 --- a/internal/federation/federatingdb/get.go +++ b/internal/federation/federatingdb/get.go @@ -33,7 +33,7 @@ import ( // Get returns the database entry for the specified id. // // The library makes this call only after acquiring a lock first. -func (f *federatingDB) Get(c context.Context, id *url.URL) (value vocab.Type, err error) { +func (f *federatingDB) Get(ctx context.Context, id *url.URL) (value vocab.Type, err error) { l := f.log.WithFields( logrus.Fields{ "func": "Get", @@ -43,17 +43,17 @@ func (f *federatingDB) Get(c context.Context, id *url.URL) (value vocab.Type, er l.Debug("entering GET function") if util.IsUserPath(id) { - acct, err := f.db.GetAccountByURI(id.String()) + 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(acct) + return f.typeConverter.AccountToAS(ctx, acct) } if util.IsFollowersPath(id) { acct := >smodel.Account{} - if err := f.db.GetWhere([]db.Where{{Key: "followers_uri", Value: id.String()}}, acct); err != nil { + if err := f.db.GetWhere(ctx, []db.Where{{Key: "followers_uri", Value: id.String()}}, acct); err != nil { return nil, err } @@ -62,12 +62,12 @@ func (f *federatingDB) Get(c context.Context, id *url.URL) (value vocab.Type, er return nil, err } - return f.Followers(c, followersURI) + return f.Followers(ctx, followersURI) } if util.IsFollowingPath(id) { acct := >smodel.Account{} - if err := f.db.GetWhere([]db.Where{{Key: "following_uri", Value: id.String()}}, acct); err != nil { + if err := f.db.GetWhere(ctx, []db.Where{{Key: "following_uri", Value: id.String()}}, acct); err != nil { return nil, err } @@ -76,7 +76,7 @@ func (f *federatingDB) Get(c context.Context, id *url.URL) (value vocab.Type, er return nil, err } - return f.Following(c, followingURI) + return f.Following(ctx, followingURI) } return nil, errors.New("could not get") diff --git a/internal/federation/federatingdb/outbox.go b/internal/federation/federatingdb/outbox.go index 849014432..81b90aae2 100644 --- a/internal/federation/federatingdb/outbox.go +++ b/internal/federation/federatingdb/outbox.go @@ -35,7 +35,7 @@ import ( // at the specified IRI, for prepending new items. // // The library makes this call only after acquiring a lock first. -func (f *federatingDB) GetOutbox(c context.Context, outboxIRI *url.URL) (inbox vocab.ActivityStreamsOrderedCollectionPage, err error) { +func (f *federatingDB) GetOutbox(ctx context.Context, outboxIRI *url.URL) (inbox vocab.ActivityStreamsOrderedCollectionPage, err error) { l := f.log.WithFields( logrus.Fields{ "func": "GetOutbox", @@ -51,7 +51,7 @@ func (f *federatingDB) GetOutbox(c context.Context, outboxIRI *url.URL) (inbox v // database entries. Separate calls to Create will do that. // // The library makes this call only after acquiring a lock first. -func (f *federatingDB) SetOutbox(c context.Context, outbox vocab.ActivityStreamsOrderedCollectionPage) error { +func (f *federatingDB) SetOutbox(ctx context.Context, outbox vocab.ActivityStreamsOrderedCollectionPage) error { l := f.log.WithFields( logrus.Fields{ "func": "SetOutbox", @@ -66,7 +66,7 @@ func (f *federatingDB) SetOutbox(c context.Context, outbox vocab.ActivityStreams // actor's inbox IRI. // // The library makes this call only after acquiring a lock first. -func (f *federatingDB) OutboxForInbox(c context.Context, inboxIRI *url.URL) (outboxIRI *url.URL, err error) { +func (f *federatingDB) OutboxForInbox(ctx context.Context, inboxIRI *url.URL) (outboxIRI *url.URL, err error) { l := f.log.WithFields( logrus.Fields{ "func": "OutboxForInbox", @@ -79,7 +79,7 @@ func (f *federatingDB) OutboxForInbox(c context.Context, inboxIRI *url.URL) (out return nil, fmt.Errorf("%s is not an inbox URI", inboxIRI.String()) } acct := >smodel.Account{} - if err := f.db.GetWhere([]db.Where{{Key: "inbox_uri", Value: inboxIRI.String()}}, acct); err != nil { + 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()) } diff --git a/internal/federation/federatingdb/owns.go b/internal/federation/federatingdb/owns.go index 0a65397ff..1c1f2512d 100644 --- a/internal/federation/federatingdb/owns.go +++ b/internal/federation/federatingdb/owns.go @@ -32,7 +32,7 @@ import ( // Owns returns true if the IRI belongs to this instance, and if // the database has an entry for the IRI. // The library makes this call only after acquiring a lock first. -func (f *federatingDB) Owns(c context.Context, id *url.URL) (bool, error) { +func (f *federatingDB) Owns(ctx context.Context, id *url.URL) (bool, error) { l := f.log.WithFields( logrus.Fields{ "func": "Owns", @@ -54,7 +54,7 @@ func (f *federatingDB) Owns(c context.Context, id *url.URL) (bool, error) { if err != nil { return false, fmt.Errorf("error parsing statuses path for url %s: %s", id.String(), err) } - status, err := f.db.GetStatusByURI(uid) + status, err := f.db.GetStatusByURI(ctx, uid) if err != nil { if err == db.ErrNoEntries { // there are no entries for this status @@ -71,7 +71,7 @@ func (f *federatingDB) Owns(c context.Context, id *url.URL) (bool, error) { if err != nil { return false, fmt.Errorf("error parsing statuses path for url %s: %s", id.String(), err) } - if _, err := f.db.GetLocalAccountByUsername(username); err != nil { + if _, err := f.db.GetLocalAccountByUsername(ctx, username); err != nil { if err == db.ErrNoEntries { // there are no entries for this username return false, nil @@ -88,7 +88,7 @@ func (f *federatingDB) Owns(c context.Context, id *url.URL) (bool, error) { if err != nil { return false, fmt.Errorf("error parsing statuses path for url %s: %s", id.String(), err) } - if _, err := f.db.GetLocalAccountByUsername(username); err != nil { + if _, err := f.db.GetLocalAccountByUsername(ctx, username); err != nil { if err == db.ErrNoEntries { // there are no entries for this username return false, nil @@ -105,7 +105,7 @@ func (f *federatingDB) Owns(c context.Context, id *url.URL) (bool, error) { if err != nil { return false, fmt.Errorf("error parsing statuses path for url %s: %s", id.String(), err) } - if _, err := f.db.GetLocalAccountByUsername(username); err != nil { + if _, err := f.db.GetLocalAccountByUsername(ctx, username); err != nil { if err == db.ErrNoEntries { // there are no entries for this username return false, nil @@ -122,7 +122,7 @@ func (f *federatingDB) Owns(c context.Context, id *url.URL) (bool, error) { if err != nil { return false, fmt.Errorf("error parsing like path for url %s: %s", id.String(), err) } - if _, err := f.db.GetLocalAccountByUsername(username); err != nil { + if _, err := f.db.GetLocalAccountByUsername(ctx, username); err != nil { if err == db.ErrNoEntries { // there are no entries for this username return false, nil @@ -130,7 +130,7 @@ func (f *federatingDB) Owns(c context.Context, id *url.URL) (bool, error) { // an actual error happened return false, fmt.Errorf("database error fetching account with username %s: %s", username, err) } - if err := f.db.GetByID(likeID, >smodel.StatusFave{}); err != nil { + if err := f.db.GetByID(ctx, likeID, >smodel.StatusFave{}); err != nil { if err == db.ErrNoEntries { // there are no entries return false, nil @@ -147,7 +147,7 @@ func (f *federatingDB) Owns(c context.Context, id *url.URL) (bool, error) { if err != nil { return false, fmt.Errorf("error parsing block path for url %s: %s", id.String(), err) } - if _, err := f.db.GetLocalAccountByUsername(username); err != nil { + if _, err := f.db.GetLocalAccountByUsername(ctx, username); err != nil { if err == db.ErrNoEntries { // there are no entries for this username return false, nil @@ -155,7 +155,7 @@ func (f *federatingDB) Owns(c context.Context, id *url.URL) (bool, error) { // an actual error happened return false, fmt.Errorf("database error fetching account with username %s: %s", username, err) } - if err := f.db.GetByID(blockID, >smodel.Block{}); err != nil { + if err := f.db.GetByID(ctx, blockID, >smodel.Block{}); err != nil { if err == db.ErrNoEntries { // there are no entries return false, nil diff --git a/internal/federation/federatingdb/undo.go b/internal/federation/federatingdb/undo.go index c527833b4..0fa38114d 100644 --- a/internal/federation/federatingdb/undo.go +++ b/internal/federation/federatingdb/undo.go @@ -83,7 +83,7 @@ func (f *federatingDB) Undo(ctx context.Context, undo vocab.ActivityStreamsUndo) return errors.New("UNDO: follow actor and activity actor not the same") } // convert the follow to something we can understand - gtsFollow, err := f.typeConverter.ASFollowToFollow(ASFollow) + gtsFollow, err := f.typeConverter.ASFollowToFollow(ctx, ASFollow) if err != nil { return fmt.Errorf("UNDO: error converting asfollow to gtsfollow: %s", err) } @@ -92,11 +92,11 @@ func (f *federatingDB) Undo(ctx context.Context, undo vocab.ActivityStreamsUndo) return errors.New("UNDO: follow object account and inbox account were not the same") } // delete any existing FOLLOW - if err := f.db.DeleteWhere([]db.Where{{Key: "uri", Value: gtsFollow.URI}}, >smodel.Follow{}); err != nil { + if err := f.db.DeleteWhere(ctx, []db.Where{{Key: "uri", Value: gtsFollow.URI}}, >smodel.Follow{}); err != nil { return fmt.Errorf("UNDO: db error removing follow: %s", err) } // delete any existing FOLLOW REQUEST - if err := f.db.DeleteWhere([]db.Where{{Key: "uri", Value: gtsFollow.URI}}, >smodel.FollowRequest{}); err != nil { + if err := f.db.DeleteWhere(ctx, []db.Where{{Key: "uri", Value: gtsFollow.URI}}, >smodel.FollowRequest{}); err != nil { return fmt.Errorf("UNDO: db error removing follow request: %s", err) } l.Debug("follow undone") @@ -116,7 +116,7 @@ func (f *federatingDB) Undo(ctx context.Context, undo vocab.ActivityStreamsUndo) return errors.New("UNDO: block actor and activity actor not the same") } // convert the block to something we can understand - gtsBlock, err := f.typeConverter.ASBlockToBlock(ASBlock) + gtsBlock, err := f.typeConverter.ASBlockToBlock(ctx, ASBlock) if err != nil { return fmt.Errorf("UNDO: error converting asblock to gtsblock: %s", err) } @@ -125,7 +125,7 @@ func (f *federatingDB) Undo(ctx context.Context, undo vocab.ActivityStreamsUndo) return errors.New("UNDO: block object account and inbox account were not the same") } // delete any existing BLOCK - if err := f.db.DeleteWhere([]db.Where{{Key: "uri", Value: gtsBlock.URI}}, >smodel.Block{}); err != nil { + if err := f.db.DeleteWhere(ctx, []db.Where{{Key: "uri", Value: gtsBlock.URI}}, >smodel.Block{}); err != nil { return fmt.Errorf("UNDO: db error removing block: %s", err) } l.Debug("block undone") diff --git a/internal/federation/federatingdb/update.go b/internal/federation/federatingdb/update.go index 88ffc23b4..324509ebd 100644 --- a/internal/federation/federatingdb/update.go +++ b/internal/federation/federatingdb/update.go @@ -136,7 +136,7 @@ func (f *federatingDB) Update(ctx context.Context, asType vocab.Type) error { accountable = i } - updatedAcct, err := f.typeConverter.ASRepresentationToAccount(accountable, true) + updatedAcct, err := f.typeConverter.ASRepresentationToAccount(ctx, accountable, true) if err != nil { return fmt.Errorf("UPDATE: error converting to account: %s", err) } @@ -152,7 +152,7 @@ func (f *federatingDB) Update(ctx context.Context, asType vocab.Type) error { } updatedAcct.ID = requestingAcct.ID // set this here so the db will update properly instead of trying to PUT this and getting constraint issues - if err := f.db.UpdateByID(requestingAcct.ID, updatedAcct); err != nil { + if err := f.db.UpdateByID(ctx, requestingAcct.ID, updatedAcct); err != nil { return fmt.Errorf("UPDATE: database error inserting updated account: %s", err) } diff --git a/internal/federation/federatingdb/util.go b/internal/federation/federatingdb/util.go index eac70d85c..b5befc613 100644 --- a/internal/federation/federatingdb/util.go +++ b/internal/federation/federatingdb/util.go @@ -60,7 +60,7 @@ func sameActor(activityActor vocab.ActivityStreamsActorProperty, followActor voc // // The go-fed library will handle setting the 'id' property on the // activity or object provided with the value returned. -func (f *federatingDB) NewID(c context.Context, t vocab.Type) (idURL *url.URL, err error) { +func (f *federatingDB) NewID(ctx context.Context, t vocab.Type) (idURL *url.URL, err error) { l := f.log.WithFields( logrus.Fields{ "func": "NewID", @@ -98,7 +98,7 @@ func (f *federatingDB) NewID(c context.Context, t vocab.Type) (idURL *url.URL, e // take the IRI of the first actor we can find (there should only be one) if iter.IsIRI() { // if there's an error here, just use the fallback behavior -- we don't need to return an error here - if actorAccount, err := f.db.GetAccountByURI(iter.GetIRI().String()); err == nil { + if actorAccount, err := f.db.GetAccountByURI(ctx, iter.GetIRI().String()); err == nil { newID, err := id.NewRandomULID() if err != nil { return nil, err @@ -199,7 +199,7 @@ func (f *federatingDB) NewID(c context.Context, t vocab.Type) (idURL *url.URL, e // ActorForOutbox fetches the actor's IRI for the given outbox IRI. // // The library makes this call only after acquiring a lock first. -func (f *federatingDB) ActorForOutbox(c context.Context, outboxIRI *url.URL) (actorIRI *url.URL, err error) { +func (f *federatingDB) ActorForOutbox(ctx context.Context, outboxIRI *url.URL) (actorIRI *url.URL, err error) { l := f.log.WithFields( logrus.Fields{ "func": "ActorForOutbox", @@ -212,7 +212,7 @@ func (f *federatingDB) ActorForOutbox(c context.Context, outboxIRI *url.URL) (ac return nil, fmt.Errorf("%s is not an outbox URI", outboxIRI.String()) } acct := >smodel.Account{} - if err := f.db.GetWhere([]db.Where{{Key: "outbox_uri", Value: outboxIRI.String()}}, acct); err != nil { + 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()) } @@ -224,7 +224,7 @@ func (f *federatingDB) ActorForOutbox(c context.Context, outboxIRI *url.URL) (ac // ActorForInbox fetches the actor's IRI for the given outbox IRI. // // The library makes this call only after acquiring a lock first. -func (f *federatingDB) ActorForInbox(c context.Context, inboxIRI *url.URL) (actorIRI *url.URL, err error) { +func (f *federatingDB) ActorForInbox(ctx context.Context, inboxIRI *url.URL) (actorIRI *url.URL, err error) { l := f.log.WithFields( logrus.Fields{ "func": "ActorForInbox", @@ -237,7 +237,7 @@ func (f *federatingDB) ActorForInbox(c context.Context, inboxIRI *url.URL) (acto return nil, fmt.Errorf("%s is not an inbox URI", inboxIRI.String()) } acct := >smodel.Account{} - if err := f.db.GetWhere([]db.Where{{Key: "inbox_uri", Value: inboxIRI.String()}}, acct); err != nil { + 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()) } diff --git a/internal/federation/federatingprotocol.go b/internal/federation/federatingprotocol.go index 5da68afd3..7f8958111 100644 --- a/internal/federation/federatingprotocol.go +++ b/internal/federation/federatingprotocol.go @@ -113,7 +113,7 @@ func (f *federator) AuthenticatePostInbox(ctx context.Context, w http.ResponseWr return nil, false, errors.New("username was empty") } - requestedAccount, err := f.db.GetLocalAccountByUsername(username) + requestedAccount, err := f.db.GetLocalAccountByUsername(ctx, username) if err != nil { return nil, false, fmt.Errorf("could not fetch requested account with username %s: %s", username, err) } @@ -131,14 +131,14 @@ func (f *federator) AuthenticatePostInbox(ctx context.Context, w http.ResponseWr // authentication has passed, so add an instance entry for this instance if it hasn't been done already i := >smodel.Instance{} - if err := f.db.GetWhere([]db.Where{{Key: "domain", Value: publicKeyOwnerURI.Host, CaseInsensitive: true}}, i); err != nil { + if err := f.db.GetWhere(ctx, []db.Where{{Key: "domain", Value: publicKeyOwnerURI.Host, CaseInsensitive: true}}, i); err != nil { if err != db.ErrNoEntries { // there's been an actual error return ctx, false, fmt.Errorf("error getting requesting account with public key id %s: %s", publicKeyOwnerURI.String(), err) } // we don't have an entry for this instance yet so dereference it - i, err = f.GetRemoteInstance(username, &url.URL{ + i, err = f.GetRemoteInstance(ctx, username, &url.URL{ Scheme: publicKeyOwnerURI.Scheme, Host: publicKeyOwnerURI.Host, }) @@ -147,12 +147,12 @@ func (f *federator) AuthenticatePostInbox(ctx context.Context, w http.ResponseWr } // and put it in the db - if err := f.db.Put(i); err != nil { + if err := f.db.Put(ctx, i); err != nil { return nil, false, fmt.Errorf("error inserting newly dereferenced instance %s: %s", publicKeyOwnerURI.Host, err) } } - requestingAccount, _, err := f.GetRemoteAccount(username, publicKeyOwnerURI, false) + requestingAccount, _, err := f.GetRemoteAccount(ctx, username, publicKeyOwnerURI, false) if err != nil { return nil, false, fmt.Errorf("couldn't get remote account: %s", err) } @@ -189,7 +189,7 @@ func (f *federator) Blocked(ctx context.Context, actorIRIs []*url.URL) (bool, er return false, errors.New("requested account not set on request context, so couldn't determine blocks") } - blocked, err := f.db.AreURIsBlocked(actorIRIs) + blocked, err := f.db.AreURIsBlocked(ctx, actorIRIs) if err != nil { return false, fmt.Errorf("error checking domain blocks: %s", err) } @@ -198,7 +198,7 @@ func (f *federator) Blocked(ctx context.Context, actorIRIs []*url.URL) (bool, er } for _, uri := range actorIRIs { - requestingAccount, err := f.db.GetAccountByURI(uri.String()) + requestingAccount, err := f.db.GetAccountByURI(ctx, uri.String()) if err != nil { if err == db.ErrNoEntries { // we don't have an entry for this account so it's not blocked @@ -208,7 +208,7 @@ func (f *federator) Blocked(ctx context.Context, actorIRIs []*url.URL) (bool, er return false, fmt.Errorf("error getting account with uri %s: %s", uri.String(), err) } - blocked, err = f.db.IsBlocked(requestedAccount.ID, requestingAccount.ID, true) + blocked, err = f.db.IsBlocked(ctx, requestedAccount.ID, requestingAccount.ID, true) if err != nil { return false, fmt.Errorf("error checking account block: %s", err) } diff --git a/internal/federation/federator.go b/internal/federation/federator.go index 1b5f5441a..5eddcbb99 100644 --- a/internal/federation/federator.go +++ b/internal/federation/federator.go @@ -54,21 +54,21 @@ type Federator interface { // FingerRemoteAccount performs a webfinger lookup for a remote account, using the .well-known path. It will return the ActivityPub URI for that // account, or an error if it doesn't exist or can't be retrieved. - FingerRemoteAccount(requestingUsername string, targetUsername string, targetDomain string) (*url.URL, error) + FingerRemoteAccount(ctx context.Context, requestingUsername string, targetUsername string, targetDomain string) (*url.URL, error) - DereferenceRemoteThread(username string, statusURI *url.URL) error - DereferenceAnnounce(announce *gtsmodel.Status, requestingUsername string) error + DereferenceRemoteThread(ctx context.Context, username string, statusURI *url.URL) error + DereferenceAnnounce(ctx context.Context, announce *gtsmodel.Status, requestingUsername string) error - GetRemoteAccount(username string, remoteAccountID *url.URL, refresh bool) (*gtsmodel.Account, bool, error) - EnrichRemoteAccount(username string, account *gtsmodel.Account) (*gtsmodel.Account, error) + GetRemoteAccount(ctx context.Context, username string, remoteAccountID *url.URL, refresh bool) (*gtsmodel.Account, bool, error) + EnrichRemoteAccount(ctx context.Context, username string, account *gtsmodel.Account) (*gtsmodel.Account, error) - GetRemoteStatus(username string, remoteStatusID *url.URL, refresh bool) (*gtsmodel.Status, ap.Statusable, bool, error) - EnrichRemoteStatus(username string, status *gtsmodel.Status) (*gtsmodel.Status, error) + GetRemoteStatus(ctx context.Context, username string, remoteStatusID *url.URL, refresh bool) (*gtsmodel.Status, ap.Statusable, bool, error) + EnrichRemoteStatus(ctx context.Context, username string, status *gtsmodel.Status) (*gtsmodel.Status, error) - GetRemoteInstance(username string, remoteInstanceURI *url.URL) (*gtsmodel.Instance, error) + GetRemoteInstance(ctx context.Context, username string, remoteInstanceURI *url.URL) (*gtsmodel.Instance, error) // Handshaking returns true if the given username is currently in the process of dereferencing the remoteAccountID. - Handshaking(username string, remoteAccountID *url.URL) bool + Handshaking(ctx context.Context, username string, remoteAccountID *url.URL) bool pub.CommonBehavior pub.FederatingProtocol } diff --git a/internal/federation/finger.go b/internal/federation/finger.go index a5a4fa0e7..5cdd4c04d 100644 --- a/internal/federation/finger.go +++ b/internal/federation/finger.go @@ -29,12 +29,12 @@ import ( apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" ) -func (f *federator) FingerRemoteAccount(requestingUsername string, targetUsername string, targetDomain string) (*url.URL, error) { - if blocked, err := f.db.IsDomainBlocked(targetDomain); blocked || err != nil { +func (f *federator) FingerRemoteAccount(ctx context.Context, requestingUsername string, targetUsername string, targetDomain string) (*url.URL, error) { + if blocked, err := f.db.IsDomainBlocked(ctx, targetDomain); blocked || err != nil { return nil, fmt.Errorf("FingerRemoteAccount: domain %s is blocked", targetDomain) } - t, err := f.transportController.NewTransportForUsername(requestingUsername) + t, err := f.transportController.NewTransportForUsername(ctx, requestingUsername) if err != nil { return nil, fmt.Errorf("FingerRemoteAccount: error getting transport for username %s while dereferencing @%s@%s: %s", requestingUsername, targetUsername, targetDomain, err) } diff --git a/internal/federation/handshake.go b/internal/federation/handshake.go index 0671e78a9..b973680b3 100644 --- a/internal/federation/handshake.go +++ b/internal/federation/handshake.go @@ -18,8 +18,11 @@ package federation -import "net/url" +import ( + "context" + "net/url" +) -func (f *federator) Handshaking(username string, remoteAccountID *url.URL) bool { - return f.dereferencer.Handshaking(username, remoteAccountID) +func (f *federator) Handshaking(ctx context.Context, username string, remoteAccountID *url.URL) bool { + return f.dereferencer.Handshaking(ctx, username, remoteAccountID) } diff --git a/internal/federation/transport.go b/internal/federation/transport.go index 20aee964b..9e2e38e19 100644 --- a/internal/federation/transport.go +++ b/internal/federation/transport.go @@ -68,5 +68,5 @@ func (f *federator) NewTransport(ctx context.Context, actorBoxIRI *url.URL, gofe return nil, fmt.Errorf("id %s was neither an inbox path nor an outbox path", actorBoxIRI.String()) } - return f.transportController.NewTransportForUsername(username) + return f.transportController.NewTransportForUsername(ctx, username) } diff --git a/internal/media/handler.go b/internal/media/handler.go index c383a922e..1150f7e87 100644 --- a/internal/media/handler.go +++ b/internal/media/handler.go @@ -67,27 +67,27 @@ type Handler interface { // ProcessHeaderOrAvatar takes a new header image for an account, checks it out, removes exif data from it, // puts it in whatever storage backend we're using, sets the relevant fields in the database for the new image, // and then returns information to the caller about the new header. - ProcessHeaderOrAvatar(attachment []byte, accountID string, mediaType Type, remoteURL string) (*gtsmodel.MediaAttachment, error) + ProcessHeaderOrAvatar(ctx context.Context, attachment []byte, accountID string, mediaType Type, remoteURL string) (*gtsmodel.MediaAttachment, error) // ProcessLocalAttachment takes a new attachment and the requesting account, checks it out, removes exif data from it, // puts it in whatever storage backend we're using, sets the relevant fields in the database for the new media, // and then returns information to the caller about the attachment. It's the caller's responsibility to put the returned struct // in the database. - ProcessAttachment(attachment []byte, accountID string, remoteURL string) (*gtsmodel.MediaAttachment, error) + ProcessAttachment(ctx context.Context, attachment []byte, accountID string, remoteURL string) (*gtsmodel.MediaAttachment, error) // ProcessLocalEmoji takes a new emoji and a shortcode, cleans it up, puts it in storage, and creates a new // *gts.Emoji for it, then returns it to the caller. It's the caller's responsibility to put the returned struct // in the database. - ProcessLocalEmoji(emojiBytes []byte, shortcode string) (*gtsmodel.Emoji, error) + ProcessLocalEmoji(ctx context.Context, emojiBytes []byte, shortcode string) (*gtsmodel.Emoji, error) // ProcessRemoteAttachment takes a transport, a bare-bones current attachment, and an accountID that the attachment belongs to. // It then dereferences the attachment (ie., fetches the attachment bytes from the remote server), ensuring that the bytes are // the correct content type. It stores the attachment in whatever storage backend the Handler has been initalized with, and returns // information to the caller about the new attachment. It's the caller's responsibility to put the returned struct // in the database. - ProcessRemoteAttachment(t transport.Transport, currentAttachment *gtsmodel.MediaAttachment, accountID string) (*gtsmodel.MediaAttachment, error) + ProcessRemoteAttachment(ctx context.Context, t transport.Transport, currentAttachment *gtsmodel.MediaAttachment, accountID string) (*gtsmodel.MediaAttachment, error) - ProcessRemoteHeaderOrAvatar(t transport.Transport, currentAttachment *gtsmodel.MediaAttachment, accountID string) (*gtsmodel.MediaAttachment, error) + ProcessRemoteHeaderOrAvatar(ctx context.Context, t transport.Transport, currentAttachment *gtsmodel.MediaAttachment, accountID string) (*gtsmodel.MediaAttachment, error) } type mediaHandler struct { @@ -114,7 +114,7 @@ func New(config *config.Config, database db.DB, storage blob.Storage, log *logru // ProcessHeaderOrAvatar takes a new header image for an account, checks it out, removes exif data from it, // puts it in whatever storage backend we're using, sets the relevant fields in the database for the new image, // and then returns information to the caller about the new header. -func (mh *mediaHandler) ProcessHeaderOrAvatar(attachment []byte, accountID string, mediaType Type, remoteURL string) (*gtsmodel.MediaAttachment, error) { +func (mh *mediaHandler) ProcessHeaderOrAvatar(ctx context.Context, attachment []byte, accountID string, mediaType Type, remoteURL string) (*gtsmodel.MediaAttachment, error) { l := mh.log.WithField("func", "SetHeaderForAccountID") if mediaType != Header && mediaType != Avatar { @@ -142,7 +142,7 @@ func (mh *mediaHandler) ProcessHeaderOrAvatar(attachment []byte, accountID strin } // set it in the database - if err := mh.db.SetAccountHeaderOrAvatar(ma, accountID); err != nil { + if err := mh.db.SetAccountHeaderOrAvatar(ctx, ma, accountID); err != nil { return nil, fmt.Errorf("error putting %s in database: %s", mediaType, err) } @@ -152,7 +152,7 @@ func (mh *mediaHandler) ProcessHeaderOrAvatar(attachment []byte, accountID strin // ProcessAttachment takes a new attachment and the owning account, checks it out, removes exif data from it, // puts it in whatever storage backend we're using, sets the relevant fields in the database for the new media, // and then returns information to the caller about the attachment. -func (mh *mediaHandler) ProcessAttachment(attachment []byte, accountID string, remoteURL string) (*gtsmodel.MediaAttachment, error) { +func (mh *mediaHandler) ProcessAttachment(ctx context.Context, attachment []byte, accountID string, remoteURL string) (*gtsmodel.MediaAttachment, error) { contentType, err := parseContentType(attachment) if err != nil { return nil, err @@ -184,7 +184,7 @@ func (mh *mediaHandler) ProcessAttachment(attachment []byte, accountID string, r // ProcessLocalEmoji takes a new emoji and a shortcode, cleans it up, puts it in storage, and creates a new // *gts.Emoji for it, then returns it to the caller. It's the caller's responsibility to put the returned struct // in the database. -func (mh *mediaHandler) ProcessLocalEmoji(emojiBytes []byte, shortcode string) (*gtsmodel.Emoji, error) { +func (mh *mediaHandler) ProcessLocalEmoji(ctx context.Context, emojiBytes []byte, shortcode string) (*gtsmodel.Emoji, error) { var clean []byte var err error var original *imageAndMeta @@ -231,7 +231,7 @@ func (mh *mediaHandler) ProcessLocalEmoji(emojiBytes []byte, shortcode string) ( // since emoji aren't 'owned' by an account, but we still want to use the same pattern for serving them through the filserver, // (ie., fileserver/ACCOUNT_ID/etc etc) we need to fetch the INSTANCE ACCOUNT from the database. That is, the account that's created // with the same username as the instance hostname, which doesn't belong to any particular user. - instanceAccount, err := mh.db.GetInstanceAccount("") + instanceAccount, err := mh.db.GetInstanceAccount(ctx, "") if err != nil { return nil, fmt.Errorf("error fetching instance account: %s", err) } @@ -296,7 +296,7 @@ func (mh *mediaHandler) ProcessLocalEmoji(emojiBytes []byte, shortcode string) ( return e, nil } -func (mh *mediaHandler) ProcessRemoteAttachment(t transport.Transport, currentAttachment *gtsmodel.MediaAttachment, accountID string) (*gtsmodel.MediaAttachment, error) { +func (mh *mediaHandler) ProcessRemoteAttachment(ctx context.Context, t transport.Transport, currentAttachment *gtsmodel.MediaAttachment, accountID string) (*gtsmodel.MediaAttachment, error) { if currentAttachment.RemoteURL == "" { return nil, errors.New("no remote URL on media attachment to dereference") } @@ -317,10 +317,10 @@ func (mh *mediaHandler) ProcessRemoteAttachment(t transport.Transport, currentAt return nil, fmt.Errorf("dereferencing remote media with url %s: %s", remoteIRI.String(), err) } - return mh.ProcessAttachment(attachmentBytes, accountID, currentAttachment.RemoteURL) + return mh.ProcessAttachment(ctx, attachmentBytes, accountID, currentAttachment.RemoteURL) } -func (mh *mediaHandler) ProcessRemoteHeaderOrAvatar(t transport.Transport, currentAttachment *gtsmodel.MediaAttachment, accountID string) (*gtsmodel.MediaAttachment, error) { +func (mh *mediaHandler) ProcessRemoteHeaderOrAvatar(ctx context.Context, t transport.Transport, currentAttachment *gtsmodel.MediaAttachment, accountID string) (*gtsmodel.MediaAttachment, error) { if !currentAttachment.Header && !currentAttachment.Avatar { return nil, errors.New("provided attachment was set to neither header nor avatar") @@ -357,5 +357,5 @@ func (mh *mediaHandler) ProcessRemoteHeaderOrAvatar(t transport.Transport, curre return nil, fmt.Errorf("dereferencing remote media with url %s: %s", remoteIRI.String(), err) } - return mh.ProcessHeaderOrAvatar(attachmentBytes, accountID, headerOrAvi, currentAttachment.RemoteURL) + return mh.ProcessHeaderOrAvatar(ctx, attachmentBytes, accountID, headerOrAvi, currentAttachment.RemoteURL) } diff --git a/internal/oauth/clientstore.go b/internal/oauth/clientstore.go index 2e7e0ae88..2f04243cb 100644 --- a/internal/oauth/clientstore.go +++ b/internal/oauth/clientstore.go @@ -42,7 +42,7 @@ func (cs *clientStore) GetByID(ctx context.Context, clientID string) (oauth2.Cli poc := &Client{ ID: clientID, } - if err := cs.db.GetByID(clientID, poc); err != nil { + if err := cs.db.GetByID(ctx, clientID, poc); err != nil { return nil, err } return models.New(poc.ID, poc.Secret, poc.Domain, poc.UserID), nil @@ -55,14 +55,14 @@ func (cs *clientStore) Set(ctx context.Context, id string, cli oauth2.ClientInfo Domain: cli.GetDomain(), UserID: cli.GetUserID(), } - return cs.db.UpdateByID(id, poc) + return cs.db.UpdateByID(ctx, id, poc) } func (cs *clientStore) Delete(ctx context.Context, id string) error { poc := &Client{ ID: id, } - return cs.db.DeleteByID(id, poc) + return cs.db.DeleteByID(ctx, id, poc) } // Client is a handy little wrapper for typical oauth client details diff --git a/internal/oauth/tokenstore.go b/internal/oauth/tokenstore.go index 4fd3183fc..33b4f7d00 100644 --- a/internal/oauth/tokenstore.go +++ b/internal/oauth/tokenstore.go @@ -58,7 +58,7 @@ func newTokenStore(ctx context.Context, db db.Basic, log *logrus.Logger) oauth2. break cleanloop case <-time.After(1 * time.Minute): log.Trace("sweeping out old oauth entries broom broom") - if err := pts.sweep(); err != nil { + if err := pts.sweep(ctx); err != nil { log.Errorf("error while sweeping oauth entries: %s", err) } } @@ -68,11 +68,11 @@ func newTokenStore(ctx context.Context, db db.Basic, log *logrus.Logger) oauth2. } // sweep clears out old tokens that have expired; it should be run on a loop about once per minute or so. -func (pts *tokenStore) sweep() error { +func (pts *tokenStore) sweep(ctx context.Context) error { // select *all* tokens from the db // todo: if this becomes expensive (ie., there are fucking LOADS of tokens) then figure out a better way. tokens := new([]*Token) - if err := pts.db.GetAll(tokens); err != nil { + if err := pts.db.GetAll(ctx, tokens); err != nil { return err } @@ -83,7 +83,7 @@ func (pts *tokenStore) sweep() error { // we only want to check if a token expired before now if the expiry time is *not zero*; // ie., if it's been explicity set. if !pgt.CodeExpiresAt.IsZero() && pgt.CodeExpiresAt.Before(now) || !pgt.RefreshExpiresAt.IsZero() && pgt.RefreshExpiresAt.Before(now) || !pgt.AccessExpiresAt.IsZero() && pgt.AccessExpiresAt.Before(now) { - if err := pts.db.DeleteByID(pgt.ID, pgt); err != nil { + if err := pts.db.DeleteByID(ctx, pgt.ID, pgt); err != nil { return err } } @@ -109,7 +109,7 @@ func (pts *tokenStore) Create(ctx context.Context, info oauth2.TokenInfo) error pgt.ID = pgtID } - if err := pts.db.Put(pgt); err != nil { + if err := pts.db.Put(ctx, pgt); err != nil { return fmt.Errorf("error in tokenstore create: %s", err) } return nil @@ -117,17 +117,17 @@ func (pts *tokenStore) Create(ctx context.Context, info oauth2.TokenInfo) error // RemoveByCode deletes a token from the DB based on the Code field func (pts *tokenStore) RemoveByCode(ctx context.Context, code string) error { - return pts.db.DeleteWhere([]db.Where{{Key: "code", Value: code}}, &Token{}) + return pts.db.DeleteWhere(ctx, []db.Where{{Key: "code", Value: code}}, &Token{}) } // RemoveByAccess deletes a token from the DB based on the Access field func (pts *tokenStore) RemoveByAccess(ctx context.Context, access string) error { - return pts.db.DeleteWhere([]db.Where{{Key: "access", Value: access}}, &Token{}) + return pts.db.DeleteWhere(ctx, []db.Where{{Key: "access", Value: access}}, &Token{}) } // RemoveByRefresh deletes a token from the DB based on the Refresh field func (pts *tokenStore) RemoveByRefresh(ctx context.Context, refresh string) error { - return pts.db.DeleteWhere([]db.Where{{Key: "refresh", Value: refresh}}, &Token{}) + return pts.db.DeleteWhere(ctx, []db.Where{{Key: "refresh", Value: refresh}}, &Token{}) } // GetByCode selects a token from the DB based on the Code field @@ -138,7 +138,7 @@ func (pts *tokenStore) GetByCode(ctx context.Context, code string) (oauth2.Token pgt := &Token{ Code: code, } - if err := pts.db.GetWhere([]db.Where{{Key: "code", Value: code}}, pgt); err != nil { + if err := pts.db.GetWhere(ctx, []db.Where{{Key: "code", Value: code}}, pgt); err != nil { return nil, err } return TokenToOauthToken(pgt), nil @@ -152,7 +152,7 @@ func (pts *tokenStore) GetByAccess(ctx context.Context, access string) (oauth2.T pgt := &Token{ Access: access, } - if err := pts.db.GetWhere([]db.Where{{Key: "access", Value: access}}, pgt); err != nil { + if err := pts.db.GetWhere(ctx, []db.Where{{Key: "access", Value: access}}, pgt); err != nil { return nil, err } return TokenToOauthToken(pgt), nil @@ -166,7 +166,7 @@ func (pts *tokenStore) GetByRefresh(ctx context.Context, refresh string) (oauth2 pgt := &Token{ Refresh: refresh, } - if err := pts.db.GetWhere([]db.Where{{Key: "refresh", Value: refresh}}, pgt); err != nil { + if err := pts.db.GetWhere(ctx, []db.Where{{Key: "refresh", Value: refresh}}, pgt); err != nil { return nil, err } return TokenToOauthToken(pgt), nil diff --git a/internal/router/router.go b/internal/router/router.go index c5f105448..621d93ff5 100644 --- a/internal/router/router.go +++ b/internal/router/router.go @@ -103,7 +103,7 @@ func (r *router) Stop(ctx context.Context) error { // // The given DB is only used in the New function for parsing config values, and is not otherwise // pinned to the router. -func New(cfg *config.Config, db db.DB, logger *logrus.Logger) (Router, error) { +func New(ctx context.Context, cfg *config.Config, db db.DB, logger *logrus.Logger) (Router, error) { // gin has different log modes; for convenience, we match the gin log mode to // whatever log mode has been set for logrus @@ -141,7 +141,7 @@ func New(cfg *config.Config, db db.DB, logger *logrus.Logger) (Router, error) { } // enable session store middleware on the engine - if err := useSession(cfg, db, engine); err != nil { + if err := useSession(ctx, cfg, db, engine); err != nil { return nil, err } diff --git a/internal/router/session.go b/internal/router/session.go index 38810572f..f336521d2 100644 --- a/internal/router/session.go +++ b/internal/router/session.go @@ -19,6 +19,7 @@ package router import ( + "context" "crypto/rand" "errors" "fmt" @@ -45,10 +46,10 @@ func SessionOptions(cfg *config.Config) sessions.Options { } } -func useSession(cfg *config.Config, dbService db.DB, engine *gin.Engine) error { +func useSession(ctx context.Context, cfg *config.Config, dbService db.DB, engine *gin.Engine) error { // check if we have a saved router session already routerSessions := []*gtsmodel.RouterSession{} - if err := dbService.GetAll(&routerSessions); err != nil { + if err := dbService.GetAll(ctx, &routerSessions); err != nil { if err != db.ErrNoEntries { // proper error occurred return err @@ -62,7 +63,7 @@ func useSession(cfg *config.Config, dbService db.DB, engine *gin.Engine) error { } else if len(routerSessions) == 0 { // we have no router sessions so we need to create a new one var err error - rs, err = routerSession(dbService) + rs, err = routerSession(ctx, dbService) if err != nil { return fmt.Errorf("error creating new router session: %s", err) } @@ -84,7 +85,7 @@ func useSession(cfg *config.Config, dbService db.DB, engine *gin.Engine) error { // routerSession generates a new router session with random auth and crypt bytes, // puts it in the database for persistence, and returns it for use. -func routerSession(dbService db.DB) (*gtsmodel.RouterSession, error) { +func routerSession(ctx context.Context, dbService db.DB) (*gtsmodel.RouterSession, error) { auth := make([]byte, 32) crypt := make([]byte, 32) @@ -106,7 +107,7 @@ func routerSession(dbService db.DB) (*gtsmodel.RouterSession, error) { Crypt: crypt, } - if err := dbService.Put(rs); err != nil { + if err := dbService.Put(ctx, rs); err != nil { return nil, err } diff --git a/internal/text/common.go b/internal/text/common.go index af77521dd..ecf7a7a98 100644 --- a/internal/text/common.go +++ b/internal/text/common.go @@ -19,6 +19,7 @@ package text import ( + "context" "fmt" "html" "strings" @@ -59,7 +60,7 @@ func postformat(in string) string { return mini } -func (f *formatter) ReplaceTags(in string, tags []*gtsmodel.Tag) string { +func (f *formatter) ReplaceTags(ctx context.Context, in string, tags []*gtsmodel.Tag) string { return util.HashtagFinderRegex.ReplaceAllStringFunc(in, func(match string) string { // we have a match matchTrimmed := strings.TrimSpace(match) @@ -88,7 +89,7 @@ func (f *formatter) ReplaceTags(in string, tags []*gtsmodel.Tag) string { }) } -func (f *formatter) ReplaceMentions(in string, mentions []*gtsmodel.Mention) string { +func (f *formatter) ReplaceMentions(ctx context.Context, in string, mentions []*gtsmodel.Mention) string { for _, menchie := range mentions { // make sure we have a target account, either by getting one pinned on the mention, // or by pulling it from the database @@ -98,7 +99,7 @@ func (f *formatter) ReplaceMentions(in string, mentions []*gtsmodel.Mention) str targetAccount = menchie.OriginAccount } else { a := >smodel.Account{} - if err := f.db.GetByID(menchie.TargetAccountID, a); err == nil { + if err := f.db.GetByID(ctx, menchie.TargetAccountID, a); err == nil { // got it from the db targetAccount = a } else { diff --git a/internal/text/common_test.go b/internal/text/common_test.go index 69fe7d446..174b79177 100644 --- a/internal/text/common_test.go +++ b/internal/text/common_test.go @@ -19,6 +19,7 @@ package text_test import ( + "context" "testing" "github.com/stretchr/testify/assert" @@ -87,7 +88,7 @@ func (suite *CommonTestSuite) TestReplaceMentions() { suite.testMentions["zork_mention_foss_satan"], } - f := suite.formatter.ReplaceMentions(replaceMentionsString, foundMentions) + f := suite.formatter.ReplaceMentions(context.Background(), replaceMentionsString, foundMentions) assert.Equal(suite.T(), replaceMentionsExpected, f) } @@ -96,7 +97,7 @@ func (suite *CommonTestSuite) TestReplaceHashtags() { suite.testTags["Hashtag"], } - f := suite.formatter.ReplaceTags(replaceMentionsString, foundTags) + f := suite.formatter.ReplaceTags(context.Background(), replaceMentionsString, foundTags) assert.Equal(suite.T(), replaceHashtagsExpected, f) } @@ -106,7 +107,7 @@ func (suite *CommonTestSuite) TestReplaceHashtagsAfterReplaceMentions() { suite.testTags["Hashtag"], } - f := suite.formatter.ReplaceTags(replaceMentionsExpected, foundTags) + f := suite.formatter.ReplaceTags(context.Background(), replaceMentionsExpected, foundTags) assert.Equal(suite.T(), replaceHashtagsAfterMentionsExpected, f) } diff --git a/internal/text/formatter.go b/internal/text/formatter.go index 39aaae559..769ecafbb 100644 --- a/internal/text/formatter.go +++ b/internal/text/formatter.go @@ -19,6 +19,8 @@ package text import ( + "context" + "github.com/sirupsen/logrus" "github.com/superseriousbusiness/gotosocial/internal/config" "github.com/superseriousbusiness/gotosocial/internal/db" @@ -28,16 +30,16 @@ import ( // Formatter wraps some logic and functions for parsing statuses and other text input into nice html. type Formatter interface { // FromMarkdown parses an HTML text from a markdown-formatted text. - FromMarkdown(md string, mentions []*gtsmodel.Mention, tags []*gtsmodel.Tag) string + FromMarkdown(ctx context.Context, md string, mentions []*gtsmodel.Mention, tags []*gtsmodel.Tag) string // FromPlain parses an HTML text from a plaintext. - FromPlain(plain string, mentions []*gtsmodel.Mention, tags []*gtsmodel.Tag) string + FromPlain(ctx context.Context, plain string, mentions []*gtsmodel.Mention, tags []*gtsmodel.Tag) string // ReplaceTags takes a piece of text and a slice of tags, and returns the same text with the tags nicely formatted as hrefs. - ReplaceTags(in string, tags []*gtsmodel.Tag) string + ReplaceTags(ctx context.Context, in string, tags []*gtsmodel.Tag) string // ReplaceMentions takes a piece of text and a slice of mentions, and returns the same text with the mentions nicely formatted as hrefs. - ReplaceMentions(in string, mentions []*gtsmodel.Mention) string + ReplaceMentions(ctx context.Context, in string, mentions []*gtsmodel.Mention) string // ReplaceLinks takes a piece of text, finds all recognizable links in that text, and replaces them with hrefs. - ReplaceLinks(in string) string + ReplaceLinks(ctx context.Context, in string) string } type formatter struct { diff --git a/internal/text/link.go b/internal/text/link.go index d42cc3b68..0a0f0c60d 100644 --- a/internal/text/link.go +++ b/internal/text/link.go @@ -19,6 +19,7 @@ package text import ( + "context" "fmt" "net/url" @@ -82,7 +83,7 @@ func contains(urls []*url.URL, url *url.URL) bool { // Note: because Go doesn't allow negative lookbehinds in regex, it's possible that an already-formatted // href will end up double-formatted, if the text you pass here contains one or more hrefs already. // To avoid this, you should sanitize any HTML out of text before you pass it into this function. -func (f *formatter) ReplaceLinks(in string) string { +func (f *formatter) ReplaceLinks(ctx context.Context, in string) string { rxStrict, err := xurls.StrictMatchingScheme(schemes) if err != nil { panic(err) diff --git a/internal/text/link_test.go b/internal/text/link_test.go index 83c42f045..f8d6a1adc 100644 --- a/internal/text/link_test.go +++ b/internal/text/link_test.go @@ -19,6 +19,7 @@ package text_test import ( + "context" "testing" "github.com/stretchr/testify/assert" @@ -94,7 +95,7 @@ func (suite *LinkTestSuite) TearDownTest() { } func (suite *LinkTestSuite) TestParseSimple() { - f := suite.formatter.FromPlain(simple, nil, nil) + f := suite.formatter.FromPlain(context.Background(), simple, nil, nil) assert.Equal(suite.T(), simpleExpected, f) } @@ -126,7 +127,7 @@ func (suite *LinkTestSuite) TestParseURLsFromText3() { } func (suite *LinkTestSuite) TestReplaceLinksFromText1() { - replaced := suite.formatter.ReplaceLinks(text1) + replaced := suite.formatter.ReplaceLinks(context.Background(), text1) assert.Equal(suite.T(), ` This is a text with some links in it. Here's link number one: example.org/link/to/something#fragment @@ -141,7 +142,7 @@ really.cool.website <-- this one shouldn't be parsed as a link because it doesn' } func (suite *LinkTestSuite) TestReplaceLinksFromText2() { - replaced := suite.formatter.ReplaceLinks(text2) + replaced := suite.formatter.ReplaceLinks(context.Background(), text2) assert.Equal(suite.T(), ` this is one link: example.org @@ -153,14 +154,14 @@ these should be deduplicated func (suite *LinkTestSuite) TestReplaceLinksFromText3() { // we know mailto links won't be replaced with hrefs -- we only accept https and http - replaced := suite.formatter.ReplaceLinks(text3) + replaced := suite.formatter.ReplaceLinks(context.Background(), text3) assert.Equal(suite.T(), ` here's a mailto link: mailto:whatever@test.org `, replaced) } func (suite *LinkTestSuite) TestReplaceLinksFromText4() { - replaced := suite.formatter.ReplaceLinks(text4) + replaced := suite.formatter.ReplaceLinks(context.Background(), text4) assert.Equal(suite.T(), ` two similar links: @@ -172,7 +173,7 @@ two similar links: func (suite *LinkTestSuite) TestReplaceLinksFromText5() { // we know this one doesn't work properly, which is why html should always be sanitized before being passed into the ReplaceLinks function - replaced := suite.formatter.ReplaceLinks(text5) + replaced := suite.formatter.ReplaceLinks(context.Background(), text5) assert.Equal(suite.T(), ` what happens when we already have a link within an href? diff --git a/internal/text/markdown.go b/internal/text/markdown.go index 5a7603615..eeeae0edf 100644 --- a/internal/text/markdown.go +++ b/internal/text/markdown.go @@ -19,21 +19,23 @@ package text import ( + "context" + "github.com/russross/blackfriday/v2" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" ) -func (f *formatter) FromMarkdown(md string, mentions []*gtsmodel.Mention, tags []*gtsmodel.Tag) string { +func (f *formatter) FromMarkdown(ctx context.Context, md string, mentions []*gtsmodel.Mention, tags []*gtsmodel.Tag) string { content := preformat(md) // do the markdown parsing *first* contentBytes := blackfriday.Run([]byte(content)) // format tags nicely - content = f.ReplaceTags(string(contentBytes), tags) + content = f.ReplaceTags(ctx, string(contentBytes), tags) // format mentions nicely - content = f.ReplaceMentions(content, mentions) + content = f.ReplaceMentions(ctx, content, mentions) return postformat(content) } diff --git a/internal/text/markdown_test.go b/internal/text/markdown_test.go index 432e9a4ec..085f211d2 100644 --- a/internal/text/markdown_test.go +++ b/internal/text/markdown_test.go @@ -19,6 +19,7 @@ package text_test import ( + "context" "fmt" "testing" @@ -92,13 +93,13 @@ func (suite *MarkdownTestSuite) TearDownTest() { } func (suite *MarkdownTestSuite) TestParseSimple() { - s := suite.formatter.FromMarkdown(simpleMarkdown, nil, nil) + s := suite.formatter.FromMarkdown(context.Background(), simpleMarkdown, nil, nil) suite.Equal(simpleMarkdownExpected, s) } func (suite *MarkdownTestSuite) TestParseWithCodeBlock() { fmt.Println(withCodeBlock) - s := suite.formatter.FromMarkdown(withCodeBlock, nil, nil) + s := suite.formatter.FromMarkdown(context.Background(), withCodeBlock, nil, nil) suite.Equal(withCodeBlockExpected, s) } @@ -107,7 +108,7 @@ func (suite *MarkdownTestSuite) TestParseWithHashtag() { suite.testTags["Hashtag"], } - s := suite.formatter.FromMarkdown(withHashtag, nil, foundTags) + s := suite.formatter.FromMarkdown(context.Background(), withHashtag, nil, foundTags) suite.Equal(withHashtagExpected, s) } diff --git a/internal/text/plain.go b/internal/text/plain.go index a44e02c80..34cc3fa06 100644 --- a/internal/text/plain.go +++ b/internal/text/plain.go @@ -19,26 +19,27 @@ package text import ( + "context" "fmt" "strings" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" ) -func (f *formatter) FromPlain(plain string, mentions []*gtsmodel.Mention, tags []*gtsmodel.Tag) string { +func (f *formatter) FromPlain(ctx context.Context, plain string, mentions []*gtsmodel.Mention, tags []*gtsmodel.Tag) string { content := preformat(plain) // sanitize any html elements content = RemoveHTML(content) // format links nicely - content = f.ReplaceLinks(content) + content = f.ReplaceLinks(ctx, content) // format tags nicely - content = f.ReplaceTags(content, tags) + content = f.ReplaceTags(ctx, content, tags) // format mentions nicely - content = f.ReplaceMentions(content, mentions) + content = f.ReplaceMentions(ctx, content, mentions) // replace newlines with breaks content = strings.ReplaceAll(content, "\n", "
") diff --git a/internal/text/plain_test.go b/internal/text/plain_test.go index 33c95234c..62c43406d 100644 --- a/internal/text/plain_test.go +++ b/internal/text/plain_test.go @@ -19,6 +19,7 @@ package text_test import ( + "context" "fmt" "testing" @@ -74,7 +75,7 @@ func (suite *PlainTestSuite) TearDownTest() { } func (suite *PlainTestSuite) TestParseSimple() { - f := suite.formatter.FromPlain(simple, nil, nil) + f := suite.formatter.FromPlain(context.Background(), simple, nil, nil) assert.Equal(suite.T(), simpleExpected, f) } @@ -84,7 +85,7 @@ func (suite *PlainTestSuite) TestParseWithTag() { suite.testTags["welcome"], } - f := suite.formatter.FromPlain(withTag, nil, foundTags) + f := suite.formatter.FromPlain(context.Background(), withTag, nil, foundTags) assert.Equal(suite.T(), withTagExpected, f) } @@ -98,7 +99,7 @@ func (suite *PlainTestSuite) TestParseMoreComplex() { suite.testMentions["zork_mention_foss_satan"], } - f := suite.formatter.FromPlain(moreComplex, foundMentions, foundTags) + f := suite.formatter.FromPlain(context.Background(), moreComplex, foundMentions, foundTags) fmt.Println(f) diff --git a/internal/timeline/get.go b/internal/timeline/get.go index d800da4e3..c22935550 100644 --- a/internal/timeline/get.go +++ b/internal/timeline/get.go @@ -20,6 +20,7 @@ package timeline import ( "container/list" + "context" "errors" "fmt" @@ -29,7 +30,7 @@ import ( const retries = 5 -func (t *timeline) Get(amount int, maxID string, sinceID string, minID string, prepareNext bool) ([]*apimodel.Status, error) { +func (t *timeline) Get(ctx context.Context, amount int, maxID string, sinceID string, minID string, prepareNext bool) ([]*apimodel.Status, error) { l := t.log.WithFields(logrus.Fields{ "func": "Get", "accountID": t.accountID, @@ -46,14 +47,14 @@ func (t *timeline) Get(amount int, maxID string, sinceID string, minID string, p // no params are defined to just fetch from the top // this is equivalent to a user asking for the top x posts from their timeline if maxID == "" && sinceID == "" && minID == "" { - statuses, err = t.GetXFromTop(amount) + statuses, err = t.GetXFromTop(ctx, amount) // aysnchronously prepare the next predicted query so it's ready when the user asks for it if len(statuses) != 0 { nextMaxID := statuses[len(statuses)-1].ID if prepareNext { // already cache the next query to speed up scrolling go func() { - if err := t.prepareNextQuery(amount, nextMaxID, "", ""); err != nil { + if err := t.prepareNextQuery(ctx, amount, nextMaxID, "", ""); err != nil { l.Errorf("error preparing next query: %s", err) } }() @@ -65,14 +66,14 @@ func (t *timeline) Get(amount int, maxID string, sinceID string, minID string, p // this is equivalent to a user asking for the next x posts from their timeline, starting from maxID if maxID != "" && sinceID == "" { attempts := 0 - statuses, err = t.GetXBehindID(amount, maxID, &attempts) + statuses, err = t.GetXBehindID(ctx, amount, maxID, &attempts) // aysnchronously prepare the next predicted query so it's ready when the user asks for it if len(statuses) != 0 { nextMaxID := statuses[len(statuses)-1].ID if prepareNext { // already cache the next query to speed up scrolling go func() { - if err := t.prepareNextQuery(amount, nextMaxID, "", ""); err != nil { + if err := t.prepareNextQuery(ctx, amount, nextMaxID, "", ""); err != nil { l.Errorf("error preparing next query: %s", err) } }() @@ -83,25 +84,25 @@ func (t *timeline) Get(amount int, maxID string, sinceID string, minID string, p // maxID is defined and sinceID || minID are as well, so take a slice between them // this is equivalent to a user asking for posts older than x but newer than y if maxID != "" && sinceID != "" { - statuses, err = t.GetXBetweenID(amount, maxID, minID) + statuses, err = t.GetXBetweenID(ctx, amount, maxID, minID) } if maxID != "" && minID != "" { - statuses, err = t.GetXBetweenID(amount, maxID, minID) + statuses, err = t.GetXBetweenID(ctx, amount, maxID, minID) } // maxID isn't defined, but sinceID || minID are, so take x before // this is equivalent to a user asking for posts newer than x (eg., refreshing the top of their timeline) if maxID == "" && sinceID != "" { - statuses, err = t.GetXBeforeID(amount, sinceID, true) + statuses, err = t.GetXBeforeID(ctx, amount, sinceID, true) } if maxID == "" && minID != "" { - statuses, err = t.GetXBeforeID(amount, minID, true) + statuses, err = t.GetXBeforeID(ctx, amount, minID, true) } return statuses, err } -func (t *timeline) GetXFromTop(amount int) ([]*apimodel.Status, error) { +func (t *timeline) GetXFromTop(ctx context.Context, amount int) ([]*apimodel.Status, error) { // make a slice of statuses with the length we need to return statuses := make([]*apimodel.Status, 0, amount) @@ -111,7 +112,7 @@ func (t *timeline) GetXFromTop(amount int) ([]*apimodel.Status, error) { // make sure we have enough posts prepared to return if t.preparedPosts.data.Len() < amount { - if err := t.PrepareFromTop(amount); err != nil { + if err := t.PrepareFromTop(ctx, amount); err != nil { return nil, err } } @@ -133,7 +134,7 @@ func (t *timeline) GetXFromTop(amount int) ([]*apimodel.Status, error) { return statuses, nil } -func (t *timeline) GetXBehindID(amount int, behindID string, attempts *int) ([]*apimodel.Status, error) { +func (t *timeline) GetXBehindID(ctx context.Context, amount int, behindID string, attempts *int) ([]*apimodel.Status, error) { l := t.log.WithFields(logrus.Fields{ "func": "GetXBehindID", "amount": amount, @@ -174,10 +175,10 @@ findMarkLoop: // we didn't find it, so we need to make sure it's indexed and prepared and then try again // this can happen when a user asks for really old posts if behindIDMark == nil { - if err := t.PrepareBehind(behindID, amount); err != nil { + if err := t.PrepareBehind(ctx, behindID, amount); err != nil { return nil, fmt.Errorf("GetXBehindID: error preparing behind and including ID %s", behindID) } - oldestID, err := t.OldestPreparedPostID() + oldestID, err := t.OldestPreparedPostID(ctx) if err != nil { return nil, err } @@ -194,12 +195,12 @@ findMarkLoop: return statuses, nil } l.Trace("trying GetXBehindID again") - return t.GetXBehindID(amount, behindID, attempts) + return t.GetXBehindID(ctx, amount, behindID, attempts) } // make sure we have enough posts prepared behind it to return what we're being asked for if t.preparedPosts.data.Len() < amount+position { - if err := t.PrepareBehind(behindID, amount); err != nil { + if err := t.PrepareBehind(ctx, behindID, amount); err != nil { return nil, err } } @@ -224,7 +225,7 @@ serveloop: return statuses, nil } -func (t *timeline) GetXBeforeID(amount int, beforeID string, startFromTop bool) ([]*apimodel.Status, error) { +func (t *timeline) GetXBeforeID(ctx context.Context, amount int, beforeID string, startFromTop bool) ([]*apimodel.Status, error) { // make a slice of statuses with the length we need to return statuses := make([]*apimodel.Status, 0, amount) @@ -295,7 +296,7 @@ findMarkLoop: return statuses, nil } -func (t *timeline) GetXBetweenID(amount int, behindID string, beforeID string) ([]*apimodel.Status, error) { +func (t *timeline) GetXBetweenID(ctx context.Context, amount int, behindID string, beforeID string) ([]*apimodel.Status, error) { // make a slice of statuses with the length we need to return statuses := make([]*apimodel.Status, 0, amount) @@ -327,7 +328,7 @@ findMarkLoop: // make sure we have enough posts prepared behind it to return what we're being asked for if t.preparedPosts.data.Len() < amount+position { - if err := t.PrepareBehind(behindID, amount); err != nil { + if err := t.PrepareBehind(ctx, behindID, amount); err != nil { return nil, err } } diff --git a/internal/timeline/get_test.go b/internal/timeline/get_test.go index 0866f3bdd..96c333c5f 100644 --- a/internal/timeline/get_test.go +++ b/internal/timeline/get_test.go @@ -19,6 +19,7 @@ package timeline_test import ( + "context" "testing" "time" @@ -45,14 +46,14 @@ func (suite *GetTestSuite) SetupTest() { testrig.StandardDBSetup(suite.db, nil) // let's take local_account_1 as the timeline owner - tl, err := timeline.NewTimeline(suite.testAccounts["local_account_1"].ID, suite.db, suite.tc, suite.log) + tl, err := timeline.NewTimeline(context.Background(), suite.testAccounts["local_account_1"].ID, suite.db, suite.tc, suite.log) if err != nil { suite.FailNow(err.Error()) } // prepare the timeline by just shoving all test statuses in it -- let's not be fussy about who sees what for _, s := range suite.testStatuses { - _, err := tl.IndexAndPrepareOne(s.CreatedAt, s.ID, s.BoostOfID, s.AccountID, s.BoostOfAccountID) + _, err := tl.IndexAndPrepareOne(context.Background(), s.CreatedAt, s.ID, s.BoostOfID, s.AccountID, s.BoostOfAccountID) if err != nil { suite.FailNow(err.Error()) } @@ -67,7 +68,7 @@ func (suite *GetTestSuite) TearDownTest() { func (suite *GetTestSuite) TestGetDefault() { // get 10 20 the top and don't prepare the next query - statuses, err := suite.timeline.Get(20, "", "", "", false) + statuses, err := suite.timeline.Get(context.Background(), 20, "", "", "", false) if err != nil { suite.FailNow(err.Error()) } @@ -89,7 +90,7 @@ func (suite *GetTestSuite) TestGetDefault() { func (suite *GetTestSuite) TestGetDefaultPrepareNext() { // get 10 from the top and prepare the next query - statuses, err := suite.timeline.Get(10, "", "", "", true) + statuses, err := suite.timeline.Get(context.Background(), 10, "", "", "", true) if err != nil { suite.FailNow(err.Error()) } @@ -113,7 +114,7 @@ func (suite *GetTestSuite) TestGetDefaultPrepareNext() { func (suite *GetTestSuite) TestGetMaxID() { // ask for 10 with a max ID somewhere in the middle of the stack - statuses, err := suite.timeline.Get(10, "01F8MHBQCBTDKN6X5VHGMMN4MA", "", "", false) + statuses, err := suite.timeline.Get(context.Background(), 10, "01F8MHBQCBTDKN6X5VHGMMN4MA", "", "", false) if err != nil { suite.FailNow(err.Error()) } @@ -135,7 +136,7 @@ func (suite *GetTestSuite) TestGetMaxID() { func (suite *GetTestSuite) TestGetMaxIDPrepareNext() { // ask for 10 with a max ID somewhere in the middle of the stack - statuses, err := suite.timeline.Get(10, "01F8MHBQCBTDKN6X5VHGMMN4MA", "", "", true) + statuses, err := suite.timeline.Get(context.Background(), 10, "01F8MHBQCBTDKN6X5VHGMMN4MA", "", "", true) if err != nil { suite.FailNow(err.Error()) } @@ -160,7 +161,7 @@ func (suite *GetTestSuite) TestGetMaxIDPrepareNext() { func (suite *GetTestSuite) TestGetMinID() { // ask for 10 with a min ID somewhere in the middle of the stack - statuses, err := suite.timeline.Get(10, "", "01F8MHBQCBTDKN6X5VHGMMN4MA", "", false) + statuses, err := suite.timeline.Get(context.Background(), 10, "", "01F8MHBQCBTDKN6X5VHGMMN4MA", "", false) if err != nil { suite.FailNow(err.Error()) } @@ -182,7 +183,7 @@ func (suite *GetTestSuite) TestGetMinID() { func (suite *GetTestSuite) TestGetSinceID() { // ask for 10 with a since ID somewhere in the middle of the stack - statuses, err := suite.timeline.Get(10, "", "", "01F8MHBQCBTDKN6X5VHGMMN4MA", false) + statuses, err := suite.timeline.Get(context.Background(), 10, "", "", "01F8MHBQCBTDKN6X5VHGMMN4MA", false) if err != nil { suite.FailNow(err.Error()) } @@ -204,7 +205,7 @@ func (suite *GetTestSuite) TestGetSinceID() { func (suite *GetTestSuite) TestGetSinceIDPrepareNext() { // ask for 10 with a since ID somewhere in the middle of the stack - statuses, err := suite.timeline.Get(10, "", "", "01F8MHBQCBTDKN6X5VHGMMN4MA", true) + statuses, err := suite.timeline.Get(context.Background(), 10, "", "", "01F8MHBQCBTDKN6X5VHGMMN4MA", true) if err != nil { suite.FailNow(err.Error()) } @@ -229,7 +230,7 @@ func (suite *GetTestSuite) TestGetSinceIDPrepareNext() { func (suite *GetTestSuite) TestGetBetweenID() { // ask for 10 between these two IDs - statuses, err := suite.timeline.Get(10, "01F8MHCP5P2NWYQ416SBA0XSEV", "", "01F8MHBQCBTDKN6X5VHGMMN4MA", false) + statuses, err := suite.timeline.Get(context.Background(), 10, "01F8MHCP5P2NWYQ416SBA0XSEV", "", "01F8MHBQCBTDKN6X5VHGMMN4MA", false) if err != nil { suite.FailNow(err.Error()) } @@ -251,7 +252,7 @@ func (suite *GetTestSuite) TestGetBetweenID() { func (suite *GetTestSuite) TestGetBetweenIDPrepareNext() { // ask for 10 between these two IDs - statuses, err := suite.timeline.Get(10, "01F8MHCP5P2NWYQ416SBA0XSEV", "", "01F8MHBQCBTDKN6X5VHGMMN4MA", true) + statuses, err := suite.timeline.Get(context.Background(), 10, "01F8MHCP5P2NWYQ416SBA0XSEV", "", "01F8MHBQCBTDKN6X5VHGMMN4MA", true) if err != nil { suite.FailNow(err.Error()) } @@ -276,7 +277,7 @@ func (suite *GetTestSuite) TestGetBetweenIDPrepareNext() { func (suite *GetTestSuite) TestGetXFromTop() { // get 5 from the top - statuses, err := suite.timeline.GetXFromTop(5) + statuses, err := suite.timeline.GetXFromTop(context.Background(), 5) if err != nil { suite.FailNow(err.Error()) } @@ -300,7 +301,7 @@ func (suite *GetTestSuite) TestGetXBehindID() { var attempts *int a := 0 attempts = &a - statuses, err := suite.timeline.GetXBehindID(3, "01F8MHBQCBTDKN6X5VHGMMN4MA", attempts) + statuses, err := suite.timeline.GetXBehindID(context.Background(), 3, "01F8MHBQCBTDKN6X5VHGMMN4MA", attempts) if err != nil { suite.FailNow(err.Error()) } @@ -326,7 +327,7 @@ func (suite *GetTestSuite) TestGetXBehindID0() { var attempts *int a := 0 attempts = &a - statuses, err := suite.timeline.GetXBehindID(3, "0", attempts) + statuses, err := suite.timeline.GetXBehindID(context.Background(), 3, "0", attempts) if err != nil { suite.FailNow(err.Error()) } @@ -340,7 +341,7 @@ func (suite *GetTestSuite) TestGetXBehindNonexistentReasonableID() { var attempts *int a := 0 attempts = &a - statuses, err := suite.timeline.GetXBehindID(3, "01F8MHBQCBTDKN6X5VHGMMN4MB", attempts) // change the last A to a B + statuses, err := suite.timeline.GetXBehindID(context.Background(), 3, "01F8MHBQCBTDKN6X5VHGMMN4MB", attempts) // change the last A to a B if err != nil { suite.FailNow(err.Error()) } @@ -365,7 +366,7 @@ func (suite *GetTestSuite) TestGetXBehindVeryHighID() { var attempts *int a := 0 attempts = &a - statuses, err := suite.timeline.GetXBehindID(7, "9998MHBQCBTDKN6X5VHGMMN4MA", attempts) + statuses, err := suite.timeline.GetXBehindID(context.Background(), 7, "9998MHBQCBTDKN6X5VHGMMN4MA", attempts) if err != nil { suite.FailNow(err.Error()) } @@ -389,7 +390,7 @@ func (suite *GetTestSuite) TestGetXBehindVeryHighID() { func (suite *GetTestSuite) TestGetXBeforeID() { // get 3 before the 'middle' id - statuses, err := suite.timeline.GetXBeforeID(3, "01F8MHBQCBTDKN6X5VHGMMN4MA", true) + statuses, err := suite.timeline.GetXBeforeID(context.Background(), 3, "01F8MHBQCBTDKN6X5VHGMMN4MA", true) if err != nil { suite.FailNow(err.Error()) } @@ -412,7 +413,7 @@ func (suite *GetTestSuite) TestGetXBeforeID() { func (suite *GetTestSuite) TestGetXBeforeIDNoStartFromTop() { // get 3 before the 'middle' id - statuses, err := suite.timeline.GetXBeforeID(3, "01F8MHBQCBTDKN6X5VHGMMN4MA", false) + statuses, err := suite.timeline.GetXBeforeID(context.Background(), 3, "01F8MHBQCBTDKN6X5VHGMMN4MA", false) if err != nil { suite.FailNow(err.Error()) } diff --git a/internal/timeline/index.go b/internal/timeline/index.go index 7cffe7ab9..7d7dc8873 100644 --- a/internal/timeline/index.go +++ b/internal/timeline/index.go @@ -20,6 +20,7 @@ package timeline import ( "container/list" + "context" "errors" "fmt" "time" @@ -29,7 +30,7 @@ import ( "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" ) -func (t *timeline) IndexBefore(statusID string, include bool, amount int) error { +func (t *timeline) IndexBefore(ctx context.Context, statusID string, include bool, amount int) error { // lazily initialize index if it hasn't been done already if t.postIndex.data == nil { t.postIndex.data = &list.List{} @@ -42,7 +43,7 @@ func (t *timeline) IndexBefore(statusID string, include bool, amount int) error if include { // if we have the status with given statusID in the database, include it in the results set as well s := >smodel.Status{} - if err := t.db.GetByID(statusID, s); err == nil { + if err := t.db.GetByID(ctx, statusID, s); err == nil { filtered = append(filtered, s) } } @@ -50,7 +51,7 @@ func (t *timeline) IndexBefore(statusID string, include bool, amount int) error i := 0 grabloop: for ; len(filtered) < amount && i < 5; i = i + 1 { // try the grabloop 5 times only - statuses, err := t.db.GetHomeTimeline(t.accountID, "", "", offsetStatus, amount, false) + statuses, err := t.db.GetHomeTimeline(ctx, t.accountID, "", "", offsetStatus, amount, false) if err != nil { if err == db.ErrNoEntries { break grabloop // we just don't have enough statuses left in the db so index what we've got and then bail @@ -59,7 +60,7 @@ grabloop: } for _, s := range statuses { - timelineable, err := t.filter.StatusHometimelineable(s, t.account) + timelineable, err := t.filter.StatusHometimelineable(ctx, s, t.account) if err != nil { continue } @@ -71,7 +72,7 @@ grabloop: } for _, s := range filtered { - if _, err := t.IndexOne(s.CreatedAt, s.ID, s.BoostOfID, s.AccountID, s.BoostOfAccountID); err != nil { + if _, err := t.IndexOne(ctx, s.CreatedAt, s.ID, s.BoostOfID, s.AccountID, s.BoostOfAccountID); err != nil { return fmt.Errorf("IndexBefore: error indexing status with id %s: %s", s.ID, err) } } @@ -79,7 +80,7 @@ grabloop: return nil } -func (t *timeline) IndexBehind(statusID string, include bool, amount int) error { +func (t *timeline) IndexBehind(ctx context.Context, statusID string, include bool, amount int) error { l := t.log.WithFields(logrus.Fields{ "func": "IndexBehind", "include": include, @@ -121,7 +122,7 @@ positionLoop: if include { // if we have the status with given statusID in the database, include it in the results set as well s := >smodel.Status{} - if err := t.db.GetByID(statusID, s); err == nil { + if err := t.db.GetByID(ctx, statusID, s); err == nil { filtered = append(filtered, s) } } @@ -130,7 +131,7 @@ positionLoop: grabloop: for ; len(filtered) < amount && i < 5; i = i + 1 { // try the grabloop 5 times only l.Tracef("entering grabloop; i is %d; len(filtered) is %d", i, len(filtered)) - statuses, err := t.db.GetHomeTimeline(t.accountID, offsetStatus, "", "", amount, false) + statuses, err := t.db.GetHomeTimeline(ctx, t.accountID, offsetStatus, "", "", amount, false) if err != nil { if err == db.ErrNoEntries { break grabloop // we just don't have enough statuses left in the db so index what we've got and then bail @@ -140,7 +141,7 @@ grabloop: l.Tracef("got %d statuses", len(statuses)) for _, s := range statuses { - timelineable, err := t.filter.StatusHometimelineable(s, t.account) + timelineable, err := t.filter.StatusHometimelineable(ctx, s, t.account) if err != nil { l.Tracef("status was not hometimelineable: %s", err) continue @@ -154,7 +155,7 @@ grabloop: l.Trace("left grabloop") for _, s := range filtered { - if _, err := t.IndexOne(s.CreatedAt, s.ID, s.BoostOfID, s.AccountID, s.BoostOfAccountID); err != nil { + if _, err := t.IndexOne(ctx, s.CreatedAt, s.ID, s.BoostOfID, s.AccountID, s.BoostOfAccountID); err != nil { return fmt.Errorf("IndexBehind: error indexing status with id %s: %s", s.ID, err) } } @@ -163,7 +164,7 @@ grabloop: return nil } -func (t *timeline) IndexOne(statusCreatedAt time.Time, statusID string, boostOfID string, accountID string, boostOfAccountID string) (bool, error) { +func (t *timeline) IndexOne(ctx context.Context, statusCreatedAt time.Time, statusID string, boostOfID string, accountID string, boostOfAccountID string) (bool, error) { t.Lock() defer t.Unlock() @@ -177,7 +178,7 @@ func (t *timeline) IndexOne(statusCreatedAt time.Time, statusID string, boostOfI return t.postIndex.insertIndexed(postIndexEntry) } -func (t *timeline) IndexAndPrepareOne(statusCreatedAt time.Time, statusID string, boostOfID string, accountID string, boostOfAccountID string) (bool, error) { +func (t *timeline) IndexAndPrepareOne(ctx context.Context, statusCreatedAt time.Time, statusID string, boostOfID string, accountID string, boostOfAccountID string) (bool, error) { t.Lock() defer t.Unlock() @@ -194,7 +195,7 @@ func (t *timeline) IndexAndPrepareOne(statusCreatedAt time.Time, statusID string } if inserted { - if err := t.prepare(statusID); err != nil { + if err := t.prepare(ctx, statusID); err != nil { return inserted, fmt.Errorf("IndexAndPrepareOne: error preparing: %s", err) } } @@ -202,7 +203,7 @@ func (t *timeline) IndexAndPrepareOne(statusCreatedAt time.Time, statusID string return inserted, nil } -func (t *timeline) OldestIndexedPostID() (string, error) { +func (t *timeline) OldestIndexedPostID(ctx context.Context) (string, error) { var id string if t.postIndex == nil || t.postIndex.data == nil || t.postIndex.data.Back() == nil { // return an empty string if postindex hasn't been initialized yet @@ -217,7 +218,7 @@ func (t *timeline) OldestIndexedPostID() (string, error) { return entry.statusID, nil } -func (t *timeline) NewestIndexedPostID() (string, error) { +func (t *timeline) NewestIndexedPostID(ctx context.Context) (string, error) { var id string if t.postIndex == nil || t.postIndex.data == nil || t.postIndex.data.Front() == nil { // return an empty string if postindex hasn't been initialized yet diff --git a/internal/timeline/index_test.go b/internal/timeline/index_test.go index 4201a27dd..25565a1de 100644 --- a/internal/timeline/index_test.go +++ b/internal/timeline/index_test.go @@ -19,6 +19,7 @@ package timeline_test import ( + "context" "testing" "time" @@ -46,7 +47,7 @@ func (suite *IndexTestSuite) SetupTest() { testrig.StandardDBSetup(suite.db, nil) // let's take local_account_1 as the timeline owner, and start with an empty timeline - tl, err := timeline.NewTimeline(suite.testAccounts["local_account_1"].ID, suite.db, suite.tc, suite.log) + tl, err := timeline.NewTimeline(context.Background(), suite.testAccounts["local_account_1"].ID, suite.db, suite.tc, suite.log) if err != nil { suite.FailNow(err.Error()) } @@ -59,82 +60,82 @@ func (suite *IndexTestSuite) TearDownTest() { func (suite *IndexTestSuite) TestIndexBeforeLowID() { // index 10 before the lowest status ID possible - err := suite.timeline.IndexBefore("00000000000000000000000000", true, 10) + err := suite.timeline.IndexBefore(context.Background(), "00000000000000000000000000", true, 10) suite.NoError(err) // the oldest indexed post should be the lowest one we have in our testrig - postID, err := suite.timeline.OldestIndexedPostID() + postID, err := suite.timeline.OldestIndexedPostID(context.Background()) suite.NoError(err) suite.Equal("01F8MHAAY43M6RJ473VQFCVH37", postID) - indexLength := suite.timeline.PostIndexLength() + indexLength := suite.timeline.PostIndexLength(context.Background()) suite.Equal(10, indexLength) } func (suite *IndexTestSuite) TestIndexBeforeHighID() { // index 10 before the highest status ID possible - err := suite.timeline.IndexBefore("ZZZZZZZZZZZZZZZZZZZZZZZZZZ", true, 10) + err := suite.timeline.IndexBefore(context.Background(), "ZZZZZZZZZZZZZZZZZZZZZZZZZZ", true, 10) suite.NoError(err) // the oldest indexed post should be empty - postID, err := suite.timeline.OldestIndexedPostID() + postID, err := suite.timeline.OldestIndexedPostID(context.Background()) suite.NoError(err) suite.Empty(postID) // indexLength should be 0 - indexLength := suite.timeline.PostIndexLength() + indexLength := suite.timeline.PostIndexLength(context.Background()) suite.Equal(0, indexLength) } func (suite *IndexTestSuite) TestIndexBehindHighID() { // index 10 behind the highest status ID possible - err := suite.timeline.IndexBehind("ZZZZZZZZZZZZZZZZZZZZZZZZZZ", true, 10) + err := suite.timeline.IndexBehind(context.Background(), "ZZZZZZZZZZZZZZZZZZZZZZZZZZ", true, 10) suite.NoError(err) // the newest indexed post should be the highest one we have in our testrig - postID, err := suite.timeline.NewestIndexedPostID() + postID, err := suite.timeline.NewestIndexedPostID(context.Background()) suite.NoError(err) suite.Equal("01FCTA44PW9H1TB328S9AQXKDS", postID) // indexLength should be 10 because that's all this user has hometimelineable - indexLength := suite.timeline.PostIndexLength() + indexLength := suite.timeline.PostIndexLength(context.Background()) suite.Equal(10, indexLength) } func (suite *IndexTestSuite) TestIndexBehindLowID() { // index 10 behind the lowest status ID possible - err := suite.timeline.IndexBehind("00000000000000000000000000", true, 10) + err := suite.timeline.IndexBehind(context.Background(), "00000000000000000000000000", true, 10) suite.NoError(err) // the newest indexed post should be empty - postID, err := suite.timeline.NewestIndexedPostID() + postID, err := suite.timeline.NewestIndexedPostID(context.Background()) suite.NoError(err) suite.Empty(postID) // indexLength should be 0 - indexLength := suite.timeline.PostIndexLength() + indexLength := suite.timeline.PostIndexLength(context.Background()) suite.Equal(0, indexLength) } func (suite *IndexTestSuite) TestOldestIndexedPostIDEmpty() { // the oldest indexed post should be an empty string since there's nothing indexed yet - postID, err := suite.timeline.OldestIndexedPostID() + postID, err := suite.timeline.OldestIndexedPostID(context.Background()) suite.NoError(err) suite.Empty(postID) // indexLength should be 0 - indexLength := suite.timeline.PostIndexLength() + indexLength := suite.timeline.PostIndexLength(context.Background()) suite.Equal(0, indexLength) } func (suite *IndexTestSuite) TestNewestIndexedPostIDEmpty() { // the newest indexed post should be an empty string since there's nothing indexed yet - postID, err := suite.timeline.NewestIndexedPostID() + postID, err := suite.timeline.NewestIndexedPostID(context.Background()) suite.NoError(err) suite.Empty(postID) // indexLength should be 0 - indexLength := suite.timeline.PostIndexLength() + indexLength := suite.timeline.PostIndexLength(context.Background()) suite.Equal(0, indexLength) } @@ -142,12 +143,12 @@ func (suite *IndexTestSuite) TestIndexAlreadyIndexed() { testStatus := suite.testStatuses["local_account_1_status_1"] // index one post -- it should be indexed - indexed, err := suite.timeline.IndexOne(testStatus.CreatedAt, testStatus.ID, testStatus.BoostOfID, testStatus.AccountID, testStatus.BoostOfAccountID) + indexed, err := suite.timeline.IndexOne(context.Background(), testStatus.CreatedAt, testStatus.ID, testStatus.BoostOfID, testStatus.AccountID, testStatus.BoostOfAccountID) suite.NoError(err) suite.True(indexed) // try to index the same post again -- it should not be indexed - indexed, err = suite.timeline.IndexOne(testStatus.CreatedAt, testStatus.ID, testStatus.BoostOfID, testStatus.AccountID, testStatus.BoostOfAccountID) + indexed, err = suite.timeline.IndexOne(context.Background(), testStatus.CreatedAt, testStatus.ID, testStatus.BoostOfID, testStatus.AccountID, testStatus.BoostOfAccountID) suite.NoError(err) suite.False(indexed) } @@ -156,12 +157,12 @@ func (suite *IndexTestSuite) TestIndexAndPrepareAlreadyIndexedAndPrepared() { testStatus := suite.testStatuses["local_account_1_status_1"] // index and prepare one post -- it should be indexed - indexed, err := suite.timeline.IndexAndPrepareOne(testStatus.CreatedAt, testStatus.ID, testStatus.BoostOfID, testStatus.AccountID, testStatus.BoostOfAccountID) + indexed, err := suite.timeline.IndexAndPrepareOne(context.Background(), testStatus.CreatedAt, testStatus.ID, testStatus.BoostOfID, testStatus.AccountID, testStatus.BoostOfAccountID) suite.NoError(err) suite.True(indexed) // try to index and prepare the same post again -- it should not be indexed - indexed, err = suite.timeline.IndexAndPrepareOne(testStatus.CreatedAt, testStatus.ID, testStatus.BoostOfID, testStatus.AccountID, testStatus.BoostOfAccountID) + indexed, err = suite.timeline.IndexAndPrepareOne(context.Background(), testStatus.CreatedAt, testStatus.ID, testStatus.BoostOfID, testStatus.AccountID, testStatus.BoostOfAccountID) suite.NoError(err) suite.False(indexed) } @@ -177,12 +178,12 @@ func (suite *IndexTestSuite) TestIndexBoostOfAlreadyIndexed() { } // index one post -- it should be indexed - indexed, err := suite.timeline.IndexOne(testStatus.CreatedAt, testStatus.ID, testStatus.BoostOfID, testStatus.AccountID, testStatus.BoostOfAccountID) + indexed, err := suite.timeline.IndexOne(context.Background(), testStatus.CreatedAt, testStatus.ID, testStatus.BoostOfID, testStatus.AccountID, testStatus.BoostOfAccountID) suite.NoError(err) suite.True(indexed) // try to index the a boost of that post -- it should not be indexed - indexed, err = suite.timeline.IndexOne(boostOfTestStatus.CreatedAt, boostOfTestStatus.ID, boostOfTestStatus.BoostOfID, boostOfTestStatus.AccountID, boostOfTestStatus.BoostOfAccountID) + indexed, err = suite.timeline.IndexOne(context.Background(), boostOfTestStatus.CreatedAt, boostOfTestStatus.ID, boostOfTestStatus.BoostOfID, boostOfTestStatus.AccountID, boostOfTestStatus.BoostOfAccountID) suite.NoError(err) suite.False(indexed) } diff --git a/internal/timeline/manager.go b/internal/timeline/manager.go index a592670a8..7f42e2f51 100644 --- a/internal/timeline/manager.go +++ b/internal/timeline/manager.go @@ -19,6 +19,7 @@ package timeline import ( + "context" "fmt" "strings" "sync" @@ -54,7 +55,7 @@ type Manager interface { // // The returned bool indicates whether the status was actually put in the timeline. This could be false in cases where // the status is a boost, but a boost of the original post or the post itself already exists recently in the timeline. - Ingest(status *gtsmodel.Status, timelineAccountID string) (bool, error) + Ingest(ctx context.Context, status *gtsmodel.Status, timelineAccountID string) (bool, error) // IngestAndPrepare takes one status and indexes it into the timeline for the given account ID, and then immediately prepares it for serving. // This is useful in cases where we know the status will need to be shown at the top of a user's timeline immediately (eg., a new status is created). // @@ -62,24 +63,24 @@ type Manager interface { // // The returned bool indicates whether the status was actually put in the timeline. This could be false in cases where // the status is a boost, but a boost of the original post or the post itself already exists recently in the timeline. - IngestAndPrepare(status *gtsmodel.Status, timelineAccountID string) (bool, error) + IngestAndPrepare(ctx context.Context, status *gtsmodel.Status, timelineAccountID string) (bool, error) // HomeTimeline returns limit n amount of entries from the home timeline of the given account ID, in descending chronological order. // If maxID is provided, it will return entries from that maxID onwards, inclusive. - HomeTimeline(accountID string, maxID string, sinceID string, minID string, limit int, local bool) ([]*apimodel.Status, error) + HomeTimeline(ctx context.Context, accountID string, maxID string, sinceID string, minID string, limit int, local bool) ([]*apimodel.Status, error) // GetIndexedLength returns the amount of posts/statuses that have been *indexed* for the given account ID. - GetIndexedLength(timelineAccountID string) int + GetIndexedLength(ctx context.Context, timelineAccountID string) int // GetDesiredIndexLength returns the amount of posts that we, ideally, index for each user. - GetDesiredIndexLength() int + GetDesiredIndexLength(ctx context.Context) int // GetOldestIndexedID returns the status ID for the oldest post that we have indexed for the given account. - GetOldestIndexedID(timelineAccountID string) (string, error) + GetOldestIndexedID(ctx context.Context, timelineAccountID string) (string, error) // PrepareXFromTop prepares limit n amount of posts, based on their indexed representations, from the top of the index. - PrepareXFromTop(timelineAccountID string, limit int) error + PrepareXFromTop(ctx context.Context, timelineAccountID string, limit int) error // Remove removes one status from the timeline of the given timelineAccountID - Remove(timelineAccountID string, statusID string) (int, error) + Remove(ctx context.Context, timelineAccountID string, statusID string) (int, error) // WipeStatusFromAllTimelines removes one status from the index and prepared posts of all timelines - WipeStatusFromAllTimelines(statusID string) error + WipeStatusFromAllTimelines(ctx context.Context, statusID string) error // WipeStatusesFromAccountID removes all statuses by the given accountID from the timelineAccountID's timelines. - WipeStatusesFromAccountID(timelineAccountID string, accountID string) error + WipeStatusesFromAccountID(ctx context.Context, timelineAccountID string, accountID string) error } // NewManager returns a new timeline manager with the given database, typeconverter, config, and log. @@ -101,104 +102,104 @@ type manager struct { log *logrus.Logger } -func (m *manager) Ingest(status *gtsmodel.Status, timelineAccountID string) (bool, error) { +func (m *manager) Ingest(ctx context.Context, status *gtsmodel.Status, timelineAccountID string) (bool, error) { l := m.log.WithFields(logrus.Fields{ "func": "Ingest", "timelineAccountID": timelineAccountID, "statusID": status.ID, }) - t, err := m.getOrCreateTimeline(timelineAccountID) + t, err := m.getOrCreateTimeline(ctx, timelineAccountID) if err != nil { return false, err } l.Trace("ingesting status") - return t.IndexOne(status.CreatedAt, status.ID, status.BoostOfID, status.AccountID, status.BoostOfAccountID) + return t.IndexOne(ctx, status.CreatedAt, status.ID, status.BoostOfID, status.AccountID, status.BoostOfAccountID) } -func (m *manager) IngestAndPrepare(status *gtsmodel.Status, timelineAccountID string) (bool, error) { +func (m *manager) IngestAndPrepare(ctx context.Context, status *gtsmodel.Status, timelineAccountID string) (bool, error) { l := m.log.WithFields(logrus.Fields{ "func": "IngestAndPrepare", "timelineAccountID": timelineAccountID, "statusID": status.ID, }) - t, err := m.getOrCreateTimeline(timelineAccountID) + t, err := m.getOrCreateTimeline(ctx, timelineAccountID) if err != nil { return false, err } l.Trace("ingesting status") - return t.IndexAndPrepareOne(status.CreatedAt, status.ID, status.BoostOfID, status.AccountID, status.BoostOfAccountID) + return t.IndexAndPrepareOne(ctx, status.CreatedAt, status.ID, status.BoostOfID, status.AccountID, status.BoostOfAccountID) } -func (m *manager) Remove(timelineAccountID string, statusID string) (int, error) { +func (m *manager) Remove(ctx context.Context, timelineAccountID string, statusID string) (int, error) { l := m.log.WithFields(logrus.Fields{ "func": "Remove", "timelineAccountID": timelineAccountID, "statusID": statusID, }) - t, err := m.getOrCreateTimeline(timelineAccountID) + t, err := m.getOrCreateTimeline(ctx, timelineAccountID) if err != nil { return 0, err } l.Trace("removing status") - return t.Remove(statusID) + return t.Remove(ctx, statusID) } -func (m *manager) HomeTimeline(timelineAccountID string, maxID string, sinceID string, minID string, limit int, local bool) ([]*apimodel.Status, error) { +func (m *manager) HomeTimeline(ctx context.Context, timelineAccountID string, maxID string, sinceID string, minID string, limit int, local bool) ([]*apimodel.Status, error) { l := m.log.WithFields(logrus.Fields{ "func": "HomeTimelineGet", "timelineAccountID": timelineAccountID, }) - t, err := m.getOrCreateTimeline(timelineAccountID) + t, err := m.getOrCreateTimeline(ctx, timelineAccountID) if err != nil { return nil, err } - statuses, err := t.Get(limit, maxID, sinceID, minID, true) + statuses, err := t.Get(ctx, limit, maxID, sinceID, minID, true) if err != nil { l.Errorf("error getting statuses: %s", err) } return statuses, nil } -func (m *manager) GetIndexedLength(timelineAccountID string) int { - t, err := m.getOrCreateTimeline(timelineAccountID) +func (m *manager) GetIndexedLength(ctx context.Context, timelineAccountID string) int { + t, err := m.getOrCreateTimeline(ctx, timelineAccountID) if err != nil { return 0 } - return t.PostIndexLength() + return t.PostIndexLength(ctx) } -func (m *manager) GetDesiredIndexLength() int { +func (m *manager) GetDesiredIndexLength(ctx context.Context) int { return desiredPostIndexLength } -func (m *manager) GetOldestIndexedID(timelineAccountID string) (string, error) { - t, err := m.getOrCreateTimeline(timelineAccountID) +func (m *manager) GetOldestIndexedID(ctx context.Context, timelineAccountID string) (string, error) { + t, err := m.getOrCreateTimeline(ctx, timelineAccountID) if err != nil { return "", err } - return t.OldestIndexedPostID() + return t.OldestIndexedPostID(ctx) } -func (m *manager) PrepareXFromTop(timelineAccountID string, limit int) error { - t, err := m.getOrCreateTimeline(timelineAccountID) +func (m *manager) PrepareXFromTop(ctx context.Context, timelineAccountID string, limit int) error { + t, err := m.getOrCreateTimeline(ctx, timelineAccountID) if err != nil { return err } - return t.PrepareFromTop(limit) + return t.PrepareFromTop(ctx, limit) } -func (m *manager) WipeStatusFromAllTimelines(statusID string) error { +func (m *manager) WipeStatusFromAllTimelines(ctx context.Context, statusID string) error { errors := []string{} m.accountTimelines.Range(func(k interface{}, i interface{}) bool { t, ok := i.(Timeline) @@ -206,7 +207,7 @@ func (m *manager) WipeStatusFromAllTimelines(statusID string) error { panic("couldn't parse entry as Timeline, this should never happen so panic") } - if _, err := t.Remove(statusID); err != nil { + if _, err := t.Remove(ctx, statusID); err != nil { errors = append(errors, err.Error()) } @@ -221,22 +222,22 @@ func (m *manager) WipeStatusFromAllTimelines(statusID string) error { return err } -func (m *manager) WipeStatusesFromAccountID(timelineAccountID string, accountID string) error { - t, err := m.getOrCreateTimeline(timelineAccountID) +func (m *manager) WipeStatusesFromAccountID(ctx context.Context, timelineAccountID string, accountID string) error { + t, err := m.getOrCreateTimeline(ctx, timelineAccountID) if err != nil { return err } - _, err = t.RemoveAllBy(accountID) + _, err = t.RemoveAllBy(ctx, accountID) return err } -func (m *manager) getOrCreateTimeline(timelineAccountID string) (Timeline, error) { +func (m *manager) getOrCreateTimeline(ctx context.Context, timelineAccountID string) (Timeline, error) { var t Timeline i, ok := m.accountTimelines.Load(timelineAccountID) if !ok { var err error - t, err = NewTimeline(timelineAccountID, m.db, m.tc, m.log) + t, err = NewTimeline(ctx, timelineAccountID, m.db, m.tc, m.log) if err != nil { return nil, err } diff --git a/internal/timeline/manager_test.go b/internal/timeline/manager_test.go index 00c6dcb4a..ea4dc4c12 100644 --- a/internal/timeline/manager_test.go +++ b/internal/timeline/manager_test.go @@ -19,6 +19,7 @@ package timeline_test import ( + "context" "testing" "github.com/stretchr/testify/suite" @@ -54,85 +55,85 @@ func (suite *ManagerTestSuite) TestManagerIntegration() { testAccount := suite.testAccounts["local_account_1"] // should start at 0 - indexedLen := suite.manager.GetIndexedLength(testAccount.ID) + indexedLen := suite.manager.GetIndexedLength(context.Background(), testAccount.ID) suite.Equal(0, indexedLen) // oldestIndexed should be empty string since there's nothing indexed - oldestIndexed, err := suite.manager.GetOldestIndexedID(testAccount.ID) + oldestIndexed, err := suite.manager.GetOldestIndexedID(context.Background(), testAccount.ID) suite.NoError(err) suite.Empty(oldestIndexed) // trigger status preparation - err = suite.manager.PrepareXFromTop(testAccount.ID, 20) + err = suite.manager.PrepareXFromTop(context.Background(), testAccount.ID, 20) suite.NoError(err) // local_account_1 can see 12 statuses out of the testrig statuses in its home timeline - indexedLen = suite.manager.GetIndexedLength(testAccount.ID) + indexedLen = suite.manager.GetIndexedLength(context.Background(), testAccount.ID) suite.Equal(12, indexedLen) // oldest should now be set - oldestIndexed, err = suite.manager.GetOldestIndexedID(testAccount.ID) + oldestIndexed, err = suite.manager.GetOldestIndexedID(context.Background(), testAccount.ID) suite.NoError(err) suite.Equal("01F8MH75CBF9JFX4ZAD54N0W0R", oldestIndexed) // get hometimeline - statuses, err := suite.manager.HomeTimeline(testAccount.ID, "", "", "", 20, false) + statuses, err := suite.manager.HomeTimeline(context.Background(), testAccount.ID, "", "", "", 20, false) suite.NoError(err) suite.Len(statuses, 12) // now wipe the last status from all timelines, as though it had been deleted by the owner - err = suite.manager.WipeStatusFromAllTimelines("01F8MH75CBF9JFX4ZAD54N0W0R") + err = suite.manager.WipeStatusFromAllTimelines(context.Background(), "01F8MH75CBF9JFX4ZAD54N0W0R") suite.NoError(err) // timeline should be shorter - indexedLen = suite.manager.GetIndexedLength(testAccount.ID) + indexedLen = suite.manager.GetIndexedLength(context.Background(), testAccount.ID) suite.Equal(11, indexedLen) // oldest should now be different - oldestIndexed, err = suite.manager.GetOldestIndexedID(testAccount.ID) + oldestIndexed, err = suite.manager.GetOldestIndexedID(context.Background(), testAccount.ID) suite.NoError(err) suite.Equal("01F8MH82FYRXD2RC6108DAJ5HB", oldestIndexed) // delete the new oldest status specifically from this timeline, as though local_account_1 had muted or blocked it - removed, err := suite.manager.Remove(testAccount.ID, "01F8MH82FYRXD2RC6108DAJ5HB") + removed, err := suite.manager.Remove(context.Background(), testAccount.ID, "01F8MH82FYRXD2RC6108DAJ5HB") suite.NoError(err) suite.Equal(2, removed) // 1 status should be removed, but from both indexed and prepared, so 2 removals total // timeline should be shorter - indexedLen = suite.manager.GetIndexedLength(testAccount.ID) + indexedLen = suite.manager.GetIndexedLength(context.Background(), testAccount.ID) suite.Equal(10, indexedLen) // oldest should now be different - oldestIndexed, err = suite.manager.GetOldestIndexedID(testAccount.ID) + oldestIndexed, err = suite.manager.GetOldestIndexedID(context.Background(), testAccount.ID) suite.NoError(err) suite.Equal("01F8MHAAY43M6RJ473VQFCVH37", oldestIndexed) // now remove all entries by local_account_2 from the timeline - err = suite.manager.WipeStatusesFromAccountID(testAccount.ID, suite.testAccounts["local_account_2"].ID) + err = suite.manager.WipeStatusesFromAccountID(context.Background(), testAccount.ID, suite.testAccounts["local_account_2"].ID) suite.NoError(err) // timeline should be empty now - indexedLen = suite.manager.GetIndexedLength(testAccount.ID) + indexedLen = suite.manager.GetIndexedLength(context.Background(), testAccount.ID) suite.Equal(5, indexedLen) // ingest 1 into the timeline status1 := suite.testStatuses["admin_account_status_1"] - ingested, err := suite.manager.Ingest(status1, testAccount.ID) + ingested, err := suite.manager.Ingest(context.Background(), status1, testAccount.ID) suite.NoError(err) suite.True(ingested) // ingest and prepare another one into the timeline status2 := suite.testStatuses["local_account_2_status_1"] - ingested, err = suite.manager.IngestAndPrepare(status2, testAccount.ID) + ingested, err = suite.manager.IngestAndPrepare(context.Background(), status2, testAccount.ID) suite.NoError(err) suite.True(ingested) // timeline should be longer now - indexedLen = suite.manager.GetIndexedLength(testAccount.ID) + indexedLen = suite.manager.GetIndexedLength(context.Background(), testAccount.ID) suite.Equal(7, indexedLen) // try to ingest status 2 again - ingested, err = suite.manager.IngestAndPrepare(status2, testAccount.ID) + ingested, err = suite.manager.IngestAndPrepare(context.Background(), status2, testAccount.ID) suite.NoError(err) suite.False(ingested) // should be false since it's a duplicate } diff --git a/internal/timeline/prepare.go b/internal/timeline/prepare.go index 20000b4e9..d57222ee8 100644 --- a/internal/timeline/prepare.go +++ b/internal/timeline/prepare.go @@ -20,6 +20,7 @@ package timeline import ( "container/list" + "context" "errors" "fmt" @@ -28,7 +29,7 @@ import ( "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" ) -func (t *timeline) prepareNextQuery(amount int, maxID string, sinceID string, minID string) error { +func (t *timeline) prepareNextQuery(ctx context.Context, amount int, maxID string, sinceID string, minID string) error { l := t.log.WithFields(logrus.Fields{ "func": "prepareNextQuery", "amount": amount, @@ -42,30 +43,30 @@ func (t *timeline) prepareNextQuery(amount int, maxID string, sinceID string, mi // maxID is defined but sinceID isn't so take from behind if maxID != "" && sinceID == "" { l.Debug("preparing behind maxID") - err = t.PrepareBehind(maxID, amount) + err = t.PrepareBehind(ctx, maxID, amount) } // maxID isn't defined, but sinceID || minID are, so take x before if maxID == "" && sinceID != "" { l.Debug("preparing before sinceID") - err = t.PrepareBefore(sinceID, false, amount) + err = t.PrepareBefore(ctx, sinceID, false, amount) } if maxID == "" && minID != "" { l.Debug("preparing before minID") - err = t.PrepareBefore(minID, false, amount) + err = t.PrepareBefore(ctx, minID, false, amount) } return err } -func (t *timeline) PrepareBehind(statusID string, amount int) error { +func (t *timeline) PrepareBehind(ctx context.Context, statusID string, amount int) error { // lazily initialize prepared posts if it hasn't been done already if t.preparedPosts.data == nil { t.preparedPosts.data = &list.List{} t.preparedPosts.data.Init() } - if err := t.IndexBehind(statusID, true, amount); err != nil { + if err := t.IndexBehind(ctx, statusID, true, amount); err != nil { return fmt.Errorf("PrepareBehind: error indexing behind id %s: %s", statusID, err) } @@ -93,7 +94,7 @@ prepareloop: } if preparing { - if err := t.prepare(entry.statusID); err != nil { + if err := t.prepare(ctx, entry.statusID); err != nil { // there's been an error if err != db.ErrNoEntries { // it's a real error @@ -113,7 +114,7 @@ prepareloop: return nil } -func (t *timeline) PrepareBefore(statusID string, include bool, amount int) error { +func (t *timeline) PrepareBefore(ctx context.Context, statusID string, include bool, amount int) error { t.Lock() defer t.Unlock() @@ -148,7 +149,7 @@ prepareloop: } if preparing { - if err := t.prepare(entry.statusID); err != nil { + if err := t.prepare(ctx, entry.statusID); err != nil { // there's been an error if err != db.ErrNoEntries { // it's a real error @@ -168,7 +169,7 @@ prepareloop: return nil } -func (t *timeline) PrepareFromTop(amount int) error { +func (t *timeline) PrepareFromTop(ctx context.Context, amount int) error { l := t.log.WithFields(logrus.Fields{ "func": "PrepareFromTop", "amount": amount, @@ -183,7 +184,7 @@ func (t *timeline) PrepareFromTop(amount int) error { // if the postindex is nil, nothing has been indexed yet so index from the highest ID possible if t.postIndex.data == nil { l.Debug("postindex.data was nil, indexing behind highest possible ID") - if err := t.IndexBehind("ZZZZZZZZZZZZZZZZZZZZZZZZZZ", false, amount); err != nil { + if err := t.IndexBehind(ctx, "ZZZZZZZZZZZZZZZZZZZZZZZZZZ", false, amount); err != nil { return fmt.Errorf("PrepareFromTop: error indexing behind id %s: %s", "ZZZZZZZZZZZZZZZZZZZZZZZZZZ", err) } } @@ -203,7 +204,7 @@ prepareloop: return errors.New("PrepareFromTop: could not parse e as a postIndexEntry") } - if err := t.prepare(entry.statusID); err != nil { + if err := t.prepare(ctx, entry.statusID); err != nil { // there's been an error if err != db.ErrNoEntries { // it's a real error @@ -225,25 +226,25 @@ prepareloop: return nil } -func (t *timeline) prepare(statusID string) error { +func (t *timeline) prepare(ctx context.Context, statusID string) error { // start by getting the status out of the database according to its indexed ID gtsStatus := >smodel.Status{} - if err := t.db.GetByID(statusID, gtsStatus); err != nil { + if err := t.db.GetByID(ctx, statusID, gtsStatus); err != nil { return err } // if the account pointer hasn't been set on this timeline already, set it lazily here if t.account == nil { timelineOwnerAccount := >smodel.Account{} - if err := t.db.GetByID(t.accountID, timelineOwnerAccount); err != nil { + if err := t.db.GetByID(ctx, t.accountID, timelineOwnerAccount); err != nil { return err } t.account = timelineOwnerAccount } // serialize the status (or, at least, convert it to a form that's ready to be serialized) - apiModelStatus, err := t.tc.StatusToMasto(gtsStatus, t.account) + apiModelStatus, err := t.tc.StatusToMasto(ctx, gtsStatus, t.account) if err != nil { return err } @@ -260,7 +261,7 @@ func (t *timeline) prepare(statusID string) error { return t.preparedPosts.insertPrepared(preparedPostsEntry) } -func (t *timeline) OldestPreparedPostID() (string, error) { +func (t *timeline) OldestPreparedPostID(ctx context.Context) (string, error) { var id string if t.preparedPosts == nil || t.preparedPosts.data == nil { // return an empty string if prepared posts hasn't been initialized yet diff --git a/internal/timeline/remove.go b/internal/timeline/remove.go index cf0b0b617..031dace1f 100644 --- a/internal/timeline/remove.go +++ b/internal/timeline/remove.go @@ -20,12 +20,13 @@ package timeline import ( "container/list" + "context" "errors" "github.com/sirupsen/logrus" ) -func (t *timeline) Remove(statusID string) (int, error) { +func (t *timeline) Remove(ctx context.Context, statusID string) (int, error) { l := t.log.WithFields(logrus.Fields{ "func": "Remove", "accountTimeline": t.accountID, @@ -77,7 +78,7 @@ func (t *timeline) Remove(statusID string) (int, error) { return removed, nil } -func (t *timeline) RemoveAllBy(accountID string) (int, error) { +func (t *timeline) RemoveAllBy(ctx context.Context, accountID string) (int, error) { l := t.log.WithFields(logrus.Fields{ "func": "RemoveAllBy", "accountTimeline": t.accountID, diff --git a/internal/timeline/timeline.go b/internal/timeline/timeline.go index 6274a86ac..5f5fa1b4f 100644 --- a/internal/timeline/timeline.go +++ b/internal/timeline/timeline.go @@ -19,6 +19,7 @@ package timeline import ( + "context" "sync" "time" @@ -41,24 +42,24 @@ type Timeline interface { // Get returns an amount of statuses with the given parameters. // If prepareNext is true, then the next predicted query will be prepared already in a goroutine, // to make the next call to Get faster. - Get(amount int, maxID string, sinceID string, minID string, prepareNext bool) ([]*apimodel.Status, error) + Get(ctx context.Context, amount int, maxID string, sinceID string, minID string, prepareNext bool) ([]*apimodel.Status, error) // GetXFromTop returns x amount of posts from the top of the timeline, from newest to oldest. - GetXFromTop(amount int) ([]*apimodel.Status, error) + GetXFromTop(ctx context.Context, amount int) ([]*apimodel.Status, error) // GetXBehindID returns x amount of posts from the given id onwards, from newest to oldest. // This will NOT include the status with the given ID. // // This corresponds to an api call to /timelines/home?max_id=WHATEVER - GetXBehindID(amount int, fromID string, attempts *int) ([]*apimodel.Status, error) + GetXBehindID(ctx context.Context, amount int, fromID string, attempts *int) ([]*apimodel.Status, error) // GetXBeforeID returns x amount of posts up to the given id, from newest to oldest. // This will NOT include the status with the given ID. // // This corresponds to an api call to /timelines/home?since_id=WHATEVER - GetXBeforeID(amount int, sinceID string, startFromTop bool) ([]*apimodel.Status, error) + GetXBeforeID(ctx context.Context, amount int, sinceID string, startFromTop bool) ([]*apimodel.Status, error) // GetXBetweenID returns x amount of posts from the given maxID, up to the given id, from newest to oldest. // This will NOT include the status with the given IDs. // // This corresponds to an api call to /timelines/home?since_id=WHATEVER&max_id=WHATEVER_ELSE - GetXBetweenID(amount int, maxID string, sinceID string) ([]*apimodel.Status, error) + GetXBetweenID(ctx context.Context, amount int, maxID string, sinceID string) ([]*apimodel.Status, error) /* INDEXING FUNCTIONS @@ -68,43 +69,43 @@ type Timeline interface { // // The returned bool indicates whether or not the status was actually inserted into the timeline. This will be false // if the status is a boost and the original post or another boost of it already exists < boostReinsertionDepth back in the timeline. - IndexOne(statusCreatedAt time.Time, statusID string, boostOfID string, accountID string, boostOfAccountID string) (bool, error) + IndexOne(ctx context.Context, statusCreatedAt time.Time, statusID string, boostOfID string, accountID string, boostOfAccountID string) (bool, error) // OldestIndexedPostID returns the id of the rearmost (ie., the oldest) indexed post, or an error if something goes wrong. // If nothing goes wrong but there's no oldest post, an empty string will be returned so make sure to check for this. - OldestIndexedPostID() (string, error) + OldestIndexedPostID(ctx context.Context) (string, error) // NewestIndexedPostID returns the id of the frontmost (ie., the newest) indexed post, or an error if something goes wrong. // If nothing goes wrong but there's no newest post, an empty string will be returned so make sure to check for this. - NewestIndexedPostID() (string, error) + NewestIndexedPostID(ctx context.Context) (string, error) - IndexBefore(statusID string, include bool, amount int) error - IndexBehind(statusID string, include bool, amount int) error + IndexBefore(ctx context.Context, statusID string, include bool, amount int) error + IndexBehind(ctx context.Context, statusID string, include bool, amount int) error /* PREPARATION FUNCTIONS */ // PrepareXFromTop instructs the timeline to prepare x amount of posts from the top of the timeline. - PrepareFromTop(amount int) error + PrepareFromTop(ctx context.Context, amount int) error // PrepareBehind instructs the timeline to prepare the next amount of entries for serialization, from position onwards. // If include is true, then the given status ID will also be prepared, otherwise only entries behind it will be prepared. - PrepareBehind(statusID string, amount int) error + PrepareBehind(ctx context.Context, statusID string, amount int) error // IndexOne puts a status into the timeline at the appropriate place according to its 'createdAt' property, // and then immediately prepares it. // // The returned bool indicates whether or not the status was actually inserted into the timeline. This will be false // if the status is a boost and the original post or another boost of it already exists < boostReinsertionDepth back in the timeline. - IndexAndPrepareOne(statusCreatedAt time.Time, statusID string, boostOfID string, accountID string, boostOfAccountID string) (bool, error) + IndexAndPrepareOne(ctx context.Context, statusCreatedAt time.Time, statusID string, boostOfID string, accountID string, boostOfAccountID string) (bool, error) // OldestPreparedPostID returns the id of the rearmost (ie., the oldest) prepared post, or an error if something goes wrong. // If nothing goes wrong but there's no oldest post, an empty string will be returned so make sure to check for this. - OldestPreparedPostID() (string, error) + OldestPreparedPostID(ctx context.Context) (string, error) /* INFO FUNCTIONS */ // ActualPostIndexLength returns the actual length of the post index at this point in time. - PostIndexLength() int + PostIndexLength(ctx context.Context) int /* UTILITY FUNCTIONS @@ -117,11 +118,11 @@ type Timeline interface { // If a status has multiple entries in a timeline, they will all be removed. // // The returned int indicates the amount of entries that were removed. - Remove(statusID string) (int, error) + Remove(ctx context.Context, statusID string) (int, error) // RemoveAllBy removes all statuses by the given accountID, from both the index and prepared posts. // // The returned int indicates the amount of entries that were removed. - RemoveAllBy(accountID string) (int, error) + RemoveAllBy(ctx context.Context, accountID string) (int, error) } // timeline fulfils the Timeline interface @@ -138,9 +139,9 @@ type timeline struct { } // NewTimeline returns a new Timeline for the given account ID -func NewTimeline(accountID string, db db.DB, typeConverter typeutils.TypeConverter, log *logrus.Logger) (Timeline, error) { +func NewTimeline(ctx context.Context, accountID string, db db.DB, typeConverter typeutils.TypeConverter, log *logrus.Logger) (Timeline, error) { timelineOwnerAccount := >smodel.Account{} - if err := db.GetByID(accountID, timelineOwnerAccount); err != nil { + if err := db.GetByID(ctx, accountID, timelineOwnerAccount); err != nil { return nil, err } @@ -160,7 +161,7 @@ func (t *timeline) Reset() error { return nil } -func (t *timeline) PostIndexLength() int { +func (t *timeline) PostIndexLength(ctx context.Context) int { if t.postIndex == nil || t.postIndex.data == nil { return 0 } diff --git a/internal/typeutils/astointernal.go b/internal/typeutils/astointernal.go index 887716a69..ad4c2e109 100644 --- a/internal/typeutils/astointernal.go +++ b/internal/typeutils/astointernal.go @@ -19,6 +19,7 @@ package typeutils import ( + "context" "errors" "fmt" "net/url" @@ -29,7 +30,7 @@ import ( "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" ) -func (c *converter) ASRepresentationToAccount(accountable ap.Accountable, update bool) (*gtsmodel.Account, error) { +func (c *converter) ASRepresentationToAccount(ctx context.Context, accountable ap.Accountable, update bool) (*gtsmodel.Account, error) { // first check if we actually already know this account uriProp := accountable.GetJSONLDId() if uriProp == nil || !uriProp.IsIRI() { @@ -38,7 +39,7 @@ func (c *converter) ASRepresentationToAccount(accountable ap.Accountable, update uri := uriProp.GetIRI() if !update { - acct, err := c.db.GetAccountByURI(uri.String()) + acct, err := c.db.GetAccountByURI(ctx, uri.String()) if err == nil { // we already know this account so we can skip generating it return acct, nil @@ -170,7 +171,7 @@ func (c *converter) ASRepresentationToAccount(accountable ap.Accountable, update return acct, nil } -func (c *converter) ASStatusToStatus(statusable ap.Statusable) (*gtsmodel.Status, error) { +func (c *converter) ASStatusToStatus(ctx context.Context, statusable ap.Statusable) (*gtsmodel.Status, error) { status := >smodel.Status{} // uri at which this status is reachable @@ -229,7 +230,7 @@ func (c *converter) ASStatusToStatus(statusable ap.Statusable) (*gtsmodel.Status } status.AccountURI = attributedTo.String() - statusOwner, err := c.db.GetAccountByURI(attributedTo.String()) + statusOwner, err := c.db.GetAccountByURI(ctx, attributedTo.String()) if err != nil { return nil, fmt.Errorf("couldn't get status owner from db: %s", err) } @@ -245,14 +246,14 @@ func (c *converter) ASStatusToStatus(statusable ap.Statusable) (*gtsmodel.Status status.InReplyToURI = inReplyToURI.String() // now we can check if we have the replied-to status in our db already - if inReplyToStatus, err := c.db.GetStatusByURI(inReplyToURI.String()); err == nil { + if inReplyToStatus, err := c.db.GetStatusByURI(ctx, inReplyToURI.String()); err == nil { // we have the status in our database already // so we can set these fields here and now... status.InReplyToID = inReplyToStatus.ID status.InReplyToAccountID = inReplyToStatus.AccountID status.InReplyTo = inReplyToStatus if status.InReplyToAccount == nil { - if inReplyToAccount, err := c.db.GetAccountByID(inReplyToStatus.AccountID); err == nil { + if inReplyToAccount, err := c.db.GetAccountByID(ctx, inReplyToStatus.AccountID); err == nil { status.InReplyToAccount = inReplyToAccount } } @@ -318,7 +319,7 @@ func (c *converter) ASStatusToStatus(statusable ap.Statusable) (*gtsmodel.Status return status, nil } -func (c *converter) ASFollowToFollowRequest(followable ap.Followable) (*gtsmodel.FollowRequest, error) { +func (c *converter) ASFollowToFollowRequest(ctx context.Context, followable ap.Followable) (*gtsmodel.FollowRequest, error) { idProp := followable.GetJSONLDId() if idProp == nil || !idProp.IsIRI() { @@ -330,7 +331,7 @@ func (c *converter) ASFollowToFollowRequest(followable ap.Followable) (*gtsmodel if err != nil { return nil, errors.New("error extracting actor property from follow") } - originAccount, err := c.db.GetAccountByURI(origin.String()) + originAccount, err := c.db.GetAccountByURI(ctx, origin.String()) if err != nil { return nil, fmt.Errorf("error extracting account with uri %s from the database: %s", origin.String(), err) } @@ -339,7 +340,7 @@ func (c *converter) ASFollowToFollowRequest(followable ap.Followable) (*gtsmodel if err != nil { return nil, errors.New("error extracting object property from follow") } - targetAccount, err := c.db.GetAccountByURI(target.String()) + targetAccount, err := c.db.GetAccountByURI(ctx, target.String()) if err != nil { return nil, fmt.Errorf("error extracting account with uri %s from the database: %s", origin.String(), err) } @@ -353,7 +354,7 @@ func (c *converter) ASFollowToFollowRequest(followable ap.Followable) (*gtsmodel return followRequest, nil } -func (c *converter) ASFollowToFollow(followable ap.Followable) (*gtsmodel.Follow, error) { +func (c *converter) ASFollowToFollow(ctx context.Context, followable ap.Followable) (*gtsmodel.Follow, error) { idProp := followable.GetJSONLDId() if idProp == nil || !idProp.IsIRI() { return nil, errors.New("no id property set on follow, or was not an iri") @@ -364,7 +365,7 @@ func (c *converter) ASFollowToFollow(followable ap.Followable) (*gtsmodel.Follow if err != nil { return nil, errors.New("error extracting actor property from follow") } - originAccount, err := c.db.GetAccountByURI(origin.String()) + originAccount, err := c.db.GetAccountByURI(ctx, origin.String()) if err != nil { return nil, fmt.Errorf("error extracting account with uri %s from the database: %s", origin.String(), err) } @@ -373,7 +374,7 @@ func (c *converter) ASFollowToFollow(followable ap.Followable) (*gtsmodel.Follow if err != nil { return nil, errors.New("error extracting object property from follow") } - targetAccount, err := c.db.GetAccountByURI(target.String()) + targetAccount, err := c.db.GetAccountByURI(ctx, target.String()) if err != nil { return nil, fmt.Errorf("error extracting account with uri %s from the database: %s", origin.String(), err) } @@ -387,7 +388,7 @@ func (c *converter) ASFollowToFollow(followable ap.Followable) (*gtsmodel.Follow return follow, nil } -func (c *converter) ASLikeToFave(likeable ap.Likeable) (*gtsmodel.StatusFave, error) { +func (c *converter) ASLikeToFave(ctx context.Context, likeable ap.Likeable) (*gtsmodel.StatusFave, error) { idProp := likeable.GetJSONLDId() if idProp == nil || !idProp.IsIRI() { return nil, errors.New("no id property set on like, or was not an iri") @@ -398,7 +399,7 @@ func (c *converter) ASLikeToFave(likeable ap.Likeable) (*gtsmodel.StatusFave, er if err != nil { return nil, errors.New("error extracting actor property from like") } - originAccount, err := c.db.GetAccountByURI(origin.String()) + originAccount, err := c.db.GetAccountByURI(ctx, origin.String()) if err != nil { return nil, fmt.Errorf("error extracting account with uri %s from the database: %s", origin.String(), err) } @@ -408,7 +409,7 @@ func (c *converter) ASLikeToFave(likeable ap.Likeable) (*gtsmodel.StatusFave, er return nil, errors.New("error extracting object property from like") } - targetStatus, err := c.db.GetStatusByURI(target.String()) + targetStatus, err := c.db.GetStatusByURI(ctx, target.String()) if err != nil { return nil, fmt.Errorf("error extracting status with uri %s from the database: %s", target.String(), err) } @@ -417,7 +418,7 @@ func (c *converter) ASLikeToFave(likeable ap.Likeable) (*gtsmodel.StatusFave, er if targetStatus.Account != nil { targetAccount = targetStatus.Account } else { - a, err := c.db.GetAccountByID(targetStatus.AccountID) + a, err := c.db.GetAccountByID(ctx, targetStatus.AccountID) if err != nil { return nil, fmt.Errorf("error extracting account with id %s from the database: %s", targetStatus.AccountID, err) } @@ -435,7 +436,7 @@ func (c *converter) ASLikeToFave(likeable ap.Likeable) (*gtsmodel.StatusFave, er }, nil } -func (c *converter) ASBlockToBlock(blockable ap.Blockable) (*gtsmodel.Block, error) { +func (c *converter) ASBlockToBlock(ctx context.Context, blockable ap.Blockable) (*gtsmodel.Block, error) { idProp := blockable.GetJSONLDId() if idProp == nil || !idProp.IsIRI() { return nil, errors.New("ASBlockToBlock: no id property set on block, or was not an iri") @@ -446,7 +447,7 @@ func (c *converter) ASBlockToBlock(blockable ap.Blockable) (*gtsmodel.Block, err if err != nil { return nil, errors.New("ASBlockToBlock: error extracting actor property from block") } - originAccount, err := c.db.GetAccountByURI(origin.String()) + originAccount, err := c.db.GetAccountByURI(ctx, origin.String()) if err != nil { return nil, fmt.Errorf("error extracting account with uri %s from the database: %s", origin.String(), err) } @@ -456,7 +457,7 @@ func (c *converter) ASBlockToBlock(blockable ap.Blockable) (*gtsmodel.Block, err return nil, errors.New("ASBlockToBlock: error extracting object property from block") } - targetAccount, err := c.db.GetAccountByURI(target.String()) + targetAccount, err := c.db.GetAccountByURI(ctx, target.String()) if err != nil { return nil, fmt.Errorf("error extracting account with uri %s from the database: %s", origin.String(), err) } @@ -470,7 +471,7 @@ func (c *converter) ASBlockToBlock(blockable ap.Blockable) (*gtsmodel.Block, err }, nil } -func (c *converter) ASAnnounceToStatus(announceable ap.Announceable) (*gtsmodel.Status, bool, error) { +func (c *converter) ASAnnounceToStatus(ctx context.Context, announceable ap.Announceable) (*gtsmodel.Status, bool, error) { status := >smodel.Status{} isNew := true @@ -481,7 +482,7 @@ func (c *converter) ASAnnounceToStatus(announceable ap.Announceable) (*gtsmodel. } uri := idProp.GetIRI().String() - if status, err := c.db.GetStatusByURI(uri); err == nil { + if status, err := c.db.GetStatusByURI(ctx, uri); err == nil { // we already have it, great, just return it as-is :) isNew = false return status, isNew, nil @@ -515,7 +516,7 @@ func (c *converter) ASAnnounceToStatus(announceable ap.Announceable) (*gtsmodel. // get the boosting account based on the URI // this should have been dereferenced already before we hit this point so we can confidently error out if we don't have it - boostingAccount, err := c.db.GetAccountByURI(actor.String()) + boostingAccount, err := c.db.GetAccountByURI(ctx, actor.String()) if err != nil { return nil, isNew, fmt.Errorf("ASAnnounceToStatus: error in db fetching account with uri %s: %s", actor.String(), err) } diff --git a/internal/typeutils/astointernal_test.go b/internal/typeutils/astointernal_test.go index 1d02dec5a..a01e79202 100644 --- a/internal/typeutils/astointernal_test.go +++ b/internal/typeutils/astointernal_test.go @@ -348,7 +348,7 @@ func (suite *ASToInternalTestSuite) SetupTest() { func (suite *ASToInternalTestSuite) TestParsePerson() { testPerson := suite.people["new_person_1"] - acct, err := suite.typeconverter.ASRepresentationToAccount(testPerson, false) + acct, err := suite.typeconverter.ASRepresentationToAccount(context.Background(), testPerson, false) assert.NoError(suite.T(), err) suite.Equal("https://unknown-instance.com/users/brand_new_person", acct.URI) @@ -379,7 +379,7 @@ func (suite *ASToInternalTestSuite) TestParseGargron() { rep, ok := t.(ap.Accountable) assert.True(suite.T(), ok) - acct, err := suite.typeconverter.ASRepresentationToAccount(rep, false) + acct, err := suite.typeconverter.ASRepresentationToAccount(context.Background(), rep, false) assert.NoError(suite.T(), err) fmt.Printf("%+v", acct) diff --git a/internal/typeutils/converter.go b/internal/typeutils/converter.go index e477a6135..4af9767bc 100644 --- a/internal/typeutils/converter.go +++ b/internal/typeutils/converter.go @@ -19,6 +19,7 @@ package typeutils import ( + "context" "net/url" "github.com/go-fed/activity/streams/vocab" @@ -47,45 +48,45 @@ type TypeConverter interface { // AccountToMastoSensitive takes a db model account as a param, and returns a populated mastotype 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(account *gtsmodel.Account) (*model.Account, error) + 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 // 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(account *gtsmodel.Account) (*model.Account, error) + 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 // 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(account *gtsmodel.Account) (*model.Account, error) + 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 // 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(application *gtsmodel.Application) (*model.Application, error) + 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 // 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(application *gtsmodel.Application) (*model.Application, error) + 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(attachment *gtsmodel.MediaAttachment) (model.Attachment, error) + 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(m *gtsmodel.Mention) (model.Mention, error) + 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(e *gtsmodel.Emoji) (model.Emoji, error) + 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(t *gtsmodel.Tag) (model.Tag, error) + 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. // // Requesting account can be nil. - StatusToMasto(s *gtsmodel.Status, requestingAccount *gtsmodel.Account) (*model.Status, error) + StatusToMasto(ctx context.Context, s *gtsmodel.Status, requestingAccount *gtsmodel.Account) (*model.Status, error) // VisToMasto converts a gts visibility into its mastodon equivalent - VisToMasto(m gtsmodel.Visibility) model.Visibility + 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(i *gtsmodel.Instance) (*model.Instance, error) + 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(r *gtsmodel.Relationship) (*model.Relationship, error) + RelationshipToMasto(ctx context.Context, r *gtsmodel.Relationship) (*model.Relationship, error) // NotificationToMasto converts a gts notification into a mastodon notification - NotificationToMasto(n *gtsmodel.Notification) (*model.Notification, error) + 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(b *gtsmodel.DomainBlock, export bool) (*model.DomainBlock, error) + DomainBlockToMasto(ctx context.Context, b *gtsmodel.DomainBlock, export bool) (*model.DomainBlock, error) /* FRONTEND (mastodon) MODEL TO INTERNAL (gts) MODEL @@ -103,17 +104,17 @@ type TypeConverter interface { // If update is false, and the account is already known in the database, then the existing account entry will be returned. // If update is true, then even if the account is already known, all fields in the accountable will be parsed and a new *gtsmodel.Account // will be generated. This is useful when one needs to force refresh of an account, eg., during an Update of a Profile. - ASRepresentationToAccount(accountable ap.Accountable, update bool) (*gtsmodel.Account, error) + ASRepresentationToAccount(ctx context.Context, accountable ap.Accountable, update bool) (*gtsmodel.Account, error) // ASStatus converts a remote activitystreams 'status' representation into a gts model status. - ASStatusToStatus(statusable ap.Statusable) (*gtsmodel.Status, error) + ASStatusToStatus(ctx context.Context, statusable ap.Statusable) (*gtsmodel.Status, error) // ASFollowToFollowRequest converts a remote activitystreams `follow` representation into gts model follow request. - ASFollowToFollowRequest(followable ap.Followable) (*gtsmodel.FollowRequest, error) + ASFollowToFollowRequest(ctx context.Context, followable ap.Followable) (*gtsmodel.FollowRequest, error) // ASFollowToFollowRequest converts a remote activitystreams `follow` representation into gts model follow. - ASFollowToFollow(followable ap.Followable) (*gtsmodel.Follow, error) + ASFollowToFollow(ctx context.Context, followable ap.Followable) (*gtsmodel.Follow, error) // ASLikeToFave converts a remote activitystreams 'like' representation into a gts model status fave. - ASLikeToFave(likeable ap.Likeable) (*gtsmodel.StatusFave, error) + ASLikeToFave(ctx context.Context, likeable ap.Likeable) (*gtsmodel.StatusFave, error) // ASBlockToBlock converts a remote activity streams 'block' representation into a gts model block. - ASBlockToBlock(blockable ap.Blockable) (*gtsmodel.Block, error) + ASBlockToBlock(ctx context.Context, blockable ap.Blockable) (*gtsmodel.Block, error) // ASAnnounceToStatus converts an activitystreams 'announce' into a status. // // The returned bool indicates whether this status is new (true) or not new (false). @@ -126,46 +127,46 @@ type TypeConverter interface { // This is useful when multiple users on an instance might receive the same boost, and we only want to process the boost once. // // NOTE -- this is different from one status being boosted multiple times! In this case, new boosts should indeed be created. - ASAnnounceToStatus(announceable ap.Announceable) (status *gtsmodel.Status, new bool, err error) + ASAnnounceToStatus(ctx context.Context, announceable ap.Announceable) (status *gtsmodel.Status, new bool, err error) /* INTERNAL (gts) MODEL TO ACTIVITYSTREAMS MODEL */ // AccountToAS converts a gts model account into an activity streams person, suitable for federation - AccountToAS(a *gtsmodel.Account) (vocab.ActivityStreamsPerson, error) + AccountToAS(ctx context.Context, a *gtsmodel.Account) (vocab.ActivityStreamsPerson, error) // AccountToASMinimal converts a gts model account into an activity streams person, suitable for federation. // // The returned account will just have the Type, Username, PublicKey, and ID properties set. This is // suitable for serving to requesters to whom we want to give as little information as possible because // we don't trust them (yet). - AccountToASMinimal(a *gtsmodel.Account) (vocab.ActivityStreamsPerson, error) + AccountToASMinimal(ctx context.Context, a *gtsmodel.Account) (vocab.ActivityStreamsPerson, error) // StatusToAS converts a gts model status into an activity streams note, suitable for federation - StatusToAS(s *gtsmodel.Status) (vocab.ActivityStreamsNote, error) + StatusToAS(ctx context.Context, s *gtsmodel.Status) (vocab.ActivityStreamsNote, error) // FollowToASFollow converts a gts model Follow into an activity streams Follow, suitable for federation - FollowToAS(f *gtsmodel.Follow, originAccount *gtsmodel.Account, targetAccount *gtsmodel.Account) (vocab.ActivityStreamsFollow, error) + FollowToAS(ctx context.Context, f *gtsmodel.Follow, originAccount *gtsmodel.Account, targetAccount *gtsmodel.Account) (vocab.ActivityStreamsFollow, error) // MentionToAS converts a gts model mention into an activity streams Mention, suitable for federation - MentionToAS(m *gtsmodel.Mention) (vocab.ActivityStreamsMention, error) + MentionToAS(ctx context.Context, m *gtsmodel.Mention) (vocab.ActivityStreamsMention, error) // AttachmentToAS converts a gts model media attachment into an activity streams Attachment, suitable for federation - AttachmentToAS(a *gtsmodel.MediaAttachment) (vocab.ActivityStreamsDocument, error) + AttachmentToAS(ctx context.Context, a *gtsmodel.MediaAttachment) (vocab.ActivityStreamsDocument, error) // FaveToAS converts a gts model status fave into an activityStreams LIKE, suitable for federation. - FaveToAS(f *gtsmodel.StatusFave) (vocab.ActivityStreamsLike, error) + FaveToAS(ctx context.Context, f *gtsmodel.StatusFave) (vocab.ActivityStreamsLike, error) // BoostToAS converts a gts model boost into an activityStreams ANNOUNCE, suitable for federation - BoostToAS(boostWrapperStatus *gtsmodel.Status, boostingAccount *gtsmodel.Account, boostedAccount *gtsmodel.Account) (vocab.ActivityStreamsAnnounce, error) + BoostToAS(ctx context.Context, boostWrapperStatus *gtsmodel.Status, boostingAccount *gtsmodel.Account, boostedAccount *gtsmodel.Account) (vocab.ActivityStreamsAnnounce, error) // BlockToAS converts a gts model block into an activityStreams BLOCK, suitable for federation. - BlockToAS(block *gtsmodel.Block) (vocab.ActivityStreamsBlock, error) + BlockToAS(ctx context.Context, block *gtsmodel.Block) (vocab.ActivityStreamsBlock, error) // StatusToASRepliesCollection converts a gts model status into an activityStreams REPLIES collection. - StatusToASRepliesCollection(status *gtsmodel.Status, onlyOtherAccounts bool) (vocab.ActivityStreamsCollection, error) + StatusToASRepliesCollection(ctx context.Context, status *gtsmodel.Status, onlyOtherAccounts bool) (vocab.ActivityStreamsCollection, error) // StatusURIsToASRepliesPage returns a collection page with appropriate next/part of pagination. - StatusURIsToASRepliesPage(status *gtsmodel.Status, onlyOtherAccounts bool, minID string, replies map[string]*url.URL) (vocab.ActivityStreamsCollectionPage, error) + StatusURIsToASRepliesPage(ctx context.Context, status *gtsmodel.Status, onlyOtherAccounts bool, minID string, replies map[string]*url.URL) (vocab.ActivityStreamsCollectionPage, error) /* INTERNAL (gts) MODEL TO INTERNAL MODEL */ // FollowRequestToFollow just converts a follow request into a follow, that's it! No bells and whistles. - FollowRequestToFollow(f *gtsmodel.FollowRequest) *gtsmodel.Follow + FollowRequestToFollow(ctx context.Context, f *gtsmodel.FollowRequest) *gtsmodel.Follow // StatusToBoost wraps the given status into a boosting status. - StatusToBoost(s *gtsmodel.Status, boostingAccount *gtsmodel.Account) (*gtsmodel.Status, error) + StatusToBoost(ctx context.Context, s *gtsmodel.Status, boostingAccount *gtsmodel.Account) (*gtsmodel.Status, error) /* WRAPPER CONVENIENCE FUNCTIONS diff --git a/internal/typeutils/internal.go b/internal/typeutils/internal.go index ad15ecbee..23839b9a8 100644 --- a/internal/typeutils/internal.go +++ b/internal/typeutils/internal.go @@ -1,6 +1,7 @@ package typeutils import ( + "context" "fmt" "time" @@ -9,7 +10,7 @@ import ( "github.com/superseriousbusiness/gotosocial/internal/util" ) -func (c *converter) FollowRequestToFollow(f *gtsmodel.FollowRequest) *gtsmodel.Follow { +func (c *converter) FollowRequestToFollow(ctx context.Context, f *gtsmodel.FollowRequest) *gtsmodel.Follow { return >smodel.Follow{ ID: f.ID, CreatedAt: f.CreatedAt, @@ -22,7 +23,7 @@ func (c *converter) FollowRequestToFollow(f *gtsmodel.FollowRequest) *gtsmodel.F } } -func (c *converter) StatusToBoost(s *gtsmodel.Status, boostingAccount *gtsmodel.Account) (*gtsmodel.Status, error) { +func (c *converter) StatusToBoost(ctx context.Context, s *gtsmodel.Status, boostingAccount *gtsmodel.Account) (*gtsmodel.Status, error) { // the wrapper won't use the same ID as the boosted status so we generate some new UUIDs uris := util.GenerateURIsForAccount(boostingAccount.Username, c.config.Protocol, c.config.Host) boostWrapperStatusID, err := id.NewULID() diff --git a/internal/typeutils/internaltoas.go b/internal/typeutils/internaltoas.go index 178567dc6..abba3ea35 100644 --- a/internal/typeutils/internaltoas.go +++ b/internal/typeutils/internaltoas.go @@ -19,6 +19,7 @@ package typeutils import ( + "context" "crypto/x509" "encoding/pem" "fmt" @@ -33,7 +34,7 @@ import ( // 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/ -func (c *converter) AccountToAS(a *gtsmodel.Account) (vocab.ActivityStreamsPerson, error) { +func (c *converter) AccountToAS(ctx context.Context, a *gtsmodel.Account) (vocab.ActivityStreamsPerson, error) { // first check if we have this person in our asCache already if personI, err := c.asCache.Fetch(a.ID); err == nil { if person, ok := personI.(vocab.ActivityStreamsPerson); ok { @@ -214,7 +215,7 @@ func (c *converter) AccountToAS(a *gtsmodel.Account) (vocab.ActivityStreamsPerso // Used as profile avatar. if a.AvatarMediaAttachmentID != "" { avatar := >smodel.MediaAttachment{} - if err := c.db.GetByID(a.AvatarMediaAttachmentID, avatar); err != nil { + if err := c.db.GetByID(ctx, a.AvatarMediaAttachmentID, avatar); err != nil { return nil, err } @@ -242,7 +243,7 @@ func (c *converter) AccountToAS(a *gtsmodel.Account) (vocab.ActivityStreamsPerso // Used as profile header. if a.HeaderMediaAttachmentID != "" { header := >smodel.MediaAttachment{} - if err := c.db.GetByID(a.HeaderMediaAttachmentID, header); err != nil { + if err := c.db.GetByID(ctx, a.HeaderMediaAttachmentID, header); err != nil { return nil, err } @@ -278,7 +279,7 @@ func (c *converter) AccountToAS(a *gtsmodel.Account) (vocab.ActivityStreamsPerso // the spec laid out for mastodon here: https://docs.joinmastodon.org/spec/activitypub/ // // The returned account will just have the Type, Username, PublicKey, and ID properties set. -func (c *converter) AccountToASMinimal(a *gtsmodel.Account) (vocab.ActivityStreamsPerson, error) { +func (c *converter) AccountToASMinimal(ctx context.Context, a *gtsmodel.Account) (vocab.ActivityStreamsPerson, error) { person := streams.NewActivityStreamsPerson() // id should be the activitypub URI of this user @@ -340,7 +341,7 @@ func (c *converter) AccountToASMinimal(a *gtsmodel.Account) (vocab.ActivityStrea return person, nil } -func (c *converter) StatusToAS(s *gtsmodel.Status) (vocab.ActivityStreamsNote, error) { +func (c *converter) StatusToAS(ctx context.Context, s *gtsmodel.Status) (vocab.ActivityStreamsNote, error) { // first check if we have this note in our asCache already if noteI, err := c.asCache.Fetch(s.ID); err == nil { if note, ok := noteI.(vocab.ActivityStreamsNote); ok { @@ -354,7 +355,7 @@ func (c *converter) StatusToAS(s *gtsmodel.Status) (vocab.ActivityStreamsNote, e // check if author account is already attached to status and attach it if not // if we can't retrieve this, bail here already because we can't attribute the status to anyone if s.Account == nil { - a, err := c.db.GetAccountByID(s.AccountID) + a, err := c.db.GetAccountByID(ctx, s.AccountID) if err != nil { return nil, fmt.Errorf("StatusToAS: error retrieving author account from db: %s", err) } @@ -386,7 +387,7 @@ func (c *converter) StatusToAS(s *gtsmodel.Status) (vocab.ActivityStreamsNote, e // fetch the replied status if we don't have it on hand already if s.InReplyTo == nil { rs := >smodel.Status{} - if err := c.db.GetByID(s.InReplyToID, rs); err != nil { + if err := c.db.GetByID(ctx, s.InReplyToID, rs); err != nil { return nil, fmt.Errorf("StatusToAS: error retrieving replied-to status from db: %s", err) } s.InReplyTo = rs @@ -432,7 +433,7 @@ func (c *converter) StatusToAS(s *gtsmodel.Status) (vocab.ActivityStreamsNote, e // tag -- mentions for _, m := range s.Mentions { - asMention, err := c.MentionToAS(m) + asMention, err := c.MentionToAS(ctx, m) if err != nil { return nil, fmt.Errorf("StatusToAS: error converting mention to AS mention: %s", err) } @@ -520,7 +521,7 @@ func (c *converter) StatusToAS(s *gtsmodel.Status) (vocab.ActivityStreamsNote, e // attachment attachmentProp := streams.NewActivityStreamsAttachmentProperty() for _, a := range s.Attachments { - doc, err := c.AttachmentToAS(a) + doc, err := c.AttachmentToAS(ctx, a) if err != nil { return nil, fmt.Errorf("StatusToAS: error converting attachment: %s", err) } @@ -529,7 +530,7 @@ func (c *converter) StatusToAS(s *gtsmodel.Status) (vocab.ActivityStreamsNote, e status.SetActivityStreamsAttachment(attachmentProp) // replies - repliesCollection, err := c.StatusToASRepliesCollection(s, false) + repliesCollection, err := c.StatusToASRepliesCollection(ctx, s, false) if err != nil { return nil, fmt.Errorf("error creating repliesCollection: %s", err) } @@ -546,7 +547,7 @@ func (c *converter) StatusToAS(s *gtsmodel.Status) (vocab.ActivityStreamsNote, e return status, nil } -func (c *converter) FollowToAS(f *gtsmodel.Follow, originAccount *gtsmodel.Account, targetAccount *gtsmodel.Account) (vocab.ActivityStreamsFollow, error) { +func (c *converter) FollowToAS(ctx context.Context, f *gtsmodel.Follow, originAccount *gtsmodel.Account, targetAccount *gtsmodel.Account) (vocab.ActivityStreamsFollow, error) { // parse out the various URIs we need for this // origin account (who's doing the follow) originAccountURI, err := url.Parse(originAccount.URI) @@ -592,10 +593,10 @@ func (c *converter) FollowToAS(f *gtsmodel.Follow, originAccount *gtsmodel.Accou return follow, nil } -func (c *converter) MentionToAS(m *gtsmodel.Mention) (vocab.ActivityStreamsMention, error) { +func (c *converter) MentionToAS(ctx context.Context, m *gtsmodel.Mention) (vocab.ActivityStreamsMention, error) { if m.OriginAccount == nil { a := >smodel.Account{} - if err := c.db.GetWhere([]db.Where{{Key: "target_account_id", Value: m.TargetAccountID}}, a); err != nil { + if err := c.db.GetWhere(ctx, []db.Where{{Key: "target_account_id", Value: m.TargetAccountID}}, a); err != nil { return nil, fmt.Errorf("MentionToAS: error getting target account from db: %s", err) } m.OriginAccount = a @@ -629,7 +630,7 @@ func (c *converter) MentionToAS(m *gtsmodel.Mention) (vocab.ActivityStreamsMenti return mention, nil } -func (c *converter) AttachmentToAS(a *gtsmodel.MediaAttachment) (vocab.ActivityStreamsDocument, error) { +func (c *converter) AttachmentToAS(ctx context.Context, a *gtsmodel.MediaAttachment) (vocab.ActivityStreamsDocument, error) { // type -- Document doc := streams.NewActivityStreamsDocument() @@ -674,11 +675,11 @@ func (c *converter) AttachmentToAS(a *gtsmodel.MediaAttachment) (vocab.ActivityS "type": "Like" } */ -func (c *converter) FaveToAS(f *gtsmodel.StatusFave) (vocab.ActivityStreamsLike, error) { +func (c *converter) FaveToAS(ctx context.Context, f *gtsmodel.StatusFave) (vocab.ActivityStreamsLike, error) { // check if targetStatus is already pinned to this fave, and fetch it if not if f.Status == nil { s := >smodel.Status{} - if err := c.db.GetByID(f.StatusID, s); err != nil { + if err := c.db.GetByID(ctx, f.StatusID, s); err != nil { return nil, fmt.Errorf("FaveToAS: error fetching target status from database: %s", err) } f.Status = s @@ -687,7 +688,7 @@ func (c *converter) FaveToAS(f *gtsmodel.StatusFave) (vocab.ActivityStreamsLike, // check if the targetAccount is already pinned to this fave, and fetch it if not if f.TargetAccount == nil { a := >smodel.Account{} - if err := c.db.GetByID(f.TargetAccountID, a); err != nil { + if err := c.db.GetByID(ctx, f.TargetAccountID, a); err != nil { return nil, fmt.Errorf("FaveToAS: error fetching target account from database: %s", err) } f.TargetAccount = a @@ -696,7 +697,7 @@ func (c *converter) FaveToAS(f *gtsmodel.StatusFave) (vocab.ActivityStreamsLike, // check if the faving account is already pinned to this fave, and fetch it if not if f.Account == nil { a := >smodel.Account{} - if err := c.db.GetByID(f.AccountID, a); err != nil { + if err := c.db.GetByID(ctx, f.AccountID, a); err != nil { return nil, fmt.Errorf("FaveToAS: error fetching faving account from database: %s", err) } f.Account = a @@ -744,11 +745,11 @@ func (c *converter) FaveToAS(f *gtsmodel.StatusFave) (vocab.ActivityStreamsLike, return like, nil } -func (c *converter) BoostToAS(boostWrapperStatus *gtsmodel.Status, boostingAccount *gtsmodel.Account, boostedAccount *gtsmodel.Account) (vocab.ActivityStreamsAnnounce, error) { +func (c *converter) BoostToAS(ctx context.Context, boostWrapperStatus *gtsmodel.Status, boostingAccount *gtsmodel.Account, boostedAccount *gtsmodel.Account) (vocab.ActivityStreamsAnnounce, error) { // the boosted status is probably pinned to the boostWrapperStatus but double check to make sure if boostWrapperStatus.BoostOf == nil { b := >smodel.Status{} - if err := c.db.GetByID(boostWrapperStatus.BoostOfID, b); err != nil { + if err := c.db.GetByID(ctx, boostWrapperStatus.BoostOfID, b); err != nil { return nil, fmt.Errorf("BoostToAS: error getting status with ID %s from the db: %s", boostWrapperStatus.BoostOfID, err) } boostWrapperStatus.BoostOf = b @@ -828,10 +829,10 @@ func (c *converter) BoostToAS(boostWrapperStatus *gtsmodel.Status, boostingAccou "type":"Block" } */ -func (c *converter) BlockToAS(b *gtsmodel.Block) (vocab.ActivityStreamsBlock, error) { +func (c *converter) BlockToAS(ctx context.Context, b *gtsmodel.Block) (vocab.ActivityStreamsBlock, error) { if b.Account == nil { a := >smodel.Account{} - if err := c.db.GetByID(b.AccountID, a); err != nil { + if err := c.db.GetByID(ctx, b.AccountID, a); err != nil { return nil, fmt.Errorf("BlockToAS: error getting block account from database: %s", err) } b.Account = a @@ -839,7 +840,7 @@ func (c *converter) BlockToAS(b *gtsmodel.Block) (vocab.ActivityStreamsBlock, er if b.TargetAccount == nil { a := >smodel.Account{} - if err := c.db.GetByID(b.TargetAccountID, a); err != nil { + if err := c.db.GetByID(ctx, b.TargetAccountID, a); err != nil { return nil, fmt.Errorf("BlockToAS: error getting block target account from database: %s", err) } b.TargetAccount = a @@ -903,7 +904,7 @@ func (c *converter) BlockToAS(b *gtsmodel.Block) (vocab.ActivityStreamsBlock, er } } */ -func (c *converter) StatusToASRepliesCollection(status *gtsmodel.Status, onlyOtherAccounts bool) (vocab.ActivityStreamsCollection, error) { +func (c *converter) StatusToASRepliesCollection(ctx context.Context, status *gtsmodel.Status, onlyOtherAccounts bool) (vocab.ActivityStreamsCollection, error) { collectionID := fmt.Sprintf("%s/replies", status.URI) collectionIDURI, err := url.Parse(collectionID) if err != nil { @@ -966,7 +967,7 @@ func (c *converter) StatusToASRepliesCollection(status *gtsmodel.Status, onlyOth ] } */ -func (c *converter) StatusURIsToASRepliesPage(status *gtsmodel.Status, onlyOtherAccounts bool, minID string, replies map[string]*url.URL) (vocab.ActivityStreamsCollectionPage, error) { +func (c *converter) StatusURIsToASRepliesPage(ctx context.Context, status *gtsmodel.Status, onlyOtherAccounts bool, minID string, replies map[string]*url.URL) (vocab.ActivityStreamsCollectionPage, error) { collectionID := fmt.Sprintf("%s/replies", status.URI) page := streams.NewActivityStreamsCollectionPage() diff --git a/internal/typeutils/internaltoas_test.go b/internal/typeutils/internaltoas_test.go index caa56ce0d..46f04df2f 100644 --- a/internal/typeutils/internaltoas_test.go +++ b/internal/typeutils/internaltoas_test.go @@ -19,6 +19,7 @@ package typeutils_test import ( + "context" "encoding/json" "fmt" "testing" @@ -58,7 +59,7 @@ func (suite *InternalToASTestSuite) TearDownTest() { func (suite *InternalToASTestSuite) TestAccountToAS() { testAccount := suite.accounts["local_account_1"] // take zork for this test - asPerson, err := suite.typeconverter.AccountToAS(testAccount) + asPerson, err := suite.typeconverter.AccountToAS(context.Background(), testAccount) assert.NoError(suite.T(), err) ser, err := streams.Serialize(asPerson) diff --git a/internal/typeutils/internaltofrontend.go b/internal/typeutils/internaltofrontend.go index caa14e211..52e394698 100644 --- a/internal/typeutils/internaltofrontend.go +++ b/internal/typeutils/internaltofrontend.go @@ -19,6 +19,7 @@ package typeutils import ( + "context" "fmt" "strings" "time" @@ -28,9 +29,9 @@ import ( "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" ) -func (c *converter) AccountToMastoSensitive(a *gtsmodel.Account) (*model.Account, error) { +func (c *converter) AccountToMastoSensitive(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(a) + mastoAccount, err := c.AccountToMastoPublic(ctx, a) if err != nil { return nil, err } @@ -38,7 +39,7 @@ func (c *converter) AccountToMastoSensitive(a *gtsmodel.Account) (*model.Account // then adding the Source object to it... // check pending follow requests aimed at this account - frs, err := c.db.GetAccountFollowRequests(a.ID) + frs, err := c.db.GetAccountFollowRequests(ctx, a.ID) if err != nil { if err != db.ErrNoEntries { return nil, fmt.Errorf("error getting follow requests: %s", err) @@ -50,7 +51,7 @@ func (c *converter) AccountToMastoSensitive(a *gtsmodel.Account) (*model.Account } mastoAccount.Source = &model.Source{ - Privacy: c.VisToMasto(a.Privacy), + Privacy: c.VisToMasto(ctx, a.Privacy), Sensitive: a.Sensitive, Language: a.Language, Note: a.Note, @@ -61,7 +62,7 @@ func (c *converter) AccountToMastoSensitive(a *gtsmodel.Account) (*model.Account return mastoAccount, nil } -func (c *converter) AccountToMastoPublic(a *gtsmodel.Account) (*model.Account, error) { +func (c *converter) AccountToMastoPublic(ctx context.Context, a *gtsmodel.Account) (*model.Account, error) { // first check if we have this account in our frontEnd cache if accountI, err := c.frontendCache.Fetch(a.ID); err == nil { if account, ok := accountI.(*model.Account); ok { @@ -71,26 +72,26 @@ func (c *converter) AccountToMastoPublic(a *gtsmodel.Account) (*model.Account, e } // count followers - followersCount, err := c.db.CountAccountFollowedBy(a.ID, false) + followersCount, err := c.db.CountAccountFollowedBy(ctx, a.ID, false) if err != nil { return nil, fmt.Errorf("error counting followers: %s", err) } // count following - followingCount, err := c.db.CountAccountFollows(a.ID, false) + followingCount, err := c.db.CountAccountFollows(ctx, a.ID, false) if err != nil { return nil, fmt.Errorf("error counting following: %s", err) } // count statuses - statusesCount, err := c.db.CountAccountStatuses(a.ID) + statusesCount, err := c.db.CountAccountStatuses(ctx, a.ID) if err != nil { return nil, fmt.Errorf("error counting statuses: %s", err) } // check when the last status was var lastStatusAt string - lastPosted, err := c.db.GetAccountLastPosted(a.ID) + lastPosted, err := c.db.GetAccountLastPosted(ctx, a.ID) if err == nil && !lastPosted.IsZero() { lastStatusAt = lastPosted.Format(time.RFC3339) } @@ -101,7 +102,7 @@ func (c *converter) AccountToMastoPublic(a *gtsmodel.Account) (*model.Account, e if a.AvatarMediaAttachmentID != "" { // make sure avi is pinned to this account if a.AvatarMediaAttachment == nil { - avi, err := c.db.GetAttachmentByID(a.AvatarMediaAttachmentID) + avi, err := c.db.GetAttachmentByID(ctx, a.AvatarMediaAttachmentID) if err != nil { return nil, fmt.Errorf("error retrieving avatar: %s", err) } @@ -116,7 +117,7 @@ func (c *converter) AccountToMastoPublic(a *gtsmodel.Account) (*model.Account, e if a.HeaderMediaAttachmentID != "" { // make sure header is pinned to this account if a.HeaderMediaAttachment == nil { - avi, err := c.db.GetAttachmentByID(a.HeaderMediaAttachmentID) + avi, err := c.db.GetAttachmentByID(ctx, a.HeaderMediaAttachmentID) if err != nil { return nil, fmt.Errorf("error retrieving avatar: %s", err) } @@ -187,7 +188,7 @@ func (c *converter) AccountToMastoPublic(a *gtsmodel.Account) (*model.Account, e return accountFrontend, nil } -func (c *converter) AccountToMastoBlocked(a *gtsmodel.Account) (*model.Account, error) { +func (c *converter) AccountToMastoBlocked(ctx context.Context, a *gtsmodel.Account) (*model.Account, error) { var acct string if a.Domain != "" { // this is a remote user @@ -214,7 +215,7 @@ func (c *converter) AccountToMastoBlocked(a *gtsmodel.Account) (*model.Account, }, nil } -func (c *converter) AppToMastoSensitive(a *gtsmodel.Application) (*model.Application, error) { +func (c *converter) AppToMastoSensitive(ctx context.Context, a *gtsmodel.Application) (*model.Application, error) { return &model.Application{ ID: a.ID, Name: a.Name, @@ -226,14 +227,14 @@ func (c *converter) AppToMastoSensitive(a *gtsmodel.Application) (*model.Applica }, nil } -func (c *converter) AppToMastoPublic(a *gtsmodel.Application) (*model.Application, error) { +func (c *converter) AppToMastoPublic(ctx context.Context, a *gtsmodel.Application) (*model.Application, error) { return &model.Application{ Name: a.Name, Website: a.Website, }, nil } -func (c *converter) AttachmentToMasto(a *gtsmodel.MediaAttachment) (model.Attachment, error) { +func (c *converter) AttachmentToMasto(ctx context.Context, a *gtsmodel.MediaAttachment) (model.Attachment, error) { return model.Attachment{ ID: a.ID, Type: strings.ToLower(string(a.Type)), @@ -264,9 +265,9 @@ func (c *converter) AttachmentToMasto(a *gtsmodel.MediaAttachment) (model.Attach }, nil } -func (c *converter) MentionToMasto(m *gtsmodel.Mention) (model.Mention, error) { +func (c *converter) MentionToMasto(ctx context.Context, m *gtsmodel.Mention) (model.Mention, error) { target := >smodel.Account{} - if err := c.db.GetByID(m.TargetAccountID, target); err != nil { + if err := c.db.GetByID(ctx, m.TargetAccountID, target); err != nil { return model.Mention{}, err } @@ -290,7 +291,7 @@ func (c *converter) MentionToMasto(m *gtsmodel.Mention) (model.Mention, error) { }, nil } -func (c *converter) EmojiToMasto(e *gtsmodel.Emoji) (model.Emoji, error) { +func (c *converter) EmojiToMasto(ctx context.Context, e *gtsmodel.Emoji) (model.Emoji, error) { return model.Emoji{ Shortcode: e.Shortcode, URL: e.ImageURL, @@ -300,7 +301,7 @@ func (c *converter) EmojiToMasto(e *gtsmodel.Emoji) (model.Emoji, error) { }, nil } -func (c *converter) TagToMasto(t *gtsmodel.Tag) (model.Tag, error) { +func (c *converter) TagToMasto(ctx context.Context, t *gtsmodel.Tag) (model.Tag, error) { tagURL := fmt.Sprintf("%s://%s/tags/%s", c.config.Protocol, c.config.Host, t.Name) return model.Tag{ @@ -309,18 +310,18 @@ func (c *converter) TagToMasto(t *gtsmodel.Tag) (model.Tag, error) { }, nil } -func (c *converter) StatusToMasto(s *gtsmodel.Status, requestingAccount *gtsmodel.Account) (*model.Status, error) { - repliesCount, err := c.db.CountStatusReplies(s) +func (c *converter) StatusToMasto(ctx context.Context, s *gtsmodel.Status, requestingAccount *gtsmodel.Account) (*model.Status, error) { + repliesCount, err := c.db.CountStatusReplies(ctx, s) if err != nil { return nil, fmt.Errorf("error counting replies: %s", err) } - reblogsCount, err := c.db.CountStatusReblogs(s) + reblogsCount, err := c.db.CountStatusReblogs(ctx, s) if err != nil { return nil, fmt.Errorf("error counting reblogs: %s", err) } - favesCount, err := c.db.CountStatusFaves(s) + favesCount, err := c.db.CountStatusFaves(ctx, s) if err != nil { return nil, fmt.Errorf("error counting faves: %s", err) } @@ -331,7 +332,7 @@ func (c *converter) StatusToMasto(s *gtsmodel.Status, requestingAccount *gtsmode if s.BoostOf == nil { // it's not set so fetch it from the db bs := >smodel.Status{} - if err := c.db.GetByID(s.BoostOfID, bs); err != nil { + if err := c.db.GetByID(ctx, s.BoostOfID, bs); err != nil { return nil, fmt.Errorf("error getting boosted status with id %s: %s", s.BoostOfID, err) } s.BoostOf = bs @@ -341,14 +342,14 @@ func (c *converter) StatusToMasto(s *gtsmodel.Status, requestingAccount *gtsmode if s.BoostOfAccount == nil { // it's not set so fetch it from the db ba := >smodel.Account{} - if err := c.db.GetByID(s.BoostOf.AccountID, ba); err != nil { + if err := c.db.GetByID(ctx, s.BoostOf.AccountID, ba); err != nil { return nil, fmt.Errorf("error getting boosted account %s from status with id %s: %s", s.BoostOf.AccountID, s.BoostOfID, err) } s.BoostOfAccount = ba s.BoostOf.Account = ba } - mastoRebloggedStatus, err = c.StatusToMasto(s.BoostOf, requestingAccount) + mastoRebloggedStatus, err = c.StatusToMasto(ctx, s.BoostOf, requestingAccount) if err != nil { return nil, fmt.Errorf("error converting boosted status to mastotype: %s", err) } @@ -357,10 +358,10 @@ func (c *converter) StatusToMasto(s *gtsmodel.Status, requestingAccount *gtsmode var mastoApplication *model.Application if s.CreatedWithApplicationID != "" { gtsApplication := >smodel.Application{} - if err := c.db.GetByID(s.CreatedWithApplicationID, gtsApplication); err != nil { + 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(gtsApplication) + mastoApplication, err = c.AppToMastoPublic(ctx, gtsApplication) if err != nil { return nil, fmt.Errorf("error parsing application used to create status: %s", err) } @@ -368,13 +369,13 @@ func (c *converter) StatusToMasto(s *gtsmodel.Status, requestingAccount *gtsmode if s.Account == nil { a := >smodel.Account{} - if err := c.db.GetByID(s.AccountID, a); err != nil { + if err := c.db.GetByID(ctx, s.AccountID, a); err != nil { return nil, fmt.Errorf("error getting status author: %s", err) } s.Account = a } - mastoAuthorAccount, err := c.AccountToMastoPublic(s.Account) + mastoAuthorAccount, err := c.AccountToMastoPublic(ctx, s.Account) if err != nil { return nil, fmt.Errorf("error parsing account of status author: %s", err) } @@ -384,7 +385,7 @@ func (c *converter) StatusToMasto(s *gtsmodel.Status, requestingAccount *gtsmode // if so, we can directly convert the gts attachments into masto ones if s.Attachments != nil { for _, gtsAttachment := range s.Attachments { - mastoAttachment, err := c.AttachmentToMasto(gtsAttachment) + mastoAttachment, err := c.AttachmentToMasto(ctx, gtsAttachment) if err != nil { return nil, fmt.Errorf("error converting attachment with id %s: %s", gtsAttachment.ID, err) } @@ -395,10 +396,10 @@ func (c *converter) StatusToMasto(s *gtsmodel.Status, requestingAccount *gtsmode } else { for _, a := range s.AttachmentIDs { gtsAttachment := >smodel.MediaAttachment{} - if err := c.db.GetByID(a, gtsAttachment); err != nil { + if err := c.db.GetByID(ctx, a, gtsAttachment); err != nil { return nil, fmt.Errorf("error getting attachment with id %s: %s", a, err) } - mastoAttachment, err := c.AttachmentToMasto(gtsAttachment) + mastoAttachment, err := c.AttachmentToMasto(ctx, gtsAttachment) if err != nil { return nil, fmt.Errorf("error converting attachment with id %s: %s", a, err) } @@ -411,7 +412,7 @@ func (c *converter) StatusToMasto(s *gtsmodel.Status, requestingAccount *gtsmode // if so, we can directly convert the gts mentions into masto ones if s.Mentions != nil { for _, gtsMention := range s.Mentions { - mastoMention, err := c.MentionToMasto(gtsMention) + mastoMention, err := c.MentionToMasto(ctx, gtsMention) if err != nil { return nil, fmt.Errorf("error converting mention with id %s: %s", gtsMention.ID, err) } @@ -422,10 +423,10 @@ func (c *converter) StatusToMasto(s *gtsmodel.Status, requestingAccount *gtsmode } else { for _, m := range s.MentionIDs { gtsMention := >smodel.Mention{} - if err := c.db.GetByID(m, gtsMention); err != nil { + if err := c.db.GetByID(ctx, m, gtsMention); err != nil { return nil, fmt.Errorf("error getting mention with id %s: %s", m, err) } - mastoMention, err := c.MentionToMasto(gtsMention) + mastoMention, err := c.MentionToMasto(ctx, gtsMention) if err != nil { return nil, fmt.Errorf("error converting mention with id %s: %s", gtsMention.ID, err) } @@ -438,7 +439,7 @@ func (c *converter) StatusToMasto(s *gtsmodel.Status, requestingAccount *gtsmode // if so, we can directly convert the gts tags into masto ones if s.Tags != nil { for _, gtsTag := range s.Tags { - mastoTag, err := c.TagToMasto(gtsTag) + mastoTag, err := c.TagToMasto(ctx, gtsTag) if err != nil { return nil, fmt.Errorf("error converting tag with id %s: %s", gtsTag.ID, err) } @@ -449,10 +450,10 @@ func (c *converter) StatusToMasto(s *gtsmodel.Status, requestingAccount *gtsmode } else { for _, t := range s.TagIDs { gtsTag := >smodel.Tag{} - if err := c.db.GetByID(t, gtsTag); err != nil { + if err := c.db.GetByID(ctx, t, gtsTag); err != nil { return nil, fmt.Errorf("error getting tag with id %s: %s", t, err) } - mastoTag, err := c.TagToMasto(gtsTag) + mastoTag, err := c.TagToMasto(ctx, gtsTag) if err != nil { return nil, fmt.Errorf("error converting tag with id %s: %s", gtsTag.ID, err) } @@ -465,7 +466,7 @@ func (c *converter) StatusToMasto(s *gtsmodel.Status, requestingAccount *gtsmode // if so, we can directly convert the gts emojis into masto ones if s.Emojis != nil { for _, gtsEmoji := range s.Emojis { - mastoEmoji, err := c.EmojiToMasto(gtsEmoji) + mastoEmoji, err := c.EmojiToMasto(ctx, gtsEmoji) if err != nil { return nil, fmt.Errorf("error converting emoji with id %s: %s", gtsEmoji.ID, err) } @@ -476,10 +477,10 @@ func (c *converter) StatusToMasto(s *gtsmodel.Status, requestingAccount *gtsmode } else { for _, e := range s.EmojiIDs { gtsEmoji := >smodel.Emoji{} - if err := c.db.GetByID(e, gtsEmoji); err != nil { + if err := c.db.GetByID(ctx, e, gtsEmoji); err != nil { return nil, fmt.Errorf("error getting emoji with id %s: %s", e, err) } - mastoEmoji, err := c.EmojiToMasto(gtsEmoji) + mastoEmoji, err := c.EmojiToMasto(ctx, gtsEmoji) if err != nil { return nil, fmt.Errorf("error converting emoji with id %s: %s", gtsEmoji.ID, err) } @@ -491,7 +492,7 @@ func (c *converter) StatusToMasto(s *gtsmodel.Status, requestingAccount *gtsmode var mastoPoll *model.Poll statusInteractions := &statusInteractions{} - si, err := c.interactionsWithStatusForAccount(s, requestingAccount) + si, err := c.interactionsWithStatusForAccount(ctx, s, requestingAccount) if err == nil { statusInteractions = si } @@ -503,7 +504,7 @@ func (c *converter) StatusToMasto(s *gtsmodel.Status, requestingAccount *gtsmode InReplyToAccountID: s.InReplyToAccountID, Sensitive: s.Sensitive, SpoilerText: s.ContentWarning, - Visibility: c.VisToMasto(s.Visibility), + Visibility: c.VisToMasto(ctx, s.Visibility), Language: s.Language, URI: s.URI, URL: s.URL, @@ -535,7 +536,7 @@ func (c *converter) StatusToMasto(s *gtsmodel.Status, requestingAccount *gtsmode } // VisToMasto converts a gts visibility into its mastodon equivalent -func (c *converter) VisToMasto(m gtsmodel.Visibility) model.Visibility { +func (c *converter) VisToMasto(ctx context.Context, m gtsmodel.Visibility) model.Visibility { switch m { case gtsmodel.VisibilityPublic: return model.VisibilityPublic @@ -549,7 +550,7 @@ func (c *converter) VisToMasto(m gtsmodel.Visibility) model.Visibility { return "" } -func (c *converter) InstanceToMasto(i *gtsmodel.Instance) (*model.Instance, error) { +func (c *converter) InstanceToMasto(ctx context.Context, i *gtsmodel.Instance) (*model.Instance, error) { mi := &model.Instance{ URI: i.URI, Title: i.Title, @@ -567,17 +568,17 @@ func (c *converter) InstanceToMasto(i *gtsmodel.Instance) (*model.Instance, erro statusCountKey := "status_count" domainCountKey := "domain_count" - userCount, err := c.db.CountInstanceUsers(c.config.Host) + userCount, err := c.db.CountInstanceUsers(ctx, c.config.Host) if err == nil { mi.Stats[userCountKey] = userCount } - statusCount, err := c.db.CountInstanceStatuses(c.config.Host) + statusCount, err := c.db.CountInstanceStatuses(ctx, c.config.Host) if err == nil { mi.Stats[statusCountKey] = statusCount } - domainCount, err := c.db.CountInstanceDomains(c.config.Host) + domainCount, err := c.db.CountInstanceDomains(ctx, c.config.Host) if err == nil { mi.Stats[domainCountKey] = domainCount } @@ -593,7 +594,7 @@ func (c *converter) InstanceToMasto(i *gtsmodel.Instance) (*model.Instance, erro } // get the instance account if it exists and just skip if it doesn't - ia, err := c.db.GetInstanceAccount("") + ia, err := c.db.GetInstanceAccount(ctx, "") if err == nil { if ia.HeaderMediaAttachment != nil { mi.Thumbnail = ia.HeaderMediaAttachment.URL @@ -603,8 +604,8 @@ func (c *converter) InstanceToMasto(i *gtsmodel.Instance) (*model.Instance, erro // contact account is optional but let's try to get it if i.ContactAccountID != "" { ia := >smodel.Account{} - if err := c.db.GetByID(i.ContactAccountID, ia); err == nil { - ma, err := c.AccountToMastoPublic(ia) + if err := c.db.GetByID(ctx, i.ContactAccountID, ia); err == nil { + ma, err := c.AccountToMastoPublic(ctx, ia) if err == nil { mi.ContactAccount = ma } @@ -614,7 +615,7 @@ func (c *converter) InstanceToMasto(i *gtsmodel.Instance) (*model.Instance, erro return mi, nil } -func (c *converter) RelationshipToMasto(r *gtsmodel.Relationship) (*model.Relationship, error) { +func (c *converter) RelationshipToMasto(ctx context.Context, r *gtsmodel.Relationship) (*model.Relationship, error) { return &model.Relationship{ ID: r.ID, Following: r.Following, @@ -632,9 +633,9 @@ func (c *converter) RelationshipToMasto(r *gtsmodel.Relationship) (*model.Relati }, nil } -func (c *converter) NotificationToMasto(n *gtsmodel.Notification) (*model.Notification, error) { +func (c *converter) NotificationToMasto(ctx context.Context, n *gtsmodel.Notification) (*model.Notification, error) { if n.TargetAccount == nil { - tAccount, err := c.db.GetAccountByID(n.TargetAccountID) + 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) } @@ -642,14 +643,14 @@ func (c *converter) NotificationToMasto(n *gtsmodel.Notification) (*model.Notifi } if n.OriginAccount == nil { - ogAccount, err := c.db.GetAccountByID(n.OriginAccountID) + 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) } n.OriginAccount = ogAccount } - mastoAccount, err := c.AccountToMastoPublic(n.OriginAccount) + mastoAccount, err := c.AccountToMastoPublic(ctx, n.OriginAccount) if err != nil { return nil, fmt.Errorf("NotificationToMasto: error converting account to masto: %s", err) } @@ -657,7 +658,7 @@ func (c *converter) NotificationToMasto(n *gtsmodel.Notification) (*model.Notifi var mastoStatus *model.Status if n.StatusID != "" { if n.Status == nil { - status, err := c.db.GetStatusByID(n.StatusID) + 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) } @@ -673,7 +674,7 @@ func (c *converter) NotificationToMasto(n *gtsmodel.Notification) (*model.Notifi } var err error - mastoStatus, err = c.StatusToMasto(n.Status, nil) + mastoStatus, err = c.StatusToMasto(ctx, n.Status, nil) if err != nil { return nil, fmt.Errorf("NotificationToMasto: error converting status to masto: %s", err) } @@ -688,7 +689,7 @@ func (c *converter) NotificationToMasto(n *gtsmodel.Notification) (*model.Notifi }, nil } -func (c *converter) DomainBlockToMasto(b *gtsmodel.DomainBlock, export bool) (*model.DomainBlock, error) { +func (c *converter) DomainBlockToMasto(ctx context.Context, b *gtsmodel.DomainBlock, export bool) (*model.DomainBlock, error) { domainBlock := &model.DomainBlock{ Domain: b.Domain, diff --git a/internal/typeutils/util.go b/internal/typeutils/util.go index 5751fbc84..1d1903afc 100644 --- a/internal/typeutils/util.go +++ b/internal/typeutils/util.go @@ -1,34 +1,35 @@ package typeutils import ( + "context" "fmt" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" ) -func (c *converter) interactionsWithStatusForAccount(s *gtsmodel.Status, requestingAccount *gtsmodel.Account) (*statusInteractions, error) { +func (c *converter) interactionsWithStatusForAccount(ctx context.Context, s *gtsmodel.Status, requestingAccount *gtsmodel.Account) (*statusInteractions, error) { si := &statusInteractions{} if requestingAccount != nil { - faved, err := c.db.IsStatusFavedBy(s, requestingAccount.ID) + faved, err := c.db.IsStatusFavedBy(ctx, s, requestingAccount.ID) if err != nil { return nil, fmt.Errorf("error checking if requesting account has faved status: %s", err) } si.Faved = faved - reblogged, err := c.db.IsStatusRebloggedBy(s, requestingAccount.ID) + reblogged, err := c.db.IsStatusRebloggedBy(ctx, s, requestingAccount.ID) if err != nil { return nil, fmt.Errorf("error checking if requesting account has reblogged status: %s", err) } si.Reblogged = reblogged - muted, err := c.db.IsStatusMutedBy(s, requestingAccount.ID) + muted, err := c.db.IsStatusMutedBy(ctx, s, requestingAccount.ID) if err != nil { return nil, fmt.Errorf("error checking if requesting account has muted status: %s", err) } si.Muted = muted - bookmarked, err := c.db.IsStatusBookmarkedBy(s, requestingAccount.ID) + bookmarked, err := c.db.IsStatusBookmarkedBy(ctx, s, requestingAccount.ID) if err != nil { return nil, fmt.Errorf("error checking if requesting account has bookmarked status: %s", err) } diff --git a/internal/visibility/filter.go b/internal/visibility/filter.go index 2c43fa4ee..644e85b35 100644 --- a/internal/visibility/filter.go +++ b/internal/visibility/filter.go @@ -19,6 +19,8 @@ package visibility import ( + "context" + "github.com/sirupsen/logrus" "github.com/superseriousbusiness/gotosocial/internal/db" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" @@ -29,17 +31,17 @@ type Filter interface { // StatusVisible returns true if targetStatus is visible to requestingAccount, based on the // privacy settings of the status, and any blocks/mutes that might exist between the two accounts // or account domains, and other relevant accounts mentioned in or replied to by the status. - StatusVisible(targetStatus *gtsmodel.Status, requestingAccount *gtsmodel.Account) (bool, error) + StatusVisible(ctx context.Context, targetStatus *gtsmodel.Status, requestingAccount *gtsmodel.Account) (bool, error) // StatusHometimelineable returns true if targetStatus should be in the home timeline of the requesting account. // // This function will call StatusVisible internally, so it's not necessary to call it beforehand. - StatusHometimelineable(targetStatus *gtsmodel.Status, requestingAccount *gtsmodel.Account) (bool, error) + StatusHometimelineable(ctx context.Context, targetStatus *gtsmodel.Status, requestingAccount *gtsmodel.Account) (bool, error) // StatusPublictimelineable returns true if targetStatus should be in the public timeline of the requesting account. // // This function will call StatusVisible internally, so it's not necessary to call it beforehand. - StatusPublictimelineable(targetStatus *gtsmodel.Status, timelineOwnerAccount *gtsmodel.Account) (bool, error) + StatusPublictimelineable(ctx context.Context, targetStatus *gtsmodel.Status, timelineOwnerAccount *gtsmodel.Account) (bool, error) } type filter struct { diff --git a/internal/visibility/relevantaccounts.go b/internal/visibility/relevantaccounts.go index 5957d3111..d19d26ff4 100644 --- a/internal/visibility/relevantaccounts.go +++ b/internal/visibility/relevantaccounts.go @@ -19,6 +19,7 @@ package visibility import ( + "context" "errors" "fmt" @@ -41,7 +42,7 @@ type relevantAccounts struct { BoostedMentionedAccounts []*gtsmodel.Account } -func (f *filter) relevantAccounts(status *gtsmodel.Status, getBoosted bool) (*relevantAccounts, error) { +func (f *filter) relevantAccounts(ctx context.Context, status *gtsmodel.Status, getBoosted bool) (*relevantAccounts, error) { relAccts := &relevantAccounts{ MentionedAccounts: []*gtsmodel.Account{}, BoostedMentionedAccounts: []*gtsmodel.Account{}, @@ -77,7 +78,7 @@ func (f *filter) relevantAccounts(status *gtsmodel.Status, getBoosted bool) (*re relAccts.Account = status.Account } else { // it wasn't set, so get it from the db - account, err := f.db.GetAccountByID(status.AccountID) + account, err := f.db.GetAccountByID(ctx, status.AccountID) if err != nil { return nil, fmt.Errorf("relevantAccounts: error getting account with id %s: %s", status.AccountID, err) } @@ -96,7 +97,7 @@ func (f *filter) relevantAccounts(status *gtsmodel.Status, getBoosted bool) (*re relAccts.InReplyToAccount = status.InReplyToAccount } else { // it wasn't set, so get it from the db - inReplyToAccount, err := f.db.GetAccountByID(status.InReplyToAccountID) + inReplyToAccount, err := f.db.GetAccountByID(ctx, status.InReplyToAccountID) if err != nil { return nil, fmt.Errorf("relevantAccounts: error getting inReplyToAccount with id %s: %s", status.InReplyToAccountID, err) } @@ -115,7 +116,7 @@ func (f *filter) relevantAccounts(status *gtsmodel.Status, getBoosted bool) (*re } if !idIn(mID, status.Mentions) { // mention with ID isn't in status.Mentions - mention, err := f.db.GetMention(mID) + mention, err := f.db.GetMention(ctx, mID) if err != nil { return nil, fmt.Errorf("relevantAccounts: error getting mention with id %s: %s", mID, err) } @@ -146,7 +147,7 @@ func (f *filter) relevantAccounts(status *gtsmodel.Status, getBoosted bool) (*re // 4, 5, 6. Boosted status items // get the boosted status if it's not set on the status already if status.BoostOfID != "" && status.BoostOf == nil { - boostedStatus, err := f.db.GetStatusByID(status.BoostOfID) + boostedStatus, err := f.db.GetStatusByID(ctx, status.BoostOfID) if err != nil { return nil, fmt.Errorf("relevantAccounts: error getting boosted status with id %s: %s", status.BoostOfID, err) } @@ -155,7 +156,7 @@ func (f *filter) relevantAccounts(status *gtsmodel.Status, getBoosted bool) (*re if status.BoostOf != nil { // return relevant accounts for the boosted status - boostedRelAccts, err := f.relevantAccounts(status.BoostOf, false) // false because we don't want to recurse + boostedRelAccts, err := f.relevantAccounts(ctx, status.BoostOf, false) // false because we don't want to recurse if err != nil { return nil, fmt.Errorf("relevantAccounts: error getting relevant accounts of boosted status %s: %s", status.BoostOf.ID, err) } @@ -170,7 +171,7 @@ func (f *filter) relevantAccounts(status *gtsmodel.Status, getBoosted bool) (*re // domainBlockedRelevant checks through all relevant accounts attached to a status // to make sure none of them are domain blocked by this instance. -func (f *filter) domainBlockedRelevant(r *relevantAccounts) (bool, error) { +func (f *filter) domainBlockedRelevant(ctx context.Context, r *relevantAccounts) (bool, error) { domains := []string{} if r.Account != nil { @@ -201,7 +202,7 @@ func (f *filter) domainBlockedRelevant(r *relevantAccounts) (bool, error) { } } - return f.db.AreDomainsBlocked(domains) + return f.db.AreDomainsBlocked(ctx, domains) } func idIn(id string, mentions []*gtsmodel.Mention) bool { diff --git a/internal/visibility/statushometimelineable.go b/internal/visibility/statushometimelineable.go index a3ca62fb3..dd0ca079b 100644 --- a/internal/visibility/statushometimelineable.go +++ b/internal/visibility/statushometimelineable.go @@ -19,13 +19,14 @@ package visibility import ( + "context" "fmt" "github.com/sirupsen/logrus" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" ) -func (f *filter) StatusHometimelineable(targetStatus *gtsmodel.Status, timelineOwnerAccount *gtsmodel.Account) (bool, error) { +func (f *filter) StatusHometimelineable(ctx context.Context, targetStatus *gtsmodel.Status, timelineOwnerAccount *gtsmodel.Account) (bool, error) { l := f.log.WithFields(logrus.Fields{ "func": "StatusHometimelineable", "statusID": targetStatus.ID, @@ -36,7 +37,7 @@ func (f *filter) StatusHometimelineable(targetStatus *gtsmodel.Status, timelineO return true, nil } - v, err := f.StatusVisible(targetStatus, timelineOwnerAccount) + v, err := f.StatusVisible(ctx, targetStatus, timelineOwnerAccount) if err != nil { return false, fmt.Errorf("StatusHometimelineable: error checking visibility of status with id %s: %s", targetStatus.ID, err) } @@ -63,7 +64,7 @@ func (f *filter) StatusHometimelineable(targetStatus *gtsmodel.Status, timelineO if targetStatus.InReplyToID != "" { // pin the reply to status on to this status if it hasn't been done already if targetStatus.InReplyTo == nil { - rs, err := f.db.GetStatusByID(targetStatus.InReplyToID) + rs, err := f.db.GetStatusByID(ctx, targetStatus.InReplyToID) if err != nil { return false, fmt.Errorf("StatusHometimelineable: error getting replied to status with id %s: %s", targetStatus.InReplyToID, err) } @@ -72,7 +73,7 @@ func (f *filter) StatusHometimelineable(targetStatus *gtsmodel.Status, timelineO // pin the reply to account on to this status if it hasn't been done already if targetStatus.InReplyToAccount == nil { - ra, err := f.db.GetAccountByID(targetStatus.InReplyToAccountID) + ra, err := f.db.GetAccountByID(ctx, targetStatus.InReplyToAccountID) if err != nil { return false, fmt.Errorf("StatusHometimelineable: error getting replied to account with id %s: %s", targetStatus.InReplyToAccountID, err) } @@ -85,7 +86,7 @@ func (f *filter) StatusHometimelineable(targetStatus *gtsmodel.Status, timelineO } // the replied-to account != timelineOwnerAccount, so make sure the timelineOwnerAccount follows the replied-to account - follows, err := f.db.IsFollowing(timelineOwnerAccount, targetStatus.InReplyToAccount) + follows, err := f.db.IsFollowing(ctx, timelineOwnerAccount, targetStatus.InReplyToAccount) if err != nil { return false, fmt.Errorf("StatusHometimelineable: error checking follow from account %s to account %s: %s", timelineOwnerAccount.ID, targetStatus.InReplyToAccountID, err) } diff --git a/internal/visibility/statuspublictimelineable.go b/internal/visibility/statuspublictimelineable.go index f07e06aae..8d0a7aa28 100644 --- a/internal/visibility/statuspublictimelineable.go +++ b/internal/visibility/statuspublictimelineable.go @@ -19,13 +19,14 @@ package visibility import ( + "context" "fmt" "github.com/sirupsen/logrus" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" ) -func (f *filter) StatusPublictimelineable(targetStatus *gtsmodel.Status, timelineOwnerAccount *gtsmodel.Account) (bool, error) { +func (f *filter) StatusPublictimelineable(ctx context.Context, targetStatus *gtsmodel.Status, timelineOwnerAccount *gtsmodel.Account) (bool, error) { l := f.log.WithFields(logrus.Fields{ "func": "StatusPublictimelineable", "statusID": targetStatus.ID, @@ -41,7 +42,7 @@ func (f *filter) StatusPublictimelineable(targetStatus *gtsmodel.Status, timelin return true, nil } - v, err := f.StatusVisible(targetStatus, timelineOwnerAccount) + v, err := f.StatusVisible(ctx, targetStatus, timelineOwnerAccount) if err != nil { return false, fmt.Errorf("StatusPublictimelineable: error checking visibility of status with id %s: %s", targetStatus.ID, err) } diff --git a/internal/visibility/statusvisible.go b/internal/visibility/statusvisible.go index 15e545881..5b6fe0c1e 100644 --- a/internal/visibility/statusvisible.go +++ b/internal/visibility/statusvisible.go @@ -19,6 +19,7 @@ package visibility import ( + "context" "errors" "fmt" @@ -28,20 +29,20 @@ import ( "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" ) -func (f *filter) StatusVisible(targetStatus *gtsmodel.Status, requestingAccount *gtsmodel.Account) (bool, error) { +func (f *filter) StatusVisible(ctx context.Context, targetStatus *gtsmodel.Status, requestingAccount *gtsmodel.Account) (bool, error) { l := f.log.WithFields(logrus.Fields{ "func": "StatusVisible", "statusID": targetStatus.ID, }) getBoosted := true - relevantAccounts, err := f.relevantAccounts(targetStatus, getBoosted) + relevantAccounts, err := f.relevantAccounts(ctx, targetStatus, getBoosted) if err != nil { l.Debugf("error pulling relevant accounts for status %s: %s", targetStatus.ID, err) return false, fmt.Errorf("StatusVisible: error pulling relevant accounts for status %s: %s", targetStatus.ID, err) } - domainBlocked, err := f.domainBlockedRelevant(relevantAccounts) + domainBlocked, err := f.domainBlockedRelevant(ctx, relevantAccounts) if err != nil { l.Debugf("error checking domain block: %s", err) return false, fmt.Errorf("error checking domain block: %s", err) @@ -67,7 +68,7 @@ func (f *filter) StatusVisible(targetStatus *gtsmodel.Status, requestingAccount // note: we only do this for local users if targetAccount.Domain == "" { targetUser := >smodel.User{} - if err := f.db.GetWhere([]db.Where{{Key: "account_id", Value: targetAccount.ID}}, targetUser); err != nil { + if err := f.db.GetWhere(ctx, []db.Where{{Key: "account_id", Value: targetAccount.ID}}, targetUser); err != nil { l.Debug("target user could not be selected") if err == db.ErrNoEntries { return false, nil @@ -97,7 +98,7 @@ func (f *filter) StatusVisible(targetStatus *gtsmodel.Status, requestingAccount // note: we only do this for local users if requestingAccount.Domain == "" { requestingUser := >smodel.User{} - if err := f.db.GetWhere([]db.Where{{Key: "account_id", Value: requestingAccount.ID}}, requestingUser); err != nil { + if err := f.db.GetWhere(ctx, []db.Where{{Key: "account_id", Value: requestingAccount.ID}}, requestingUser); err != nil { // if the requesting account is local but doesn't have a corresponding user in the db this is a problem l.Debug("requesting user could not be selected") if err == db.ErrNoEntries { @@ -126,7 +127,7 @@ func (f *filter) StatusVisible(targetStatus *gtsmodel.Status, requestingAccount // At this point we have a populated targetAccount, targetStatus, and requestingAccount, so we can check for blocks and whathaveyou // First check if a block exists directly between the target account (which authored the status) and the requesting account. - if blocked, err := f.db.IsBlocked(targetAccount.ID, requestingAccount.ID, true); err != nil { + if blocked, err := f.db.IsBlocked(ctx, targetAccount.ID, requestingAccount.ID, true); err != nil { l.Debugf("something went wrong figuring out if the accounts have a block: %s", err) return false, err } else if blocked { @@ -137,7 +138,7 @@ func (f *filter) StatusVisible(targetStatus *gtsmodel.Status, requestingAccount // status replies to account id if relevantAccounts.InReplyToAccount != nil && relevantAccounts.InReplyToAccount.ID != requestingAccount.ID { - if blocked, err := f.db.IsBlocked(relevantAccounts.InReplyToAccount.ID, requestingAccount.ID, true); err != nil { + if blocked, err := f.db.IsBlocked(ctx, relevantAccounts.InReplyToAccount.ID, requestingAccount.ID, true); err != nil { return false, err } else if blocked { l.Trace("a block exists between requesting account and reply to account") @@ -146,7 +147,7 @@ func (f *filter) StatusVisible(targetStatus *gtsmodel.Status, requestingAccount // check reply to ID if targetStatus.InReplyToID != "" && (targetStatus.Visibility == gtsmodel.VisibilityFollowersOnly || targetStatus.Visibility == gtsmodel.VisibilityDirect) { - followsRepliedAccount, err := f.db.IsFollowing(requestingAccount, relevantAccounts.InReplyToAccount) + followsRepliedAccount, err := f.db.IsFollowing(ctx, requestingAccount, relevantAccounts.InReplyToAccount) if err != nil { return false, err } @@ -159,7 +160,7 @@ func (f *filter) StatusVisible(targetStatus *gtsmodel.Status, requestingAccount // status boosts accounts id if relevantAccounts.BoostedAccount != nil { - if blocked, err := f.db.IsBlocked(relevantAccounts.BoostedAccount.ID, requestingAccount.ID, true); err != nil { + if blocked, err := f.db.IsBlocked(ctx, relevantAccounts.BoostedAccount.ID, requestingAccount.ID, true); err != nil { return false, err } else if blocked { l.Trace("a block exists between requesting account and boosted account") @@ -169,7 +170,7 @@ func (f *filter) StatusVisible(targetStatus *gtsmodel.Status, requestingAccount // status boosts a reply to account id if relevantAccounts.BoostedInReplyToAccount != nil { - if blocked, err := f.db.IsBlocked(relevantAccounts.BoostedInReplyToAccount.ID, requestingAccount.ID, true); err != nil { + if blocked, err := f.db.IsBlocked(ctx, relevantAccounts.BoostedInReplyToAccount.ID, requestingAccount.ID, true); err != nil { return false, err } else if blocked { l.Trace("a block exists between requesting account and boosted reply to account") @@ -182,7 +183,7 @@ func (f *filter) StatusVisible(targetStatus *gtsmodel.Status, requestingAccount if a == nil { continue } - if blocked, err := f.db.IsBlocked(a.ID, requestingAccount.ID, true); err != nil { + if blocked, err := f.db.IsBlocked(ctx, a.ID, requestingAccount.ID, true); err != nil { return false, err } else if blocked { l.Trace("a block exists between requesting account and a mentioned account") @@ -195,7 +196,7 @@ func (f *filter) StatusVisible(targetStatus *gtsmodel.Status, requestingAccount if a == nil { continue } - if blocked, err := f.db.IsBlocked(a.ID, requestingAccount.ID, true); err != nil { + if blocked, err := f.db.IsBlocked(ctx, a.ID, requestingAccount.ID, true); err != nil { return false, err } else if blocked { l.Trace("a block exists between requesting account and a boosted mentioned account") @@ -221,7 +222,7 @@ func (f *filter) StatusVisible(targetStatus *gtsmodel.Status, requestingAccount return true, nil case gtsmodel.VisibilityFollowersOnly: // check one-way follow - follows, err := f.db.IsFollowing(requestingAccount, targetAccount) + follows, err := f.db.IsFollowing(ctx, requestingAccount, targetAccount) if err != nil { return false, err } @@ -232,7 +233,7 @@ func (f *filter) StatusVisible(targetStatus *gtsmodel.Status, requestingAccount return true, nil case gtsmodel.VisibilityMutualsOnly: // check mutual follow - mutuals, err := f.db.IsMutualFollowing(requestingAccount, targetAccount) + mutuals, err := f.db.IsMutualFollowing(ctx, requestingAccount, targetAccount) if err != nil { return false, err } diff --git a/internal/web/base.go b/internal/web/base.go index c0b85b613..eabde676c 100644 --- a/internal/web/base.go +++ b/internal/web/base.go @@ -50,7 +50,7 @@ func (m *Module) baseHandler(c *gin.Context) { l := m.log.WithField("func", "BaseGETHandler") l.Trace("serving index html") - instance, err := m.processor.InstanceGet(m.config.Host) + instance, err := m.processor.InstanceGet(c.Request.Context(), m.config.Host) if err != nil { l.Debugf("error getting instance from processor: %s", err) c.JSON(http.StatusInternalServerError, gin.H{"error": "internal server error"}) @@ -71,7 +71,7 @@ func (m *Module) NotFoundHandler(c *gin.Context) { l := m.log.WithField("func", "404") l.Trace("serving 404 html") - instance, err := m.processor.InstanceGet(m.config.Host) + instance, err := m.processor.InstanceGet(c.Request.Context(), m.config.Host) if err != nil { l.Debugf("error getting instance from processor: %s", err) c.JSON(http.StatusInternalServerError, gin.H{"error": "internal server error"})