2020-03-16 18:19:04 +00:00
|
|
|
use crate::apub::{create_apub_response, make_apub_endpoint, EndpointType};
|
2020-03-14 00:05:42 +00:00
|
|
|
use crate::db::post_view::PostView;
|
2020-04-03 05:02:43 +00:00
|
|
|
use crate::{convert_datetime, naive_now};
|
2020-03-19 01:16:17 +00:00
|
|
|
use activitystreams::{object::properties::ObjectProperties, object::Page};
|
2020-03-16 18:19:04 +00:00
|
|
|
use actix_web::body::Body;
|
|
|
|
use actix_web::web::Path;
|
|
|
|
use actix_web::{web, HttpResponse};
|
|
|
|
use diesel::r2d2::{ConnectionManager, Pool};
|
|
|
|
use diesel::PgConnection;
|
2020-03-12 00:01:25 +00:00
|
|
|
use failure::Error;
|
2020-03-16 18:19:04 +00:00
|
|
|
use serde::Deserialize;
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct PostQuery {
|
|
|
|
post_id: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
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>()?;
|
|
|
|
// TODO: shows error: missing field `user_name`
|
|
|
|
let post = PostView::read(&&db.get()?, id, None)?;
|
2020-03-19 01:16:17 +00:00
|
|
|
Ok(create_apub_response(&post.as_page()?))
|
2020-03-16 18:19:04 +00:00
|
|
|
}
|
2019-12-19 21:59:13 +00:00
|
|
|
|
2020-03-14 00:05:42 +00:00
|
|
|
impl PostView {
|
2020-03-12 00:01:25 +00:00
|
|
|
pub fn as_page(&self) -> Result<Page, Error> {
|
2020-03-16 18:19:04 +00:00
|
|
|
let base_url = make_apub_endpoint(EndpointType::Post, &self.id.to_string());
|
2019-12-19 21:59:13 +00:00
|
|
|
let mut page = Page::default();
|
2020-03-12 00:01:25 +00:00
|
|
|
let oprops: &mut ObjectProperties = page.as_mut();
|
2019-12-19 21:59:13 +00:00
|
|
|
|
2020-03-12 00:01:25 +00:00
|
|
|
oprops
|
2020-03-14 00:05:42 +00:00
|
|
|
// Not needed when the Post is embedded in a collection (like for community outbox)
|
|
|
|
//.set_context_xsd_any_uri(context())?
|
2020-03-12 00:01:25 +00:00
|
|
|
.set_id(base_url)?
|
|
|
|
.set_name_xsd_string(self.name.to_owned())?
|
|
|
|
.set_published(convert_datetime(self.published))?
|
2020-03-16 18:19:04 +00:00
|
|
|
.set_attributed_to_xsd_any_uri(make_apub_endpoint(
|
|
|
|
EndpointType::User,
|
|
|
|
&self.creator_id.to_string(),
|
|
|
|
))?;
|
2019-12-19 21:59:13 +00:00
|
|
|
|
|
|
|
if let Some(body) = &self.body {
|
2020-03-12 00:01:25 +00:00
|
|
|
oprops.set_content_xsd_string(body.to_owned())?;
|
2019-12-19 21:59:13 +00:00
|
|
|
}
|
|
|
|
|
2020-03-14 21:03:05 +00:00
|
|
|
// TODO: hacky code because we get self.url == Some("")
|
2020-03-16 18:19:04 +00:00
|
|
|
// 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())?;
|
2019-12-19 21:59:13 +00:00
|
|
|
}
|
|
|
|
|
2020-03-12 00:01:25 +00:00
|
|
|
if let Some(u) = self.updated {
|
|
|
|
oprops.set_updated(convert_datetime(u))?;
|
2019-12-19 21:59:13 +00:00
|
|
|
}
|
|
|
|
|
2020-03-12 00:01:25 +00:00
|
|
|
Ok(page)
|
2019-12-19 21:59:13 +00:00
|
|
|
}
|
2020-04-03 05:02:43 +00:00
|
|
|
|
|
|
|
pub fn from_page(page: &Page) -> Result<PostView, Error> {
|
|
|
|
let oprops = &page.object_props;
|
|
|
|
Ok(PostView {
|
|
|
|
id: -1,
|
|
|
|
name: oprops.get_name_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()),
|
|
|
|
creator_id: -1,
|
|
|
|
community_id: -1,
|
|
|
|
removed: false,
|
|
|
|
locked: false,
|
|
|
|
published: oprops
|
|
|
|
.get_published()
|
|
|
|
.unwrap()
|
|
|
|
.as_ref()
|
|
|
|
.naive_local()
|
|
|
|
.to_owned(),
|
|
|
|
updated: oprops
|
|
|
|
.get_updated()
|
|
|
|
.map(|u| u.as_ref().to_owned().naive_local()),
|
|
|
|
deleted: false,
|
|
|
|
nsfw: false,
|
|
|
|
stickied: false,
|
|
|
|
embed_title: None,
|
|
|
|
embed_description: None,
|
|
|
|
embed_html: None,
|
|
|
|
thumbnail_url: None,
|
|
|
|
banned: false,
|
|
|
|
banned_from_community: false,
|
|
|
|
creator_name: "".to_string(),
|
|
|
|
creator_avatar: None,
|
|
|
|
community_name: "".to_string(),
|
|
|
|
community_removed: false,
|
|
|
|
community_deleted: false,
|
|
|
|
community_nsfw: false,
|
|
|
|
number_of_comments: -1,
|
|
|
|
score: -1,
|
|
|
|
upvotes: -1,
|
|
|
|
downvotes: -1,
|
|
|
|
hot_rank: -1,
|
|
|
|
newest_activity_time: naive_now(),
|
|
|
|
user_id: None,
|
|
|
|
my_vote: None,
|
|
|
|
subscribed: None,
|
|
|
|
read: None,
|
|
|
|
saved: None,
|
|
|
|
})
|
|
|
|
}
|
2019-12-19 21:59:13 +00:00
|
|
|
}
|