mirror of
https://github.com/superseriousbusiness/gotosocial.git
synced 2024-11-22 03:36:39 +00:00
[performance] cache more database calls, reduce required database calls overall (#3290)
* improvements to caching for lists and relationship to accounts / follows * fix nil panic in AddToList() * ensure list related caches are correctly invalidated * ensure returned ID lists are ordered correctly * bump go-structr to v0.8.9 (returns early if zero uncached keys to be loaded) * remove zero checks in uncached key load functions (go-structr now handles this) * fix issues after rebase on upstream/main * update the expected return order of CSV exports (since list entries are now down by entry creation date) * rename some funcs, allow deleting list entries for multiple follow IDs at a time, fix up more tests * use returning statements on delete to get cache invalidation info * fixes to recent database delete changes * fix broken list entries delete sql * remove unused db function * update remainder of delete functions to behave in similar way, some other small tweaks * fix delete user sql, allow returning on err no entries * uncomment + fix list database tests * update remaining list tests * update envparsing test * add comments to each specific key being invalidated * add more cache invalidation explanatory comments * whoops; actually delete poll votes from database in the DeletePollByID() func * remove added but-commented-out field * improved comment regarding paging being disabled * make cache invalidation comments match what's actually happening * fix up delete query comments to match what is happening * rename function to read a bit better * don't use ErrNoEntries on delete when not needed (it's only needed for a RETURNING call) * update function name in test * move list exclusivity check to AFTER eligibility check. use log.Panic() instead of panic() * use the poll_id column in poll_votes for selecting votes in poll ID * fix function name
This commit is contained in:
parent
0567b319c6
commit
84279f6a6a
2
go.mod
2
go.mod
|
@ -23,7 +23,7 @@ require (
|
||||||
codeberg.org/gruf/go-runners v1.6.2
|
codeberg.org/gruf/go-runners v1.6.2
|
||||||
codeberg.org/gruf/go-sched v1.2.3
|
codeberg.org/gruf/go-sched v1.2.3
|
||||||
codeberg.org/gruf/go-storage v0.1.2
|
codeberg.org/gruf/go-storage v0.1.2
|
||||||
codeberg.org/gruf/go-structr v0.8.8
|
codeberg.org/gruf/go-structr v0.8.9
|
||||||
codeberg.org/superseriousbusiness/exif-terminator v0.9.0
|
codeberg.org/superseriousbusiness/exif-terminator v0.9.0
|
||||||
github.com/DmitriyVTitov/size v1.5.0
|
github.com/DmitriyVTitov/size v1.5.0
|
||||||
github.com/KimMachineGun/automemlimit v0.6.1
|
github.com/KimMachineGun/automemlimit v0.6.1
|
||||||
|
|
4
go.sum
4
go.sum
|
@ -80,8 +80,8 @@ codeberg.org/gruf/go-sched v1.2.3 h1:H5ViDxxzOBR3uIyGBCf0eH8b1L8wMybOXcdtUUTXZHk
|
||||||
codeberg.org/gruf/go-sched v1.2.3/go.mod h1:vT9uB6KWFIIwnG9vcPY2a0alYNoqdL1mSzRM8I+PK7A=
|
codeberg.org/gruf/go-sched v1.2.3/go.mod h1:vT9uB6KWFIIwnG9vcPY2a0alYNoqdL1mSzRM8I+PK7A=
|
||||||
codeberg.org/gruf/go-storage v0.1.2 h1:dIOVOKq1CJpRmuhbB8Zok3mmo8V6VV/nX5GLIm6hywA=
|
codeberg.org/gruf/go-storage v0.1.2 h1:dIOVOKq1CJpRmuhbB8Zok3mmo8V6VV/nX5GLIm6hywA=
|
||||||
codeberg.org/gruf/go-storage v0.1.2/go.mod h1:LRDpFHqRJi0f+35c3ltBH2e/pGfwY5dGlNlgCJ/R1DA=
|
codeberg.org/gruf/go-storage v0.1.2/go.mod h1:LRDpFHqRJi0f+35c3ltBH2e/pGfwY5dGlNlgCJ/R1DA=
|
||||||
codeberg.org/gruf/go-structr v0.8.8 h1:lRPpyTmLKvQCkkQiSUbOAh6jtL2wncEO8DwksMqQXM8=
|
codeberg.org/gruf/go-structr v0.8.9 h1:OyiSspWYCeJOm356fFPd+bDRumPrard2VAUXAPqZiJ0=
|
||||||
codeberg.org/gruf/go-structr v0.8.8/go.mod h1:zkoXVrAnKosh8VFAsbP/Hhs8FmLBjbVVy5w/Ngm8ApM=
|
codeberg.org/gruf/go-structr v0.8.9/go.mod h1:zkoXVrAnKosh8VFAsbP/Hhs8FmLBjbVVy5w/Ngm8ApM=
|
||||||
codeberg.org/superseriousbusiness/exif-terminator v0.9.0 h1:/EfyGI6HIrbkhFwgXGSjZ9o1kr/+k8v4mKdfXTH02Go=
|
codeberg.org/superseriousbusiness/exif-terminator v0.9.0 h1:/EfyGI6HIrbkhFwgXGSjZ9o1kr/+k8v4mKdfXTH02Go=
|
||||||
codeberg.org/superseriousbusiness/exif-terminator v0.9.0/go.mod h1:gCWKduudUWFzsnixoMzu0FYVdxHWG+AbXnZ50DqxsUE=
|
codeberg.org/superseriousbusiness/exif-terminator v0.9.0/go.mod h1:gCWKduudUWFzsnixoMzu0FYVdxHWG+AbXnZ50DqxsUE=
|
||||||
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
|
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
|
||||||
|
|
|
@ -188,8 +188,8 @@ type testCase struct {
|
||||||
token: suite.testTokens["local_account_1"],
|
token: suite.testTokens["local_account_1"],
|
||||||
user: suite.testUsers["local_account_1"],
|
user: suite.testUsers["local_account_1"],
|
||||||
account: suite.testAccounts["local_account_1"],
|
account: suite.testAccounts["local_account_1"],
|
||||||
expect: `Cool Ass Posters From This Instance,admin@localhost:8080
|
expect: `Cool Ass Posters From This Instance,1happyturtle@localhost:8080
|
||||||
Cool Ass Posters From This Instance,1happyturtle@localhost:8080
|
Cool Ass Posters From This Instance,admin@localhost:8080
|
||||||
`,
|
`,
|
||||||
},
|
},
|
||||||
// Export Mutes.
|
// Export Mutes.
|
||||||
|
|
|
@ -25,6 +25,7 @@
|
||||||
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)
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
limit, errWithCode := apiutil.ParseLimit(c.Query(apiutil.LimitKey), 40, 80, 0)
|
|
||||||
if errWithCode != nil {
|
|
||||||
apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1)
|
apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
page, errWithCode := paging.ParseIDPage(c,
|
||||||
ctx = c.Request.Context()
|
1, // min limit
|
||||||
|
80, // max limit
|
||||||
|
0, // default = paging disabled
|
||||||
)
|
)
|
||||||
|
|
||||||
if limit == 0 {
|
|
||||||
// Return all accounts in the list without pagination.
|
|
||||||
accounts, errWithCode := m.processor.List().GetAllListAccounts(ctx, authed.Account, targetListID)
|
|
||||||
if errWithCode != nil {
|
if errWithCode != nil {
|
||||||
apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1)
|
apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1)
|
||||||
return
|
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)
|
||||||
|
|
|
@ -19,7 +19,7 @@
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io/ioutil"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
@ -97,7 +97,7 @@ func (suite *ListAccountsTestSuite) getListAccounts(
|
||||||
result := recorder.Result()
|
result := recorder.Result()
|
||||||
defer result.Body.Close()
|
defer result.Body.Close()
|
||||||
|
|
||||||
b, err := ioutil.ReadAll(result.Body)
|
b, err := io.ReadAll(result.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, "", err
|
return nil, "", err
|
||||||
}
|
}
|
||||||
|
@ -151,8 +151,7 @@ func (suite *ListAccountsTestSuite) TestGetListAccountsPaginatedDefaultLimit() {
|
||||||
|
|
||||||
suite.Len(accounts, 2)
|
suite.Len(accounts, 2)
|
||||||
suite.Equal(
|
suite.Equal(
|
||||||
`<http://localhost:8080/api/v1/lists/01H0G8E4Q2J3FE3JDWJVWEDCD1/accounts?limit=40&max_id=01H0G89MWVQE0M58VD2HQYMQWH>; rel="next", `+
|
"<http://localhost:8080/api/v1/lists/01H0G8E4Q2J3FE3JDWJVWEDCD1/accounts?limit=40&max_id=01F8MH5NBDF2MV7CTC4Q5128HF>; rel=\"next\", <http://localhost:8080/api/v1/lists/01H0G8E4Q2J3FE3JDWJVWEDCD1/accounts?limit=40&min_id=01F8MH17FWEB39HZJ76B6VXSKF>; rel=\"prev\"",
|
||||||
`<http://localhost:8080/api/v1/lists/01H0G8E4Q2J3FE3JDWJVWEDCD1/accounts?limit=40&min_id=01H0G8FFM1AGQDRNGBGGX8CYJQ>; rel="prev"`,
|
|
||||||
link,
|
link,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -184,8 +183,7 @@ func (suite *ListAccountsTestSuite) TestGetListAccountsPaginatedNextPage() {
|
||||||
|
|
||||||
suite.Len(accounts, 1)
|
suite.Len(accounts, 1)
|
||||||
suite.Equal(
|
suite.Equal(
|
||||||
`<http://localhost:8080/api/v1/lists/01H0G8E4Q2J3FE3JDWJVWEDCD1/accounts?limit=1&max_id=01H0G8FFM1AGQDRNGBGGX8CYJQ>; rel="next", `+
|
"<http://localhost:8080/api/v1/lists/01H0G8E4Q2J3FE3JDWJVWEDCD1/accounts?limit=1&max_id=01F8MH17FWEB39HZJ76B6VXSKF>; rel=\"next\", <http://localhost:8080/api/v1/lists/01H0G8E4Q2J3FE3JDWJVWEDCD1/accounts?limit=1&min_id=01F8MH17FWEB39HZJ76B6VXSKF>; rel=\"prev\"",
|
||||||
`<http://localhost:8080/api/v1/lists/01H0G8E4Q2J3FE3JDWJVWEDCD1/accounts?limit=1&min_id=01H0G8FFM1AGQDRNGBGGX8CYJQ>; rel="prev"`,
|
|
||||||
link,
|
link,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -206,8 +204,7 @@ func (suite *ListAccountsTestSuite) TestGetListAccountsPaginatedNextPage() {
|
||||||
|
|
||||||
suite.Len(accounts, 1)
|
suite.Len(accounts, 1)
|
||||||
suite.Equal(
|
suite.Equal(
|
||||||
`<http://localhost:8080/api/v1/lists/01H0G8E4Q2J3FE3JDWJVWEDCD1/accounts?limit=1&max_id=01H0G89MWVQE0M58VD2HQYMQWH>; rel="next", `+
|
"<http://localhost:8080/api/v1/lists/01H0G8E4Q2J3FE3JDWJVWEDCD1/accounts?limit=1&max_id=01F8MH17FWEB39HZJ76B6VXSKF>; rel=\"next\", <http://localhost:8080/api/v1/lists/01H0G8E4Q2J3FE3JDWJVWEDCD1/accounts?limit=1&min_id=01F8MH17FWEB39HZJ76B6VXSKF>; rel=\"prev\"",
|
||||||
`<http://localhost:8080/api/v1/lists/01H0G8E4Q2J3FE3JDWJVWEDCD1/accounts?limit=1&min_id=01H0G89MWVQE0M58VD2HQYMQWH>; rel="prev"`,
|
|
||||||
link,
|
link,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -98,14 +98,17 @@ func (suite *ListAccountsAddTestSuite) TestPostListAccountNotFollowed() {
|
||||||
|
|
||||||
resp, err := suite.postListAccounts(http.StatusNotFound, listID, accountIDs)
|
resp, err := suite.postListAccounts(http.StatusNotFound, listID, accountIDs)
|
||||||
suite.NoError(err)
|
suite.NoError(err)
|
||||||
suite.Equal(`{"error":"Not Found: you do not follow account 01F8MH5ZK5VRH73AKHQM6Y9VNX"}`, string(resp))
|
suite.Equal(`{"error":"Not Found: account 01F8MH5ZK5VRH73AKHQM6Y9VNX not currently followed"}`, string(resp))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (suite *ListAccountsAddTestSuite) TestPostListAccountOK() {
|
func (suite *ListAccountsAddTestSuite) TestPostListAccountOK() {
|
||||||
|
entry := suite.testListEntries["local_account_1_list_1_entry_1"]
|
||||||
|
|
||||||
// Remove turtle from the list.
|
// Remove turtle from the list.
|
||||||
if err := suite.db.DeleteListEntry(
|
if err := suite.db.DeleteListEntry(
|
||||||
context.Background(),
|
context.Background(),
|
||||||
suite.testListEntries["local_account_1_list_1_entry_1"].ID,
|
entry.ListID,
|
||||||
|
entry.FollowID,
|
||||||
); err != nil {
|
); err != nil {
|
||||||
suite.FailNow(err.Error())
|
suite.FailNow(err.Error())
|
||||||
}
|
}
|
||||||
|
|
12
internal/cache/cache.go
vendored
12
internal/cache/cache.go
vendored
|
@ -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)
|
||||||
|
|
153
internal/cache/db.go
vendored
153
internal/cache/db.go
vendored
|
@ -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]
|
||||||
|
|
||||||
|
@ -88,10 +85,23 @@ type DBCaches struct {
|
||||||
|
|
||||||
// FollowIDs provides access to the follower / following IDs database cache.
|
// FollowIDs provides access to the follower / following IDs database cache.
|
||||||
// THIS CACHE IS KEYED AS THE FOLLOWING {prefix}{accountID} WHERE PREFIX IS:
|
// THIS CACHE IS KEYED AS THE FOLLOWING {prefix}{accountID} WHERE PREFIX IS:
|
||||||
// - '>' for following IDs
|
//
|
||||||
// - 'l>' for local following IDs
|
// - '>{$accountID}' for following IDs
|
||||||
// - '<' for follower IDs
|
// e.g. FollowIDs.Load(">" + account.ID, func() {})
|
||||||
// - 'l<' for local follower IDs
|
// which will load a slice of follows IDs FROM account.
|
||||||
|
//
|
||||||
|
// - 'l>{$accountID}' for local following IDs
|
||||||
|
// e.g. FollowIDs.Load("l>" + account.ID, func() {})
|
||||||
|
// which will load a slice of LOCAL follows IDs FROM account.
|
||||||
|
//
|
||||||
|
// - '<{$accountID}' for follower IDs
|
||||||
|
// e.g. FollowIDs.Load("<" + account.ID, func() {})
|
||||||
|
// which will load a slice of follows IDs TARGETTING account.
|
||||||
|
//
|
||||||
|
// - 'l<{$accountID}' for local follower IDs
|
||||||
|
// e.g. FollowIDs.Load("l<" + account.ID, func() {})
|
||||||
|
// which will load a slice of LOCAL follows IDs TARGETTING account.
|
||||||
|
//
|
||||||
FollowIDs SliceCache[string]
|
FollowIDs SliceCache[string]
|
||||||
|
|
||||||
// FollowRequest provides access to the gtsmodel FollowRequest database cache.
|
// FollowRequest provides access to the gtsmodel FollowRequest database cache.
|
||||||
|
@ -99,10 +109,30 @@ type DBCaches struct {
|
||||||
|
|
||||||
// FollowRequestIDs provides access to the follow requester / requesting IDs database
|
// FollowRequestIDs provides access to the follow requester / requesting IDs database
|
||||||
// cache. THIS CACHE IS KEYED AS THE FOLLOWING {prefix}{accountID} WHERE PREFIX IS:
|
// cache. THIS CACHE IS KEYED AS THE FOLLOWING {prefix}{accountID} WHERE PREFIX IS:
|
||||||
// - '>' for following IDs
|
//
|
||||||
// - '<' for follower IDs
|
// - '>{$accountID}' for follow request IDs
|
||||||
|
// e.g. FollowRequestIDs.Load(">" + account.ID, func() {})
|
||||||
|
// which will load a slice of follow request IDs TARGETTING account.
|
||||||
|
//
|
||||||
|
// - '<{$accountID}' for follow request IDs
|
||||||
|
// e.g. FollowRequestIDs.Load("<" + account.ID, func() {})
|
||||||
|
// which will load a slice of follow request IDs FROM account.
|
||||||
|
//
|
||||||
FollowRequestIDs SliceCache[string]
|
FollowRequestIDs SliceCache[string]
|
||||||
|
|
||||||
|
// FollowingTagIDs provides access to account IDs following / tag IDs followed by
|
||||||
|
// account db cache. THIS CACHE IS KEYED AS THE FOLLOWING {prefix}{id} WHERE:
|
||||||
|
//
|
||||||
|
// - '>{$accountID}' for tag IDs followed by account
|
||||||
|
// e.g. FollowingTagIDs.Load(">" + account.ID, func() {})
|
||||||
|
// which will load a slice of tag IDs followed by account.
|
||||||
|
//
|
||||||
|
// - '<{$tagIDs}' for account IDs following tag
|
||||||
|
// e.g. FollowingTagIDs.Load("<" + tag.ID, func() {})
|
||||||
|
// which will load a slice of account IDs following tag.
|
||||||
|
//
|
||||||
|
FollowingTagIDs SliceCache[string]
|
||||||
|
|
||||||
// Instance provides access to the gtsmodel Instance database cache.
|
// Instance provides access to the gtsmodel Instance database cache.
|
||||||
Instance StructCache[*gtsmodel.Instance]
|
Instance StructCache[*gtsmodel.Instance]
|
||||||
|
|
||||||
|
@ -115,8 +145,31 @@ 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
|
||||||
|
// e.g. ListIDs.Load("a" + account.ID, func() {})
|
||||||
|
// which will load a slice of list IDs owned by account.
|
||||||
|
//
|
||||||
|
// - 'f{$followID}' for list IDs follow contained in
|
||||||
|
// e.g. ListIDs.Load("f" + follow.ID, func() {})
|
||||||
|
// which will load a slice of list IDs containing follow.
|
||||||
|
//
|
||||||
|
ListIDs SliceCache[string]
|
||||||
|
|
||||||
|
// ListedIDs provides access to the account IDs in list / follow IDs in
|
||||||
|
// list db cache. THIS CACHE IS KEYED AS FOLLOWING {prefix}{id} WHERE:
|
||||||
|
//
|
||||||
|
// - 'a{listID}' for account IDs in list ID
|
||||||
|
// e.g. ListedIDs.Load("a" + list.ID, func() {})
|
||||||
|
// which will load a slice of account IDs in list.
|
||||||
|
//
|
||||||
|
// - 'f{listID}' for follow IDs in list ID
|
||||||
|
// e.g. ListedIDs.Load("f" + list.ID, func() {})
|
||||||
|
// which will load a slice of follow IDs in list.
|
||||||
|
//
|
||||||
|
ListedIDs SliceCache[string]
|
||||||
|
|
||||||
// Marker provides access to the gtsmodel Marker database cache.
|
// Marker provides access to the gtsmodel Marker database cache.
|
||||||
Marker StructCache[*gtsmodel.Marker]
|
Marker StructCache[*gtsmodel.Marker]
|
||||||
|
@ -151,10 +204,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 +219,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 +293,6 @@ func (c *Caches) initAccount() {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Caches) initAccountIDsFollowingTag() {
|
|
||||||
// Calculate maximum cache size.
|
|
||||||
cap := calculateSliceCacheMax(
|
|
||||||
config.GetCacheAccountIDsFollowingTagMemRatio(),
|
|
||||||
)
|
|
||||||
|
|
||||||
log.Infof(nil, "cache size = %d", cap)
|
|
||||||
|
|
||||||
c.DB.AccountIDsFollowingTag.Init(0, cap)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Caches) initAccountNote() {
|
func (c *Caches) initAccountNote() {
|
||||||
// Calculate maximum cache size.
|
// Calculate maximum cache size.
|
||||||
cap := calculateResultCacheMax(
|
cap := calculateResultCacheMax(
|
||||||
|
@ -761,6 +800,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 +910,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 +925,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
|
|
||||||
// will be populated separately.
|
|
||||||
// See internal/db/bundb/list.go.
|
|
||||||
l2.Follow = nil
|
|
||||||
|
|
||||||
return l2
|
|
||||||
}
|
}
|
||||||
|
|
||||||
c.DB.ListEntry.Init(structr.CacheConfig[*gtsmodel.ListEntry]{
|
func (c *Caches) initListedIDs() {
|
||||||
Indices: []structr.IndexConfig{
|
// Calculate maximum cache size.
|
||||||
{Fields: "ID"},
|
cap := calculateSliceCacheMax(
|
||||||
{Fields: "ListID", Multiple: true},
|
config.GetCacheListedIDsMemRatio(),
|
||||||
{Fields: "FollowID", Multiple: true},
|
)
|
||||||
},
|
|
||||||
MaxSize: cap,
|
log.Infof(nil, "cache size = %d", cap)
|
||||||
IgnoreErr: ignoreErrors,
|
|
||||||
Copy: copyF,
|
c.DB.ListedIDs.Init(0, cap)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Caches) initMarker() {
|
func (c *Caches) initMarker() {
|
||||||
|
@ -1368,17 +1406,6 @@ func (c *Caches) initTag() {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Caches) initTagIDsFollowedByAccount() {
|
|
||||||
// Calculate maximum cache size.
|
|
||||||
cap := calculateSliceCacheMax(
|
|
||||||
config.GetCacheTagIDsFollowedByAccountMemRatio(),
|
|
||||||
)
|
|
||||||
|
|
||||||
log.Infof(nil, "cache size = %d", cap)
|
|
||||||
|
|
||||||
c.DB.TagIDsFollowedByAccount.Init(0, cap)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Caches) initThreadMute() {
|
func (c *Caches) initThreadMute() {
|
||||||
cap := calculateResultCacheMax(
|
cap := calculateResultCacheMax(
|
||||||
sizeofThreadMute(), // model in-mem size.
|
sizeofThreadMute(), // model in-mem size.
|
||||||
|
|
82
internal/cache/invalidate.go
vendored
82
internal/cache/invalidate.go
vendored
|
@ -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)
|
||||||
|
@ -108,18 +105,47 @@ func (c *Caches) OnInvalidateFollow(follow *gtsmodel.Follow) {
|
||||||
c.Visibility.Invalidate("ItemID", follow.TargetAccountID)
|
c.Visibility.Invalidate("ItemID", follow.TargetAccountID)
|
||||||
c.Visibility.Invalidate("RequesterID", follow.TargetAccountID)
|
c.Visibility.Invalidate("RequesterID", follow.TargetAccountID)
|
||||||
|
|
||||||
// Invalidate source account's following
|
// Invalidate ID slice cache.
|
||||||
// lists, and destination's follwer lists.
|
|
||||||
// (see FollowIDs() comment for details).
|
|
||||||
c.DB.FollowIDs.Invalidate(
|
c.DB.FollowIDs.Invalidate(
|
||||||
|
|
||||||
|
// Invalidate follow ID lists
|
||||||
|
// TARGETTING origin account
|
||||||
|
// (including local-only follows).
|
||||||
">"+follow.AccountID,
|
">"+follow.AccountID,
|
||||||
"l>"+follow.AccountID,
|
"l>"+follow.AccountID,
|
||||||
|
|
||||||
|
// Invalidate follow ID lists
|
||||||
|
// FROM the origin account
|
||||||
|
// (including local-only follows).
|
||||||
"<"+follow.AccountID,
|
"<"+follow.AccountID,
|
||||||
"l<"+follow.AccountID,
|
"l<"+follow.AccountID,
|
||||||
"<"+follow.TargetAccountID,
|
|
||||||
"l<"+follow.TargetAccountID,
|
// Invalidate follow ID lists
|
||||||
|
// TARGETTING the target account
|
||||||
|
// (including local-only follows).
|
||||||
">"+follow.TargetAccountID,
|
">"+follow.TargetAccountID,
|
||||||
"l>"+follow.TargetAccountID,
|
"l>"+follow.TargetAccountID,
|
||||||
|
|
||||||
|
// Invalidate follow ID lists
|
||||||
|
// FROM the target account
|
||||||
|
// (including local-only follows).
|
||||||
|
"<"+follow.TargetAccountID,
|
||||||
|
"l<"+follow.TargetAccountID,
|
||||||
|
)
|
||||||
|
|
||||||
|
// Invalidate ID slice cache.
|
||||||
|
c.DB.ListIDs.Invalidate(
|
||||||
|
|
||||||
|
// Invalidate source
|
||||||
|
// account's owned lists.
|
||||||
|
"a"+follow.AccountID,
|
||||||
|
|
||||||
|
// Invalidate target account's.
|
||||||
|
"a"+follow.TargetAccountID,
|
||||||
|
|
||||||
|
// Invalidate lists containing
|
||||||
|
// list entries for follow.
|
||||||
|
"f"+follow.ID,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -127,20 +153,48 @@ func (c *Caches) OnInvalidateFollowRequest(followReq *gtsmodel.FollowRequest) {
|
||||||
// Invalidate follow with this same ID.
|
// Invalidate follow with this same ID.
|
||||||
c.DB.Follow.Invalidate("ID", followReq.ID)
|
c.DB.Follow.Invalidate("ID", followReq.ID)
|
||||||
|
|
||||||
// Invalidate source account's followreq
|
// Invalidate ID slice cache.
|
||||||
// lists, and destinations follow req lists.
|
|
||||||
// (see FollowRequestIDs() comment for details).
|
|
||||||
c.DB.FollowRequestIDs.Invalidate(
|
c.DB.FollowRequestIDs.Invalidate(
|
||||||
|
|
||||||
|
// Invalidate follow request ID
|
||||||
|
// lists TARGETTING origin account
|
||||||
|
// (including local-only follows).
|
||||||
">"+followReq.AccountID,
|
">"+followReq.AccountID,
|
||||||
|
|
||||||
|
// Invalidate follow request ID
|
||||||
|
// lists FROM the origin account
|
||||||
|
// (including local-only follows).
|
||||||
"<"+followReq.AccountID,
|
"<"+followReq.AccountID,
|
||||||
|
|
||||||
|
// Invalidate follow request ID
|
||||||
|
// lists TARGETTING target account
|
||||||
|
// (including local-only follows).
|
||||||
">"+followReq.TargetAccountID,
|
">"+followReq.TargetAccountID,
|
||||||
|
|
||||||
|
// Invalidate follow request ID
|
||||||
|
// lists FROM the target account
|
||||||
|
// (including local-only follows).
|
||||||
"<"+followReq.TargetAccountID,
|
"<"+followReq.TargetAccountID,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Caches) OnInvalidateList(list *gtsmodel.List) {
|
func (c *Caches) OnInvalidateList(list *gtsmodel.List) {
|
||||||
// Invalidate all cached entries of this list.
|
// Invalidate list IDs cache.
|
||||||
c.DB.ListEntry.Invalidate("ListID", list.ID)
|
c.DB.ListIDs.Invalidate(
|
||||||
|
"a" + list.AccountID,
|
||||||
|
)
|
||||||
|
|
||||||
|
// Invalidate ID slice cache.
|
||||||
|
c.DB.ListedIDs.Invalidate(
|
||||||
|
|
||||||
|
// Invalidate list of
|
||||||
|
// account IDs in list.
|
||||||
|
"a"+list.ID,
|
||||||
|
|
||||||
|
// Invalidate list of
|
||||||
|
// follow IDs in list.
|
||||||
|
"f"+list.ID,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Caches) OnInvalidateMedia(media *gtsmodel.MediaAttachment) {
|
func (c *Caches) OnInvalidateMedia(media *gtsmodel.MediaAttachment) {
|
||||||
|
@ -184,7 +238,7 @@ func (c *Caches) OnInvalidateStatus(status *gtsmodel.Status) {
|
||||||
// the media IDs in use before the media table is
|
// the media IDs in use before the media table is
|
||||||
// aware of the status ID they are linked to.
|
// aware of the status ID they are linked to.
|
||||||
//
|
//
|
||||||
// c.DB.Media().Invalidate("StatusID") will not work.
|
// c.DB.Media.Invalidate("StatusID") will not work.
|
||||||
c.DB.Media.InvalidateIDs("ID", status.AttachmentIDs)
|
c.DB.Media.InvalidateIDs("ID", status.AttachmentIDs)
|
||||||
|
|
||||||
if status.BoostOfID != "" {
|
if status.BoostOfID != "" {
|
||||||
|
|
21
internal/cache/size.go
vendored
21
internal/cache/size.go
vendored
|
@ -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(>smodel.ListEntry{
|
|
||||||
ID: exampleID,
|
|
||||||
CreatedAt: exampleTime,
|
|
||||||
UpdatedAt: exampleTime,
|
|
||||||
ListID: exampleID,
|
|
||||||
FollowID: exampleID,
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
|
|
||||||
func sizeofMarker() uintptr {
|
func sizeofMarker() uintptr {
|
||||||
return uintptr(size.Of(>smodel.Marker{
|
return uintptr(size.Of(>smodel.Marker{
|
||||||
AccountID: exampleID,
|
AccountID: exampleID,
|
||||||
|
|
|
@ -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"`
|
||||||
|
|
|
@ -159,7 +159,6 @@
|
||||||
// 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 @@
|
||||||
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 @@
|
||||||
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,
|
||||||
|
|
|
@ -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()
|
||||||
|
|
|
@ -123,9 +123,6 @@ type Account interface {
|
||||||
// In the case of no statuses, this function will return db.ErrNoEntries.
|
// In the case of no statuses, this function will return db.ErrNoEntries.
|
||||||
GetAccountWebStatuses(ctx context.Context, account *gtsmodel.Account, limit int, maxID string) ([]*gtsmodel.Status, error)
|
GetAccountWebStatuses(ctx context.Context, account *gtsmodel.Account, limit int, maxID string) ([]*gtsmodel.Status, error)
|
||||||
|
|
||||||
// SetAccountHeaderOrAvatar sets the header or avatar for the given accountID to the given media attachment.
|
|
||||||
SetAccountHeaderOrAvatar(ctx context.Context, mediaAttachment *gtsmodel.MediaAttachment, accountID string) error
|
|
||||||
|
|
||||||
// GetInstanceAccount returns the instance account for the given domain.
|
// GetInstanceAccount returns the instance account for the given domain.
|
||||||
// If domain is empty, this instance account will be returned.
|
// If domain is empty, this instance account will be returned.
|
||||||
GetInstanceAccount(ctx context.Context, domain string) (*gtsmodel.Account, error)
|
GetInstanceAccount(ctx context.Context, domain string) (*gtsmodel.Account, error)
|
||||||
|
|
|
@ -64,15 +64,8 @@ func (a *accountDB) GetAccountsByIDs(ctx context.Context, ids []string) ([]*gtsm
|
||||||
accounts, err := a.state.Caches.DB.Account.LoadIDs("ID",
|
accounts, err := a.state.Caches.DB.Account.LoadIDs("ID",
|
||||||
ids,
|
ids,
|
||||||
func(uncached []string) ([]*gtsmodel.Account, error) {
|
func(uncached []string) ([]*gtsmodel.Account, error) {
|
||||||
// Avoid querying
|
|
||||||
// if none uncached.
|
|
||||||
count := len(uncached)
|
|
||||||
if count == 0 {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Preallocate expected length of uncached accounts.
|
// Preallocate expected length of uncached accounts.
|
||||||
accounts := make([]*gtsmodel.Account, 0, count)
|
accounts := make([]*gtsmodel.Account, 0, len(uncached))
|
||||||
|
|
||||||
// Perform database query scanning
|
// Perform database query scanning
|
||||||
// the remaining (uncached) account IDs.
|
// the remaining (uncached) account IDs.
|
||||||
|
@ -796,20 +789,14 @@ func (a *accountDB) UpdateAccount(ctx context.Context, account *gtsmodel.Account
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *accountDB) DeleteAccount(ctx context.Context, id string) error {
|
func (a *accountDB) DeleteAccount(ctx context.Context, id string) error {
|
||||||
defer a.state.Caches.DB.Account.Invalidate("ID", id)
|
// Gather necessary fields from
|
||||||
|
// deleted for cache invaliation.
|
||||||
|
var deleted gtsmodel.Account
|
||||||
|
deleted.ID = id
|
||||||
|
|
||||||
// Load account into cache before attempting a delete,
|
// Delete account from database and any related links in a transaction.
|
||||||
// as we need it cached in order to trigger the invalidate
|
if err := a.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
|
||||||
// callback. This in turn invalidates others.
|
|
||||||
_, err := a.GetAccountByID(gtscontext.SetBarebones(ctx), id)
|
|
||||||
if err != nil && !errors.Is(err, db.ErrNoEntries) {
|
|
||||||
// NOTE: even if db.ErrNoEntries is returned, we
|
|
||||||
// still run the below transaction to ensure related
|
|
||||||
// objects are appropriately deleted.
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return a.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
|
|
||||||
// clear out any emoji links
|
// clear out any emoji links
|
||||||
if _, err := tx.
|
if _, err := tx.
|
||||||
NewDelete().
|
NewDelete().
|
||||||
|
@ -822,44 +809,19 @@ func (a *accountDB) DeleteAccount(ctx context.Context, id string) error {
|
||||||
// delete the account
|
// delete the account
|
||||||
_, err := tx.
|
_, err := tx.
|
||||||
NewDelete().
|
NewDelete().
|
||||||
TableExpr("? AS ?", bun.Ident("accounts"), bun.Ident("account")).
|
Model(&deleted).
|
||||||
Where("? = ?", bun.Ident("account.id"), id).
|
Where("? = ?", bun.Ident("id"), id).
|
||||||
|
Returning("?", bun.Ident("uri")).
|
||||||
Exec(ctx)
|
Exec(ctx)
|
||||||
return err
|
return err
|
||||||
})
|
}); err != nil {
|
||||||
}
|
|
||||||
|
|
||||||
func (a *accountDB) SetAccountHeaderOrAvatar(ctx context.Context, mediaAttachment *gtsmodel.MediaAttachment, accountID string) error {
|
|
||||||
if *mediaAttachment.Avatar && *mediaAttachment.Header {
|
|
||||||
return errors.New("one media attachment cannot be both header and avatar")
|
|
||||||
}
|
|
||||||
|
|
||||||
var column bun.Ident
|
|
||||||
switch {
|
|
||||||
case *mediaAttachment.Avatar:
|
|
||||||
column = bun.Ident("account.avatar_media_attachment_id")
|
|
||||||
case *mediaAttachment.Header:
|
|
||||||
column = bun.Ident("account.header_media_attachment_id")
|
|
||||||
default:
|
|
||||||
return errors.New("given media attachment was neither a header nor an avatar")
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: there are probably more side effects here that need to be handled
|
|
||||||
if _, err := a.db.
|
|
||||||
NewInsert().
|
|
||||||
Model(mediaAttachment).
|
|
||||||
Exec(ctx); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := a.db.
|
// Invalidate cached account by its ID, manually
|
||||||
NewUpdate().
|
// call invalidate hook in case not cached.
|
||||||
TableExpr("? AS ?", bun.Ident("accounts"), bun.Ident("account")).
|
a.state.Caches.DB.Account.Invalidate("ID", id)
|
||||||
Set("? = ?", column, mediaAttachment.ID).
|
a.state.Caches.OnInvalidateAccount(&deleted)
|
||||||
Where("? = ?", bun.Ident("account.id"), accountID).
|
|
||||||
Exec(ctx); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -147,15 +147,8 @@ func (a *applicationDB) GetAllTokens(ctx context.Context) ([]*gtsmodel.Token, er
|
||||||
tokens, err := a.state.Caches.DB.Token.LoadIDs("ID",
|
tokens, err := a.state.Caches.DB.Token.LoadIDs("ID",
|
||||||
tokenIDs,
|
tokenIDs,
|
||||||
func(uncached []string) ([]*gtsmodel.Token, error) {
|
func(uncached []string) ([]*gtsmodel.Token, error) {
|
||||||
// Avoid querying
|
|
||||||
// if none uncached.
|
|
||||||
count := len(uncached)
|
|
||||||
if count == 0 {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Preallocate expected length of uncached tokens.
|
// Preallocate expected length of uncached tokens.
|
||||||
tokens := make([]*gtsmodel.Token, 0, count)
|
tokens := make([]*gtsmodel.Token, 0, len(uncached))
|
||||||
|
|
||||||
// Perform database query scanning
|
// Perform database query scanning
|
||||||
// the remaining (uncached) token IDs.
|
// the remaining (uncached) token IDs.
|
||||||
|
|
|
@ -188,15 +188,8 @@ func (c *conversationDB) getConversationsByLastStatusIDs(
|
||||||
accountID,
|
accountID,
|
||||||
conversationLastStatusIDs,
|
conversationLastStatusIDs,
|
||||||
func(accountID string, uncached []string) ([]*gtsmodel.Conversation, error) {
|
func(accountID string, uncached []string) ([]*gtsmodel.Conversation, error) {
|
||||||
// Avoid querying
|
|
||||||
// if none uncached.
|
|
||||||
count := len(uncached)
|
|
||||||
if count == 0 {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Preallocate expected length of uncached conversations.
|
// Preallocate expected length of uncached conversations.
|
||||||
conversations := make([]*gtsmodel.Conversation, 0, count)
|
conversations := make([]*gtsmodel.Conversation, 0, len(uncached))
|
||||||
|
|
||||||
// Perform database query scanning the remaining (uncached) IDs.
|
// Perform database query scanning the remaining (uncached) IDs.
|
||||||
if err := c.db.NewSelect().
|
if err := c.db.NewSelect().
|
||||||
|
@ -267,29 +260,29 @@ func (c *conversationDB) LinkConversationToStatus(ctx context.Context, conversat
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *conversationDB) DeleteConversationByID(ctx context.Context, id string) error {
|
func (c *conversationDB) DeleteConversationByID(ctx context.Context, id string) error {
|
||||||
// Load conversation into cache before attempting a delete,
|
// Gather necessary fields from
|
||||||
// as we need it cached in order to trigger the invalidate
|
// deleted for cache invaliation.
|
||||||
// callback. This in turn invalidates others.
|
var deleted gtsmodel.Conversation
|
||||||
_, err := c.GetConversationByID(gtscontext.SetBarebones(ctx), id)
|
deleted.ID = id
|
||||||
if err != nil {
|
|
||||||
if errors.Is(err, db.ErrNoEntries) {
|
|
||||||
// not an issue.
|
|
||||||
err = nil
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Drop this now-cached conversation on return after delete.
|
// Delete conversation from DB.
|
||||||
defer c.state.Caches.DB.Conversation.Invalidate("ID", id)
|
if _, err := c.db.NewDelete().
|
||||||
|
Model(&deleted).
|
||||||
// Finally delete conversation from DB.
|
|
||||||
_, err = c.db.NewDelete().
|
|
||||||
Model((*gtsmodel.Conversation)(nil)).
|
|
||||||
Where("? = ?", bun.Ident("id"), id).
|
Where("? = ?", bun.Ident("id"), id).
|
||||||
Exec(ctx)
|
Returning("?", bun.Ident("account_id")).
|
||||||
|
Exec(ctx); err != nil &&
|
||||||
|
!errors.Is(err, db.ErrNoEntries) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Invalidate cached conversation by ID,
|
||||||
|
// manually invalidate hook in case not cached.
|
||||||
|
c.state.Caches.DB.Conversation.Invalidate("ID", id)
|
||||||
|
c.state.Caches.OnInvalidateConversation(&deleted)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (c *conversationDB) DeleteConversationsByOwnerAccountID(ctx context.Context, accountID string) error {
|
func (c *conversationDB) DeleteConversationsByOwnerAccountID(ctx context.Context, accountID string) error {
|
||||||
defer func() {
|
defer func() {
|
||||||
// Invalidate any cached conversations and conversation IDs owned by this account on return.
|
// Invalidate any cached conversations and conversation IDs owned by this account on return.
|
||||||
|
|
|
@ -20,7 +20,6 @@
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"errors"
|
|
||||||
"slices"
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
@ -70,34 +69,15 @@ func (e *emojiDB) UpdateEmoji(ctx context.Context, emoji *gtsmodel.Emoji, column
|
||||||
|
|
||||||
func (e *emojiDB) DeleteEmojiByID(ctx context.Context, id string) error {
|
func (e *emojiDB) DeleteEmojiByID(ctx context.Context, id string) error {
|
||||||
var (
|
var (
|
||||||
|
// Gather necessary fields from
|
||||||
|
// deleted for cache invaliation.
|
||||||
accountIDs []string
|
accountIDs []string
|
||||||
statusIDs []string
|
statusIDs []string
|
||||||
)
|
)
|
||||||
|
|
||||||
defer func() {
|
// Delete the emoji and all related links to it in a singular transaction.
|
||||||
// Invalidate cached emoji.
|
if err := e.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
|
||||||
e.state.Caches.DB.Emoji.Invalidate("ID", id)
|
|
||||||
|
|
||||||
// Invalidate cached account and status IDs.
|
|
||||||
e.state.Caches.DB.Account.InvalidateIDs("ID", accountIDs)
|
|
||||||
e.state.Caches.DB.Status.InvalidateIDs("ID", statusIDs)
|
|
||||||
}()
|
|
||||||
|
|
||||||
// Load emoji into cache before attempting a delete,
|
|
||||||
// as we need it cached in order to trigger the invalidate
|
|
||||||
// callback. This in turn invalidates others.
|
|
||||||
_, err := e.GetEmojiByID(
|
|
||||||
gtscontext.SetBarebones(ctx),
|
|
||||||
id,
|
|
||||||
)
|
|
||||||
if err != nil && !errors.Is(err, db.ErrNoEntries) {
|
|
||||||
// NOTE: even if db.ErrNoEntries is returned, we
|
|
||||||
// still run the below transaction to ensure related
|
|
||||||
// objects are appropriately deleted.
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return e.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
|
|
||||||
// Delete relational links between this emoji
|
// Delete relational links between this emoji
|
||||||
// and any statuses using it, returning the
|
// and any statuses using it, returning the
|
||||||
// status IDs so we can later update them.
|
// status IDs so we can later update them.
|
||||||
|
@ -195,7 +175,16 @@ func(emojiID string) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
})
|
}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Invalidate emoji, and any effected statuses / accounts.
|
||||||
|
e.state.Caches.DB.Emoji.Invalidate("ID", id)
|
||||||
|
e.state.Caches.DB.Account.InvalidateIDs("ID", accountIDs)
|
||||||
|
e.state.Caches.DB.Status.InvalidateIDs("ID", statusIDs)
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *emojiDB) GetEmojisBy(ctx context.Context, domain string, includeDisabled bool, includeEnabled bool, shortcode string, maxShortcodeDomain string, minShortcodeDomain string, limit int) ([]*gtsmodel.Emoji, error) {
|
func (e *emojiDB) GetEmojisBy(ctx context.Context, domain string, includeDisabled bool, includeEnabled bool, shortcode string, maxShortcodeDomain string, minShortcodeDomain string, limit int) ([]*gtsmodel.Emoji, error) {
|
||||||
|
@ -586,15 +575,8 @@ func (e *emojiDB) GetEmojisByIDs(ctx context.Context, ids []string) ([]*gtsmodel
|
||||||
emojis, err := e.state.Caches.DB.Emoji.LoadIDs("ID",
|
emojis, err := e.state.Caches.DB.Emoji.LoadIDs("ID",
|
||||||
ids,
|
ids,
|
||||||
func(uncached []string) ([]*gtsmodel.Emoji, error) {
|
func(uncached []string) ([]*gtsmodel.Emoji, error) {
|
||||||
// Avoid querying
|
|
||||||
// if none uncached.
|
|
||||||
count := len(uncached)
|
|
||||||
if count == 0 {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Preallocate expected length of uncached emojis.
|
// Preallocate expected length of uncached emojis.
|
||||||
emojis := make([]*gtsmodel.Emoji, 0, count)
|
emojis := make([]*gtsmodel.Emoji, 0, len(uncached))
|
||||||
|
|
||||||
// Perform database query scanning
|
// Perform database query scanning
|
||||||
// the remaining (uncached) IDs.
|
// the remaining (uncached) IDs.
|
||||||
|
@ -657,15 +639,8 @@ func (e *emojiDB) GetEmojiCategoriesByIDs(ctx context.Context, ids []string) ([]
|
||||||
categories, err := e.state.Caches.DB.EmojiCategory.LoadIDs("ID",
|
categories, err := e.state.Caches.DB.EmojiCategory.LoadIDs("ID",
|
||||||
ids,
|
ids,
|
||||||
func(uncached []string) ([]*gtsmodel.EmojiCategory, error) {
|
func(uncached []string) ([]*gtsmodel.EmojiCategory, error) {
|
||||||
// Avoid querying
|
|
||||||
// if none uncached.
|
|
||||||
count := len(uncached)
|
|
||||||
if count == 0 {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Preallocate expected length of uncached categories.
|
// Preallocate expected length of uncached categories.
|
||||||
categories := make([]*gtsmodel.EmojiCategory, 0, count)
|
categories := make([]*gtsmodel.EmojiCategory, 0, len(uncached))
|
||||||
|
|
||||||
// Perform database query scanning
|
// Perform database query scanning
|
||||||
// the remaining (uncached) IDs.
|
// the remaining (uncached) IDs.
|
||||||
|
|
|
@ -83,14 +83,7 @@ func (f *filterDB) GetFiltersForAccountID(ctx context.Context, accountID string)
|
||||||
filters, err := f.state.Caches.DB.Filter.LoadIDs("ID",
|
filters, err := f.state.Caches.DB.Filter.LoadIDs("ID",
|
||||||
filterIDs,
|
filterIDs,
|
||||||
func(uncached []string) ([]*gtsmodel.Filter, error) {
|
func(uncached []string) ([]*gtsmodel.Filter, error) {
|
||||||
// Avoid querying
|
filters := make([]*gtsmodel.Filter, 0, len(uncached))
|
||||||
// if none uncached.
|
|
||||||
count := len(uncached)
|
|
||||||
if count == 0 {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
filters := make([]*gtsmodel.Filter, 0, count)
|
|
||||||
if err := f.db.
|
if err := f.db.
|
||||||
NewSelect().
|
NewSelect().
|
||||||
Model(&filters).
|
Model(&filters).
|
||||||
|
|
|
@ -113,14 +113,8 @@ func (f *filterDB) getFilterKeywords(ctx context.Context, idColumn string, id st
|
||||||
filterKeywords, err := f.state.Caches.DB.FilterKeyword.LoadIDs("ID",
|
filterKeywords, err := f.state.Caches.DB.FilterKeyword.LoadIDs("ID",
|
||||||
filterKeywordIDs,
|
filterKeywordIDs,
|
||||||
func(uncached []string) ([]*gtsmodel.FilterKeyword, error) {
|
func(uncached []string) ([]*gtsmodel.FilterKeyword, error) {
|
||||||
// Avoid querying
|
filterKeywords := make([]*gtsmodel.FilterKeyword, 0, len(uncached))
|
||||||
// if none uncached.
|
|
||||||
count := len(uncached)
|
|
||||||
if count == 0 {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
filterKeywords := make([]*gtsmodel.FilterKeyword, 0, count)
|
|
||||||
if err := f.db.
|
if err := f.db.
|
||||||
NewSelect().
|
NewSelect().
|
||||||
Model(&filterKeywords).
|
Model(&filterKeywords).
|
||||||
|
|
|
@ -100,14 +100,7 @@ func (f *filterDB) getFilterStatuses(ctx context.Context, idColumn string, id st
|
||||||
filterStatuses, err := f.state.Caches.DB.FilterStatus.LoadIDs("ID",
|
filterStatuses, err := f.state.Caches.DB.FilterStatus.LoadIDs("ID",
|
||||||
filterStatusIDs,
|
filterStatusIDs,
|
||||||
func(uncached []string) ([]*gtsmodel.FilterStatus, error) {
|
func(uncached []string) ([]*gtsmodel.FilterStatus, error) {
|
||||||
// Avoid querying
|
filterStatuses := make([]*gtsmodel.FilterStatus, 0, len(uncached))
|
||||||
// if none uncached.
|
|
||||||
count := len(uncached)
|
|
||||||
if count == 0 {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
filterStatuses := make([]*gtsmodel.FilterStatus, 0, count)
|
|
||||||
if err := f.db.
|
if err := f.db.
|
||||||
NewSelect().
|
NewSelect().
|
||||||
Model(&filterStatuses).
|
Model(&filterStatuses).
|
||||||
|
|
|
@ -29,6 +29,7 @@
|
||||||
"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) GetListsContainingFollowID(ctx context.Context, followID string) ([]*gtsmodel.List, error) {
|
||||||
|
listIDs, err := l.getListIDsWithFollowID(ctx, followID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return l.GetListsByIDs(ctx, listIDs)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *listDB) GetFollowsInList(ctx context.Context, listID string, page *paging.Page) ([]*gtsmodel.Follow, error) {
|
||||||
|
followIDs, err := l.GetFollowIDsInList(ctx, listID, page)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return l.state.DB.GetFollowsByIDs(ctx, followIDs)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *listDB) GetAccountsInList(ctx context.Context, listID string, page *paging.Page) ([]*gtsmodel.Account, error) {
|
||||||
|
accountIDs, err := l.GetAccountIDsInList(ctx, listID, page)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return l.state.DB.GetAccountsByIDs(ctx, accountIDs)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *listDB) IsAccountInList(ctx context.Context, listID string, accountID string) (bool, error) {
|
||||||
|
accountIDs, err := l.GetAccountIDsInList(ctx, listID, nil)
|
||||||
|
return slices.Contains(accountIDs, accountID), err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *listDB) PopulateList(ctx context.Context, list *gtsmodel.List) error {
|
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,22 +145,12 @@ func (l *listDB) PopulateList(ctx context.Context, list *gtsmodel.List) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if list.ListEntries == nil {
|
|
||||||
// List entries are not set, fetch from the database.
|
|
||||||
list.ListEntries, err = l.state.DB.GetListEntries(
|
|
||||||
gtscontext.SetBarebones(ctx),
|
|
||||||
list.ID,
|
|
||||||
"", "", "", 0,
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
errs.Appendf("error populating list entries: %w", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return errs.Combine()
|
return errs.Combine()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *listDB) PutList(ctx context.Context, list *gtsmodel.List) error {
|
func (l *listDB) PutList(ctx context.Context, list *gtsmodel.List) error {
|
||||||
|
// note that inserting list will call OnInvalidateList()
|
||||||
|
// which will handle clearing caches other than List cache.
|
||||||
return l.state.Caches.DB.List.Store(list, func() error {
|
return l.state.Caches.DB.List.Store(list, func() error {
|
||||||
_, err := l.db.NewInsert().Model(list).Exec(ctx)
|
_, err := l.db.NewInsert().Model(list).Exec(ctx)
|
||||||
return err
|
return err
|
||||||
|
@ -160,192 +164,146 @@ func (l *listDB) UpdateList(ctx context.Context, list *gtsmodel.List, columns ..
|
||||||
columns = append(columns, "updated_at")
|
columns = append(columns, "updated_at")
|
||||||
}
|
}
|
||||||
|
|
||||||
defer func() {
|
// Update list in the database, invalidating main list cache.
|
||||||
// Invalidate all entries for this list ID.
|
if err := l.state.Caches.DB.List.Store(list, func() error {
|
||||||
l.state.Caches.DB.ListEntry.Invalidate("ListID", list.ID)
|
|
||||||
|
|
||||||
// Invalidate this entire list's timeline.
|
|
||||||
if err := l.state.Timelines.List.RemoveTimeline(ctx, list.ID); err != nil {
|
|
||||||
log.Errorf(ctx, "error invalidating list timeline: %q", err)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
return l.state.Caches.DB.List.Store(list, func() error {
|
|
||||||
_, err := l.db.NewUpdate().
|
_, err := l.db.NewUpdate().
|
||||||
Model(list).
|
Model(list).
|
||||||
Where("? = ?", bun.Ident("list.id"), list.ID).
|
Where("? = ?", bun.Ident("list.id"), list.ID).
|
||||||
Column(columns...).
|
Column(columns...).
|
||||||
Exec(ctx)
|
Exec(ctx)
|
||||||
return err
|
return err
|
||||||
})
|
}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Invalidate this entire list's timeline.
|
||||||
|
if err := l.state.Timelines.List.RemoveTimeline(ctx, list.ID); err != nil {
|
||||||
|
log.Errorf(ctx, "error invalidating list timeline: %q", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *listDB) DeleteListByID(ctx context.Context, id string) error {
|
func (l *listDB) DeleteListByID(ctx context.Context, id string) error {
|
||||||
// Load list by ID into cache to ensure we can perform
|
// Acquire list owner ID.
|
||||||
// all necessary cache invalidation hooks on removal.
|
var accountID string
|
||||||
_, 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() {
|
// Gather follow IDs of all
|
||||||
// Invalidate this list from cache.
|
// entries contained in list.
|
||||||
l.state.Caches.DB.List.Invalidate("ID", id)
|
var followIDs []string
|
||||||
|
|
||||||
// Invalidate this entire list's timeline.
|
// Delete all list entries associated with list, and list itself in transaction.
|
||||||
if err := l.state.Timelines.List.RemoveTimeline(ctx, id); err != nil {
|
if err := l.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
|
||||||
log.Errorf(ctx, "error invalidating list timeline: %q", err)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
return l.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
|
|
||||||
// Delete all entries attached to list.
|
|
||||||
if _, err := tx.NewDelete().
|
if _, err := tx.NewDelete().
|
||||||
Table("list_entries").
|
Table("list_entries").
|
||||||
Where("? = ?", bun.Ident("list_id"), id).
|
Where("? = ?", bun.Ident("list_id"), id).
|
||||||
Exec(ctx); err != nil {
|
Returning("?", bun.Ident("follow_id")).
|
||||||
|
Exec(ctx, &followIDs); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete the list itself.
|
|
||||||
_, err := tx.NewDelete().
|
_, err := tx.NewDelete().
|
||||||
Table("lists").
|
Table("lists").
|
||||||
Where("? = ?", bun.Ident("id"), id).
|
Where("? = ?", bun.Ident("id"), id).
|
||||||
Exec(ctx)
|
Returning("?", bun.Ident("account_id")).
|
||||||
|
Exec(ctx, &accountID)
|
||||||
return err
|
return err
|
||||||
|
}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Invalidate the main list database cache.
|
||||||
|
l.state.Caches.DB.List.Invalidate("ID", id)
|
||||||
|
|
||||||
|
// Invalidate cache of list IDs owned by account.
|
||||||
|
l.state.Caches.DB.ListIDs.Invalidate("a" + accountID)
|
||||||
|
|
||||||
|
// Invalidate all related entry caches for this list.
|
||||||
|
l.invalidateEntryCaches(ctx, []string{id}, followIDs)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *listDB) getListIDsByAccountID(ctx context.Context, accountID string) ([]string, error) {
|
||||||
|
return l.state.Caches.DB.ListIDs.Load("a"+accountID, func() ([]string, error) {
|
||||||
|
var listIDs []string
|
||||||
|
|
||||||
|
// List IDs not in cache.
|
||||||
|
// Perform the DB query.
|
||||||
|
if _, err := l.db.NewSelect().
|
||||||
|
Table("lists").
|
||||||
|
Column("id").
|
||||||
|
Where("? = ?", bun.Ident("account_id"), accountID).
|
||||||
|
OrderExpr("? DESC", bun.Ident("created_at")).
|
||||||
|
Exec(ctx, &listIDs); err != nil &&
|
||||||
|
!errors.Is(err, db.ErrNoEntries) {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return listIDs, nil
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
func (l *listDB) getListIDsWithFollowID(ctx context.Context, followID string) ([]string, error) {
|
||||||
LIST ENTRY functions
|
return l.state.Caches.DB.ListIDs.Load("f"+followID, 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("list_entries").
|
||||||
func(listEntry *gtsmodel.ListEntry) error {
|
Column("list_id").
|
||||||
return l.db.NewSelect().
|
Where("? = ?", bun.Ident("follow_id"), followID).
|
||||||
Model(listEntry).
|
OrderExpr("? DESC", bun.Ident("created_at")).
|
||||||
Where("? = ?", bun.Ident("list_entry.id"), id).
|
Exec(ctx, &listIDs); err != nil &&
|
||||||
Scan(ctx)
|
!errors.Is(err, db.ErrNoEntries) {
|
||||||
},
|
|
||||||
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) {
|
func (l *listDB) GetFollowIDsInList(ctx context.Context, listID string, page *paging.Page) ([]string, error) {
|
||||||
// Only a barebones model was requested.
|
return loadPagedIDs(&l.state.Caches.DB.ListedIDs, "f"+listID, page, func() ([]string, error) {
|
||||||
return listEntry, nil
|
var followIDs []string
|
||||||
}
|
|
||||||
|
|
||||||
// Further populate the list entry fields where applicable.
|
// Follow IDs not in cache.
|
||||||
if err := l.state.DB.PopulateListEntry(ctx, listEntry); err != nil {
|
// Perform the DB query.
|
||||||
|
_, err := l.db.NewSelect().
|
||||||
|
Table("list_entries").
|
||||||
|
Column("follow_id").
|
||||||
|
Where("? = ?", bun.Ident("list_id"), listID).
|
||||||
|
OrderExpr("? DESC", bun.Ident("created_at")).
|
||||||
|
Exec(ctx, &followIDs)
|
||||||
|
if err != nil && !errors.Is(err, db.ErrNoEntries) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return listEntry, nil
|
return followIDs, nil
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *listDB) GetListEntries(ctx context.Context,
|
func (l *listDB) GetAccountIDsInList(ctx context.Context, listID string, page *paging.Page) ([]string, error) {
|
||||||
listID string,
|
return loadPagedIDs(&l.state.Caches.DB.ListedIDs, "a"+listID, page, func() ([]string, error) {
|
||||||
maxID string,
|
var accountIDs []string
|
||||||
sinceID string,
|
|
||||||
minID string,
|
|
||||||
limit int,
|
|
||||||
) ([]*gtsmodel.ListEntry, error) {
|
|
||||||
// Ensure reasonable
|
|
||||||
if limit < 0 {
|
|
||||||
limit = 0
|
|
||||||
}
|
|
||||||
|
|
||||||
// Make educated guess for slice size
|
// Account IDs not in cache.
|
||||||
var (
|
// Perform the DB query.
|
||||||
entryIDs = make([]string, 0, limit)
|
_, err := l.db.NewSelect().
|
||||||
frontToBack = true
|
Table("follows").
|
||||||
)
|
Column("follows.target_account_id").
|
||||||
|
Join("INNER JOIN ?", bun.Ident("list_entries")).
|
||||||
q := l.db.
|
JoinOn("? = ?", bun.Ident("follows.id"), bun.Ident("list_entries.follow_id")).
|
||||||
NewSelect().
|
Where("? = ?", bun.Ident("list_entries.list_id"), listID).
|
||||||
TableExpr("? AS ?", bun.Ident("list_entries"), bun.Ident("entry")).
|
OrderExpr("? DESC", bun.Ident("list_entries.id")).
|
||||||
// Select only IDs from table
|
Exec(ctx, &accountIDs)
|
||||||
Column("entry.id").
|
if err != nil && !errors.Is(err, db.ErrNoEntries) {
|
||||||
// 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
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(entryIDs) == 0 {
|
return accountIDs, nil
|
||||||
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 l.GetListEntriesByIDs(ctx, entryIDs)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *listDB) GetListsByIDs(ctx context.Context, ids []string) ([]*gtsmodel.List, error) {
|
func (l *listDB) GetListsByIDs(ctx context.Context, ids []string) ([]*gtsmodel.List, error) {
|
||||||
|
@ -353,15 +311,8 @@ func (l *listDB) GetListsByIDs(ctx context.Context, ids []string) ([]*gtsmodel.L
|
||||||
lists, err := l.state.Caches.DB.List.LoadIDs("ID",
|
lists, err := l.state.Caches.DB.List.LoadIDs("ID",
|
||||||
ids,
|
ids,
|
||||||
func(uncached []string) ([]*gtsmodel.List, error) {
|
func(uncached []string) ([]*gtsmodel.List, error) {
|
||||||
// Avoid querying
|
|
||||||
// if none uncached.
|
|
||||||
count := len(uncached)
|
|
||||||
if count == 0 {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Preallocate expected length of uncached lists.
|
// Preallocate expected length of uncached lists.
|
||||||
lists := make([]*gtsmodel.List, 0, count)
|
lists := make([]*gtsmodel.List, 0, len(uncached))
|
||||||
|
|
||||||
// Perform database query scanning
|
// Perform database query scanning
|
||||||
// the remaining (uncached) IDs.
|
// the remaining (uncached) IDs.
|
||||||
|
@ -402,82 +353,6 @@ func(uncached []string) ([]*gtsmodel.List, error) {
|
||||||
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
|
||||||
|
|
||||||
|
@ -496,109 +371,111 @@ func (l *listDB) PopulateListEntry(ctx context.Context, listEntry *gtsmodel.List
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *listDB) PutListEntries(ctx context.Context, entries []*gtsmodel.ListEntry) error {
|
func (l *listDB) PutListEntries(ctx context.Context, entries []*gtsmodel.ListEntry) error {
|
||||||
defer func() {
|
// Insert all entries into the database in a single transaction (all or nothing!).
|
||||||
// Collect unique list IDs from the provided entries.
|
if err := l.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
|
||||||
|
for _, entry := range entries {
|
||||||
|
if _, err := tx.
|
||||||
|
NewInsert().
|
||||||
|
Model(entry).
|
||||||
|
Exec(ctx); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Collect unique list IDs from the provided list entries.
|
||||||
listIDs := util.Collate(entries, func(e *gtsmodel.ListEntry) string {
|
listIDs := util.Collate(entries, func(e *gtsmodel.ListEntry) string {
|
||||||
return e.ListID
|
return e.ListID
|
||||||
})
|
})
|
||||||
|
|
||||||
for _, id := range listIDs {
|
// Collect unique follow IDs from the provided list entries.
|
||||||
// Invalidate the timeline for the list this entry belongs to.
|
followIDs := util.Collate(entries, func(e *gtsmodel.ListEntry) string {
|
||||||
if err := l.state.Timelines.List.RemoveTimeline(ctx, id); err != nil {
|
return e.FollowID
|
||||||
log.Errorf(ctx, "error invalidating list timeline: %q", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
// Finally, insert each list entry into the database.
|
|
||||||
return l.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
|
|
||||||
for _, entry := range entries {
|
|
||||||
entry := entry // rescope
|
|
||||||
if err := l.state.Caches.DB.ListEntry.Store(entry, func() error {
|
|
||||||
_, err := tx.
|
|
||||||
NewInsert().
|
|
||||||
Model(entry).
|
|
||||||
Exec(ctx)
|
|
||||||
return err
|
|
||||||
}); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
})
|
})
|
||||||
}
|
|
||||||
|
|
||||||
func (l *listDB) DeleteListEntry(ctx context.Context, id string) error {
|
// Invalidate all related list entry caches.
|
||||||
// Load list entry into cache to ensure we can perform
|
l.invalidateEntryCaches(ctx, listIDs, followIDs)
|
||||||
// all necessary cache invalidation hooks on removal.
|
|
||||||
entry, err := l.GetListEntryByID(
|
|
||||||
// Don't populate the entry;
|
|
||||||
// we only want the list ID.
|
|
||||||
gtscontext.SetBarebones(ctx),
|
|
||||||
id,
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
if errors.Is(err, db.ErrNoEntries) {
|
|
||||||
// Already gone.
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (l *listDB) DeleteListEntry(ctx context.Context, listID string, followID string) error {
|
||||||
|
// Delete list entry with given
|
||||||
|
// ID, returning its list ID.
|
||||||
|
if _, err := l.db.NewDelete().
|
||||||
|
Table("list_entries").
|
||||||
|
Where("? = ?", bun.Ident("list_id"), listID).
|
||||||
|
Where("? = ?", bun.Ident("follow_id"), followID).
|
||||||
|
Exec(ctx, &listID); err != nil &&
|
||||||
|
!errors.Is(err, db.ErrNoEntries) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
defer func() {
|
// Invalidate all related list entry caches.
|
||||||
// Invalidate this list entry upon delete.
|
l.invalidateEntryCaches(ctx, []string{listID},
|
||||||
l.state.Caches.DB.ListEntry.Invalidate("ID", id)
|
[]string{followID})
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *listDB) DeleteAllListEntriesByFollows(ctx context.Context, followIDs ...string) error {
|
||||||
|
var listIDs []string
|
||||||
|
|
||||||
|
// Check for empty list.
|
||||||
|
if len(followIDs) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete all entries with follow
|
||||||
|
// ID, returning IDs and list IDs.
|
||||||
|
if _, err := l.db.NewDelete().
|
||||||
|
Table("list_entries").
|
||||||
|
Where("? IN (?)", bun.Ident("follow_id"), bun.In(followIDs)).
|
||||||
|
Returning("?", bun.Ident("list_id")).
|
||||||
|
Exec(ctx, &listIDs); err != nil &&
|
||||||
|
!errors.Is(err, db.ErrNoEntries) {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Deduplicate IDs before invalidate.
|
||||||
|
listIDs = util.Deduplicate(listIDs)
|
||||||
|
|
||||||
|
// Invalidate all related list entry caches.
|
||||||
|
l.invalidateEntryCaches(ctx, listIDs, followIDs)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// invalidateEntryCaches will invalidate all related ListEntry caches for given list IDs and follow IDs, including timelines.
|
||||||
|
func (l *listDB) invalidateEntryCaches(ctx context.Context, listIDs, followIDs []string) {
|
||||||
|
var keys []string
|
||||||
|
|
||||||
|
// Generate ListedID keys to invalidate.
|
||||||
|
keys = slices.Grow(keys[:0], 2*len(listIDs))
|
||||||
|
for _, listID := range listIDs {
|
||||||
|
keys = append(keys,
|
||||||
|
"a"+listID,
|
||||||
|
"f"+listID,
|
||||||
|
)
|
||||||
|
|
||||||
// Invalidate the timeline for the list this entry belongs to.
|
// Invalidate the timeline for the list this entry belongs to.
|
||||||
if err := l.state.Timelines.List.RemoveTimeline(ctx, entry.ListID); err != nil {
|
if err := l.state.Timelines.List.RemoveTimeline(ctx, listID); err != nil {
|
||||||
log.Errorf(ctx, "error invalidating list timeline: %q", err)
|
log.Errorf(ctx, "error invalidating list timeline: %q", err)
|
||||||
}
|
}
|
||||||
}()
|
|
||||||
|
|
||||||
// Finally delete the list entry.
|
|
||||||
_, err = l.db.NewDelete().
|
|
||||||
Table("list_entries").
|
|
||||||
Where("? = ?", bun.Ident("id"), id).
|
|
||||||
Exec(ctx)
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *listDB) DeleteListEntriesForFollowID(ctx context.Context, followID string) error {
|
// Invalidate ListedID slice cache entries.
|
||||||
var entryIDs []string
|
l.state.Caches.DB.ListedIDs.Invalidate(keys...)
|
||||||
|
|
||||||
// Fetch entry IDs for follow ID.
|
// Generate ListID keys to invalidate.
|
||||||
if err := l.db.
|
keys = slices.Grow(keys[:0], len(followIDs))
|
||||||
NewSelect().
|
for _, followID := range followIDs {
|
||||||
Table("list_entries").
|
keys = append(keys, "f"+followID)
|
||||||
Column("id").
|
|
||||||
Where("? = ?", bun.Ident("follow_id"), followID).
|
|
||||||
Order("id DESC").
|
|
||||||
Scan(ctx, &entryIDs); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, id := range entryIDs {
|
// Invalidate ListID slice cache entries.
|
||||||
// Delete each separately to trigger cache invalidations.
|
l.state.Caches.DB.ListIDs.Invalidate(keys...)
|
||||||
if err := l.DeleteListEntry(ctx, id); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *listDB) ListIncludesAccount(ctx context.Context, listID string, accountID string) (bool, error) {
|
|
||||||
exists, err := l.db.
|
|
||||||
NewSelect().
|
|
||||||
TableExpr("? AS ?", bun.Ident("list_entries"), bun.Ident("list_entry")).
|
|
||||||
Join(
|
|
||||||
"JOIN ? AS ? ON ? = ?",
|
|
||||||
bun.Ident("follows"), bun.Ident("follow"),
|
|
||||||
bun.Ident("list_entry.follow_id"), bun.Ident("follow.id"),
|
|
||||||
).
|
|
||||||
Where("? = ?", bun.Ident("list_entry.list_id"), listID).
|
|
||||||
Where("? = ?", bun.Ident("follow.target_account_id"), accountID).
|
|
||||||
Exists(ctx)
|
|
||||||
|
|
||||||
return exists, err
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,7 +24,6 @@
|
||||||
|
|
||||||
"github.com/stretchr/testify/suite"
|
"github.com/stretchr/testify/suite"
|
||||||
"github.com/superseriousbusiness/gotosocial/internal/db"
|
"github.com/superseriousbusiness/gotosocial/internal/db"
|
||||||
"github.com/superseriousbusiness/gotosocial/internal/gtscontext"
|
|
||||||
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -32,7 +31,7 @@ type ListTestSuite struct {
|
||||||
BunDBStandardTestSuite
|
BunDBStandardTestSuite
|
||||||
}
|
}
|
||||||
|
|
||||||
func (suite *ListTestSuite) testStructs() (*gtsmodel.List, *gtsmodel.Account) {
|
func (suite *ListTestSuite) testStructs() (*gtsmodel.List, []*gtsmodel.ListEntry, *gtsmodel.Account) {
|
||||||
testList := >smodel.List{}
|
testList := >smodel.List{}
|
||||||
*testList = *suite.testLists["local_account_1_list_1"]
|
*testList = *suite.testLists["local_account_1_list_1"]
|
||||||
|
|
||||||
|
@ -55,12 +54,10 @@ func (suite *ListTestSuite) testStructs() (*gtsmodel.List, *gtsmodel.Account) {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
testList.ListEntries = entries
|
|
||||||
|
|
||||||
testAccount := >smodel.Account{}
|
testAccount := >smodel.Account{}
|
||||||
*testAccount = *suite.testAccounts["local_account_1"]
|
*testAccount = *suite.testAccounts["local_account_1"]
|
||||||
|
|
||||||
return testList, testAccount
|
return testList, entries, testAccount
|
||||||
}
|
}
|
||||||
|
|
||||||
func (suite *ListTestSuite) checkList(expected *gtsmodel.List, actual *gtsmodel.List) {
|
func (suite *ListTestSuite) checkList(expected *gtsmodel.List, actual *gtsmodel.List) {
|
||||||
|
@ -103,7 +100,7 @@ func (suite *ListTestSuite) checkListEntries(expected []*gtsmodel.ListEntry, act
|
||||||
}
|
}
|
||||||
|
|
||||||
func (suite *ListTestSuite) TestGetListByID() {
|
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 {
|
||||||
|
@ -111,13 +108,12 @@ func (suite *ListTestSuite) TestGetListByID() {
|
||||||
}
|
}
|
||||||
|
|
||||||
suite.checkList(testList, dbList)
|
suite.checkList(testList, dbList)
|
||||||
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.GetListsByAccountID(context.Background(), testAccount.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
suite.FailNow(err.Error())
|
suite.FailNow(err.Error())
|
||||||
}
|
}
|
||||||
|
@ -129,20 +125,9 @@ func (suite *ListTestSuite) TestGetListsForAccountID() {
|
||||||
suite.checkList(testList, dbLists[0])
|
suite.checkList(testList, dbLists[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
func (suite *ListTestSuite) TestGetListEntries() {
|
|
||||||
testList, _ := suite.testStructs()
|
|
||||||
|
|
||||||
dbListEntries, err := suite.db.GetListEntries(context.Background(), testList.ID, "", "", "", 0)
|
|
||||||
if err != nil {
|
|
||||||
suite.FailNow(err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
suite.checkListEntries(testList.ListEntries, dbListEntries)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (suite *ListTestSuite) TestPutList() {
|
func (suite *ListTestSuite) TestPutList() {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
_, testAccount := suite.testStructs()
|
_, _, testAccount := suite.testStructs()
|
||||||
|
|
||||||
testList := >smodel.List{
|
testList := >smodel.List{
|
||||||
ID: "01H0J2PMYM54618VCV8Y8QYAT4",
|
ID: "01H0J2PMYM54618VCV8Y8QYAT4",
|
||||||
|
@ -166,7 +151,7 @@ func (suite *ListTestSuite) TestPutList() {
|
||||||
|
|
||||||
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)
|
||||||
|
@ -192,7 +177,7 @@ func (suite *ListTestSuite) TestUpdateList() {
|
||||||
|
|
||||||
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 {
|
||||||
|
@ -209,18 +194,19 @@ func (suite *ListTestSuite) TestDeleteList() {
|
||||||
_, 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 accounts / follows attached to this
|
||||||
// list should now be deleted.
|
// list should now be return empty values.
|
||||||
listEntries, err := suite.db.GetListEntries(ctx, testList.ID, "", "", "", 0)
|
listAccounts, err1 := suite.db.GetAccountsInList(ctx, testList.ID, nil)
|
||||||
if err != nil {
|
listFollows, err2 := suite.db.GetFollowsInList(ctx, testList.ID, nil)
|
||||||
suite.FailNow(err.Error())
|
suite.NoError(err1)
|
||||||
}
|
suite.NoError(err2)
|
||||||
suite.Empty(listEntries)
|
suite.Empty(listAccounts)
|
||||||
|
suite.Empty(listFollows)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (suite *ListTestSuite) TestPutListEntries() {
|
func (suite *ListTestSuite) TestPutListEntries() {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
testList, _ := suite.testStructs()
|
testList, testEntries, _ := suite.testStructs()
|
||||||
|
|
||||||
listEntries := []*gtsmodel.ListEntry{
|
listEntries := []*gtsmodel.ListEntry{
|
||||||
{
|
{
|
||||||
|
@ -244,91 +230,58 @@ func (suite *ListTestSuite) TestPutListEntries() {
|
||||||
suite.FailNow(err.Error())
|
suite.FailNow(err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add these entries to the test list, sort it again
|
// Get all follows stored under this list ID, to ensure
|
||||||
// to reflect what we'd expect to get from the db.
|
// the newly added list entry follows are among these.
|
||||||
testList.ListEntries = append(testList.ListEntries, listEntries...)
|
followIDs, err := suite.db.GetFollowIDsInList(ctx, testList.ID, nil)
|
||||||
slices.SortFunc(testList.ListEntries, func(a, b *gtsmodel.ListEntry) int {
|
suite.NoError(err)
|
||||||
const k = -1
|
suite.Len(followIDs, len(testEntries)+len(listEntries))
|
||||||
switch {
|
suite.Contains(followIDs, "01H0MKNFRFZS8R9WV6DBX31Y03")
|
||||||
case a.ID > b.ID:
|
suite.Contains(followIDs, "01H0MKP6RR8VEHN3GVWFBP2H30")
|
||||||
return +k
|
suite.Contains(followIDs, "01H0MKQ0KA29C6NFJ27GTZD16J")
|
||||||
case a.ID < b.ID:
|
|
||||||
return -k
|
|
||||||
default:
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
// Now get all list entries from the db.
|
|
||||||
// Use barebones for this because the ones
|
|
||||||
// we just added will fail if we try to get
|
|
||||||
// the nonexistent follows.
|
|
||||||
dbListEntries, err := suite.db.GetListEntries(
|
|
||||||
gtscontext.SetBarebones(ctx),
|
|
||||||
testList.ID,
|
|
||||||
"", "", "", 0)
|
|
||||||
if err != nil {
|
|
||||||
suite.FailNow(err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
suite.checkListEntries(testList.ListEntries, dbListEntries)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (suite *ListTestSuite) TestDeleteListEntry() {
|
func (suite *ListTestSuite) TestDeleteListEntry() {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
testList, _ := suite.testStructs()
|
testList, testEntries, _ := suite.testStructs()
|
||||||
|
|
||||||
// Get List in the cache first.
|
|
||||||
if _, err := suite.db.GetListByID(ctx, testList.ID); err != nil {
|
|
||||||
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,
|
||||||
|
testEntries[0].ListID,
|
||||||
|
testEntries[0].FollowID,
|
||||||
|
); err != nil {
|
||||||
suite.FailNow(err.Error())
|
suite.FailNow(err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get list from the db again.
|
// Get all follows stored under this list ID, to ensure
|
||||||
dbList, err := suite.db.GetListByID(ctx, testList.ID)
|
// the newly removed list entry follow is now missing.
|
||||||
if err != nil {
|
followIDs, err := suite.db.GetFollowIDsInList(ctx, testList.ID, nil)
|
||||||
suite.FailNow(err.Error())
|
suite.NoError(err)
|
||||||
|
suite.Len(followIDs, len(testEntries)-1)
|
||||||
|
suite.NotContains(followIDs, testEntries[0].FollowID)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Bodge the testlist as though
|
func (suite *ListTestSuite) TestDeleteAllListEntriesByFollows() {
|
||||||
// we'd removed the first entry.
|
|
||||||
testList.ListEntries = testList.ListEntries[1:]
|
|
||||||
suite.checkList(testList, dbList)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (suite *ListTestSuite) TestDeleteListEntriesForFollowID() {
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
testList, _ := suite.testStructs()
|
testList, testEntries, _ := suite.testStructs()
|
||||||
|
|
||||||
// Get List in the cache first.
|
|
||||||
if _, err := suite.db.GetListByID(ctx, testList.ID); err != nil {
|
|
||||||
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.DeleteAllListEntriesByFollows(ctx,
|
||||||
|
testEntries[0].FollowID,
|
||||||
|
); err != nil {
|
||||||
suite.FailNow(err.Error())
|
suite.FailNow(err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get list from the db again.
|
// Get all follows stored under this list ID, to ensure
|
||||||
dbList, err := suite.db.GetListByID(ctx, testList.ID)
|
// the newly removed list entry follow is now missing.
|
||||||
if err != nil {
|
followIDs, err := suite.db.GetFollowIDsInList(ctx, testList.ID, nil)
|
||||||
suite.FailNow(err.Error())
|
suite.NoError(err)
|
||||||
}
|
suite.Len(followIDs, len(testEntries)-1)
|
||||||
|
suite.NotContains(followIDs, testEntries[0].FollowID)
|
||||||
// Bodge the testlist as though
|
|
||||||
// we'd removed the first entry.
|
|
||||||
testList.ListEntries = testList.ListEntries[1:]
|
|
||||||
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,
|
||||||
|
@ -336,7 +289,7 @@ func (suite *ListTestSuite) TestListIncludesAccount() {
|
||||||
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.IsAccountInList(ctx, testList.ID, accountID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
suite.FailNow(err.Error())
|
suite.FailNow(err.Error())
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,7 +24,6 @@
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"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/paging"
|
"github.com/superseriousbusiness/gotosocial/internal/paging"
|
||||||
|
@ -57,15 +56,8 @@ func (m *mediaDB) GetAttachmentsByIDs(ctx context.Context, ids []string) ([]*gts
|
||||||
media, err := m.state.Caches.DB.Media.LoadIDs("ID",
|
media, err := m.state.Caches.DB.Media.LoadIDs("ID",
|
||||||
ids,
|
ids,
|
||||||
func(uncached []string) ([]*gtsmodel.MediaAttachment, error) {
|
func(uncached []string) ([]*gtsmodel.MediaAttachment, error) {
|
||||||
// Avoid querying
|
|
||||||
// if none uncached.
|
|
||||||
count := len(uncached)
|
|
||||||
if count == 0 {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Preallocate expected length of uncached media attachments.
|
// Preallocate expected length of uncached media attachments.
|
||||||
media := make([]*gtsmodel.MediaAttachment, 0, count)
|
media := make([]*gtsmodel.MediaAttachment, 0, len(uncached))
|
||||||
|
|
||||||
// Perform database query scanning
|
// Perform database query scanning
|
||||||
// the remaining (uncached) IDs.
|
// the remaining (uncached) IDs.
|
||||||
|
@ -129,30 +121,38 @@ func (m *mediaDB) UpdateAttachment(ctx context.Context, media *gtsmodel.MediaAtt
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *mediaDB) DeleteAttachment(ctx context.Context, id string) error {
|
func (m *mediaDB) DeleteAttachment(ctx context.Context, id string) error {
|
||||||
// Load media into cache before attempting a delete,
|
// Gather necessary fields from
|
||||||
// as we need it cached in order to trigger the invalidate
|
// deleted for cache invaliation.
|
||||||
// callback. This in turn invalidates others.
|
var deleted gtsmodel.MediaAttachment
|
||||||
media, err := m.GetAttachmentByID(gtscontext.SetBarebones(ctx), id)
|
deleted.ID = id
|
||||||
if err != nil {
|
|
||||||
if errors.Is(err, db.ErrNoEntries) {
|
// Delete media attachment and update related models in new transaction.
|
||||||
// not an issue.
|
err := m.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
|
||||||
err = nil
|
|
||||||
}
|
// Initially, delete the media model,
|
||||||
return err
|
// returning the required fields we need.
|
||||||
|
if _, err := tx.NewDelete().
|
||||||
|
Model(&deleted).
|
||||||
|
Where("? = ?", bun.Ident("id"), id).
|
||||||
|
Returning("?, ?, ?, ?",
|
||||||
|
bun.Ident("account_id"),
|
||||||
|
bun.Ident("status_id"),
|
||||||
|
bun.Ident("avatar"),
|
||||||
|
bun.Ident("header"),
|
||||||
|
).
|
||||||
|
Exec(ctx); err != nil {
|
||||||
|
return gtserror.Newf("error deleting media: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// On return, ensure that media with ID is invalidated.
|
// If media was attached to account,
|
||||||
defer m.state.Caches.DB.Media.Invalidate("ID", id)
|
// we need to remove link from account.
|
||||||
|
if deleted.AccountID != "" {
|
||||||
// Delete media attachment in new transaction.
|
|
||||||
err = m.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
|
|
||||||
if media.AccountID != "" {
|
|
||||||
var account gtsmodel.Account
|
var account gtsmodel.Account
|
||||||
|
|
||||||
// Get related account model.
|
// Get related account model.
|
||||||
if _, err := tx.NewSelect().
|
if _, err := tx.NewSelect().
|
||||||
Model(&account).
|
Model(&account).
|
||||||
Where("? = ?", bun.Ident("id"), media.AccountID).
|
Where("? = ?", bun.Ident("id"), deleted.AccountID).
|
||||||
Exec(ctx); err != nil && !errors.Is(err, db.ErrNoEntries) {
|
Exec(ctx); err != nil && !errors.Is(err, db.ErrNoEntries) {
|
||||||
return gtserror.Newf("error selecting account: %w", err)
|
return gtserror.Newf("error selecting account: %w", err)
|
||||||
}
|
}
|
||||||
|
@ -160,11 +160,11 @@ func (m *mediaDB) DeleteAttachment(ctx context.Context, id string) error {
|
||||||
var set func(*bun.UpdateQuery) *bun.UpdateQuery
|
var set func(*bun.UpdateQuery) *bun.UpdateQuery
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
case *media.Avatar && account.AvatarMediaAttachmentID == id:
|
case *deleted.Avatar && account.AvatarMediaAttachmentID == id:
|
||||||
set = func(q *bun.UpdateQuery) *bun.UpdateQuery {
|
set = func(q *bun.UpdateQuery) *bun.UpdateQuery {
|
||||||
return q.Set("? = NULL", bun.Ident("avatar_media_attachment_id"))
|
return q.Set("? = NULL", bun.Ident("avatar_media_attachment_id"))
|
||||||
}
|
}
|
||||||
case *media.Header && account.HeaderMediaAttachmentID == id:
|
case *deleted.Header && account.HeaderMediaAttachmentID == id:
|
||||||
set = func(q *bun.UpdateQuery) *bun.UpdateQuery {
|
set = func(q *bun.UpdateQuery) *bun.UpdateQuery {
|
||||||
return q.Set("? = NULL", bun.Ident("header_media_attachment_id"))
|
return q.Set("? = NULL", bun.Ident("header_media_attachment_id"))
|
||||||
}
|
}
|
||||||
|
@ -183,13 +183,15 @@ func (m *mediaDB) DeleteAttachment(ctx context.Context, id string) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if media.StatusID != "" {
|
// If media was attached to a status,
|
||||||
|
// we need to remove link from status.
|
||||||
|
if deleted.StatusID != "" {
|
||||||
var status gtsmodel.Status
|
var status gtsmodel.Status
|
||||||
|
|
||||||
// Get related status model.
|
// Get related status model.
|
||||||
if _, err := tx.NewSelect().
|
if _, err := tx.NewSelect().
|
||||||
Model(&status).
|
Model(&status).
|
||||||
Where("? = ?", bun.Ident("id"), media.StatusID).
|
Where("? = ?", bun.Ident("id"), deleted.StatusID).
|
||||||
Exec(ctx); err != nil && !errors.Is(err, db.ErrNoEntries) {
|
Exec(ctx); err != nil && !errors.Is(err, db.ErrNoEntries) {
|
||||||
return gtserror.Newf("error selecting status: %w", err)
|
return gtserror.Newf("error selecting status: %w", err)
|
||||||
}
|
}
|
||||||
|
@ -213,17 +215,14 @@ func (m *mediaDB) DeleteAttachment(ctx context.Context, id string) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Finally delete this media.
|
|
||||||
if _, err := tx.NewDelete().
|
|
||||||
Table("media_attachments").
|
|
||||||
Where("? = ?", bun.Ident("id"), id).
|
|
||||||
Exec(ctx); err != nil {
|
|
||||||
return gtserror.Newf("error deleting media: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Invalidate cached media with ID, manually
|
||||||
|
// call invalidate hook in case not in cache.
|
||||||
|
m.state.Caches.DB.Media.Invalidate("ID", id)
|
||||||
|
m.state.Caches.OnInvalidateMedia(&deleted)
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -69,15 +69,8 @@ func (m *mentionDB) GetMentions(ctx context.Context, ids []string) ([]*gtsmodel.
|
||||||
mentions, err := m.state.Caches.DB.Mention.LoadIDs("ID",
|
mentions, err := m.state.Caches.DB.Mention.LoadIDs("ID",
|
||||||
ids,
|
ids,
|
||||||
func(uncached []string) ([]*gtsmodel.Mention, error) {
|
func(uncached []string) ([]*gtsmodel.Mention, error) {
|
||||||
// Avoid querying
|
|
||||||
// if none uncached.
|
|
||||||
count := len(uncached)
|
|
||||||
if count == 0 {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Preallocate expected length of uncached mentions.
|
// Preallocate expected length of uncached mentions.
|
||||||
mentions := make([]*gtsmodel.Mention, 0, count)
|
mentions := make([]*gtsmodel.Mention, 0, len(uncached))
|
||||||
|
|
||||||
// Perform database query scanning
|
// Perform database query scanning
|
||||||
// the remaining (uncached) IDs.
|
// the remaining (uncached) IDs.
|
||||||
|
@ -166,24 +159,18 @@ func (m *mentionDB) PutMention(ctx context.Context, mention *gtsmodel.Mention) e
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *mentionDB) DeleteMentionByID(ctx context.Context, id string) error {
|
func (m *mentionDB) DeleteMentionByID(ctx context.Context, id string) error {
|
||||||
defer m.state.Caches.DB.Mention.Invalidate("ID", id)
|
// Delete mention with given ID,
|
||||||
|
// returning the deleted models.
|
||||||
// Load mention into cache before attempting a delete,
|
if _, err := m.db.NewDelete().
|
||||||
// as we need it cached in order to trigger the invalidate
|
|
||||||
// callback. This in turn invalidates others.
|
|
||||||
_, err := m.GetMention(gtscontext.SetBarebones(ctx), id)
|
|
||||||
if err != nil {
|
|
||||||
if errors.Is(err, db.ErrNoEntries) {
|
|
||||||
// not an issue.
|
|
||||||
err = nil
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Finally delete mention from DB.
|
|
||||||
_, err = m.db.NewDelete().
|
|
||||||
Table("mentions").
|
Table("mentions").
|
||||||
Where("? = ?", bun.Ident("id"), id).
|
Where("? = ?", bun.Ident("id"), id).
|
||||||
Exec(ctx)
|
Exec(ctx); err != nil &&
|
||||||
|
!errors.Is(err, db.ErrNoEntries) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Invalidate the cached mention with ID.
|
||||||
|
m.state.Caches.DB.Mention.Invalidate("ID", id)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
|
@ -234,13 +234,17 @@ func (m *moveDB) UpdateMove(ctx context.Context, move *gtsmodel.Move, columns ..
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *moveDB) DeleteMoveByID(ctx context.Context, id string) error {
|
func (m *moveDB) DeleteMoveByID(ctx context.Context, id string) error {
|
||||||
defer m.state.Caches.DB.Move.Invalidate("ID", id)
|
// Delete move with given ID.
|
||||||
|
if _, err := m.db.NewDelete().
|
||||||
_, err := m.db.
|
|
||||||
NewDelete().
|
|
||||||
TableExpr("? AS ?", bun.Ident("moves"), bun.Ident("move")).
|
TableExpr("? AS ?", bun.Ident("moves"), bun.Ident("move")).
|
||||||
Where("? = ?", bun.Ident("move.id"), id).
|
Where("? = ?", bun.Ident("move.id"), id).
|
||||||
Exec(ctx)
|
Exec(ctx); err != nil &&
|
||||||
|
!errors.Is(err, db.ErrNoEntries) {
|
||||||
return err
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Invalidate the cached move model with ID.
|
||||||
|
m.state.Caches.DB.Move.Invalidate("ID", id)
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,6 +22,7 @@
|
||||||
"errors"
|
"errors"
|
||||||
"slices"
|
"slices"
|
||||||
|
|
||||||
|
"github.com/superseriousbusiness/gotosocial/internal/db"
|
||||||
"github.com/superseriousbusiness/gotosocial/internal/gtscontext"
|
"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"
|
||||||
|
@ -107,15 +108,8 @@ func (n *notificationDB) GetNotificationsByIDs(ctx context.Context, ids []string
|
||||||
notifs, err := n.state.Caches.DB.Notification.LoadIDs("ID",
|
notifs, err := n.state.Caches.DB.Notification.LoadIDs("ID",
|
||||||
ids,
|
ids,
|
||||||
func(uncached []string) ([]*gtsmodel.Notification, error) {
|
func(uncached []string) ([]*gtsmodel.Notification, error) {
|
||||||
// Avoid querying
|
|
||||||
// if none uncached.
|
|
||||||
count := len(uncached)
|
|
||||||
if count == 0 {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Preallocate expected length of uncached notifications.
|
// Preallocate expected length of uncached notifications.
|
||||||
notifs := make([]*gtsmodel.Notification, 0, count)
|
notifs := make([]*gtsmodel.Notification, 0, len(uncached))
|
||||||
|
|
||||||
// Perform database query scanning
|
// Perform database query scanning
|
||||||
// the remaining (uncached) IDs.
|
// the remaining (uncached) IDs.
|
||||||
|
@ -299,7 +293,8 @@ func (n *notificationDB) DeleteNotificationByID(ctx context.Context, id string)
|
||||||
NewDelete().
|
NewDelete().
|
||||||
Table("notifications").
|
Table("notifications").
|
||||||
Where("? = ?", bun.Ident("id"), id).
|
Where("? = ?", bun.Ident("id"), id).
|
||||||
Exec(ctx); err != nil {
|
Exec(ctx); err != nil &&
|
||||||
|
!errors.Is(err, db.ErrNoEntries) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -310,7 +305,7 @@ func (n *notificationDB) DeleteNotificationByID(ctx context.Context, id string)
|
||||||
|
|
||||||
func (n *notificationDB) DeleteNotifications(ctx context.Context, types []string, targetAccountID string, originAccountID string) error {
|
func (n *notificationDB) DeleteNotifications(ctx context.Context, types []string, targetAccountID string, originAccountID string) error {
|
||||||
if targetAccountID == "" && originAccountID == "" {
|
if targetAccountID == "" && originAccountID == "" {
|
||||||
return errors.New("DeleteNotifications: one of targetAccountID or originAccountID must be set")
|
return gtserror.New("one of targetAccountID or originAccountID must be set")
|
||||||
}
|
}
|
||||||
|
|
||||||
q := n.db.
|
q := n.db.
|
||||||
|
|
|
@ -177,17 +177,36 @@ func (p *pollDB) UpdatePoll(ctx context.Context, poll *gtsmodel.Poll, cols ...st
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *pollDB) DeletePollByID(ctx context.Context, id string) error {
|
func (p *pollDB) DeletePollByID(ctx context.Context, id string) error {
|
||||||
// Delete poll by ID from database.
|
// Delete poll vote with ID, and its associated votes from the database.
|
||||||
if _, err := p.db.NewDelete().
|
if err := p.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
|
||||||
|
|
||||||
|
// Delete poll from database.
|
||||||
|
if _, err := tx.NewDelete().
|
||||||
Table("polls").
|
Table("polls").
|
||||||
Where("? = ?", bun.Ident("id"), id).
|
Where("? = ?", bun.Ident("id"), id).
|
||||||
Exec(ctx); err != nil {
|
Exec(ctx); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Invalidate poll by ID from cache.
|
// Delete the poll votes.
|
||||||
|
_, err := tx.NewDelete().
|
||||||
|
Table("poll_votes").
|
||||||
|
Where("? = ?", bun.Ident("poll_id"), id).
|
||||||
|
Exec(ctx)
|
||||||
|
return err
|
||||||
|
}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wrap provided ID in a poll
|
||||||
|
// model for calling cache hook.
|
||||||
|
var deleted gtsmodel.Poll
|
||||||
|
deleted.ID = id
|
||||||
|
|
||||||
|
// Invalidate cached poll with ID, manually
|
||||||
|
// call invalidate hook in case not cached.
|
||||||
p.state.Caches.DB.Poll.Invalidate("ID", id)
|
p.state.Caches.DB.Poll.Invalidate("ID", id)
|
||||||
p.state.Caches.DB.PollVoteIDs.Invalidate(id)
|
p.state.Caches.OnInvalidatePoll(&deleted)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -274,15 +293,8 @@ func (p *pollDB) GetPollVotes(ctx context.Context, pollID string) ([]*gtsmodel.P
|
||||||
votes, err := p.state.Caches.DB.PollVote.LoadIDs("ID",
|
votes, err := p.state.Caches.DB.PollVote.LoadIDs("ID",
|
||||||
voteIDs,
|
voteIDs,
|
||||||
func(uncached []string) ([]*gtsmodel.PollVote, error) {
|
func(uncached []string) ([]*gtsmodel.PollVote, error) {
|
||||||
// Avoid querying
|
|
||||||
// if none uncached.
|
|
||||||
count := len(uncached)
|
|
||||||
if count == 0 {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Preallocate expected length of uncached votes.
|
// Preallocate expected length of uncached votes.
|
||||||
votes := make([]*gtsmodel.PollVote, 0, count)
|
votes := make([]*gtsmodel.PollVote, 0, len(uncached))
|
||||||
|
|
||||||
// Perform database query scanning
|
// Perform database query scanning
|
||||||
// the remaining (uncached) IDs.
|
// the remaining (uncached) IDs.
|
||||||
|
@ -391,148 +403,44 @@ func (p *pollDB) PutPollVote(ctx context.Context, vote *gtsmodel.PollVote) error
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *pollDB) DeletePollVotes(ctx context.Context, pollID string) error {
|
|
||||||
err := p.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
|
|
||||||
// Delete all votes in poll.
|
|
||||||
res, err := tx.NewDelete().
|
|
||||||
Table("poll_votes").
|
|
||||||
Where("? = ?", bun.Ident("poll_id"), pollID).
|
|
||||||
Exec(ctx)
|
|
||||||
if err != nil {
|
|
||||||
// irrecoverable
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
ra, err := res.RowsAffected()
|
|
||||||
if err != nil {
|
|
||||||
// irrecoverable
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if ra == 0 {
|
|
||||||
// No poll votes deleted,
|
|
||||||
// nothing to update.
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Select current poll counts from DB,
|
|
||||||
// taking minimal columns needed to
|
|
||||||
// increment/decrement votes.
|
|
||||||
var poll gtsmodel.Poll
|
|
||||||
switch err := tx.NewSelect().
|
|
||||||
Model(&poll).
|
|
||||||
Column("options", "votes", "voters").
|
|
||||||
Where("? = ?", bun.Ident("id"), pollID).
|
|
||||||
Scan(ctx); {
|
|
||||||
|
|
||||||
case err == nil:
|
|
||||||
// no issue.
|
|
||||||
|
|
||||||
case errors.Is(err, db.ErrNoEntries):
|
|
||||||
// no votes found,
|
|
||||||
// return here.
|
|
||||||
return nil
|
|
||||||
|
|
||||||
default:
|
|
||||||
// irrecoverable.
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Zero all counts.
|
|
||||||
poll.ResetVotes()
|
|
||||||
|
|
||||||
// Finally, update the poll entry.
|
|
||||||
_, err = tx.NewUpdate().
|
|
||||||
Model(&poll).
|
|
||||||
Column("votes", "voters").
|
|
||||||
Where("? = ?", bun.Ident("id"), pollID).
|
|
||||||
Exec(ctx)
|
|
||||||
return err
|
|
||||||
})
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Invalidate poll vote and poll entry from caches.
|
|
||||||
p.state.Caches.DB.Poll.Invalidate("ID", pollID)
|
|
||||||
p.state.Caches.DB.PollVote.Invalidate("PollID", pollID)
|
|
||||||
p.state.Caches.DB.PollVoteIDs.Invalidate(pollID)
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *pollDB) DeletePollVoteBy(ctx context.Context, pollID string, accountID string) error {
|
func (p *pollDB) DeletePollVoteBy(ctx context.Context, pollID string, accountID string) error {
|
||||||
err := p.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
|
// Gather necessary fields from
|
||||||
// Slice should only ever be of length
|
// deleted for cache invaliation.
|
||||||
// 0 or 1; it's a slice of slices only
|
var deleted gtsmodel.PollVote
|
||||||
// because we can't LIMIT deletes to 1.
|
deleted.AccountID = accountID
|
||||||
var choicesSlice [][]int
|
deleted.PollID = pollID
|
||||||
|
|
||||||
|
// Delete the poll vote with given poll and account IDs, and update vote counts.
|
||||||
|
if err := p.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
|
||||||
|
|
||||||
// Delete vote in poll by account,
|
// Delete vote in poll by account,
|
||||||
// returning the ID + choices of the vote.
|
// returning deleted model info.
|
||||||
if err := tx.NewDelete().
|
switch _, err := tx.NewDelete().
|
||||||
Table("poll_votes").
|
Model(&deleted).
|
||||||
Where("? = ?", bun.Ident("poll_id"), pollID).
|
Where("? = ?", bun.Ident("poll_id"), pollID).
|
||||||
Where("? = ?", bun.Ident("account_id"), accountID).
|
Where("? = ?", bun.Ident("account_id"), accountID).
|
||||||
Returning("?", bun.Ident("choices")).
|
Returning("?", bun.Ident("choices")).
|
||||||
Scan(ctx, &choicesSlice); err != nil {
|
Exec(ctx); {
|
||||||
// irrecoverable.
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(choicesSlice) != 1 {
|
|
||||||
// No poll votes by this
|
|
||||||
// acct on this poll.
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Extract the *actual* choices.
|
|
||||||
choices := choicesSlice[0]
|
|
||||||
|
|
||||||
// Select current poll counts from DB,
|
|
||||||
// taking minimal columns needed to
|
|
||||||
// increment/decrement votes.
|
|
||||||
var poll gtsmodel.Poll
|
|
||||||
switch err := tx.NewSelect().
|
|
||||||
Model(&poll).
|
|
||||||
Column("options", "votes", "voters").
|
|
||||||
Where("? = ?", bun.Ident("id"), pollID).
|
|
||||||
Scan(ctx); {
|
|
||||||
|
|
||||||
case err == nil:
|
case err == nil:
|
||||||
// no issue.
|
// no issue
|
||||||
|
|
||||||
case errors.Is(err, db.ErrNoEntries):
|
case errors.Is(err, db.ErrNoEntries):
|
||||||
// no poll found,
|
|
||||||
// return here.
|
|
||||||
return nil
|
return nil
|
||||||
|
|
||||||
default:
|
default:
|
||||||
// irrecoverable.
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Decrement votes for choices.
|
// Update the votes for this deleted poll.
|
||||||
poll.DecrementVotes(choices)
|
err := updatePollCounts(ctx, tx, &deleted)
|
||||||
|
|
||||||
// Finally, update the poll entry.
|
|
||||||
_, err := tx.NewUpdate().
|
|
||||||
Model(&poll).
|
|
||||||
Column("votes", "voters").
|
|
||||||
Where("? = ?", bun.Ident("id"), pollID).
|
|
||||||
Exec(ctx)
|
|
||||||
return err
|
return err
|
||||||
})
|
}); err != nil {
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Invalidate poll vote and poll entry from caches.
|
// Invalidate the poll vote cache by given poll + account IDs, also
|
||||||
p.state.Caches.DB.Poll.Invalidate("ID", pollID)
|
// manually call invalidation hook in case not actually stored in cache.
|
||||||
p.state.Caches.DB.PollVote.Invalidate("PollID,AccountID", pollID, accountID)
|
p.state.Caches.DB.PollVote.Invalidate("PollID,AccountID", pollID, accountID)
|
||||||
p.state.Caches.DB.PollVoteIDs.Invalidate(pollID)
|
p.state.Caches.OnInvalidatePollVote(&deleted)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -562,6 +470,48 @@ func (p *pollDB) DeletePollVotesByAccountID(ctx context.Context, accountID strin
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// updatePollCounts updates the vote counts on a poll for the given deleted PollVote model.
|
||||||
|
func updatePollCounts(ctx context.Context, tx bun.Tx, deleted *gtsmodel.PollVote) error {
|
||||||
|
|
||||||
|
// Select current poll counts from DB,
|
||||||
|
// taking minimal columns needed to
|
||||||
|
// increment/decrement votes.
|
||||||
|
var poll gtsmodel.Poll
|
||||||
|
switch err := tx.NewSelect().
|
||||||
|
Model(&poll).
|
||||||
|
Column("options", "votes", "voters").
|
||||||
|
Where("? = ?", bun.Ident("id"), deleted.PollID).
|
||||||
|
Scan(ctx); {
|
||||||
|
|
||||||
|
case err == nil:
|
||||||
|
// no issue.
|
||||||
|
|
||||||
|
case errors.Is(err, db.ErrNoEntries):
|
||||||
|
// no poll found,
|
||||||
|
// return here.
|
||||||
|
return nil
|
||||||
|
|
||||||
|
default:
|
||||||
|
// irrecoverable.
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Decrement votes for these choices.
|
||||||
|
poll.DecrementVotes(deleted.Choices)
|
||||||
|
|
||||||
|
// Finally, update the poll entry.
|
||||||
|
if _, err := tx.NewUpdate().
|
||||||
|
Model(&poll).
|
||||||
|
Column("votes", "voters").
|
||||||
|
Where("? = ?", bun.Ident("id"), deleted.PollID).
|
||||||
|
Exec(ctx); err != nil &&
|
||||||
|
!errors.Is(err, db.ErrNoEntries) {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// newSelectPollVotes returns a new select query for all rows in the poll_votes table with poll_id = pollID.
|
// newSelectPollVotes returns a new select query for all rows in the poll_votes table with poll_id = pollID.
|
||||||
func newSelectPollVotes(db *bun.DB, pollID string) *bun.SelectQuery {
|
func newSelectPollVotes(db *bun.DB, pollID string) *bun.SelectQuery {
|
||||||
return db.NewSelect().
|
return db.NewSelect().
|
||||||
|
|
|
@ -26,7 +26,6 @@
|
||||||
|
|
||||||
"github.com/stretchr/testify/suite"
|
"github.com/stretchr/testify/suite"
|
||||||
"github.com/superseriousbusiness/gotosocial/internal/db"
|
"github.com/superseriousbusiness/gotosocial/internal/db"
|
||||||
"github.com/superseriousbusiness/gotosocial/internal/gtscontext"
|
|
||||||
"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"
|
"github.com/superseriousbusiness/gotosocial/internal/util"
|
||||||
|
@ -286,41 +285,6 @@ func (suite *PollTestSuite) TestDeletePoll() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (suite *PollTestSuite) TestDeletePollVotes() {
|
|
||||||
// Create a new context for this test.
|
|
||||||
ctx, cncl := context.WithCancel(context.Background())
|
|
||||||
defer cncl()
|
|
||||||
|
|
||||||
for _, poll := range suite.testPolls {
|
|
||||||
// Delete votes associated with poll from database.
|
|
||||||
err := suite.db.DeletePollVotes(ctx, poll.ID)
|
|
||||||
suite.NoError(err)
|
|
||||||
|
|
||||||
// Fetch latest version of poll from database.
|
|
||||||
poll, err = suite.db.GetPollByID(
|
|
||||||
gtscontext.SetBarebones(ctx),
|
|
||||||
poll.ID,
|
|
||||||
)
|
|
||||||
suite.NoError(err)
|
|
||||||
|
|
||||||
// Check that poll counts are all zero.
|
|
||||||
suite.Equal(*poll.Voters, 0)
|
|
||||||
suite.Equal(make([]int, len(poll.Options)), poll.Votes)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (suite *PollTestSuite) TestDeletePollVotesNoPoll() {
|
|
||||||
// Create a new context for this test.
|
|
||||||
ctx, cncl := context.WithCancel(context.Background())
|
|
||||||
defer cncl()
|
|
||||||
|
|
||||||
// Try to delete votes of nonexistent poll.
|
|
||||||
nonPollID := "01HF6V4XWTSZWJ80JNPPDTD4DB"
|
|
||||||
|
|
||||||
err := suite.db.DeletePollVotes(ctx, nonPollID)
|
|
||||||
suite.NoError(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (suite *PollTestSuite) TestDeletePollVotesBy() {
|
func (suite *PollTestSuite) TestDeletePollVotesBy() {
|
||||||
ctx, cncl := context.WithCancel(context.Background())
|
ctx, cncl := context.WithCancel(context.Background())
|
||||||
defer cncl()
|
defer cncl()
|
||||||
|
|
|
@ -105,15 +105,8 @@ func (r *relationshipDB) GetBlocksByIDs(ctx context.Context, ids []string) ([]*g
|
||||||
blocks, err := r.state.Caches.DB.Block.LoadIDs("ID",
|
blocks, err := r.state.Caches.DB.Block.LoadIDs("ID",
|
||||||
ids,
|
ids,
|
||||||
func(uncached []string) ([]*gtsmodel.Block, error) {
|
func(uncached []string) ([]*gtsmodel.Block, error) {
|
||||||
// Avoid querying
|
|
||||||
// if none uncached.
|
|
||||||
count := len(uncached)
|
|
||||||
if count == 0 {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Preallocate expected length of uncached blocks.
|
// Preallocate expected length of uncached blocks.
|
||||||
blocks := make([]*gtsmodel.Block, 0, count)
|
blocks := make([]*gtsmodel.Block, 0, len(uncached))
|
||||||
|
|
||||||
// Perform database query scanning
|
// Perform database query scanning
|
||||||
// the remaining (uncached) IDs.
|
// the remaining (uncached) IDs.
|
||||||
|
@ -222,94 +215,93 @@ func (r *relationshipDB) PutBlock(ctx context.Context, block *gtsmodel.Block) er
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *relationshipDB) DeleteBlockByID(ctx context.Context, id string) error {
|
func (r *relationshipDB) DeleteBlockByID(ctx context.Context, id string) error {
|
||||||
// Load block into cache before attempting a delete,
|
// Gather necessary fields from
|
||||||
// as we need it cached in order to trigger the invalidate
|
// deleted for cache invaliation.
|
||||||
// callback. This in turn invalidates others.
|
var deleted gtsmodel.Block
|
||||||
_, err := r.GetBlockByID(gtscontext.SetBarebones(ctx), id)
|
|
||||||
if err != nil {
|
|
||||||
if errors.Is(err, db.ErrNoEntries) {
|
|
||||||
// not an issue.
|
|
||||||
err = nil
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Drop this now-cached block on return after delete.
|
// Delete block with given ID,
|
||||||
defer r.state.Caches.DB.Block.Invalidate("ID", id)
|
// returning the deleted models.
|
||||||
|
if _, err := r.db.NewDelete().
|
||||||
// Finally delete block from DB.
|
Model(&deleted).
|
||||||
_, err = r.db.NewDelete().
|
|
||||||
Table("blocks").
|
|
||||||
Where("? = ?", bun.Ident("id"), id).
|
Where("? = ?", bun.Ident("id"), id).
|
||||||
Exec(ctx)
|
Returning("?, ?",
|
||||||
|
bun.Ident("account_id"),
|
||||||
|
bun.Ident("target_account_id"),
|
||||||
|
).
|
||||||
|
Exec(ctx); err != nil &&
|
||||||
|
!errors.Is(err, db.ErrNoEntries) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Invalidate cached block with ID, manually
|
||||||
|
// call invalidate hook in case not cached.
|
||||||
|
r.state.Caches.DB.Block.Invalidate("ID", id)
|
||||||
|
r.state.Caches.OnInvalidateBlock(&deleted)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (r *relationshipDB) DeleteBlockByURI(ctx context.Context, uri string) error {
|
func (r *relationshipDB) DeleteBlockByURI(ctx context.Context, uri string) error {
|
||||||
// Load block into cache before attempting a delete,
|
// Gather necessary fields from
|
||||||
// as we need it cached in order to trigger the invalidate
|
// deleted for cache invaliation.
|
||||||
// callback. This in turn invalidates others.
|
var deleted gtsmodel.Block
|
||||||
_, err := r.GetBlockByURI(gtscontext.SetBarebones(ctx), uri)
|
|
||||||
if err != nil {
|
|
||||||
if errors.Is(err, db.ErrNoEntries) {
|
|
||||||
// not an issue.
|
|
||||||
err = nil
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Drop this now-cached block on return after delete.
|
// Delete block with given URI,
|
||||||
defer r.state.Caches.DB.Block.Invalidate("URI", uri)
|
// returning the deleted models.
|
||||||
|
if _, err := r.db.NewDelete().
|
||||||
// Finally delete block from DB.
|
Model(&deleted).
|
||||||
_, err = r.db.NewDelete().
|
|
||||||
Table("blocks").
|
|
||||||
Where("? = ?", bun.Ident("uri"), uri).
|
Where("? = ?", bun.Ident("uri"), uri).
|
||||||
Exec(ctx)
|
Returning("?, ?",
|
||||||
|
bun.Ident("account_id"),
|
||||||
|
bun.Ident("target_account_id"),
|
||||||
|
).
|
||||||
|
Exec(ctx); err != nil &&
|
||||||
|
!errors.Is(err, db.ErrNoEntries) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Invalidate cached block with URI, manually
|
||||||
|
// call invalidate hook in case not cached.
|
||||||
|
r.state.Caches.DB.Block.Invalidate("URI", uri)
|
||||||
|
r.state.Caches.OnInvalidateBlock(&deleted)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (r *relationshipDB) DeleteAccountBlocks(ctx context.Context, accountID string) error {
|
func (r *relationshipDB) DeleteAccountBlocks(ctx context.Context, accountID string) error {
|
||||||
var blockIDs []string
|
// Gather necessary fields from
|
||||||
|
// deleted for cache invaliation.
|
||||||
|
var deleted []*gtsmodel.Block
|
||||||
|
|
||||||
// Get full list of IDs.
|
// Delete all blocks either from
|
||||||
if err := r.db.NewSelect().
|
// account, or targeting account,
|
||||||
Column("id").
|
// returning the deleted models.
|
||||||
Table("blocks").
|
if _, err := r.db.NewDelete().
|
||||||
|
Model(&deleted).
|
||||||
WhereOr("? = ? OR ? = ?",
|
WhereOr("? = ? OR ? = ?",
|
||||||
bun.Ident("account_id"),
|
bun.Ident("account_id"),
|
||||||
accountID,
|
accountID,
|
||||||
bun.Ident("target_account_id"),
|
bun.Ident("target_account_id"),
|
||||||
accountID,
|
accountID,
|
||||||
).
|
).
|
||||||
Scan(ctx, &blockIDs); err != nil {
|
Returning("?, ?",
|
||||||
|
bun.Ident("account_id"),
|
||||||
|
bun.Ident("target_account_id"),
|
||||||
|
).
|
||||||
|
Exec(ctx); err != nil &&
|
||||||
|
!errors.Is(err, db.ErrNoEntries) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(blockIDs) == 0 {
|
// Invalidate all account's incoming / outoing blocks.
|
||||||
// Nothing
|
|
||||||
// to delete.
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
defer func() {
|
|
||||||
// Invalidate all account's incoming / outoing blocks on return.
|
|
||||||
r.state.Caches.DB.Block.Invalidate("AccountID", accountID)
|
r.state.Caches.DB.Block.Invalidate("AccountID", accountID)
|
||||||
r.state.Caches.DB.Block.Invalidate("TargetAccountID", accountID)
|
r.state.Caches.DB.Block.Invalidate("TargetAccountID", accountID)
|
||||||
}()
|
|
||||||
|
|
||||||
// Load all blocks into cache, this *really* isn't great
|
// In case not all blocks were in
|
||||||
// but it is the only way we can ensure we invalidate all
|
// cache, manually call invalidate hooks.
|
||||||
// related caches correctly (e.g. visibility).
|
for _, block := range deleted {
|
||||||
_, err := r.GetAccountBlocks(ctx, accountID, nil)
|
r.state.Caches.OnInvalidateBlock(block)
|
||||||
if err != nil && !errors.Is(err, db.ErrNoEntries) {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Finally delete all from DB.
|
return nil
|
||||||
_, err = r.db.NewDelete().
|
|
||||||
Table("blocks").
|
|
||||||
Where("? IN (?)", bun.Ident("id"), bun.In(blockIDs)).
|
|
||||||
Exec(ctx)
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,7 +20,6 @@
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
"slices"
|
"slices"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -82,15 +81,8 @@ func (r *relationshipDB) GetFollowsByIDs(ctx context.Context, ids []string) ([]*
|
||||||
follows, err := r.state.Caches.DB.Follow.LoadIDs("ID",
|
follows, err := r.state.Caches.DB.Follow.LoadIDs("ID",
|
||||||
ids,
|
ids,
|
||||||
func(uncached []string) ([]*gtsmodel.Follow, error) {
|
func(uncached []string) ([]*gtsmodel.Follow, error) {
|
||||||
// Avoid querying
|
|
||||||
// if none uncached.
|
|
||||||
count := len(uncached)
|
|
||||||
if count == 0 {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Preallocate expected length of uncached follows.
|
// Preallocate expected length of uncached follows.
|
||||||
follows := make([]*gtsmodel.Follow, 0, count)
|
follows := make([]*gtsmodel.Follow, 0, len(uncached))
|
||||||
|
|
||||||
// Perform database query scanning
|
// Perform database query scanning
|
||||||
// the remaining (uncached) IDs.
|
// the remaining (uncached) IDs.
|
||||||
|
@ -252,139 +244,155 @@ func (r *relationshipDB) UpdateFollow(ctx context.Context, follow *gtsmodel.Foll
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *relationshipDB) deleteFollow(ctx context.Context, id string) error {
|
func (r *relationshipDB) DeleteFollow(
|
||||||
// Delete the follow itself using the given ID.
|
ctx context.Context,
|
||||||
|
sourceAccountID string,
|
||||||
|
targetAccountID string,
|
||||||
|
) error {
|
||||||
|
|
||||||
|
// Gather necessary fields from
|
||||||
|
// deleted for cache invaliation.
|
||||||
|
var deleted gtsmodel.Follow
|
||||||
|
deleted.AccountID = sourceAccountID
|
||||||
|
deleted.TargetAccountID = targetAccountID
|
||||||
|
|
||||||
|
// Delete follow from origin
|
||||||
|
// account, to targeting account,
|
||||||
|
// returning the deleted models.
|
||||||
if _, err := r.db.NewDelete().
|
if _, err := r.db.NewDelete().
|
||||||
Table("follows").
|
Model(&deleted).
|
||||||
Where("? = ?", bun.Ident("id"), id).
|
Where("? = ?", bun.Ident("account_id"), sourceAccountID).
|
||||||
Exec(ctx); err != nil {
|
Where("? = ?", bun.Ident("target_account_id"), targetAccountID).
|
||||||
|
Returning("?", bun.Ident("id")).
|
||||||
|
Exec(ctx); err != nil &&
|
||||||
|
!errors.Is(err, db.ErrNoEntries) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete every list entry that used this followID.
|
// Invalidate cached follow with source / target account IDs,
|
||||||
if err := r.state.DB.DeleteListEntriesForFollowID(ctx, id); err != nil {
|
// manually calling invalidate hook in case it isn't cached.
|
||||||
return fmt.Errorf("deleteFollow: error deleting list entries: %w", err)
|
r.state.Caches.DB.Follow.Invalidate("AccountID,TargetAccountID",
|
||||||
|
sourceAccountID, targetAccountID)
|
||||||
|
r.state.Caches.OnInvalidateFollow(&deleted)
|
||||||
|
|
||||||
|
// Delete every list entry that was created targetting this follow ID.
|
||||||
|
if err := r.state.DB.DeleteAllListEntriesByFollows(ctx, deleted.ID); err != nil {
|
||||||
|
return gtserror.Newf("error deleting list entries: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *relationshipDB) DeleteFollow(ctx context.Context, sourceAccountID string, targetAccountID string) error {
|
|
||||||
// Load follow into cache before attempting a delete,
|
|
||||||
// as we need it cached in order to trigger the invalidate
|
|
||||||
// callback. This in turn invalidates others.
|
|
||||||
follow, err := r.GetFollow(
|
|
||||||
gtscontext.SetBarebones(ctx),
|
|
||||||
sourceAccountID,
|
|
||||||
targetAccountID,
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
if errors.Is(err, db.ErrNoEntries) {
|
|
||||||
// Already gone.
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Drop this now-cached follow on return after delete.
|
|
||||||
defer r.state.Caches.DB.Follow.Invalidate("AccountID,TargetAccountID", sourceAccountID, targetAccountID)
|
|
||||||
|
|
||||||
// Finally delete follow from DB.
|
|
||||||
return r.deleteFollow(ctx, follow.ID)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *relationshipDB) DeleteFollowByID(ctx context.Context, id string) error {
|
func (r *relationshipDB) DeleteFollowByID(ctx context.Context, id string) error {
|
||||||
// Load follow into cache before attempting a delete,
|
// Gather necessary fields from
|
||||||
// as we need it cached in order to trigger the invalidate
|
// deleted for cache invaliation.
|
||||||
// callback. This in turn invalidates others.
|
var deleted gtsmodel.Follow
|
||||||
follow, err := r.GetFollowByID(gtscontext.SetBarebones(ctx), id)
|
deleted.ID = id
|
||||||
if err != nil {
|
|
||||||
if errors.Is(err, db.ErrNoEntries) {
|
// Delete follow with given ID,
|
||||||
// Already gone.
|
// returning the deleted models.
|
||||||
return nil
|
if _, err := r.db.NewDelete().
|
||||||
}
|
Model(&deleted).
|
||||||
|
Where("? = ?", bun.Ident("id"), id).
|
||||||
|
Returning("?, ?",
|
||||||
|
bun.Ident("account_id"),
|
||||||
|
bun.Ident("target_account_id"),
|
||||||
|
).
|
||||||
|
Exec(ctx); err != nil &&
|
||||||
|
!errors.Is(err, db.ErrNoEntries) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Drop this now-cached follow on return after delete.
|
// Invalidate cached follow with ID, manually
|
||||||
defer r.state.Caches.DB.Follow.Invalidate("ID", id)
|
// call invalidate hook in case not cached.
|
||||||
|
r.state.Caches.DB.Follow.Invalidate("ID", id)
|
||||||
|
r.state.Caches.OnInvalidateFollow(&deleted)
|
||||||
|
|
||||||
// Finally delete follow from DB.
|
// Delete every list entry that was created targetting this follow ID.
|
||||||
return r.deleteFollow(ctx, follow.ID)
|
if err := r.state.DB.DeleteAllListEntriesByFollows(ctx, id); err != nil {
|
||||||
|
return gtserror.Newf("error deleting list entries: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *relationshipDB) DeleteFollowByURI(ctx context.Context, uri string) error {
|
func (r *relationshipDB) DeleteFollowByURI(ctx context.Context, uri string) error {
|
||||||
// Load follow into cache before attempting a delete,
|
// Gather necessary fields from
|
||||||
// as we need it cached in order to trigger the invalidate
|
// deleted for cache invaliation.
|
||||||
// callback. This in turn invalidates others.
|
var deleted gtsmodel.Follow
|
||||||
follow, err := r.GetFollowByURI(gtscontext.SetBarebones(ctx), uri)
|
|
||||||
if err != nil {
|
// Delete follow with given URI,
|
||||||
if errors.Is(err, db.ErrNoEntries) {
|
// returning the deleted models.
|
||||||
// Already gone.
|
if _, err := r.db.NewDelete().
|
||||||
return nil
|
Model(&deleted).
|
||||||
}
|
Where("? = ?", bun.Ident("uri"), uri).
|
||||||
|
Returning("?, ?, ?",
|
||||||
|
bun.Ident("id"),
|
||||||
|
bun.Ident("account_id"),
|
||||||
|
bun.Ident("target_account_id"),
|
||||||
|
).
|
||||||
|
Exec(ctx); err != nil &&
|
||||||
|
!errors.Is(err, db.ErrNoEntries) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Drop this now-cached follow on return after delete.
|
// Invalidate cached follow with URI, manually
|
||||||
defer r.state.Caches.DB.Follow.Invalidate("URI", uri)
|
// call invalidate hook in case not cached.
|
||||||
|
r.state.Caches.DB.Follow.Invalidate("URI", uri)
|
||||||
|
r.state.Caches.OnInvalidateFollow(&deleted)
|
||||||
|
|
||||||
// Finally delete follow from DB.
|
// Delete every list entry that was created targetting this follow ID.
|
||||||
return r.deleteFollow(ctx, follow.ID)
|
if err := r.state.DB.DeleteAllListEntriesByFollows(ctx, deleted.ID); err != nil {
|
||||||
|
return gtserror.Newf("error deleting list entries: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *relationshipDB) DeleteAccountFollows(ctx context.Context, accountID string) error {
|
func (r *relationshipDB) DeleteAccountFollows(ctx context.Context, accountID string) error {
|
||||||
var followIDs []string
|
// Gather necessary fields from
|
||||||
|
// deleted for cache invaliation.
|
||||||
|
var deleted []*gtsmodel.Follow
|
||||||
|
|
||||||
// Get full list of IDs.
|
// Delete all follows either from
|
||||||
if _, err := r.db.
|
// account, or targeting account,
|
||||||
NewSelect().
|
// returning the deleted models.
|
||||||
Column("id").
|
if _, err := r.db.NewDelete().
|
||||||
Table("follows").
|
Model(&deleted).
|
||||||
WhereOr("? = ? OR ? = ?",
|
WhereOr("? = ? OR ? = ?",
|
||||||
bun.Ident("account_id"),
|
bun.Ident("account_id"),
|
||||||
accountID,
|
accountID,
|
||||||
bun.Ident("target_account_id"),
|
bun.Ident("target_account_id"),
|
||||||
accountID,
|
accountID,
|
||||||
).
|
).
|
||||||
Exec(ctx, &followIDs); err != nil {
|
Returning("?, ?, ?",
|
||||||
|
bun.Ident("id"),
|
||||||
|
bun.Ident("account_id"),
|
||||||
|
bun.Ident("target_account_id"),
|
||||||
|
).
|
||||||
|
Exec(ctx); err != nil &&
|
||||||
|
!errors.Is(err, db.ErrNoEntries) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(followIDs) == 0 {
|
// Gather the follow IDs that were deleted for removing related list entries.
|
||||||
// Nothing
|
followIDs := util.Gather(nil, deleted, func(follow *gtsmodel.Follow) string {
|
||||||
// to delete.
|
return follow.ID
|
||||||
return nil
|
})
|
||||||
|
|
||||||
|
// Delete every list entry that was created targetting any of these follow IDs.
|
||||||
|
if err := r.state.DB.DeleteAllListEntriesByFollows(ctx, followIDs...); err != nil {
|
||||||
|
return gtserror.Newf("error deleting list entries: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
defer func() {
|
// Invalidate all account's incoming / outoing follows.
|
||||||
// Invalidate all account's incoming / outoing follows on return.
|
|
||||||
r.state.Caches.DB.Follow.Invalidate("AccountID", accountID)
|
r.state.Caches.DB.Follow.Invalidate("AccountID", accountID)
|
||||||
r.state.Caches.DB.Follow.Invalidate("TargetAccountID", accountID)
|
r.state.Caches.DB.Follow.Invalidate("TargetAccountID", accountID)
|
||||||
}()
|
|
||||||
|
|
||||||
// Load all follows into cache, this *really* isn't great
|
// In case not all follow were in
|
||||||
// but it is the only way we can ensure we invalidate all
|
// cache, manually call invalidate hooks.
|
||||||
// related caches correctly (e.g. visibility).
|
for _, follow := range deleted {
|
||||||
_, err := r.GetAccountFollows(ctx, accountID, nil)
|
r.state.Caches.OnInvalidateFollow(follow)
|
||||||
if err != nil && !errors.Is(err, db.ErrNoEntries) {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Delete all follows from DB.
|
|
||||||
_, err = r.db.NewDelete().
|
|
||||||
Table("follows").
|
|
||||||
Where("? IN (?)", bun.Ident("id"), bun.In(followIDs)).
|
|
||||||
Exec(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, id := range followIDs {
|
|
||||||
// Finally, delete all list entries associated with each follow ID.
|
|
||||||
if err := r.state.DB.DeleteListEntriesForFollowID(ctx, id); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -81,15 +81,8 @@ func (r *relationshipDB) GetFollowRequestsByIDs(ctx context.Context, ids []strin
|
||||||
follows, err := r.state.Caches.DB.FollowRequest.LoadIDs("ID",
|
follows, err := r.state.Caches.DB.FollowRequest.LoadIDs("ID",
|
||||||
ids,
|
ids,
|
||||||
func(uncached []string) ([]*gtsmodel.FollowRequest, error) {
|
func(uncached []string) ([]*gtsmodel.FollowRequest, error) {
|
||||||
// Avoid querying
|
|
||||||
// if none uncached.
|
|
||||||
count := len(uncached)
|
|
||||||
if count == 0 {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Preallocate expected length of uncached followReqs.
|
// Preallocate expected length of uncached followReqs.
|
||||||
follows := make([]*gtsmodel.FollowRequest, 0, count)
|
follows := make([]*gtsmodel.FollowRequest, 0, len(uncached))
|
||||||
|
|
||||||
// Perform database query scanning
|
// Perform database query scanning
|
||||||
// the remaining (uncached) IDs.
|
// the remaining (uncached) IDs.
|
||||||
|
@ -293,124 +286,131 @@ func (r *relationshipDB) RejectFollowRequest(ctx context.Context, sourceAccountI
|
||||||
}, targetAccountID, sourceAccountID)
|
}, targetAccountID, sourceAccountID)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *relationshipDB) DeleteFollowRequest(ctx context.Context, sourceAccountID string, targetAccountID string) error {
|
func (r *relationshipDB) DeleteFollowRequest(
|
||||||
// Load followreq into cache before attempting a delete,
|
ctx context.Context,
|
||||||
// as we need it cached in order to trigger the invalidate
|
sourceAccountID string,
|
||||||
// callback. This in turn invalidates others.
|
targetAccountID string,
|
||||||
follow, err := r.GetFollowRequest(
|
) error {
|
||||||
gtscontext.SetBarebones(ctx),
|
|
||||||
sourceAccountID,
|
// Gather necessary fields from
|
||||||
targetAccountID,
|
// deleted for cache invaliation.
|
||||||
)
|
var deleted gtsmodel.FollowRequest
|
||||||
if err != nil {
|
deleted.AccountID = sourceAccountID
|
||||||
if errors.Is(err, db.ErrNoEntries) {
|
deleted.TargetAccountID = targetAccountID
|
||||||
// Already gone.
|
|
||||||
|
// Delete all follow reqs either
|
||||||
|
// from account, or targeting account,
|
||||||
|
// returning the deleted models.
|
||||||
|
if _, err := r.db.NewDelete().
|
||||||
|
Model(&deleted).
|
||||||
|
Where("? = ?", bun.Ident("account_id"), sourceAccountID).
|
||||||
|
Where("? = ?", bun.Ident("target_account_id"), targetAccountID).
|
||||||
|
Returning("?", bun.Ident("id")).
|
||||||
|
Exec(ctx); err != nil &&
|
||||||
|
!errors.Is(err, db.ErrNoEntries) {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Invalidate cached follow with source / target account IDs,
|
||||||
|
// manually calling invalidate hook in case it isn't cached.
|
||||||
|
r.state.Caches.DB.FollowRequest.Invalidate("AccountID,TargetAccountID",
|
||||||
|
sourceAccountID, targetAccountID)
|
||||||
|
r.state.Caches.OnInvalidateFollowRequest(&deleted)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Drop this now-cached follow request on return after delete.
|
|
||||||
defer r.state.Caches.DB.FollowRequest.Invalidate("AccountID,TargetAccountID", sourceAccountID, targetAccountID)
|
|
||||||
|
|
||||||
// Finally delete followreq from DB.
|
|
||||||
_, err = r.db.NewDelete().
|
|
||||||
Table("follow_requests").
|
|
||||||
Where("? = ?", bun.Ident("id"), follow.ID).
|
|
||||||
Exec(ctx)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *relationshipDB) DeleteFollowRequestByID(ctx context.Context, id string) error {
|
func (r *relationshipDB) DeleteFollowRequestByID(ctx context.Context, id string) error {
|
||||||
// Load followreq into cache before attempting a delete,
|
// Gather necessary fields from
|
||||||
// as we need it cached in order to trigger the invalidate
|
// deleted for cache invaliation.
|
||||||
// callback. This in turn invalidates others.
|
var deleted gtsmodel.FollowRequest
|
||||||
_, err := r.GetFollowRequestByID(gtscontext.SetBarebones(ctx), id)
|
deleted.ID = id
|
||||||
if err != nil {
|
|
||||||
if errors.Is(err, db.ErrNoEntries) {
|
|
||||||
// not an issue.
|
|
||||||
err = nil
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Drop this now-cached follow request on return after delete.
|
// Delete follow with given URI,
|
||||||
defer r.state.Caches.DB.FollowRequest.Invalidate("ID", id)
|
// returning the deleted models.
|
||||||
|
if _, err := r.db.NewDelete().
|
||||||
// Finally delete followreq from DB.
|
Model(&deleted).
|
||||||
_, err = r.db.NewDelete().
|
|
||||||
Table("follow_requests").
|
|
||||||
Where("? = ?", bun.Ident("id"), id).
|
Where("? = ?", bun.Ident("id"), id).
|
||||||
Exec(ctx)
|
Returning("?, ?",
|
||||||
|
bun.Ident("account_id"),
|
||||||
|
bun.Ident("target_account_id"),
|
||||||
|
).
|
||||||
|
Exec(ctx); err != nil &&
|
||||||
|
!errors.Is(err, db.ErrNoEntries) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Invalidate cached follow with URI, manually
|
||||||
|
// call invalidate hook in case not cached.
|
||||||
|
r.state.Caches.DB.FollowRequest.Invalidate("ID", id)
|
||||||
|
r.state.Caches.OnInvalidateFollowRequest(&deleted)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (r *relationshipDB) DeleteFollowRequestByURI(ctx context.Context, uri string) error {
|
func (r *relationshipDB) DeleteFollowRequestByURI(ctx context.Context, uri string) error {
|
||||||
// Load followreq into cache before attempting a delete,
|
// Gather necessary fields from
|
||||||
// as we need it cached in order to trigger the invalidate
|
// deleted for cache invaliation.
|
||||||
// callback. This in turn invalidates others.
|
var deleted gtsmodel.FollowRequest
|
||||||
_, err := r.GetFollowRequestByURI(gtscontext.SetBarebones(ctx), uri)
|
|
||||||
if err != nil {
|
|
||||||
if errors.Is(err, db.ErrNoEntries) {
|
|
||||||
// not an issue.
|
|
||||||
err = nil
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Drop this now-cached follow request on return after delete.
|
// Delete follow with given URI,
|
||||||
defer r.state.Caches.DB.FollowRequest.Invalidate("URI", uri)
|
// returning the deleted models.
|
||||||
|
if _, err := r.db.NewDelete().
|
||||||
// Finally delete followreq from DB.
|
Model(&deleted).
|
||||||
_, err = r.db.NewDelete().
|
|
||||||
Table("follow_requests").
|
|
||||||
Where("? = ?", bun.Ident("uri"), uri).
|
Where("? = ?", bun.Ident("uri"), uri).
|
||||||
Exec(ctx)
|
Returning("?, ?, ?",
|
||||||
|
bun.Ident("id"),
|
||||||
|
bun.Ident("account_id"),
|
||||||
|
bun.Ident("target_account_id"),
|
||||||
|
).
|
||||||
|
Exec(ctx); err != nil &&
|
||||||
|
!errors.Is(err, db.ErrNoEntries) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Invalidate cached follow with URI, manually
|
||||||
|
// call invalidate hook in case not cached.
|
||||||
|
r.state.Caches.DB.FollowRequest.Invalidate("URI", uri)
|
||||||
|
r.state.Caches.OnInvalidateFollowRequest(&deleted)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (r *relationshipDB) DeleteAccountFollowRequests(ctx context.Context, accountID string) error {
|
func (r *relationshipDB) DeleteAccountFollowRequests(ctx context.Context, accountID string) error {
|
||||||
var followReqIDs []string
|
// Gather necessary fields from
|
||||||
|
// deleted for cache invaliation.
|
||||||
|
var deleted []*gtsmodel.FollowRequest
|
||||||
|
|
||||||
// Get full list of IDs.
|
// Delete all follows either from
|
||||||
if _, err := r.db.
|
// account, or targeting account,
|
||||||
NewSelect().
|
// returning the deleted models.
|
||||||
Column("id").
|
if _, err := r.db.NewDelete().
|
||||||
Table("follow_requests").
|
Model(&deleted).
|
||||||
WhereOr("? = ? OR ? = ?",
|
WhereOr("? = ? OR ? = ?",
|
||||||
bun.Ident("account_id"),
|
bun.Ident("account_id"),
|
||||||
accountID,
|
accountID,
|
||||||
bun.Ident("target_account_id"),
|
bun.Ident("target_account_id"),
|
||||||
accountID,
|
accountID,
|
||||||
).
|
).
|
||||||
Exec(ctx, &followReqIDs); err != nil {
|
Returning("?, ?, ?",
|
||||||
|
bun.Ident("id"),
|
||||||
|
bun.Ident("account_id"),
|
||||||
|
bun.Ident("target_account_id"),
|
||||||
|
).
|
||||||
|
Exec(ctx); err != nil &&
|
||||||
|
!errors.Is(err, db.ErrNoEntries) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(followReqIDs) == 0 {
|
// Invalidate all account's incoming / outoing follows requests.
|
||||||
// Nothing
|
|
||||||
// to delete.
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
defer func() {
|
|
||||||
// Invalidate all account's incoming / outoing follow requests on return.
|
|
||||||
r.state.Caches.DB.FollowRequest.Invalidate("AccountID", accountID)
|
r.state.Caches.DB.FollowRequest.Invalidate("AccountID", accountID)
|
||||||
r.state.Caches.DB.FollowRequest.Invalidate("TargetAccountID", accountID)
|
r.state.Caches.DB.FollowRequest.Invalidate("TargetAccountID", accountID)
|
||||||
}()
|
|
||||||
|
|
||||||
// Load all followreqs into cache, this *really* isn't
|
// In case not all follow were in
|
||||||
// great but it is the only way we can ensure we invalidate
|
// cache, manually call invalidate hooks.
|
||||||
// all related caches correctly (e.g. visibility).
|
for _, followReq := range deleted {
|
||||||
_, err := r.GetAccountFollowRequests(ctx, accountID, nil)
|
r.state.Caches.OnInvalidateFollowRequest(followReq)
|
||||||
if err != nil && !errors.Is(err, db.ErrNoEntries) {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Finally delete all from DB.
|
return nil
|
||||||
_, err = r.db.NewDelete().
|
|
||||||
Table("follow_requests").
|
|
||||||
Where("? IN (?)", bun.Ident("id"), bun.In(followReqIDs)).
|
|
||||||
Exec(ctx)
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -87,15 +87,8 @@ func (r *relationshipDB) getMutesByIDs(ctx context.Context, ids []string) ([]*gt
|
||||||
mutes, err := r.state.Caches.DB.UserMute.LoadIDs("ID",
|
mutes, err := r.state.Caches.DB.UserMute.LoadIDs("ID",
|
||||||
ids,
|
ids,
|
||||||
func(uncached []string) ([]*gtsmodel.UserMute, error) {
|
func(uncached []string) ([]*gtsmodel.UserMute, error) {
|
||||||
// Avoid querying
|
|
||||||
// if none uncached.
|
|
||||||
count := len(uncached)
|
|
||||||
if count == 0 {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Preallocate expected length of uncached mutes.
|
// Preallocate expected length of uncached mutes.
|
||||||
mutes := make([]*gtsmodel.UserMute, 0, count)
|
mutes := make([]*gtsmodel.UserMute, 0, len(uncached))
|
||||||
|
|
||||||
// Perform database query scanning
|
// Perform database query scanning
|
||||||
// the remaining (uncached) IDs.
|
// the remaining (uncached) IDs.
|
||||||
|
@ -209,72 +202,64 @@ func (r *relationshipDB) PutMute(ctx context.Context, mute *gtsmodel.UserMute) e
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *relationshipDB) DeleteMuteByID(ctx context.Context, id string) error {
|
func (r *relationshipDB) DeleteMuteByID(ctx context.Context, id string) error {
|
||||||
// Load mute into cache before attempting a delete,
|
// Gather necessary fields from
|
||||||
// as we need it cached in order to trigger the invalidate
|
// deleted for cache invaliation.
|
||||||
// callback. This in turn invalidates others.
|
var deleted gtsmodel.UserMute
|
||||||
_, err := r.GetMuteByID(gtscontext.SetBarebones(ctx), id)
|
|
||||||
if err != nil {
|
|
||||||
if errors.Is(err, db.ErrNoEntries) {
|
|
||||||
// not an issue.
|
|
||||||
err = nil
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Drop this now-cached mute on return after delete.
|
// Delete mute with given ID,
|
||||||
defer r.state.Caches.DB.UserMute.Invalidate("ID", id)
|
// returning the deleted models.
|
||||||
|
if _, err := r.db.NewDelete().
|
||||||
// Finally delete mute from DB.
|
Model(&deleted).
|
||||||
_, err = r.db.NewDelete().
|
|
||||||
Table("user_mutes").
|
|
||||||
Where("? = ?", bun.Ident("id"), id).
|
Where("? = ?", bun.Ident("id"), id).
|
||||||
Exec(ctx)
|
Returning("?", bun.Ident("account_id")).
|
||||||
|
Exec(ctx); err != nil &&
|
||||||
|
!errors.Is(err, db.ErrNoEntries) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Invalidate cached mute with ID, manually
|
||||||
|
// call invalidate hook in case not cached.
|
||||||
|
r.state.Caches.DB.UserMute.Invalidate("ID", id)
|
||||||
|
r.state.Caches.OnInvalidateUserMute(&deleted)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (r *relationshipDB) DeleteAccountMutes(ctx context.Context, accountID string) error {
|
func (r *relationshipDB) DeleteAccountMutes(ctx context.Context, accountID string) error {
|
||||||
var muteIDs []string
|
// Gather necessary fields from
|
||||||
|
// deleted for cache invaliation.
|
||||||
|
var deleted []*gtsmodel.UserMute
|
||||||
|
|
||||||
// Get full list of IDs.
|
// Delete all mutes either from
|
||||||
if err := r.db.NewSelect().
|
// account, or targeting account,
|
||||||
Column("id").
|
// returning the deleted models.
|
||||||
Table("user_mutes").
|
if _, err := r.db.NewDelete().
|
||||||
|
Model(&deleted).
|
||||||
WhereOr("? = ? OR ? = ?",
|
WhereOr("? = ? OR ? = ?",
|
||||||
bun.Ident("account_id"),
|
bun.Ident("account_id"),
|
||||||
accountID,
|
accountID,
|
||||||
bun.Ident("target_account_id"),
|
bun.Ident("target_account_id"),
|
||||||
accountID,
|
accountID,
|
||||||
).
|
).
|
||||||
Scan(ctx, &muteIDs); err != nil {
|
Returning("?",
|
||||||
|
bun.Ident("account_id"),
|
||||||
|
).
|
||||||
|
Exec(ctx); err != nil &&
|
||||||
|
!errors.Is(err, db.ErrNoEntries) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(muteIDs) == 0 {
|
// Invalidate all account's incoming / outoing user mutes.
|
||||||
// Nothing
|
|
||||||
// to delete.
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
defer func() {
|
|
||||||
// Invalidate all account's incoming / outoing mutes on return.
|
|
||||||
r.state.Caches.DB.UserMute.Invalidate("AccountID", accountID)
|
r.state.Caches.DB.UserMute.Invalidate("AccountID", accountID)
|
||||||
r.state.Caches.DB.UserMute.Invalidate("TargetAccountID", accountID)
|
r.state.Caches.DB.UserMute.Invalidate("TargetAccountID", accountID)
|
||||||
}()
|
|
||||||
|
|
||||||
// Load all mutes into cache, this *really* isn't great
|
// In case not all user mutes were in
|
||||||
// but it is the only way we can ensure we invalidate all
|
// cache, manually call invalidate hooks.
|
||||||
// related caches correctly (e.g. visibility).
|
for _, block := range deleted {
|
||||||
_, err := r.GetAccountMutes(ctx, accountID, nil)
|
r.state.Caches.OnInvalidateUserMute(block)
|
||||||
if err != nil && !errors.Is(err, db.ErrNoEntries) {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Finally delete all from DB.
|
return nil
|
||||||
_, err = r.db.NewDelete().
|
|
||||||
Table("user_mutes").
|
|
||||||
Where("? IN (?)", bun.Ident("id"), bun.In(muteIDs)).
|
|
||||||
Exec(ctx)
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *relationshipDB) GetAccountMutes(
|
func (r *relationshipDB) GetAccountMutes(
|
||||||
|
|
|
@ -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.GetListsContainingFollowID(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.GetListsContainingFollowID(context.Background(), followID)
|
||||||
suite.NoError(err)
|
suite.NoError(err)
|
||||||
suite.Empty(listEntries)
|
suite.Empty(lists)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (suite *RelationshipTestSuite) TestGetFollowNotExisting() {
|
func (suite *RelationshipTestSuite) TestGetFollowNotExisting() {
|
||||||
|
|
|
@ -248,45 +248,36 @@ func (r *reportDB) PutReport(ctx context.Context, report *gtsmodel.Report) error
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *reportDB) UpdateReport(ctx context.Context, report *gtsmodel.Report, columns ...string) (*gtsmodel.Report, error) {
|
func (r *reportDB) UpdateReport(ctx context.Context, report *gtsmodel.Report, columns ...string) error {
|
||||||
// Update the report's last-updated
|
// Update the report's last-updated
|
||||||
report.UpdatedAt = time.Now()
|
report.UpdatedAt = time.Now()
|
||||||
if len(columns) != 0 {
|
if len(columns) != 0 {
|
||||||
columns = append(columns, "updated_at")
|
columns = append(columns, "updated_at")
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := r.db.
|
return r.state.Caches.DB.Report.Store(report, func() error {
|
||||||
|
_, err := r.db.
|
||||||
NewUpdate().
|
NewUpdate().
|
||||||
Model(report).
|
Model(report).
|
||||||
Where("? = ?", bun.Ident("report.id"), report.ID).
|
Where("? = ?", bun.Ident("report.id"), report.ID).
|
||||||
Column(columns...).
|
Column(columns...).
|
||||||
Exec(ctx); err != nil {
|
Exec(ctx)
|
||||||
return nil, err
|
return err
|
||||||
}
|
})
|
||||||
|
|
||||||
r.state.Caches.DB.Report.Invalidate("ID", report.ID)
|
|
||||||
return report, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *reportDB) DeleteReportByID(ctx context.Context, id string) error {
|
func (r *reportDB) DeleteReportByID(ctx context.Context, id string) error {
|
||||||
defer r.state.Caches.DB.Report.Invalidate("ID", id)
|
// Delete the report from DB.
|
||||||
|
if _, err := r.db.NewDelete().
|
||||||
// Load status into cache before attempting a delete,
|
|
||||||
// as we need it cached in order to trigger the invalidate
|
|
||||||
// callback. This in turn invalidates others.
|
|
||||||
_, err := r.GetReportByID(gtscontext.SetBarebones(ctx), id)
|
|
||||||
if err != nil {
|
|
||||||
if errors.Is(err, db.ErrNoEntries) {
|
|
||||||
// not an issue.
|
|
||||||
err = nil
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Finally delete report from DB.
|
|
||||||
_, err = r.db.NewDelete().
|
|
||||||
TableExpr("? AS ?", bun.Ident("reports"), bun.Ident("report")).
|
TableExpr("? AS ?", bun.Ident("reports"), bun.Ident("report")).
|
||||||
Where("? = ?", bun.Ident("report.id"), id).
|
Where("? = ?", bun.Ident("report.id"), id).
|
||||||
Exec(ctx)
|
Exec(ctx); err != nil &&
|
||||||
|
!errors.Is(err, db.ErrNoEntries) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Invalidate any cached report model by ID.
|
||||||
|
r.state.Caches.DB.Report.Invalidate("ID", id)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
|
@ -202,7 +202,7 @@ func (suite *ReportTestSuite) TestUpdateReport() {
|
||||||
report.ActionTakenByAccountID = suite.testAccounts["admin_account"].ID
|
report.ActionTakenByAccountID = suite.testAccounts["admin_account"].ID
|
||||||
report.ActionTakenAt = testrig.TimeMustParse("2022-05-14T12:20:03+02:00")
|
report.ActionTakenAt = testrig.TimeMustParse("2022-05-14T12:20:03+02:00")
|
||||||
|
|
||||||
if _, err := suite.db.UpdateReport(ctx, report, "action_taken", "action_taken_by_account_id", "action_taken_at"); err != nil {
|
if err := suite.db.UpdateReport(ctx, report, "action_taken", "action_taken_by_account_id", "action_taken_at"); err != nil {
|
||||||
suite.FailNow(err.Error())
|
suite.FailNow(err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -228,7 +228,7 @@ func (suite *ReportTestSuite) TestUpdateReportAllColumns() {
|
||||||
report.ActionTakenByAccountID = suite.testAccounts["admin_account"].ID
|
report.ActionTakenByAccountID = suite.testAccounts["admin_account"].ID
|
||||||
report.ActionTakenAt = testrig.TimeMustParse("2022-05-14T12:20:03+02:00")
|
report.ActionTakenAt = testrig.TimeMustParse("2022-05-14T12:20:03+02:00")
|
||||||
|
|
||||||
if _, err := suite.db.UpdateReport(ctx, report); err != nil {
|
if err := suite.db.UpdateReport(ctx, report); err != nil {
|
||||||
suite.FailNow(err.Error())
|
suite.FailNow(err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -19,8 +19,10 @@
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/superseriousbusiness/gotosocial/internal/db"
|
||||||
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
||||||
"github.com/superseriousbusiness/gotosocial/internal/state"
|
"github.com/superseriousbusiness/gotosocial/internal/state"
|
||||||
"github.com/uptrace/bun"
|
"github.com/uptrace/bun"
|
||||||
|
@ -110,13 +112,18 @@ func (s *sinBinStatusDB) UpdateSinBinStatus(
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *sinBinStatusDB) DeleteSinBinStatusByID(ctx context.Context, id string) error {
|
func (s *sinBinStatusDB) DeleteSinBinStatusByID(ctx context.Context, id string) error {
|
||||||
// On return ensure status invalidated from cache.
|
// Delete the status from DB.
|
||||||
defer s.state.Caches.DB.SinBinStatus.Invalidate("ID", id)
|
if _, err := s.db.
|
||||||
|
|
||||||
_, err := s.db.
|
|
||||||
NewDelete().
|
NewDelete().
|
||||||
TableExpr("? AS ?", bun.Ident("sin_bin_statuses"), bun.Ident("sin_bin_status")).
|
TableExpr("? AS ?", bun.Ident("sin_bin_statuses"), bun.Ident("sin_bin_status")).
|
||||||
Where("? = ?", bun.Ident("sin_bin_status.id"), id).
|
Where("? = ?", bun.Ident("sin_bin_status.id"), id).
|
||||||
Exec(ctx)
|
Exec(ctx); err != nil &&
|
||||||
|
!errors.Is(err, db.ErrNoEntries) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Invalidate any cached sinbin status model by ID.
|
||||||
|
s.state.Caches.DB.SinBinStatus.Invalidate("ID", id)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
|
@ -54,15 +54,8 @@ func (s *statusDB) GetStatusesByIDs(ctx context.Context, ids []string) ([]*gtsmo
|
||||||
statuses, err := s.state.Caches.DB.Status.LoadIDs("ID",
|
statuses, err := s.state.Caches.DB.Status.LoadIDs("ID",
|
||||||
ids,
|
ids,
|
||||||
func(uncached []string) ([]*gtsmodel.Status, error) {
|
func(uncached []string) ([]*gtsmodel.Status, error) {
|
||||||
// Avoid querying
|
|
||||||
// if none uncached.
|
|
||||||
count := len(uncached)
|
|
||||||
if count == 0 {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Preallocate expected length of uncached statuses.
|
// Preallocate expected length of uncached statuses.
|
||||||
statuses := make([]*gtsmodel.Status, 0, count)
|
statuses := make([]*gtsmodel.Status, 0, len(uncached))
|
||||||
|
|
||||||
// Perform database query scanning
|
// Perform database query scanning
|
||||||
// the remaining (uncached) status IDs.
|
// the remaining (uncached) status IDs.
|
||||||
|
@ -486,24 +479,13 @@ func (s *statusDB) UpdateStatus(ctx context.Context, status *gtsmodel.Status, co
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *statusDB) DeleteStatusByID(ctx context.Context, id string) error {
|
func (s *statusDB) DeleteStatusByID(ctx context.Context, id string) error {
|
||||||
// Load status into cache before attempting a delete,
|
// Gather necessary fields from
|
||||||
// as we need it cached in order to trigger the invalidate
|
// deleted for cache invaliation.
|
||||||
// callback. This in turn invalidates others.
|
var deleted gtsmodel.Status
|
||||||
_, err := s.GetStatusByID(
|
deleted.ID = 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
|
|
||||||
}
|
|
||||||
|
|
||||||
// On return ensure status invalidated from cache.
|
// Delete status from database and any related links in a transaction.
|
||||||
defer s.state.Caches.DB.Status.Invalidate("ID", id)
|
if err := s.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
|
||||||
|
|
||||||
return s.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
|
|
||||||
// delete links between this status and any emojis it uses
|
// delete links between this status and any emojis it uses
|
||||||
if _, err := tx.
|
if _, err := tx.
|
||||||
NewDelete().
|
NewDelete().
|
||||||
|
@ -524,26 +506,42 @@ func (s *statusDB) DeleteStatusByID(ctx context.Context, id string) error {
|
||||||
|
|
||||||
// Delete links between this status
|
// Delete links between this status
|
||||||
// and any threads it was a part of.
|
// and any threads it was a part of.
|
||||||
_, err = tx.
|
if _, err := tx.
|
||||||
NewDelete().
|
NewDelete().
|
||||||
TableExpr("? AS ?", bun.Ident("thread_to_statuses"), bun.Ident("thread_to_status")).
|
TableExpr("? AS ?", bun.Ident("thread_to_statuses"), bun.Ident("thread_to_status")).
|
||||||
Where("? = ?", bun.Ident("thread_to_status.status_id"), id).
|
Where("? = ?", bun.Ident("thread_to_status.status_id"), id).
|
||||||
Exec(ctx)
|
Exec(ctx); err != nil {
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// delete the status itself
|
// delete the status itself
|
||||||
if _, err := tx.
|
if _, err := tx.
|
||||||
NewDelete().
|
NewDelete().
|
||||||
TableExpr("? AS ?", bun.Ident("statuses"), bun.Ident("status")).
|
Model(&deleted).
|
||||||
Where("? = ?", bun.Ident("status.id"), id).
|
Where("? = ?", bun.Ident("id"), id).
|
||||||
Exec(ctx); err != nil {
|
Returning("?, ?, ?, ?, ?",
|
||||||
|
bun.Ident("account_id"),
|
||||||
|
bun.Ident("boost_of_id"),
|
||||||
|
bun.Ident("in_reply_to_id"),
|
||||||
|
bun.Ident("attachments"),
|
||||||
|
bun.Ident("poll_id"),
|
||||||
|
).
|
||||||
|
Exec(ctx); err != nil &&
|
||||||
|
!errors.Is(err, db.ErrNoEntries) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
})
|
}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Invalidate cached status by its ID, manually
|
||||||
|
// call the invalidate hook in case not cached.
|
||||||
|
s.state.Caches.DB.Status.Invalidate("ID", id)
|
||||||
|
s.state.Caches.OnInvalidateStatus(&deleted)
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *statusDB) GetStatusesUsingEmoji(ctx context.Context, emojiID string) ([]*gtsmodel.Status, error) {
|
func (s *statusDB) GetStatusesUsingEmoji(ctx context.Context, emojiID string) ([]*gtsmodel.Status, error) {
|
||||||
|
|
|
@ -73,15 +73,8 @@ func (s *statusBookmarkDB) GetStatusBookmarksByIDs(ctx context.Context, ids []st
|
||||||
bookmarks, err := s.state.Caches.DB.StatusBookmark.LoadIDs("ID",
|
bookmarks, err := s.state.Caches.DB.StatusBookmark.LoadIDs("ID",
|
||||||
ids,
|
ids,
|
||||||
func(uncached []string) ([]*gtsmodel.StatusBookmark, error) {
|
func(uncached []string) ([]*gtsmodel.StatusBookmark, error) {
|
||||||
// Avoid querying
|
|
||||||
// if none uncached.
|
|
||||||
count := len(uncached)
|
|
||||||
if count == 0 {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Preallocate expected length of uncached bookmarks.
|
// Preallocate expected length of uncached bookmarks.
|
||||||
bookmarks := make([]*gtsmodel.StatusBookmark, 0, count)
|
bookmarks := make([]*gtsmodel.StatusBookmark, 0, len(uncached))
|
||||||
|
|
||||||
// Perform database query scanning
|
// Perform database query scanning
|
||||||
// the remaining (uncached) bookmarks.
|
// the remaining (uncached) bookmarks.
|
||||||
|
@ -264,60 +257,86 @@ func (s *statusBookmarkDB) PutStatusBookmark(ctx context.Context, bookmark *gtsm
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *statusBookmarkDB) DeleteStatusBookmarkByID(ctx context.Context, id string) error {
|
func (s *statusBookmarkDB) DeleteStatusBookmarkByID(ctx context.Context, id string) error {
|
||||||
_, err := s.db.
|
// Gather necessary fields from
|
||||||
NewDelete().
|
// deleted for cache invaliation.
|
||||||
Table("status_bookmarks").
|
var deleted gtsmodel.StatusBookmark
|
||||||
|
deleted.ID = id
|
||||||
|
|
||||||
|
// Delete block with given URI,
|
||||||
|
// returning the deleted models.
|
||||||
|
if _, err := s.db.NewDelete().
|
||||||
|
Model(&deleted).
|
||||||
Where("? = ?", bun.Ident("id"), id).
|
Where("? = ?", bun.Ident("id"), id).
|
||||||
Exec(ctx)
|
Returning("?", bun.Ident("status_id")).
|
||||||
if err != nil {
|
Exec(ctx); err != nil &&
|
||||||
|
!errors.Is(err, db.ErrNoEntries) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Invalidate cached status bookmark by its ID,
|
||||||
|
// manually call invalidate hook in case not cached.
|
||||||
s.state.Caches.DB.StatusBookmark.Invalidate("ID", id)
|
s.state.Caches.DB.StatusBookmark.Invalidate("ID", id)
|
||||||
|
s.state.Caches.OnInvalidateStatusBookmark(&deleted)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *statusBookmarkDB) DeleteStatusBookmarks(ctx context.Context, targetAccountID string, originAccountID string) error {
|
func (s *statusBookmarkDB) DeleteStatusBookmarks(ctx context.Context, targetAccountID string, originAccountID string) error {
|
||||||
if targetAccountID == "" && originAccountID == "" {
|
if targetAccountID == "" && originAccountID == "" {
|
||||||
return errors.New("DeleteBookmarks: one of targetAccountID or originAccountID must be set")
|
return gtserror.New("one of targetAccountID or originAccountID must be set")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Gather necessary fields from
|
||||||
|
// deleted for cache invaliation.
|
||||||
|
var deleted []*gtsmodel.StatusBookmark
|
||||||
|
|
||||||
q := s.db.
|
q := s.db.
|
||||||
NewDelete().
|
NewDelete().
|
||||||
TableExpr("? AS ?", bun.Ident("status_bookmarks"), bun.Ident("status_bookmark"))
|
Model(&deleted).
|
||||||
|
Returning("?", bun.Ident("status_id"))
|
||||||
|
|
||||||
if targetAccountID != "" {
|
if targetAccountID != "" {
|
||||||
q = q.Where("? = ?", bun.Ident("status_bookmark.target_account_id"), targetAccountID)
|
q = q.Where("? = ?", bun.Ident("target_account_id"), targetAccountID)
|
||||||
defer s.state.Caches.DB.StatusBookmark.Invalidate("TargetAccountID", targetAccountID)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if originAccountID != "" {
|
if originAccountID != "" {
|
||||||
q = q.Where("? = ?", bun.Ident("status_bookmark.account_id"), originAccountID)
|
q = q.Where("? = ?", bun.Ident("account_id"), originAccountID)
|
||||||
defer s.state.Caches.DB.StatusBookmark.Invalidate("AccountID", originAccountID)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := q.Exec(ctx); err != nil {
|
if _, err := q.Exec(ctx); err != nil &&
|
||||||
|
!errors.Is(err, db.ErrNoEntries) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if targetAccountID != "" {
|
for _, deleted := range deleted {
|
||||||
s.state.Caches.DB.StatusBookmark.Invalidate("TargetAccountID", targetAccountID)
|
// Invalidate cached status bookmark by status ID,
|
||||||
}
|
// manually call invalidate hook in case not cached.
|
||||||
|
s.state.Caches.DB.StatusBookmark.Invalidate("StatusID", deleted.StatusID)
|
||||||
if originAccountID != "" {
|
s.state.Caches.OnInvalidateStatusBookmark(deleted)
|
||||||
s.state.Caches.DB.StatusBookmark.Invalidate("AccountID", originAccountID)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *statusBookmarkDB) DeleteStatusBookmarksForStatus(ctx context.Context, statusID string) error {
|
func (s *statusBookmarkDB) DeleteStatusBookmarksForStatus(ctx context.Context, statusID string) error {
|
||||||
q := s.db.
|
// Delete status bookmarks
|
||||||
NewDelete().
|
// from database by status ID.
|
||||||
|
q := s.db.NewDelete().
|
||||||
TableExpr("? AS ?", bun.Ident("status_bookmarks"), bun.Ident("status_bookmark")).
|
TableExpr("? AS ?", bun.Ident("status_bookmarks"), bun.Ident("status_bookmark")).
|
||||||
Where("? = ?", bun.Ident("status_bookmark.status_id"), statusID)
|
Where("? = ?", bun.Ident("status_bookmark.status_id"), statusID)
|
||||||
if _, err := q.Exec(ctx); err != nil {
|
if _, err := q.Exec(ctx); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Wrap provided ID in a bookmark
|
||||||
|
// model for calling cache hook.
|
||||||
|
var deleted gtsmodel.StatusBookmark
|
||||||
|
deleted.StatusID = statusID
|
||||||
|
|
||||||
|
// Invalidate cached status bookmark by status ID,
|
||||||
|
// manually call invalidate hook in case not cached.
|
||||||
s.state.Caches.DB.StatusBookmark.Invalidate("StatusID", statusID)
|
s.state.Caches.DB.StatusBookmark.Invalidate("StatusID", statusID)
|
||||||
|
s.state.Caches.OnInvalidateStatusBookmark(&deleted)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -133,15 +133,8 @@ func (s *statusFaveDB) GetStatusFaves(ctx context.Context, statusID string) ([]*
|
||||||
faves, err := s.state.Caches.DB.StatusFave.LoadIDs("ID",
|
faves, err := s.state.Caches.DB.StatusFave.LoadIDs("ID",
|
||||||
faveIDs,
|
faveIDs,
|
||||||
func(uncached []string) ([]*gtsmodel.StatusFave, error) {
|
func(uncached []string) ([]*gtsmodel.StatusFave, error) {
|
||||||
// Avoid querying
|
|
||||||
// if none uncached.
|
|
||||||
count := len(uncached)
|
|
||||||
if count == 0 {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Preallocate expected length of uncached faves.
|
// Preallocate expected length of uncached faves.
|
||||||
faves := make([]*gtsmodel.StatusFave, 0, count)
|
faves := make([]*gtsmodel.StatusFave, 0, len(uncached))
|
||||||
|
|
||||||
// Perform database query scanning
|
// Perform database query scanning
|
||||||
// the remaining (uncached) fave IDs.
|
// the remaining (uncached) fave IDs.
|
||||||
|
|
|
@ -20,6 +20,7 @@
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/superseriousbusiness/gotosocial/internal/db"
|
"github.com/superseriousbusiness/gotosocial/internal/db"
|
||||||
|
@ -79,15 +80,8 @@ func (t *tagDB) GetTags(ctx context.Context, ids []string) ([]*gtsmodel.Tag, err
|
||||||
tags, err := t.state.Caches.DB.Tag.LoadIDs("ID",
|
tags, err := t.state.Caches.DB.Tag.LoadIDs("ID",
|
||||||
ids,
|
ids,
|
||||||
func(uncached []string) ([]*gtsmodel.Tag, error) {
|
func(uncached []string) ([]*gtsmodel.Tag, error) {
|
||||||
// Avoid querying
|
|
||||||
// if none uncached.
|
|
||||||
count := len(uncached)
|
|
||||||
if count == 0 {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Preallocate expected length of uncached tags.
|
// Preallocate expected length of uncached tags.
|
||||||
tags := make([]*gtsmodel.Tag, 0, count)
|
tags := make([]*gtsmodel.Tag, 0, len(uncached))
|
||||||
|
|
||||||
// Perform database query scanning
|
// Perform database query scanning
|
||||||
// the remaining (uncached) IDs.
|
// the remaining (uncached) IDs.
|
||||||
|
@ -148,17 +142,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 +166,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 +186,11 @@ 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
|
||||||
}
|
}
|
||||||
|
return slices.Contains(followingTagIDs, tagID), nil
|
||||||
for _, accountTagID := range accountTagIDs {
|
|
||||||
if accountTagID == tagID {
|
|
||||||
return true, 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 +215,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.
|
// We updated something, 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 +246,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.
|
// We deleted something, 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 +271,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 +298,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
|
||||||
}
|
}
|
||||||
|
|
|
@ -70,7 +70,7 @@ func (t *timelineDB) GetHomeTimeline(ctx context.Context, accountID string, maxI
|
||||||
// To take account of exclusive lists, get all of
|
// To take account of exclusive lists, get all of
|
||||||
// this account's lists, so we can filter out follows
|
// this account's lists, so we can filter out follows
|
||||||
// that are in contained in exclusive lists.
|
// that are in contained in exclusive lists.
|
||||||
lists, err := t.state.DB.GetListsForAccountID(ctx, accountID)
|
lists, err := t.state.DB.GetListsByAccountID(ctx, accountID)
|
||||||
if err != nil && !errors.Is(err, db.ErrNoEntries) {
|
if err != nil && !errors.Is(err, db.ErrNoEntries) {
|
||||||
return nil, gtserror.Newf("db error getting lists for account %s: %w", accountID, err)
|
return nil, gtserror.Newf("db error getting lists for account %s: %w", accountID, err)
|
||||||
}
|
}
|
||||||
|
@ -84,9 +84,15 @@ func (t *timelineDB) GetHomeTimeline(ctx context.Context, accountID string, maxI
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Fetch all follow IDs of the entries ccontained in this list.
|
||||||
|
listFollowIDs, err := t.state.DB.GetFollowIDsInList(ctx, list.ID, nil)
|
||||||
|
if err != nil && !errors.Is(err, db.ErrNoEntries) {
|
||||||
|
return nil, gtserror.Newf("db error getting list entry follow ids: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
// Exclusive list, index all its follow IDs.
|
// Exclusive list, index all its follow IDs.
|
||||||
for _, listEntry := range list.ListEntries {
|
for _, followID := range listFollowIDs {
|
||||||
ignoreFollowIDs[listEntry.FollowID] = struct{}{}
|
ignoreFollowIDs[followID] = struct{}{}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -370,30 +376,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().
|
||||||
|
|
|
@ -184,8 +184,8 @@ func (suite *TimelineTestSuite) TestGetHomeTimelineIgnoreExclusive() {
|
||||||
suite.checkStatuses(s, id.Highest, id.Lowest, 8)
|
suite.checkStatuses(s, id.Highest, id.Lowest, 8)
|
||||||
|
|
||||||
// Remove admin account from the exclusive list.
|
// Remove admin account from the exclusive list.
|
||||||
listEntryID := suite.testListEntries["local_account_1_list_1_entry_2"].ID
|
listEntry := suite.testListEntries["local_account_1_list_1_entry_2"]
|
||||||
if err := suite.db.DeleteListEntry(ctx, listEntryID); err != nil {
|
if err := suite.db.DeleteListEntry(ctx, listEntry.ListID, listEntry.FollowID); err != nil {
|
||||||
suite.FailNow(err.Error())
|
suite.FailNow(err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -67,12 +67,14 @@ func (t *tombstoneDB) PutTombstone(ctx context.Context, tombstone *gtsmodel.Tomb
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *tombstoneDB) DeleteTombstone(ctx context.Context, id string) error {
|
func (t *tombstoneDB) DeleteTombstone(ctx context.Context, id string) error {
|
||||||
defer t.state.Caches.DB.Tombstone.Invalidate("ID", id)
|
|
||||||
|
|
||||||
// Delete tombstone from DB.
|
// Delete tombstone from DB.
|
||||||
_, err := t.db.NewDelete().
|
_, err := t.db.NewDelete().
|
||||||
TableExpr("? AS ?", bun.Ident("tombstones"), bun.Ident("tombstone")).
|
TableExpr("? AS ?", bun.Ident("tombstones"), bun.Ident("tombstone")).
|
||||||
Where("? = ?", bun.Ident("tombstone.id"), id).
|
Where("? = ?", bun.Ident("tombstone.id"), id).
|
||||||
Exec(ctx)
|
Exec(ctx)
|
||||||
|
|
||||||
|
// Invalidate any cached tombstone by given ID.
|
||||||
|
t.state.Caches.DB.Tombstone.Invalidate("ID", id)
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,10 +19,8 @@
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/superseriousbusiness/gotosocial/internal/db"
|
|
||||||
"github.com/superseriousbusiness/gotosocial/internal/gtscontext"
|
"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"
|
||||||
|
@ -209,26 +207,26 @@ func (u *userDB) UpdateUser(ctx context.Context, user *gtsmodel.User, columns ..
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *userDB) DeleteUserByID(ctx context.Context, userID string) error {
|
func (u *userDB) DeleteUserByID(ctx context.Context, userID string) error {
|
||||||
defer u.state.Caches.DB.User.Invalidate("ID", userID)
|
// Gather necessary fields from
|
||||||
|
// deleted for cache invaliation.
|
||||||
|
var deleted gtsmodel.User
|
||||||
|
deleted.ID = userID
|
||||||
|
|
||||||
// Load user into cache before attempting a delete,
|
// Delete user from DB.
|
||||||
// as we need it cached in order to trigger the invalidate
|
if _, err := u.db.NewDelete().
|
||||||
// callback. This in turn invalidates others.
|
Model(&deleted).
|
||||||
_, err := u.GetUserByID(gtscontext.SetBarebones(ctx), userID)
|
Where("? = ?", bun.Ident("id"), userID).
|
||||||
if err != nil {
|
Returning("?", bun.Ident("account_id")).
|
||||||
if errors.Is(err, db.ErrNoEntries) {
|
Exec(ctx); err != nil {
|
||||||
// not an issue.
|
|
||||||
err = nil
|
|
||||||
}
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Finally delete user from DB.
|
// Invalidate cached user by ID, manually
|
||||||
_, err = u.db.NewDelete().
|
// call invalidate hook in case not cached.
|
||||||
TableExpr("? AS ?", bun.Ident("users"), bun.Ident("user")).
|
u.state.Caches.DB.User.Invalidate("ID", userID)
|
||||||
Where("? = ?", bun.Ident("user.id"), userID).
|
u.state.Caches.OnInvalidateUser(&deleted)
|
||||||
Exec(ctx)
|
|
||||||
return err
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *userDB) PutDeniedUser(ctx context.Context, deniedUser *gtsmodel.DeniedUser) error {
|
func (u *userDB) PutDeniedUser(ctx context.Context, deniedUser *gtsmodel.DeniedUser) error {
|
||||||
|
|
|
@ -21,6 +21,7 @@
|
||||||
"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)
|
||||||
|
|
||||||
|
// GetListsContainingFollowID gets all lists that contain the given follow with ID.
|
||||||
|
GetListsContainingFollowID(ctx context.Context, followID string) ([]*gtsmodel.List, error)
|
||||||
|
|
||||||
|
// GetFollowIDsInList returns all the follow IDs contained within given list ID.
|
||||||
|
GetFollowIDsInList(ctx context.Context, listID string, page *paging.Page) ([]string, error)
|
||||||
|
|
||||||
|
// GetFollowsInList returns all the follows contained within given list ID.
|
||||||
|
GetFollowsInList(ctx context.Context, listID string, page *paging.Page) ([]*gtsmodel.Follow, error)
|
||||||
|
|
||||||
|
// GetAccountIDsInList return all the account IDs (follow targets) contained within given list ID.
|
||||||
|
GetAccountIDsInList(ctx context.Context, listID string, page *paging.Page) ([]string, error)
|
||||||
|
|
||||||
|
// GetAccountsInList return all the accounts (follow targets) contained within given list ID.
|
||||||
|
GetAccountsInList(ctx context.Context, listID string, page *paging.Page) ([]*gtsmodel.Account, error)
|
||||||
|
|
||||||
|
// IsAccountInListID returns whether given account with ID is in the list with ID.
|
||||||
|
IsAccountInList(ctx context.Context, listID string, accountID string) (bool, error)
|
||||||
|
|
||||||
// PopulateList ensures that the list's struct fields are populated.
|
// PopulateList 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.
|
// DeleteAllListEntryByFollow deletes all list entries with the given followIDs.
|
||||||
DeleteListEntriesForFollowID(ctx context.Context, followID string) error
|
DeleteAllListEntriesByFollows(ctx context.Context, followIDs ...string) error
|
||||||
|
|
||||||
// ListIncludesAccount returns true if the given listID includes the given accountID.
|
|
||||||
ListIncludesAccount(ctx context.Context, listID string, accountID string) (bool, error)
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,7 +39,8 @@ type Poll interface {
|
||||||
// UpdatePoll updates the Poll in the database, only on selected columns if provided (else, all).
|
// UpdatePoll updates the Poll in the database, only on selected columns if provided (else, all).
|
||||||
UpdatePoll(ctx context.Context, poll *gtsmodel.Poll, cols ...string) error
|
UpdatePoll(ctx context.Context, poll *gtsmodel.Poll, cols ...string) error
|
||||||
|
|
||||||
// DeletePollByID deletes the Poll with given ID from the database.
|
// DeletePollByID deletes the Poll with given ID from the
|
||||||
|
// database, along with all its associated poll votes.
|
||||||
DeletePollByID(ctx context.Context, id string) error
|
DeletePollByID(ctx context.Context, id string) error
|
||||||
|
|
||||||
// GetPollVoteByID gets the PollVote with given ID from the database.
|
// GetPollVoteByID gets the PollVote with given ID from the database.
|
||||||
|
@ -57,9 +58,6 @@ type Poll interface {
|
||||||
// PutPollVote puts the given PollVote in the database.
|
// PutPollVote puts the given PollVote in the database.
|
||||||
PutPollVote(ctx context.Context, vote *gtsmodel.PollVote) error
|
PutPollVote(ctx context.Context, vote *gtsmodel.PollVote) error
|
||||||
|
|
||||||
// DeletePollVotes deletes all PollVotes in Poll with given ID from the database.
|
|
||||||
DeletePollVotes(ctx context.Context, pollID string) error
|
|
||||||
|
|
||||||
// DeletePollVoteBy deletes the PollVote in Poll with ID, by account ID, from the database.
|
// DeletePollVoteBy deletes the PollVote in Poll with ID, by account ID, from the database.
|
||||||
DeletePollVoteBy(ctx context.Context, pollID string, accountID string) error
|
DeletePollVoteBy(ctx context.Context, pollID string, accountID string) error
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
|
|
||||||
|
|
|
@ -44,7 +44,7 @@ type Report interface {
|
||||||
// provided, then all columns will be updated.
|
// provided, then all columns will be updated.
|
||||||
// updated_at will also be updated, no need to pass this
|
// updated_at will also be updated, no need to pass this
|
||||||
// as a specific column.
|
// as a specific column.
|
||||||
UpdateReport(ctx context.Context, report *gtsmodel.Report, columns ...string) (*gtsmodel.Report, error)
|
UpdateReport(ctx context.Context, report *gtsmodel.Report, columns ...string) error
|
||||||
|
|
||||||
// DeleteReportByID deletes report with the given id.
|
// DeleteReportByID deletes report with the given id.
|
||||||
DeleteReportByID(ctx context.Context, id string) error
|
DeleteReportByID(ctx context.Context, id string) error
|
||||||
|
|
|
@ -826,9 +826,6 @@ func (d *Dereferencer) fetchStatusPoll(
|
||||||
if err := d.state.DB.DeletePollByID(ctx, pollID); err != nil {
|
if err := d.state.DB.DeletePollByID(ctx, pollID); err != nil {
|
||||||
return gtserror.Newf("error deleting existing poll from database: %w", err)
|
return gtserror.Newf("error deleting existing poll from database: %w", err)
|
||||||
}
|
}
|
||||||
if err := d.state.DB.DeletePollVotes(ctx, pollID); err != nil {
|
|
||||||
return gtserror.Newf("error deleting existing votes from database: %w", err)
|
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
|
@ -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.
|
||||||
}
|
}
|
||||||
|
|
|
@ -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)
|
||||||
|
|
|
@ -30,8 +30,6 @@
|
||||||
"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.GetListsContainingFollowID(ctx, follow.ID)
|
||||||
gtscontext.SetBarebones(ctx),
|
|
||||||
follow.ID,
|
|
||||||
)
|
|
||||||
if err != nil && !errors.Is(err, db.ErrNoEntries) {
|
|
||||||
return nil, gtserror.NewErrorInternalError(fmt.Errorf("db error: %w", err))
|
|
||||||
}
|
|
||||||
|
|
||||||
count := len(listEntries)
|
|
||||||
if count == 0 {
|
|
||||||
return noLists, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
apiLists := make([]*apimodel.List, 0, count)
|
|
||||||
for _, listEntry := range listEntries {
|
|
||||||
list, err := p.state.DB.GetListByID(
|
|
||||||
// Don't populate list.
|
|
||||||
gtscontext.SetBarebones(ctx),
|
|
||||||
listEntry.ListID,
|
|
||||||
)
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Debugf(ctx, "skipping list %s due to error %q", listEntry.ListID, err)
|
err := gtserror.Newf("error getting lists for follow: %w", err)
|
||||||
continue
|
return nil, gtserror.NewErrorInternalError(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
apiLists := make([]*apimodel.List, 0, len(lists))
|
||||||
|
for _, list := range lists {
|
||||||
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -142,7 +142,7 @@ func (p *Processor) ReportResolve(ctx context.Context, account *gtsmodel.Account
|
||||||
columns = append(columns, "action_taken")
|
columns = append(columns, "action_taken")
|
||||||
}
|
}
|
||||||
|
|
||||||
updatedReport, err := p.state.DB.UpdateReport(ctx, report, columns...)
|
err = p.state.DB.UpdateReport(ctx, report, columns...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, gtserror.NewErrorInternalError(err)
|
return nil, gtserror.NewErrorInternalError(err)
|
||||||
}
|
}
|
||||||
|
@ -156,7 +156,7 @@ func (p *Processor) ReportResolve(ctx context.Context, account *gtsmodel.Account
|
||||||
Target: report.Account,
|
Target: report.Account,
|
||||||
})
|
})
|
||||||
|
|
||||||
apimodelReport, err := p.converter.ReportToAdminAPIReport(ctx, updatedReport, account)
|
apimodelReport, err := p.converter.ReportToAdminAPIReport(ctx, report, account)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, gtserror.NewErrorInternalError(err)
|
return nil, gtserror.NewErrorInternalError(err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -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)
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,7 +20,6 @@
|
||||||
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 @@
|
||||||
"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)
|
||||||
})
|
if err != nil {
|
||||||
|
log.Errorf(ctx, "error converting to api account: %v", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
items = append(items, apiAccount)
|
||||||
|
}
|
||||||
|
|
||||||
return util.PackagePageableResponse(util.PageableResponseParams{
|
return paging.PackageResponse(paging.ResponseParams{
|
||||||
Items: items,
|
Items: items,
|
||||||
Path: "/api/v1/lists/" + listID + "/accounts",
|
Path: "/api/v1/lists/" + listID + "/accounts",
|
||||||
NextMaxIDValue: nextMaxIDValue,
|
Next: page.Next(lo, hi),
|
||||||
PrevMinIDValue: prevMinIDValue,
|
Prev: page.Prev(lo, hi),
|
||||||
Limit: limit,
|
}), nil
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
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 {
|
|
||||||
log.Errorf(ctx, "error converting to public api account: %v", err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
appendAcc(apiAccount)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,73 +23,90 @@
|
||||||
"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,
|
||||||
for _, targetAccountID := range targetAccountIDs {
|
nil,
|
||||||
// Ensure follow exists.
|
)
|
||||||
follow, err := p.state.DB.GetFollow(ctx, account.ID, targetAccountID)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, db.ErrNoEntries) {
|
err := gtserror.Newf("error getting list follows: %w", err)
|
||||||
err = fmt.Errorf("you do not follow account %s", targetAccountID)
|
|
||||||
return gtserror.NewErrorNotFound(err, err.Error())
|
|
||||||
}
|
|
||||||
return gtserror.NewErrorInternalError(err)
|
return gtserror.NewErrorInternalError(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure followID not already in list.
|
// Convert the follows to a hash set containing the target account IDs.
|
||||||
// This particular call to isInList will
|
inFollows := util.ToSetFunc(follows, func(follow *gtsmodel.Follow) string {
|
||||||
// never error, so just check entryID.
|
return follow.TargetAccountID
|
||||||
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
|
// Preallocate a slice of expected list entries, we specifically
|
||||||
// followID wasn't found in the list.
|
// gather and add all the target accounts in one go rather than
|
||||||
if entryID != "" {
|
// individually, to ensure we don't end up with partial updates.
|
||||||
err = fmt.Errorf("account with id %s is already in list %s with entryID %s", targetAccountID, listID, entryID)
|
entries := make([]*gtsmodel.ListEntry, 0, len(targetAccountIDs))
|
||||||
return gtserror.NewErrorUnprocessableEntity(err, err.Error())
|
|
||||||
|
// Iterate all the account IDs in given target list.
|
||||||
|
for _, targetAccountID := range targetAccountIDs {
|
||||||
|
|
||||||
|
// Look for follow to target account.
|
||||||
|
if inFollows.Has(targetAccountID) {
|
||||||
|
text := fmt.Sprintf("account %s is already in list %s", targetAccountID, listID)
|
||||||
|
return gtserror.NewErrorUnprocessableEntity(errors.New(text), text)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Entry wasn't in the list, we can add it.
|
// Get the actual follow to target.
|
||||||
listEntries = append(listEntries, >smodel.ListEntry{
|
follow, err := p.state.DB.GetFollow(
|
||||||
|
|
||||||
|
// We don't need any sub-models.
|
||||||
|
gtscontext.SetBarebones(ctx),
|
||||||
|
account.ID,
|
||||||
|
targetAccountID,
|
||||||
|
)
|
||||||
|
if err != nil && !errors.Is(err, db.ErrNoEntries) {
|
||||||
|
err := gtserror.Newf("db error getting follow: %w", err)
|
||||||
|
return gtserror.NewErrorInternalError(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if follow == nil {
|
||||||
|
text := fmt.Sprintf("account %s not currently followed", targetAccountID)
|
||||||
|
return gtserror.NewErrorNotFound(errors.New(text), text)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generate new entry for this follow in list.
|
||||||
|
entries = append(entries, >smodel.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 +114,61 @@ func(listEntry *gtsmodel.ListEntry) (string, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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.
|
|
||||||
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.
|
// We only need barebones model.
|
||||||
return listEntry.Follow.TargetAccountID, nil
|
gtscontext.SetBarebones(ctx),
|
||||||
},
|
listID,
|
||||||
|
nil,
|
||||||
)
|
)
|
||||||
|
if err != nil {
|
||||||
// Error may be returned here if there was an issue
|
err := gtserror.Newf("error getting list follows: %w", err)
|
||||||
// populating the list entry. We only return on proper
|
|
||||||
// DB errors, we can just skip no entry errors.
|
|
||||||
if err != nil && !errors.Is(err, db.ErrNoEntries) {
|
|
||||||
err = fmt.Errorf("error checking if targetAccountID %s was in list %s: %w", targetAccountID, listID, err)
|
|
||||||
return gtserror.NewErrorInternalError(err)
|
return gtserror.NewErrorInternalError(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if entryID == "" {
|
// Convert the follows to a map keyed by the target account ID.
|
||||||
// There was an errNoEntries or targetAccount
|
followsMap := util.KeyBy(follows, func(follow *gtsmodel.Follow) string {
|
||||||
// wasn't in this list anyway, so we can skip it.
|
return follow.TargetAccountID
|
||||||
|
})
|
||||||
|
|
||||||
|
var errs gtserror.MultiError
|
||||||
|
|
||||||
|
// Iterate all the account IDs in given target list.
|
||||||
|
for _, targetAccountID := range targetAccountIDs {
|
||||||
|
|
||||||
|
// Look for follow targetting this account.
|
||||||
|
follow, ok := followsMap[targetAccountID]
|
||||||
|
|
||||||
|
if !ok {
|
||||||
|
// not in list.
|
||||||
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
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,18 +33,25 @@
|
||||||
// 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
|
|
||||||
}
|
|
||||||
|
|
|
@ -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())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -21,7 +21,6 @@
|
||||||
"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 given follow.
|
||||||
|
lists, err := s.State.DB.GetListsContainingFollowID(
|
||||||
|
|
||||||
|
// 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
|
// Check whether list is eligible for this status.
|
||||||
for _, listEntry := range listEntries {
|
eligible, err := s.listEligible(ctx, list, status)
|
||||||
eligible, err := s.listEligible(ctx, listEntry, 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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Update exclusive flag if list is so.
|
||||||
|
exclusive = exclusive || *list.Exclusive
|
||||||
|
|
||||||
// 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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return exclusive, listTimelined
|
// Update flag based on if timelined.
|
||||||
|
timelined = timelined || listTimelined
|
||||||
}
|
}
|
||||||
|
|
||||||
// getListEntries returns list entries for a given follow.
|
return timelined, exclusive, nil
|
||||||
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.IsAccountInList(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,14 @@ 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??
|
log.Panicf(ctx, "unknown reply policy: %s", list.RepliesPolicy)
|
||||||
err := gtserror.Newf(
|
return false, nil // unreachable code
|
||||||
"reply policy '%s' not recognized on list %s",
|
|
||||||
list.RepliesPolicy, list.ID,
|
|
||||||
)
|
|
||||||
return false, err
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -452,6 +382,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 +392,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 +404,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 +425,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 +457,7 @@ func (s *Surface) timelineAndNotifyStatusForTagFollowers(
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return errs.Combine()
|
return errs.Combine()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -667,17 +602,15 @@ func (s *Surface) timelineStatusUpdate(ctx context.Context, status *gtsmodel.Sta
|
||||||
follows = append(follows, >smodel.Follow{
|
follows = append(follows, >smodel.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 +628,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 +646,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 +655,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 +692,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 +714,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 given follow.
|
||||||
|
lists, err := s.State.DB.GetListsContainingFollowID(
|
||||||
|
|
||||||
|
// 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)
|
|
||||||
|
// 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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Update exclusive flag if list is so.
|
||||||
|
exclusive = exclusive || *list.Exclusive
|
||||||
|
|
||||||
// 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
|
||||||
}
|
}
|
||||||
|
|
||||||
return exclusive
|
// Update flag based on if timelined.
|
||||||
|
timelined = timelined || listTimelined
|
||||||
|
}
|
||||||
|
|
||||||
|
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 +781,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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -126,11 +126,6 @@ func (u *utils) wipeStatus(
|
||||||
errs.Appendf("error deleting status poll: %w", err)
|
errs.Appendf("error deleting status poll: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete any poll votes pointing to this poll ID.
|
|
||||||
if err := u.state.DB.DeletePollVotes(ctx, pollID); err != nil {
|
|
||||||
errs.Appendf("error deleting status poll votes: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Cancel any scheduled expiry task for poll.
|
// Cancel any scheduled expiry task for poll.
|
||||||
_ = u.state.Workers.Scheduler.Cancel(pollID)
|
_ = u.state.Workers.Scheduler.Cancel(pollID)
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 {
|
// Get all follows contained with this list.
|
||||||
// Retrieve follow.
|
follows, err := c.state.DB.GetFollowsInList(ctx,
|
||||||
var err error
|
list.ID,
|
||||||
entry.Follow, err = c.state.DB.GetFollowByID(
|
nil,
|
||||||
ctx,
|
|
||||||
entry.FollowID,
|
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, gtserror.Newf(
|
err := gtserror.Newf("db error getting follows for list: %w", err)
|
||||||
"db error getting follow for list entry %s: %w",
|
return nil, err
|
||||||
entry.ID, err,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if entry.Follow.TargetAccount == nil {
|
|
||||||
// Retrieve account.
|
|
||||||
var err error
|
|
||||||
entry.Follow.TargetAccount, err = c.state.DB.GetAccountByID(
|
|
||||||
// Barebones is fine here.
|
|
||||||
gtscontext.SetBarebones(ctx),
|
|
||||||
entry.Follow.TargetAccountID,
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
return nil, gtserror.Newf(
|
|
||||||
"db error getting target account for list entry %s: %w",
|
|
||||||
entry.ID, err,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
|
|
|
@ -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,19 @@ func ToSet[T comparable](in []T) Set[T] {
|
||||||
return set
|
return set
|
||||||
}
|
}
|
||||||
|
|
||||||
// FromSet extracts the values from set to slice,
|
// ToSetFunc creates a Set[T] from input slice, keys provided by func.
|
||||||
// noting that this does not maintain any order.
|
func ToSetFunc[S any, T comparable](in []S, key func(S) T) Set[T] {
|
||||||
|
if key == nil {
|
||||||
|
panic("nil func")
|
||||||
|
}
|
||||||
|
set := make(Set[T], len(in))
|
||||||
|
for _, v := range in {
|
||||||
|
set[key(v)] = struct{}{}
|
||||||
|
}
|
||||||
|
return set
|
||||||
|
}
|
||||||
|
|
||||||
|
// FromSet extracts the values from set to slice.
|
||||||
func FromSet[T comparable](in Set[T]) []T {
|
func FromSet[T comparable](in Set[T]) []T {
|
||||||
out := make([]T, len(in))
|
out := make([]T, len(in))
|
||||||
var i int
|
var i int
|
||||||
|
|
|
@ -23,7 +23,6 @@ EXPECT=$(cat << "EOF"
|
||||||
"application-name": "gts",
|
"application-name": "gts",
|
||||||
"bind-address": "127.0.0.1",
|
"bind-address": "127.0.0.1",
|
||||||
"cache": {
|
"cache": {
|
||||||
"account-ids-following-tag-mem-ratio": 1,
|
|
||||||
"account-mem-ratio": 5,
|
"account-mem-ratio": 5,
|
||||||
"account-note-mem-ratio": 1,
|
"account-note-mem-ratio": 1,
|
||||||
"account-settings-mem-ratio": 0.1,
|
"account-settings-mem-ratio": 0.1,
|
||||||
|
@ -44,11 +43,13 @@ EXPECT=$(cat << "EOF"
|
||||||
"follow-mem-ratio": 2,
|
"follow-mem-ratio": 2,
|
||||||
"follow-request-ids-mem-ratio": 2,
|
"follow-request-ids-mem-ratio": 2,
|
||||||
"follow-request-mem-ratio": 2,
|
"follow-request-mem-ratio": 2,
|
||||||
|
"following-tag-ids-mem-ratio": 2,
|
||||||
"in-reply-to-ids-mem-ratio": 3,
|
"in-reply-to-ids-mem-ratio": 3,
|
||||||
"instance-mem-ratio": 1,
|
"instance-mem-ratio": 1,
|
||||||
"interaction-request-mem-ratio": 1,
|
"interaction-request-mem-ratio": 1,
|
||||||
"list-entry-mem-ratio": 2,
|
"list-ids-mem-ratio": 2,
|
||||||
"list-mem-ratio": 1,
|
"list-mem-ratio": 1,
|
||||||
|
"listed-ids-mem-ratio": 2,
|
||||||
"marker-mem-ratio": 0.5,
|
"marker-mem-ratio": 0.5,
|
||||||
"media-mem-ratio": 4,
|
"media-mem-ratio": 4,
|
||||||
"memory-target": 104857600,
|
"memory-target": 104857600,
|
||||||
|
@ -65,7 +66,6 @@ EXPECT=$(cat << "EOF"
|
||||||
"status-fave-ids-mem-ratio": 3,
|
"status-fave-ids-mem-ratio": 3,
|
||||||
"status-fave-mem-ratio": 2,
|
"status-fave-mem-ratio": 2,
|
||||||
"status-mem-ratio": 5,
|
"status-mem-ratio": 5,
|
||||||
"tag-ids-followed-by-account-mem-ratio": 1,
|
|
||||||
"tag-mem-ratio": 2,
|
"tag-mem-ratio": 2,
|
||||||
"thread-mute-mem-ratio": 0.2,
|
"thread-mute-mem-ratio": 0.2,
|
||||||
"token-mem-ratio": 0.75,
|
"token-mem-ratio": 0.75,
|
||||||
|
|
5
vendor/codeberg.org/gruf/go-structr/cache.go
generated
vendored
5
vendor/codeberg.org/gruf/go-structr/cache.go
generated
vendored
|
@ -375,6 +375,11 @@ func (c *Cache[T]) Load(index *Index, keys []Key, load func([]Key) ([]T, error))
|
||||||
// the lock.
|
// the lock.
|
||||||
unlock()
|
unlock()
|
||||||
|
|
||||||
|
if len(keys) == 0 {
|
||||||
|
// We loaded everything!
|
||||||
|
return values, nil
|
||||||
|
}
|
||||||
|
|
||||||
// Load uncached values.
|
// Load uncached values.
|
||||||
uncached, err := load(keys)
|
uncached, err := load(keys)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
1
vendor/codeberg.org/gruf/go-structr/item.go
generated
vendored
1
vendor/codeberg.org/gruf/go-structr/item.go
generated
vendored
|
@ -41,7 +41,6 @@ func free_indexed_item(item *indexed_item) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// drop_index will drop the given index entry from item's indexed.
|
// drop_index will drop the given index entry from item's indexed.
|
||||||
// note this also handles freeing the index_entry memory (e.g. to pool)
|
|
||||||
func (i *indexed_item) drop_index(entry *index_entry) {
|
func (i *indexed_item) drop_index(entry *index_entry) {
|
||||||
for x := 0; x < len(i.indexed); x++ {
|
for x := 0; x < len(i.indexed); x++ {
|
||||||
if i.indexed[x] != entry {
|
if i.indexed[x] != entry {
|
||||||
|
|
2
vendor/codeberg.org/gruf/go-structr/runtime.go
generated
vendored
2
vendor/codeberg.org/gruf/go-structr/runtime.go
generated
vendored
|
@ -152,8 +152,10 @@ func extract_fields(ptr unsafe.Pointer, fields []struct_field) []unsafe.Pointer
|
||||||
fptr = field.zero
|
fptr = field.zero
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set field ptr.
|
||||||
ptrs[i] = fptr
|
ptrs[i] = fptr
|
||||||
}
|
}
|
||||||
|
|
||||||
return ptrs
|
return ptrs
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
2
vendor/modules.txt
vendored
2
vendor/modules.txt
vendored
|
@ -75,7 +75,7 @@ codeberg.org/gruf/go-storage/disk
|
||||||
codeberg.org/gruf/go-storage/internal
|
codeberg.org/gruf/go-storage/internal
|
||||||
codeberg.org/gruf/go-storage/memory
|
codeberg.org/gruf/go-storage/memory
|
||||||
codeberg.org/gruf/go-storage/s3
|
codeberg.org/gruf/go-storage/s3
|
||||||
# codeberg.org/gruf/go-structr v0.8.8
|
# codeberg.org/gruf/go-structr v0.8.9
|
||||||
## explicit; go 1.21
|
## explicit; go 1.21
|
||||||
codeberg.org/gruf/go-structr
|
codeberg.org/gruf/go-structr
|
||||||
# codeberg.org/superseriousbusiness/exif-terminator v0.9.0
|
# codeberg.org/superseriousbusiness/exif-terminator v0.9.0
|
||||||
|
|
Loading…
Reference in a new issue