]> Untitled Git - lemmy.git/commitdiff
Add helper function to generate proper activity IDs
authorFelix Ableitner <me@nutomic.com>
Tue, 28 Jul 2020 16:47:26 +0000 (18:47 +0200)
committerFelix Ableitner <me@nutomic.com>
Tue, 28 Jul 2020 17:23:16 +0000 (19:23 +0200)
19 files changed:
server/src/apub/activities.rs
server/src/apub/comment.rs
server/src/apub/community.rs
server/src/apub/fetcher.rs
server/src/apub/inbox/activities/announce.rs
server/src/apub/inbox/activities/create.rs
server/src/apub/inbox/activities/delete.rs
server/src/apub/inbox/activities/dislike.rs
server/src/apub/inbox/activities/like.rs
server/src/apub/inbox/activities/remove.rs
server/src/apub/inbox/activities/undo.rs
server/src/apub/inbox/activities/update.rs
server/src/apub/inbox/community_inbox.rs
server/src/apub/inbox/shared_inbox.rs
server/src/apub/inbox/user_inbox.rs
server/src/apub/mod.rs
server/src/apub/post.rs
server/src/apub/private_message.rs
server/src/apub/user.rs

index a14e6ec37324a77d139ac979afae878a9d86456f..9cc20af80f8844e297036cdcbfe234f665ce87b7 100644 (file)
@@ -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<T>(kind: T) -> Result<Url, ParseError>
+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)
+}
index 774abba6bc68f825e636c93e87a729db627f43b0..61a1d15e4ddf0c3a81209de1161b5a68f9ecbe9e 100644 (file)
@@ -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<Tombstone, LemmyError> {
-    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()]);
 
index a3f58f5d43fdc81b7a9f46f083641f3bc840ea63..16d13711e7c177850fb0bbc07125f121a3354a47 100644 (file)
@@ -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<Tombstone, LemmyError> {
-    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()]);
 
index 5fd3941571239779b359228fa69befbdaf18615f..cf1986365d25c42db2c56952101936b498b4a27a 100644 (file)
@@ -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;
index 78a005fb17618a97e1cb0a5b05c70421a1f70696..ae1351836d56d0be84e6c505e7ff2e8223d8b6e7 100644 (file)
@@ -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};
index da90bea53109a198d323052ddc6b74a8d8c04161..4390b1a82da388b0e5b01dc13df991524c2c7d2b 100644 (file)
@@ -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};
index 4b072ebdd3e3eebe03fd18ddd294742a6dbf0dd8..82078653991b3dca4064a1957fe2faf34bbf63de 100644 (file)
@@ -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};
index 94790220c98230f6c7d716cf6a05698f65cd0f22..bbb868e7d8d7127e28e7668c801ea4633fe1ca1b 100644 (file)
@@ -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};
index 1df20a0563e391d23fb7e071774c60fcdd83afbf..893b68c31f4a5c8a695b62ec9ca73bc0c318fe19 100644 (file)
@@ -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};
index cb2a1292b5d1f87eef20dae1ecc2d48809779b39..8b1630d80e8a970e486582b5d8cc803def331afe 100644 (file)
@@ -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};
index 3c5bdb68b4b1170c35b841f4acb766ec64a51a68..ad62a8f6efe4f0d7304e6cf79eeea6e8ff9005d2 100644 (file)
@@ -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(
index f46c7ff78cc7f434bbf02a590afaccb9443b72bf..306d8ef71ea210a2d1129c5b1177e7423f2389ee 100644 (file)
@@ -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};
index 3b823674937d083799a7ab87c3cc3bff60425c66..44a1c949dfa8275d50e6a068ab57b87fd8cd8d49 100644 (file)
@@ -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},
index f406d83ac40953acd1431f993226dfdddba4e2fe..eb3d521bb3f3709b1c811a30a91730a6790898b4 100644 (file)
@@ -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},
index 7b589b28c88b26817630c55779331b48bb44231c..73916ee2cdaa8fd11c066b5f1dec5fa8a444b806 100644 (file)
@@ -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;
index d27dc97cc8915d8187fc68528e282d4670e8bc59..a5d8984c8a2cf4ac5044626722b098c37be7a9e4 100644 (file)
@@ -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<T>(
   deleted: bool,
   object_id: &str,
   updated: Option<NaiveDateTime>,
-  former_type: String,
-) -> Result<Tombstone, LemmyError> {
+  former_type: T,
+) -> Result<Tombstone, LemmyError>
+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 {
index 9575a6c7c7d5aff708a2422a7f54970ff6271f58..aca2984b31c90ae0fb7ea720d312d725c93a250e 100644 (file)
@@ -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<Tombstone, LemmyError> {
-    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()]);
 
index 94cadeec2786abadd380d61efa5d311f083b403d..b10f87b5504ee03aa73f5358266540d9a9db462e 100644 (file)
@@ -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<Tombstone, LemmyError> {
-    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?;
index 85ed1846cec7023404d89dee968c577cbc984465..8ec972dadb69d0b6db363fe9449c66f23aa8107f 100644 (file)
@@ -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?;