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};
12 pub fn populate_object_props(
13 props: &mut ObjectProperties,
14 addressed_ccs: Vec<String>,
16 ) -> Result<(), Error> {
18 .set_context_xsd_any_uri(context())?
19 // TODO: the activity needs a seperate id from the object
21 // TODO: should to/cc go on the Create, or on the Post? or on both?
22 // TODO: handle privacy on the receiving side (at least ignore anything thats not public)
23 .set_to_xsd_any_uri(public())?
24 .set_many_cc_xsd_any_uris(addressed_ccs)?;
28 pub fn send_activity_to_community<A>(
31 community: &Community,
34 ) -> Result<(), Error>
36 A: Activity + Base + Serialize + Debug,
38 insert_activity(&conn, creator.id, &activity, true)?;
40 // if this is a local community, we need to do an announce from the community instead
42 Community::do_announce(activity, &community, creator, conn)?;
44 send_activity(&activity, creator, to)?;
49 /// Send an activity to a list of recipients, using the correct headers etc.
50 pub fn send_activity<A>(activity: &A, actor: &dyn ActorType, to: Vec<String>) -> Result<(), Error>
54 let json = serde_json::to_string(&activity)?;
55 debug!("Sending activitypub activity {} to {:?}", json, to);
57 let to_url = Url::parse(&t)?;
58 if !is_apub_id_valid(&to_url) {
59 debug!("Not sending activity to {} (invalid or blacklisted)", t);
62 let mut request = attohttpc::post(t).header("Host", to_url.domain().unwrap());
63 let signature = sign(&mut request, actor)?;
65 .header("Signature", signature)
66 .header("Content-Type", "application/json")
67 .text(json.to_owned())
71 debug!("Result for activity send: {:?}", res);