-
Notifications
You must be signed in to change notification settings - Fork 13
/
receive_like_any.go
71 lines (53 loc) · 2.43 KB
/
receive_like_any.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
package activitypub_user
import (
"github.com/EmissarySocial/emissary/handler/activitypub"
"github.com/EmissarySocial/emissary/model"
"github.com/benpate/derp"
"github.com/benpate/hannibal/streams"
"github.com/benpate/hannibal/vocab"
)
func init() {
inboxRouter.Add(vocab.ActivityTypeAnnounce, vocab.Any, receiveLikeOrAnnounce)
inboxRouter.Add(vocab.ActivityTypeLike, vocab.Any, receiveLikeOrAnnounce)
inboxRouter.Add(vocab.ActivityTypeDislike, vocab.Any, receiveLikeOrAnnounce)
}
// receiveLikeOrAnnounce handles all Like Dislike activities
func receiveLikeOrAnnounce(context Context, activity streams.Document) error {
const location = "handler.activitypub_user.receiveLikeOrAnnounce"
// Add then Shared/Liked Object into the ActivityStream cache
if err := inboxRouter.Handle(context, activity.Object().LoadLink()); err != nil {
return derp.Wrap(err, location, "Error processing activity Object", activity.Object().ID())
}
// RULE: If the Activity does not have an ID, then make a new "fake" one.
if activity.ID() == "" {
activity.SetProperty(vocab.PropertyID, activitypub.FakeActivityID(activity))
}
// Add the Announce/Like/Dislike into the ActivityStream cache
context.factory.ActivityStream().Put(activity)
// Add the activity into the User's Inbox
if err := saveMessage(context, activity, activity.Actor().ID(), getOriginType(activity.Type())); err != nil {
return derp.Wrap(err, location, "Error saving message", context.user.UserID, activity.Value())
}
// Success.
return nil
}
// saveMessage saves a message into the User's inbox
func saveMessage(context Context, activity streams.Document, actorID string, originType string) error {
const location = "handler.activitypub_user.saveMessage"
// Verify that this message comes from a valid "Following" object.
followingService := context.factory.Following()
following := model.NewFollowing()
// If the "Following" record cannot be found, then do not add a message
if err := followingService.LoadByURL(context.user.UserID, actorID, &following); err != nil {
if derp.NotFound(err) {
return nil
}
return derp.Wrap(err, location, "Error loading Following record", context.user.UserID, actorID)
}
// Try to save the message to the database (with de-duplication)
if err := followingService.SaveMessage(&following, activity, originType); err != nil {
return derp.Wrap(err, location, "Error saving message", context.user.UserID, activity.Value())
}
// Success.
return nil
}