From 850ac6e495f29f768279a77d08a5a151592af44c Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Wed, 27 Oct 2021 14:10:55 +0200 Subject: [PATCH] Rewrite tombstone --- .cargo-husky/hooks/pre-commit | 2 +- crates/apub/src/objects/comment.rs | 12 +++----- crates/apub/src/objects/community.rs | 12 +++----- crates/apub/src/objects/mod.rs | 36 ++-------------------- crates/apub/src/objects/post.rs | 15 +++------ crates/apub/src/objects/private_message.rs | 11 ++----- crates/apub/src/objects/tombstone.rs | 33 ++++++++++++++++++++ 7 files changed, 55 insertions(+), 66 deletions(-) create mode 100644 crates/apub/src/objects/tombstone.rs diff --git a/.cargo-husky/hooks/pre-commit b/.cargo-husky/hooks/pre-commit index 1c2858d4e..4b50f2a5b 100755 --- a/.cargo-husky/hooks/pre-commit +++ b/.cargo-husky/hooks/pre-commit @@ -1,7 +1,7 @@ #!/bin/bash set -e -cargo +nightly fmt -- --check +cargo +nightly fmt cargo +nightly clippy --workspace --tests --all-targets --all-features -- \ -D warnings -D deprecated -D clippy::perf -D clippy::complexity -D clippy::dbg_macro diff --git a/crates/apub/src/objects/comment.rs b/crates/apub/src/objects/comment.rs index 7dd04dd2c..b79f53e06 100644 --- a/crates/apub/src/objects/comment.rs +++ b/crates/apub/src/objects/comment.rs @@ -2,13 +2,13 @@ use crate::{ activities::{verify_is_public, verify_person_in_community}, context::lemmy_context, fetcher::object_id::ObjectId, - objects::{create_tombstone, person::ApubPerson, post::ApubPost, Source}, + objects::{person::ApubPerson, post::ApubPost, tombstone::Tombstone, Source}, PostOrComment, }; use activitystreams::{ base::AnyBase, chrono::NaiveDateTime, - object::{kind::NoteType, Tombstone}, + object::kind::NoteType, primitives::OneOrMany, public, unparsed::Unparsed, @@ -223,12 +223,10 @@ impl ApubObject for ApubComment { } fn to_tombstone(&self) -> Result { - create_tombstone( - self.deleted, - self.ap_id.to_owned().into(), - self.updated, + Ok(Tombstone::new( NoteType::Note, - ) + self.updated.unwrap_or(self.published), + )) } /// Converts a `Note` to `Comment`. diff --git a/crates/apub/src/objects/community.rs b/crates/apub/src/objects/community.rs index bcd2f1adb..e71addba5 100644 --- a/crates/apub/src/objects/community.rs +++ b/crates/apub/src/objects/community.rs @@ -9,14 +9,14 @@ use crate::{ fetcher::object_id::ObjectId, generate_moderators_url, generate_outbox_url, - objects::{create_tombstone, get_summary_from_string_or_source, ImageObject, Source}, + objects::{get_summary_from_string_or_source, tombstone::Tombstone, ImageObject, Source}, CommunityType, }; use activitystreams::{ actor::{kind::GroupType, Endpoints}, base::AnyBase, chrono::NaiveDateTime, - object::{kind::ImageType, Tombstone}, + object::kind::ImageType, primitives::OneOrMany, unparsed::Unparsed, }; @@ -215,12 +215,10 @@ impl ApubObject for ApubCommunity { } fn to_tombstone(&self) -> Result { - create_tombstone( - self.deleted, - self.actor_id.to_owned().into(), - self.updated, + Ok(Tombstone::new( GroupType::Group, - ) + self.updated.unwrap_or(self.published), + )) } /// Converts a `Group` to `Community`, inserts it into the database and updates moderators. diff --git a/crates/apub/src/objects/mod.rs b/crates/apub/src/objects/mod.rs index 76c258ce2..96700cb71 100644 --- a/crates/apub/src/objects/mod.rs +++ b/crates/apub/src/objects/mod.rs @@ -1,12 +1,6 @@ -use activitystreams::{ - base::BaseExt, - object::{kind::ImageType, Tombstone, TombstoneExt}, -}; -use anyhow::anyhow; -use chrono::NaiveDateTime; +use activitystreams::object::kind::ImageType; use html2md::parse_html; use lemmy_apub_lib::values::MediaTypeMarkdown; -use lemmy_utils::{utils::convert_datetime, LemmyError}; use serde::{Deserialize, Serialize}; use url::Url; @@ -15,6 +9,7 @@ pub mod community; pub mod person; pub mod post; pub mod private_message; +pub mod tombstone; #[derive(Clone, Debug, Deserialize, Serialize)] #[serde(rename_all = "camelCase")] @@ -31,31 +26,6 @@ pub struct ImageObject { url: Url, } -/// Updated is actually the deletion time -fn create_tombstone( - deleted: bool, - object_id: Url, - updated: Option, - former_type: T, -) -> Result -where - T: ToString, -{ - if deleted { - if let Some(updated) = updated { - let mut tombstone = Tombstone::new(); - tombstone.set_id(object_id); - tombstone.set_former_type(former_type.to_string()); - tombstone.set_deleted(convert_datetime(updated)); - Ok(tombstone) - } else { - Err(anyhow!("Cant convert to tombstone because updated time was None.").into()) - } - } else { - Err(anyhow!("Cant convert object to tombstone if it wasnt deleted").into()) - } -} - fn get_summary_from_string_or_source( raw: &Option, source: &Option, @@ -69,7 +39,6 @@ fn get_summary_from_string_or_source( #[cfg(test)] mod tests { - use super::*; use actix::Actor; use diesel::{ r2d2::{ConnectionManager, Pool}, @@ -85,6 +54,7 @@ mod tests { rate_limit::{rate_limiter::RateLimiter, RateLimit}, request::build_user_agent, settings::structs::Settings, + LemmyError, }; use lemmy_websocket::{chat_server::ChatServer, LemmyContext}; use reqwest::Client; diff --git a/crates/apub/src/objects/post.rs b/crates/apub/src/objects/post.rs index 0814bf46d..8b0161883 100644 --- a/crates/apub/src/objects/post.rs +++ b/crates/apub/src/objects/post.rs @@ -2,14 +2,11 @@ use crate::{ activities::{extract_community, verify_is_public, verify_person_in_community}, context::lemmy_context, fetcher::object_id::ObjectId, - objects::{create_tombstone, person::ApubPerson, ImageObject, Source}, + objects::{person::ApubPerson, tombstone::Tombstone, ImageObject, Source}, }; use activitystreams::{ base::AnyBase, - object::{ - kind::{ImageType, PageType}, - Tombstone, - }, + object::kind::{ImageType, PageType}, primitives::OneOrMany, public, unparsed::Unparsed, @@ -202,12 +199,10 @@ impl ApubObject for ApubPost { } fn to_tombstone(&self) -> Result { - create_tombstone( - self.deleted, - self.ap_id.to_owned().into(), - self.updated, + Ok(Tombstone::new( PageType::Page, - ) + self.updated.unwrap_or(self.published), + )) } async fn from_apub( diff --git a/crates/apub/src/objects/private_message.rs b/crates/apub/src/objects/private_message.rs index 146dd7125..407ae8101 100644 --- a/crates/apub/src/objects/private_message.rs +++ b/crates/apub/src/objects/private_message.rs @@ -1,12 +1,12 @@ use crate::{ context::lemmy_context, fetcher::object_id::ObjectId, - objects::{create_tombstone, person::ApubPerson, Source}, + objects::{person::ApubPerson, Source}, }; use activitystreams::{ base::AnyBase, chrono::NaiveDateTime, - object::{kind::NoteType, Tombstone}, + object::Tombstone, primitives::OneOrMany, unparsed::Unparsed, }; @@ -156,12 +156,7 @@ impl ApubObject for ApubPrivateMessage { } fn to_tombstone(&self) -> Result { - create_tombstone( - self.deleted, - self.ap_id.to_owned().into(), - self.updated, - NoteType::Note, - ) + unimplemented!() } async fn from_apub( diff --git a/crates/apub/src/objects/tombstone.rs b/crates/apub/src/objects/tombstone.rs new file mode 100644 index 000000000..fc78c9682 --- /dev/null +++ b/crates/apub/src/objects/tombstone.rs @@ -0,0 +1,33 @@ +use crate::context::lemmy_context; +use activitystreams::{ + base::AnyBase, + chrono::{DateTime, FixedOffset, NaiveDateTime}, + object::kind::TombstoneType, + primitives::OneOrMany, +}; +use lemmy_utils::utils::convert_datetime; +use serde::{Deserialize, Serialize}; +use serde_with::skip_serializing_none; + +#[skip_serializing_none] +#[derive(Clone, Debug, Deserialize, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct Tombstone { + #[serde(rename = "@context")] + context: OneOrMany, + #[serde(rename = "type")] + kind: TombstoneType, + former_type: String, + deleted: DateTime, +} + +impl Tombstone { + pub fn new(former_type: T, updated_time: NaiveDateTime) -> Tombstone { + Tombstone { + context: lemmy_context(), + kind: TombstoneType::Tombstone, + former_type: former_type.to_string(), + deleted: convert_datetime(updated_time), + } + } +}