]> Untitled Git - lemmy.git/commitdiff
Add helper function for Activity::create()
authorFelix <me@nutomic.com>
Thu, 14 May 2020 12:26:44 +0000 (14:26 +0200)
committerFelix <me@nutomic.com>
Thu, 14 May 2020 12:26:44 +0000 (14:26 +0200)
server/src/apub/comment.rs
server/src/apub/community.rs
server/src/apub/community_inbox.rs
server/src/apub/mod.rs
server/src/apub/post.rs
server/src/apub/private_message.rs
server/src/apub/shared_inbox.rs
server/src/apub/user.rs
server/src/apub/user_inbox.rs
server/src/db/activity.rs

index 17da45a6bca857ab409e1fcf35b429d20fb3c7ff..9160c7efb1f743c1ecc35638040c8ff3ed4a0ba7 100644 (file)
@@ -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,
index 5e158d5ca6c32933706d482b305158f69ca83222..03be220de957afedbbd8a875b8336bb39399f2d8 100644 (file)
@@ -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.
index 45017250198163704b4fccd3b50946676b1b5aab..eb52bbce9cd7700c17fff9daf831b4f224bd521f 100644 (file)
@@ -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,
index a87c5c3907c070dd57c010e716fa652b761b082a..53e33655559a8b87eac4cd52e06683a3da911fa9 100644 (file)
@@ -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::{
index 8a13d390769f1a9cd7397d24940cc67aef9785bb..7ec9cd5a10c8f3191b92bcd00d6aa258c0acccba 100644 (file)
@@ -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,
index 2fb8f6ace641a9a0c6cfce204efc1a1940b0800c..4e111b8faa5bb4bd7a5492218483a58820692a21 100644 (file)
@@ -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,
index a409d70b6ee039353009d80a268350b54b90d2f3..d7021a6fb061014a5988607407904d6c40de68a2 100644 (file)
@@ -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(&note, &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(&note, &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(&note, &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(&note, &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(&note, &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(&note, &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(&note, &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(&note, &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(&note, &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;
index 71f6f5c93d19f7d37f51f7ca5d7b4fb0c83e72d0..6c45fe1e151156aff7024f7416a2ea1e3f9951df 100644 (file)
@@ -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,
index f1c449a5ae4b838d0e31d7895bf981c9b979932d..2705f017de7755d980dd25200ad6b99dec37bb31 100644 (file)
@@ -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(&note, &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(&note, &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(&note, &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(&note, &conn)?;
   let private_message_id = PrivateMessage::read_from_apub_id(&conn, &private_message.ap_id)?.id;
index abe172e12d7244c43440ad72845b9af345fcf871..70f9d6b64c8f40c0614395d18ecb7f4abd648dab 100644 (file)
@@ -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<ActivityForm> for Activity {
   fn read(conn: &PgConnection, activity_id: i32) -> Result<Self, Error> {
+    use crate::schema::activity::dsl::*;
     activity.find(activity_id).first::<Self>(conn)
   }
 
   fn delete(conn: &PgConnection, activity_id: i32) -> Result<usize, Error> {
+    use crate::schema::activity::dsl::*;
     diesel::delete(activity.find(activity_id)).execute(conn)
   }
 
   fn create(conn: &PgConnection, new_activity: &ActivityForm) -> Result<Self, Error> {
+    use crate::schema::activity::dsl::*;
     insert_into(activity)
       .values(new_activity)
       .get_result::<Self>(conn)
@@ -43,12 +45,31 @@ impl Crud<ActivityForm> for Activity {
     activity_id: i32,
     new_activity: &ActivityForm,
   ) -> Result<Self, Error> {
+    use crate::schema::activity::dsl::*;
     diesel::update(activity.find(activity_id))
       .set(new_activity)
       .get_result::<Self>(conn)
   }
 }
 
+pub fn insert_activity<T>(
+  conn: &PgConnection,
+  user_id: i32,
+  data: &T,
+  local: bool,
+) -> Result<Activity, failure::Error>
+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::*;