summaryrefslogtreecommitdiff
path: root/internal/federation/federatingdb/reject.go
blob: 498dfa8e937d19cebcc4c6ea8b7ebc6dc5822bbf (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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
// 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 federatingdb

import (
	"context"
	"errors"
	"time"

	"github.com/superseriousbusiness/activity/streams/vocab"
	"github.com/superseriousbusiness/gotosocial/internal/ap"
	"github.com/superseriousbusiness/gotosocial/internal/db"
	"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/messages"
	"github.com/superseriousbusiness/gotosocial/internal/uris"
)

func (f *federatingDB) Reject(ctx context.Context, reject vocab.ActivityStreamsReject) error {
	log.DebugKV(ctx, "reject", serialize{reject})

	activityContext := getActivityContext(ctx)
	if activityContext.internal {
		return nil // Already processed.
	}

	requestingAcct := activityContext.requestingAcct
	receivingAcct := activityContext.receivingAcct

	activityID := ap.GetJSONLDId(reject)
	if activityID == nil {
		// We need an ID.
		const text = "Reject had no id property"
		return gtserror.NewErrorBadRequest(errors.New(text), text)
	}

	for _, object := range ap.ExtractObjects(reject) {
		if asType := object.GetType(); asType != nil {
			// Check and handle any vocab.Type objects.
			switch name := asType.GetTypeName(); name {

			// REJECT FOLLOW
			case ap.ActivityFollow:
				if err := f.rejectFollowType(
					ctx,
					asType,
					receivingAcct,
					requestingAcct,
				); err != nil {
					return err
				}

			// UNHANDLED
			default:
				log.Debugf(ctx, "unhandled object type: %s", name)
			}

		} else if object.IsIRI() {
			// Check and handle any
			// IRI type objects.
			switch objIRI := object.GetIRI(); {

			// REJECT FOLLOW
			case uris.IsFollowPath(objIRI):
				if err := f.rejectFollowIRI(
					ctx,
					objIRI.String(),
					receivingAcct,
					requestingAcct,
				); err != nil {
					return err
				}

			// REJECT STATUS (reply/boost)
			case uris.IsStatusesPath(objIRI):
				if err := f.rejectStatusIRI(
					ctx,
					activityID.String(),
					objIRI.String(),
					receivingAcct,
					requestingAcct,
				); err != nil {
					return err
				}

			// REJECT LIKE
			case uris.IsLikePath(objIRI):
				if err := f.rejectLikeIRI(
					ctx,
					activityID.String(),
					objIRI.String(),
					receivingAcct,
					requestingAcct,
				); err != nil {
					return err
				}

			// UNHANDLED
			default:
				log.Debugf(ctx, "unhandled iri type: %s", objIRI)
			}
		}
	}

	return nil
}

func (f *federatingDB) rejectFollowType(
	ctx context.Context,
	asType vocab.Type,
	receivingAcct *gtsmodel.Account,
	requestingAcct *gtsmodel.Account,
) error {
	// Cast the vocab.Type object to known AS type.
	asFollow := asType.(vocab.ActivityStreamsFollow)

	// Reconstruct the follow.
	follow, err := f.converter.ASFollowToFollow(ctx, asFollow)
	if err != nil {
		err := gtserror.Newf("error converting Follow to *gtsmodel.Follow: %w", err)
		return gtserror.NewErrorInternalError(err)
	}

	// Lock on the Follow URI
	// as we may be updating it.
	unlock := f.state.FedLocks.Lock(follow.URI)
	defer unlock()

	// Make sure the creator of the original follow
	// is the same as whatever inbox this landed in.
	if follow.AccountID != receivingAcct.ID {
		const text = "Follow account and inbox account were not the same"
		return gtserror.NewErrorUnprocessableEntity(errors.New(text), text)
	}

	// Make sure the target of the original follow
	// is the same as the account making the request.
	if follow.TargetAccountID != requestingAcct.ID {
		const text = "Follow target account and requesting account were not the same"
		return gtserror.NewErrorForbidden(errors.New(text), text)
	}

	// Reject the follow.
	err = f.state.DB.RejectFollowRequest(
		ctx,
		follow.AccountID,
		follow.TargetAccountID,
	)
	if err != nil && !errors.Is(err, db.ErrNoEntries) {
		err := gtserror.Newf("db error rejecting follow request: %w", err)
		return gtserror.NewErrorInternalError(err)
	}

	return nil
}

func (f *federatingDB) rejectFollowIRI(
	ctx context.Context,
	objectIRI string,
	receivingAcct *gtsmodel.Account,
	requestingAcct *gtsmodel.Account,
) error {
	// Lock on this potential Follow
	// URI as we may be updating it.
	unlock := f.state.FedLocks.Lock(objectIRI)
	defer unlock()

	// Get the follow req from the db.
	followReq, err := f.state.DB.GetFollowRequestByURI(ctx, objectIRI)
	if err != nil && !errors.Is(err, db.ErrNoEntries) {
		err := gtserror.Newf("db error getting follow request: %w", err)
		return gtserror.NewErrorInternalError(err)
	}

	if followReq == nil {
		// We didn't have a follow request
		// with this URI, so nothing to do.
		// Just return.
		//
		// TODO: Handle Reject Follow to remove
		// an already-accepted follow relationship.
		return nil
	}

	// Make sure the creator of the original follow
	// is the same as whatever inbox this landed in.
	if followReq.AccountID != receivingAcct.ID {
		const text = "Follow account and inbox account were not the same"
		return gtserror.NewErrorUnprocessableEntity(errors.New(text), text)
	}

	// Make sure the target of the original follow
	// is the same as the account making the request.
	if followReq.TargetAccountID != requestingAcct.ID {
		const text = "Follow target account and requesting account were not the same"
		return gtserror.NewErrorForbidden(errors.New(text), text)
	}

	// Reject the follow.
	err = f.state.DB.RejectFollowRequest(
		ctx,
		followReq.AccountID,
		followReq.TargetAccountID,
	)
	if err != nil && !errors.Is(err, db.ErrNoEntries) {
		err := gtserror.Newf("db error rejecting follow request: %w", err)
		return gtserror.NewErrorInternalError(err)
	}

	return nil
}

func (f *federatingDB) rejectStatusIRI(
	ctx context.Context,
	activityID string,
	objectIRI string,
	receivingAcct *gtsmodel.Account,
	requestingAcct *gtsmodel.Account,
) error {
	// Lock on this potential status URI.
	unlock := f.state.FedLocks.Lock(objectIRI)
	defer unlock()

	// Get the status from the db.
	status, err := f.state.DB.GetStatusByURI(ctx, objectIRI)
	if err != nil && !errors.Is(err, db.ErrNoEntries) {
		err := gtserror.Newf("db error getting status: %w", err)
		return gtserror.NewErrorInternalError(err)
	}

	if status == nil {
		// We didn't have a status with
		// this URI, so nothing to do.
		// Just return.
		return nil
	}

	if !status.IsLocal() {
		// We don't process Rejects of statuses
		// that weren't created on our instance.
		// Just return.
		//
		// TODO: Handle Reject to remove *remote*
		// posts replying-to or boosting the
		// Rejecting account.
		return nil
	}

	// Make sure the creator of the original status
	// is the same as the inbox processing the Reject;
	// this also ensures the status is local.
	if status.AccountID != receivingAcct.ID {
		const text = "status author account and inbox account were not the same"
		return gtserror.NewErrorUnprocessableEntity(errors.New(text), text)
	}

	// Check if we're dealing with a reply
	// or an announce, and make sure the
	// requester is permitted to Reject.
	var apObjectType string
	if status.InReplyToID != "" {
		// Rejecting a Reply.
		apObjectType = ap.ObjectNote
		if status.InReplyToAccountID != requestingAcct.ID {
			const text = "status reply to account and requesting account were not the same"
			return gtserror.NewErrorForbidden(errors.New(text), text)
		}

		// You can't mention an account and then Reject replies from that
		// same account (harassment vector); don't process these Rejects.
		if status.InReplyTo != nil && status.InReplyTo.MentionsAccount(status.AccountID) {
			const text = "refusing to process Reject of a reply from a mentioned account"
			return gtserror.NewErrorForbidden(errors.New(text), text)
		}

	} else {
		// Rejecting an Announce.
		apObjectType = ap.ActivityAnnounce
		if status.BoostOfAccountID != requestingAcct.ID {
			const text = "status boost of account and requesting account were not the same"
			return gtserror.NewErrorForbidden(errors.New(text), text)
		}
	}

	// Check if there's an interaction request in the db for this status.
	req, err := f.state.DB.GetInteractionRequestByInteractionURI(ctx, status.URI)
	if err != nil && !errors.Is(err, db.ErrNoEntries) {
		err := gtserror.Newf("db error getting interaction request: %w", err)
		return gtserror.NewErrorInternalError(err)
	}

	switch {
	case req == nil:
		// No interaction request existed yet for this
		// status, create a pre-rejected request now.
		req = &gtsmodel.InteractionRequest{
			ID:                   id.NewULID(),
			TargetAccountID:      requestingAcct.ID,
			TargetAccount:        requestingAcct,
			InteractingAccountID: receivingAcct.ID,
			InteractingAccount:   receivingAcct,
			InteractionURI:       status.URI,
			URI:                  activityID,
			RejectedAt:           time.Now(),
		}

		if apObjectType == ap.ObjectNote {
			// Reply.
			req.InteractionType = gtsmodel.InteractionReply
			req.StatusID = status.InReplyToID
			req.Status = status.InReplyTo
			req.Reply = status
		} else {
			// Announce.
			req.InteractionType = gtsmodel.InteractionAnnounce
			req.StatusID = status.BoostOfID
			req.Status = status.BoostOf
			req.Announce = status
		}

		if err := f.state.DB.PutInteractionRequest(ctx, req); err != nil {
			err := gtserror.Newf("db error inserting interaction request: %w", err)
			return gtserror.NewErrorInternalError(err)
		}

	case req.IsRejected():
		// Interaction has already been rejected. Just
		// update to this Reject URI and then return early.
		req.URI = activityID
		if err := f.state.DB.UpdateInteractionRequest(ctx, req, "uri"); err != nil {
			err := gtserror.Newf("db error updating interaction request: %w", err)
			return gtserror.NewErrorInternalError(err)
		}
		return nil

	default:
		// Mark existing interaction request as
		// Rejected, even if previously Accepted.
		req.AcceptedAt = time.Time{}
		req.RejectedAt = time.Now()
		req.URI = activityID
		if err := f.state.DB.UpdateInteractionRequest(ctx, req,
			"accepted_at",
			"rejected_at",
			"uri",
		); err != nil {
			err := gtserror.Newf("db error updating interaction request: %w", err)
			return gtserror.NewErrorInternalError(err)
		}
	}

	// Send the rejected request through to
	// the fedi worker to process side effects.
	f.state.Workers.Federator.Queue.Push(&messages.FromFediAPI{
		APObjectType:   apObjectType,
		APActivityType: ap.ActivityReject,
		GTSModel:       req,
		Receiving:      receivingAcct,
		Requesting:     requestingAcct,
	})

	return nil
}

func (f *federatingDB) rejectLikeIRI(
	ctx context.Context,
	activityID string,
	objectIRI string,
	receivingAcct *gtsmodel.Account,
	requestingAcct *gtsmodel.Account,
) error {
	// Lock on this potential Like
	// URI as we may be updating it.
	unlock := f.state.FedLocks.Lock(objectIRI)
	defer unlock()

	// Get the fave from the db.
	fave, err := f.state.DB.GetStatusFaveByURI(ctx, objectIRI)
	if err != nil && !errors.Is(err, db.ErrNoEntries) {
		err := gtserror.Newf("db error getting fave: %w", err)
		return gtserror.NewErrorInternalError(err)
	}

	if fave == nil {
		// We didn't have a fave with
		// this URI, so nothing to do.
		// Just return.
		return nil
	}

	if !fave.Account.IsLocal() {
		// We don't process Rejects of Likes
		// that weren't created on our instance.
		// Just return.
		//
		// TODO: Handle Reject to remove *remote*
		// likes targeting the Rejecting account.
		return nil
	}

	// Make sure the creator of the original Like
	// is the same as the inbox processing the Reject;
	// this also ensures the Like is local.
	if fave.AccountID != receivingAcct.ID {
		const text = "fave creator account and inbox account were not the same"
		return gtserror.NewErrorUnprocessableEntity(errors.New(text), text)
	}

	// Make sure the target of the Like is the
	// same as the account doing the Reject.
	if fave.TargetAccountID != requestingAcct.ID {
		const text = "status fave target account and requesting account were not the same"
		return gtserror.NewErrorForbidden(errors.New(text), text)
	}

	// Check if there's an interaction request in the db for this like.
	req, err := f.state.DB.GetInteractionRequestByInteractionURI(ctx, fave.URI)
	if err != nil && !errors.Is(err, db.ErrNoEntries) {
		err := gtserror.Newf("db error getting interaction request: %w", err)
		return gtserror.NewErrorInternalError(err)
	}

	switch {
	case req == nil:
		// No interaction request existed yet for this
		// fave, create a pre-rejected request now.
		req = &gtsmodel.InteractionRequest{
			ID:                   id.NewULID(),
			TargetAccountID:      requestingAcct.ID,
			TargetAccount:        requestingAcct,
			InteractingAccountID: receivingAcct.ID,
			InteractingAccount:   receivingAcct,
			InteractionURI:       fave.URI,
			InteractionType:      gtsmodel.InteractionLike,
			Like:                 fave,
			URI:                  activityID,
			RejectedAt:           time.Now(),
		}

		if err := f.state.DB.PutInteractionRequest(ctx, req); err != nil {
			err := gtserror.Newf("db error inserting interaction request: %w", err)
			return gtserror.NewErrorInternalError(err)
		}

	case req.IsRejected():
		// Interaction has already been rejected. Just
		// update to this Reject URI and then return early.
		req.URI = activityID
		if err := f.state.DB.UpdateInteractionRequest(ctx, req, "uri"); err != nil {
			err := gtserror.Newf("db error updating interaction request: %w", err)
			return gtserror.NewErrorInternalError(err)
		}
		return nil

	default:
		// Mark existing interaction request as
		// Rejected, even if previously Accepted.
		req.AcceptedAt = time.Time{}
		req.RejectedAt = time.Now()
		req.URI = activityID
		if err := f.state.DB.UpdateInteractionRequest(ctx, req,
			"accepted_at",
			"rejected_at",
			"uri",
		); err != nil {
			err := gtserror.Newf("db error updating interaction request: %w", err)
			return gtserror.NewErrorInternalError(err)
		}
	}

	// Send the rejected request through to
	// the fedi worker to process side effects.
	f.state.Workers.Federator.Queue.Push(&messages.FromFediAPI{
		APObjectType:   ap.ActivityLike,
		APActivityType: ap.ActivityReject,
		GTSModel:       req,
		Receiving:      receivingAcct,
		Requesting:     requestingAcct,
	})

	return nil
}