Plume/src/routes/user.rs

120 lines
3.7 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-05-01 11:48:19 +00:00
use serde_json;
2018-04-22 18:13:12 +00:00
2018-05-04 13:13:55 +00:00
use activity_pub::{activity, activity_pub, ActivityPub, context};
2018-04-24 09:21:39 +00:00
use activity_pub::actor::Actor;
2018-05-01 14:00:29 +00:00
use activity_pub::inbox::Inbox;
use activity_pub::object::Object;
2018-04-29 18:01:42 +00:00
use activity_pub::outbox::Outbox;
2018-04-22 18:13:12 +00:00
use db_conn::DbConn;
2018-05-01 19:57:30 +00:00
use models::follows::*;
2018-04-22 18:13:12 +00:00
use models::instance::Instance;
use models::posts::Post;
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")]
2018-05-09 19:09:52 +00:00
fn me(user: User) -> Redirect {
Redirect::to(format!("/@/{}", user.username).as_ref())
2018-04-23 09:52:44 +00:00
}
2018-04-22 18:13:12 +00:00
2018-04-23 15:09:05 +00:00
#[get("/@/<name>", rank = 2)]
2018-05-10 20:31:52 +00:00
fn details(name: String, conn: DbConn, account: Option<User>) -> Template {
2018-05-01 11:48:19 +00:00
let user = User::find_by_fqn(&*conn, name).unwrap();
let recents = Post::get_recents_for_author(&*conn, &user, 5);
2018-05-01 11:48:19 +00:00
Template::render("users/details", json!({
2018-05-10 20:31:52 +00:00
"user": serde_json::to_value(user).unwrap(),
"account": account,
"recents": recents.into_iter().map(|p| {
json!({
"post": p,
"author": p.get_authors(&*conn)[0],
"url": p.compute_id(&*conn),
"date": p.creation_date.timestamp()
})
}).collect::<Vec<serde_json::Value>>()
2018-05-01 11:48:19 +00:00
}))
2018-04-22 18:13:12 +00:00
}
2018-05-01 19:57:30 +00: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 19:57:30 +00:00
Redirect::to(format!("/@/{}", name).as_ref())
}
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-05-01 11:48:19 +00:00
let user = User::find_local(&*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")]
2018-05-10 20:31:52 +00:00
fn new(user: Option<User>) -> Template {
Template::render("users/new", json!({
"account": user
}))
2018-04-22 18:13:12 +00: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 18:13:12 +00:00
}
Redirect::to(format!("/@/{}", data.get().username).as_str())
}
2018-04-29 18:01:42 +00:00
#[get("/@/<name>/outbox")]
fn outbox(name: String, conn: DbConn) -> Outbox {
2018-05-01 11:48:19 +00:00
let user = User::find_local(&*conn, name).unwrap();
2018-04-29 18:01:42 +00:00
user.outbox(&*conn)
}
2018-05-01 14:00:29 +00: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 13:13:55 +00: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)
}