improvements to caching for lists and relationship to accounts / follows

This commit is contained in:
kim 2024-09-11 22:38:35 +01:00
commit 002bd86a39
27 changed files with 1002 additions and 1333 deletions

View file

@ -25,6 +25,7 @@ import (
apiutil "github.com/superseriousbusiness/gotosocial/internal/api/util" apiutil "github.com/superseriousbusiness/gotosocial/internal/api/util"
"github.com/superseriousbusiness/gotosocial/internal/gtserror" "github.com/superseriousbusiness/gotosocial/internal/gtserror"
"github.com/superseriousbusiness/gotosocial/internal/oauth" "github.com/superseriousbusiness/gotosocial/internal/oauth"
"github.com/superseriousbusiness/gotosocial/internal/paging"
) )
// ListAccountsGETHandler swagger:operation GET /api/v1/lists/{id}/accounts listAccounts // 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) targetListID := c.Param(IDKey)
if targetListID == "" { if targetListID == "" {
err := errors.New("no list id specified") const text = "no list id specified"
apiutil.ErrorHandler(c, gtserror.NewErrorBadRequest(err, err.Error()), m.processor.InstanceGetV1) errWithCode := gtserror.NewErrorBadRequest(errors.New(text), text)
apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1)
return return
} }
limit, errWithCode := apiutil.ParseLimit(c.Query(apiutil.LimitKey), 40, 80, 0) page, errWithCode := paging.ParseIDPage(c,
1, // min limit
80, // max limit
0, // no paging allowed
)
if errWithCode != nil { if errWithCode != nil {
apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1) apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1)
return 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( resp, errWithCode := m.processor.List().GetListAccounts(
ctx, c.Request.Context(),
authed.Account, authed.Account,
targetListID, targetListID,
c.Query(MaxIDKey), page,
c.Query(SinceIDKey),
c.Query(MinIDKey),
limit,
) )
if errWithCode != nil { if errWithCode != nil {
apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1) apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1)

View file

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

98
internal/cache/db.go vendored
View file

