lemmy/server/src/apub/post.rs

106 lines
3.8 KiB
Rust
Raw Normal View History

2020-04-14 15:37:23 +00:00
use crate::apub::create_apub_response;
use crate::apub::fetcher::{fetch_remote_community, fetch_remote_user};
use crate::convert_datetime;
2020-04-09 19:04:31 +00:00
use crate::db::community::Community;
use crate::db::post::{Post, PostForm};
2020-04-07 21:02:32 +00:00
use crate::db::user::User_;
use crate::db::Crud;
use activitystreams::{context, object::properties::ObjectProperties, object::Page};
use actix_web::body::Body;
use actix_web::web::Path;
use actix_web::{web, HttpResponse};
use diesel::r2d2::{ConnectionManager, Pool};
use diesel::PgConnection;
use failure::Error;
use serde::Deserialize;
2020-04-08 12:37:05 +00:00
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: Path<PostQuery>,
db: web::Data<Pool<ConnectionManager<PgConnection>>>,
) -> Result<HttpResponse<Body>, Error> {
let id = info.post_id.parse::<i32>()?;
let post = Post::read(&&db.get()?, id)?;
2020-04-07 21:02:32 +00:00
Ok(create_apub_response(&post.as_page(&db.get().unwrap())?))
}
impl Post {
2020-04-17 15:33:55 +00:00
// Turn a Lemmy post into an ActivityPub page that can be sent out over the network.
2020-04-07 21:02:32 +00:00
pub fn as_page(&self, conn: &PgConnection) -> Result<Page, Error> {
let mut page = Page::default();
let oprops: &mut ObjectProperties = page.as_mut();
2020-04-07 21:02:32 +00:00
let creator = User_::read(conn, self.creator_id)?;
2020-04-09 19:04:31 +00:00
let community = Community::read(conn, self.community_id)?;
oprops
2020-03-14 00:05:42 +00:00
// Not needed when the Post is embedded in a collection (like for community outbox)
2020-04-07 21:02:32 +00:00
.set_context_xsd_any_uri(context())?
2020-04-14 15:37:23 +00:00
.set_id(self.ap_id.to_owned())?
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
.set_summary_xsd_string(self.name.to_owned())?
.set_published(convert_datetime(self.published))?
2020-04-09 19:04:31 +00:00
.set_to_xsd_any_uri(community.actor_id)?
2020-04-14 15:37:23 +00:00
.set_attributed_to_xsd_any_uri(creator.actor_id)?;
if let Some(body) = &self.body {
oprops.set_content_xsd_string(body.to_owned())?;
}
// TODO: hacky code because we get self.url == Some("")
// https://github.com/dessalines/lemmy/issues/602
let url = self.url.as_ref().filter(|u| !u.is_empty());
if let Some(u) = url {
oprops.set_url_xsd_any_uri(u.to_owned())?;
}
if let Some(u) = self.updated {
oprops.set_updated(convert_datetime(u))?;
}
Ok(page)
}
}
impl PostForm {
2020-04-17 15:33:55 +00:00
/// Parse an ActivityPub page received from another instance into a Lemmy post.
2020-04-07 21:02:32 +00:00
pub fn from_page(page: &Page, conn: &PgConnection) -> Result<PostForm, Error> {
let oprops = &page.object_props;
2020-04-09 19:04:31 +00:00
let creator_id = Url::parse(&oprops.get_attributed_to_xsd_any_uri().unwrap().to_string())?;
let creator = fetch_remote_user(&creator_id, conn)?;
let community_id = Url::parse(&oprops.get_to_xsd_any_uri().unwrap().to_string())?;
let community = fetch_remote_community(&community_id, conn)?;
Ok(PostForm {
2020-04-13 12:13:06 +00:00
name: oprops.get_summary_xsd_string().unwrap().to_string(),
url: oprops.get_url_xsd_any_uri().map(|u| u.to_string()),
body: oprops.get_content_xsd_string().map(|c| c.to_string()),
2020-04-07 21:02:32 +00:00
creator_id: creator.id,
2020-04-09 19:04:31 +00:00
community_id: community.id,
removed: None, // -> Delete activity / tombstone
locked: None, // -> commentsEnabled
published: oprops
.get_published()
.map(|u| u.as_ref().to_owned().naive_local()),
updated: oprops
.get_updated()
.map(|u| u.as_ref().to_owned().naive_local()),
deleted: None, // -> Delete activity / tombstone
nsfw: false, // -> sensitive
stickied: None, // -> put it in "featured" collection of the community
embed_title: None, // -> attachment?
embed_description: None,
embed_html: None,
thumbnail_url: None,
ap_id: oprops.get_id().unwrap().to_string(),
local: false,
})
}
}