]> Untitled Git - lemmy.git/commitdiff
Some code cleanup and better logging
authorFelix <me@nutomic.com>
Fri, 17 Apr 2020 14:55:28 +0000 (16:55 +0200)
committerFelix <me@nutomic.com>
Fri, 17 Apr 2020 14:55:28 +0000 (16:55 +0200)
server/src/apub/activities.rs
server/src/apub/community_inbox.rs
server/src/apub/user_inbox.rs
server/src/routes/federation.rs

index 2e35400d0f351ff03ad5ce0d4676569a790bb4f8..8885f5558ef793621a2a39fb4a82bd287432d16c 100644 (file)
@@ -11,6 +11,7 @@ use diesel::PgConnection;
 use failure::Error;
 use failure::_core::fmt::Debug;
 use isahc::prelude::*;
+use log::debug;
 use serde::Serialize;
 
 fn populate_object_props(
@@ -34,14 +35,14 @@ where
   A: Serialize + Debug,
 {
   let json = serde_json::to_string(&activity)?;
-  println!("sending data {}", json);
+  debug!("Sending activitypub activity {}", json);
   for t in to {
-    println!("to: {}", t);
+    debug!("Sending activity to: {}", t);
     let res = Request::post(t)
       .header("Content-Type", "application/json")
       .body(json.to_owned())?
       .send()?;
-    dbg!(res);
+    debug!("Result for activity send: {:?}", res);
   }
   Ok(())
 }
index 0fe3fd709ec29d0fd39dc5f1927cd5377a6c40c6..caadecf132765e1996b8707bcf16d9938e196237 100644 (file)
@@ -7,28 +7,38 @@ use actix_web::{web, HttpResponse};
 use diesel::r2d2::{ConnectionManager, Pool};
 use diesel::PgConnection;
 use failure::Error;
+use log::debug;
+use serde::Deserialize;
 use url::Url;
 
 #[serde(untagged)]
-#[derive(serde::Deserialize)]
+#[derive(Deserialize, Debug)]
 pub enum CommunityAcceptedObjects {
   Follow(Follow),
 }
 
+#[derive(Deserialize)]
+pub struct Params {
+  community_name: String,
+}
+
 pub async fn community_inbox(
   input: web::Json<CommunityAcceptedObjects>,
+  params: web::Query<Params>,
   db: web::Data<Pool<ConnectionManager<PgConnection>>>,
 ) -> Result<HttpResponse, Error> {
   let input = input.into_inner();
   let conn = &db.get().unwrap();
+  debug!(
+    "Community {} received activity {:?}",
+    &params.community_name, &input
+  );
   match input {
     CommunityAcceptedObjects::Follow(f) => handle_follow(&f, conn),
   }
 }
 
 fn handle_follow(follow: &Follow, conn: &PgConnection) -> Result<HttpResponse, Error> {
-  println!("received follow: {:?}", &follow);
-
   // TODO: make sure this is a local community
   let community_uri = follow
     .follow_props
@@ -42,7 +52,6 @@ fn handle_follow(follow: &Follow, conn: &PgConnection) -> Result<HttpResponse, E
     .unwrap()
     .to_string();
   let user = fetch_remote_user(&Url::parse(&user_uri)?, conn)?;
-  // TODO: insert ID of the user into follows of the community
   let community_follower_form = CommunityFollowerForm {
     community_id: community.id,
     user_id: user.id,
index 02517afe07f3d48a6aad0d0561b8bce97cea0fb3..3b8d1df35be7cdc8517e6c8475e16edcbd54287d 100644 (file)
@@ -6,21 +6,31 @@ use actix_web::{web, HttpResponse};
 use diesel::r2d2::{ConnectionManager, Pool};
 use diesel::PgConnection;
 use failure::Error;
+use log::debug;
+use serde::Deserialize;
 
 #[serde(untagged)]
-#[derive(serde::Deserialize)]
+#[derive(Deserialize, Debug)]
 pub enum UserAcceptedObjects {
   Create(Create),
   Update(Update),
   Accept(Accept),
 }
 
+#[derive(Deserialize)]
+pub struct Params {
+  user_name: String,
+}
+
 pub async fn user_inbox(
   input: web::Json<UserAcceptedObjects>,
+  params: web::Query<Params>,
   db: web::Data<Pool<ConnectionManager<PgConnection>>>,
 ) -> Result<HttpResponse, Error> {
   let input = input.into_inner();
   let conn = &db.get().unwrap();
+  debug!("User {} received activity: {:?}", &params.user_name, &input);
+
   match input {
     UserAcceptedObjects::Create(c) => handle_create(&c, conn),
     UserAcceptedObjects::Update(u) => handle_update(&u, conn),
@@ -57,8 +67,8 @@ fn handle_update(update: &Update, conn: &PgConnection) -> Result<HttpResponse, E
   Ok(HttpResponse::Ok().finish())
 }
 
-fn handle_accept(accept: &Accept, _conn: &PgConnection) -> Result<HttpResponse, Error> {
-  println!("received accept: {:?}", &accept);
+fn handle_accept(_accept: &Accept, _conn: &PgConnection) -> Result<HttpResponse, Error> {
+  // TODO: make sure that we actually requested a follow
   // TODO: at this point, indicate to the user that they are following the community
   Ok(HttpResponse::Ok().finish())
 }
index ef7ba56f826068ca568b540bfe2188cd73f8a3f1..d80759c3e821085bb52335ac5082d30ac2b1c8af 100644 (file)
@@ -8,11 +8,11 @@ pub fn config(cfg: &mut web::ServiceConfig) {
     cfg
       // TODO: check the user/community params for these
       .route(
-        "/federation/c/{_}/inbox",
+        "/federation/c/{community_name}/inbox",
         web::post().to(apub::community_inbox::community_inbox),
       )
       .route(
-        "/federation/u/{_}/inbox",
+        "/federation/u/{user_name}/inbox",
         web::post().to(apub::user_inbox::user_inbox),
       )
       .route(