From bb1b4ee33e75917fb16f71fb302f867dd135ccf5 Mon Sep 17 00:00:00 2001 From: Felix Date: Wed, 13 May 2020 19:21:32 +0200 Subject: [PATCH 1/5] Comment search and apub endpoint --- server/src/apub/comment.rs | 19 +++++++++++++++++++ server/src/apub/fetcher.rs | 27 ++++++++++++++++++++++++++- server/src/routes/federation.rs | 4 +++- ui/src/api_tests/api.spec.ts | 32 +++++++++++++++++++++++++++++++- 4 files changed, 79 insertions(+), 3 deletions(-) diff --git a/server/src/apub/comment.rs b/server/src/apub/comment.rs index 17da45a6b..4c2faa21d 100644 --- a/server/src/apub/comment.rs +++ b/server/src/apub/comment.rs @@ -1,5 +1,24 @@ use super::*; +#[derive(Deserialize)] +pub struct CommentQuery { + comment_id: String, +} + +/// Return the post json over HTTP. +pub async fn get_apub_comment( + info: Path, + db: DbPoolParam, +) -> Result, Error> { + let id = info.comment_id.parse::()?; + let comment = Comment::read(&&db.get()?, id)?; + if !comment.deleted { + Ok(create_apub_response(&comment.to_apub(&db.get().unwrap())?)) + } else { + Ok(create_apub_tombstone_response(&comment.to_tombstone()?)) + } +} + impl ToApub for Comment { type Response = Note; diff --git a/server/src/apub/fetcher.rs b/server/src/apub/fetcher.rs index 115ef6ff9..994e75f2b 100644 --- a/server/src/apub/fetcher.rs +++ b/server/src/apub/fetcher.rs @@ -40,6 +40,7 @@ pub enum SearchAcceptedObjects { Person(Box), Group(Box), Page(Box), + Comment(Box), } /// Attempt to parse the query as URL, and fetch an ActivityPub object from it. @@ -47,7 +48,8 @@ pub enum SearchAcceptedObjects { /// Some working examples for use with the docker/federation/ setup: /// http://lemmy_alpha:8540/c/main, or !main@lemmy_alpha:8540 /// http://lemmy_alpha:8540/u/lemmy_alpha, or @lemmy_alpha@lemmy_alpha:8540 -/// http://lemmy_alpha:8540/p/3 +/// http://lemmy_alpha:8540/post/3 +/// http://lemmy_alpha:8540/comment/2 pub fn search_by_apub_id(query: &str, conn: &PgConnection) -> Result { // Parse the shorthand query url let query_url = if query.contains('@') { @@ -99,6 +101,20 @@ pub fn search_by_apub_id(query: &str, conn: &PgConnection) -> Result { + let post_url = c + .object_props + .get_many_in_reply_to_xsd_any_uris() + .unwrap() + .next() + .unwrap() + .to_string(); + // TODO: also fetch parent comments if any + let post = fetch_remote_object(&Url::parse(&post_url)?)?; + upsert_post(&PostForm::from_apub(&post, conn)?, conn)?; + let c = upsert_comment(&CommentForm::from_apub(&c, conn)?, conn)?; + response.comments = vec![CommentView::read(conn, c.id, None)?]; + } } Ok(response) } @@ -198,6 +214,15 @@ fn upsert_post(post_form: &PostForm, conn: &PgConnection) -> Result } } +fn upsert_comment(comment_form: &CommentForm, conn: &PgConnection) -> Result { + let existing = Comment::read_from_apub_id(conn, &comment_form.ap_id); + match existing { + Err(NotFound {}) => Ok(Comment::create(conn, &comment_form)?), + Ok(p) => Ok(Comment::update(conn, p.id, &comment_form)?), + Err(e) => Err(Error::from(e)), + } +} + // TODO It should not be fetching data from a community outbox. // All posts, comments, comment likes, etc should be posts to our community_inbox // The only data we should be periodically fetching (if it hasn't been fetched in the last day diff --git a/server/src/routes/federation.rs b/server/src/routes/federation.rs index c1cb7408a..ed5c25965 100644 --- a/server/src/routes/federation.rs +++ b/server/src/routes/federation.rs @@ -1,4 +1,5 @@ use super::*; +use crate::apub::comment::get_apub_comment; use crate::apub::community::*; use crate::apub::community_inbox::community_inbox; use crate::apub::post::get_apub_post; @@ -28,7 +29,8 @@ pub fn config(cfg: &mut web::ServiceConfig) { // web::get().to(get_apub_community_outbox), // ) .route("/u/{user_name}", web::get().to(get_apub_user_http)) - .route("/post/{post_id}", web::get().to(get_apub_post)), + .route("/post/{post_id}", web::get().to(get_apub_post)) + .route("/comment/{comment_id}", web::get().to(get_apub_comment)), ) // Inboxes dont work with the header guard for some reason. .route("/c/{community_name}/inbox", web::post().to(community_inbox)) diff --git a/ui/src/api_tests/api.spec.ts b/ui/src/api_tests/api.spec.ts index 8734169ac..dcfb5e628 100644 --- a/ui/src/api_tests/api.spec.ts +++ b/ui/src/api_tests/api.spec.ts @@ -68,7 +68,7 @@ describe('main', () => { lemmyBetaAuth = resB.jwt; }); - describe('beta_fetch', () => { + describe('post_search', () => { test('Create test post on alpha and fetch it on beta', async () => { let name = 'A jest test post'; let postForm: PostForm = { @@ -1107,6 +1107,36 @@ describe('main', () => { expect(getPrivateMessagesUnDeletedRes.messages[0].deleted).toBe(false); }); }); + + describe('comment_search', () => { + test('Create comment on alpha and search it', async () => { + let content = 'A jest test federated comment for search'; + let commentForm: CommentForm = { + content, + post_id: 1, + auth: lemmyAlphaAuth, + }; + + let createResponse: CommentResponse = await fetch( + `${lemmyAlphaApiUrl}/comment`, + { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: wrapper(commentForm), + } + ).then(d => d.json()); + + let searchUrl = `${lemmyBetaApiUrl}/search?q=${createResponse.comment.ap_id}&type_=All&sort=TopAll`; + let searchResponse: SearchResponse = await fetch(searchUrl, { + method: 'GET', + }).then(d => d.json()); + + // TODO: check more fields + expect(searchResponse.comments[0].content).toBe(content); + }); + }); }); function wrapper(form: any): string { From a1ad21ec56dea6926329f3e56fcc7d9817557b57 Mon Sep 17 00:00:00 2001 From: Felix Date: Thu, 14 May 2020 13:23:56 +0200 Subject: [PATCH 2/5] remove outdated comments --- server/src/apub/activities.rs | 2 -- server/src/apub/community.rs | 39 +---------------------------------- server/src/apub/fetcher.rs | 23 --------------------- server/src/apub/mod.rs | 9 +------- server/src/apub/user_inbox.rs | 1 - 5 files changed, 2 insertions(+), 72 deletions(-) diff --git a/server/src/apub/activities.rs b/server/src/apub/activities.rs index 517fd2481..6903d1757 100644 --- a/server/src/apub/activities.rs +++ b/server/src/apub/activities.rs @@ -28,8 +28,6 @@ where { let json = serde_json::to_string(&activity)?; debug!("Sending activitypub activity {} to {:?}", json, to); - // TODO it needs to expand, the to field needs to expand and dedup the followers urls - // The inbox is determined by first retrieving the target actor's JSON-LD representation and then looking up the inbox property. If a recipient is a Collection or OrderedCollection, then the server MUST dereference the collection (with the user's credentials) and discover inboxes for each item in the collection. Servers MUST limit the number of layers of indirections through collections which will be performed, which MAY be one. for t in to { let to_url = Url::parse(&t)?; if !is_apub_id_valid(&to_url) { diff --git a/server/src/apub/community.rs b/server/src/apub/community.rs index f7cd213d3..5e158d5ca 100644 --- a/server/src/apub/community.rs +++ b/server/src/apub/community.rs @@ -316,11 +316,6 @@ impl FromApub for CommunityForm { let aprops = &group.base.extension; let public_key: &PublicKey = &group.extension.public_key; - let _followers_uri = Url::parse(&aprops.get_followers().unwrap().to_string())?; - let _outbox_uri = Url::parse(&aprops.get_outbox().to_string())?; - // TODO don't do extra fetching here - // let _outbox = fetch_remote_object::(&outbox_uri)?; - // let _followers = fetch_remote_object::(&followers_uri)?; let mut creator_and_moderator_uris = oprops.get_many_attributed_to_xsd_any_uris().unwrap(); let creator = creator_and_moderator_uris .next() @@ -368,8 +363,7 @@ pub async fn get_apub_community_http( } } -/// Returns an empty followers collection, only populating the siz (for privacy). -// TODO this needs to return the actual followers, and the to: field needs this +/// Returns an empty followers collection, only populating the size (for privacy). pub async fn get_apub_community_followers( info: Path, db: DbPoolParam, @@ -391,34 +385,3 @@ pub async fn get_apub_community_followers( .set_total_items(community_followers.len() as u64)?; Ok(create_apub_response(&collection)) } - -// TODO should not be doing this -// Returns an UnorderedCollection with the latest posts from the community. -//pub async fn get_apub_community_outbox( -// info: Path, -// db: DbPoolParam, -// chat_server: ChatServerParam, -//) -> Result, Error> { -// let community = Community::read_from_name(&&db.get()?, &info.community_name)?; - -// let conn = establish_unpooled_connection(); -// //As we are an object, we validated that the community id was valid -// let community_posts: Vec = Post::list_for_community(&conn, community.id)?; - -// let mut collection = OrderedCollection::default(); -// let oprops: &mut ObjectProperties = collection.as_mut(); -// oprops -// .set_context_xsd_any_uri(context())? -// .set_id(community.actor_id)?; -// collection -// .collection_props -// .set_many_items_base_boxes( -// community_posts -// .iter() -// .map(|c| c.as_page(&conn).unwrap()) -// .collect(), -// )? -// .set_total_items(community_posts.len() as u64)?; - -// Ok(create_apub_response(&collection)) -//} diff --git a/server/src/apub/fetcher.rs b/server/src/apub/fetcher.rs index 115ef6ff9..aa2a75ea5 100644 --- a/server/src/apub/fetcher.rs +++ b/server/src/apub/fetcher.rs @@ -197,26 +197,3 @@ fn upsert_post(post_form: &PostForm, conn: &PgConnection) -> Result Err(e) => Err(Error::from(e)), } } - -// TODO It should not be fetching data from a community outbox. -// All posts, comments, comment likes, etc should be posts to our community_inbox -// The only data we should be periodically fetching (if it hasn't been fetched in the last day -// maybe), is community and user actors -// and user actors -// Fetch all posts in the outbox of the given user, and insert them into the database. -// fn fetch_community_outbox(community: &Community, conn: &PgConnection) -> Result, Error> { -// let outbox_url = Url::parse(&community.get_outbox_url())?; -// let outbox = fetch_remote_object::(&outbox_url)?; -// let items = outbox.collection_props.get_many_items_base_boxes(); - -// Ok( -// items -// .unwrap() -// .map(|obox: &BaseBox| -> Result { -// let page = obox.clone().to_concrete::()?; -// PostForm::from_page(&page, conn) -// }) -// .map(|pf| upsert_post(&pf?, conn)) -// .collect::, Error>>()?, -// ) -// } diff --git a/server/src/apub/mod.rs b/server/src/apub/mod.rs index 77950d414..a87c5c390 100644 --- a/server/src/apub/mod.rs +++ b/server/src/apub/mod.rs @@ -105,18 +105,12 @@ where .json(data) } -/// Generates the ActivityPub ID for a given object type and name. -/// -/// TODO: we will probably need to change apub endpoint urls so that html and activity+json content -/// types are handled at the same endpoint, so that you can copy the url into mastodon search -/// and have it fetch the object. +/// Generates the ActivityPub ID for a given object type and ID. pub fn make_apub_endpoint(endpoint_type: EndpointType, name: &str) -> Url { let point = match endpoint_type { EndpointType::Community => "c", EndpointType::User => "u", EndpointType::Post => "post", - // TODO I have to change this else my update advanced_migrations crashes the - // server if a comment exists. EndpointType::Comment => "comment", EndpointType::PrivateMessage => "private_message", }; @@ -157,7 +151,6 @@ fn is_apub_id_valid(apub_id: &Url) -> bool { } } -// TODO Not sure good names for these pub trait ToApub { type Response; fn to_apub(&self, conn: &PgConnection) -> Result; diff --git a/server/src/apub/user_inbox.rs b/server/src/apub/user_inbox.rs index 9c25d8054..f1c449a5a 100644 --- a/server/src/apub/user_inbox.rs +++ b/server/src/apub/user_inbox.rs @@ -78,7 +78,6 @@ fn receive_accept( CommunityFollower::follow(&conn, &community_follower_form)?; // TODO: make sure that we actually requested a follow - // TODO: at this point, indicate to the user that they are following the community Ok(HttpResponse::Ok().finish()) } From 11acc7225eaa4d0fac9c6168c3d1cb4c80a56e41 Mon Sep 17 00:00:00 2001 From: Felix Date: Thu, 14 May 2020 14:26:44 +0200 Subject: [PATCH 3/5] 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::*; From 13ca47a3b46597936a17bc8e03e9038cfa611d92 Mon Sep 17 00:00:00 2001 From: Felix Date: Thu, 14 May 2020 15:38:07 +0200 Subject: [PATCH 4/5] Use ActorType for sign/verify, instead of passing raw privatekey/actor_id --- server/src/apub/activities.rs | 9 +--- server/src/apub/comment.rs | 63 ++++-------------------- server/src/apub/community.rs | 38 +++----------- server/src/apub/community_inbox.rs | 4 +- server/src/apub/extensions/signatures.rs | 18 +++---- server/src/apub/mod.rs | 1 + server/src/apub/post.rs | 63 ++++-------------------- server/src/apub/private_message.rs | 28 ++--------- server/src/apub/shared_inbox.rs | 44 ++++++++--------- server/src/apub/user.rs | 18 +++---- server/src/apub/user_inbox.rs | 10 ++-- 11 files changed, 77 insertions(+), 219 deletions(-) diff --git a/server/src/apub/activities.rs b/server/src/apub/activities.rs index 6903d1757..23ae96987 100644 --- a/server/src/apub/activities.rs +++ b/server/src/apub/activities.rs @@ -17,12 +17,7 @@ pub fn populate_object_props( } /// Send an activity to a list of recipients, using the correct headers etc. -pub fn send_activity( - activity: &A, - private_key: &str, - sender_id: &str, - to: Vec, -) -> Result<(), Error> +pub fn send_activity(activity: &A, actor: &dyn ActorType, to: Vec) -> Result<(), Error> where A: Serialize + Debug, { @@ -35,7 +30,7 @@ where continue; } let request = Request::post(t).header("Host", to_url.domain().unwrap()); - let signature = sign(&request, private_key, sender_id)?; + let signature = sign(&request, actor)?; let res = request .header("Signature", signature) .header("Content-Type", "application/json") diff --git a/server/src/apub/comment.rs b/server/src/apub/comment.rs index 9160c7efb..d483a997a 100644 --- a/server/src/apub/comment.rs +++ b/server/src/apub/comment.rs @@ -115,12 +115,7 @@ impl ApubObjectType for Comment { insert_activity(&conn, creator.id, &create, true)?; - send_activity( - &create, - &creator.private_key.as_ref().unwrap(), - &creator.actor_id, - community.get_follower_inboxes(&conn)?, - )?; + send_activity(&create, creator, community.get_follower_inboxes(&conn)?)?; Ok(()) } @@ -144,12 +139,7 @@ impl ApubObjectType for Comment { insert_activity(&conn, creator.id, &update, true)?; - send_activity( - &update, - &creator.private_key.as_ref().unwrap(), - &creator.actor_id, - community.get_follower_inboxes(&conn)?, - )?; + send_activity(&update, creator, community.get_follower_inboxes(&conn)?)?; Ok(()) } @@ -173,12 +163,7 @@ impl ApubObjectType for Comment { insert_activity(&conn, creator.id, &delete, true)?; - send_activity( - &delete, - &creator.private_key.as_ref().unwrap(), - &creator.actor_id, - community.get_follower_inboxes(&conn)?, - )?; + send_activity(&delete, creator, community.get_follower_inboxes(&conn)?)?; Ok(()) } @@ -220,12 +205,7 @@ impl ApubObjectType for Comment { insert_activity(&conn, creator.id, &undo, true)?; - send_activity( - &undo, - &creator.private_key.as_ref().unwrap(), - &creator.actor_id, - community.get_follower_inboxes(&conn)?, - )?; + send_activity(&undo, creator, community.get_follower_inboxes(&conn)?)?; Ok(()) } @@ -249,12 +229,7 @@ impl ApubObjectType for Comment { insert_activity(&conn, mod_.id, &remove, true)?; - send_activity( - &remove, - &mod_.private_key.as_ref().unwrap(), - &mod_.actor_id, - community.get_follower_inboxes(&conn)?, - )?; + send_activity(&remove, mod_, community.get_follower_inboxes(&conn)?)?; Ok(()) } @@ -295,12 +270,7 @@ impl ApubObjectType for Comment { insert_activity(&conn, mod_.id, &undo, true)?; - send_activity( - &undo, - &mod_.private_key.as_ref().unwrap(), - &mod_.actor_id, - community.get_follower_inboxes(&conn)?, - )?; + send_activity(&undo, mod_, community.get_follower_inboxes(&conn)?)?; Ok(()) } } @@ -321,12 +291,7 @@ impl ApubLikeableType for Comment { insert_activity(&conn, creator.id, &like, true)?; - send_activity( - &like, - &creator.private_key.as_ref().unwrap(), - &creator.actor_id, - community.get_follower_inboxes(&conn)?, - )?; + send_activity(&like, creator, community.get_follower_inboxes(&conn)?)?; Ok(()) } @@ -349,12 +314,7 @@ impl ApubLikeableType for Comment { insert_activity(&conn, creator.id, &dislike, true)?; - send_activity( - &dislike, - &creator.private_key.as_ref().unwrap(), - &creator.actor_id, - community.get_follower_inboxes(&conn)?, - )?; + send_activity(&dislike, creator, community.get_follower_inboxes(&conn)?)?; Ok(()) } @@ -389,12 +349,7 @@ impl ApubLikeableType for Comment { insert_activity(&conn, creator.id, &undo, true)?; - send_activity( - &undo, - &creator.private_key.as_ref().unwrap(), - &creator.actor_id, - community.get_follower_inboxes(&conn)?, - )?; + send_activity(&undo, creator, community.get_follower_inboxes(&conn)?)?; Ok(()) } } diff --git a/server/src/apub/community.rs b/server/src/apub/community.rs index 03be220de..f4e8848f2 100644 --- a/server/src/apub/community.rs +++ b/server/src/apub/community.rs @@ -79,6 +79,9 @@ impl ActorType for Community { fn public_key(&self) -> String { self.public_key.to_owned().unwrap() } + fn private_key(&self) -> String { + self.private_key.to_owned().unwrap() + } /// As a local community, accept the follow request from a remote user. fn send_accept_follow(&self, follow: &Follow, conn: &PgConnection) -> Result<(), Error> { @@ -102,12 +105,7 @@ impl ActorType for Community { insert_activity(&conn, self.creator_id, &accept, true)?; - send_activity( - &accept, - &self.private_key.to_owned().unwrap(), - &self.actor_id, - vec![to], - )?; + send_activity(&accept, self, vec![to])?; Ok(()) } @@ -128,12 +126,7 @@ impl ActorType for Community { // Note: For an accept, since it was automatic, no one pushed a button, // the community was the actor. // But for delete, the creator is the actor, and does the signing - send_activity( - &delete, - &creator.private_key.as_ref().unwrap(), - &creator.actor_id, - self.get_follower_inboxes(&conn)?, - )?; + send_activity(&delete, creator, self.get_follower_inboxes(&conn)?)?; Ok(()) } @@ -166,12 +159,7 @@ impl ActorType for Community { // Note: For an accept, since it was automatic, no one pushed a button, // the community was the actor. // But for delete, the creator is the actor, and does the signing - send_activity( - &undo, - &creator.private_key.as_ref().unwrap(), - &creator.actor_id, - self.get_follower_inboxes(&conn)?, - )?; + send_activity(&undo, creator, self.get_follower_inboxes(&conn)?)?; Ok(()) } @@ -192,12 +180,7 @@ impl ActorType for Community { // Note: For an accept, since it was automatic, no one pushed a button, // the community was the actor. // But for delete, the creator is the actor, and does the signing - send_activity( - &remove, - &mod_.private_key.as_ref().unwrap(), - &mod_.actor_id, - self.get_follower_inboxes(&conn)?, - )?; + send_activity(&remove, mod_, self.get_follower_inboxes(&conn)?)?; Ok(()) } @@ -229,12 +212,7 @@ impl ActorType for Community { // Note: For an accept, since it was automatic, no one pushed a button, // the community was the actor. // But for remove , the creator is the actor, and does the signing - send_activity( - &undo, - &mod_.private_key.as_ref().unwrap(), - &mod_.actor_id, - self.get_follower_inboxes(&conn)?, - )?; + send_activity(&undo, mod_, self.get_follower_inboxes(&conn)?)?; Ok(()) } diff --git a/server/src/apub/community_inbox.rs b/server/src/apub/community_inbox.rs index eb52bbce9..81cbee813 100644 --- a/server/src/apub/community_inbox.rs +++ b/server/src/apub/community_inbox.rs @@ -57,7 +57,7 @@ fn handle_follow( let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?; let community = Community::read_from_name(&conn, &community_name)?; - verify(&request, &user.public_key.unwrap())?; + verify(&request, &user)?; insert_activity(&conn, user.id, &follow, false)?; @@ -106,7 +106,7 @@ fn handle_undo_follow( let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?; let community = Community::read_from_name(&conn, &community_name)?; - verify(&request, &user.public_key.unwrap())?; + verify(&request, &user)?; insert_activity(&conn, user.id, &follow, false)?; diff --git a/server/src/apub/extensions/signatures.rs b/server/src/apub/extensions/signatures.rs index d89e0dd30..e8630f214 100644 --- a/server/src/apub/extensions/signatures.rs +++ b/server/src/apub/extensions/signatures.rs @@ -1,5 +1,5 @@ +use crate::apub::ActorType; use activitystreams::ext::Extension; -use activitystreams::Actor; use actix_web::HttpRequest; use failure::Error; use http::request::Builder; @@ -33,9 +33,8 @@ pub fn generate_actor_keypair() -> Result { } /// Signs request headers with the given keypair. -/// TODO: would be nice to pass the sending actor in, instead of raw privatekey/id strings -pub fn sign(request: &Builder, private_key: &str, sender_id: &str) -> Result { - let signing_key_id = format!("{}#main-key", sender_id); +pub fn sign(request: &Builder, actor: &dyn ActorType) -> Result { + let signing_key_id = format!("{}#main-key", actor.actor_id()); let headers = request .headers_ref() @@ -58,7 +57,7 @@ pub fn sign(request: &Builder, private_key: &str, sender_id: &str) -> Result @@ -68,7 +67,7 @@ pub fn sign(request: &Builder, private_key: &str, sender_id: &str) -> Result Result<(), Error> { +pub fn verify(request: &HttpRequest, actor: &dyn ActorType) -> Result<(), Error> { let headers = request .headers() .iter() @@ -86,9 +85,10 @@ pub fn verify(request: &HttpRequest, public_key: &str) -> Result<(), Error> { .verify(|signature, signing_string| -> Result { debug!( "Verifying with key {}, message {}", - &public_key, &signing_string + &actor.public_key(), + &signing_string ); - let public_key = PKey::public_key_from_pem(public_key.as_bytes())?; + let public_key = PKey::public_key_from_pem(actor.public_key().as_bytes())?; let mut verifier = Verifier::new(MessageDigest::sha256(), &public_key).unwrap(); verifier.update(&signing_string.as_bytes()).unwrap(); Ok(verifier.verify(&base64::decode(signature)?)?) @@ -130,4 +130,4 @@ impl PublicKey { } } -impl Extension for PublicKeyExtension where T: Actor {} +impl Extension for PublicKeyExtension where T: activitystreams::Actor {} diff --git a/server/src/apub/mod.rs b/server/src/apub/mod.rs index 53e336555..6ff6a0f8f 100644 --- a/server/src/apub/mod.rs +++ b/server/src/apub/mod.rs @@ -226,6 +226,7 @@ pub trait ActorType { fn actor_id(&self) -> String; fn public_key(&self) -> String; + fn private_key(&self) -> String; // These two have default impls, since currently a community can't follow anything, // and a user can't be followed (yet) diff --git a/server/src/apub/post.rs b/server/src/apub/post.rs index 7ec9cd5a1..b2374ffd7 100644 --- a/server/src/apub/post.rs +++ b/server/src/apub/post.rs @@ -134,12 +134,7 @@ impl ApubObjectType for Post { insert_activity(&conn, creator.id, &create, true)?; - send_activity( - &create, - &creator.private_key.as_ref().unwrap(), - &creator.actor_id, - community.get_follower_inboxes(&conn)?, - )?; + send_activity(&create, creator, community.get_follower_inboxes(&conn)?)?; Ok(()) } @@ -162,12 +157,7 @@ impl ApubObjectType for Post { insert_activity(&conn, creator.id, &update, true)?; - send_activity( - &update, - &creator.private_key.as_ref().unwrap(), - &creator.actor_id, - community.get_follower_inboxes(&conn)?, - )?; + send_activity(&update, creator, community.get_follower_inboxes(&conn)?)?; Ok(()) } @@ -191,12 +181,7 @@ impl ApubObjectType for Post { insert_activity(&conn, self.creator_id, &delete, true)?; let community = Community::read(conn, self.community_id)?; - send_activity( - &delete, - &creator.private_key.as_ref().unwrap(), - &creator.actor_id, - community.get_follower_inboxes(&conn)?, - )?; + send_activity(&delete, creator, community.get_follower_inboxes(&conn)?)?; Ok(()) } @@ -236,12 +221,7 @@ impl ApubObjectType for Post { insert_activity(&conn, self.creator_id, &undo, true)?; let community = Community::read(conn, self.community_id)?; - send_activity( - &undo, - &creator.private_key.as_ref().unwrap(), - &creator.actor_id, - community.get_follower_inboxes(&conn)?, - )?; + send_activity(&undo, creator, community.get_follower_inboxes(&conn)?)?; Ok(()) } @@ -265,12 +245,7 @@ impl ApubObjectType for Post { insert_activity(&conn, mod_.id, &remove, true)?; let community = Community::read(conn, self.community_id)?; - send_activity( - &remove, - &mod_.private_key.as_ref().unwrap(), - &mod_.actor_id, - community.get_follower_inboxes(&conn)?, - )?; + send_activity(&remove, mod_, community.get_follower_inboxes(&conn)?)?; Ok(()) } fn send_undo_remove(&self, mod_: &User_, conn: &PgConnection) -> Result<(), Error> { @@ -308,12 +283,7 @@ impl ApubObjectType for Post { insert_activity(&conn, mod_.id, &undo, true)?; let community = Community::read(conn, self.community_id)?; - send_activity( - &undo, - &mod_.private_key.as_ref().unwrap(), - &mod_.actor_id, - community.get_follower_inboxes(&conn)?, - )?; + send_activity(&undo, mod_, community.get_follower_inboxes(&conn)?)?; Ok(()) } } @@ -333,12 +303,7 @@ impl ApubLikeableType for Post { insert_activity(&conn, creator.id, &like, true)?; - send_activity( - &like, - &creator.private_key.as_ref().unwrap(), - &creator.actor_id, - community.get_follower_inboxes(&conn)?, - )?; + send_activity(&like, creator, community.get_follower_inboxes(&conn)?)?; Ok(()) } @@ -360,12 +325,7 @@ impl ApubLikeableType for Post { insert_activity(&conn, creator.id, &dislike, true)?; - send_activity( - &dislike, - &creator.private_key.as_ref().unwrap(), - &creator.actor_id, - community.get_follower_inboxes(&conn)?, - )?; + send_activity(&dislike, creator, community.get_follower_inboxes(&conn)?)?; Ok(()) } @@ -399,12 +359,7 @@ impl ApubLikeableType for Post { insert_activity(&conn, creator.id, &undo, true)?; - send_activity( - &undo, - &creator.private_key.as_ref().unwrap(), - &creator.actor_id, - community.get_follower_inboxes(&conn)?, - )?; + send_activity(&undo, creator, community.get_follower_inboxes(&conn)?)?; Ok(()) } } diff --git a/server/src/apub/private_message.rs b/server/src/apub/private_message.rs index 4e111b8fa..3fff75dc2 100644 --- a/server/src/apub/private_message.rs +++ b/server/src/apub/private_message.rs @@ -87,12 +87,7 @@ impl ApubObjectType for PrivateMessage { insert_activity(&conn, creator.id, &create, true)?; - send_activity( - &create, - &creator.private_key.as_ref().unwrap(), - &creator.actor_id, - vec![to], - )?; + send_activity(&create, creator, vec![to])?; Ok(()) } @@ -116,12 +111,7 @@ impl ApubObjectType for PrivateMessage { insert_activity(&conn, creator.id, &update, true)?; - send_activity( - &update, - &creator.private_key.as_ref().unwrap(), - &creator.actor_id, - vec![to], - )?; + send_activity(&update, creator, vec![to])?; Ok(()) } @@ -144,12 +134,7 @@ impl ApubObjectType for PrivateMessage { insert_activity(&conn, creator.id, &delete, true)?; - send_activity( - &delete, - &creator.private_key.as_ref().unwrap(), - &creator.actor_id, - vec![to], - )?; + send_activity(&delete, creator, vec![to])?; Ok(()) } @@ -187,12 +172,7 @@ impl ApubObjectType for PrivateMessage { insert_activity(&conn, creator.id, &undo, true)?; - send_activity( - &undo, - &creator.private_key.as_ref().unwrap(), - &creator.actor_id, - vec![to], - )?; + send_activity(&undo, creator, vec![to])?; Ok(()) } diff --git a/server/src/apub/shared_inbox.rs b/server/src/apub/shared_inbox.rs index d7021a6fb..6bbe97251 100644 --- a/server/src/apub/shared_inbox.rs +++ b/server/src/apub/shared_inbox.rs @@ -119,7 +119,7 @@ fn receive_create_post( .to_string(); let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?; - verify(request, &user.public_key.unwrap())?; + verify(request, &user)?; insert_activity(&conn, user.id, &create, false)?; @@ -161,7 +161,7 @@ fn receive_create_comment( .to_string(); let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?; - verify(request, &user.public_key.unwrap())?; + verify(request, &user)?; insert_activity(&conn, user.id, &create, false)?; @@ -208,7 +208,7 @@ fn receive_update_post( .to_string(); let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?; - verify(request, &user.public_key.unwrap())?; + verify(request, &user)?; insert_activity(&conn, user.id, &update, false)?; @@ -247,7 +247,7 @@ fn receive_like_post( let user_uri = like.like_props.get_actor_xsd_any_uri().unwrap().to_string(); let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?; - verify(request, &user.public_key.unwrap())?; + verify(request, &user)?; insert_activity(&conn, user.id, &like, false)?; @@ -297,7 +297,7 @@ fn receive_dislike_post( .to_string(); let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?; - verify(request, &user.public_key.unwrap())?; + verify(request, &user)?; insert_activity(&conn, user.id, &dislike, false)?; @@ -347,7 +347,7 @@ fn receive_update_comment( .to_string(); let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?; - verify(request, &user.public_key.unwrap())?; + verify(request, &user)?; insert_activity(&conn, user.id, &update, false)?; @@ -391,7 +391,7 @@ fn receive_like_comment( let user_uri = like.like_props.get_actor_xsd_any_uri().unwrap().to_string(); let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?; - verify(request, &user.public_key.unwrap())?; + verify(request, &user)?; insert_activity(&conn, user.id, &like, false)?; @@ -446,7 +446,7 @@ fn receive_dislike_comment( .to_string(); let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?; - verify(request, &user.public_key.unwrap())?; + verify(request, &user)?; insert_activity(&conn, user.id, &dislike, false)?; @@ -501,7 +501,7 @@ fn receive_delete_community( .into_concrete::()?; let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?; - verify(request, &user.public_key.unwrap())?; + verify(request, &user)?; insert_activity(&conn, user.id, &delete, false)?; @@ -563,7 +563,7 @@ fn receive_remove_community( .into_concrete::()?; let mod_ = get_or_fetch_and_upsert_remote_user(&mod_uri, &conn)?; - verify(request, &mod_.public_key.unwrap())?; + verify(request, &mod_)?; insert_activity(&conn, mod_.id, &remove, false)?; @@ -625,7 +625,7 @@ fn receive_delete_post( .into_concrete::()?; let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?; - verify(request, &user.public_key.unwrap())?; + verify(request, &user)?; insert_activity(&conn, user.id, &delete, false)?; @@ -689,7 +689,7 @@ fn receive_remove_post( .into_concrete::()?; let mod_ = get_or_fetch_and_upsert_remote_user(&mod_uri, &conn)?; - verify(request, &mod_.public_key.unwrap())?; + verify(request, &mod_)?; insert_activity(&conn, mod_.id, &remove, false)?; @@ -753,7 +753,7 @@ fn receive_delete_comment( .into_concrete::()?; let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?; - verify(request, &user.public_key.unwrap())?; + verify(request, &user)?; insert_activity(&conn, user.id, &delete, false)?; @@ -814,7 +814,7 @@ fn receive_remove_comment( .into_concrete::()?; let mod_ = get_or_fetch_and_upsert_remote_user(&mod_uri, &conn)?; - verify(request, &mod_.public_key.unwrap())?; + verify(request, &mod_)?; insert_activity(&conn, mod_.id, &remove, false)?; @@ -935,7 +935,7 @@ fn receive_undo_delete_comment( .into_concrete::()?; let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?; - verify(request, &user.public_key.unwrap())?; + verify(request, &user)?; insert_activity(&conn, user.id, &delete, false)?; @@ -996,7 +996,7 @@ fn receive_undo_remove_comment( .into_concrete::()?; let mod_ = get_or_fetch_and_upsert_remote_user(&mod_uri, &conn)?; - verify(request, &mod_.public_key.unwrap())?; + verify(request, &mod_)?; insert_activity(&conn, mod_.id, &remove, false)?; @@ -1057,7 +1057,7 @@ fn receive_undo_delete_post( .into_concrete::()?; let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?; - verify(request, &user.public_key.unwrap())?; + verify(request, &user)?; insert_activity(&conn, user.id, &delete, false)?; @@ -1121,7 +1121,7 @@ fn receive_undo_remove_post( .into_concrete::()?; let mod_ = get_or_fetch_and_upsert_remote_user(&mod_uri, &conn)?; - verify(request, &mod_.public_key.unwrap())?; + verify(request, &mod_)?; insert_activity(&conn, mod_.id, &remove, false)?; @@ -1185,7 +1185,7 @@ fn receive_undo_delete_community( .into_concrete::()?; let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?; - verify(request, &user.public_key.unwrap())?; + verify(request, &user)?; insert_activity(&conn, user.id, &delete, false)?; @@ -1247,7 +1247,7 @@ fn receive_undo_remove_community( .into_concrete::()?; let mod_ = get_or_fetch_and_upsert_remote_user(&mod_uri, &conn)?; - verify(request, &mod_.public_key.unwrap())?; + verify(request, &mod_)?; insert_activity(&conn, mod_.id, &remove, false)?; @@ -1334,7 +1334,7 @@ fn receive_undo_like_comment( let user_uri = like.like_props.get_actor_xsd_any_uri().unwrap().to_string(); let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?; - verify(request, &user.public_key.unwrap())?; + verify(request, &user)?; insert_activity(&conn, user.id, &like, false)?; @@ -1384,7 +1384,7 @@ fn receive_undo_like_post( let user_uri = like.like_props.get_actor_xsd_any_uri().unwrap().to_string(); let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?; - verify(request, &user.public_key.unwrap())?; + verify(request, &user)?; insert_activity(&conn, user.id, &like, false)?; diff --git a/server/src/apub/user.rs b/server/src/apub/user.rs index 6c45fe1e1..61661de47 100644 --- a/server/src/apub/user.rs +++ b/server/src/apub/user.rs @@ -57,6 +57,10 @@ impl ActorType for User_ { self.public_key.to_owned().unwrap() } + fn private_key(&self) -> String { + self.private_key.to_owned().unwrap() + } + /// As a given local user, send out a follow request to a remote community. fn send_follow(&self, follow_actor_id: &str, conn: &PgConnection) -> Result<(), Error> { let mut follow = Follow::new(); @@ -75,12 +79,7 @@ impl ActorType for User_ { insert_activity(&conn, self.id, &follow, true)?; - send_activity( - &follow, - &self.private_key.as_ref().unwrap(), - &follow_actor_id, - vec![to], - )?; + send_activity(&follow, self, vec![to])?; Ok(()) } @@ -116,12 +115,7 @@ impl ActorType for User_ { insert_activity(&conn, self.id, &undo, true)?; - send_activity( - &undo, - &self.private_key.as_ref().unwrap(), - &follow_actor_id, - vec![to], - )?; + send_activity(&undo, self, vec![to])?; Ok(()) } diff --git a/server/src/apub/user_inbox.rs b/server/src/apub/user_inbox.rs index 2705f017d..3035deba8 100644 --- a/server/src/apub/user_inbox.rs +++ b/server/src/apub/user_inbox.rs @@ -55,7 +55,7 @@ fn receive_accept( .to_string(); let community = get_or_fetch_and_upsert_remote_community(&community_uri, conn)?; - verify(request, &community.public_key.unwrap())?; + verify(request, &community)?; let user = User_::read_from_name(&conn, username)?; @@ -95,7 +95,7 @@ fn receive_create_private_message( .to_string(); let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?; - verify(request, &user.public_key.unwrap())?; + verify(request, &user)?; insert_activity(&conn, user.id, &create, false)?; @@ -139,7 +139,7 @@ fn receive_update_private_message( .to_string(); let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?; - verify(request, &user.public_key.unwrap())?; + verify(request, &user)?; insert_activity(&conn, user.id, &update, false)?; @@ -184,7 +184,7 @@ fn receive_delete_private_message( .to_string(); let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?; - verify(request, &user.public_key.unwrap())?; + verify(request, &user)?; insert_activity(&conn, user.id, &delete, false)?; @@ -248,7 +248,7 @@ fn receive_undo_delete_private_message( .to_string(); let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?; - verify(request, &user.public_key.unwrap())?; + verify(request, &user)?; insert_activity(&conn, user.id, &delete, false)?; From 0fb8450e56da430ece7502ecde085d4c3a2e2ff2 Mon Sep 17 00:00:00 2001 From: Felix Date: Thu, 14 May 2020 17:44:01 +0200 Subject: [PATCH 5/5] Simplify community_inbox --- server/src/apub/community_inbox.rs | 92 ++++++++++++------------------ 1 file changed, 38 insertions(+), 54 deletions(-) diff --git a/server/src/apub/community_inbox.rs b/server/src/apub/community_inbox.rs index 81cbee813..6c35ce0aa 100644 --- a/server/src/apub/community_inbox.rs +++ b/server/src/apub/community_inbox.rs @@ -7,6 +7,22 @@ pub enum CommunityAcceptedObjects { Undo(Undo), } +impl CommunityAcceptedObjects { + fn follow(&self) -> Result { + match self { + CommunityAcceptedObjects::Follow(f) => Ok(f.to_owned()), + CommunityAcceptedObjects::Undo(u) => Ok( + u.undo_props + .get_object_base_box() + .to_owned() + .unwrap() + .to_owned() + .into_concrete::()?, + ), + } + } +} + // TODO Consolidate community and user inboxes into a single shared one /// Handler for all incoming activities to community inboxes. pub async fn community_inbox( @@ -14,7 +30,7 @@ pub async fn community_inbox( input: web::Json, path: web::Path, db: DbPoolParam, - chat_server: ChatServerParam, + _chat_server: ChatServerParam, ) -> Result { let input = input.into_inner(); let community_name = path.into_inner(); @@ -22,31 +38,13 @@ pub async fn community_inbox( "Community {} received activity {:?}", &community_name, &input ); - match input { - CommunityAcceptedObjects::Follow(f) => { - handle_follow(&f, &request, &community_name, db, chat_server) - } - CommunityAcceptedObjects::Undo(u) => { - handle_undo_follow(&u, &request, &community_name, db, chat_server) - } - } -} - -/// Handle a follow request from a remote user, adding it to the local database and returning an -/// Accept activity. -fn handle_follow( - follow: &Follow, - request: &HttpRequest, - community_name: &str, - db: DbPoolParam, - _chat_server: ChatServerParam, -) -> Result { + let follow = input.follow()?; let user_uri = follow .follow_props .get_actor_xsd_any_uri() .unwrap() .to_string(); - let _community_uri = follow + let community_uri = follow .follow_props .get_object_xsd_any_uri() .unwrap() @@ -55,10 +53,24 @@ fn handle_follow( let conn = db.get()?; let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?; - let community = Community::read_from_name(&conn, &community_name)?; + let community = get_or_fetch_and_upsert_remote_community(&community_uri, &conn)?; verify(&request, &user)?; + match input { + CommunityAcceptedObjects::Follow(f) => handle_follow(&f, &user, &community, &conn), + CommunityAcceptedObjects::Undo(u) => handle_undo_follow(&u, &user, &community, &conn), + } +} + +/// Handle a follow request from a remote user, adding it to the local database and returning an +/// Accept activity. +fn handle_follow( + follow: &Follow, + user: &User_, + community: &Community, + conn: &PgConnection, +) -> Result { insert_activity(&conn, user.id, &follow, false)?; let community_follower_form = CommunityFollowerForm { @@ -76,39 +88,11 @@ fn handle_follow( fn handle_undo_follow( undo: &Undo, - request: &HttpRequest, - community_name: &str, - db: DbPoolParam, - _chat_server: ChatServerParam, + user: &User_, + community: &Community, + conn: &PgConnection, ) -> Result { - let follow = undo - .undo_props - .get_object_base_box() - .to_owned() - .unwrap() - .to_owned() - .into_concrete::()?; - - let user_uri = follow - .follow_props - .get_actor_xsd_any_uri() - .unwrap() - .to_string(); - - let _community_uri = follow - .follow_props - .get_object_xsd_any_uri() - .unwrap() - .to_string(); - - let conn = db.get()?; - - let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?; - let community = Community::read_from_name(&conn, &community_name)?; - - verify(&request, &user)?; - - insert_activity(&conn, user.id, &follow, false)?; + insert_activity(&conn, user.id, &undo, false)?; let community_follower_form = CommunityFollowerForm { community_id: community.id,