-
Notifications
You must be signed in to change notification settings - Fork 12
/
receive_undo_follow.go
63 lines (48 loc) · 2 KB
/
receive_undo_follow.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
package activitypub_user
import (
"github.com/EmissarySocial/emissary/model"
"github.com/benpate/derp"
"github.com/benpate/hannibal/streams"
"github.com/benpate/hannibal/vocab"
)
func init() {
inboxRouter.Add(vocab.ActivityTypeUndo, vocab.ActivityTypeFollow, undoFollow)
inboxRouter.Add(vocab.ActivityTypeDelete, vocab.ActivityTypeFollow, undoFollow)
}
// undoFollow handles "Undo/Follow" and "Delete/Follow" activitites, which means
// that this code is called when a remote user unfollows an actor on this server.
func undoFollow(context Context, activity streams.Document) error {
const location = "handler.activitypub.activityPub_HandleRequest_Undo_Follow"
// Try to load the existing follower record
followerService := context.factory.Follower()
follower := model.NewFollower()
// Load the original follow
originalFollow, err := activity.Object().Load()
if err != nil {
if derp.NotFound(err) {
return nil // If there is no follower record, then there's nothing to delete.
}
// All other errors are bad, tho.
return derp.Wrap(err, location, "Error retrieving original follow request", activity.Value())
}
// Collect data from the original follow
actorURL := originalFollow.Actor().ID() // The "actor" of the original follow is our follower.actor.ProfileURL
userURL := originalFollow.Object().ID() // The "object" of the original follow is our local UserURL
userService := context.factory.User()
userID, err := userService.ParseProfileURL(userURL)
if err != nil {
return derp.Wrap(err, location, "Invalid User URL", userURL)
}
if err := followerService.LoadByActivityPubFollower(userID, actorURL, &follower); err != nil {
if derp.NotFound(err) {
return nil
}
return derp.Wrap(err, location, "Error locating follower", activity.Value(), userID, actorURL)
}
// Try to delete the existing follower record
if err := followerService.Delete(&follower, "Removed by remote client"); err != nil {
return derp.Wrap(err, location, "Error deleting follower", follower)
}
// Voila!
return nil
}