-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreactions.go
331 lines (303 loc) · 10.2 KB
/
reactions.go
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
package processing
import (
"strings"
vocab "github.com/go-ap/activitypub"
"github.com/go-ap/errors"
)
// ReactionsActivity processes matching activities
//
// https://www.w3.org/TR/activitystreams-vocabulary/#h-motivations-respond
//
// The Reactions use case primarily deals with reactions to content.
// This can include activities such as liking or disliking content, ignoring updates,
// flagging content as being inappropriate, accepting or rejecting objects, etc.
func ReactionsActivity(p P, act *vocab.Activity, receivedIn vocab.IRI) (*vocab.Activity, error) {
var err error
if act.Object != nil {
switch act.Type {
case vocab.DislikeType:
fallthrough
case vocab.LikeType:
act, err = AppreciationActivity(p, act)
case vocab.RejectType:
fallthrough
case vocab.TentativeRejectType:
// I think nothing happens here.
act, err = RejectActivity(p.s, act)
case vocab.TentativeAcceptType:
fallthrough
case vocab.AcceptType:
act, err = AcceptActivity(p, act, receivedIn)
case vocab.BlockType:
act, err = BlockActivity(p, act, receivedIn)
case vocab.FlagType:
act, err = FlagActivity(p.s, act)
case vocab.IgnoreType:
act, err = IgnoreActivity(p, act)
}
}
return act, err
}
type multi []error
func (m multi) Error() string {
b := strings.Builder{}
for _, err := range m {
b.WriteString(err.Error())
}
return b.String()
}
func (m multi) As(e any) bool {
if len(m) == 0 {
return false
}
return errors.As(m[0], e)
}
// AppreciationActivity
// The Like(and Dislike) activity indicates the actor likes the object.
// The side effect of receiving this in an outbox is that the server SHOULD add the object to the actor's liked Collection.
func AppreciationActivity(p P, act *vocab.Activity) (*vocab.Activity, error) {
if vocab.IsNil(act.Object) {
return act, errors.NotValidf("Missing object for %s Activity", act.Type)
}
if vocab.IsNil(act.Actor) {
return act, errors.NotValidf("Missing actor for %s Activity", act.Type)
}
good := vocab.ActivityVocabularyTypes{vocab.LikeType, vocab.DislikeType}
if !good.Contains(act.Type) {
return act, errors.NotValidf("Activity has wrong type %s, expected %v", act.Type, good)
}
saveToCollections := func(actors, objects vocab.ItemCollection) error {
errs := make(multi, 0)
colToAdd := make(map[vocab.IRI][]vocab.IRI)
for _, object := range objects {
for _, actor := range actors {
liked := vocab.Liked.IRI(actor)
colToAdd[liked] = append(colToAdd[liked], object.GetLink())
}
likes := vocab.Likes.IRI(object)
colToAdd[likes] = append(colToAdd[likes], act.GetLink())
}
for col, iris := range colToAdd {
for _, iri := range iris {
if err := p.AddItemToCollection(col, iri); err != nil {
errs = append(errs, errors.Annotatef(err, "Unable to save %s to collection %s", iris, col))
}
}
}
if len(errs) > 0 {
return errs
}
return nil
}
var actors, objects vocab.ItemCollection
if vocab.IsItemCollection(act.Actor) {
vocab.OnItemCollection(act.Actor, func(c *vocab.ItemCollection) error {
actors = *c
return nil
})
} else {
actors = make(vocab.ItemCollection, 1)
actors[0] = act.Actor
}
if vocab.IsItemCollection(act.Object) {
vocab.OnItemCollection(act.Object, func(c *vocab.ItemCollection) error {
objects = *c
return nil
})
} else {
objects = make(vocab.ItemCollection, 1)
objects[0] = act.Object
}
// NOTE(marius): we're only saving to the Liked and Likes collections for Likes in order to conform to the spec.
if act.GetType() == vocab.LikeType {
// TODO(marius): do something sensible with these errors, they shouldn't stop execution,
// but they are still good to know
_ = saveToCollections(actors, objects)
}
return act, nil
}
func firstOrItem(it vocab.Item) vocab.Item {
if vocab.IsNil(it) {
return it
}
if it.IsCollection() {
vocab.OnCollectionIntf(it, func(col vocab.CollectionInterface) error {
it = col.Collection().First()
return nil
})
}
return it
}
// AcceptActivity
//
// In Inbox: https://www.w3.org/TR/activitypub/#follow-activity-inbox
//
// The side effect of receiving this in an inbox is determined by the type of the object received, and it is possible
// to accept types not described in this document (for example, an Offer).
//
// If the object of an Accept received to an inbox is a Follow activity previously sent by the receiver, the server
// SHOULD add the actor to the receiver's Following Collection.
func AcceptActivity(p P, act *vocab.Activity, receivedIn vocab.IRI) (*vocab.Activity, error) {
if vocab.IsNil(act.Object) {
return act, errors.NotValidf("Missing object for %s Activity", act.Type)
}
if vocab.IsNil(act.Actor) {
return act, errors.NotValidf("Missing actor for %s Activity", act.Type)
}
good := vocab.ActivityVocabularyTypes{vocab.AcceptType, vocab.TentativeAcceptType}
if !good.Contains(act.Type) {
return act, errors.NotValidf("Activity has wrong type %s, expected %v", act.Type, good)
}
if act.Object.IsLink() {
// dereference object activity
if actLoader, ok := p.s.(ReadStore); ok {
obj, err := actLoader.Load(act.Object.GetLink())
if err != nil {
return act, errors.NotValidf("Unable to dereference object: %s", act.Object.GetLink())
}
act.Object = firstOrItem(obj)
}
}
err := vocab.OnActivity(act.Object, func(objAct *vocab.Activity) error {
if objAct.GetType() != vocab.FollowType {
return nil
}
if !act.Actor.GetLink().Equals(objAct.Object.GetLink(), false) {
return errors.NotValidf(
"The %s activity has a different actor than the received %s's object: %s, expected %s",
act.Type,
objAct.Type,
act.Actor.GetLink(),
objAct.Object.GetLink(),
)
}
return finalizeFollowActivity(p, objAct)
})
return act, err
}
func finalizeFollowActivity(p P, a *vocab.Activity) error {
good := vocab.ActivityVocabularyTypes{vocab.FollowType}
if !good.Contains(a.Type) {
return errors.NotValidf("Object Activity has wrong type %s, expected %v", a.Type, good)
}
errs := make(multi, 0)
if err := p.AddItemToCollection(vocab.Followers.IRI(a.Object), a.Actor); err != nil {
errs = append(errs, err)
}
if err := p.AddItemToCollection(vocab.Following.IRI(a.Actor), a.Object); err != nil {
errs = append(errs, err)
}
if len(errs) > 0 {
return errs
}
return nil
}
func RejectActivity(l WriteStore, act *vocab.Activity) (*vocab.Activity, error) {
if vocab.IsNil(act.Object) {
return act, errors.NotValidf("Missing object for %s Activity", act.Type)
}
if vocab.IsNil(act.Actor) {
return act, errors.NotValidf("Missing actor for %s Activity", act.Type)
}
good := vocab.ActivityVocabularyTypes{vocab.RejectType, vocab.TentativeRejectType}
if !good.Contains(act.Type) {
return act, errors.NotValidf("Activity has wrong type %s, expected %v", act.Type, good)
}
errs := make(multi, 0)
if colSaver, ok := l.(CollectionStore); ok {
inbox := vocab.Inbox.IRI(act.Actor)
err := colSaver.RemoveFrom(inbox, act.Object.GetLink())
if err != nil {
errs = append(errs, err)
}
}
if len(errs) > 0 {
return act, errs
}
return act, nil
}
const BlockedCollection = vocab.CollectionPath("blocked")
// BlockActivity
//
// https://www.w3.org/TR/activitypub/#block-activity-outbox
//
// The Block activity is used to indicate that the posting actor does not want another actor
// (defined in the object property) to be able to interact with objects posted by the actor posting the Block activity.
// The server SHOULD prevent the blocked user from interacting with any object posted by the actor.
//
// Servers SHOULD NOT deliver Block Activities to their object.
func BlockActivity(p P, act *vocab.Activity, receivedIn vocab.IRI) (*vocab.Activity, error) {
if vocab.IsNil(act.Object) {
return act, errors.NotValidf("Missing object for %s Activity", act.Type)
}
if vocab.IsNil(act.Actor) {
return act, errors.NotValidf("Missing actor for %s Activity", act.Type)
}
if act.Type != vocab.BlockType {
return act, errors.NotValidf("Activity has wrong type %s, expected %s", act.Type, vocab.BlockType)
}
obIRI := act.Object.GetLink()
// Remove object from any recipients collections
act.To.Remove(obIRI)
act.CC.Remove(obIRI)
act.Bto.Remove(obIRI)
act.BCC.Remove(obIRI)
return act, p.AddItemToCollection(BlockedCollection.IRI(act.Actor), obIRI)
}
// FlagActivity
// There isn't any side effect to this activity except delivering it to the inboxes of its recipients.
// From the list of recipients we remove the Object itself if it represents an Actor being flagged,
// or its author if it's another type of object.
func FlagActivity(l WriteStore, act *vocab.Activity) (*vocab.Activity, error) {
if vocab.IsNil(act.Object) {
return act, errors.NotValidf("Missing object for %s Activity", act.Type)
}
if vocab.IsNil(act.Actor) {
return act, errors.NotValidf("Missing actor for %s Activity", act.Type)
}
if act.Type != vocab.FlagType {
return act, errors.NotValidf("Activity has wrong type %s, expected %s", act.Type, vocab.FlagType)
}
vocab.OnObject(act.Object, func(o *vocab.Object) error {
var toRemoveIRI vocab.IRI
if !vocab.ActorTypes.Contains(o.Type) {
if o.AttributedTo != nil {
// Remove object's author from any recipients collections
toRemoveIRI = o.AttributedTo.GetLink()
}
} else {
// Remove object from any recipients collections
toRemoveIRI = o.GetLink()
}
if toRemoveIRI.IsValid() {
act.To.Remove(toRemoveIRI)
act.CC.Remove(toRemoveIRI)
act.Bto.Remove(toRemoveIRI)
act.BCC.Remove(toRemoveIRI)
}
return nil
})
return act, nil
}
const IgnoredCollection = vocab.CollectionPath("ignored")
// IgnoreActivity
// This relies on custom behavior for the repository, which would allow for an ignored collection,
// where we save these
func IgnoreActivity(p P, act *vocab.Activity) (*vocab.Activity, error) {
if vocab.IsNil(act.Object) {
return act, errors.NotValidf("Missing object for %s Activity", act.Type)
}
if vocab.IsNil(act.Actor) {
return act, errors.NotValidf("Missing actor for %s Activity", act.Type)
}
if act.Type != vocab.IgnoreType {
return act, errors.NotValidf("Activity has wrong type %s, expected %s", act.Type, vocab.IgnoreType)
}
obIRI := act.Object.GetLink()
// Remove object from any recipients collections
act.To.Remove(obIRI)
act.CC.Remove(obIRI)
act.Bto.Remove(obIRI)
act.BCC.Remove(obIRI)
return act, p.AddItemToCollection(IgnoredCollection.IRI(act.Actor), obIRI)
}