2023-03-12 15:00:57 +00:00
|
|
|
// GoToSocial
|
|
|
|
// Copyright (C) GoToSocial Authors admin@gotosocial.org
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2021-08-20 11:26:56 +01:00
|
|
|
|
2021-08-25 14:34:33 +01:00
|
|
|
package bundb
|
2021-08-20 11:26:56 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2024-04-13 12:25:10 +01:00
|
|
|
"fmt"
|
|
|
|
"net/netip"
|
2024-04-13 10:36:10 +01:00
|
|
|
"slices"
|
2022-12-01 15:06:09 +00:00
|
|
|
"strings"
|
2021-08-20 11:26:56 +01:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/config"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/db"
|
[performance] refactoring + add fave / follow / request / visibility caching (#1607)
* refactor visibility checking, add caching for visibility
* invalidate visibility cache items on account / status deletes
* fix requester ID passed to visibility cache nil ptr
* de-interface caches, fix home / public timeline caching + visibility
* finish adding code comments for visibility filter
* fix angry goconst linter warnings
* actually finish adding filter visibility code comments for timeline functions
* move home timeline status author check to after visibility
* remove now-unused code
* add more code comments
* add TODO code comment, update printed cache start names
* update printed cache names on stop
* start adding separate follow(request) delete db functions, add specific visibility cache tests
* add relationship type caching
* fix getting local account follows / followed-bys, other small codebase improvements
* simplify invalidation using cache hooks, add more GetAccountBy___() functions
* fix boosting to return 404 if not boostable but no error (to not leak status ID)
* remove dead code
* improved placement of cache invalidation
* update license headers
* add example follow, follow-request config entries
* add example visibility cache configuration to config file
* use specific PutFollowRequest() instead of just Put()
* add tests for all GetAccountBy()
* add GetBlockBy() tests
* update block to check primitive fields
* update and finish adding Get{Account,Block,Follow,FollowRequest}By() tests
* fix copy-pasted code
* update envparsing test
* whitespace
* fix bun struct tag
* add license header to gtscontext
* fix old license header
* improved error creation to not use fmt.Errorf() when not needed
* fix various rebase conflicts, fix account test
* remove commented-out code, fix-up mention caching
* fix mention select bun statement
* ensure mention target account populated, pass in context to customrenderer logging
* remove more uncommented code, fix typeutil test
* add statusfave database model caching
* add status fave cache configuration
* add status fave cache example config
* woops, catch missed error. nice catch linter!
* add back testrig panic on nil db
* update example configuration to match defaults, slight tweak to cache configuration defaults
* update envparsing test with new defaults
* fetch followingget to use the follow target account
* use accounnt.IsLocal() instead of empty domain check
* use constants for the cache visibility type check
* use bun.In() for notification type restriction in db query
* include replies when fetching PublicTimeline() (to account for single-author threads in Visibility{}.StatusPublicTimelineable())
* use bun query building for nested select statements to ensure working with postgres
* update public timeline future status checks to match visibility filter
* same as previous, for home timeline
* update public timeline tests to dynamically check for appropriate statuses
* migrate accounts to allow unique constraint on public_key
* provide minimal account with publicKey
---------
Signed-off-by: kim <grufwub@gmail.com>
Co-authored-by: tsmethurst <tobi.smethurst@protonmail.com>
2023-03-28 14:03:14 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtscontext"
|
2023-05-07 18:53:21 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
|
2021-08-20 11:26:56 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
2023-05-22 15:32:36 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/id"
|
2022-07-19 09:47:55 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/log"
|
2024-04-13 12:25:10 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/paging"
|
2022-12-08 17:35:14 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/state"
|
2023-05-07 18:53:21 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/util"
|
2021-08-25 14:34:33 +01:00
|
|
|
"github.com/uptrace/bun"
|
2022-06-10 09:56:49 +01:00
|
|
|
"github.com/uptrace/bun/dialect"
|
2021-08-20 11:26:56 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type accountDB struct {
|
2024-02-07 14:43:27 +00:00
|
|
|
db *bun.DB
|
2022-12-08 17:35:14 +00:00
|
|
|
state *state.State
|
2022-11-15 18:45:15 +00:00
|
|
|
}
|
|
|
|
|
2023-07-25 09:34:05 +01:00
|
|
|
func (a *accountDB) GetAccountByID(ctx context.Context, id string) (*gtsmodel.Account, error) {
|
2021-09-01 10:08:21 +01:00
|
|
|
return a.getAccount(
|
|
|
|
ctx,
|
2022-11-15 18:45:15 +00:00
|
|
|
"ID",
|
2021-09-01 10:08:21 +01:00
|
|
|
func(account *gtsmodel.Account) error {
|
2023-07-25 09:34:05 +01:00
|
|
|
return a.db.NewSelect().
|
[performance] refactoring + add fave / follow / request / visibility caching (#1607)
* refactor visibility checking, add caching for visibility
* invalidate visibility cache items on account / status deletes
* fix requester ID passed to visibility cache nil ptr
* de-interface caches, fix home / public timeline caching + visibility
* finish adding code comments for visibility filter
* fix angry goconst linter warnings
* actually finish adding filter visibility code comments for timeline functions
* move home timeline status author check to after visibility
* remove now-unused code
* add more code comments
* add TODO code comment, update printed cache start names
* update printed cache names on stop
* start adding separate follow(request) delete db functions, add specific visibility cache tests
* add relationship type caching
* fix getting local account follows / followed-bys, other small codebase improvements
* simplify invalidation using cache hooks, add more GetAccountBy___() functions
* fix boosting to return 404 if not boostable but no error (to not leak status ID)
* remove dead code
* improved placement of cache invalidation
* update license headers
* add example follow, follow-request config entries
* add example visibility cache configuration to config file
* use specific PutFollowRequest() instead of just Put()
* add tests for all GetAccountBy()
* add GetBlockBy() tests
* update block to check primitive fields
* update and finish adding Get{Account,Block,Follow,FollowRequest}By() tests
* fix copy-pasted code
* update envparsing test
* whitespace
* fix bun struct tag
* add license header to gtscontext
* fix old license header
* improved error creation to not use fmt.Errorf() when not needed
* fix various rebase conflicts, fix account test
* remove commented-out code, fix-up mention caching
* fix mention select bun statement
* ensure mention target account populated, pass in context to customrenderer logging
* remove more uncommented code, fix typeutil test
* add statusfave database model caching
* add status fave cache configuration
* add status fave cache example config
* woops, catch missed error. nice catch linter!
* add back testrig panic on nil db
* update example configuration to match defaults, slight tweak to cache configuration defaults
* update envparsing test with new defaults
* fetch followingget to use the follow target account
* use accounnt.IsLocal() instead of empty domain check
* use constants for the cache visibility type check
* use bun.In() for notification type restriction in db query
* include replies when fetching PublicTimeline() (to account for single-author threads in Visibility{}.StatusPublicTimelineable())
* use bun query building for nested select statements to ensure working with postgres
* update public timeline future status checks to match visibility filter
* same as previous, for home timeline
* update public timeline tests to dynamically check for appropriate statuses
* migrate accounts to allow unique constraint on public_key
* provide minimal account with publicKey
---------
Signed-off-by: kim <grufwub@gmail.com>
Co-authored-by: tsmethurst <tobi.smethurst@protonmail.com>
2023-03-28 14:03:14 +01:00
|
|
|
Model(account).
|
|
|
|
Where("? = ?", bun.Ident("account.id"), id).
|
|
|
|
Scan(ctx)
|
2021-09-01 10:08:21 +01:00
|
|
|
},
|
2022-11-15 18:45:15 +00:00
|
|
|
id,
|
2021-09-01 10:08:21 +01:00
|
|
|
)
|
2021-08-20 11:26:56 +01:00
|
|
|
}
|
|
|
|
|
2023-07-24 13:14:13 +01:00
|
|
|
func (a *accountDB) GetAccountsByIDs(ctx context.Context, ids []string) ([]*gtsmodel.Account, error) {
|
2024-04-13 10:36:10 +01:00
|
|
|
// Load all input account IDs via cache loader callback.
|
|
|
|
accounts, err := a.state.Caches.GTS.Account.LoadIDs("ID",
|
|
|
|
ids,
|
|
|
|
func(uncached []string) ([]*gtsmodel.Account, error) {
|
|
|
|
// Preallocate expected length of uncached accounts.
|
|
|
|
accounts := make([]*gtsmodel.Account, 0, len(uncached))
|
2023-07-24 13:14:13 +01:00
|
|
|
|
2024-04-13 10:36:10 +01:00
|
|
|
// Perform database query scanning
|
|
|
|
// the remaining (uncached) account IDs.
|
|
|
|
if err := a.db.NewSelect().
|
|
|
|
Model(&accounts).
|
|
|
|
Where("? IN (?)", bun.Ident("id"), bun.In(uncached)).
|
|
|
|
Scan(ctx); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return accounts, nil
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reorder the statuses by their
|
|
|
|
// IDs to ensure in correct order.
|
|
|
|
getID := func(a *gtsmodel.Account) string { return a.ID }
|
|
|
|
util.OrderBy(accounts, ids, getID)
|
2023-07-24 13:14:13 +01:00
|
|
|
|
2024-04-13 10:36:10 +01:00
|
|
|
if gtscontext.Barebones(ctx) {
|
|
|
|
// no need to fully populate.
|
|
|
|
return accounts, nil
|
2023-07-24 13:14:13 +01:00
|
|
|
}
|
|
|
|
|
2024-04-13 10:36:10 +01:00
|
|
|
// Populate all loaded accounts, removing those we fail to
|
|
|
|
// populate (removes needing so many nil checks everywhere).
|
|
|
|
accounts = slices.DeleteFunc(accounts, func(account *gtsmodel.Account) bool {
|
|
|
|
if err := a.PopulateAccount(ctx, account); err != nil {
|
|
|
|
log.Errorf(ctx, "error populating account %s: %v", account.ID, err)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
|
2023-07-24 13:14:13 +01:00
|
|
|
return accounts, nil
|
|
|
|
}
|
|
|
|
|
2023-07-25 09:34:05 +01:00
|
|
|
func (a *accountDB) GetAccountByURI(ctx context.Context, uri string) (*gtsmodel.Account, error) {
|
2021-09-01 10:08:21 +01:00
|
|
|
return a.getAccount(
|
|
|
|
ctx,
|
2022-11-15 18:45:15 +00:00
|
|
|
"URI",
|
2021-09-01 10:08:21 +01:00
|
|
|
func(account *gtsmodel.Account) error {
|
2023-07-25 09:34:05 +01:00
|
|
|
return a.db.NewSelect().
|
[performance] refactoring + add fave / follow / request / visibility caching (#1607)
* refactor visibility checking, add caching for visibility
* invalidate visibility cache items on account / status deletes
* fix requester ID passed to visibility cache nil ptr
* de-interface caches, fix home / public timeline caching + visibility
* finish adding code comments for visibility filter
* fix angry goconst linter warnings
* actually finish adding filter visibility code comments for timeline functions
* move home timeline status author check to after visibility
* remove now-unused code
* add more code comments
* add TODO code comment, update printed cache start names
* update printed cache names on stop
* start adding separate follow(request) delete db functions, add specific visibility cache tests
* add relationship type caching
* fix getting local account follows / followed-bys, other small codebase improvements
* simplify invalidation using cache hooks, add more GetAccountBy___() functions
* fix boosting to return 404 if not boostable but no error (to not leak status ID)
* remove dead code
* improved placement of cache invalidation
* update license headers
* add example follow, follow-request config entries
* add example visibility cache configuration to config file
* use specific PutFollowRequest() instead of just Put()
* add tests for all GetAccountBy()
* add GetBlockBy() tests
* update block to check primitive fields
* update and finish adding Get{Account,Block,Follow,FollowRequest}By() tests
* fix copy-pasted code
* update envparsing test
* whitespace
* fix bun struct tag
* add license header to gtscontext
* fix old license header
* improved error creation to not use fmt.Errorf() when not needed
* fix various rebase conflicts, fix account test
* remove commented-out code, fix-up mention caching
* fix mention select bun statement
* ensure mention target account populated, pass in context to customrenderer logging
* remove more uncommented code, fix typeutil test
* add statusfave database model caching
* add status fave cache configuration
* add status fave cache example config
* woops, catch missed error. nice catch linter!
* add back testrig panic on nil db
* update example configuration to match defaults, slight tweak to cache configuration defaults
* update envparsing test with new defaults
* fetch followingget to use the follow target account
* use accounnt.IsLocal() instead of empty domain check
* use constants for the cache visibility type check
* use bun.In() for notification type restriction in db query
* include replies when fetching PublicTimeline() (to account for single-author threads in Visibility{}.StatusPublicTimelineable())
* use bun query building for nested select statements to ensure working with postgres
* update public timeline future status checks to match visibility filter
* same as previous, for home timeline
* update public timeline tests to dynamically check for appropriate statuses
* migrate accounts to allow unique constraint on public_key
* provide minimal account with publicKey
---------
Signed-off-by: kim <grufwub@gmail.com>
Co-authored-by: tsmethurst <tobi.smethurst@protonmail.com>
2023-03-28 14:03:14 +01:00
|
|
|
Model(account).
|
|
|
|
Where("? = ?", bun.Ident("account.uri"), uri).
|
|
|
|
Scan(ctx)
|
2021-09-01 10:08:21 +01:00
|
|
|
},
|
2022-11-15 18:45:15 +00:00
|
|
|
uri,
|
2021-09-01 10:08:21 +01:00
|
|
|
)
|
|
|
|
}
|
2021-08-20 11:26:56 +01:00
|
|
|
|
2023-07-25 09:34:05 +01:00
|
|
|
func (a *accountDB) GetAccountByURL(ctx context.Context, url string) (*gtsmodel.Account, error) {
|
2021-09-01 10:08:21 +01:00
|
|
|
return a.getAccount(
|
|
|
|
ctx,
|
2022-11-15 18:45:15 +00:00
|
|
|
"URL",
|
2021-09-01 10:08:21 +01:00
|
|
|
func(account *gtsmodel.Account) error {
|
2023-07-25 09:34:05 +01:00
|
|
|
return a.db.NewSelect().
|
[performance] refactoring + add fave / follow / request / visibility caching (#1607)
* refactor visibility checking, add caching for visibility
* invalidate visibility cache items on account / status deletes
* fix requester ID passed to visibility cache nil ptr
* de-interface caches, fix home / public timeline caching + visibility
* finish adding code comments for visibility filter
* fix angry goconst linter warnings
* actually finish adding filter visibility code comments for timeline functions
* move home timeline status author check to after visibility
* remove now-unused code
* add more code comments
* add TODO code comment, update printed cache start names
* update printed cache names on stop
* start adding separate follow(request) delete db functions, add specific visibility cache tests
* add relationship type caching
* fix getting local account follows / followed-bys, other small codebase improvements
* simplify invalidation using cache hooks, add more GetAccountBy___() functions
* fix boosting to return 404 if not boostable but no error (to not leak status ID)
* remove dead code
* improved placement of cache invalidation
* update license headers
* add example follow, follow-request config entries
* add example visibility cache configuration to config file
* use specific PutFollowRequest() instead of just Put()
* add tests for all GetAccountBy()
* add GetBlockBy() tests
* update block to check primitive fields
* update and finish adding Get{Account,Block,Follow,FollowRequest}By() tests
* fix copy-pasted code
* update envparsing test
* whitespace
* fix bun struct tag
* add license header to gtscontext
* fix old license header
* improved error creation to not use fmt.Errorf() when not needed
* fix various rebase conflicts, fix account test
* remove commented-out code, fix-up mention caching
* fix mention select bun statement
* ensure mention target account populated, pass in context to customrenderer logging
* remove more uncommented code, fix typeutil test
* add statusfave database model caching
* add status fave cache configuration
* add status fave cache example config
* woops, catch missed error. nice catch linter!
* add back testrig panic on nil db
* update example configuration to match defaults, slight tweak to cache configuration defaults
* update envparsing test with new defaults
* fetch followingget to use the follow target account
* use accounnt.IsLocal() instead of empty domain check
* use constants for the cache visibility type check
* use bun.In() for notification type restriction in db query
* include replies when fetching PublicTimeline() (to account for single-author threads in Visibility{}.StatusPublicTimelineable())
* use bun query building for nested select statements to ensure working with postgres
* update public timeline future status checks to match visibility filter
* same as previous, for home timeline
* update public timeline tests to dynamically check for appropriate statuses
* migrate accounts to allow unique constraint on public_key
* provide minimal account with publicKey
---------
Signed-off-by: kim <grufwub@gmail.com>
Co-authored-by: tsmethurst <tobi.smethurst@protonmail.com>
2023-03-28 14:03:14 +01:00
|
|
|
Model(account).
|
|
|
|
Where("? = ?", bun.Ident("account.url"), url).
|
|
|
|
Scan(ctx)
|
2021-09-01 10:08:21 +01:00
|
|
|
},
|
2022-11-15 18:45:15 +00:00
|
|
|
url,
|
2021-09-01 10:08:21 +01:00
|
|
|
)
|
2021-08-20 11:26:56 +01:00
|
|
|
}
|
|
|
|
|
2023-07-25 09:34:05 +01:00
|
|
|
func (a *accountDB) GetAccountByUsernameDomain(ctx context.Context, username string, domain string) (*gtsmodel.Account, error) {
|
2023-05-07 18:53:21 +01:00
|
|
|
if domain != "" {
|
|
|
|
// Normalize the domain as punycode
|
|
|
|
var err error
|
|
|
|
domain, err = util.Punify(domain)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-20 21:47:19 +01:00
|
|
|
return a.getAccount(
|
|
|
|
ctx,
|
2024-01-19 12:57:29 +00:00
|
|
|
"Username,Domain",
|
2022-08-20 21:47:19 +01:00
|
|
|
func(account *gtsmodel.Account) error {
|
2023-07-25 09:34:05 +01:00
|
|
|
q := a.db.NewSelect().
|
[performance] refactoring + add fave / follow / request / visibility caching (#1607)
* refactor visibility checking, add caching for visibility
* invalidate visibility cache items on account / status deletes
* fix requester ID passed to visibility cache nil ptr
* de-interface caches, fix home / public timeline caching + visibility
* finish adding code comments for visibility filter
* fix angry goconst linter warnings
* actually finish adding filter visibility code comments for timeline functions
* move home timeline status author check to after visibility
* remove now-unused code
* add more code comments
* add TODO code comment, update printed cache start names
* update printed cache names on stop
* start adding separate follow(request) delete db functions, add specific visibility cache tests
* add relationship type caching
* fix getting local account follows / followed-bys, other small codebase improvements
* simplify invalidation using cache hooks, add more GetAccountBy___() functions
* fix boosting to return 404 if not boostable but no error (to not leak status ID)
* remove dead code
* improved placement of cache invalidation
* update license headers
* add example follow, follow-request config entries
* add example visibility cache configuration to config file
* use specific PutFollowRequest() instead of just Put()
* add tests for all GetAccountBy()
* add GetBlockBy() tests
* update block to check primitive fields
* update and finish adding Get{Account,Block,Follow,FollowRequest}By() tests
* fix copy-pasted code
* update envparsing test
* whitespace
* fix bun struct tag
* add license header to gtscontext
* fix old license header
* improved error creation to not use fmt.Errorf() when not needed
* fix various rebase conflicts, fix account test
* remove commented-out code, fix-up mention caching
* fix mention select bun statement
* ensure mention target account populated, pass in context to customrenderer logging
* remove more uncommented code, fix typeutil test
* add statusfave database model caching
* add status fave cache configuration
* add status fave cache example config
* woops, catch missed error. nice catch linter!
* add back testrig panic on nil db
* update example configuration to match defaults, slight tweak to cache configuration defaults
* update envparsing test with new defaults
* fetch followingget to use the follow target account
* use accounnt.IsLocal() instead of empty domain check
* use constants for the cache visibility type check
* use bun.In() for notification type restriction in db query
* include replies when fetching PublicTimeline() (to account for single-author threads in Visibility{}.StatusPublicTimelineable())
* use bun query building for nested select statements to ensure working with postgres
* update public timeline future status checks to match visibility filter
* same as previous, for home timeline
* update public timeline tests to dynamically check for appropriate statuses
* migrate accounts to allow unique constraint on public_key
* provide minimal account with publicKey
---------
Signed-off-by: kim <grufwub@gmail.com>
Co-authored-by: tsmethurst <tobi.smethurst@protonmail.com>
2023-03-28 14:03:14 +01:00
|
|
|
Model(account)
|
2022-08-20 21:47:19 +01:00
|
|
|
|
|
|
|
if domain != "" {
|
2022-12-01 15:06:09 +00:00
|
|
|
q = q.
|
|
|
|
Where("LOWER(?) = ?", bun.Ident("account.username"), strings.ToLower(username)).
|
|
|
|
Where("? = ?", bun.Ident("account.domain"), domain)
|
2022-08-20 21:47:19 +01:00
|
|
|
} else {
|
2022-12-01 15:06:09 +00:00
|
|
|
q = q.
|
|
|
|
Where("? = ?", bun.Ident("account.username"), strings.ToLower(username)). // usernames on our instance are always lowercase
|
|
|
|
Where("? IS NULL", bun.Ident("account.domain"))
|
2022-08-20 21:47:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return q.Scan(ctx)
|
|
|
|
},
|
2022-11-15 18:45:15 +00:00
|
|
|
username,
|
|
|
|
domain,
|
2022-08-20 21:47:19 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-07-25 09:34:05 +01:00
|
|
|
func (a *accountDB) GetAccountByPubkeyID(ctx context.Context, id string) (*gtsmodel.Account, error) {
|
2022-09-02 10:58:42 +01:00
|
|
|
return a.getAccount(
|
|
|
|
ctx,
|
2022-11-15 18:45:15 +00:00
|
|
|
"PublicKeyURI",
|
2022-09-02 10:58:42 +01:00
|
|
|
func(account *gtsmodel.Account) error {
|
2023-07-25 09:34:05 +01:00
|
|
|
return a.db.NewSelect().
|
[performance] refactoring + add fave / follow / request / visibility caching (#1607)
* refactor visibility checking, add caching for visibility
* invalidate visibility cache items on account / status deletes
* fix requester ID passed to visibility cache nil ptr
* de-interface caches, fix home / public timeline caching + visibility
* finish adding code comments for visibility filter
* fix angry goconst linter warnings
* actually finish adding filter visibility code comments for timeline functions
* move home timeline status author check to after visibility
* remove now-unused code
* add more code comments
* add TODO code comment, update printed cache start names
* update printed cache names on stop
* start adding separate follow(request) delete db functions, add specific visibility cache tests
* add relationship type caching
* fix getting local account follows / followed-bys, other small codebase improvements
* simplify invalidation using cache hooks, add more GetAccountBy___() functions
* fix boosting to return 404 if not boostable but no error (to not leak status ID)
* remove dead code
* improved placement of cache invalidation
* update license headers
* add example follow, follow-request config entries
* add example visibility cache configuration to config file
* use specific PutFollowRequest() instead of just Put()
* add tests for all GetAccountBy()
* add GetBlockBy() tests
* update block to check primitive fields
* update and finish adding Get{Account,Block,Follow,FollowRequest}By() tests
* fix copy-pasted code
* update envparsing test
* whitespace
* fix bun struct tag
* add license header to gtscontext
* fix old license header
* improved error creation to not use fmt.Errorf() when not needed
* fix various rebase conflicts, fix account test
* remove commented-out code, fix-up mention caching
* fix mention select bun statement
* ensure mention target account populated, pass in context to customrenderer logging
* remove more uncommented code, fix typeutil test
* add statusfave database model caching
* add status fave cache configuration
* add status fave cache example config
* woops, catch missed error. nice catch linter!
* add back testrig panic on nil db
* update example configuration to match defaults, slight tweak to cache configuration defaults
* update envparsing test with new defaults
* fetch followingget to use the follow target account
* use accounnt.IsLocal() instead of empty domain check
* use constants for the cache visibility type check
* use bun.In() for notification type restriction in db query
* include replies when fetching PublicTimeline() (to account for single-author threads in Visibility{}.StatusPublicTimelineable())
* use bun query building for nested select statements to ensure working with postgres
* update public timeline future status checks to match visibility filter
* same as previous, for home timeline
* update public timeline tests to dynamically check for appropriate statuses
* migrate accounts to allow unique constraint on public_key
* provide minimal account with publicKey
---------
Signed-off-by: kim <grufwub@gmail.com>
Co-authored-by: tsmethurst <tobi.smethurst@protonmail.com>
2023-03-28 14:03:14 +01:00
|
|
|
Model(account).
|
|
|
|
Where("? = ?", bun.Ident("account.public_key_uri"), id).
|
|
|
|
Scan(ctx)
|
2022-09-02 10:58:42 +01:00
|
|
|
},
|
2022-11-15 18:45:15 +00:00
|
|
|
id,
|
2022-09-02 10:58:42 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-07-25 09:34:05 +01:00
|
|
|
func (a *accountDB) GetAccountByInboxURI(ctx context.Context, uri string) (*gtsmodel.Account, error) {
|
[performance] refactoring + add fave / follow / request / visibility caching (#1607)
* refactor visibility checking, add caching for visibility
* invalidate visibility cache items on account / status deletes
* fix requester ID passed to visibility cache nil ptr
* de-interface caches, fix home / public timeline caching + visibility
* finish adding code comments for visibility filter
* fix angry goconst linter warnings
* actually finish adding filter visibility code comments for timeline functions
* move home timeline status author check to after visibility
* remove now-unused code
* add more code comments
* add TODO code comment, update printed cache start names
* update printed cache names on stop
* start adding separate follow(request) delete db functions, add specific visibility cache tests
* add relationship type caching
* fix getting local account follows / followed-bys, other small codebase improvements
* simplify invalidation using cache hooks, add more GetAccountBy___() functions
* fix boosting to return 404 if not boostable but no error (to not leak status ID)
* remove dead code
* improved placement of cache invalidation
* update license headers
* add example follow, follow-request config entries
* add example visibility cache configuration to config file
* use specific PutFollowRequest() instead of just Put()
* add tests for all GetAccountBy()
* add GetBlockBy() tests
* update block to check primitive fields
* update and finish adding Get{Account,Block,Follow,FollowRequest}By() tests
* fix copy-pasted code
* update envparsing test
* whitespace
* fix bun struct tag
* add license header to gtscontext
* fix old license header
* improved error creation to not use fmt.Errorf() when not needed
* fix various rebase conflicts, fix account test
* remove commented-out code, fix-up mention caching
* fix mention select bun statement
* ensure mention target account populated, pass in context to customrenderer logging
* remove more uncommented code, fix typeutil test
* add statusfave database model caching
* add status fave cache configuration
* add status fave cache example config
* woops, catch missed error. nice catch linter!
* add back testrig panic on nil db
* update example configuration to match defaults, slight tweak to cache configuration defaults
* update envparsing test with new defaults
* fetch followingget to use the follow target account
* use accounnt.IsLocal() instead of empty domain check
* use constants for the cache visibility type check
* use bun.In() for notification type restriction in db query
* include replies when fetching PublicTimeline() (to account for single-author threads in Visibility{}.StatusPublicTimelineable())
* use bun query building for nested select statements to ensure working with postgres
* update public timeline future status checks to match visibility filter
* same as previous, for home timeline
* update public timeline tests to dynamically check for appropriate statuses
* migrate accounts to allow unique constraint on public_key
* provide minimal account with publicKey
---------
Signed-off-by: kim <grufwub@gmail.com>
Co-authored-by: tsmethurst <tobi.smethurst@protonmail.com>
2023-03-28 14:03:14 +01:00
|
|
|
return a.getAccount(
|
|
|
|
ctx,
|
|
|
|
"InboxURI",
|
|
|
|
func(account *gtsmodel.Account) error {
|
2023-07-25 09:34:05 +01:00
|
|
|
return a.db.NewSelect().
|
[performance] refactoring + add fave / follow / request / visibility caching (#1607)
* refactor visibility checking, add caching for visibility
* invalidate visibility cache items on account / status deletes
* fix requester ID passed to visibility cache nil ptr
* de-interface caches, fix home / public timeline caching + visibility
* finish adding code comments for visibility filter
* fix angry goconst linter warnings
* actually finish adding filter visibility code comments for timeline functions
* move home timeline status author check to after visibility
* remove now-unused code
* add more code comments
* add TODO code comment, update printed cache start names
* update printed cache names on stop
* start adding separate follow(request) delete db functions, add specific visibility cache tests
* add relationship type caching
* fix getting local account follows / followed-bys, other small codebase improvements
* simplify invalidation using cache hooks, add more GetAccountBy___() functions
* fix boosting to return 404 if not boostable but no error (to not leak status ID)
* remove dead code
* improved placement of cache invalidation
* update license headers
* add example follow, follow-request config entries
* add example visibility cache configuration to config file
* use specific PutFollowRequest() instead of just Put()
* add tests for all GetAccountBy()
* add GetBlockBy() tests
* update block to check primitive fields
* update and finish adding Get{Account,Block,Follow,FollowRequest}By() tests
* fix copy-pasted code
* update envparsing test
* whitespace
* fix bun struct tag
* add license header to gtscontext
* fix old license header
* improved error creation to not use fmt.Errorf() when not needed
* fix various rebase conflicts, fix account test
* remove commented-out code, fix-up mention caching
* fix mention select bun statement
* ensure mention target account populated, pass in context to customrenderer logging
* remove more uncommented code, fix typeutil test
* add statusfave database model caching
* add status fave cache configuration
* add status fave cache example config
* woops, catch missed error. nice catch linter!
* add back testrig panic on nil db
* update example configuration to match defaults, slight tweak to cache configuration defaults
* update envparsing test with new defaults
* fetch followingget to use the follow target account
* use accounnt.IsLocal() instead of empty domain check
* use constants for the cache visibility type check
* use bun.In() for notification type restriction in db query
* include replies when fetching PublicTimeline() (to account for single-author threads in Visibility{}.StatusPublicTimelineable())
* use bun query building for nested select statements to ensure working with postgres
* update public timeline future status checks to match visibility filter
* same as previous, for home timeline
* update public timeline tests to dynamically check for appropriate statuses
* migrate accounts to allow unique constraint on public_key
* provide minimal account with publicKey
---------
Signed-off-by: kim <grufwub@gmail.com>
Co-authored-by: tsmethurst <tobi.smethurst@protonmail.com>
2023-03-28 14:03:14 +01:00
|
|
|
Model(account).
|
|
|
|
Where("? = ?", bun.Ident("account.inbox_uri"), uri).
|
|
|
|
Scan(ctx)
|
|
|
|
},
|
|
|
|
uri,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-07-25 09:34:05 +01:00
|
|
|
func (a *accountDB) GetAccountByOutboxURI(ctx context.Context, uri string) (*gtsmodel.Account, error) {
|
[performance] refactoring + add fave / follow / request / visibility caching (#1607)
* refactor visibility checking, add caching for visibility
* invalidate visibility cache items on account / status deletes
* fix requester ID passed to visibility cache nil ptr
* de-interface caches, fix home / public timeline caching + visibility
* finish adding code comments for visibility filter
* fix angry goconst linter warnings
* actually finish adding filter visibility code comments for timeline functions
* move home timeline status author check to after visibility
* remove now-unused code
* add more code comments
* add TODO code comment, update printed cache start names
* update printed cache names on stop
* start adding separate follow(request) delete db functions, add specific visibility cache tests
* add relationship type caching
* fix getting local account follows / followed-bys, other small codebase improvements
* simplify invalidation using cache hooks, add more GetAccountBy___() functions
* fix boosting to return 404 if not boostable but no error (to not leak status ID)
* remove dead code
* improved placement of cache invalidation
* update license headers
* add example follow, follow-request config entries
* add example visibility cache configuration to config file
* use specific PutFollowRequest() instead of just Put()
* add tests for all GetAccountBy()
* add GetBlockBy() tests
* update block to check primitive fields
* update and finish adding Get{Account,Block,Follow,FollowRequest}By() tests
* fix copy-pasted code
* update envparsing test
* whitespace
* fix bun struct tag
* add license header to gtscontext
* fix old license header
* improved error creation to not use fmt.Errorf() when not needed
* fix various rebase conflicts, fix account test
* remove commented-out code, fix-up mention caching
* fix mention select bun statement
* ensure mention target account populated, pass in context to customrenderer logging
* remove more uncommented code, fix typeutil test
* add statusfave database model caching
* add status fave cache configuration
* add status fave cache example config
* woops, catch missed error. nice catch linter!
* add back testrig panic on nil db
* update example configuration to match defaults, slight tweak to cache configuration defaults
* update envparsing test with new defaults
* fetch followingget to use the follow target account
* use accounnt.IsLocal() instead of empty domain check
* use constants for the cache visibility type check
* use bun.In() for notification type restriction in db query
* include replies when fetching PublicTimeline() (to account for single-author threads in Visibility{}.StatusPublicTimelineable())
* use bun query building for nested select statements to ensure working with postgres
* update public timeline future status checks to match visibility filter
* same as previous, for home timeline
* update public timeline tests to dynamically check for appropriate statuses
* migrate accounts to allow unique constraint on public_key
* provide minimal account with publicKey
---------
Signed-off-by: kim <grufwub@gmail.com>
Co-authored-by: tsmethurst <tobi.smethurst@protonmail.com>
2023-03-28 14:03:14 +01:00
|
|
|
return a.getAccount(
|
|
|
|
ctx,
|
|
|
|
"OutboxURI",
|
|
|
|
func(account *gtsmodel.Account) error {
|
2023-07-25 09:34:05 +01:00
|
|
|
return a.db.NewSelect().
|
[performance] refactoring + add fave / follow / request / visibility caching (#1607)
* refactor visibility checking, add caching for visibility
* invalidate visibility cache items on account / status deletes
* fix requester ID passed to visibility cache nil ptr
* de-interface caches, fix home / public timeline caching + visibility
* finish adding code comments for visibility filter
* fix angry goconst linter warnings
* actually finish adding filter visibility code comments for timeline functions
* move home timeline status author check to after visibility
* remove now-unused code
* add more code comments
* add TODO code comment, update printed cache start names
* update printed cache names on stop
* start adding separate follow(request) delete db functions, add specific visibility cache tests
* add relationship type caching
* fix getting local account follows / followed-bys, other small codebase improvements
* simplify invalidation using cache hooks, add more GetAccountBy___() functions
* fix boosting to return 404 if not boostable but no error (to not leak status ID)
* remove dead code
* improved placement of cache invalidation
* update license headers
* add example follow, follow-request config entries
* add example visibility cache configuration to config file
* use specific PutFollowRequest() instead of just Put()
* add tests for all GetAccountBy()
* add GetBlockBy() tests
* update block to check primitive fields
* update and finish adding Get{Account,Block,Follow,FollowRequest}By() tests
* fix copy-pasted code
* update envparsing test
* whitespace
* fix bun struct tag
* add license header to gtscontext
* fix old license header
* improved error creation to not use fmt.Errorf() when not needed
* fix various rebase conflicts, fix account test
* remove commented-out code, fix-up mention caching
* fix mention select bun statement
* ensure mention target account populated, pass in context to customrenderer logging
* remove more uncommented code, fix typeutil test
* add statusfave database model caching
* add status fave cache configuration
* add status fave cache example config
* woops, catch missed error. nice catch linter!
* add back testrig panic on nil db
* update example configuration to match defaults, slight tweak to cache configuration defaults
* update envparsing test with new defaults
* fetch followingget to use the follow target account
* use accounnt.IsLocal() instead of empty domain check
* use constants for the cache visibility type check
* use bun.In() for notification type restriction in db query
* include replies when fetching PublicTimeline() (to account for single-author threads in Visibility{}.StatusPublicTimelineable())
* use bun query building for nested select statements to ensure working with postgres
* update public timeline future status checks to match visibility filter
* same as previous, for home timeline
* update public timeline tests to dynamically check for appropriate statuses
* migrate accounts to allow unique constraint on public_key
* provide minimal account with publicKey
---------
Signed-off-by: kim <grufwub@gmail.com>
Co-authored-by: tsmethurst <tobi.smethurst@protonmail.com>
2023-03-28 14:03:14 +01:00
|
|
|
Model(account).
|
|
|
|
Where("? = ?", bun.Ident("account.outbox_uri"), uri).
|
|
|
|
Scan(ctx)
|
|
|
|
},
|
|
|
|
uri,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-07-25 09:34:05 +01:00
|
|
|
func (a *accountDB) GetAccountByFollowersURI(ctx context.Context, uri string) (*gtsmodel.Account, error) {
|
[performance] refactoring + add fave / follow / request / visibility caching (#1607)
* refactor visibility checking, add caching for visibility
* invalidate visibility cache items on account / status deletes
* fix requester ID passed to visibility cache nil ptr
* de-interface caches, fix home / public timeline caching + visibility
* finish adding code comments for visibility filter
* fix angry goconst linter warnings
* actually finish adding filter visibility code comments for timeline functions
* move home timeline status author check to after visibility
* remove now-unused code
* add more code comments
* add TODO code comment, update printed cache start names
* update printed cache names on stop
* start adding separate follow(request) delete db functions, add specific visibility cache tests
* add relationship type caching
* fix getting local account follows / followed-bys, other small codebase improvements
* simplify invalidation using cache hooks, add more GetAccountBy___() functions
* fix boosting to return 404 if not boostable but no error (to not leak status ID)
* remove dead code
* improved placement of cache invalidation
* update license headers
* add example follow, follow-request config entries
* add example visibility cache configuration to config file
* use specific PutFollowRequest() instead of just Put()
* add tests for all GetAccountBy()
* add GetBlockBy() tests
* update block to check primitive fields
* update and finish adding Get{Account,Block,Follow,FollowRequest}By() tests
* fix copy-pasted code
* update envparsing test
* whitespace
* fix bun struct tag
* add license header to gtscontext
* fix old license header
* improved error creation to not use fmt.Errorf() when not needed
* fix various rebase conflicts, fix account test
* remove commented-out code, fix-up mention caching
* fix mention select bun statement
* ensure mention target account populated, pass in context to customrenderer logging
* remove more uncommented code, fix typeutil test
* add statusfave database model caching
* add status fave cache configuration
* add status fave cache example config
* woops, catch missed error. nice catch linter!
* add back testrig panic on nil db
* update example configuration to match defaults, slight tweak to cache configuration defaults
* update envparsing test with new defaults
* fetch followingget to use the follow target account
* use accounnt.IsLocal() instead of empty domain check
* use constants for the cache visibility type check
* use bun.In() for notification type restriction in db query
* include replies when fetching PublicTimeline() (to account for single-author threads in Visibility{}.StatusPublicTimelineable())
* use bun query building for nested select statements to ensure working with postgres
* update public timeline future status checks to match visibility filter
* same as previous, for home timeline
* update public timeline tests to dynamically check for appropriate statuses
* migrate accounts to allow unique constraint on public_key
* provide minimal account with publicKey
---------
Signed-off-by: kim <grufwub@gmail.com>
Co-authored-by: tsmethurst <tobi.smethurst@protonmail.com>
2023-03-28 14:03:14 +01:00
|
|
|
return a.getAccount(
|
|
|
|
ctx,
|
|
|
|
"FollowersURI",
|
|
|
|
func(account *gtsmodel.Account) error {
|
2023-07-25 09:34:05 +01:00
|
|
|
return a.db.NewSelect().
|
[performance] refactoring + add fave / follow / request / visibility caching (#1607)
* refactor visibility checking, add caching for visibility
* invalidate visibility cache items on account / status deletes
* fix requester ID passed to visibility cache nil ptr
* de-interface caches, fix home / public timeline caching + visibility
* finish adding code comments for visibility filter
* fix angry goconst linter warnings
* actually finish adding filter visibility code comments for timeline functions
* move home timeline status author check to after visibility
* remove now-unused code
* add more code comments
* add TODO code comment, update printed cache start names
* update printed cache names on stop
* start adding separate follow(request) delete db functions, add specific visibility cache tests
* add relationship type caching
* fix getting local account follows / followed-bys, other small codebase improvements
* simplify invalidation using cache hooks, add more GetAccountBy___() functions
* fix boosting to return 404 if not boostable but no error (to not leak status ID)
* remove dead code
* improved placement of cache invalidation
* update license headers
* add example follow, follow-request config entries
* add example visibility cache configuration to config file
* use specific PutFollowRequest() instead of just Put()
* add tests for all GetAccountBy()
* add GetBlockBy() tests
* update block to check primitive fields
* update and finish adding Get{Account,Block,Follow,FollowRequest}By() tests
* fix copy-pasted code
* update envparsing test
* whitespace
* fix bun struct tag
* add license header to gtscontext
* fix old license header
* improved error creation to not use fmt.Errorf() when not needed
* fix various rebase conflicts, fix account test
* remove commented-out code, fix-up mention caching
* fix mention select bun statement
* ensure mention target account populated, pass in context to customrenderer logging
* remove more uncommented code, fix typeutil test
* add statusfave database model caching
* add status fave cache configuration
* add status fave cache example config
* woops, catch missed error. nice catch linter!
* add back testrig panic on nil db
* update example configuration to match defaults, slight tweak to cache configuration defaults
* update envparsing test with new defaults
* fetch followingget to use the follow target account
* use accounnt.IsLocal() instead of empty domain check
* use constants for the cache visibility type check
* use bun.In() for notification type restriction in db query
* include replies when fetching PublicTimeline() (to account for single-author threads in Visibility{}.StatusPublicTimelineable())
* use bun query building for nested select statements to ensure working with postgres
* update public timeline future status checks to match visibility filter
* same as previous, for home timeline
* update public timeline tests to dynamically check for appropriate statuses
* migrate accounts to allow unique constraint on public_key
* provide minimal account with publicKey
---------
Signed-off-by: kim <grufwub@gmail.com>
Co-authored-by: tsmethurst <tobi.smethurst@protonmail.com>
2023-03-28 14:03:14 +01:00
|
|
|
Model(account).
|
|
|
|
Where("? = ?", bun.Ident("account.followers_uri"), uri).
|
|
|
|
Scan(ctx)
|
|
|
|
},
|
|
|
|
uri,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-07-25 09:34:05 +01:00
|
|
|
func (a *accountDB) GetAccountByFollowingURI(ctx context.Context, uri string) (*gtsmodel.Account, error) {
|
[performance] refactoring + add fave / follow / request / visibility caching (#1607)
* refactor visibility checking, add caching for visibility
* invalidate visibility cache items on account / status deletes
* fix requester ID passed to visibility cache nil ptr
* de-interface caches, fix home / public timeline caching + visibility
* finish adding code comments for visibility filter
* fix angry goconst linter warnings
* actually finish adding filter visibility code comments for timeline functions
* move home timeline status author check to after visibility
* remove now-unused code
* add more code comments
* add TODO code comment, update printed cache start names
* update printed cache names on stop
* start adding separate follow(request) delete db functions, add specific visibility cache tests
* add relationship type caching
* fix getting local account follows / followed-bys, other small codebase improvements
* simplify invalidation using cache hooks, add more GetAccountBy___() functions
* fix boosting to return 404 if not boostable but no error (to not leak status ID)
* remove dead code
* improved placement of cache invalidation
* update license headers
* add example follow, follow-request config entries
* add example visibility cache configuration to config file
* use specific PutFollowRequest() instead of just Put()
* add tests for all GetAccountBy()
* add GetBlockBy() tests
* update block to check primitive fields
* update and finish adding Get{Account,Block,Follow,FollowRequest}By() tests
* fix copy-pasted code
* update envparsing test
* whitespace
* fix bun struct tag
* add license header to gtscontext
* fix old license header
* improved error creation to not use fmt.Errorf() when not needed
* fix various rebase conflicts, fix account test
* remove commented-out code, fix-up mention caching
* fix mention select bun statement
* ensure mention target account populated, pass in context to customrenderer logging
* remove more uncommented code, fix typeutil test
* add statusfave database model caching
* add status fave cache configuration
* add status fave cache example config
* woops, catch missed error. nice catch linter!
* add back testrig panic on nil db
* update example configuration to match defaults, slight tweak to cache configuration defaults
* update envparsing test with new defaults
* fetch followingget to use the follow target account
* use accounnt.IsLocal() instead of empty domain check
* use constants for the cache visibility type check
* use bun.In() for notification type restriction in db query
* include replies when fetching PublicTimeline() (to account for single-author threads in Visibility{}.StatusPublicTimelineable())
* use bun query building for nested select statements to ensure working with postgres
* update public timeline future status checks to match visibility filter
* same as previous, for home timeline
* update public timeline tests to dynamically check for appropriate statuses
* migrate accounts to allow unique constraint on public_key
* provide minimal account with publicKey
---------
Signed-off-by: kim <grufwub@gmail.com>
Co-authored-by: tsmethurst <tobi.smethurst@protonmail.com>
2023-03-28 14:03:14 +01:00
|
|
|
return a.getAccount(
|
|
|
|
ctx,
|
|
|
|
"FollowingURI",
|
|
|
|
func(account *gtsmodel.Account) error {
|
2023-07-25 09:34:05 +01:00
|
|
|
return a.db.NewSelect().
|
[performance] refactoring + add fave / follow / request / visibility caching (#1607)
* refactor visibility checking, add caching for visibility
* invalidate visibility cache items on account / status deletes
* fix requester ID passed to visibility cache nil ptr
* de-interface caches, fix home / public timeline caching + visibility
* finish adding code comments for visibility filter
* fix angry goconst linter warnings
* actually finish adding filter visibility code comments for timeline functions
* move home timeline status author check to after visibility
* remove now-unused code
* add more code comments
* add TODO code comment, update printed cache start names
* update printed cache names on stop
* start adding separate follow(request) delete db functions, add specific visibility cache tests
* add relationship type caching
* fix getting local account follows / followed-bys, other small codebase improvements
* simplify invalidation using cache hooks, add more GetAccountBy___() functions
* fix boosting to return 404 if not boostable but no error (to not leak status ID)
* remove dead code
* improved placement of cache invalidation
* update license headers
* add example follow, follow-request config entries
* add example visibility cache configuration to config file
* use specific PutFollowRequest() instead of just Put()
* add tests for all GetAccountBy()
* add GetBlockBy() tests
* update block to check primitive fields
* update and finish adding Get{Account,Block,Follow,FollowRequest}By() tests
* fix copy-pasted code
* update envparsing test
* whitespace
* fix bun struct tag
* add license header to gtscontext
* fix old license header
* improved error creation to not use fmt.Errorf() when not needed
* fix various rebase conflicts, fix account test
* remove commented-out code, fix-up mention caching
* fix mention select bun statement
* ensure mention target account populated, pass in context to customrenderer logging
* remove more uncommented code, fix typeutil test
* add statusfave database model caching
* add status fave cache configuration
* add status fave cache example config
* woops, catch missed error. nice catch linter!
* add back testrig panic on nil db
* update example configuration to match defaults, slight tweak to cache configuration defaults
* update envparsing test with new defaults
* fetch followingget to use the follow target account
* use accounnt.IsLocal() instead of empty domain check
* use constants for the cache visibility type check
* use bun.In() for notification type restriction in db query
* include replies when fetching PublicTimeline() (to account for single-author threads in Visibility{}.StatusPublicTimelineable())
* use bun query building for nested select statements to ensure working with postgres
* update public timeline future status checks to match visibility filter
* same as previous, for home timeline
* update public timeline tests to dynamically check for appropriate statuses
* migrate accounts to allow unique constraint on public_key
* provide minimal account with publicKey
---------
Signed-off-by: kim <grufwub@gmail.com>
Co-authored-by: tsmethurst <tobi.smethurst@protonmail.com>
2023-03-28 14:03:14 +01:00
|
|
|
Model(account).
|
|
|
|
Where("? = ?", bun.Ident("account.following_uri"), uri).
|
|
|
|
Scan(ctx)
|
|
|
|
},
|
|
|
|
uri,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-07-25 09:34:05 +01:00
|
|
|
func (a *accountDB) GetInstanceAccount(ctx context.Context, domain string) (*gtsmodel.Account, error) {
|
2022-11-15 18:45:15 +00:00
|
|
|
var username string
|
2021-08-20 11:26:56 +01:00
|
|
|
|
2022-11-15 18:45:15 +00:00
|
|
|
if domain == "" {
|
|
|
|
// I.e. our local instance account
|
|
|
|
username = config.GetHost()
|
|
|
|
} else {
|
|
|
|
// A remote instance account
|
|
|
|
username = domain
|
|
|
|
}
|
|
|
|
|
|
|
|
return a.GetAccountByUsernameDomain(ctx, username, domain)
|
|
|
|
}
|
|
|
|
|
2024-04-13 12:25:10 +01:00
|
|
|
func (a *accountDB) GetAccounts(
|
|
|
|
ctx context.Context,
|
|
|
|
origin string,
|
|
|
|
status string,
|
|
|
|
mods bool,
|
|
|
|
invitedBy string,
|
|
|
|
username string,
|
|
|
|
displayName string,
|
|
|
|
domain string,
|
|
|
|
email string,
|
|
|
|
ip netip.Addr,
|
|
|
|
page *paging.Page,
|
|
|
|
) (
|
|
|
|
[]*gtsmodel.Account,
|
|
|
|
error,
|
|
|
|
) {
|
|
|
|
var (
|
|
|
|
// local users lists,
|
|
|
|
// required for some
|
|
|
|
// limiting parameters.
|
|
|
|
users []*gtsmodel.User
|
|
|
|
|
|
|
|
// lazyLoadUsers only loads the users
|
|
|
|
// slice if it's required by params.
|
|
|
|
lazyLoadUsers = func() (err error) {
|
|
|
|
if users == nil {
|
|
|
|
users, err = a.state.DB.GetAllUsers(gtscontext.SetBarebones(ctx))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error getting users: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get paging params.
|
|
|
|
//
|
|
|
|
// Note this may be min_id OR since_id
|
|
|
|
// from the API, this gets handled below
|
|
|
|
// when checking order to reverse slice.
|
|
|
|
minID = page.GetMin()
|
|
|
|
maxID = page.GetMax()
|
|
|
|
limit = page.GetLimit()
|
|
|
|
order = page.GetOrder()
|
|
|
|
|
|
|
|
// Make educated guess for slice size
|
|
|
|
accountIDs = make([]string, 0, limit)
|
|
|
|
accountIDIn []string
|
|
|
|
|
|
|
|
useAccountIDIn bool
|
|
|
|
)
|
|
|
|
|
|
|
|
q := a.db.
|
|
|
|
NewSelect().
|
|
|
|
TableExpr("? AS ?", bun.Ident("accounts"), bun.Ident("account")).
|
|
|
|
// Select only IDs from table
|
|
|
|
Column("account.id")
|
|
|
|
|
|
|
|
// Return only accounts OLDER
|
|
|
|
// than account with maxID.
|
|
|
|
if maxID != "" {
|
|
|
|
maxIDAcct, err := a.GetAccountByID(
|
|
|
|
gtscontext.SetBarebones(ctx),
|
|
|
|
maxID,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error getting maxID account %s: %w", maxID, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
q = q.Where("? < ?", bun.Ident("account.created_at"), maxIDAcct.CreatedAt)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return only accounts NEWER
|
|
|
|
// than account with minID.
|
|
|
|
if minID != "" {
|
|
|
|
minIDAcct, err := a.GetAccountByID(
|
|
|
|
gtscontext.SetBarebones(ctx),
|
|
|
|
minID,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error getting minID account %s: %w", minID, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
q = q.Where("? > ?", bun.Ident("account.created_at"), minIDAcct.CreatedAt)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch status {
|
|
|
|
|
|
|
|
case "active":
|
|
|
|
// Get only enabled accounts.
|
|
|
|
if err := lazyLoadUsers(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, user := range users {
|
|
|
|
if !*user.Disabled {
|
|
|
|
accountIDIn = append(accountIDIn, user.AccountID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
useAccountIDIn = true
|
|
|
|
|
|
|
|
case "pending":
|
|
|
|
// Get only unapproved accounts.
|
|
|
|
if err := lazyLoadUsers(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, user := range users {
|
|
|
|
if !*user.Approved {
|
|
|
|
accountIDIn = append(accountIDIn, user.AccountID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
useAccountIDIn = true
|
|
|
|
|
|
|
|
case "disabled":
|
|
|
|
// Get only disabled accounts.
|
|
|
|
if err := lazyLoadUsers(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, user := range users {
|
|
|
|
if *user.Disabled {
|
|
|
|
accountIDIn = append(accountIDIn, user.AccountID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
useAccountIDIn = true
|
|
|
|
|
|
|
|
case "silenced":
|
|
|
|
// Get only silenced accounts.
|
|
|
|
q = q.Where("? IS NOT NULL", bun.Ident("account.silenced_at"))
|
|
|
|
|
|
|
|
case "suspended":
|
|
|
|
// Get only suspended accounts.
|
|
|
|
q = q.Where("? IS NOT NULL", bun.Ident("account.suspended_at"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if mods {
|
|
|
|
// Get only mod accounts.
|
|
|
|
if err := lazyLoadUsers(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, user := range users {
|
|
|
|
if *user.Moderator || *user.Admin {
|
|
|
|
accountIDIn = append(accountIDIn, user.AccountID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
useAccountIDIn = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: invitedBy
|
|
|
|
|
|
|
|
if username != "" {
|
|
|
|
q = q.Where("? = ?", bun.Ident("account.username"), username)
|
|
|
|
}
|
|
|
|
|
|
|
|
if displayName != "" {
|
|
|
|
q = q.Where("? = ?", bun.Ident("account.display_name"), displayName)
|
|
|
|
}
|
|
|
|
|
|
|
|
if domain != "" {
|
|
|
|
q = q.Where("? = ?", bun.Ident("account.domain"), domain)
|
|
|
|
}
|
|
|
|
|
|
|
|
if email != "" {
|
|
|
|
if err := lazyLoadUsers(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, user := range users {
|
|
|
|
if user.Email == email || user.UnconfirmedEmail == email {
|
|
|
|
accountIDIn = append(accountIDIn, user.AccountID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
useAccountIDIn = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use ip if not zero value.
|
|
|
|
if ip.IsValid() {
|
|
|
|
if err := lazyLoadUsers(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, user := range users {
|
|
|
|
if user.SignUpIP.String() == ip.String() {
|
|
|
|
accountIDIn = append(accountIDIn, user.AccountID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
useAccountIDIn = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if origin == "local" && !useAccountIDIn {
|
|
|
|
// In the case we're not already limiting
|
|
|
|
// by specific subset of account IDs, just
|
|
|
|
// use existing list of user.AccountIDs
|
|
|
|
// instead of adding WHERE to the query.
|
|
|
|
if err := lazyLoadUsers(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, user := range users {
|
|
|
|
accountIDIn = append(accountIDIn, user.AccountID)
|
|
|
|
}
|
|
|
|
useAccountIDIn = true
|
|
|
|
|
|
|
|
} else if origin == "remote" {
|
|
|
|
if useAccountIDIn {
|
|
|
|
// useAccountIDIn specifically indicates
|
|
|
|
// a parameter that limits querying to
|
|
|
|
// local accounts, there will be none.
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get only remote accounts.
|
|
|
|
q = q.Where("? IS NOT NULL", bun.Ident("account.domain"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if useAccountIDIn {
|
|
|
|
if len(accountIDIn) == 0 {
|
|
|
|
// There will be no
|
|
|
|
// possible answer.
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
q = q.Where("? IN (?)", bun.Ident("account.id"), bun.In(accountIDIn))
|
|
|
|
}
|
|
|
|
|
|
|
|
if limit > 0 {
|
|
|
|
// Limit amount of
|
|
|
|
// accounts returned.
|
|
|
|
q = q.Limit(limit)
|
|
|
|
}
|
|
|
|
|
|
|
|
if order == paging.OrderAscending {
|
|
|
|
// Page up.
|
|
|
|
q = q.Order("account.created_at ASC")
|
|
|
|
} else {
|
|
|
|
// Page down.
|
|
|
|
q = q.Order("account.created_at DESC")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := q.Scan(ctx, &accountIDs); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(accountIDs) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we're paging up, we still want accounts
|
|
|
|
// to be sorted by createdAt desc, so reverse ids slice.
|
|
|
|
if order == paging.OrderAscending {
|
|
|
|
slices.Reverse(accountIDs)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return account IDs loaded from cache + db.
|
|
|
|
return a.state.DB.GetAccountsByIDs(ctx, accountIDs)
|
|
|
|
}
|
|
|
|
|
2023-07-25 09:34:05 +01:00
|
|
|
func (a *accountDB) getAccount(ctx context.Context, lookup string, dbQuery func(*gtsmodel.Account) error, keyParts ...any) (*gtsmodel.Account, error) {
|
2022-11-18 17:29:25 +00:00
|
|
|
// Fetch account from database cache with loader callback
|
2024-01-19 12:57:29 +00:00
|
|
|
account, err := a.state.Caches.GTS.Account.LoadOne(lookup, func() (*gtsmodel.Account, error) {
|
2022-11-15 18:45:15 +00:00
|
|
|
var account gtsmodel.Account
|
2021-08-20 11:26:56 +01:00
|
|
|
|
2021-09-01 10:08:21 +01:00
|
|
|
// Not cached! Perform database query
|
2022-11-15 18:45:15 +00:00
|
|
|
if err := dbQuery(&account); err != nil {
|
2023-08-17 17:26:21 +01:00
|
|
|
return nil, err
|
2021-09-01 10:08:21 +01:00
|
|
|
}
|
|
|
|
|
2022-11-15 18:45:15 +00:00
|
|
|
return &account, nil
|
|
|
|
}, keyParts...)
|
2022-11-18 17:29:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
[performance] refactoring + add fave / follow / request / visibility caching (#1607)
* refactor visibility checking, add caching for visibility
* invalidate visibility cache items on account / status deletes
* fix requester ID passed to visibility cache nil ptr
* de-interface caches, fix home / public timeline caching + visibility
* finish adding code comments for visibility filter
* fix angry goconst linter warnings
* actually finish adding filter visibility code comments for timeline functions
* move home timeline status author check to after visibility
* remove now-unused code
* add more code comments
* add TODO code comment, update printed cache start names
* update printed cache names on stop
* start adding separate follow(request) delete db functions, add specific visibility cache tests
* add relationship type caching
* fix getting local account follows / followed-bys, other small codebase improvements
* simplify invalidation using cache hooks, add more GetAccountBy___() functions
* fix boosting to return 404 if not boostable but no error (to not leak status ID)
* remove dead code
* improved placement of cache invalidation
* update license headers
* add example follow, follow-request config entries
* add example visibility cache configuration to config file
* use specific PutFollowRequest() instead of just Put()
* add tests for all GetAccountBy()
* add GetBlockBy() tests
* update block to check primitive fields
* update and finish adding Get{Account,Block,Follow,FollowRequest}By() tests
* fix copy-pasted code
* update envparsing test
* whitespace
* fix bun struct tag
* add license header to gtscontext
* fix old license header
* improved error creation to not use fmt.Errorf() when not needed
* fix various rebase conflicts, fix account test
* remove commented-out code, fix-up mention caching
* fix mention select bun statement
* ensure mention target account populated, pass in context to customrenderer logging
* remove more uncommented code, fix typeutil test
* add statusfave database model caching
* add status fave cache configuration
* add status fave cache example config
* woops, catch missed error. nice catch linter!
* add back testrig panic on nil db
* update example configuration to match defaults, slight tweak to cache configuration defaults
* update envparsing test with new defaults
* fetch followingget to use the follow target account
* use accounnt.IsLocal() instead of empty domain check
* use constants for the cache visibility type check
* use bun.In() for notification type restriction in db query
* include replies when fetching PublicTimeline() (to account for single-author threads in Visibility{}.StatusPublicTimelineable())
* use bun query building for nested select statements to ensure working with postgres
* update public timeline future status checks to match visibility filter
* same as previous, for home timeline
* update public timeline tests to dynamically check for appropriate statuses
* migrate accounts to allow unique constraint on public_key
* provide minimal account with publicKey
---------
Signed-off-by: kim <grufwub@gmail.com>
Co-authored-by: tsmethurst <tobi.smethurst@protonmail.com>
2023-03-28 14:03:14 +01:00
|
|
|
if gtscontext.Barebones(ctx) {
|
|
|
|
// no need to fully populate.
|
|
|
|
return account, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Further populate the account fields where applicable.
|
|
|
|
if err := a.PopulateAccount(ctx, account); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return account, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *accountDB) PopulateAccount(ctx context.Context, account *gtsmodel.Account) error {
|
2023-05-07 18:53:21 +01:00
|
|
|
var (
|
|
|
|
err error
|
2024-01-16 16:22:44 +00:00
|
|
|
errs = gtserror.NewMultiError(5)
|
2023-05-07 18:53:21 +01:00
|
|
|
)
|
[performance] refactoring + add fave / follow / request / visibility caching (#1607)
* refactor visibility checking, add caching for visibility
* invalidate visibility cache items on account / status deletes
* fix requester ID passed to visibility cache nil ptr
* de-interface caches, fix home / public timeline caching + visibility
* finish adding code comments for visibility filter
* fix angry goconst linter warnings
* actually finish adding filter visibility code comments for timeline functions
* move home timeline status author check to after visibility
* remove now-unused code
* add more code comments
* add TODO code comment, update printed cache start names
* update printed cache names on stop
* start adding separate follow(request) delete db functions, add specific visibility cache tests
* add relationship type caching
* fix getting local account follows / followed-bys, other small codebase improvements
* simplify invalidation using cache hooks, add more GetAccountBy___() functions
* fix boosting to return 404 if not boostable but no error (to not leak status ID)
* remove dead code
* improved placement of cache invalidation
* update license headers
* add example follow, follow-request config entries
* add example visibility cache configuration to config file
* use specific PutFollowRequest() instead of just Put()
* add tests for all GetAccountBy()
* add GetBlockBy() tests
* update block to check primitive fields
* update and finish adding Get{Account,Block,Follow,FollowRequest}By() tests
* fix copy-pasted code
* update envparsing test
* whitespace
* fix bun struct tag
* add license header to gtscontext
* fix old license header
* improved error creation to not use fmt.Errorf() when not needed
* fix various rebase conflicts, fix account test
* remove commented-out code, fix-up mention caching
* fix mention select bun statement
* ensure mention target account populated, pass in context to customrenderer logging
* remove more uncommented code, fix typeutil test
* add statusfave database model caching
* add status fave cache configuration
* add status fave cache example config
* woops, catch missed error. nice catch linter!
* add back testrig panic on nil db
* update example configuration to match defaults, slight tweak to cache configuration defaults
* update envparsing test with new defaults
* fetch followingget to use the follow target account
* use accounnt.IsLocal() instead of empty domain check
* use constants for the cache visibility type check
* use bun.In() for notification type restriction in db query
* include replies when fetching PublicTimeline() (to account for single-author threads in Visibility{}.StatusPublicTimelineable())
* use bun query building for nested select statements to ensure working with postgres
* update public timeline future status checks to match visibility filter
* same as previous, for home timeline
* update public timeline tests to dynamically check for appropriate statuses
* migrate accounts to allow unique constraint on public_key
* provide minimal account with publicKey
---------
Signed-off-by: kim <grufwub@gmail.com>
Co-authored-by: tsmethurst <tobi.smethurst@protonmail.com>
2023-03-28 14:03:14 +01:00
|
|
|
|
|
|
|
if account.AvatarMediaAttachment == nil && account.AvatarMediaAttachmentID != "" {
|
|
|
|
// Account avatar attachment is not set, fetch from database.
|
|
|
|
account.AvatarMediaAttachment, err = a.state.DB.GetAttachmentByID(
|
|
|
|
ctx, // these are already barebones
|
|
|
|
account.AvatarMediaAttachmentID,
|
|
|
|
)
|
2023-02-03 20:03:05 +00:00
|
|
|
if err != nil {
|
2023-08-02 16:21:46 +01:00
|
|
|
errs.Appendf("error populating account avatar: %w", err)
|
2023-02-03 20:03:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
[performance] refactoring + add fave / follow / request / visibility caching (#1607)
* refactor visibility checking, add caching for visibility
* invalidate visibility cache items on account / status deletes
* fix requester ID passed to visibility cache nil ptr
* de-interface caches, fix home / public timeline caching + visibility
* finish adding code comments for visibility filter
* fix angry goconst linter warnings
* actually finish adding filter visibility code comments for timeline functions
* move home timeline status author check to after visibility
* remove now-unused code
* add more code comments
* add TODO code comment, update printed cache start names
* update printed cache names on stop
* start adding separate follow(request) delete db functions, add specific visibility cache tests
* add relationship type caching
* fix getting local account follows / followed-bys, other small codebase improvements
* simplify invalidation using cache hooks, add more GetAccountBy___() functions
* fix boosting to return 404 if not boostable but no error (to not leak status ID)
* remove dead code
* improved placement of cache invalidation
* update license headers
* add example follow, follow-request config entries
* add example visibility cache configuration to config file
* use specific PutFollowRequest() instead of just Put()
* add tests for all GetAccountBy()
* add GetBlockBy() tests
* update block to check primitive fields
* update and finish adding Get{Account,Block,Follow,FollowRequest}By() tests
* fix copy-pasted code
* update envparsing test
* whitespace
* fix bun struct tag
* add license header to gtscontext
* fix old license header
* improved error creation to not use fmt.Errorf() when not needed
* fix various rebase conflicts, fix account test
* remove commented-out code, fix-up mention caching
* fix mention select bun statement
* ensure mention target account populated, pass in context to customrenderer logging
* remove more uncommented code, fix typeutil test
* add statusfave database model caching
* add status fave cache configuration
* add status fave cache example config
* woops, catch missed error. nice catch linter!
* add back testrig panic on nil db
* update example configuration to match defaults, slight tweak to cache configuration defaults
* update envparsing test with new defaults
* fetch followingget to use the follow target account
* use accounnt.IsLocal() instead of empty domain check
* use constants for the cache visibility type check
* use bun.In() for notification type restriction in db query
* include replies when fetching PublicTimeline() (to account for single-author threads in Visibility{}.StatusPublicTimelineable())
* use bun query building for nested select statements to ensure working with postgres
* update public timeline future status checks to match visibility filter
* same as previous, for home timeline
* update public timeline tests to dynamically check for appropriate statuses
* migrate accounts to allow unique constraint on public_key
* provide minimal account with publicKey
---------
Signed-off-by: kim <grufwub@gmail.com>
Co-authored-by: tsmethurst <tobi.smethurst@protonmail.com>
2023-03-28 14:03:14 +01:00
|
|
|
if account.HeaderMediaAttachment == nil && account.HeaderMediaAttachmentID != "" {
|
|
|
|
// Account header attachment is not set, fetch from database.
|
|
|
|
account.HeaderMediaAttachment, err = a.state.DB.GetAttachmentByID(
|
|
|
|
ctx, // these are already barebones
|
|
|
|
account.HeaderMediaAttachmentID,
|
|
|
|
)
|
2023-02-03 20:03:05 +00:00
|
|
|
if err != nil {
|
2023-08-02 16:21:46 +01:00
|
|
|
errs.Appendf("error populating account header: %w", err)
|
2023-02-03 20:03:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-06 09:45:46 +00:00
|
|
|
// Only try to populate AlsoKnownAs for local accounts,
|
|
|
|
// since those are the only accounts to which it's relevant.
|
|
|
|
//
|
|
|
|
// AKA from remotes might have loads of random-ass values
|
|
|
|
// set here, and we don't want to do lots of failing DB calls.
|
|
|
|
if account.IsLocal() && !account.AlsoKnownAsPopulated() {
|
2024-01-16 16:22:44 +00:00
|
|
|
// Account alsoKnownAs accounts are
|
|
|
|
// out-of-date with URIs, repopulate.
|
|
|
|
alsoKnownAs := make([]*gtsmodel.Account, 0)
|
|
|
|
for _, uri := range account.AlsoKnownAsURIs {
|
|
|
|
akaAcct, err := a.state.DB.GetAccountByURI(
|
|
|
|
gtscontext.SetBarebones(ctx),
|
|
|
|
uri,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
errs.Appendf("error populating also known as account %s: %w", uri, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
alsoKnownAs = append(alsoKnownAs, akaAcct)
|
|
|
|
}
|
|
|
|
|
|
|
|
account.AlsoKnownAs = alsoKnownAs
|
|
|
|
}
|
|
|
|
|
2024-03-06 10:18:57 +00:00
|
|
|
if account.Move == nil && account.MoveID != "" {
|
|
|
|
// Account move is not set, fetch from database.
|
|
|
|
account.Move, err = a.state.DB.GetMoveByID(
|
|
|
|
ctx,
|
2024-03-06 14:40:37 +00:00
|
|
|
account.MoveID,
|
2024-03-06 10:18:57 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
errs.Appendf("error populating move: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-16 16:22:44 +00:00
|
|
|
if account.MovedTo == nil && account.MovedToURI != "" {
|
2024-03-10 10:59:55 +00:00
|
|
|
// Account movedTo is not set, try to fetch from database,
|
|
|
|
// but only error on real errors since this field is optional.
|
2024-01-16 16:22:44 +00:00
|
|
|
account.MovedTo, err = a.state.DB.GetAccountByURI(
|
|
|
|
gtscontext.SetBarebones(ctx),
|
|
|
|
account.MovedToURI,
|
|
|
|
)
|
2024-03-10 10:59:55 +00:00
|
|
|
if err != nil && !errors.Is(err, db.ErrNoEntries) {
|
2024-01-16 16:22:44 +00:00
|
|
|
errs.Appendf("error populating moved to account: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
[performance] refactoring + add fave / follow / request / visibility caching (#1607)
* refactor visibility checking, add caching for visibility
* invalidate visibility cache items on account / status deletes
* fix requester ID passed to visibility cache nil ptr
* de-interface caches, fix home / public timeline caching + visibility
* finish adding code comments for visibility filter
* fix angry goconst linter warnings
* actually finish adding filter visibility code comments for timeline functions
* move home timeline status author check to after visibility
* remove now-unused code
* add more code comments
* add TODO code comment, update printed cache start names
* update printed cache names on stop
* start adding separate follow(request) delete db functions, add specific visibility cache tests
* add relationship type caching
* fix getting local account follows / followed-bys, other small codebase improvements
* simplify invalidation using cache hooks, add more GetAccountBy___() functions
* fix boosting to return 404 if not boostable but no error (to not leak status ID)
* remove dead code
* improved placement of cache invalidation
* update license headers
* add example follow, follow-request config entries
* add example visibility cache configuration to config file
* use specific PutFollowRequest() instead of just Put()
* add tests for all GetAccountBy()
* add GetBlockBy() tests
* update block to check primitive fields
* update and finish adding Get{Account,Block,Follow,FollowRequest}By() tests
* fix copy-pasted code
* update envparsing test
* whitespace
* fix bun struct tag
* add license header to gtscontext
* fix old license header
* improved error creation to not use fmt.Errorf() when not needed
* fix various rebase conflicts, fix account test
* remove commented-out code, fix-up mention caching
* fix mention select bun statement
* ensure mention target account populated, pass in context to customrenderer logging
* remove more uncommented code, fix typeutil test
* add statusfave database model caching
* add status fave cache configuration
* add status fave cache example config
* woops, catch missed error. nice catch linter!
* add back testrig panic on nil db
* update example configuration to match defaults, slight tweak to cache configuration defaults
* update envparsing test with new defaults
* fetch followingget to use the follow target account
* use accounnt.IsLocal() instead of empty domain check
* use constants for the cache visibility type check
* use bun.In() for notification type restriction in db query
* include replies when fetching PublicTimeline() (to account for single-author threads in Visibility{}.StatusPublicTimelineable())
* use bun query building for nested select statements to ensure working with postgres
* update public timeline future status checks to match visibility filter
* same as previous, for home timeline
* update public timeline tests to dynamically check for appropriate statuses
* migrate accounts to allow unique constraint on public_key
* provide minimal account with publicKey
---------
Signed-off-by: kim <grufwub@gmail.com>
Co-authored-by: tsmethurst <tobi.smethurst@protonmail.com>
2023-03-28 14:03:14 +01:00
|
|
|
if !account.EmojisPopulated() {
|
|
|
|
// Account emojis are out-of-date with IDs, repopulate.
|
|
|
|
account.Emojis, err = a.state.DB.GetEmojisByIDs(
|
|
|
|
ctx, // these are already barebones
|
|
|
|
account.EmojiIDs,
|
|
|
|
)
|
2022-11-18 17:29:25 +00:00
|
|
|
if err != nil {
|
2023-08-02 16:21:46 +01:00
|
|
|
errs.Appendf("error populating account emojis: %w", err)
|
2022-11-18 17:29:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-22 13:03:46 +00:00
|
|
|
if account.IsLocal() && account.Settings == nil && !account.IsInstance() {
|
|
|
|
// Account settings not set, fetch from db.
|
|
|
|
account.Settings, err = a.state.DB.GetAccountSettings(
|
|
|
|
ctx, // these are already barebones
|
|
|
|
account.ID,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
errs.Appendf("error populating account settings: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-09 18:14:33 +01:00
|
|
|
return errs.Combine()
|
2021-08-20 11:26:56 +01:00
|
|
|
}
|
|
|
|
|
2023-07-25 09:34:05 +01:00
|
|
|
func (a *accountDB) PutAccount(ctx context.Context, account *gtsmodel.Account) error {
|
2024-01-19 12:57:29 +00:00
|
|
|
return a.state.Caches.GTS.Account.Store(account, func() error {
|
2022-11-15 18:45:15 +00:00
|
|
|
// It is safe to run this database transaction within cache.Store
|
|
|
|
// as the cache does not attempt a mutex lock until AFTER hook.
|
|
|
|
//
|
2024-02-07 14:43:27 +00:00
|
|
|
return a.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
|
2022-11-15 18:45:15 +00:00
|
|
|
// create links between this account and any emojis it uses
|
|
|
|
for _, i := range account.EmojiIDs {
|
|
|
|
if _, err := tx.NewInsert().Model(>smodel.AccountToEmoji{
|
|
|
|
AccountID: account.ID,
|
|
|
|
EmojiID: i,
|
|
|
|
}).Exec(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-26 10:56:01 +01:00
|
|
|
}
|
|
|
|
|
2022-11-15 18:45:15 +00:00
|
|
|
// insert the account
|
|
|
|
_, err := tx.NewInsert().Model(account).Exec(ctx)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
})
|
2022-09-26 10:56:01 +01:00
|
|
|
}
|
|
|
|
|
2023-07-25 09:34:05 +01:00
|
|
|
func (a *accountDB) UpdateAccount(ctx context.Context, account *gtsmodel.Account, columns ...string) error {
|
2021-08-25 14:34:33 +01:00
|
|
|
account.UpdatedAt = time.Now()
|
2023-03-20 18:10:08 +00:00
|
|
|
if len(columns) > 0 {
|
|
|
|
// If we're updating by column, ensure "updated_at" is included.
|
|
|
|
columns = append(columns, "updated_at")
|
|
|
|
}
|
2021-08-25 14:34:33 +01:00
|
|
|
|
2024-01-19 12:57:29 +00:00
|
|
|
return a.state.Caches.GTS.Account.Store(account, func() error {
|
2022-11-15 18:45:15 +00:00
|
|
|
// It is safe to run this database transaction within cache.Store
|
|
|
|
// as the cache does not attempt a mutex lock until AFTER hook.
|
|
|
|
//
|
2024-02-07 14:43:27 +00:00
|
|
|
return a.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
|
2022-11-15 18:45:15 +00:00
|
|
|
// create links between this account and any emojis it uses
|
|
|
|
// first clear out any old emoji links
|
2022-10-08 12:50:48 +01:00
|
|
|
if _, err := tx.
|
2022-11-15 18:45:15 +00:00
|
|
|
NewDelete().
|
|
|
|
TableExpr("? AS ?", bun.Ident("account_to_emojis"), bun.Ident("account_to_emoji")).
|
|
|
|
Where("? = ?", bun.Ident("account_to_emoji.account_id"), account.ID).
|
|
|
|
Exec(ctx); err != nil {
|
2022-09-26 10:56:01 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-15 18:45:15 +00:00
|
|
|
// now populate new emoji links
|
|
|
|
for _, i := range account.EmojiIDs {
|
|
|
|
if _, err := tx.
|
|
|
|
NewInsert().
|
|
|
|
Model(>smodel.AccountToEmoji{
|
|
|
|
AccountID: account.ID,
|
|
|
|
EmojiID: i,
|
|
|
|
}).Exec(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2021-09-01 10:08:21 +01:00
|
|
|
|
2022-11-15 18:45:15 +00:00
|
|
|
// update the account
|
|
|
|
_, err := tx.NewUpdate().
|
|
|
|
Model(account).
|
|
|
|
Where("? = ?", bun.Ident("account.id"), account.ID).
|
2023-03-20 18:10:08 +00:00
|
|
|
Column(columns...).
|
2022-11-15 18:45:15 +00:00
|
|
|
Exec(ctx)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
})
|
2021-08-25 14:34:33 +01:00
|
|
|
}
|
|
|
|
|
2023-07-25 09:34:05 +01:00
|
|
|
func (a *accountDB) DeleteAccount(ctx context.Context, id string) error {
|
2024-01-19 12:57:29 +00:00
|
|
|
defer a.state.Caches.GTS.Account.Invalidate("ID", id)
|
2023-05-12 10:15:54 +01:00
|
|
|
|
|
|
|
// Load account into cache before attempting a delete,
|
|
|
|
// as we need it cached in order to trigger the invalidate
|
|
|
|
// callback. This in turn invalidates others.
|
|
|
|
_, err := a.GetAccountByID(gtscontext.SetBarebones(ctx), id)
|
|
|
|
if err != nil && !errors.Is(err, db.ErrNoEntries) {
|
|
|
|
// NOTE: even if db.ErrNoEntries is returned, we
|
|
|
|
// still run the below transaction to ensure related
|
|
|
|
// objects are appropriately deleted.
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-02-07 14:43:27 +00:00
|
|
|
return a.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
|
2022-10-08 12:50:48 +01:00
|
|
|
// clear out any emoji links
|
|
|
|
if _, err := tx.
|
|
|
|
NewDelete().
|
|
|
|
TableExpr("? AS ?", bun.Ident("account_to_emojis"), bun.Ident("account_to_emoji")).
|
|
|
|
Where("? = ?", bun.Ident("account_to_emoji.account_id"), id).
|
|
|
|
Exec(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// delete the account
|
|
|
|
_, err := tx.
|
2022-11-15 18:45:15 +00:00
|
|
|
NewDelete().
|
2022-10-08 12:50:48 +01:00
|
|
|
TableExpr("? AS ?", bun.Ident("accounts"), bun.Ident("account")).
|
|
|
|
Where("? = ?", bun.Ident("account.id"), id).
|
|
|
|
Exec(ctx)
|
|
|
|
return err
|
2023-05-12 10:15:54 +01:00
|
|
|
})
|
2022-10-08 12:50:48 +01:00
|
|
|
}
|
|
|
|
|
2023-07-25 09:34:05 +01:00
|
|
|
func (a *accountDB) GetAccountLastPosted(ctx context.Context, accountID string, webOnly bool) (time.Time, error) {
|
2022-10-08 13:00:39 +01:00
|
|
|
createdAt := time.Time{}
|
2021-08-20 11:26:56 +01:00
|
|
|
|
2023-07-25 09:34:05 +01:00
|
|
|
q := a.db.
|
2021-08-25 14:34:33 +01:00
|
|
|
NewSelect().
|
2022-10-08 13:00:39 +01:00
|
|
|
TableExpr("? AS ?", bun.Ident("statuses"), bun.Ident("status")).
|
2022-10-08 12:50:48 +01:00
|
|
|
Column("status.created_at").
|
|
|
|
Where("? = ?", bun.Ident("status.account_id"), accountID).
|
|
|
|
Order("status.id DESC").
|
|
|
|
Limit(1)
|
2021-08-20 11:26:56 +01:00
|
|
|
|
2022-10-08 13:00:39 +01:00
|
|
|
if webOnly {
|
|
|
|
q = q.
|
2023-07-05 11:34:37 +01:00
|
|
|
Where("? IS NULL", bun.Ident("status.in_reply_to_uri")).
|
|
|
|
Where("? IS NULL", bun.Ident("status.boost_of_id")).
|
2022-10-08 13:00:39 +01:00
|
|
|
Where("? = ?", bun.Ident("status.visibility"), gtsmodel.VisibilityPublic).
|
|
|
|
Where("? = ?", bun.Ident("status.federated"), true)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := q.Scan(ctx, &createdAt); err != nil {
|
2023-08-17 17:26:21 +01:00
|
|
|
return time.Time{}, err
|
2021-08-29 15:41:41 +01:00
|
|
|
}
|
2022-10-08 13:00:39 +01:00
|
|
|
return createdAt, nil
|
2021-08-20 11:26:56 +01:00
|
|
|
}
|
|
|
|
|
2023-07-25 09:34:05 +01:00
|
|
|
func (a *accountDB) SetAccountHeaderOrAvatar(ctx context.Context, mediaAttachment *gtsmodel.MediaAttachment, accountID string) error {
|
2022-08-15 11:35:05 +01:00
|
|
|
if *mediaAttachment.Avatar && *mediaAttachment.Header {
|
2021-08-20 11:26:56 +01:00
|
|
|
return errors.New("one media attachment cannot be both header and avatar")
|
|
|
|
}
|
|
|
|
|
2022-10-08 12:50:48 +01:00
|
|
|
var column bun.Ident
|
2021-11-22 07:46:19 +00:00
|
|
|
switch {
|
2022-08-15 11:35:05 +01:00
|
|
|
case *mediaAttachment.Avatar:
|
2022-10-08 12:50:48 +01:00
|
|
|
column = bun.Ident("account.avatar_media_attachment_id")
|
2022-08-15 11:35:05 +01:00
|
|
|
case *mediaAttachment.Header:
|
2022-10-08 12:50:48 +01:00
|
|
|
column = bun.Ident("account.header_media_attachment_id")
|
2021-11-22 07:46:19 +00:00
|
|
|
default:
|
2021-08-20 11:26:56 +01:00
|
|
|
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
|
2023-07-25 09:34:05 +01:00
|
|
|
if _, err := a.db.
|
2021-08-25 14:34:33 +01:00
|
|
|
NewInsert().
|
|
|
|
Model(mediaAttachment).
|
|
|
|
Exec(ctx); err != nil {
|
2023-08-17 17:26:21 +01:00
|
|
|
return err
|
2021-08-20 11:26:56 +01:00
|
|
|
}
|
2022-10-08 12:50:48 +01:00
|
|
|
|
2023-07-25 09:34:05 +01:00
|
|
|
if _, err := a.db.
|
2021-08-25 14:34:33 +01:00
|
|
|
NewUpdate().
|
2022-10-08 12:50:48 +01:00
|
|
|
TableExpr("? AS ?", bun.Ident("accounts"), bun.Ident("account")).
|
|
|
|
Set("? = ?", column, mediaAttachment.ID).
|
|
|
|
Where("? = ?", bun.Ident("account.id"), accountID).
|
2021-08-25 14:34:33 +01:00
|
|
|
Exec(ctx); err != nil {
|
2023-08-17 17:26:21 +01:00
|
|
|
return err
|
2021-08-20 11:26:56 +01:00
|
|
|
}
|
2021-08-29 15:41:41 +01:00
|
|
|
|
2021-08-20 11:26:56 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-25 09:34:05 +01:00
|
|
|
func (a *accountDB) GetAccountCustomCSSByUsername(ctx context.Context, username string) (string, error) {
|
2024-03-22 13:03:46 +00:00
|
|
|
// Get local account.
|
2022-09-12 12:14:29 +01:00
|
|
|
account, err := a.GetAccountByUsernameDomain(ctx, username, "")
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2024-03-22 13:03:46 +00:00
|
|
|
// Ensure settings populated, in case
|
|
|
|
// barebones context was passed.
|
|
|
|
if account.Settings == nil {
|
|
|
|
account.Settings, err = a.GetAccountSettings(ctx, account.ID)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return account.Settings.CustomCSS, nil
|
2022-09-12 12:14:29 +01:00
|
|
|
}
|
|
|
|
|
2023-07-24 13:14:13 +01:00
|
|
|
func (a *accountDB) GetAccountsUsingEmoji(ctx context.Context, emojiID string) ([]*gtsmodel.Account, error) {
|
|
|
|
var accountIDs []string
|
|
|
|
|
2023-08-02 15:11:23 +01:00
|
|
|
// SELECT all accounts using this emoji,
|
|
|
|
// using a relational table for improved perf.
|
|
|
|
if _, err := a.db.NewSelect().
|
|
|
|
Table("account_to_emojis").
|
|
|
|
Column("account_id").
|
|
|
|
Where("? = ?", bun.Ident("emoji_id"), emojiID).
|
|
|
|
Exec(ctx, &accountIDs); err != nil {
|
2023-08-17 17:26:21 +01:00
|
|
|
return nil, err
|
2023-07-24 13:14:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Convert account IDs into account objects.
|
|
|
|
return a.GetAccountsByIDs(ctx, accountIDs)
|
|
|
|
}
|
|
|
|
|
2023-07-25 09:34:05 +01:00
|
|
|
func (a *accountDB) GetAccountFaves(ctx context.Context, accountID string) ([]*gtsmodel.StatusFave, error) {
|
2021-08-25 14:34:33 +01:00
|
|
|
faves := new([]*gtsmodel.StatusFave)
|
2021-08-20 11:26:56 +01:00
|
|
|
|
2023-07-25 09:34:05 +01:00
|
|
|
if err := a.db.
|
2021-08-25 14:34:33 +01:00
|
|
|
NewSelect().
|
|
|
|
Model(faves).
|
2022-10-08 12:50:48 +01:00
|
|
|
Where("? = ?", bun.Ident("status_fave.account_id"), accountID).
|
2021-08-25 14:34:33 +01:00
|
|
|
Scan(ctx); err != nil {
|
2023-08-17 17:26:21 +01:00
|
|
|
return nil, err
|
2021-08-20 11:26:56 +01:00
|
|
|
}
|
2021-08-29 15:41:41 +01:00
|
|
|
|
2021-08-25 14:34:33 +01:00
|
|
|
return *faves, nil
|
2021-08-20 11:26:56 +01:00
|
|
|
}
|
|
|
|
|
2023-07-25 09:34:05 +01:00
|
|
|
func (a *accountDB) CountAccountStatuses(ctx context.Context, accountID string) (int, error) {
|
2024-02-12 11:52:12 +00:00
|
|
|
counts, err := a.getAccountStatusCounts(ctx, accountID)
|
|
|
|
return counts.Statuses, err
|
2021-08-20 11:26:56 +01:00
|
|
|
}
|
|
|
|
|
2023-07-25 09:34:05 +01:00
|
|
|
func (a *accountDB) CountAccountPinned(ctx context.Context, accountID string) (int, error) {
|
2024-02-12 11:52:12 +00:00
|
|
|
counts, err := a.getAccountStatusCounts(ctx, accountID)
|
|
|
|
return counts.Pinned, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *accountDB) getAccountStatusCounts(ctx context.Context, accountID string) (struct {
|
|
|
|
Statuses int
|
|
|
|
Pinned int
|
|
|
|
}, error) {
|
|
|
|
// Check for an already cached copy of account status counts.
|
|
|
|
counts, ok := a.state.Caches.GTS.AccountCounts.Get(accountID)
|
|
|
|
if ok {
|
|
|
|
return counts, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := a.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// Scan database for account statuses.
|
|
|
|
counts.Statuses, err = tx.NewSelect().
|
|
|
|
Table("statuses").
|
|
|
|
Where("? = ?", bun.Ident("account_id"), accountID).
|
|
|
|
Count(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scan database for pinned statuses.
|
|
|
|
counts.Pinned, err = tx.NewSelect().
|
|
|
|
Table("statuses").
|
|
|
|
Where("? = ?", bun.Ident("account_id"), accountID).
|
|
|
|
Where("? IS NOT NULL", bun.Ident("pinned_at")).
|
|
|
|
Count(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return counts, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store this account counts result in the cache.
|
|
|
|
a.state.Caches.GTS.AccountCounts.Set(accountID, counts)
|
|
|
|
|
|
|
|
return counts, nil
|
2023-02-25 12:16:30 +00:00
|
|
|
}
|
|
|
|
|
2023-07-25 09:34:05 +01:00
|
|
|
func (a *accountDB) GetAccountStatuses(ctx context.Context, accountID string, limit int, excludeReplies bool, excludeReblogs bool, maxID string, minID string, mediaOnly bool, publicOnly bool) ([]*gtsmodel.Status, error) {
|
2023-05-22 15:32:36 +01:00
|
|
|
// Ensure reasonable
|
|
|
|
if limit < 0 {
|
|
|
|
limit = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make educated guess for slice size
|
|
|
|
var (
|
|
|
|
statusIDs = make([]string, 0, limit)
|
|
|
|
frontToBack = true
|
|
|
|
)
|
2021-08-20 11:26:56 +01:00
|
|
|
|
2023-07-25 09:34:05 +01:00
|
|
|
q := a.db.
|
2021-08-25 14:34:33 +01:00
|
|
|
NewSelect().
|
2022-10-08 12:50:48 +01:00
|
|
|
TableExpr("? AS ?", bun.Ident("statuses"), bun.Ident("status")).
|
2023-05-22 15:32:36 +01:00
|
|
|
// Select only IDs from table
|
2022-10-08 12:50:48 +01:00
|
|
|
Column("status.id").
|
2023-05-22 15:32:36 +01:00
|
|
|
Where("? = ?", bun.Ident("status.account_id"), accountID)
|
2021-08-20 11:26:56 +01:00
|
|
|
|
2021-10-24 10:57:39 +01:00
|
|
|
if excludeReplies {
|
2024-02-27 17:18:40 +00:00
|
|
|
q = q.WhereGroup(" AND ", func(q *bun.SelectQuery) *bun.SelectQuery {
|
2022-08-27 10:35:31 +01:00
|
|
|
return q.
|
2023-05-22 15:32:36 +01:00
|
|
|
// Do include self replies (threads), but
|
|
|
|
// don't include replies to other people.
|
|
|
|
Where("? = ?", bun.Ident("status.in_reply_to_account_id"), accountID).
|
|
|
|
WhereOr("? IS NULL", bun.Ident("status.in_reply_to_uri"))
|
|
|
|
})
|
2024-02-27 17:18:40 +00:00
|
|
|
// Don't include replies that mention other people:
|
|
|
|
// for example, an account's reply to its own reply to someone else.
|
|
|
|
q = whereArrayIsNullOrEmpty(q, bun.Ident("status.mentions"))
|
2021-08-20 11:26:56 +01:00
|
|
|
}
|
|
|
|
|
2022-04-15 13:33:01 +01:00
|
|
|
if excludeReblogs {
|
2023-05-22 15:32:36 +01:00
|
|
|
q = q.Where("? IS NULL", bun.Ident("status.boost_of_id"))
|
2021-10-24 10:57:39 +01:00
|
|
|
}
|
|
|
|
|
2021-08-25 14:34:33 +01:00
|
|
|
if mediaOnly {
|
2023-05-22 15:32:36 +01:00
|
|
|
// Attachments are stored as a json object; this
|
|
|
|
// implementation differs between SQLite and Postgres,
|
2022-06-10 09:56:49 +01:00
|
|
|
// so we have to be thorough to cover all eventualities
|
2021-08-25 14:34:33 +01:00
|
|
|
q = q.WhereGroup(" AND ", func(q *bun.SelectQuery) *bun.SelectQuery {
|
2023-07-25 09:34:05 +01:00
|
|
|
switch a.db.Dialect().Name() {
|
2022-06-10 09:56:49 +01:00
|
|
|
case dialect.PG:
|
|
|
|
return q.
|
2022-10-08 12:50:48 +01:00
|
|
|
Where("? IS NOT NULL", bun.Ident("status.attachments")).
|
|
|
|
Where("? != '{}'", bun.Ident("status.attachments"))
|
2022-06-10 09:56:49 +01:00
|
|
|
case dialect.SQLite:
|
|
|
|
return q.
|
2022-10-08 12:50:48 +01:00
|
|
|
Where("? IS NOT NULL", bun.Ident("status.attachments")).
|
|
|
|
Where("? != ''", bun.Ident("status.attachments")).
|
|
|
|
Where("? != 'null'", bun.Ident("status.attachments")).
|
|
|
|
Where("? != '{}'", bun.Ident("status.attachments")).
|
|
|
|
Where("? != '[]'", bun.Ident("status.attachments"))
|
2022-06-10 09:56:49 +01:00
|
|
|
default:
|
2023-02-17 11:02:29 +00:00
|
|
|
log.Panic(ctx, "db dialect was neither pg nor sqlite")
|
2022-06-10 09:56:49 +01:00
|
|
|
return q
|
|
|
|
}
|
2021-08-25 14:34:33 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-10-24 10:57:39 +01:00
|
|
|
if publicOnly {
|
2022-10-08 12:50:48 +01:00
|
|
|
q = q.Where("? = ?", bun.Ident("status.visibility"), gtsmodel.VisibilityPublic)
|
2021-08-26 21:06:34 +01:00
|
|
|
}
|
|
|
|
|
2023-05-22 15:32:36 +01:00
|
|
|
// return only statuses LOWER (ie., older) than maxID
|
|
|
|
if maxID == "" {
|
|
|
|
maxID = id.Highest
|
|
|
|
}
|
|
|
|
q = q.Where("? < ?", bun.Ident("status.id"), maxID)
|
|
|
|
|
|
|
|
if minID != "" {
|
|
|
|
// return only statuses HIGHER (ie., newer) than minID
|
|
|
|
q = q.Where("? > ?", bun.Ident("status.id"), minID)
|
|
|
|
|
|
|
|
// page up
|
|
|
|
frontToBack = false
|
|
|
|
}
|
|
|
|
|
|
|
|
if limit > 0 {
|
|
|
|
// limit amount of statuses returned
|
|
|
|
q = q.Limit(limit)
|
|
|
|
}
|
|
|
|
|
|
|
|
if frontToBack {
|
|
|
|
// Page down.
|
|
|
|
q = q.Order("status.id DESC")
|
|
|
|
} else {
|
|
|
|
// Page up.
|
|
|
|
q = q.Order("status.id ASC")
|
|
|
|
}
|
|
|
|
|
2022-07-10 16:18:21 +01:00
|
|
|
if err := q.Scan(ctx, &statusIDs); err != nil {
|
2023-08-17 17:26:21 +01:00
|
|
|
return nil, err
|
2021-08-20 11:26:56 +01:00
|
|
|
}
|
|
|
|
|
2024-01-19 12:57:29 +00:00
|
|
|
if len(statusIDs) == 0 {
|
|
|
|
return nil, db.ErrNoEntries
|
|
|
|
}
|
|
|
|
|
2023-05-22 15:32:36 +01:00
|
|
|
// If we're paging up, we still want statuses
|
|
|
|
// 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(statusIDs)-1; l < r; l, r = l+1, r-1 {
|
|
|
|
statusIDs[l], statusIDs[r] = statusIDs[r], statusIDs[l]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-19 12:57:29 +00:00
|
|
|
return a.state.DB.GetStatusesByIDs(ctx, statusIDs)
|
2022-07-13 08:57:47 +01:00
|
|
|
}
|
2021-08-20 11:26:56 +01:00
|
|
|
|
2023-07-25 09:34:05 +01:00
|
|
|
func (a *accountDB) GetAccountPinnedStatuses(ctx context.Context, accountID string) ([]*gtsmodel.Status, error) {
|
2023-02-25 12:16:30 +00:00
|
|
|
statusIDs := []string{}
|
|
|
|
|
2023-07-25 09:34:05 +01:00
|
|
|
q := a.db.
|
2023-02-25 12:16:30 +00:00
|
|
|
NewSelect().
|
|
|
|
TableExpr("? AS ?", bun.Ident("statuses"), bun.Ident("status")).
|
|
|
|
Column("status.id").
|
|
|
|
Where("? = ?", bun.Ident("status.account_id"), accountID).
|
|
|
|
Where("? IS NOT NULL", bun.Ident("status.pinned_at")).
|
|
|
|
Order("status.pinned_at DESC")
|
|
|
|
|
|
|
|
if err := q.Scan(ctx, &statusIDs); err != nil {
|
2023-08-17 17:26:21 +01:00
|
|
|
return nil, err
|
2023-02-25 12:16:30 +00:00
|
|
|
}
|
|
|
|
|
2024-01-19 12:57:29 +00:00
|
|
|
if len(statusIDs) == 0 {
|
|
|
|
return nil, db.ErrNoEntries
|
|
|
|
}
|
|
|
|
|
|
|
|
return a.state.DB.GetStatusesByIDs(ctx, statusIDs)
|
2023-02-25 12:16:30 +00:00
|
|
|
}
|
|
|
|
|
2023-07-25 09:34:05 +01:00
|
|
|
func (a *accountDB) GetAccountWebStatuses(ctx context.Context, accountID string, limit int, maxID string) ([]*gtsmodel.Status, error) {
|
2023-05-22 15:32:36 +01:00
|
|
|
// Ensure reasonable
|
|
|
|
if limit < 0 {
|
|
|
|
limit = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make educated guess for slice size
|
|
|
|
statusIDs := make([]string, 0, limit)
|
2022-07-10 16:18:21 +01:00
|
|
|
|
2023-07-25 09:34:05 +01:00
|
|
|
q := a.db.
|
2022-07-13 08:57:47 +01:00
|
|
|
NewSelect().
|
2022-10-08 12:50:48 +01:00
|
|
|
TableExpr("? AS ?", bun.Ident("statuses"), bun.Ident("status")).
|
2023-05-22 15:32:36 +01:00
|
|
|
// Select only IDs from table
|
2022-10-08 12:50:48 +01:00
|
|
|
Column("status.id").
|
|
|
|
Where("? = ?", bun.Ident("status.account_id"), accountID).
|
2023-05-22 15:32:36 +01:00
|
|
|
// Don't show replies or boosts.
|
|
|
|
Where("? IS NULL", bun.Ident("status.in_reply_to_uri")).
|
|
|
|
Where("? IS NULL", bun.Ident("status.boost_of_id")).
|
|
|
|
// Only Public statuses.
|
2022-10-08 12:50:48 +01:00
|
|
|
Where("? = ?", bun.Ident("status.visibility"), gtsmodel.VisibilityPublic).
|
2023-05-22 15:32:36 +01:00
|
|
|
// Don't show local-only statuses on the web view.
|
2022-10-08 12:50:48 +01:00
|
|
|
Where("? = ?", bun.Ident("status.federated"), true)
|
2022-07-10 16:18:21 +01:00
|
|
|
|
2023-05-22 15:32:36 +01:00
|
|
|
// return only statuses LOWER (ie., older) than maxID
|
|
|
|
if maxID == "" {
|
|
|
|
maxID = id.Highest
|
|
|
|
}
|
|
|
|
q = q.Where("? < ?", bun.Ident("status.id"), maxID)
|
|
|
|
|
|
|
|
if limit > 0 {
|
|
|
|
// limit amount of statuses returned
|
|
|
|
q = q.Limit(limit)
|
|
|
|
}
|
|
|
|
|
|
|
|
if limit > 0 {
|
|
|
|
// limit amount of statuses returned
|
|
|
|
q = q.Limit(limit)
|
2022-07-10 16:18:21 +01:00
|
|
|
}
|
|
|
|
|
2023-05-22 15:32:36 +01:00
|
|
|
q = q.Order("status.id DESC")
|
2022-07-13 08:57:47 +01:00
|
|
|
|
|
|
|
if err := q.Scan(ctx, &statusIDs); err != nil {
|
2023-08-17 17:26:21 +01:00
|
|
|
return nil, err
|
2022-07-13 08:57:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(statusIDs) == 0 {
|
|
|
|
return nil, db.ErrNoEntries
|
|
|
|
}
|
|
|
|
|
2024-01-19 12:57:29 +00:00
|
|
|
return a.state.DB.GetStatusesByIDs(ctx, statusIDs)
|
2022-07-13 08:57:47 +01:00
|
|
|
}
|
2024-03-22 13:03:46 +00:00
|
|
|
|
|
|
|
func (a *accountDB) GetAccountSettings(
|
|
|
|
ctx context.Context,
|
|
|
|
accountID string,
|
|
|
|
) (*gtsmodel.AccountSettings, error) {
|
|
|
|
// Fetch settings from db cache with loader callback.
|
|
|
|
return a.state.Caches.GTS.AccountSettings.LoadOne(
|
|
|
|
"AccountID",
|
|
|
|
func() (*gtsmodel.AccountSettings, error) {
|
|
|
|
// Not cached! Perform database query.
|
|
|
|
var settings gtsmodel.AccountSettings
|
|
|
|
if err := a.db.
|
|
|
|
NewSelect().
|
|
|
|
Model(&settings).
|
|
|
|
Where("? = ?", bun.Ident("account_settings.account_id"), accountID).
|
|
|
|
Scan(ctx); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &settings, nil
|
|
|
|
},
|
|
|
|
accountID,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *accountDB) PutAccountSettings(
|
|
|
|
ctx context.Context,
|
|
|
|
settings *gtsmodel.AccountSettings,
|
|
|
|
) error {
|
|
|
|
return a.state.Caches.GTS.AccountSettings.Store(settings, func() error {
|
|
|
|
if _, err := a.db.
|
|
|
|
NewInsert().
|
|
|
|
Model(settings).
|
|
|
|
Exec(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *accountDB) UpdateAccountSettings(
|
|
|
|
ctx context.Context,
|
|
|
|
settings *gtsmodel.AccountSettings,
|
|
|
|
columns ...string,
|
|
|
|
) error {
|
|
|
|
return a.state.Caches.GTS.AccountSettings.Store(settings, func() error {
|
|
|
|
settings.UpdatedAt = time.Now()
|
|
|
|
if len(columns) > 0 {
|
|
|
|
// If we're updating by column,
|
|
|
|
// ensure "updated_at" is included.
|
|
|
|
columns = append(columns, "updated_at")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := a.db.
|
|
|
|
NewUpdate().
|
|
|
|
Model(settings).
|
|
|
|
Column(columns...).
|
|
|
|
Where("? = ?", bun.Ident("account_settings.account_id"), settings.AccountID).
|
|
|
|
Exec(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|