From 4b741c37595a04da99118a3c26de02f253d91cd2 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Mon, 27 Apr 2020 19:21:41 -0400 Subject: [PATCH 1/2] Adding a better switching for activity kinds for the shared inbox. --- server/src/apub/mod.rs | 6 +- server/src/apub/shared_inbox.rs | 151 ++++++++++++++------------------ 2 files changed, 67 insertions(+), 90 deletions(-) diff --git a/server/src/apub/mod.rs b/server/src/apub/mod.rs index a861156f5..36f26e4b0 100644 --- a/server/src/apub/mod.rs +++ b/server/src/apub/mod.rs @@ -16,11 +16,7 @@ use activitystreams::{ context, endpoint::EndpointProperties, ext::{Ext, Extensible, Extension}, - object::{ - kind::{NoteType, PageType}, - properties::ObjectProperties, - Note, Page, - }, + object::{properties::ObjectProperties, Note, Page}, public, BaseBox, }; use actix_web::body::Body; diff --git a/server/src/apub/shared_inbox.rs b/server/src/apub/shared_inbox.rs index f0cfc9900..d720681f9 100644 --- a/server/src/apub/shared_inbox.rs +++ b/server/src/apub/shared_inbox.rs @@ -7,6 +7,15 @@ pub enum SharedAcceptedObjects { Update(Update), } +impl SharedAcceptedObjects { + fn object(&self) -> Option<&BaseBox> { + match self { + SharedAcceptedObjects::Create(c) => c.create_props.get_object_base_box(), + SharedAcceptedObjects::Update(u) => u.update_props.get_object_base_box(), + } + } +} + /// Handler for all incoming activities to user inboxes. pub async fn shared_inbox( request: HttpRequest, @@ -15,59 +24,45 @@ pub async fn shared_inbox( chat_server: ChatServerParam, ) -> Result { // TODO: would be nice if we could do the signature check here, but we cant access the actor property - let input = input.into_inner(); + let activity = input.into_inner(); let conn = &db.get().unwrap(); - let json = serde_json::to_string(&input)?; - debug!("Shared inbox received activity: {:?}", &json); + let json = serde_json::to_string(&activity)?; + debug!("Shared inbox received activity: {}", json); - match input { - SharedAcceptedObjects::Create(c) => handle_create(&c, &request, &conn, chat_server), - SharedAcceptedObjects::Update(u) => handle_update(&u, &request, &conn, chat_server), + let object = activity.object().cloned().unwrap(); + + match (activity, object.kind()) { + (SharedAcceptedObjects::Create(c), Some("Note")) => { + receive_create_comment(&c, &request, &conn, chat_server) + } + (SharedAcceptedObjects::Create(c), Some("Page")) => { + receive_create_post(&c, &request, &conn, chat_server) + } + (SharedAcceptedObjects::Update(u), Some("Note")) => { + receive_update_comment(&u, &request, &conn, chat_server) + } + (SharedAcceptedObjects::Update(u), Some("Page")) => { + receive_update_post(&u, &request, &conn, chat_server) + } + _ => Err(format_err!("Unknown incoming activity type.")), } } -/// Handle create activities and insert them in the database. -fn handle_create( +fn receive_create_post( create: &Create, request: &HttpRequest, conn: &PgConnection, chat_server: ChatServerParam, ) -> Result { - let base_box = create.create_props.get_object_base_box().unwrap(); + let page = create + .create_props + .get_object_base_box() + .to_owned() + .unwrap() + .to_owned() + .to_concrete::()?; - if base_box.is_kind(PageType) { - let page = create - .create_props - .get_object_base_box() - .to_owned() - .unwrap() - .to_owned() - .to_concrete::()?; - receive_create_post(&create, &page, &request, &conn, chat_server)?; - } else if base_box.is_kind(NoteType) { - let note = create - .create_props - .get_object_base_box() - .to_owned() - .unwrap() - .to_owned() - .to_concrete::()?; - receive_create_comment(&create, ¬e, &request, &conn, chat_server)?; - } else { - return Err(format_err!("Unknown base box type")); - } - - Ok(HttpResponse::Ok().finish()) -} - -fn receive_create_post( - create: &Create, - page: &Page, - request: &HttpRequest, - conn: &PgConnection, - chat_server: ChatServerParam, -) -> Result<(), Error> { let user_uri = create .create_props .get_actor_xsd_any_uri() @@ -91,16 +86,23 @@ fn receive_create_post( my_id: None, }); - Ok(()) + Ok(HttpResponse::Ok().finish()) } fn receive_create_comment( create: &Create, - note: &Note, request: &HttpRequest, conn: &PgConnection, chat_server: ChatServerParam, -) -> Result<(), Error> { +) -> Result { + let note = create + .create_props + .get_object_base_box() + .to_owned() + .unwrap() + .to_owned() + .to_concrete::()?; + let user_uri = create .create_props .get_actor_xsd_any_uri() @@ -129,51 +131,23 @@ fn receive_create_comment( my_id: None, }); - Ok(()) -} - -/// Handle create activities and insert them in the database. -fn handle_update( - update: &Update, - request: &HttpRequest, - conn: &PgConnection, - chat_server: ChatServerParam, -) -> Result { - let base_box = update.update_props.get_object_base_box().unwrap(); - - if base_box.is_kind(PageType) { - let page = update - .update_props - .get_object_base_box() - .to_owned() - .unwrap() - .to_owned() - .to_concrete::()?; - - receive_update_post(&update, &page, &request, &conn, chat_server)?; - } else if base_box.is_kind(NoteType) { - let note = update - .update_props - .get_object_base_box() - .to_owned() - .unwrap() - .to_owned() - .to_concrete::()?; - receive_update_comment(&update, ¬e, &request, &conn, chat_server)?; - } else { - return Err(format_err!("Unknown base box type")); - } - Ok(HttpResponse::Ok().finish()) } fn receive_update_post( update: &Update, - page: &Page, request: &HttpRequest, conn: &PgConnection, chat_server: ChatServerParam, -) -> Result<(), Error> { +) -> Result { + let page = update + .update_props + .get_object_base_box() + .to_owned() + .unwrap() + .to_owned() + .to_concrete::()?; + let user_uri = update .update_props .get_actor_xsd_any_uri() @@ -198,16 +172,23 @@ fn receive_update_post( my_id: None, }); - Ok(()) + Ok(HttpResponse::Ok().finish()) } fn receive_update_comment( update: &Update, - note: &Note, request: &HttpRequest, conn: &PgConnection, chat_server: ChatServerParam, -) -> Result<(), Error> { +) -> Result { + let note = update + .update_props + .get_object_base_box() + .to_owned() + .unwrap() + .to_owned() + .to_concrete::()?; + let user_uri = update .update_props .get_actor_xsd_any_uri() @@ -237,5 +218,5 @@ fn receive_update_comment( my_id: None, }); - Ok(()) + Ok(HttpResponse::Ok().finish()) } From 07a9d84ed08d85847ebe15fa68e20a2763508046 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Tue, 28 Apr 2020 09:57:28 -0400 Subject: [PATCH 2/2] Removing some comments. --- server/src/apub/comment.rs | 3 --- server/src/apub/mod.rs | 4 ++-- ui/src/api_tests/api.spec.ts | 2 -- 3 files changed, 2 insertions(+), 7 deletions(-) diff --git a/server/src/apub/comment.rs b/server/src/apub/comment.rs index 3b7c0dfe0..3b647fa32 100644 --- a/server/src/apub/comment.rs +++ b/server/src/apub/comment.rs @@ -23,9 +23,6 @@ impl ToApub for Comment { // Not needed when the Post is embedded in a collection (like for community outbox) .set_context_xsd_any_uri(context())? .set_id(self.ap_id.to_owned())? - // Use summary field to be consistent with mastodon content warning. - // https://mastodon.xyz/@Louisa/103987265222901387.json - // .set_summary_xsd_string(self.name.to_owned())? .set_published(convert_datetime(self.published))? .set_to_xsd_any_uri(community.actor_id)? .set_many_in_reply_to_xsd_any_uris(in_reply_to_vec)? diff --git a/server/src/apub/mod.rs b/server/src/apub/mod.rs index 36f26e4b0..373fa8da0 100644 --- a/server/src/apub/mod.rs +++ b/server/src/apub/mod.rs @@ -179,12 +179,12 @@ pub trait ActorType { // and a user can't be followed (yet) #[allow(unused_variables)] fn send_follow(&self, follow_actor_id: &str) -> Result<(), Error> { - Ok(()) + Err(format_err!("Follow not implemented.")) } #[allow(unused_variables)] fn send_accept_follow(&self, follow: &Follow) -> Result<(), Error> { - Ok(()) + Err(format_err!("Accept not implemented.")) } // TODO default because there is no user following yet. diff --git a/ui/src/api_tests/api.spec.ts b/ui/src/api_tests/api.spec.ts index 49fd08782..783ce709d 100644 --- a/ui/src/api_tests/api.spec.ts +++ b/ui/src/api_tests/api.spec.ts @@ -102,8 +102,6 @@ describe('main', () => { expect(searchResponse.communities[0].name).toBe('main'); - // TODO - // Unfortunately the search is correctly let followForm: FollowCommunityForm = { community_id: searchResponse.communities[0].id, follow: true,