summaryrefslogtreecommitdiff
path: root/internal/processing/conversations/update.go
blob: 7445994ae67a138ff20dbd4ee57d527d715cae6f (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// 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 conversations

import (
	"context"
	"errors"

	apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
	"github.com/superseriousbusiness/gotosocial/internal/db"
	statusfilter "github.com/superseriousbusiness/gotosocial/internal/filter/status"
	"github.com/superseriousbusiness/gotosocial/internal/gtserror"
	"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
	"github.com/superseriousbusiness/gotosocial/internal/id"
	"github.com/superseriousbusiness/gotosocial/internal/log"
	"github.com/superseriousbusiness/gotosocial/internal/util"
)

// ConversationNotification carries the arguments to processing/stream.Processor.Conversation.
type ConversationNotification struct {
	// AccountID of a local account to deliver the notification to.
	AccountID string
	// Conversation as the notification payload.
	Conversation *apimodel.Conversation
}

// UpdateConversationsForStatus updates all conversations related to a status,
// and returns a map from local account IDs to conversation notifications that should be sent to them.
func (p *Processor) UpdateConversationsForStatus(ctx context.Context, status *gtsmodel.Status) ([]ConversationNotification, error) {
	if status.Visibility != gtsmodel.VisibilityDirect {
		// Only DMs are considered part of conversations.
		return nil, nil
	}
	if status.BoostOfID != "" {
		// Boosts can't be part of conversations.
		// FUTURE: This may change if we ever implement quote posts.
		return nil, nil
	}
	if status.ThreadID == "" {
		// If the status doesn't have a thread ID, it didn't mention a local account,
		// and thus can't be part of a conversation.
		return nil, nil
	}

	// We need accounts to be populated for this.
	if err := p.state.DB.PopulateStatus(ctx, status); err != nil {
		return nil, gtserror.Newf("DB error populating status %s: %w", status.ID, err)
	}

	// The account which authored the status plus all mentioned accounts.
	allParticipantsSet := make(map[string]*gtsmodel.Account, 1+len(status.Mentions))
	allParticipantsSet[status.AccountID] = status.Account
	for _, mention := range status.Mentions {
		allParticipantsSet[mention.TargetAccountID] = mention.TargetAccount
	}

	// Create or update conversations for and send notifications to each local participant.
	notifications := make([]ConversationNotification, 0, len(allParticipantsSet))
	for _, participant := range allParticipantsSet {
		if participant.IsRemote() {
			continue
		}
		localAccount := participant

		// If the status is not visible to this account, skip processing it for this account.
		visible, err := p.filter.StatusVisible(ctx, localAccount, status)
		if err != nil {
			log.Errorf(
				ctx,
				"error checking status %s visibility for account %s: %v",
				status.ID,
				localAccount.ID,
				err,
			)
			continue
		} else if !visible {
			continue
		}

		// Is the status filtered or muted for this user?
		// Converting the status to an API status runs the filter/mute checks.
		filters, mutes, errWithCode := p.getFiltersAndMutes(ctx, localAccount)
		if errWithCode != nil {
			log.Error(ctx, errWithCode)
			continue
		}
		_, err = p.converter.StatusToAPIStatus(
			ctx,
			status,
			localAccount,
			statusfilter.FilterContextNotifications,
			filters,
			mutes,
		)
		if err != nil {
			// If the status matched a hide filter, skip processing it for this account.
			// If there was another kind of error, log that and skip it anyway.
			if !errors.Is(err, statusfilter.ErrHideStatus) {
				log.Errorf(
					ctx,
					"error checking status %s filtering/muting for account %s: %v",
					status.ID,
					localAccount.ID,
					err,
				)
			}
			continue
		}

		// Collect other accounts participating in the conversation.
		otherAccounts := make([]*gtsmodel.Account, 0, len(allParticipantsSet)-1)
		otherAccountIDs := make([]string, 0, len(allParticipantsSet)-1)
		for accountID, account := range allParticipantsSet {
			if accountID != localAccount.ID {
				otherAccounts = append(otherAccounts, account)
				otherAccountIDs = append(otherAccountIDs, accountID)
			}
		}

		// Check for a previously existing conversation, if there is one.
		conversation, err := p.state.DB.GetConversationByThreadAndAccountIDs(
			ctx,
			status.ThreadID,
			localAccount.ID,
			otherAccountIDs,
		)
		if err != nil && !errors.Is(err, db.ErrNoEntries) {
			log.Errorf(
				ctx,
				"error trying to find a previous conversation for status %s and account %s: %v",
				status.ID,
				localAccount.ID,
				err,
			)
			continue
		}

		if conversation == nil {
			// Create a new conversation.
			conversation = &gtsmodel.Conversation{
				ID:               id.NewULID(),
				AccountID:        localAccount.ID,
				OtherAccountIDs:  otherAccountIDs,
				OtherAccounts:    otherAccounts,
				OtherAccountsKey: gtsmodel.ConversationOtherAccountsKey(otherAccountIDs),
				ThreadID:         status.ThreadID,
				Read:             util.Ptr(true),
			}
		}

		// Assume that if the conversation owner posted the status, they've already read it.
		statusAuthoredByConversationOwner := status.AccountID == conversation.AccountID

		// Update the conversation.
		// If there is no previous last status or this one is more recently created, set it as the last status.
		if conversation.LastStatus == nil || conversation.LastStatus.CreatedAt.Before(status.CreatedAt) {
			conversation.LastStatusID = status.ID
			conversation.LastStatus = status
		}
		// If the conversation is unread, leave it marked as unread.
		// If the conversation is read but this status might not have been, mark the conversation as unread.
		if !statusAuthoredByConversationOwner {
			conversation.Read = util.Ptr(false)
		}

		// Create or update the conversation.
		err = p.state.DB.UpsertConversation(ctx, conversation)
		if err != nil {
			log.Errorf(
				ctx,
				"error creating or updating conversation %s for status %s and account %s: %v",
				conversation.ID,
				status.ID,
				localAccount.ID,
				err,
			)
			continue
		}

		// Link the conversation to the status.
		if err := p.state.DB.LinkConversationToStatus(ctx, conversation.ID, status.ID); err != nil {
			log.Errorf(
				ctx,
				"error linking conversation %s to status %s: %v",
				conversation.ID,
				status.ID,
				err,
			)
			continue
		}

		// Convert the conversation to API representation.
		apiConversation, err := p.converter.ConversationToAPIConversation(
			ctx,
			conversation,
			localAccount,
			filters,
			mutes,
		)
		if err != nil {
			// If the conversation's last status matched a hide filter, skip it.
			// If there was another kind of error, log that and skip it anyway.
			if !errors.Is(err, statusfilter.ErrHideStatus) {
				log.Errorf(
					ctx,
					"error converting conversation %s to API representation for account %s: %v",
					status.ID,
					localAccount.ID,
					err,
				)
			}
			continue
		}

		// Generate a notification,
		// unless the status was authored by the user who would be notified,
		// in which case they already know.
		if status.AccountID != localAccount.ID {
			notifications = append(notifications, ConversationNotification{
				AccountID:    localAccount.ID,
				Conversation: apiConversation,
			})
		}
	}

	return notifications, nil
}