2 apub::{extensions::signatures::sign, is_apub_id_valid, ActorType},
3 db::{activity::insert_activity, community::Community, user::User_},
5 use activitystreams::{context, object::properties::ObjectProperties, public, Activity, Base};
6 use diesel::PgConnection;
7 use failure::{Error, _core::fmt::Debug};
13 pub fn populate_object_props(
14 props: &mut ObjectProperties,
15 addressed_ccs: Vec<String>,
17 ) -> Result<(), Error> {
19 .set_context_xsd_any_uri(context())?
20 // TODO: the activity needs a seperate id from the object
22 // TODO: should to/cc go on the Create, or on the Post? or on both?
23 // TODO: handle privacy on the receiving side (at least ignore anything thats not public)
24 .set_to_xsd_any_uri(public())?
25 .set_many_cc_xsd_any_uris(addressed_ccs)?;
29 pub fn send_activity_to_community<A>(
32 community: &Community,
35 ) -> Result<(), Error>
37 A: Activity + Base + Serialize + Debug,
39 insert_activity(&conn, creator.id, &activity, true)?;
41 // if this is a local community, we need to do an announce from the community instead
43 Community::do_announce(activity, &community, creator, conn)?;
45 send_activity(&activity, creator, to)?;
50 /// Send an activity to a list of recipients, using the correct headers etc.
51 pub fn send_activity<A>(activity: &A, actor: &dyn ActorType, to: Vec<String>) -> Result<(), Error>
55 let json = serde_json::to_string(&activity)?;
56 debug!("Sending activitypub activity {} to {:?}", json, to);
58 let to_url = Url::parse(&t)?;
59 if !is_apub_id_valid(&to_url) {
60 debug!("Not sending activity to {} (invalid or blocklisted)", t);
63 let request = Request::post(t).header("Host", to_url.domain().unwrap());
64 let signature = sign(&request, actor)?;
66 .header("Signature", signature)
67 .header("Content-Type", "application/json")
68 .body(json.to_owned())?
70 debug!("Result for activity send: {:?}", res);