@ -29,9 +29,6 @@ type DBCaches struct {
// Account provides access to the gtsmodel Account database cache. // Account provides access to the gtsmodel Account database cache.
Account StructCache[*gtsmodel.Account] 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 provides access to the gtsmodel Note database cache.
AccountNote StructCache[*gtsmodel.AccountNote] AccountNote StructCache[*gtsmodel.AccountNote]
@ -103,6 +100,12 @@ type DBCaches struct {
// - '<' for follower IDs // - '<' for follower IDs
FollowRequestIDs SliceCache[string] 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
// - '<{$tagIDs}' for account IDs following tag
FollowingTagIDs SliceCache[string]
// Instance provides access to the gtsmodel Instance database cache. // Instance provides access to the gtsmodel Instance database cache.
Instance StructCache[*gtsmodel.Instance] Instance StructCache[*gtsmodel.Instance]
@ -115,8 +118,17 @@ type DBCaches struct {
// List provides access to the gtsmodel List database cache. // List provides access to the gtsmodel List database cache.
List StructCache[*gtsmodel.List] List StructCache[*gtsmodel.List]
// ListEntry provides access to the gtsmodel ListEntry database cache. // ListIDs provides access to the list IDs owned by account / list IDs follow
ListEntry StructCache[*gtsmodel.ListEntry] // contained in db cache. THIS CACHE IS KEYED AS FOLLOWING {prefix}{id} WHERE:
// - 'a{$accountID}' for list IDs owned by account
// - 'f{$followID}' for list IDs follow contained in
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
// - 'f{listID}' for follow IDs in list ID
ListedIDs SliceCache[string]
// Marker provides access to the gtsmodel Marker database cache. // Marker provides access to the gtsmodel Marker database cache.
Marker StructCache[*gtsmodel.Marker] Marker StructCache[*gtsmodel.Marker]
@ -151,10 +163,10 @@ type DBCaches struct {
// Status provides access to the gtsmodel Status database cache. // Status provides access to the gtsmodel Status database cache.
Status StructCache[*gtsmodel.Status] Status StructCache[*gtsmodel.Status]
// StatusBookmark ... // StatusBookmark provides access to the gtsmodel StatusBookmark database cache.
StatusBookmark StructCache[*gtsmodel.StatusBookmark] StatusBookmark StructCache[*gtsmodel.StatusBookmark]
// StatusBookmarkIDs ... // StatusBookmarkIDs provides access to the status bookmark IDs list database cache.
StatusBookmarkIDs SliceCache[string] StatusBookmarkIDs SliceCache[string]
// StatusFave provides access to the gtsmodel StatusFave database cache. // StatusFave provides access to the gtsmodel StatusFave database cache.
@ -166,9 +178,6 @@ type DBCaches struct {
// Tag provides access to the gtsmodel Tag database cache. // Tag provides access to the gtsmodel Tag database cache.
Tag StructCache[*gtsmodel.Tag] 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 provides access to the gtsmodel ThreadMute database cache.
ThreadMute StructCache[*gtsmodel.ThreadMute] ThreadMute StructCache[*gtsmodel.ThreadMute]
@ -243,17 +252,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() { func (c *Caches) initAccountNote() {
// Calculate maximum cache size. // Calculate maximum cache size.
cap := calculateResultCacheMax( cap := calculateResultCacheMax(
@ -761,6 +759,17 @@ func (c *Caches) initFollowRequestIDs() {
c.DB.FollowRequestIDs.Init(0, cap) 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() { func (c *Caches) initInReplyToIDs() {
// Calculate maximum cache size. // Calculate maximum cache size.
cap := calculateSliceCacheMax( cap := calculateSliceCacheMax(
@ -860,7 +869,6 @@ func (c *Caches) initList() {
// will be populated separately. // will be populated separately.
// See internal/db/bundb/list.go. // See internal/db/bundb/list.go.
l2.Account = nil l2.Account = nil
l2.ListEntries = nil
return l2 return l2
} }
@ -876,37 +884,26 @@ func (c *Caches) initList() {
}) })
} }
func (c *Caches) initListEntry() { func (c *Caches) initListIDs() {
// Calculate maximum cache size. // Calculate maximum cache size.
cap := calculateResultCacheMax( cap := calculateSliceCacheMax(
sizeofListEntry(), // model in-mem size. config.GetCacheListIDsMemRatio(),
config.GetCacheListEntryMemRatio(),
) )
log.Infof(nil, "cache size = %d", cap) log.Infof(nil, "cache size = %d", cap)
copyF := func(l1 *gtsmodel.ListEntry) *gtsmodel.ListEntry { c.DB.ListIDs.Init(0, cap)
l2 := new(gtsmodel.ListEntry) }
*l2 = *l1
// Don't include ptr fields that func (c *Caches) initListedIDs() {
// will be populated separately. // Calculate maximum cache size.
// See internal/db/bundb/list.go. cap := calculateSliceCacheMax(
l2.Follow = nil config.GetCacheListedIDsMemRatio(),
)
return l2 log.Infof(nil, "cache size = %d", cap)
}
c.DB.ListEntry.Init(structr.CacheConfig[*gtsmodel.ListEntry]{ c.DB.ListedIDs.Init(0, cap)
Indices: []structr.IndexConfig{
{Fields: "ID"},
{Fields: "ListID", Multiple: true},
{Fields: "FollowID", Multiple: true},
},
MaxSize: cap,
IgnoreErr: ignoreErrors,
Copy: copyF,
})
} }
func (c *Caches) initMarker() { func (c *Caches) initMarker() {
@ -1368,17 +1365,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() { func (c *Caches) initThreadMute() {
cap := calculateResultCacheMax( cap := calculateResultCacheMax(
sizeofThreadMute(), // model in-mem size. 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. // Invalidate follow request with this same ID.
c.DB.FollowRequest.Invalidate("ID", follow.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. // Invalidate follow origin account ID cached visibility.
c.Visibility.Invalidate("ItemID", follow.AccountID) c.Visibility.Invalidate("ItemID", follow.AccountID)
c.Visibility.Invalidate("RequesterID", follow.AccountID) c.Visibility.Invalidate("RequesterID", follow.AccountID)
@ -121,6 +118,15 @@ func (c *Caches) OnInvalidateFollow(follow *gtsmodel.Follow) {
">"+follow.TargetAccountID, ">"+follow.TargetAccountID,
"l>"+follow.TargetAccountID, "l>"+follow.TargetAccountID,
) )
// Invalidate source account's lists
// and destination account's lists, and
// those specifically for this follow.
c.DB.ListIDs.Invalidate(
"a"+follow.AccountID,
"a"+follow.TargetAccountID,
"f"+follow.ID,
)
} }
func (c *Caches) OnInvalidateFollowRequest(followReq *gtsmodel.FollowRequest) { func (c *Caches) OnInvalidateFollowRequest(followReq *gtsmodel.FollowRequest) {
@ -139,8 +145,11 @@ func (c *Caches) OnInvalidateFollowRequest(followReq *gtsmodel.FollowRequest) {
} }
func (c *Caches) OnInvalidateList(list *gtsmodel.List) { func (c *Caches) OnInvalidateList(list *gtsmodel.List) {
// Invalidate all cached entries of this list. // Invalidate list ID entries.
c.DB.ListEntry.Invalidate("ListID", list.ID) c.DB.ListedIDs.Invalidate(
"a"+list.ID,
"f"+list.ID,
)
} }
func (c *Caches) OnInvalidateMedia(media *gtsmodel.MediaAttachment) { func (c *Caches) OnInvalidateMedia(media *gtsmodel.MediaAttachment) {

View file

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

View file

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

View file

@ -159,7 +159,6 @@ var Defaults = Configuration{
// file have been addressed, these should // file have been addressed, these should
// be able to make some more sense :D // be able to make some more sense :D
AccountMemRatio: 5, AccountMemRatio: 5,
AccountIDsFollowingTagMemRatio: 1,
AccountNoteMemRatio: 1, AccountNoteMemRatio: 1,
AccountSettingsMemRatio: 0.1, AccountSettingsMemRatio: 0.1,
AccountStatsMemRatio: 2, AccountStatsMemRatio: 2,
@ -179,11 +178,13 @@ var Defaults = Configuration{
FollowIDsMemRatio: 4, FollowIDsMemRatio: 4,
FollowRequestMemRatio: 2, FollowRequestMemRatio: 2,
FollowRequestIDsMemRatio: 2, FollowRequestIDsMemRatio: 2,
FollowingTagIDsMemRatio: 2,
InReplyToIDsMemRatio: 3, InReplyToIDsMemRatio: 3,
InstanceMemRatio: 1, InstanceMemRatio: 1,
InteractionRequestMemRatio: 1, InteractionRequestMemRatio: 1,
ListMemRatio: 1, ListMemRatio: 1,
ListEntryMemRatio: 2, ListIDsMemRatio: 2,
ListedIDsMemRatio: 2,
MarkerMemRatio: 0.5, MarkerMemRatio: 0.5,
MediaMemRatio: 4, MediaMemRatio: 4,
MentionMemRatio: 2, MentionMemRatio: 2,
@ -200,7 +201,6 @@ var Defaults = Configuration{
StatusFaveMemRatio: 2, StatusFaveMemRatio: 2,
StatusFaveIDsMemRatio: 3, StatusFaveIDsMemRatio: 3,
TagMemRatio: 2, TagMemRatio: 2,
TagIDsFollowedByAccountMemRatio: 1,
ThreadMuteMemRatio: 0.2, ThreadMuteMemRatio: 0.2,
TokenMemRatio: 0.75, TokenMemRatio: 0.75,
TombstoneMemRatio: 0.5, 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 // SetCacheAccountMemRatio safely sets the value for global configuration 'Cache.AccountMemRatio' field
func SetCacheAccountMemRatio(v float64) { global.SetCacheAccountMemRatio(v) } 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 // GetCacheAccountNoteMemRatio safely fetches the Configuration value for state's 'Cache.AccountNoteMemRatio' field
func (st *ConfigState) GetCacheAccountNoteMemRatio() (v float64) { func (st *ConfigState) GetCacheAccountNoteMemRatio() (v float64) {
st.mutex.RLock() st.mutex.RLock()
@ -3362,6 +3331,31 @@ func GetCacheFollowRequestIDsMemRatio() float64 { return global.GetCacheFollowRe
// SetCacheFollowRequestIDsMemRatio safely sets the value for global configuration 'Cache.FollowRequestIDsMemRatio' field // SetCacheFollowRequestIDsMemRatio safely sets the value for global configuration 'Cache.FollowRequestIDsMemRatio' field
func SetCacheFollowRequestIDsMemRatio(v float64) { global.SetCacheFollowRequestIDsMemRatio(v) } 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 // GetCacheInReplyToIDsMemRatio safely fetches the Configuration value for state's 'Cache.InReplyToIDsMemRatio' field
func (st *ConfigState) GetCacheInReplyToIDsMemRatio() (v float64) { func (st *ConfigState) GetCacheInReplyToIDsMemRatio() (v float64) {
st.mutex.RLock() st.mutex.RLock()
@ -3462,30 +3456,55 @@ func GetCacheListMemRatio() float64 { return global.GetCacheListMemRatio() }
// SetCacheListMemRatio safely sets the value for global configuration 'Cache.ListMemRatio' field // SetCacheListMemRatio safely sets the value for global configuration 'Cache.ListMemRatio' field
func SetCacheListMemRatio(v float64) { global.SetCacheListMemRatio(v) } func SetCacheListMemRatio(v float64) { global.SetCacheListMemRatio(v) }
// GetCacheListEntryMemRatio safely fetches the Configuration value for state's 'Cache.ListEntryMemRatio' field // GetCacheListIDsMemRatio safely fetches the Configuration value for state's 'Cache.ListIDsMemRatio' field
func (st *ConfigState) GetCacheListEntryMemRatio() (v float64) { func (st *ConfigState) GetCacheListIDsMemRatio() (v float64) {
st.mutex.RLock() st.mutex.RLock()
v = st.config.Cache.ListEntryMemRatio v = st.config.Cache.ListIDsMemRatio
st.mutex.RUnlock() st.mutex.RUnlock()
return return
} }
// SetCacheListEntryMemRatio safely sets the Configuration value for state's 'Cache.ListEntryMemRatio' field // SetCacheListIDsMemRatio safely sets the Configuration value for state's 'Cache.ListIDsMemRatio' field
func (st *ConfigState) SetCacheListEntryMemRatio(v float64) { func (st *ConfigState) SetCacheListIDsMemRatio(v float64) {
st.mutex.Lock() st.mutex.Lock()
defer st.mutex.Unlock() defer st.mutex.Unlock()
st.config.Cache.ListEntryMemRatio = v st.config.Cache.ListIDsMemRatio = v
st.reloadToViper() st.reloadToViper()
} }
// CacheListEntryMemRatioFlag returns the flag name for the 'Cache.ListEntryMemRatio' field // CacheListIDsMemRatioFlag returns the flag name for the 'Cache.ListIDsMemRatio' field
func CacheListEntryMemRatioFlag() string { return "cache-list-entry-mem-ratio" } func CacheListIDsMemRatioFlag() string { return "cache-list-ids-mem-ratio" }
// GetCacheListEntryMemRatio safely fetches the value for global configuration 'Cache.ListEntryMemRatio' field // GetCacheListIDsMemRatio safely fetches the value for global configuration 'Cache.ListIDsMemRatio' field
func GetCacheListEntryMemRatio() float64 { return global.GetCacheListEntryMemRatio() } func GetCacheListIDsMemRatio() float64 { return global.GetCacheListIDsMemRatio() }
// SetCacheListEntryMemRatio safely sets the value for global configuration 'Cache.ListEntryMemRatio' field // SetCacheListIDsMemRatio safely sets the value for global configuration 'Cache.ListIDsMemRatio' field
func SetCacheListEntryMemRatio(v float64) { global.SetCacheListEntryMemRatio(v) } 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 // GetCacheMarkerMemRatio safely fetches the Configuration value for state's 'Cache.MarkerMemRatio' field
func (st *ConfigState) GetCacheMarkerMemRatio() (v float64) { 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 // SetCacheTagMemRatio safely sets the value for global configuration 'Cache.TagMemRatio' field
func SetCacheTagMemRatio(v float64) { global.SetCacheTagMemRatio(v) } 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 // GetCacheThreadMuteMemRatio safely fetches the Configuration value for state's 'Cache.ThreadMuteMemRatio' field
func (st *ConfigState) GetCacheThreadMuteMemRatio() (v float64) { func (st *ConfigState) GetCacheThreadMuteMemRatio() (v float64) {
st.mutex.RLock() st.mutex.RLock()

View file

@ -29,6 +29,7 @@ import (
"github.com/superseriousbusiness/gotosocial/internal/gtserror" "github.com/superseriousbusiness/gotosocial/internal/gtserror"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/internal/log" "github.com/superseriousbusiness/gotosocial/internal/log"
"github.com/superseriousbusiness/gotosocial/internal/paging"
"github.com/superseriousbusiness/gotosocial/internal/state" "github.com/superseriousbusiness/gotosocial/internal/state"
"github.com/superseriousbusiness/gotosocial/internal/util" "github.com/superseriousbusiness/gotosocial/internal/util"
"github.com/uptrace/bun" "github.com/uptrace/bun"
@ -85,39 +86,52 @@ func (l *listDB) getList(ctx context.Context, lookup string, dbQuery func(*gtsmo
return list, nil return list, nil
} }
func (l *listDB) GetListsForAccountID(ctx context.Context, accountID string) ([]*gtsmodel.List, error) { func (l *listDB) GetListsByAccountID(ctx context.Context, accountID string) ([]*gtsmodel.List, error) {
// Fetch IDs of all lists owned by this account. listIDs, err := l.getListIDsByAccountID(ctx, accountID)
var listIDs []string if err != nil {
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 {
return nil, err return nil, err
} }
if len(listIDs) == 0 {
return nil, nil
}
// Return lists by their IDs.
return l.GetListsByIDs(ctx, listIDs) return l.GetListsByIDs(ctx, listIDs)
} }
func (l *listDB) CountListsForAccountID(ctx context.Context, accountID string) (int, error) { func (l *listDB) CountListsByAccountID(ctx context.Context, accountID string) (int, error) {
return l.db. listIDs, err := l.getListIDsByAccountID(ctx, accountID)
NewSelect(). return len(listIDs), err
Table("lists"). }
Where("? = ?", bun.Ident("account_id"), accountID).
Count(ctx) func (l *listDB) GetListsWithFollowID(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) IsAccountInListID(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 { func (l *listDB) PopulateList(ctx context.Context, list *gtsmodel.List) error {
var ( var (
err error err error
errs = gtserror.NewMultiError(2) errs gtserror.MultiError
) )
if list.Account == nil { if list.Account == nil {
@ -131,18 +145,6 @@ 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() return errs.Combine()
} }
@ -161,9 +163,6 @@ func (l *listDB) UpdateList(ctx context.Context, list *gtsmodel.List, columns ..
} }
defer func() { defer func() {
// Invalidate all entries for this list ID.
l.state.Caches.DB.ListEntry.Invalidate("ListID", list.ID)
// Invalidate this entire list's timeline. // Invalidate this entire list's timeline.
if err := l.state.Timelines.List.RemoveTimeline(ctx, list.ID); err != nil { if err := l.state.Timelines.List.RemoveTimeline(ctx, list.ID); err != nil {
log.Errorf(ctx, "error invalidating list timeline: %q", err) log.Errorf(ctx, "error invalidating list timeline: %q", err)
@ -181,21 +180,6 @@ func (l *listDB) UpdateList(ctx context.Context, list *gtsmodel.List, columns ..
} }
func (l *listDB) DeleteListByID(ctx context.Context, id string) error { 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
}
defer func() { defer func() {
// Invalidate this list from cache. // Invalidate this list from cache.
l.state.Caches.DB.List.Invalidate("ID", id) l.state.Caches.DB.List.Invalidate("ID", id)
@ -224,128 +208,82 @@ func (l *listDB) DeleteListByID(ctx context.Context, id string) error {
}) })
} }
/* func (l *listDB) getListIDsByAccountID(ctx context.Context, accountID string) ([]string, error) {
LIST ENTRY functions 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) { // List IDs not in cache.
return l.getListEntry( // Perform the DB query.
ctx, if _, err := l.db.NewSelect().
"ID", Table("lists").
func(listEntry *gtsmodel.ListEntry) error { Column("id").
return l.db.NewSelect(). Where("? = ?", bun.Ident("account_id"), accountID).
Model(listEntry). Exec(ctx, &listIDs); err != nil &&
Where("? = ?", bun.Ident("list_entry.id"), id). !errors.Is(err, db.ErrNoEntries) {
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 {
return nil, err return nil, err
} }
return &listEntry, nil return listIDs, 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
} }
func (l *listDB) GetListEntries(ctx context.Context, func (l *listDB) getListIDsWithFollowID(ctx context.Context, followID string) ([]string, error) {
listID string, return l.state.Caches.DB.ListIDs.Load("f"+followID, func() ([]string, error) {
maxID string, var listIDs []string
sinceID string,
minID string,
limit int,
) ([]*gtsmodel.ListEntry, error) {
// Ensure reasonable
if limit < 0 {
limit = 0
}
// Make educated guess for slice size // List IDs not in cache.
var ( // Perform the DB query.
entryIDs = make([]string, 0, limit) if _, err := l.db.NewSelect().
frontToBack = true Table("list_entries").
) Column("list_id").
Where("? = ?", bun.Ident("follow_id"), followID).
q := l.db. Exec(ctx, &listIDs); err != nil &&
NewSelect(). !errors.Is(err, db.ErrNoEntries) {
TableExpr("? AS ?", bun.Ident("list_entries"), bun.Ident("entry")). return nil, err
// 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]
} }
}
// Return list entries by their IDs. return listIDs, nil
return l.GetListEntriesByIDs(ctx, entryIDs) })
}
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).
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).
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) { func (l *listDB) GetListsByIDs(ctx context.Context, ids []string) ([]*gtsmodel.List, error) {
@ -402,82 +340,6 @@ func (l *listDB) GetListsByIDs(ctx context.Context, ids []string) ([]*gtsmodel.L
return lists, nil 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 { func (l *listDB) PopulateListEntry(ctx context.Context, listEntry *gtsmodel.ListEntry) error {
var err error var err error
@ -513,14 +375,10 @@ func (l *listDB) PutListEntries(ctx context.Context, entries []*gtsmodel.ListEnt
// Finally, insert each list entry into the database. // Finally, insert each list entry into the database.
return l.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error { return l.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
for _, entry := range entries { for _, entry := range entries {
entry := entry // rescope if _, err := tx.
if err := l.state.Caches.DB.ListEntry.Store(entry, func() error { NewInsert().
_, err := tx. Model(entry).
NewInsert(). Exec(ctx); err != nil {
Model(entry).
Exec(ctx)
return err
}); err != nil {
return err return err
} }
} }
@ -528,77 +386,70 @@ func (l *listDB) PutListEntries(ctx context.Context, entries []*gtsmodel.ListEnt
}) })
} }
func (l *listDB) DeleteListEntry(ctx context.Context, id string) error { func (l *listDB) DeleteListEntry(ctx context.Context, listID string, followID string) error {
// Load list entry into cache to ensure we can perform // Delete list entry with given
// all necessary cache invalidation hooks on removal. // ID, returning its list ID.
entry, err := l.GetListEntryByID( if _, err := l.db.NewDelete().
// 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
}
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"). Table("list_entries").
Where("? = ?", bun.Ident("id"), id). Where("? = ?", bun.Ident("list_id"), listID).
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). Where("? = ?", bun.Ident("follow_id"), followID).
Order("id DESC"). Exec(ctx, &listID); err != nil &&
Scan(ctx, &entryIDs); err != nil { !errors.Is(err, db.ErrNoEntries) {
return err return err
} }
for _, id := range entryIDs { // Invalidate list IDs containing follow.
// Delete each separately to trigger cache invalidations. l.state.Caches.DB.ListIDs.Invalidate(
if err := l.DeleteListEntry(ctx, id); err != nil { "f" + followID,
return err )
}
// Invalidate account / follow IDs in list.
l.state.Caches.DB.ListedIDs.Invalidate(
"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)
} }
return nil return nil
} }
func (l *listDB) ListIncludesAccount(ctx context.Context, listID string, accountID string) (bool, error) { func (l *listDB) DeleteListEntriesTargettingFollowID(ctx context.Context, followID string) error {
exists, err := l.db. var listIDs []string
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)
return exists, err // Delete all entries with follow
// ID, returning IDs and list IDs.
if _, err := l.db.NewDelete().
Table("list_entries").
Where("? = ?", bun.Ident("follow_id"), followID).
Returning("?", bun.Ident("list_id")).
Exec(ctx, &listIDs); err != nil &&
!errors.Is(err, db.ErrNoEntries) {
return err
}
// Invalidate list IDs containing follow.
l.state.Caches.DB.ListIDs.Invalidate(
"f" + followID,
)
// Iterate through list IDs of deleted entries.
for _, listID := range util.Deduplicate(listIDs) {
// Invalidate account / follow IDs in list.
l.state.Caches.DB.ListedIDs.Invalidate(
"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)
}
}
return nil
} }

View file

@ -18,334 +18,329 @@
package bundb_test package bundb_test
import ( import (
"context"
"slices"
"testing" "testing"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
"github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/gtscontext"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
) )
type ListTestSuite struct { type ListTestSuite struct {
BunDBStandardTestSuite BunDBStandardTestSuite
} }
func (suite *ListTestSuite) testStructs() (*gtsmodel.List, *gtsmodel.Account) { // func (suite *ListTestSuite) testStructs() (*gtsmodel.List, *gtsmodel.Account) {
testList := &gtsmodel.List{} // testList := &gtsmodel.List{}
*testList = *suite.testLists["local_account_1_list_1"] // *testList = *suite.testLists["local_account_1_list_1"]
// Populate entries on this list as we'd expect them back from the db. // // Populate entries on this list as we'd expect them back from the db.
entries := make([]*gtsmodel.ListEntry, 0, len(suite.testListEntries)) // entries := make([]*gtsmodel.ListEntry, 0, len(suite.testListEntries))
for _, entry := range suite.testListEntries { // for _, entry := range suite.testListEntries {
entries = append(entries, entry) // entries = append(entries, entry)
} // }
// Sort by ID descending (again, as we'd expect from the db). // // Sort by ID descending (again, as we'd expect from the db).
slices.SortFunc(entries, func(a, b *gtsmodel.ListEntry) int { // slices.SortFunc(entries, func(a, b *gtsmodel.ListEntry) int {
const k = -1 // const k = -1
switch { // switch {
case a.ID > b.ID: // case a.ID > b.ID:
return +k // return +k
case a.ID < b.ID: // case a.ID < b.ID:
return -k // return -k
default: // default:
return 0 // return 0
} // }
}) // })
testList.ListEntries = entries // testList.ListEntries = entries
testAccount := &gtsmodel.Account{} // testAccount := &gtsmodel.Account{}
*testAccount = *suite.testAccounts["local_account_1"] // *testAccount = *suite.testAccounts["local_account_1"]
return testList, testAccount // return testList, testAccount
} // }
func (suite *ListTestSuite) checkList(expected *gtsmodel.List, actual *gtsmodel.List) { // func (suite *ListTestSuite) checkList(expected *gtsmodel.List, actual *gtsmodel.List) {
suite.Equal(expected.ID, actual.ID) // suite.Equal(expected.ID, actual.ID)
suite.Equal(expected.Title, actual.Title) // suite.Equal(expected.Title, actual.Title)
suite.Equal(expected.AccountID, actual.AccountID) // suite.Equal(expected.AccountID, actual.AccountID)
suite.Equal(expected.RepliesPolicy, actual.RepliesPolicy) // suite.Equal(expected.RepliesPolicy, actual.RepliesPolicy)
suite.NotNil(actual.Account) // suite.NotNil(actual.Account)
} // }
func (suite *ListTestSuite) checkListEntry(expected *gtsmodel.ListEntry, actual *gtsmodel.ListEntry) { // func (suite *ListTestSuite) checkListEntry(expected *gtsmodel.ListEntry, actual *gtsmodel.ListEntry) {
suite.Equal(expected.ID, actual.ID) // suite.Equal(expected.ID, actual.ID)
suite.Equal(expected.ListID, actual.ListID) // suite.Equal(expected.ListID, actual.ListID)
suite.Equal(expected.FollowID, actual.FollowID) // suite.Equal(expected.FollowID, actual.FollowID)
} // }
func (suite *ListTestSuite) checkListEntries(expected []*gtsmodel.ListEntry, actual []*gtsmodel.ListEntry) { // func (suite *ListTestSuite) checkListEntries(expected []*gtsmodel.ListEntry, actual []*gtsmodel.ListEntry) {
var ( // var (
lExpected = len(expected) // lExpected = len(expected)
lActual = len(actual) // lActual = len(actual)
) // )
if lExpected != lActual { // if lExpected != lActual {
suite.FailNow("", "expected %d list entries, got %d", lExpected, lActual) // suite.FailNow("", "expected %d list entries, got %d", lExpected, lActual)
} // }
var topID string // var topID string
for i, expectedEntry := range expected { // for i, expectedEntry := range expected {
actualEntry := actual[i] // actualEntry := actual[i]
// Ensure ID descending. // // Ensure ID descending.
if topID == "" { // if topID == "" {
topID = actualEntry.ID // topID = actualEntry.ID
} else { // } else {
suite.Less(actualEntry.ID, topID) // suite.Less(actualEntry.ID, topID)
} // }
suite.checkListEntry(expectedEntry, actualEntry) // suite.checkListEntry(expectedEntry, actualEntry)
} // }
} // }
func (suite *ListTestSuite) TestGetListByID() { // func (suite *ListTestSuite) TestGetListByID() {
testList, _ := suite.testStructs() // testList, _ := suite.testStructs()
dbList, err := suite.db.GetListByID(context.Background(), testList.ID) // dbList, err := suite.db.GetListByID(context.Background(), testList.ID)
if err != nil { // if err != nil {
suite.FailNow(err.Error()) // suite.FailNow(err.Error())
} // }
suite.checkList(testList, dbList) // suite.checkList(testList, dbList)
suite.checkListEntries(testList.ListEntries, dbList.ListEntries) // suite.checkListEntries(testList.ListEntries, dbList.ListEntries)
} // }
func (suite *ListTestSuite) TestGetListsForAccountID() { // 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.GetListsForAccountID(context.Background(), testAccount.ID)
if err != nil { // if err != nil {
suite.FailNow(err.Error()) // suite.FailNow(err.Error())
} // }
if l := len(dbLists); l != 1 { // if l := len(dbLists); l != 1 {
suite.FailNow("", "expected %d lists, got %d", 1, l) // suite.FailNow("", "expected %d lists, got %d", 1, l)
} // }
suite.checkList(testList, dbLists[0]) // suite.checkList(testList, dbLists[0])
} // }
func (suite *ListTestSuite) TestGetListEntries() { // func (suite *ListTestSuite) TestGetListEntries() {
testList, _ := suite.testStructs() // testList, _ := suite.testStructs()
dbListEntries, err := suite.db.GetListEntries(context.Background(), testList.ID, "", "", "", 0) // dbListEntries, err := suite.db.GetListEntries(context.Background(), testList.ID, "", "", "", 0)
if err != nil { // if err != nil {
suite.FailNow(err.Error()) // suite.FailNow(err.Error())
} // }
suite.checkListEntries(testList.ListEntries, dbListEntries) // suite.checkListEntries(testList.ListEntries, dbListEntries)
} // }
func (suite *ListTestSuite) TestPutList() { // func (suite *ListTestSuite) TestPutList() {
ctx := context.Background() // ctx := context.Background()
_, testAccount := suite.testStructs() // _, testAccount := suite.testStructs()
testList := &gtsmodel.List{ // testList := &gtsmodel.List{
ID: "01H0J2PMYM54618VCV8Y8QYAT4", // ID: "01H0J2PMYM54618VCV8Y8QYAT4",
Title: "Test List!", // Title: "Test List!",
AccountID: testAccount.ID, // AccountID: testAccount.ID,
} // }
if err := suite.db.PutList(ctx, testList); err != nil { // if err := suite.db.PutList(ctx, testList); err != nil {
suite.FailNow(err.Error()) // suite.FailNow(err.Error())
} // }
dbList, err := suite.db.GetListByID(ctx, testList.ID) // dbList, err := suite.db.GetListByID(ctx, testList.ID)
if err != nil { // if err != nil {
suite.FailNow(err.Error()) // suite.FailNow(err.Error())
} // }
// Bodge testlist as though default had been set. // // Bodge testlist as though default had been set.
testList.RepliesPolicy = gtsmodel.RepliesPolicyFollowed // testList.RepliesPolicy = gtsmodel.RepliesPolicyFollowed
suite.checkList(testList, dbList) // suite.checkList(testList, dbList)
} // }
func (suite *ListTestSuite) TestUpdateList() { // func (suite *ListTestSuite) TestUpdateList() {
ctx := context.Background() // ctx := context.Background()
testList, _ := suite.testStructs() // testList, _ := suite.testStructs()
// Get List in the cache first. // // Get List in the cache first.
dbList, err := suite.db.GetListByID(ctx, testList.ID) // dbList, err := suite.db.GetListByID(ctx, testList.ID)
if err != nil { // if err != nil {
suite.FailNow(err.Error()) // suite.FailNow(err.Error())
} // }
// Now do the update. // // Now do the update.
testList.Title = "New Title!" // testList.Title = "New Title!"
if err := suite.db.UpdateList(ctx, testList, "title"); err != nil { // if err := suite.db.UpdateList(ctx, testList, "title"); err != nil {
suite.FailNow(err.Error()) // suite.FailNow(err.Error())
} // }
// Cache should be invalidated // // Cache should be invalidated
// + we should have updated list. // // + we should have updated list.
dbList, err = suite.db.GetListByID(ctx, testList.ID) // dbList, err = suite.db.GetListByID(ctx, testList.ID)
if err != nil { // if err != nil {
suite.FailNow(err.Error()) // suite.FailNow(err.Error())
} // }
suite.checkList(testList, dbList) // suite.checkList(testList, dbList)
} // }
func (suite *ListTestSuite) TestDeleteList() { // func (suite *ListTestSuite) TestDeleteList() {
ctx := context.Background() // ctx := context.Background()
testList, _ := suite.testStructs() // testList, _ := suite.testStructs()
// Get List in the cache first. // // Get List in the cache first.
if _, err := suite.db.GetListByID(ctx, testList.ID); err != nil { // if _, err := suite.db.GetListByID(ctx, testList.ID); err != nil {
suite.FailNow(err.Error()) // suite.FailNow(err.Error())
} // }
// Now do the delete. // // Now do the delete.
if err := suite.db.DeleteListByID(ctx, testList.ID); err != nil { // if err := suite.db.DeleteListByID(ctx, testList.ID); err != nil {
suite.FailNow(err.Error()) // suite.FailNow(err.Error())
} // }
// Cache should be invalidated // // Cache should be invalidated
// + we should have no list. // // + we should have no list.
_, err := suite.db.GetListByID(ctx, testList.ID) // _, err := suite.db.GetListByID(ctx, testList.ID)
suite.ErrorIs(err, db.ErrNoEntries) // suite.ErrorIs(err, db.ErrNoEntries)
// All entries belonging to this // // All entries belonging to this
// list should now be deleted. // // list should now be deleted.
listEntries, err := suite.db.GetListEntries(ctx, testList.ID, "", "", "", 0) // listEntries, err := suite.db.GetListEntries(ctx, testList.ID, "", "", "", 0)
if err != nil { // if err != nil {
suite.FailNow(err.Error()) // suite.FailNow(err.Error())
} // }
suite.Empty(listEntries) // suite.Empty(listEntries)
} // }
func (suite *ListTestSuite) TestPutListEntries() { // func (suite *ListTestSuite) TestPutListEntries() {
ctx := context.Background() // ctx := context.Background()
testList, _ := suite.testStructs() // testList, _ := suite.testStructs()
listEntries := []*gtsmodel.ListEntry{ // listEntries := []*gtsmodel.ListEntry{
{ // {
ID: "01H0MKMQY69HWDSDR2SWGA17R4", // ID: "01H0MKMQY69HWDSDR2SWGA17R4",
ListID: testList.ID, // ListID: testList.ID,
FollowID: "01H0MKNFRFZS8R9WV6DBX31Y03", // random id, doesn't exist // FollowID: "01H0MKNFRFZS8R9WV6DBX31Y03", // random id, doesn't exist
}, // },
{ // {
ID: "01H0MKPGQF0E7QAVW5BKTHZ630", // ID: "01H0MKPGQF0E7QAVW5BKTHZ630",
ListID: testList.ID, // ListID: testList.ID,
FollowID: "01H0MKP6RR8VEHN3GVWFBP2H30", // random id, doesn't exist // FollowID: "01H0MKP6RR8VEHN3GVWFBP2H30", // random id, doesn't exist
}, // },
{ // {
ID: "01H0MKPPP2DT68FRBMR1FJM32T", // ID: "01H0MKPPP2DT68FRBMR1FJM32T",
ListID: testList.ID, // ListID: testList.ID,
FollowID: "01H0MKQ0KA29C6NFJ27GTZD16J", // random id, doesn't exist // FollowID: "01H0MKQ0KA29C6NFJ27GTZD16J", // random id, doesn't exist
}, // },
} // }
if err := suite.db.PutListEntries(ctx, listEntries); err != nil { // if err := suite.db.PutListEntries(ctx, listEntries); err != nil {
suite.FailNow(err.Error()) // suite.FailNow(err.Error())
} // }
// Add these entries to the test list, sort it again // // Add these entries to the test list, sort it again
// to reflect what we'd expect to get from the db. // // to reflect what we'd expect to get from the db.
testList.ListEntries = append(testList.ListEntries, listEntries...) // testList.ListEntries = append(testList.ListEntries, listEntries...)
slices.SortFunc(testList.ListEntries, func(a, b *gtsmodel.ListEntry) int { // slices.SortFunc(testList.ListEntries, func(a, b *gtsmodel.ListEntry) int {
const k = -1 // const k = -1
switch { // switch {
case a.ID > b.ID: // case a.ID > b.ID:
return +k // return +k
case a.ID < b.ID: // case a.ID < b.ID:
return -k // return -k
default: // default:
return 0 // return 0
} // }
}) // })
// Now get all list entries from the db. // // Now get all list entries from the db.
// Use barebones for this because the ones // // Use barebones for this because the ones
// we just added will fail if we try to get // // we just added will fail if we try to get
// the nonexistent follows. // // the nonexistent follows.
dbListEntries, err := suite.db.GetListEntries( // dbListEntries, err := suite.db.GetListEntries(
gtscontext.SetBarebones(ctx), // gtscontext.SetBarebones(ctx),
testList.ID, // testList.ID,
"", "", "", 0) // "", "", "", 0)
if err != nil { // if err != nil {
suite.FailNow(err.Error()) // suite.FailNow(err.Error())
} // }
suite.checkListEntries(testList.ListEntries, dbListEntries) // suite.checkListEntries(testList.ListEntries, dbListEntries)
} // }
func (suite *ListTestSuite) TestDeleteListEntry() { // func (suite *ListTestSuite) TestDeleteListEntry() {
ctx := context.Background() // ctx := context.Background()
testList, _ := suite.testStructs() // testList, _ := suite.testStructs()
// Get List in the cache first. // // Get List in the cache first.
if _, err := suite.db.GetListByID(ctx, testList.ID); err != nil { // if _, err := suite.db.GetListByID(ctx, testList.ID); err != nil {
suite.FailNow(err.Error()) // suite.FailNow(err.Error())
} // }
// Delete the first entry. // // Delete the first entry.
if err := suite.db.DeleteListEntry(ctx, testList.ListEntries[0].ID); err != nil { // if err := suite.db.DeleteListEntry(ctx, testList.ListEntries[0].ID); err != nil {
suite.FailNow(err.Error()) // suite.FailNow(err.Error())
} // }
// Get list from the db again. // // Get list from the db again.
dbList, err := suite.db.GetListByID(ctx, testList.ID) // dbList, err := suite.db.GetListByID(ctx, testList.ID)
if err != nil { // if err != nil {
suite.FailNow(err.Error()) // suite.FailNow(err.Error())
} // }
// Bodge the testlist as though // // Bodge the testlist as though
// we'd removed the first entry. // // we'd removed the first entry.
testList.ListEntries = testList.ListEntries[1:] // testList.ListEntries = testList.ListEntries[1:]
suite.checkList(testList, dbList) // suite.checkList(testList, dbList)
} // }
func (suite *ListTestSuite) TestDeleteListEntriesForFollowID() { // func (suite *ListTestSuite) TestDeleteListEntriesForFollowID() {
ctx := context.Background() // ctx := context.Background()
testList, _ := suite.testStructs() // testList, _ := suite.testStructs()
// Get List in the cache first. // // Get List in the cache first.
if _, err := suite.db.GetListByID(ctx, testList.ID); err != nil { // if _, err := suite.db.GetListByID(ctx, testList.ID); err != nil {
suite.FailNow(err.Error()) // suite.FailNow(err.Error())
} // }
// Delete the first entry. // // Delete the first entry.
if err := suite.db.DeleteListEntriesForFollowID(ctx, testList.ListEntries[0].FollowID); err != nil { // if err := suite.db.DeleteListEntriesTargettingFollowID(ctx, testList.ListEntries[0].FollowID); err != nil {
suite.FailNow(err.Error()) // suite.FailNow(err.Error())
} // }
// Get list from the db again. // // Get list from the db again.
dbList, err := suite.db.GetListByID(ctx, testList.ID) // dbList, err := suite.db.GetListByID(ctx, testList.ID)
if err != nil { // if err != nil {
suite.FailNow(err.Error()) // suite.FailNow(err.Error())
} // }
// Bodge the testlist as though // // Bodge the testlist as though
// we'd removed the first entry. // // we'd removed the first entry.
testList.ListEntries = testList.ListEntries[1:] // testList.ListEntries = testList.ListEntries[1:]
suite.checkList(testList, dbList) // suite.checkList(testList, dbList)
} // }
func (suite *ListTestSuite) TestListIncludesAccount() { // func (suite *ListTestSuite) TestListIncludesAccount() {
ctx := context.Background() // ctx := context.Background()
testList, _ := suite.testStructs() // testList, _ := suite.testStructs()
for accountID, expected := range map[string]bool{ // for accountID, expected := range map[string]bool{
suite.testAccounts["admin_account"].ID: true, // suite.testAccounts["admin_account"].ID: true,
suite.testAccounts["local_account_1"].ID: false, // suite.testAccounts["local_account_1"].ID: false,
suite.testAccounts["local_account_2"].ID: true, // suite.testAccounts["local_account_2"].ID: true,
"01H7074GEZJ56J5C86PFB0V2CT": false, // "01H7074GEZJ56J5C86PFB0V2CT": false,
} { // } {
includes, err := suite.db.ListIncludesAccount(ctx, testList.ID, accountID) // includes, err := suite.db.ListIncludesAccount(ctx, testList.ID, accountID)
if err != nil { // if err != nil {
suite.FailNow(err.Error()) // suite.FailNow(err.Error())
} // }
if includes != expected { // if includes != expected {
suite.FailNow("", "expected %t for accountID %s got %t", expected, accountID, includes) // suite.FailNow("", "expected %t for accountID %s got %t", expected, accountID, includes)
} // }
} // }
} // }
func TestListTestSuite(t *testing.T) { func TestListTestSuite(t *testing.T) {
suite.Run(t, new(ListTestSuite)) suite.Run(t, new(ListTestSuite))

View file

@ -262,7 +262,7 @@ func (r *relationshipDB) deleteFollow(ctx context.Context, id string) error {
} }
// Delete every list entry that used this followID. // Delete every list entry that used this followID.
if err := r.state.DB.DeleteListEntriesForFollowID(ctx, id); err != nil { if err := r.state.DB.DeleteListEntriesTargettingFollowID(ctx, id); err != nil {
return fmt.Errorf("deleteFollow: error deleting list entries: %w", err) return fmt.Errorf("deleteFollow: error deleting list entries: %w", err)
} }
@ -382,7 +382,7 @@ func (r *relationshipDB) DeleteAccountFollows(ctx context.Context, accountID str
for _, id := range followIDs { for _, id := range followIDs {
// Finally, delete all list entries associated with each follow ID. // Finally, delete all list entries associated with each follow ID.
if err := r.state.DB.DeleteListEntriesForFollowID(ctx, id); err != nil { if err := r.state.DB.DeleteListEntriesTargettingFollowID(ctx, id); err != nil {
return err return err
} }
} }

View file

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

View file

@ -20,6 +20,7 @@ package bundb
import ( import (
"context" "context"
"errors" "errors"
"slices"
"strings" "strings"
"github.com/superseriousbusiness/gotosocial/internal/db" "github.com/superseriousbusiness/gotosocial/internal/db"
@ -148,17 +149,11 @@ func (t *tagDB) GetFollowedTags(ctx context.Context, accountID string, page *pag
if err != nil { if err != nil {
return nil, err return nil, err
} }
return t.GetTags(ctx, tagIDs)
tags, err := t.GetTags(ctx, tagIDs)
if err != nil {
return nil, err
}
return tags, nil
} }
func (t *tagDB) getTagIDsFollowedByAccount(ctx context.Context, accountID string, page *paging.Page) ([]string, error) { 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 var tagIDs []string
// Tag IDs not in cache. Perform DB query. // Tag IDs not in cache. Perform DB query.
@ -178,7 +173,7 @@ func (t *tagDB) getTagIDsFollowedByAccount(ctx context.Context, accountID string
} }
func (t *tagDB) getAccountIDsFollowingTag(ctx context.Context, tagID string) ([]string, error) { 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 var accountIDs []string
// Account IDs not in cache. Perform DB query. // Account IDs not in cache. Perform DB query.
@ -198,18 +193,17 @@ func (t *tagDB) getAccountIDsFollowingTag(ctx context.Context, tagID string) ([]
} }
func (t *tagDB) IsAccountFollowingTag(ctx context.Context, accountID string, tagID string) (bool, error) { 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 { if err != nil {
return false, err return false, err
} }
for _, accountTagID := range accountTagIDs { // NOTE: it should only become useful to cache
if accountTagID == tagID { // FollowedTag{} objects separately here (for
return true, nil // caching by AccountID.TagID) only if the number
} // of accounts following a tag becomes rather ridiculous,
} // i.e. in the order of thousands. So we should be good.
return slices.Contains(followingTagIDs, tagID), nil
return false, nil
} }
func (t *tagDB) PutFollowedTag(ctx context.Context, accountID string, tagID string) error { func (t *tagDB) PutFollowedTag(ctx context.Context, accountID string, tagID string) error {
@ -234,9 +228,15 @@ func (t *tagDB) PutFollowedTag(ctx context.Context, accountID string, tagID stri
return nil return nil
} }
// Otherwise, this is a new followed tag, so we invalidate caches related to it. // If we deleted anything, invalidate caches.
t.state.Caches.DB.AccountIDsFollowingTag.Invalidate(tagID) t.state.Caches.DB.FollowingTagIDs.Invalidate(
t.state.Caches.DB.TagIDsFollowedByAccount.Invalidate(accountID)
// tag IDs followed by account
">"+accountID,
// account IDs following tag
"<"+tagID,
)
return nil return nil
} }
@ -259,9 +259,15 @@ func (t *tagDB) DeleteFollowedTag(ctx context.Context, accountID string, tagID s
return nil return nil
} }
// If we deleted anything, invalidate caches related to it. // If we deleted anything, invalidate caches.
t.state.Caches.DB.AccountIDsFollowingTag.Invalidate(tagID) t.state.Caches.DB.FollowingTagIDs.Invalidate(
t.state.Caches.DB.TagIDsFollowedByAccount.Invalidate(accountID)
// tag IDs followed by account
">"+accountID,
// account IDs following tag
"<"+tagID,
)
return err return err
} }
@ -278,16 +284,26 @@ func (t *tagDB) DeleteFollowedTagsByAccountID(ctx context.Context, accountID str
return gtserror.Newf("error deleting followed tags for account %s: %w", accountID, err) return gtserror.Newf("error deleting followed tags for account %s: %w", accountID, err)
} }
// Invalidate account ID caches for the account and those tags. // Convert tag IDs to the keys
t.state.Caches.DB.TagIDsFollowedByAccount.Invalidate(accountID) // we use for caching tag follow
t.state.Caches.DB.AccountIDsFollowingTag.Invalidate(tagIDs...) // 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 return nil
} }
func (t *tagDB) GetAccountIDsFollowingTagIDs(ctx context.Context, tagIDs []string) ([]string, error) { 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. // Make conservative estimate for no. accounts.
accountIDs := []string{} accountIDs := make([]string, 0, len(tagIDs))
// Gather all accounts following tags.
for _, tagID := range tagIDs { for _, tagID := range tagIDs {
tagAccountIDs, err := t.getAccountIDsFollowingTag(ctx, tagID) tagAccountIDs, err := t.getAccountIDsFollowingTag(ctx, tagID)
if err != nil { if err != nil {
@ -295,5 +311,8 @@ func (t *tagDB) GetAccountIDsFollowingTagIDs(ctx context.Context, tagIDs []strin
} }
accountIDs = append(accountIDs, tagAccountIDs...) 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 return util.Deduplicate(accountIDs), nil
} }

View file

@ -370,30 +370,20 @@ func (t *timelineDB) GetListTimeline(
frontToBack = true frontToBack = true
) )
// Fetch all listEntries entries from the database. // Fetch all follow IDs contained in list from DB.
listEntries, err := t.state.DB.GetListEntries( followIDs, err := t.state.DB.GetFollowIDsInList(
// Don't need actual follows ctx, listID, nil,
// for this, just the IDs.
gtscontext.SetBarebones(ctx),
listID,
"", "", "", 0,
) )
if err != 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. // possibly return anything for this list.
if len(listEntries) == 0 { if len(followIDs) == 0 {
return make([]*gtsmodel.Status, 0), nil 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. // Select target account IDs from follows.
subQ := t.db. subQ := t.db.
NewSelect(). NewSelect().

View file

@ -21,6 +21,7 @@ import (
"context" "context"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/internal/paging"
) )
type List interface { type List interface {
@ -30,11 +31,29 @@ type List interface {
// GetListsByIDs fetches all lists with the provided IDs. // GetListsByIDs fetches all lists with the provided IDs.
GetListsByIDs(ctx context.Context, ids []string) ([]*gtsmodel.List, error) GetListsByIDs(ctx context.Context, ids []string) ([]*gtsmodel.List, error)
// GetListsForAccountID gets all lists owned by the given accountID. // GetListsByAccountID gets all lists owned by the given accountID.
GetListsForAccountID(ctx context.Context, accountID string) ([]*gtsmodel.List, error) GetListsByAccountID(ctx context.Context, accountID string) ([]*gtsmodel.List, error)
// CountListsForAccountID counts the number of lists owned by the given accountID. // CountListsByAccountID counts the number of lists owned by the given accountID.
CountListsForAccountID(ctx context.Context, accountID string) (int, error) CountListsByAccountID(ctx context.Context, accountID string) (int, error)
// GetListsWithFollowID gets all lists that contain the given follow with ID.
GetListsWithFollowID(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.
IsAccountInListID(ctx context.Context, listID string, accountID string) (bool, error)
// PopulateList ensures that the list's struct fields are populated. // PopulateList ensures that the list's struct fields are populated.
PopulateList(ctx context.Context, list *gtsmodel.List) error PopulateList(ctx context.Context, list *gtsmodel.List) error
@ -49,31 +68,13 @@ type List interface {
// DeleteListByID deletes one list with the given ID. // DeleteListByID deletes one list with the given ID.
DeleteListByID(ctx context.Context, id string) error 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. // PutListEntries inserts a slice of listEntries into the database.
// It uses a transaction to ensure no partial updates. // It uses a transaction to ensure no partial updates.
PutListEntries(ctx context.Context, listEntries []*gtsmodel.ListEntry) error PutListEntries(ctx context.Context, listEntries []*gtsmodel.ListEntry) error
// DeleteListEntry deletes one list entry with the given id. // DeleteListEntry deletes the list entry with given list ID and follow ID.
DeleteListEntry(ctx context.Context, id string) error DeleteListEntry(ctx context.Context, listID string, followID string) error
// DeleteListEntryForFollowID deletes all list entries with the given followID. // DeleteListEntryForFollowID deletes all list entries with the given followID.
DeleteListEntriesForFollowID(ctx context.Context, followID string) error DeleteListEntriesTargettingFollowID(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)
} }

View file

@ -68,6 +68,9 @@ type Relationship interface {
// GetFollow retrieves a follow if it exists between source and target accounts. // GetFollow retrieves a follow if it exists between source and target accounts.
GetFollow(ctx context.Context, sourceAccountID string, targetAccountID string) (*gtsmodel.Follow, error) 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 populates the struct pointers on the given follow.
PopulateFollow(ctx context.Context, follow *gtsmodel.Follow) error PopulateFollow(ctx context.Context, follow *gtsmodel.Follow) error

View file

@ -27,7 +27,6 @@ type List struct {
Title string `bun:",nullzero,notnull,unique:listaccounttitle"` // Title of this list. 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 AccountID string `bun:"type:CHAR(26),notnull,nullzero,unique:listaccounttitle"` // Account that created/owns the list
Account *Account `bun:"-"` // Account corresponding to accountID 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. 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. Exclusive *bool `bun:",nullzero,notnull,default:false"` // Hide posts from members of this list from your home timeline.
} }
@ -38,8 +37,9 @@ type ListEntry struct {
CreatedAt time.Time `bun:"type:timestamptz,nullzero,notnull,default:current_timestamp"` // when was item created CreatedAt time.Time `bun:"type:timestamptz,nullzero,notnull,default:current_timestamp"` // when was item created
UpdatedAt time.Time `bun:"type:timestamptz,nullzero,notnull,default:current_timestamp"` // when was item last updated UpdatedAt time.Time `bun:"type:timestamptz,nullzero,notnull,default:current_timestamp"` // when was item last updated
ListID string `bun:"type:CHAR(26),notnull,nullzero,unique:listentrylistfollow"` // ID of the list that this entry belongs to. ListID string `bun:"type:CHAR(26),notnull,nullzero,unique:listentrylistfollow"` // ID of the list that this entry belongs to.
FollowID string `bun:"type:CHAR(26),notnull,nullzero,unique:listentrylistfollow"` // Follow that the account owning this entry wants to see posts of in the timeline. // List *List `bun:"-"` //
Follow *Follow `bun:"-"` // Follow corresponding to followID. FollowID string `bun:"type:CHAR(26),notnull,nullzero,unique:listentrylistfollow"` // Follow that the account owning this entry wants to see posts of in the timeline.
Follow *Follow `bun:"-"` // Follow corresponding to followID.
} }
// RepliesPolicy denotes which replies should be shown in the list. // RepliesPolicy denotes which replies should be shown in the list.

View file

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

View file

@ -30,8 +30,6 @@ import (
"github.com/superseriousbusiness/gotosocial/internal/log" "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. // 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) { func (p *Processor) ListsGet(ctx context.Context, requestingAccount *gtsmodel.Account, targetAccountID string) ([]*apimodel.List, gtserror.WithCode) {
targetAccount, err := p.state.DB.GetAccountByID(ctx, targetAccountID) 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 // Requester has to follow targetAccount
// for them to be in any of their lists. // for them to be in any of their lists.
follow, err := p.state.DB.GetFollow( follow, err := p.state.DB.GetFollow(
// Don't populate follow. // Don't populate follow.
gtscontext.SetBarebones(ctx), gtscontext.SetBarebones(ctx),
requestingAccount.ID, requestingAccount.ID,
targetAccountID, targetAccountID,
) )
if err != nil && !errors.Is(err, db.ErrNoEntries) { 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 { 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( // Get all lists that this follow is an entry within.
// Don't populate entries. lists, err := p.state.DB.GetListsWithFollowID(ctx, follow.ID)
gtscontext.SetBarebones(ctx), if err != nil {
follow.ID, err := gtserror.Newf("error getting lists for follow: %w", err)
) return nil, gtserror.NewErrorInternalError(err)
if err != nil && !errors.Is(err, db.ErrNoEntries) {
return nil, gtserror.NewErrorInternalError(fmt.Errorf("db error: %w", err))
} }
count := len(listEntries) apiLists := make([]*apimodel.List, 0, len(lists))
if count == 0 { for _, list := range lists {
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
}
apiList, err := p.converter.ListToAPIList(ctx, list) apiList, err := p.converter.ListToAPIList(ctx, list)
if err != nil { 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 continue
} }
apiLists = append(apiLists, apiList) apiLists = append(apiLists, apiList)
} }

View file

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

View file

@ -20,7 +20,6 @@ package list
import ( import (
"context" "context"
"errors" "errors"
"fmt"
apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
"github.com/superseriousbusiness/gotosocial/internal/db" "github.com/superseriousbusiness/gotosocial/internal/db"
@ -28,7 +27,7 @@ import (
"github.com/superseriousbusiness/gotosocial/internal/gtserror" "github.com/superseriousbusiness/gotosocial/internal/gtserror"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/internal/log" "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. // 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). // 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) { 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 // Use barebones ctx; no embedded
// structs necessary for simple GET. // structs necessary for simple GET.
gtscontext.SetBarebones(ctx), gtscontext.SetBarebones(ctx),
account.ID, account.ID,
) )
if err != nil { if err != nil && !errors.Is(err, db.ErrNoEntries) {
if errors.Is(err, db.ErrNoEntries) {
return nil, nil
}
return nil, gtserror.NewErrorInternalError(err) return nil, gtserror.NewErrorInternalError(err)
} }
@ -68,66 +65,23 @@ func (p *Processor) GetAll(ctx context.Context, account *gtsmodel.Account) ([]*a
if errWithCode != nil { if errWithCode != nil {
return nil, errWithCode return nil, errWithCode
} }
apiLists = append(apiLists, apiList) apiLists = append(apiLists, apiList)
} }
return apiLists, nil 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. // 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( func (p *Processor) GetListAccounts(
ctx context.Context, ctx context.Context,
account *gtsmodel.Account, account *gtsmodel.Account,
listID string, listID string,
maxID string, page *paging.Page,
sinceID string,
minID string,
limit int,
) (*apimodel.PageableResponse, gtserror.WithCode) { ) (*apimodel.PageableResponse, gtserror.WithCode) {
// Ensure list exists + is owned by requesting account. // Ensure list exists + is owned by requesting account.
_, errWithCode := p.getList( _, errWithCode := p.getList(
// Use barebones ctx; no embedded // Use barebones ctx; no embedded
// structs necessary for this call. // structs necessary for this call.
gtscontext.SetBarebones(ctx), gtscontext.SetBarebones(ctx),
@ -138,71 +92,45 @@ func (p *Processor) GetListAccounts(
return nil, errWithCode return nil, errWithCode
} }
// To know which accounts are in the list, // Get all accounts contained within list.
// we need to first get requested list entries. accounts, err := p.state.DB.GetAccountsInList(ctx,
listEntries, err := p.state.DB.GetListEntries(ctx, listID, maxID, sinceID, minID, limit) listID,
if err != nil && !errors.Is(err, db.ErrNoEntries) { page,
err = fmt.Errorf("GetListAccounts: error getting list entries: %w", err) )
if err != nil {
err := gtserror.Newf("db error getting accounts in list: %w", err)
return nil, gtserror.NewErrorInternalError(err) return nil, gtserror.NewErrorInternalError(err)
} }
count := len(listEntries) // Check for any accounts.
count := len(accounts)
if count == 0 { if count == 0 {
// No list entries means no accounts. return paging.EmptyResponse(), nil
return util.EmptyPageableResponse(), nil
} }
var ( var (
// Preallocate expected frontend items.
items = make([]interface{}, 0, count) items = make([]interface{}, 0, count)
// Set next + prev values before filtering and API // Set paging low / high IDs.
// converting, so caller can still page properly. lo = accounts[count-1].ID
nextMaxIDValue = listEntries[count-1].ID hi = accounts[0].ID
prevMinIDValue = listEntries[0].ID
) )
// Extract accounts from list entries + add them to response. // Convert accounts to frontend.
p.accountsFromListEntries(ctx, listEntries, func(acc *apimodel.Account) { for _, account := range accounts {
items = append(items, acc) apiAccount, err := p.converter.AccountToAPIAccountPublic(ctx, account)
})
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)
if err != nil { 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 continue
} }
items = append(items, apiAccount)
appendAcc(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,74 @@ import (
"fmt" "fmt"
"github.com/superseriousbusiness/gotosocial/internal/db" "github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/gtscontext"
"github.com/superseriousbusiness/gotosocial/internal/gtserror" "github.com/superseriousbusiness/gotosocial/internal/gtserror"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/internal/id" "github.com/superseriousbusiness/gotosocial/internal/id"
"github.com/superseriousbusiness/gotosocial/internal/util"
) )
// AddToList adds targetAccountIDs to the given list, if valid. // 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 { func (p *Processor) AddToList(ctx context.Context, account *gtsmodel.Account, listID string, targetAccountIDs []string) gtserror.WithCode {
// Ensure this list exists + account owns it. // 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 { if errWithCode != nil {
return errWithCode return errWithCode
} }
// Pre-assemble list of entries to add. We *could* add these // Get all follows that are entries in list.
// one by one as we iterate through accountIDs, but according follows, err := p.state.DB.GetFollowsInList(
// 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))
// Check each targetAccountID is valid. // We only need barebones model.
// - Follow must exist. gtscontext.SetBarebones(ctx),
// - Follow must not already be in the given list. 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
})
// 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 { for _, targetAccountID := range targetAccountIDs {
// Ensure follow exists.
follow, err := p.state.DB.GetFollow(ctx, account.ID, targetAccountID) // Look for existing follow targetting ID.
if err != nil { follow, ok := followsMap[targetAccountID]
if errors.Is(err, db.ErrNoEntries) {
err = fmt.Errorf("you do not follow account %s", targetAccountID) if ok {
return gtserror.NewErrorNotFound(err, err.Error()) text := fmt.Sprintf("account %s is already in list %s", targetAccountID, listID)
} return gtserror.NewErrorUnprocessableEntity(errors.New(text), text)
return gtserror.NewErrorInternalError(err)
} }
// Ensure followID not already in list. // Generate new entry for this follow in list.
// This particular call to isInList will entries = append(entries, &gtsmodel.ListEntry{
// 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())
}
// Entry wasn't in the list, we can add it.
listEntries = append(listEntries, &gtsmodel.ListEntry{
ID: id.NewULID(), ID: id.NewULID(),
ListID: listID, ListID: listID,
FollowID: follow.ID, FollowID: follow.ID,
}) })
} }
// If we get to here we can assume all // Add all of the gathered list entries to the database.
// entries are valid, so try to add them. switch err := p.state.DB.PutListEntries(ctx, entries); {
if err := p.state.DB.PutListEntries(ctx, listEntries); err != nil { case err == nil:
if errors.Is(err, db.ErrAlreadyExists) {
err = fmt.Errorf("one or more errors inserting list entries: %w", err) case errors.Is(err, db.ErrAlreadyExists):
return gtserror.NewErrorUnprocessableEntity(err, err.Error()) 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) return gtserror.NewErrorInternalError(err)
} }
@ -97,55 +98,61 @@ func (p *Processor) AddToList(ctx context.Context, account *gtsmodel.Account, li
} }
// RemoveFromList removes targetAccountIDs from the given list, if valid. // 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. // 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 { if errWithCode != nil {
return errWithCode return errWithCode
} }
// For each targetAccountID, we want to check if // Get all follows that are entries in list.
// a follow with that targetAccountID is in the follows, err := p.state.DB.GetFollowsInList(
// given list. If it is in there, we want to remove
// it from the 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 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 { 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. // Look for follow targetting this account.
return listEntry.Follow.TargetAccountID, nil follow, ok := followsMap[targetAccountID]
},
)
// Error may be returned here if there was an issue if !ok {
// populating the list entry. We only return on proper // not in list.
// 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.
continue continue
} }
// TargetAccount was in the list, remove the entry. // Delete the list entry containing follow ID in list.
if err := p.state.DB.DeleteListEntry(ctx, entryID); err != nil && !errors.Is(err, db.ErrNoEntries) { err := p.state.DB.DeleteListEntry(ctx, listID, follow.ID)
err = fmt.Errorf("error removing list entry %s from list %s: %w", entryID, listID, err) if err != nil && !errors.Is(err, db.ErrNoEntries) {
return gtserror.NewErrorInternalError(err) 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 return nil
} }

View file

@ -33,18 +33,25 @@ import (
// appropriate errors so caller doesn't need to bother. // appropriate errors so caller doesn't need to bother.
func (p *Processor) getList(ctx context.Context, accountID string, listID string) (*gtsmodel.List, gtserror.WithCode) { func (p *Processor) getList(ctx context.Context, accountID string, listID string) (*gtsmodel.List, gtserror.WithCode) {
list, err := p.state.DB.GetListByID(ctx, listID) list, err := p.state.DB.GetListByID(ctx, listID)
if err != nil { if err != nil && !errors.Is(err, db.ErrNoEntries) {
if errors.Is(err, db.ErrNoEntries) { err := gtserror.Newf("db error getting list: %w", err)
// List doesn't seem to exist.
return nil, gtserror.NewErrorNotFound(err)
}
// Real database error.
return nil, gtserror.NewErrorInternalError(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 { if list.AccountID != accountID {
err = fmt.Errorf("list with id %s does not belong to account %s", list.ID, accountID) const text = "list not found"
return nil, gtserror.NewErrorNotFound(err) return nil, gtserror.NewErrorNotFound(
errors.New("list does not belong to account"),
text,
)
} }
return list, nil return list, nil
@ -60,26 +67,3 @@ func (p *Processor) apiList(ctx context.Context, list *gtsmodel.List) (*apimodel
return apiList, nil 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. // 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()) suite.FailNow(err.Error())
} }

View file

@ -21,7 +21,6 @@ import (
"context" "context"
"errors" "errors"
"github.com/superseriousbusiness/gotosocial/internal/db"
statusfilter "github.com/superseriousbusiness/gotosocial/internal/filter/status" statusfilter "github.com/superseriousbusiness/gotosocial/internal/filter/status"
"github.com/superseriousbusiness/gotosocial/internal/filter/usermute" "github.com/superseriousbusiness/gotosocial/internal/filter/usermute"
"github.com/superseriousbusiness/gotosocial/internal/gtscontext" "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. // Timeline the status for each local follower of this account. This will
// This will also handle notifying any followers with notify // also handle notifying any followers with notify set to true on their follow.
// set to true on their follow. homeTimelinedAccountIDs := s.timelineAndNotifyStatusForFollowers(ctx, status, follows)
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 account who follows a tag used by this status. // Timeline the status for each local account who follows a tag used by this status.
if err := s.timelineAndNotifyStatusForTagFollowers(ctx, status, homeTimelinedAccountIDs); err != nil { if err := s.timelineAndNotifyStatusForTagFollowers(ctx, status, homeTimelinedAccountIDs); err != nil {
@ -105,12 +100,10 @@ func (s *Surface) timelineAndNotifyStatusForFollowers(
ctx context.Context, ctx context.Context,
status *gtsmodel.Status, status *gtsmodel.Status,
follows []*gtsmodel.Follow, follows []*gtsmodel.Follow,
) ([]string, error) { ) (homeTimelinedAccountIDs []string) {
var ( var (
errs gtserror.MultiError boost = (status.BoostOfID != "")
boost = status.BoostOfID != "" reply = (status.InReplyToURI != "")
reply = status.InReplyToURI != ""
homeTimelinedAccountIDs = []string{}
) )
for _, follow := range follows { for _, follow := range follows {
@ -130,7 +123,7 @@ func (s *Surface) timelineAndNotifyStatusForFollowers(
ctx, follow.Account, status, ctx, follow.Account, status,
) )
if err != nil { 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 continue
} }
@ -139,29 +132,36 @@ func (s *Surface) timelineAndNotifyStatusForFollowers(
continue 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) filters, mutes, err := s.getFiltersAndMutes(ctx, follow.AccountID)
if err != nil { if err != nil {
errs.Append(err) log.Error(ctx, err)
continue continue
} }
// Add status to any relevant lists // Add status to any relevant lists for this follow, if applicable.
// for this follow, if applicable. listTimelined, exclusive, err := s.listTimelineStatusForFollow(ctx,
exclusive, listTimelined := s.listTimelineStatusForFollow(
ctx,
status, status,
follow, follow,
&errs,
filters, filters,
mutes, mutes,
) )
if err != nil {
log.Errorf(ctx, "error list timelining status: %v", err)
continue
}
// Add status to home timeline for owner var homeTimelined bool
// of this follow, if applicable.
homeTimelined := false // If this was timelined into
// list with exclusive flag set,
// don't add to home timeline.
if !exclusive { 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, s.State.Timelines.Home.IngestOne,
follow.AccountID, // home timelines are keyed by account ID follow.AccountID, // home timelines are keyed by account ID
follow.Account, follow.Account,
@ -171,10 +171,12 @@ func (s *Surface) timelineAndNotifyStatusForFollowers(
mutes, mutes,
) )
if err != nil { if err != nil {
errs.Appendf("error home timelining status: %w", err) log.Errorf(ctx, "error home timelining status: %v", err)
continue continue
} }
if homeTimelined { if homeTimelined {
// If hometimelined, add to list of returned account IDs.
homeTimelinedAccountIDs = append(homeTimelinedAccountIDs, follow.AccountID) homeTimelinedAccountIDs = append(homeTimelinedAccountIDs, follow.AccountID)
} }
} }
@ -210,11 +212,12 @@ func (s *Surface) timelineAndNotifyStatusForFollowers(
status.Account, status.Account,
status.ID, status.ID,
); err != nil { ); 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 // listTimelineStatusForFollow puts the given status
@ -227,107 +230,59 @@ func (s *Surface) listTimelineStatusForFollow(
ctx context.Context, ctx context.Context,
status *gtsmodel.Status, status *gtsmodel.Status,
follow *gtsmodel.Follow, follow *gtsmodel.Follow,
errs *gtserror.MultiError,
filters []*gtsmodel.Filter, filters []*gtsmodel.Filter,
mutes *usermute.CompiledUserMuteList, mutes *usermute.CompiledUserMuteList,
) (bool, bool) { ) (timelined bool, exclusive bool, err error) {
// 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.
listEntries, err := s.getListEntries(ctx, follow) // Get all lists that contain this follow.
lists, err := s.State.DB.GetListsWithFollowID(
// We don't need list sub-models.
gtscontext.SetBarebones(ctx),
follow.ID,
)
if err != nil { if err != nil {
errs.Append(err) return false, false, gtserror.Newf("error getting lists for follow: %w", err)
return false, false
}
exclusive, err := s.isAnyListExclusive(ctx, listEntries)
if err != nil {
errs.Append(err)
return false, false
} }
// Check eligibility for each list entry (if any). for _, list := range lists {
listTimelined := false // Update exclusive flag if list is so.
for _, listEntry := range listEntries { exclusive = exclusive || *list.Exclusive
eligible, err := s.listEligible(ctx, listEntry, status)
// Check whether list is eligible for this status.
eligible, err := s.listEligible(ctx, list, status)
if err != nil { if err != nil {
errs.Appendf("error checking list eligibility: %w", err) log.Errorf(ctx, "error checking list eligibility: %v", err)
continue continue
} }
if !eligible { if !eligible {
// Don't add this.
continue continue
} }
// At this point we are certain this status // At this point we are certain this status
// should be included in the timeline of the // should be included in the timeline of the
// list that this list entry belongs to. // list that this list entry belongs to.
timelined, err := s.timelineStatus( listTimelined, err := s.timelineStatus(
ctx, ctx,
s.State.Timelines.List.IngestOne, 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, follow.Account,
status, status,
stream.TimelineList+":"+listEntry.ListID, // key streamType to this specific list stream.TimelineList+":"+list.ID, // key streamType to this specific list
filters, filters,
mutes, mutes,
) )
if err != nil { if err != nil {
errs.Appendf("error adding status to timeline for list %s: %w", listEntry.ListID, err) log.Errorf(ctx, "error adding status to list timeline: %v", err)
// implicit continue continue
} }
listTimelined = listTimelined || timelined
// Update flag based on if timelined.
timelined = timelined || listTimelined
} }
return exclusive, listTimelined return timelined, exclusive, nil
}
// 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
} }
// getFiltersAndMutes returns an account's filters and mutes. // getFiltersAndMutes returns an account's filters and mutes.
@ -341,8 +296,8 @@ func (s *Surface) getFiltersAndMutes(ctx context.Context, accountID string) ([]*
if err != nil { if err != nil {
return nil, nil, gtserror.Newf("couldn't retrieve mutes for account %s: %w", accountID, err) 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 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. // belongs to, based on the replies policy of the list.
func (s *Surface) listEligible( func (s *Surface) listEligible(
ctx context.Context, ctx context.Context,
listEntry *gtsmodel.ListEntry, list *gtsmodel.List,
status *gtsmodel.Status, status *gtsmodel.Status,
) (bool, error) { ) (bool, error) {
if status.InReplyToURI == "" { if status.InReplyToURI == "" {
@ -366,18 +321,6 @@ func (s *Surface) listEligible(
return false, nil 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 { switch list.RepliesPolicy {
case gtsmodel.RepliesPolicyNone: case gtsmodel.RepliesPolicyNone:
// This list should not show // This list should not show
@ -390,20 +333,15 @@ func (s *Surface) listEligible(
// //
// Check if replied-to account is // Check if replied-to account is
// also included in this list. // also included in this list.
includes, err := s.State.DB.ListIncludesAccount( in, err := s.State.DB.IsAccountInListID(ctx,
ctx,
list.ID, list.ID,
status.InReplyToAccountID, status.InReplyToAccountID,
) )
if err != nil { if err != nil {
err := gtserror.Newf( err := gtserror.Newf("db error checking if account in list: %w", err)
"db error checking if account %s in list %s: %w",
status.InReplyToAccountID, listEntry.ListID, err,
)
return false, err return false, err
} }
return in, nil
return includes, nil
case gtsmodel.RepliesPolicyFollowed: case gtsmodel.RepliesPolicyFollowed:
// This list should show replies // This list should show replies
@ -418,22 +356,13 @@ func (s *Surface) listEligible(
status.InReplyToAccountID, status.InReplyToAccountID,
) )
if err != nil { if err != nil {
err := gtserror.Newf( err := gtserror.Newf("db error checking if account followed: %w", err)
"db error checking if account %s is followed by %s: %w",
status.InReplyToAccountID, list.AccountID, err,
)
return false, err return false, err
} }
return follows, nil return follows, nil
default: default:
// HUH?? panic("unknown reply policy: " + list.RepliesPolicy)
err := gtserror.Newf(
"reply policy '%s' not recognized on list %s",
list.RepliesPolicy, list.ID,
)
return false, err
} }
} }
@ -452,6 +381,7 @@ func (s *Surface) timelineStatus(
filters []*gtsmodel.Filter, filters []*gtsmodel.Filter,
mutes *usermute.CompiledUserMuteList, mutes *usermute.CompiledUserMuteList,
) (bool, error) { ) (bool, error) {
// Ingest status into given timeline using provided function. // Ingest status into given timeline using provided function.
if inserted, err := ingest(ctx, timelineID, status); err != nil { if inserted, err := ingest(ctx, timelineID, status); err != nil {
err = gtserror.Newf("error ingesting status %s: %w", status.ID, err) err = gtserror.Newf("error ingesting status %s: %w", status.ID, err)
@ -461,7 +391,7 @@ func (s *Surface) timelineStatus(
return false, nil 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, apiStatus, err := s.Converter.StatusToAPIStatus(ctx,
status, status,
account, account,
@ -473,6 +403,8 @@ func (s *Surface) timelineStatus(
err = gtserror.Newf("error converting status %s to frontend representation: %w", status.ID, err) err = gtserror.Newf("error converting status %s to frontend representation: %w", status.ID, err)
return true, err return true, err
} }
// The status was inserted so stream it to the user.
s.Stream.Update(ctx, account, apiStatus, streamType) s.Stream.Update(ctx, account, apiStatus, streamType)
return true, nil return true, nil
@ -492,7 +424,8 @@ func (s *Surface) timelineAndNotifyStatusForTagFollowers(
} }
if status.BoostOf != nil { if status.BoostOf != nil {
// Unwrap boost and work with the original status. // Unwrap boost and work
// with the original status.
status = status.BoostOf status = status.BoostOf
} }
@ -523,6 +456,7 @@ func (s *Surface) timelineAndNotifyStatusForTagFollowers(
) )
} }
} }
return errs.Combine() return errs.Combine()
} }
@ -667,17 +601,15 @@ func (s *Surface) timelineStatusUpdate(ctx context.Context, status *gtsmodel.Sta
follows = append(follows, &gtsmodel.Follow{ follows = append(follows, &gtsmodel.Follow{
AccountID: status.AccountID, AccountID: status.AccountID,
Account: status.Account, Account: status.Account,
Notify: func() *bool { b := false; return &b }(), // Account shouldn't notify itself. Notify: util.Ptr(false), // Account shouldn't notify itself.
ShowReblogs: func() *bool { b := true; return &b }(), // Account should show own reblogs. ShowReblogs: util.Ptr(true), // Account should show own reblogs.
}) })
} }
// Push to streams for each local follower of this account. // Push updated status to streams for each local follower of this account.
homeTimelinedAccountIDs, err := s.timelineStatusUpdateForFollowers(ctx, status, follows) homeTimelinedAccountIDs := 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 tags in status, if applicable.
if err := s.timelineStatusUpdateForTagFollowers(ctx, status, homeTimelinedAccountIDs); err != nil { if err := s.timelineStatusUpdateForTagFollowers(ctx, status, homeTimelinedAccountIDs); err != nil {
return gtserror.Newf("error timelining status %s for tag followers: %w", status.ID, err) return gtserror.Newf("error timelining status %s for tag followers: %w", status.ID, err)
} }
@ -695,12 +627,7 @@ func (s *Surface) timelineStatusUpdateForFollowers(
ctx context.Context, ctx context.Context,
status *gtsmodel.Status, status *gtsmodel.Status,
follows []*gtsmodel.Follow, follows []*gtsmodel.Follow,
) ([]string, error) { ) (homeTimelinedAccountIDs []string) {
var (
errs gtserror.MultiError
homeTimelinedAccountIDs = []string{}
)
for _, follow := range follows { for _, follow := range follows {
// Check to see if the status is timelineable for this follower, // Check to see if the status is timelineable for this follower,
// taking account of its visibility, who it replies to, and, if // taking account of its visibility, who it replies to, and, if
@ -718,7 +645,7 @@ func (s *Surface) timelineStatusUpdateForFollowers(
ctx, follow.Account, status, ctx, follow.Account, status,
) )
if err != nil { 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 continue
} }
@ -727,31 +654,36 @@ func (s *Surface) timelineStatusUpdateForFollowers(
continue 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) filters, mutes, err := s.getFiltersAndMutes(ctx, follow.AccountID)
if err != nil { if err != nil {
errs.Append(err) log.Error(ctx, err)
continue continue
} }
// Add status to any relevant lists // Add status to relevant lists for this follow, if applicable.
// for this follow, if applicable. _, exclusive, err := s.listTimelineStatusUpdateForFollow(ctx,
exclusive := s.listTimelineStatusUpdateForFollow(
ctx,
status, status,
follow, follow,
&errs,
filters, filters,
mutes, 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 { if exclusive {
continue continue
} }
// Add status to home timeline for owner // Add status to home timeline for owner of
// of this follow, if applicable. // this follow (origin account), if applicable.
homeTimelined, err := s.timelineStreamStatusUpdate( homeTimelined, err := s.timelineStreamStatusUpdate(ctx,
ctx,
follow.Account, follow.Account,
status, status,
stream.TimelineHome, stream.TimelineHome,
@ -759,15 +691,17 @@ func (s *Surface) timelineStatusUpdateForFollowers(
mutes, mutes,
) )
if err != nil { if err != nil {
errs.Appendf("error home timelining status: %w", err) log.Errorf(ctx, "error home timelining status: %v", err)
continue continue
} }
if homeTimelined { if homeTimelined {
// If hometimelined, add to list of returned account IDs.
homeTimelinedAccountIDs = append(homeTimelinedAccountIDs, follow.AccountID) homeTimelinedAccountIDs = append(homeTimelinedAccountIDs, follow.AccountID)
} }
} }
return homeTimelinedAccountIDs, errs.Combine() return homeTimelinedAccountIDs
} }
// listTimelineStatusUpdateForFollow pushes edits of the given status // listTimelineStatusUpdateForFollow pushes edits of the given status
@ -779,58 +713,59 @@ func (s *Surface) listTimelineStatusUpdateForFollow(
ctx context.Context, ctx context.Context,
status *gtsmodel.Status, status *gtsmodel.Status,
follow *gtsmodel.Follow, follow *gtsmodel.Follow,
errs *gtserror.MultiError,
filters []*gtsmodel.Filter, filters []*gtsmodel.Filter,
mutes *usermute.CompiledUserMuteList, mutes *usermute.CompiledUserMuteList,
) bool { ) (bool, bool, error) {
// 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.
listEntries, err := s.getListEntries(ctx, follow) // Get all lists that contain this follow.
lists, err := s.State.DB.GetListsWithFollowID(
// We don't need list sub-models.
gtscontext.SetBarebones(ctx),
follow.ID,
)
if err != nil { if err != nil {
errs.Append(err) return false, false, gtserror.Newf("error getting lists for follow: %w", err)
return false
}
exclusive, err := s.isAnyListExclusive(ctx, listEntries)
if err != nil {
errs.Append(err)
return false
} }
// Check eligibility for each list entry (if any). var exclusive, timelined bool
for _, listEntry := range listEntries { for _, list := range lists {
eligible, err := s.listEligible(ctx, listEntry, status)
// Update exclusive flag if list is so.
exclusive = exclusive || *list.Exclusive
// Check whether list is eligible for this status.
eligible, err := s.listEligible(ctx, list, status)
if err != nil { if err != nil {
errs.Appendf("error checking list eligibility: %w", err) log.Errorf(ctx, "error checking list eligibility: %v", err)
continue continue
} }
if !eligible { if !eligible {
// Don't add this.
continue continue
} }
// At this point we are certain this status // At this point we are certain this status
// should be included in the timeline of the // should be included in the timeline of the
// list that this list entry belongs to. // list that this list entry belongs to.
if _, err := s.timelineStreamStatusUpdate( listTimelined, err := s.timelineStreamStatusUpdate(
ctx, ctx,
follow.Account, follow.Account,
status, status,
stream.TimelineList+":"+listEntry.ListID, // key streamType to this specific list stream.TimelineList+":"+list.ID, // key streamType to this specific list
filters, filters,
mutes, mutes,
); err != nil { )
errs.Appendf("error adding status to timeline for list %s: %w", listEntry.ListID, err) if err != nil {
// implicit continue 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 // timelineStatusUpdate streams the edited status to the user using the
@ -845,16 +780,31 @@ func (s *Surface) timelineStreamStatusUpdate(
filters []*gtsmodel.Filter, filters []*gtsmodel.Filter,
mutes *usermute.CompiledUserMuteList, mutes *usermute.CompiledUserMuteList,
) (bool, error) { ) (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. // Don't put this status in the stream.
return false, nil 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) // The status was updated so stream it to the user.
return false, err
}
s.Stream.StatusUpdate(ctx, account, apiStatus, streamType) s.Stream.StatusUpdate(ctx, account, apiStatus, streamType)
return true, nil return true, nil
} }

View file

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

View file

@ -17,12 +17,23 @@
package util 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, // Set represents a hashmap of only keys,
// useful for deduplication / key checking. // useful for deduplication / key checking.
type Set[T comparable] map[T]struct{} type Set[T comparable] map[T]struct{}
// ToSet creates a Set[T] from given values, // ToSet creates a Set[T] from given values.
// noting that this does not maintain any order.
func ToSet[T comparable](in []T) Set[T] { func ToSet[T comparable](in []T) Set[T] {
set := make(Set[T], len(in)) set := make(Set[T], len(in))
for _, v := range in { for _, v := range in {
@ -31,8 +42,7 @@ func ToSet[T comparable](in []T) Set[T] {
return set return set
} }
// FromSet extracts the values from set to slice, // FromSet extracts the values from set to slice.
// noting that this does not maintain any order.
func FromSet[T comparable](in Set[T]) []T { func FromSet[T comparable](in Set[T]) []T {
out := make([]T, len(in)) out := make([]T, len(in))
var i int var i int