Address comments, implement delete for posts and comments

This commit is contained in:
Felix 2020-04-29 16:51:25 +02:00
parent 0c0c683986
commit c43f06124a
9 changed files with 165 additions and 63 deletions

View file

@ -339,6 +339,14 @@ impl Perform for Oper<EditComment> {
updated_comment.send_update(&user, &conn)?; updated_comment.send_update(&user, &conn)?;
if let Some(deleted) = data.deleted.to_owned() {
if deleted {
updated_comment.send_delete(&user, &conn)?;
} else {
// TODO: undo delete
}
}
let mut recipient_ids = Vec::new(); let mut recipient_ids = Vec::new();
// Scan the comment for user mentions, add those rows // Scan the comment for user mentions, add those rows

View file

@ -377,11 +377,14 @@ impl Perform for Oper<EditCommunity> {
expires, expires,
}; };
ModRemoveCommunity::create(&conn, &form)?; ModRemoveCommunity::create(&conn, &form)?;
updated_community.send_delete(&conn)?;
} }
if let Some(_deleted) = data.deleted.to_owned() { if let Some(deleted) = data.deleted.to_owned() {
if deleted {
updated_community.send_delete(&conn)?; updated_community.send_delete(&conn)?;
} else {
// TODO: undo delete
}
} }
let community_view = CommunityView::read(&conn, data.edit_id, Some(user_id))?; let community_view = CommunityView::read(&conn, data.edit_id, Some(user_id))?;

View file

