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-03-01 14:41:43 +00:00
|
|
|
|
2021-05-30 11:12:00 +00:00
|
|
|
package processing
|
2021-04-01 18:46:45 +00:00
|
|
|
|
2021-05-27 14:06:24 +00:00
|
|
|
import (
|
2021-08-25 13:34:33 +00:00
|
|
|
"context"
|
2023-03-19 12:11:46 +00:00
|
|
|
"errors"
|
2021-05-27 14:06:24 +00:00
|
|
|
"fmt"
|
|
|
|
|
2023-03-19 12:11:46 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/config"
|
2021-05-27 14:06:24 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/db"
|
2023-03-19 12:11:46 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/email"
|
2023-03-20 18:10:08 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
|
2021-05-27 14:06:24 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
2021-06-13 16:42:28 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/id"
|
2021-11-22 18:03:21 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/stream"
|
2021-05-27 14:06:24 +00:00
|
|
|
)
|
2021-04-01 18:46:45 +00:00
|
|
|
|
2023-02-22 15:05:26 +00:00
|
|
|
func (p *Processor) notifyStatus(ctx context.Context, status *gtsmodel.Status) error {
|
2021-05-27 14:06:24 +00:00
|
|
|
// if there are no mentions in this status then just bail
|
2021-08-20 10:26:56 +00:00
|
|
|
if len(status.MentionIDs) == 0 {
|
2021-05-27 14:06:24 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-20 10:26:56 +00:00
|
|
|
if status.Mentions == nil {
|
2021-05-27 14:06:24 +00:00
|
|
|
// there are mentions but they're not fully populated on the status yet so do this
|
2023-03-01 18:26:53 +00:00
|
|
|
menchies, err := p.state.DB.GetMentions(ctx, status.MentionIDs)
|
2021-08-20 10:26:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("notifyStatus: error getting mentions for status %s from the db: %s", status.ID, err)
|
2021-05-27 14:06:24 +00:00
|
|
|
}
|
2021-08-20 10:26:56 +00:00
|
|
|
status.Mentions = menchies
|
2021-05-27 14:06:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// now we have mentions as full gtsmodel.Mention structs on the status we can continue
|
2021-08-20 10:26:56 +00:00
|
|
|
for _, m := range status.Mentions {
|
2021-05-27 14:06:24 +00:00
|
|
|
// make sure this is a local account, otherwise we don't need to create a notification for it
|
2021-08-20 10:26:56 +00:00
|
|
|
if m.TargetAccount == nil {
|
2023-03-01 18:26:53 +00:00
|
|
|
a, err := p.state.DB.GetAccountByID(ctx, m.TargetAccountID)
|
2021-08-20 10:26:56 +00:00
|
|
|
if err != nil {
|
2021-05-27 14:06:24 +00:00
|
|
|
// 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)
|
|
|
|
}
|
2021-08-20 10:26:56 +00:00
|
|
|
m.TargetAccount = a
|
2021-05-27 14:06:24 +00:00
|
|
|
}
|
2021-08-20 10:26:56 +00:00
|
|
|
if m.TargetAccount.Domain != "" {
|
2021-05-27 14:06:24 +00:00
|
|
|
// not a local account so skip it
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure a notif doesn't already exist for this mention
|
2023-03-01 18:26:53 +00:00
|
|
|
if err := p.state.DB.GetWhere(ctx, []db.Where{
|
2021-05-27 14:06:24 +00:00
|
|
|
{Key: "notification_type", Value: gtsmodel.NotificationMention},
|
|
|
|
{Key: "target_account_id", Value: m.TargetAccountID},
|
2021-09-04 11:29:56 +00:00
|
|
|
{Key: "origin_account_id", Value: m.OriginAccountID},
|
|
|
|
{Key: "status_id", Value: m.StatusID},
|
|
|
|
}, >smodel.Notification{}); err == nil {
|
2021-05-27 14:06:24 +00:00
|
|
|
// notification exists already so just continue
|
|
|
|
continue
|
2021-09-04 11:29:56 +00:00
|
|
|
} else if err != db.ErrNoEntries {
|
2021-05-27 14:06:24 +00:00
|
|
|
// 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 := >smodel.Notification{
|
2023-02-03 20:03:05 +00:00
|
|
|
ID: id.NewULID(),
|
2021-05-27 14:06:24 +00:00
|
|
|
NotificationType: gtsmodel.NotificationMention,
|
|
|
|
TargetAccountID: m.TargetAccountID,
|
2021-08-20 10:26:56 +00:00
|
|
|
TargetAccount: m.TargetAccount,
|
2021-05-27 14:06:24 +00:00
|
|
|
OriginAccountID: status.AccountID,
|
2021-08-20 10:26:56 +00:00
|
|
|
OriginAccount: status.Account,
|
2021-05-27 14:06:24 +00:00
|
|
|
StatusID: status.ID,
|
2021-08-20 10:26:56 +00:00
|
|
|
Status: status,
|
2021-05-27 14:06:24 +00:00
|
|
|
}
|
|
|
|
|
2023-03-01 18:26:53 +00:00
|
|
|
if err := p.state.DB.Put(ctx, notif); err != nil {
|
2021-05-27 14:06:24 +00:00
|
|
|
return fmt.Errorf("notifyStatus: error putting notification in database: %s", err)
|
|
|
|
}
|
2021-06-19 09:18:55 +00:00
|
|
|
|
|
|
|
// now stream the notification to the user
|
2021-10-04 13:24:19 +00:00
|
|
|
apiNotif, err := p.tc.NotificationToAPINotification(ctx, notif)
|
2021-06-19 09:18:55 +00:00
|
|
|
if err != nil {
|
2021-10-04 13:24:19 +00:00
|
|
|
return fmt.Errorf("notifyStatus: error converting notification to api representation: %s", err)
|
2021-06-19 09:18:55 +00:00
|
|
|
}
|
|
|
|
|
2023-02-22 15:05:26 +00:00
|
|
|
if err := p.stream.Notify(apiNotif, m.TargetAccount); err != nil {
|
2021-06-19 09:18:55 +00:00
|
|
|
return fmt.Errorf("notifyStatus: error streaming notification to account: %s", err)
|
|
|
|
}
|
2021-05-27 14:06:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-02-22 15:05:26 +00:00
|
|
|
func (p *Processor) notifyFollowRequest(ctx context.Context, followRequest *gtsmodel.FollowRequest) error {
|
2021-10-01 17:08:50 +00:00
|
|
|
// make sure we have the target account pinned on the follow request
|
|
|
|
if followRequest.TargetAccount == nil {
|
2023-03-01 18:26:53 +00:00
|
|
|
a, err := p.state.DB.GetAccountByID(ctx, followRequest.TargetAccountID)
|
2021-10-01 17:08:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
followRequest.TargetAccount = a
|
|
|
|
}
|
|
|
|
targetAccount := followRequest.TargetAccount
|
|
|
|
|
2021-05-27 14:06:24 +00:00
|
|
|
// return if this isn't a local account
|
2021-10-01 17:08:50 +00:00
|
|
|
if targetAccount.Domain != "" {
|
|
|
|
// this isn't a local account so we've got nothing to do here
|
2021-05-27 14:06:24 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
notif := >smodel.Notification{
|
2023-02-03 20:03:05 +00:00
|
|
|
ID: id.NewULID(),
|
2021-05-27 14:06:24 +00:00
|
|
|
NotificationType: gtsmodel.NotificationFollowRequest,
|
|
|
|
TargetAccountID: followRequest.TargetAccountID,
|
|
|
|
OriginAccountID: followRequest.AccountID,
|
|
|
|
}
|
|
|
|
|
2023-03-01 18:26:53 +00:00
|
|
|
if err := p.state.DB.Put(ctx, notif); err != nil {
|
2021-05-27 14:06:24 +00:00
|
|
|
return fmt.Errorf("notifyFollowRequest: error putting notification in database: %s", err)
|
|
|
|
}
|
|
|
|
|
2021-06-19 09:18:55 +00:00
|
|
|
// now stream the notification to the user
|
2021-10-04 13:24:19 +00:00
|
|
|
apiNotif, err := p.tc.NotificationToAPINotification(ctx, notif)
|
2021-06-19 09:18:55 +00:00
|
|
|
if err != nil {
|
2021-10-04 13:24:19 +00:00
|
|
|
return fmt.Errorf("notifyStatus: error converting notification to api representation: %s", err)
|
2021-06-19 09:18:55 +00:00
|
|
|
}
|
|
|
|
|
2023-02-22 15:05:26 +00:00
|
|
|
if err := p.stream.Notify(apiNotif, targetAccount); err != nil {
|
2021-06-19 09:18:55 +00:00
|
|
|
return fmt.Errorf("notifyStatus: error streaming notification to account: %s", err)
|
|
|
|
}
|
|
|
|
|
2021-05-17 17:06:58 +00:00
|
|
|
return nil
|
2021-03-22 21:26:54 +00:00
|
|
|
}
|
2021-05-21 13:48:26 +00:00
|
|
|
|
2023-02-22 15:05:26 +00:00
|
|
|
func (p *Processor) notifyFollow(ctx context.Context, follow *gtsmodel.Follow, targetAccount *gtsmodel.Account) error {
|
2021-05-27 14:06:24 +00:00
|
|
|
// return if this isn't a local account
|
2021-08-20 10:26:56 +00:00
|
|
|
if targetAccount.Domain != "" {
|
2021-05-27 14:06:24 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// first remove the follow request notification
|
2023-03-01 18:26:53 +00:00
|
|
|
if err := p.state.DB.DeleteWhere(ctx, []db.Where{
|
2021-05-27 14:06:24 +00:00
|
|
|
{Key: "notification_type", Value: gtsmodel.NotificationFollowRequest},
|
|
|
|
{Key: "target_account_id", Value: follow.TargetAccountID},
|
|
|
|
{Key: "origin_account_id", Value: follow.AccountID},
|
|
|
|
}, >smodel.Notification{}); err != nil {
|
|
|
|
return fmt.Errorf("notifyFollow: error removing old follow request notification from database: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// now create the new follow notification
|
|
|
|
notif := >smodel.Notification{
|
2023-02-03 20:03:05 +00:00
|
|
|
ID: id.NewULID(),
|
2021-05-27 14:06:24 +00:00
|
|
|
NotificationType: gtsmodel.NotificationFollow,
|
|
|
|
TargetAccountID: follow.TargetAccountID,
|
2021-08-20 10:26:56 +00:00
|
|
|
TargetAccount: follow.TargetAccount,
|
2021-05-27 14:06:24 +00:00
|
|
|
OriginAccountID: follow.AccountID,
|
2021-08-20 10:26:56 +00:00
|
|
|
OriginAccount: follow.Account,
|
2021-05-27 14:06:24 +00:00
|
|
|
}
|
2023-03-01 18:26:53 +00:00
|
|
|
if err := p.state.DB.Put(ctx, notif); err != nil {
|
2021-05-27 14:06:24 +00:00
|
|
|
return fmt.Errorf("notifyFollow: error putting notification in database: %s", err)
|
|
|
|
}
|
|
|
|
|
2021-06-19 09:18:55 +00:00
|
|
|
// now stream the notification to the user
|
2021-10-04 13:24:19 +00:00
|
|
|
apiNotif, err := p.tc.NotificationToAPINotification(ctx, notif)
|
2021-06-19 09:18:55 +00:00
|
|
|
if err != nil {
|
2021-10-04 13:24:19 +00:00
|
|
|
return fmt.Errorf("notifyStatus: error converting notification to api representation: %s", err)
|
2021-06-19 09:18:55 +00:00
|
|
|
}
|
|
|
|
|
2023-02-22 15:05:26 +00:00
|
|
|
if err := p.stream.Notify(apiNotif, targetAccount); err != nil {
|
2021-06-19 09:18:55 +00:00
|
|
|
return fmt.Errorf("notifyStatus: error streaming notification to account: %s", err)
|
|
|
|
}
|
|
|
|
|
2021-05-21 21:04:59 +00:00
|
|
|
return nil
|
2021-05-21 13:48:26 +00:00
|
|
|
}
|
2021-05-24 16:49:48 +00:00
|
|
|
|
2023-02-22 15:05:26 +00:00
|
|
|
func (p *Processor) notifyFave(ctx context.Context, fave *gtsmodel.StatusFave) error {
|
2022-08-30 09:43:29 +00:00
|
|
|
// ignore self-faves
|
|
|
|
if fave.TargetAccountID == fave.AccountID {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-27 15:42:20 +00:00
|
|
|
if fave.TargetAccount == nil {
|
2023-03-01 18:26:53 +00:00
|
|
|
a, err := p.state.DB.GetAccountByID(ctx, fave.TargetAccountID)
|
2021-09-27 15:42:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fave.TargetAccount = a
|
|
|
|
}
|
|
|
|
targetAccount := fave.TargetAccount
|
|
|
|
|
|
|
|
// just return if target isn't a local account
|
2021-08-20 10:26:56 +00:00
|
|
|
if targetAccount.Domain != "" {
|
2021-05-27 14:06:24 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
notif := >smodel.Notification{
|
2023-02-03 20:03:05 +00:00
|
|
|
ID: id.NewULID(),
|
2021-05-27 14:06:24 +00:00
|
|
|
NotificationType: gtsmodel.NotificationFave,
|
|
|
|
TargetAccountID: fave.TargetAccountID,
|
2021-08-20 10:26:56 +00:00
|
|
|
TargetAccount: fave.TargetAccount,
|
2021-05-27 14:06:24 +00:00
|
|
|
OriginAccountID: fave.AccountID,
|
2021-08-20 10:26:56 +00:00
|
|
|
OriginAccount: fave.Account,
|
2021-05-27 14:06:24 +00:00
|
|
|
StatusID: fave.StatusID,
|
2021-08-20 10:26:56 +00:00
|
|
|
Status: fave.Status,
|
2021-05-27 14:06:24 +00:00
|
|
|
}
|
|
|
|
|
2023-03-01 18:26:53 +00:00
|
|
|
if err := p.state.DB.Put(ctx, notif); err != nil {
|
2021-05-27 14:06:24 +00:00
|
|
|
return fmt.Errorf("notifyFave: error putting notification in database: %s", err)
|
|
|
|
}
|
|
|
|
|
2021-06-19 09:18:55 +00:00
|
|
|
// now stream the notification to the user
|
2021-10-04 13:24:19 +00:00
|
|
|
apiNotif, err := p.tc.NotificationToAPINotification(ctx, notif)
|
2021-06-19 09:18:55 +00:00
|
|
|
if err != nil {
|
2021-10-04 13:24:19 +00:00
|
|
|
return fmt.Errorf("notifyStatus: error converting notification to api representation: %s", err)
|
2021-06-19 09:18:55 +00:00
|
|
|
}
|
|
|
|
|
2023-02-22 15:05:26 +00:00
|
|
|
if err := p.stream.Notify(apiNotif, targetAccount); err != nil {
|
2021-06-19 09:18:55 +00:00
|
|
|
return fmt.Errorf("notifyStatus: error streaming notification to account: %s", err)
|
|
|
|
}
|
|
|
|
|
2021-05-27 14:06:24 +00:00
|
|
|
return nil
|
2021-05-24 16:49:48 +00:00
|
|
|
}
|
2021-05-28 17:57:04 +00:00
|
|
|
|
2023-02-22 15:05:26 +00:00
|
|
|
func (p *Processor) notifyAnnounce(ctx context.Context, status *gtsmodel.Status) error {
|
2021-05-31 15:36:35 +00:00
|
|
|
if status.BoostOfID == "" {
|
|
|
|
// not a boost, nothing to do
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-20 10:26:56 +00:00
|
|
|
if status.BoostOf == nil {
|
2023-03-01 18:26:53 +00:00
|
|
|
boostedStatus, err := p.state.DB.GetStatusByID(ctx, status.BoostOfID)
|
2021-08-20 10:26:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("notifyAnnounce: error getting status with id %s: %s", status.BoostOfID, err)
|
|
|
|
}
|
|
|
|
status.BoostOf = boostedStatus
|
2021-05-31 15:36:35 +00:00
|
|
|
}
|
|
|
|
|
2021-08-20 10:26:56 +00:00
|
|
|
if status.BoostOfAccount == nil {
|
2023-03-01 18:26:53 +00:00
|
|
|
boostedAcct, err := p.state.DB.GetAccountByID(ctx, status.BoostOfAccountID)
|
2021-08-20 10:26:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("notifyAnnounce: error getting account with id %s: %s", status.BoostOfAccountID, err)
|
|
|
|
}
|
|
|
|
status.BoostOf.Account = boostedAcct
|
|
|
|
status.BoostOfAccount = boostedAcct
|
2021-05-31 15:36:35 +00:00
|
|
|
}
|
|
|
|
|
2021-09-04 11:29:56 +00:00
|
|
|
if status.BoostOfAccount.Domain != "" {
|
2021-05-31 15:36:35 +00:00
|
|
|
// remote account, nothing to do
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-20 10:26:56 +00:00
|
|
|
if status.BoostOfAccountID == status.AccountID {
|
2021-05-31 15:36:35 +00:00
|
|
|
// it's a self boost, nothing to do
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure a notif doesn't already exist for this announce
|
2023-03-01 18:26:53 +00:00
|
|
|
err := p.state.DB.GetWhere(ctx, []db.Where{
|
2021-05-31 15:36:35 +00:00
|
|
|
{Key: "notification_type", Value: gtsmodel.NotificationReblog},
|
2021-08-20 10:26:56 +00:00
|
|
|
{Key: "target_account_id", Value: status.BoostOfAccountID},
|
2021-05-31 15:36:35 +00:00
|
|
|
{Key: "origin_account_id", Value: status.AccountID},
|
|
|
|
{Key: "status_id", Value: status.ID},
|
|
|
|
}, >smodel.Notification{})
|
|
|
|
if err == nil {
|
|
|
|
// notification exists already so just bail
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// now create the new reblog notification
|
|
|
|
notif := >smodel.Notification{
|
2023-02-03 20:03:05 +00:00
|
|
|
ID: id.NewULID(),
|
2021-05-31 15:36:35 +00:00
|
|
|
NotificationType: gtsmodel.NotificationReblog,
|
2021-08-20 10:26:56 +00:00
|
|
|
TargetAccountID: status.BoostOfAccountID,
|
|
|
|
TargetAccount: status.BoostOfAccount,
|
2021-05-31 15:36:35 +00:00
|
|
|
OriginAccountID: status.AccountID,
|
2021-08-20 10:26:56 +00:00
|
|
|
OriginAccount: status.Account,
|
2021-05-31 15:36:35 +00:00
|
|
|
StatusID: status.ID,
|
2021-08-20 10:26:56 +00:00
|
|
|
Status: status,
|
2021-05-31 15:36:35 +00:00
|
|
|
}
|
|
|
|
|
2023-03-01 18:26:53 +00:00
|
|
|
if err := p.state.DB.Put(ctx, notif); err != nil {
|
2021-05-31 15:36:35 +00:00
|
|
|
return fmt.Errorf("notifyAnnounce: error putting notification in database: %s", err)
|
|
|
|
}
|
|
|
|
|
2021-06-19 09:18:55 +00:00
|
|
|
// now stream the notification to the user
|
2021-10-04 13:24:19 +00:00
|
|
|
apiNotif, err := p.tc.NotificationToAPINotification(ctx, notif)
|
2021-06-19 09:18:55 +00:00
|
|
|
if err != nil {
|
2021-10-04 13:24:19 +00:00
|
|
|
return fmt.Errorf("notifyStatus: error converting notification to api representation: %s", err)
|
2021-06-19 09:18:55 +00:00
|
|
|
}
|
|
|
|
|
2023-02-22 15:05:26 +00:00
|
|
|
if err := p.stream.Notify(apiNotif, status.BoostOfAccount); err != nil {
|
2021-06-19 09:18:55 +00:00
|
|
|
return fmt.Errorf("notifyStatus: error streaming notification to account: %s", err)
|
|
|
|
}
|
|
|
|
|
2021-05-28 17:57:04 +00:00
|
|
|
return nil
|
|
|
|
}
|
2023-03-19 12:11:46 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2021-06-13 16:42:28 +00:00
|
|
|
|
2021-11-22 18:03:21 +00:00
|
|
|
// timelineStatus processes the given new status and inserts it into
|
|
|
|
// the HOME timelines of accounts that follow the status author.
|
2023-02-22 15:05:26 +00:00
|
|
|
func (p *Processor) timelineStatus(ctx context.Context, status *gtsmodel.Status) error {
|
2021-06-13 16:42:28 +00:00
|
|
|
// make sure the author account is pinned onto the status
|
2021-08-20 10:26:56 +00:00
|
|
|
if status.Account == nil {
|
2023-03-01 18:26:53 +00:00
|
|
|
a, err := p.state.DB.GetAccountByID(ctx, status.AccountID)
|
2021-08-20 10:26:56 +00:00
|
|
|
if err != nil {
|
2021-06-13 16:42:28 +00:00
|
|
|
return fmt.Errorf("timelineStatus: error getting author account with id %s: %s", status.AccountID, err)
|
|
|
|
}
|
2021-08-20 10:26:56 +00:00
|
|
|
status.Account = a
|
2021-06-13 16:42:28 +00:00
|
|
|
}
|
|
|
|
|
2023-03-20 18:10:08 +00:00
|
|
|
// Get LOCAL followers of the account that posted the status;
|
|
|
|
// we know that remote accounts don't have timelines on this
|
|
|
|
// instance, so there's no point selecting them too.
|
|
|
|
accountIDs, err := p.state.DB.GetLocalFollowersIDs(ctx, status.AccountID)
|
2021-08-20 10:26:56 +00:00
|
|
|
if err != nil {
|
2021-06-13 16:42:28 +00:00
|
|
|
return fmt.Errorf("timelineStatus: error getting followers for account id %s: %s", status.AccountID, err)
|
|
|
|
}
|
|
|
|
|
2023-03-20 18:10:08 +00:00
|
|
|
// 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() {
|
|
|
|
accountIDs = append(accountIDs, status.AccountID)
|
2021-06-13 16:42:28 +00:00
|
|
|
}
|
|
|
|
|
2023-03-20 18:10:08 +00:00
|
|
|
// Timeline the status for each local following account.
|
|
|
|
errors := gtserror.MultiError{}
|
|
|
|
for _, accountID := range accountIDs {
|
|
|
|
if err := p.timelineStatusForAccount(ctx, status, accountID); err != nil {
|
|
|
|
errors.Append(err)
|
2021-06-13 16:42:28 +00:00
|
|
|
}
|
2023-03-20 18:10:08 +00:00
|
|
|
}
|
2021-06-13 16:42:28 +00:00
|
|
|
|
2023-03-20 18:10:08 +00:00
|
|
|
if len(errors) != 0 {
|
|
|
|
return fmt.Errorf("timelineStatus: one or more errors timelining statuses: %w", errors.Combine())
|
2021-06-13 16:42:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-22 18:03:21 +00:00
|
|
|
// 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.
|
2023-03-20 18:10:08 +00:00
|
|
|
func (p *Processor) timelineStatusForAccount(ctx context.Context, status *gtsmodel.Status, accountID string) error {
|
2021-06-17 16:02:33 +00:00
|
|
|
// get the timeline owner account
|
2023-03-01 18:26:53 +00:00
|
|
|
timelineAccount, err := p.state.DB.GetAccountByID(ctx, accountID)
|
2021-08-20 10:26:56 +00:00
|
|
|
if err != nil {
|
2023-03-20 18:10:08 +00:00
|
|
|
return fmt.Errorf("timelineStatusForAccount: error getting account for timeline with id %s: %w", accountID, err)
|
2021-06-13 16:42:28 +00:00
|
|
|
}
|
|
|
|
|
2021-06-17 16:02:33 +00:00
|
|
|
// make sure the status is timelineable
|
2023-03-20 18:10:08 +00:00
|
|
|
if timelineable, err := p.filter.StatusHometimelineable(ctx, status, timelineAccount); err != nil {
|
|
|
|
return fmt.Errorf("timelineStatusForAccount: error getting timelineability for status for timeline with id %s: %w", accountID, err)
|
|
|
|
} else if !timelineable {
|
|
|
|
return nil
|
2021-06-13 16:42:28 +00:00
|
|
|
}
|
|
|
|
|
2021-06-19 09:18:55 +00:00
|
|
|
// stick the status in the timeline for the account and then immediately prepare it so they can see it right away
|
2023-03-20 18:10:08 +00:00
|
|
|
if inserted, err := p.statusTimelines.IngestAndPrepare(ctx, status, timelineAccount.ID); err != nil {
|
|
|
|
return fmt.Errorf("timelineStatusForAccount: error ingesting status %s: %w", status.ID, err)
|
|
|
|
} else if !inserted {
|
|
|
|
return nil
|
2021-06-19 09:18:55 +00:00
|
|
|
}
|
|
|
|
|
2021-11-22 18:03:21 +00:00
|
|
|
// the status was inserted so stream it to the user
|
2023-03-20 18:10:08 +00:00
|
|
|
apiStatus, err := p.tc.StatusToAPIStatus(ctx, status, timelineAccount)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("timelineStatusForAccount: error converting status %s to frontend representation: %w", status.ID, err)
|
|
|
|
}
|
2021-06-19 09:18:55 +00:00
|
|
|
|
2023-03-20 18:10:08 +00:00
|
|
|
if err := p.stream.Update(apiStatus, timelineAccount, stream.TimelineHome); err != nil {
|
|
|
|
return fmt.Errorf("timelineStatusForAccount: error streaming update for status %s: %w", status.ID, err)
|
2021-06-13 16:42:28 +00:00
|
|
|
}
|
2023-03-20 18:10:08 +00:00
|
|
|
|
|
|
|
return nil
|
2021-06-13 16:42:28 +00:00
|
|
|
}
|
|
|
|
|
2021-11-22 18:03:21 +00:00
|
|
|
// deleteStatusFromTimelines completely removes the given status from all timelines.
|
|
|
|
// It will also stream deletion of the status to all open streams.
|
2023-02-22 15:05:26 +00:00
|
|
|
func (p *Processor) deleteStatusFromTimelines(ctx context.Context, status *gtsmodel.Status) error {
|
2022-02-05 11:47:38 +00:00
|
|
|
if err := p.statusTimelines.WipeItemFromAllTimelines(ctx, status.ID); err != nil {
|
2021-06-21 13:56:00 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-02-22 15:05:26 +00:00
|
|
|
return p.stream.Delete(status.ID)
|
2021-06-13 16:42:28 +00:00
|
|
|
}
|
2022-05-18 21:13:03 +00:00
|
|
|
|
|
|
|
// wipeStatus contains common logic used to totally delete a status
|
|
|
|
// + all its attachments, notifications, boosts, and timeline entries.
|
2023-02-22 15:05:26 +00:00
|
|
|
func (p *Processor) wipeStatus(ctx context.Context, statusToDelete *gtsmodel.Status, deleteAttachments bool) error {
|
2022-06-24 15:17:40 +00:00
|
|
|
// 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 {
|
2023-02-22 15:05:26 +00:00
|
|
|
if err := p.media.Delete(ctx, a); err != nil {
|
2022-06-24 15:17:40 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for _, a := range statusToDelete.AttachmentIDs {
|
2023-02-22 15:05:26 +00:00
|
|
|
if _, err := p.media.Unattach(ctx, statusToDelete.Account, a); err != nil {
|
2022-06-24 15:17:40 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-05-18 21:13:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-21 17:55:52 +00:00
|
|
|
// delete all mention entries generated by this status
|
2022-05-18 21:13:03 +00:00
|
|
|
for _, m := range statusToDelete.MentionIDs {
|
2023-03-01 18:26:53 +00:00
|
|
|
if err := p.state.DB.DeleteByID(ctx, m, >smodel.Mention{}); err != nil {
|
2022-05-18 21:13:03 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-21 17:55:52 +00:00
|
|
|
// delete all notification entries generated by this status
|
2023-03-20 18:10:08 +00:00
|
|
|
if err := p.state.DB.DeleteNotificationsForStatus(ctx, statusToDelete.ID); err != nil {
|
2022-05-18 21:13:03 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-02-23 22:11:10 +00:00
|
|
|
// delete all bookmarks that point to this status
|
2023-03-20 18:10:08 +00:00
|
|
|
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 {
|
2023-02-23 22:11:10 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-05-18 21:13:03 +00:00
|
|
|
// delete all boosts for this status + remove them from timelines
|
2023-03-01 18:26:53 +00:00
|
|
|
if boosts, err := p.state.DB.GetStatusReblogs(ctx, statusToDelete); err == nil {
|
2022-09-21 17:55:52 +00:00
|
|
|
for _, b := range boosts {
|
|
|
|
if err := p.deleteStatusFromTimelines(ctx, b); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-03-01 18:26:53 +00:00
|
|
|
if err := p.state.DB.DeleteStatusByID(ctx, b.ID); err != nil {
|
2022-09-21 17:55:52 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-05-18 21:13:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// delete this status from any and all timelines
|
|
|
|
if err := p.deleteStatusFromTimelines(ctx, statusToDelete); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-09-21 17:55:52 +00:00
|
|
|
// delete the status itself
|
2023-03-01 18:26:53 +00:00
|
|
|
if err := p.state.DB.DeleteStatusByID(ctx, statusToDelete.ID); err != nil {
|
2022-09-21 17:55:52 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-05-18 21:13:03 +00:00
|
|
|
return nil
|
|
|
|
}
|