2021-11-05 00:24:10 +00:00
|
|
|
use crate::objects::{comment::ApubComment, community::ApubCommunity, person::ApubPerson};
|
2021-07-31 14:57:37 +00:00
|
|
|
use activitystreams::{
|
|
|
|
base::BaseExt,
|
|
|
|
link::{LinkExt, Mention},
|
|
|
|
};
|
|
|
|
use anyhow::anyhow;
|
|
|
|
use itertools::Itertools;
|
2021-10-18 21:36:44 +00:00
|
|
|
use lemmy_api_common::blocking;
|
2021-11-05 00:24:10 +00:00
|
|
|
use lemmy_apub_lib::{object_id::ObjectId, traits::ActorType, webfinger::WebfingerResponse};
|
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
|
|
|
use lemmy_db_schema::{
|
2021-10-16 13:33:38 +00:00
|
|
|
newtypes::LocalUserId,
|
2021-10-18 21:36:44 +00:00
|
|
|
source::{comment::Comment, person::Person, post::Post},
|
2021-10-16 13:33:38 +00:00
|
|
|
traits::Crud,
|
|
|
|
DbPool,
|
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
|
|
|
};
|
2021-07-31 14:57:37 +00:00
|
|
|
use lemmy_utils::{
|
|
|
|
request::{retry, RecvError},
|
|
|
|
utils::{scrape_text_for_mentions, MentionData},
|
|
|
|
LemmyError,
|
|
|
|
};
|
2021-10-18 21:36:44 +00:00
|
|
|
use lemmy_websocket::{send::send_local_notifs, LemmyContext};
|
2021-07-31 14:57:37 +00:00
|
|
|
use log::debug;
|
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
|
|
|
use url::Url;
|
|
|
|
|
2021-07-31 15:09:43 +00:00
|
|
|
pub mod create_or_update;
|
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
|
|
|
|
|
|
|
async fn get_notif_recipients(
|
2021-10-18 21:36:44 +00:00
|
|
|
actor: &ObjectId<ApubPerson>,
|
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
|
|
|
comment: &Comment,
|
|
|
|
context: &LemmyContext,
|
|
|
|
request_counter: &mut i32,
|
|
|
|
) -> Result<Vec<LocalUserId>, LemmyError> {
|
|
|
|
let post_id = comment.post_id;
|
|
|
|
let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
|
2021-09-25 15:44:52 +00:00
|
|
|
let actor = actor.dereference(context, request_counter).await?;
|
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
|
|
|
|
|
|
|
// Note:
|
|
|
|
// Although mentions could be gotten from the post tags (they are included there), or the ccs,
|
|
|
|
// Its much easier to scrape them from the comment body, since the API has to do that
|
|
|
|
// anyway.
|
|
|
|
// TODO: for compatibility with other projects, it would be much better to read this from cc or tags
|
|
|
|
let mentions = scrape_text_for_mentions(&comment.content);
|
2021-10-18 21:36:44 +00:00
|
|
|
send_local_notifs(mentions, comment, &*actor, &post, true, context).await
|
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
|
|
|
}
|
|
|
|
|
2021-07-31 14:57:37 +00:00
|
|
|
pub struct MentionsAndAddresses {
|
|
|
|
pub ccs: Vec<Url>,
|
|
|
|
pub inboxes: Vec<Url>,
|
|
|
|
pub tags: Vec<Mention>,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This takes a comment, and builds a list of to_addresses, inboxes,
|
|
|
|
/// and mention tags, so they know where to be sent to.
|
|
|
|
/// Addresses are the persons / addresses that go in the cc field.
|
|
|
|
pub async fn collect_non_local_mentions(
|
2021-10-18 21:36:44 +00:00
|
|
|
comment: &ApubComment,
|
|
|
|
community: &ApubCommunity,
|
2021-07-31 14:57:37 +00:00
|
|
|
context: &LemmyContext,
|
|
|
|
) -> Result<MentionsAndAddresses, LemmyError> {
|
|
|
|
let parent_creator = get_comment_parent_creator(context.pool(), comment).await?;
|
2021-10-18 21:36:44 +00:00
|
|
|
let mut addressed_ccs: Vec<Url> = vec![community.actor_id(), parent_creator.actor_id()];
|
2021-07-31 14:57:37 +00:00
|
|
|
// Note: dont include community inbox here, as we send to it separately with `send_to_community()`
|
2021-10-06 20:20:05 +00:00
|
|
|
let mut inboxes = vec![parent_creator.shared_inbox_or_inbox_url()];
|
2021-07-31 14:57:37 +00:00
|
|
|
|
|
|
|
// Add the mention tag
|
|
|
|
let mut tags = Vec::new();
|
|
|
|
|
|
|
|
// Get the person IDs for any mentions
|
|
|
|
let mentions = scrape_text_for_mentions(&comment.content)
|
|
|
|
.into_iter()
|
|
|
|
// Filter only the non-local ones
|
2021-09-22 15:57:09 +00:00
|
|
|
.filter(|m| !m.is_local(&context.settings().hostname))
|
2021-07-31 14:57:37 +00:00
|
|
|
.collect::<Vec<MentionData>>();
|
|
|
|
|
|
|
|
for mention in &mentions {
|
|
|
|
// TODO should it be fetching it every time?
|
2021-09-22 15:57:09 +00:00
|
|
|
if let Ok(actor_id) = fetch_webfinger_url(mention, context).await {
|
2021-10-18 21:36:44 +00:00
|
|
|
let actor_id: ObjectId<ApubPerson> = ObjectId::new(actor_id);
|
2021-07-31 14:57:37 +00:00
|
|
|
debug!("mention actor_id: {}", actor_id);
|
2021-10-18 21:36:44 +00:00
|
|
|
addressed_ccs.push(actor_id.to_string().parse()?);
|
2021-07-31 14:57:37 +00:00
|
|
|
|
2021-09-25 15:44:52 +00:00
|
|
|
let mention_person = actor_id.dereference(context, &mut 0).await?;
|
2021-10-06 20:20:05 +00:00
|
|
|
inboxes.push(mention_person.shared_inbox_or_inbox_url());
|
2021-07-31 14:57:37 +00:00
|
|
|
|
|
|
|
let mut mention_tag = Mention::new();
|
2021-09-25 15:44:52 +00:00
|
|
|
mention_tag
|
|
|
|
.set_href(actor_id.into())
|
|
|
|
.set_name(mention.full_name());
|
2021-07-31 14:57:37 +00:00
|
|
|
tags.push(mention_tag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let inboxes = inboxes.into_iter().unique().collect();
|
|
|
|
|
|
|
|
Ok(MentionsAndAddresses {
|
|
|
|
ccs: addressed_ccs,
|
|
|
|
inboxes,
|
|
|
|
tags,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the apub ID of the person this comment is responding to. Meaning, in case this is a
|
|
|
|
/// top-level comment, the creator of the post, otherwise the creator of the parent comment.
|
|
|
|
async fn get_comment_parent_creator(
|
|
|
|
pool: &DbPool,
|
|
|
|
comment: &Comment,
|
2021-10-18 21:36:44 +00:00
|
|
|
) -> Result<ApubPerson, LemmyError> {
|
2021-07-31 14:57:37 +00:00
|
|
|
let parent_creator_id = if let Some(parent_comment_id) = comment.parent_id {
|
|
|
|
let parent_comment =
|
|
|
|
blocking(pool, move |conn| Comment::read(conn, parent_comment_id)).await??;
|
|
|
|
parent_comment.creator_id
|
|
|
|
} else {
|
|
|
|
let parent_post_id = comment.post_id;
|
|
|
|
let parent_post = blocking(pool, move |conn| Post::read(conn, parent_post_id)).await??;
|
|
|
|
parent_post.creator_id
|
|
|
|
};
|
2021-10-18 21:36:44 +00:00
|
|
|
Ok(
|
|
|
|
blocking(pool, move |conn| Person::read(conn, parent_creator_id))
|
|
|
|
.await??
|
|
|
|
.into(),
|
|
|
|
)
|
2021-07-31 14:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Turns a person id like `@name@example.com` into an apub ID, like `https://example.com/user/name`,
|
|
|
|
/// using webfinger.
|
2021-09-22 15:57:09 +00:00
|
|
|
async fn fetch_webfinger_url(
|
|
|
|
mention: &MentionData,
|
|
|
|
context: &LemmyContext,
|
|
|
|
) -> Result<Url, LemmyError> {
|
2021-07-31 14:57:37 +00:00
|
|
|
let fetch_url = format!(
|
|
|
|
"{}://{}/.well-known/webfinger?resource=acct:{}@{}",
|
2021-09-22 15:57:09 +00:00
|
|
|
context.settings().get_protocol_string(),
|
2021-07-31 14:57:37 +00:00
|
|
|
mention.domain,
|
|
|
|
mention.name,
|
|
|
|
mention.domain
|
|
|
|
);
|
|
|
|
debug!("Fetching webfinger url: {}", &fetch_url);
|
|
|
|
|
2021-09-22 15:57:09 +00:00
|
|
|
let response = retry(|| context.client().get(&fetch_url).send()).await?;
|
2021-07-31 14:57:37 +00:00
|
|
|
|
2021-07-20 07:00:20 +00:00
|
|
|
let res: WebfingerResponse = response
|
2021-07-31 14:57:37 +00:00
|
|
|
.json()
|
|
|
|
.await
|
|
|
|
.map_err(|e| RecvError(e.to_string()))?;
|
|
|
|
|
|
|
|
let link = res
|
|
|
|
.links
|
|
|
|
.iter()
|
|
|
|
.find(|l| l.type_.eq(&Some("application/activity+json".to_string())))
|
|
|
|
.ok_or_else(|| anyhow!("No application/activity+json link found."))?;
|
|
|
|
link
|
|
|
|
.href
|
|
|
|
.to_owned()
|
|
|
|
.ok_or_else(|| anyhow!("No href found.").into())
|
|
|
|
}
|