2020-05-16 14:04:08 +00:00
|
|
|
use crate::{
|
2020-10-12 14:10:09 +00:00
|
|
|
activities::send::generate_activity_id,
|
2020-09-30 16:19:14 +00:00
|
|
|
activity_queue::{send_comment_mentions, send_to_community},
|
2020-10-12 14:10:09 +00:00
|
|
|
fetcher::get_or_fetch_and_upsert_user,
|
2020-09-24 13:53:21 +00:00
|
|
|
ActorType,
|
|
|
|
ApubLikeableType,
|
|
|
|
ApubObjectType,
|
|
|
|
ToApub,
|
2020-05-16 14:04:08 +00:00
|
|
|
};
|
2020-08-01 13:25:17 +00:00
|
|
|
use activitystreams::{
|
2020-07-28 16:47:26 +00:00
|
|
|
activity::{
|
|
|
|
kind::{CreateType, DeleteType, DislikeType, LikeType, RemoveType, UndoType, UpdateType},
|
2020-07-29 11:46:11 +00:00
|
|
|
Create,
|
|
|
|
Delete,
|
|
|
|
Dislike,
|
|
|
|
Like,
|
|
|
|
Remove,
|
|
|
|
Undo,
|
|
|
|
Update,
|
2020-07-28 16:47:26 +00:00
|
|
|
},
|
2020-07-14 14:09:13 +00:00
|
|
|
base::AnyBase,
|
2020-05-17 01:09:26 +00:00
|
|
|
link::Mention,
|
2020-07-14 14:09:13 +00:00
|
|
|
prelude::*,
|
|
|
|
public,
|
2020-05-16 14:04:08 +00:00
|
|
|
};
|
2020-10-12 14:10:09 +00:00
|
|
|
use anyhow::anyhow;
|
2020-05-17 01:09:26 +00:00
|
|
|
use itertools::Itertools;
|
2020-10-12 14:10:09 +00:00
|
|
|
use lemmy_db::{comment::Comment, community::Community, post::Post, user::User_, Crud};
|
|
|
|
use lemmy_structs::{blocking, WebFingerResponse};
|
2020-08-11 14:31:05 +00:00
|
|
|
use lemmy_utils::{
|
2020-10-12 14:10:09 +00:00
|
|
|
request::{retry, RecvError},
|
|
|
|
settings::Settings,
|
|
|
|
utils::{scrape_text_for_mentions, MentionData},
|
2020-09-01 14:25:34 +00:00
|
|
|
LemmyError,
|
2020-08-11 14:31:05 +00:00
|
|
|
};
|
2020-09-24 13:53:21 +00:00
|
|
|
use lemmy_websocket::LemmyContext;
|
2020-05-17 01:09:26 +00:00
|
|
|
use log::debug;
|
2020-10-12 14:10:09 +00:00
|
|
|
use reqwest::Client;
|
2020-07-14 14:09:13 +00:00
|
|
|
use serde_json::Error;
|
2020-07-17 21:11:07 +00:00
|
|
|
use url::Url;
|
2020-04-27 16:57:00 +00:00
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
2020-04-27 16:57:00 +00:00
|
|
|
impl ApubObjectType for Comment {
|
2020-10-19 14:29:35 +00:00
|
|
|
/// Send out information about a newly created comment, to the followers of the community and
|
|
|
|
/// mentioned users.
|
2020-08-18 13:43:50 +00:00
|
|
|
async fn send_create(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
|
|
|
|
let note = self.to_apub(context.pool()).await?;
|
2020-04-28 04:16:02 +00:00
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
let post_id = self.post_id;
|
2020-08-18 13:43:50 +00:00
|
|
|
let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
|
2020-05-15 16:36:11 +00:00
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
let community_id = post.community_id;
|
2020-08-18 13:43:50 +00:00
|
|
|
let community = blocking(context.pool(), move |conn| {
|
|
|
|
Community::read(conn, community_id)
|
|
|
|
})
|
|
|
|
.await??;
|
2020-07-01 12:54:29 +00:00
|
|
|
|
2020-10-12 16:02:28 +00:00
|
|
|
let mut maa =
|
|
|
|
collect_non_local_mentions_and_addresses(&self.content, &community, context).await?;
|
|
|
|
let mut ccs = vec![community.actor_id()?];
|
|
|
|
ccs.append(&mut maa.addressed_ccs);
|
2020-07-01 12:54:29 +00:00
|
|
|
|
2020-07-14 14:09:13 +00:00
|
|
|
let mut create = Create::new(creator.actor_id.to_owned(), note.into_any_base()?);
|
2020-04-27 16:57:00 +00:00
|
|
|
create
|
2020-08-18 13:43:50 +00:00
|
|
|
.set_context(activitystreams::context())
|
2020-07-28 16:47:26 +00:00
|
|
|
.set_id(generate_activity_id(CreateType::Create)?)
|
2020-07-14 14:09:13 +00:00
|
|
|
.set_to(public())
|
2020-10-12 16:02:28 +00:00
|
|
|
.set_many_ccs(ccs)
|
2020-07-14 14:09:13 +00:00
|
|
|
// Set the mention tags
|
|
|
|
.set_many_tags(maa.get_tags()?);
|
2020-04-27 22:17:02 +00:00
|
|
|
|
2020-10-19 14:29:35 +00:00
|
|
|
send_to_community(create.clone(), &creator, &community, context).await?;
|
2020-09-30 16:19:14 +00:00
|
|
|
send_comment_mentions(&creator, maa.inboxes, create, context).await?;
|
2020-04-27 16:57:00 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-10-19 14:29:35 +00:00
|
|
|
/// Send out information about an edited post, to the followers of the community and mentioned
|
|
|
|
/// users.
|
2020-08-18 13:43:50 +00:00
|
|
|
async fn send_update(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
|
|
|
|
let note = self.to_apub(context.pool()).await?;
|
2020-07-01 12:54:29 +00:00
|
|
|
|
|
|
|
let post_id = self.post_id;
|
2020-08-18 13:43:50 +00:00
|
|
|
let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
|
2020-07-01 12:54:29 +00:00
|
|
|
|
|
|
|
let community_id = post.community_id;
|
2020-08-18 13:43:50 +00:00
|
|
|
let community = blocking(context.pool(), move |conn| {
|
|
|
|
Community::read(conn, community_id)
|
|
|
|
})
|
|
|
|
.await??;
|
2020-04-28 04:16:02 +00:00
|
|
|
|
2020-10-12 16:02:28 +00:00
|
|
|
let mut maa =
|
|
|
|
collect_non_local_mentions_and_addresses(&self.content, &community, context).await?;
|
|
|
|
let mut ccs = vec![community.actor_id()?];
|
|
|
|
ccs.append(&mut maa.addressed_ccs);
|
2020-05-15 16:36:11 +00:00
|
|
|
|
2020-07-14 14:09:13 +00:00
|
|
|
let mut update = Update::new(creator.actor_id.to_owned(), note.into_any_base()?);
|
2020-04-27 16:57:00 +00:00
|
|
|
update
|
2020-08-18 13:43:50 +00:00
|
|
|
.set_context(activitystreams::context())
|
2020-07-28 16:47:26 +00:00
|
|
|
.set_id(generate_activity_id(UpdateType::Update)?)
|
2020-07-14 14:09:13 +00:00
|
|
|
.set_to(public())
|
2020-10-12 16:02:28 +00:00
|
|
|
.set_many_ccs(ccs)
|
2020-07-14 14:09:13 +00:00
|
|
|
// Set the mention tags
|
|
|
|
.set_many_tags(maa.get_tags()?);
|
2020-04-27 22:17:02 +00:00
|
|
|
|
2020-10-19 14:29:35 +00:00
|
|
|
send_to_community(update.clone(), &creator, &community, context).await?;
|
2020-09-30 16:19:14 +00:00
|
|
|
send_comment_mentions(&creator, maa.inboxes, update, context).await?;
|
2020-04-27 16:57:00 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2020-04-29 14:51:25 +00:00
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
async fn send_delete(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
|
2020-07-01 12:54:29 +00:00
|
|
|
let post_id = self.post_id;
|
2020-08-18 13:43:50 +00:00
|
|
|
let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
|
2020-07-01 12:54:29 +00:00
|
|
|
|
|
|
|
let community_id = post.community_id;
|
2020-08-18 13:43:50 +00:00
|
|
|
let community = blocking(context.pool(), move |conn| {
|
|
|
|
Community::read(conn, community_id)
|
|
|
|
})
|
|
|
|
.await??;
|
2020-07-01 12:54:29 +00:00
|
|
|
|
2020-10-13 12:09:49 +00:00
|
|
|
let mut delete = Delete::new(creator.actor_id.to_owned(), Url::parse(&self.ap_id)?);
|
2020-04-29 14:51:25 +00:00
|
|
|
delete
|
2020-08-18 13:43:50 +00:00
|
|
|
.set_context(activitystreams::context())
|
2020-07-28 16:47:26 +00:00
|
|
|
.set_id(generate_activity_id(DeleteType::Delete)?)
|
2020-07-14 14:09:13 +00:00
|
|
|
.set_to(public())
|
2020-10-12 16:02:28 +00:00
|
|
|
.set_many_ccs(vec![community.actor_id()?]);
|
2020-04-29 14:51:25 +00:00
|
|
|
|
2020-10-19 14:29:35 +00:00
|
|
|
send_to_community(delete, &creator, &community, context).await?;
|
2020-04-29 14:51:25 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2020-05-01 19:01:29 +00:00
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
async fn send_undo_delete(
|
|
|
|
&self,
|
|
|
|
creator: &User_,
|
2020-08-18 13:43:50 +00:00
|
|
|
context: &LemmyContext,
|
2020-07-01 12:54:29 +00:00
|
|
|
) -> Result<(), LemmyError> {
|
|
|
|
let post_id = self.post_id;
|
2020-08-18 13:43:50 +00:00
|
|
|
let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
|
2020-07-01 12:54:29 +00:00
|
|
|
|
|
|
|
let community_id = post.community_id;
|
2020-08-18 13:43:50 +00:00
|
|
|
let community = blocking(context.pool(), move |conn| {
|
|
|
|
Community::read(conn, community_id)
|
|
|
|
})
|
|
|
|
.await??;
|
2020-05-01 19:01:29 +00:00
|
|
|
|
|
|
|
// Generate a fake delete activity, with the correct object
|
2020-10-13 12:09:49 +00:00
|
|
|
let mut delete = Delete::new(creator.actor_id.to_owned(), Url::parse(&self.ap_id)?);
|
2020-05-01 19:01:29 +00:00
|
|
|
delete
|
2020-08-18 13:43:50 +00:00
|
|
|
.set_context(activitystreams::context())
|
2020-07-28 16:47:26 +00:00
|
|
|
.set_id(generate_activity_id(DeleteType::Delete)?)
|
2020-07-14 14:09:13 +00:00
|
|
|
.set_to(public())
|
2020-10-12 16:02:28 +00:00
|
|
|
.set_many_ccs(vec![community.actor_id()?]);
|
2020-05-01 19:01:29 +00:00
|
|
|
|
|
|
|
// Undo that fake activity
|
2020-07-14 14:09:13 +00:00
|
|
|
let mut undo = Undo::new(creator.actor_id.to_owned(), delete.into_any_base()?);
|
2020-05-01 19:01:29 +00:00
|
|
|
undo
|
2020-08-18 13:43:50 +00:00
|
|
|
.set_context(activitystreams::context())
|
2020-07-28 16:47:26 +00:00
|
|
|
.set_id(generate_activity_id(UndoType::Undo)?)
|
2020-07-14 14:09:13 +00:00
|
|
|
.set_to(public())
|
2020-10-12 16:02:28 +00:00
|
|
|
.set_many_ccs(vec![community.actor_id()?]);
|
2020-05-01 19:01:29 +00:00
|
|
|
|
2020-10-19 14:29:35 +00:00
|
|
|
send_to_community(undo, &creator, &community, context).await?;
|
2020-05-01 19:01:29 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2020-05-03 14:00:59 +00:00
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
async fn send_remove(&self, mod_: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
|
2020-07-01 12:54:29 +00:00
|
|
|
let post_id = self.post_id;
|
2020-08-18 13:43:50 +00:00
|
|
|
let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
|
2020-07-01 12:54:29 +00:00
|
|
|
|
|
|
|
let community_id = post.community_id;
|
2020-08-18 13:43:50 +00:00
|
|
|
let community = blocking(context.pool(), move |conn| {
|
|
|
|
Community::read(conn, community_id)
|
|
|
|
})
|
|
|
|
.await??;
|
2020-07-01 12:54:29 +00:00
|
|
|
|
2020-10-13 12:09:49 +00:00
|
|
|
let mut remove = Remove::new(mod_.actor_id.to_owned(), Url::parse(&self.ap_id)?);
|
2020-05-03 14:00:59 +00:00
|
|
|
remove
|
2020-08-18 13:43:50 +00:00
|
|
|
.set_context(activitystreams::context())
|
2020-07-28 16:47:26 +00:00
|
|
|
.set_id(generate_activity_id(RemoveType::Remove)?)
|
2020-07-14 14:09:13 +00:00
|
|
|
.set_to(public())
|
2020-10-12 16:02:28 +00:00
|
|
|
.set_many_ccs(vec![community.actor_id()?]);
|
2020-05-03 14:00:59 +00:00
|
|
|
|
2020-10-19 14:29:35 +00:00
|
|
|
send_to_community(remove, &mod_, &community, context).await?;
|
2020-05-03 14:00:59 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
async fn send_undo_remove(&self, mod_: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
|
2020-07-01 12:54:29 +00:00
|
|
|
let post_id = self.post_id;
|
2020-08-18 13:43:50 +00:00
|
|
|
let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
|
2020-07-01 12:54:29 +00:00
|
|
|
|
|
|
|
let community_id = post.community_id;
|
2020-08-18 13:43:50 +00:00
|
|
|
let community = blocking(context.pool(), move |conn| {
|
|
|
|
Community::read(conn, community_id)
|
|
|
|
})
|
|
|
|
.await??;
|
2020-05-03 14:00:59 +00:00
|
|
|
|
|
|
|
// Generate a fake delete activity, with the correct object
|
2020-10-13 12:09:49 +00:00
|
|
|
let mut remove = Remove::new(mod_.actor_id.to_owned(), Url::parse(&self.ap_id)?);
|
2020-05-03 14:00:59 +00:00
|
|
|
remove
|
2020-08-18 13:43:50 +00:00
|
|
|
.set_context(activitystreams::context())
|
2020-07-28 16:47:26 +00:00
|
|
|
.set_id(generate_activity_id(RemoveType::Remove)?)
|
2020-07-14 14:09:13 +00:00
|
|
|
.set_to(public())
|
2020-10-12 16:02:28 +00:00
|
|
|
.set_many_ccs(vec![community.actor_id()?]);
|
2020-05-03 14:00:59 +00:00
|
|
|
|
|
|
|
// Undo that fake activity
|
2020-07-14 14:09:13 +00:00
|
|
|
let mut undo = Undo::new(mod_.actor_id.to_owned(), remove.into_any_base()?);
|
2020-05-03 14:00:59 +00:00
|
|
|
undo
|
2020-08-18 13:43:50 +00:00
|
|
|
.set_context(activitystreams::context())
|
2020-07-28 16:47:26 +00:00
|
|
|
.set_id(generate_activity_id(UndoType::Undo)?)
|
2020-07-14 14:09:13 +00:00
|
|
|
.set_to(public())
|
2020-10-12 16:02:28 +00:00
|
|
|
.set_many_ccs(vec![community.actor_id()?]);
|
2020-05-03 14:00:59 +00:00
|
|
|
|
2020-10-19 14:29:35 +00:00
|
|
|
send_to_community(undo, &mod_, &community, context).await?;
|
2020-05-03 14:00:59 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2020-04-27 16:57:00 +00:00
|
|
|
}
|
2020-04-28 04:16:02 +00:00
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
2020-04-28 04:16:02 +00:00
|
|
|
impl ApubLikeableType for Comment {
|
2020-08-18 13:43:50 +00:00
|
|
|
async fn send_like(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
|
|
|
|
let note = self.to_apub(context.pool()).await?;
|
2020-07-01 12:54:29 +00:00
|
|
|
|
|
|
|
let post_id = self.post_id;
|
2020-08-18 13:43:50 +00:00
|
|
|
let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
|
2020-07-01 12:54:29 +00:00
|
|
|
|
|
|
|
let community_id = post.community_id;
|
2020-08-18 13:43:50 +00:00
|
|
|
let community = blocking(context.pool(), move |conn| {
|
|
|
|
Community::read(conn, community_id)
|
|
|
|
})
|
|
|
|
.await??;
|
2020-07-01 12:54:29 +00:00
|
|
|
|
2020-07-14 14:09:13 +00:00
|
|
|
let mut like = Like::new(creator.actor_id.to_owned(), note.into_any_base()?);
|
2020-04-28 04:16:02 +00:00
|
|
|
like
|
2020-08-18 13:43:50 +00:00
|
|
|
.set_context(activitystreams::context())
|
2020-07-28 16:47:26 +00:00
|
|
|
.set_id(generate_activity_id(LikeType::Like)?)
|
2020-07-14 14:09:13 +00:00
|
|
|
.set_to(public())
|
2020-10-12 16:02:28 +00:00
|
|
|
.set_many_ccs(vec![community.actor_id()?]);
|
2020-04-28 04:16:02 +00:00
|
|
|
|
2020-10-19 14:29:35 +00:00
|
|
|
send_to_community(like, &creator, &community, context).await?;
|
2020-04-28 04:16:02 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
async fn send_dislike(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
|
|
|
|
let note = self.to_apub(context.pool()).await?;
|
2020-07-01 12:54:29 +00:00
|
|
|
|
|
|
|
let post_id = self.post_id;
|
2020-08-18 13:43:50 +00:00
|
|
|
let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
|
2020-07-01 12:54:29 +00:00
|
|
|
|
|
|
|
let community_id = post.community_id;
|
2020-08-18 13:43:50 +00:00
|
|
|
let community = blocking(context.pool(), move |conn| {
|
|
|
|
Community::read(conn, community_id)
|
|
|
|
})
|
|
|
|
.await??;
|
2020-07-01 12:54:29 +00:00
|
|
|
|
2020-07-14 14:09:13 +00:00
|
|
|
let mut dislike = Dislike::new(creator.actor_id.to_owned(), note.into_any_base()?);
|
2020-04-28 04:16:02 +00:00
|
|
|
dislike
|
2020-08-18 13:43:50 +00:00
|
|
|
.set_context(activitystreams::context())
|
2020-07-28 16:47:26 +00:00
|
|
|
.set_id(generate_activity_id(DislikeType::Dislike)?)
|
2020-07-14 14:09:13 +00:00
|
|
|
.set_to(public())
|
2020-10-12 16:02:28 +00:00
|
|
|
.set_many_ccs(vec![community.actor_id()?]);
|
2020-04-28 04:16:02 +00:00
|
|
|
|
2020-10-19 14:29:35 +00:00
|
|
|
send_to_community(dislike, &creator, &community, context).await?;
|
2020-04-28 04:16:02 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2020-05-04 00:34:04 +00:00
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
async fn send_undo_like(
|
|
|
|
&self,
|
|
|
|
creator: &User_,
|
2020-08-18 13:43:50 +00:00
|
|
|
context: &LemmyContext,
|
2020-07-01 12:54:29 +00:00
|
|
|
) -> Result<(), LemmyError> {
|
2020-08-18 13:43:50 +00:00
|
|
|
let note = self.to_apub(context.pool()).await?;
|
2020-07-01 12:54:29 +00:00
|
|
|
|
|
|
|
let post_id = self.post_id;
|
2020-08-18 13:43:50 +00:00
|
|
|
let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
|
2020-07-01 12:54:29 +00:00
|
|
|
|
|
|
|
let community_id = post.community_id;
|
2020-08-18 13:43:50 +00:00
|
|
|
let community = blocking(context.pool(), move |conn| {
|
|
|
|
Community::read(conn, community_id)
|
|
|
|
})
|
|
|
|
.await??;
|
2020-07-01 12:54:29 +00:00
|
|
|
|
2020-07-14 14:09:13 +00:00
|
|
|
let mut like = Like::new(creator.actor_id.to_owned(), note.into_any_base()?);
|
2020-05-04 00:34:04 +00:00
|
|
|
like
|
2020-08-18 13:43:50 +00:00
|
|
|
.set_context(activitystreams::context())
|
2020-07-28 16:47:26 +00:00
|
|
|
.set_id(generate_activity_id(DislikeType::Dislike)?)
|
2020-07-14 14:09:13 +00:00
|
|
|
.set_to(public())
|
2020-10-12 16:02:28 +00:00
|
|
|
.set_many_ccs(vec![community.actor_id()?]);
|
2020-05-04 00:34:04 +00:00
|
|
|
|
|
|
|
// Undo that fake activity
|
2020-07-14 14:09:13 +00:00
|
|
|
let mut undo = Undo::new(creator.actor_id.to_owned(), like.into_any_base()?);
|
2020-05-04 00:34:04 +00:00
|
|
|
undo
|
2020-08-18 13:43:50 +00:00
|
|
|
.set_context(activitystreams::context())
|
2020-07-28 16:47:26 +00:00
|
|
|
.set_id(generate_activity_id(UndoType::Undo)?)
|
2020-07-14 14:09:13 +00:00
|
|
|
.set_to(public())
|
2020-10-12 16:02:28 +00:00
|
|
|
.set_many_ccs(vec![community.actor_id()?]);
|
2020-05-04 00:34:04 +00:00
|
|
|
|
2020-10-19 14:29:35 +00:00
|
|
|
send_to_community(undo, &creator, &community, context).await?;
|
2020-05-04 00:34:04 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2020-04-28 04:16:02 +00:00
|
|
|
}
|
2020-05-15 16:36:11 +00:00
|
|
|
|
|
|
|
struct MentionsAndAddresses {
|
2020-08-12 14:43:45 +00:00
|
|
|
addressed_ccs: Vec<Url>,
|
2020-08-11 14:31:05 +00:00
|
|
|
inboxes: Vec<Url>,
|
2020-05-15 16:36:11 +00:00
|
|
|
tags: Vec<Mention>,
|
|
|
|
}
|
|
|
|
|
2020-07-14 14:09:13 +00:00
|
|
|
impl MentionsAndAddresses {
|
|
|
|
fn get_tags(&self) -> Result<Vec<AnyBase>, Error> {
|
|
|
|
self
|
|
|
|
.tags
|
|
|
|
.iter()
|
|
|
|
.map(|t| t.to_owned().into_any_base())
|
|
|
|
.collect::<Result<Vec<AnyBase>, Error>>()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-15 16:36:11 +00:00
|
|
|
/// 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 users / addresses that go in the cc field.
|
2020-07-01 12:54:29 +00:00
|
|
|
async fn collect_non_local_mentions_and_addresses(
|
2020-05-15 16:36:11 +00:00
|
|
|
content: &str,
|
|
|
|
community: &Community,
|
2020-08-18 13:43:50 +00:00
|
|
|
context: &LemmyContext,
|
2020-07-01 12:54:29 +00:00
|
|
|
) -> Result<MentionsAndAddresses, LemmyError> {
|
2020-08-12 14:43:45 +00:00
|
|
|
let mut addressed_ccs = vec![community.get_followers_url()?];
|
2020-05-15 16:36:11 +00:00
|
|
|
|
|
|
|
// Add the mention tag
|
|
|
|
let mut tags = Vec::new();
|
|
|
|
|
|
|
|
// Get the inboxes for any mentions
|
|
|
|
let mentions = scrape_text_for_mentions(&content)
|
|
|
|
.into_iter()
|
|
|
|
// Filter only the non-local ones
|
|
|
|
.filter(|m| !m.is_local())
|
|
|
|
.collect::<Vec<MentionData>>();
|
2020-07-01 12:54:29 +00:00
|
|
|
|
2020-08-11 14:31:05 +00:00
|
|
|
let mut mention_inboxes: Vec<Url> = Vec::new();
|
2020-05-15 16:36:11 +00:00
|
|
|
for mention in &mentions {
|
|
|
|
// TODO should it be fetching it every time?
|
2020-08-18 13:43:50 +00:00
|
|
|
if let Ok(actor_id) = fetch_webfinger_url(mention, context.client()).await {
|
2020-05-15 16:36:11 +00:00
|
|
|
debug!("mention actor_id: {}", actor_id);
|
2020-08-12 14:43:45 +00:00
|
|
|
addressed_ccs.push(actor_id.to_owned().to_string().parse()?);
|
2020-07-01 12:54:29 +00:00
|
|
|
|
2020-10-22 18:27:32 +00:00
|
|
|
let mention_user = get_or_fetch_and_upsert_user(&actor_id, context, &mut 0).await?;
|
2020-08-11 14:31:05 +00:00
|
|
|
let shared_inbox = mention_user.get_shared_inbox_url()?;
|
2020-07-01 12:54:29 +00:00
|
|
|
|
2020-05-15 16:36:11 +00:00
|
|
|
mention_inboxes.push(shared_inbox);
|
|
|
|
let mut mention_tag = Mention::new();
|
2020-07-14 14:09:13 +00:00
|
|
|
mention_tag.set_href(actor_id).set_name(mention.full_name());
|
2020-05-15 16:36:11 +00:00
|
|
|
tags.push(mention_tag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-06 16:28:31 +00:00
|
|
|
let inboxes = mention_inboxes.into_iter().unique().collect();
|
2020-05-15 16:36:11 +00:00
|
|
|
|
|
|
|
Ok(MentionsAndAddresses {
|
|
|
|
addressed_ccs,
|
|
|
|
inboxes,
|
|
|
|
tags,
|
|
|
|
})
|
|
|
|
}
|
2020-10-12 14:10:09 +00:00
|
|
|
|
|
|
|
async fn fetch_webfinger_url(mention: &MentionData, client: &Client) -> Result<Url, LemmyError> {
|
|
|
|
let fetch_url = format!(
|
|
|
|
"{}://{}/.well-known/webfinger?resource=acct:{}@{}",
|
|
|
|
Settings::get().get_protocol_string(),
|
|
|
|
mention.domain,
|
|
|
|
mention.name,
|
|
|
|
mention.domain
|
|
|
|
);
|
|
|
|
debug!("Fetching webfinger url: {}", &fetch_url);
|
|
|
|
|
|
|
|
let response = retry(|| 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()
|
|
|
|
.map(|u| Url::parse(&u))
|
|
|
|
.transpose()?
|
|
|
|
.ok_or_else(|| anyhow!("No href found.").into())
|
|
|
|
}
|