lemmy/lemmy_apub/src/post.rs

523 lines
15 KiB
Rust
Raw Normal View History

2020-05-16 14:04:08 +00:00
use crate::{
activities::{generate_activity_id, send_activity_to_community},
check_actor_domain,
create_apub_response,
create_apub_tombstone_response,
create_tombstone,
extensions::page_extension::PageExtension,
fetcher::{get_or_fetch_and_upsert_community, get_or_fetch_and_upsert_user},
ActorType,
ApubLikeableType,
ApubObjectType,
FromApub,
PageExt,
ToApub,
2020-05-16 14:04:08 +00:00
};
use activitystreams::{
activity::{
kind::{CreateType, DeleteType, DislikeType, LikeType, RemoveType, UndoType, UpdateType},
Create,
Delete,
Dislike,
Like,
Remove,
Undo,
Update,
},
object::{kind::PageType, Image, Page, Tombstone},
2020-07-14 12:19:33 +00:00
prelude::*,
public,
2020-07-14 12:19:33 +00:00
};
use activitystreams_ext::Ext1;
use actix_web::{body::Body, web, HttpResponse};
use anyhow::Context;
use lemmy_db::{
community::Community,
post::{Post, PostForm},
user::User_,
Crud,
DbPool,
};
2020-09-16 13:31:30 +00:00
use lemmy_structs::blocking;
use lemmy_utils::{
location_info,
request::fetch_iframely_and_pictrs_data,
utils::{check_slurs, convert_datetime, remove_slurs},
LemmyError,
};
use lemmy_websocket::LemmyContext;
2020-06-01 14:17:20 +00:00
use serde::Deserialize;
use url::Url;
#[derive(Deserialize)]
pub struct PostQuery {
post_id: String,
}
2020-04-17 15:33:55 +00:00
/// Return the post json over HTTP.
pub async fn get_apub_post(
info: web::Path<PostQuery>,
context: web::Data<LemmyContext>,
) -> Result<HttpResponse<Body>, LemmyError> {
let id = info.post_id.parse::<i32>()?;
let post = blocking(context.pool(), move |conn| Post::read(conn, id)).await??;
if !post.deleted {
Ok(create_apub_response(&post.to_apub(context.pool()).await?))
} else {
Ok(create_apub_tombstone_response(&post.to_tombstone()?))
}
}
#[async_trait::async_trait(?Send)]
impl ToApub for Post {
2020-05-05 00:04:48 +00:00
type Response = PageExt;
2020-04-17 15:33:55 +00:00
// Turn a Lemmy post into an ActivityPub page that can be sent out over the network.
async fn to_apub(&self, pool: &DbPool) -> Result<PageExt, LemmyError> {
2020-07-14 12:19:33 +00:00
let mut page = Page::new();
let creator_id = self.creator_id;
let creator = blocking(pool, move |conn| User_::read(conn, creator_id)).await??;
let community_id = self.community_id;
let community = blocking(pool, move |conn| Community::read(conn, community_id)).await??;
2020-07-14 12:19:33 +00:00
page
2020-03-14 00:05:42 +00:00
// Not needed when the Post is embedded in a collection (like for community outbox)
2020-05-05 00:04:48 +00:00
// TODO: need to set proper context defining sensitive/commentsEnabled fields
// https://git.asonix.dog/Aardwolf/activitystreams/issues/5
.set_context(activitystreams::context())
.set_id(self.ap_id.parse::<Url>()?)
2020-04-13 13:06:41 +00:00
// Use summary field to be consistent with mastodon content warning.
2020-04-13 12:13:06 +00:00
// https://mastodon.xyz/@Louisa/103987265222901387.json
2020-07-14 12:19:33 +00:00
.set_summary(self.name.to_owned())
.set_published(convert_datetime(self.published))
2020-07-14 12:19:33 +00:00
.set_to(community.actor_id)
.set_attributed_to(creator.actor_id);
if let Some(body) = &self.body {
2020-07-14 12:19:33 +00:00
page.set_content(body.to_owned());
}
// TODO: hacky code because we get self.url == Some("")
2020-05-05 00:04:48 +00:00
// https://github.com/LemmyNet/lemmy/issues/602
let url = self.url.as_ref().filter(|u| !u.is_empty());
if let Some(u) = url {
2020-07-14 12:19:33 +00:00
page.set_url(u.to_owned());
}
if let Some(thumbnail_url) = &self.thumbnail_url {
let mut image = Image::new();
image.set_url(thumbnail_url.to_string());
2020-07-14 12:19:33 +00:00
page.set_image(image.into_any_base()?);
}
if let Some(u) = self.updated {
page.set_updated(convert_datetime(u));
}
2020-05-05 00:04:48 +00:00
let ext = PageExtension {
comments_enabled: !self.locked,
sensitive: self.nsfw,
stickied: self.stickied,
2020-05-05 00:04:48 +00:00
};
2020-05-18 16:15:26 +00:00
Ok(Ext1::new(page, ext))
}
fn to_tombstone(&self) -> Result<Tombstone, LemmyError> {
create_tombstone(self.deleted, &self.ap_id, self.updated, PageType::Page)
}
}
#[async_trait::async_trait(?Send)]
impl FromApub for PostForm {
2020-05-05 00:04:48 +00:00
type ApubType = PageExt;
2020-04-17 15:33:55 +00:00
/// Parse an ActivityPub page received from another instance into a Lemmy post.
async fn from_apub(
page: &PageExt,
context: &LemmyContext,
expected_domain: Option<Url>,
) -> Result<PostForm, LemmyError> {
2020-05-18 16:15:26 +00:00
let ext = &page.ext_one;
2020-07-14 12:19:33 +00:00
let creator_actor_id = page
.inner
.attributed_to()
2020-07-14 12:19:33 +00:00
.as_ref()
.context(location_info!())?
2020-07-14 12:19:33 +00:00
.as_single_xsd_any_uri()
.context(location_info!())?;
let creator = get_or_fetch_and_upsert_user(creator_actor_id, context).await?;
2020-07-14 12:19:33 +00:00
let community_actor_id = page
.inner
.to()
2020-07-14 12:19:33 +00:00
.as_ref()
.context(location_info!())?
2020-07-14 12:19:33 +00:00
.as_single_xsd_any_uri()
.context(location_info!())?;
let community = get_or_fetch_and_upsert_community(community_actor_id, context).await?;
2020-07-14 12:19:33 +00:00
let thumbnail_url = match &page.inner.image() {
Some(any_image) => Image::from_any_base(
any_image
.to_owned()
.as_one()
.context(location_info!())?
.to_owned(),
)?
.context(location_info!())?
.url()
.context(location_info!())?
.as_single_xsd_any_uri()
.map(|u| u.to_string()),
None => None,
};
let url = page
.inner
.url()
.map(|u| u.as_single_xsd_any_uri())
.flatten()
.map(|s| s.to_string());
let (iframely_title, iframely_description, iframely_html, pictrs_thumbnail) =
if let Some(url) = &url {
fetch_iframely_and_pictrs_data(context.client(), Some(url.to_owned())).await
} else {
(None, None, None, thumbnail_url)
};
let name = page
.inner
.summary()
.as_ref()
.context(location_info!())?
.as_single_xsd_string()
.context(location_info!())?
.to_string();
2020-07-14 12:19:33 +00:00
let body = page
.inner
.content()
2020-07-14 12:19:33 +00:00
.as_ref()
.map(|c| c.as_single_xsd_string())
.flatten()
.map(|s| s.to_string());
check_slurs(&name)?;
2020-08-06 19:44:47 +00:00
let body_slurs_removed = body.map(|b| remove_slurs(&b));
Ok(PostForm {
name,
url,
2020-08-06 19:44:47 +00:00
body: body_slurs_removed,
2020-04-07 21:02:32 +00:00
creator_id: creator.id,
2020-04-09 19:04:31 +00:00
community_id: community.id,
2020-05-05 00:04:48 +00:00
removed: None,
locked: Some(!ext.comments_enabled),
2020-07-14 12:19:33 +00:00
published: page
.inner
.published()
2020-07-14 12:19:33 +00:00
.as_ref()
.map(|u| u.to_owned().naive_local()),
2020-07-14 12:19:33 +00:00
updated: page
.inner
.updated()
2020-07-14 12:19:33 +00:00
.as_ref()
.map(|u| u.to_owned().naive_local()),
2020-05-05 00:04:48 +00:00
deleted: None,
nsfw: ext.sensitive,
stickied: Some(ext.stickied),
embed_title: iframely_title,
embed_description: iframely_description,
embed_html: iframely_html,
thumbnail_url: pictrs_thumbnail,
ap_id: Some(check_actor_domain(page, expected_domain)?),
local: false,
})
}
}
#[async_trait::async_trait(?Send)]
impl ApubObjectType for Post {
/// Send out information about a newly created post, to the followers of the community.
async fn send_create(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
let page = self.to_apub(context.pool()).await?;
let community_id = self.community_id;
let community = blocking(context.pool(), move |conn| {
Community::read(conn, community_id)
})
.await??;
let mut create = Create::new(creator.actor_id.to_owned(), page.into_any_base()?);
create
.set_context(activitystreams::context())
.set_id(generate_activity_id(CreateType::Create)?)
.set_to(public())
.set_many_ccs(vec![community.get_followers_url()?]);
2020-04-27 22:17:02 +00:00
2020-06-01 14:17:20 +00:00
send_activity_to_community(
creator,
&community,
vec![community.get_shared_inbox_url()?],
create,
context,
)
.await?;
Ok(())
}
/// Send out information about an edited post, to the followers of the community.
async fn send_update(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
let page = self.to_apub(context.pool()).await?;
let community_id = self.community_id;
let community = blocking(context.pool(), move |conn| {
Community::read(conn, community_id)
})
.await??;
let mut update = Update::new(creator.actor_id.to_owned(), page.into_any_base()?);
update
.set_context(activitystreams::context())
.set_id(generate_activity_id(UpdateType::Update)?)
.set_to(public())
.set_many_ccs(vec![community.get_followers_url()?]);
2020-04-27 22:17:02 +00:00
2020-06-01 14:17:20 +00:00
send_activity_to_community(
creator,
&community,
vec![community.get_shared_inbox_url()?],
update,
context,
)
.await?;
Ok(())
}
async fn send_delete(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
let page = self.to_apub(context.pool()).await?;
let community_id = self.community_id;
let community = blocking(context.pool(), move |conn| {
Community::read(conn, community_id)
})
.await??;
let mut delete = Delete::new(creator.actor_id.to_owned(), page.into_any_base()?);
delete
.set_context(activitystreams::context())
.set_id(generate_activity_id(DeleteType::Delete)?)
.set_to(public())
.set_many_ccs(vec![community.get_followers_url()?]);
2020-06-01 14:17:20 +00:00
send_activity_to_community(
creator,
&community,
vec![community.get_shared_inbox_url()?],
delete,
context,
)
.await?;
Ok(())
}
async fn send_undo_delete(
&self,
creator: &User_,
context: &LemmyContext,
) -> Result<(), LemmyError> {
let page = self.to_apub(context.pool()).await?;
let community_id = self.community_id;
let community = blocking(context.pool(), move |conn| {
Community::read(conn, community_id)
})
.await??;
let mut delete = Delete::new(creator.actor_id.to_owned(), page.into_any_base()?);
delete
.set_context(activitystreams::context())
.set_id(generate_activity_id(DeleteType::Delete)?)
.set_to(public())
.set_many_ccs(vec![community.get_followers_url()?]);
// Undo that fake activity
let mut undo = Undo::new(creator.actor_id.to_owned(), delete.into_any_base()?);
undo
.set_context(activitystreams::context())
.set_id(generate_activity_id(UndoType::Undo)?)
.set_to(public())
.set_many_ccs(vec![community.get_followers_url()?]);
2020-06-01 14:17:20 +00:00
send_activity_to_community(
creator,
&community,
vec![community.get_shared_inbox_url()?],
undo,
context,
)
.await?;
2020-05-03 14:00:59 +00:00
Ok(())
}
async fn send_remove(&self, mod_: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
let page = self.to_apub(context.pool()).await?;
let community_id = self.community_id;
let community = blocking(context.pool(), move |conn| {
Community::read(conn, community_id)
})
.await??;
let mut remove = Remove::new(mod_.actor_id.to_owned(), page.into_any_base()?);
2020-05-03 14:00:59 +00:00
remove
.set_context(activitystreams::context())
.set_id(generate_activity_id(RemoveType::Remove)?)
.set_to(public())
.set_many_ccs(vec![community.get_followers_url()?]);
2020-05-03 14:00:59 +00:00
2020-06-01 14:17:20 +00:00
send_activity_to_community(
mod_,
&community,
vec![community.get_shared_inbox_url()?],
remove,
context,
)
.await?;
2020-05-03 14:00:59 +00:00
Ok(())
}
async fn send_undo_remove(&self, mod_: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
let page = self.to_apub(context.pool()).await?;
let community_id = self.community_id;
let community = blocking(context.pool(), move |conn| {
Community::read(conn, community_id)
})
.await??;
let mut remove = Remove::new(mod_.actor_id.to_owned(), page.into_any_base()?);
2020-05-03 14:00:59 +00:00
remove
.set_context(activitystreams::context())
.set_id(generate_activity_id(RemoveType::Remove)?)
.set_to(public())
.set_many_ccs(vec![community.get_followers_url()?]);
2020-05-03 14:00:59 +00:00
// Undo that fake activity
let mut undo = Undo::new(mod_.actor_id.to_owned(), remove.into_any_base()?);
2020-05-03 14:00:59 +00:00
undo
.set_context(activitystreams::context())
.set_id(generate_activity_id(UndoType::Undo)?)
.set_to(public())
.set_many_ccs(vec![community.get_followers_url()?]);
2020-05-03 14:00:59 +00:00
2020-06-01 14:17:20 +00:00
send_activity_to_community(
mod_,
&community,
vec![community.get_shared_inbox_url()?],
undo,
context,
)
.await?;
Ok(())
}
}
2020-04-28 02:46:09 +00:00
#[async_trait::async_trait(?Send)]
2020-04-28 02:46:09 +00:00
impl ApubLikeableType for Post {
async fn send_like(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
let page = self.to_apub(context.pool()).await?;
let community_id = self.community_id;
let community = blocking(context.pool(), move |conn| {
Community::read(conn, community_id)
})
.await??;
let mut like = Like::new(creator.actor_id.to_owned(), page.into_any_base()?);
2020-04-28 02:46:09 +00:00
like
.set_context(activitystreams::context())
.set_id(generate_activity_id(LikeType::Like)?)
.set_to(public())
.set_many_ccs(vec![community.get_followers_url()?]);
2020-04-28 02:46:09 +00:00
2020-06-01 14:17:20 +00:00
send_activity_to_community(
&creator,
&community,
vec![community.get_shared_inbox_url()?],
like,
context,
)
.await?;
2020-04-28 02:46:09 +00:00
Ok(())
}
async fn send_dislike(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
let page = self.to_apub(context.pool()).await?;
let community_id = self.community_id;
let community = blocking(context.pool(), move |conn| {
Community::read(conn, community_id)
})
.await??;
let mut dislike = Dislike::new(creator.actor_id.to_owned(), page.into_any_base()?);
2020-04-28 02:46:09 +00:00
dislike
.set_context(activitystreams::context())
.set_id(generate_activity_id(DislikeType::Dislike)?)
.set_to(public())
.set_many_ccs(vec![community.get_followers_url()?]);
2020-04-28 02:46:09 +00:00
2020-06-01 14:17:20 +00:00
send_activity_to_community(
&creator,
&community,
vec![community.get_shared_inbox_url()?],
dislike,
context,
)
.await?;
2020-04-28 02:46:09 +00:00
Ok(())
}
async fn send_undo_like(
&self,
creator: &User_,
context: &LemmyContext,
) -> Result<(), LemmyError> {
let page = self.to_apub(context.pool()).await?;
let community_id = self.community_id;
let community = blocking(context.pool(), move |conn| {
Community::read(conn, community_id)
})
.await??;
let mut like = Like::new(creator.actor_id.to_owned(), page.into_any_base()?);
like
.set_context(activitystreams::context())
.set_id(generate_activity_id(LikeType::Like)?)
.set_to(public())
.set_many_ccs(vec![community.get_followers_url()?]);
// Undo that fake activity
let mut undo = Undo::new(creator.actor_id.to_owned(), like.into_any_base()?);
undo
.set_context(activitystreams::context())
.set_id(generate_activity_id(UndoType::Undo)?)
.set_to(public())
.set_many_ccs(vec![community.get_followers_url()?]);
2020-06-01 14:17:20 +00:00
send_activity_to_community(
&creator,
&community,
vec![community.get_shared_inbox_url()?],
undo,
context,
)
.await?;
2020-05-07 17:07:33 +00:00
Ok(())
}
}