2020-06-01 14:17:20 +00:00
|
|
|
use crate::{
|
|
|
|
apub::{extensions::signatures::sign, is_apub_id_valid, ActorType},
|
|
|
|
db::{activity::insert_activity, community::Community, user::User_},
|
|
|
|
};
|
|
|
|
use activitystreams::{context, object::properties::ObjectProperties, public, Activity, Base};
|
|
|
|
use diesel::PgConnection;
|
2020-05-16 14:04:08 +00:00
|
|
|
use failure::{Error, _core::fmt::Debug};
|
|
|
|
use isahc::prelude::*;
|
|
|
|
use log::debug;
|
|
|
|
use serde::Serialize;
|
|
|
|
use url::Url;
|
2020-04-09 19:04:31 +00:00
|
|
|
|
2020-04-27 16:57:00 +00:00
|
|
|
pub fn populate_object_props(
|
2020-04-13 13:06:41 +00:00
|
|
|
props: &mut ObjectProperties,
|
2020-05-15 16:36:11 +00:00
|
|
|
addressed_ccs: Vec<String>,
|
2020-04-13 13:06:41 +00:00
|
|
|
object_id: &str,
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
props
|
|
|
|
.set_context_xsd_any_uri(context())?
|
|
|
|
// TODO: the activity needs a seperate id from the object
|
|
|
|
.set_id(object_id)?
|
2020-04-10 11:26:06 +00:00
|
|
|
// TODO: should to/cc go on the Create, or on the Post? or on both?
|
|
|
|
// TODO: handle privacy on the receiving side (at least ignore anything thats not public)
|
2020-04-13 13:06:41 +00:00
|
|
|
.set_to_xsd_any_uri(public())?
|
2020-05-15 16:36:11 +00:00
|
|
|
.set_many_cc_xsd_any_uris(addressed_ccs)?;
|
2020-04-13 13:06:41 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-06-01 14:17:20 +00:00
|
|
|
pub fn send_activity_to_community<A>(
|
|
|
|
creator: &User_,
|
|
|
|
conn: &PgConnection,
|
|
|
|
community: &Community,
|
|
|
|
to: Vec<String>,
|
|
|
|
activity: A,
|
|
|
|
) -> Result<(), Error>
|
|
|
|
where
|
|
|
|
A: Activity + Base + Serialize + Debug,
|
|
|
|
{
|
|
|
|
insert_activity(&conn, creator.id, &activity, true)?;
|
|
|
|
|
|
|
|
// if this is a local community, we need to do an announce from the community instead
|
|
|
|
if community.local {
|
|
|
|
Community::do_announce(activity, &community, creator, conn)?;
|
|
|
|
} else {
|
|
|
|
send_activity(&activity, creator, to)?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-04-17 15:33:55 +00:00
|
|
|
/// Send an activity to a list of recipients, using the correct headers etc.
|
2020-05-14 13:38:07 +00:00
|
|
|
pub fn send_activity<A>(activity: &A, actor: &dyn ActorType, to: Vec<String>) -> Result<(), Error>
|
2020-04-13 13:06:41 +00:00
|
|
|
where
|
|
|
|
A: Serialize + Debug,
|
|
|
|
{
|
|
|
|
let json = serde_json::to_string(&activity)?;
|
2020-04-17 17:34:18 +00:00
|
|
|
debug!("Sending activitypub activity {} to {:?}", json, to);
|
2020-04-14 15:37:23 +00:00
|
|
|
for t in to {
|
2020-04-18 18:54:20 +00:00
|
|
|
let to_url = Url::parse(&t)?;
|
|
|
|
if !is_apub_id_valid(&to_url) {
|
2020-06-18 01:29:10 +00:00
|
|
|
debug!("Not sending activity to {} (invalid or blocklisted)", t);
|
2020-04-17 17:34:18 +00:00
|
|
|
continue;
|
|
|
|
}
|
2020-04-18 18:54:20 +00:00
|
|
|
let request = Request::post(t).header("Host", to_url.domain().unwrap());
|
2020-05-14 13:38:07 +00:00
|
|
|
let signature = sign(&request, actor)?;
|
2020-04-18 18:54:20 +00:00
|
|
|
let res = request
|
|
|
|
.header("Signature", signature)
|
2020-04-09 19:04:31 +00:00
|
|
|
.header("Content-Type", "application/json")
|
|
|
|
.body(json.to_owned())?
|
|
|
|
.send()?;
|
2020-04-17 14:55:28 +00:00
|
|
|
debug!("Result for activity send: {:?}", res);
|
2020-04-09 19:04:31 +00:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|