2023-03-12 15:00:57 +00:00
|
|
|
// GoToSocial
|
|
|
|
// Copyright (C) GoToSocial Authors admin@gotosocial.org
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
//
|
|
|
|
// 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 <http://www.gnu.org/licenses/>.
|
2021-11-22 18:03:21 +00:00
|
|
|
|
|
|
|
package processing_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2023-05-25 09:37:38 +01:00
|
|
|
"errors"
|
2021-11-22 18:03:21 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/ap"
|
2023-01-02 12:10:50 +00:00
|
|
|
apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
|
2022-05-18 22:13:03 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/db"
|
2021-11-22 18:03:21 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/messages"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/stream"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/testrig"
|
|
|
|
)
|
|
|
|
|
|
|
|
type FromClientAPITestSuite struct {
|
|
|
|
ProcessingStandardTestSuite
|
|
|
|
}
|
|
|
|
|
2023-05-25 09:37:38 +01:00
|
|
|
// This test ensures that when admin_account posts a new
|
|
|
|
// status, it ends up in the correct streaming timelines
|
|
|
|
// of local_account_1, which follows it.
|
2021-11-22 18:03:21 +00:00
|
|
|
func (suite *FromClientAPITestSuite) TestProcessStreamNewStatus() {
|
2023-05-25 09:37:38 +01:00
|
|
|
var (
|
|
|
|
ctx = context.Background()
|
|
|
|
postingAccount = suite.testAccounts["admin_account"]
|
|
|
|
receivingAccount = suite.testAccounts["local_account_1"]
|
|
|
|
testList = suite.testLists["local_account_1_list_1"]
|
|
|
|
streams = suite.openStreams(ctx, receivingAccount, []string{testList.ID})
|
|
|
|
homeStream = streams[stream.TimelineHome]
|
|
|
|
listStream = streams[stream.TimelineList+":"+testList.ID]
|
|
|
|
)
|
2021-11-22 18:03:21 +00:00
|
|
|
|
2023-05-25 09:37:38 +01:00
|
|
|
// Make a new status from admin account.
|
2021-11-22 18:03:21 +00:00
|
|
|
newStatus := >smodel.Status{
|
|
|
|
ID: "01FN4B2F88TF9676DYNXWE1WSS",
|
|
|
|
URI: "http://localhost:8080/users/admin/statuses/01FN4B2F88TF9676DYNXWE1WSS",
|
|
|
|
URL: "http://localhost:8080/@admin/statuses/01FN4B2F88TF9676DYNXWE1WSS",
|
|
|
|
Content: "this status should stream :)",
|
|
|
|
AttachmentIDs: []string{},
|
|
|
|
TagIDs: []string{},
|
|
|
|
MentionIDs: []string{},
|
|
|
|
EmojiIDs: []string{},
|
|
|
|
CreatedAt: testrig.TimeMustParse("2021-10-20T11:36:45Z"),
|
|
|
|
UpdatedAt: testrig.TimeMustParse("2021-10-20T11:36:45Z"),
|
2022-08-15 11:35:05 +01:00
|
|
|
Local: testrig.TrueBool(),
|
2021-11-22 18:03:21 +00:00
|
|
|
AccountURI: "http://localhost:8080/users/admin",
|
|
|
|
AccountID: "01F8MH17FWEB39HZJ76B6VXSKF",
|
|
|
|
InReplyToID: "",
|
|
|
|
BoostOfID: "",
|
|
|
|
ContentWarning: "",
|
|
|
|
Visibility: gtsmodel.VisibilityFollowersOnly,
|
2022-08-15 11:35:05 +01:00
|
|
|
Sensitive: testrig.FalseBool(),
|
2021-11-22 18:03:21 +00:00
|
|
|
Language: "en",
|
|
|
|
CreatedWithApplicationID: "01F8MGXQRHYF5QPMTMXP78QC2F",
|
2022-08-15 11:35:05 +01:00
|
|
|
Federated: testrig.FalseBool(),
|
|
|
|
Boostable: testrig.TrueBool(),
|
|
|
|
Replyable: testrig.TrueBool(),
|
|
|
|
Likeable: testrig.TrueBool(),
|
2021-11-22 18:03:21 +00:00
|
|
|
ActivityStreamsType: ap.ObjectNote,
|
|
|
|
}
|
|
|
|
|
2023-05-25 09:37:38 +01:00
|
|
|
// Put the status in the db first, to mimic what
|
|
|
|
// would have already happened earlier up the flow.
|
|
|
|
if err := suite.db.PutStatus(ctx, newStatus); err != nil {
|
|
|
|
suite.FailNow(err.Error())
|
|
|
|
}
|
2021-11-22 18:03:21 +00:00
|
|
|
|
2023-05-25 09:37:38 +01:00
|
|
|
// Process the new status.
|
|
|
|
if err := suite.processor.ProcessFromClientAPI(ctx, messages.FromClientAPI{
|
2021-11-22 18:03:21 +00:00
|
|
|
APObjectType: ap.ObjectNote,
|
|
|
|
APActivityType: ap.ActivityCreate,
|
|
|
|
GTSModel: newStatus,
|
|
|
|
OriginAccount: postingAccount,
|
2023-05-25 09:37:38 +01:00
|
|
|
}); err != nil {
|
|
|
|
suite.FailNow(err.Error())
|
|
|
|
}
|
2021-11-22 18:03:21 +00:00
|
|
|
|
2023-05-25 09:37:38 +01:00
|
|
|
// Check message in home stream.
|
|
|
|
homeMsg := <-homeStream.Messages
|
|
|
|
suite.Equal(stream.EventTypeUpdate, homeMsg.Event)
|
|
|
|
suite.EqualValues([]string{stream.TimelineHome}, homeMsg.Stream)
|
|
|
|
suite.Empty(homeStream.Messages) // Stream should now be empty.
|
2021-11-22 18:03:21 +00:00
|
|
|
|
2023-05-25 09:37:38 +01:00
|
|
|
// Check status from home stream.
|
|
|
|
homeStreamStatus := &apimodel.Status{}
|
|
|
|
if err := json.Unmarshal([]byte(homeMsg.Payload), homeStreamStatus); err != nil {
|
|
|
|
suite.FailNow(err.Error())
|
|
|
|
}
|
|
|
|
suite.Equal(newStatus.ID, homeStreamStatus.ID)
|
|
|
|
suite.Equal(newStatus.Content, homeStreamStatus.Content)
|
|
|
|
|
|
|
|
// Check message in list stream.
|
|
|
|
listMsg := <-listStream.Messages
|
|
|
|
suite.Equal(stream.EventTypeUpdate, listMsg.Event)
|
|
|
|
suite.EqualValues([]string{stream.TimelineList + ":" + testList.ID}, listMsg.Stream)
|
|
|
|
suite.Empty(listStream.Messages) // Stream should now be empty.
|
2021-11-22 18:03:21 +00:00
|
|
|
|
2023-05-25 09:37:38 +01:00
|
|
|
// Check status from list stream.
|
|
|
|
listStreamStatus := &apimodel.Status{}
|
|
|
|
if err := json.Unmarshal([]byte(listMsg.Payload), listStreamStatus); err != nil {
|
|
|
|
suite.FailNow(err.Error())
|
|
|
|
}
|
|
|
|
suite.Equal(newStatus.ID, listStreamStatus.ID)
|
|
|
|
suite.Equal(newStatus.Content, listStreamStatus.Content)
|
2021-11-22 18:03:21 +00:00
|
|
|
}
|
|
|
|
|
2022-05-18 22:13:03 +01:00
|
|
|
func (suite *FromClientAPITestSuite) TestProcessStatusDelete() {
|
2023-05-25 09:37:38 +01:00
|
|
|
var (
|
|
|
|
ctx = context.Background()
|
|
|
|
deletingAccount = suite.testAccounts["local_account_1"]
|
|
|
|
receivingAccount = suite.testAccounts["local_account_2"]
|
|
|
|
deletedStatus = suite.testStatuses["local_account_1_status_1"]
|
|
|
|
boostOfDeletedStatus = suite.testStatuses["admin_account_status_4"]
|
|
|
|
streams = suite.openStreams(ctx, receivingAccount, nil)
|
|
|
|
homeStream = streams[stream.TimelineHome]
|
|
|
|
)
|
2022-05-18 22:13:03 +01:00
|
|
|
|
2023-05-25 09:37:38 +01:00
|
|
|
// Delete the status from the db first, to mimic what
|
|
|
|
// would have already happened earlier up the flow
|
|
|
|
if err := suite.db.DeleteStatusByID(ctx, deletedStatus.ID); err != nil {
|
|
|
|
suite.FailNow(err.Error())
|
|
|
|
}
|
2022-05-18 22:13:03 +01:00
|
|
|
|
2023-05-25 09:37:38 +01:00
|
|
|
// Process the status delete.
|
|
|
|
if err := suite.processor.ProcessFromClientAPI(ctx, messages.FromClientAPI{
|
2022-05-18 22:13:03 +01:00
|
|
|
APObjectType: ap.ObjectNote,
|
|
|
|
APActivityType: ap.ActivityDelete,
|
|
|
|
GTSModel: deletedStatus,
|
|
|
|
OriginAccount: deletingAccount,
|
2023-05-25 09:37:38 +01:00
|
|
|
}); err != nil {
|
|
|
|
suite.FailNow(err.Error())
|
|
|
|
}
|
2022-05-18 22:13:03 +01:00
|
|
|
|
2023-05-25 09:37:38 +01:00
|
|
|
// Stream should have the delete of admin's boost in it now.
|
|
|
|
msg := <-homeStream.Messages
|
2022-05-18 22:13:03 +01:00
|
|
|
suite.Equal(stream.EventTypeDelete, msg.Event)
|
|
|
|
suite.Equal(boostOfDeletedStatus.ID, msg.Payload)
|
|
|
|
suite.EqualValues([]string{stream.TimelineHome}, msg.Stream)
|
|
|
|
|
2023-05-25 09:37:38 +01:00
|
|
|
// Stream should also have the delete of the message itself in it.
|
|
|
|
msg = <-homeStream.Messages
|
2022-05-18 22:13:03 +01:00
|
|
|
suite.Equal(stream.EventTypeDelete, msg.Event)
|
|
|
|
suite.Equal(deletedStatus.ID, msg.Payload)
|
|
|
|
suite.EqualValues([]string{stream.TimelineHome}, msg.Stream)
|
|
|
|
|
2023-05-25 09:37:38 +01:00
|
|
|
// Stream should now be empty.
|
|
|
|
suite.Empty(homeStream.Messages)
|
2022-05-18 22:13:03 +01:00
|
|
|
|
2023-05-25 09:37:38 +01:00
|
|
|
// Boost should no longer be in the database.
|
|
|
|
if !testrig.WaitFor(func() bool {
|
|
|
|
_, err := suite.db.GetStatusByID(ctx, boostOfDeletedStatus.ID)
|
|
|
|
return errors.Is(err, db.ErrNoEntries)
|
|
|
|
}) {
|
|
|
|
suite.FailNow("timed out waiting for status delete")
|
|
|
|
}
|
2022-05-18 22:13:03 +01:00
|
|
|
}
|
|
|
|
|
2023-04-10 20:56:02 +01:00
|
|
|
func (suite *FromClientAPITestSuite) TestProcessNewStatusWithNotification() {
|
2023-05-25 09:37:38 +01:00
|
|
|
var (
|
|
|
|
ctx = context.Background()
|
|
|
|
postingAccount = suite.testAccounts["admin_account"]
|
|
|
|
receivingAccount = suite.testAccounts["local_account_1"]
|
|
|
|
streams = suite.openStreams(ctx, receivingAccount, nil)
|
|
|
|
notifStream = streams[stream.TimelineNotifications]
|
|
|
|
)
|
2023-04-10 20:56:02 +01:00
|
|
|
|
|
|
|
// Update the follow from receiving account -> posting account so
|
|
|
|
// that receiving account wants notifs when posting account posts.
|
|
|
|
follow := >smodel.Follow{}
|
|
|
|
*follow = *suite.testFollows["local_account_1_admin_account"]
|
|
|
|
follow.Notify = testrig.TrueBool()
|
|
|
|
if err := suite.db.UpdateFollow(ctx, follow); err != nil {
|
|
|
|
suite.FailNow(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make a new status from admin account.
|
|
|
|
newStatus := >smodel.Status{
|
|
|
|
ID: "01FN4B2F88TF9676DYNXWE1WSS",
|
|
|
|
URI: "http://localhost:8080/users/admin/statuses/01FN4B2F88TF9676DYNXWE1WSS",
|
|
|
|
URL: "http://localhost:8080/@admin/statuses/01FN4B2F88TF9676DYNXWE1WSS",
|
|
|
|
Content: "this status should create a notification",
|
|
|
|
AttachmentIDs: []string{},
|
|
|
|
TagIDs: []string{},
|
|
|
|
MentionIDs: []string{},
|
|
|
|
EmojiIDs: []string{},
|
|
|
|
CreatedAt: testrig.TimeMustParse("2021-10-20T11:36:45Z"),
|
|
|
|
UpdatedAt: testrig.TimeMustParse("2021-10-20T11:36:45Z"),
|
|
|
|
Local: testrig.TrueBool(),
|
|
|
|
AccountURI: "http://localhost:8080/users/admin",
|
|
|
|
AccountID: "01F8MH17FWEB39HZJ76B6VXSKF",
|
|
|
|
InReplyToID: "",
|
|
|
|
BoostOfID: "",
|
|
|
|
ContentWarning: "",
|
|
|
|
Visibility: gtsmodel.VisibilityFollowersOnly,
|
|
|
|
Sensitive: testrig.FalseBool(),
|
|
|
|
Language: "en",
|
|
|
|
CreatedWithApplicationID: "01F8MGXQRHYF5QPMTMXP78QC2F",
|
|
|
|
Federated: testrig.FalseBool(),
|
|
|
|
Boostable: testrig.TrueBool(),
|
|
|
|
Replyable: testrig.TrueBool(),
|
|
|
|
Likeable: testrig.TrueBool(),
|
|
|
|
ActivityStreamsType: ap.ObjectNote,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put the status in the db first, to mimic what
|
|
|
|
// would have already happened earlier up the flow.
|
2023-05-25 09:37:38 +01:00
|
|
|
if err := suite.db.PutStatus(ctx, newStatus); err != nil {
|
|
|
|
suite.FailNow(err.Error())
|
|
|
|
}
|
2023-04-10 20:56:02 +01:00
|
|
|
|
|
|
|
// Process the new status.
|
|
|
|
if err := suite.processor.ProcessFromClientAPI(ctx, messages.FromClientAPI{
|
|
|
|
APObjectType: ap.ObjectNote,
|
|
|
|
APActivityType: ap.ActivityCreate,
|
|
|
|
GTSModel: newStatus,
|
|
|
|
OriginAccount: postingAccount,
|
|
|
|
}); err != nil {
|
|
|
|
suite.FailNow(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for a notification to appear for the status.
|
|
|
|
if !testrig.WaitFor(func() bool {
|
|
|
|
_, err := suite.db.GetNotification(
|
|
|
|
ctx,
|
|
|
|
gtsmodel.NotificationStatus,
|
|
|
|
receivingAccount.ID,
|
|
|
|
postingAccount.ID,
|
|
|
|
newStatus.ID,
|
|
|
|
)
|
|
|
|
return err == nil
|
|
|
|
}) {
|
|
|
|
suite.FailNow("timed out waiting for new status notification")
|
|
|
|
}
|
2023-05-25 09:37:38 +01:00
|
|
|
|
|
|
|
// Check message in notification stream.
|
|
|
|
notifMsg := <-notifStream.Messages
|
|
|
|
suite.Equal(stream.EventTypeNotification, notifMsg.Event)
|
|
|
|
suite.EqualValues([]string{stream.TimelineNotifications}, notifMsg.Stream)
|
|
|
|
suite.Empty(notifStream.Messages) // Stream should now be empty.
|
|
|
|
|
|
|
|
// Check notif.
|
|
|
|
notif := &apimodel.Notification{}
|
|
|
|
if err := json.Unmarshal([]byte(notifMsg.Payload), notif); err != nil {
|
|
|
|
suite.FailNow(err.Error())
|
|
|
|
}
|
|
|
|
suite.Equal(newStatus.ID, notif.Status.ID)
|
2023-04-10 20:56:02 +01:00
|
|
|
}
|
|
|
|
|
2021-11-22 18:03:21 +00:00
|
|
|
func TestFromClientAPITestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, &FromClientAPITestSuite{})
|
|
|
|
}
|