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();
// Scan the comment for user mentions, add those rows
expires,
};
ModRemoveCommunity::create(&conn, &form)?;
- updated_community.send_delete(&conn)?;
}
- if let Some(_deleted) = data.deleted.to_owned() {
- updated_community.send_delete(&conn)?;
+ if let Some(deleted) = data.deleted.to_owned() {
+ if deleted {
+ updated_community.send_delete(&conn)?;
+ } else {
+ // TODO: undo delete
+ }
}
let community_view = CommunityView::read(&conn, data.edit_id, Some(user_id))?;
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 res = PostResponse { post: post_view };
impl ToApub for Comment {
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 oprops: &mut ObjectProperties = comment.as_mut();
let creator = User_::read(&conn, self.creator_id)?;
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)
}
}
create
.create_props
.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
let activity_form = activity::ActivityForm {
update
.update_props
.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
let activity_form = activity::ActivityForm {
)?;
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 {
like
.like_props
.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
let activity_form = activity::ActivityForm {
dislike
.dislike_props
.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
let activity_form = activity::ActivityForm {
type Response = GroupExt;
// 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> {
- 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)));
- }
-
+ fn to_apub(&self, conn: &PgConnection) -> Result<GroupExt, Error> {
let mut group = Group::default();
let oprops: &mut ObjectProperties = group.as_mut();
.set_endpoints(endpoint_props)?
.set_followers(self.get_followers_url())?;
- Ok(ResponseOrTombstone::Response(
- group.extend(actor_props).extend(self.get_public_key_ext()),
- ))
+ Ok(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)
}
}
}
fn send_delete(&self, conn: &PgConnection) -> Result<(), Error> {
- let community = self.to_apub(conn)?;
let mut delete = Delete::default();
delete
.delete_props
.set_actor_xsd_any_uri(self.actor_id.to_owned())?
- .set_object_base_box(BaseBox::from_concrete(
- community.as_tombstone()?.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 {
db: DbPoolParam,
) -> Result<HttpResponse<Body>, Error> {
let community = Community::read_from_name(&&db.get()?, &info.community_name)?;
- let c = community.to_apub(&db.get().unwrap())?;
- Ok(create_apub_response(&c))
+ if !community.deleted {
+ 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).
use crate::{convert_datetime, naive_now, Settings};
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 signatures::verify;
use signatures::{sign, PublicKey, PublicKeyExtension};
.content_type(APUB_JSON_CONTENT_TYPE)
.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.
///
}
}
-#[derive(Serialize)]
-pub enum ResponseOrTombstone<Response> {
- Response(Response),
- Tombstone(Box<Tombstone>),
+// TODO Not sure good names for these
+pub trait ToApub {
+ type Response;
+ fn to_apub(&self, conn: &PgConnection) -> Result<Self::Response, Error>;
}
-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")),
+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"
+ ))
}
}
-// TODO Not sure good names for these
-pub trait ToApub {
- type Response;
- fn to_apub(&self, conn: &PgConnection) -> Result<ResponseOrTombstone<Self::Response>, Error>;
+pub trait ToTombstone {
+ fn to_tombstone(&self) -> Result<Tombstone, Error>;
}
pub trait FromApub {
pub trait ApubObjectType {
fn send_create(&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 {
) -> Result<HttpResponse<Body>, Error> {
let id = info.post_id.parse::<i32>()?;
let post = Post::read(&&db.get()?, id)?;
- Ok(create_apub_response(&post.to_apub(&db.get().unwrap())?))
+ if !post.deleted {
+ Ok(create_apub_response(&post.to_apub(&db.get().unwrap())?))
+ } else {
+ Ok(create_apub_tombstone_response(&post.to_tombstone()?))
+ }
}
impl ToApub for Post {
type Response = Page;
// 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 oprops: &mut ObjectProperties = page.as_mut();
let creator = User_::read(conn, self.creator_id)?;
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)
}
}
create
.create_props
.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
let activity_form = activity::ActivityForm {
update
.update_props
.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
let activity_form = activity::ActivityForm {
)?;
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 {
like
.like_props
.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
let activity_form = activity::ActivityForm {
dislike
.dislike_props
.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
let activity_form = activity::ActivityForm {
receive_dislike_comment(&d, &request, &conn, chat_server)
}
(SharedAcceptedObjects::Delete(d), Some("Tombstone")) => {
+ // TODO: is this deleting a community, post, comment or what?
receive_delete_community(&d, &request, &conn, chat_server)
}
_ => Err(format_err!("Unknown incoming activity type.")),
type Response = PersonExt;
// 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()
let mut person = Person::default();
let oprops: &mut ObjectProperties = person.as_mut();
.set_following(self.get_following_url())?
.set_liked(self.get_liked_url())?;
- Ok(ResponseOrTombstone::Response(
- person.extend(actor_props).extend(self.get_public_key_ext()),
- ))
+ Ok(person.extend(actor_props).extend(self.get_public_key_ext()))
}
}