2020-05-16 14:04:08 +00:00
|
|
|
use crate::{
|
2020-09-30 16:19:14 +00:00
|
|
|
activity_queue::{send_comment_mentions, send_to_community},
|
2020-09-24 13:53:21 +00:00
|
|
|
check_actor_domain,
|
|
|
|
create_apub_response,
|
|
|
|
create_apub_tombstone_response,
|
|
|
|
create_tombstone,
|
|
|
|
fetch_webfinger_url,
|
|
|
|
fetcher::{
|
|
|
|
get_or_fetch_and_insert_comment,
|
|
|
|
get_or_fetch_and_insert_post,
|
|
|
|
get_or_fetch_and_upsert_user,
|
2020-05-16 14:04:08 +00:00
|
|
|
},
|
2020-09-30 16:19:14 +00:00
|
|
|
generate_activity_id,
|
2020-09-24 13:53:21 +00:00
|
|
|
ActorType,
|
|
|
|
ApubLikeableType,
|
|
|
|
ApubObjectType,
|
|
|
|
FromApub,
|
|
|
|
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
|
|
|
object::{kind::NoteType, Note, Tombstone},
|
|
|
|
prelude::*,
|
|
|
|
public,
|
2020-05-16 14:04:08 +00:00
|
|
|
};
|
2020-08-18 13:43:50 +00:00
|
|
|
use actix_web::{body::Body, web, web::Path, HttpResponse};
|
2020-08-11 14:31:05 +00:00
|
|
|
use anyhow::Context;
|
2020-10-02 12:16:40 +00:00
|
|
|
use diesel::result::Error::NotFound;
|
2020-05-17 01:09:26 +00:00
|
|
|
use itertools::Itertools;
|
2020-07-10 18:15:41 +00:00
|
|
|
use lemmy_db::{
|
|
|
|
comment::{Comment, CommentForm},
|
|
|
|
community::Community,
|
|
|
|
post::Post,
|
|
|
|
user::User_,
|
|
|
|
Crud,
|
2020-09-24 13:53:21 +00:00
|
|
|
DbPool,
|
2020-07-10 18:15:41 +00:00
|
|
|
};
|
2020-09-16 13:31:30 +00:00
|
|
|
use lemmy_structs::blocking;
|
2020-08-11 14:31:05 +00:00
|
|
|
use lemmy_utils::{
|
|
|
|
location_info,
|
2020-09-14 15:29:50 +00:00
|
|
|
utils::{convert_datetime, remove_slurs, 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-06-01 14:17:20 +00:00
|
|
|
use serde::Deserialize;
|
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-05-13 17:21:32 +00:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct CommentQuery {
|
|
|
|
comment_id: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the post json over HTTP.
|
|
|
|
pub async fn get_apub_comment(
|
|
|
|
info: Path<CommentQuery>,
|
2020-08-18 13:43:50 +00:00
|
|
|
context: web::Data<LemmyContext>,
|
2020-07-01 12:54:29 +00:00
|
|
|
) -> Result<HttpResponse<Body>, LemmyError> {
|
2020-05-13 17:21:32 +00:00
|
|
|
let id = info.comment_id.parse::<i32>()?;
|
2020-08-18 13:43:50 +00:00
|
|
|
let comment = blocking(context.pool(), move |conn| Comment::read(conn, id)).await??;
|
2020-10-02 12:16:40 +00:00
|
|
|
if !comment.local {
|
|
|
|
return Err(NotFound.into());
|
|
|
|
}
|
2020-07-01 12:54:29 +00:00
|
|
|
|
2020-05-13 17:21:32 +00:00
|
|
|
if !comment.deleted {
|
2020-08-18 13:43:50 +00:00
|
|
|
Ok(create_apub_response(
|
|
|
|
&comment.to_apub(context.pool()).await?,
|
|
|
|
))
|
2020-05-13 17:21:32 +00:00
|
|
|
} else {
|
|
|
|
Ok(create_apub_tombstone_response(&comment.to_tombstone()?))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
2020-04-27 16:57:00 +00:00
|
|
|
impl ToApub for Comment {
|
|
|
|
type Response = Note;
|
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
async fn to_apub(&self, pool: &DbPool) -> Result<Note, LemmyError> {
|
2020-07-14 14:09:13 +00:00
|
|
|
let mut comment = Note::new();
|
2020-07-01 12:54:29 +00:00
|
|
|
|
|
|
|
let creator_id = self.creator_id;
|
|
|
|
let creator = blocking(pool, move |conn| User_::read(conn, creator_id)).await??;
|
|
|
|
|
|
|
|
let post_id = self.post_id;
|
|
|
|
let post = blocking(pool, move |conn| Post::read(conn, post_id)).await??;
|
|
|
|
|
|
|
|
let community_id = post.community_id;
|
|
|
|
let community = blocking(pool, move |conn| Community::read(conn, community_id)).await??;
|
2020-04-27 16:57:00 +00:00
|
|
|
|
|
|
|
// Add a vector containing some important info to the "in_reply_to" field
|
|
|
|
// [post_ap_id, Option(parent_comment_ap_id)]
|
|
|
|
let mut in_reply_to_vec = vec![post.ap_id];
|
|
|
|
|
|
|
|
if let Some(parent_id) = self.parent_id {
|
2020-07-01 12:54:29 +00:00
|
|
|
let parent_comment = blocking(pool, move |conn| Comment::read(conn, parent_id)).await??;
|
|
|
|
|
2020-04-27 16:57:00 +00:00
|
|
|
in_reply_to_vec.push(parent_comment.ap_id);
|
|
|
|
}
|
|
|
|
|
2020-07-14 14:09:13 +00:00
|
|
|
comment
|
2020-04-27 16:57:00 +00:00
|
|
|
// Not needed when the Post is embedded in a collection (like for community outbox)
|
2020-08-18 13:43:50 +00:00
|
|
|
.set_context(activitystreams::context())
|
2020-07-17 21:11:07 +00:00
|
|
|
.set_id(Url::parse(&self.ap_id)?)
|
|
|
|
.set_published(convert_datetime(self.published))
|
2020-07-14 14:09:13 +00:00
|
|
|
.set_to(community.actor_id)
|
|
|
|
.set_many_in_reply_tos(in_reply_to_vec)
|
|
|
|
.set_content(self.content.to_owned())
|
|
|
|
.set_attributed_to(creator.actor_id);
|
2020-04-27 16:57:00 +00:00
|
|
|
|
|
|
|
if let Some(u) = self.updated {
|
2020-07-17 21:11:07 +00:00
|
|
|
comment.set_updated(convert_datetime(u));
|
2020-04-27 16:57:00 +00:00
|
|
|
}
|
|
|
|
|
2020-04-29 14:51:25 +00:00
|
|
|
Ok(comment)
|
|
|
|
}
|
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
fn to_tombstone(&self) -> Result<Tombstone, LemmyError> {
|
2020-07-28 16:47:26 +00:00
|
|
|
create_tombstone(self.deleted, &self.ap_id, self.updated, NoteType::Note)
|
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 FromApub for CommentForm {
|
|
|
|
type ApubType = Note;
|
|
|
|
|
|
|
|
/// Parse an ActivityPub note received from another instance into a Lemmy comment
|
2020-07-01 12:54:29 +00:00
|
|
|
async fn from_apub(
|
2020-07-13 13:56:58 +00:00
|
|
|
note: &Note,
|
2020-08-18 13:43:50 +00:00
|
|
|
context: &LemmyContext,
|
2020-08-06 12:53:58 +00:00
|
|
|
expected_domain: Option<Url>,
|
2020-07-01 12:54:29 +00:00
|
|
|
) -> Result<CommentForm, LemmyError> {
|
2020-07-14 14:09:13 +00:00
|
|
|
let creator_actor_id = ¬e
|
|
|
|
.attributed_to()
|
2020-08-11 14:31:05 +00:00
|
|
|
.context(location_info!())?
|
2020-07-14 14:09:13 +00:00
|
|
|
.as_single_xsd_any_uri()
|
2020-08-11 14:31:05 +00:00
|
|
|
.context(location_info!())?;
|
2020-07-14 14:09:13 +00:00
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
let creator = get_or_fetch_and_upsert_user(creator_actor_id, context).await?;
|
2020-07-14 14:09:13 +00:00
|
|
|
|
|
|
|
let mut in_reply_tos = note
|
2020-07-17 21:11:07 +00:00
|
|
|
.in_reply_to()
|
2020-07-14 14:09:13 +00:00
|
|
|
.as_ref()
|
2020-08-11 14:31:05 +00:00
|
|
|
.context(location_info!())?
|
2020-07-14 14:09:13 +00:00
|
|
|
.as_many()
|
2020-08-11 14:31:05 +00:00
|
|
|
.context(location_info!())?
|
2020-07-14 14:09:13 +00:00
|
|
|
.iter()
|
2020-08-11 14:31:05 +00:00
|
|
|
.map(|i| i.as_xsd_any_uri().context(""));
|
|
|
|
let post_ap_id = in_reply_tos.next().context(location_info!())??;
|
2020-04-27 16:57:00 +00:00
|
|
|
|
2020-06-11 15:16:33 +00:00
|
|
|
// This post, or the parent comment might not yet exist on this server yet, fetch them.
|
2020-08-18 13:43:50 +00:00
|
|
|
let post = get_or_fetch_and_insert_post(&post_ap_id, context).await?;
|
2020-06-11 15:16:33 +00:00
|
|
|
|
2020-04-27 16:57:00 +00:00
|
|
|
// The 2nd item, if it exists, is the parent comment apub_id
|
2020-06-11 15:16:33 +00:00
|
|
|
// For deeply nested comments, FromApub automatically gets called recursively
|
2020-04-27 16:57:00 +00:00
|
|
|
let parent_id: Option<i32> = match in_reply_tos.next() {
|
|
|
|
Some(parent_comment_uri) => {
|
2020-08-11 14:31:05 +00:00
|
|
|
let parent_comment_ap_id = &parent_comment_uri?;
|
2020-07-01 12:54:29 +00:00
|
|
|
let parent_comment =
|
2020-08-18 13:43:50 +00:00
|
|
|
get_or_fetch_and_insert_comment(&parent_comment_ap_id, context).await?;
|
2020-04-27 16:57:00 +00:00
|
|
|
|
|
|
|
Some(parent_comment.id)
|
|
|
|
}
|
|
|
|
None => None,
|
|
|
|
};
|
2020-08-06 12:53:58 +00:00
|
|
|
let content = note
|
|
|
|
.content()
|
2020-08-11 14:31:05 +00:00
|
|
|
.context(location_info!())?
|
2020-08-06 12:53:58 +00:00
|
|
|
.as_single_xsd_string()
|
2020-08-11 14:31:05 +00:00
|
|
|
.context(location_info!())?
|
2020-08-06 12:53:58 +00:00
|
|
|
.to_string();
|
2020-08-06 19:44:47 +00:00
|
|
|
let content_slurs_removed = remove_slurs(&content);
|
2020-08-05 12:18:08 +00:00
|
|
|
|
2020-04-27 16:57:00 +00:00
|
|
|
Ok(CommentForm {
|
|
|
|
creator_id: creator.id,
|
|
|
|
post_id: post.id,
|
|
|
|
parent_id,
|
2020-08-06 19:44:47 +00:00
|
|
|
content: content_slurs_removed,
|
2020-04-27 16:57:00 +00:00
|
|
|
removed: None,
|
|
|
|
read: None,
|
2020-07-17 21:11:07 +00:00
|
|
|
published: note.published().map(|u| u.to_owned().naive_local()),
|
|
|
|
updated: note.updated().map(|u| u.to_owned().naive_local()),
|
2020-04-27 16:57:00 +00:00
|
|
|
deleted: None,
|
2020-08-31 13:48:02 +00:00
|
|
|
ap_id: Some(check_actor_domain(note, expected_domain)?),
|
2020-04-27 16:57:00 +00:00
|
|
|
local: false,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
/// Send out information about a newly created comment, to the followers of the community.
|
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-08-18 13:43:50 +00:00
|
|
|
let maa = collect_non_local_mentions_and_addresses(&self.content, &community, context).await?;
|
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())
|
|
|
|
.set_many_ccs(maa.addressed_ccs.to_owned())
|
|
|
|
// Set the mention tags
|
|
|
|
.set_many_tags(maa.get_tags()?);
|
2020-04-27 22:17:02 +00:00
|
|
|
|
2020-09-30 16:19:14 +00:00
|
|
|
send_to_community(&creator, &community, create.clone(), context).await?;
|
|
|
|
send_comment_mentions(&creator, maa.inboxes, create, context).await?;
|
2020-04-27 16:57:00 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Send out information about an edited post, to the followers of the community.
|
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-08-18 13:43:50 +00:00
|
|
|
let maa = collect_non_local_mentions_and_addresses(&self.content, &community, context).await?;
|
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())
|
|
|
|
.set_many_ccs(maa.addressed_ccs.to_owned())
|
|
|
|
// Set the mention tags
|
|
|
|
.set_many_tags(maa.get_tags()?);
|
2020-04-27 22:17:02 +00:00
|
|
|
|
2020-09-30 16:19:14 +00:00
|
|
|
send_to_community(&creator, &community, update.clone(), context).await?;
|
|
|
|
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> {
|
|
|
|
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 delete = Delete::new(creator.actor_id.to_owned(), note.into_any_base()?);
|
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-08-12 14:43:45 +00:00
|
|
|
.set_many_ccs(vec![community.get_followers_url()?]);
|
2020-04-29 14:51:25 +00:00
|
|
|
|
2020-09-30 16:19:14 +00:00
|
|
|
send_to_community(&creator, &community, delete, 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> {
|
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-05-01 19:01:29 +00:00
|
|
|
|
|
|
|
// Generate a fake delete activity, with the correct object
|
2020-07-14 14:09:13 +00:00
|
|
|
let mut delete = Delete::new(creator.actor_id.to_owned(), note.into_any_base()?);
|
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-08-12 14:43:45 +00:00
|
|
|
.set_many_ccs(vec![community.get_followers_url()?]);
|
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-08-12 14:43:45 +00:00
|
|
|
.set_many_ccs(vec![community.get_followers_url()?]);
|
2020-05-01 19:01:29 +00:00
|
|
|
|
2020-09-30 16:19:14 +00:00
|
|
|
send_to_community(&creator, &community, undo, 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> {
|
|
|
|
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 remove = Remove::new(mod_.actor_id.to_owned(), note.into_any_base()?);
|
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-08-12 14:43:45 +00:00
|
|
|
.set_many_ccs(vec![community.get_followers_url()?]);
|
2020-05-03 14:00:59 +00:00
|
|
|
|
2020-09-30 16:19:14 +00:00
|
|
|
send_to_community(&mod_, &community, remove, 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> {
|
|
|
|
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-05-03 14:00:59 +00:00
|
|
|
|
|
|
|
// Generate a fake delete activity, with the correct object
|
2020-07-14 14:09:13 +00:00
|
|
|
let mut remove = Remove::new(mod_.actor_id.to_owned(), note.into_any_base()?);
|
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-08-12 14:43:45 +00:00
|
|
|
.set_many_ccs(vec![community.get_followers_url()?]);
|
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-08-12 14:43:45 +00:00
|
|
|
.set_many_ccs(vec![community.get_followers_url()?]);
|
2020-05-03 14:00:59 +00:00
|
|
|
|
2020-09-30 16:19:14 +00:00
|
|
|
send_to_community(&mod_, &community, undo, 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-08-12 14:43:45 +00:00
|
|
|
.set_many_ccs(vec![community.get_followers_url()?]);
|
2020-04-28 04:16:02 +00:00
|
|
|
|
2020-09-30 16:19:14 +00:00
|
|
|
send_to_community(&creator, &community, like, 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-08-12 14:43:45 +00:00
|
|
|
.set_many_ccs(vec![community.get_followers_url()?]);
|
2020-04-28 04:16:02 +00:00
|
|
|
|
2020-09-30 16:19:14 +00:00
|
|
|
send_to_community(&creator, &community, dislike, 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-08-12 14:43:45 +00:00
|
|
|
.set_many_ccs(vec![community.get_followers_url()?]);
|
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-08-12 14:43:45 +00:00
|
|
|
.set_many_ccs(vec![community.get_followers_url()?]);
|
2020-05-04 00:34:04 +00:00
|
|
|
|
2020-09-30 16:19:14 +00:00
|
|
|
send_to_community(&creator, &community, undo, 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-08-18 13:43:50 +00:00
|
|
|
let mention_user = get_or_fetch_and_upsert_user(&actor_id, context).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,
|
|
|
|
})
|
|
|
|
}
|