-
Notifications
You must be signed in to change notification settings - Fork 10
/
following_activityPub.go
79 lines (61 loc) · 2.52 KB
/
following_activityPub.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
package service
import (
"github.com/EmissarySocial/emissary/model"
"github.com/benpate/derp"
"github.com/benpate/digit"
"github.com/benpate/hannibal/pub"
"github.com/benpate/hannibal/streams"
)
// connect_ActivityPub attempts to connect to a remote user using ActivityPub.
// It returns (TRUE, nil) if successful.
// If there was an error connecting to the remote server, then it returns (FALSE, error)
// If the remote server does not support ActivityPub, then it returns (FALSE, nil)
func (service *Following) connect_ActivityPub(following *model.Following) (bool, error) {
const location = "service.Following.connect_ActivityPub"
// Search for an ActivityPub link for this resource
remoteProfile := following.Links.Find(
digit.NewLink(digit.RelationTypeSelf, model.MimeTypeActivityPub, ""),
)
// if no ActivityPub link, then exit.
if remoteProfile.IsEmpty() {
return false, nil
}
// Update the Following record with the remote URL
following.ProfileURL = remoteProfile.Href
if err := service.SetStatus(following, model.FollowingStatusLoading, ""); err != nil {
return false, derp.Wrap(err, location, "Error saving following", following)
}
// Try to get the Actor (with encryption keys)
actor, err := service.userService.ActivityPubActor(following.UserID)
if err != nil {
return false, derp.Wrap(err, location, "Error getting ActivityPub actor", following.UserID)
}
// Try to send the ActivityPub follow request
if err := pub.SendFollow(actor, service.ActivityPubID(following), remoteProfile.Href); err != nil {
return false, derp.Wrap(err, location, "Error sending follow request", following)
}
// Success!
return true, nil
}
func (service *Following) disconnect_ActivityPub(following *model.Following) error {
const location = "service.Following.disconnect_ActivityPub"
// Search for an ActivityPub link for this resource
remoteProfile := following.Links.Find(
digit.NewLink(digit.RelationTypeSelf, model.MimeTypeActivityPub, ""),
)
// if no ActivityPub link, then exit.
if remoteProfile.IsEmpty() {
return nil
}
// Try to get the Actor (with encryption keys)
actor, err := service.userService.ActivityPubActor(following.UserID)
if err != nil {
return derp.Wrap(err, location, "Error getting ActivityPub actor", following.UserID)
}
// Try to send the ActivityPub Undo request
activity := streams.NewDocument(service.AsJSONLD(following), nil)
if err := pub.SendUndo(actor, activity, following.URL); err != nil {
return derp.Wrap(err, location, "Error sending follow request", following)
}
return nil
}