Plume/src/routes/instance.rs

77 lines
2.3 KiB
Rust
Raw Normal View History

use rocket::request::Form;
2018-04-24 11:21:39 +02:00
use rocket::response::Redirect;
use rocket_contrib::Template;
use serde_json;
use BASE_URL;
2018-05-13 19:39:18 +02:00
use activity_pub::inbox::Inbox;
use db_conn::DbConn;
use models::posts::Post;
2018-05-10 22:31:52 +02:00
use models::users::User;
use models::instance::*;
2018-04-29 19:50:46 +02:00
#[get("/")]
2018-05-10 22:31:52 +02:00
fn index(conn: DbConn, user: Option<User>) -> Template {
2018-04-29 19:50:46 +02:00
match Instance::get_local(&*conn) {
Some(inst) => {
let recents = Post::get_recents(&*conn, 5);
2018-05-09 21:09:52 +02:00
Template::render("instance/index", json!({
2018-05-10 22:31:52 +02:00
"instance": inst,
"account": user,
"recents": recents.into_iter().map(|p| {
json!({
"post": p,
"author": ({
let author = &p.get_authors(&*conn)[0];
let mut json = serde_json::to_value(author).unwrap();
json["fqn"] = serde_json::Value::String(author.get_fqn(&*conn));
json
}),
2018-05-12 19:59:38 +02:00
"url": format!("/~/{}/{}/", p.get_blog(&*conn).actor_id, p.slug),
"date": p.creation_date.timestamp()
})
}).collect::<Vec<serde_json::Value>>()
2018-05-09 21:09:52 +02:00
}))
2018-04-29 19:50:46 +02:00
}
None => {
2018-05-09 21:09:52 +02:00
Template::render("errors/500", json!({
"error_message": "You need to configure your instance before using it."
}))
2018-04-29 19:50:46 +02:00
}
}
}
#[get("/configure")]
fn configure() -> Template {
2018-05-10 22:31:52 +02:00
Template::render("instance/configure", json!({}))
}
#[derive(FromForm)]
struct NewInstanceForm {
name: String
}
#[post("/configure", data = "<data>")]
fn post_config(conn: DbConn, data: Form<NewInstanceForm>) -> Redirect {
let form = data.get();
let inst = Instance::insert(
&*conn,
BASE_URL.as_str().to_string(),
form.name.to_string(),
true);
if inst.has_admin(&*conn) {
Redirect::to("/")
} else {
Redirect::to("/users/new")
}
}
2018-05-13 19:39:18 +02:00
#[post("/inbox", data = "<data>")]
fn shared_inbox(conn: DbConn, data: String) -> String {
let act: serde_json::Value = serde_json::from_str(&data[..]).unwrap();
let instance = Instance::get_local(&*conn).unwrap();
instance.received(&*conn, act);
String::from("")
}