summaryrefslogtreecommitdiff
path: root/internal/processing/fromclientapi.go
blob: 6035130e2ce93db18aaafc4a9a81d6d88861235e (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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
/*
   GoToSocial
   Copyright (C) 2021-2023 GoToSocial Authors admin@gotosocial.org

   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"
	"net/url"

	"codeberg.org/gruf/go-kv"
	"codeberg.org/gruf/go-logger/v2/level"
	"github.com/superseriousbusiness/activity/pub"
	"github.com/superseriousbusiness/activity/streams"
	"github.com/superseriousbusiness/gotosocial/internal/ap"
	"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
	"github.com/superseriousbusiness/gotosocial/internal/log"
	"github.com/superseriousbusiness/gotosocial/internal/messages"
)

func (p *processor) ProcessFromClientAPI(ctx context.Context, clientMsg messages.FromClientAPI) error {
	// Allocate new log fields slice
	fields := make([]kv.Field, 3, 4)
	fields[0] = kv.Field{"activityType", clientMsg.APActivityType}
	fields[1] = kv.Field{"objectType", clientMsg.APObjectType}
	fields[2] = kv.Field{"fromAccount", clientMsg.OriginAccount.Username}

	if clientMsg.GTSModel != nil &&
		log.Level() >= level.DEBUG {
		// Append converted model to log
		fields = append(fields, kv.Field{
			"model", clientMsg.GTSModel,
		})
	}

	// Log this federated message
	l := log.WithFields(fields...)
	l.Info("processing from client")

	switch clientMsg.APActivityType {
	case ap.ActivityCreate:
		// CREATE
		switch clientMsg.APObjectType {
		case ap.ObjectProfile, ap.ActorPerson:
			// CREATE ACCOUNT/PROFILE
			return p.processCreateAccountFromClientAPI(ctx, clientMsg)
		case ap.ObjectNote:
			// CREATE NOTE
			return p.processCreateStatusFromClientAPI(ctx, clientMsg)
		case ap.ActivityFollow:
			// CREATE FOLLOW REQUEST
			return p.processCreateFollowRequestFromClientAPI(ctx, clientMsg)
		case ap.ActivityLike:
			// CREATE LIKE/FAVE
			return p.processCreateFaveFromClientAPI(ctx, clientMsg)
		case ap.ActivityAnnounce:
			// CREATE BOOST/ANNOUNCE
			return p.processCreateAnnounceFromClientAPI(ctx, clientMsg)
		case ap.ActivityBlock:
			// CREATE BLOCK
			return p.processCreateBlockFromClientAPI(ctx, clientMsg)
		}
	case ap.ActivityUpdate:
		// UPDATE
		switch clientMsg.APObjectType {
		case ap.ObjectProfile, ap.ActorPerson:
			// UPDATE ACCOUNT/PROFILE
			return p.processUpdateAccountFromClientAPI(ctx, clientMsg)
		}
	case ap.ActivityAccept:
		// ACCEPT
		if clientMsg.APObjectType == ap.ActivityFollow {
			// ACCEPT FOLLOW
			return p.processAcceptFollowFromClientAPI(ctx, clientMsg)
		}
	case ap.ActivityReject:
		// REJECT
		if clientMsg.APObjectType == ap.ActivityFollow {
			// REJECT FOLLOW (request)
			return p.processRejectFollowFromClientAPI(ctx, clientMsg)
		}
	case ap.ActivityUndo:
		// UNDO
		switch clientMsg.APObjectType {
		case ap.ActivityFollow:
			// UNDO FOLLOW
			return p.processUndoFollowFromClientAPI(ctx, clientMsg)
		case ap.ActivityBlock:
			// UNDO BLOCK
			return p.processUndoBlockFromClientAPI(ctx, clientMsg)
		case ap.ActivityLike:
			// UNDO LIKE/FAVE
			return p.processUndoFaveFromClientAPI(ctx, clientMsg)
		case ap.ActivityAnnounce:
			// UNDO ANNOUNCE/BOOST
			return p.processUndoAnnounceFromClientAPI(ctx, clientMsg)
		}
	case ap.ActivityDelete:
		// DELETE
		switch clientMsg.APObjectType {
		case ap.ObjectNote:
			// DELETE STATUS/NOTE
			return p.processDeleteStatusFromClientAPI(ctx, clientMsg)
		case ap.ObjectProfile, ap.ActorPerson:
			// DELETE ACCOUNT/PROFILE
			return p.processDeleteAccountFromClientAPI(ctx, clientMsg)
		}
	case ap.ActivityFlag:
		// FLAG
		if clientMsg.APObjectType == ap.ObjectProfile {
			// FLAG/REPORT A PROFILE
			return p.processReportAccountFromClientAPI(ctx, clientMsg)
		}
	}
	return nil
}

func (p *processor) processCreateAccountFromClientAPI(ctx context.Context, clientMsg messages.FromClientAPI) error {
	account, ok := clientMsg.GTSModel.(*gtsmodel.Account)
	if !ok {
		return errors.New("account was not parseable as *gtsmodel.Account")
	}

	// return if the account isn't from this domain
	if account.Domain != "" {
		return nil
	}

	// get the user this account belongs to
	user, err := p.db.GetUserByAccountID(ctx, account.ID)
	if err != nil {
		return err
	}

	// email a confirmation to this user
	return p.userProcessor.SendConfirmEmail(ctx, user, account.Username)
}

func (p *processor) processCreateStatusFromClientAPI(ctx context.Context, clientMsg messages.FromClientAPI) error {
	status, ok := clientMsg.GTSModel.(*gtsmodel.Status)
	if !ok {
		return errors.New("note was not parseable as *gtsmodel.Status")
	}

	if err := p.timelineStatus(ctx, status); err != nil {
		return err
	}

	if err := p.notifyStatus(ctx, status); err != nil {
		return err
	}

	return p.federateStatus(ctx, status)
}

func (p *processor) processCreateFollowRequestFromClientAPI(ctx context.Context, clientMsg messages.FromClientAPI) error {
	followRequest, ok := clientMsg.GTSModel.(*gtsmodel.FollowRequest)
	if !ok {
		return errors.New("followrequest was not parseable as *gtsmodel.FollowRequest")
	}

	if err := p.notifyFollowRequest(ctx, followRequest); err != nil {
		return err
	}

	return p.federateFollow(ctx, followRequest, clientMsg.OriginAccount, clientMsg.TargetAccount)
}

func (p *processor) processCreateFaveFromClientAPI(ctx context.Context, clientMsg messages.FromClientAPI) error {
	fave, ok := clientMsg.GTSModel.(*gtsmodel.StatusFave)
	if !ok {
		return errors.New("fave was not parseable as *gtsmodel.StatusFave")
	}

	if err := p.notifyFave(ctx, fave); err != nil {
		return err
	}

	return p.federateFave(ctx, fave, clientMsg.OriginAccount, clientMsg.TargetAccount)
}

func (p *processor) processCreateAnnounceFromClientAPI(ctx context.Context, clientMsg messages.FromClientAPI) error {
	boostWrapperStatus, ok := clientMsg.GTSModel.(*gtsmodel.Status)
	if !ok {
		return errors.New("boost was not parseable as *gtsmodel.Status")
	}

	if err := p.timelineStatus(ctx, boostWrapperStatus); err != nil {
		return err
	}

	if err := p.notifyAnnounce(ctx, boostWrapperStatus); err != nil {
		return err
	}

	return p.federateAnnounce(ctx, boostWrapperStatus, clientMsg.OriginAccount, clientMsg.TargetAccount)
}

func (p *processor) processCreateBlockFromClientAPI(ctx context.Context, clientMsg messages.FromClientAPI) error {
	block, ok := clientMsg.GTSModel.(*gtsmodel.Block)
	if !ok {
		return errors.New("block was not parseable as *gtsmodel.Block")
	}

	// remove any of the blocking account's statuses from the blocked account's timeline, and vice versa
	if err := p.statusTimelines.WipeItemsFromAccountID(ctx, block.AccountID, block.TargetAccountID); err != nil {
		return err
	}
	if err := p.statusTimelines.WipeItemsFromAccountID(ctx, block.TargetAccountID, block.AccountID); err != nil {
		return err
	}

	// TODO: same with notifications
	// TODO: same with bookmarks

	return p.federateBlock(ctx, block)
}

func (p *processor) processUpdateAccountFromClientAPI(ctx context.Context, clientMsg messages.FromClientAPI) error {
	account, ok := clientMsg.GTSModel.(*gtsmodel.Account)
	if !ok {
		return errors.New("account was not parseable as *gtsmodel.Account")
	}

	return p.federateAccountUpdate(ctx, account, clientMsg.OriginAccount)
}

func (p *processor) processAcceptFollowFromClientAPI(ctx context.Context, clientMsg messages.FromClientAPI) error {
	follow, ok := clientMsg.GTSModel.(*gtsmodel.Follow)
	if !ok {
		return errors.New("accept was not parseable as *gtsmodel.Follow")
	}

	if err := p.notifyFollow(ctx, follow, clientMsg.TargetAccount); err != nil {
		return err
	}

	return p.federateAcceptFollowRequest(ctx, follow)
}

func (p *processor) processRejectFollowFromClientAPI(ctx context.Context, clientMsg messages.FromClientAPI) error {
	followRequest, ok := clientMsg.GTSModel.(*gtsmodel.FollowRequest)
	if !ok {
		return errors.New("reject was not parseable as *gtsmodel.FollowRequest")
	}

	return p.federateRejectFollowRequest(ctx, followRequest)
}

func (p *processor) processUndoFollowFromClientAPI(ctx context.Context, clientMsg messages.FromClientAPI) error {
	follow, ok := clientMsg.GTSModel.(*gtsmodel.Follow)
	if !ok {
		return errors.New("undo was not parseable as *gtsmodel.Follow")
	}
	return p.federateUnfollow(ctx, follow, clientMsg.OriginAccount, clientMsg.TargetAccount)
}

func (p *processor) processUndoBlockFromClientAPI(ctx context.Context, clientMsg messages.FromClientAPI) error {
	block, ok := clientMsg.GTSModel.(*gtsmodel.Block)
	if !ok {
		return errors.New("undo was not parseable as *gtsmodel.Block")
	}
	return p.federateUnblock(ctx, block)
}

func (p *processor) processUndoFaveFromClientAPI(ctx context.Context, clientMsg messages.FromClientAPI) error {
	fave, ok := clientMsg.GTSModel.(*gtsmodel.StatusFave)
	if !ok {
		return errors.New("undo was not parseable as *gtsmodel.StatusFave")
	}
	return p.federateUnfave(ctx, fave, clientMsg.OriginAccount, clientMsg.TargetAccount)
}

func (p *processor) processUndoAnnounceFromClientAPI(ctx context.Context, clientMsg messages.FromClientAPI) error {
	boost, ok := clientMsg.GTSModel.(*gtsmodel.Status)
	if !ok {
		return errors.New("undo was not parseable as *gtsmodel.Status")
	}

	if err := p.db.DeleteStatusByID(ctx, boost.ID); err != nil {
		return err
	}

	if err := p.deleteStatusFromTimelines(ctx, boost); err != nil {
		return err
	}

	return p.federateUnannounce(ctx, boost, clientMsg.OriginAccount, clientMsg.TargetAccount)
}

func (p *processor) processDeleteStatusFromClientAPI(ctx context.Context, clientMsg messages.FromClientAPI) error {
	statusToDelete, ok := clientMsg.GTSModel.(*gtsmodel.Status)
	if !ok {
		return errors.New("note was not parseable as *gtsmodel.Status")
	}

	if statusToDelete.Account == nil {
		statusToDelete.Account = clientMsg.OriginAccount
	}

	// don't delete attachments, just unattach them;
	// since this request comes from the client API
	// and the poster might want to use the attachments
	// again in a new post
	deleteAttachments := false
	if err := p.wipeStatus(ctx, statusToDelete, deleteAttachments); err != nil {
		return err
	}

	return p.federateStatusDelete(ctx, statusToDelete)
}

func (p *processor) processDeleteAccountFromClientAPI(ctx context.Context, clientMsg messages.FromClientAPI) error {
	// the origin of the delete could be either a domain block, or an action by another (or this) account
	var origin string
	if domainBlock, ok := clientMsg.GTSModel.(*gtsmodel.DomainBlock); ok {
		// origin is a domain block
		origin = domainBlock.ID
	} else {
		// origin is whichever account caused this message
		origin = clientMsg.OriginAccount.ID
	}

	if err := p.federateAccountDelete(ctx, clientMsg.TargetAccount); err != nil {
		return err
	}

	return p.accountProcessor.Delete(ctx, clientMsg.TargetAccount, origin)
}

func (p *processor) processReportAccountFromClientAPI(ctx context.Context, clientMsg messages.FromClientAPI) error {
	report, ok := clientMsg.GTSModel.(*gtsmodel.Report)
	if !ok {
		return errors.New("report was not parseable as *gtsmodel.Report")
	}

	// TODO: in a separate PR, also email admin(s)

	if !*report.Forwarded {
		// nothing to do, don't federate the report
		return nil
	}

	return p.federateReport(ctx, report)
}

// TODO: move all the below functions into federation.Federator

func (p *processor) federateAccountDelete(ctx context.Context, account *gtsmodel.Account) error {
	// do nothing if this isn't our account
	if account.Domain != "" {
		return nil
	}

	outboxIRI, err := url.Parse(account.OutboxURI)
	if err != nil {
		return fmt.Errorf("federateAccountDelete: error parsing outboxURI %s: %s", account.OutboxURI, err)
	}

	actorIRI, err := url.Parse(account.URI)
	if err != nil {
		return fmt.Errorf("federateAccountDelete: error parsing actorIRI %s: %s", account.URI, err)
	}

	followersIRI, err := url.Parse(account.FollowersURI)
	if err != nil {
		return fmt.Errorf("federateAccountDelete: error parsing followersIRI %s: %s", account.FollowersURI, err)
	}

	publicIRI, err := url.Parse(pub.PublicActivityPubIRI)
	if err != nil {
		return fmt.Errorf("federateAccountDelete: error parsing url %s: %s", pub.PublicActivityPubIRI, err)
	}

	// create a delete and set the appropriate actor on it
	delete := streams.NewActivityStreamsDelete()

	// set the actor for the delete; no matter who deleted it we should use the account owner for this
	deleteActor := streams.NewActivityStreamsActorProperty()
	deleteActor.AppendIRI(actorIRI)
	delete.SetActivityStreamsActor(deleteActor)

	// Set the account IRI as the 'object' property.
	deleteObject := streams.NewActivityStreamsObjectProperty()
	deleteObject.AppendIRI(actorIRI)
	delete.SetActivityStreamsObject(deleteObject)

	// send to followers...
	deleteTo := streams.NewActivityStreamsToProperty()
	deleteTo.AppendIRI(followersIRI)
	delete.SetActivityStreamsTo(deleteTo)

	// ... and CC to public
	deleteCC := streams.NewActivityStreamsCcProperty()
	deleteCC.AppendIRI(publicIRI)
	delete.SetActivityStreamsCc(deleteCC)

	_, err = p.federator.FederatingActor().Send(ctx, outboxIRI, delete)
	return err
}

func (p *processor) federateStatus(ctx context.Context, status *gtsmodel.Status) error {
	// do nothing if the status shouldn't be federated
	if !*status.Federated {
		return nil
	}

	if status.Account == nil {
		statusAccount, err := p.db.GetAccountByID(ctx, status.AccountID)
		if err != nil {
			return fmt.Errorf("federateStatus: error fetching status author account: %s", err)
		}
		status.Account = statusAccount
	}

	// do nothing if this isn't our status
	if status.Account.Domain != "" {
		return nil
	}

	asStatus, err := p.tc.StatusToAS(ctx, status)
	if err != nil {
		return fmt.Errorf("federateStatus: error converting status to as format: %s", err)
	}

	create, err := p.tc.WrapNoteInCreate(asStatus, false)
	if err != nil {
		return fmt.Errorf("federateStatus: error wrapping status in create: %s", err)
	}

	outboxIRI, err := url.Parse(status.Account.OutboxURI)
	if err != nil {
		return fmt.Errorf("federateStatus: error parsing outboxURI %s: %s", status.Account.OutboxURI, err)
	}

	_, err = p.federator.FederatingActor().Send(ctx, outboxIRI, create)
	return err
}

func (p *processor) federateStatusDelete(ctx context.Context, status *gtsmodel.Status) error {
	if status.Account == nil {
		statusAccount, err := p.db.GetAccountByID(ctx, status.AccountID)
		if err != nil {
			return fmt.Errorf("federateStatusDelete: error fetching status author account: %s", err)
		}
		status.Account = statusAccount
	}

	// do nothing if this isn't our status
	if status.Account.Domain != "" {
		return nil
	}

	asStatus, err := p.tc.StatusToAS(ctx, status)
	if err != nil {
		return fmt.Errorf("federateStatusDelete: error converting status to as format: %s", err)
	}

	outboxIRI, err := url.Parse(status.Account.OutboxURI)
	if err != nil {
		return fmt.Errorf("federateStatusDelete: error parsing outboxURI %s: %s", status.Account.OutboxURI, err)
	}

	actorIRI, err := url.Parse(status.Account.URI)
	if err != nil {
		return fmt.Errorf("federateStatusDelete: error parsing actorIRI %s: %s", status.Account.URI, err)
	}

	// create a delete and set the appropriate actor on it
	delete := streams.NewActivityStreamsDelete()

	// set the actor for the delete
	deleteActor := streams.NewActivityStreamsActorProperty()
	deleteActor.AppendIRI(actorIRI)
	delete.SetActivityStreamsActor(deleteActor)

	// Set the status as the 'object' property.
	deleteObject := streams.NewActivityStreamsObjectProperty()
	deleteObject.AppendActivityStreamsNote(asStatus)
	delete.SetActivityStreamsObject(deleteObject)

	// set the to and cc as the original to/cc of the original status
	delete.SetActivityStreamsTo(asStatus.GetActivityStreamsTo())
	delete.SetActivityStreamsCc(asStatus.GetActivityStreamsCc())

	_, err = p.federator.FederatingActor().Send(ctx, outboxIRI, delete)
	return err
}

func (p *processor) federateFollow(ctx context.Context, followRequest *gtsmodel.FollowRequest, originAccount *gtsmodel.Account, targetAccount *gtsmodel.Account) error {
	// if both accounts are local there's nothing to do here
	if originAccount.Domain == "" && targetAccount.Domain == "" {
		return nil
	}

	follow := p.tc.FollowRequestToFollow(ctx, followRequest)

	asFollow, err := p.tc.FollowToAS(ctx, follow, originAccount, targetAccount)
	if err != nil {
		return fmt.Errorf("federateFollow: error converting follow to as format: %s", err)
	}

	outboxIRI, err := url.Parse(originAccount.OutboxURI)
	if err != nil {
		return fmt.Errorf("federateFollow: error parsing outboxURI %s: %s", originAccount.OutboxURI, err)
	}

	_, err = p.federator.FederatingActor().Send(ctx, outboxIRI, asFollow)
	return err
}

func (p *processor) federateUnfollow(ctx context.Context, follow *gtsmodel.Follow, originAccount *gtsmodel.Account, targetAccount *gtsmodel.Account) error {
	// if both accounts are local there's nothing to do here
	if originAccount.Domain == "" && targetAccount.Domain == "" {
		return nil
	}

	// recreate the follow
	asFollow, err := p.tc.FollowToAS(ctx, follow, originAccount, targetAccount)
	if err != nil {
		return fmt.Errorf("federateUnfollow: error converting follow to as format: %s", err)
	}

	targetAccountURI, err := url.Parse(targetAccount.URI)
	if err != nil {
		return fmt.Errorf("error parsing uri %s: %s", targetAccount.URI, err)
	}

	// create an Undo and set the appropriate actor on it
	undo := streams.NewActivityStreamsUndo()
	undo.SetActivityStreamsActor(asFollow.GetActivityStreamsActor())

	// Set the recreated follow as the 'object' property.
	undoObject := streams.NewActivityStreamsObjectProperty()
	undoObject.AppendActivityStreamsFollow(asFollow)
	undo.SetActivityStreamsObject(undoObject)

	// Set the To of the undo as the target of the recreated follow
	undoTo := streams.NewActivityStreamsToProperty()
	undoTo.AppendIRI(targetAccountURI)
	undo.SetActivityStreamsTo(undoTo)

	outboxIRI, err := url.Parse(originAccount.OutboxURI)
	if err != nil {
		return fmt.Errorf("federateUnfollow: error parsing outboxURI %s: %s", originAccount.OutboxURI, err)
	}

	// send off the Undo
	_, err = p.federator.FederatingActor().Send(ctx, outboxIRI, undo)
	return err
}

func (p *processor) federateUnfave(ctx context.Context, fave *gtsmodel.StatusFave, originAccount *gtsmodel.Account, targetAccount *gtsmodel.Account) error {
	// if both accounts are local there's nothing to do here
	if originAccount.Domain == "" && targetAccount.Domain == "" {
		return nil
	}

	// create the AS fave
	asFave, err := p.tc.FaveToAS(ctx, fave)
	if err != nil {
		return fmt.Errorf("federateFave: error converting fave to as format: %s", err)
	}

	targetAccountURI, err := url.Parse(targetAccount.URI)
	if err != nil {
		return fmt.Errorf("error parsing uri %s: %s", targetAccount.URI, err)
	}

	// create an Undo and set the appropriate actor on it
	undo := streams.NewActivityStreamsUndo()
	undo.SetActivityStreamsActor(asFave.GetActivityStreamsActor())

	// Set the fave as the 'object' property.
	undoObject := streams.NewActivityStreamsObjectProperty()
	undoObject.AppendActivityStreamsLike(asFave)
	undo.SetActivityStreamsObject(undoObject)

	// Set the To of the undo as the target of the fave
	undoTo := streams.NewActivityStreamsToProperty()
	undoTo.AppendIRI(targetAccountURI)
	undo.SetActivityStreamsTo(undoTo)

	outboxIRI, err := url.Parse(originAccount.OutboxURI)
	if err != nil {
		return fmt.Errorf("federateFave: error parsing outboxURI %s: %s", originAccount.OutboxURI, err)
	}
	_, err = p.federator.FederatingActor().Send(ctx, outboxIRI, undo)
	return err
}

func (p *processor) federateUnannounce(ctx context.Context, boost *gtsmodel.Status, originAccount *gtsmodel.Account, targetAccount *gtsmodel.Account) error {
	if originAccount.Domain != "" {
		// nothing to do here
		return nil
	}

	asAnnounce, err := p.tc.BoostToAS(ctx, boost, originAccount, targetAccount)
	if err != nil {
		return fmt.Errorf("federateUnannounce: error converting status to announce: %s", err)
	}

	// create an Undo and set the appropriate actor on it
	undo := streams.NewActivityStreamsUndo()
	undo.SetActivityStreamsActor(asAnnounce.GetActivityStreamsActor())

	// Set the boost as the 'object' property.
	undoObject := streams.NewActivityStreamsObjectProperty()
	undoObject.AppendActivityStreamsAnnounce(asAnnounce)
	undo.SetActivityStreamsObject(undoObject)

	// set the to
	undo.SetActivityStreamsTo(asAnnounce.GetActivityStreamsTo())

	// set the cc
	undo.SetActivityStreamsCc(asAnnounce.GetActivityStreamsCc())

	outboxIRI, err := url.Parse(originAccount.OutboxURI)
	if err != nil {
		return fmt.Errorf("federateUnannounce: error parsing outboxURI %s: %s", originAccount.OutboxURI, err)
	}

	_, err = p.federator.FederatingActor().Send(ctx, outboxIRI, undo)
	return err
}

func (p *processor) federateAcceptFollowRequest(ctx context.Context, follow *gtsmodel.Follow) error {
	if follow.Account == nil {
		a, err := p.db.GetAccountByID(ctx, follow.AccountID)
		if err != nil {
			return err
		}
		follow.Account = a
	}
	originAccount := follow.Account

	if follow.TargetAccount == nil {
		a, err := p.db.GetAccountByID(ctx, follow.TargetAccountID)
		if err != nil {
			return err
		}
		follow.TargetAccount = a
	}
	targetAccount := follow.TargetAccount

	// if target account isn't from our domain we shouldn't do anything
	if targetAccount.Domain != "" {
		return nil
	}

	// if both accounts are local there's nothing to do here
	if originAccount.Domain == "" && targetAccount.Domain == "" {
		return nil
	}

	// recreate the AS follow
	asFollow, err := p.tc.FollowToAS(ctx, follow, originAccount, targetAccount)
	if err != nil {
		return fmt.Errorf("federateUnfollow: error converting follow to as format: %s", err)
	}

	acceptingAccountURI, err := url.Parse(targetAccount.URI)
	if err != nil {
		return fmt.Errorf("error parsing uri %s: %s", targetAccount.URI, err)
	}

	requestingAccountURI, err := url.Parse(originAccount.URI)
	if err != nil {
		return fmt.Errorf("error parsing uri %s: %s", targetAccount.URI, err)
	}

	// create an Accept
	accept := streams.NewActivityStreamsAccept()

	// set the accepting actor on it
	acceptActorProp := streams.NewActivityStreamsActorProperty()
	acceptActorProp.AppendIRI(acceptingAccountURI)
	accept.SetActivityStreamsActor(acceptActorProp)

	// Set the recreated follow as the 'object' property.
	acceptObject := streams.NewActivityStreamsObjectProperty()
	acceptObject.AppendActivityStreamsFollow(asFollow)
	accept.SetActivityStreamsObject(acceptObject)

	// Set the To of the accept as the originator of the follow
	acceptTo := streams.NewActivityStreamsToProperty()
	acceptTo.AppendIRI(requestingAccountURI)
	accept.SetActivityStreamsTo(acceptTo)

	outboxIRI, err := url.Parse(targetAccount.OutboxURI)
	if err != nil {
		return fmt.Errorf("federateAcceptFollowRequest: error parsing outboxURI %s: %s", originAccount.OutboxURI, err)
	}

	// send off the accept using the accepter's outbox
	_, err = p.federator.FederatingActor().Send(ctx, outboxIRI, accept)
	return err
}

func (p *processor) federateRejectFollowRequest(ctx context.Context, followRequest *gtsmodel.FollowRequest) error {
	if followRequest.Account == nil {
		a, err := p.db.GetAccountByID(ctx, followRequest.AccountID)
		if err != nil {
			return err
		}
		followRequest.Account = a
	}
	originAccount := followRequest.Account

	if followRequest.TargetAccount == nil {
		a, err := p.db.GetAccountByID(ctx, followRequest.TargetAccountID)
		if err != nil {
			return err
		}
		followRequest.TargetAccount = a
	}
	targetAccount := followRequest.TargetAccount

	// if target account isn't from our domain we shouldn't do anything
	if targetAccount.Domain != "" {
		return nil
	}

	// if both accounts are local there's nothing to do here
	if originAccount.Domain == "" && targetAccount.Domain == "" {
		return nil
	}

	// recreate the AS follow
	follow := p.tc.FollowRequestToFollow(ctx, followRequest)
	asFollow, err := p.tc.FollowToAS(ctx, follow, originAccount, targetAccount)
	if err != nil {
		return fmt.Errorf("federateUnfollow: error converting follow to as format: %s", err)
	}

	rejectingAccountURI, err := url.Parse(targetAccount.URI)
	if err != nil {
		return fmt.Errorf("error parsing uri %s: %s", targetAccount.URI, err)
	}

	requestingAccountURI, err := url.Parse(originAccount.URI)
	if err != nil {
		return fmt.Errorf("error parsing uri %s: %s", targetAccount.URI, err)
	}

	// create a Reject
	reject := streams.NewActivityStreamsReject()

	// set the rejecting actor on it
	acceptActorProp := streams.NewActivityStreamsActorProperty()
	acceptActorProp.AppendIRI(rejectingAccountURI)
	reject.SetActivityStreamsActor(acceptActorProp)

	// Set the recreated follow as the 'object' property.
	acceptObject := streams.NewActivityStreamsObjectProperty()
	acceptObject.AppendActivityStreamsFollow(asFollow)
	reject.SetActivityStreamsObject(acceptObject)

	// Set the To of the reject as the originator of the follow
	acceptTo := streams.NewActivityStreamsToProperty()
	acceptTo.AppendIRI(requestingAccountURI)
	reject.SetActivityStreamsTo(acceptTo)

	outboxIRI, err := url.Parse(targetAccount.OutboxURI)
	if err != nil {
		return fmt.Errorf("federateRejectFollowRequest: error parsing outboxURI %s: %s", originAccount.OutboxURI, err)
	}

	// send off the reject using the rejecting account's outbox
	_, err = p.federator.FederatingActor().Send(ctx, outboxIRI, reject)
	return err
}

func (p *processor) federateFave(ctx context.Context, fave *gtsmodel.StatusFave, originAccount *gtsmodel.Account, targetAccount *gtsmodel.Account) error {
	// if both accounts are local there's nothing to do here
	if originAccount.Domain == "" && targetAccount.Domain == "" {
		return nil
	}

	// create the AS fave
	asFave, err := p.tc.FaveToAS(ctx, fave)
	if err != nil {
		return fmt.Errorf("federateFave: error converting fave to as format: %s", err)
	}

	outboxIRI, err := url.Parse(originAccount.OutboxURI)
	if err != nil {
		return fmt.Errorf("federateFave: error parsing outboxURI %s: %s", originAccount.OutboxURI, err)
	}
	_, err = p.federator.FederatingActor().Send(ctx, outboxIRI, asFave)
	return err
}

func (p *processor) federateAnnounce(ctx context.Context, boostWrapperStatus *gtsmodel.Status, boostingAccount *gtsmodel.Account, boostedAccount *gtsmodel.Account) error {
	announce, err := p.tc.BoostToAS(ctx, boostWrapperStatus, boostingAccount, boostedAccount)
	if err != nil {
		return fmt.Errorf("federateAnnounce: error converting status to announce: %s", err)
	}

	outboxIRI, err := url.Parse(boostingAccount.OutboxURI)
	if err != nil {
		return fmt.Errorf("federateAnnounce: error parsing outboxURI %s: %s", boostingAccount.OutboxURI, err)
	}

	_, err = p.federator.FederatingActor().Send(ctx, outboxIRI, announce)
	return err
}

func (p *processor) federateAccountUpdate(ctx context.Context, updatedAccount *gtsmodel.Account, originAccount *gtsmodel.Account) error {
	person, err := p.tc.AccountToAS(ctx, updatedAccount)
	if err != nil {
		return fmt.Errorf("federateAccountUpdate: error converting account to person: %s", err)
	}

	update, err := p.tc.WrapPersonInUpdate(person, originAccount)
	if err != nil {
		return fmt.Errorf("federateAccountUpdate: error wrapping person in update: %s", err)
	}

	outboxIRI, err := url.Parse(originAccount.OutboxURI)
	if err != nil {
		return fmt.Errorf("federateAnnounce: error parsing outboxURI %s: %s", originAccount.OutboxURI, err)
	}

	_, err = p.federator.FederatingActor().Send(ctx, outboxIRI, update)
	return err
}

func (p *processor) federateBlock(ctx context.Context, block *gtsmodel.Block) error {
	if block.Account == nil {
		blockAccount, err := p.db.GetAccountByID(ctx, block.AccountID)
		if err != nil {
			return fmt.Errorf("federateBlock: error getting block account from database: %s", err)
		}
		block.Account = blockAccount
	}

	if block.TargetAccount == nil {
		blockTargetAccount, err := p.db.GetAccountByID(ctx, block.TargetAccountID)
		if err != nil {
			return fmt.Errorf("federateBlock: error getting block target account from database: %s", err)
		}
		block.TargetAccount = blockTargetAccount
	}

	// if both accounts are local there's nothing to do here
	if block.Account.Domain == "" && block.TargetAccount.Domain == "" {
		return nil
	}

	asBlock, err := p.tc.BlockToAS(ctx, block)
	if err != nil {
		return fmt.Errorf("federateBlock: error converting block to AS format: %s", err)
	}

	outboxIRI, err := url.Parse(block.Account.OutboxURI)
	if err != nil {
		return fmt.Errorf("federateBlock: error parsing outboxURI %s: %s", block.Account.OutboxURI, err)
	}

	_, err = p.federator.FederatingActor().Send(ctx, outboxIRI, asBlock)
	return err
}

func (p *processor) federateUnblock(ctx context.Context, block *gtsmodel.Block) error {
	if block.Account == nil {
		blockAccount, err := p.db.GetAccountByID(ctx, block.AccountID)
		if err != nil {
			return fmt.Errorf("federateUnblock: error getting block account from database: %s", err)
		}
		block.Account = blockAccount
	}

	if block.TargetAccount == nil {
		blockTargetAccount, err := p.db.GetAccountByID(ctx, block.TargetAccountID)
		if err != nil {
			return fmt.Errorf("federateUnblock: error getting block target account from database: %s", err)
		}
		block.TargetAccount = blockTargetAccount
	}

	// if both accounts are local there's nothing to do here
	if block.Account.Domain == "" && block.TargetAccount.Domain == "" {
		return nil
	}

	asBlock, err := p.tc.BlockToAS(ctx, block)
	if err != nil {
		return fmt.Errorf("federateUnblock: error converting block to AS format: %s", err)
	}

	targetAccountURI, err := url.Parse(block.TargetAccount.URI)
	if err != nil {
		return fmt.Errorf("federateUnblock: error parsing uri %s: %s", block.TargetAccount.URI, err)
	}

	// create an Undo and set the appropriate actor on it
	undo := streams.NewActivityStreamsUndo()
	undo.SetActivityStreamsActor(asBlock.GetActivityStreamsActor())

	// Set the block as the 'object' property.
	undoObject := streams.NewActivityStreamsObjectProperty()
	undoObject.AppendActivityStreamsBlock(asBlock)
	undo.SetActivityStreamsObject(undoObject)

	// Set the To of the undo as the target of the block
	undoTo := streams.NewActivityStreamsToProperty()
	undoTo.AppendIRI(targetAccountURI)
	undo.SetActivityStreamsTo(undoTo)

	outboxIRI, err := url.Parse(block.Account.OutboxURI)
	if err != nil {
		return fmt.Errorf("federateUnblock: error parsing outboxURI %s: %s", block.Account.OutboxURI, err)
	}
	_, err = p.federator.FederatingActor().Send(ctx, outboxIRI, undo)
	return err
}

func (p *processor) federateReport(ctx context.Context, report *gtsmodel.Report) error {
	if report.TargetAccount == nil {
		reportTargetAccount, err := p.db.GetAccountByID(ctx, report.TargetAccountID)
		if err != nil {
			return fmt.Errorf("federateReport: error getting report target account from database: %w", err)
		}
		report.TargetAccount = reportTargetAccount
	}

	if len(report.StatusIDs) > 0 && len(report.Statuses) == 0 {
		statuses, err := p.db.GetStatuses(ctx, report.StatusIDs)
		if err != nil {
			return fmt.Errorf("federateReport: error getting report statuses from database: %w", err)
		}
		report.Statuses = statuses
	}

	flag, err := p.tc.ReportToASFlag(ctx, report)
	if err != nil {
		return fmt.Errorf("federateReport: error converting report to AS flag: %w", err)
	}

	// add bto so that our federating actor knows where to
	// send the Flag; it'll still use a shared inbox if possible
	reportTargetURI, err := url.Parse(report.TargetAccount.URI)
	if err != nil {
		return fmt.Errorf("federateReport: error parsing outboxURI %s: %w", report.TargetAccount.URI, err)
	}
	bTo := streams.NewActivityStreamsBtoProperty()
	bTo.AppendIRI(reportTargetURI)
	flag.SetActivityStreamsBto(bTo)

	// deliver the flag using the outbox of the
	// instance account to anonymize the report
	instanceAccount, err := p.db.GetInstanceAccount(ctx, "")
	if err != nil {
		return fmt.Errorf("federateReport: error getting instance account: %w", err)
	}

	outboxIRI, err := url.Parse(instanceAccount.OutboxURI)
	if err != nil {
		return fmt.Errorf("federateReport: error parsing outboxURI %s: %w", instanceAccount.OutboxURI, err)
	}

	_, err = p.federator.FederatingActor().Send(ctx, outboxIRI, flag)
	return err
}