From 4e098e6fc690c554d255272b4b431a1b26ff9179 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Mon, 14 Sep 2020 16:02:07 +0200 Subject: [PATCH] In activity table, remove user_id column and add activity_id --- server/lemmy_db/src/activity.rs | 113 +++++++----------- server/lemmy_db/src/category.rs | 2 +- server/lemmy_db/src/comment_view.rs | 4 +- server/lemmy_db/src/community_view.rs | 4 +- server/lemmy_db/src/moderator_views.rs | 38 ++---- server/lemmy_db/src/post_view.rs | 6 +- server/lemmy_db/src/private_message_view.rs | 6 +- server/lemmy_db/src/schema.rs | 3 +- server/lemmy_db/src/site_view.rs | 6 +- server/lemmy_db/src/user.rs | 2 +- server/lemmy_db/src/user_mention_view.rs | 6 +- server/lemmy_db/src/user_view.rs | 6 +- .../down.sql | 3 + .../up.sql | 4 + server/src/apub/activities.rs | 11 +- server/src/apub/community.rs | 30 +++-- server/src/apub/inbox/community_inbox.rs | 6 +- server/src/apub/inbox/shared_inbox.rs | 5 +- server/src/apub/inbox/user_inbox.rs | 5 +- server/src/apub/mod.rs | 7 +- server/src/apub/private_message.rs | 20 ++-- server/src/apub/user.rs | 10 +- 22 files changed, 136 insertions(+), 161 deletions(-) create mode 100644 server/migrations/2020-09-14-135047_activity_id_column/down.sql create mode 100644 server/migrations/2020-09-14-135047_activity_id_column/up.sql diff --git a/server/lemmy_db/src/activity.rs b/server/lemmy_db/src/activity.rs index 6a5beb3bb..cbd5084b8 100644 --- a/server/lemmy_db/src/activity.rs +++ b/server/lemmy_db/src/activity.rs @@ -1,7 +1,7 @@ use crate::{schema::activity, Crud}; use diesel::{dsl::*, result::Error, *}; use log::debug; -use serde::{Serialize}; +use serde::Serialize; use serde_json::Value; use std::{ fmt::Debug, @@ -12,7 +12,7 @@ use std::{ #[table_name = "activity"] pub struct Activity { pub id: i32, - pub user_id: i32, + pub activity_id: String, pub data: Value, pub local: bool, pub published: chrono::NaiveDateTime, @@ -22,16 +22,16 @@ pub struct Activity { #[derive(Insertable, AsChangeset)] #[table_name = "activity"] pub struct ActivityForm { - pub user_id: i32, + pub activity_id: String, pub data: Value, pub local: bool, pub updated: Option, } impl Crud for Activity { - fn read(conn: &PgConnection, activity_id: i32) -> Result { + fn read(conn: &PgConnection, aid: i32) -> Result { use crate::schema::activity::dsl::*; - activity.find(activity_id).first::(conn) + activity.find(aid).first::(conn) } fn create(conn: &PgConnection, new_activity: &ActivityForm) -> Result { @@ -42,40 +42,46 @@ impl Crud for Activity { } fn update( - conn: &PgConnection, - activity_id: i32, - new_activity: &ActivityForm, + _conn: &PgConnection, + _activity_id: i32, + _new_activity: &ActivityForm, ) -> Result { - use crate::schema::activity::dsl::*; - diesel::update(activity.find(activity_id)) - .set(new_activity) - .get_result::(conn) + unimplemented!() } } -pub fn do_insert_activity( - conn: &PgConnection, - user_id: i32, - data: &T, - local: bool, -) -> Result -where - T: Serialize + Debug, -{ - debug!("inserting activity for user {}, data {:?}", user_id, &data); - let activity_form = ActivityForm { - user_id, - data: serde_json::to_value(&data)?, - local, - updated: None, - }; - let result = Activity::create(&conn, &activity_form); - match result { - Ok(s) => Ok(s), - Err(e) => Err(IoError::new( - ErrorKind::Other, - format!("Failed to insert activity into database: {}", e), - )), +impl Activity { + pub fn do_insert_activity( + conn: &PgConnection, + activity_id: String, + data: &T, + local: bool, + ) -> Result + where + T: Serialize + Debug, + { + debug!("inserting activity {} with data {:?}", activity_id, &data); + let activity_form = ActivityForm { + activity_id, + data: serde_json::to_value(&data)?, + local, + updated: None, + }; + let result = Activity::create(&conn, &activity_form); + match result { + Ok(s) => Ok(s), + Err(e) => Err(IoError::new( + ErrorKind::Other, + format!("Failed to insert activity into database: {}", e), + )), + } + } + + pub fn read_from_activity_id(conn: &PgConnection, object_id: String) -> Result { + use crate::schema::activity::dsl::*; + activity + .filter(activity_id.eq(object_id)) + .first::(conn) } } @@ -95,34 +101,7 @@ mod tests { fn test_crud() { let conn = establish_unpooled_connection(); - let creator_form = UserForm { - name: "activity_creator_pm".into(), - preferred_username: None, - password_encrypted: "nope".into(), - email: None, - matrix_user_id: None, - avatar: None, - banner: None, - admin: false, - banned: false, - updated: None, - show_nsfw: false, - theme: "darkly".into(), - default_sort_type: SortType::Hot as i16, - default_listing_type: ListingType::Subscribed as i16, - lang: "browser".into(), - show_avatars: true, - send_notifications_to_email: false, - actor_id: None, - bio: None, - local: true, - private_key: None, - public_key: None, - last_refreshed_at: None, - }; - - let inserted_creator = User_::create(&conn, &creator_form).unwrap(); - + let test_activity_id = "http://localhost/activities/123"; let test_json: Value = serde_json::from_str( r#"{ "street": "Article Circle Expressway 1", @@ -133,7 +112,7 @@ mod tests { ) .unwrap(); let activity_form = ActivityForm { - user_id: inserted_creator.id, + activity_id: test_activity_id.to_string(), data: test_json.to_owned(), local: true, updated: None, @@ -143,15 +122,15 @@ mod tests { let expected_activity = Activity { id: inserted_activity.id, - user_id: inserted_creator.id, + activity_id: test_activity_id.to_string(), data: test_json, local: true, published: inserted_activity.published, updated: None, }; - let read_activity = Activity::read(&conn, inserted_activity.id).unwrap(); - User_::delete(&conn, inserted_creator.id).unwrap(); + let read_activity = + Activity::read_from_activity_id(&conn, inserted_activity.activity_id).unwrap(); assert_eq!(expected_activity, read_activity); assert_eq!(expected_activity, inserted_activity); diff --git a/server/lemmy_db/src/category.rs b/server/lemmy_db/src/category.rs index c4165c22f..36beb9ff6 100644 --- a/server/lemmy_db/src/category.rs +++ b/server/lemmy_db/src/category.rs @@ -3,7 +3,7 @@ use crate::{ Crud, }; use diesel::{dsl::*, result::Error, *}; -use serde::{Serialize}; +use serde::Serialize; #[derive(Queryable, Identifiable, PartialEq, Debug, Serialize)] #[table_name = "category"] diff --git a/server/lemmy_db/src/comment_view.rs b/server/lemmy_db/src/comment_view.rs index d7e5c08d0..cb121cf88 100644 --- a/server/lemmy_db/src/comment_view.rs +++ b/server/lemmy_db/src/comment_view.rs @@ -84,9 +84,7 @@ table! { } } -#[derive( - Queryable, Identifiable, PartialEq, Debug, Serialize, QueryableByName, Clone, -)] +#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, QueryableByName, Clone)] #[table_name = "comment_fast_view"] pub struct CommentView { pub id: i32, diff --git a/server/lemmy_db/src/community_view.rs b/server/lemmy_db/src/community_view.rs index 1ecca0a1f..a6355504d 100644 --- a/server/lemmy_db/src/community_view.rs +++ b/server/lemmy_db/src/community_view.rs @@ -123,9 +123,7 @@ table! { } } -#[derive( - Queryable, Identifiable, PartialEq, Debug, Serialize, QueryableByName, Clone, -)] +#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, QueryableByName, Clone)] #[table_name = "community_fast_view"] pub struct CommunityView { pub id: i32, diff --git a/server/lemmy_db/src/moderator_views.rs b/server/lemmy_db/src/moderator_views.rs index 7054a05a0..efa949a4a 100644 --- a/server/lemmy_db/src/moderator_views.rs +++ b/server/lemmy_db/src/moderator_views.rs @@ -1,6 +1,6 @@ use crate::limit_and_offset; use diesel::{result::Error, *}; -use serde::{Serialize}; +use serde::Serialize; table! { mod_remove_post_view (id) { @@ -17,9 +17,7 @@ table! { } } -#[derive( - Queryable, Identifiable, PartialEq, Debug, Serialize, QueryableByName, Clone, -)] +#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, QueryableByName, Clone)] #[table_name = "mod_remove_post_view"] pub struct ModRemovePostView { pub id: i32, @@ -77,9 +75,7 @@ table! { } } -#[derive( - Queryable, Identifiable, PartialEq, Debug, Serialize, QueryableByName, Clone, -)] +#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, QueryableByName, Clone)] #[table_name = "mod_lock_post_view"] pub struct ModLockPostView { pub id: i32, @@ -136,9 +132,7 @@ table! { } } -#[derive( - Queryable, Identifiable, PartialEq, Debug, Serialize, QueryableByName, Clone, -)] +#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, QueryableByName, Clone)] #[table_name = "mod_sticky_post_view"] pub struct ModStickyPostView { pub id: i32, @@ -200,9 +194,7 @@ table! { } } -#[derive( - Queryable, Identifiable, PartialEq, Debug, Serialize, QueryableByName, Clone, -)] +#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, QueryableByName, Clone)] #[table_name = "mod_remove_comment_view"] pub struct ModRemoveCommentView { pub id: i32, @@ -264,9 +256,7 @@ table! { } } -#[derive( - Queryable, Identifiable, PartialEq, Debug, Serialize, QueryableByName, Clone, -)] +#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, QueryableByName, Clone)] #[table_name = "mod_remove_community_view"] pub struct ModRemoveCommunityView { pub id: i32, @@ -320,9 +310,7 @@ table! { } } -#[derive( - Queryable, Identifiable, PartialEq, Debug, Serialize, QueryableByName, Clone, -)] +#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, QueryableByName, Clone)] #[table_name = "mod_ban_from_community_view"] pub struct ModBanFromCommunityView { pub id: i32, @@ -381,9 +369,7 @@ table! { } } -#[derive( - Queryable, Identifiable, PartialEq, Debug, Serialize, QueryableByName, Clone, -)] +#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, QueryableByName, Clone)] #[table_name = "mod_ban_view"] pub struct ModBanView { pub id: i32, @@ -435,9 +421,7 @@ table! { } } -#[derive( - Queryable, Identifiable, PartialEq, Debug, Serialize, QueryableByName, Clone, -)] +#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, QueryableByName, Clone)] #[table_name = "mod_add_community_view"] pub struct ModAddCommunityView { pub id: i32, @@ -492,9 +476,7 @@ table! { } } -#[derive( - Queryable, Identifiable, PartialEq, Debug, Serialize, QueryableByName, Clone, -)] +#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, QueryableByName, Clone)] #[table_name = "mod_add_view"] pub struct ModAddView { pub id: i32, diff --git a/server/lemmy_db/src/post_view.rs b/server/lemmy_db/src/post_view.rs index 3d3afb709..e88a80e30 100644 --- a/server/lemmy_db/src/post_view.rs +++ b/server/lemmy_db/src/post_view.rs @@ -1,7 +1,7 @@ use super::post_view::post_fast_view::BoxedQuery; use crate::{fuzzy_search, limit_and_offset, ListingType, MaybeOptional, SortType}; use diesel::{dsl::*, pg::Pg, result::Error, *}; -use serde::{Serialize}; +use serde::Serialize; // The faked schema since diesel doesn't do views table! { @@ -106,9 +106,7 @@ table! { } } -#[derive( - Queryable, Identifiable, PartialEq, Debug, Serialize, QueryableByName, Clone, -)] +#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, QueryableByName, Clone)] #[table_name = "post_fast_view"] pub struct PostView { pub id: i32, diff --git a/server/lemmy_db/src/private_message_view.rs b/server/lemmy_db/src/private_message_view.rs index 39fe66efa..68f7df42c 100644 --- a/server/lemmy_db/src/private_message_view.rs +++ b/server/lemmy_db/src/private_message_view.rs @@ -1,6 +1,6 @@ use crate::{limit_and_offset, MaybeOptional}; use diesel::{pg::Pg, result::Error, *}; -use serde::{Serialize}; +use serde::Serialize; // The faked schema since diesel doesn't do views table! { @@ -28,9 +28,7 @@ table! { } } -#[derive( - Queryable, Identifiable, PartialEq, Debug, Serialize, QueryableByName, Clone, -)] +#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, QueryableByName, Clone)] #[table_name = "private_message_view"] pub struct PrivateMessageView { pub id: i32, diff --git a/server/lemmy_db/src/schema.rs b/server/lemmy_db/src/schema.rs index c446edd9f..69562ec04 100644 --- a/server/lemmy_db/src/schema.rs +++ b/server/lemmy_db/src/schema.rs @@ -1,11 +1,11 @@ table! { activity (id) { id -> Int4, - user_id -> Int4, data -> Jsonb, local -> Bool, published -> Timestamp, updated -> Nullable, + activity_id -> Nullable, } } @@ -480,7 +480,6 @@ table! { } } -joinable!(activity -> user_ (user_id)); joinable!(comment -> post (post_id)); joinable!(comment -> user_ (creator_id)); joinable!(comment_like -> comment (comment_id)); diff --git a/server/lemmy_db/src/site_view.rs b/server/lemmy_db/src/site_view.rs index 0edfd2b70..fd15ac778 100644 --- a/server/lemmy_db/src/site_view.rs +++ b/server/lemmy_db/src/site_view.rs @@ -1,5 +1,5 @@ use diesel::{result::Error, *}; -use serde::{Serialize}; +use serde::Serialize; table! { site_view (id) { @@ -24,9 +24,7 @@ table! { } } -#[derive( - Queryable, Identifiable, PartialEq, Debug, Serialize, QueryableByName, Clone, -)] +#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, QueryableByName, Clone)] #[table_name = "site_view"] pub struct SiteView { pub id: i32, diff --git a/server/lemmy_db/src/user.rs b/server/lemmy_db/src/user.rs index 247b5b360..986701c43 100644 --- a/server/lemmy_db/src/user.rs +++ b/server/lemmy_db/src/user.rs @@ -6,7 +6,7 @@ use crate::{ }; use bcrypt::{hash, DEFAULT_COST}; use diesel::{dsl::*, result::Error, *}; -use serde::{Serialize}; +use serde::Serialize; #[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)] #[table_name = "user_"] diff --git a/server/lemmy_db/src/user_mention_view.rs b/server/lemmy_db/src/user_mention_view.rs index c35ad3613..d1ce5ebd0 100644 --- a/server/lemmy_db/src/user_mention_view.rs +++ b/server/lemmy_db/src/user_mention_view.rs @@ -1,6 +1,6 @@ use crate::{limit_and_offset, MaybeOptional, SortType}; use diesel::{dsl::*, pg::Pg, result::Error, *}; -use serde::{Serialize}; +use serde::Serialize; // The faked schema since diesel doesn't do views table! { @@ -83,9 +83,7 @@ table! { } } -#[derive( - Queryable, Identifiable, PartialEq, Debug, Serialize, QueryableByName, Clone, -)] +#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, QueryableByName, Clone)] #[table_name = "user_mention_fast_view"] pub struct UserMentionView { pub id: i32, diff --git a/server/lemmy_db/src/user_view.rs b/server/lemmy_db/src/user_view.rs index a4f6f47ae..b0c28d31c 100644 --- a/server/lemmy_db/src/user_view.rs +++ b/server/lemmy_db/src/user_view.rs @@ -1,7 +1,7 @@ use super::user_view::user_fast::BoxedQuery; use crate::{fuzzy_search, limit_and_offset, MaybeOptional, SortType}; use diesel::{dsl::*, pg::Pg, result::Error, *}; -use serde::{Serialize}; +use serde::Serialize; table! { user_view (id) { @@ -51,9 +51,7 @@ table! { } } -#[derive( - Queryable, Identifiable, PartialEq, Debug, Serialize, QueryableByName, Clone, -)] +#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, QueryableByName, Clone)] #[table_name = "user_fast"] pub struct UserView { pub id: i32, diff --git a/server/migrations/2020-09-14-135047_activity_id_column/down.sql b/server/migrations/2020-09-14-135047_activity_id_column/down.sql new file mode 100644 index 000000000..acca353e1 --- /dev/null +++ b/server/migrations/2020-09-14-135047_activity_id_column/down.sql @@ -0,0 +1,3 @@ +-- Recreate user_id and remove activity_id +ALTER TABLE activity DROP COLUMN activity_id; +ALTER TABLE activity ADD COLUMN user_id Int4; \ No newline at end of file diff --git a/server/migrations/2020-09-14-135047_activity_id_column/up.sql b/server/migrations/2020-09-14-135047_activity_id_column/up.sql new file mode 100644 index 000000000..eca76ade2 --- /dev/null +++ b/server/migrations/2020-09-14-135047_activity_id_column/up.sql @@ -0,0 +1,4 @@ +-- For activity table, remove the user_id column and add activity_id + +ALTER TABLE activity ADD COLUMN activity_id Text; +ALTER TABLE activity DROP COLUMN user_id; \ No newline at end of file diff --git a/server/src/apub/activities.rs b/server/src/apub/activities.rs index 0ba8f8735..b08371ba2 100644 --- a/server/src/apub/activities.rs +++ b/server/src/apub/activities.rs @@ -3,11 +3,12 @@ use crate::{ LemmyContext, }; use activitystreams::{ - base::{Extends, ExtendsExt}, + base::{AsBase, BaseExt, Extends, ExtendsExt}, object::AsObject, }; +use anyhow::Context; use lemmy_db::{community::Community, user::User_}; -use lemmy_utils::{get_apub_protocol_string, settings::Settings, LemmyError}; +use lemmy_utils::{get_apub_protocol_string, location_info, settings::Settings, LemmyError}; use serde::{export::fmt::Debug, Serialize}; use url::{ParseError, Url}; use uuid::Uuid; @@ -20,12 +21,12 @@ pub async fn send_activity_to_community( context: &LemmyContext, ) -> Result<(), LemmyError> where - T: AsObject + Extends + Serialize + Debug + Send + Clone + 'static, + T: AsObject + Extends + AsBase + Serialize + Debug + Send + Clone + 'static, Kind: Serialize, >::Error: From + Send + Sync + 'static, { - // TODO: looks like call this sometimes with activity, and sometimes with any_base - insert_activity(creator.id, activity.clone(), true, context.pool()).await?; + let id = activity.id_unchecked().context(location_info!())?; + insert_activity(id, activity.clone(), true, context.pool()).await?; // if this is a local community, we need to do an announce from the community instead if community.local { diff --git a/server/src/apub/community.rs b/server/src/apub/community.rs index 8d9b4dc1c..372bd65dc 100644 --- a/server/src/apub/community.rs +++ b/server/src/apub/community.rs @@ -148,12 +148,13 @@ impl ActorType for Community { let mut accept = Accept::new(self.actor_id.to_owned(), follow.into_any_base()?); let to = actor.get_inbox_url()?; + let id = &generate_activity_id(AcceptType::Accept)?; accept .set_context(activitystreams::context()) - .set_id(generate_activity_id(AcceptType::Accept)?) + .set_id(id.to_owned()) .set_to(to.clone()); - insert_activity(self.creator_id, accept.clone(), true, context.pool()).await?; + insert_activity(id, accept.clone(), true, context.pool()).await?; send_activity(context.activity_queue(), accept, self, vec![to])?; Ok(()) @@ -163,13 +164,14 @@ impl ActorType for Community { let group = self.to_apub(context.pool()).await?; let mut delete = Delete::new(creator.actor_id.to_owned(), group.into_any_base()?); + let id = &generate_activity_id(DeleteType::Delete)?; delete .set_context(activitystreams::context()) - .set_id(generate_activity_id(DeleteType::Delete)?) + .set_id(id.to_owned()) .set_to(public()) .set_many_ccs(vec![self.get_followers_url()?]); - insert_activity(self.creator_id, delete.clone(), true, context.pool()).await?; + insert_activity(id, delete.clone(), true, context.pool()).await?; let inboxes = self.get_follower_inboxes(context.pool()).await?; @@ -195,13 +197,14 @@ impl ActorType for Community { .set_many_ccs(vec![self.get_followers_url()?]); let mut undo = Undo::new(creator.actor_id.to_owned(), delete.into_any_base()?); + let id = &generate_activity_id(UndoType::Undo)?; undo .set_context(activitystreams::context()) - .set_id(generate_activity_id(UndoType::Undo)?) + .set_id(id.to_owned()) .set_to(public()) .set_many_ccs(vec![self.get_followers_url()?]); - insert_activity(self.creator_id, undo.clone(), true, context.pool()).await?; + insert_activity(id, undo.clone(), true, context.pool()).await?; let inboxes = self.get_follower_inboxes(context.pool()).await?; @@ -216,13 +219,14 @@ impl ActorType for Community { let group = self.to_apub(context.pool()).await?; let mut remove = Remove::new(mod_.actor_id.to_owned(), group.into_any_base()?); + let id = &generate_activity_id(RemoveType::Remove)?; remove .set_context(activitystreams::context()) - .set_id(generate_activity_id(RemoveType::Remove)?) + .set_id(id.to_owned()) .set_to(public()) .set_many_ccs(vec![self.get_followers_url()?]); - insert_activity(mod_.id, remove.clone(), true, context.pool()).await?; + insert_activity(id, remove.clone(), true, context.pool()).await?; let inboxes = self.get_follower_inboxes(context.pool()).await?; @@ -245,13 +249,14 @@ impl ActorType for Community { // Undo that fake activity let mut undo = Undo::new(mod_.actor_id.to_owned(), remove.into_any_base()?); + let id = &generate_activity_id(LikeType::Like)?; undo .set_context(activitystreams::context()) - .set_id(generate_activity_id(LikeType::Like)?) + .set_id(id.to_owned()) .set_to(public()) .set_many_ccs(vec![self.get_followers_url()?]); - insert_activity(mod_.id, undo.clone(), true, context.pool()).await?; + insert_activity(id, undo.clone(), true, context.pool()).await?; let inboxes = self.get_follower_inboxes(context.pool()).await?; @@ -493,13 +498,14 @@ pub async fn do_announce( context: &LemmyContext, ) -> Result<(), LemmyError> { let mut announce = Announce::new(community.actor_id.to_owned(), activity); + let id = &generate_activity_id(AnnounceType::Announce)?; announce .set_context(activitystreams::context()) - .set_id(generate_activity_id(AnnounceType::Announce)?) + .set_id(id.to_owned()) .set_to(public()) .set_many_ccs(vec![community.get_followers_url()?]); - insert_activity(community.creator_id, announce.clone(), true, context.pool()).await?; + insert_activity(id, announce.clone(), true, context.pool()).await?; let mut to: Vec = community.get_follower_inboxes(context.pool()).await?; diff --git a/server/src/apub/inbox/community_inbox.rs b/server/src/apub/inbox/community_inbox.rs index 0631f9391..7f1a2c21e 100644 --- a/server/src/apub/inbox/community_inbox.rs +++ b/server/src/apub/inbox/community_inbox.rs @@ -75,13 +75,15 @@ pub async fn community_inbox( let any_base = activity.clone().into_any_base()?; let kind = activity.kind().context(location_info!())?; - let user_id = user.id; let res = match kind { ValidTypes::Follow => handle_follow(any_base, user, community, &context).await, ValidTypes::Undo => handle_undo_follow(any_base, user, community, &context).await, }; - insert_activity(user_id, activity.clone(), false, context.pool()).await?; + let id = activity + .id(sender.domain().context(location_info!())?)? + .context(location_info!())?; + insert_activity(id, activity.clone(), false, context.pool()).await?; res } diff --git a/server/src/apub/inbox/shared_inbox.rs b/server/src/apub/inbox/shared_inbox.rs index da7951082..3f1136455 100644 --- a/server/src/apub/inbox/shared_inbox.rs +++ b/server/src/apub/inbox/shared_inbox.rs @@ -93,7 +93,10 @@ pub async fn shared_inbox( ValidTypes::Undo => receive_undo(any_base, &context).await, }; - insert_activity(actor.user_id(), activity.clone(), false, context.pool()).await?; + let id = activity + .id(sender.domain().context(location_info!())?)? + .context(location_info!())?; + insert_activity(id, activity.clone(), false, context.pool()).await?; res } diff --git a/server/src/apub/inbox/user_inbox.rs b/server/src/apub/inbox/user_inbox.rs index 7ea95833e..85f77fcac 100644 --- a/server/src/apub/inbox/user_inbox.rs +++ b/server/src/apub/inbox/user_inbox.rs @@ -76,7 +76,10 @@ pub async fn user_inbox( ValidTypes::Undo => receive_undo_delete_private_message(any_base, &context).await, }; - insert_activity(actor.user_id(), activity.clone(), false, context.pool()).await?; + let id = activity + .id(sender.domain().context(location_info!())?)? + .context(location_info!())?; + insert_activity(id, activity.clone(), false, context.pool()).await?; res } diff --git a/server/src/apub/mod.rs b/server/src/apub/mod.rs index b3b161c7d..f7d0be02e 100644 --- a/server/src/apub/mod.rs +++ b/server/src/apub/mod.rs @@ -33,7 +33,7 @@ use activitystreams_ext::{Ext1, Ext2}; use actix_web::{body::Body, HttpResponse}; use anyhow::{anyhow, Context}; use chrono::NaiveDateTime; -use lemmy_db::{activity::do_insert_activity, user::User_}; +use lemmy_db::{activity::Activity, user::User_}; use lemmy_utils::{ convert_datetime, get_apub_protocol_string, @@ -349,7 +349,7 @@ pub async fn fetch_webfinger_url( } pub async fn insert_activity( - user_id: i32, + activity_id: &Url, data: T, local: bool, pool: &DbPool, @@ -357,8 +357,9 @@ pub async fn insert_activity( where T: Serialize + std::fmt::Debug + Send + 'static, { + let activity_id = activity_id.to_string(); blocking(pool, move |conn| { - do_insert_activity(conn, user_id, &data, local) + Activity::do_insert_activity(conn, activity_id, &data, local) }) .await??; Ok(()) diff --git a/server/src/apub/private_message.rs b/server/src/apub/private_message.rs index 5563aef36..b54a1ac9c 100644 --- a/server/src/apub/private_message.rs +++ b/server/src/apub/private_message.rs @@ -127,12 +127,13 @@ impl ApubObjectType for PrivateMessage { let mut create = Create::new(creator.actor_id.to_owned(), note.into_any_base()?); let to = recipient.get_inbox_url()?; + let id = &generate_activity_id(CreateType::Create)?; create .set_context(activitystreams::context()) - .set_id(generate_activity_id(CreateType::Create)?) + .set_id(id.to_owned()) .set_to(to.clone()); - insert_activity(creator.id, create.clone(), true, context.pool()).await?; + insert_activity(id, create.clone(), true, context.pool()).await?; send_activity(context.activity_queue(), create, creator, vec![to])?; Ok(()) @@ -147,12 +148,13 @@ impl ApubObjectType for PrivateMessage { let mut update = Update::new(creator.actor_id.to_owned(), note.into_any_base()?); let to = recipient.get_inbox_url()?; + let id = &generate_activity_id(UpdateType::Update)?; update .set_context(activitystreams::context()) - .set_id(generate_activity_id(UpdateType::Update)?) + .set_id(id.to_owned()) .set_to(to.clone()); - insert_activity(creator.id, update.clone(), true, context.pool()).await?; + insert_activity(id, update.clone(), true, context.pool()).await?; send_activity(context.activity_queue(), update, creator, vec![to])?; Ok(()) @@ -166,12 +168,13 @@ impl ApubObjectType for PrivateMessage { let mut delete = Delete::new(creator.actor_id.to_owned(), note.into_any_base()?); let to = recipient.get_inbox_url()?; + let id = &generate_activity_id(DeleteType::Delete)?; delete .set_context(activitystreams::context()) - .set_id(generate_activity_id(DeleteType::Delete)?) + .set_id(id.to_owned()) .set_to(to.clone()); - insert_activity(creator.id, delete.clone(), true, context.pool()).await?; + insert_activity(id, delete.clone(), true, context.pool()).await?; send_activity(context.activity_queue(), delete, creator, vec![to])?; Ok(()) @@ -196,12 +199,13 @@ impl ApubObjectType for PrivateMessage { // Undo that fake activity let mut undo = Undo::new(creator.actor_id.to_owned(), delete.into_any_base()?); + let id = &generate_activity_id(UndoType::Undo)?; undo .set_context(activitystreams::context()) - .set_id(generate_activity_id(UndoType::Undo)?) + .set_id(id.to_owned()) .set_to(to.clone()); - insert_activity(creator.id, undo.clone(), true, context.pool()).await?; + insert_activity(id, undo.clone(), true, context.pool()).await?; send_activity(context.activity_queue(), undo, creator, vec![to])?; Ok(()) diff --git a/server/src/apub/user.rs b/server/src/apub/user.rs index 5522a3413..ed4fa1146 100644 --- a/server/src/apub/user.rs +++ b/server/src/apub/user.rs @@ -119,13 +119,14 @@ impl ActorType for User_ { context: &LemmyContext, ) -> Result<(), LemmyError> { let mut follow = Follow::new(self.actor_id.to_owned(), follow_actor_id.as_str()); + let id = &generate_activity_id(FollowType::Follow)?; follow .set_context(activitystreams::context()) - .set_id(generate_activity_id(FollowType::Follow)?); + .set_id(id.to_owned()); let follow_actor = get_or_fetch_and_upsert_actor(follow_actor_id, context).await?; let to = follow_actor.get_inbox_url()?; - insert_activity(self.id, follow.clone(), true, context.pool()).await?; + insert_activity(id, follow.clone(), true, context.pool()).await?; send_activity(context.activity_queue(), follow, self, vec![to])?; Ok(()) @@ -146,11 +147,12 @@ impl ActorType for User_ { // Undo that fake activity let mut undo = Undo::new(Url::parse(&self.actor_id)?, follow.into_any_base()?); + let id = &generate_activity_id(UndoType::Undo)?; undo .set_context(activitystreams::context()) - .set_id(generate_activity_id(UndoType::Undo)?); + .set_id(id.to_owned()); - insert_activity(self.id, undo.clone(), true, context.pool()).await?; + insert_activity(id, undo.clone(), true, context.pool()).await?; send_activity(context.activity_queue(), undo, self, vec![to])?; Ok(())