From 52206998aaff0a32e97c250cadd537c11699398f Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Sat, 30 May 2020 15:38:01 +0200 Subject: [PATCH] more fixes --- server/src/api/post.rs | 15 +---- server/src/apub/shared_inbox.rs | 111 +++++++++++++++++++++++-------- server/src/db/code_migrations.rs | 2 +- server/src/db/post.rs | 2 +- 4 files changed, 85 insertions(+), 45 deletions(-) diff --git a/server/src/api/post.rs b/server/src/api/post.rs index aee607325..9bbde7919 100644 --- a/server/src/api/post.rs +++ b/server/src/api/post.rs @@ -34,9 +34,6 @@ use diesel::{ use failure::Error; use serde::{Deserialize, Serialize}; use std::str::FromStr; -use crate::apub::get_apub_protocol_string; -use crate::db::community::Community; -use url::Url; #[derive(Serialize, Deserialize, Debug)] pub struct CreatePost { @@ -193,17 +190,7 @@ impl Perform for Oper { } }; - // TODO: we should be able to remove Post::update_ap_id with this - let community = Url::parse(&Community::read(&conn, data.community_id)?.actor_id)?; - let apub_id = - format!( - "{}://{}/{}/{}", - get_apub_protocol_string(), - community.domain().ok_or(format_err!("community has invalid domain"))?, - "post", - inserted_post.id - ); - let updated_post = match Post::update_ap_id(&conn, inserted_post.id, &apub_id) { + let updated_post = match Post::update_ap_id(&conn, inserted_post.id) { Ok(post) => post, Err(_e) => return Err(APIError::err("couldnt_create_post").into()), }; diff --git a/server/src/apub/shared_inbox.rs b/server/src/apub/shared_inbox.rs index 6322e7fc6..0510cbba8 100644 --- a/server/src/apub/shared_inbox.rs +++ b/server/src/apub/shared_inbox.rs @@ -183,34 +183,92 @@ pub async fn shared_inbox( (SharedAcceptedObjects::Undo(u), Some("Delete")) => receive_undo_delete(&u, &conn, chat_server), (SharedAcceptedObjects::Undo(u), Some("Remove")) => receive_undo_remove(&u, &conn, chat_server), (SharedAcceptedObjects::Undo(u), Some("Like")) => receive_undo_like(&u, &conn, chat_server), - (SharedAcceptedObjects::Announce(_a), Some("Create")) => { - let create = object.into_concrete::()?; - receive_create_post(&create, &conn, chat_server) - } - (SharedAcceptedObjects::Announce(_a), Some("Update")) => { - let update = object.into_concrete::()?; - receive_update_post(&update, &conn, chat_server) - } - (SharedAcceptedObjects::Announce(_a), Some("Like")) => { - let like = object.into_concrete::()?; - receive_like_post(&like, &conn, chat_server) - } - (SharedAcceptedObjects::Announce(_a), Some("Dislike")) => { - let dislike = object.into_concrete::()?; - receive_dislike_post(&dislike, &conn, chat_server) - } - (SharedAcceptedObjects::Announce(_a), Some("Delete")) => { - let delete = object.into_concrete::()?; - receive_delete_post(&delete, &conn, chat_server) - } - (SharedAcceptedObjects::Announce(_a), Some("Remove")) => { - let remove = object.into_concrete::()?; - receive_remove_post(&remove, &conn, chat_server) - } + (SharedAcceptedObjects::Announce(a), _) => receive_announce(a, &conn, chat_server), _ => Err(format_err!("Unknown incoming activity type.")), } } +fn receive_announce( + announce: Box, + conn: &PgConnection, + chat_server: ChatServerParam, +) -> Result { + let object = announce + .announce_props + .get_object_base_box() + .unwrap() + .to_owned(); + // TODO: too much copy paste + // TODO: we should log all unhandled events + match object.kind() { + Some("Create") => { + let create = object.into_concrete::()?; + let inner_object = create.create_props.get_object_base_box().unwrap(); + match inner_object.kind() { + Some("Page") => receive_create_post(&create, &conn, chat_server), + Some("Note") => receive_create_comment(&create, &conn, chat_server), + _ => Ok(HttpResponse::NotImplemented().finish()), + } + } + Some("Update") => { + let update = object.into_concrete::()?; + let inner_object = update.update_props.get_object_base_box().unwrap(); + match inner_object.kind() { + Some("Page") => receive_update_post(&update, &conn, chat_server), + Some("Note") => receive_update_comment(&update, &conn, chat_server), + _ => Ok(HttpResponse::NotImplemented().finish()), + } + } + Some("Like") => { + let like = object.into_concrete::()?; + let inner_object = like.like_props.get_object_base_box().unwrap(); + match inner_object.kind() { + Some("Page") => receive_like_post(&like, &conn, chat_server), + Some("Note") => receive_like_comment(&like, &conn, chat_server), + _ => Ok(HttpResponse::NotImplemented().finish()), + } + } + Some("Dislike") => { + let dislike = object.into_concrete::()?; + let inner_object = dislike.dislike_props.get_object_base_box().unwrap(); + match inner_object.kind() { + Some("Page") => receive_dislike_post(&dislike, &conn, chat_server), + Some("Note") => receive_dislike_comment(&dislike, &conn, chat_server), + _ => Ok(HttpResponse::NotImplemented().finish()), + } + } + Some("Delete") => { + let delete = object.into_concrete::()?; + let inner_object = delete.delete_props.get_object_base_box().unwrap(); + match inner_object.kind() { + Some("Page") => receive_delete_post(&delete, &conn, chat_server), + Some("Note") => receive_delete_comment(&delete, &conn, chat_server), + _ => Ok(HttpResponse::NotImplemented().finish()), + } + } + Some("Remove") => { + let remove = object.into_concrete::()?; + let inner_object = remove.remove_props.get_object_base_box().unwrap(); + match inner_object.kind() { + Some("Page") => receive_remove_post(&remove, &conn, chat_server), + Some("Note") => receive_remove_comment(&remove, &conn, chat_server), + _ => Ok(HttpResponse::NotImplemented().finish()), + } + } + Some("Undo") => { + let undo = object.into_concrete::()?; + let inner_object = undo.undo_props.get_object_base_box().unwrap(); + match inner_object.kind() { + Some("Delete") => receive_undo_delete(&undo, &conn, chat_server), + Some("Remove") => receive_undo_remove(&undo, &conn, chat_server), + Some("Like") => receive_undo_like(&undo, &conn, chat_server), + _ => Ok(HttpResponse::NotImplemented().finish()), + } + } + _ => Ok(HttpResponse::NotImplemented().finish()), + } +} + fn receive_create_post( create: &Create, conn: &PgConnection, @@ -1518,8 +1576,6 @@ pub fn do_announce( where A: Activity + Base + Serialize + Debug, { - dbg!(&community_uri); - // TODO: this fails for some reason let community = Community::read_from_actor_id(conn, &community_uri)?; // TODO: need to add boolean param is_local_activity @@ -1545,9 +1601,6 @@ where // this seems to be the "easiest" stable alternative for remove_item() to.retain(|x| *x != sending_user.get_shared_inbox_url()); - dbg!(&announce); - dbg!(&to); - send_activity(&announce, &community, to)?; Ok(HttpResponse::Ok().finish()) diff --git a/server/src/db/code_migrations.rs b/server/src/db/code_migrations.rs index 11c8ba1fe..204bfe791 100644 --- a/server/src/db/code_migrations.rs +++ b/server/src/db/code_migrations.rs @@ -124,7 +124,7 @@ fn post_updates_2020_04_03(conn: &PgConnection) -> Result<(), Error> { .load::(conn)?; for cpost in &incorrect_posts { - Post::update_ap_id(&conn, cpost.id, todo!())?; + Post::update_ap_id(&conn, cpost.id)?; } info!("{} post rows updated.", incorrect_posts.len()); diff --git a/server/src/db/post.rs b/server/src/db/post.rs index ff3af55d5..d12f98d81 100644 --- a/server/src/db/post.rs +++ b/server/src/db/post.rs @@ -75,7 +75,7 @@ impl Post { post.filter(ap_id.eq(object_id)).first::(conn) } - pub fn update_ap_id(conn: &PgConnection, post_id: i32, ap_id: &str) -> Result { + pub fn update_ap_id(conn: &PgConnection, post_id: i32) -> Result { use crate::schema::post::dsl::*; let apid = make_apub_endpoint(EndpointType::Post, &post_id.to_string()).to_string();