mirror of
1
Fork 0

[performance] cache more database calls, reduce required database calls overall (#3290)

* improvements to caching for lists and relationship to accounts / follows

* fix nil panic in AddToList()

* ensure list related caches are correctly invalidated

* ensure returned ID lists are ordered correctly

* bump go-structr to v0.8.9 (returns early if zero uncached keys to be loaded)

* remove zero checks in uncached key load functions (go-structr now handles this)

* fix issues after rebase on upstream/main

* update the expected return order of CSV exports (since list entries are now down by entry creation date)

* rename some funcs, allow deleting list entries for multiple follow IDs at a time, fix up more tests

* use returning statements on delete to get cache invalidation info

* fixes to recent database delete changes

* fix broken list entries delete sql

* remove unused db function

* update remainder of delete functions to behave in similar way, some other small tweaks

* fix delete user sql, allow returning on err no entries

* uncomment + fix list database tests

* update remaining list tests

* update envparsing test

* add comments to each specific key being invalidated

* add more cache invalidation explanatory comments

* whoops; actually delete poll votes from database in the DeletePollByID() func

* remove added but-commented-out field

* improved comment regarding paging being disabled

* make cache invalidation comments match what's actually happening

* fix up delete query comments to match what is happening

* rename function to read a bit better

* don't use ErrNoEntries on delete when not needed (it's only needed for a RETURNING call)

* update function name in test

* move list exclusivity check to AFTER eligibility check. use log.Panic() instead of panic()

* use the poll_id column in poll_votes for selecting votes in poll ID

* fix function name
This commit is contained in:
kim 2024-09-16 16:46:09 +00:00 committed by GitHub
parent 0567b319c6
commit 84279f6a6a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
68 changed files with 1683 additions and 2135 deletions

2
go.mod
View File

