3 fn populate_object_props(
4 props: &mut ObjectProperties,
7 ) -> Result<(), Error> {
9 .set_context_xsd_any_uri(context())?
10 // TODO: the activity needs a seperate id from the object
12 // TODO: should to/cc go on the Create, or on the Post? or on both?
13 // TODO: handle privacy on the receiving side (at least ignore anything thats not public)
14 .set_to_xsd_any_uri(public())?
15 .set_cc_xsd_any_uri(addressed_to)?;
19 /// Send an activity to a list of recipients, using the correct headers etc.
25 ) -> Result<(), Error>
29 let json = serde_json::to_string(&activity)?;
30 debug!("Sending activitypub activity {} to {:?}", json, to);
31 // TODO it needs to expand, the to field needs to expand and dedup the followers urls
32 // The inbox is determined by first retrieving the target actor's JSON-LD representation and then looking up the inbox property. If a recipient is a Collection or OrderedCollection, then the server MUST dereference the collection (with the user's credentials) and discover inboxes for each item in the collection. Servers MUST limit the number of layers of indirections through collections which will be performed, which MAY be one.
34 let to_url = Url::parse(&t)?;
35 if !is_apub_id_valid(&to_url) {
36 debug!("Not sending activity to {} (invalid or blacklisted)", t);
39 let request = Request::post(t).header("Host", to_url.domain().unwrap());
40 let signature = sign(&request, private_key, sender_id)?;
42 .header("Signature", signature)
43 .header("Content-Type", "application/json")
44 .body(json.to_owned())?
46 debug!("Result for activity send: {:?}", res);
51 /// For a given community, returns the inboxes of all followers.
52 fn get_follower_inboxes(conn: &PgConnection, community: &Community) -> Result<Vec<String>, Error> {
54 CommunityFollowerView::for_community(conn, community.id)?
56 .filter(|c| !c.user_local)
57 .map(|c| format!("{}/inbox", c.user_actor_id.to_owned()))
62 /// Send out information about a newly created post, to the followers of the community.
63 pub fn post_create(post: &Post, creator: &User_, conn: &PgConnection) -> Result<(), Error> {
64 let page = post.to_apub(conn)?;
65 let community = Community::read(conn, post.community_id)?;
66 let mut create = Create::new();
67 populate_object_props(
68 &mut create.object_props,
69 &community.get_followers_url(),
74 .set_actor_xsd_any_uri(creator.actor_id.to_owned())?
75 .set_object_base_box(page)?;
78 &creator.private_key.as_ref().unwrap(),
80 get_follower_inboxes(conn, &community)?,
85 /// Send out information about an edited post, to the followers of the community.
86 pub fn post_update(post: &Post, creator: &User_, conn: &PgConnection) -> Result<(), Error> {
87 let page = post.to_apub(conn)?;
88 let community = Community::read(conn, post.community_id)?;
89 let mut update = Update::new();
90 populate_object_props(
91 &mut update.object_props,
92 &community.get_followers_url(),
97 .set_actor_xsd_any_uri(creator.actor_id.to_owned())?
98 .set_object_base_box(page)?;
101 &creator.private_key.as_ref().unwrap(),
103 get_follower_inboxes(conn, &community)?,
108 /// As a given local user, send out a follow request to a remote community.
109 pub fn follow_community(
110 community: &Community,
112 _conn: &PgConnection,
113 ) -> Result<(), Error> {
114 let mut follow = Follow::new();
117 .set_context_xsd_any_uri(context())?
118 // TODO: needs proper id
119 .set_id(user.actor_id.clone())?;
122 .set_actor_xsd_any_uri(user.actor_id.clone())?
123 .set_object_xsd_any_uri(community.actor_id.clone())?;
124 // TODO this is incorrect, the to field should not be the inbox, but the followers url
125 let to = format!("{}/inbox", community.actor_id);
128 &user.private_key.as_ref().unwrap(),
135 /// As a local community, accept the follow request from a remote user.
136 pub fn accept_follow(follow: &Follow, conn: &PgConnection) -> Result<(), Error> {
137 let community_uri = follow
139 .get_object_xsd_any_uri()
142 let actor_uri = follow
144 .get_actor_xsd_any_uri()
147 let community = Community::read_from_actor_id(conn, &community_uri)?;
148 let mut accept = Accept::new();
151 .set_context_xsd_any_uri(context())?
152 // TODO: needs proper id
156 .get_actor_xsd_any_uri()
162 .set_actor_xsd_any_uri(community.actor_id.clone())?
163 .set_object_base_box(BaseBox::from_concrete(follow.clone())?)?;
164 // TODO this is incorrect, the to field should not be the inbox, but the followers url
165 let to = format!("{}/inbox", actor_uri);
168 &community.private_key.unwrap(),