Plume/src/routes/user.rs

60 lines
1.6 KiB
Rust
Raw Normal View History

2018-04-22 18:13:12 +00:00
use rocket::request::Form;
use rocket::response::Redirect;
2018-04-24 12:31:02 +00:00
use rocket_contrib::Template;
2018-04-22 18:13:12 +00:00
use std::collections::HashMap;
use activity_pub::ActivityPub;
2018-04-24 09:21:39 +00:00
use activity_pub::actor::Actor;
2018-04-22 18:13:12 +00:00
use db_conn::DbConn;
use models::instance::Instance;
2018-04-24 09:21:39 +00:00
use models::users::*;
2018-04-22 18:13:12 +00:00
2018-04-23 09:52:44 +00:00
#[get("/me")]
fn me(user: User) -> String {
format!("Logged in as {}", user.username.to_string())
}
2018-04-22 18:13:12 +00:00
2018-04-23 15:09:05 +00:00
#[get("/@/<name>", rank = 2)]
2018-04-23 09:52:44 +00:00
fn details(name: String) -> String {
format!("Hello, @{}", name)
2018-04-22 18:13:12 +00:00
}
2018-04-23 15:09:05 +00:00
#[get("/@/<name>", format = "application/activity+json", rank = 1)]
fn activity_details(name: String, conn: DbConn) -> ActivityPub {
2018-04-23 15:09:05 +00:00
let user = User::find_by_name(&*conn, name).unwrap();
2018-04-24 12:31:02 +00:00
user.as_activity_pub(&*conn)
2018-04-23 15:09:05 +00:00
}
2018-04-22 18:13:12 +00: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 18:13:12 +00:00
}
Redirect::to(format!("/@/{}", data.get().username).as_str())
}