summaryrefslogtreecommitdiff
path: root/internal/processing/fromfederator.go
blob: feb22b2ba54648707e2ce0e74cc8ea79f93f9f0e (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
/*
   GoToSocial
   Copyright (C) 2021 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"

	"github.com/sirupsen/logrus"
	"github.com/superseriousbusiness/gotosocial/internal/ap"
	"github.com/superseriousbusiness/gotosocial/internal/db"
	"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
	"github.com/superseriousbusiness/gotosocial/internal/id"
	"github.com/superseriousbusiness/gotosocial/internal/messages"
)

func (p *processor) ProcessFromFederator(ctx context.Context, federatorMsg messages.FromFederator) error {
	l := p.log.WithFields(logrus.Fields{
		"func":         "processFromFederator",
		"federatorMsg": fmt.Sprintf("%+v", federatorMsg),
	})

	l.Trace("entering function PROCESS FROM FEDERATOR")

	switch federatorMsg.APActivityType {
	case ap.ActivityCreate:
		// CREATE
		switch federatorMsg.APObjectType {
		case ap.ObjectNote:
			// CREATE A STATUS
			incomingStatus, ok := federatorMsg.GTSModel.(*gtsmodel.Status)
			if !ok {
				return errors.New("note was not parseable as *gtsmodel.Status")
			}

			status, err := p.federator.EnrichRemoteStatus(ctx, federatorMsg.ReceivingAccount.Username, incomingStatus, true)
			if err != nil {
				return err
			}

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

			if err := p.notifyStatus(ctx, status); err != nil {
				return err
			}
		case ap.ObjectProfile:
			// CREATE AN ACCOUNT
			// nothing to do here
		case ap.ActivityLike:
			// CREATE A FAVE
			incomingFave, ok := federatorMsg.GTSModel.(*gtsmodel.StatusFave)
			if !ok {
				return errors.New("like was not parseable as *gtsmodel.StatusFave")
			}

			if err := p.notifyFave(ctx, incomingFave); err != nil {
				return err
			}
		case ap.ActivityFollow:
			// CREATE A FOLLOW REQUEST
			incomingFollowRequest, ok := federatorMsg.GTSModel.(*gtsmodel.FollowRequest)
			if !ok {
				return errors.New("incomingFollowRequest was not parseable as *gtsmodel.FollowRequest")
			}

			if err := p.notifyFollowRequest(ctx, incomingFollowRequest, federatorMsg.ReceivingAccount); err != nil {
				return err
			}
		case ap.ActivityAnnounce:
			// CREATE AN ANNOUNCE
			incomingAnnounce, ok := federatorMsg.GTSModel.(*gtsmodel.Status)
			if !ok {
				return errors.New("announce was not parseable as *gtsmodel.Status")
			}

			if err := p.federator.DereferenceAnnounce(ctx, incomingAnnounce, federatorMsg.ReceivingAccount.Username); err != nil {
				return fmt.Errorf("error dereferencing announce from federator: %s", err)
			}

			incomingAnnounceID, err := id.NewULIDFromTime(incomingAnnounce.CreatedAt)
			if err != nil {
				return err
			}
			incomingAnnounce.ID = incomingAnnounceID

			if err := p.db.PutStatus(ctx, incomingAnnounce); err != nil {
				return fmt.Errorf("error adding dereferenced announce to the db: %s", err)
			}

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

			if err := p.notifyAnnounce(ctx, incomingAnnounce); err != nil {
				return err
			}
		case ap.ActivityBlock:
			// CREATE A BLOCK
			block, ok := federatorMsg.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.timelineManager.WipeStatusesFromAccountID(ctx, block.AccountID, block.TargetAccountID); err != nil {
				return err
			}
			if err := p.timelineManager.WipeStatusesFromAccountID(ctx, block.TargetAccountID, block.AccountID); err != nil {
				return err
			}
			// TODO: same with notifications
			// TODO: same with bookmarks
		}
	case ap.ActivityUpdate:
		// UPDATE
		switch federatorMsg.APObjectType {
		case ap.ObjectProfile:
			// UPDATE AN ACCOUNT
			incomingAccount, ok := federatorMsg.GTSModel.(*gtsmodel.Account)
			if !ok {
				return errors.New("profile was not parseable as *gtsmodel.Account")
			}

			if _, err := p.federator.EnrichRemoteAccount(ctx, federatorMsg.ReceivingAccount.Username, incomingAccount); err != nil {
				return fmt.Errorf("error enriching updated account from federator: %s", err)
			}
		}
	case ap.ActivityDelete:
		// DELETE
		switch federatorMsg.APObjectType {
		case ap.ObjectNote:
			// DELETE A STATUS
			// TODO: handle side effects of status deletion here:
			// 1. delete all media associated with status
			// 2. delete boosts of status
			// 3. etc etc etc
			statusToDelete, ok := federatorMsg.GTSModel.(*gtsmodel.Status)
			if !ok {
				return errors.New("note was not parseable as *gtsmodel.Status")
			}

			// delete all attachments for this status
			for _, a := range statusToDelete.AttachmentIDs {
				if err := p.mediaProcessor.Delete(ctx, a); err != nil {
					return err
				}
			}

			// delete all mentions for this status
			for _, m := range statusToDelete.MentionIDs {
				if err := p.db.DeleteByID(ctx, m, &gtsmodel.Mention{}); err != nil {
					return err
				}
			}

			// delete all notifications for this status
			if err := p.db.DeleteWhere(ctx, []db.Where{{Key: "status_id", Value: statusToDelete.ID}}, &[]*gtsmodel.Notification{}); err != nil {
				return err
			}

			// remove this status from any and all timelines
			return p.deleteStatusFromTimelines(ctx, statusToDelete)
		case ap.ObjectProfile:
			// DELETE A PROFILE/ACCOUNT
			// TODO: handle side effects of account deletion here: delete all objects, statuses, media etc associated with account
		}
	case ap.ActivityAccept:
		// ACCEPT
		switch federatorMsg.APObjectType {
		case ap.ActivityFollow:
			// ACCEPT A FOLLOW
			follow, ok := federatorMsg.GTSModel.(*gtsmodel.Follow)
			if !ok {
				return errors.New("follow was not parseable as *gtsmodel.Follow")
			}

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

	return nil
}