Plume/src/routes/user.rs

110 lines
3.3 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;
2018-05-04 15:13:55 +02:00
use activity_pub::{activity, activity_pub, ActivityPub, context};
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")]
2018-05-09 21:09:52 +02:00
fn me(user: User) -> Redirect {
Redirect::to(format!("/@/{}", user.username).as_ref())
2018-04-23 11:52:44 +02:00
}
2018-04-22 20:13:12 +02:00
2018-04-23 17:09:05 +02:00
#[get("/@/<name>", rank = 2)]
2018-05-10 22:31:52 +02:00
fn details(name: String, conn: DbConn, account: Option<User>) -> Template {
2018-05-01 13:48:19 +02:00
let user = User::find_by_fqn(&*conn, name).unwrap();
Template::render("users/details", json!({
2018-05-10 22:31:52 +02:00
"user": serde_json::to_value(user).unwrap(),
"account": account
2018-05-01 13:48:19 +02:00
}))
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, &user, activity::Follow::new(&user, &target, &*conn));
2018-05-01 21:57:30 +02:00
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")]
2018-05-10 22:31:52 +02:00
fn new(user: Option<User>) -> Template {
Template::render("users/new", json!({
"account": user
}))
2018-04-22 20:13:12 +02:00
}
#[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("")
}
2018-05-04 15:13:55 +02:00
#[get("/@/<name>/followers")]
fn followers(name: String, conn: DbConn) -> ActivityPub {
let user = User::find_local(&*conn, name).unwrap();
let followers = user.get_followers(&*conn).into_iter().map(|f| f.compute_id(&*conn)).collect::<Vec<String>>();
let json = json!({
"@context": context(),
"id": user.compute_box(&*conn, "followers"),
"type": "OrderedCollection",
"totalItems": followers.len(),
"orderedItems": followers
});
activity_pub(json)
}