2020-04-15 18:12:25 +00:00
|
|
|
use crate::db::post::{Post, PostForm};
|
|
|
|
use crate::db::Crud;
|
|
|
|
use activitystreams::activity::{Accept, Create, Update};
|
|
|
|
use activitystreams::object::Page;
|
|
|
|
use actix_web::{web, HttpResponse};
|
|
|
|
use diesel::r2d2::{ConnectionManager, Pool};
|
|
|
|
use diesel::PgConnection;
|
|
|
|
use failure::Error;
|
2020-04-17 14:55:28 +00:00
|
|
|
use log::debug;
|
|
|
|
use serde::Deserialize;
|
2020-04-15 18:12:25 +00:00
|
|
|
|
|
|
|
#[serde(untagged)]
|
2020-04-17 14:55:28 +00:00
|
|
|
#[derive(Deserialize, Debug)]
|
2020-04-15 18:12:25 +00:00
|
|
|
pub enum UserAcceptedObjects {
|
|
|
|
Create(Create),
|
|
|
|
Update(Update),
|
|
|
|
Accept(Accept),
|
|
|
|
}
|
|
|
|
|
2020-04-17 14:55:28 +00:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct Params {
|
|
|
|
user_name: String,
|
|
|
|
}
|
|
|
|
|
2020-04-17 15:33:55 +00:00
|
|
|
/// Handler for all incoming activities to user inboxes.
|
2020-04-15 18:12:25 +00:00
|
|
|
pub async fn user_inbox(
|
|
|
|
input: web::Json<UserAcceptedObjects>,
|
2020-04-17 14:55:28 +00:00
|
|
|
params: web::Query<Params>,
|
2020-04-15 18:12:25 +00:00
|
|
|
db: web::Data<Pool<ConnectionManager<PgConnection>>>,
|
|
|
|
) -> Result<HttpResponse, Error> {
|
|
|
|
let input = input.into_inner();
|
|
|
|
let conn = &db.get().unwrap();
|
2020-04-17 14:55:28 +00:00
|
|
|
debug!("User {} received activity: {:?}", ¶ms.user_name, &input);
|
|
|
|
|
2020-04-15 18:12:25 +00:00
|
|
|
match input {
|
|
|
|
UserAcceptedObjects::Create(c) => handle_create(&c, conn),
|
|
|
|
UserAcceptedObjects::Update(u) => handle_update(&u, conn),
|
|
|
|
UserAcceptedObjects::Accept(a) => handle_accept(&a, conn),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-17 15:33:55 +00:00
|
|
|
/// Handle create activities and insert them in the database.
|
2020-04-15 18:12:25 +00:00
|
|
|
fn handle_create(create: &Create, conn: &PgConnection) -> Result<HttpResponse, Error> {
|
|
|
|
let page = create
|
|
|
|
.create_props
|
|
|
|
.get_object_base_box()
|
|
|
|
.to_owned()
|
|
|
|
.unwrap()
|
|
|
|
.to_owned()
|
|
|
|
.to_concrete::<Page>()?;
|
|
|
|
let post = PostForm::from_page(&page, conn)?;
|
|
|
|
Post::create(conn, &post)?;
|
|
|
|
// TODO: send the new post out via websocket
|
|
|
|
Ok(HttpResponse::Ok().finish())
|
|
|
|
}
|
|
|
|
|
2020-04-17 15:33:55 +00:00
|
|
|
/// Handle update activities and insert them in the database.
|
2020-04-15 18:12:25 +00:00
|
|
|
fn handle_update(update: &Update, conn: &PgConnection) -> Result<HttpResponse, Error> {
|
|
|
|
let page = update
|
|
|
|
.update_props
|
|
|
|
.get_object_base_box()
|
|
|
|
.to_owned()
|
|
|
|
.unwrap()
|
|
|
|
.to_owned()
|
|
|
|
.to_concrete::<Page>()?;
|
|
|
|
let post = PostForm::from_page(&page, conn)?;
|
|
|
|
let id = Post::read_from_apub_id(conn, &post.ap_id)?.id;
|
|
|
|
Post::update(conn, id, &post)?;
|
|
|
|
// TODO: send the new post out via websocket
|
|
|
|
Ok(HttpResponse::Ok().finish())
|
|
|
|
}
|
|
|
|
|
2020-04-17 15:33:55 +00:00
|
|
|
/// Handle accepted follows.
|
2020-04-17 14:55:28 +00:00
|
|
|
fn handle_accept(_accept: &Accept, _conn: &PgConnection) -> Result<HttpResponse, Error> {
|
|
|
|
// TODO: make sure that we actually requested a follow
|
2020-04-15 18:12:25 +00:00
|
|
|
// TODO: at this point, indicate to the user that they are following the community
|
|
|
|
Ok(HttpResponse::Ok().finish())
|
|
|
|
}
|