]> Untitled Git - lemmy.git/commitdiff
more fixes
authorFelix Ableitner <me@nutomic.com>
Sat, 30 May 2020 13:38:01 +0000 (15:38 +0200)
committerFelix Ableitner <me@nutomic.com>
Sat, 30 May 2020 13:38:01 +0000 (15:38 +0200)
server/src/api/post.rs
server/src/apub/shared_inbox.rs
server/src/db/code_migrations.rs
server/src/db/post.rs

index aee6073253f5a8af53650a699b6e497c196c1514..9bbde791977b8f4f16b2010b85a66616f311473b 100644 (file)
@@ -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<CreatePost> {
       }
     };
 
-    // 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()),
     };
index 6322e7fc69a4b1b3c6df4aa31708a82b3e441d4f..0510cbba8dae1058ca0921ac036853cd6186d5fe 100644 (file)
@@ -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<Announce>,
+  conn: &PgConnection,
+  chat_server: ChatServerParam,
+) -> Result<HttpResponse, Error> {
+  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::<Create>()?;
-      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::<Update>()?;
-      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::<Like>()?;
-      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::<Dislike>()?;
-      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::<Delete>()?;
-      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::<Remove>()?;
-      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::<Undo>()?;
+      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<A>(
 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())
index 11c8ba1fe255e6172db3661ff71dd0a6ee7a293d..204bfe791b1058c83ff58fb17d572378d3edbf5b 100644 (file)
@@ -124,7 +124,7 @@ fn post_updates_2020_04_03(conn: &PgConnection) -> Result<(), Error> {
     .load::<Post>(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());
index ff3af55d57ad4af384b3dfff4901488a280aad27..d12f98d81542fdb526642db75092df13a5557dcb 100644 (file)
@@ -75,7 +75,7 @@ impl Post {
     post.filter(ap_id.eq(object_id)).first::<Self>(conn)
   }
 
-  pub fn update_ap_id(conn: &PgConnection, post_id: i32, ap_id: &str) -> Result<Self, Error> {
+  pub fn update_ap_id(conn: &PgConnection, post_id: i32) -> Result<Self, Error> {
     use crate::schema::post::dsl::*;
 
     let apid = make_apub_endpoint(EndpointType::Post, &post_id.to_string()).to_string();