2020-04-24 14:04:36 +00:00
|
|
|
use super::*;
|
2020-03-16 18:19:04 +00:00
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct PostQuery {
|
|
|
|
post_id: String,
|
|
|
|
}
|
|
|
|
|
2020-04-17 15:33:55 +00:00
|
|
|
/// Return the post json over HTTP.
|
2020-03-16 18:19:04 +00:00
|
|
|
pub async fn get_apub_post(
|
|
|
|
info: Path<PostQuery>,
|
2020-04-24 14:04:36 +00:00
|
|
|
db: DbPoolParam,
|
2020-03-16 18:19:04 +00:00
|
|
|
) -> Result<HttpResponse<Body>, Error> {
|
|
|
|
let id = info.post_id.parse::<i32>()?;
|
2020-04-03 09:00:24 +00:00
|
|
|
let post = Post::read(&&db.get()?, id)?;
|
2020-04-29 14:51:25 +00:00
|
|
|
if !post.deleted {
|
|
|
|
Ok(create_apub_response(&post.to_apub(&db.get().unwrap())?))
|
|
|
|
} else {
|
|
|
|
Ok(create_apub_tombstone_response(&post.to_tombstone()?))
|
|
|
|
}
|
2020-03-16 18:19:04 +00:00
|
|
|
}
|
2019-12-19 21:59:13 +00:00
|
|
|
|
2020-04-24 21:30:27 +00:00
|
|
|
impl ToApub for Post {
|
|
|
|
type Response = Page;
|
|
|
|
|
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-29 14:51:25 +00:00
|
|
|
fn to_apub(&self, conn: &PgConnection) -> Result<Page, Error> {
|
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();
|
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)?;
|
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)
|
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())?
|
2020-03-12 00:01:25 +00:00
|
|
|
.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)?;
|
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-04-29 14:51:25 +00:00
|
|
|
Ok(page)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_tombstone(&self) -> Result<Tombstone, Error> {
|
2020-05-01 14:07:38 +00:00
|
|
|
create_tombstone(
|
|
|
|
self.deleted,
|
|
|
|
&self.ap_id,
|
|
|
|
self.updated,
|
|
|
|
PageType.to_string(),
|
|
|
|
)
|
2019-12-19 21:59:13 +00:00
|
|
|
}
|
2020-04-03 09:00:24 +00:00
|
|
|
}
|
2020-04-03 05:02:43 +00:00
|
|
|
|
2020-04-24 21:30:27 +00:00
|
|
|
impl FromApub for PostForm {
|
|
|
|
type ApubType = Page;
|
|
|
|
|
2020-04-17 15:33:55 +00:00
|
|
|
/// Parse an ActivityPub page received from another instance into a Lemmy post.
|
2020-04-24 19:55:54 +00:00
|
|
|
fn from_apub(page: &Page, conn: &PgConnection) -> Result<PostForm, Error> {
|
2020-04-03 05:02:43 +00:00
|
|
|
let oprops = &page.object_props;
|
2020-04-24 14:04:36 +00:00
|
|
|
let creator_actor_id = &oprops.get_attributed_to_xsd_any_uri().unwrap().to_string();
|
|
|
|
let creator = get_or_fetch_and_upsert_remote_user(&creator_actor_id, &conn)?;
|
|
|
|
let community_actor_id = &oprops.get_to_xsd_any_uri().unwrap().to_string();
|
|
|
|
let community = get_or_fetch_and_upsert_remote_community(&community_actor_id, &conn)?;
|
2020-04-09 19:04:31 +00:00
|
|
|
|
2020-04-07 16:47:19 +00:00
|
|
|
Ok(PostForm {
|
2020-04-13 12:13:06 +00:00
|
|
|
name: oprops.get_summary_xsd_string().unwrap().to_string(),
|
2020-04-03 05:02:43 +00:00
|
|
|
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,
|
2020-04-12 14:53:55 +00:00
|
|
|
removed: None, // -> Delete activity / tombstone
|
|
|
|
locked: None, // -> commentsEnabled
|
2020-04-03 05:02:43 +00:00
|
|
|
published: oprops
|
|
|
|
.get_published()
|
2020-04-07 16:47:19 +00:00
|
|
|
.map(|u| u.as_ref().to_owned().naive_local()),
|
2020-04-03 05:02:43 +00:00
|
|
|
updated: oprops
|
|
|
|
.get_updated()
|
|
|
|
.map(|u| u.as_ref().to_owned().naive_local()),
|
2020-04-12 14:53:55 +00:00
|
|
|
deleted: None, // -> Delete activity / tombstone
|
|
|
|
nsfw: false, // -> sensitive
|
|
|
|
stickied: None, // -> put it in "featured" collection of the community
|
|
|
|
embed_title: None, // -> attachment?
|
2020-04-03 05:02:43 +00:00
|
|
|
embed_description: None,
|
|
|
|
embed_html: None,
|
|
|
|
thumbnail_url: None,
|
2020-04-07 16:47:19 +00:00
|
|
|
ap_id: oprops.get_id().unwrap().to_string(),
|
|
|
|
local: false,
|
2020-04-03 05:02:43 +00:00
|
|
|
})
|
|
|
|
}
|
2019-12-19 21:59:13 +00:00
|
|
|
}
|
2020-04-27 16:57:00 +00:00
|
|
|
|
|
|
|
impl ApubObjectType for Post {
|
|
|
|
/// Send out information about a newly created post, to the followers of the community.
|
|
|
|
fn send_create(&self, creator: &User_, conn: &PgConnection) -> Result<(), Error> {
|
|
|
|
let page = self.to_apub(conn)?;
|
|
|
|
let community = Community::read(conn, self.community_id)?;
|
2020-04-28 04:16:02 +00:00
|
|
|
let id = format!("{}/create/{}", self.ap_id, uuid::Uuid::new_v4());
|
|
|
|
|
2020-04-27 16:57:00 +00:00
|
|
|
let mut create = Create::new();
|
|
|
|
populate_object_props(
|
|
|
|
&mut create.object_props,
|
|
|
|
&community.get_followers_url(),
|
2020-04-28 04:16:02 +00:00
|
|
|
&id,
|
2020-04-27 16:57:00 +00:00
|
|
|
)?;
|
|
|
|
create
|
|
|
|
.create_props
|
|
|
|
.set_actor_xsd_any_uri(creator.actor_id.to_owned())?
|
2020-04-29 14:51:25 +00:00
|
|
|
.set_object_base_box(page)?;
|
2020-04-27 22:17:02 +00:00
|
|
|
|
|
|
|
// Insert the sent activity into the activity table
|
|
|
|
let activity_form = activity::ActivityForm {
|
|
|
|
user_id: creator.id,
|
|
|
|
data: serde_json::to_value(&create)?,
|
|
|
|
local: true,
|
|
|
|
updated: None,
|
|
|
|
};
|
|
|
|
activity::Activity::create(&conn, &activity_form)?;
|
|
|
|
|
2020-04-27 16:57:00 +00:00
|
|
|
send_activity(
|
|
|
|
&create,
|
|
|
|
&creator.private_key.as_ref().unwrap(),
|
|
|
|
&creator.actor_id,
|
|
|
|
community.get_follower_inboxes(&conn)?,
|
|
|
|
)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Send out information about an edited post, to the followers of the community.
|
|
|
|
fn send_update(&self, creator: &User_, conn: &PgConnection) -> Result<(), Error> {
|
|
|
|
let page = self.to_apub(conn)?;
|
|
|
|
let community = Community::read(conn, self.community_id)?;
|
2020-04-28 04:16:02 +00:00
|
|
|
let id = format!("{}/update/{}", self.ap_id, uuid::Uuid::new_v4());
|
|
|
|
|
2020-04-27 16:57:00 +00:00
|
|
|
let mut update = Update::new();
|
|
|
|
populate_object_props(
|
|
|
|
&mut update.object_props,
|
|
|
|
&community.get_followers_url(),
|
2020-04-28 04:16:02 +00:00
|
|
|
&id,
|
2020-04-27 16:57:00 +00:00
|
|
|
)?;
|
|
|
|
update
|
|
|
|
.update_props
|
|
|
|
.set_actor_xsd_any_uri(creator.actor_id.to_owned())?
|
2020-04-29 14:51:25 +00:00
|
|
|
.set_object_base_box(page)?;
|
2020-04-27 22:17:02 +00:00
|
|
|
|
|
|
|
// Insert the sent activity into the activity table
|
|
|
|
let activity_form = activity::ActivityForm {
|
|
|
|
user_id: creator.id,
|
|
|
|
data: serde_json::to_value(&update)?,
|
|
|
|
local: true,
|
|
|
|
updated: None,
|
|
|
|
};
|
|
|
|
activity::Activity::create(&conn, &activity_form)?;
|
|
|
|
|
2020-04-27 16:57:00 +00:00
|
|
|
send_activity(
|
|
|
|
&update,
|
|
|
|
&creator.private_key.as_ref().unwrap(),
|
|
|
|
&creator.actor_id,
|
|
|
|
community.get_follower_inboxes(&conn)?,
|
|
|
|
)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-04-29 14:51:25 +00:00
|
|
|
|
2020-05-01 14:07:38 +00:00
|
|
|
fn send_delete(&self, creator: &User_, conn: &PgConnection) -> Result<(), Error> {
|
|
|
|
let page = self.to_apub(conn)?;
|
|
|
|
let community = Community::read(conn, self.community_id)?;
|
|
|
|
let id = format!("{}/delete/{}", self.ap_id, uuid::Uuid::new_v4());
|
2020-04-29 14:51:25 +00:00
|
|
|
let mut delete = Delete::default();
|
2020-05-01 14:07:38 +00:00
|
|
|
|
|
|
|
populate_object_props(
|
|
|
|
&mut delete.object_props,
|
|
|
|
&community.get_followers_url(),
|
|
|
|
&id,
|
|
|
|
)?;
|
|
|
|
|
2020-04-29 14:51:25 +00:00
|
|
|
delete
|
|
|
|
.delete_props
|
2020-05-01 14:07:38 +00:00
|
|
|
.set_actor_xsd_any_uri(creator.actor_id.to_owned())?
|
|
|
|
.set_object_base_box(page)?;
|
2020-04-29 14:51:25 +00:00
|
|
|
|
|
|
|
// Insert the sent activity into the activity table
|
|
|
|
let activity_form = activity::ActivityForm {
|
|
|
|
user_id: self.creator_id,
|
|
|
|
data: serde_json::to_value(&delete)?,
|
|
|
|
local: true,
|
|
|
|
updated: None,
|
|
|
|
};
|
|
|
|
activity::Activity::create(&conn, &activity_form)?;
|
|
|
|
|
|
|
|
let community = Community::read(conn, self.community_id)?;
|
|
|
|
send_activity(
|
|
|
|
&delete,
|
2020-05-01 14:07:38 +00:00
|
|
|
&creator.private_key.as_ref().unwrap(),
|
|
|
|
&creator.actor_id,
|
2020-04-29 14:51:25 +00:00
|
|
|
community.get_follower_inboxes(&conn)?,
|
2020-05-01 19:01:29 +00:00
|
|
|
)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn send_undo_delete(&self, creator: &User_, conn: &PgConnection) -> Result<(), Error> {
|
|
|
|
let page = self.to_apub(conn)?;
|
|
|
|
let community = Community::read(conn, self.community_id)?;
|
|
|
|
let id = format!("{}/delete/{}", self.ap_id, uuid::Uuid::new_v4());
|
|
|
|
let mut delete = Delete::default();
|
|
|
|
|
|
|
|
populate_object_props(
|
|
|
|
&mut delete.object_props,
|
|
|
|
&community.get_followers_url(),
|
|
|
|
&id,
|
|
|
|
)?;
|
|
|
|
|
|
|
|
delete
|
|
|
|
.delete_props
|
|
|
|
.set_actor_xsd_any_uri(creator.actor_id.to_owned())?
|
|
|
|
.set_object_base_box(page)?;
|
|
|
|
|
|
|
|
// Undo that fake activity
|
|
|
|
let undo_id = format!("{}/undo/delete/{}", self.ap_id, uuid::Uuid::new_v4());
|
|
|
|
let mut undo = Undo::default();
|
|
|
|
|
|
|
|
populate_object_props(
|
|
|
|
&mut undo.object_props,
|
|
|
|
&community.get_followers_url(),
|
|
|
|
&undo_id,
|
|
|
|
)?;
|
|
|
|
|
|
|
|
undo
|
|
|
|
.undo_props
|
|
|
|
.set_actor_xsd_any_uri(creator.actor_id.to_owned())?
|
|
|
|
.set_object_base_box(delete)?;
|
|
|
|
|
|
|
|
// Insert the sent activity into the activity table
|
|
|
|
let activity_form = activity::ActivityForm {
|
|
|
|
user_id: self.creator_id,
|
|
|
|
data: serde_json::to_value(&undo)?,
|
|
|
|
local: true,
|
|
|
|
updated: None,
|
|
|
|
};
|
|
|
|
activity::Activity::create(&conn, &activity_form)?;
|
|
|
|
|
|
|
|
let community = Community::read(conn, self.community_id)?;
|
|
|
|
send_activity(
|
|
|
|
&undo,
|
|
|
|
&creator.private_key.as_ref().unwrap(),
|
|
|
|
&creator.actor_id,
|
|
|
|
community.get_follower_inboxes(&conn)?,
|
2020-04-29 14:51:25 +00:00
|
|
|
)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-04-27 16:57:00 +00:00
|
|
|
}
|
2020-04-28 02:46:09 +00:00
|
|
|
|
|
|
|
impl ApubLikeableType for Post {
|
|
|
|
fn send_like(&self, creator: &User_, conn: &PgConnection) -> Result<(), Error> {
|
|
|
|
let page = self.to_apub(conn)?;
|
|
|
|
let community = Community::read(conn, self.community_id)?;
|
2020-04-28 04:16:02 +00:00
|
|
|
let id = format!("{}/like/{}", self.ap_id, uuid::Uuid::new_v4());
|
|
|
|
|
2020-04-28 02:46:09 +00:00
|
|
|
let mut like = Like::new();
|
2020-04-28 04:16:02 +00:00
|
|
|
populate_object_props(&mut like.object_props, &community.get_followers_url(), &id)?;
|
2020-04-28 02:46:09 +00:00
|
|
|
like
|
|
|
|
.like_props
|
|
|
|
.set_actor_xsd_any_uri(creator.actor_id.to_owned())?
|
2020-04-29 14:51:25 +00:00
|
|
|
.set_object_base_box(page)?;
|
2020-04-28 02:46:09 +00:00
|
|
|
|
|
|
|
// Insert the sent activity into the activity table
|
|
|
|
let activity_form = activity::ActivityForm {
|
|
|
|
user_id: creator.id,
|
|
|
|
data: serde_json::to_value(&like)?,
|
|
|
|
local: true,
|
|
|
|
updated: None,
|
|
|
|
};
|
|
|
|
activity::Activity::create(&conn, &activity_form)?;
|
|
|
|
|
|
|
|
send_activity(
|
|
|
|
&like,
|
|
|
|
&creator.private_key.as_ref().unwrap(),
|
|
|
|
&creator.actor_id,
|
|
|
|
community.get_follower_inboxes(&conn)?,
|
|
|
|
)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn send_dislike(&self, creator: &User_, conn: &PgConnection) -> Result<(), Error> {
|
|
|
|
let page = self.to_apub(conn)?;
|
|
|
|
let community = Community::read(conn, self.community_id)?;
|
2020-04-28 04:16:02 +00:00
|
|
|
let id = format!("{}/dislike/{}", self.ap_id, uuid::Uuid::new_v4());
|
|
|
|
|
2020-04-28 02:46:09 +00:00
|
|
|
let mut dislike = Dislike::new();
|
|
|
|
populate_object_props(
|
|
|
|
&mut dislike.object_props,
|
|
|
|
&community.get_followers_url(),
|
2020-04-28 04:16:02 +00:00
|
|
|
&id,
|
2020-04-28 02:46:09 +00:00
|
|
|
)?;
|
|
|
|
dislike
|
|
|
|
.dislike_props
|
|
|
|
.set_actor_xsd_any_uri(creator.actor_id.to_owned())?
|
2020-04-29 14:51:25 +00:00
|
|
|
.set_object_base_box(page)?;
|
2020-04-28 02:46:09 +00:00
|
|
|
|
|
|
|
// Insert the sent activity into the activity table
|
|
|
|
let activity_form = activity::ActivityForm {
|
|
|
|
user_id: creator.id,
|
|
|
|
data: serde_json::to_value(&dislike)?,
|
|
|
|
local: true,
|
|
|
|
updated: None,
|
|
|
|
};
|
|
|
|
activity::Activity::create(&conn, &activity_form)?;
|
|
|
|
|
|
|
|
send_activity(
|
|
|
|
&dislike,
|
|
|
|
&creator.private_key.as_ref().unwrap(),
|
|
|
|
&creator.actor_id,
|
|
|
|
community.get_follower_inboxes(&conn)?,
|
|
|
|
)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|