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-08-20 11:26:56 +01:00
|
|
|
|
2021-08-25 14:34:33 +01:00
|
|
|
package bundb_test
|
2021-08-20 11:26:56 +01:00
|
|
|
|
|
|
|
import (
|
2021-08-25 14:34:33 +01:00
|
|
|
"context"
|
2021-09-28 14:21:59 +01:00
|
|
|
"crypto/rand"
|
|
|
|
"crypto/rsa"
|
[performance] refactoring + add fave / follow / request / visibility caching (#1607)
* refactor visibility checking, add caching for visibility
* invalidate visibility cache items on account / status deletes
* fix requester ID passed to visibility cache nil ptr
* de-interface caches, fix home / public timeline caching + visibility
* finish adding code comments for visibility filter
* fix angry goconst linter warnings
* actually finish adding filter visibility code comments for timeline functions
* move home timeline status author check to after visibility
* remove now-unused code
* add more code comments
* add TODO code comment, update printed cache start names
* update printed cache names on stop
* start adding separate follow(request) delete db functions, add specific visibility cache tests
* add relationship type caching
* fix getting local account follows / followed-bys, other small codebase improvements
* simplify invalidation using cache hooks, add more GetAccountBy___() functions
* fix boosting to return 404 if not boostable but no error (to not leak status ID)
* remove dead code
* improved placement of cache invalidation
* update license headers
* add example follow, follow-request config entries
* add example visibility cache configuration to config file
* use specific PutFollowRequest() instead of just Put()
* add tests for all GetAccountBy()
* add GetBlockBy() tests
* update block to check primitive fields
* update and finish adding Get{Account,Block,Follow,FollowRequest}By() tests
* fix copy-pasted code
* update envparsing test
* whitespace
* fix bun struct tag
* add license header to gtscontext
* fix old license header
* improved error creation to not use fmt.Errorf() when not needed
* fix various rebase conflicts, fix account test
* remove commented-out code, fix-up mention caching
* fix mention select bun statement
* ensure mention target account populated, pass in context to customrenderer logging
* remove more uncommented code, fix typeutil test
* add statusfave database model caching
* add status fave cache configuration
* add status fave cache example config
* woops, catch missed error. nice catch linter!
* add back testrig panic on nil db
* update example configuration to match defaults, slight tweak to cache configuration defaults
* update envparsing test with new defaults
* fetch followingget to use the follow target account
* use accounnt.IsLocal() instead of empty domain check
* use constants for the cache visibility type check
* use bun.In() for notification type restriction in db query
* include replies when fetching PublicTimeline() (to account for single-author threads in Visibility{}.StatusPublicTimelineable())
* use bun query building for nested select statements to ensure working with postgres
* update public timeline future status checks to match visibility filter
* same as previous, for home timeline
* update public timeline tests to dynamically check for appropriate statuses
* migrate accounts to allow unique constraint on public_key
* provide minimal account with publicKey
---------
Signed-off-by: kim <grufwub@gmail.com>
Co-authored-by: tsmethurst <tobi.smethurst@protonmail.com>
2023-03-28 14:03:14 +01:00
|
|
|
"errors"
|
2024-02-27 17:18:40 +00:00
|
|
|
"fmt"
|
[performance] refactoring + add fave / follow / request / visibility caching (#1607)
* refactor visibility checking, add caching for visibility
* invalidate visibility cache items on account / status deletes
* fix requester ID passed to visibility cache nil ptr
* de-interface caches, fix home / public timeline caching + visibility
* finish adding code comments for visibility filter
* fix angry goconst linter warnings
* actually finish adding filter visibility code comments for timeline functions
* move home timeline status author check to after visibility
* remove now-unused code
* add more code comments
* add TODO code comment, update printed cache start names
* update printed cache names on stop
* start adding separate follow(request) delete db functions, add specific visibility cache tests
* add relationship type caching
* fix getting local account follows / followed-bys, other small codebase improvements
* simplify invalidation using cache hooks, add more GetAccountBy___() functions
* fix boosting to return 404 if not boostable but no error (to not leak status ID)
* remove dead code
* improved placement of cache invalidation
* update license headers
* add example follow, follow-request config entries
* add example visibility cache configuration to config file
* use specific PutFollowRequest() instead of just Put()
* add tests for all GetAccountBy()
* add GetBlockBy() tests
* update block to check primitive fields
* update and finish adding Get{Account,Block,Follow,FollowRequest}By() tests
* fix copy-pasted code
* update envparsing test
* whitespace
* fix bun struct tag
* add license header to gtscontext
* fix old license header
* improved error creation to not use fmt.Errorf() when not needed
* fix various rebase conflicts, fix account test
* remove commented-out code, fix-up mention caching
* fix mention select bun statement
* ensure mention target account populated, pass in context to customrenderer logging
* remove more uncommented code, fix typeutil test
* add statusfave database model caching
* add status fave cache configuration
* add status fave cache example config
* woops, catch missed error. nice catch linter!
* add back testrig panic on nil db
* update example configuration to match defaults, slight tweak to cache configuration defaults
* update envparsing test with new defaults
* fetch followingget to use the follow target account
* use accounnt.IsLocal() instead of empty domain check
* use constants for the cache visibility type check
* use bun.In() for notification type restriction in db query
* include replies when fetching PublicTimeline() (to account for single-author threads in Visibility{}.StatusPublicTimelineable())
* use bun query building for nested select statements to ensure working with postgres
* update public timeline future status checks to match visibility filter
* same as previous, for home timeline
* update public timeline tests to dynamically check for appropriate statuses
* migrate accounts to allow unique constraint on public_key
* provide minimal account with publicKey
---------
Signed-off-by: kim <grufwub@gmail.com>
Co-authored-by: tsmethurst <tobi.smethurst@protonmail.com>
2023-03-28 14:03:14 +01:00
|
|
|
"reflect"
|
2022-12-01 15:06:09 +00:00
|
|
|
"strings"
|
2021-08-20 11:26:56 +01:00
|
|
|
"testing"
|
2021-08-25 14:34:33 +01:00
|
|
|
"time"
|
2021-08-20 11:26:56 +01:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/suite"
|
2021-09-28 14:21:59 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/ap"
|
2023-02-25 12:16:30 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/db"
|
2022-09-26 10:56:01 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/db/bundb"
|
2021-09-28 14:21:59 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
2024-02-27 17:18:40 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/util"
|
2022-09-26 10:56:01 +01:00
|
|
|
"github.com/uptrace/bun"
|
2021-08-20 11:26:56 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type AccountTestSuite struct {
|
2021-08-25 14:34:33 +01:00
|
|
|
BunDBStandardTestSuite
|
2021-08-20 11:26:56 +01:00
|
|
|
}
|
|
|
|
|
2022-05-23 16:40:03 +01:00
|
|
|
func (suite *AccountTestSuite) TestGetAccountStatuses() {
|
2023-02-25 12:16:30 +00:00
|
|
|
statuses, err := suite.db.GetAccountStatuses(context.Background(), suite.testAccounts["local_account_1"].ID, 20, false, false, "", "", false, false)
|
2022-05-23 16:40:03 +01:00
|
|
|
suite.NoError(err)
|
2023-12-10 11:36:00 +00:00
|
|
|
suite.Len(statuses, 7)
|
2022-05-23 16:40:03 +01:00
|
|
|
}
|
|
|
|
|
2023-05-22 15:32:36 +01:00
|
|
|
func (suite *AccountTestSuite) TestGetAccountStatusesPageDown() {
|
|
|
|
// get the first page
|
2023-12-10 11:36:00 +00:00
|
|
|
statuses, err := suite.db.GetAccountStatuses(context.Background(), suite.testAccounts["local_account_1"].ID, 3, false, false, "", "", false, false)
|
2023-05-22 15:32:36 +01:00
|
|
|
if err != nil {
|
|
|
|
suite.FailNow(err.Error())
|
|
|
|
}
|
2023-12-10 11:36:00 +00:00
|
|
|
suite.Len(statuses, 3)
|
2023-05-22 15:32:36 +01:00
|
|
|
|
|
|
|
// get the second page
|
2023-12-10 11:36:00 +00:00
|
|
|
statuses, err = suite.db.GetAccountStatuses(context.Background(), suite.testAccounts["local_account_1"].ID, 3, false, false, statuses[len(statuses)-1].ID, "", false, false)
|
2023-05-22 15:32:36 +01:00
|
|
|
if err != nil {
|
|
|
|
suite.FailNow(err.Error())
|
|
|
|
}
|
2023-12-10 11:36:00 +00:00
|
|
|
suite.Len(statuses, 3)
|
2023-05-22 15:32:36 +01:00
|
|
|
|
|
|
|
// get the third page
|
2023-12-10 11:36:00 +00:00
|
|
|
statuses, err = suite.db.GetAccountStatuses(context.Background(), suite.testAccounts["local_account_1"].ID, 3, false, false, statuses[len(statuses)-1].ID, "", false, false)
|
2023-05-22 15:32:36 +01:00
|
|
|
if err != nil {
|
|
|
|
suite.FailNow(err.Error())
|
|
|
|
}
|
2023-12-10 11:36:00 +00:00
|
|
|
suite.Len(statuses, 1)
|
2023-05-22 15:32:36 +01:00
|
|
|
|
|
|
|
// try to get the last page (should be empty)
|
2023-12-10 11:36:00 +00:00
|
|
|
statuses, err = suite.db.GetAccountStatuses(context.Background(), suite.testAccounts["local_account_1"].ID, 3, false, false, statuses[len(statuses)-1].ID, "", false, false)
|
2023-05-22 15:32:36 +01:00
|
|
|
suite.ErrorIs(err, db.ErrNoEntries)
|
|
|
|
suite.Empty(statuses)
|
|
|
|
}
|
|
|
|
|
2022-10-08 12:50:48 +01:00
|
|
|
func (suite *AccountTestSuite) TestGetAccountStatusesExcludeRepliesAndReblogs() {
|
2023-02-25 12:16:30 +00:00
|
|
|
statuses, err := suite.db.GetAccountStatuses(context.Background(), suite.testAccounts["local_account_1"].ID, 20, true, true, "", "", false, false)
|
2022-10-08 12:50:48 +01:00
|
|
|
suite.NoError(err)
|
2023-12-10 11:36:00 +00:00
|
|
|
suite.Len(statuses, 7)
|
2022-10-08 12:50:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *AccountTestSuite) TestGetAccountStatusesExcludeRepliesAndReblogsPublicOnly() {
|
2023-02-25 12:16:30 +00:00
|
|
|
statuses, err := suite.db.GetAccountStatuses(context.Background(), suite.testAccounts["local_account_1"].ID, 20, true, true, "", "", false, true)
|
2022-10-08 12:50:48 +01:00
|
|
|
suite.NoError(err)
|
2023-12-10 11:36:00 +00:00
|
|
|
suite.Len(statuses, 2)
|
2022-10-08 12:50:48 +01:00
|
|
|
}
|
|
|
|
|
2024-02-27 17:18:40 +00:00
|
|
|
// populateTestStatus adds mandatory fields to a partially populated status.
|
|
|
|
func (suite *AccountTestSuite) populateTestStatus(testAccountKey string, status *gtsmodel.Status, inReplyTo *gtsmodel.Status) *gtsmodel.Status {
|
|
|
|
testAccount := suite.testAccounts[testAccountKey]
|
|
|
|
if testAccount == nil {
|
|
|
|
suite.FailNowf("", "Missing test account: %s", testAccountKey)
|
|
|
|
return status
|
|
|
|
}
|
|
|
|
if testAccount.Domain != "" {
|
|
|
|
suite.FailNowf("", "Only local test accounts are supported: %s is remote", testAccountKey)
|
|
|
|
return status
|
|
|
|
}
|
|
|
|
|
|
|
|
status.AccountID = testAccount.ID
|
|
|
|
status.AccountURI = testAccount.URI
|
|
|
|
status.URI = fmt.Sprintf("http://localhost:8080/users/%s/statuses/%s", testAccount.Username, status.ID)
|
|
|
|
status.Local = util.Ptr(true)
|
|
|
|
|
|
|
|
if status.Visibility == "" {
|
|
|
|
status.Visibility = gtsmodel.VisibilityDefault
|
|
|
|
}
|
|
|
|
if status.ActivityStreamsType == "" {
|
|
|
|
status.ActivityStreamsType = ap.ObjectNote
|
|
|
|
}
|
|
|
|
if status.Federated == nil {
|
|
|
|
status.Federated = util.Ptr(true)
|
|
|
|
}
|
|
|
|
if status.Boostable == nil {
|
|
|
|
status.Boostable = util.Ptr(true)
|
|
|
|
}
|
|
|
|
if status.Likeable == nil {
|
|
|
|
status.Likeable = util.Ptr(true)
|
|
|
|
}
|
|
|
|
if status.Replyable == nil {
|
|
|
|
status.Replyable = util.Ptr(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
if inReplyTo != nil {
|
|
|
|
status.InReplyToAccountID = inReplyTo.AccountID
|
|
|
|
status.InReplyToID = inReplyTo.ID
|
|
|
|
status.InReplyToURI = inReplyTo.URI
|
|
|
|
}
|
|
|
|
|
|
|
|
return status
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that we're including self-replies but excluding those that mention other accounts.
|
|
|
|
func (suite *AccountTestSuite) TestGetAccountStatusesExcludeRepliesExcludesSelfRepliesWithMentions() {
|
|
|
|
post := suite.populateTestStatus(
|
|
|
|
"local_account_1",
|
|
|
|
>smodel.Status{
|
|
|
|
ID: "01HQ1FGN679M5F81DZ18WS6JQG",
|
|
|
|
Content: "post",
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
reply := suite.populateTestStatus(
|
|
|
|
"local_account_2",
|
|
|
|
>smodel.Status{
|
|
|
|
ID: "01HQ1GTXMT2W6PF8MA2XG9DG6Q",
|
|
|
|
Content: "post",
|
|
|
|
MentionIDs: []string{post.InReplyToAccountID},
|
|
|
|
},
|
|
|
|
post,
|
|
|
|
)
|
|
|
|
riposte := suite.populateTestStatus(
|
|
|
|
"local_account_1",
|
|
|
|
>smodel.Status{
|
|
|
|
ID: "01HQ1GTXN0RWG9ZWJKRFAEF5RE",
|
|
|
|
Content: "riposte",
|
|
|
|
MentionIDs: []string{reply.InReplyToAccountID},
|
|
|
|
},
|
|
|
|
reply,
|
|
|
|
)
|
|
|
|
followup := suite.populateTestStatus(
|
|
|
|
"local_account_1",
|
|
|
|
>smodel.Status{
|
|
|
|
ID: "01HQ1GTXN52X7MM9Z12PNJWEHQ",
|
|
|
|
Content: "followup",
|
|
|
|
MentionIDs: []string{reply.InReplyToAccountID},
|
|
|
|
},
|
|
|
|
riposte,
|
|
|
|
)
|
|
|
|
|
|
|
|
for _, status := range []*gtsmodel.Status{post, reply, riposte, followup} {
|
|
|
|
if err := suite.db.PutStatus(context.Background(), status); err != nil {
|
|
|
|
suite.FailNowf("", "Error while adding test status with ID %s: %v", status.ID, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
testAccount := suite.testAccounts["local_account_1"]
|
|
|
|
statuses, err := suite.db.GetAccountStatuses(context.Background(), testAccount.ID, 20, true, true, "", "", false, false)
|
|
|
|
suite.NoError(err)
|
|
|
|
suite.Len(statuses, 8)
|
|
|
|
for _, status := range statuses {
|
|
|
|
if status.InReplyToID != "" && status.InReplyToAccountID != testAccount.ID {
|
|
|
|
suite.FailNowf("", "Status with ID %s is a non-self reply and should have been excluded", status.ID)
|
|
|
|
}
|
|
|
|
if len(status.Mentions) != 0 {
|
|
|
|
suite.FailNowf("", "Status with ID %s has mentions and should have been excluded", status.ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-10 09:56:49 +01:00
|
|
|
func (suite *AccountTestSuite) TestGetAccountStatusesMediaOnly() {
|
2023-02-25 12:16:30 +00:00
|
|
|
statuses, err := suite.db.GetAccountStatuses(context.Background(), suite.testAccounts["local_account_1"].ID, 20, false, false, "", "", true, false)
|
2022-06-10 09:56:49 +01:00
|
|
|
suite.NoError(err)
|
|
|
|
suite.Len(statuses, 1)
|
|
|
|
}
|
|
|
|
|
[performance] refactoring + add fave / follow / request / visibility caching (#1607)
* refactor visibility checking, add caching for visibility
* invalidate visibility cache items on account / status deletes
* fix requester ID passed to visibility cache nil ptr
* de-interface caches, fix home / public timeline caching + visibility
* finish adding code comments for visibility filter
* fix angry goconst linter warnings
* actually finish adding filter visibility code comments for timeline functions
* move home timeline status author check to after visibility
* remove now-unused code
* add more code comments
* add TODO code comment, update printed cache start names
* update printed cache names on stop
* start adding separate follow(request) delete db functions, add specific visibility cache tests
* add relationship type caching
* fix getting local account follows / followed-bys, other small codebase improvements
* simplify invalidation using cache hooks, add more GetAccountBy___() functions
* fix boosting to return 404 if not boostable but no error (to not leak status ID)
* remove dead code
* improved placement of cache invalidation
* update license headers
* add example follow, follow-request config entries
* add example visibility cache configuration to config file
* use specific PutFollowRequest() instead of just Put()
* add tests for all GetAccountBy()
* add GetBlockBy() tests
* update block to check primitive fields
* update and finish adding Get{Account,Block,Follow,FollowRequest}By() tests
* fix copy-pasted code
* update envparsing test
* whitespace
* fix bun struct tag
* add license header to gtscontext
* fix old license header
* improved error creation to not use fmt.Errorf() when not needed
* fix various rebase conflicts, fix account test
* remove commented-out code, fix-up mention caching
* fix mention select bun statement
* ensure mention target account populated, pass in context to customrenderer logging
* remove more uncommented code, fix typeutil test
* add statusfave database model caching
* add status fave cache configuration
* add status fave cache example config
* woops, catch missed error. nice catch linter!
* add back testrig panic on nil db
* update example configuration to match defaults, slight tweak to cache configuration defaults
* update envparsing test with new defaults
* fetch followingget to use the follow target account
* use accounnt.IsLocal() instead of empty domain check
* use constants for the cache visibility type check
* use bun.In() for notification type restriction in db query
* include replies when fetching PublicTimeline() (to account for single-author threads in Visibility{}.StatusPublicTimelineable())
* use bun query building for nested select statements to ensure working with postgres
* update public timeline future status checks to match visibility filter
* same as previous, for home timeline
* update public timeline tests to dynamically check for appropriate statuses
* migrate accounts to allow unique constraint on public_key
* provide minimal account with publicKey
---------
Signed-off-by: kim <grufwub@gmail.com>
Co-authored-by: tsmethurst <tobi.smethurst@protonmail.com>
2023-03-28 14:03:14 +01:00
|
|
|
func (suite *AccountTestSuite) TestGetAccountBy() {
|
|
|
|
t := suite.T()
|
|
|
|
|
|
|
|
// Create a new context for this test.
|
|
|
|
ctx, cncl := context.WithCancel(context.Background())
|
|
|
|
defer cncl()
|
|
|
|
|
|
|
|
// Sentinel error to mark avoiding a test case.
|
|
|
|
sentinelErr := errors.New("sentinel")
|
|
|
|
|
|
|
|
// isEqual checks if 2 account models are equal.
|
|
|
|
isEqual := func(a1, a2 gtsmodel.Account) bool {
|
|
|
|
// Clear populated sub-models.
|
|
|
|
a1.HeaderMediaAttachment = nil
|
|
|
|
a2.HeaderMediaAttachment = nil
|
|
|
|
a1.AvatarMediaAttachment = nil
|
|
|
|
a2.AvatarMediaAttachment = nil
|
|
|
|
a1.Emojis = nil
|
|
|
|
a2.Emojis = nil
|
2024-03-22 13:03:46 +00:00
|
|
|
a1.Settings = nil
|
|
|
|
a2.Settings = nil
|
[performance] refactoring + add fave / follow / request / visibility caching (#1607)
* refactor visibility checking, add caching for visibility
* invalidate visibility cache items on account / status deletes
* fix requester ID passed to visibility cache nil ptr
* de-interface caches, fix home / public timeline caching + visibility
* finish adding code comments for visibility filter
* fix angry goconst linter warnings
* actually finish adding filter visibility code comments for timeline functions
* move home timeline status author check to after visibility
* remove now-unused code
* add more code comments
* add TODO code comment, update printed cache start names
* update printed cache names on stop
* start adding separate follow(request) delete db functions, add specific visibility cache tests
* add relationship type caching
* fix getting local account follows / followed-bys, other small codebase improvements
* simplify invalidation using cache hooks, add more GetAccountBy___() functions
* fix boosting to return 404 if not boostable but no error (to not leak status ID)
* remove dead code
* improved placement of cache invalidation
* update license headers
* add example follow, follow-request config entries
* add example visibility cache configuration to config file
* use specific PutFollowRequest() instead of just Put()
* add tests for all GetAccountBy()
* add GetBlockBy() tests
* update block to check primitive fields
* update and finish adding Get{Account,Block,Follow,FollowRequest}By() tests
* fix copy-pasted code
* update envparsing test
* whitespace
* fix bun struct tag
* add license header to gtscontext
* fix old license header
* improved error creation to not use fmt.Errorf() when not needed
* fix various rebase conflicts, fix account test
* remove commented-out code, fix-up mention caching
* fix mention select bun statement
* ensure mention target account populated, pass in context to customrenderer logging
* remove more uncommented code, fix typeutil test
* add statusfave database model caching
* add status fave cache configuration
* add status fave cache example config
* woops, catch missed error. nice catch linter!
* add back testrig panic on nil db
* update example configuration to match defaults, slight tweak to cache configuration defaults
* update envparsing test with new defaults
* fetch followingget to use the follow target account
* use accounnt.IsLocal() instead of empty domain check
* use constants for the cache visibility type check
* use bun.In() for notification type restriction in db query
* include replies when fetching PublicTimeline() (to account for single-author threads in Visibility{}.StatusPublicTimelineable())
* use bun query building for nested select statements to ensure working with postgres
* update public timeline future status checks to match visibility filter
* same as previous, for home timeline
* update public timeline tests to dynamically check for appropriate statuses
* migrate accounts to allow unique constraint on public_key
* provide minimal account with publicKey
---------
Signed-off-by: kim <grufwub@gmail.com>
Co-authored-by: tsmethurst <tobi.smethurst@protonmail.com>
2023-03-28 14:03:14 +01:00
|
|
|
|
|
|
|
// Clear database-set fields.
|
|
|
|
a1.CreatedAt = time.Time{}
|
|
|
|
a2.CreatedAt = time.Time{}
|
|
|
|
a1.UpdatedAt = time.Time{}
|
|
|
|
a2.UpdatedAt = time.Time{}
|
|
|
|
|
|
|
|
// Manually compare keys.
|
|
|
|
pk1 := a1.PublicKey
|
|
|
|
pv1 := a1.PrivateKey
|
|
|
|
pk2 := a2.PublicKey
|
|
|
|
pv2 := a2.PrivateKey
|
|
|
|
a1.PublicKey = nil
|
|
|
|
a1.PrivateKey = nil
|
|
|
|
a2.PublicKey = nil
|
|
|
|
a2.PrivateKey = nil
|
|
|
|
|
|
|
|
return reflect.DeepEqual(a1, a2) &&
|
|
|
|
((pk1 == nil && pk2 == nil) || pk1.Equal(pk2)) &&
|
|
|
|
((pv1 == nil && pv2 == nil) || pv1.Equal(pv2))
|
2021-08-20 11:26:56 +01:00
|
|
|
}
|
2022-12-01 15:06:09 +00:00
|
|
|
|
[performance] refactoring + add fave / follow / request / visibility caching (#1607)
* refactor visibility checking, add caching for visibility
* invalidate visibility cache items on account / status deletes
* fix requester ID passed to visibility cache nil ptr
* de-interface caches, fix home / public timeline caching + visibility
* finish adding code comments for visibility filter
* fix angry goconst linter warnings
* actually finish adding filter visibility code comments for timeline functions
* move home timeline status author check to after visibility
* remove now-unused code
* add more code comments
* add TODO code comment, update printed cache start names
* update printed cache names on stop
* start adding separate follow(request) delete db functions, add specific visibility cache tests
* add relationship type caching
* fix getting local account follows / followed-bys, other small codebase improvements
* simplify invalidation using cache hooks, add more GetAccountBy___() functions
* fix boosting to return 404 if not boostable but no error (to not leak status ID)
* remove dead code
* improved placement of cache invalidation
* update license headers
* add example follow, follow-request config entries
* add example visibility cache configuration to config file
* use specific PutFollowRequest() instead of just Put()
* add tests for all GetAccountBy()
* add GetBlockBy() tests
* update block to check primitive fields
* update and finish adding Get{Account,Block,Follow,FollowRequest}By() tests
* fix copy-pasted code
* update envparsing test
* whitespace
* fix bun struct tag
* add license header to gtscontext
* fix old license header
* improved error creation to not use fmt.Errorf() when not needed
* fix various rebase conflicts, fix account test
* remove commented-out code, fix-up mention caching
* fix mention select bun statement
* ensure mention target account populated, pass in context to customrenderer logging
* remove more uncommented code, fix typeutil test
* add statusfave database model caching
* add status fave cache configuration
* add status fave cache example config
* woops, catch missed error. nice catch linter!
* add back testrig panic on nil db
* update example configuration to match defaults, slight tweak to cache configuration defaults
* update envparsing test with new defaults
* fetch followingget to use the follow target account
* use accounnt.IsLocal() instead of empty domain check
* use constants for the cache visibility type check
* use bun.In() for notification type restriction in db query
* include replies when fetching PublicTimeline() (to account for single-author threads in Visibility{}.StatusPublicTimelineable())
* use bun query building for nested select statements to ensure working with postgres
* update public timeline future status checks to match visibility filter
* same as previous, for home timeline
* update public timeline tests to dynamically check for appropriate statuses
* migrate accounts to allow unique constraint on public_key
* provide minimal account with publicKey
---------
Signed-off-by: kim <grufwub@gmail.com>
Co-authored-by: tsmethurst <tobi.smethurst@protonmail.com>
2023-03-28 14:03:14 +01:00
|
|
|
for _, account := range suite.testAccounts {
|
|
|
|
for lookup, dbfunc := range map[string]func() (*gtsmodel.Account, error){
|
|
|
|
"id": func() (*gtsmodel.Account, error) {
|
|
|
|
return suite.db.GetAccountByID(ctx, account.ID)
|
|
|
|
},
|
|
|
|
|
|
|
|
"uri": func() (*gtsmodel.Account, error) {
|
|
|
|
return suite.db.GetAccountByURI(ctx, account.URI)
|
|
|
|
},
|
|
|
|
|
|
|
|
"url": func() (*gtsmodel.Account, error) {
|
|
|
|
if account.URL == "" {
|
|
|
|
return nil, sentinelErr
|
|
|
|
}
|
|
|
|
return suite.db.GetAccountByURL(ctx, account.URL)
|
|
|
|
},
|
|
|
|
|
|
|
|
"username@domain": func() (*gtsmodel.Account, error) {
|
|
|
|
return suite.db.GetAccountByUsernameDomain(ctx, account.Username, account.Domain)
|
|
|
|
},
|
|
|
|
|
|
|
|
"username_upper@domain": func() (*gtsmodel.Account, error) {
|
|
|
|
return suite.db.GetAccountByUsernameDomain(ctx, strings.ToUpper(account.Username), account.Domain)
|
|
|
|
},
|
|
|
|
|
|
|
|
"username_lower@domain": func() (*gtsmodel.Account, error) {
|
|
|
|
return suite.db.GetAccountByUsernameDomain(ctx, strings.ToLower(account.Username), account.Domain)
|
|
|
|
},
|
|
|
|
|
|
|
|
"public_key_uri": func() (*gtsmodel.Account, error) {
|
|
|
|
if account.PublicKeyURI == "" {
|
|
|
|
return nil, sentinelErr
|
|
|
|
}
|
|
|
|
return suite.db.GetAccountByPubkeyID(ctx, account.PublicKeyURI)
|
|
|
|
},
|
|
|
|
|
|
|
|
"inbox_uri": func() (*gtsmodel.Account, error) {
|
|
|
|
if account.InboxURI == "" {
|
|
|
|
return nil, sentinelErr
|
|
|
|
}
|
|
|
|
return suite.db.GetAccountByInboxURI(ctx, account.InboxURI)
|
|
|
|
},
|
|
|
|
|
|
|
|
"outbox_uri": func() (*gtsmodel.Account, error) {
|
|
|
|
if account.OutboxURI == "" {
|
|
|
|
return nil, sentinelErr
|
|
|
|
}
|
|
|
|
return suite.db.GetAccountByOutboxURI(ctx, account.OutboxURI)
|
|
|
|
},
|
|
|
|
|
|
|
|
"following_uri": func() (*gtsmodel.Account, error) {
|
|
|
|
if account.FollowingURI == "" {
|
|
|
|
return nil, sentinelErr
|
|
|
|
}
|
|
|
|
return suite.db.GetAccountByFollowingURI(ctx, account.FollowingURI)
|
|
|
|
},
|
|
|
|
|
|
|
|
"followers_uri": func() (*gtsmodel.Account, error) {
|
|
|
|
if account.FollowersURI == "" {
|
|
|
|
return nil, sentinelErr
|
|
|
|
}
|
|
|
|
return suite.db.GetAccountByFollowersURI(ctx, account.FollowersURI)
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
|
|
|
|
// Clear database caches.
|
|
|
|
suite.state.Caches.Init()
|
|
|
|
|
|
|
|
t.Logf("checking database lookup %q", lookup)
|
|
|
|
|
|
|
|
// Perform database function.
|
|
|
|
checkAcc, err := dbfunc()
|
|
|
|
if err != nil {
|
|
|
|
if err == sentinelErr {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Errorf("error encountered for database lookup %q: %v", lookup, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check received account data.
|
|
|
|
if !isEqual(*checkAcc, *account) {
|
|
|
|
t.Errorf("account does not contain expected data: %+v", checkAcc)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that avatar attachment populated.
|
|
|
|
if account.AvatarMediaAttachmentID != "" &&
|
|
|
|
(checkAcc.AvatarMediaAttachment == nil || checkAcc.AvatarMediaAttachment.ID != account.AvatarMediaAttachmentID) {
|
|
|
|
t.Errorf("account avatar media attachment not correctly populated for: %+v", account)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that header attachment populated.
|
|
|
|
if account.HeaderMediaAttachmentID != "" &&
|
|
|
|
(checkAcc.HeaderMediaAttachment == nil || checkAcc.HeaderMediaAttachment.ID != account.HeaderMediaAttachmentID) {
|
|
|
|
t.Errorf("account header media attachment not correctly populated for: %+v", account)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-12-01 15:06:09 +00:00
|
|
|
}
|
|
|
|
|
2021-08-25 14:34:33 +01:00
|
|
|
func (suite *AccountTestSuite) TestUpdateAccount() {
|
2022-09-26 10:56:01 +01:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2021-08-25 14:34:33 +01:00
|
|
|
testAccount := suite.testAccounts["local_account_1"]
|
|
|
|
|
|
|
|
testAccount.DisplayName = "new display name!"
|
2022-09-26 10:56:01 +01:00
|
|
|
testAccount.EmojiIDs = []string{"01GD36ZKWTKY3T1JJ24JR7KY1Q", "01GD36ZV904SHBHNAYV6DX5QEF"}
|
|
|
|
|
2022-11-15 18:45:15 +00:00
|
|
|
err := suite.db.UpdateAccount(ctx, testAccount)
|
2022-09-26 10:56:01 +01:00
|
|
|
suite.NoError(err)
|
|
|
|
|
|
|
|
updated, err := suite.db.GetAccountByID(ctx, testAccount.ID)
|
|
|
|
suite.NoError(err)
|
|
|
|
suite.Equal("new display name!", updated.DisplayName)
|
|
|
|
suite.Equal([]string{"01GD36ZKWTKY3T1JJ24JR7KY1Q", "01GD36ZV904SHBHNAYV6DX5QEF"}, updated.EmojiIDs)
|
|
|
|
suite.WithinDuration(time.Now(), updated.UpdatedAt, 5*time.Second)
|
|
|
|
|
|
|
|
// get account without cache + make sure it's really in the db as desired
|
|
|
|
dbService, ok := suite.db.(*bundb.DBService)
|
|
|
|
if !ok {
|
|
|
|
panic("db was not *bundb.DBService")
|
|
|
|
}
|
|
|
|
|
|
|
|
noCache := >smodel.Account{}
|
2023-07-25 09:34:05 +01:00
|
|
|
err = dbService.DB().
|
2022-09-26 10:56:01 +01:00
|
|
|
NewSelect().
|
|
|
|
Model(noCache).
|
2022-10-08 12:50:48 +01:00
|
|
|
Where("? = ?", bun.Ident("account.id"), testAccount.ID).
|
2022-09-26 10:56:01 +01:00
|
|
|
Relation("AvatarMediaAttachment").
|
|
|
|
Relation("HeaderMediaAttachment").
|
|
|
|
Relation("Emojis").
|
|
|
|
Scan(ctx)
|
|
|
|
|
|
|
|
suite.NoError(err)
|
|
|
|
suite.Equal("new display name!", noCache.DisplayName)
|
|
|
|
suite.Equal([]string{"01GD36ZKWTKY3T1JJ24JR7KY1Q", "01GD36ZV904SHBHNAYV6DX5QEF"}, noCache.EmojiIDs)
|
|
|
|
suite.WithinDuration(time.Now(), noCache.UpdatedAt, 5*time.Second)
|
|
|
|
suite.NotNil(noCache.AvatarMediaAttachment)
|
|
|
|
suite.NotNil(noCache.HeaderMediaAttachment)
|
2021-08-25 14:34:33 +01:00
|
|
|
|
2022-09-26 10:56:01 +01:00
|
|
|
// update again to remove emoji associations
|
|
|
|
testAccount.EmojiIDs = []string{}
|
|
|
|
|
2022-11-15 18:45:15 +00:00
|
|
|
err = suite.db.UpdateAccount(ctx, testAccount)
|
2021-08-25 14:34:33 +01:00
|
|
|
suite.NoError(err)
|
|
|
|
|
2022-09-26 10:56:01 +01:00
|
|
|
updated, err = suite.db.GetAccountByID(ctx, testAccount.ID)
|
2021-08-25 14:34:33 +01:00
|
|
|
suite.NoError(err)
|
|
|
|
suite.Equal("new display name!", updated.DisplayName)
|
2022-09-26 10:56:01 +01:00
|
|
|
suite.Empty(updated.EmojiIDs)
|
2021-08-25 14:34:33 +01:00
|
|
|
suite.WithinDuration(time.Now(), updated.UpdatedAt, 5*time.Second)
|
2022-09-26 10:56:01 +01:00
|
|
|
|
2023-07-25 09:34:05 +01:00
|
|
|
err = dbService.DB().
|
2022-09-26 10:56:01 +01:00
|
|
|
NewSelect().
|
|
|
|
Model(noCache).
|
2022-10-08 12:50:48 +01:00
|
|
|
Where("? = ?", bun.Ident("account.id"), testAccount.ID).
|
2022-09-26 10:56:01 +01:00
|
|
|
Relation("AvatarMediaAttachment").
|
|
|
|
Relation("HeaderMediaAttachment").
|
|
|
|
Relation("Emojis").
|
|
|
|
Scan(ctx)
|
|
|
|
|
|
|
|
suite.NoError(err)
|
|
|
|
suite.Equal("new display name!", noCache.DisplayName)
|
|
|
|
suite.Empty(noCache.EmojiIDs)
|
|
|
|
suite.WithinDuration(time.Now(), noCache.UpdatedAt, 5*time.Second)
|
2021-08-25 14:34:33 +01:00
|
|
|
}
|
|
|
|
|
2022-09-26 13:43:19 +01:00
|
|
|
func (suite *AccountTestSuite) TestGetAccountLastPosted() {
|
2022-10-08 13:00:39 +01:00
|
|
|
lastPosted, err := suite.db.GetAccountLastPosted(context.Background(), suite.testAccounts["local_account_1"].ID, false)
|
2022-09-26 13:43:19 +01:00
|
|
|
suite.NoError(err)
|
2023-12-10 11:36:00 +00:00
|
|
|
suite.EqualValues(1702200240, lastPosted.Unix())
|
2022-09-26 13:43:19 +01:00
|
|
|
}
|
|
|
|
|
2022-10-08 13:00:39 +01:00
|
|
|
func (suite *AccountTestSuite) TestGetAccountLastPostedWebOnly() {
|
|
|
|
lastPosted, err := suite.db.GetAccountLastPosted(context.Background(), suite.testAccounts["local_account_1"].ID, true)
|
|
|
|
suite.NoError(err)
|
2023-12-10 11:36:00 +00:00
|
|
|
suite.EqualValues(1702200240, lastPosted.Unix())
|
2022-10-08 13:00:39 +01:00
|
|
|
}
|
|
|
|
|
2021-09-28 14:21:59 +01:00
|
|
|
func (suite *AccountTestSuite) TestInsertAccountWithDefaults() {
|
|
|
|
key, err := rsa.GenerateKey(rand.Reader, 2048)
|
|
|
|
suite.NoError(err)
|
|
|
|
|
|
|
|
newAccount := >smodel.Account{
|
|
|
|
ID: "01FGP5P4VJ9SPFB0T3E36Q60DW",
|
|
|
|
Username: "test_service",
|
|
|
|
Domain: "example.org",
|
|
|
|
URI: "https://example.org/users/test_service",
|
|
|
|
URL: "https://example.org/@test_service",
|
|
|
|
ActorType: ap.ActorService,
|
|
|
|
PublicKey: &key.PublicKey,
|
|
|
|
PublicKeyURI: "https://example.org/users/test_service#main-key",
|
|
|
|
}
|
|
|
|
|
|
|
|
err = suite.db.Put(context.Background(), newAccount)
|
|
|
|
suite.NoError(err)
|
|
|
|
|
|
|
|
suite.WithinDuration(time.Now(), newAccount.CreatedAt, 30*time.Second)
|
|
|
|
suite.WithinDuration(time.Now(), newAccount.UpdatedAt, 30*time.Second)
|
2024-01-19 13:02:04 +00:00
|
|
|
suite.True(*newAccount.Locked)
|
2022-08-15 11:35:05 +01:00
|
|
|
suite.False(*newAccount.Bot)
|
|
|
|
suite.False(*newAccount.Discoverable)
|
2021-09-28 14:21:59 +01:00
|
|
|
}
|
|
|
|
|
2023-02-25 12:16:30 +00:00
|
|
|
func (suite *AccountTestSuite) TestGetAccountPinnedStatusesSomeResults() {
|
|
|
|
testAccount := suite.testAccounts["admin_account"]
|
|
|
|
|
|
|
|
statuses, err := suite.db.GetAccountPinnedStatuses(context.Background(), testAccount.ID)
|
|
|
|
suite.NoError(err)
|
|
|
|
suite.Len(statuses, 2) // This account has 2 statuses pinned.
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *AccountTestSuite) TestGetAccountPinnedStatusesNothingPinned() {
|
|
|
|
testAccount := suite.testAccounts["local_account_1"]
|
|
|
|
|
|
|
|
statuses, err := suite.db.GetAccountPinnedStatuses(context.Background(), testAccount.ID)
|
|
|
|
suite.ErrorIs(err, db.ErrNoEntries)
|
|
|
|
suite.Empty(statuses) // This account has nothing pinned.
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *AccountTestSuite) TestCountAccountPinnedSomeResults() {
|
|
|
|
testAccount := suite.testAccounts["admin_account"]
|
|
|
|
|
|
|
|
pinned, err := suite.db.CountAccountPinned(context.Background(), testAccount.ID)
|
|
|
|
suite.NoError(err)
|
|
|
|
suite.Equal(pinned, 2) // This account has 2 statuses pinned.
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *AccountTestSuite) TestCountAccountPinnedNothingPinned() {
|
|
|
|
testAccount := suite.testAccounts["local_account_1"]
|
|
|
|
|
|
|
|
pinned, err := suite.db.CountAccountPinned(context.Background(), testAccount.ID)
|
|
|
|
suite.NoError(err)
|
|
|
|
suite.Equal(pinned, 0) // This account has nothing pinned.
|
|
|
|
}
|
|
|
|
|
2024-03-10 10:59:55 +00:00
|
|
|
func (suite *AccountTestSuite) TestPopulateAccountWithUnknownMovedToURI() {
|
|
|
|
testAccount := >smodel.Account{}
|
|
|
|
*testAccount = *suite.testAccounts["local_account_1"]
|
|
|
|
|
|
|
|
// Set test account MovedToURI to something we don't have in the database.
|
|
|
|
// We should not get an error when populating.
|
|
|
|
testAccount.MovedToURI = "https://unknown-instance.example.org/users/someone_we_dont_know"
|
|
|
|
err := suite.db.PopulateAccount(context.Background(), testAccount)
|
|
|
|
suite.NoError(err)
|
|
|
|
}
|
|
|
|
|
2021-08-20 11:26:56 +01:00
|
|
|
func TestAccountTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(AccountTestSuite))
|
|
|
|
}
|