From: Felix Ableitner Date: Sat, 30 May 2020 13:38:01 +0000 (+0200) Subject: more fixes X-Git-Url: http://these/git/%22%7Burl%7D/%7B%60%24%7BghostArchiveUrl%7D/static/%7BpictshareAvatarThumbnail%28post.creator_avatar%29%7D?a=commitdiff_plain;h=52206998aaff0a32e97c250cadd537c11699398f;p=lemmy.git more fixes --- diff --git a/server/src/api/post.rs b/server/src/api/post.rs index aee60732..9bbde791 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 6322e7fc..0510cbba 100644 --- a/server/src/apub/shared_inbox.rs +++ b/server/src/apub/shared_inbox.rs @@ -183,31 +183,89 @@ 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")) => { + (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::()?; - receive_create_post(&create, &conn, chat_server) + 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()), + } } - (SharedAcceptedObjects::Announce(_a), Some("Update")) => { + Some("Update") => { let update = object.into_concrete::()?; - receive_update_post(&update, &conn, chat_server) + 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()), + } } - (SharedAcceptedObjects::Announce(_a), Some("Like")) => { + Some("Like") => { let like = object.into_concrete::()?; - receive_like_post(&like, &conn, chat_server) + 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()), + } } - (SharedAcceptedObjects::Announce(_a), Some("Dislike")) => { + Some("Dislike") => { let dislike = object.into_concrete::()?; - receive_dislike_post(&dislike, &conn, chat_server) + 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()), + } } - (SharedAcceptedObjects::Announce(_a), Some("Delete")) => { + Some("Delete") => { let delete = object.into_concrete::()?; - receive_delete_post(&delete, &conn, chat_server) + 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()), + } } - (SharedAcceptedObjects::Announce(_a), Some("Remove")) => { + Some("Remove") => { let remove = object.into_concrete::()?; - receive_remove_post(&remove, &conn, chat_server) + 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()), + } } - _ => Err(format_err!("Unknown incoming activity type.")), + 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()), } } @@ -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 11c8ba1f..204bfe79 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 ff3af55d..d12f98d8 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();