From de6e3e5f2a8ea639d76e310a11cb9bc093fef3a9 Mon Sep 17 00:00:00 2001 From: kim <89579420+NyaaaWhatsUpDoc@users.noreply.github.com> Date: Tue, 28 Mar 2023 14:03:14 +0100 Subject: [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 Co-authored-by: tsmethurst --- internal/db/relationship.go | 166 +++++++++++++++++++++++++------------------- 1 file changed, 93 insertions(+), 73 deletions(-) (limited to 'internal/db/relationship.go') diff --git a/internal/db/relationship.go b/internal/db/relationship.go index d13a73dea..838647154 100644 --- a/internal/db/relationship.go +++ b/internal/db/relationship.go @@ -25,42 +25,86 @@ import ( // Relationship contains functions for getting or modifying the relationship between two accounts. type Relationship interface { - // IsBlocked checks whether account 1 has a block in place against account2. - // If eitherDirection is true, then the function returns true if account1 blocks account2, OR if account2 blocks account1. - IsBlocked(ctx context.Context, account1 string, account2 string, eitherDirection bool) (bool, Error) + // IsBlocked checks whether source account has a block in place against target. + IsBlocked(ctx context.Context, sourceAccountID string, targetAccountID string) (bool, Error) + + // IsEitherBlocked checks whether there is a block in place between either of account1 and account2. + IsEitherBlocked(ctx context.Context, accountID1 string, accountID2 string) (bool, error) + + // GetBlockByID fetches block with given ID from the database. + GetBlockByID(ctx context.Context, id string) (*gtsmodel.Block, error) + + // GetBlockByURI fetches block with given AP URI from the database. + GetBlockByURI(ctx context.Context, uri string) (*gtsmodel.Block, error) // GetBlock returns the block from account1 targeting account2, if it exists, or an error if it doesn't. - // - // Because this is slower than Blocked, only use it if you need the actual Block struct for some reason, - // not if you're just checking for the existence of a block. - GetBlock(ctx context.Context, account1 string, account2 string) (*gtsmodel.Block, Error) + GetBlock(ctx context.Context, account1 string, account2 string) (*gtsmodel.Block, error) // PutBlock attempts to place the given account block in the database. - PutBlock(ctx context.Context, block *gtsmodel.Block) Error + PutBlock(ctx context.Context, block *gtsmodel.Block) error // DeleteBlockByID removes block with given ID from the database. - DeleteBlockByID(ctx context.Context, id string) Error + DeleteBlockByID(ctx context.Context, id string) error // DeleteBlockByURI removes block with given AP URI from the database. - DeleteBlockByURI(ctx context.Context, uri string) Error - - // DeleteBlocksByOriginAccountID removes any blocks with accountID equal to originAccountID. - DeleteBlocksByOriginAccountID(ctx context.Context, originAccountID string) Error + DeleteBlockByURI(ctx context.Context, uri string) error - // DeleteBlocksByTargetAccountID removes any blocks with given targetAccountID. - DeleteBlocksByTargetAccountID(ctx context.Context, targetAccountID string) Error + // DeleteAccountBlocks will delete all database blocks to / from the given account ID. + DeleteAccountBlocks(ctx context.Context, accountID string) error // GetRelationship retrieves the relationship of the targetAccount to the requestingAccount. GetRelationship(ctx context.Context, requestingAccount string, targetAccount string) (*gtsmodel.Relationship, Error) + // GetFollowByID fetches follow with given ID from the database. + GetFollowByID(ctx context.Context, id string) (*gtsmodel.Follow, error) + + // GetFollowByURI fetches follow with given AP URI from the database. + GetFollowByURI(ctx context.Context, uri string) (*gtsmodel.Follow, error) + + // GetFollow retrieves a follow if it exists between source and target accounts. + GetFollow(ctx context.Context, sourceAccountID string, targetAccountID string) (*gtsmodel.Follow, error) + + // GetFollowRequestByID fetches follow request with given ID from the database. + GetFollowRequestByID(ctx context.Context, id string) (*gtsmodel.FollowRequest, error) + + // GetFollowRequestByURI fetches follow request with given AP URI from the database. + GetFollowRequestByURI(ctx context.Context, uri string) (*gtsmodel.FollowRequest, error) + + // GetFollowRequest retrieves a follow request if it exists between source and target accounts. + GetFollowRequest(ctx context.Context, sourceAccountID string, targetAccountID string) (*gtsmodel.FollowRequest, error) + // IsFollowing returns true if sourceAccount follows target account, or an error if something goes wrong while finding out. - IsFollowing(ctx context.Context, sourceAccount *gtsmodel.Account, targetAccount *gtsmodel.Account) (bool, Error) + IsFollowing(ctx context.Context, sourceAccountID string, targetAccountID string) (bool, Error) + + // IsMutualFollowing returns true if account1 and account2 both follow each other, or an error if something goes wrong while finding out. + IsMutualFollowing(ctx context.Context, sourceAccountID string, targetAccountID string) (bool, Error) // IsFollowRequested returns true if sourceAccount has requested to follow target account, or an error if something goes wrong while finding out. - IsFollowRequested(ctx context.Context, sourceAccount *gtsmodel.Account, targetAccount *gtsmodel.Account) (bool, Error) + IsFollowRequested(ctx context.Context, sourceAccountID string, targetAccountID string) (bool, Error) - // IsMutualFollowing returns true if account1 and account2 both follow each other, or an error if something goes wrong while finding out. - IsMutualFollowing(ctx context.Context, account1 *gtsmodel.Account, account2 *gtsmodel.Account) (bool, Error) + // PutFollow attempts to place the given account follow in the database. + PutFollow(ctx context.Context, follow *gtsmodel.Follow) error + + // PutFollowRequest attempts to place the given account follow request in the database. + PutFollowRequest(ctx context.Context, follow *gtsmodel.FollowRequest) error + + // DeleteFollowByID deletes a follow from the database with the given ID. + DeleteFollowByID(ctx context.Context, id string) error + + // DeleteFollowByURI deletes a follow from the database with the given URI. + DeleteFollowByURI(ctx context.Context, uri string) error + + // DeleteFollowRequestByID deletes a follow request from the database with the given ID. + DeleteFollowRequestByID(ctx context.Context, id string) error + + // DeleteFollowRequestByURI deletes a follow request from the database with the given URI. + DeleteFollowRequestByURI(ctx context.Context, uri string) error + + // DeleteAccountFollows will delete all database follows to / from the given account ID. + DeleteAccountFollows(ctx context.Context, accountID string) error + + // DeleteAccountFollowRequests will delete all database follow requests to / from the given account ID. + DeleteAccountFollowRequests(ctx context.Context, accountID string) error // AcceptFollowRequest moves a follow request in the database from the follow_requests table to the follows table. // In other words, it should create the follow, and delete the existing follow request. @@ -69,65 +113,41 @@ type Relationship interface { AcceptFollowRequest(ctx context.Context, originAccountID string, targetAccountID string) (*gtsmodel.Follow, Error) // RejectFollowRequest fetches a follow request from the database, and then deletes it. - // - // The deleted follow request will be returned so that further processing can be done on it. - RejectFollowRequest(ctx context.Context, originAccountID string, targetAccountID string) (*gtsmodel.FollowRequest, Error) + RejectFollowRequest(ctx context.Context, originAccountID string, targetAccountID string) Error - // GetFollows returns a slice of follows owned by the given accountID, and/or - // targeting the given account id. - // - // If accountID is set and targetAccountID isn't, then all follows created by - // accountID will be returned. - // - // If targetAccountID is set and accountID isn't, then all follows targeting - // targetAccountID will be returned. - // - // If both accountID and targetAccountID are set, then only 0 or 1 follows will - // be in the returned slice. - GetFollows(ctx context.Context, accountID string, targetAccountID string) ([]*gtsmodel.Follow, Error) + // GetAccountFollows returns a slice of follows owned by the given accountID. + GetAccountFollows(ctx context.Context, accountID string) ([]*gtsmodel.Follow, error) - // GetLocalFollowersIDs returns a list of local account IDs which follow the - // targetAccountID. The returned IDs are not guaranteed to be ordered in any - // particular way, so take care. - GetLocalFollowersIDs(ctx context.Context, targetAccountID string) ([]string, Error) + // GetAccountLocalFollows returns a slice of follows owned by the given accountID, only including follows from this instance. + GetAccountLocalFollows(ctx context.Context, accountID string) ([]*gtsmodel.Follow, error) - // CountFollows is like GetFollows, but just counts rather than returning. - CountFollows(ctx context.Context, accountID string, targetAccountID string) (int, Error) + // CountAccountFollows returns the amount of accounts that the given accountID is following. + CountAccountFollows(ctx context.Context, accountID string) (int, error) - // GetFollowRequests returns a slice of follows requests owned by the given - // accountID, and/or targeting the given account id. - // - // If accountID is set and targetAccountID isn't, then all requests created by - // accountID will be returned. - // - // If targetAccountID is set and accountID isn't, then all requests targeting - // targetAccountID will be returned. - // - // If both accountID and targetAccountID are set, then only 0 or 1 requests will - // be in the returned slice. - GetFollowRequests(ctx context.Context, accountID string, targetAccountID string) ([]*gtsmodel.FollowRequest, Error) + // CountAccountLocalFollows returns the amount of accounts that the given accountID is following, only including follows from this instance. + CountAccountLocalFollows(ctx context.Context, accountID string) (int, error) - // CountFollowRequests is like GetFollowRequests, but just counts rather than returning. - CountFollowRequests(ctx context.Context, accountID string, targetAccountID string) (int, Error) + // GetAccountFollowers fetches follows that target given accountID. + GetAccountFollowers(ctx context.Context, accountID string) ([]*gtsmodel.Follow, error) - // Unfollow removes a follow targeting targetAccountID and originating - // from originAccountID. - // - // If a follow was removed this way, the AP URI of the follow will be - // returned to the caller, so that further processing can take place - // if necessary. - // - // If no follow was removed this way, the returned string will be empty. - Unfollow(ctx context.Context, originAccountID string, targetAccountID string) (string, Error) + // GetAccountLocalFollowers fetches follows that target given accountID, only including follows from this instance. + GetAccountLocalFollowers(ctx context.Context, accountID string) ([]*gtsmodel.Follow, error) - // UnfollowRequest removes a follow request targeting targetAccountID - // and originating from originAccountID. - // - // If a follow request was removed this way, the AP URI of the follow - // request will be returned to the caller, so that further processing - // can take place if necessary. - // - // If no follow request was removed this way, the returned string will - // be empty. - UnfollowRequest(ctx context.Context, originAccountID string, targetAccountID string) (string, Error) + // CountAccountFollowers returns the amounts that the given ID is followed by. + CountAccountFollowers(ctx context.Context, accountID string) (int, error) + + // CountAccountLocalFollowers returns the amounts that the given ID is followed by, only including follows from this instance. + CountAccountLocalFollowers(ctx context.Context, accountID string) (int, error) + + // GetAccountFollowRequests returns all follow requests targeting the given account. + GetAccountFollowRequests(ctx context.Context, accountID string) ([]*gtsmodel.FollowRequest, error) + + // GetAccountFollowRequesting returns all follow requests originating from the given account. + GetAccountFollowRequesting(ctx context.Context, accountID string) ([]*gtsmodel.FollowRequest, error) + + // CountAccountFollowRequests returns number of follow requests targeting the given account. + CountAccountFollowRequests(ctx context.Context, accountID string) (int, error) + + // CountAccountFollowerRequests returns number of follow requests originating from the given account. + CountAccountFollowRequesting(ctx context.Context, accountID string) (int, error) } -- cgit v1.2.3