From 3c0ca63e5d73d4e6baac22933270d75eaba042cb Mon Sep 17 00:00:00 2001 From: Felix Date: Thu, 14 May 2020 14:26:44 +0200 Subject: [PATCH] Add helper function for Activity::create() --- server/src/apub/comment.rs | 81 ++---------- server/src/apub/community.rs | 45 +------ server/src/apub/community_inbox.rs | 18 +-- server/src/apub/mod.rs | 3 +- server/src/apub/post.rs | 81 ++---------- server/src/apub/private_message.rs | 36 +----- server/src/apub/shared_inbox.rs | 198 ++++------------------------- server/src/apub/user.rs | 18 +-- server/src/apub/user_inbox.rs | 45 +------ server/src/db/activity.rs | 23 +++- 10 files changed, 82 insertions(+), 466 deletions(-) diff --git a/server/src/apub/comment.rs b/server/src/apub/comment.rs index 17da45a6b..9160c7efb 100644 --- a/server/src/apub/comment.rs +++ b/server/src/apub/comment.rs @@ -113,14 +113,7 @@ impl ApubObjectType for Comment { .set_actor_xsd_any_uri(creator.actor_id.to_owned())? .set_object_base_box(note)?; - // Insert the sent activity into the activity table - let activity_form = activity::ActivityForm { - user_id: creator.id, - data: serde_json::to_value(&create)?, - local: true, - updated: None, - }; - activity::Activity::create(&conn, &activity_form)?; + insert_activity(&conn, creator.id, &create, true)?; send_activity( &create, @@ -149,14 +142,7 @@ impl ApubObjectType for Comment { .set_actor_xsd_any_uri(creator.actor_id.to_owned())? .set_object_base_box(note)?; - // Insert the sent activity into the activity table - let activity_form = activity::ActivityForm { - user_id: creator.id, - data: serde_json::to_value(&update)?, - local: true, - updated: None, - }; - activity::Activity::create(&conn, &activity_form)?; + insert_activity(&conn, creator.id, &update, true)?; send_activity( &update, @@ -185,14 +171,7 @@ impl ApubObjectType for Comment { .set_actor_xsd_any_uri(creator.actor_id.to_owned())? .set_object_base_box(note)?; - // Insert the sent activity into the activity table - let activity_form = activity::ActivityForm { - user_id: creator.id, - data: serde_json::to_value(&delete)?, - local: true, - updated: None, - }; - activity::Activity::create(&conn, &activity_form)?; + insert_activity(&conn, creator.id, &delete, true)?; send_activity( &delete, @@ -239,14 +218,7 @@ impl ApubObjectType for Comment { .set_actor_xsd_any_uri(creator.actor_id.to_owned())? .set_object_base_box(delete)?; - // Insert the sent activity into the activity table - let activity_form = activity::ActivityForm { - user_id: creator.id, - data: serde_json::to_value(&undo)?, - local: true, - updated: None, - }; - activity::Activity::create(&conn, &activity_form)?; + insert_activity(&conn, creator.id, &undo, true)?; send_activity( &undo, @@ -275,14 +247,7 @@ impl ApubObjectType for Comment { .set_actor_xsd_any_uri(mod_.actor_id.to_owned())? .set_object_base_box(note)?; - // Insert the sent activity into the activity table - let activity_form = activity::ActivityForm { - user_id: mod_.id, - data: serde_json::to_value(&remove)?, - local: true, - updated: None, - }; - activity::Activity::create(&conn, &activity_form)?; + insert_activity(&conn, mod_.id, &remove, true)?; send_activity( &remove, @@ -328,14 +293,7 @@ impl ApubObjectType for Comment { .set_actor_xsd_any_uri(mod_.actor_id.to_owned())? .set_object_base_box(remove)?; - // Insert the sent activity into the activity table - let activity_form = activity::ActivityForm { - user_id: mod_.id, - data: serde_json::to_value(&undo)?, - local: true, - updated: None, - }; - activity::Activity::create(&conn, &activity_form)?; + insert_activity(&conn, mod_.id, &undo, true)?; send_activity( &undo, @@ -361,14 +319,7 @@ impl ApubLikeableType for Comment { .set_actor_xsd_any_uri(creator.actor_id.to_owned())? .set_object_base_box(note)?; - // Insert the sent activity into the activity table - let activity_form = activity::ActivityForm { - user_id: creator.id, - data: serde_json::to_value(&like)?, - local: true, - updated: None, - }; - activity::Activity::create(&conn, &activity_form)?; + insert_activity(&conn, creator.id, &like, true)?; send_activity( &like, @@ -396,14 +347,7 @@ impl ApubLikeableType for Comment { .set_actor_xsd_any_uri(creator.actor_id.to_owned())? .set_object_base_box(note)?; - // Insert the sent activity into the activity table - let activity_form = activity::ActivityForm { - user_id: creator.id, - data: serde_json::to_value(&dislike)?, - local: true, - updated: None, - }; - activity::Activity::create(&conn, &activity_form)?; + insert_activity(&conn, creator.id, &dislike, true)?; send_activity( &dislike, @@ -443,14 +387,7 @@ impl ApubLikeableType for Comment { .set_actor_xsd_any_uri(creator.actor_id.to_owned())? .set_object_base_box(like)?; - // Insert the sent activity into the activity table - let activity_form = activity::ActivityForm { - user_id: creator.id, - data: serde_json::to_value(&undo)?, - local: true, - updated: None, - }; - activity::Activity::create(&conn, &activity_form)?; + insert_activity(&conn, creator.id, &undo, true)?; send_activity( &undo, diff --git a/server/src/apub/community.rs b/server/src/apub/community.rs index 5e158d5ca..03be220de 100644 --- a/server/src/apub/community.rs +++ b/server/src/apub/community.rs @@ -100,14 +100,7 @@ impl ActorType for Community { .set_object_base_box(BaseBox::from_concrete(follow.clone())?)?; let to = format!("{}/inbox", actor_uri); - // Insert the sent activity into the activity table - let activity_form = activity::ActivityForm { - user_id: self.creator_id, - data: serde_json::to_value(&accept)?, - local: true, - updated: None, - }; - activity::Activity::create(&conn, &activity_form)?; + insert_activity(&conn, self.creator_id, &accept, true)?; send_activity( &accept, @@ -130,14 +123,7 @@ impl ActorType for Community { .set_actor_xsd_any_uri(creator.actor_id.to_owned())? .set_object_base_box(group)?; - // Insert the sent activity into the activity table - let activity_form = activity::ActivityForm { - user_id: self.creator_id, - data: serde_json::to_value(&delete)?, - local: true, - updated: None, - }; - activity::Activity::create(&conn, &activity_form)?; + insert_activity(&conn, self.creator_id, &delete, true)?; // Note: For an accept, since it was automatic, no one pushed a button, // the community was the actor. @@ -175,14 +161,7 @@ impl ActorType for Community { .set_actor_xsd_any_uri(creator.actor_id.to_owned())? .set_object_base_box(delete)?; - // Insert the sent activity into the activity table - let activity_form = activity::ActivityForm { - user_id: self.creator_id, - data: serde_json::to_value(&undo)?, - local: true, - updated: None, - }; - activity::Activity::create(&conn, &activity_form)?; + insert_activity(&conn, self.creator_id, &undo, true)?; // Note: For an accept, since it was automatic, no one pushed a button, // the community was the actor. @@ -208,14 +187,7 @@ impl ActorType for Community { .set_actor_xsd_any_uri(mod_.actor_id.to_owned())? .set_object_base_box(group)?; - // Insert the sent activity into the activity table - let activity_form = activity::ActivityForm { - user_id: mod_.id, - data: serde_json::to_value(&remove)?, - local: true, - updated: None, - }; - activity::Activity::create(&conn, &activity_form)?; + insert_activity(&conn, mod_.id, &remove, true)?; // Note: For an accept, since it was automatic, no one pushed a button, // the community was the actor. @@ -252,14 +224,7 @@ impl ActorType for Community { .set_actor_xsd_any_uri(mod_.actor_id.to_owned())? .set_object_base_box(remove)?; - // Insert the sent activity into the activity table - let activity_form = activity::ActivityForm { - user_id: mod_.id, - data: serde_json::to_value(&undo)?, - local: true, - updated: None, - }; - activity::Activity::create(&conn, &activity_form)?; + insert_activity(&conn, mod_.id, &undo, true)?; // Note: For an accept, since it was automatic, no one pushed a button, // the community was the actor. diff --git a/server/src/apub/community_inbox.rs b/server/src/apub/community_inbox.rs index 450172501..eb52bbce9 100644 --- a/server/src/apub/community_inbox.rs +++ b/server/src/apub/community_inbox.rs @@ -59,14 +59,7 @@ fn handle_follow( verify(&request, &user.public_key.unwrap())?; - // Insert the received activity into the activity table - let activity_form = activity::ActivityForm { - user_id: user.id, - data: serde_json::to_value(&follow)?, - local: false, - updated: None, - }; - activity::Activity::create(&conn, &activity_form)?; + insert_activity(&conn, user.id, &follow, false)?; let community_follower_form = CommunityFollowerForm { community_id: community.id, @@ -115,14 +108,7 @@ fn handle_undo_follow( verify(&request, &user.public_key.unwrap())?; - // Insert the received activity into the activity table - let activity_form = activity::ActivityForm { - user_id: user.id, - data: serde_json::to_value(&follow)?, - local: false, - updated: None, - }; - activity::Activity::create(&conn, &activity_form)?; + insert_activity(&conn, user.id, &follow, false)?; let community_follower_form = CommunityFollowerForm { community_id: community.id, diff --git a/server/src/apub/mod.rs b/server/src/apub/mod.rs index a87c5c390..53e336555 100644 --- a/server/src/apub/mod.rs +++ b/server/src/apub/mod.rs @@ -11,6 +11,7 @@ pub mod user; pub mod user_inbox; use crate::api::community::CommunityResponse; +use crate::db::activity::insert_activity; use crate::websocket::server::SendCommunityRoomMessage; use activitystreams::object::kind::{NoteType, PageType}; use activitystreams::{ @@ -54,7 +55,7 @@ use crate::db::private_message::{PrivateMessage, PrivateMessageForm}; use crate::db::private_message_view::PrivateMessageView; use crate::db::user::{UserForm, User_}; use crate::db::user_view::UserView; -use crate::db::{activity, Crud, Followable, Joinable, Likeable, SearchType}; +use crate::db::{Crud, Followable, Joinable, Likeable, SearchType}; use crate::routes::nodeinfo::{NodeInfo, NodeInfoWellKnown}; use crate::routes::{ChatServerParam, DbPoolParam}; use crate::websocket::{ diff --git a/server/src/apub/post.rs b/server/src/apub/post.rs index 8a13d3907..7ec9cd5a1 100644 --- a/server/src/apub/post.rs +++ b/server/src/apub/post.rs @@ -132,14 +132,7 @@ impl ApubObjectType for Post { .set_actor_xsd_any_uri(creator.actor_id.to_owned())? .set_object_base_box(page)?; - // Insert the sent activity into the activity table - let activity_form = activity::ActivityForm { - user_id: creator.id, - data: serde_json::to_value(&create)?, - local: true, - updated: None, - }; - activity::Activity::create(&conn, &activity_form)?; + insert_activity(&conn, creator.id, &create, true)?; send_activity( &create, @@ -167,14 +160,7 @@ impl ApubObjectType for Post { .set_actor_xsd_any_uri(creator.actor_id.to_owned())? .set_object_base_box(page)?; - // Insert the sent activity into the activity table - let activity_form = activity::ActivityForm { - user_id: creator.id, - data: serde_json::to_value(&update)?, - local: true, - updated: None, - }; - activity::Activity::create(&conn, &activity_form)?; + insert_activity(&conn, creator.id, &update, true)?; send_activity( &update, @@ -202,14 +188,7 @@ impl ApubObjectType for Post { .set_actor_xsd_any_uri(creator.actor_id.to_owned())? .set_object_base_box(page)?; - // Insert the sent activity into the activity table - let activity_form = activity::ActivityForm { - user_id: self.creator_id, - data: serde_json::to_value(&delete)?, - local: true, - updated: None, - }; - activity::Activity::create(&conn, &activity_form)?; + insert_activity(&conn, self.creator_id, &delete, true)?; let community = Community::read(conn, self.community_id)?; send_activity( @@ -254,14 +233,7 @@ impl ApubObjectType for Post { .set_actor_xsd_any_uri(creator.actor_id.to_owned())? .set_object_base_box(delete)?; - // Insert the sent activity into the activity table - let activity_form = activity::ActivityForm { - user_id: self.creator_id, - data: serde_json::to_value(&undo)?, - local: true, - updated: None, - }; - activity::Activity::create(&conn, &activity_form)?; + insert_activity(&conn, self.creator_id, &undo, true)?; let community = Community::read(conn, self.community_id)?; send_activity( @@ -290,14 +262,7 @@ impl ApubObjectType for Post { .set_actor_xsd_any_uri(mod_.actor_id.to_owned())? .set_object_base_box(page)?; - // Insert the sent activity into the activity table - let activity_form = activity::ActivityForm { - user_id: mod_.id, - data: serde_json::to_value(&remove)?, - local: true, - updated: None, - }; - activity::Activity::create(&conn, &activity_form)?; + insert_activity(&conn, mod_.id, &remove, true)?; let community = Community::read(conn, self.community_id)?; send_activity( @@ -340,14 +305,7 @@ impl ApubObjectType for Post { .set_actor_xsd_any_uri(mod_.actor_id.to_owned())? .set_object_base_box(remove)?; - // Insert the sent activity into the activity table - let activity_form = activity::ActivityForm { - user_id: mod_.id, - data: serde_json::to_value(&undo)?, - local: true, - updated: None, - }; - activity::Activity::create(&conn, &activity_form)?; + insert_activity(&conn, mod_.id, &undo, true)?; let community = Community::read(conn, self.community_id)?; send_activity( @@ -373,14 +331,7 @@ impl ApubLikeableType for Post { .set_actor_xsd_any_uri(creator.actor_id.to_owned())? .set_object_base_box(page)?; - // Insert the sent activity into the activity table - let activity_form = activity::ActivityForm { - user_id: creator.id, - data: serde_json::to_value(&like)?, - local: true, - updated: None, - }; - activity::Activity::create(&conn, &activity_form)?; + insert_activity(&conn, creator.id, &like, true)?; send_activity( &like, @@ -407,14 +358,7 @@ impl ApubLikeableType for Post { .set_actor_xsd_any_uri(creator.actor_id.to_owned())? .set_object_base_box(page)?; - // Insert the sent activity into the activity table - let activity_form = activity::ActivityForm { - user_id: creator.id, - data: serde_json::to_value(&dislike)?, - local: true, - updated: None, - }; - activity::Activity::create(&conn, &activity_form)?; + insert_activity(&conn, creator.id, &dislike, true)?; send_activity( &dislike, @@ -453,14 +397,7 @@ impl ApubLikeableType for Post { .set_actor_xsd_any_uri(creator.actor_id.to_owned())? .set_object_base_box(like)?; - // Insert the sent activity into the activity table - let activity_form = activity::ActivityForm { - user_id: creator.id, - data: serde_json::to_value(&undo)?, - local: true, - updated: None, - }; - activity::Activity::create(&conn, &activity_form)?; + insert_activity(&conn, creator.id, &undo, true)?; send_activity( &undo, diff --git a/server/src/apub/private_message.rs b/server/src/apub/private_message.rs index 2fb8f6ace..4e111b8fa 100644 --- a/server/src/apub/private_message.rs +++ b/server/src/apub/private_message.rs @@ -85,14 +85,7 @@ impl ApubObjectType for PrivateMessage { .set_actor_xsd_any_uri(creator.actor_id.to_owned())? .set_object_base_box(note)?; - // Insert the sent activity into the activity table - let activity_form = activity::ActivityForm { - user_id: creator.id, - data: serde_json::to_value(&create)?, - local: true, - updated: None, - }; - activity::Activity::create(&conn, &activity_form)?; + insert_activity(&conn, creator.id, &create, true)?; send_activity( &create, @@ -121,14 +114,7 @@ impl ApubObjectType for PrivateMessage { .set_actor_xsd_any_uri(creator.actor_id.to_owned())? .set_object_base_box(note)?; - // Insert the sent activity into the activity table - let activity_form = activity::ActivityForm { - user_id: creator.id, - data: serde_json::to_value(&update)?, - local: true, - updated: None, - }; - activity::Activity::create(&conn, &activity_form)?; + insert_activity(&conn, creator.id, &update, true)?; send_activity( &update, @@ -156,14 +142,7 @@ impl ApubObjectType for PrivateMessage { .set_actor_xsd_any_uri(creator.actor_id.to_owned())? .set_object_base_box(note)?; - // Insert the sent activity into the activity table - let activity_form = activity::ActivityForm { - user_id: creator.id, - data: serde_json::to_value(&delete)?, - local: true, - updated: None, - }; - activity::Activity::create(&conn, &activity_form)?; + insert_activity(&conn, creator.id, &delete, true)?; send_activity( &delete, @@ -206,14 +185,7 @@ impl ApubObjectType for PrivateMessage { .set_actor_xsd_any_uri(creator.actor_id.to_owned())? .set_object_base_box(delete)?; - // Insert the sent activity into the activity table - let activity_form = activity::ActivityForm { - user_id: creator.id, - data: serde_json::to_value(&undo)?, - local: true, - updated: None, - }; - activity::Activity::create(&conn, &activity_form)?; + insert_activity(&conn, creator.id, &undo, true)?; send_activity( &undo, diff --git a/server/src/apub/shared_inbox.rs b/server/src/apub/shared_inbox.rs index a409d70b6..d7021a6fb 100644 --- a/server/src/apub/shared_inbox.rs +++ b/server/src/apub/shared_inbox.rs @@ -121,14 +121,7 @@ fn receive_create_post( let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?; verify(request, &user.public_key.unwrap())?; - // Insert the received activity into the activity table - let activity_form = activity::ActivityForm { - user_id: user.id, - data: serde_json::to_value(&create)?, - local: false, - updated: None, - }; - activity::Activity::create(&conn, &activity_form)?; + insert_activity(&conn, user.id, &create, false)?; let post = PostForm::from_apub(&page, &conn)?; let inserted_post = Post::create(conn, &post)?; @@ -170,14 +163,7 @@ fn receive_create_comment( let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?; verify(request, &user.public_key.unwrap())?; - // Insert the received activity into the activity table - let activity_form = activity::ActivityForm { - user_id: user.id, - data: serde_json::to_value(&create)?, - local: false, - updated: None, - }; - activity::Activity::create(&conn, &activity_form)?; + insert_activity(&conn, user.id, &create, false)?; let comment = CommentForm::from_apub(¬e, &conn)?; let inserted_comment = Comment::create(conn, &comment)?; @@ -224,14 +210,7 @@ fn receive_update_post( let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?; verify(request, &user.public_key.unwrap())?; - // Insert the received activity into the activity table - let activity_form = activity::ActivityForm { - user_id: user.id, - data: serde_json::to_value(&update)?, - local: false, - updated: None, - }; - activity::Activity::create(&conn, &activity_form)?; + insert_activity(&conn, user.id, &update, false)?; let post = PostForm::from_apub(&page, conn)?; let post_id = Post::read_from_apub_id(conn, &post.ap_id)?.id; @@ -270,14 +249,7 @@ fn receive_like_post( let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?; verify(request, &user.public_key.unwrap())?; - // Insert the received activity into the activity table - let activity_form = activity::ActivityForm { - user_id: user.id, - data: serde_json::to_value(&like)?, - local: false, - updated: None, - }; - activity::Activity::create(&conn, &activity_form)?; + insert_activity(&conn, user.id, &like, false)?; let post = PostForm::from_apub(&page, conn)?; let post_id = Post::read_from_apub_id(conn, &post.ap_id)?.id; @@ -327,14 +299,7 @@ fn receive_dislike_post( let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?; verify(request, &user.public_key.unwrap())?; - // Insert the received activity into the activity table - let activity_form = activity::ActivityForm { - user_id: user.id, - data: serde_json::to_value(&dislike)?, - local: false, - updated: None, - }; - activity::Activity::create(&conn, &activity_form)?; + insert_activity(&conn, user.id, &dislike, false)?; let post = PostForm::from_apub(&page, conn)?; let post_id = Post::read_from_apub_id(conn, &post.ap_id)?.id; @@ -384,14 +349,7 @@ fn receive_update_comment( let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?; verify(request, &user.public_key.unwrap())?; - // Insert the received activity into the activity table - let activity_form = activity::ActivityForm { - user_id: user.id, - data: serde_json::to_value(&update)?, - local: false, - updated: None, - }; - activity::Activity::create(&conn, &activity_form)?; + insert_activity(&conn, user.id, &update, false)?; let comment = CommentForm::from_apub(¬e, &conn)?; let comment_id = Comment::read_from_apub_id(conn, &comment.ap_id)?.id; @@ -435,14 +393,7 @@ fn receive_like_comment( let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?; verify(request, &user.public_key.unwrap())?; - // Insert the received activity into the activity table - let activity_form = activity::ActivityForm { - user_id: user.id, - data: serde_json::to_value(&like)?, - local: false, - updated: None, - }; - activity::Activity::create(&conn, &activity_form)?; + insert_activity(&conn, user.id, &like, false)?; let comment = CommentForm::from_apub(¬e, &conn)?; let comment_id = Comment::read_from_apub_id(conn, &comment.ap_id)?.id; @@ -497,14 +448,7 @@ fn receive_dislike_comment( let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?; verify(request, &user.public_key.unwrap())?; - // Insert the received activity into the activity table - let activity_form = activity::ActivityForm { - user_id: user.id, - data: serde_json::to_value(&dislike)?, - local: false, - updated: None, - }; - activity::Activity::create(&conn, &activity_form)?; + insert_activity(&conn, user.id, &dislike, false)?; let comment = CommentForm::from_apub(¬e, &conn)?; let comment_id = Comment::read_from_apub_id(conn, &comment.ap_id)?.id; @@ -559,14 +503,7 @@ fn receive_delete_community( let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?; verify(request, &user.public_key.unwrap())?; - // Insert the received activity into the activity table - let activity_form = activity::ActivityForm { - user_id: user.id, - data: serde_json::to_value(&delete)?, - local: false, - updated: None, - }; - activity::Activity::create(&conn, &activity_form)?; + insert_activity(&conn, user.id, &delete, false)?; let community_actor_id = CommunityForm::from_apub(&group, &conn)?.actor_id; let community = Community::read_from_actor_id(conn, &community_actor_id)?; @@ -628,14 +565,7 @@ fn receive_remove_community( let mod_ = get_or_fetch_and_upsert_remote_user(&mod_uri, &conn)?; verify(request, &mod_.public_key.unwrap())?; - // Insert the received activity into the activity table - let activity_form = activity::ActivityForm { - user_id: mod_.id, - data: serde_json::to_value(&remove)?, - local: false, - updated: None, - }; - activity::Activity::create(&conn, &activity_form)?; + insert_activity(&conn, mod_.id, &remove, false)?; let community_actor_id = CommunityForm::from_apub(&group, &conn)?.actor_id; let community = Community::read_from_actor_id(conn, &community_actor_id)?; @@ -697,14 +627,7 @@ fn receive_delete_post( let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?; verify(request, &user.public_key.unwrap())?; - // Insert the received activity into the activity table - let activity_form = activity::ActivityForm { - user_id: user.id, - data: serde_json::to_value(&delete)?, - local: false, - updated: None, - }; - activity::Activity::create(&conn, &activity_form)?; + insert_activity(&conn, user.id, &delete, false)?; let post_ap_id = PostForm::from_apub(&page, conn)?.ap_id; let post = Post::read_from_apub_id(conn, &post_ap_id)?; @@ -768,14 +691,7 @@ fn receive_remove_post( let mod_ = get_or_fetch_and_upsert_remote_user(&mod_uri, &conn)?; verify(request, &mod_.public_key.unwrap())?; - // Insert the received activity into the activity table - let activity_form = activity::ActivityForm { - user_id: mod_.id, - data: serde_json::to_value(&remove)?, - local: false, - updated: None, - }; - activity::Activity::create(&conn, &activity_form)?; + insert_activity(&conn, mod_.id, &remove, false)?; let post_ap_id = PostForm::from_apub(&page, conn)?.ap_id; let post = Post::read_from_apub_id(conn, &post_ap_id)?; @@ -839,14 +755,7 @@ fn receive_delete_comment( let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?; verify(request, &user.public_key.unwrap())?; - // Insert the received activity into the activity table - let activity_form = activity::ActivityForm { - user_id: user.id, - data: serde_json::to_value(&delete)?, - local: false, - updated: None, - }; - activity::Activity::create(&conn, &activity_form)?; + insert_activity(&conn, user.id, &delete, false)?; let comment_ap_id = CommentForm::from_apub(¬e, &conn)?.ap_id; let comment = Comment::read_from_apub_id(conn, &comment_ap_id)?; @@ -907,14 +816,7 @@ fn receive_remove_comment( let mod_ = get_or_fetch_and_upsert_remote_user(&mod_uri, &conn)?; verify(request, &mod_.public_key.unwrap())?; - // Insert the received activity into the activity table - let activity_form = activity::ActivityForm { - user_id: mod_.id, - data: serde_json::to_value(&remove)?, - local: false, - updated: None, - }; - activity::Activity::create(&conn, &activity_form)?; + insert_activity(&conn, mod_.id, &remove, false)?; let comment_ap_id = CommentForm::from_apub(¬e, &conn)?.ap_id; let comment = Comment::read_from_apub_id(conn, &comment_ap_id)?; @@ -1035,14 +937,7 @@ fn receive_undo_delete_comment( let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?; verify(request, &user.public_key.unwrap())?; - // Insert the received activity into the activity table - let activity_form = activity::ActivityForm { - user_id: user.id, - data: serde_json::to_value(&delete)?, - local: false, - updated: None, - }; - activity::Activity::create(&conn, &activity_form)?; + insert_activity(&conn, user.id, &delete, false)?; let comment_ap_id = CommentForm::from_apub(¬e, &conn)?.ap_id; let comment = Comment::read_from_apub_id(conn, &comment_ap_id)?; @@ -1103,14 +998,7 @@ fn receive_undo_remove_comment( let mod_ = get_or_fetch_and_upsert_remote_user(&mod_uri, &conn)?; verify(request, &mod_.public_key.unwrap())?; - // Insert the received activity into the activity table - let activity_form = activity::ActivityForm { - user_id: mod_.id, - data: serde_json::to_value(&remove)?, - local: false, - updated: None, - }; - activity::Activity::create(&conn, &activity_form)?; + insert_activity(&conn, mod_.id, &remove, false)?; let comment_ap_id = CommentForm::from_apub(¬e, &conn)?.ap_id; let comment = Comment::read_from_apub_id(conn, &comment_ap_id)?; @@ -1171,14 +1059,7 @@ fn receive_undo_delete_post( let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?; verify(request, &user.public_key.unwrap())?; - // Insert the received activity into the activity table - let activity_form = activity::ActivityForm { - user_id: user.id, - data: serde_json::to_value(&delete)?, - local: false, - updated: None, - }; - activity::Activity::create(&conn, &activity_form)?; + insert_activity(&conn, user.id, &delete, false)?; let post_ap_id = PostForm::from_apub(&page, conn)?.ap_id; let post = Post::read_from_apub_id(conn, &post_ap_id)?; @@ -1242,14 +1123,7 @@ fn receive_undo_remove_post( let mod_ = get_or_fetch_and_upsert_remote_user(&mod_uri, &conn)?; verify(request, &mod_.public_key.unwrap())?; - // Insert the received activity into the activity table - let activity_form = activity::ActivityForm { - user_id: mod_.id, - data: serde_json::to_value(&remove)?, - local: false, - updated: None, - }; - activity::Activity::create(&conn, &activity_form)?; + insert_activity(&conn, mod_.id, &remove, false)?; let post_ap_id = PostForm::from_apub(&page, conn)?.ap_id; let post = Post::read_from_apub_id(conn, &post_ap_id)?; @@ -1313,14 +1187,7 @@ fn receive_undo_delete_community( let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?; verify(request, &user.public_key.unwrap())?; - // Insert the received activity into the activity table - let activity_form = activity::ActivityForm { - user_id: user.id, - data: serde_json::to_value(&delete)?, - local: false, - updated: None, - }; - activity::Activity::create(&conn, &activity_form)?; + insert_activity(&conn, user.id, &delete, false)?; let community_actor_id = CommunityForm::from_apub(&group, &conn)?.actor_id; let community = Community::read_from_actor_id(conn, &community_actor_id)?; @@ -1382,14 +1249,7 @@ fn receive_undo_remove_community( let mod_ = get_or_fetch_and_upsert_remote_user(&mod_uri, &conn)?; verify(request, &mod_.public_key.unwrap())?; - // Insert the received activity into the activity table - let activity_form = activity::ActivityForm { - user_id: mod_.id, - data: serde_json::to_value(&remove)?, - local: false, - updated: None, - }; - activity::Activity::create(&conn, &activity_form)?; + insert_activity(&conn, mod_.id, &remove, false)?; let community_actor_id = CommunityForm::from_apub(&group, &conn)?.actor_id; let community = Community::read_from_actor_id(conn, &community_actor_id)?; @@ -1476,14 +1336,7 @@ fn receive_undo_like_comment( let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?; verify(request, &user.public_key.unwrap())?; - // Insert the received activity into the activity table - let activity_form = activity::ActivityForm { - user_id: user.id, - data: serde_json::to_value(&like)?, - local: false, - updated: None, - }; - activity::Activity::create(&conn, &activity_form)?; + insert_activity(&conn, user.id, &like, false)?; let comment = CommentForm::from_apub(¬e, &conn)?; let comment_id = Comment::read_from_apub_id(conn, &comment.ap_id)?.id; @@ -1533,14 +1386,7 @@ fn receive_undo_like_post( let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?; verify(request, &user.public_key.unwrap())?; - // Insert the received activity into the activity table - let activity_form = activity::ActivityForm { - user_id: user.id, - data: serde_json::to_value(&like)?, - local: false, - updated: None, - }; - activity::Activity::create(&conn, &activity_form)?; + insert_activity(&conn, user.id, &like, false)?; let post = PostForm::from_apub(&page, conn)?; let post_id = Post::read_from_apub_id(conn, &post.ap_id)?.id; diff --git a/server/src/apub/user.rs b/server/src/apub/user.rs index 71f6f5c93..6c45fe1e1 100644 --- a/server/src/apub/user.rs +++ b/server/src/apub/user.rs @@ -73,14 +73,7 @@ impl ActorType for User_ { .set_object_xsd_any_uri(follow_actor_id)?; let to = format!("{}/inbox", follow_actor_id); - // Insert the sent activity into the activity table - let activity_form = activity::ActivityForm { - user_id: self.id, - data: serde_json::to_value(&follow)?, - local: true, - updated: None, - }; - activity::Activity::create(&conn, &activity_form)?; + insert_activity(&conn, self.id, &follow, true)?; send_activity( &follow, @@ -121,14 +114,7 @@ impl ActorType for User_ { .set_actor_xsd_any_uri(self.actor_id.to_owned())? .set_object_base_box(follow)?; - // Insert the sent activity into the activity table - let activity_form = activity::ActivityForm { - user_id: self.id, - data: serde_json::to_value(&undo)?, - local: true, - updated: None, - }; - activity::Activity::create(&conn, &activity_form)?; + insert_activity(&conn, self.id, &undo, true)?; send_activity( &undo, diff --git a/server/src/apub/user_inbox.rs b/server/src/apub/user_inbox.rs index f1c449a5a..2705f017d 100644 --- a/server/src/apub/user_inbox.rs +++ b/server/src/apub/user_inbox.rs @@ -59,14 +59,7 @@ fn receive_accept( let user = User_::read_from_name(&conn, username)?; - // Insert the received activity into the activity table - let activity_form = activity::ActivityForm { - user_id: community.creator_id, - data: serde_json::to_value(&accept)?, - local: false, - updated: None, - }; - activity::Activity::create(&conn, &activity_form)?; + insert_activity(&conn, community.creator_id, &accept, false)?; // Now you need to add this to the community follower let community_follower_form = CommunityFollowerForm { @@ -104,14 +97,7 @@ fn receive_create_private_message( let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?; verify(request, &user.public_key.unwrap())?; - // Insert the received activity into the activity table - let activity_form = activity::ActivityForm { - user_id: user.id, - data: serde_json::to_value(&create)?, - local: false, - updated: None, - }; - activity::Activity::create(&conn, &activity_form)?; + insert_activity(&conn, user.id, &create, false)?; let private_message = PrivateMessageForm::from_apub(¬e, &conn)?; let inserted_private_message = PrivateMessage::create(&conn, &private_message)?; @@ -155,14 +141,7 @@ fn receive_update_private_message( let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?; verify(request, &user.public_key.unwrap())?; - // Insert the received activity into the activity table - let activity_form = activity::ActivityForm { - user_id: user.id, - data: serde_json::to_value(&update)?, - local: false, - updated: None, - }; - activity::Activity::create(&conn, &activity_form)?; + insert_activity(&conn, user.id, &update, false)?; let private_message = PrivateMessageForm::from_apub(¬e, &conn)?; let private_message_id = PrivateMessage::read_from_apub_id(&conn, &private_message.ap_id)?.id; @@ -207,14 +186,7 @@ fn receive_delete_private_message( let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?; verify(request, &user.public_key.unwrap())?; - // Insert the received activity into the activity table - let activity_form = activity::ActivityForm { - user_id: user.id, - data: serde_json::to_value(&delete)?, - local: false, - updated: None, - }; - activity::Activity::create(&conn, &activity_form)?; + insert_activity(&conn, user.id, &delete, false)?; let private_message = PrivateMessageForm::from_apub(¬e, &conn)?; let private_message_id = PrivateMessage::read_from_apub_id(&conn, &private_message.ap_id)?.id; @@ -278,14 +250,7 @@ fn receive_undo_delete_private_message( let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?; verify(request, &user.public_key.unwrap())?; - // Insert the received activity into the activity table - let activity_form = activity::ActivityForm { - user_id: user.id, - data: serde_json::to_value(&delete)?, - local: false, - updated: None, - }; - activity::Activity::create(&conn, &activity_form)?; + insert_activity(&conn, user.id, &delete, false)?; let private_message = PrivateMessageForm::from_apub(¬e, &conn)?; let private_message_id = PrivateMessage::read_from_apub_id(&conn, &private_message.ap_id)?.id; diff --git a/server/src/db/activity.rs b/server/src/db/activity.rs index abe172e12..70f9d6b64 100644 --- a/server/src/db/activity.rs +++ b/server/src/db/activity.rs @@ -1,6 +1,5 @@ use super::*; use crate::schema::activity; -use crate::schema::activity::dsl::*; use serde_json::Value; #[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)] @@ -25,14 +24,17 @@ pub struct ActivityForm { impl Crud for Activity { fn read(conn: &PgConnection, activity_id: i32) -> Result { + use crate::schema::activity::dsl::*; activity.find(activity_id).first::(conn) } fn delete(conn: &PgConnection, activity_id: i32) -> Result { + use crate::schema::activity::dsl::*; diesel::delete(activity.find(activity_id)).execute(conn) } fn create(conn: &PgConnection, new_activity: &ActivityForm) -> Result { + use crate::schema::activity::dsl::*; insert_into(activity) .values(new_activity) .get_result::(conn) @@ -43,12 +45,31 @@ impl Crud for Activity { activity_id: i32, new_activity: &ActivityForm, ) -> Result { + use crate::schema::activity::dsl::*; diesel::update(activity.find(activity_id)) .set(new_activity) .get_result::(conn) } } +pub fn insert_activity( + conn: &PgConnection, + user_id: i32, + data: &T, + local: bool, +) -> Result +where + T: Serialize, +{ + let activity_form = ActivityForm { + user_id, + data: serde_json::to_value(&data)?, + local, + updated: None, + }; + Ok(Activity::create(&conn, &activity_form)?) +} + #[cfg(test)] mod tests { use super::super::user::*;