@ -23,7 +23,7 @@ require (
codeberg.org/gruf/go-runners v1.6.2
codeberg.org/gruf/go-sched v1.2.3
codeberg.org/gruf/go-storage v0.1.2
codeberg.org/gruf/go-structr v0.8.8
codeberg.org/gruf/go-structr v0.8.9
codeberg.org/superseriousbusiness/exif-terminator v0.9.0
github.com/DmitriyVTitov/size v1.5.0
github.com/KimMachineGun/automemlimit v0.6.1

4
go.sum
View File

@ -80,8 +80,8 @@ codeberg.org/gruf/go-sched v1.2.3 h1:H5ViDxxzOBR3uIyGBCf0eH8b1L8wMybOXcdtUUTXZHk
codeberg.org/gruf/go-sched v1.2.3/go.mod h1:vT9uB6KWFIIwnG9vcPY2a0alYNoqdL1mSzRM8I+PK7A=
codeberg.org/gruf/go-storage v0.1.2 h1:dIOVOKq1CJpRmuhbB8Zok3mmo8V6VV/nX5GLIm6hywA=
codeberg.org/gruf/go-storage v0.1.2/go.mod h1:LRDpFHqRJi0f+35c3ltBH2e/pGfwY5dGlNlgCJ/R1DA=
codeberg.org/gruf/go-structr v0.8.8 h1:lRPpyTmLKvQCkkQiSUbOAh6jtL2wncEO8DwksMqQXM8=
codeberg.org/gruf/go-structr v0.8.8/go.mod h1:zkoXVrAnKosh8VFAsbP/Hhs8FmLBjbVVy5w/Ngm8ApM=
codeberg.org/gruf/go-structr v0.8.9 h1:OyiSspWYCeJOm356fFPd+bDRumPrard2VAUXAPqZiJ0=
codeberg.org/gruf/go-structr v0.8.9/go.mod h1:zkoXVrAnKosh8VFAsbP/Hhs8FmLBjbVVy5w/Ngm8ApM=
codeberg.org/superseriousbusiness/exif-terminator v0.9.0 h1:/EfyGI6HIrbkhFwgXGSjZ9o1kr/+k8v4mKdfXTH02Go=
codeberg.org/superseriousbusiness/exif-terminator v0.9.0/go.mod h1:gCWKduudUWFzsnixoMzu0FYVdxHWG+AbXnZ50DqxsUE=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=

View File

@ -188,8 +188,8 @@ admin@localhost:8080
token: suite.testTokens["local_account_1"],
user: suite.testUsers["local_account_1"],
account: suite.testAccounts["local_account_1"],
expect: `Cool Ass Posters From This Instance,admin@localhost:8080
Cool Ass Posters From This Instance,1happyturtle@localhost:8080
expect: `Cool Ass Posters From This Instance,1happyturtle@localhost:8080
Cool Ass Posters From This Instance,admin@localhost:8080
`,
},
// Export Mutes.

View File

@ -25,6 +25,7 @@ import (
apiutil "github.com/superseriousbusiness/gotosocial/internal/api/util"
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
"github.com/superseriousbusiness/gotosocial/internal/oauth"
"github.com/superseriousbusiness/gotosocial/internal/paging"
)
// ListAccountsGETHandler swagger:operation GET /api/v1/lists/{id}/accounts listAccounts
@ -129,42 +130,27 @@ func (m *Module) ListAccountsGETHandler(c *gin.Context) {
targetListID := c.Param(IDKey)
if targetListID == "" {
err := errors.New("no list id specified")
apiutil.ErrorHandler(c, gtserror.NewErrorBadRequest(err, err.Error()), m.processor.InstanceGetV1)
const text = "no list id specified"
errWithCode := gtserror.NewErrorBadRequest(errors.New(text), text)
apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1)
return
}
limit, errWithCode := apiutil.ParseLimit(c.Query(apiutil.LimitKey), 40, 80, 0)
page, errWithCode := paging.ParseIDPage(c,
1, // min limit
80, // max limit
0, // default = paging disabled
)
if errWithCode != nil {
apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1)
return
}
var (
ctx = c.Request.Context()
)
if limit == 0 {
// Return all accounts in the list without pagination.
accounts, errWithCode := m.processor.List().GetAllListAccounts(ctx, authed.Account, targetListID)
if errWithCode != nil {
apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1)
return
}
c.JSON(http.StatusOK, accounts)
return
}
// Return subset of accounts in the list with pagination.
resp, errWithCode := m.processor.List().GetListAccounts(
ctx,
c.Request.Context(),
authed.Account,
targetListID,
c.Query(MaxIDKey),
c.Query(SinceIDKey),
c.Query(MinIDKey),
limit,
page,
)
if errWithCode != nil {
apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1)

View File

@ -19,7 +19,7 @@ package lists_test
import (
"encoding/json"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"strconv"
@ -97,7 +97,7 @@ func (suite *ListAccountsTestSuite) getListAccounts(
result := recorder.Result()
defer result.Body.Close()
b, err := ioutil.ReadAll(result.Body)
b, err := io.ReadAll(result.Body)
if err != nil {
return nil, "", err
}
@ -151,8 +151,7 @@ func (suite *ListAccountsTestSuite) TestGetListAccountsPaginatedDefaultLimit() {
suite.Len(accounts, 2)
suite.Equal(
`<http://localhost:8080/api/v1/lists/01H0G8E4Q2J3FE3JDWJVWEDCD1/accounts?limit=40&max_id=01H0G89MWVQE0M58VD2HQYMQWH>; rel="next", `+
`<http://localhost:8080/api/v1/lists/01H0G8E4Q2J3FE3JDWJVWEDCD1/accounts?limit=40&min_id=01H0G8FFM1AGQDRNGBGGX8CYJQ>; rel="prev"`,
"<http://localhost:8080/api/v1/lists/01H0G8E4Q2J3FE3JDWJVWEDCD1/accounts?limit=40&max_id=01F8MH5NBDF2MV7CTC4Q5128HF>; rel=\"next\", <http://localhost:8080/api/v1/lists/01H0G8E4Q2J3FE3JDWJVWEDCD1/accounts?limit=40&min_id=01F8MH17FWEB39HZJ76B6VXSKF>; rel=\"prev\"",
link,
)
}
@ -184,8 +183,7 @@ func (suite *ListAccountsTestSuite) TestGetListAccountsPaginatedNextPage() {
suite.Len(accounts, 1)
suite.Equal(
`<http://localhost:8080/api/v1/lists/01H0G8E4Q2J3FE3JDWJVWEDCD1/accounts?limit=1&max_id=01H0G8FFM1AGQDRNGBGGX8CYJQ>; rel="next", `+
`<http://localhost:8080/api/v1/lists/01H0G8E4Q2J3FE3JDWJVWEDCD1/accounts?limit=1&min_id=01H0G8FFM1AGQDRNGBGGX8CYJQ>; rel="prev"`,
"<http://localhost:8080/api/v1/lists/01H0G8E4Q2J3FE3JDWJVWEDCD1/accounts?limit=1&max_id=01F8MH17FWEB39HZJ76B6VXSKF>; rel=\"next\", <http://localhost:8080/api/v1/lists/01H0G8E4Q2J3FE3JDWJVWEDCD1/accounts?limit=1&min_id=01F8MH17FWEB39HZJ76B6VXSKF>; rel=\"prev\"",
link,
)
@ -206,8 +204,7 @@ func (suite *ListAccountsTestSuite) TestGetListAccountsPaginatedNextPage() {
suite.Len(accounts, 1)
suite.Equal(
`<http://localhost:8080/api/v1/lists/01H0G8E4Q2J3FE3JDWJVWEDCD1/accounts?limit=1&max_id=01H0G89MWVQE0M58VD2HQYMQWH>; rel="next", `+
`<http://localhost:8080/api/v1/lists/01H0G8E4Q2J3FE3JDWJVWEDCD1/accounts?limit=1&min_id=01H0G89MWVQE0M58VD2HQYMQWH>; rel="prev"`,
"<http://localhost:8080/api/v1/lists/01H0G8E4Q2J3FE3JDWJVWEDCD1/accounts?limit=1&max_id=01F8MH17FWEB39HZJ76B6VXSKF>; rel=\"next\", <http://localhost:8080/api/v1/lists/01H0G8E4Q2J3FE3JDWJVWEDCD1/accounts?limit=1&min_id=01F8MH17FWEB39HZJ76B6VXSKF>; rel=\"prev\"",
link,
)
}

View File

@ -98,14 +98,17 @@ func (suite *ListAccountsAddTestSuite) TestPostListAccountNotFollowed() {
resp, err := suite.postListAccounts(http.StatusNotFound, listID, accountIDs)
suite.NoError(err)
suite.Equal(`{"error":"Not Found: you do not follow account 01F8MH5ZK5VRH73AKHQM6Y9VNX"}`, string(resp))
suite.Equal(`{"error":"Not Found: account 01F8MH5ZK5VRH73AKHQM6Y9VNX not currently followed"}`, string(resp))
}
func (suite *ListAccountsAddTestSuite) TestPostListAccountOK() {
entry := suite.testListEntries["local_account_1_list_1_entry_1"]
// Remove turtle from the list.
if err := suite.db.DeleteListEntry(
context.Background(),
suite.testListEntries["local_account_1_list_1_entry_1"].ID,
entry.ListID,
entry.FollowID,
); err != nil {
suite.FailNow(err.Error())
}

View File

@ -62,7 +62,6 @@ func (c *Caches) Init() {
log.Infof(nil, "init: %p", c)
c.initAccount()
c.initAccountIDsFollowingTag()
c.initAccountNote()
c.initAccountSettings()
c.initAccountStats()
@ -84,11 +83,13 @@ func (c *Caches) Init() {
c.initFollowIDs()
c.initFollowRequest()
c.initFollowRequestIDs()
c.initFollowingTagIDs()
c.initInReplyToIDs()
c.initInstance()
c.initInteractionRequest()
c.initList()
c.initListEntry()
c.initListIDs()
c.initListedIDs()
c.initMarker()
c.initMedia()
c.initMention()
@ -105,7 +106,6 @@ func (c *Caches) Init() {
c.initStatusFave()
c.initStatusFaveIDs()
c.initTag()
c.initTagIDsFollowedByAccount()
c.initThreadMute()
c.initToken()
c.initTombstone()
@ -148,7 +148,6 @@ func (c *Caches) Stop() {
// significant overhead to all cache writes.
func (c *Caches) Sweep(threshold float64) {
c.DB.Account.Trim(threshold)
c.DB.AccountIDsFollowingTag.Trim(threshold)
c.DB.AccountNote.Trim(threshold)
c.DB.AccountSettings.Trim(threshold)
c.DB.AccountStats.Trim(threshold)
@ -168,11 +167,13 @@ func (c *Caches) Sweep(threshold float64) {
c.DB.FollowIDs.Trim(threshold)
c.DB.FollowRequest.Trim(threshold)
c.DB.FollowRequestIDs.Trim(threshold)
c.DB.FollowingTagIDs.Trim(threshold)
c.DB.InReplyToIDs.Trim(threshold)
c.DB.Instance.Trim(threshold)
c.DB.InteractionRequest.Trim(threshold)
c.DB.List.Trim(threshold)
c.DB.ListEntry.Trim(threshold)
c.DB.ListIDs.Trim(threshold)
c.DB.ListedIDs.Trim(threshold)
c.DB.Marker.Trim(threshold)
c.DB.Media.Trim(threshold)
c.DB.Mention.Trim(threshold)
@ -189,7 +190,6 @@ func (c *Caches) Sweep(threshold float64) {
c.DB.StatusFave.Trim(threshold)
c.DB.StatusFaveIDs.Trim(threshold)
c.DB.Tag.Trim(threshold)
c.DB.TagIDsFollowedByAccount.Trim(threshold)
c.DB.ThreadMute.Trim(threshold)
c.DB.Token.Trim(threshold)
c.DB.Tombstone.Trim(threshold)

151
internal/cache/db.go vendored
View File

@ -29,9 +29,6 @@ type DBCaches struct {
// Account provides access to the gtsmodel Account database cache.
Account StructCache[*gtsmodel.Account]
// AccountIDsFollowingTag caches account IDs following a given tag ID.
AccountIDsFollowingTag SliceCache[string]
// AccountNote provides access to the gtsmodel Note database cache.
AccountNote StructCache[*gtsmodel.AccountNote]
@ -88,10 +85,23 @@ type DBCaches struct {
// FollowIDs provides access to the follower / following IDs database cache.
// THIS CACHE IS KEYED AS THE FOLLOWING {prefix}{accountID} WHERE PREFIX IS:
// - '>' for following IDs
// - 'l>' for local following IDs
// - '<' for follower IDs
// - 'l<' for local follower IDs
//
// - '>{$accountID}' for following IDs
// e.g. FollowIDs.Load(">" + account.ID, func() {})
// which will load a slice of follows IDs FROM account.
//
// - 'l>{$accountID}' for local following IDs
// e.g. FollowIDs.Load("l>" + account.ID, func() {})
// which will load a slice of LOCAL follows IDs FROM account.
//
// - '<{$accountID}' for follower IDs
// e.g. FollowIDs.Load("<" + account.ID, func() {})
// which will load a slice of follows IDs TARGETTING account.
//
// - 'l<{$accountID}' for local follower IDs
// e.g. FollowIDs.Load("l<" + account.ID, func() {})
// which will load a slice of LOCAL follows IDs TARGETTING account.
//
FollowIDs SliceCache[string]
// FollowRequest provides access to the gtsmodel FollowRequest database cache.
@ -99,10 +109,30 @@ type DBCaches struct {
// FollowRequestIDs provides access to the follow requester / requesting IDs database
// cache. THIS CACHE IS KEYED AS THE FOLLOWING {prefix}{accountID} WHERE PREFIX IS:
// - '>' for following IDs
// - '<' for follower IDs
//
// - '>{$accountID}' for follow request IDs
// e.g. FollowRequestIDs.Load(">" + account.ID, func() {})
// which will load a slice of follow request IDs TARGETTING account.
//
// - '<{$accountID}' for follow request IDs
// e.g. FollowRequestIDs.Load("<" + account.ID, func() {})
// which will load a slice of follow request IDs FROM account.
//
FollowRequestIDs SliceCache[string]
// FollowingTagIDs provides access to account IDs following / tag IDs followed by
// account db cache. THIS CACHE IS KEYED AS THE FOLLOWING {prefix}{id} WHERE:
//
// - '>{$accountID}' for tag IDs followed by account
// e.g. FollowingTagIDs.Load(">" + account.ID, func() {})
// which will load a slice of tag IDs followed by account.
//
// - '<{$tagIDs}' for account IDs following tag
// e.g. FollowingTagIDs.Load("<" + tag.ID, func() {})
// which will load a slice of account IDs following tag.
//
FollowingTagIDs SliceCache[string]
// Instance provides access to the gtsmodel Instance database cache.
Instance StructCache[*gtsmodel.Instance]
@ -115,8 +145,31 @@ type DBCaches struct {
// List provides access to the gtsmodel List database cache.
List StructCache[*gtsmodel.List]
// ListEntry provides access to the gtsmodel ListEntry database cache.
ListEntry StructCache[*gtsmodel.ListEntry]
// ListIDs provides access to the list IDs owned by account / list IDs follow
// contained in db cache. THIS CACHE IS KEYED AS FOLLOWING {prefix}{id} WHERE:
//
// - 'a{$accountID}' for list IDs owned by account
// e.g. ListIDs.Load("a" + account.ID, func() {})
// which will load a slice of list IDs owned by account.
//
// - 'f{$followID}' for list IDs follow contained in
// e.g. ListIDs.Load("f" + follow.ID, func() {})
// which will load a slice of list IDs containing follow.
//
ListIDs SliceCache[string]
// ListedIDs provides access to the account IDs in list / follow IDs in
// list db cache. THIS CACHE IS KEYED AS FOLLOWING {prefix}{id} WHERE:
//
// - 'a{listID}' for account IDs in list ID
// e.g. ListedIDs.Load("a" + list.ID, func() {})
// which will load a slice of account IDs in list.
//
// - 'f{listID}' for follow IDs in list ID
// e.g. ListedIDs.Load("f" + list.ID, func() {})
// which will load a slice of follow IDs in list.
//
ListedIDs SliceCache[string]
// Marker provides access to the gtsmodel Marker database cache.
Marker StructCache[*gtsmodel.Marker]
@ -151,10 +204,10 @@ type DBCaches struct {
// Status provides access to the gtsmodel Status database cache.
Status StructCache[*gtsmodel.Status]
// StatusBookmark ...
// StatusBookmark provides access to the gtsmodel StatusBookmark database cache.
StatusBookmark StructCache[*gtsmodel.StatusBookmark]
// StatusBookmarkIDs ...
// StatusBookmarkIDs provides access to the status bookmark IDs list database cache.
StatusBookmarkIDs SliceCache[string]
// StatusFave provides access to the gtsmodel StatusFave database cache.
@ -166,9 +219,6 @@ type DBCaches struct {
// Tag provides access to the gtsmodel Tag database cache.
Tag StructCache[*gtsmodel.Tag]
// TagIDsFollowedByAccount caches tag IDs followed by a given account ID.
TagIDsFollowedByAccount SliceCache[string]
// ThreadMute provides access to the gtsmodel ThreadMute database cache.
ThreadMute StructCache[*gtsmodel.ThreadMute]
@ -243,17 +293,6 @@ func (c *Caches) initAccount() {
})
}
func (c *Caches) initAccountIDsFollowingTag() {
// Calculate maximum cache size.
cap := calculateSliceCacheMax(
config.GetCacheAccountIDsFollowingTagMemRatio(),
)
log.Infof(nil, "cache size = %d", cap)
c.DB.AccountIDsFollowingTag.Init(0, cap)
}
func (c *Caches) initAccountNote() {
// Calculate maximum cache size.
cap := calculateResultCacheMax(
@ -761,6 +800,17 @@ func (c *Caches) initFollowRequestIDs() {
c.DB.FollowRequestIDs.Init(0, cap)
}
func (c *Caches) initFollowingTagIDs() {
// Calculate maximum cache size.
cap := calculateSliceCacheMax(
config.GetCacheFollowingTagIDsMemRatio(),
)
log.Infof(nil, "cache size = %d", cap)
c.DB.FollowingTagIDs.Init(0, cap)
}
func (c *Caches) initInReplyToIDs() {
// Calculate maximum cache size.
cap := calculateSliceCacheMax(
@ -860,7 +910,6 @@ func (c *Caches) initList() {
// will be populated separately.
// See internal/db/bundb/list.go.
l2.Account = nil
l2.ListEntries = nil
return l2
}
@ -876,37 +925,26 @@ func (c *Caches) initList() {
})
}
func (c *Caches) initListEntry() {
func (c *Caches) initListIDs() {
// Calculate maximum cache size.
cap := calculateResultCacheMax(
sizeofListEntry(), // model in-mem size.
config.GetCacheListEntryMemRatio(),
cap := calculateSliceCacheMax(
config.GetCacheListIDsMemRatio(),
)
log.Infof(nil, "cache size = %d", cap)
copyF := func(l1 *gtsmodel.ListEntry) *gtsmodel.ListEntry {
l2 := new(gtsmodel.ListEntry)
*l2 = *l1
c.DB.ListIDs.Init(0, cap)
}
// Don't include ptr fields that
// will be populated separately.
// See internal/db/bundb/list.go.
l2.Follow = nil
func (c *Caches) initListedIDs() {
// Calculate maximum cache size.
cap := calculateSliceCacheMax(
config.GetCacheListedIDsMemRatio(),
)
return l2
}
log.Infof(nil, "cache size = %d", cap)
c.DB.ListEntry.Init(structr.CacheConfig[*gtsmodel.ListEntry]{
Indices: []structr.IndexConfig{
{Fields: "ID"},
{Fields: "ListID", Multiple: true},
{Fields: "FollowID", Multiple: true},
},
MaxSize: cap,
IgnoreErr: ignoreErrors,
Copy: copyF,
})
c.DB.ListedIDs.Init(0, cap)
}
func (c *Caches) initMarker() {
@ -1368,17 +1406,6 @@ func (c *Caches) initTag() {
})
}
func (c *Caches) initTagIDsFollowedByAccount() {
// Calculate maximum cache size.
cap := calculateSliceCacheMax(
config.GetCacheTagIDsFollowedByAccountMemRatio(),
)
log.Infof(nil, "cache size = %d", cap)
c.DB.TagIDsFollowedByAccount.Init(0, cap)
}
func (c *Caches) initThreadMute() {
cap := calculateResultCacheMax(
sizeofThreadMute(), // model in-mem size.

View File

@ -97,9 +97,6 @@ func (c *Caches) OnInvalidateFollow(follow *gtsmodel.Follow) {
// Invalidate follow request with this same ID.
c.DB.FollowRequest.Invalidate("ID", follow.ID)
// Invalidate any related list entries.
c.DB.ListEntry.Invalidate("FollowID", follow.ID)
// Invalidate follow origin account ID cached visibility.
c.Visibility.Invalidate("ItemID", follow.AccountID)
c.Visibility.Invalidate("RequesterID", follow.AccountID)
@ -108,18 +105,47 @@ func (c *Caches) OnInvalidateFollow(follow *gtsmodel.Follow) {
c.Visibility.Invalidate("ItemID", follow.TargetAccountID)
c.Visibility.Invalidate("RequesterID", follow.TargetAccountID)
// Invalidate source account's following
// lists, and destination's follwer lists.
// (see FollowIDs() comment for details).
// Invalidate ID slice cache.
c.DB.FollowIDs.Invalidate(
// Invalidate follow ID lists
// TARGETTING origin account
// (including local-only follows).
">"+follow.AccountID,
"l>"+follow.AccountID,
// Invalidate follow ID lists
// FROM the origin account
// (including local-only follows).
"<"+follow.AccountID,
"l<"+follow.AccountID,
"<"+follow.TargetAccountID,
"l<"+follow.TargetAccountID,
// Invalidate follow ID lists
// TARGETTING the target account
// (including local-only follows).
">"+follow.TargetAccountID,
"l>"+follow.TargetAccountID,
// Invalidate follow ID lists
// FROM the target account
// (including local-only follows).
"<"+follow.TargetAccountID,
"l<"+follow.TargetAccountID,
)
// Invalidate ID slice cache.
c.DB.ListIDs.Invalidate(
// Invalidate source
// account's owned lists.
"a"+follow.AccountID,
// Invalidate target account's.
"a"+follow.TargetAccountID,
// Invalidate lists containing
// list entries for follow.
"f"+follow.ID,
)
}
@ -127,20 +153,48 @@ func (c *Caches) OnInvalidateFollowRequest(followReq *gtsmodel.FollowRequest) {
// Invalidate follow with this same ID.
c.DB.Follow.Invalidate("ID", followReq.ID)
// Invalidate source account's followreq
// lists, and destinations follow req lists.
// (see FollowRequestIDs() comment for details).
// Invalidate ID slice cache.
c.DB.FollowRequestIDs.Invalidate(
// Invalidate follow request ID
// lists TARGETTING origin account
// (including local-only follows).
">"+followReq.AccountID,
// Invalidate follow request ID
// lists FROM the origin account
// (including local-only follows).
"<"+followReq.AccountID,
// Invalidate follow request ID
// lists TARGETTING target account
// (including local-only follows).
">"+followReq.TargetAccountID,
// Invalidate follow request ID
// lists FROM the target account
// (including local-only follows).
"<"+followReq.TargetAccountID,
)
}
func (c *Caches) OnInvalidateList(list *gtsmodel.List) {
// Invalidate all cached entries of this list.
c.DB.ListEntry.Invalidate("ListID", list.ID)
// Invalidate list IDs cache.
c.DB.ListIDs.Invalidate(
"a" + list.AccountID,
)
// Invalidate ID slice cache.
c.DB.ListedIDs.Invalidate(
// Invalidate list of
// account IDs in list.
"a"+list.ID,
// Invalidate list of
// follow IDs in list.
"f"+list.ID,
)
}
func (c *Caches) OnInvalidateMedia(media *gtsmodel.MediaAttachment) {
@ -184,7 +238,7 @@ func (c *Caches) OnInvalidateStatus(status *gtsmodel.Status) {
// the media IDs in use before the media table is
// aware of the status ID they are linked to.
//
// c.DB.Media().Invalidate("StatusID") will not work.
// c.DB.Media.Invalidate("StatusID") will not work.
c.DB.Media.InvalidateIDs("ID", status.AttachmentIDs)
if status.BoostOfID != "" {

View File

@ -166,6 +166,7 @@ func calculateCacheMax(keySz, valSz uintptr, ratio float64) int {
// totalOfRatios returns the total of all cache ratios added together.
func totalOfRatios() float64 {
// NOTE: this is not performant calculating
// this every damn time (mainly the mutex unlocks
// required to access each config var). fortunately
@ -189,11 +190,13 @@ func totalOfRatios() float64 {
config.GetCacheFollowIDsMemRatio() +
config.GetCacheFollowRequestMemRatio() +
config.GetCacheFollowRequestIDsMemRatio() +
config.GetCacheFollowingTagIDsMemRatio() +
config.GetCacheInReplyToIDsMemRatio() +
config.GetCacheInstanceMemRatio() +
config.GetCacheInteractionRequestMemRatio() +
config.GetCacheInReplyToIDsMemRatio() +
config.GetCacheListMemRatio() +
config.GetCacheListEntryMemRatio() +
config.GetCacheListIDsMemRatio() +
config.GetCacheListedIDsMemRatio() +
config.GetCacheMarkerMemRatio() +
config.GetCacheMediaMemRatio() +
config.GetCacheMentionMemRatio() +
@ -201,7 +204,9 @@ func totalOfRatios() float64 {
config.GetCacheNotificationMemRatio() +
config.GetCachePollMemRatio() +
config.GetCachePollVoteMemRatio() +
config.GetCachePollVoteIDsMemRatio() +
config.GetCacheReportMemRatio() +
config.GetCacheSinBinStatusMemRatio() +
config.GetCacheStatusMemRatio() +
config.GetCacheStatusBookmarkMemRatio() +
config.GetCacheStatusBookmarkIDsMemRatio() +
@ -212,6 +217,8 @@ func totalOfRatios() float64 {
config.GetCacheTokenMemRatio() +
config.GetCacheTombstoneMemRatio() +
config.GetCacheUserMemRatio() +
config.GetCacheUserMuteMemRatio() +
config.GetCacheUserMuteIDsMemRatio() +
config.GetCacheWebfingerMemRatio() +
config.GetCacheVisibilityMemRatio()
}
@ -466,16 +473,6 @@ func sizeofList() uintptr {
}))
}
func sizeofListEntry() uintptr {
return uintptr(size.Of(&gtsmodel.ListEntry{
ID: exampleID,
CreatedAt: exampleTime,
UpdatedAt: exampleTime,
ListID: exampleID,
FollowID: exampleID,
}))
}
func sizeofMarker() uintptr {
return uintptr(size.Of(&gtsmodel.Marker{
AccountID: exampleID,

View File

@ -196,7 +196,6 @@ type HTTPClientConfiguration struct {
type CacheConfiguration struct {
MemoryTarget bytesize.Size `name:"memory-target"`
AccountMemRatio float64 `name:"account-mem-ratio"`
AccountIDsFollowingTagMemRatio float64 `name:"account-ids-following-tag-mem-ratio"`
AccountNoteMemRatio float64 `name:"account-note-mem-ratio"`
AccountSettingsMemRatio float64 `name:"account-settings-mem-ratio"`
AccountStatsMemRatio float64 `name:"account-stats-mem-ratio"`
@ -216,11 +215,13 @@ type CacheConfiguration struct {
FollowIDsMemRatio float64 `name:"follow-ids-mem-ratio"`
FollowRequestMemRatio float64 `name:"follow-request-mem-ratio"`
FollowRequestIDsMemRatio float64 `name:"follow-request-ids-mem-ratio"`
FollowingTagIDsMemRatio float64 `name:"following-tag-ids-mem-ratio"`
InReplyToIDsMemRatio float64 `name:"in-reply-to-ids-mem-ratio"`
InstanceMemRatio float64 `name:"instance-mem-ratio"`
InteractionRequestMemRatio float64 `name:"interaction-request-mem-ratio"`
ListMemRatio float64 `name:"list-mem-ratio"`
ListEntryMemRatio float64 `name:"list-entry-mem-ratio"`
ListIDsMemRatio float64 `name:"list-ids-mem-ratio"`
ListedIDsMemRatio float64 `name:"listed-ids-mem-ratio"`
MarkerMemRatio float64 `name:"marker-mem-ratio"`
MediaMemRatio float64 `name:"media-mem-ratio"`
MentionMemRatio float64 `name:"mention-mem-ratio"`
@ -237,7 +238,6 @@ type CacheConfiguration struct {
StatusFaveMemRatio float64 `name:"status-fave-mem-ratio"`
StatusFaveIDsMemRatio float64 `name:"status-fave-ids-mem-ratio"`
TagMemRatio float64 `name:"tag-mem-ratio"`
TagIDsFollowedByAccountMemRatio float64 `name:"tag-ids-followed-by-account-mem-ratio"`
ThreadMuteMemRatio float64 `name:"thread-mute-mem-ratio"`
TokenMemRatio float64 `name:"token-mem-ratio"`
TombstoneMemRatio float64 `name:"tombstone-mem-ratio"`

View File

@ -159,7 +159,6 @@ var Defaults = Configuration{
// file have been addressed, these should
// be able to make some more sense :D
AccountMemRatio: 5,
AccountIDsFollowingTagMemRatio: 1,
AccountNoteMemRatio: 1,
AccountSettingsMemRatio: 0.1,
AccountStatsMemRatio: 2,
@ -179,11 +178,13 @@ var Defaults = Configuration{
FollowIDsMemRatio: 4,
FollowRequestMemRatio: 2,
FollowRequestIDsMemRatio: 2,
FollowingTagIDsMemRatio: 2,
InReplyToIDsMemRatio: 3,
InstanceMemRatio: 1,
InteractionRequestMemRatio: 1,
ListMemRatio: 1,
ListEntryMemRatio: 2,
ListIDsMemRatio: 2,
ListedIDsMemRatio: 2,
MarkerMemRatio: 0.5,
MediaMemRatio: 4,
MentionMemRatio: 2,
@ -200,7 +201,6 @@ var Defaults = Configuration{
StatusFaveMemRatio: 2,
StatusFaveIDsMemRatio: 3,
TagMemRatio: 2,
TagIDsFollowedByAccountMemRatio: 1,
ThreadMuteMemRatio: 0.2,
TokenMemRatio: 0.75,
TombstoneMemRatio: 0.5,

View File

@ -2850,37 +2850,6 @@ func GetCacheAccountMemRatio() float64 { return global.GetCacheAccountMemRatio()
// SetCacheAccountMemRatio safely sets the value for global configuration 'Cache.AccountMemRatio' field
func SetCacheAccountMemRatio(v float64) { global.SetCacheAccountMemRatio(v) }
// GetCacheAccountIDsFollowingTagMemRatio safely fetches the Configuration value for state's 'Cache.AccountIDsFollowingTagMemRatio' field
func (st *ConfigState) GetCacheAccountIDsFollowingTagMemRatio() (v float64) {
st.mutex.RLock()
v = st.config.Cache.AccountIDsFollowingTagMemRatio
st.mutex.RUnlock()
return
}
// SetCacheAccountIDsFollowingTagMemRatio safely sets the Configuration value for state's 'Cache.AccountIDsFollowingTagMemRatio' field
func (st *ConfigState) SetCacheAccountIDsFollowingTagMemRatio(v float64) {
st.mutex.Lock()
defer st.mutex.Unlock()
st.config.Cache.AccountIDsFollowingTagMemRatio = v
st.reloadToViper()
}
// CacheAccountIDsFollowingTagMemRatioFlag returns the flag name for the 'Cache.AccountIDsFollowingTagMemRatio' field
func CacheAccountIDsFollowingTagMemRatioFlag() string {
return "cache-account-ids-following-tag-mem-ratio"
}
// GetCacheAccountIDsFollowingTagMemRatio safely fetches the value for global configuration 'Cache.AccountIDsFollowingTagMemRatio' field
func GetCacheAccountIDsFollowingTagMemRatio() float64 {
return global.GetCacheAccountIDsFollowingTagMemRatio()
}
// SetCacheAccountIDsFollowingTagMemRatio safely sets the value for global configuration 'Cache.AccountIDsFollowingTagMemRatio' field
func SetCacheAccountIDsFollowingTagMemRatio(v float64) {
global.SetCacheAccountIDsFollowingTagMemRatio(v)
}
// GetCacheAccountNoteMemRatio safely fetches the Configuration value for state's 'Cache.AccountNoteMemRatio' field
func (st *ConfigState) GetCacheAccountNoteMemRatio() (v float64) {
st.mutex.RLock()
@ -3362,6 +3331,31 @@ func GetCacheFollowRequestIDsMemRatio() float64 { return global.GetCacheFollowRe
// SetCacheFollowRequestIDsMemRatio safely sets the value for global configuration 'Cache.FollowRequestIDsMemRatio' field
func SetCacheFollowRequestIDsMemRatio(v float64) { global.SetCacheFollowRequestIDsMemRatio(v) }
// GetCacheFollowingTagIDsMemRatio safely fetches the Configuration value for state's 'Cache.FollowingTagIDsMemRatio' field
func (st *ConfigState) GetCacheFollowingTagIDsMemRatio() (v float64) {
st.mutex.RLock()
v = st.config.Cache.FollowingTagIDsMemRatio
st.mutex.RUnlock()
return
}
// SetCacheFollowingTagIDsMemRatio safely sets the Configuration value for state's 'Cache.FollowingTagIDsMemRatio' field
func (st *ConfigState) SetCacheFollowingTagIDsMemRatio(v float64) {
st.mutex.Lock()
defer st.mutex.Unlock()
st.config.Cache.FollowingTagIDsMemRatio = v
st.reloadToViper()
}
// CacheFollowingTagIDsMemRatioFlag returns the flag name for the 'Cache.FollowingTagIDsMemRatio' field
func CacheFollowingTagIDsMemRatioFlag() string { return "cache-following-tag-ids-mem-ratio" }
// GetCacheFollowingTagIDsMemRatio safely fetches the value for global configuration 'Cache.FollowingTagIDsMemRatio' field
func GetCacheFollowingTagIDsMemRatio() float64 { return global.GetCacheFollowingTagIDsMemRatio() }
// SetCacheFollowingTagIDsMemRatio safely sets the value for global configuration 'Cache.FollowingTagIDsMemRatio' field
func SetCacheFollowingTagIDsMemRatio(v float64) { global.SetCacheFollowingTagIDsMemRatio(v) }
// GetCacheInReplyToIDsMemRatio safely fetches the Configuration value for state's 'Cache.InReplyToIDsMemRatio' field
func (st *ConfigState) GetCacheInReplyToIDsMemRatio() (v float64) {
st.mutex.RLock()
@ -3462,30 +3456,55 @@ func GetCacheListMemRatio() float64 { return global.GetCacheListMemRatio() }
// SetCacheListMemRatio safely sets the value for global configuration 'Cache.ListMemRatio' field
func SetCacheListMemRatio(v float64) { global.SetCacheListMemRatio(v) }
// GetCacheListEntryMemRatio safely fetches the Configuration value for state's 'Cache.ListEntryMemRatio' field
func (st *ConfigState) GetCacheListEntryMemRatio() (v float64) {
// GetCacheListIDsMemRatio safely fetches the Configuration value for state's 'Cache.ListIDsMemRatio' field
func (st *ConfigState) GetCacheListIDsMemRatio() (v float64) {
st.mutex.RLock()
v = st.config.Cache.ListEntryMemRatio
v = st.config.Cache.ListIDsMemRatio
st.mutex.RUnlock()
return
}
// SetCacheListEntryMemRatio safely sets the Configuration value for state's 'Cache.ListEntryMemRatio' field
func (st *ConfigState) SetCacheListEntryMemRatio(v float64) {
// SetCacheListIDsMemRatio safely sets the Configuration value for state's 'Cache.ListIDsMemRatio' field
func (st *ConfigState) SetCacheListIDsMemRatio(v float64) {
st.mutex.Lock()
defer st.mutex.Unlock()
st.config.Cache.ListEntryMemRatio = v
st.config.Cache.ListIDsMemRatio = v
st.reloadToViper()
}
// CacheListEntryMemRatioFlag returns the flag name for the 'Cache.ListEntryMemRatio' field
func CacheListEntryMemRatioFlag() string { return "cache-list-entry-mem-ratio" }
// CacheListIDsMemRatioFlag returns the flag name for the 'Cache.ListIDsMemRatio' field
func CacheListIDsMemRatioFlag() string { return "cache-list-ids-mem-ratio" }
// GetCacheListEntryMemRatio safely fetches the value for global configuration 'Cache.ListEntryMemRatio' field
func GetCacheListEntryMemRatio() float64 { return global.GetCacheListEntryMemRatio() }
// GetCacheListIDsMemRatio safely fetches the value for global configuration 'Cache.ListIDsMemRatio' field
func GetCacheListIDsMemRatio() float64 { return global.GetCacheListIDsMemRatio() }
// SetCacheListEntryMemRatio safely sets the value for global configuration 'Cache.ListEntryMemRatio' field
func SetCacheListEntryMemRatio(v float64) { global.SetCacheListEntryMemRatio(v) }
// SetCacheListIDsMemRatio safely sets the value for global configuration 'Cache.ListIDsMemRatio' field
func SetCacheListIDsMemRatio(v float64) { global.SetCacheListIDsMemRatio(v) }
// GetCacheListedIDsMemRatio safely fetches the Configuration value for state's 'Cache.ListedIDsMemRatio' field
func (st *ConfigState) GetCacheListedIDsMemRatio() (v float64) {
st.mutex.RLock()
v = st.config.Cache.ListedIDsMemRatio
st.mutex.RUnlock()
return
}
// SetCacheListedIDsMemRatio safely sets the Configuration value for state's 'Cache.ListedIDsMemRatio' field
func (st *ConfigState) SetCacheListedIDsMemRatio(v float64) {
st.mutex.Lock()
defer st.mutex.Unlock()
st.config.Cache.ListedIDsMemRatio = v
st.reloadToViper()
}
// CacheListedIDsMemRatioFlag returns the flag name for the 'Cache.ListedIDsMemRatio' field
func CacheListedIDsMemRatioFlag() string { return "cache-listed-ids-mem-ratio" }
// GetCacheListedIDsMemRatio safely fetches the value for global configuration 'Cache.ListedIDsMemRatio' field
func GetCacheListedIDsMemRatio() float64 { return global.GetCacheListedIDsMemRatio() }
// SetCacheListedIDsMemRatio safely sets the value for global configuration 'Cache.ListedIDsMemRatio' field
func SetCacheListedIDsMemRatio(v float64) { global.SetCacheListedIDsMemRatio(v) }
// GetCacheMarkerMemRatio safely fetches the Configuration value for state's 'Cache.MarkerMemRatio' field
func (st *ConfigState) GetCacheMarkerMemRatio() (v float64) {
@ -3887,37 +3906,6 @@ func GetCacheTagMemRatio() float64 { return global.GetCacheTagMemRatio() }
// SetCacheTagMemRatio safely sets the value for global configuration 'Cache.TagMemRatio' field
func SetCacheTagMemRatio(v float64) { global.SetCacheTagMemRatio(v) }
// GetCacheTagIDsFollowedByAccountMemRatio safely fetches the Configuration value for state's 'Cache.TagIDsFollowedByAccountMemRatio' field
func (st *ConfigState) GetCacheTagIDsFollowedByAccountMemRatio() (v float64) {
st.mutex.RLock()
v = st.config.Cache.TagIDsFollowedByAccountMemRatio
st.mutex.RUnlock()
return
}
// SetCacheTagIDsFollowedByAccountMemRatio safely sets the Configuration value for state's 'Cache.TagIDsFollowedByAccountMemRatio' field
func (st *ConfigState) SetCacheTagIDsFollowedByAccountMemRatio(v float64) {
st.mutex.Lock()
defer st.mutex.Unlock()
st.config.Cache.TagIDsFollowedByAccountMemRatio = v
st.reloadToViper()
}
// CacheTagIDsFollowedByAccountMemRatioFlag returns the flag name for the 'Cache.TagIDsFollowedByAccountMemRatio' field
func CacheTagIDsFollowedByAccountMemRatioFlag() string {
return "cache-tag-ids-followed-by-account-mem-ratio"
}
// GetCacheTagIDsFollowedByAccountMemRatio safely fetches the value for global configuration 'Cache.TagIDsFollowedByAccountMemRatio' field
func GetCacheTagIDsFollowedByAccountMemRatio() float64 {
return global.GetCacheTagIDsFollowedByAccountMemRatio()
}
// SetCacheTagIDsFollowedByAccountMemRatio safely sets the value for global configuration 'Cache.TagIDsFollowedByAccountMemRatio' field
func SetCacheTagIDsFollowedByAccountMemRatio(v float64) {
global.SetCacheTagIDsFollowedByAccountMemRatio(v)
}
// GetCacheThreadMuteMemRatio safely fetches the Configuration value for state's 'Cache.ThreadMuteMemRatio' field
func (st *ConfigState) GetCacheThreadMuteMemRatio() (v float64) {
st.mutex.RLock()

View File

@ -123,9 +123,6 @@ type Account interface {
// In the case of no statuses, this function will return db.ErrNoEntries.
GetAccountWebStatuses(ctx context.Context, account *gtsmodel.Account, limit int, maxID string) ([]*gtsmodel.Status, error)
// SetAccountHeaderOrAvatar sets the header or avatar for the given accountID to the given media attachment.
SetAccountHeaderOrAvatar(ctx context.Context, mediaAttachment *gtsmodel.MediaAttachment, accountID string) error
// GetInstanceAccount returns the instance account for the given domain.
// If domain is empty, this instance account will be returned.
GetInstanceAccount(ctx context.Context, domain string) (*gtsmodel.Account, error)

View File

@ -64,15 +64,8 @@ func (a *accountDB) GetAccountsByIDs(ctx context.Context, ids []string) ([]*gtsm
accounts, err := a.state.Caches.DB.Account.LoadIDs("ID",
ids,
func(uncached []string) ([]*gtsmodel.Account, error) {
// Avoid querying
// if none uncached.
count := len(uncached)
if count == 0 {
return nil, nil
}
// Preallocate expected length of uncached accounts.
accounts := make([]*gtsmodel.Account, 0, count)
accounts := make([]*gtsmodel.Account, 0, len(uncached))
// Perform database query scanning
// the remaining (uncached) account IDs.
@ -796,20 +789,14 @@ func (a *accountDB) UpdateAccount(ctx context.Context, account *gtsmodel.Account
}
func (a *accountDB) DeleteAccount(ctx context.Context, id string) error {
defer a.state.Caches.DB.Account.Invalidate("ID", id)
// Gather necessary fields from
// deleted for cache invaliation.
var deleted gtsmodel.Account
deleted.ID = id
// Load account into cache before attempting a delete,
// as we need it cached in order to trigger the invalidate
// callback. This in turn invalidates others.
_, err := a.GetAccountByID(gtscontext.SetBarebones(ctx), id)
if err != nil && !errors.Is(err, db.ErrNoEntries) {
// NOTE: even if db.ErrNoEntries is returned, we
// still run the below transaction to ensure related
// objects are appropriately deleted.
return err
}
// Delete account from database and any related links in a transaction.
if err := a.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
return a.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
// clear out any emoji links
if _, err := tx.
NewDelete().
@ -822,44 +809,19 @@ func (a *accountDB) DeleteAccount(ctx context.Context, id string) error {
// delete the account
_, err := tx.
NewDelete().
TableExpr("? AS ?", bun.Ident("accounts"), bun.Ident("account")).
Where("? = ?", bun.Ident("account.id"), id).
Model(&deleted).
Where("? = ?", bun.Ident("id"), id).
Returning("?", bun.Ident("uri")).
Exec(ctx)
return err
})
}
func (a *accountDB) SetAccountHeaderOrAvatar(ctx context.Context, mediaAttachment *gtsmodel.MediaAttachment, accountID string) error {
if *mediaAttachment.Avatar && *mediaAttachment.Header {
return errors.New("one media attachment cannot be both header and avatar")
}
var column bun.Ident
switch {
case *mediaAttachment.Avatar:
column = bun.Ident("account.avatar_media_attachment_id")
case *mediaAttachment.Header:
column = bun.Ident("account.header_media_attachment_id")
default:
return errors.New("given media attachment was neither a header nor an avatar")
}
// TODO: there are probably more side effects here that need to be handled
if _, err := a.db.
NewInsert().
Model(mediaAttachment).
Exec(ctx); err != nil {
}); err != nil {
return err
}
if _, err := a.db.
NewUpdate().
TableExpr("? AS ?", bun.Ident("accounts"), bun.Ident("account")).
Set("? = ?", column, mediaAttachment.ID).
Where("? = ?", bun.Ident("account.id"), accountID).
Exec(ctx); err != nil {
return err
}
// Invalidate cached account by its ID, manually
// call invalidate hook in case not cached.
a.state.Caches.DB.Account.Invalidate("ID", id)
a.state.Caches.OnInvalidateAccount(&deleted)
return nil
}

View File

@ -147,15 +147,8 @@ func (a *applicationDB) GetAllTokens(ctx context.Context) ([]*gtsmodel.Token, er
tokens, err := a.state.Caches.DB.Token.LoadIDs("ID",
tokenIDs,
func(uncached []string) ([]*gtsmodel.Token, error) {
// Avoid querying
// if none uncached.
count := len(uncached)
if count == 0 {
return nil, nil
}
// Preallocate expected length of uncached tokens.
tokens := make([]*gtsmodel.Token, 0, count)
tokens := make([]*gtsmodel.Token, 0, len(uncached))
// Perform database query scanning
// the remaining (uncached) token IDs.

View File

@ -188,15 +188,8 @@ func (c *conversationDB) getConversationsByLastStatusIDs(
accountID,
conversationLastStatusIDs,
func(accountID string, uncached []string) ([]*gtsmodel.Conversation, error) {
// Avoid querying
// if none uncached.
count := len(uncached)
if count == 0 {
return nil, nil
}
// Preallocate expected length of uncached conversations.
conversations := make([]*gtsmodel.Conversation, 0, count)
conversations := make([]*gtsmodel.Conversation, 0, len(uncached))
// Perform database query scanning the remaining (uncached) IDs.
if err := c.db.NewSelect().
@ -267,27 +260,27 @@ func (c *conversationDB) LinkConversationToStatus(ctx context.Context, conversat
}
func (c *conversationDB) DeleteConversationByID(ctx context.Context, id string) error {
// Load conversation into cache before attempting a delete,
// as we need it cached in order to trigger the invalidate
// callback. This in turn invalidates others.
_, err := c.GetConversationByID(gtscontext.SetBarebones(ctx), id)
if err != nil {
if errors.Is(err, db.ErrNoEntries) {
// not an issue.
err = nil
}
// Gather necessary fields from
// deleted for cache invaliation.
var deleted gtsmodel.Conversation
deleted.ID = id
// Delete conversation from DB.
if _, err := c.db.NewDelete().
Model(&deleted).
Where("? = ?", bun.Ident("id"), id).
Returning("?", bun.Ident("account_id")).
Exec(ctx); err != nil &&
!errors.Is(err, db.ErrNoEntries) {
return err
}
// Drop this now-cached conversation on return after delete.
defer c.state.Caches.DB.Conversation.Invalidate("ID", id)
// Invalidate cached conversation by ID,
// manually invalidate hook in case not cached.
c.state.Caches.DB.Conversation.Invalidate("ID", id)
c.state.Caches.OnInvalidateConversation(&deleted)
// Finally delete conversation from DB.
_, err = c.db.NewDelete().
Model((*gtsmodel.Conversation)(nil)).
Where("? = ?", bun.Ident("id"), id).
Exec(ctx)
return err
return nil
}
func (c *conversationDB) DeleteConversationsByOwnerAccountID(ctx context.Context, accountID string) error {

View File

@ -20,7 +20,6 @@ package bundb
import (
"context"
"database/sql"
"errors"
"slices"
"strings"
"time"
@ -70,34 +69,15 @@ func (e *emojiDB) UpdateEmoji(ctx context.Context, emoji *gtsmodel.Emoji, column
func (e *emojiDB) DeleteEmojiByID(ctx context.Context, id string) error {
var (
// Gather necessary fields from
// deleted for cache invaliation.
accountIDs []string
statusIDs []string
)
defer func() {
// Invalidate cached emoji.
e.state.Caches.DB.Emoji.Invalidate("ID", id)
// Delete the emoji and all related links to it in a singular transaction.
if err := e.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
// Invalidate cached account and status IDs.
e.state.Caches.DB.Account.InvalidateIDs("ID", accountIDs)
e.state.Caches.DB.Status.InvalidateIDs("ID", statusIDs)
}()
// Load emoji into cache before attempting a delete,
// as we need it cached in order to trigger the invalidate
// callback. This in turn invalidates others.
_, err := e.GetEmojiByID(
gtscontext.SetBarebones(ctx),
id,
)
if err != nil && !errors.Is(err, db.ErrNoEntries) {
// NOTE: even if db.ErrNoEntries is returned, we
// still run the below transaction to ensure related
// objects are appropriately deleted.
return err
}
return e.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
// Delete relational links between this emoji
// and any statuses using it, returning the
// status IDs so we can later update them.
@ -195,7 +175,16 @@ func (e *emojiDB) DeleteEmojiByID(ctx context.Context, id string) error {
}
return nil
})
}); err != nil {
return err
}
// Invalidate emoji, and any effected statuses / accounts.
e.state.Caches.DB.Emoji.Invalidate("ID", id)
e.state.Caches.DB.Account.InvalidateIDs("ID", accountIDs)
e.state.Caches.DB.Status.InvalidateIDs("ID", statusIDs)
return nil
}
func (e *emojiDB) GetEmojisBy(ctx context.Context, domain string, includeDisabled bool, includeEnabled bool, shortcode string, maxShortcodeDomain string, minShortcodeDomain string, limit int) ([]*gtsmodel.Emoji, error) {
@ -586,15 +575,8 @@ func (e *emojiDB) GetEmojisByIDs(ctx context.Context, ids []string) ([]*gtsmodel
emojis, err := e.state.Caches.DB.Emoji.LoadIDs("ID",
ids,
func(uncached []string) ([]*gtsmodel.Emoji, error) {
// Avoid querying
// if none uncached.
count := len(uncached)
if count == 0 {
return nil, nil
}
// Preallocate expected length of uncached emojis.
emojis := make([]*gtsmodel.Emoji, 0, count)
emojis := make([]*gtsmodel.Emoji, 0, len(uncached))
// Perform database query scanning
// the remaining (uncached) IDs.
@ -657,15 +639,8 @@ func (e *emojiDB) GetEmojiCategoriesByIDs(ctx context.Context, ids []string) ([]
categories, err := e.state.Caches.DB.EmojiCategory.LoadIDs("ID",
ids,
func(uncached []string) ([]*gtsmodel.EmojiCategory, error) {
// Avoid querying
// if none uncached.
count := len(uncached)
if count == 0 {
return nil, nil
}
// Preallocate expected length of uncached categories.
categories := make([]*gtsmodel.EmojiCategory, 0, count)
categories := make([]*gtsmodel.EmojiCategory, 0, len(uncached))
// Perform database query scanning
// the remaining (uncached) IDs.

View File

@ -83,14 +83,7 @@ func (f *filterDB) GetFiltersForAccountID(ctx context.Context, accountID string)
filters, err := f.state.Caches.DB.Filter.LoadIDs("ID",
filterIDs,
func(uncached []string) ([]*gtsmodel.Filter, error) {
// Avoid querying
// if none uncached.
count := len(uncached)
if count == 0 {
return nil, nil
}
filters := make([]*gtsmodel.Filter, 0, count)
filters := make([]*gtsmodel.Filter, 0, len(uncached))
if err := f.db.
NewSelect().
Model(&filters).

View File

@ -113,14 +113,8 @@ func (f *filterDB) getFilterKeywords(ctx context.Context, idColumn string, id st
filterKeywords, err := f.state.Caches.DB.FilterKeyword.LoadIDs("ID",
filterKeywordIDs,
func(uncached []string) ([]*gtsmodel.FilterKeyword, error) {
// Avoid querying
// if none uncached.
count := len(uncached)
if count == 0 {
return nil, nil
}
filterKeywords := make([]*gtsmodel.FilterKeyword, 0, len(uncached))
filterKeywords := make([]*gtsmodel.FilterKeyword, 0, count)
if err := f.db.
NewSelect().
Model(&filterKeywords).

View File

@ -100,14 +100,7 @@ func (f *filterDB) getFilterStatuses(ctx context.Context, idColumn string, id st
filterStatuses, err := f.state.Caches.DB.FilterStatus.LoadIDs("ID",
filterStatusIDs,
func(uncached []string) ([]*gtsmodel.FilterStatus, error) {
// Avoid querying
// if none uncached.
count := len(uncached)
if count == 0 {
return nil, nil
}
filterStatuses := make([]*gtsmodel.FilterStatus, 0, count)
filterStatuses := make([]*gtsmodel.FilterStatus, 0, len(uncached))
if err := f.db.
NewSelect().
Model(&filterStatuses).

View File

@ -29,6 +29,7 @@ import (
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/internal/log"
"github.com/superseriousbusiness/gotosocial/internal/paging"
"github.com/superseriousbusiness/gotosocial/internal/state"
"github.com/superseriousbusiness/gotosocial/internal/util"
"github.com/uptrace/bun"
@ -85,39 +86,52 @@ func (l *listDB) getList(ctx context.Context, lookup string, dbQuery func(*gtsmo
return list, nil
}
func (l *listDB) GetListsForAccountID(ctx context.Context, accountID string) ([]*gtsmodel.List, error) {
// Fetch IDs of all lists owned by this account.
var listIDs []string
if err := l.db.
NewSelect().
TableExpr("? AS ?", bun.Ident("lists"), bun.Ident("list")).
Column("list.id").
Where("? = ?", bun.Ident("list.account_id"), accountID).
Order("list.id DESC").
Scan(ctx, &listIDs); err != nil {
func (l *listDB) GetListsByAccountID(ctx context.Context, accountID string) ([]*gtsmodel.List, error) {
listIDs, err := l.getListIDsByAccountID(ctx, accountID)
if err != nil {
return nil, err
}
if len(listIDs) == 0 {
return nil, nil
}
// Return lists by their IDs.
return l.GetListsByIDs(ctx, listIDs)
}
func (l *listDB) CountListsForAccountID(ctx context.Context, accountID string) (int, error) {
return l.db.
NewSelect().
Table("lists").
Where("? = ?", bun.Ident("account_id"), accountID).
Count(ctx)
func (l *listDB) CountListsByAccountID(ctx context.Context, accountID string) (int, error) {
listIDs, err := l.getListIDsByAccountID(ctx, accountID)
return len(listIDs), err
}
func (l *listDB) GetListsContainingFollowID(ctx context.Context, followID string) ([]*gtsmodel.List, error) {
listIDs, err := l.getListIDsWithFollowID(ctx, followID)
if err != nil {
return nil, err
}
return l.GetListsByIDs(ctx, listIDs)
}
func (l *listDB) GetFollowsInList(ctx context.Context, listID string, page *paging.Page) ([]*gtsmodel.Follow, error) {
followIDs, err := l.GetFollowIDsInList(ctx, listID, page)
if err != nil {
return nil, err
}
return l.state.DB.GetFollowsByIDs(ctx, followIDs)
}
func (l *listDB) GetAccountsInList(ctx context.Context, listID string, page *paging.Page) ([]*gtsmodel.Account, error) {
accountIDs, err := l.GetAccountIDsInList(ctx, listID, page)
if err != nil {
return nil, err
}
return l.state.DB.GetAccountsByIDs(ctx, accountIDs)
}
func (l *listDB) IsAccountInList(ctx context.Context, listID string, accountID string) (bool, error) {
accountIDs, err := l.GetAccountIDsInList(ctx, listID, nil)
return slices.Contains(accountIDs, accountID), err
}
func (l *listDB) PopulateList(ctx context.Context, list *gtsmodel.List) error {
var (
err error
errs = gtserror.NewMultiError(2)
errs gtserror.MultiError
)
if list.Account == nil {
@ -131,22 +145,12 @@ func (l *listDB) PopulateList(ctx context.Context, list *gtsmodel.List) error {
}
}
if list.ListEntries == nil {
// List entries are not set, fetch from the database.
list.ListEntries, err = l.state.DB.GetListEntries(
gtscontext.SetBarebones(ctx),
list.ID,
"", "", "", 0,
)
if err != nil {
errs.Appendf("error populating list entries: %w", err)
}
}
return errs.Combine()
}
func (l *listDB) PutList(ctx context.Context, list *gtsmodel.List) error {
// note that inserting list will call OnInvalidateList()
// which will handle clearing caches other than List cache.
return l.state.Caches.DB.List.Store(list, func() error {
_, err := l.db.NewInsert().Model(list).Exec(ctx)
return err
@ -160,192 +164,146 @@ func (l *listDB) UpdateList(ctx context.Context, list *gtsmodel.List, columns ..
columns = append(columns, "updated_at")
}
defer func() {
// Invalidate all entries for this list ID.
l.state.Caches.DB.ListEntry.Invalidate("ListID", list.ID)
// Invalidate this entire list's timeline.
if err := l.state.Timelines.List.RemoveTimeline(ctx, list.ID); err != nil {
log.Errorf(ctx, "error invalidating list timeline: %q", err)
}
}()
return l.state.Caches.DB.List.Store(list, func() error {
// Update list in the database, invalidating main list cache.
if err := l.state.Caches.DB.List.Store(list, func() error {
_, err := l.db.NewUpdate().
Model(list).
Where("? = ?", bun.Ident("list.id"), list.ID).
Column(columns...).
Exec(ctx)
return err
})
}); err != nil {
return err
}
// Invalidate this entire list's timeline.
if err := l.state.Timelines.List.RemoveTimeline(ctx, list.ID); err != nil {
log.Errorf(ctx, "error invalidating list timeline: %q", err)
}
return nil
}
func (l *listDB) DeleteListByID(ctx context.Context, id string) error {
// Load list by ID into cache to ensure we can perform
// all necessary cache invalidation hooks on removal.
_, err := l.GetListByID(
// Don't populate the entry;
// we only want the list ID.
gtscontext.SetBarebones(ctx),
id,
)
if err != nil && !errors.Is(err, db.ErrNoEntries) {
// NOTE: even if db.ErrNoEntries is returned, we
// still run the below transaction to ensure related
// objects are appropriately deleted.
return err
}
// Acquire list owner ID.
var accountID string
defer func() {
// Invalidate this list from cache.
l.state.Caches.DB.List.Invalidate("ID", id)
// Gather follow IDs of all
// entries contained in list.
var followIDs []string
// Invalidate this entire list's timeline.
if err := l.state.Timelines.List.RemoveTimeline(ctx, id); err != nil {
log.Errorf(ctx, "error invalidating list timeline: %q", err)
}
}()
return l.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
// Delete all entries attached to list.
// Delete all list entries associated with list, and list itself in transaction.
if err := l.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
if _, err := tx.NewDelete().
Table("list_entries").
Where("? = ?", bun.Ident("list_id"), id).
Exec(ctx); err != nil {
Returning("?", bun.Ident("follow_id")).
Exec(ctx, &followIDs); err != nil {
return err
}
// Delete the list itself.
_, err := tx.NewDelete().
Table("lists").
Where("? = ?", bun.Ident("id"), id).
Exec(ctx)
Returning("?", bun.Ident("account_id")).
Exec(ctx, &accountID)
return err
})
}); err != nil {
return err
}
// Invalidate the main list database cache.
l.state.Caches.DB.List.Invalidate("ID", id)
// Invalidate cache of list IDs owned by account.
l.state.Caches.DB.ListIDs.Invalidate("a" + accountID)
// Invalidate all related entry caches for this list.
l.invalidateEntryCaches(ctx, []string{id}, followIDs)
return nil
}
/*
LIST ENTRY functions
*/
func (l *listDB) getListIDsByAccountID(ctx context.Context, accountID string) ([]string, error) {
return l.state.Caches.DB.ListIDs.Load("a"+accountID, func() ([]string, error) {
var listIDs []string
func (l *listDB) GetListEntryByID(ctx context.Context, id string) (*gtsmodel.ListEntry, error) {
return l.getListEntry(
ctx,
"ID",
func(listEntry *gtsmodel.ListEntry) error {
return l.db.NewSelect().
Model(listEntry).
Where("? = ?", bun.Ident("list_entry.id"), id).
Scan(ctx)
},
id,
)
}
func (l *listDB) getListEntry(ctx context.Context, lookup string, dbQuery func(*gtsmodel.ListEntry) error, keyParts ...any) (*gtsmodel.ListEntry, error) {
listEntry, err := l.state.Caches.DB.ListEntry.LoadOne(lookup, func() (*gtsmodel.ListEntry, error) {
var listEntry gtsmodel.ListEntry
// Not cached! Perform database query.
if err := dbQuery(&listEntry); err != nil {
// List IDs not in cache.
// Perform the DB query.
if _, err := l.db.NewSelect().
Table("lists").
Column("id").
Where("? = ?", bun.Ident("account_id"), accountID).
OrderExpr("? DESC", bun.Ident("created_at")).
Exec(ctx, &listIDs); err != nil &&
!errors.Is(err, db.ErrNoEntries) {
return nil, err
}
return &listEntry, nil
}, keyParts...)
if err != nil {
return nil, err // already processed
}
if gtscontext.Barebones(ctx) {
// Only a barebones model was requested.
return listEntry, nil
}
// Further populate the list entry fields where applicable.
if err := l.state.DB.PopulateListEntry(ctx, listEntry); err != nil {
return nil, err
}
return listEntry, nil
return listIDs, nil
})
}
func (l *listDB) GetListEntries(ctx context.Context,
listID string,
maxID string,
sinceID string,
minID string,
limit int,
) ([]*gtsmodel.ListEntry, error) {
// Ensure reasonable
if limit < 0 {
limit = 0
}
func (l *listDB) getListIDsWithFollowID(ctx context.Context, followID string) ([]string, error) {
return l.state.Caches.DB.ListIDs.Load("f"+followID, func() ([]string, error) {
var listIDs []string
// Make educated guess for slice size
var (
entryIDs = make([]string, 0, limit)
frontToBack = true
)
q := l.db.
NewSelect().
TableExpr("? AS ?", bun.Ident("list_entries"), bun.Ident("entry")).
// Select only IDs from table
Column("entry.id").
// Select only entries belonging to listID.
Where("? = ?", bun.Ident("entry.list_id"), listID)
if maxID != "" {
// return only entries LOWER (ie., older) than maxID
q = q.Where("? < ?", bun.Ident("entry.id"), maxID)
}
if sinceID != "" {
// return only entries HIGHER (ie., newer) than sinceID
q = q.Where("? > ?", bun.Ident("entry.id"), sinceID)
}
if minID != "" {
// return only entries HIGHER (ie., newer) than minID
q = q.Where("? > ?", bun.Ident("entry.id"), minID)
// page up
frontToBack = false
}
if limit > 0 {
// limit amount of entries returned
q = q.Limit(limit)
}
if frontToBack {
// Page down.
q = q.Order("entry.id DESC")
} else {
// Page up.
q = q.Order("entry.id ASC")
}
if err := q.Scan(ctx, &entryIDs); err != nil {
return nil, err
}
if len(entryIDs) == 0 {
return nil, nil
}
// If we're paging up, we still want entries
// to be sorted by ID desc, so reverse ids slice.
// https://zchee.github.io/golang-wiki/SliceTricks/#reversing
if !frontToBack {
for l, r := 0, len(entryIDs)-1; l < r; l, r = l+1, r-1 {
entryIDs[l], entryIDs[r] = entryIDs[r], entryIDs[l]
// List IDs not in cache.
// Perform the DB query.
if _, err := l.db.NewSelect().
Table("list_entries").
Column("list_id").
Where("? = ?", bun.Ident("follow_id"), followID).
OrderExpr("? DESC", bun.Ident("created_at")).
Exec(ctx, &listIDs); err != nil &&
!errors.Is(err, db.ErrNoEntries) {
return nil, err
}
}
// Return list entries by their IDs.
return l.GetListEntriesByIDs(ctx, entryIDs)
return listIDs, nil
})
}
func (l *listDB) GetFollowIDsInList(ctx context.Context, listID string, page *paging.Page) ([]string, error) {
return loadPagedIDs(&l.state.Caches.DB.ListedIDs, "f"+listID, page, func() ([]string, error) {
var followIDs []string
// Follow IDs not in cache.
// Perform the DB query.
_, err := l.db.NewSelect().
Table("list_entries").
Column("follow_id").
Where("? = ?", bun.Ident("list_id"), listID).
OrderExpr("? DESC", bun.Ident("created_at")).
Exec(ctx, &followIDs)
if err != nil && !errors.Is(err, db.ErrNoEntries) {
return nil, err
}
return followIDs, nil
})
}
func (l *listDB) GetAccountIDsInList(ctx context.Context, listID string, page *paging.Page) ([]string, error) {
return loadPagedIDs(&l.state.Caches.DB.ListedIDs, "a"+listID, page, func() ([]string, error) {
var accountIDs []string
// Account IDs not in cache.
// Perform the DB query.
_, err := l.db.NewSelect().
Table("follows").
Column("follows.target_account_id").
Join("INNER JOIN ?", bun.Ident("list_entries")).
JoinOn("? = ?", bun.Ident("follows.id"), bun.Ident("list_entries.follow_id")).
Where("? = ?", bun.Ident("list_entries.list_id"), listID).
OrderExpr("? DESC", bun.Ident("list_entries.id")).
Exec(ctx, &accountIDs)
if err != nil && !errors.Is(err, db.ErrNoEntries) {
return nil, err
}
return accountIDs, nil
})
}
func (l *listDB) GetListsByIDs(ctx context.Context, ids []string) ([]*gtsmodel.List, error) {
@ -353,15 +311,8 @@ func (l *listDB) GetListsByIDs(ctx context.Context, ids []string) ([]*gtsmodel.L
lists, err := l.state.Caches.DB.List.LoadIDs("ID",
ids,
func(uncached []string) ([]*gtsmodel.List, error) {
// Avoid querying
// if none uncached.
count := len(uncached)
if count == 0 {
return nil, nil
}
// Preallocate expected length of uncached lists.
lists := make([]*gtsmodel.List, 0, count)
lists := make([]*gtsmodel.List, 0, len(uncached))
// Perform database query scanning
// the remaining (uncached) IDs.
@ -402,82 +353,6 @@ func (l *listDB) GetListsByIDs(ctx context.Context, ids []string) ([]*gtsmodel.L
return lists, nil
}
func (l *listDB) GetListEntriesByIDs(ctx context.Context, ids []string) ([]*gtsmodel.ListEntry, error) {
// Load all entry IDs via cache loader callbacks.
entries, err := l.state.Caches.DB.ListEntry.LoadIDs("ID",
ids,
func(uncached []string) ([]*gtsmodel.ListEntry, error) {
// Avoid querying
// if none uncached.
count := len(uncached)
if count == 0 {
return nil, nil
}
// Preallocate expected length of uncached entries.
entries := make([]*gtsmodel.ListEntry, 0, count)
// Perform database query scanning
// the remaining (uncached) IDs.
if err := l.db.NewSelect().
Model(&entries).
Where("? IN (?)", bun.Ident("id"), bun.In(uncached)).
Scan(ctx); err != nil {
return nil, err
}
return entries, nil
},
)
if err != nil {
return nil, err
}
// Reorder the entries by their
// IDs to ensure in correct order.
getID := func(e *gtsmodel.ListEntry) string { return e.ID }
util.OrderBy(entries, ids, getID)
if gtscontext.Barebones(ctx) {
// no need to fully populate.
return entries, nil
}
// Populate all loaded entries, removing those we fail to
// populate (removes needing so many nil checks everywhere).
entries = slices.DeleteFunc(entries, func(entry *gtsmodel.ListEntry) bool {
if err := l.PopulateListEntry(ctx, entry); err != nil {
log.Errorf(ctx, "error populating entry %s: %v", entry.ID, err)
return true
}
return false
})
return entries, nil
}
func (l *listDB) GetListEntriesForFollowID(ctx context.Context, followID string) ([]*gtsmodel.ListEntry, error) {
var entryIDs []string
if err := l.db.
NewSelect().
TableExpr("? AS ?", bun.Ident("list_entries"), bun.Ident("entry")).
// Select only IDs from table
Column("entry.id").
// Select only entries belonging with given followID.
Where("? = ?", bun.Ident("entry.follow_id"), followID).
Scan(ctx, &entryIDs); err != nil {
return nil, err
}
if len(entryIDs) == 0 {
return nil, nil
}
// Return list entries by their IDs.
return l.GetListEntriesByIDs(ctx, entryIDs)
}
func (l *listDB) PopulateListEntry(ctx context.Context, listEntry *gtsmodel.ListEntry) error {
var err error
@ -496,109 +371,111 @@ func (l *listDB) PopulateListEntry(ctx context.Context, listEntry *gtsmodel.List
}
func (l *listDB) PutListEntries(ctx context.Context, entries []*gtsmodel.ListEntry) error {
defer func() {
// Collect unique list IDs from the provided entries.
listIDs := util.Collate(entries, func(e *gtsmodel.ListEntry) string {
return e.ListID
})
for _, id := range listIDs {
// Invalidate the timeline for the list this entry belongs to.
if err := l.state.Timelines.List.RemoveTimeline(ctx, id); err != nil {
log.Errorf(ctx, "error invalidating list timeline: %q", err)
}
}
}()
// Finally, insert each list entry into the database.
return l.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
// Insert all entries into the database in a single transaction (all or nothing!).
if err := l.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
for _, entry := range entries {
entry := entry // rescope
if err := l.state.Caches.DB.ListEntry.Store(entry, func() error {
_, err := tx.
NewInsert().
Model(entry).
Exec(ctx)
return err
}); err != nil {
if _, err := tx.
NewInsert().
Model(entry).
Exec(ctx); err != nil {
return err
}
}
return nil
}); err != nil {
return err
}
// Collect unique list IDs from the provided list entries.
listIDs := util.Collate(entries, func(e *gtsmodel.ListEntry) string {
return e.ListID
})
}
func (l *listDB) DeleteListEntry(ctx context.Context, id string) error {
// Load list entry into cache to ensure we can perform
// all necessary cache invalidation hooks on removal.
entry, err := l.GetListEntryByID(
// Don't populate the entry;
// we only want the list ID.
gtscontext.SetBarebones(ctx),
id,
)
if err != nil {
if errors.Is(err, db.ErrNoEntries) {
// Already gone.
return nil
}
return err
}
// Collect unique follow IDs from the provided list entries.
followIDs := util.Collate(entries, func(e *gtsmodel.ListEntry) string {
return e.FollowID
})
defer func() {
// Invalidate this list entry upon delete.
l.state.Caches.DB.ListEntry.Invalidate("ID", id)
// Invalidate the timeline for the list this entry belongs to.
if err := l.state.Timelines.List.RemoveTimeline(ctx, entry.ListID); err != nil {
log.Errorf(ctx, "error invalidating list timeline: %q", err)
}
}()
// Finally delete the list entry.
_, err = l.db.NewDelete().
Table("list_entries").
Where("? = ?", bun.Ident("id"), id).
Exec(ctx)
return err
}
func (l *listDB) DeleteListEntriesForFollowID(ctx context.Context, followID string) error {
var entryIDs []string
// Fetch entry IDs for follow ID.
if err := l.db.
NewSelect().
Table("list_entries").
Column("id").
Where("? = ?", bun.Ident("follow_id"), followID).
Order("id DESC").
Scan(ctx, &entryIDs); err != nil {
return err
}
for _, id := range entryIDs {
// Delete each separately to trigger cache invalidations.
if err := l.DeleteListEntry(ctx, id); err != nil {
return err
}
}
// Invalidate all related list entry caches.
l.invalidateEntryCaches(ctx, listIDs, followIDs)
return nil
}
func (l *listDB) ListIncludesAccount(ctx context.Context, listID string, accountID string) (bool, error) {
exists, err := l.db.
NewSelect().
TableExpr("? AS ?", bun.Ident("list_entries"), bun.Ident("list_entry")).
Join(
"JOIN ? AS ? ON ? = ?",
bun.Ident("follows"), bun.Ident("follow"),
bun.Ident("list_entry.follow_id"), bun.Ident("follow.id"),
).
Where("? = ?", bun.Ident("list_entry.list_id"), listID).
Where("? = ?", bun.Ident("follow.target_account_id"), accountID).
Exists(ctx)
func (l *listDB) DeleteListEntry(ctx context.Context, listID string, followID string) error {
// Delete list entry with given
// ID, returning its list ID.
if _, err := l.db.NewDelete().
Table("list_entries").
Where("? = ?", bun.Ident("list_id"), listID).
Where("? = ?", bun.Ident("follow_id"), followID).
Exec(ctx, &listID); err != nil &&
!errors.Is(err, db.ErrNoEntries) {
return err
}
return exists, err
// Invalidate all related list entry caches.
l.invalidateEntryCaches(ctx, []string{listID},
[]string{followID})
return nil
}
func (l *listDB) DeleteAllListEntriesByFollows(ctx context.Context, followIDs ...string) error {
var listIDs []string
// Check for empty list.
if len(followIDs) == 0 {
return nil
}
// Delete all entries with follow
// ID, returning IDs and list IDs.
if _, err := l.db.NewDelete().
Table("list_entries").
Where("? IN (?)", bun.Ident("follow_id"), bun.In(followIDs)).
Returning("?", bun.Ident("list_id")).
Exec(ctx, &listIDs); err != nil &&
!errors.Is(err, db.ErrNoEntries) {
return err
}
// Deduplicate IDs before invalidate.
listIDs = util.Deduplicate(listIDs)
// Invalidate all related list entry caches.
l.invalidateEntryCaches(ctx, listIDs, followIDs)
return nil
}
// invalidateEntryCaches will invalidate all related ListEntry caches for given list IDs and follow IDs, including timelines.
func (l *listDB) invalidateEntryCaches(ctx context.Context, listIDs, followIDs []string) {
var keys []string
// Generate ListedID keys to invalidate.
keys = slices.Grow(keys[:0], 2*len(listIDs))
for _, listID := range listIDs {
keys = append(keys,
"a"+listID,
"f"+listID,
)
// Invalidate the timeline for the list this entry belongs to.
if err := l.state.Timelines.List.RemoveTimeline(ctx, listID); err != nil {
log.Errorf(ctx, "error invalidating list timeline: %q", err)
}
}
// Invalidate ListedID slice cache entries.
l.state.Caches.DB.ListedIDs.Invalidate(keys...)
// Generate ListID keys to invalidate.
keys = slices.Grow(keys[:0], len(followIDs))
for _, followID := range followIDs {
keys = append(keys, "f"+followID)
}
// Invalidate ListID slice cache entries.
l.state.Caches.DB.ListIDs.Invalidate(keys...)
}

View File

@ -24,7 +24,6 @@ import (
"github.com/stretchr/testify/suite"
"github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/gtscontext"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
)
@ -32,7 +31,7 @@ type ListTestSuite struct {
BunDBStandardTestSuite
}
func (suite *ListTestSuite) testStructs() (*gtsmodel.List, *gtsmodel.Account) {
func (suite *ListTestSuite) testStructs() (*gtsmodel.List, []*gtsmodel.ListEntry, *gtsmodel.Account) {
testList := &gtsmodel.List{}
*testList = *suite.testLists["local_account_1_list_1"]
@ -55,12 +54,10 @@ func (suite *ListTestSuite) testStructs() (*gtsmodel.List, *gtsmodel.Account) {
}
})
testList.ListEntries = entries
testAccount := &gtsmodel.Account{}
*testAccount = *suite.testAccounts["local_account_1"]
return testList, testAccount
return testList, entries, testAccount
}
func (suite *ListTestSuite) checkList(expected *gtsmodel.List, actual *gtsmodel.List) {
@ -103,7 +100,7 @@ func (suite *ListTestSuite) checkListEntries(expected []*gtsmodel.ListEntry, act
}
func (suite *ListTestSuite) TestGetListByID() {
testList, _ := suite.testStructs()
testList, _, _ := suite.testStructs()
dbList, err := suite.db.GetListByID(context.Background(), testList.ID)
if err != nil {
@ -111,13 +108,12 @@ func (suite *ListTestSuite) TestGetListByID() {
}
suite.checkList(testList, dbList)
suite.checkListEntries(testList.ListEntries, dbList.ListEntries)
}
func (suite *ListTestSuite) TestGetListsForAccountID() {
testList, testAccount := suite.testStructs()
testList, _, testAccount := suite.testStructs()
dbLists, err := suite.db.GetListsForAccountID(context.Background(), testAccount.ID)
dbLists, err := suite.db.GetListsByAccountID(context.Background(), testAccount.ID)
if err != nil {
suite.FailNow(err.Error())
}
@ -129,20 +125,9 @@ func (suite *ListTestSuite) TestGetListsForAccountID() {
suite.checkList(testList, dbLists[0])
}
func (suite *ListTestSuite) TestGetListEntries() {
testList, _ := suite.testStructs()
dbListEntries, err := suite.db.GetListEntries(context.Background(), testList.ID, "", "", "", 0)
if err != nil {
suite.FailNow(err.Error())
}
suite.checkListEntries(testList.ListEntries, dbListEntries)
}
func (suite *ListTestSuite) TestPutList() {
ctx := context.Background()
_, testAccount := suite.testStructs()
_, _, testAccount := suite.testStructs()
testList := &gtsmodel.List{
ID: "01H0J2PMYM54618VCV8Y8QYAT4",
@ -166,7 +151,7 @@ func (suite *ListTestSuite) TestPutList() {
func (suite *ListTestSuite) TestUpdateList() {
ctx := context.Background()
testList, _ := suite.testStructs()
testList, _, _ := suite.testStructs()
// Get List in the cache first.
dbList, err := suite.db.GetListByID(ctx, testList.ID)
@ -192,7 +177,7 @@ func (suite *ListTestSuite) TestUpdateList() {
func (suite *ListTestSuite) TestDeleteList() {
ctx := context.Background()
testList, _ := suite.testStructs()
testList, _, _ := suite.testStructs()
// Get List in the cache first.
if _, err := suite.db.GetListByID(ctx, testList.ID); err != nil {
@ -209,18 +194,19 @@ func (suite *ListTestSuite) TestDeleteList() {
_, err := suite.db.GetListByID(ctx, testList.ID)
suite.ErrorIs(err, db.ErrNoEntries)
// All entries belonging to this
// list should now be deleted.
listEntries, err := suite.db.GetListEntries(ctx, testList.ID, "", "", "", 0)
if err != nil {
suite.FailNow(err.Error())
}
suite.Empty(listEntries)
// All accounts / follows attached to this
// list should now be return empty values.
listAccounts, err1 := suite.db.GetAccountsInList(ctx, testList.ID, nil)
listFollows, err2 := suite.db.GetFollowsInList(ctx, testList.ID, nil)
suite.NoError(err1)
suite.NoError(err2)
suite.Empty(listAccounts)
suite.Empty(listFollows)
}
func (suite *ListTestSuite) TestPutListEntries() {
ctx := context.Background()
testList, _ := suite.testStructs()
testList, testEntries, _ := suite.testStructs()
listEntries := []*gtsmodel.ListEntry{
{
@ -244,91 +230,58 @@ func (suite *ListTestSuite) TestPutListEntries() {
suite.FailNow(err.Error())
}
// Add these entries to the test list, sort it again
// to reflect what we'd expect to get from the db.
testList.ListEntries = append(testList.ListEntries, listEntries...)
slices.SortFunc(testList.ListEntries, func(a, b *gtsmodel.ListEntry) int {
const k = -1
switch {
case a.ID > b.ID:
return +k
case a.ID < b.ID:
return -k
default:
return 0
}
})
// Now get all list entries from the db.
// Use barebones for this because the ones
// we just added will fail if we try to get
// the nonexistent follows.
dbListEntries, err := suite.db.GetListEntries(
gtscontext.SetBarebones(ctx),
testList.ID,
"", "", "", 0)
if err != nil {
suite.FailNow(err.Error())
}
suite.checkListEntries(testList.ListEntries, dbListEntries)
// Get all follows stored under this list ID, to ensure
// the newly added list entry follows are among these.
followIDs, err := suite.db.GetFollowIDsInList(ctx, testList.ID, nil)
suite.NoError(err)
suite.Len(followIDs, len(testEntries)+len(listEntries))
suite.Contains(followIDs, "01H0MKNFRFZS8R9WV6DBX31Y03")
suite.Contains(followIDs, "01H0MKP6RR8VEHN3GVWFBP2H30")
suite.Contains(followIDs, "01H0MKQ0KA29C6NFJ27GTZD16J")
}
func (suite *ListTestSuite) TestDeleteListEntry() {
ctx := context.Background()
testList, _ := suite.testStructs()
// Get List in the cache first.
if _, err := suite.db.GetListByID(ctx, testList.ID); err != nil {
suite.FailNow(err.Error())
}
testList, testEntries, _ := suite.testStructs()
// Delete the first entry.
if err := suite.db.DeleteListEntry(ctx, testList.ListEntries[0].ID); err != nil {
if err := suite.db.DeleteListEntry(ctx,
testEntries[0].ListID,
testEntries[0].FollowID,
); err != nil {
suite.FailNow(err.Error())
}
// Get list from the db again.
dbList, err := suite.db.GetListByID(ctx, testList.ID)
if err != nil {
suite.FailNow(err.Error())
}
// Bodge the testlist as though
// we'd removed the first entry.
testList.ListEntries = testList.ListEntries[1:]
suite.checkList(testList, dbList)
// Get all follows stored under this list ID, to ensure
// the newly removed list entry follow is now missing.
followIDs, err := suite.db.GetFollowIDsInList(ctx, testList.ID, nil)
suite.NoError(err)
suite.Len(followIDs, len(testEntries)-1)
suite.NotContains(followIDs, testEntries[0].FollowID)
}
func (suite *ListTestSuite) TestDeleteListEntriesForFollowID() {
func (suite *ListTestSuite) TestDeleteAllListEntriesByFollows() {
ctx := context.Background()
testList, _ := suite.testStructs()
// Get List in the cache first.
if _, err := suite.db.GetListByID(ctx, testList.ID); err != nil {
suite.FailNow(err.Error())
}
testList, testEntries, _ := suite.testStructs()
// Delete the first entry.
if err := suite.db.DeleteListEntriesForFollowID(ctx, testList.ListEntries[0].FollowID); err != nil {
if err := suite.db.DeleteAllListEntriesByFollows(ctx,
testEntries[0].FollowID,
); err != nil {
suite.FailNow(err.Error())
}
// Get list from the db again.
dbList, err := suite.db.GetListByID(ctx, testList.ID)
if err != nil {
suite.FailNow(err.Error())
}
// Bodge the testlist as though
// we'd removed the first entry.
testList.ListEntries = testList.ListEntries[1:]
suite.checkList(testList, dbList)
// Get all follows stored under this list ID, to ensure
// the newly removed list entry follow is now missing.
followIDs, err := suite.db.GetFollowIDsInList(ctx, testList.ID, nil)
suite.NoError(err)
suite.Len(followIDs, len(testEntries)-1)
suite.NotContains(followIDs, testEntries[0].FollowID)
}
func (suite *ListTestSuite) TestListIncludesAccount() {
ctx := context.Background()
testList, _ := suite.testStructs()
testList, _, _ := suite.testStructs()
for accountID, expected := range map[string]bool{
suite.testAccounts["admin_account"].ID: true,
@ -336,7 +289,7 @@ func (suite *ListTestSuite) TestListIncludesAccount() {
suite.testAccounts["local_account_2"].ID: true,
"01H7074GEZJ56J5C86PFB0V2CT": false,
} {
includes, err := suite.db.ListIncludesAccount(ctx, testList.ID, accountID)
includes, err := suite.db.IsAccountInList(ctx, testList.ID, accountID)
if err != nil {
suite.FailNow(err.Error())
}

View File

@ -24,7 +24,6 @@ import (
"time"
"github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/gtscontext"
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/internal/paging"
@ -57,15 +56,8 @@ func (m *mediaDB) GetAttachmentsByIDs(ctx context.Context, ids []string) ([]*gts
media, err := m.state.Caches.DB.Media.LoadIDs("ID",
ids,
func(uncached []string) ([]*gtsmodel.MediaAttachment, error) {
// Avoid querying
// if none uncached.
count := len(uncached)
if count == 0 {
return nil, nil
}
// Preallocate expected length of uncached media attachments.
media := make([]*gtsmodel.MediaAttachment, 0, count)
media := make([]*gtsmodel.MediaAttachment, 0, len(uncached))
// Perform database query scanning
// the remaining (uncached) IDs.
@ -129,30 +121,38 @@ func (m *mediaDB) UpdateAttachment(ctx context.Context, media *gtsmodel.MediaAtt
}
func (m *mediaDB) DeleteAttachment(ctx context.Context, id string) error {
// Load media into cache before attempting a delete,
// as we need it cached in order to trigger the invalidate
// callback. This in turn invalidates others.
media, err := m.GetAttachmentByID(gtscontext.SetBarebones(ctx), id)
if err != nil {
if errors.Is(err, db.ErrNoEntries) {
// not an issue.
err = nil
// Gather necessary fields from
// deleted for cache invaliation.
var deleted gtsmodel.MediaAttachment
deleted.ID = id
// Delete media attachment and update related models in new transaction.
err := m.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
// Initially, delete the media model,
// returning the required fields we need.
if _, err := tx.NewDelete().
Model(&deleted).
Where("? = ?", bun.Ident("id"), id).
Returning("?, ?, ?, ?",
bun.Ident("account_id"),
bun.Ident("status_id"),
bun.Ident("avatar"),
bun.Ident("header"),
).
Exec(ctx); err != nil {
return gtserror.Newf("error deleting media: %w", err)
}
return err
}
// On return, ensure that media with ID is invalidated.
defer m.state.Caches.DB.Media.Invalidate("ID", id)
// Delete media attachment in new transaction.
err = m.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
if media.AccountID != "" {
// If media was attached to account,
// we need to remove link from account.
if deleted.AccountID != "" {
var account gtsmodel.Account
// Get related account model.
if _, err := tx.NewSelect().
Model(&account).
Where("? = ?", bun.Ident("id"), media.AccountID).
Where("? = ?", bun.Ident("id"), deleted.AccountID).
Exec(ctx); err != nil && !errors.Is(err, db.ErrNoEntries) {
return gtserror.Newf("error selecting account: %w", err)
}
@ -160,11 +160,11 @@ func (m *mediaDB) DeleteAttachment(ctx context.Context, id string) error {
var set func(*bun.UpdateQuery) *bun.UpdateQuery
switch {
case *media.Avatar && account.AvatarMediaAttachmentID == id:
case *deleted.Avatar && account.AvatarMediaAttachmentID == id:
set = func(q *bun.UpdateQuery) *bun.UpdateQuery {
return q.Set("? = NULL", bun.Ident("avatar_media_attachment_id"))
}
case *media.Header && account.HeaderMediaAttachmentID == id:
case *deleted.Header && account.HeaderMediaAttachmentID == id:
set = func(q *bun.UpdateQuery) *bun.UpdateQuery {
return q.Set("? = NULL", bun.Ident("header_media_attachment_id"))
}
@ -183,13 +183,15 @@ func (m *mediaDB) DeleteAttachment(ctx context.Context, id string) error {
}
}
if media.StatusID != "" {
// If media was attached to a status,
// we need to remove link from status.
if deleted.StatusID != "" {
var status gtsmodel.Status
// Get related status model.
if _, err := tx.NewSelect().
Model(&status).
Where("? = ?", bun.Ident("id"), media.StatusID).
Where("? = ?", bun.Ident("id"), deleted.StatusID).
Exec(ctx); err != nil && !errors.Is(err, db.ErrNoEntries) {
return gtserror.Newf("error selecting status: %w", err)
}
@ -213,17 +215,14 @@ func (m *mediaDB) DeleteAttachment(ctx context.Context, id string) error {
}
}
// Finally delete this media.
if _, err := tx.NewDelete().
Table("media_attachments").
Where("? = ?", bun.Ident("id"), id).
Exec(ctx); err != nil {
return gtserror.Newf("error deleting media: %w", err)
}
return nil
})
// Invalidate cached media with ID, manually
// call invalidate hook in case not in cache.
m.state.Caches.DB.Media.Invalidate("ID", id)
m.state.Caches.OnInvalidateMedia(&deleted)
return err
}

View File

@ -69,15 +69,8 @@ func (m *mentionDB) GetMentions(ctx context.Context, ids []string) ([]*gtsmodel.
mentions, err := m.state.Caches.DB.Mention.LoadIDs("ID",
ids,
func(uncached []string) ([]*gtsmodel.Mention, error) {
// Avoid querying
// if none uncached.
count := len(uncached)
if count == 0 {
return nil, nil
}
// Preallocate expected length of uncached mentions.
mentions := make([]*gtsmodel.Mention, 0, count)
mentions := make([]*gtsmodel.Mention, 0, len(uncached))
// Perform database query scanning
// the remaining (uncached) IDs.
@ -166,24 +159,18 @@ func (m *mentionDB) PutMention(ctx context.Context, mention *gtsmodel.Mention) e
}
func (m *mentionDB) DeleteMentionByID(ctx context.Context, id string) error {
defer m.state.Caches.DB.Mention.Invalidate("ID", id)
// Load mention into cache before attempting a delete,
// as we need it cached in order to trigger the invalidate
// callback. This in turn invalidates others.
_, err := m.GetMention(gtscontext.SetBarebones(ctx), id)
if err != nil {
if errors.Is(err, db.ErrNoEntries) {
// not an issue.
err = nil
}
// Delete mention with given ID,
// returning the deleted models.
if _, err := m.db.NewDelete().
Table("mentions").
Where("? = ?", bun.Ident("id"), id).
Exec(ctx); err != nil &&
!errors.Is(err, db.ErrNoEntries) {
return err
}
// Finally delete mention from DB.
_, err = m.db.NewDelete().
Table("mentions").
Where("? = ?", bun.Ident("id"), id).
Exec(ctx)
return err
// Invalidate the cached mention with ID.
m.state.Caches.DB.Mention.Invalidate("ID", id)
return nil
}

View File

@ -234,13 +234,17 @@ func (m *moveDB) UpdateMove(ctx context.Context, move *gtsmodel.Move, columns ..
}
func (m *moveDB) DeleteMoveByID(ctx context.Context, id string) error {
defer m.state.Caches.DB.Move.Invalidate("ID", id)
_, err := m.db.
NewDelete().
// Delete move with given ID.
if _, err := m.db.NewDelete().
TableExpr("? AS ?", bun.Ident("moves"), bun.Ident("move")).
Where("? = ?", bun.Ident("move.id"), id).
Exec(ctx)
Exec(ctx); err != nil &&
!errors.Is(err, db.ErrNoEntries) {
return nil
}
return err
// Invalidate the cached move model with ID.
m.state.Caches.DB.Move.Invalidate("ID", id)
return nil
}

View File

@ -22,6 +22,7 @@ import (
"errors"
"slices"
"github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/gtscontext"
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
@ -107,15 +108,8 @@ func (n *notificationDB) GetNotificationsByIDs(ctx context.Context, ids []string
notifs, err := n.state.Caches.DB.Notification.LoadIDs("ID",
ids,
func(uncached []string) ([]*gtsmodel.Notification, error) {
// Avoid querying
// if none uncached.
count := len(uncached)
if count == 0 {
return nil, nil
}
// Preallocate expected length of uncached notifications.
notifs := make([]*gtsmodel.Notification, 0, count)
notifs := make([]*gtsmodel.Notification, 0, len(uncached))
// Perform database query scanning
// the remaining (uncached) IDs.
@ -299,7 +293,8 @@ func (n *notificationDB) DeleteNotificationByID(ctx context.Context, id string)
NewDelete().
Table("notifications").
Where("? = ?", bun.Ident("id"), id).
Exec(ctx); err != nil {
Exec(ctx); err != nil &&
!errors.Is(err, db.ErrNoEntries) {
return err
}
@ -310,7 +305,7 @@ func (n *notificationDB) DeleteNotificationByID(ctx context.Context, id string)
func (n *notificationDB) DeleteNotifications(ctx context.Context, types []string, targetAccountID string, originAccountID string) error {
if targetAccountID == "" && originAccountID == "" {
return errors.New("DeleteNotifications: one of targetAccountID or originAccountID must be set")
return gtserror.New("one of targetAccountID or originAccountID must be set")
}
q := n.db.

View File

@ -177,17 +177,36 @@ func (p *pollDB) UpdatePoll(ctx context.Context, poll *gtsmodel.Poll, cols ...st
}
func (p *pollDB) DeletePollByID(ctx context.Context, id string) error {
// Delete poll by ID from database.
if _, err := p.db.NewDelete().
Table("polls").
Where("? = ?", bun.Ident("id"), id).
Exec(ctx); err != nil {
// Delete poll vote with ID, and its associated votes from the database.
if err := p.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
// Delete poll from database.
if _, err := tx.NewDelete().
Table("polls").
Where("? = ?", bun.Ident("id"), id).
Exec(ctx); err != nil {
return err
}
// Delete the poll votes.
_, err := tx.NewDelete().
Table("poll_votes").
Where("? = ?", bun.Ident("poll_id"), id).
Exec(ctx)
return err
}); err != nil {
return err
}
// Invalidate poll by ID from cache.
// Wrap provided ID in a poll
// model for calling cache hook.
var deleted gtsmodel.Poll
deleted.ID = id
// Invalidate cached poll with ID, manually
// call invalidate hook in case not cached.
p.state.Caches.DB.Poll.Invalidate("ID", id)
p.state.Caches.DB.PollVoteIDs.Invalidate(id)
p.state.Caches.OnInvalidatePoll(&deleted)
return nil
}
@ -274,15 +293,8 @@ func (p *pollDB) GetPollVotes(ctx context.Context, pollID string) ([]*gtsmodel.P
votes, err := p.state.Caches.DB.PollVote.LoadIDs("ID",
voteIDs,
func(uncached []string) ([]*gtsmodel.PollVote, error) {
// Avoid querying
// if none uncached.
count := len(uncached)
if count == 0 {
return nil, nil
}
// Preallocate expected length of uncached votes.
votes := make([]*gtsmodel.PollVote, 0, count)
votes := make([]*gtsmodel.PollVote, 0, len(uncached))
// Perform database query scanning
// the remaining (uncached) IDs.
@ -391,148 +403,44 @@ func (p *pollDB) PutPollVote(ctx context.Context, vote *gtsmodel.PollVote) error
})
}
func (p *pollDB) DeletePollVotes(ctx context.Context, pollID string) error {
err := p.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
// Delete all votes in poll.
res, err := tx.NewDelete().
Table("poll_votes").
Where("? = ?", bun.Ident("poll_id"), pollID).
Exec(ctx)
if err != nil {
// irrecoverable
return err
}
ra, err := res.RowsAffected()
if err != nil {
// irrecoverable
return err
}
if ra == 0 {
// No poll votes deleted,
// nothing to update.
return nil
}
// Select current poll counts from DB,
// taking minimal columns needed to
// increment/decrement votes.
var poll gtsmodel.Poll
switch err := tx.NewSelect().
Model(&poll).
Column("options", "votes", "voters").
Where("? = ?", bun.Ident("id"), pollID).
Scan(ctx); {
case err == nil:
// no issue.
case errors.Is(err, db.ErrNoEntries):
// no votes found,
// return here.
return nil
default:
// irrecoverable.
return err
}
// Zero all counts.
poll.ResetVotes()
// Finally, update the poll entry.
_, err = tx.NewUpdate().
Model(&poll).
Column("votes", "voters").
Where("? = ?", bun.Ident("id"), pollID).
Exec(ctx)
return err
})
if err != nil {
return err
}
// Invalidate poll vote and poll entry from caches.
p.state.Caches.DB.Poll.Invalidate("ID", pollID)
p.state.Caches.DB.PollVote.Invalidate("PollID", pollID)
p.state.Caches.DB.PollVoteIDs.Invalidate(pollID)
return nil
}
func (p *pollDB) DeletePollVoteBy(ctx context.Context, pollID string, accountID string) error {
err := p.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
// Slice should only ever be of length
// 0 or 1; it's a slice of slices only
// because we can't LIMIT deletes to 1.
var choicesSlice [][]int
// Gather necessary fields from
// deleted for cache invaliation.
var deleted gtsmodel.PollVote
deleted.AccountID = accountID
deleted.PollID = pollID
// Delete the poll vote with given poll and account IDs, and update vote counts.
if err := p.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
// Delete vote in poll by account,
// returning the ID + choices of the vote.
if err := tx.NewDelete().
Table("poll_votes").
// returning deleted model info.
switch _, err := tx.NewDelete().
Model(&deleted).
Where("? = ?", bun.Ident("poll_id"), pollID).
Where("? = ?", bun.Ident("account_id"), accountID).
Returning("?", bun.Ident("choices")).
Scan(ctx, &choicesSlice); err != nil {
// irrecoverable.
return err
}
if len(choicesSlice) != 1 {
// No poll votes by this
// acct on this poll.
return nil
}
// Extract the *actual* choices.
choices := choicesSlice[0]
// Select current poll counts from DB,
// taking minimal columns needed to
// increment/decrement votes.
var poll gtsmodel.Poll
switch err := tx.NewSelect().
Model(&poll).
Column("options", "votes", "voters").
Where("? = ?", bun.Ident("id"), pollID).
Scan(ctx); {
Exec(ctx); {
case err == nil:
// no issue.
// no issue
case errors.Is(err, db.ErrNoEntries):
// no poll found,
// return here.
return nil
default:
// irrecoverable.
return err
}
// Decrement votes for choices.
poll.DecrementVotes(choices)
// Finally, update the poll entry.
_, err := tx.NewUpdate().
Model(&poll).
Column("votes", "voters").
Where("? = ?", bun.Ident("id"), pollID).
Exec(ctx)
// Update the votes for this deleted poll.
err := updatePollCounts(ctx, tx, &deleted)
return err
})
if err != nil {
}); err != nil {
return err
}
// Invalidate poll vote and poll entry from caches.
p.state.Caches.DB.Poll.Invalidate("ID", pollID)
// Invalidate the poll vote cache by given poll + account IDs, also
// manually call invalidation hook in case not actually stored in cache.
p.state.Caches.DB.PollVote.Invalidate("PollID,AccountID", pollID, accountID)
p.state.Caches.DB.PollVoteIDs.Invalidate(pollID)
p.state.Caches.OnInvalidatePollVote(&deleted)
return nil
}
@ -562,6 +470,48 @@ func (p *pollDB) DeletePollVotesByAccountID(ctx context.Context, accountID strin
return nil
}
// updatePollCounts updates the vote counts on a poll for the given deleted PollVote model.
func updatePollCounts(ctx context.Context, tx bun.Tx, deleted *gtsmodel.PollVote) error {
// Select current poll counts from DB,
// taking minimal columns needed to
// increment/decrement votes.
var poll gtsmodel.Poll
switch err := tx.NewSelect().
Model(&poll).
Column("options", "votes", "voters").
Where("? = ?", bun.Ident("id"), deleted.PollID).
Scan(ctx); {
case err == nil:
// no issue.
case errors.Is(err, db.ErrNoEntries):
// no poll found,
// return here.
return nil
default:
// irrecoverable.
return err
}
// Decrement votes for these choices.
poll.DecrementVotes(deleted.Choices)
// Finally, update the poll entry.
if _, err := tx.NewUpdate().
Model(&poll).
Column("votes", "voters").
Where("? = ?", bun.Ident("id"), deleted.PollID).
Exec(ctx); err != nil &&
!errors.Is(err, db.ErrNoEntries) {
return err
}
return nil
}
// newSelectPollVotes returns a new select query for all rows in the poll_votes table with poll_id = pollID.
func newSelectPollVotes(db *bun.DB, pollID string) *bun.SelectQuery {
return db.NewSelect().

View File

@ -26,7 +26,6 @@ import (
"github.com/stretchr/testify/suite"
"github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/gtscontext"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/internal/id"
"github.com/superseriousbusiness/gotosocial/internal/util"
@ -286,41 +285,6 @@ func (suite *PollTestSuite) TestDeletePoll() {
}
}
func (suite *PollTestSuite) TestDeletePollVotes() {
// Create a new context for this test.
ctx, cncl := context.WithCancel(context.Background())
defer cncl()
for _, poll := range suite.testPolls {
// Delete votes associated with poll from database.
err := suite.db.DeletePollVotes(ctx, poll.ID)
suite.NoError(err)
// Fetch latest version of poll from database.
poll, err = suite.db.GetPollByID(
gtscontext.SetBarebones(ctx),
poll.ID,
)
suite.NoError(err)
// Check that poll counts are all zero.
suite.Equal(*poll.Voters, 0)
suite.Equal(make([]int, len(poll.Options)), poll.Votes)
}
}
func (suite *PollTestSuite) TestDeletePollVotesNoPoll() {
// Create a new context for this test.
ctx, cncl := context.WithCancel(context.Background())
defer cncl()
// Try to delete votes of nonexistent poll.
nonPollID := "01HF6V4XWTSZWJ80JNPPDTD4DB"
err := suite.db.DeletePollVotes(ctx, nonPollID)
suite.NoError(err)
}
func (suite *PollTestSuite) TestDeletePollVotesBy() {
ctx, cncl := context.WithCancel(context.Background())
defer cncl()

View File

@ -105,15 +105,8 @@ func (r *relationshipDB) GetBlocksByIDs(ctx context.Context, ids []string) ([]*g
blocks, err := r.state.Caches.DB.Block.LoadIDs("ID",
ids,
func(uncached []string) ([]*gtsmodel.Block, error) {
// Avoid querying
// if none uncached.
count := len(uncached)
if count == 0 {
return nil, nil
}
// Preallocate expected length of uncached blocks.
blocks := make([]*gtsmodel.Block, 0, count)
blocks := make([]*gtsmodel.Block, 0, len(uncached))
// Perform database query scanning
// the remaining (uncached) IDs.
@ -222,94 +215,93 @@ func (r *relationshipDB) PutBlock(ctx context.Context, block *gtsmodel.Block) er
}
func (r *relationshipDB) DeleteBlockByID(ctx context.Context, id string) error {
// Load block into cache before attempting a delete,
// as we need it cached in order to trigger the invalidate
// callback. This in turn invalidates others.
_, err := r.GetBlockByID(gtscontext.SetBarebones(ctx), id)
if err != nil {
if errors.Is(err, db.ErrNoEntries) {
// not an issue.
err = nil
}
// Gather necessary fields from
// deleted for cache invaliation.
var deleted gtsmodel.Block
// Delete block with given ID,
// returning the deleted models.
if _, err := r.db.NewDelete().
Model(&deleted).
Where("? = ?", bun.Ident("id"), id).
Returning("?, ?",
bun.Ident("account_id"),
bun.Ident("target_account_id"),
).
Exec(ctx); err != nil &&
!errors.Is(err, db.ErrNoEntries) {
return err
}
// Drop this now-cached block on return after delete.
defer r.state.Caches.DB.Block.Invalidate("ID", id)
// Invalidate cached block with ID, manually
// call invalidate hook in case not cached.
r.state.Caches.DB.Block.Invalidate("ID", id)
r.state.Caches.OnInvalidateBlock(&deleted)
// Finally delete block from DB.
_, err = r.db.NewDelete().
Table("blocks").
Where("? = ?", bun.Ident("id"), id).
Exec(ctx)
return err
return nil
}
func (r *relationshipDB) DeleteBlockByURI(ctx context.Context, uri string) error {
// Load block into cache before attempting a delete,
// as we need it cached in order to trigger the invalidate
// callback. This in turn invalidates others.
_, err := r.GetBlockByURI(gtscontext.SetBarebones(ctx), uri)
if err != nil {
if errors.Is(err, db.ErrNoEntries) {
// not an issue.
err = nil
}
// Gather necessary fields from
// deleted for cache invaliation.
var deleted gtsmodel.Block
// Delete block with given URI,
// returning the deleted models.
if _, err := r.db.NewDelete().
Model(&deleted).
Where("? = ?", bun.Ident("uri"), uri).
Returning("?, ?",
bun.Ident("account_id"),
bun.Ident("target_account_id"),
).
Exec(ctx); err != nil &&
!errors.Is(err, db.ErrNoEntries) {
return err
}
// Drop this now-cached block on return after delete.
defer r.state.Caches.DB.Block.Invalidate("URI", uri)
// Invalidate cached block with URI, manually
// call invalidate hook in case not cached.
r.state.Caches.DB.Block.Invalidate("URI", uri)
r.state.Caches.OnInvalidateBlock(&deleted)
// Finally delete block from DB.
_, err = r.db.NewDelete().
Table("blocks").
Where("? = ?", bun.Ident("uri"), uri).
Exec(ctx)
return err
return nil
}
func (r *relationshipDB) DeleteAccountBlocks(ctx context.Context, accountID string) error {
var blockIDs []string
// Gather necessary fields from
// deleted for cache invaliation.
var deleted []*gtsmodel.Block
// Get full list of IDs.
if err := r.db.NewSelect().
Column("id").
Table("blocks").
// Delete all blocks either from
// account, or targeting account,
// returning the deleted models.
if _, err := r.db.NewDelete().
Model(&deleted).
WhereOr("? = ? OR ? = ?",
bun.Ident("account_id"),
accountID,
bun.Ident("target_account_id"),
accountID,
).
Scan(ctx, &blockIDs); err != nil {
Returning("?, ?",
bun.Ident("account_id"),
bun.Ident("target_account_id"),
).
Exec(ctx); err != nil &&
!errors.Is(err, db.ErrNoEntries) {
return err
}
if len(blockIDs) == 0 {
// Nothing
// to delete.
return nil
// Invalidate all account's incoming / outoing blocks.
r.state.Caches.DB.Block.Invalidate("AccountID", accountID)
r.state.Caches.DB.Block.Invalidate("TargetAccountID", accountID)
// In case not all blocks were in
// cache, manually call invalidate hooks.
for _, block := range deleted {
r.state.Caches.OnInvalidateBlock(block)
}
defer func() {
// Invalidate all account's incoming / outoing blocks on return.
r.state.Caches.DB.Block.Invalidate("AccountID", accountID)
r.state.Caches.DB.Block.Invalidate("TargetAccountID", accountID)
}()
// Load all blocks into cache, this *really* isn't great
// but it is the only way we can ensure we invalidate all
// related caches correctly (e.g. visibility).
_, err := r.GetAccountBlocks(ctx, accountID, nil)
if err != nil && !errors.Is(err, db.ErrNoEntries) {
return err
}
// Finally delete all from DB.
_, err = r.db.NewDelete().
Table("blocks").
Where("? IN (?)", bun.Ident("id"), bun.In(blockIDs)).
Exec(ctx)
return err
return nil
}

View File

@ -20,7 +20,6 @@ package bundb
import (
"context"
"errors"
"fmt"
"slices"
"time"
@ -82,15 +81,8 @@ func (r *relationshipDB) GetFollowsByIDs(ctx context.Context, ids []string) ([]*
follows, err := r.state.Caches.DB.Follow.LoadIDs("ID",
ids,
func(uncached []string) ([]*gtsmodel.Follow, error) {
// Avoid querying
// if none uncached.
count := len(uncached)
if count == 0 {
return nil, nil
}
// Preallocate expected length of uncached follows.
follows := make([]*gtsmodel.Follow, 0, count)
follows := make([]*gtsmodel.Follow, 0, len(uncached))
// Perform database query scanning
// the remaining (uncached) IDs.
@ -252,139 +244,155 @@ func (r *relationshipDB) UpdateFollow(ctx context.Context, follow *gtsmodel.Foll
})
}
func (r *relationshipDB) deleteFollow(ctx context.Context, id string) error {
// Delete the follow itself using the given ID.
func (r *relationshipDB) DeleteFollow(
ctx context.Context,
sourceAccountID string,
targetAccountID string,
) error {
// Gather necessary fields from
// deleted for cache invaliation.
var deleted gtsmodel.Follow
deleted.AccountID = sourceAccountID
deleted.TargetAccountID = targetAccountID
// Delete follow from origin
// account, to targeting account,
// returning the deleted models.
if _, err := r.db.NewDelete().
Table("follows").
Where("? = ?", bun.Ident("id"), id).
Exec(ctx); err != nil {
Model(&deleted).
Where("? = ?", bun.Ident("account_id"), sourceAccountID).
Where("? = ?", bun.Ident("target_account_id"), targetAccountID).
Returning("?", bun.Ident("id")).
Exec(ctx); err != nil &&
!errors.Is(err, db.ErrNoEntries) {
return err
}
// Delete every list entry that used this followID.
if err := r.state.DB.DeleteListEntriesForFollowID(ctx, id); err != nil {
return fmt.Errorf("deleteFollow: error deleting list entries: %w", err)
// Invalidate cached follow with source / target account IDs,
// manually calling invalidate hook in case it isn't cached.
r.state.Caches.DB.Follow.Invalidate("AccountID,TargetAccountID",
sourceAccountID, targetAccountID)
r.state.Caches.OnInvalidateFollow(&deleted)
// Delete every list entry that was created targetting this follow ID.
if err := r.state.DB.DeleteAllListEntriesByFollows(ctx, deleted.ID); err != nil {
return gtserror.Newf("error deleting list entries: %w", err)
}
return nil
}
func (r *relationshipDB) DeleteFollow(ctx context.Context, sourceAccountID string, targetAccountID string) error {
// Load follow into cache before attempting a delete,
// as we need it cached in order to trigger the invalidate
// callback. This in turn invalidates others.
follow, err := r.GetFollow(
gtscontext.SetBarebones(ctx),
sourceAccountID,
targetAccountID,
)
if err != nil {
if errors.Is(err, db.ErrNoEntries) {
// Already gone.
return nil
}
return err
}
// Drop this now-cached follow on return after delete.
defer r.state.Caches.DB.Follow.Invalidate("AccountID,TargetAccountID", sourceAccountID, targetAccountID)
// Finally delete follow from DB.
return r.deleteFollow(ctx, follow.ID)
}
func (r *relationshipDB) DeleteFollowByID(ctx context.Context, id string) error {
// Load follow into cache before attempting a delete,
// as we need it cached in order to trigger the invalidate
// callback. This in turn invalidates others.
follow, err := r.GetFollowByID(gtscontext.SetBarebones(ctx), id)
if err != nil {
if errors.Is(err, db.ErrNoEntries) {
// Already gone.
return nil
}
// Gather necessary fields from
// deleted for cache invaliation.
var deleted gtsmodel.Follow
deleted.ID = id
// Delete follow with given ID,
// returning the deleted models.
if _, err := r.db.NewDelete().
Model(&deleted).
Where("? = ?", bun.Ident("id"), id).
Returning("?, ?",
bun.Ident("account_id"),
bun.Ident("target_account_id"),
).
Exec(ctx); err != nil &&
!errors.Is(err, db.ErrNoEntries) {
return err
}
// Drop this now-cached follow on return after delete.
defer r.state.Caches.DB.Follow.Invalidate("ID", id)
// Invalidate cached follow with ID, manually
// call invalidate hook in case not cached.
r.state.Caches.DB.Follow.Invalidate("ID", id)
r.state.Caches.OnInvalidateFollow(&deleted)
// Finally delete follow from DB.
return r.deleteFollow(ctx, follow.ID)
// Delete every list entry that was created targetting this follow ID.
if err := r.state.DB.DeleteAllListEntriesByFollows(ctx, id); err != nil {
return gtserror.Newf("error deleting list entries: %w", err)
}
return nil
}
func (r *relationshipDB) DeleteFollowByURI(ctx context.Context, uri string) error {
// Load follow into cache before attempting a delete,
// as we need it cached in order to trigger the invalidate
// callback. This in turn invalidates others.
follow, err := r.GetFollowByURI(gtscontext.SetBarebones(ctx), uri)
if err != nil {
if errors.Is(err, db.ErrNoEntries) {
// Already gone.
return nil
}
// Gather necessary fields from
// deleted for cache invaliation.
var deleted gtsmodel.Follow
// Delete follow with given URI,
// returning the deleted models.
if _, err := r.db.NewDelete().
Model(&deleted).
Where("? = ?", bun.Ident("uri"), uri).
Returning("?, ?, ?",
bun.Ident("id"),
bun.Ident("account_id"),
bun.Ident("target_account_id"),
).
Exec(ctx); err != nil &&
!errors.Is(err, db.ErrNoEntries) {
return err
}
// Drop this now-cached follow on return after delete.
defer r.state.Caches.DB.Follow.Invalidate("URI", uri)
// Invalidate cached follow with URI, manually
// call invalidate hook in case not cached.
r.state.Caches.DB.Follow.Invalidate("URI", uri)
r.state.Caches.OnInvalidateFollow(&deleted)
// Finally delete follow from DB.
return r.deleteFollow(ctx, follow.ID)
// Delete every list entry that was created targetting this follow ID.
if err := r.state.DB.DeleteAllListEntriesByFollows(ctx, deleted.ID); err != nil {
return gtserror.Newf("error deleting list entries: %w", err)
}
return nil
}
func (r *relationshipDB) DeleteAccountFollows(ctx context.Context, accountID string) error {
var followIDs []string
// Gather necessary fields from
// deleted for cache invaliation.
var deleted []*gtsmodel.Follow
// Get full list of IDs.
if _, err := r.db.
NewSelect().
Column("id").
Table("follows").
// Delete all follows either from
// account, or targeting account,
// returning the deleted models.
if _, err := r.db.NewDelete().
Model(&deleted).
WhereOr("? = ? OR ? = ?",
bun.Ident("account_id"),
accountID,
bun.Ident("target_account_id"),
accountID,
).
Exec(ctx, &followIDs); err != nil {
Returning("?, ?, ?",
bun.Ident("id"),
bun.Ident("account_id"),
bun.Ident("target_account_id"),
).
Exec(ctx); err != nil &&
!errors.Is(err, db.ErrNoEntries) {
return err
}
if len(followIDs) == 0 {
// Nothing
// to delete.
return nil
// Gather the follow IDs that were deleted for removing related list entries.
followIDs := util.Gather(nil, deleted, func(follow *gtsmodel.Follow) string {
return follow.ID
})
// Delete every list entry that was created targetting any of these follow IDs.
if err := r.state.DB.DeleteAllListEntriesByFollows(ctx, followIDs...); err != nil {
return gtserror.Newf("error deleting list entries: %w", err)
}
defer func() {
// Invalidate all account's incoming / outoing follows on return.
r.state.Caches.DB.Follow.Invalidate("AccountID", accountID)
r.state.Caches.DB.Follow.Invalidate("TargetAccountID", accountID)
}()
// Invalidate all account's incoming / outoing follows.
r.state.Caches.DB.Follow.Invalidate("AccountID", accountID)
r.state.Caches.DB.Follow.Invalidate("TargetAccountID", accountID)
// Load all follows into cache, this *really* isn't great
// but it is the only way we can ensure we invalidate all
// related caches correctly (e.g. visibility).
_, err := r.GetAccountFollows(ctx, accountID, nil)
if err != nil && !errors.Is(err, db.ErrNoEntries) {
return err
}
// Delete all follows from DB.
_, err = r.db.NewDelete().
Table("follows").
Where("? IN (?)", bun.Ident("id"), bun.In(followIDs)).
Exec(ctx)
if err != nil {
return err
}
for _, id := range followIDs {
// Finally, delete all list entries associated with each follow ID.
if err := r.state.DB.DeleteListEntriesForFollowID(ctx, id); err != nil {
return err
}
// In case not all follow were in
// cache, manually call invalidate hooks.
for _, follow := range deleted {
r.state.Caches.OnInvalidateFollow(follow)
}
return nil

View File

@ -81,15 +81,8 @@ func (r *relationshipDB) GetFollowRequestsByIDs(ctx context.Context, ids []strin
follows, err := r.state.Caches.DB.FollowRequest.LoadIDs("ID",
ids,
func(uncached []string) ([]*gtsmodel.FollowRequest, error) {
// Avoid querying
// if none uncached.
count := len(uncached)
if count == 0 {
return nil, nil
}
// Preallocate expected length of uncached followReqs.
follows := make([]*gtsmodel.FollowRequest, 0, count)
follows := make([]*gtsmodel.FollowRequest, 0, len(uncached))
// Perform database query scanning
// the remaining (uncached) IDs.
@ -293,124 +286,131 @@ func (r *relationshipDB) RejectFollowRequest(ctx context.Context, sourceAccountI
}, targetAccountID, sourceAccountID)
}
func (r *relationshipDB) DeleteFollowRequest(ctx context.Context, sourceAccountID string, targetAccountID string) error {
// Load followreq into cache before attempting a delete,
// as we need it cached in order to trigger the invalidate
// callback. This in turn invalidates others.
follow, err := r.GetFollowRequest(
gtscontext.SetBarebones(ctx),
sourceAccountID,
targetAccountID,
)
if err != nil {
if errors.Is(err, db.ErrNoEntries) {
// Already gone.
return nil
}
func (r *relationshipDB) DeleteFollowRequest(
ctx context.Context,
sourceAccountID string,
targetAccountID string,
) error {
// Gather necessary fields from
// deleted for cache invaliation.
var deleted gtsmodel.FollowRequest
deleted.AccountID = sourceAccountID
deleted.TargetAccountID = targetAccountID
// Delete all follow reqs either
// from account, or targeting account,
// returning the deleted models.
if _, err := r.db.NewDelete().
Model(&deleted).
Where("? = ?", bun.Ident("account_id"), sourceAccountID).
Where("? = ?", bun.Ident("target_account_id"), targetAccountID).
Returning("?", bun.Ident("id")).
Exec(ctx); err != nil &&
!errors.Is(err, db.ErrNoEntries) {
return err
}
// Drop this now-cached follow request on return after delete.
defer r.state.Caches.DB.FollowRequest.Invalidate("AccountID,TargetAccountID", sourceAccountID, targetAccountID)
// Invalidate cached follow with source / target account IDs,
// manually calling invalidate hook in case it isn't cached.
r.state.Caches.DB.FollowRequest.Invalidate("AccountID,TargetAccountID",
sourceAccountID, targetAccountID)
r.state.Caches.OnInvalidateFollowRequest(&deleted)
// Finally delete followreq from DB.
_, err = r.db.NewDelete().
Table("follow_requests").
Where("? = ?", bun.Ident("id"), follow.ID).
Exec(ctx)
return err
return nil
}
func (r *relationshipDB) DeleteFollowRequestByID(ctx context.Context, id string) error {
// Load followreq into cache before attempting a delete,
// as we need it cached in order to trigger the invalidate
// callback. This in turn invalidates others.
_, err := r.GetFollowRequestByID(gtscontext.SetBarebones(ctx), id)
if err != nil {
if errors.Is(err, db.ErrNoEntries) {
// not an issue.
err = nil
}
// Gather necessary fields from
// deleted for cache invaliation.
var deleted gtsmodel.FollowRequest
deleted.ID = id
// Delete follow with given URI,
// returning the deleted models.
if _, err := r.db.NewDelete().
Model(&deleted).
Where("? = ?", bun.Ident("id"), id).
Returning("?, ?",
bun.Ident("account_id"),
bun.Ident("target_account_id"),
).
Exec(ctx); err != nil &&
!errors.Is(err, db.ErrNoEntries) {
return err
}
// Drop this now-cached follow request on return after delete.
defer r.state.Caches.DB.FollowRequest.Invalidate("ID", id)
// Invalidate cached follow with URI, manually
// call invalidate hook in case not cached.
r.state.Caches.DB.FollowRequest.Invalidate("ID", id)
r.state.Caches.OnInvalidateFollowRequest(&deleted)
// Finally delete followreq from DB.
_, err = r.db.NewDelete().
Table("follow_requests").
Where("? = ?", bun.Ident("id"), id).
Exec(ctx)
return err
return nil
}
func (r *relationshipDB) DeleteFollowRequestByURI(ctx context.Context, uri string) error {
// Load followreq into cache before attempting a delete,
// as we need it cached in order to trigger the invalidate
// callback. This in turn invalidates others.
_, err := r.GetFollowRequestByURI(gtscontext.SetBarebones(ctx), uri)
if err != nil {
if errors.Is(err, db.ErrNoEntries) {
// not an issue.
err = nil
}
// Gather necessary fields from
// deleted for cache invaliation.
var deleted gtsmodel.FollowRequest
// Delete follow with given URI,
// returning the deleted models.
if _, err := r.db.NewDelete().
Model(&deleted).
Where("? = ?", bun.Ident("uri"), uri).
Returning("?, ?, ?",
bun.Ident("id"),
bun.Ident("account_id"),
bun.Ident("target_account_id"),
).
Exec(ctx); err != nil &&
!errors.Is(err, db.ErrNoEntries) {
return err
}
// Drop this now-cached follow request on return after delete.
defer r.state.Caches.DB.FollowRequest.Invalidate("URI", uri)
// Invalidate cached follow with URI, manually
// call invalidate hook in case not cached.
r.state.Caches.DB.FollowRequest.Invalidate("URI", uri)
r.state.Caches.OnInvalidateFollowRequest(&deleted)
// Finally delete followreq from DB.
_, err = r.db.NewDelete().
Table("follow_requests").
Where("? = ?", bun.Ident("uri"), uri).
Exec(ctx)
return err
return nil
}
func (r *relationshipDB) DeleteAccountFollowRequests(ctx context.Context, accountID string) error {
var followReqIDs []string
// Gather necessary fields from
// deleted for cache invaliation.
var deleted []*gtsmodel.FollowRequest
// Get full list of IDs.
if _, err := r.db.
NewSelect().
Column("id").
Table("follow_requests").
// Delete all follows either from
// account, or targeting account,
// returning the deleted models.
if _, err := r.db.NewDelete().
Model(&deleted).
WhereOr("? = ? OR ? = ?",
bun.Ident("account_id"),
accountID,
bun.Ident("target_account_id"),
accountID,
).
Exec(ctx, &followReqIDs); err != nil {
Returning("?, ?, ?",
bun.Ident("id"),
bun.Ident("account_id"),
bun.Ident("target_account_id"),
).
Exec(ctx); err != nil &&
!errors.Is(err, db.ErrNoEntries) {
return err
}
if len(followReqIDs) == 0 {
// Nothing
// to delete.
return nil
// Invalidate all account's incoming / outoing follows requests.
r.state.Caches.DB.FollowRequest.Invalidate("AccountID", accountID)
r.state.Caches.DB.FollowRequest.Invalidate("TargetAccountID", accountID)
// In case not all follow were in
// cache, manually call invalidate hooks.
for _, followReq := range deleted {
r.state.Caches.OnInvalidateFollowRequest(followReq)
}
defer func() {
// Invalidate all account's incoming / outoing follow requests on return.
r.state.Caches.DB.FollowRequest.Invalidate("AccountID", accountID)
r.state.Caches.DB.FollowRequest.Invalidate("TargetAccountID", accountID)
}()
// Load all followreqs into cache, this *really* isn't
// great but it is the only way we can ensure we invalidate
// all related caches correctly (e.g. visibility).
_, err := r.GetAccountFollowRequests(ctx, accountID, nil)
if err != nil && !errors.Is(err, db.ErrNoEntries) {
return err
}
// Finally delete all from DB.
_, err = r.db.NewDelete().
Table("follow_requests").
Where("? IN (?)", bun.Ident("id"), bun.In(followReqIDs)).
Exec(ctx)
return err
return nil
}

View File

@ -87,15 +87,8 @@ func (r *relationshipDB) getMutesByIDs(ctx context.Context, ids []string) ([]*gt
mutes, err := r.state.Caches.DB.UserMute.LoadIDs("ID",
ids,
func(uncached []string) ([]*gtsmodel.UserMute, error) {
// Avoid querying
// if none uncached.
count := len(uncached)
if count == 0 {
return nil, nil
}
// Preallocate expected length of uncached mutes.
mutes := make([]*gtsmodel.UserMute, 0, count)
mutes := make([]*gtsmodel.UserMute, 0, len(uncached))
// Perform database query scanning
// the remaining (uncached) IDs.
@ -209,72 +202,64 @@ func (r *relationshipDB) PutMute(ctx context.Context, mute *gtsmodel.UserMute) e
}
func (r *relationshipDB) DeleteMuteByID(ctx context.Context, id string) error {
// Load mute into cache before attempting a delete,
// as we need it cached in order to trigger the invalidate
// callback. This in turn invalidates others.
_, err := r.GetMuteByID(gtscontext.SetBarebones(ctx), id)
if err != nil {
if errors.Is(err, db.ErrNoEntries) {
// not an issue.
err = nil
}
// Gather necessary fields from
// deleted for cache invaliation.
var deleted gtsmodel.UserMute
// Delete mute with given ID,
// returning the deleted models.
if _, err := r.db.NewDelete().
Model(&deleted).
Where("? = ?", bun.Ident("id"), id).
Returning("?", bun.Ident("account_id")).
Exec(ctx); err != nil &&
!errors.Is(err, db.ErrNoEntries) {
return err
}
// Drop this now-cached mute on return after delete.
defer r.state.Caches.DB.UserMute.Invalidate("ID", id)
// Invalidate cached mute with ID, manually
// call invalidate hook in case not cached.
r.state.Caches.DB.UserMute.Invalidate("ID", id)
r.state.Caches.OnInvalidateUserMute(&deleted)
// Finally delete mute from DB.
_, err = r.db.NewDelete().
Table("user_mutes").
Where("? = ?", bun.Ident("id"), id).
Exec(ctx)
return err
return nil
}
func (r *relationshipDB) DeleteAccountMutes(ctx context.Context, accountID string) error {
var muteIDs []string
// Gather necessary fields from
// deleted for cache invaliation.
var deleted []*gtsmodel.UserMute
// Get full list of IDs.
if err := r.db.NewSelect().
Column("id").
Table("user_mutes").
// Delete all mutes either from
// account, or targeting account,
// returning the deleted models.
if _, err := r.db.NewDelete().
Model(&deleted).
WhereOr("? = ? OR ? = ?",
bun.Ident("account_id"),
accountID,
bun.Ident("target_account_id"),
accountID,
).
Scan(ctx, &muteIDs); err != nil {
Returning("?",
bun.Ident("account_id"),
).
Exec(ctx); err != nil &&
!errors.Is(err, db.ErrNoEntries) {
return err
}
if len(muteIDs) == 0 {
// Nothing
// to delete.
return nil
// Invalidate all account's incoming / outoing user mutes.
r.state.Caches.DB.UserMute.Invalidate("AccountID", accountID)
r.state.Caches.DB.UserMute.Invalidate("TargetAccountID", accountID)
// In case not all user mutes were in
// cache, manually call invalidate hooks.
for _, block := range deleted {
r.state.Caches.OnInvalidateUserMute(block)
}
defer func() {
// Invalidate all account's incoming / outoing mutes on return.
r.state.Caches.DB.UserMute.Invalidate("AccountID", accountID)
r.state.Caches.DB.UserMute.Invalidate("TargetAccountID", accountID)
}()
// Load all mutes into cache, this *really* isn't great
// but it is the only way we can ensure we invalidate all
// related caches correctly (e.g. visibility).
_, err := r.GetAccountMutes(ctx, accountID, nil)
if err != nil && !errors.Is(err, db.ErrNoEntries) {
return err
}
// Finally delete all from DB.
_, err = r.db.NewDelete().
Table("user_mutes").
Where("? IN (?)", bun.Ident("id"), bun.In(muteIDs)).
Exec(ctx)
return err
return nil
}
func (r *relationshipDB) GetAccountMutes(

View File

@ -826,10 +826,10 @@ func (suite *RelationshipTestSuite) TestUnfollowExisting() {
suite.NotNil(follow)
followID := follow.ID
// We should have list entries for this follow.
listEntries, err := suite.db.GetListEntriesForFollowID(context.Background(), followID)
// We should have lists that this follow is a part of.
lists, err := suite.db.GetListsContainingFollowID(context.Background(), followID)
suite.NoError(err)
suite.NotEmpty(listEntries)
suite.NotEmpty(lists)
err = suite.db.DeleteFollowByID(context.Background(), followID)
suite.NoError(err)
@ -838,10 +838,10 @@ func (suite *RelationshipTestSuite) TestUnfollowExisting() {
suite.EqualError(err, db.ErrNoEntries.Error())
suite.Nil(follow)
// ListEntries pertaining to this follow should be deleted too.
listEntries, err = suite.db.GetListEntriesForFollowID(context.Background(), followID)
// Lists containing this follow should return empty too.
lists, err = suite.db.GetListsContainingFollowID(context.Background(), followID)
suite.NoError(err)
suite.Empty(listEntries)
suite.Empty(lists)
}
func (suite *RelationshipTestSuite) TestGetFollowNotExisting() {

View File

@ -248,45 +248,36 @@ func (r *reportDB) PutReport(ctx context.Context, report *gtsmodel.Report) error
})
}
func (r *reportDB) UpdateReport(ctx context.Context, report *gtsmodel.Report, columns ...string) (*gtsmodel.Report, error) {
func (r *reportDB) UpdateReport(ctx context.Context, report *gtsmodel.Report, columns ...string) error {
// Update the report's last-updated
report.UpdatedAt = time.Now()
if len(columns) != 0 {
columns = append(columns, "updated_at")
}
if _, err := r.db.
NewUpdate().
Model(report).
Where("? = ?", bun.Ident("report.id"), report.ID).
Column(columns...).
Exec(ctx); err != nil {
return nil, err
}
r.state.Caches.DB.Report.Invalidate("ID", report.ID)
return report, nil
return r.state.Caches.DB.Report.Store(report, func() error {
_, err := r.db.
NewUpdate().
Model(report).
Where("? = ?", bun.Ident("report.id"), report.ID).
Column(columns...).
Exec(ctx)
return err
})
}
func (r *reportDB) DeleteReportByID(ctx context.Context, id string) error {
defer r.state.Caches.DB.Report.Invalidate("ID", id)
// Load status into cache before attempting a delete,
// as we need it cached in order to trigger the invalidate
// callback. This in turn invalidates others.
_, err := r.GetReportByID(gtscontext.SetBarebones(ctx), id)
if err != nil {
if errors.Is(err, db.ErrNoEntries) {
// not an issue.
err = nil
}
// Delete the report from DB.
if _, err := r.db.NewDelete().
TableExpr("? AS ?", bun.Ident("reports"), bun.Ident("report")).
Where("? = ?", bun.Ident("report.id"), id).
Exec(ctx); err != nil &&
!errors.Is(err, db.ErrNoEntries) {
return err
}
// Finally delete report from DB.
_, err = r.db.NewDelete().
TableExpr("? AS ?", bun.Ident("reports"), bun.Ident("report")).
Where("? = ?", bun.Ident("report.id"), id).
Exec(ctx)
return err
// Invalidate any cached report model by ID.
r.state.Caches.DB.Report.Invalidate("ID", id)
return nil
}

View File

@ -202,7 +202,7 @@ func (suite *ReportTestSuite) TestUpdateReport() {
report.ActionTakenByAccountID = suite.testAccounts["admin_account"].ID
report.ActionTakenAt = testrig.TimeMustParse("2022-05-14T12:20:03+02:00")
if _, err := suite.db.UpdateReport(ctx, report, "action_taken", "action_taken_by_account_id", "action_taken_at"); err != nil {
if err := suite.db.UpdateReport(ctx, report, "action_taken", "action_taken_by_account_id", "action_taken_at"); err != nil {
suite.FailNow(err.Error())
}
@ -228,7 +228,7 @@ func (suite *ReportTestSuite) TestUpdateReportAllColumns() {
report.ActionTakenByAccountID = suite.testAccounts["admin_account"].ID
report.ActionTakenAt = testrig.TimeMustParse("2022-05-14T12:20:03+02:00")
if _, err := suite.db.UpdateReport(ctx, report); err != nil {
if err := suite.db.UpdateReport(ctx, report); err != nil {
suite.FailNow(err.Error())
}

View File

@ -19,8 +19,10 @@ package bundb
import (
"context"
"errors"
"time"
"github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/internal/state"
"github.com/uptrace/bun"
@ -110,13 +112,18 @@ func (s *sinBinStatusDB) UpdateSinBinStatus(
}
func (s *sinBinStatusDB) DeleteSinBinStatusByID(ctx context.Context, id string) error {
// On return ensure status invalidated from cache.
defer s.state.Caches.DB.SinBinStatus.Invalidate("ID", id)
_, err := s.db.
// Delete the status from DB.
if _, err := s.db.
NewDelete().
TableExpr("? AS ?", bun.Ident("sin_bin_statuses"), bun.Ident("sin_bin_status")).
Where("? = ?", bun.Ident("sin_bin_status.id"), id).
Exec(ctx)
return err
Exec(ctx); err != nil &&
!errors.Is(err, db.ErrNoEntries) {
return err
}
// Invalidate any cached sinbin status model by ID.
s.state.Caches.DB.SinBinStatus.Invalidate("ID", id)
return nil
}

View File

@ -54,15 +54,8 @@ func (s *statusDB) GetStatusesByIDs(ctx context.Context, ids []string) ([]*gtsmo
statuses, err := s.state.Caches.DB.Status.LoadIDs("ID",
ids,
func(uncached []string) ([]*gtsmodel.Status, error) {
// Avoid querying
// if none uncached.
count := len(uncached)
if count == 0 {
return nil, nil
}
// Preallocate expected length of uncached statuses.
statuses := make([]*gtsmodel.Status, 0, count)
statuses := make([]*gtsmodel.Status, 0, len(uncached))
// Perform database query scanning
// the remaining (uncached) status IDs.
@ -486,24 +479,13 @@ func (s *statusDB) UpdateStatus(ctx context.Context, status *gtsmodel.Status, co
}
func (s *statusDB) DeleteStatusByID(ctx context.Context, id string) error {
// Load status into cache before attempting a delete,
// as we need it cached in order to trigger the invalidate
// callback. This in turn invalidates others.
_, err := s.GetStatusByID(
gtscontext.SetBarebones(ctx),
id,
)
if err != nil && !errors.Is(err, db.ErrNoEntries) {
// NOTE: even if db.ErrNoEntries is returned, we
// still run the below transaction to ensure related
// objects are appropriately deleted.
return err
}
// Gather necessary fields from
// deleted for cache invaliation.
var deleted gtsmodel.Status
deleted.ID = id
// On return ensure status invalidated from cache.
defer s.state.Caches.DB.Status.Invalidate("ID", id)
return s.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
// Delete status from database and any related links in a transaction.
if err := s.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
// delete links between this status and any emojis it uses
if _, err := tx.
NewDelete().
@ -524,26 +506,42 @@ func (s *statusDB) DeleteStatusByID(ctx context.Context, id string) error {
// Delete links between this status
// and any threads it was a part of.
_, err = tx.
if _, err := tx.
NewDelete().
TableExpr("? AS ?", bun.Ident("thread_to_statuses"), bun.Ident("thread_to_status")).
Where("? = ?", bun.Ident("thread_to_status.status_id"), id).
Exec(ctx)
if err != nil {
Exec(ctx); err != nil {
return err
}
// delete the status itself
if _, err := tx.
NewDelete().
TableExpr("? AS ?", bun.Ident("statuses"), bun.Ident("status")).
Where("? = ?", bun.Ident("status.id"), id).
Exec(ctx); err != nil {
Model(&deleted).
Where("? = ?", bun.Ident("id"), id).
Returning("?, ?, ?, ?, ?",
bun.Ident("account_id"),
bun.Ident("boost_of_id"),
bun.Ident("in_reply_to_id"),
bun.Ident("attachments"),
bun.Ident("poll_id"),
).
Exec(ctx); err != nil &&
!errors.Is(err, db.ErrNoEntries) {
return err
}
return nil
})
}); err != nil {
return err
}
// Invalidate cached status by its ID, manually
// call the invalidate hook in case not cached.
s.state.Caches.DB.Status.Invalidate("ID", id)
s.state.Caches.OnInvalidateStatus(&deleted)
return nil
}
func (s *statusDB) GetStatusesUsingEmoji(ctx context.Context, emojiID string) ([]*gtsmodel.Status, error) {

View File

@ -73,15 +73,8 @@ func (s *statusBookmarkDB) GetStatusBookmarksByIDs(ctx context.Context, ids []st
bookmarks, err := s.state.Caches.DB.StatusBookmark.LoadIDs("ID",
ids,
func(uncached []string) ([]*gtsmodel.StatusBookmark, error) {
// Avoid querying
// if none uncached.
count := len(uncached)
if count == 0 {
return nil, nil
}
// Preallocate expected length of uncached bookmarks.
bookmarks := make([]*gtsmodel.StatusBookmark, 0, count)
bookmarks := make([]*gtsmodel.StatusBookmark, 0, len(uncached))
// Perform database query scanning
// the remaining (uncached) bookmarks.
@ -264,60 +257,86 @@ func (s *statusBookmarkDB) PutStatusBookmark(ctx context.Context, bookmark *gtsm
}
func (s *statusBookmarkDB) DeleteStatusBookmarkByID(ctx context.Context, id string) error {
_, err := s.db.
NewDelete().
Table("status_bookmarks").
// Gather necessary fields from
// deleted for cache invaliation.
var deleted gtsmodel.StatusBookmark
deleted.ID = id
// Delete block with given URI,
// returning the deleted models.
if _, err := s.db.NewDelete().
Model(&deleted).
Where("? = ?", bun.Ident("id"), id).
Exec(ctx)
if err != nil {
Returning("?", bun.Ident("status_id")).
Exec(ctx); err != nil &&
!errors.Is(err, db.ErrNoEntries) {
return err
}
// Invalidate cached status bookmark by its ID,
// manually call invalidate hook in case not cached.
s.state.Caches.DB.StatusBookmark.Invalidate("ID", id)
s.state.Caches.OnInvalidateStatusBookmark(&deleted)
return nil
}
func (s *statusBookmarkDB) DeleteStatusBookmarks(ctx context.Context, targetAccountID string, originAccountID string) error {
if targetAccountID == "" && originAccountID == "" {
return errors.New("DeleteBookmarks: one of targetAccountID or originAccountID must be set")
return gtserror.New("one of targetAccountID or originAccountID must be set")
}
// Gather necessary fields from
// deleted for cache invaliation.
var deleted []*gtsmodel.StatusBookmark
q := s.db.
NewDelete().
TableExpr("? AS ?", bun.Ident("status_bookmarks"), bun.Ident("status_bookmark"))
Model(&deleted).
Returning("?", bun.Ident("status_id"))
if targetAccountID != "" {
q = q.Where("? = ?", bun.Ident("status_bookmark.target_account_id"), targetAccountID)
defer s.state.Caches.DB.StatusBookmark.Invalidate("TargetAccountID", targetAccountID)
q = q.Where("? = ?", bun.Ident("target_account_id"), targetAccountID)
}
if originAccountID != "" {
q = q.Where("? = ?", bun.Ident("status_bookmark.account_id"), originAccountID)
defer s.state.Caches.DB.StatusBookmark.Invalidate("AccountID", originAccountID)
q = q.Where("? = ?", bun.Ident("account_id"), originAccountID)
}
if _, err := q.Exec(ctx); err != nil {
if _, err := q.Exec(ctx); err != nil &&
!errors.Is(err, db.ErrNoEntries) {
return err
}
if targetAccountID != "" {
s.state.Caches.DB.StatusBookmark.Invalidate("TargetAccountID", targetAccountID)
}
if originAccountID != "" {
s.state.Caches.DB.StatusBookmark.Invalidate("AccountID", originAccountID)
for _, deleted := range deleted {
// Invalidate cached status bookmark by status ID,
// manually call invalidate hook in case not cached.
s.state.Caches.DB.StatusBookmark.Invalidate("StatusID", deleted.StatusID)
s.state.Caches.OnInvalidateStatusBookmark(deleted)
}
return nil
}
func (s *statusBookmarkDB) DeleteStatusBookmarksForStatus(ctx context.Context, statusID string) error {
q := s.db.
NewDelete().
// Delete status bookmarks
// from database by status ID.
q := s.db.NewDelete().
TableExpr("? AS ?", bun.Ident("status_bookmarks"), bun.Ident("status_bookmark")).
Where("? = ?", bun.Ident("status_bookmark.status_id"), statusID)
if _, err := q.Exec(ctx); err != nil {
return err
}
// Wrap provided ID in a bookmark
// model for calling cache hook.
var deleted gtsmodel.StatusBookmark
deleted.StatusID = statusID
// Invalidate cached status bookmark by status ID,
// manually call invalidate hook in case not cached.
s.state.Caches.DB.StatusBookmark.Invalidate("StatusID", statusID)
s.state.Caches.OnInvalidateStatusBookmark(&deleted)
return nil
}

View File

@ -133,15 +133,8 @@ func (s *statusFaveDB) GetStatusFaves(ctx context.Context, statusID string) ([]*
faves, err := s.state.Caches.DB.StatusFave.LoadIDs("ID",
faveIDs,
func(uncached []string) ([]*gtsmodel.StatusFave, error) {
// Avoid querying
// if none uncached.
count := len(uncached)
if count == 0 {
return nil, nil
}
// Preallocate expected length of uncached faves.
faves := make([]*gtsmodel.StatusFave, 0, count)
faves := make([]*gtsmodel.StatusFave, 0, len(uncached))
// Perform database query scanning
// the remaining (uncached) fave IDs.

View File

@ -20,6 +20,7 @@ package bundb
import (
"context"
"errors"
"slices"
"strings"
"github.com/superseriousbusiness/gotosocial/internal/db"
@ -79,15 +80,8 @@ func (t *tagDB) GetTags(ctx context.Context, ids []string) ([]*gtsmodel.Tag, err
tags, err := t.state.Caches.DB.Tag.LoadIDs("ID",
ids,
func(uncached []string) ([]*gtsmodel.Tag, error) {
// Avoid querying
// if none uncached.
count := len(uncached)
if count == 0 {
return nil, nil
}
// Preallocate expected length of uncached tags.
tags := make([]*gtsmodel.Tag, 0, count)
tags := make([]*gtsmodel.Tag, 0, len(uncached))
// Perform database query scanning
// the remaining (uncached) IDs.
@ -148,17 +142,11 @@ func (t *tagDB) GetFollowedTags(ctx context.Context, accountID string, page *pag
if err != nil {
return nil, err
}
tags, err := t.GetTags(ctx, tagIDs)
if err != nil {
return nil, err
}
return tags, nil
return t.GetTags(ctx, tagIDs)
}
func (t *tagDB) getTagIDsFollowedByAccount(ctx context.Context, accountID string, page *paging.Page) ([]string, error) {
return loadPagedIDs(&t.state.Caches.DB.TagIDsFollowedByAccount, accountID, page, func() ([]string, error) {
return loadPagedIDs(&t.state.Caches.DB.FollowingTagIDs, ">"+accountID, page, func() ([]string, error) {
var tagIDs []string
// Tag IDs not in cache. Perform DB query.
@ -178,7 +166,7 @@ func (t *tagDB) getTagIDsFollowedByAccount(ctx context.Context, accountID string
}
func (t *tagDB) getAccountIDsFollowingTag(ctx context.Context, tagID string) ([]string, error) {
return loadPagedIDs(&t.state.Caches.DB.AccountIDsFollowingTag, tagID, nil, func() ([]string, error) {
return loadPagedIDs(&t.state.Caches.DB.FollowingTagIDs, "<"+tagID, nil, func() ([]string, error) {
var accountIDs []string
// Account IDs not in cache. Perform DB query.
@ -198,18 +186,11 @@ func (t *tagDB) getAccountIDsFollowingTag(ctx context.Context, tagID string) ([]
}
func (t *tagDB) IsAccountFollowingTag(ctx context.Context, accountID string, tagID string) (bool, error) {
accountTagIDs, err := t.getTagIDsFollowedByAccount(ctx, accountID, nil)
followingTagIDs, err := t.getTagIDsFollowedByAccount(ctx, accountID, nil)
if err != nil {
return false, err
}
for _, accountTagID := range accountTagIDs {
if accountTagID == tagID {
return true, nil
}
}
return false, nil
return slices.Contains(followingTagIDs, tagID), nil
}
func (t *tagDB) PutFollowedTag(ctx context.Context, accountID string, tagID string) error {
@ -234,9 +215,15 @@ func (t *tagDB) PutFollowedTag(ctx context.Context, accountID string, tagID stri
return nil
}
// Otherwise, this is a new followed tag, so we invalidate caches related to it.
t.state.Caches.DB.AccountIDsFollowingTag.Invalidate(tagID)
t.state.Caches.DB.TagIDsFollowedByAccount.Invalidate(accountID)
// We updated something, invalidate caches.
t.state.Caches.DB.FollowingTagIDs.Invalidate(
// tag IDs followed by account
">"+accountID,
// account IDs following tag
"<"+tagID,
)
return nil
}
@ -259,9 +246,15 @@ func (t *tagDB) DeleteFollowedTag(ctx context.Context, accountID string, tagID s
return nil
}
// If we deleted anything, invalidate caches related to it.
t.state.Caches.DB.AccountIDsFollowingTag.Invalidate(tagID)
t.state.Caches.DB.TagIDsFollowedByAccount.Invalidate(accountID)
// We deleted something, invalidate caches.
t.state.Caches.DB.FollowingTagIDs.Invalidate(
// tag IDs followed by account
">"+accountID,
// account IDs following tag
"<"+tagID,
)
return err
}
@ -278,16 +271,26 @@ func (t *tagDB) DeleteFollowedTagsByAccountID(ctx context.Context, accountID str
return gtserror.Newf("error deleting followed tags for account %s: %w", accountID, err)
}
// Invalidate account ID caches for the account and those tags.
t.state.Caches.DB.TagIDsFollowedByAccount.Invalidate(accountID)
t.state.Caches.DB.AccountIDsFollowingTag.Invalidate(tagIDs...)
// Convert tag IDs to the keys
// we use for caching tag follow
// and following IDs.
keys := tagIDs
for i := range keys {
keys[i] = "<" + keys[i]
}
keys = append(keys, ">"+accountID)
// If we deleted anything, invalidate caches with keys.
t.state.Caches.DB.FollowingTagIDs.Invalidate(keys...)
return nil
}
func (t *tagDB) GetAccountIDsFollowingTagIDs(ctx context.Context, tagIDs []string) ([]string, error) {
// Accounts might be following multiple tags in this list, but we only want to return each account once.
accountIDs := []string{}
// Make conservative estimate for no. accounts.
accountIDs := make([]string, 0, len(tagIDs))
// Gather all accounts following tags.
for _, tagID := range tagIDs {
tagAccountIDs, err := t.getAccountIDsFollowingTag(ctx, tagID)
if err != nil {
@ -295,5 +298,8 @@ func (t *tagDB) GetAccountIDsFollowingTagIDs(ctx context.Context, tagIDs []strin
}
accountIDs = append(accountIDs, tagAccountIDs...)
}
// Accounts might be following multiple tags in list,
// but we only want to return each account once.
return util.Deduplicate(accountIDs), nil
}

View File

@ -70,7 +70,7 @@ func (t *timelineDB) GetHomeTimeline(ctx context.Context, accountID string, maxI
// To take account of exclusive lists, get all of
// this account's lists, so we can filter out follows
// that are in contained in exclusive lists.
lists, err := t.state.DB.GetListsForAccountID(ctx, accountID)
lists, err := t.state.DB.GetListsByAccountID(ctx, accountID)
if err != nil && !errors.Is(err, db.ErrNoEntries) {
return nil, gtserror.Newf("db error getting lists for account %s: %w", accountID, err)
}
@ -84,9 +84,15 @@ func (t *timelineDB) GetHomeTimeline(ctx context.Context, accountID string, maxI
continue
}
// Fetch all follow IDs of the entries ccontained in this list.
listFollowIDs, err := t.state.DB.GetFollowIDsInList(ctx, list.ID, nil)
if err != nil && !errors.Is(err, db.ErrNoEntries) {
return nil, gtserror.Newf("db error getting list entry follow ids: %w", err)
}
// Exclusive list, index all its follow IDs.
for _, listEntry := range list.ListEntries {
ignoreFollowIDs[listEntry.FollowID] = struct{}{}
for _, followID := range listFollowIDs {
ignoreFollowIDs[followID] = struct{}{}
}
}
@ -370,30 +376,20 @@ func (t *timelineDB) GetListTimeline(
frontToBack = true
)
// Fetch all listEntries entries from the database.
listEntries, err := t.state.DB.GetListEntries(
// Don't need actual follows
// for this, just the IDs.
gtscontext.SetBarebones(ctx),
listID,
"", "", "", 0,
// Fetch all follow IDs contained in list from DB.
followIDs, err := t.state.DB.GetFollowIDsInList(
ctx, listID, nil,
)
if err != nil {
return nil, fmt.Errorf("error getting entries for list %s: %w", listID, err)
return nil, fmt.Errorf("error getting follows in list: %w", err)
}
// If there's no list entries we can't
// If there's no list follows we can't
// possibly return anything for this list.
if len(listEntries) == 0 {
if len(followIDs) == 0 {
return make([]*gtsmodel.Status, 0), nil
}
// Extract just the IDs of each follow.
followIDs := make([]string, 0, len(listEntries))
for _, listEntry := range listEntries {
followIDs = append(followIDs, listEntry.FollowID)
}
// Select target account IDs from follows.
subQ := t.db.
NewSelect().

View File

@ -184,8 +184,8 @@ func (suite *TimelineTestSuite) TestGetHomeTimelineIgnoreExclusive() {
suite.checkStatuses(s, id.Highest, id.Lowest, 8)
// Remove admin account from the exclusive list.
listEntryID := suite.testListEntries["local_account_1_list_1_entry_2"].ID
if err := suite.db.DeleteListEntry(ctx, listEntryID); err != nil {
listEntry := suite.testListEntries["local_account_1_list_1_entry_2"]
if err := suite.db.DeleteListEntry(ctx, listEntry.ListID, listEntry.FollowID); err != nil {
suite.FailNow(err.Error())
}

View File

@ -67,12 +67,14 @@ func (t *tombstoneDB) PutTombstone(ctx context.Context, tombstone *gtsmodel.Tomb
}
func (t *tombstoneDB) DeleteTombstone(ctx context.Context, id string) error {
defer t.state.Caches.DB.Tombstone.Invalidate("ID", id)
// Delete tombstone from DB.
_, err := t.db.NewDelete().
TableExpr("? AS ?", bun.Ident("tombstones"), bun.Ident("tombstone")).
Where("? = ?", bun.Ident("tombstone.id"), id).
Exec(ctx)
// Invalidate any cached tombstone by given ID.
t.state.Caches.DB.Tombstone.Invalidate("ID", id)
return err
}

View File

@ -19,10 +19,8 @@ package bundb
import (
"context"
"errors"
"time"
"github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/gtscontext"
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
@ -209,26 +207,26 @@ func (u *userDB) UpdateUser(ctx context.Context, user *gtsmodel.User, columns ..
}
func (u *userDB) DeleteUserByID(ctx context.Context, userID string) error {
defer u.state.Caches.DB.User.Invalidate("ID", userID)
// Gather necessary fields from
// deleted for cache invaliation.
var deleted gtsmodel.User
deleted.ID = userID
// Load user into cache before attempting a delete,
// as we need it cached in order to trigger the invalidate
// callback. This in turn invalidates others.
_, err := u.GetUserByID(gtscontext.SetBarebones(ctx), userID)
if err != nil {
if errors.Is(err, db.ErrNoEntries) {
// not an issue.
err = nil
}
// Delete user from DB.
if _, err := u.db.NewDelete().
Model(&deleted).
Where("? = ?", bun.Ident("id"), userID).
Returning("?", bun.Ident("account_id")).
Exec(ctx); err != nil {
return err
}
// Finally delete user from DB.
_, err = u.db.NewDelete().
TableExpr("? AS ?", bun.Ident("users"), bun.Ident("user")).
Where("? = ?", bun.Ident("user.id"), userID).
Exec(ctx)
return err
// Invalidate cached user by ID, manually
// call invalidate hook in case not cached.
u.state.Caches.DB.User.Invalidate("ID", userID)
u.state.Caches.OnInvalidateUser(&deleted)
return nil
}
func (u *userDB) PutDeniedUser(ctx context.Context, deniedUser *gtsmodel.DeniedUser) error {

View File

@ -21,6 +21,7 @@ import (
"context"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/internal/paging"
)
type List interface {
@ -30,11 +31,29 @@ type List interface {
// GetListsByIDs fetches all lists with the provided IDs.
GetListsByIDs(ctx context.Context, ids []string) ([]*gtsmodel.List, error)
// GetListsForAccountID gets all lists owned by the given accountID.
GetListsForAccountID(ctx context.Context, accountID string) ([]*gtsmodel.List, error)
// GetListsByAccountID gets all lists owned by the given accountID.
GetListsByAccountID(ctx context.Context, accountID string) ([]*gtsmodel.List, error)
// CountListsForAccountID counts the number of lists owned by the given accountID.
CountListsForAccountID(ctx context.Context, accountID string) (int, error)
// CountListsByAccountID counts the number of lists owned by the given accountID.
CountListsByAccountID(ctx context.Context, accountID string) (int, error)
// GetListsContainingFollowID gets all lists that contain the given follow with ID.
GetListsContainingFollowID(ctx context.Context, followID string) ([]*gtsmodel.List, error)
// GetFollowIDsInList returns all the follow IDs contained within given list ID.
GetFollowIDsInList(ctx context.Context, listID string, page *paging.Page) ([]string, error)
// GetFollowsInList returns all the follows contained within given list ID.
GetFollowsInList(ctx context.Context, listID string, page *paging.Page) ([]*gtsmodel.Follow, error)
// GetAccountIDsInList return all the account IDs (follow targets) contained within given list ID.
GetAccountIDsInList(ctx context.Context, listID string, page *paging.Page) ([]string, error)
// GetAccountsInList return all the accounts (follow targets) contained within given list ID.
GetAccountsInList(ctx context.Context, listID string, page *paging.Page) ([]*gtsmodel.Account, error)
// IsAccountInListID returns whether given account with ID is in the list with ID.
IsAccountInList(ctx context.Context, listID string, accountID string) (bool, error)
// PopulateList ensures that the list's struct fields are populated.
PopulateList(ctx context.Context, list *gtsmodel.List) error
@ -49,31 +68,13 @@ type List interface {
// DeleteListByID deletes one list with the given ID.
DeleteListByID(ctx context.Context, id string) error
// GetListEntryByID gets one list entry with the given ID.
GetListEntryByID(ctx context.Context, id string) (*gtsmodel.ListEntry, error)
// GetListEntriesyIDs fetches all list entries with the provided IDs.
GetListEntriesByIDs(ctx context.Context, ids []string) ([]*gtsmodel.ListEntry, error)
// GetListEntries gets list entries from the given listID, using the given parameters.
GetListEntries(ctx context.Context, listID string, maxID string, sinceID string, minID string, limit int) ([]*gtsmodel.ListEntry, error)
// GetListEntriesForFollowID returns all listEntries that pertain to the given followID.
GetListEntriesForFollowID(ctx context.Context, followID string) ([]*gtsmodel.ListEntry, error)
// PopulateListEntry ensures that the listEntry's struct fields are populated.
PopulateListEntry(ctx context.Context, listEntry *gtsmodel.ListEntry) error
// PutListEntries inserts a slice of listEntries into the database.
// It uses a transaction to ensure no partial updates.
PutListEntries(ctx context.Context, listEntries []*gtsmodel.ListEntry) error
// DeleteListEntry deletes one list entry with the given id.
DeleteListEntry(ctx context.Context, id string) error
// DeleteListEntry deletes the list entry with given list ID and follow ID.
DeleteListEntry(ctx context.Context, listID string, followID string) error
// DeleteListEntryForFollowID deletes all list entries with the given followID.
DeleteListEntriesForFollowID(ctx context.Context, followID string) error
// ListIncludesAccount returns true if the given listID includes the given accountID.
ListIncludesAccount(ctx context.Context, listID string, accountID string) (bool, error)
// DeleteAllListEntryByFollow deletes all list entries with the given followIDs.
DeleteAllListEntriesByFollows(ctx context.Context, followIDs ...string) error
}

View File

@ -39,7 +39,8 @@ type Poll interface {
// UpdatePoll updates the Poll in the database, only on selected columns if provided (else, all).
UpdatePoll(ctx context.Context, poll *gtsmodel.Poll, cols ...string) error
// DeletePollByID deletes the Poll with given ID from the database.
// DeletePollByID deletes the Poll with given ID from the
// database, along with all its associated poll votes.
DeletePollByID(ctx context.Context, id string) error
// GetPollVoteByID gets the PollVote with given ID from the database.
@ -57,9 +58,6 @@ type Poll interface {
// PutPollVote puts the given PollVote in the database.
PutPollVote(ctx context.Context, vote *gtsmodel.PollVote) error
// DeletePollVotes deletes all PollVotes in Poll with given ID from the database.
DeletePollVotes(ctx context.Context, pollID string) error
// DeletePollVoteBy deletes the PollVote in Poll with ID, by account ID, from the database.
DeletePollVoteBy(ctx context.Context, pollID string, accountID string) error

View File

@ -68,6 +68,9 @@ type Relationship interface {
// GetFollow retrieves a follow if it exists between source and target accounts.
GetFollow(ctx context.Context, sourceAccountID string, targetAccountID string) (*gtsmodel.Follow, error)
// GetFollowsByIDs fetches all follows from database with given IDs.
GetFollowsByIDs(ctx context.Context, ids []string) ([]*gtsmodel.Follow, error)
// PopulateFollow populates the struct pointers on the given follow.
PopulateFollow(ctx context.Context, follow *gtsmodel.Follow) error

View File

@ -44,7 +44,7 @@ type Report interface {
// provided, then all columns will be updated.
// updated_at will also be updated, no need to pass this
// as a specific column.
UpdateReport(ctx context.Context, report *gtsmodel.Report, columns ...string) (*gtsmodel.Report, error)
UpdateReport(ctx context.Context, report *gtsmodel.Report, columns ...string) error
// DeleteReportByID deletes report with the given id.
DeleteReportByID(ctx context.Context, id string) error

View File

@ -826,9 +826,6 @@ func (d *Dereferencer) fetchStatusPoll(
if err := d.state.DB.DeletePollByID(ctx, pollID); err != nil {
return gtserror.Newf("error deleting existing poll from database: %w", err)
}
if err := d.state.DB.DeletePollVotes(ctx, pollID); err != nil {
return gtserror.Newf("error deleting existing votes from database: %w", err)
}
return nil
}
)

View File

@ -27,7 +27,6 @@ type List struct {
Title string `bun:",nullzero,notnull,unique:listaccounttitle"` // Title of this list.
AccountID string `bun:"type:CHAR(26),notnull,nullzero,unique:listaccounttitle"` // Account that created/owns the list
Account *Account `bun:"-"` // Account corresponding to accountID
ListEntries []*ListEntry `bun:"-"` // Entries contained by this list.
RepliesPolicy RepliesPolicy `bun:",nullzero,notnull,default:'followed'"` // RepliesPolicy for this list.
Exclusive *bool `bun:",nullzero,notnull,default:false"` // Hide posts from members of this list from your home timeline.
}

View File

@ -98,7 +98,7 @@ func (p *Processor) ExportLists(
ctx context.Context,
requester *gtsmodel.Account,
) ([][]string, gtserror.WithCode) {
lists, err := p.state.DB.GetListsForAccountID(ctx, requester.ID)
lists, err := p.state.DB.GetListsByAccountID(ctx, requester.ID)
if err != nil && !errors.Is(err, db.ErrNoEntries) {
err = gtserror.Newf("db error getting lists: %w", err)
return nil, gtserror.NewErrorInternalError(err)

View File

@ -30,8 +30,6 @@ import (
"github.com/superseriousbusiness/gotosocial/internal/log"
)
var noLists = make([]*apimodel.List, 0)
// ListsGet returns all lists owned by requestingAccount, which contain a follow for targetAccountID.
func (p *Processor) ListsGet(ctx context.Context, requestingAccount *gtsmodel.Account, targetAccountID string) ([]*apimodel.List, gtserror.WithCode) {
targetAccount, err := p.state.DB.GetAccountByID(ctx, targetAccountID)
@ -54,52 +52,35 @@ func (p *Processor) ListsGet(ctx context.Context, requestingAccount *gtsmodel.Ac
// Requester has to follow targetAccount
// for them to be in any of their lists.
follow, err := p.state.DB.GetFollow(
// Don't populate follow.
gtscontext.SetBarebones(ctx),
requestingAccount.ID,
targetAccountID,
)
if err != nil && !errors.Is(err, db.ErrNoEntries) {
return nil, gtserror.NewErrorInternalError(fmt.Errorf("db error: %w", err))
err := gtserror.Newf("error getting follow: %w", err)
return nil, gtserror.NewErrorInternalError(err)
}
if follow == nil {
return noLists, nil // by definition we know they're in no lists
return []*apimodel.List{}, nil
}
listEntries, err := p.state.DB.GetListEntriesForFollowID(
// Don't populate entries.
gtscontext.SetBarebones(ctx),
follow.ID,
)
if err != nil && !errors.Is(err, db.ErrNoEntries) {
return nil, gtserror.NewErrorInternalError(fmt.Errorf("db error: %w", err))
// Get all lists that this follow is an entry within.
lists, err := p.state.DB.GetListsContainingFollowID(ctx, follow.ID)
if err != nil {
err := gtserror.Newf("error getting lists for follow: %w", err)
return nil, gtserror.NewErrorInternalError(err)
}
count := len(listEntries)
if count == 0 {
return noLists, nil
}
apiLists := make([]*apimodel.List, 0, count)
for _, listEntry := range listEntries {
list, err := p.state.DB.GetListByID(
// Don't populate list.
gtscontext.SetBarebones(ctx),
listEntry.ListID,
)
if err != nil {
log.Debugf(ctx, "skipping list %s due to error %q", listEntry.ListID, err)
continue
}
apiLists := make([]*apimodel.List, 0, len(lists))
for _, list := range lists {
apiList, err := p.converter.ListToAPIList(ctx, list)
if err != nil {
log.Debugf(ctx, "skipping list %s due to error %q", listEntry.ListID, err)
log.Errorf(ctx, "error converting list: %v", err)
continue
}
apiLists = append(apiLists, apiList)
}

View File

@ -142,7 +142,7 @@ func (p *Processor) ReportResolve(ctx context.Context, account *gtsmodel.Account
columns = append(columns, "action_taken")
}
updatedReport, err := p.state.DB.UpdateReport(ctx, report, columns...)
err = p.state.DB.UpdateReport(ctx, report, columns...)
if err != nil {
return nil, gtserror.NewErrorInternalError(err)
}
@ -156,7 +156,7 @@ func (p *Processor) ReportResolve(ctx context.Context, account *gtsmodel.Account
Target: report.Account,
})
apimodelReport, err := p.converter.ReportToAdminAPIReport(ctx, updatedReport, account)
apimodelReport, err := p.converter.ReportToAdminAPIReport(ctx, report, account)
if err != nil {
return nil, gtserror.NewErrorInternalError(err)
}

View File

@ -189,7 +189,7 @@ func (p *Processor) GetAPIStatus(
// such invalidation will, in that case, be handled by the processor instead.
func (p *Processor) InvalidateTimelinedStatus(ctx context.Context, accountID string, statusID string) error {
// Get lists first + bail if this fails.
lists, err := p.state.DB.GetListsForAccountID(ctx, accountID)
lists, err := p.state.DB.GetListsByAccountID(ctx, accountID)
if err != nil {
return gtserror.Newf("db error getting lists for account %s: %w", accountID, err)
}

View File

@ -20,7 +20,6 @@ package list
import (
"context"
"errors"
"fmt"
apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
"github.com/superseriousbusiness/gotosocial/internal/db"
@ -28,7 +27,7 @@ import (
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/internal/log"
"github.com/superseriousbusiness/gotosocial/internal/util"
"github.com/superseriousbusiness/gotosocial/internal/paging"
)
// Get returns the api model of one list with the given ID.
@ -49,16 +48,14 @@ func (p *Processor) Get(ctx context.Context, account *gtsmodel.Account, id strin
// GetAll returns multiple lists created by the given account, sorted by list ID DESC (newest first).
func (p *Processor) GetAll(ctx context.Context, account *gtsmodel.Account) ([]*apimodel.List, gtserror.WithCode) {
lists, err := p.state.DB.GetListsForAccountID(
lists, err := p.state.DB.GetListsByAccountID(
// Use barebones ctx; no embedded
// structs necessary for simple GET.
gtscontext.SetBarebones(ctx),
account.ID,
)
if err != nil {
if errors.Is(err, db.ErrNoEntries) {
return nil, nil
}
if err != nil && !errors.Is(err, db.ErrNoEntries) {
return nil, gtserror.NewErrorInternalError(err)
}
@ -68,66 +65,23 @@ func (p *Processor) GetAll(ctx context.Context, account *gtsmodel.Account) ([]*a
if errWithCode != nil {
return nil, errWithCode
}
apiLists = append(apiLists, apiList)
}
return apiLists, nil
}
// GetAllListAccounts returns all accounts that are in the given list,
// owned by the given account. There's no pagination for this endpoint.
//
// See https://docs.joinmastodon.org/methods/lists/#query-parameters:
//
// Limit: Integer. Maximum number of results. Defaults to 40 accounts.
// Max 80 accounts. Set to 0 in order to get all accounts without pagination.
func (p *Processor) GetAllListAccounts(
ctx context.Context,
account *gtsmodel.Account,
listID string,
) ([]*apimodel.Account, gtserror.WithCode) {
// Ensure list exists + is owned by requesting account.
_, errWithCode := p.getList(
// Use barebones ctx; no embedded
// structs necessary for this call.
gtscontext.SetBarebones(ctx),
account.ID,
listID,
)
if errWithCode != nil {
return nil, errWithCode
}
// Get all entries for this list.
listEntries, err := p.state.DB.GetListEntries(ctx, listID, "", "", "", 0)
if err != nil && !errors.Is(err, db.ErrNoEntries) {
err = gtserror.Newf("error getting list entries: %w", err)
return nil, gtserror.NewErrorInternalError(err)
}
// Extract accounts from list entries + add them to response.
accounts := make([]*apimodel.Account, 0, len(listEntries))
p.accountsFromListEntries(ctx, listEntries, func(acc *apimodel.Account) {
accounts = append(accounts, acc)
})
return accounts, nil
}
// GetListAccounts returns accounts that are in the given list, owned by the given account.
// The additional parameters can be used for paging.
// The additional parameters can be used for paging. Nil page param returns all accounts.
func (p *Processor) GetListAccounts(
ctx context.Context,
account *gtsmodel.Account,
listID string,
maxID string,
sinceID string,
minID string,
limit int,
page *paging.Page,
) (*apimodel.PageableResponse, gtserror.WithCode) {
// Ensure list exists + is owned by requesting account.
_, errWithCode := p.getList(
// Use barebones ctx; no embedded
// structs necessary for this call.
gtscontext.SetBarebones(ctx),
@ -138,71 +92,45 @@ func (p *Processor) GetListAccounts(
return nil, errWithCode
}
// To know which accounts are in the list,
// we need to first get requested list entries.
listEntries, err := p.state.DB.GetListEntries(ctx, listID, maxID, sinceID, minID, limit)
if err != nil && !errors.Is(err, db.ErrNoEntries) {
err = fmt.Errorf("GetListAccounts: error getting list entries: %w", err)
// Get all accounts contained within list.
accounts, err := p.state.DB.GetAccountsInList(ctx,
listID,
page,
)
if err != nil {
err := gtserror.Newf("db error getting accounts in list: %w", err)
return nil, gtserror.NewErrorInternalError(err)
}
count := len(listEntries)
// Check for any accounts.
count := len(accounts)
if count == 0 {
// No list entries means no accounts.
return util.EmptyPageableResponse(), nil
return paging.EmptyResponse(), nil
}
var (
// Preallocate expected frontend items.
items = make([]interface{}, 0, count)
// Set next + prev values before filtering and API
// converting, so caller can still page properly.
nextMaxIDValue = listEntries[count-1].ID
prevMinIDValue = listEntries[0].ID
// Set paging low / high IDs.
lo = accounts[count-1].ID
hi = accounts[0].ID
)
// Extract accounts from list entries + add them to response.
p.accountsFromListEntries(ctx, listEntries, func(acc *apimodel.Account) {
items = append(items, acc)
})
return util.PackagePageableResponse(util.PageableResponseParams{
Items: items,
Path: "/api/v1/lists/" + listID + "/accounts",
NextMaxIDValue: nextMaxIDValue,
PrevMinIDValue: prevMinIDValue,
Limit: limit,
})
}
func (p *Processor) accountsFromListEntries(
ctx context.Context,
listEntries []*gtsmodel.ListEntry,
appendAcc func(*apimodel.Account),
) {
// For each list entry, we want the account it points to.
// To get this, we need to first get the follow that the
// list entry pertains to, then extract the target account
// from that follow.
//
// We do paging not by account ID, but by list entry ID.
for _, listEntry := range listEntries {
if err := p.state.DB.PopulateListEntry(ctx, listEntry); err != nil {
log.Errorf(ctx, "error populating list entry: %v", err)
continue
}
if err := p.state.DB.PopulateFollow(ctx, listEntry.Follow); err != nil {
log.Errorf(ctx, "error populating follow: %v", err)
continue
}
apiAccount, err := p.converter.AccountToAPIAccountPublic(ctx, listEntry.Follow.TargetAccount)
// Convert accounts to frontend.
for _, account := range accounts {
apiAccount, err := p.converter.AccountToAPIAccountPublic(ctx, account)
if err != nil {
log.Errorf(ctx, "error converting to public api account: %v", err)
log.Errorf(ctx, "error converting to api account: %v", err)
continue
}
appendAcc(apiAccount)
items = append(items, apiAccount)
}
return paging.PackageResponse(paging.ResponseParams{
Items: items,
Path: "/api/v1/lists/" + listID + "/accounts",
Next: page.Next(lo, hi),
Prev: page.Prev(lo, hi),
}), nil
}

View File

@ -23,73 +23,90 @@ import (
"fmt"
"github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/gtscontext"
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/internal/id"
"github.com/superseriousbusiness/gotosocial/internal/util"
)
// AddToList adds targetAccountIDs to the given list, if valid.
func (p *Processor) AddToList(ctx context.Context, account *gtsmodel.Account, listID string, targetAccountIDs []string) gtserror.WithCode {
// Ensure this list exists + account owns it.
list, errWithCode := p.getList(ctx, account.ID, listID)
_, errWithCode := p.getList(ctx, account.ID, listID)
if errWithCode != nil {
return errWithCode
}
// Pre-assemble list of entries to add. We *could* add these
// one by one as we iterate through accountIDs, but according
// to the Mastodon API we should only add them all once we know
// they're all valid, no partial updates.
listEntries := make([]*gtsmodel.ListEntry, 0, len(targetAccountIDs))
// Get all follows that are entries in list.
follows, err := p.state.DB.GetFollowsInList(
// Check each targetAccountID is valid.
// - Follow must exist.
// - Follow must not already be in the given list.
// We only need barebones model.
gtscontext.SetBarebones(ctx),
listID,
nil,
)
if err != nil {
err := gtserror.Newf("error getting list follows: %w", err)
return gtserror.NewErrorInternalError(err)
}
// Convert the follows to a hash set containing the target account IDs.
inFollows := util.ToSetFunc(follows, func(follow *gtsmodel.Follow) string {
return follow.TargetAccountID
})
// Preallocate a slice of expected list entries, we specifically
// gather and add all the target accounts in one go rather than
// individually, to ensure we don't end up with partial updates.
entries := make([]*gtsmodel.ListEntry, 0, len(targetAccountIDs))
// Iterate all the account IDs in given target list.
for _, targetAccountID := range targetAccountIDs {
// Ensure follow exists.
follow, err := p.state.DB.GetFollow(ctx, account.ID, targetAccountID)
if err != nil {
if errors.Is(err, db.ErrNoEntries) {
err = fmt.Errorf("you do not follow account %s", targetAccountID)
return gtserror.NewErrorNotFound(err, err.Error())
}
// Look for follow to target account.
if inFollows.Has(targetAccountID) {
text := fmt.Sprintf("account %s is already in list %s", targetAccountID, listID)
return gtserror.NewErrorUnprocessableEntity(errors.New(text), text)
}
// Get the actual follow to target.
follow, err := p.state.DB.GetFollow(
// We don't need any sub-models.
gtscontext.SetBarebones(ctx),
account.ID,
targetAccountID,
)
if err != nil && !errors.Is(err, db.ErrNoEntries) {
err := gtserror.Newf("db error getting follow: %w", err)
return gtserror.NewErrorInternalError(err)
}
// Ensure followID not already in list.
// This particular call to isInList will
// never error, so just check entryID.
entryID, _ := isInList(
list,
follow.ID,
func(listEntry *gtsmodel.ListEntry) (string, error) {
// Looking for the listEntry follow ID.
return listEntry.FollowID, nil
},
)
// Empty entryID means entry with given
// followID wasn't found in the list.
if entryID != "" {
err = fmt.Errorf("account with id %s is already in list %s with entryID %s", targetAccountID, listID, entryID)
return gtserror.NewErrorUnprocessableEntity(err, err.Error())
if follow == nil {
text := fmt.Sprintf("account %s not currently followed", targetAccountID)
return gtserror.NewErrorNotFound(errors.New(text), text)
}
// Entry wasn't in the list, we can add it.
listEntries = append(listEntries, &gtsmodel.ListEntry{
// Generate new entry for this follow in list.
entries = append(entries, &gtsmodel.ListEntry{
ID: id.NewULID(),
ListID: listID,
FollowID: follow.ID,
})
}
// If we get to here we can assume all
// entries are valid, so try to add them.
if err := p.state.DB.PutListEntries(ctx, listEntries); err != nil {
if errors.Is(err, db.ErrAlreadyExists) {
err = fmt.Errorf("one or more errors inserting list entries: %w", err)
return gtserror.NewErrorUnprocessableEntity(err, err.Error())
}
// Add all of the gathered list entries to the database.
switch err := p.state.DB.PutListEntries(ctx, entries); {
case err == nil:
case errors.Is(err, db.ErrAlreadyExists):
err := gtserror.Newf("conflict adding list entry: %w", err)
return gtserror.NewErrorUnprocessableEntity(err)
default:
err := gtserror.Newf("db error inserting list entries: %w", err)
return gtserror.NewErrorInternalError(err)
}
@ -97,55 +114,61 @@ func (p *Processor) AddToList(ctx context.Context, account *gtsmodel.Account, li
}
// RemoveFromList removes targetAccountIDs from the given list, if valid.
func (p *Processor) RemoveFromList(ctx context.Context, account *gtsmodel.Account, listID string, targetAccountIDs []string) gtserror.WithCode {
func (p *Processor) RemoveFromList(
ctx context.Context,
account *gtsmodel.Account,
listID string,
targetAccountIDs []string,
) gtserror.WithCode {
// Ensure this list exists + account owns it.
list, errWithCode := p.getList(ctx, account.ID, listID)
_, errWithCode := p.getList(ctx, account.ID, listID)
if errWithCode != nil {
return errWithCode
}
// For each targetAccountID, we want to check if
// a follow with that targetAccountID is in the
// given list. If it is in there, we want to remove
// it from the list.
// Get all follows that are entries in list.
follows, err := p.state.DB.GetFollowsInList(
// We only need barebones model.
gtscontext.SetBarebones(ctx),
listID,
nil,
)
if err != nil {
err := gtserror.Newf("error getting list follows: %w", err)
return gtserror.NewErrorInternalError(err)
}
// Convert the follows to a map keyed by the target account ID.
followsMap := util.KeyBy(follows, func(follow *gtsmodel.Follow) string {
return follow.TargetAccountID
})
var errs gtserror.MultiError
// Iterate all the account IDs in given target list.
for _, targetAccountID := range targetAccountIDs {
// Check if targetAccountID is
// on a follow in the list.
entryID, err := isInList(
list,
targetAccountID,
func(listEntry *gtsmodel.ListEntry) (string, error) {
// We need the follow so populate this
// entry, if it's not already populated.
if err := p.state.DB.PopulateListEntry(ctx, listEntry); err != nil {
return "", err
}
// Looking for the list entry targetAccountID.
return listEntry.Follow.TargetAccountID, nil
},
)
// Look for follow targetting this account.
follow, ok := followsMap[targetAccountID]
// Error may be returned here if there was an issue
// populating the list entry. We only return on proper
// DB errors, we can just skip no entry errors.
if err != nil && !errors.Is(err, db.ErrNoEntries) {
err = fmt.Errorf("error checking if targetAccountID %s was in list %s: %w", targetAccountID, listID, err)
return gtserror.NewErrorInternalError(err)
}
if entryID == "" {
// There was an errNoEntries or targetAccount
// wasn't in this list anyway, so we can skip it.
if !ok {
// not in list.
continue
}
// TargetAccount was in the list, remove the entry.
if err := p.state.DB.DeleteListEntry(ctx, entryID); err != nil && !errors.Is(err, db.ErrNoEntries) {
err = fmt.Errorf("error removing list entry %s from list %s: %w", entryID, listID, err)
return gtserror.NewErrorInternalError(err)
// Delete the list entry containing follow ID in list.
err := p.state.DB.DeleteListEntry(ctx, listID, follow.ID)
if err != nil && !errors.Is(err, db.ErrNoEntries) {
errs.Appendf("error removing list entry: %w", err)
continue
}
}
// Wrap errors in errWithCode if set.
if err := errs.Combine(); err != nil {
return gtserror.NewErrorInternalError(err)
}
return nil
}

View File

@ -33,18 +33,25 @@ import (
// appropriate errors so caller doesn't need to bother.
func (p *Processor) getList(ctx context.Context, accountID string, listID string) (*gtsmodel.List, gtserror.WithCode) {
list, err := p.state.DB.GetListByID(ctx, listID)
if err != nil {
if errors.Is(err, db.ErrNoEntries) {
// List doesn't seem to exist.
return nil, gtserror.NewErrorNotFound(err)
}
// Real database error.
if err != nil && !errors.Is(err, db.ErrNoEntries) {
err := gtserror.Newf("db error getting list: %w", err)
return nil, gtserror.NewErrorInternalError(err)
}
if list == nil {
const text = "list not found"
return nil, gtserror.NewErrorNotFound(
errors.New(text),
text,
)
}
if list.AccountID != accountID {
err = fmt.Errorf("list with id %s does not belong to account %s", list.ID, accountID)
return nil, gtserror.NewErrorNotFound(err)
const text = "list not found"
return nil, gtserror.NewErrorNotFound(
errors.New("list does not belong to account"),
text,
)
}
return list, nil
@ -60,26 +67,3 @@ func (p *Processor) apiList(ctx context.Context, list *gtsmodel.List) (*apimodel
return apiList, nil
}
// isInList check if thisID is equal to the result of thatID
// for any entry in the given list.
//
// Will return the id of the listEntry if true, empty if false,
// or an error if the result of thatID returns an error.
func isInList(
list *gtsmodel.List,
thisID string,
getThatID func(listEntry *gtsmodel.ListEntry) (string, error),
) (string, error) {
for _, listEntry := range list.ListEntries {
thatID, err := getThatID(listEntry)
if err != nil {
return "", err
}
if thisID == thatID {
return listEntry.ID, nil
}
}
return "", nil
}

View File

@ -649,7 +649,8 @@ func (suite *FromClientAPITestSuite) TestProcessCreateStatusListRepliesPolicyLis
}
// Remove turtle from the list.
if err := testStructs.State.DB.DeleteListEntry(ctx, suite.testListEntries["local_account_1_list_1_entry_1"].ID); err != nil {
testEntry := suite.testListEntries["local_account_1_list_1_entry_1"]
if err := testStructs.State.DB.DeleteListEntry(ctx, testEntry.ListID, testEntry.FollowID); err != nil {
suite.FailNow(err.Error())
}

View File

@ -21,7 +21,6 @@ import (
"context"
"errors"
"github.com/superseriousbusiness/gotosocial/internal/db"
statusfilter "github.com/superseriousbusiness/gotosocial/internal/filter/status"
"github.com/superseriousbusiness/gotosocial/internal/filter/usermute"
"github.com/superseriousbusiness/gotosocial/internal/gtscontext"
@ -63,13 +62,9 @@ func (s *Surface) timelineAndNotifyStatus(ctx context.Context, status *gtsmodel.
})
}
// Timeline the status for each local follower of this account.
// This will also handle notifying any followers with notify
// set to true on their follow.
homeTimelinedAccountIDs, err := s.timelineAndNotifyStatusForFollowers(ctx, status, follows)
if err != nil {
return gtserror.Newf("error timelining status %s for followers: %w", status.ID, err)
}
// Timeline the status for each local follower of this account. This will
// also handle notifying any followers with notify set to true on their follow.
homeTimelinedAccountIDs := s.timelineAndNotifyStatusForFollowers(ctx, status, follows)
// Timeline the status for each local account who follows a tag used by this status.
if err := s.timelineAndNotifyStatusForTagFollowers(ctx, status, homeTimelinedAccountIDs); err != nil {
@ -105,12 +100,10 @@ func (s *Surface) timelineAndNotifyStatusForFollowers(
ctx context.Context,
status *gtsmodel.Status,
follows []*gtsmodel.Follow,
) ([]string, error) {
) (homeTimelinedAccountIDs []string) {
var (
errs gtserror.MultiError
boost = status.BoostOfID != ""
reply = status.InReplyToURI != ""
homeTimelinedAccountIDs = []string{}
boost = (status.BoostOfID != "")
reply = (status.InReplyToURI != "")
)
for _, follow := range follows {
@ -130,7 +123,7 @@ func (s *Surface) timelineAndNotifyStatusForFollowers(
ctx, follow.Account, status,
)
if err != nil {
errs.Appendf("error checking status %s hometimelineability: %w", status.ID, err)
log.Errorf(ctx, "error checking status home visibility for follow: %v", err)
continue
}
@ -139,29 +132,36 @@ func (s *Surface) timelineAndNotifyStatusForFollowers(
continue
}
// Get relevant filters and mutes for this follow's account.
// (note the origin account of the follow is receiver of status).
filters, mutes, err := s.getFiltersAndMutes(ctx, follow.AccountID)
if err != nil {
errs.Append(err)
log.Error(ctx, err)
continue
}
// Add status to any relevant lists
// for this follow, if applicable.
exclusive, listTimelined := s.listTimelineStatusForFollow(
ctx,
// Add status to any relevant lists for this follow, if applicable.
listTimelined, exclusive, err := s.listTimelineStatusForFollow(ctx,
status,
follow,
&errs,
filters,
mutes,
)
if err != nil {
log.Errorf(ctx, "error list timelining status: %v", err)
continue
}
// Add status to home timeline for owner
// of this follow, if applicable.
homeTimelined := false
var homeTimelined bool
// If this was timelined into
// list with exclusive flag set,
// don't add to home timeline.
if !exclusive {
homeTimelined, err = s.timelineStatus(
ctx,
// Add status to home timeline for owner of
// this follow (origin account), if applicable.
homeTimelined, err = s.timelineStatus(ctx,
s.State.Timelines.Home.IngestOne,
follow.AccountID, // home timelines are keyed by account ID
follow.Account,
@ -171,10 +171,12 @@ func (s *Surface) timelineAndNotifyStatusForFollowers(
mutes,
)
if err != nil {
errs.Appendf("error home timelining status: %w", err)
log.Errorf(ctx, "error home timelining status: %v", err)
continue
}
if homeTimelined {
// If hometimelined, add to list of returned account IDs.
homeTimelinedAccountIDs = append(homeTimelinedAccountIDs, follow.AccountID)
}
}
@ -210,11 +212,12 @@ func (s *Surface) timelineAndNotifyStatusForFollowers(
status.Account,
status.ID,
); err != nil {
errs.Appendf("error notifying account %s about new status: %w", follow.AccountID, err)
log.Errorf(ctx, "error notifying status for account: %v", err)
continue
}
}
return homeTimelinedAccountIDs, errs.Combine()
return homeTimelinedAccountIDs
}
// listTimelineStatusForFollow puts the given status
@ -227,107 +230,59 @@ func (s *Surface) listTimelineStatusForFollow(
ctx context.Context,
status *gtsmodel.Status,
follow *gtsmodel.Follow,
errs *gtserror.MultiError,
filters []*gtsmodel.Filter,
mutes *usermute.CompiledUserMuteList,
) (bool, bool) {
// To put this status in appropriate list timelines,
// we need to get each listEntry that pertains to
// this follow. Then, we want to iterate through all
// those list entries, and add the status to the list
// that the entry belongs to if it meets criteria for
// inclusion in the list.
) (timelined bool, exclusive bool, err error) {
listEntries, err := s.getListEntries(ctx, follow)
// Get all lists that contain this given follow.
lists, err := s.State.DB.GetListsContainingFollowID(
// We don't need list sub-models.
gtscontext.SetBarebones(ctx),
follow.ID,
)
if err != nil {
errs.Append(err)
return false, false
}
exclusive, err := s.isAnyListExclusive(ctx, listEntries)
if err != nil {
errs.Append(err)
return false, false
return false, false, gtserror.Newf("error getting lists for follow: %w", err)
}
// Check eligibility for each list entry (if any).
listTimelined := false
for _, listEntry := range listEntries {
eligible, err := s.listEligible(ctx, listEntry, status)
for _, list := range lists {
// Check whether list is eligible for this status.
eligible, err := s.listEligible(ctx, list, status)
if err != nil {
errs.Appendf("error checking list eligibility: %w", err)
log.Errorf(ctx, "error checking list eligibility: %v", err)
continue
}
if !eligible {
// Don't add this.
continue
}
// Update exclusive flag if list is so.
exclusive = exclusive || *list.Exclusive
// At this point we are certain this status
// should be included in the timeline of the
// list that this list entry belongs to.
timelined, err := s.timelineStatus(
listTimelined, err := s.timelineStatus(
ctx,
s.State.Timelines.List.IngestOne,
listEntry.ListID, // list timelines are keyed by list ID
list.ID, // list timelines are keyed by list ID
follow.Account,
status,
stream.TimelineList+":"+listEntry.ListID, // key streamType to this specific list
stream.TimelineList+":"+list.ID, // key streamType to this specific list
filters,
mutes,
)
if err != nil {
errs.Appendf("error adding status to timeline for list %s: %w", listEntry.ListID, err)
// implicit continue
log.Errorf(ctx, "error adding status to list timeline: %v", err)
continue
}
listTimelined = listTimelined || timelined
// Update flag based on if timelined.
timelined = timelined || listTimelined
}
return exclusive, listTimelined
}
// getListEntries returns list entries for a given follow.
func (s *Surface) getListEntries(ctx context.Context, follow *gtsmodel.Follow) ([]*gtsmodel.ListEntry, error) {
// Get every list entry that targets this follow's ID.
listEntries, err := s.State.DB.GetListEntriesForFollowID(
// We only need the list IDs.
gtscontext.SetBarebones(ctx),
follow.ID,
)
if err != nil && !errors.Is(err, db.ErrNoEntries) {
return nil, gtserror.Newf("DB error getting list entries: %v", err)
}
return listEntries, nil
}
// isAnyListExclusive determines whether any provided list entry corresponds to an exclusive list.
func (s *Surface) isAnyListExclusive(ctx context.Context, listEntries []*gtsmodel.ListEntry) (bool, error) {
if len(listEntries) == 0 {
return false, nil
}
listIDs := make([]string, 0, len(listEntries))
for _, listEntry := range listEntries {
listIDs = append(listIDs, listEntry.ListID)
}
lists, err := s.State.DB.GetListsByIDs(
// We only need the list exclusive flags.
gtscontext.SetBarebones(ctx),
listIDs,
)
if err != nil && !errors.Is(err, db.ErrNoEntries) {
return false, gtserror.Newf("DB error getting lists for list entries: %v", err)
}
if len(lists) == 0 {
return false, nil
}
for _, list := range lists {
if *list.Exclusive {
return true, nil
}
}
return false, nil
return timelined, exclusive, nil
}
// getFiltersAndMutes returns an account's filters and mutes.
@ -341,8 +296,8 @@ func (s *Surface) getFiltersAndMutes(ctx context.Context, accountID string) ([]*
if err != nil {
return nil, nil, gtserror.Newf("couldn't retrieve mutes for account %s: %w", accountID, err)
}
compiledMutes := usermute.NewCompiledUserMuteList(mutes)
compiledMutes := usermute.NewCompiledUserMuteList(mutes)
return filters, compiledMutes, err
}
@ -351,7 +306,7 @@ func (s *Surface) getFiltersAndMutes(ctx context.Context, accountID string) ([]*
// belongs to, based on the replies policy of the list.
func (s *Surface) listEligible(
ctx context.Context,
listEntry *gtsmodel.ListEntry,
list *gtsmodel.List,
status *gtsmodel.Status,
) (bool, error) {
if status.InReplyToURI == "" {
@ -366,18 +321,6 @@ func (s *Surface) listEligible(
return false, nil
}
// Status is a reply to a known account.
// We need to fetch the list that this
// entry belongs to, in order to check
// the list's replies policy.
list, err := s.State.DB.GetListByID(
ctx, listEntry.ListID,
)
if err != nil {
err := gtserror.Newf("db error getting list %s: %w", listEntry.ListID, err)
return false, err
}
switch list.RepliesPolicy {
case gtsmodel.RepliesPolicyNone:
// This list should not show
@ -390,20 +333,15 @@ func (s *Surface) listEligible(
//
// Check if replied-to account is
// also included in this list.
includes, err := s.State.DB.ListIncludesAccount(
ctx,
in, err := s.State.DB.IsAccountInList(ctx,
list.ID,
status.InReplyToAccountID,
)
if err != nil {
err := gtserror.Newf(
"db error checking if account %s in list %s: %w",
status.InReplyToAccountID, listEntry.ListID, err,
)
err := gtserror.Newf("db error checking if account in list: %w", err)
return false, err
}
return includes, nil
return in, nil
case gtsmodel.RepliesPolicyFollowed:
// This list should show replies
@ -418,22 +356,14 @@ func (s *Surface) listEligible(
status.InReplyToAccountID,
)
if err != nil {
err := gtserror.Newf(
"db error checking if account %s is followed by %s: %w",
status.InReplyToAccountID, list.AccountID, err,
)
err := gtserror.Newf("db error checking if account followed: %w", err)
return false, err
}
return follows, nil
default:
// HUH??
err := gtserror.Newf(
"reply policy '%s' not recognized on list %s",
list.RepliesPolicy, list.ID,
)
return false, err
log.Panicf(ctx, "unknown reply policy: %s", list.RepliesPolicy)
return false, nil // unreachable code
}
}
@ -452,6 +382,7 @@ func (s *Surface) timelineStatus(
filters []*gtsmodel.Filter,
mutes *usermute.CompiledUserMuteList,
) (bool, error) {
// Ingest status into given timeline using provided function.
if inserted, err := ingest(ctx, timelineID, status); err != nil {
err = gtserror.Newf("error ingesting status %s: %w", status.ID, err)
@ -461,7 +392,7 @@ func (s *Surface) timelineStatus(
return false, nil
}
// The status was inserted so stream it to the user.
// Convert updated database model to frontend model.
apiStatus, err := s.Converter.StatusToAPIStatus(ctx,
status,
account,
@ -473,6 +404,8 @@ func (s *Surface) timelineStatus(
err = gtserror.Newf("error converting status %s to frontend representation: %w", status.ID, err)
return true, err
}
// The status was inserted so stream it to the user.
s.Stream.Update(ctx, account, apiStatus, streamType)
return true, nil
@ -492,7 +425,8 @@ func (s *Surface) timelineAndNotifyStatusForTagFollowers(
}
if status.BoostOf != nil {
// Unwrap boost and work with the original status.
// Unwrap boost and work
// with the original status.
status = status.BoostOf
}
@ -523,6 +457,7 @@ func (s *Surface) timelineAndNotifyStatusForTagFollowers(
)
}
}
return errs.Combine()
}
@ -667,17 +602,15 @@ func (s *Surface) timelineStatusUpdate(ctx context.Context, status *gtsmodel.Sta
follows = append(follows, &gtsmodel.Follow{
AccountID: status.AccountID,
Account: status.Account,
Notify: func() *bool { b := false; return &b }(), // Account shouldn't notify itself.
ShowReblogs: func() *bool { b := true; return &b }(), // Account should show own reblogs.
Notify: util.Ptr(false), // Account shouldn't notify itself.
ShowReblogs: util.Ptr(true), // Account should show own reblogs.
})
}
// Push to streams for each local follower of this account.
homeTimelinedAccountIDs, err := s.timelineStatusUpdateForFollowers(ctx, status, follows)
if err != nil {
return gtserror.Newf("error timelining status %s for followers: %w", status.ID, err)
}
// Push updated status to streams for each local follower of this account.
homeTimelinedAccountIDs := s.timelineStatusUpdateForFollowers(ctx, status, follows)
// Push updated status to streams for each local follower of tags in status, if applicable.
if err := s.timelineStatusUpdateForTagFollowers(ctx, status, homeTimelinedAccountIDs); err != nil {
return gtserror.Newf("error timelining status %s for tag followers: %w", status.ID, err)
}
@ -695,12 +628,7 @@ func (s *Surface) timelineStatusUpdateForFollowers(
ctx context.Context,
status *gtsmodel.Status,
follows []*gtsmodel.Follow,
) ([]string, error) {
var (
errs gtserror.MultiError
homeTimelinedAccountIDs = []string{}
)
) (homeTimelinedAccountIDs []string) {
for _, follow := range follows {
// Check to see if the status is timelineable for this follower,
// taking account of its visibility, who it replies to, and, if
@ -718,7 +646,7 @@ func (s *Surface) timelineStatusUpdateForFollowers(
ctx, follow.Account, status,
)
if err != nil {
errs.Appendf("error checking status %s hometimelineability: %w", status.ID, err)
log.Errorf(ctx, "error checking status home visibility for follow: %v", err)
continue
}
@ -727,31 +655,36 @@ func (s *Surface) timelineStatusUpdateForFollowers(
continue
}
// Get relevant filters and mutes for this follow's account.
// (note the origin account of the follow is receiver of status).
filters, mutes, err := s.getFiltersAndMutes(ctx, follow.AccountID)
if err != nil {
errs.Append(err)
log.Error(ctx, err)
continue
}
// Add status to any relevant lists
// for this follow, if applicable.
exclusive := s.listTimelineStatusUpdateForFollow(
ctx,
// Add status to relevant lists for this follow, if applicable.
_, exclusive, err := s.listTimelineStatusUpdateForFollow(ctx,
status,
follow,
&errs,
filters,
mutes,
)
if err != nil {
log.Errorf(ctx, "error list timelining status: %v", err)
continue
}
// If this was timelined into
// list with exclusive flag set,
// don't add to home timeline.
if exclusive {
continue
}
// Add status to home timeline for owner
// of this follow, if applicable.
homeTimelined, err := s.timelineStreamStatusUpdate(
ctx,
// Add status to home timeline for owner of
// this follow (origin account), if applicable.
homeTimelined, err := s.timelineStreamStatusUpdate(ctx,
follow.Account,
status,
stream.TimelineHome,
@ -759,15 +692,17 @@ func (s *Surface) timelineStatusUpdateForFollowers(
mutes,
)
if err != nil {
errs.Appendf("error home timelining status: %w", err)
log.Errorf(ctx, "error home timelining status: %v", err)
continue
}
if homeTimelined {
// If hometimelined, add to list of returned account IDs.
homeTimelinedAccountIDs = append(homeTimelinedAccountIDs, follow.AccountID)
}
}
return homeTimelinedAccountIDs, errs.Combine()
return homeTimelinedAccountIDs
}
// listTimelineStatusUpdateForFollow pushes edits of the given status
@ -779,58 +714,59 @@ func (s *Surface) listTimelineStatusUpdateForFollow(
ctx context.Context,
status *gtsmodel.Status,
follow *gtsmodel.Follow,
errs *gtserror.MultiError,
filters []*gtsmodel.Filter,
mutes *usermute.CompiledUserMuteList,
) bool {
// To put this status in appropriate list timelines,
// we need to get each listEntry that pertains to
// this follow. Then, we want to iterate through all
// those list entries, and add the status to the list
// that the entry belongs to if it meets criteria for
// inclusion in the list.
) (bool, bool, error) {
listEntries, err := s.getListEntries(ctx, follow)
// Get all lists that contain this given follow.
lists, err := s.State.DB.GetListsContainingFollowID(
// We don't need list sub-models.
gtscontext.SetBarebones(ctx),
follow.ID,
)
if err != nil {
errs.Append(err)
return false
}
exclusive, err := s.isAnyListExclusive(ctx, listEntries)
if err != nil {
errs.Append(err)
return false
return false, false, gtserror.Newf("error getting lists for follow: %w", err)
}
// Check eligibility for each list entry (if any).
for _, listEntry := range listEntries {
eligible, err := s.listEligible(ctx, listEntry, status)
var exclusive, timelined bool
for _, list := range lists {
// Check whether list is eligible for this status.
eligible, err := s.listEligible(ctx, list, status)
if err != nil {
errs.Appendf("error checking list eligibility: %w", err)
log.Errorf(ctx, "error checking list eligibility: %v", err)
continue
}
if !eligible {
// Don't add this.
continue
}
// Update exclusive flag if list is so.
exclusive = exclusive || *list.Exclusive
// At this point we are certain this status
// should be included in the timeline of the
// list that this list entry belongs to.
if _, err := s.timelineStreamStatusUpdate(
listTimelined, err := s.timelineStreamStatusUpdate(
ctx,
follow.Account,
status,
stream.TimelineList+":"+listEntry.ListID, // key streamType to this specific list
stream.TimelineList+":"+list.ID, // key streamType to this specific list
filters,
mutes,
); err != nil {
errs.Appendf("error adding status to timeline for list %s: %w", listEntry.ListID, err)
// implicit continue
)
if err != nil {
log.Errorf(ctx, "error adding status to list timeline: %v", err)
continue
}
// Update flag based on if timelined.
timelined = timelined || listTimelined
}
return exclusive
return timelined, exclusive, nil
}
// timelineStatusUpdate streams the edited status to the user using the
@ -845,16 +781,31 @@ func (s *Surface) timelineStreamStatusUpdate(
filters []*gtsmodel.Filter,
mutes *usermute.CompiledUserMuteList,
) (bool, error) {
apiStatus, err := s.Converter.StatusToAPIStatus(ctx, status, account, statusfilter.FilterContextHome, filters, mutes)
if errors.Is(err, statusfilter.ErrHideStatus) {
// Convert updated database model to frontend model.
apiStatus, err := s.Converter.StatusToAPIStatus(ctx,
status,
account,
statusfilter.FilterContextHome,
filters,
mutes,
)
switch {
case err == nil:
// no issue.
case errors.Is(err, statusfilter.ErrHideStatus):
// Don't put this status in the stream.
return false, nil
default:
return false, gtserror.Newf("error converting status: %w", err)
}
if err != nil {
err = gtserror.Newf("error converting status %s to frontend representation: %w", status.ID, err)
return false, err
}
// The status was updated so stream it to the user.
s.Stream.StatusUpdate(ctx, account, apiStatus, streamType)
return true, nil
}

View File

@ -126,11 +126,6 @@ func (u *utils) wipeStatus(
errs.Appendf("error deleting status poll: %w", err)
}
// Delete any poll votes pointing to this poll ID.
if err := u.state.DB.DeletePollVotes(ctx, pollID); err != nil {
errs.Appendf("error deleting status poll votes: %w", err)
}
// Cancel any scheduled expiry task for poll.
_ = u.state.Workers.Scheduler.Cancel(pollID)
}

View File

@ -34,16 +34,14 @@ func (c *Converter) AccountToExportStats(
a *gtsmodel.Account,
) (*apimodel.AccountExportStats, error) {
// Ensure account stats populated.
if a.Stats == nil {
if err := c.state.DB.PopulateAccountStats(ctx, a); err != nil {
return nil, gtserror.Newf(
"error getting stats for account %s: %w",
a.ID, err,
)
}
if err := c.state.DB.PopulateAccountStats(ctx, a); err != nil {
return nil, gtserror.Newf(
"error getting stats for account %s: %w",
a.ID, err,
)
}
listsCount, err := c.state.DB.CountListsForAccountID(ctx, a.ID)
listsCount, err := c.state.DB.CountListsByAccountID(ctx, a.ID)
if err != nil {
return nil, gtserror.Newf(
"error counting lists for account %s: %w",
@ -202,6 +200,7 @@ func (c *Converter) ListsToCSV(
ctx context.Context,
lists []*gtsmodel.List,
) ([][]string, error) {
// We need to know our own domain for this.
// Try account domain, fall back to host.
thisDomain := config.GetAccountDomain()
@ -215,41 +214,23 @@ func (c *Converter) ListsToCSV(
// For each item, add a record.
for _, list := range lists {
for _, entry := range list.ListEntries {
if entry.Follow == nil {
// Retrieve follow.
var err error
entry.Follow, err = c.state.DB.GetFollowByID(
ctx,
entry.FollowID,
)
if err != nil {
return nil, gtserror.Newf(
"db error getting follow for list entry %s: %w",
entry.ID, err,
)
}
}
if entry.Follow.TargetAccount == nil {
// Retrieve account.
var err error
entry.Follow.TargetAccount, err = c.state.DB.GetAccountByID(
// Barebones is fine here.
gtscontext.SetBarebones(ctx),
entry.Follow.TargetAccountID,
)
if err != nil {
return nil, gtserror.Newf(
"db error getting target account for list entry %s: %w",
entry.ID, err,
)
}
}
// Get all follows contained with this list.
follows, err := c.state.DB.GetFollowsInList(ctx,
list.ID,
nil,
)
if err != nil {
err := gtserror.Newf("db error getting follows for list: %w", err)
return nil, err
}
// Append each follow as CSV record.
for _, follow := range follows {
var (
username = entry.Follow.TargetAccount.Username
domain = entry.Follow.TargetAccount.Domain
// Extract username / domain from target.
username = follow.TargetAccount.Username
domain = follow.TargetAccount.Domain
)
if domain == "" {
@ -259,14 +240,16 @@ func (c *Converter) ListsToCSV(
}
records = append(records, []string{
// List title: eg., Very cool list
// List title: e.g.
// Very cool list
list.Title,
// Account address: eg., someone@example.org
// -- NOTE: without the leading '@'!
// Account address: e.g.,
// someone@example.org
// NOTE: without the leading '@'!
username + "@" + domain,
})
}
}
return records, nil

View File

@ -17,12 +17,23 @@
package util
// KeyBy creates a map of T->S, keyed by value returned from key func.
func KeyBy[S any, T comparable](in []S, key func(S) T) map[T]S {
if key == nil {
panic("nil func")
}
m := make(map[T]S, len(in))
for _, v := range in {
m[key(v)] = v
}
return m
}
// Set represents a hashmap of only keys,
// useful for deduplication / key checking.
type Set[T comparable] map[T]struct{}
// ToSet creates a Set[T] from given values,
// noting that this does not maintain any order.
// ToSet creates a Set[T] from given values.
func ToSet[T comparable](in []T) Set[T] {
set := make(Set[T], len(in))
for _, v := range in {
@ -31,8 +42,19 @@ func ToSet[T comparable](in []T) Set[T] {
return set
}
// FromSet extracts the values from set to slice,
// noting that this does not maintain any order.
// ToSetFunc creates a Set[T] from input slice, keys provided by func.
func ToSetFunc[S any, T comparable](in []S, key func(S) T) Set[T] {
if key == nil {
panic("nil func")
}
set := make(Set[T], len(in))
for _, v := range in {
set[key(v)] = struct{}{}
}
return set
}
// FromSet extracts the values from set to slice.
func FromSet[T comparable](in Set[T]) []T {
out := make([]T, len(in))
var i int

View File

@ -23,7 +23,6 @@ EXPECT=$(cat << "EOF"
"application-name": "gts",
"bind-address": "127.0.0.1",
"cache": {
"account-ids-following-tag-mem-ratio": 1,
"account-mem-ratio": 5,
"account-note-mem-ratio": 1,
"account-settings-mem-ratio": 0.1,
@ -44,11 +43,13 @@ EXPECT=$(cat << "EOF"
"follow-mem-ratio": 2,
"follow-request-ids-mem-ratio": 2,
"follow-request-mem-ratio": 2,
"following-tag-ids-mem-ratio": 2,
"in-reply-to-ids-mem-ratio": 3,
"instance-mem-ratio": 1,
"interaction-request-mem-ratio": 1,
"list-entry-mem-ratio": 2,
"list-ids-mem-ratio": 2,
"list-mem-ratio": 1,
"listed-ids-mem-ratio": 2,
"marker-mem-ratio": 0.5,
"media-mem-ratio": 4,
"memory-target": 104857600,
@ -65,7 +66,6 @@ EXPECT=$(cat << "EOF"
"status-fave-ids-mem-ratio": 3,
"status-fave-mem-ratio": 2,
"status-mem-ratio": 5,
"tag-ids-followed-by-account-mem-ratio": 1,
"tag-mem-ratio": 2,
"thread-mute-mem-ratio": 0.2,
"token-mem-ratio": 0.75,

View File

@ -375,6 +375,11 @@ func (c *Cache[T]) Load(index *Index, keys []Key, load func([]Key) ([]T, error))
// the lock.
unlock()
if len(keys) == 0 {
// We loaded everything!
return values, nil
}
// Load uncached values.
uncached, err := load(keys)
if err != nil {

View File

@ -41,7 +41,6 @@ func free_indexed_item(item *indexed_item) {
}
// drop_index will drop the given index entry from item's indexed.
// note this also handles freeing the index_entry memory (e.g. to pool)
func (i *indexed_item) drop_index(entry *index_entry) {
for x := 0; x < len(i.indexed); x++ {
if i.indexed[x] != entry {

View File

@ -152,8 +152,10 @@ func extract_fields(ptr unsafe.Pointer, fields []struct_field) []unsafe.Pointer
fptr = field.zero
}
// Set field ptr.
ptrs[i] = fptr
}
return ptrs
}

2
vendor/modules.txt vendored
View File

@ -75,7 +75,7 @@ codeberg.org/gruf/go-storage/disk
codeberg.org/gruf/go-storage/internal
codeberg.org/gruf/go-storage/memory
codeberg.org/gruf/go-storage/s3
# codeberg.org/gruf/go-structr v0.8.8
# codeberg.org/gruf/go-structr v0.8.9
## explicit; go 1.21
codeberg.org/gruf/go-structr
# codeberg.org/superseriousbusiness/exif-terminator v0.9.0