gotosocial/internal/processing/fromcommon.go
kim de6e3e5f2a
[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

555 lines
19 KiB
Go

// 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/>.
package processing
import (
"context"
"errors"
"fmt"
"github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/email"
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/internal/id"
"github.com/superseriousbusiness/gotosocial/internal/stream"
)
func (p *Processor) notifyStatus(ctx context.Context, status *gtsmodel.Status) error {
// if there are no mentions in this status then just bail
if len(status.MentionIDs) == 0 {
return nil
}
if status.Mentions == nil {
// there are mentions but they're not fully populated on the status yet so do this
mentions, err := p.state.DB.GetMentions(ctx, status.MentionIDs)
if err != nil {
return fmt.Errorf("notifyStatus: error getting mentions for status %s from the db: %s", status.ID, err)
}
status.Mentions = mentions
}
// now we have mentions as full gtsmodel.Mention structs on the status we can continue
for _, m := range status.Mentions {
// make sure this is a local account, otherwise we don't need to create a notification for it
if m.TargetAccount == nil {
a, err := p.state.DB.GetAccountByID(ctx, m.TargetAccountID)
if err != nil {
// we don't have the account or there's been an error
return fmt.Errorf("notifyStatus: error getting account with id %s from the db: %s", m.TargetAccountID, err)
}
m.TargetAccount = a
}
if m.TargetAccount.Domain != "" {
// not a local account so skip it
continue
}
// make sure a notif doesn't already exist for this mention
if err := p.state.DB.GetWhere(ctx, []db.Where{
{Key: "notification_type", Value: gtsmodel.NotificationMention},
{Key: "target_account_id", Value: m.TargetAccountID},
{Key: "origin_account_id", Value: m.OriginAccountID},
{Key: "status_id", Value: m.StatusID},
}, &gtsmodel.Notification{}); err == nil {
// notification exists already so just continue
continue
} else if err != db.ErrNoEntries {
// there's a real error in the db
return fmt.Errorf("notifyStatus: error checking existence of notification for mention with id %s : %s", m.ID, err)
}
// if we've reached this point we know the mention is for a local account, and the notification doesn't exist, so create it
notif := &gtsmodel.Notification{
ID: id.NewULID(),
NotificationType: gtsmodel.NotificationMention,
TargetAccountID: m.TargetAccountID,
TargetAccount: m.TargetAccount,
OriginAccountID: status.AccountID,
OriginAccount: status.Account,
StatusID: status.ID,
Status: status,
}
if err := p.state.DB.PutNotification(ctx, notif); err != nil {
return fmt.Errorf("notifyStatus: error putting notification in database: %s", err)
}
// now stream the notification to the user
apiNotif, err := p.tc.NotificationToAPINotification(ctx, notif)
if err != nil {
return fmt.Errorf("notifyStatus: error converting notification to api representation: %s", err)
}
if err := p.stream.Notify(apiNotif, m.TargetAccount); err != nil {
return fmt.Errorf("notifyStatus: error streaming notification to account: %s", err)
}
}
return nil
}
func (p *Processor) notifyFollowRequest(ctx context.Context, followRequest *gtsmodel.FollowRequest) error {
// make sure we have the target account pinned on the follow request
if followRequest.TargetAccount == nil {
a, err := p.state.DB.GetAccountByID(ctx, followRequest.TargetAccountID)
if err != nil {
return err
}
followRequest.TargetAccount = a
}
targetAccount := followRequest.TargetAccount
// return if this isn't a local account
if targetAccount.Domain != "" {
// this isn't a local account so we've got nothing to do here
return nil
}
notif := &gtsmodel.Notification{
ID: id.NewULID(),
NotificationType: gtsmodel.NotificationFollowRequest,
TargetAccountID: followRequest.TargetAccountID,
OriginAccountID: followRequest.AccountID,
}
if err := p.state.DB.PutNotification(ctx, notif); err != nil {
return fmt.Errorf("notifyFollowRequest: error putting notification in database: %s", err)
}
// now stream the notification to the user
apiNotif, err := p.tc.NotificationToAPINotification(ctx, notif)
if err != nil {
return fmt.Errorf("notifyStatus: error converting notification to api representation: %s", err)
}
if err := p.stream.Notify(apiNotif, targetAccount); err != nil {
return fmt.Errorf("notifyStatus: error streaming notification to account: %s", err)
}
return nil
}
func (p *Processor) notifyFollow(ctx context.Context, follow *gtsmodel.Follow, targetAccount *gtsmodel.Account) error {
// return if this isn't a local account
if targetAccount.Domain != "" {
return nil
}
// first remove the follow request notification
if err := p.state.DB.DeleteWhere(ctx, []db.Where{
{Key: "notification_type", Value: gtsmodel.NotificationFollowRequest},
{Key: "target_account_id", Value: follow.TargetAccountID},
{Key: "origin_account_id", Value: follow.AccountID},
}, &gtsmodel.Notification{}); err != nil {
return fmt.Errorf("notifyFollow: error removing old follow request notification from database: %s", err)
}
// now create the new follow notification
notif := &gtsmodel.Notification{
ID: id.NewULID(),
NotificationType: gtsmodel.NotificationFollow,
TargetAccountID: follow.TargetAccountID,
TargetAccount: follow.TargetAccount,
OriginAccountID: follow.AccountID,
OriginAccount: follow.Account,
}
if err := p.state.DB.PutNotification(ctx, notif); err != nil {
return fmt.Errorf("notifyFollow: error putting notification in database: %s", err)
}
// now stream the notification to the user
apiNotif, err := p.tc.NotificationToAPINotification(ctx, notif)
if err != nil {
return fmt.Errorf("notifyStatus: error converting notification to api representation: %s", err)
}
if err := p.stream.Notify(apiNotif, targetAccount); err != nil {
return fmt.Errorf("notifyStatus: error streaming notification to account: %s", err)
}
return nil
}
func (p *Processor) notifyFave(ctx context.Context, fave *gtsmodel.StatusFave) error {
// ignore self-faves
if fave.TargetAccountID == fave.AccountID {
return nil
}
if fave.TargetAccount == nil {
a, err := p.state.DB.GetAccountByID(ctx, fave.TargetAccountID)
if err != nil {
return err
}
fave.TargetAccount = a
}
targetAccount := fave.TargetAccount
// just return if target isn't a local account
if targetAccount.Domain != "" {
return nil
}
notif := &gtsmodel.Notification{
ID: id.NewULID(),
NotificationType: gtsmodel.NotificationFave,
TargetAccountID: fave.TargetAccountID,
TargetAccount: fave.TargetAccount,
OriginAccountID: fave.AccountID,
OriginAccount: fave.Account,
StatusID: fave.StatusID,
Status: fave.Status,
}
if err := p.state.DB.PutNotification(ctx, notif); err != nil {
return fmt.Errorf("notifyFave: error putting notification in database: %s", err)
}
// now stream the notification to the user
apiNotif, err := p.tc.NotificationToAPINotification(ctx, notif)
if err != nil {
return fmt.Errorf("notifyStatus: error converting notification to api representation: %s", err)
}
if err := p.stream.Notify(apiNotif, targetAccount); err != nil {
return fmt.Errorf("notifyStatus: error streaming notification to account: %s", err)
}
return nil
}
func (p *Processor) notifyAnnounce(ctx context.Context, status *gtsmodel.Status) error {
if status.BoostOfID == "" {
// not a boost, nothing to do
return nil
}
if status.BoostOf == nil {
boostedStatus, err := p.state.DB.GetStatusByID(ctx, status.BoostOfID)
if err != nil {
return fmt.Errorf("notifyAnnounce: error getting status with id %s: %s", status.BoostOfID, err)
}
status.BoostOf = boostedStatus
}
if status.BoostOfAccount == nil {
boostedAcct, err := p.state.DB.GetAccountByID(ctx, status.BoostOfAccountID)
if err != nil {
return fmt.Errorf("notifyAnnounce: error getting account with id %s: %s", status.BoostOfAccountID, err)
}
status.BoostOf.Account = boostedAcct
status.BoostOfAccount = boostedAcct
}
if status.BoostOfAccount.Domain != "" {
// remote account, nothing to do
return nil
}
if status.BoostOfAccountID == status.AccountID {
// it's a self boost, nothing to do
return nil
}
// make sure a notif doesn't already exist for this announce
err := p.state.DB.GetWhere(ctx, []db.Where{
{Key: "notification_type", Value: gtsmodel.NotificationReblog},
{Key: "target_account_id", Value: status.BoostOfAccountID},
{Key: "origin_account_id", Value: status.AccountID},
{Key: "status_id", Value: status.ID},
}, &gtsmodel.Notification{})
if err == nil {
// notification exists already so just bail
return nil
}
// now create the new reblog notification
notif := &gtsmodel.Notification{
ID: id.NewULID(),
NotificationType: gtsmodel.NotificationReblog,
TargetAccountID: status.BoostOfAccountID,
TargetAccount: status.BoostOfAccount,
OriginAccountID: status.AccountID,
OriginAccount: status.Account,
StatusID: status.ID,
Status: status,
}
if err := p.state.DB.PutNotification(ctx, notif); err != nil {
return fmt.Errorf("notifyAnnounce: error putting notification in database: %s", err)
}
// now stream the notification to the user
apiNotif, err := p.tc.NotificationToAPINotification(ctx, notif)
if err != nil {
return fmt.Errorf("notifyStatus: error converting notification to api representation: %s", err)
}
if err := p.stream.Notify(apiNotif, status.BoostOfAccount); err != nil {
return fmt.Errorf("notifyStatus: error streaming notification to account: %s", err)
}
return nil
}
func (p *Processor) notifyReport(ctx context.Context, report *gtsmodel.Report) error {
instance, err := p.state.DB.GetInstance(ctx, config.GetHost())
if err != nil {
return fmt.Errorf("notifyReport: error getting instance: %w", err)
}
toAddresses, err := p.state.DB.GetInstanceModeratorAddresses(ctx)
if err != nil {
if errors.Is(err, db.ErrNoEntries) {
// No registered moderator addresses.
return nil
}
return fmt.Errorf("notifyReport: error getting instance moderator addresses: %w", err)
}
if report.Account == nil {
report.Account, err = p.state.DB.GetAccountByID(ctx, report.AccountID)
if err != nil {
return fmt.Errorf("notifyReport: error getting report account: %w", err)
}
}
if report.TargetAccount == nil {
report.TargetAccount, err = p.state.DB.GetAccountByID(ctx, report.TargetAccountID)
if err != nil {
return fmt.Errorf("notifyReport: error getting report target account: %w", err)
}
}
reportData := email.NewReportData{
InstanceURL: instance.URI,
InstanceName: instance.Title,
ReportURL: instance.URI + "/settings/admin/reports/" + report.ID,
ReportDomain: report.Account.Domain,
ReportTargetDomain: report.TargetAccount.Domain,
}
if err := p.emailSender.SendNewReportEmail(toAddresses, reportData); err != nil {
return fmt.Errorf("notifyReport: error emailing instance moderators: %w", err)
}
return nil
}
func (p *Processor) notifyReportClosed(ctx context.Context, report *gtsmodel.Report) error {
user, err := p.state.DB.GetUserByAccountID(ctx, report.Account.ID)
if err != nil {
return fmt.Errorf("notifyReportClosed: db error getting user: %w", err)
}
if user.ConfirmedAt.IsZero() || !*user.Approved || *user.Disabled || user.Email == "" {
// Only email users who:
// - are confirmed
// - are approved
// - are not disabled
// - have an email address
return nil
}
instance, err := p.state.DB.GetInstance(ctx, config.GetHost())
if err != nil {
return fmt.Errorf("notifyReportClosed: db error getting instance: %w", err)
}
if report.Account == nil {
report.Account, err = p.state.DB.GetAccountByID(ctx, report.AccountID)
if err != nil {
return fmt.Errorf("notifyReportClosed: error getting report account: %w", err)
}
}
if report.TargetAccount == nil {
report.TargetAccount, err = p.state.DB.GetAccountByID(ctx, report.TargetAccountID)
if err != nil {
return fmt.Errorf("notifyReportClosed: error getting report target account: %w", err)
}
}
reportClosedData := email.ReportClosedData{
Username: report.Account.Username,
InstanceURL: instance.URI,
InstanceName: instance.Title,
ReportTargetUsername: report.TargetAccount.Username,
ReportTargetDomain: report.TargetAccount.Domain,
ActionTakenComment: report.ActionTaken,
}
return p.emailSender.SendReportClosedEmail(user.Email, reportClosedData)
}
// timelineStatus processes the given new status and inserts it into
// the HOME timelines of accounts that follow the status author.
func (p *Processor) timelineStatus(ctx context.Context, status *gtsmodel.Status) error {
if status.Account == nil {
// ensure status fully populated (including account)
if err := p.state.DB.PopulateStatus(ctx, status); err != nil {
return fmt.Errorf("timelineStatus: error populating status with id %s: %w", status.ID, err)
}
}
// get local followers of the account that posted the status
follows, err := p.state.DB.GetAccountLocalFollowers(ctx, status.AccountID)
if err != nil {
return fmt.Errorf("timelineStatus: error getting followers for account id %s: %w", status.AccountID, err)
}
// If the poster is also local, add a fake entry for them
// so they can see their own status in their timeline.
if status.Account.IsLocal() {
follows = append(follows, &gtsmodel.Follow{
AccountID: status.AccountID,
Account: status.Account,
})
}
var errs gtserror.MultiError
for _, follow := range follows {
// Timeline the status for each local following account.
if err := p.timelineStatusForAccount(ctx, follow.Account, status); err != nil {
errs.Append(err)
}
}
if len(errs) != 0 {
return fmt.Errorf("timelineStatus: one or more errors timelining statuses: %w", errs.Combine())
}
return nil
}
// timelineStatusForAccount puts the given status in the HOME timeline
// of the account with given accountID, if it's hometimelineable.
//
// If the status was inserted into the home timeline of the given account,
// it will also be streamed via websockets to the user.
func (p *Processor) timelineStatusForAccount(ctx context.Context, account *gtsmodel.Account, status *gtsmodel.Status) error {
// make sure the status is timelineable
if timelineable, err := p.filter.StatusHomeTimelineable(ctx, account, status); err != nil {
return fmt.Errorf("timelineStatusForAccount: error getting timelineability for status for timeline with id %s: %w", account.ID, err)
} else if !timelineable {
return nil
}
// stick the status in the timeline for the account and then immediately prepare it so they can see it right away
if inserted, err := p.statusTimelines.IngestAndPrepare(ctx, status, account.ID); err != nil {
return fmt.Errorf("timelineStatusForAccount: error ingesting status %s: %w", status.ID, err)
} else if !inserted {
return nil
}
// the status was inserted so stream it to the user
apiStatus, err := p.tc.StatusToAPIStatus(ctx, status, account)
if err != nil {
return fmt.Errorf("timelineStatusForAccount: error converting status %s to frontend representation: %w", status.ID, err)
}
if err := p.stream.Update(apiStatus, account, stream.TimelineHome); err != nil {
return fmt.Errorf("timelineStatusForAccount: error streaming update for status %s: %w", status.ID, err)
}
return nil
}
// deleteStatusFromTimelines completely removes the given status from all timelines.
// It will also stream deletion of the status to all open streams.
func (p *Processor) deleteStatusFromTimelines(ctx context.Context, status *gtsmodel.Status) error {
if err := p.statusTimelines.WipeItemFromAllTimelines(ctx, status.ID); err != nil {
return err
}
return p.stream.Delete(status.ID)
}
// wipeStatus contains common logic used to totally delete a status
// + all its attachments, notifications, boosts, and timeline entries.
func (p *Processor) wipeStatus(ctx context.Context, statusToDelete *gtsmodel.Status, deleteAttachments bool) error {
// either delete all attachments for this status, or simply
// unattach all attachments for this status, so they'll be
// cleaned later by a separate process; reason to unattach rather
// than delete is that the poster might want to reattach them
// to another status immediately (in case of delete + redraft)
if deleteAttachments {
for _, a := range statusToDelete.AttachmentIDs {
if err := p.media.Delete(ctx, a); err != nil {
return err
}
}
} else {
for _, a := range statusToDelete.AttachmentIDs {
if _, err := p.media.Unattach(ctx, statusToDelete.Account, a); err != nil {
return err
}
}
}
// delete all mention entries generated by this status
for _, id := range statusToDelete.MentionIDs {
if err := p.state.DB.DeleteMentionByID(ctx, id); err != nil {
return err
}
}
// delete all notification entries generated by this status
if err := p.state.DB.DeleteNotificationsForStatus(ctx, statusToDelete.ID); err != nil {
return err
}
// delete all bookmarks that point to this status
if err := p.state.DB.DeleteStatusBookmarksForStatus(ctx, statusToDelete.ID); err != nil {
return err
}
// delete all faves of this status
if err := p.state.DB.DeleteStatusFavesForStatus(ctx, statusToDelete.ID); err != nil {
return err
}
// delete all boosts for this status + remove them from timelines
if boosts, err := p.state.DB.GetStatusReblogs(ctx, statusToDelete); err == nil {
for _, b := range boosts {
if err := p.deleteStatusFromTimelines(ctx, b); err != nil {
return err
}
if err := p.state.DB.DeleteStatusByID(ctx, b.ID); err != nil {
return err
}
}
}
// delete this status from any and all timelines
if err := p.deleteStatusFromTimelines(ctx, statusToDelete); err != nil {
return err
}
// delete the status itself
if err := p.state.DB.DeleteStatusByID(ctx, statusToDelete.ID); err != nil {
return err
}
return nil
}