2020-09-30 16:19:14 +00:00
|
|
|
use crate::{
|
|
|
|
check_is_apub_id_valid,
|
2020-10-01 20:57:47 +00:00
|
|
|
extensions::signatures::sign_and_send,
|
2020-09-30 16:19:14 +00:00
|
|
|
insert_activity,
|
|
|
|
ActorType,
|
2021-03-17 17:12:37 +00:00
|
|
|
CommunityType,
|
2020-12-16 15:30:44 +00:00
|
|
|
APUB_JSON_CONTENT_TYPE,
|
2020-09-30 16:19:14 +00:00
|
|
|
};
|
2020-08-31 13:48:02 +00:00
|
|
|
use activitystreams::{
|
2020-10-06 16:28:31 +00:00
|
|
|
base::{BaseExt, Extends, ExtendsExt},
|
2020-08-31 13:48:02 +00:00
|
|
|
object::AsObject,
|
|
|
|
};
|
|
|
|
use anyhow::{anyhow, Context, Error};
|
|
|
|
use background_jobs::{
|
|
|
|
create_server,
|
|
|
|
memory_storage::Storage,
|
|
|
|
ActixJob,
|
|
|
|
Backoff,
|
|
|
|
MaxRetries,
|
|
|
|
QueueHandle,
|
|
|
|
WorkerConfig,
|
|
|
|
};
|
2020-09-30 16:19:14 +00:00
|
|
|
use itertools::Itertools;
|
2021-03-10 22:33:55 +00:00
|
|
|
use lemmy_db_schema::source::{community::Community, person::Person};
|
2021-03-01 17:24:11 +00:00
|
|
|
use lemmy_utils::{location_info, settings::structs::Settings, LemmyError};
|
2020-09-30 16:19:14 +00:00
|
|
|
use lemmy_websocket::LemmyContext;
|
2020-10-06 16:28:31 +00:00
|
|
|
use log::{debug, warn};
|
2020-09-29 13:10:55 +00:00
|
|
|
use reqwest::Client;
|
2021-02-01 20:56:37 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use std::{collections::BTreeMap, env, fmt::Debug, future::Future, pin::Pin};
|
2020-08-31 13:48:02 +00:00
|
|
|
use url::Url;
|
|
|
|
|
2020-10-19 14:29:35 +00:00
|
|
|
/// Sends a local activity to a single, remote actor.
|
|
|
|
///
|
|
|
|
/// * `activity` the apub activity to be sent
|
|
|
|
/// * `creator` the local actor which created the activity
|
|
|
|
/// * `inbox` the inbox url where the activity should be delivered to
|
2020-12-08 17:38:48 +00:00
|
|
|
pub(crate) async fn send_activity_single_dest<T, Kind>(
|
2020-09-30 16:19:14 +00:00
|
|
|
activity: T,
|
|
|
|
creator: &dyn ActorType,
|
2020-10-12 16:02:28 +00:00
|
|
|
inbox: Url,
|
2020-09-30 16:19:14 +00:00
|
|
|
context: &LemmyContext,
|
|
|
|
) -> Result<(), LemmyError>
|
|
|
|
where
|
2020-10-06 16:28:31 +00:00
|
|
|
T: AsObject<Kind> + Extends<Kind> + Debug + BaseExt<Kind>,
|
2020-09-30 16:19:14 +00:00
|
|
|
Kind: Serialize,
|
|
|
|
<T as Extends<Kind>>::Error: From<serde_json::Error> + Send + Sync + 'static,
|
|
|
|
{
|
2021-04-21 13:36:07 +00:00
|
|
|
if check_is_apub_id_valid(&inbox, false).is_ok() {
|
2020-10-12 16:02:28 +00:00
|
|
|
debug!(
|
|
|
|
"Sending activity {:?} to {}",
|
Apub inbox rewrite (#1652)
* start to implement apub inbox routing lib
* got something that almost works
* it compiles!
* implemented some more
* move library code to separate crate (most of it)
* convert private message handlers
* convert all comment receivers (except undo comment)
* convert post receiver
* add verify trait
* convert community receivers
* add cc field for all activities which i forgot before
* convert inbox functions, add missing checks
* convert undo like/dislike receivers
* convert undo_delete and undo_remove receivers
* move block/unblock activities
* convert remaining activity receivers
* reimplement http signature verification and other checks
* also use actor type for routing, VerifyActivity and SendActivity traits
* cleanup and restructure apub_receive code
* wip: try to fix activity routing
* implement a (very bad) derive macro for activityhandler
* working activity routing!
* rework pm verify(), fix tests and confirm manually
also remove inbox username check which was broken
* rework following verify(), fix tests and test manually
* fix post/comment create/update, rework voting
* Rewrite remove/delete post/comment, fix tests, test manually
* Rework and fix (un)block user, announce, update post
* some code cleanup
* rework delete/remove activity receivers (still quite messy)
* rewrite, test and fix add/remove mod, update community handlers
* add docs for ActivityHandler derive macro
* dont try to compile macro comments
2021-07-17 16:08:46 +00:00
|
|
|
&activity.id_unchecked().map(ToString::to_string),
|
2020-10-12 16:02:28 +00:00
|
|
|
&inbox
|
|
|
|
);
|
2021-04-09 15:01:26 +00:00
|
|
|
send_activity_internal(context, activity, creator, vec![inbox], true, true).await?;
|
2020-09-30 16:19:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-10-19 14:29:35 +00:00
|
|
|
/// From a local community, send activity to all remote followers.
|
|
|
|
///
|
|
|
|
/// * `activity` the apub activity to send
|
|
|
|
/// * `community` the sending community
|
2021-04-09 15:01:26 +00:00
|
|
|
/// * `extra_inbox` actor inbox which should receive the activity, in addition to followers
|
2020-12-08 17:38:48 +00:00
|
|
|
pub(crate) async fn send_to_community_followers<T, Kind>(
|
2020-09-30 16:19:14 +00:00
|
|
|
activity: T,
|
|
|
|
community: &Community,
|
2021-04-09 15:01:26 +00:00
|
|
|
extra_inbox: Option<Url>,
|
2020-09-30 16:19:14 +00:00
|
|
|
context: &LemmyContext,
|
|
|
|
) -> Result<(), LemmyError>
|
|
|
|
where
|
2020-10-06 16:28:31 +00:00
|
|
|
T: AsObject<Kind> + Extends<Kind> + Debug + BaseExt<Kind>,
|
2020-09-30 16:19:14 +00:00
|
|
|
Kind: Serialize,
|
|
|
|
<T as Extends<Kind>>::Error: From<serde_json::Error> + Send + Sync + 'static,
|
|
|
|
{
|
2021-04-09 15:01:26 +00:00
|
|
|
let extra_inbox: Vec<Url> = extra_inbox.into_iter().collect();
|
|
|
|
let follower_inboxes: Vec<Url> = vec![
|
|
|
|
community.get_follower_inboxes(context.pool()).await?,
|
|
|
|
extra_inbox,
|
|
|
|
]
|
|
|
|
.iter()
|
|
|
|
.flatten()
|
|
|
|
.unique()
|
|
|
|
.filter(|inbox| inbox.host_str() != Some(&Settings::get().hostname()))
|
2021-04-21 13:36:07 +00:00
|
|
|
.filter(|inbox| check_is_apub_id_valid(inbox, false).is_ok())
|
2021-04-09 15:01:26 +00:00
|
|
|
.map(|inbox| inbox.to_owned())
|
|
|
|
.collect();
|
2020-10-06 16:28:31 +00:00
|
|
|
debug!(
|
|
|
|
"Sending activity {:?} to followers of {}",
|
Apub inbox rewrite (#1652)
* start to implement apub inbox routing lib
* got something that almost works
* it compiles!
* implemented some more
* move library code to separate crate (most of it)
* convert private message handlers
* convert all comment receivers (except undo comment)
* convert post receiver
* add verify trait
* convert community receivers
* add cc field for all activities which i forgot before
* convert inbox functions, add missing checks
* convert undo like/dislike receivers
* convert undo_delete and undo_remove receivers
* move block/unblock activities
* convert remaining activity receivers
* reimplement http signature verification and other checks
* also use actor type for routing, VerifyActivity and SendActivity traits
* cleanup and restructure apub_receive code
* wip: try to fix activity routing
* implement a (very bad) derive macro for activityhandler
* working activity routing!
* rework pm verify(), fix tests and confirm manually
also remove inbox username check which was broken
* rework following verify(), fix tests and test manually
* fix post/comment create/update, rework voting
* Rewrite remove/delete post/comment, fix tests, test manually
* Rework and fix (un)block user, announce, update post
* some code cleanup
* rework delete/remove activity receivers (still quite messy)
* rewrite, test and fix add/remove mod, update community handlers
* add docs for ActivityHandler derive macro
* dont try to compile macro comments
2021-07-17 16:08:46 +00:00
|
|
|
&activity.id_unchecked().map(ToString::to_string),
|
2020-10-06 16:28:31 +00:00
|
|
|
&community.actor_id
|
|
|
|
);
|
2020-09-30 16:19:14 +00:00
|
|
|
|
2021-04-09 15:01:26 +00:00
|
|
|
send_activity_internal(context, activity, community, follower_inboxes, true, false).await?;
|
2020-09-30 16:19:14 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-03-10 22:33:55 +00:00
|
|
|
/// Sends an activity from a local person to a remote community.
|
2020-10-19 14:29:35 +00:00
|
|
|
///
|
|
|
|
/// * `activity` the activity to send
|
|
|
|
/// * `creator` the creator of the activity
|
|
|
|
/// * `community` the destination community
|
2021-04-09 15:01:26 +00:00
|
|
|
/// * `object_actor` if the object of the activity is an actor, it should be passed here so it can
|
|
|
|
/// be sent directly to the actor
|
2020-10-19 14:29:35 +00:00
|
|
|
///
|
2020-12-08 17:38:48 +00:00
|
|
|
pub(crate) async fn send_to_community<T, Kind>(
|
2020-10-19 14:29:35 +00:00
|
|
|
activity: T,
|
2021-03-10 22:33:55 +00:00
|
|
|
creator: &Person,
|
2020-09-30 16:19:14 +00:00
|
|
|
community: &Community,
|
2021-04-09 15:01:26 +00:00
|
|
|
object_actor: Option<Url>,
|
2020-09-30 16:19:14 +00:00
|
|
|
context: &LemmyContext,
|
|
|
|
) -> Result<(), LemmyError>
|
|
|
|
where
|
2020-10-06 16:28:31 +00:00
|
|
|
T: AsObject<Kind> + Extends<Kind> + Debug + BaseExt<Kind>,
|
2020-09-30 16:19:14 +00:00
|
|
|
Kind: Serialize,
|
|
|
|
<T as Extends<Kind>>::Error: From<serde_json::Error> + Send + Sync + 'static,
|
|
|
|
{
|
|
|
|
// if this is a local community, we need to do an announce from the community instead
|
|
|
|
if community.local {
|
2020-10-12 14:10:09 +00:00
|
|
|
community
|
2021-04-09 15:01:26 +00:00
|
|
|
.send_announce(activity.into_any_base()?, object_actor, context)
|
2020-10-12 14:10:09 +00:00
|
|
|
.await?;
|
2020-09-30 16:19:14 +00:00
|
|
|
} else {
|
2021-02-04 16:34:58 +00:00
|
|
|
let inbox = community.get_shared_inbox_or_inbox_url();
|
2021-04-21 13:36:07 +00:00
|
|
|
check_is_apub_id_valid(&inbox, false)?;
|
2020-10-06 16:28:31 +00:00
|
|
|
debug!(
|
|
|
|
"Sending activity {:?} to community {}",
|
Apub inbox rewrite (#1652)
* start to implement apub inbox routing lib
* got something that almost works
* it compiles!
* implemented some more
* move library code to separate crate (most of it)
* convert private message handlers
* convert all comment receivers (except undo comment)
* convert post receiver
* add verify trait
* convert community receivers
* add cc field for all activities which i forgot before
* convert inbox functions, add missing checks
* convert undo like/dislike receivers
* convert undo_delete and undo_remove receivers
* move block/unblock activities
* convert remaining activity receivers
* reimplement http signature verification and other checks
* also use actor type for routing, VerifyActivity and SendActivity traits
* cleanup and restructure apub_receive code
* wip: try to fix activity routing
* implement a (very bad) derive macro for activityhandler
* working activity routing!
* rework pm verify(), fix tests and confirm manually
also remove inbox username check which was broken
* rework following verify(), fix tests and test manually
* fix post/comment create/update, rework voting
* Rewrite remove/delete post/comment, fix tests, test manually
* Rework and fix (un)block user, announce, update post
* some code cleanup
* rework delete/remove activity receivers (still quite messy)
* rewrite, test and fix add/remove mod, update community handlers
* add docs for ActivityHandler derive macro
* dont try to compile macro comments
2021-07-17 16:08:46 +00:00
|
|
|
&activity.id_unchecked().map(ToString::to_string),
|
2020-10-06 16:28:31 +00:00
|
|
|
&community.actor_id
|
|
|
|
);
|
2021-04-09 15:01:26 +00:00
|
|
|
// dont send to object_actor here, as that is responsibility of the community itself
|
|
|
|
send_activity_internal(context, activity, creator, vec![inbox], true, false).await?;
|
2020-09-30 16:19:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-03-10 22:33:55 +00:00
|
|
|
/// Sends notification to any persons mentioned in a comment
|
2020-10-19 14:29:35 +00:00
|
|
|
///
|
2021-03-10 22:33:55 +00:00
|
|
|
/// * `creator` person who created the comment
|
|
|
|
/// * `mentions` list of inboxes of persons which are mentioned in the comment
|
2020-10-19 14:29:35 +00:00
|
|
|
/// * `activity` either a `Create/Note` or `Update/Note`
|
2020-12-08 17:38:48 +00:00
|
|
|
pub(crate) async fn send_comment_mentions<T, Kind>(
|
2021-03-10 22:33:55 +00:00
|
|
|
creator: &Person,
|
2020-09-30 16:19:14 +00:00
|
|
|
mentions: Vec<Url>,
|
|
|
|
activity: T,
|
|
|
|
context: &LemmyContext,
|
|
|
|
) -> Result<(), LemmyError>
|
|
|
|
where
|
2020-10-06 16:28:31 +00:00
|
|
|
T: AsObject<Kind> + Extends<Kind> + Debug + BaseExt<Kind>,
|
2020-09-30 16:19:14 +00:00
|
|
|
Kind: Serialize,
|
|
|
|
<T as Extends<Kind>>::Error: From<serde_json::Error> + Send + Sync + 'static,
|
|
|
|
{
|
2020-10-06 16:28:31 +00:00
|
|
|
debug!(
|
|
|
|
"Sending mentions activity {:?} to {:?}",
|
|
|
|
&activity.id_unchecked(),
|
|
|
|
&mentions
|
|
|
|
);
|
2020-09-30 16:19:14 +00:00
|
|
|
let mentions = mentions
|
|
|
|
.iter()
|
2021-04-21 13:36:07 +00:00
|
|
|
.filter(|inbox| check_is_apub_id_valid(inbox, false).is_ok())
|
2020-09-30 16:19:14 +00:00
|
|
|
.map(|i| i.to_owned())
|
|
|
|
.collect();
|
|
|
|
send_activity_internal(
|
2021-04-09 15:01:26 +00:00
|
|
|
context, activity, creator, mentions, false, // Don't create a new DB row
|
2020-11-06 13:06:47 +00:00
|
|
|
false,
|
2020-09-30 16:19:14 +00:00
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-10-19 14:29:35 +00:00
|
|
|
/// Create new `SendActivityTasks`, which will deliver the given activity to inboxes, as well as
|
|
|
|
/// handling signing and retrying failed deliveres.
|
2020-09-30 16:19:14 +00:00
|
|
|
///
|
|
|
|
/// The caller of this function needs to remove any blocked domains from `to`,
|
|
|
|
/// using `check_is_apub_id_valid()`.
|
|
|
|
async fn send_activity_internal<T, Kind>(
|
2021-04-09 15:01:26 +00:00
|
|
|
context: &LemmyContext,
|
2020-08-31 13:48:02 +00:00
|
|
|
activity: T,
|
|
|
|
actor: &dyn ActorType,
|
2020-10-12 16:02:28 +00:00
|
|
|
inboxes: Vec<Url>,
|
2020-10-06 15:19:01 +00:00
|
|
|
insert_into_db: bool,
|
2020-11-06 13:06:47 +00:00
|
|
|
sensitive: bool,
|
2020-08-31 13:48:02 +00:00
|
|
|
) -> Result<(), LemmyError>
|
|
|
|
where
|
2020-10-06 16:28:31 +00:00
|
|
|
T: AsObject<Kind> + Extends<Kind> + Debug,
|
2020-08-31 13:48:02 +00:00
|
|
|
Kind: Serialize,
|
|
|
|
<T as Extends<Kind>>::Error: From<serde_json::Error> + Send + Sync + 'static,
|
|
|
|
{
|
2021-03-01 17:24:11 +00:00
|
|
|
if !Settings::get().federation().enabled || inboxes.is_empty() {
|
2020-08-31 13:48:02 +00:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2021-01-15 01:18:18 +00:00
|
|
|
// Don't send anything to ourselves
|
|
|
|
let hostname = Settings::get().get_hostname_without_port()?;
|
|
|
|
let inboxes: Vec<&Url> = inboxes
|
|
|
|
.iter()
|
2021-03-01 12:56:07 +00:00
|
|
|
.filter(|i| i.domain().expect("valid inbox url") != hostname)
|
2021-01-15 01:18:18 +00:00
|
|
|
.collect();
|
|
|
|
|
2020-09-30 16:19:14 +00:00
|
|
|
let activity = activity.into_any_base()?;
|
|
|
|
let serialised_activity = serde_json::to_string(&activity)?;
|
2020-10-06 15:19:01 +00:00
|
|
|
|
|
|
|
// This is necessary because send_comment and send_comment_mentions
|
|
|
|
// might send the same ap_id
|
|
|
|
if insert_into_db {
|
2020-10-23 12:29:56 +00:00
|
|
|
let id = activity.id().context(location_info!())?;
|
2021-04-09 15:01:26 +00:00
|
|
|
insert_activity(id, activity.clone(), true, sensitive, context.pool()).await?;
|
2020-10-06 15:19:01 +00:00
|
|
|
}
|
2020-09-30 16:19:14 +00:00
|
|
|
|
2020-10-12 16:02:28 +00:00
|
|
|
for i in inboxes {
|
2020-10-06 17:19:53 +00:00
|
|
|
let message = SendActivityTask {
|
|
|
|
activity: serialised_activity.to_owned(),
|
2021-01-15 01:18:18 +00:00
|
|
|
inbox: i.to_owned(),
|
2021-01-27 16:42:23 +00:00
|
|
|
actor_id: actor.actor_id(),
|
2020-10-06 17:19:53 +00:00
|
|
|
private_key: actor.private_key().context(location_info!())?,
|
|
|
|
};
|
2020-12-14 16:44:27 +00:00
|
|
|
if env::var("LEMMY_TEST_SEND_SYNC").is_ok() {
|
|
|
|
do_send(message, &Client::default()).await?;
|
|
|
|
} else {
|
2021-04-09 15:01:26 +00:00
|
|
|
context.activity_queue.queue::<SendActivityTask>(message)?;
|
2020-12-14 16:44:27 +00:00
|
|
|
}
|
2020-10-06 17:19:53 +00:00
|
|
|
}
|
2020-08-31 13:48:02 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
|
|
|
struct SendActivityTask {
|
|
|
|
activity: String,
|
2020-10-12 16:02:28 +00:00
|
|
|
inbox: Url,
|
2020-08-31 13:48:02 +00:00
|
|
|
actor_id: Url,
|
|
|
|
private_key: String,
|
|
|
|
}
|
|
|
|
|
2020-10-19 14:29:35 +00:00
|
|
|
/// Signs the activity with the sending actor's key, and delivers to the given inbox. Also retries
|
|
|
|
/// if the delivery failed.
|
2020-08-31 13:48:02 +00:00
|
|
|
impl ActixJob for SendActivityTask {
|
|
|
|
type State = MyState;
|
|
|
|
type Future = Pin<Box<dyn Future<Output = Result<(), Error>>>>;
|
|
|
|
const NAME: &'static str = "SendActivityTask";
|
|
|
|
|
|
|
|
const MAX_RETRIES: MaxRetries = MaxRetries::Count(10);
|
|
|
|
const BACKOFF: Backoff = Backoff::Exponential(2);
|
|
|
|
|
|
|
|
fn run(self, state: Self::State) -> Self::Future {
|
2020-12-14 17:01:12 +00:00
|
|
|
Box::pin(async move { do_send(self, &state.client).await })
|
2020-12-14 16:44:27 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-29 13:10:55 +00:00
|
|
|
|
2020-12-14 17:01:12 +00:00
|
|
|
async fn do_send(task: SendActivityTask, client: &Client) -> Result<(), Error> {
|
2020-12-14 16:44:27 +00:00
|
|
|
let mut headers = BTreeMap::<String, String>::new();
|
2021-01-06 04:55:02 +00:00
|
|
|
headers.insert("Content-Type".into(), APUB_JSON_CONTENT_TYPE.to_string());
|
2020-12-14 16:44:27 +00:00
|
|
|
let result = sign_and_send(
|
|
|
|
client,
|
|
|
|
headers,
|
|
|
|
&task.inbox,
|
|
|
|
task.activity.clone(),
|
|
|
|
&task.actor_id,
|
|
|
|
task.private_key.to_owned(),
|
|
|
|
)
|
2020-12-14 17:01:12 +00:00
|
|
|
.await;
|
2020-12-14 16:44:27 +00:00
|
|
|
|
|
|
|
if let Err(e) = result {
|
|
|
|
warn!("{}", e);
|
|
|
|
return Err(anyhow!(
|
2020-12-14 17:01:12 +00:00
|
|
|
"Failed to send activity {} to {}",
|
|
|
|
&task.activity,
|
|
|
|
task.inbox
|
|
|
|
));
|
2020-08-31 13:48:02 +00:00
|
|
|
}
|
2020-12-14 16:44:27 +00:00
|
|
|
Ok(())
|
2020-08-31 13:48:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn create_activity_queue() -> QueueHandle {
|
|
|
|
// Start the application server. This guards access to to the jobs store
|
|
|
|
let queue_handle = create_server(Storage::new());
|
|
|
|
|
|
|
|
// Configure and start our workers
|
|
|
|
WorkerConfig::new(|| MyState {
|
|
|
|
client: Client::default(),
|
|
|
|
})
|
|
|
|
.register::<SendActivityTask>()
|
|
|
|
.start(queue_handle.clone());
|
|
|
|
|
|
|
|
queue_handle
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
struct MyState {
|
|
|
|
pub client: Client,
|
|
|
|
}
|