From 4bf0ec94c8c0b87375aea5f147b48139361bc481 Mon Sep 17 00:00:00 2001
From: Felix Ableitner <me@nutomic.com>
Date: Wed, 16 Dec 2020 21:07:48 +0100
Subject: [PATCH] Create empty inbox collections for actors (ref #1322)

---
 docker/prod/deploy-federation-test.sh | 14 ++++++++++++++
 lemmy_apub/src/http/community.rs      | 16 ++++++++++++++++
 lemmy_apub/src/http/user.rs           | 16 ++++++++++++++++
 src/routes/federation.rs              | 14 ++++++++++++--
 4 files changed, 58 insertions(+), 2 deletions(-)
 create mode 100755 docker/prod/deploy-federation-test.sh

diff --git a/docker/prod/deploy-federation-test.sh b/docker/prod/deploy-federation-test.sh
new file mode 100755
index 00000000..e9d9aef8
--- /dev/null
+++ b/docker/prod/deploy-federation-test.sh
@@ -0,0 +1,14 @@
+#!/bin/bash
+set -e
+
+TAG="federation-test"
+
+sudo docker build ../../ --file Dockerfile -t "dessalines/lemmy:$TAG"
+sudo docker save "dessalines/lemmy:$TAG" -o "$TAG.tar"
+sudo chown "$(id -u):$(id -g)" "$TAG.tar"
+
+scp "$TAG.tar" enterprise.lemmy.ml:
+rm "$TAG.tar"
+ssh lemmy-test "cat $TAG.tar | docker load"
+ssh lemmy-test "rm $TAG.tar"
+ssh lemmy-test "cd /lemmy/enterprise.lemmy.ml && docker-compose up -d"
\ No newline at end of file
diff --git a/lemmy_apub/src/http/community.rs b/lemmy_apub/src/http/community.rs
index 0a90571f..eb23a7ed 100644
--- a/lemmy_apub/src/http/community.rs
+++ b/lemmy_apub/src/http/community.rs
@@ -94,3 +94,19 @@ pub async fn get_apub_community_outbox(
     .set_total_items(len as u64);
   Ok(create_apub_response(&collection))
 }
+
+pub async fn get_apub_community_inbox(
+  info: web::Path<CommunityQuery>,
+  context: web::Data<LemmyContext>,
+) -> Result<HttpResponse<Body>, LemmyError> {
+  let community = blocking(context.pool(), move |conn| {
+    Community::read_from_name(&conn, &info.community_name)
+  })
+  .await??;
+
+  let mut collection = OrderedCollection::new();
+  collection
+    .set_id(format!("{}/inbox", community.actor_id).parse()?)
+    .set_many_contexts(lemmy_context()?);
+  Ok(create_apub_response(&collection))
+}
diff --git a/lemmy_apub/src/http/user.rs b/lemmy_apub/src/http/user.rs
index 1e546d95..5acdd28d 100644
--- a/lemmy_apub/src/http/user.rs
+++ b/lemmy_apub/src/http/user.rs
@@ -52,3 +52,19 @@ pub async fn get_apub_user_outbox(
     .set_total_items(0_u64);
   Ok(create_apub_response(&collection))
 }
+
+pub async fn get_apub_user_inbox(
+  info: web::Path<UserQuery>,
+  context: web::Data<LemmyContext>,
+) -> Result<HttpResponse<Body>, LemmyError> {
+  let user = blocking(context.pool(), move |conn| {
+    User_::read_from_name(&conn, &info.user_name)
+  })
+  .await??;
+
+  let mut collection = OrderedCollection::new();
+  collection
+    .set_id(format!("{}/inbox", user.actor_id).parse()?)
+    .set_many_contexts(lemmy_context()?);
+  Ok(create_apub_response(&collection))
+}
diff --git a/src/routes/federation.rs b/src/routes/federation.rs
index 4d03de77..2cad8bc0 100644
--- a/src/routes/federation.rs
+++ b/src/routes/federation.rs
@@ -3,10 +3,15 @@ use http_signature_normalization_actix::digest::middleware::VerifyDigest;
 use lemmy_apub::{
   http::{
     comment::get_apub_comment,
-    community::{get_apub_community_followers, get_apub_community_http, get_apub_community_outbox},
+    community::{
+      get_apub_community_followers,
+      get_apub_community_http,
+      get_apub_community_inbox,
+      get_apub_community_outbox,
+    },
     get_activity,
     post::get_apub_post,
-    user::{get_apub_user_http, get_apub_user_outbox},
+    user::{get_apub_user_http, get_apub_user_inbox, get_apub_user_outbox},
   },
   inbox::{community_inbox::community_inbox, shared_inbox::shared_inbox, user_inbox::user_inbox},
   APUB_JSON_CONTENT_TYPE,
@@ -41,8 +46,13 @@ pub fn config(cfg: &mut web::ServiceConfig) {
             "/c/{community_name}/outbox",
             web::get().to(get_apub_community_outbox),
           )
+          .route(
+            "/c/{community_name}/inbox",
+            web::get().to(get_apub_community_inbox),
+          )
           .route("/u/{user_name}", web::get().to(get_apub_user_http))
           .route("/u/{user_name}/outbox", web::get().to(get_apub_user_outbox))
+          .route("/u/{user_name}/inbox", web::get().to(get_apub_user_inbox))
           .route("/post/{post_id}", web::get().to(get_apub_post))
           .route("/comment/{comment_id}", web::get().to(get_apub_comment))
           .route("/activities/{type_}/{id}", web::get().to(get_activity)),
-- 
2.44.1