2020-06-01 14:17:20 +00:00
|
|
|
use crate::{
|
2020-07-10 18:15:41 +00:00
|
|
|
apub::{
|
2020-08-04 14:39:55 +00:00
|
|
|
check_is_apub_id_valid,
|
2020-07-29 11:46:11 +00:00
|
|
|
community::do_announce,
|
|
|
|
extensions::signatures::sign,
|
|
|
|
insert_activity,
|
2020-07-10 18:15:41 +00:00
|
|
|
ActorType,
|
|
|
|
},
|
2020-07-01 12:54:29 +00:00
|
|
|
request::retry_custom,
|
2020-07-29 11:46:11 +00:00
|
|
|
DbPool,
|
|
|
|
LemmyError,
|
2020-06-01 14:17:20 +00:00
|
|
|
};
|
2020-08-01 13:25:17 +00:00
|
|
|
use activitystreams::base::AnyBase;
|
2020-07-01 12:54:29 +00:00
|
|
|
use actix_web::client::Client;
|
2020-07-10 18:15:41 +00:00
|
|
|
use lemmy_db::{community::Community, user::User_};
|
2020-07-28 16:47:26 +00:00
|
|
|
use lemmy_utils::{get_apub_protocol_string, settings::Settings};
|
2020-05-16 14:04:08 +00:00
|
|
|
use log::debug;
|
2020-07-28 16:47:26 +00:00
|
|
|
use url::{ParseError, Url};
|
|
|
|
use uuid::Uuid;
|
2020-04-09 19:04:31 +00:00
|
|
|
|
2020-07-17 21:11:07 +00:00
|
|
|
pub async fn send_activity_to_community(
|
2020-06-01 14:17:20 +00:00
|
|
|
creator: &User_,
|
|
|
|
community: &Community,
|
|
|
|
to: Vec<String>,
|
2020-07-17 21:11:07 +00:00
|
|
|
activity: AnyBase,
|
2020-07-01 12:54:29 +00:00
|
|
|
client: &Client,
|
|
|
|
pool: &DbPool,
|
2020-07-17 21:11:07 +00:00
|
|
|
) -> Result<(), LemmyError> {
|
2020-07-01 12:54:29 +00:00
|
|
|
insert_activity(creator.id, activity.clone(), true, pool).await?;
|
2020-06-01 14:17:20 +00:00
|
|
|
|
|
|
|
// if this is a local community, we need to do an announce from the community instead
|
|
|
|
if community.local {
|
2020-07-10 18:15:41 +00:00
|
|
|
do_announce(activity, &community, creator, client, pool).await?;
|
2020-06-01 14:17:20 +00:00
|
|
|
} else {
|
2020-07-01 12:54:29 +00:00
|
|
|
send_activity(client, &activity, creator, to).await?;
|
2020-06-01 14:17:20 +00:00
|
|
|
}
|
2020-07-01 12:54:29 +00:00
|
|
|
|
2020-06-01 14:17:20 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-04-17 15:33:55 +00:00
|
|
|
/// Send an activity to a list of recipients, using the correct headers etc.
|
2020-07-17 21:11:07 +00:00
|
|
|
pub async fn send_activity(
|
2020-07-01 12:54:29 +00:00
|
|
|
client: &Client,
|
2020-07-17 21:11:07 +00:00
|
|
|
activity: &AnyBase,
|
2020-07-01 12:54:29 +00:00
|
|
|
actor: &dyn ActorType,
|
|
|
|
to: Vec<String>,
|
2020-07-17 21:11:07 +00:00
|
|
|
) -> Result<(), LemmyError> {
|
2020-07-01 12:54:29 +00:00
|
|
|
let activity = serde_json::to_string(&activity)?;
|
|
|
|
debug!("Sending activitypub activity {} to {:?}", activity, 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)?;
|
2020-08-04 14:39:55 +00:00
|
|
|
check_is_apub_id_valid(&to_url)?;
|
2020-07-01 12:54:29 +00:00
|
|
|
|
|
|
|
let res = retry_custom(|| async {
|
|
|
|
let request = client.post(&t).header("Content-Type", "application/json");
|
|
|
|
|
|
|
|
match sign(request, actor, activity.clone()).await {
|
|
|
|
Ok(signed) => Ok(signed.send().await),
|
|
|
|
Err(e) => Err(e),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.await?;
|
|
|
|
|
2020-04-17 14:55:28 +00:00
|
|
|
debug!("Result for activity send: {:?}", res);
|
2020-04-09 19:04:31 +00:00
|
|
|
}
|
2020-07-01 12:54:29 +00:00
|
|
|
|
2020-04-09 19:04:31 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2020-07-28 16:47:26 +00:00
|
|
|
|
|
|
|
pub(in crate::apub) fn generate_activity_id<T>(kind: T) -> Result<Url, ParseError>
|
|
|
|
where
|
|
|
|
T: ToString,
|
|
|
|
{
|
|
|
|
let id = format!(
|
|
|
|
"{}://{}/activities/{}/{}",
|
|
|
|
get_apub_protocol_string(),
|
|
|
|
Settings::get().hostname,
|
|
|
|
kind.to_string().to_lowercase(),
|
|
|
|
Uuid::new_v4()
|
|
|
|
);
|
|
|
|
Url::parse(&id)
|
|
|
|
}
|