Create and Note always need to tag parent creator, for mastodon notifications

This commit is contained in:
Felix Ableitner 2021-11-15 23:54:25 +01:00
parent c1f1b8aa0f
commit 7f4a773b88
14 changed files with 208 additions and 153 deletions

View file

@ -152,6 +152,7 @@ impl PerformCrud for CreateComment {
&local_user_view.person.clone().into(),
CreateOrUpdateType::Create,
context,
&mut 0,
)
.await?;
let object = PostOrComment::Comment(Box::new(apub_comment));

View file

@ -91,6 +91,7 @@ impl PerformCrud for EditComment {
&local_user_view.person.into(),
CreateOrUpdateType::Update,
context,
&mut 0,
)
.await?;

View file

@ -23,7 +23,13 @@
"http://enterprise.lemmy.ml/c/main",
"http://ds9.lemmy.ml/u/lemmy_alpha"
],
"tag": [],
"tag": [
{
"href": "http://ds9.lemmy.ml/u/lemmy_alpha",
"type": "Mention",
"name": "@lemmy_alpha@ds9.lemmy.ml"
}
],
"type": "Create",
"id": "http://ds9.lemmy.ml/activities/create/1e77d67c-44ac-45ed-bf2a-460e21f60236"
}

View file

@ -3,6 +3,10 @@
"type": "Note",
"attributedTo": "https://enterprise.lemmy.ml/u/picard",
"to": ["https://www.w3.org/ns/activitystreams#Public"],
"cc": [
"https://enterprise.lemmy.ml/c/tenforward",
"https://enterprise.lemmy.ml/u/picard"
],
"inReplyTo": "https://enterprise.lemmy.ml/post/55143",
"content": "<p>first comment!</p>\n",
"mediaType": "text/html",
@ -10,6 +14,13 @@
"content": "first comment!",
"mediaType": "text/markdown"
},
"tag": [
{
"href": "https://enterprise.lemmy.ml/u/picard",
"type": "Mention",
"name": "@picard@enterprise.lemmy.ml"
}
],
"published": "2021-03-01T13:42:43.966208+00:00",
"updated": "2021-03-01T13:43:03.955787+00:00"
}

View file

