Some code cleanup and better logging

This commit is contained in:
Felix 2020-04-17 16:55:28 +02:00
parent 9c974fbe50
commit 8908c8b184
4 changed files with 32 additions and 12 deletions

View file

@ -11,6 +11,7 @@ use diesel::PgConnection;
use failure::Error; use failure::Error;
use failure::_core::fmt::Debug; use failure::_core::fmt::Debug;
use isahc::prelude::*; use isahc::prelude::*;
use log::debug;
use serde::Serialize; use serde::Serialize;
fn populate_object_props( fn populate_object_props(
@ -34,14 +35,14 @@ where
A: Serialize + Debug, A: Serialize + Debug,
{ {
let json = serde_json::to_string(&activity)?; let json = serde_json::to_string(&activity)?;
println!("sending data {}", json); debug!("Sending activitypub activity {}", json);
for t in to { for t in to {
println!("to: {}", t); debug!("Sending activity to: {}", t);
let res = Request::post(t) let res = Request::post(t)
.header("Content-Type", "application/json") .header("Content-Type", "application/json")
.body(json.to_owned())? .body(json.to_owned())?
.send()?; .send()?;
dbg!(res); debug!("Result for activity send: {:?}", res);
} }
Ok(()) Ok(())
} }

View file

@ -7,28 +7,38 @@ use actix_web::{web, HttpResponse};
use diesel::r2d2::{ConnectionManager, Pool}; use diesel::r2d2::{ConnectionManager, Pool};
use diesel::PgConnection; use diesel::PgConnection;
use failure::Error; use failure::Error;
use log::debug;
use serde::Deserialize;
use url::Url; use url::Url;
#[serde(untagged)] #[serde(untagged)]
#[derive(serde::Deserialize)] #[derive(Deserialize, Debug)]
pub enum CommunityAcceptedObjects { pub enum CommunityAcceptedObjects {
Follow(Follow), Follow(Follow),
} }
#[derive(Deserialize)]
pub struct Params {
community_name: String,
}
pub async fn community_inbox( pub async fn community_inbox(
input: web::Json<CommunityAcceptedObjects>, input: web::Json<CommunityAcceptedObjects>,
params: web::Query<Params>,
db: web::Data<Pool<ConnectionManager<PgConnection>>>, db: web::Data<Pool<ConnectionManager<PgConnection>>>,
) -> Result<HttpResponse, Error> { ) -> Result<HttpResponse, Error> {
let input = input.into_inner(); let input = input.into_inner();
let conn = &db.get().unwrap(); let conn = &db.get().unwrap();
debug!(
"Community {} received activity {:?}",
&params.community_name, &input
);
match input { match input {
CommunityAcceptedObjects::Follow(f) => handle_follow(&f, conn), CommunityAcceptedObjects::Follow(f) => handle_follow(&f, conn),
} }
} }
fn handle_follow(follow: &Follow, conn: &PgConnection) -> Result<HttpResponse, Error> { fn handle_follow(follow: &Follow, conn: &PgConnection) -> Result<HttpResponse, Error> {
println!("received follow: {:?}", &follow);
// TODO: make sure this is a local community // TODO: make sure this is a local community
let community_uri = follow let community_uri = follow
.follow_props .follow_props
@ -42,7 +52,6 @@ fn handle_follow(follow: &Follow, conn: &PgConnection) -> Result<HttpResponse, E
.unwrap() .unwrap()
.to_string(); .to_string();
let user = fetch_remote_user(&Url::parse(&user_uri)?, conn)?; 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 { let community_follower_form = CommunityFollowerForm {
community_id: community.id, community_id: community.id,
user_id: user.id, user_id: user.id,

View file

@ -6,21 +6,31 @@ use actix_web::{web, HttpResponse};
use diesel::r2d2::{ConnectionManager, Pool}; use diesel::r2d2::{ConnectionManager, Pool};
use diesel::PgConnection; use diesel::PgConnection;
use failure::Error; use failure::Error;
use log::debug;
use serde::Deserialize;
#[serde(untagged)] #[serde(untagged)]
#[derive(serde::Deserialize)] #[derive(Deserialize, Debug)]
pub enum UserAcceptedObjects { pub enum UserAcceptedObjects {
Create(Create), Create(Create),
Update(Update), Update(Update),
Accept(Accept), Accept(Accept),
} }
#[derive(Deserialize)]
pub struct Params {
user_name: String,
}
pub async fn user_inbox( pub async fn user_inbox(
input: web::Json<UserAcceptedObjects>, input: web::Json<UserAcceptedObjects>,
params: web::Query<Params>,
db: web::Data<Pool<ConnectionManager<PgConnection>>>, db: web::Data<Pool<ConnectionManager<PgConnection>>>,
) -> Result<HttpResponse, Error> { ) -> Result<HttpResponse, Error> {
let input = input.into_inner(); let input = input.into_inner();
let conn = &db.get().unwrap(); let conn = &db.get().unwrap();
debug!("User {} received activity: {:?}", &params.user_name, &input);
match input { match input {
UserAcceptedObjects::Create(c) => handle_create(&c, conn), UserAcceptedObjects::Create(c) => handle_create(&c, conn),
UserAcceptedObjects::Update(u) => handle_update(&u, 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()) Ok(HttpResponse::Ok().finish())
} }
fn handle_accept(accept: &Accept, _conn: &PgConnection) -> Result<HttpResponse, Error> { fn handle_accept(_accept: &Accept, _conn: &PgConnection) -> Result<HttpResponse, Error> {
println!("received accept: {:?}", &accept); // TODO: make sure that we actually requested a follow
// TODO: at this point, indicate to the user that they are following the community // TODO: at this point, indicate to the user that they are following the community
Ok(HttpResponse::Ok().finish()) Ok(HttpResponse::Ok().finish())
} }

View file

@ -8,11 +8,11 @@ pub fn config(cfg: &mut web::ServiceConfig) {
cfg cfg
// TODO: check the user/community params for these // TODO: check the user/community params for these
.route( .route(
"/federation/c/{_}/inbox", "/federation/c/{community_name}/inbox",
web::post().to(apub::community_inbox::community_inbox), web::post().to(apub::community_inbox::community_inbox),
) )
.route( .route(
"/federation/u/{_}/inbox", "/federation/u/{user_name}/inbox",
web::post().to(apub::user_inbox::user_inbox), web::post().to(apub::user_inbox::user_inbox),
) )
.route( .route(