/*
GoToSocial
Copyright (C) 2021 GoToSocial Authors admin@gotosocial.org
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see
@the_mighty_zork nice there it is:
https://social.pixie.town/users/f0x/statuses/106221628567855262/activity
`, Mentions: []*gtsmodel.Mention{ { TargetAccountURI: repliedAccount.URI, NameString: "@the_mighty_zork@localhost:8080", }, }, AccountID: replyingAccount.ID, AccountURI: replyingAccount.URI, InReplyToID: repliedStatus.ID, InReplyToURI: repliedStatus.URI, InReplyToAccountID: repliedAccount.ID, Visibility: gtsmodel.VisibilityUnlocked, ActivityStreamsType: ap.ObjectNote, Federated: true, Boostable: true, Replyable: true, Likeable: true, } // id the status based on the time it was created statusID, err := id.NewULIDFromTime(replyingStatus.CreatedAt) suite.NoError(err) replyingStatus.ID = statusID err = suite.db.PutStatus(context.Background(), replyingStatus) suite.NoError(err) err = suite.processor.ProcessFromFederator(context.Background(), messages.FromFederator{ APObjectType: ap.ObjectNote, APActivityType: ap.ActivityCreate, GTSModel: replyingStatus, ReceivingAccount: suite.testAccounts["local_account_1"], }) suite.NoError(err) // side effects should be triggered // 1. status should be in the database suite.NotEmpty(replyingStatus.ID) _, err = suite.db.GetStatusByID(context.Background(), replyingStatus.ID) suite.NoError(err) // 2. a notification should exist for the mention where := []db.Where{ { Key: "status_id", Value: replyingStatus.ID, }, } notif := >smodel.Notification{} err = suite.db.GetWhere(context.Background(), where, notif) suite.NoError(err) suite.Equal(gtsmodel.NotificationMention, notif.NotificationType) suite.Equal(replyingStatus.InReplyToAccountID, notif.TargetAccountID) suite.Equal(replyingStatus.AccountID, notif.OriginAccountID) suite.Equal(replyingStatus.ID, notif.StatusID) suite.False(notif.Read) } func (suite *FromFederatorTestSuite) TestProcessFave() { favedAccount := suite.testAccounts["local_account_1"] favedStatus := suite.testStatuses["local_account_1_status_1"] favingAccount := suite.testAccounts["remote_account_1"] stream, errWithCode := suite.processor.OpenStreamForAccount(context.Background(), favedAccount, "user") suite.NoError(errWithCode) fave := >smodel.StatusFave{ ID: "01FGKJPXFTVQPG9YSSZ95ADS7Q", CreatedAt: time.Now(), UpdatedAt: time.Now(), AccountID: favingAccount.ID, Account: favingAccount, TargetAccountID: favedAccount.ID, TargetAccount: favedAccount, StatusID: favedStatus.ID, Status: favedStatus, URI: favingAccount.URI + "/faves/aaaaaaaaaaaa", } err := suite.db.Put(context.Background(), fave) suite.NoError(err) err = suite.processor.ProcessFromFederator(context.Background(), messages.FromFederator{ APObjectType: ap.ActivityLike, APActivityType: ap.ActivityCreate, GTSModel: fave, ReceivingAccount: favedAccount, }) suite.NoError(err) // side effects should be triggered // 1. a notification should exist for the fave where := []db.Where{ { Key: "status_id", Value: favedStatus.ID, }, { Key: "origin_account_id", Value: favingAccount.ID, }, } notif := >smodel.Notification{} err = suite.db.GetWhere(context.Background(), where, notif) suite.NoError(err) suite.Equal(gtsmodel.NotificationFave, notif.NotificationType) suite.Equal(fave.TargetAccountID, notif.TargetAccountID) suite.Equal(fave.AccountID, notif.OriginAccountID) suite.Equal(fave.StatusID, notif.StatusID) suite.False(notif.Read) // 2. a notification should be streamed msg := <-stream.Messages suite.Equal("notification", msg.Event) suite.NotEmpty(msg.Payload) suite.EqualValues([]string{"user"}, msg.Stream) } // TestProcessFaveWithDifferentReceivingAccount ensures that when an account receives a fave that's for // another account in their AP inbox, a notification isn't streamed to the receiving account. // // This tests for an issue we were seeing where Misskey sends out faves to inboxes of people that don't own // the fave, but just follow the actor who received the fave. func (suite *FromFederatorTestSuite) TestProcessFaveWithDifferentReceivingAccount() { receivingAccount := suite.testAccounts["local_account_2"] favedAccount := suite.testAccounts["local_account_1"] favedStatus := suite.testStatuses["local_account_1_status_1"] favingAccount := suite.testAccounts["remote_account_1"] stream, errWithCode := suite.processor.OpenStreamForAccount(context.Background(), receivingAccount, "user") suite.NoError(errWithCode) fave := >smodel.StatusFave{ ID: "01FGKJPXFTVQPG9YSSZ95ADS7Q", CreatedAt: time.Now(), UpdatedAt: time.Now(), AccountID: favingAccount.ID, Account: favingAccount, TargetAccountID: favedAccount.ID, TargetAccount: favedAccount, StatusID: favedStatus.ID, Status: favedStatus, URI: favingAccount.URI + "/faves/aaaaaaaaaaaa", } err := suite.db.Put(context.Background(), fave) suite.NoError(err) err = suite.processor.ProcessFromFederator(context.Background(), messages.FromFederator{ APObjectType: ap.ActivityLike, APActivityType: ap.ActivityCreate, GTSModel: fave, ReceivingAccount: receivingAccount, }) suite.NoError(err) // side effects should be triggered // 1. a notification should exist for the fave where := []db.Where{ { Key: "status_id", Value: favedStatus.ID, }, { Key: "origin_account_id", Value: favingAccount.ID, }, } notif := >smodel.Notification{} err = suite.db.GetWhere(context.Background(), where, notif) suite.NoError(err) suite.Equal(gtsmodel.NotificationFave, notif.NotificationType) suite.Equal(fave.TargetAccountID, notif.TargetAccountID) suite.Equal(fave.AccountID, notif.OriginAccountID) suite.Equal(fave.StatusID, notif.StatusID) suite.False(notif.Read) // 2. no notification should be streamed to the account that received the fave message, because they weren't the target suite.Empty(stream.Messages) } func (suite *FromFederatorTestSuite) TestProcessAccountDelete() { ctx := context.Background() deletedAccount := suite.testAccounts["remote_account_1"] receivingAccount := suite.testAccounts["local_account_1"] // before doing the delete.... // make local_account_1 and remote_account_1 into mufos zorkFollowSatan := >smodel.Follow{ ID: "01FGRY72ASHBSET64353DPHK9T", CreatedAt: time.Now().Add(-1 * time.Hour), UpdatedAt: time.Now().Add(-1 * time.Hour), AccountID: deletedAccount.ID, TargetAccountID: receivingAccount.ID, ShowReblogs: true, URI: fmt.Sprintf("%s/follows/01FGRY72ASHBSET64353DPHK9T", deletedAccount.URI), Notify: false, } err := suite.db.Put(ctx, zorkFollowSatan) suite.NoError(err) satanFollowZork := >smodel.Follow{ ID: "01FGRYAVAWWPP926J175QGM0WV", CreatedAt: time.Now().Add(-1 * time.Hour), UpdatedAt: time.Now().Add(-1 * time.Hour), AccountID: receivingAccount.ID, TargetAccountID: deletedAccount.ID, ShowReblogs: true, URI: fmt.Sprintf("%s/follows/01FGRYAVAWWPP926J175QGM0WV", receivingAccount.URI), Notify: false, } err = suite.db.Put(ctx, satanFollowZork) suite.NoError(err) // now they are mufos! err = suite.processor.ProcessFromFederator(ctx, messages.FromFederator{ APObjectType: ap.ObjectProfile, APActivityType: ap.ActivityDelete, GTSModel: deletedAccount, ReceivingAccount: receivingAccount, }) suite.NoError(err) // local account 2 blocked foss_satan, that block should be gone now testBlock := suite.testBlocks["local_account_2_block_remote_account_1"] dbBlock := >smodel.Block{} err = suite.db.GetByID(ctx, testBlock.ID, dbBlock) suite.ErrorIs(err, db.ErrNoEntries) // the mufos should be gone now too satanFollowsZork, err := suite.db.IsFollowing(ctx, deletedAccount, receivingAccount) suite.NoError(err) suite.False(satanFollowsZork) zorkFollowsSatan, err := suite.db.IsFollowing(ctx, receivingAccount, deletedAccount) suite.NoError(err) suite.False(zorkFollowsSatan) // no statuses from foss satan should be left in the database dbStatuses, err := suite.db.GetAccountStatuses(ctx, deletedAccount.ID, 0, false, "", "", false, false, false) suite.ErrorIs(err, db.ErrNoEntries) suite.Empty(dbStatuses) dbAccount, err := suite.db.GetAccountByID(ctx, deletedAccount.ID) suite.NoError(err) suite.Empty(dbAccount.Note) suite.Empty(dbAccount.DisplayName) suite.Empty(dbAccount.AvatarMediaAttachmentID) suite.Empty(dbAccount.AvatarRemoteURL) suite.Empty(dbAccount.HeaderMediaAttachmentID) suite.Empty(dbAccount.HeaderRemoteURL) suite.Empty(dbAccount.Reason) suite.Empty(dbAccount.Fields) suite.True(dbAccount.HideCollections) suite.False(dbAccount.Discoverable) suite.WithinDuration(time.Now(), dbAccount.SuspendedAt, 30*time.Second) suite.Equal(dbAccount.ID, dbAccount.SuspensionOrigin) } func (suite *FromFederatorTestSuite) TestProcessFollowRequestLocked() { ctx := context.Background() originAccount := suite.testAccounts["remote_account_1"] // target is a locked account targetAccount := suite.testAccounts["local_account_2"] stream, errWithCode := suite.processor.OpenStreamForAccount(context.Background(), targetAccount, "user") suite.NoError(errWithCode) // put the follow request in the database as though it had passed through the federating db already satanFollowRequestTurtle := >smodel.FollowRequest{ ID: "01FGRYAVAWWPP926J175QGM0WV", CreatedAt: time.Now(), UpdatedAt: time.Now(), AccountID: originAccount.ID, Account: originAccount, TargetAccountID: targetAccount.ID, TargetAccount: targetAccount, ShowReblogs: true, URI: fmt.Sprintf("%s/follows/01FGRYAVAWWPP926J175QGM0WV", originAccount.URI), Notify: false, } err := suite.db.Put(ctx, satanFollowRequestTurtle) suite.NoError(err) err = suite.processor.ProcessFromFederator(ctx, messages.FromFederator{ APObjectType: ap.ActivityFollow, APActivityType: ap.ActivityCreate, GTSModel: satanFollowRequestTurtle, ReceivingAccount: targetAccount, }) suite.NoError(err) // a notification should be streamed msg := <-stream.Messages suite.Equal("notification", msg.Event) suite.NotEmpty(msg.Payload) suite.EqualValues([]string{"user"}, msg.Stream) notif := &model.Notification{} err = json.Unmarshal([]byte(msg.Payload), notif) suite.NoError(err) suite.Equal("follow_request", notif.Type) suite.Equal(originAccount.ID, notif.Account.ID) // no messages should have been sent out, since we didn't need to federate an accept suite.Empty(suite.sentHTTPRequests) } func (suite *FromFederatorTestSuite) TestProcessFollowRequestUnlocked() { ctx := context.Background() originAccount := suite.testAccounts["remote_account_1"] // target is an unlocked account targetAccount := suite.testAccounts["local_account_1"] stream, errWithCode := suite.processor.OpenStreamForAccount(context.Background(), targetAccount, "user") suite.NoError(errWithCode) // put the follow request in the database as though it had passed through the federating db already satanFollowRequestTurtle := >smodel.FollowRequest{ ID: "01FGRYAVAWWPP926J175QGM0WV", CreatedAt: time.Now(), UpdatedAt: time.Now(), AccountID: originAccount.ID, Account: originAccount, TargetAccountID: targetAccount.ID, TargetAccount: targetAccount, ShowReblogs: true, URI: fmt.Sprintf("%s/follows/01FGRYAVAWWPP926J175QGM0WV", originAccount.URI), Notify: false, } err := suite.db.Put(ctx, satanFollowRequestTurtle) suite.NoError(err) err = suite.processor.ProcessFromFederator(ctx, messages.FromFederator{ APObjectType: ap.ActivityFollow, APActivityType: ap.ActivityCreate, GTSModel: satanFollowRequestTurtle, ReceivingAccount: targetAccount, }) suite.NoError(err) // a notification should be streamed msg := <-stream.Messages suite.Equal("notification", msg.Event) suite.NotEmpty(msg.Payload) suite.EqualValues([]string{"user"}, msg.Stream) notif := &model.Notification{} err = json.Unmarshal([]byte(msg.Payload), notif) suite.NoError(err) suite.Equal("follow", notif.Type) suite.Equal(originAccount.ID, notif.Account.ID) // an accept message should be sent to satan's inbox suite.Len(suite.sentHTTPRequests, 1) acceptBytes := suite.sentHTTPRequests[originAccount.InboxURI] accept := &struct { Actor string `json:"actor"` ID string `json:"id"` Object struct { Actor string `json:"actor"` ID string `json:"id"` Object string `json:"object"` To string `json:"to"` Type string `json:"type"` } To string `json:"to"` Type string `json:"type"` }{} err = json.Unmarshal(acceptBytes, accept) suite.NoError(err) suite.Equal(targetAccount.URI, accept.Actor) suite.Equal(originAccount.URI, accept.Object.Actor) suite.Equal(satanFollowRequestTurtle.URI, accept.Object.ID) suite.Equal(targetAccount.URI, accept.Object.Object) suite.Equal(targetAccount.URI, accept.Object.To) suite.Equal("Follow", accept.Object.Type) suite.Equal(originAccount.URI, accept.To) suite.Equal("Accept", accept.Type) } // TestCreateStatusFromIRI checks if a forwarded status can be dereferenced by the processor. func (suite *FromFederatorTestSuite) TestCreateStatusFromIRI() { ctx := context.Background() receivingAccount := suite.testAccounts["local_account_1"] statusCreator := suite.testAccounts["remote_account_2"] err := suite.processor.ProcessFromFederator(ctx, messages.FromFederator{ APObjectType: ap.ObjectNote, APActivityType: ap.ActivityCreate, GTSModel: nil, // gtsmodel is nil because this is a forwarded status -- we want to dereference it using the iri ReceivingAccount: receivingAccount, APIri: testrig.URLMustParse("http://example.org/users/some_user/statuses/afaba698-5740-4e32-a702-af61aa543bc1"), }) suite.NoError(err) // status should now be in the database, attributed to remote_account_2 s, err := suite.db.GetStatusByURI(context.Background(), "http://example.org/users/some_user/statuses/afaba698-5740-4e32-a702-af61aa543bc1") suite.NoError(err) suite.Equal(statusCreator.URI, s.AccountURI) } func TestFromFederatorTestSuite(t *testing.T) { suite.Run(t, &FromFederatorTestSuite{}) }