@ -16,10 +16,12 @@
"content": "<p>So does this federate now?</p>",
"inReplyTo": "https://ds9.lemmy.ml/post/1723",
"published": "2021-11-09T11:42:35Z",
"tag": {
"type": "Mention",
"href": "https://ds9.lemmy.ml/u/nutomic"
},
"tag": [
{
"type": "Mention",
"href": "https://ds9.lemmy.ml/u/nutomic"
}
],
"url": "https://friends.grishka.me/posts/66561",
"to": [
"https://www.w3.org/ns/activitystreams#Public"

View file

@ -5,10 +5,12 @@
"content": "<p>So does this federate now?</p>",
"inReplyTo": "https://ds9.lemmy.ml/post/1723",
"published": "2021-11-09T11:42:35Z",
"tag": {
"type": "Mention",
"href": "https://ds9.lemmy.ml/u/nutomic"
},
"tag": [
{
"type": "Mention",
"href": "https://ds9.lemmy.ml/u/nutomic"
}
],
"url": "https://friends.grishka.me/posts/66561",
"to": [
"https://www.w3.org/ns/activitystreams#Public"

View file

@ -1,7 +1,7 @@
use crate::{
activities::{
check_community_deleted_or_removed,
comment::{collect_non_local_mentions, get_notif_recipients},
comment::get_notif_recipients,
community::{announce::GetCommunity, send_activity_in_community},
generate_activity_id,
verify_activity,
@ -12,7 +12,7 @@ use crate::{
objects::{comment::ApubComment, community::ApubCommunity, person::ApubPerson},
protocol::activities::{create_or_update::comment::CreateOrUpdateComment, CreateOrUpdateType},
};
use activitystreams::public;
use activitystreams::{link::LinkExt, public};
use lemmy_api_common::{blocking, check_post_deleted_or_removed};
use lemmy_apub_lib::{
data::Data,
@ -33,6 +33,7 @@ impl CreateOrUpdateComment {
actor: &ApubPerson,
kind: CreateOrUpdateType,
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
// TODO: might be helpful to add a comment method to retrieve community directly
let post_id = comment.post_id;
@ -48,21 +49,34 @@ impl CreateOrUpdateComment {
kind.clone(),
&context.settings().get_protocol_and_hostname(),
)?;
let maa = collect_non_local_mentions(&comment, &community, context).await?;
let note = comment.into_apub(context).await?;
let create_or_update = CreateOrUpdateComment {
actor: ObjectId::new(actor.actor_id()),
to: vec![public()],
object: comment.into_apub(context).await?,
cc: maa.ccs,
tag: maa.tags,
cc: note.cc.clone(),
tag: note.tag.clone(),
object: note,
kind,
id: id.clone(),
unparsed: Default::default(),
};
let tagged_users: Vec<ObjectId<ApubPerson>> = create_or_update
.tag
.iter()
.map(|t| t.href())
.flatten()
.map(|t| ObjectId::new(t.clone()))
.collect();
let mut inboxes = vec![];
for t in tagged_users {
let person = t.dereference(context, request_counter).await?;
inboxes.push(person.shared_inbox_or_inbox_url());
}
let activity = AnnouncableActivities::CreateOrUpdateComment(create_or_update);
send_activity_in_community(activity, &id, actor, &community, maa.inboxes, context).await
send_activity_in_community(activity, &id, actor, &community, inboxes, context).await
}
}

View file

@ -1,32 +1,14 @@
use activitystreams::{
base::BaseExt,
link::{LinkExt, Mention},
};
use anyhow::anyhow;
use itertools::Itertools;
use log::debug;
use url::Url;
use crate::objects::person::ApubPerson;
use lemmy_api_common::blocking;
use lemmy_apub_lib::{object_id::ObjectId, traits::ActorType};
use lemmy_apub_lib::object_id::ObjectId;
use lemmy_db_schema::{
newtypes::LocalUserId,
source::{comment::Comment, person::Person, post::Post},
source::{comment::Comment, post::Post},
traits::Crud,
DbPool,
};
use lemmy_utils::{
request::{retry, RecvError},
utils::{scrape_text_for_mentions, MentionData},
LemmyError,
};
use lemmy_utils::{utils::scrape_text_for_mentions, LemmyError};
use lemmy_websocket::{send::send_local_notifs, LemmyContext};
use crate::{
fetcher::webfinger::WebfingerResponse,
objects::{comment::ApubComment, community::ApubCommunity, person::ApubPerson},
};
pub mod create_or_update;
async fn get_notif_recipients(
@ -47,114 +29,3 @@ async fn get_notif_recipients(
let mentions = scrape_text_for_mentions(&comment.content);
send_local_notifs(mentions, comment, &*actor, &post, true, context).await
}
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(
comment: &ApubComment,
community: &ApubCommunity,
context: &LemmyContext,
) -> Result<MentionsAndAddresses, LemmyError> {
let parent_creator = get_comment_parent_creator(context.pool(), comment).await?;
let mut addressed_ccs: Vec<Url> = vec![community.actor_id(), parent_creator.actor_id()];
// Note: dont include community inbox here, as we send to it separately with `send_to_community()`
let mut inboxes = vec![parent_creator.shared_inbox_or_inbox_url()];
// 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
.filter(|m| !m.is_local(&context.settings().hostname))
.collect::<Vec<MentionData>>();
for mention in &mentions {
// TODO should it be fetching it every time?
if let Ok(actor_id) = fetch_webfinger_url(mention, context).await {
let actor_id: ObjectId<ApubPerson> = ObjectId::new(actor_id);
debug!("mention actor_id: {}", actor_id);
addressed_ccs.push(actor_id.to_string().parse()?);
let mention_person = actor_id.dereference(context, &mut 0).await?;
inboxes.push(mention_person.shared_inbox_or_inbox_url());
let mut mention_tag = Mention::new();
mention_tag
.set_href(actor_id.into())
.set_name(mention.full_name());
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,
) -> Result<ApubPerson, LemmyError> {
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
};
Ok(
blocking(pool, move |conn| Person::read(conn, parent_creator_id))
.await??
.into(),
)
}
/// Turns a person id like `@name@example.com` into an apub ID, like `https://example.com/user/name`,
/// using webfinger.
async fn fetch_webfinger_url(
mention: &MentionData,
context: &LemmyContext,
) -> Result<Url, LemmyError> {
let fetch_url = format!(
"{}://{}/.well-known/webfinger?resource=acct:{}@{}",
context.settings().get_protocol_string(),
mention.domain,
mention.name,
mention.domain
);
debug!("Fetching webfinger url: {}", &fetch_url);
let response = retry(|| context.client().get(&fetch_url).send()).await?;
let res: WebfingerResponse = response
.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())
}

View file

@ -12,6 +12,7 @@ pub(crate) mod collections;
mod context;
pub mod fetcher;
pub mod http;
pub(crate) mod mentions;
pub mod migrations;
pub mod objects;
pub mod protocol;

134
crates/apub/src/mentions.rs Normal file
View file

@ -0,0 +1,134 @@
use crate::{
fetcher::webfinger::WebfingerResponse,
objects::{comment::ApubComment, community::ApubCommunity, person::ApubPerson},
};
use activitystreams::{
base::BaseExt,
link::{LinkExt, Mention},
};
use anyhow::anyhow;
use lemmy_api_common::blocking;
use lemmy_apub_lib::{object_id::ObjectId, traits::ActorType};
use lemmy_db_schema::{
source::{comment::Comment, person::Person, post::Post},
traits::Crud,
DbPool,
};
use lemmy_utils::{
request::{retry, RecvError},
utils::{scrape_text_for_mentions, MentionData},
LemmyError,
};
use lemmy_websocket::LemmyContext;
use log::debug;
use url::Url;
pub struct MentionsAndAddresses {
pub ccs: 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(
comment: &ApubComment,
community_id: ObjectId<ApubCommunity>,
context: &LemmyContext,
) -> Result<MentionsAndAddresses, LemmyError> {
let parent_creator = get_comment_parent_creator(context.pool(), comment).await?;
let mut addressed_ccs: Vec<Url> = vec![community_id.into(), parent_creator.actor_id()];
// Add the mention tag
let mut parent_creator_tag = Mention::new();
parent_creator_tag
.set_href(parent_creator.actor_id.clone().into())
.set_name(format!(
"@{}@{}",
&parent_creator.name,
&parent_creator.actor_id().domain().expect("has domain")
));
let mut tags = vec![parent_creator_tag];
// Get the person IDs for any mentions
let mentions = scrape_text_for_mentions(&comment.content)
.into_iter()
// Filter only the non-local ones
.filter(|m| !m.is_local(&context.settings().hostname))
.collect::<Vec<MentionData>>();
for mention in &mentions {
// TODO should it be fetching it every time?
if let Ok(actor_id) = fetch_webfinger_url(mention, context).await {
let actor_id: ObjectId<ApubPerson> = ObjectId::new(actor_id);
debug!("mention actor_id: {}", actor_id);
addressed_ccs.push(actor_id.to_string().parse()?);
let mut mention_tag = Mention::new();
mention_tag
.set_href(actor_id.into())
.set_name(mention.full_name());
tags.push(mention_tag);
}
}
Ok(MentionsAndAddresses {
ccs: addressed_ccs,
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,
) -> Result<ApubPerson, LemmyError> {
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
};
Ok(
blocking(pool, move |conn| Person::read(conn, parent_creator_id))
.await??
.into(),
)
}
/// Turns a person id like `@name@example.com` into an apub ID, like `https://example.com/user/name`,
/// using webfinger.
async fn fetch_webfinger_url(
mention: &MentionData,
context: &LemmyContext,
) -> Result<Url, LemmyError> {
let fetch_url = format!(
"{}://{}/.well-known/webfinger?resource=acct:{}@{}",
context.settings().get_protocol_string(),
mention.domain,
mention.name,
mention.domain
);
debug!("Fetching webfinger url: {}", &fetch_url);
let response = retry(|| context.client().get(&fetch_url).send()).await?;
let res: WebfingerResponse = response
.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())
}

View file

@ -1,6 +1,7 @@
use crate::{
activities::{verify_is_public, verify_person_in_community},
check_is_apub_id_valid,
mentions::collect_non_local_mentions,
protocol::{
objects::{
note::{Note, SourceCompat},
@ -93,6 +94,11 @@ impl ApubObject for ApubComment {
let post_id = self.post_id;
let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
let community_id = post.community_id;
let community = blocking(context.pool(), move |conn| {
Community::read(conn, community_id)
})
.await??;
let in_reply_to = if let Some(comment_id) = self.parent_id {
let parent_comment =
@ -101,13 +107,14 @@ impl ApubObject for ApubComment {
} else {
ObjectId::<PostOrComment>::new(post.ap_id)
};
let maa = collect_non_local_mentions(&self, ObjectId::new(community.actor_id), context).await?;
let note = Note {
r#type: NoteType::Note,
id: ObjectId::new(self.ap_id.clone()),
attributed_to: ObjectId::new(creator.actor_id),
to: vec![public()],
cc: vec![],
cc: maa.ccs,
content: markdown_to_html(&self.content),
media_type: Some(MediaTypeHtml::Html),
source: SourceCompat::Lemmy(Source {
@ -117,6 +124,7 @@ impl ApubObject for ApubComment {
in_reply_to,
published: Some(convert_datetime(self.published)),
updated: self.updated.map(convert_datetime),
tag: maa.tags,
unparsed: Default::default(),
};

View file

@ -14,7 +14,7 @@ mod tests {
#[actix_rt::test]
#[serial]
async fn test_parsey_create_or_update() {
async fn test_parse_create_or_update() {
test_parse_lemmy_item::<CreateOrUpdatePost>(
"assets/lemmy/activities/create_or_update/create_page.json",
);

View file

@ -30,7 +30,9 @@ pub(crate) mod tests {
use serde::{de::DeserializeOwned, Serialize};
use std::collections::HashMap;
pub(crate) fn test_parse_lemmy_item<T: Serialize + DeserializeOwned>(path: &str) -> T {
pub(crate) fn test_parse_lemmy_item<T: Serialize + DeserializeOwned + std::fmt::Debug>(
path: &str,
) -> T {
let parsed = file_to_json_object::<T>(path);
// ensure that no field is ignored when parsing

View file

@ -3,7 +3,7 @@ use crate::{
objects::{comment::ApubComment, person::ApubPerson, post::ApubPost},
protocol::Source,
};
use activitystreams::{object::kind::NoteType, unparsed::Unparsed};
use activitystreams::{link::Mention, object::kind::NoteType, unparsed::Unparsed};
use anyhow::anyhow;
use chrono::{DateTime, FixedOffset};
use lemmy_api_common::blocking;
@ -38,6 +38,8 @@ pub struct Note {
pub(crate) in_reply_to: ObjectId<PostOrComment>,
pub(crate) published: Option<DateTime<FixedOffset>>,
pub(crate) updated: Option<DateTime<FixedOffset>>,
#[serde(default)]
pub(crate) tag: Vec<Mention>,
#[serde(flatten)]
pub(crate) unparsed: Unparsed,
}