Plume/src/routes/user.rs

59 lines
1.5 KiB
Rust
Raw Normal View History

2018-04-22 20:13:12 +02:00
use rocket::request::Form;
use rocket::response::Redirect;
2018-04-23 17:09:05 +02:00
use rocket_contrib::{Json, Template};
2018-04-22 20:13:12 +02:00
use std::collections::HashMap;
use db_conn::DbConn;
2018-04-23 17:19:28 +02:00
use models::users::*;
2018-04-22 20:13:12 +02:00
use models::instance::Instance;
2018-04-23 17:09:05 +02:00
use activity_pub::Actor;
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-04-23 11:52:44 +02:00
fn details(name: String) -> String {
format!("Hello, @{}", name)
2018-04-22 20:13:12 +02:00
}
2018-04-23 17:09:05 +02:00
#[get("/@/<name>", format = "application/activity+json", rank = 1)]
fn activity(name: String, conn: DbConn) -> Json {
let user = User::find_by_name(&*conn, name).unwrap();
Json(user.as_activity_pub())
}
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())
}