summaryrefslogtreecommitdiff
path: root/internal/visibility/util.go
blob: f52661d0b1dea60840103e2157fb044ad1d5401b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package visibility

import (
	"fmt"

	"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
)

func (f *filter) pullRelevantAccountsFromStatus(targetStatus *gtsmodel.Status) (*relevantAccounts, error) {
	accounts := &relevantAccounts{
		MentionedAccounts: []*gtsmodel.Account{},
	}

	// get the author account
	if targetStatus.GTSAuthorAccount == nil {
		statusAuthor := &gtsmodel.Account{}
		if err := f.db.GetByID(targetStatus.AccountID, statusAuthor); err != nil {
			return accounts, fmt.Errorf("PullRelevantAccountsFromStatus: error getting statusAuthor with id %s: %s", targetStatus.AccountID, err)
		}
		targetStatus.GTSAuthorAccount = statusAuthor
	}
	accounts.StatusAuthor = targetStatus.GTSAuthorAccount

	// get the replied to account from the status and add it to the pile
	if targetStatus.InReplyToAccountID != "" {
		repliedToAccount := &gtsmodel.Account{}
		if err := f.db.GetByID(targetStatus.InReplyToAccountID, repliedToAccount); err != nil {
			return accounts, fmt.Errorf("PullRelevantAccountsFromStatus: error getting repliedToAcount with id %s: %s", targetStatus.InReplyToAccountID, err)
		}
		accounts.ReplyToAccount = repliedToAccount
	}

	// get the boosted account from the status and add it to the pile
	if targetStatus.BoostOfID != "" {
		// retrieve the boosted status first
		boostedStatus := &gtsmodel.Status{}
		if err := f.db.GetByID(targetStatus.BoostOfID, boostedStatus); err != nil {
			return accounts, fmt.Errorf("PullRelevantAccountsFromStatus: error getting boostedStatus with id %s: %s", targetStatus.BoostOfID, err)
		}
		boostedAccount := &gtsmodel.Account{}
		if err := f.db.GetByID(boostedStatus.AccountID, boostedAccount); err != nil {
			return accounts, fmt.Errorf("PullRelevantAccountsFromStatus: error getting boostedAccount with id %s: %s", boostedStatus.AccountID, err)
		}
		accounts.BoostedAccount = boostedAccount

		// the boosted status might be a reply to another account so we should get that too
		if boostedStatus.InReplyToAccountID != "" {
			boostedStatusRepliedToAccount := &gtsmodel.Account{}
			if err := f.db.GetByID(boostedStatus.InReplyToAccountID, boostedStatusRepliedToAccount); err != nil {
				return accounts, fmt.Errorf("PullRelevantAccountsFromStatus: error getting boostedStatusRepliedToAccount with id %s: %s", boostedStatus.InReplyToAccountID, err)
			}
			accounts.BoostedReplyToAccount = boostedStatusRepliedToAccount
		}
	}

	// now get all accounts with IDs that are mentioned in the status
	for _, mentionID := range targetStatus.Mentions {

		mention := &gtsmodel.Mention{}
		if err := f.db.GetByID(mentionID, mention); err != nil {
			return accounts, fmt.Errorf("PullRelevantAccountsFromStatus: error getting mention with id %s: %s", mentionID, err)
		}

		mentionedAccount := &gtsmodel.Account{}
		if err := f.db.GetByID(mention.TargetAccountID, mentionedAccount); err != nil {
			return accounts, fmt.Errorf("PullRelevantAccountsFromStatus: error getting mentioned account: %s", err)
		}
		accounts.MentionedAccounts = append(accounts.MentionedAccounts, mentionedAccount)
	}

	return accounts, nil
}

// relevantAccounts denotes accounts that are replied to, boosted by, or mentioned in a status.
type relevantAccounts struct {
	StatusAuthor          *gtsmodel.Account
	ReplyToAccount        *gtsmodel.Account
	BoostedAccount        *gtsmodel.Account
	BoostedReplyToAccount *gtsmodel.Account
	MentionedAccounts     []*gtsmodel.Account
}