Some code cleanup and better logging

This commit is contained in:
Felix 2020-04-17 16:55:28 +02:00
parent 5ddbe8c627
commit 7d152c81be
4 changed files with 32 additions and 12 deletions

View 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(())
}

View 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,

View 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())
}

View 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(