Plume/src/routes/user.rs

93 lines
2.7 KiB
Rust
Raw Normal View History

2018-04-22 20:13:12 +02:00
use rocket::request::Form;
use rocket::response::Redirect;
2018-04-24 14:31:02 +02:00
use rocket_contrib::Template;
2018-05-01 13:48:19 +02:00
use serde_json;
2018-04-22 20:13:12 +02:00
use std::collections::HashMap;
use activity_pub::ActivityPub;
2018-05-01 21:57:30 +02:00
use activity_pub::activity::Activity;
2018-04-24 11:21:39 +02:00
use activity_pub::actor::Actor;
2018-05-01 16:00:29 +02:00
use activity_pub::inbox::Inbox;
2018-04-29 20:01:42 +02:00
use activity_pub::outbox::Outbox;
2018-04-22 20:13:12 +02:00
use db_conn::DbConn;
2018-05-01 21:57:30 +02:00
use models::follows::*;
2018-04-22 20:13:12 +02:00
use models::instance::Instance;
2018-04-24 11:21:39 +02:00
use models::users::*;
2018-04-22 20:13:12 +02:00
2018-04-23 11:52:44 +02:00
#[get("/me")]
fn me(user: User) -> String {
format!("Logged in as {}", user.username.to_string())
}
2018-04-22 20:13:12 +02:00
2018-04-23 17:09:05 +02:00
#[get("/@/<name>", rank = 2)]
2018-05-01 13:48:19 +02:00
fn details(name: String, conn: DbConn) -> Template {
let user = User::find_by_fqn(&*conn, name).unwrap();
Template::render("users/details", json!({
"user": serde_json::to_value(user).unwrap()
}))
2018-04-22 20:13:12 +02:00
}
2018-05-01 21:57:30 +02:00
#[get("/@/<name>/follow")]
fn follow(name: String, conn: DbConn, user: User) -> Redirect {
let target = User::find_by_fqn(&*conn, name.clone()).unwrap();
Follow::insert(&*conn, NewFollow {
follower_id: user.id,
following_id: target.id
});
target.send_to_inbox(&*conn, Activity::follow(&user, &target, &*conn));
Redirect::to(format!("/@/{}", name).as_ref())
}
2018-04-23 17:09:05 +02:00
#[get("/@/<name>", format = "application/activity+json", rank = 1)]
fn activity_details(name: String, conn: DbConn) -> ActivityPub {
2018-05-01 13:48:19 +02:00
let user = User::find_local(&*conn, name).unwrap();
2018-04-24 14:31:02 +02:00
user.as_activity_pub(&*conn)
2018-04-23 17:09:05 +02:00
}
2018-04-22 20:13:12 +02:00
#[get("/users/new")]
fn new() -> Template {
Template::render("users/new", HashMap::<String, i32>::new())
}
#[derive(FromForm)]
struct NewUserForm {
username: String,
email: String,
password: String,
password_confirmation: String
}
#[post("/users/new", data = "<data>")]
fn create(conn: DbConn, data: Form<NewUserForm>) -> Redirect {
let inst = Instance::get_local(&*conn).unwrap();
let form = data.get();
if form.password == form.password_confirmation {
User::insert(&*conn, NewUser::new_local(
form.username.to_string(),
form.username.to_string(),
!inst.has_admin(&*conn),
String::from(""),
form.email.to_string(),
User::hash_pass(form.password.to_string()),
inst.id
)).update_boxes(&*conn);
2018-04-22 20:13:12 +02:00
}
Redirect::to(format!("/@/{}", data.get().username).as_str())
}
2018-04-29 20:01:42 +02:00
#[get("/@/<name>/outbox")]
fn outbox(name: String, conn: DbConn) -> Outbox {
2018-05-01 13:48:19 +02:00
let user = User::find_local(&*conn, name).unwrap();
2018-04-29 20:01:42 +02:00
user.outbox(&*conn)
}
2018-05-01 16:00:29 +02:00
#[post("/@/<name>/inbox", data = "<data>")]
fn inbox(name: String, conn: DbConn, data: String) -> String {
let user = User::find_local(&*conn, name).unwrap();
let act: serde_json::Value = serde_json::from_str(&data[..]).unwrap();
user.received(&*conn, act);
String::from("")
}