@ -543,6 +543,14 @@ impl Perform for Oper<EditPost> {
updated_post.send_update(&user, &conn)?; updated_post.send_update(&user, &conn)?;
if let Some(deleted) = data.deleted.to_owned() {
if deleted {
updated_post.send_delete(&user, &conn)?;
} else {
// TODO: undo delete
}
}
let post_view = PostView::read(&conn, data.edit_id, Some(user_id))?; let post_view = PostView::read(&conn, data.edit_id, Some(user_id))?;
let res = PostResponse { post: post_view }; let res = PostResponse { post: post_view };

View file

@ -3,7 +3,7 @@ use super::*;
impl ToApub for Comment { impl ToApub for Comment {
type Response = Note; type Response = Note;
fn to_apub(&self, conn: &PgConnection) -> Result<ResponseOrTombstone<Note>, Error> { fn to_apub(&self, conn: &PgConnection) -> Result<Note, Error> {
let mut comment = Note::default(); let mut comment = Note::default();
let oprops: &mut ObjectProperties = comment.as_mut(); let oprops: &mut ObjectProperties = comment.as_mut();
let creator = User_::read(&conn, self.creator_id)?; let creator = User_::read(&conn, self.creator_id)?;
@ -33,7 +33,13 @@ impl ToApub for Comment {
oprops.set_updated(convert_datetime(u))?; oprops.set_updated(convert_datetime(u))?;
} }
Ok(ResponseOrTombstone::Response(comment)) Ok(comment)
}
}
impl ToTombstone for Comment {
fn to_tombstone(&self) -> Result<Tombstone, Error> {
create_tombstone(self.deleted, &self.ap_id, self.published, self.updated)
} }
} }
@ -102,7 +108,7 @@ impl ApubObjectType for Comment {
create create
.create_props .create_props
.set_actor_xsd_any_uri(creator.actor_id.to_owned())? .set_actor_xsd_any_uri(creator.actor_id.to_owned())?
.set_object_base_box(note.as_response()?.to_owned())?; .set_object_base_box(note)?;
// Insert the sent activity into the activity table // Insert the sent activity into the activity table
let activity_form = activity::ActivityForm { let activity_form = activity::ActivityForm {
@ -138,7 +144,7 @@ impl ApubObjectType for Comment {
update update
.update_props .update_props
.set_actor_xsd_any_uri(creator.actor_id.to_owned())? .set_actor_xsd_any_uri(creator.actor_id.to_owned())?
.set_object_base_box(note.as_response()?.to_owned())?; .set_object_base_box(note)?;
// Insert the sent activity into the activity table // Insert the sent activity into the activity table
let activity_form = activity::ActivityForm { let activity_form = activity::ActivityForm {
@ -157,6 +163,34 @@ impl ApubObjectType for Comment {
)?; )?;
Ok(()) Ok(())
} }
// TODO: this code is literally copied from post.rs
fn send_delete(&self, actor: &User_, conn: &PgConnection) -> Result<(), Error> {
let mut delete = Delete::default();
delete
.delete_props
.set_actor_xsd_any_uri(actor.actor_id.to_owned())?
.set_object_base_box(BaseBox::from_concrete(self.to_tombstone()?)?)?;
// 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 post = Post::read(conn, self.post_id)?;
let community = Community::read(conn, post.community_id)?;
send_activity(
&delete,
&actor.private_key.to_owned().unwrap(),
&actor.actor_id,
community.get_follower_inboxes(&conn)?,
)?;
Ok(())
}
} }
impl ApubLikeableType for Comment { impl ApubLikeableType for Comment {
@ -171,7 +205,7 @@ impl ApubLikeableType for Comment {
like like
.like_props .like_props
.set_actor_xsd_any_uri(creator.actor_id.to_owned())? .set_actor_xsd_any_uri(creator.actor_id.to_owned())?
.set_object_base_box(note.as_response()?.to_owned())?; .set_object_base_box(note)?;
// Insert the sent activity into the activity table // Insert the sent activity into the activity table
let activity_form = activity::ActivityForm { let activity_form = activity::ActivityForm {
@ -206,7 +240,7 @@ impl ApubLikeableType for Comment {
dislike dislike
.dislike_props .dislike_props
.set_actor_xsd_any_uri(creator.actor_id.to_owned())? .set_actor_xsd_any_uri(creator.actor_id.to_owned())?
.set_object_base_box(note.as_response()?.to_owned())?; .set_object_base_box(note)?;
// Insert the sent activity into the activity table // Insert the sent activity into the activity table
let activity_form = activity::ActivityForm { let activity_form = activity::ActivityForm {

View file

@ -9,17 +9,7 @@ impl ToApub for Community {
type Response = GroupExt; type Response = GroupExt;
// Turn a Lemmy Community into an ActivityPub group that can be sent out over the network. // Turn a Lemmy Community into an ActivityPub group that can be sent out over the network.
fn to_apub(&self, conn: &PgConnection) -> Result<ResponseOrTombstone<GroupExt>, Error> { fn to_apub(&self, conn: &PgConnection) -> Result<GroupExt, Error> {
if self.deleted || self.removed {
let mut tombstone = Tombstone::default();
// TODO: might want to include updated/deleted times as well
tombstone
.object_props
.set_id(self.actor_id.to_owned())?
.set_published(convert_datetime(self.published))?;
return Ok(ResponseOrTombstone::Tombstone(Box::new(tombstone)));
}
let mut group = Group::default(); let mut group = Group::default();
let oprops: &mut ObjectProperties = group.as_mut(); let oprops: &mut ObjectProperties = group.as_mut();
@ -53,9 +43,13 @@ impl ToApub for Community {
.set_endpoints(endpoint_props)? .set_endpoints(endpoint_props)?
.set_followers(self.get_followers_url())?; .set_followers(self.get_followers_url())?;
Ok(ResponseOrTombstone::Response( Ok(group.extend(actor_props).extend(self.get_public_key_ext()))
group.extend(actor_props).extend(self.get_public_key_ext()), }
)) }
impl ToTombstone for Community {
fn to_tombstone(&self) -> Result<Tombstone, Error> {
create_tombstone(self.deleted, &self.actor_id, self.published, self.updated)
} }
} }
@ -107,14 +101,11 @@ impl ActorType for Community {
} }
fn send_delete(&self, conn: &PgConnection) -> Result<(), Error> { fn send_delete(&self, conn: &PgConnection) -> Result<(), Error> {
let community = self.to_apub(conn)?;
let mut delete = Delete::default(); let mut delete = Delete::default();
delete delete
.delete_props .delete_props
.set_actor_xsd_any_uri(self.actor_id.to_owned())? .set_actor_xsd_any_uri(self.actor_id.to_owned())?
.set_object_base_box(BaseBox::from_concrete( .set_object_base_box(BaseBox::from_concrete(self.to_tombstone()?)?)?;
community.as_tombstone()?.to_owned(),
)?)?;
// Insert the sent activity into the activity table // Insert the sent activity into the activity table
let activity_form = activity::ActivityForm { let activity_form = activity::ActivityForm {
@ -208,8 +199,13 @@ pub async fn get_apub_community_http(
db: DbPoolParam, db: DbPoolParam,
) -> Result<HttpResponse<Body>, Error> { ) -> Result<HttpResponse<Body>, Error> {
let community = Community::read_from_name(&&db.get()?, &info.community_name)?; let community = Community::read_from_name(&&db.get()?, &info.community_name)?;
let c = community.to_apub(&db.get().unwrap())?; if !community.deleted {
Ok(create_apub_response(&c)) Ok(create_apub_response(
&community.to_apub(&db.get().unwrap())?,
))
} else {
Ok(create_apub_tombstone_response(&community.to_tombstone()?))
}
} }
/// Returns an empty followers collection, only populating the siz (for privacy). /// Returns an empty followers collection, only populating the siz (for privacy).

View file

@ -60,6 +60,7 @@ use crate::websocket::{
use crate::{convert_datetime, naive_now, Settings}; use crate::{convert_datetime, naive_now, Settings};
use activities::{populate_object_props, send_activity}; use activities::{populate_object_props, send_activity};
use chrono::NaiveDateTime;
use fetcher::{get_or_fetch_and_upsert_remote_community, get_or_fetch_and_upsert_remote_user}; use fetcher::{get_or_fetch_and_upsert_remote_community, get_or_fetch_and_upsert_remote_user};
use signatures::verify; use signatures::verify;
use signatures::{sign, PublicKey, PublicKeyExtension}; use signatures::{sign, PublicKey, PublicKeyExtension};
@ -86,6 +87,14 @@ where
.content_type(APUB_JSON_CONTENT_TYPE) .content_type(APUB_JSON_CONTENT_TYPE)
.json(data) .json(data)
} }
fn create_apub_tombstone_response<T>(data: &T) -> HttpResponse<Body>
where
T: Serialize,
{
HttpResponse::Gone()
.content_type(APUB_JSON_CONTENT_TYPE)
.json(data)
}
/// Generates the ActivityPub ID for a given object type and name. /// Generates the ActivityPub ID for a given object type and name.
/// ///
@ -138,31 +147,40 @@ fn is_apub_id_valid(apub_id: &Url) -> bool {
} }
} }
#[derive(Serialize)]
pub enum ResponseOrTombstone<Response> {
Response(Response),
Tombstone(Box<Tombstone>),
}
impl<Response> ResponseOrTombstone<Response> {
fn as_response(&self) -> Result<&Response, Error> {
match self {
ResponseOrTombstone::Response(r) => Ok(r),
ResponseOrTombstone::Tombstone(_t) => Err(format_err!("Value is a tombstone")),
}
}
fn as_tombstone(&self) -> Result<&Tombstone, Error> {
match self {
ResponseOrTombstone::Tombstone(t) => Ok(t),
ResponseOrTombstone::Response(_r) => Err(format_err!("Value is a response")),
}
}
}
// TODO Not sure good names for these // TODO Not sure good names for these
pub trait ToApub { pub trait ToApub {
type Response; type Response;
fn to_apub(&self, conn: &PgConnection) -> Result<ResponseOrTombstone<Self::Response>, Error>; fn to_apub(&self, conn: &PgConnection) -> Result<Self::Response, Error>;
}
fn create_tombstone(
deleted: bool,
object_id: &str,
published: NaiveDateTime,
updated: Option<NaiveDateTime>,
) -> Result<Tombstone, Error> {
if deleted {
let mut tombstone = Tombstone::default();
// TODO: might want to include deleted time as well
tombstone
.object_props
.set_id(object_id)?
.set_published(convert_datetime(published))?;
if let Some(updated) = updated {
tombstone
.object_props
.set_updated(convert_datetime(updated))?;
}
Ok(tombstone)
} else {
Err(format_err!(
"Cant convert object to tombstone if it wasnt deleted"
))
}
}
pub trait ToTombstone {
fn to_tombstone(&self) -> Result<Tombstone, Error>;
} }
pub trait FromApub { pub trait FromApub {
@ -175,7 +193,7 @@ pub trait FromApub {
pub trait ApubObjectType { pub trait ApubObjectType {
fn send_create(&self, creator: &User_, conn: &PgConnection) -> Result<(), Error>; fn send_create(&self, creator: &User_, conn: &PgConnection) -> Result<(), Error>;
fn send_update(&self, creator: &User_, conn: &PgConnection) -> Result<(), Error>; fn send_update(&self, creator: &User_, conn: &PgConnection) -> Result<(), Error>;
//fn send_delete(&self, creator: &User_, conn: &PgConnection) -> Result<(), Error>; fn send_delete(&self, actor: &User_, conn: &PgConnection) -> Result<(), Error>;
} }
pub trait ApubLikeableType { pub trait ApubLikeableType {

View file

@ -12,14 +12,18 @@ pub async fn get_apub_post(
) -> Result<HttpResponse<Body>, Error> { ) -> Result<HttpResponse<Body>, Error> {
let id = info.post_id.parse::<i32>()?; let id = info.post_id.parse::<i32>()?;
let post = Post::read(&&db.get()?, id)?; let post = Post::read(&&db.get()?, id)?;
if !post.deleted {
Ok(create_apub_response(&post.to_apub(&db.get().unwrap())?)) Ok(create_apub_response(&post.to_apub(&db.get().unwrap())?))
} else {
Ok(create_apub_tombstone_response(&post.to_tombstone()?))
}
} }
impl ToApub for Post { impl ToApub for Post {
type Response = Page; type Response = Page;
// Turn a Lemmy post into an ActivityPub page that can be sent out over the network. // Turn a Lemmy post into an ActivityPub page that can be sent out over the network.
fn to_apub(&self, conn: &PgConnection) -> Result<ResponseOrTombstone<Page>, Error> { fn to_apub(&self, conn: &PgConnection) -> Result<Page, Error> {
let mut page = Page::default(); let mut page = Page::default();
let oprops: &mut ObjectProperties = page.as_mut(); let oprops: &mut ObjectProperties = page.as_mut();
let creator = User_::read(conn, self.creator_id)?; let creator = User_::read(conn, self.creator_id)?;
@ -51,7 +55,13 @@ impl ToApub for Post {
oprops.set_updated(convert_datetime(u))?; oprops.set_updated(convert_datetime(u))?;
} }
Ok(ResponseOrTombstone::Response(page)) Ok(page)
}
}
impl ToTombstone for Post {
fn to_tombstone(&self) -> Result<Tombstone, Error> {
create_tombstone(self.deleted, &self.ap_id, self.published, self.updated)
} }
} }
@ -109,7 +119,7 @@ impl ApubObjectType for Post {
create create
.create_props .create_props
.set_actor_xsd_any_uri(creator.actor_id.to_owned())? .set_actor_xsd_any_uri(creator.actor_id.to_owned())?
.set_object_base_box(page.as_response()?.to_owned())?; .set_object_base_box(page)?;
// Insert the sent activity into the activity table // Insert the sent activity into the activity table
let activity_form = activity::ActivityForm { let activity_form = activity::ActivityForm {
@ -144,7 +154,7 @@ impl ApubObjectType for Post {
update update
.update_props .update_props
.set_actor_xsd_any_uri(creator.actor_id.to_owned())? .set_actor_xsd_any_uri(creator.actor_id.to_owned())?
.set_object_base_box(page.as_response()?.to_owned())?; .set_object_base_box(page)?;
// Insert the sent activity into the activity table // Insert the sent activity into the activity table
let activity_form = activity::ActivityForm { let activity_form = activity::ActivityForm {
@ -163,6 +173,32 @@ impl ApubObjectType for Post {
)?; )?;
Ok(()) Ok(())
} }
fn send_delete(&self, actor: &User_, conn: &PgConnection) -> Result<(), Error> {
let mut delete = Delete::default();
delete
.delete_props
.set_actor_xsd_any_uri(actor.actor_id.to_owned())?
.set_object_base_box(BaseBox::from_concrete(self.to_tombstone()?)?)?;
// 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,
&actor.private_key.to_owned().unwrap(),
&actor.actor_id,
community.get_follower_inboxes(&conn)?,
)?;
Ok(())
}
} }
impl ApubLikeableType for Post { impl ApubLikeableType for Post {
@ -176,7 +212,7 @@ impl ApubLikeableType for Post {
like like
.like_props .like_props
.set_actor_xsd_any_uri(creator.actor_id.to_owned())? .set_actor_xsd_any_uri(creator.actor_id.to_owned())?
.set_object_base_box(page.as_response()?.to_owned())?; .set_object_base_box(page)?;
// Insert the sent activity into the activity table // Insert the sent activity into the activity table
let activity_form = activity::ActivityForm { let activity_form = activity::ActivityForm {
@ -210,7 +246,7 @@ impl ApubLikeableType for Post {
dislike dislike
.dislike_props .dislike_props
.set_actor_xsd_any_uri(creator.actor_id.to_owned())? .set_actor_xsd_any_uri(creator.actor_id.to_owned())?
.set_object_base_box(page.as_response()?.to_owned())?; .set_object_base_box(page)?;
// Insert the sent activity into the activity table // Insert the sent activity into the activity table
let activity_form = activity::ActivityForm { let activity_form = activity::ActivityForm {

View file

@ -66,6 +66,7 @@ pub async fn shared_inbox(
receive_dislike_comment(&d, &request, &conn, chat_server) receive_dislike_comment(&d, &request, &conn, chat_server)
} }
(SharedAcceptedObjects::Delete(d), Some("Tombstone")) => { (SharedAcceptedObjects::Delete(d), Some("Tombstone")) => {
// TODO: is this deleting a community, post, comment or what?
receive_delete_community(&d, &request, &conn, chat_server) receive_delete_community(&d, &request, &conn, chat_server)
} }
_ => Err(format_err!("Unknown incoming activity type.")), _ => Err(format_err!("Unknown incoming activity type.")),

View file

@ -9,7 +9,7 @@ impl ToApub for User_ {
type Response = PersonExt; type Response = PersonExt;
// Turn a Lemmy Community into an ActivityPub group that can be sent out over the network. // Turn a Lemmy Community into an ActivityPub group that can be sent out over the network.
fn to_apub(&self, _conn: &PgConnection) -> Result<ResponseOrTombstone<PersonExt>, Error> { fn to_apub(&self, _conn: &PgConnection) -> Result<PersonExt, Error> {
// TODO go through all these to_string and to_owned() // TODO go through all these to_string and to_owned()
let mut person = Person::default(); let mut person = Person::default();
let oprops: &mut ObjectProperties = person.as_mut(); let oprops: &mut ObjectProperties = person.as_mut();
@ -41,9 +41,7 @@ impl ToApub for User_ {
.set_following(self.get_following_url())? .set_following(self.get_following_url())?
.set_liked(self.get_liked_url())?; .set_liked(self.get_liked_url())?;
Ok(ResponseOrTombstone::Response( Ok(person.extend(actor_props).extend(self.get_public_key_ext()))
person.extend(actor_props).extend(self.get_public_key_ext()),
))
} }
} }