From 494fcfdb8fb2a6db18872005ce2061bc7ee6876b Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Tue, 28 Jul 2020 18:47:26 +0200 Subject: [PATCH] Add helper function to generate proper activity IDs --- server/src/apub/activities.rs | 26 +++++-- server/src/apub/comment.rs | 72 ++++++-------------- server/src/apub/community.rs | 56 +++++---------- server/src/apub/fetcher.rs | 15 +--- server/src/apub/inbox/activities/announce.rs | 12 +--- server/src/apub/inbox/activities/create.rs | 11 +-- server/src/apub/inbox/activities/delete.rs | 12 +--- server/src/apub/inbox/activities/dislike.rs | 11 +-- server/src/apub/inbox/activities/like.rs | 11 +-- server/src/apub/inbox/activities/remove.rs | 12 +--- server/src/apub/inbox/activities/undo.rs | 15 ++-- server/src/apub/inbox/activities/update.rs | 11 +-- server/src/apub/inbox/community_inbox.rs | 3 +- server/src/apub/inbox/shared_inbox.rs | 15 ++-- server/src/apub/inbox/user_inbox.rs | 9 +-- server/src/apub/mod.rs | 14 ++-- server/src/apub/post.rs | 71 ++++++------------- server/src/apub/private_message.rs | 39 ++++------- server/src/apub/user.rs | 33 +++++---- 19 files changed, 152 insertions(+), 296 deletions(-) diff --git a/server/src/apub/activities.rs b/server/src/apub/activities.rs index a14e6ec3..9cc20af8 100644 --- a/server/src/apub/activities.rs +++ b/server/src/apub/activities.rs @@ -1,20 +1,18 @@ use crate::{ apub::{ - community::do_announce, - extensions::signatures::sign, - insert_activity, - is_apub_id_valid, + community::do_announce, extensions::signatures::sign, insert_activity, is_apub_id_valid, ActorType, }, request::retry_custom, - DbPool, - LemmyError, + DbPool, LemmyError, }; use activitystreams_new::base::AnyBase; use actix_web::client::Client; use lemmy_db::{community::Community, user::User_}; +use lemmy_utils::{get_apub_protocol_string, settings::Settings}; use log::debug; -use url::Url; +use url::{ParseError, Url}; +use uuid::Uuid; pub async fn send_activity_to_community( creator: &User_, @@ -68,3 +66,17 @@ pub async fn send_activity( Ok(()) } + +pub(in crate::apub) fn generate_activity_id(kind: T) -> Result +where + T: ToString, +{ + let id = format!( + "{}://{}/activities/{}/{}", + get_apub_protocol_string(), + Settings::get().hostname, + kind.to_string().to_lowercase(), + Uuid::new_v4() + ); + Url::parse(&id) +} diff --git a/server/src/apub/comment.rs b/server/src/apub/comment.rs index 774abba6..61a1d15e 100644 --- a/server/src/apub/comment.rs +++ b/server/src/apub/comment.rs @@ -1,28 +1,22 @@ use crate::{ apub::{ - activities::send_activity_to_community, - create_apub_response, - create_apub_tombstone_response, - create_tombstone, - fetch_webfinger_url, + activities::{generate_activity_id, send_activity_to_community}, + create_apub_response, create_apub_tombstone_response, create_tombstone, fetch_webfinger_url, fetcher::{ - get_or_fetch_and_insert_remote_comment, - get_or_fetch_and_insert_remote_post, + get_or_fetch_and_insert_remote_comment, get_or_fetch_and_insert_remote_post, get_or_fetch_and_upsert_remote_user, }, - ActorType, - ApubLikeableType, - ApubObjectType, - FromApub, - ToApub, + ActorType, ApubLikeableType, ApubObjectType, FromApub, ToApub, }, blocking, routes::DbPoolParam, - DbPool, - LemmyError, + DbPool, LemmyError, }; use activitystreams_new::{ - activity::{Create, Delete, Dislike, Like, Remove, Undo, Update}, + activity::{ + kind::{CreateType, DeleteType, DislikeType, LikeType, RemoveType, UndoType, UpdateType}, + Create, Delete, Dislike, Like, Remove, Undo, Update, + }, base::AnyBase, context, link::Mention, @@ -109,12 +103,7 @@ impl ToApub for Comment { } fn to_tombstone(&self) -> Result { - create_tombstone( - self.deleted, - &self.ap_id, - self.updated, - NoteType::Note.to_string(), - ) + create_tombstone(self.deleted, &self.ap_id, self.updated, NoteType::Note) } } @@ -204,11 +193,10 @@ impl ApubObjectType for Comment { let maa = collect_non_local_mentions_and_addresses(&self.content, &community, client, pool).await?; - let id = format!("{}/create/{}", self.ap_id, uuid::Uuid::new_v4()); let mut create = Create::new(creator.actor_id.to_owned(), note.into_any_base()?); create .set_context(context()) - .set_id(Url::parse(&id)?) + .set_id(generate_activity_id(CreateType::Create)?) .set_to(public()) .set_many_ccs(maa.addressed_ccs.to_owned()) // Set the mention tags @@ -244,11 +232,10 @@ impl ApubObjectType for Comment { let maa = collect_non_local_mentions_and_addresses(&self.content, &community, client, pool).await?; - let id = format!("{}/update/{}", self.ap_id, uuid::Uuid::new_v4()); let mut update = Update::new(creator.actor_id.to_owned(), note.into_any_base()?); update .set_context(context()) - .set_id(Url::parse(&id)?) + .set_id(generate_activity_id(UpdateType::Update)?) .set_to(public()) .set_many_ccs(maa.addressed_ccs.to_owned()) // Set the mention tags @@ -280,11 +267,10 @@ impl ApubObjectType for Comment { let community_id = post.community_id; let community = blocking(pool, move |conn| Community::read(conn, community_id)).await??; - let id = format!("{}/delete/{}", self.ap_id, uuid::Uuid::new_v4()); let mut delete = Delete::new(creator.actor_id.to_owned(), note.into_any_base()?); delete .set_context(context()) - .set_id(Url::parse(&id)?) + .set_id(generate_activity_id(DeleteType::Delete)?) .set_to(public()) .set_many_ccs(vec![community.get_followers_url()]); @@ -315,21 +301,18 @@ impl ApubObjectType for Comment { let community = blocking(pool, move |conn| Community::read(conn, community_id)).await??; // Generate a fake delete activity, with the correct object - let id = format!("{}/delete/{}", self.ap_id, uuid::Uuid::new_v4()); let mut delete = Delete::new(creator.actor_id.to_owned(), note.into_any_base()?); delete .set_context(context()) - .set_id(Url::parse(&id)?) + .set_id(generate_activity_id(DeleteType::Delete)?) .set_to(public()) .set_many_ccs(vec![community.get_followers_url()]); - // TODO // Undo that fake activity - let undo_id = format!("{}/undo/delete/{}", self.ap_id, uuid::Uuid::new_v4()); let mut undo = Undo::new(creator.actor_id.to_owned(), delete.into_any_base()?); undo .set_context(context()) - .set_id(Url::parse(&undo_id)?) + .set_id(generate_activity_id(UndoType::Undo)?) .set_to(public()) .set_many_ccs(vec![community.get_followers_url()]); @@ -359,11 +342,10 @@ impl ApubObjectType for Comment { let community_id = post.community_id; let community = blocking(pool, move |conn| Community::read(conn, community_id)).await??; - let id = format!("{}/remove/{}", self.ap_id, uuid::Uuid::new_v4()); let mut remove = Remove::new(mod_.actor_id.to_owned(), note.into_any_base()?); remove .set_context(context()) - .set_id(Url::parse(&id)?) + .set_id(generate_activity_id(RemoveType::Remove)?) .set_to(public()) .set_many_ccs(vec![community.get_followers_url()]); @@ -394,20 +376,18 @@ impl ApubObjectType for Comment { let community = blocking(pool, move |conn| Community::read(conn, community_id)).await??; // Generate a fake delete activity, with the correct object - let id = format!("{}/remove/{}", self.ap_id, uuid::Uuid::new_v4()); let mut remove = Remove::new(mod_.actor_id.to_owned(), note.into_any_base()?); remove .set_context(context()) - .set_id(Url::parse(&id)?) + .set_id(generate_activity_id(RemoveType::Remove)?) .set_to(public()) .set_many_ccs(vec![community.get_followers_url()]); // Undo that fake activity - let undo_id = format!("{}/undo/remove/{}", self.ap_id, uuid::Uuid::new_v4()); let mut undo = Undo::new(mod_.actor_id.to_owned(), remove.into_any_base()?); undo .set_context(context()) - .set_id(Url::parse(&undo_id)?) + .set_id(generate_activity_id(UndoType::Undo)?) .set_to(public()) .set_many_ccs(vec![community.get_followers_url()]); @@ -440,12 +420,10 @@ impl ApubLikeableType for Comment { let community_id = post.community_id; let community = blocking(pool, move |conn| Community::read(conn, community_id)).await??; - let id = format!("{}/like/{}", self.ap_id, uuid::Uuid::new_v4()); - let mut like = Like::new(creator.actor_id.to_owned(), note.into_any_base()?); like .set_context(context()) - .set_id(Url::parse(&id)?) + .set_id(generate_activity_id(LikeType::Like)?) .set_to(public()) .set_many_ccs(vec![community.get_followers_url()]); @@ -475,12 +453,10 @@ impl ApubLikeableType for Comment { let community_id = post.community_id; let community = blocking(pool, move |conn| Community::read(conn, community_id)).await??; - let id = format!("{}/dislike/{}", self.ap_id, uuid::Uuid::new_v4()); - let mut dislike = Dislike::new(creator.actor_id.to_owned(), note.into_any_base()?); dislike .set_context(context()) - .set_id(Url::parse(&id)?) + .set_id(generate_activity_id(DislikeType::Dislike)?) .set_to(public()) .set_many_ccs(vec![community.get_followers_url()]); @@ -510,22 +486,18 @@ impl ApubLikeableType for Comment { let community_id = post.community_id; let community = blocking(pool, move |conn| Community::read(conn, community_id)).await??; - let id = format!("{}/dislike/{}", self.ap_id, uuid::Uuid::new_v4()); - let mut like = Like::new(creator.actor_id.to_owned(), note.into_any_base()?); like .set_context(context()) - .set_id(Url::parse(&id)?) + .set_id(generate_activity_id(DislikeType::Dislike)?) .set_to(public()) .set_many_ccs(vec![community.get_followers_url()]); - // TODO // Undo that fake activity - let undo_id = format!("{}/undo/like/{}", self.ap_id, uuid::Uuid::new_v4()); let mut undo = Undo::new(creator.actor_id.to_owned(), like.into_any_base()?); undo .set_context(context()) - .set_id(Url::parse(&undo_id)?) + .set_id(generate_activity_id(UndoType::Undo)?) .set_to(public()) .set_many_ccs(vec![community.get_followers_url()]); diff --git a/server/src/apub/community.rs b/server/src/apub/community.rs index a3f58f5d..16d13711 100644 --- a/server/src/apub/community.rs +++ b/server/src/apub/community.rs @@ -1,26 +1,21 @@ use crate::{ apub::{ - activities::send_activity, - create_apub_response, - create_apub_tombstone_response, - create_tombstone, + activities::{generate_activity_id, send_activity}, + create_apub_response, create_apub_tombstone_response, create_tombstone, extensions::group_extensions::GroupExtension, fetcher::get_or_fetch_and_upsert_remote_user, - get_shared_inbox, - insert_activity, - ActorType, - FromApub, - GroupExt, - ToApub, + get_shared_inbox, insert_activity, ActorType, FromApub, GroupExt, ToApub, }, blocking, routes::DbPoolParam, - DbPool, - LemmyError, + DbPool, LemmyError, }; use activitystreams_ext::Ext2; use activitystreams_new::{ - activity::{Accept, Announce, Delete, Follow, Remove, Undo}, + activity::{ + kind::{AcceptType, AnnounceType, DeleteType, LikeType, RemoveType, UndoType}, + Accept, Announce, Delete, Follow, Remove, Undo, + }, actor::{kind::GroupType, ApActor, Endpoints, Group}, base::{AnyBase, BaseExt}, collection::UnorderedCollection, @@ -107,12 +102,7 @@ impl ToApub for Community { } fn to_tombstone(&self) -> Result { - create_tombstone( - self.deleted, - &self.actor_id, - self.updated, - GroupType::Group.to_string(), - ) + create_tombstone(self.deleted, &self.actor_id, self.updated, GroupType::Group) } } @@ -137,13 +127,12 @@ impl ActorType for Community { pool: &DbPool, ) -> Result<(), LemmyError> { let actor_uri = follow.actor()?.as_single_xsd_any_uri().unwrap().to_string(); - let id = format!("{}/accept/{}", self.actor_id, uuid::Uuid::new_v4()); let mut accept = Accept::new(self.actor_id.to_owned(), follow.into_any_base()?); let to = format!("{}/inbox", actor_uri); accept .set_context(context()) - .set_id(Url::parse(&id)?) + .set_id(generate_activity_id(AcceptType::Accept)?) .set_to(to.clone()); insert_activity(self.creator_id, accept.clone(), true, pool).await?; @@ -160,12 +149,10 @@ impl ActorType for Community { ) -> Result<(), LemmyError> { let group = self.to_apub(pool).await?; - let id = format!("{}/delete/{}", self.actor_id, uuid::Uuid::new_v4()); - let mut delete = Delete::new(creator.actor_id.to_owned(), group.into_any_base()?); delete .set_context(context()) - .set_id(Url::parse(&id)?) + .set_id(generate_activity_id(DeleteType::Delete)?) .set_to(public()) .set_many_ccs(vec![self.get_followers_url()]); @@ -188,22 +175,19 @@ impl ActorType for Community { ) -> Result<(), LemmyError> { let group = self.to_apub(pool).await?; - let id = format!("{}/delete/{}", self.actor_id, uuid::Uuid::new_v4()); - let mut delete = Delete::new(creator.actor_id.to_owned(), group.into_any_base()?); delete .set_context(context()) - .set_id(Url::parse(&id)?) + .set_id(generate_activity_id(DeleteType::Delete)?) .set_to(public()) .set_many_ccs(vec![self.get_followers_url()]); // TODO // Undo that fake activity - let undo_id = format!("{}/undo/delete/{}", self.actor_id, uuid::Uuid::new_v4()); let mut undo = Undo::new(creator.actor_id.to_owned(), delete.into_any_base()?); undo .set_context(context()) - .set_id(Url::parse(&undo_id)?) + .set_id(generate_activity_id(UndoType::Undo)?) .set_to(public()) .set_many_ccs(vec![self.get_followers_url()]); @@ -226,12 +210,10 @@ impl ActorType for Community { ) -> Result<(), LemmyError> { let group = self.to_apub(pool).await?; - let id = format!("{}/remove/{}", self.actor_id, uuid::Uuid::new_v4()); - let mut remove = Remove::new(mod_.actor_id.to_owned(), group.into_any_base()?); remove .set_context(context()) - .set_id(Url::parse(&id)?) + .set_id(generate_activity_id(RemoveType::Remove)?) .set_to(public()) .set_many_ccs(vec![self.get_followers_url()]); @@ -254,21 +236,18 @@ impl ActorType for Community { ) -> Result<(), LemmyError> { let group = self.to_apub(pool).await?; - let id = format!("{}/remove/{}", self.actor_id, uuid::Uuid::new_v4()); - let mut remove = Remove::new(mod_.actor_id.to_owned(), group.into_any_base()?); remove .set_context(context()) - .set_id(Url::parse(&id)?) + .set_id(generate_activity_id(RemoveType::Remove)?) .set_to(public()) .set_many_ccs(vec![self.get_followers_url()]); // Undo that fake activity - let undo_id = format!("{}/undo/remove/{}", self.actor_id, uuid::Uuid::new_v4()); let mut undo = Undo::new(mod_.actor_id.to_owned(), remove.into_any_base()?); undo .set_context(context()) - .set_id(Url::parse(&undo_id)?) + .set_id(generate_activity_id(LikeType::Like)?) .set_to(public()) .set_many_ccs(vec![self.get_followers_url()]); @@ -435,11 +414,10 @@ pub async fn do_announce( client: &Client, pool: &DbPool, ) -> Result<(), LemmyError> { - let id = format!("{}/announce/{}", community.actor_id, uuid::Uuid::new_v4()); let mut announce = Announce::new(community.actor_id.to_owned(), activity); announce .set_context(context()) - .set_id(Url::parse(&id)?) + .set_id(generate_activity_id(AnnounceType::Announce)?) .set_to(public()) .set_many_ccs(vec![community.get_followers_url()]); diff --git a/server/src/apub/fetcher.rs b/server/src/apub/fetcher.rs index 5fd39415..cf198636 100644 --- a/server/src/apub/fetcher.rs +++ b/server/src/apub/fetcher.rs @@ -1,19 +1,12 @@ use crate::{ api::site::SearchResponse, apub::{ - is_apub_id_valid, - ActorType, - FromApub, - GroupExt, - PageExt, - PersonExt, - APUB_JSON_CONTENT_TYPE, + is_apub_id_valid, ActorType, FromApub, GroupExt, PageExt, PersonExt, APUB_JSON_CONTENT_TYPE, }, blocking, request::{retry, RecvError}, routes::nodeinfo::{NodeInfo, NodeInfoWellKnown}, - DbPool, - LemmyError, + DbPool, LemmyError, }; use activitystreams_new::{base::BaseExt, object::Note, prelude::*}; use actix_web::client::Client; @@ -29,9 +22,7 @@ use lemmy_db::{ post_view::PostView, user::{UserForm, User_}, user_view::UserView, - Crud, - Joinable, - SearchType, + Crud, Joinable, SearchType, }; use lemmy_utils::get_apub_protocol_string; use log::debug; diff --git a/server/src/apub/inbox/activities/announce.rs b/server/src/apub/inbox/activities/announce.rs index 78a005fb..ae135183 100644 --- a/server/src/apub/inbox/activities/announce.rs +++ b/server/src/apub/inbox/activities/announce.rs @@ -1,19 +1,13 @@ use crate::{ apub::inbox::{ activities::{ - create::receive_create, - delete::receive_delete, - dislike::receive_dislike, - like::receive_like, - remove::receive_remove, - undo::receive_undo, - update::receive_update, + create::receive_create, delete::receive_delete, dislike::receive_dislike, like::receive_like, + remove::receive_remove, undo::receive_undo, update::receive_update, }, shared_inbox::receive_unhandled_activity, }, routes::ChatServerParam, - DbPool, - LemmyError, + DbPool, LemmyError, }; use activitystreams_new::{activity::*, base::AnyBase, prelude::ExtendsExt}; use actix_web::{client::Client, HttpResponse}; diff --git a/server/src/apub/inbox/activities/create.rs b/server/src/apub/inbox/activities/create.rs index da90bea5..4390b1a8 100644 --- a/server/src/apub/inbox/activities/create.rs +++ b/server/src/apub/inbox/activities/create.rs @@ -5,13 +5,9 @@ use crate::{ }, apub::{ inbox::shared_inbox::{ - announce_if_community_is_local, - get_user_from_activity, - receive_unhandled_activity, + announce_if_community_is_local, get_user_from_activity, receive_unhandled_activity, }, - ActorType, - FromApub, - PageExt, + ActorType, FromApub, PageExt, }, blocking, routes::ChatServerParam, @@ -19,8 +15,7 @@ use crate::{ server::{SendComment, SendPost}, UserOperation, }, - DbPool, - LemmyError, + DbPool, LemmyError, }; use activitystreams_new::{activity::Create, base::AnyBase, object::Note, prelude::*}; use actix_web::{client::Client, HttpResponse}; diff --git a/server/src/apub/inbox/activities/delete.rs b/server/src/apub/inbox/activities/delete.rs index 4b072ebd..82078653 100644 --- a/server/src/apub/inbox/activities/delete.rs +++ b/server/src/apub/inbox/activities/delete.rs @@ -3,14 +3,9 @@ use crate::{ apub::{ fetcher::{get_or_fetch_and_insert_remote_comment, get_or_fetch_and_insert_remote_post}, inbox::shared_inbox::{ - announce_if_community_is_local, - get_user_from_activity, - receive_unhandled_activity, + announce_if_community_is_local, get_user_from_activity, receive_unhandled_activity, }, - ActorType, - FromApub, - GroupExt, - PageExt, + ActorType, FromApub, GroupExt, PageExt, }, blocking, routes::ChatServerParam, @@ -18,8 +13,7 @@ use crate::{ server::{SendComment, SendCommunityRoomMessage, SendPost}, UserOperation, }, - DbPool, - LemmyError, + DbPool, LemmyError, }; use activitystreams_new::{activity::Delete, base::AnyBase, object::Note, prelude::*}; use actix_web::{client::Client, HttpResponse}; diff --git a/server/src/apub/inbox/activities/dislike.rs b/server/src/apub/inbox/activities/dislike.rs index 94790220..bbb868e7 100644 --- a/server/src/apub/inbox/activities/dislike.rs +++ b/server/src/apub/inbox/activities/dislike.rs @@ -3,13 +3,9 @@ use crate::{ apub::{ fetcher::{get_or_fetch_and_insert_remote_comment, get_or_fetch_and_insert_remote_post}, inbox::shared_inbox::{ - announce_if_community_is_local, - get_user_from_activity, - receive_unhandled_activity, + announce_if_community_is_local, get_user_from_activity, receive_unhandled_activity, }, - ActorType, - FromApub, - PageExt, + ActorType, FromApub, PageExt, }, blocking, routes::ChatServerParam, @@ -17,8 +13,7 @@ use crate::{ server::{SendComment, SendPost}, UserOperation, }, - DbPool, - LemmyError, + DbPool, LemmyError, }; use activitystreams_new::{activity::Dislike, base::AnyBase, object::Note, prelude::*}; use actix_web::{client::Client, HttpResponse}; diff --git a/server/src/apub/inbox/activities/like.rs b/server/src/apub/inbox/activities/like.rs index 1df20a05..893b68c3 100644 --- a/server/src/apub/inbox/activities/like.rs +++ b/server/src/apub/inbox/activities/like.rs @@ -3,13 +3,9 @@ use crate::{ apub::{ fetcher::{get_or_fetch_and_insert_remote_comment, get_or_fetch_and_insert_remote_post}, inbox::shared_inbox::{ - announce_if_community_is_local, - get_user_from_activity, - receive_unhandled_activity, + announce_if_community_is_local, get_user_from_activity, receive_unhandled_activity, }, - ActorType, - FromApub, - PageExt, + ActorType, FromApub, PageExt, }, blocking, routes::ChatServerParam, @@ -17,8 +13,7 @@ use crate::{ server::{SendComment, SendPost}, UserOperation, }, - DbPool, - LemmyError, + DbPool, LemmyError, }; use activitystreams_new::{activity::Like, base::AnyBase, object::Note, prelude::*}; use actix_web::{client::Client, HttpResponse}; diff --git a/server/src/apub/inbox/activities/remove.rs b/server/src/apub/inbox/activities/remove.rs index cb2a1292..8b1630d8 100644 --- a/server/src/apub/inbox/activities/remove.rs +++ b/server/src/apub/inbox/activities/remove.rs @@ -3,14 +3,9 @@ use crate::{ apub::{ fetcher::{get_or_fetch_and_insert_remote_comment, get_or_fetch_and_insert_remote_post}, inbox::shared_inbox::{ - announce_if_community_is_local, - get_user_from_activity, - receive_unhandled_activity, + announce_if_community_is_local, get_user_from_activity, receive_unhandled_activity, }, - ActorType, - FromApub, - GroupExt, - PageExt, + ActorType, FromApub, GroupExt, PageExt, }, blocking, routes::ChatServerParam, @@ -18,8 +13,7 @@ use crate::{ server::{SendComment, SendCommunityRoomMessage, SendPost}, UserOperation, }, - DbPool, - LemmyError, + DbPool, LemmyError, }; use activitystreams_new::{activity::Remove, base::AnyBase, object::Note, prelude::*}; use actix_web::{client::Client, HttpResponse}; diff --git a/server/src/apub/inbox/activities/undo.rs b/server/src/apub/inbox/activities/undo.rs index 3c5bdb68..ad62a8f6 100644 --- a/server/src/apub/inbox/activities/undo.rs +++ b/server/src/apub/inbox/activities/undo.rs @@ -3,14 +3,9 @@ use crate::{ apub::{ fetcher::{get_or_fetch_and_insert_remote_comment, get_or_fetch_and_insert_remote_post}, inbox::shared_inbox::{ - announce_if_community_is_local, - get_user_from_activity, - receive_unhandled_activity, + announce_if_community_is_local, get_user_from_activity, receive_unhandled_activity, }, - ActorType, - FromApub, - GroupExt, - PageExt, + ActorType, FromApub, GroupExt, PageExt, }, blocking, routes::ChatServerParam, @@ -18,8 +13,7 @@ use crate::{ server::{SendComment, SendCommunityRoomMessage, SendPost}, UserOperation, }, - DbPool, - LemmyError, + DbPool, LemmyError, }; use activitystreams_new::{activity::*, base::AnyBase, object::Note, prelude::*}; use actix_web::{client::Client, HttpResponse}; @@ -31,8 +25,7 @@ use lemmy_db::{ naive_now, post::{Post, PostForm, PostLike, PostLikeForm}, post_view::PostView, - Crud, - Likeable, + Crud, Likeable, }; pub async fn receive_undo( diff --git a/server/src/apub/inbox/activities/update.rs b/server/src/apub/inbox/activities/update.rs index f46c7ff7..306d8ef7 100644 --- a/server/src/apub/inbox/activities/update.rs +++ b/server/src/apub/inbox/activities/update.rs @@ -6,13 +6,9 @@ use crate::{ apub::{ fetcher::{get_or_fetch_and_insert_remote_comment, get_or_fetch_and_insert_remote_post}, inbox::shared_inbox::{ - announce_if_community_is_local, - get_user_from_activity, - receive_unhandled_activity, + announce_if_community_is_local, get_user_from_activity, receive_unhandled_activity, }, - ActorType, - FromApub, - PageExt, + ActorType, FromApub, PageExt, }, blocking, routes::ChatServerParam, @@ -20,8 +16,7 @@ use crate::{ server::{SendComment, SendPost}, UserOperation, }, - DbPool, - LemmyError, + DbPool, LemmyError, }; use activitystreams_new::{activity::Update, base::AnyBase, object::Note, prelude::*}; use actix_web::{client::Client, HttpResponse}; diff --git a/server/src/apub/inbox/community_inbox.rs b/server/src/apub/inbox/community_inbox.rs index 3b823674..44a1c949 100644 --- a/server/src/apub/inbox/community_inbox.rs +++ b/server/src/apub/inbox/community_inbox.rs @@ -2,8 +2,7 @@ use crate::{ apub::{ extensions::signatures::verify, fetcher::{get_or_fetch_and_upsert_remote_community, get_or_fetch_and_upsert_remote_user}, - insert_activity, - ActorType, + insert_activity, ActorType, }, blocking, routes::{ChatServerParam, DbPoolParam}, diff --git a/server/src/apub/inbox/shared_inbox.rs b/server/src/apub/inbox/shared_inbox.rs index f406d83a..eb3d521b 100644 --- a/server/src/apub/inbox/shared_inbox.rs +++ b/server/src/apub/inbox/shared_inbox.rs @@ -3,25 +3,18 @@ use crate::{ community::do_announce, extensions::signatures::verify, fetcher::{ - get_or_fetch_and_upsert_remote_actor, - get_or_fetch_and_upsert_remote_community, + get_or_fetch_and_upsert_remote_actor, get_or_fetch_and_upsert_remote_community, get_or_fetch_and_upsert_remote_user, }, inbox::activities::{ - announce::receive_announce, - create::receive_create, - delete::receive_delete, - dislike::receive_dislike, - like::receive_like, - remove::receive_remove, - undo::receive_undo, + announce::receive_announce, create::receive_create, delete::receive_delete, + dislike::receive_dislike, like::receive_like, remove::receive_remove, undo::receive_undo, update::receive_update, }, insert_activity, }, routes::{ChatServerParam, DbPoolParam}, - DbPool, - LemmyError, + DbPool, LemmyError, }; use activitystreams_new::{ activity::{ActorAndObject, ActorAndObjectRef}, diff --git a/server/src/apub/inbox/user_inbox.rs b/server/src/apub/inbox/user_inbox.rs index 7b589b28..73916ee2 100644 --- a/server/src/apub/inbox/user_inbox.rs +++ b/server/src/apub/inbox/user_inbox.rs @@ -3,14 +3,12 @@ use crate::{ apub::{ extensions::signatures::verify, fetcher::{get_or_fetch_and_upsert_remote_community, get_or_fetch_and_upsert_remote_user}, - insert_activity, - FromApub, + insert_activity, FromApub, }, blocking, routes::{ChatServerParam, DbPoolParam}, websocket::{server::SendUserRoomMessage, UserOperation}, - DbPool, - LemmyError, + DbPool, LemmyError, }; use activitystreams_new::{ activity::{Accept, Create, Delete, Undo, Update}, @@ -24,8 +22,7 @@ use lemmy_db::{ private_message::{PrivateMessage, PrivateMessageForm}, private_message_view::PrivateMessageView, user::User_, - Crud, - Followable, + Crud, Followable, }; use log::debug; use serde::Deserialize; diff --git a/server/src/apub/mod.rs b/server/src/apub/mod.rs index d27dc97c..a5d8984c 100644 --- a/server/src/apub/mod.rs +++ b/server/src/apub/mod.rs @@ -17,8 +17,7 @@ use crate::{ blocking, request::{retry, RecvError}, routes::webfinger::WebFingerResponse, - DbPool, - LemmyError, + DbPool, LemmyError, }; use activitystreams_ext::{Ext1, Ext2}; use activitystreams_new::{ @@ -101,17 +100,20 @@ pub trait ToApub { } /// Updated is actually the deletion time -fn create_tombstone( +fn create_tombstone( deleted: bool, object_id: &str, updated: Option, - former_type: String, -) -> Result { + former_type: T, +) -> Result +where + T: ToString, +{ if deleted { if let Some(updated) = updated { let mut tombstone = Tombstone::new(); tombstone.set_id(object_id.parse()?); - tombstone.set_former_type(former_type); + tombstone.set_former_type(former_type.to_string()); tombstone.set_deleted(convert_datetime(updated)); Ok(tombstone) } else { diff --git a/server/src/apub/post.rs b/server/src/apub/post.rs index 9575a6c7..aca2984b 100644 --- a/server/src/apub/post.rs +++ b/server/src/apub/post.rs @@ -1,26 +1,21 @@ use crate::{ apub::{ - activities::send_activity_to_community, - create_apub_response, - create_apub_tombstone_response, - create_tombstone, + activities::{generate_activity_id, send_activity_to_community}, + create_apub_response, create_apub_tombstone_response, create_tombstone, extensions::page_extension::PageExtension, fetcher::{get_or_fetch_and_upsert_remote_community, get_or_fetch_and_upsert_remote_user}, - ActorType, - ApubLikeableType, - ApubObjectType, - FromApub, - PageExt, - ToApub, + ActorType, ApubLikeableType, ApubObjectType, FromApub, PageExt, ToApub, }, blocking, routes::DbPoolParam, - DbPool, - LemmyError, + DbPool, LemmyError, }; use activitystreams_ext::Ext1; use activitystreams_new::{ - activity::{Create, Delete, Dislike, Like, Remove, Undo, Update}, + activity::{ + kind::{CreateType, DeleteType, DislikeType, LikeType, RemoveType, UndoType, UpdateType}, + Create, Delete, Dislike, Like, Remove, Undo, Update, + }, context, object::{kind::PageType, Image, Page, Tombstone}, prelude::*, @@ -139,12 +134,7 @@ impl ToApub for Post { } fn to_tombstone(&self) -> Result { - create_tombstone( - self.deleted, - &self.ap_id, - self.updated, - PageType::Page.to_string(), - ) + create_tombstone(self.deleted, &self.ap_id, self.updated, PageType::Page) } } @@ -274,12 +264,10 @@ impl ApubObjectType for Post { let community_id = self.community_id; let community = blocking(pool, move |conn| Community::read(conn, community_id)).await??; - let id = format!("{}/create/{}", self.ap_id, uuid::Uuid::new_v4()); - let mut create = Create::new(creator.actor_id.to_owned(), page.into_any_base()?); create .set_context(context()) - .set_id(Url::parse(&id)?) + .set_id(generate_activity_id(CreateType::Create)?) .set_to(public()) .set_many_ccs(vec![community.get_followers_url()]); @@ -307,12 +295,10 @@ impl ApubObjectType for Post { let community_id = self.community_id; let community = blocking(pool, move |conn| Community::read(conn, community_id)).await??; - let id = format!("{}/update/{}", self.ap_id, uuid::Uuid::new_v4()); - let mut update = Update::new(creator.actor_id.to_owned(), page.into_any_base()?); update .set_context(context()) - .set_id(Url::parse(&id)?) + .set_id(generate_activity_id(UpdateType::Update)?) .set_to(public()) .set_many_ccs(vec![community.get_followers_url()]); @@ -339,11 +325,10 @@ impl ApubObjectType for Post { let community_id = self.community_id; let community = blocking(pool, move |conn| Community::read(conn, community_id)).await??; - let id = format!("{}/delete/{}", self.ap_id, uuid::Uuid::new_v4()); let mut delete = Delete::new(creator.actor_id.to_owned(), page.into_any_base()?); delete .set_context(context()) - .set_id(Url::parse(&id)?) + .set_id(generate_activity_id(DeleteType::Delete)?) .set_to(public()) .set_many_ccs(vec![community.get_followers_url()]); @@ -370,21 +355,18 @@ impl ApubObjectType for Post { let community_id = self.community_id; let community = blocking(pool, move |conn| Community::read(conn, community_id)).await??; - let id = format!("{}/delete/{}", self.ap_id, uuid::Uuid::new_v4()); let mut delete = Delete::new(creator.actor_id.to_owned(), page.into_any_base()?); delete .set_context(context()) - .set_id(Url::parse(&id)?) + .set_id(generate_activity_id(DeleteType::Delete)?) .set_to(public()) .set_many_ccs(vec![community.get_followers_url()]); - // TODO // Undo that fake activity - let undo_id = format!("{}/undo/delete/{}", self.ap_id, uuid::Uuid::new_v4()); let mut undo = Undo::new(creator.actor_id.to_owned(), delete.into_any_base()?); undo .set_context(context()) - .set_id(Url::parse(&undo_id)?) + .set_id(generate_activity_id(UndoType::Undo)?) .set_to(public()) .set_many_ccs(vec![community.get_followers_url()]); @@ -411,11 +393,10 @@ impl ApubObjectType for Post { let community_id = self.community_id; let community = blocking(pool, move |conn| Community::read(conn, community_id)).await??; - let id = format!("{}/remove/{}", self.ap_id, uuid::Uuid::new_v4()); let mut remove = Remove::new(mod_.actor_id.to_owned(), page.into_any_base()?); remove .set_context(context()) - .set_id(Url::parse(&id)?) + .set_id(generate_activity_id(RemoveType::Remove)?) .set_to(public()) .set_many_ccs(vec![community.get_followers_url()]); @@ -442,20 +423,18 @@ impl ApubObjectType for Post { let community_id = self.community_id; let community = blocking(pool, move |conn| Community::read(conn, community_id)).await??; - let id = format!("{}/remove/{}", self.ap_id, uuid::Uuid::new_v4()); let mut remove = Remove::new(mod_.actor_id.to_owned(), page.into_any_base()?); remove .set_context(context()) - .set_id(Url::parse(&id)?) + .set_id(generate_activity_id(RemoveType::Remove)?) .set_to(public()) .set_many_ccs(vec![community.get_followers_url()]); // Undo that fake activity - let undo_id = format!("{}/undo/remove/{}", self.ap_id, uuid::Uuid::new_v4()); let mut undo = Undo::new(mod_.actor_id.to_owned(), remove.into_any_base()?); undo .set_context(context()) - .set_id(Url::parse(&undo_id)?) + .set_id(generate_activity_id(UndoType::Undo)?) .set_to(public()) .set_many_ccs(vec![community.get_followers_url()]); @@ -485,12 +464,10 @@ impl ApubLikeableType for Post { let community_id = self.community_id; let community = blocking(pool, move |conn| Community::read(conn, community_id)).await??; - let id = format!("{}/like/{}", self.ap_id, uuid::Uuid::new_v4()); - let mut like = Like::new(creator.actor_id.to_owned(), page.into_any_base()?); like .set_context(context()) - .set_id(Url::parse(&id)?) + .set_id(generate_activity_id(LikeType::Like)?) .set_to(public()) .set_many_ccs(vec![community.get_followers_url()]); @@ -517,12 +494,10 @@ impl ApubLikeableType for Post { let community_id = self.community_id; let community = blocking(pool, move |conn| Community::read(conn, community_id)).await??; - let id = format!("{}/dislike/{}", self.ap_id, uuid::Uuid::new_v4()); - let mut dislike = Dislike::new(creator.actor_id.to_owned(), page.into_any_base()?); dislike .set_context(context()) - .set_id(Url::parse(&id)?) + .set_id(generate_activity_id(DislikeType::Dislike)?) .set_to(public()) .set_many_ccs(vec![community.get_followers_url()]); @@ -549,22 +524,18 @@ impl ApubLikeableType for Post { let community_id = self.community_id; let community = blocking(pool, move |conn| Community::read(conn, community_id)).await??; - let id = format!("{}/like/{}", self.ap_id, uuid::Uuid::new_v4()); - let mut like = Like::new(creator.actor_id.to_owned(), page.into_any_base()?); like .set_context(context()) - .set_id(Url::parse(&id)?) + .set_id(generate_activity_id(LikeType::Like)?) .set_to(public()) .set_many_ccs(vec![community.get_followers_url()]); - // TODO // Undo that fake activity - let undo_id = format!("{}/undo/like/{}", self.ap_id, uuid::Uuid::new_v4()); let mut undo = Undo::new(creator.actor_id.to_owned(), like.into_any_base()?); undo .set_context(context()) - .set_id(Url::parse(&undo_id)?) + .set_id(generate_activity_id(UndoType::Undo)?) .set_to(public()) .set_many_ccs(vec![community.get_followers_url()]); diff --git a/server/src/apub/private_message.rs b/server/src/apub/private_message.rs index 94cadeec..b10f87b5 100644 --- a/server/src/apub/private_message.rs +++ b/server/src/apub/private_message.rs @@ -1,19 +1,17 @@ use crate::{ apub::{ - activities::send_activity, + activities::{generate_activity_id, send_activity}, create_tombstone, fetcher::get_or_fetch_and_upsert_remote_user, - insert_activity, - ApubObjectType, - FromApub, - ToApub, + insert_activity, ApubObjectType, FromApub, ToApub, }, - blocking, - DbPool, - LemmyError, + blocking, DbPool, LemmyError, }; use activitystreams_new::{ - activity::{Create, Delete, Undo, Update}, + activity::{ + kind::{CreateType, DeleteType, UndoType, UpdateType}, + Create, Delete, Undo, Update, + }, context, object::{kind::NoteType, Note, Tombstone}, prelude::*, @@ -56,12 +54,7 @@ impl ToApub for PrivateMessage { } fn to_tombstone(&self) -> Result { - create_tombstone( - self.deleted, - &self.ap_id, - self.updated, - NoteType::Note.to_string(), - ) + create_tombstone(self.deleted, &self.ap_id, self.updated, NoteType::Note) } } @@ -118,7 +111,6 @@ impl ApubObjectType for PrivateMessage { pool: &DbPool, ) -> Result<(), LemmyError> { let note = self.to_apub(pool).await?; - let id = format!("{}/create/{}", self.ap_id, uuid::Uuid::new_v4()); let recipient_id = self.recipient_id; let recipient = blocking(pool, move |conn| User_::read(conn, recipient_id)).await??; @@ -127,7 +119,7 @@ impl ApubObjectType for PrivateMessage { let to = format!("{}/inbox", recipient.actor_id); create .set_context(context()) - .set_id(Url::parse(&id)?) + .set_id(generate_activity_id(CreateType::Create)?) .set_to(to.clone()); insert_activity(creator.id, create.clone(), true, pool).await?; @@ -144,7 +136,6 @@ impl ApubObjectType for PrivateMessage { pool: &DbPool, ) -> Result<(), LemmyError> { let note = self.to_apub(pool).await?; - let id = format!("{}/update/{}", self.ap_id, uuid::Uuid::new_v4()); let recipient_id = self.recipient_id; let recipient = blocking(pool, move |conn| User_::read(conn, recipient_id)).await??; @@ -153,7 +144,7 @@ impl ApubObjectType for PrivateMessage { let to = format!("{}/inbox", recipient.actor_id); update .set_context(context()) - .set_id(Url::parse(&id)?) + .set_id(generate_activity_id(UpdateType::Update)?) .set_to(to.clone()); insert_activity(creator.id, update.clone(), true, pool).await?; @@ -169,7 +160,6 @@ impl ApubObjectType for PrivateMessage { pool: &DbPool, ) -> Result<(), LemmyError> { let note = self.to_apub(pool).await?; - let id = format!("{}/delete/{}", self.ap_id, uuid::Uuid::new_v4()); let recipient_id = self.recipient_id; let recipient = blocking(pool, move |conn| User_::read(conn, recipient_id)).await??; @@ -178,7 +168,7 @@ impl ApubObjectType for PrivateMessage { let to = format!("{}/inbox", recipient.actor_id); delete .set_context(context()) - .set_id(Url::parse(&id)?) + .set_id(generate_activity_id(DeleteType::Delete)?) .set_to(to.clone()); insert_activity(creator.id, delete.clone(), true, pool).await?; @@ -194,7 +184,6 @@ impl ApubObjectType for PrivateMessage { pool: &DbPool, ) -> Result<(), LemmyError> { let note = self.to_apub(pool).await?; - let id = format!("{}/delete/{}", self.ap_id, uuid::Uuid::new_v4()); let recipient_id = self.recipient_id; let recipient = blocking(pool, move |conn| User_::read(conn, recipient_id)).await??; @@ -203,16 +192,14 @@ impl ApubObjectType for PrivateMessage { let to = format!("{}/inbox", recipient.actor_id); delete .set_context(context()) - .set_id(Url::parse(&id)?) + .set_id(generate_activity_id(DeleteType::Delete)?) .set_to(to.clone()); - // TODO // Undo that fake activity - let undo_id = format!("{}/undo/delete/{}", self.ap_id, uuid::Uuid::new_v4()); let mut undo = Undo::new(creator.actor_id.to_owned(), delete.into_any_base()?); undo .set_context(context()) - .set_id(Url::parse(&undo_id)?) + .set_id(generate_activity_id(UndoType::Undo)?) .set_to(to.clone()); insert_activity(creator.id, undo.clone(), true, pool).await?; diff --git a/server/src/apub/user.rs b/server/src/apub/user.rs index 85ed1846..8ec972da 100644 --- a/server/src/apub/user.rs +++ b/server/src/apub/user.rs @@ -1,21 +1,18 @@ use crate::{ apub::{ - activities::send_activity, - create_apub_response, - insert_activity, - ActorType, - FromApub, - PersonExt, - ToApub, + activities::{generate_activity_id, send_activity}, + create_apub_response, insert_activity, ActorType, FromApub, PersonExt, ToApub, }, blocking, routes::DbPoolParam, - DbPool, - LemmyError, + DbPool, LemmyError, }; use activitystreams_ext::Ext1; use activitystreams_new::{ - activity::{Follow, Undo}, + activity::{ + kind::{FollowType, UndoType}, + Follow, Undo, + }, actor::{ApActor, Endpoints, Person}, context, object::{Image, Tombstone}, @@ -102,9 +99,10 @@ impl ActorType for User_ { client: &Client, pool: &DbPool, ) -> Result<(), LemmyError> { - let id = format!("{}/follow/{}", self.actor_id, uuid::Uuid::new_v4()); let mut follow = Follow::new(self.actor_id.to_owned(), follow_actor_id); - follow.set_context(context()).set_id(id.parse()?); + follow + .set_context(context()) + .set_id(generate_activity_id(FollowType::Follow)?); let to = format!("{}/inbox", follow_actor_id); insert_activity(self.id, follow.clone(), true, pool).await?; @@ -119,17 +117,18 @@ impl ActorType for User_ { client: &Client, pool: &DbPool, ) -> Result<(), LemmyError> { - let id = format!("{}/follow/{}", self.actor_id, uuid::Uuid::new_v4()); let mut follow = Follow::new(self.actor_id.to_owned(), follow_actor_id); - follow.set_context(context()).set_id(id.parse()?); + follow + .set_context(context()) + .set_id(generate_activity_id(FollowType::Follow)?); let to = format!("{}/inbox", follow_actor_id); - // TODO // Undo that fake activity - let undo_id = format!("{}/undo/follow/{}", self.actor_id, uuid::Uuid::new_v4()); let mut undo = Undo::new(Url::parse(&self.actor_id)?, follow.into_any_base()?); - undo.set_context(context()).set_id(undo_id.parse()?); + undo + .set_context(context()) + .set_id(generate_activity_id(UndoType::Undo)?); insert_activity(self.id, undo.clone(), true, pool).await?; -- 2.44.1