2018-06-17 18:06:47 +02:00
|
|
|
use gettextrs::gettext;
|
2018-07-27 19:05:36 +02:00
|
|
|
use rocket::{request::LenientForm, response::Redirect};
|
2018-06-10 21:33:42 +02:00
|
|
|
use rocket_contrib::{Json, Template};
|
2018-05-12 14:56:38 +02:00
|
|
|
use serde_json;
|
2018-07-27 19:05:36 +02:00
|
|
|
use validator::{Validate};
|
2018-04-22 15:35:37 +02:00
|
|
|
|
2018-06-23 18:36:11 +02:00
|
|
|
use plume_models::{
|
2018-07-27 19:05:36 +02:00
|
|
|
admin::Admin,
|
2018-06-10 21:33:42 +02:00
|
|
|
comments::Comment,
|
2018-06-23 18:36:11 +02:00
|
|
|
db_conn::DbConn,
|
2018-05-19 09:39:59 +02:00
|
|
|
posts::Post,
|
|
|
|
users::User,
|
|
|
|
instance::*
|
|
|
|
};
|
2018-06-23 18:36:11 +02:00
|
|
|
use inbox::Inbox;
|
2018-07-25 15:20:09 +02:00
|
|
|
use routes::Page;
|
2018-04-22 15:35:37 +02:00
|
|
|
|
2018-07-25 15:20:09 +02:00
|
|
|
#[get("/?<page>")]
|
|
|
|
fn paginated_index(conn: DbConn, user: Option<User>, page: Page) -> Template {
|
2018-04-29 19:50:46 +02:00
|
|
|
match Instance::get_local(&*conn) {
|
|
|
|
Some(inst) => {
|
2018-07-25 15:20:09 +02:00
|
|
|
let recents = Post::get_recents_page(&*conn, page.limits());
|
2018-05-12 14:56:38 +02:00
|
|
|
|
2018-05-09 21:09:52 +02:00
|
|
|
Template::render("instance/index", json!({
|
2018-05-10 22:31:52 +02:00
|
|
|
"instance": inst,
|
2018-05-12 14:56:38 +02:00
|
|
|
"account": user,
|
2018-07-25 15:20:09 +02:00
|
|
|
"recents": recents.into_iter().map(|p| p.to_json(&*conn)).collect::<Vec<serde_json::Value>>(),
|
|
|
|
"page": page.page,
|
2018-07-27 22:16:17 +02:00
|
|
|
"n_pages": Page::total(Post::count(&*conn) as i32),
|
|
|
|
"n_users": User::count_local(&*conn),
|
|
|
|
"n_articles": Post::count_local(&*conn)
|
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!({
|
2018-06-17 18:06:47 +02:00
|
|
|
"error_message": gettext("You need to configure your instance before using it.".to_string())
|
2018-05-09 21:09:52 +02:00
|
|
|
}))
|
2018-04-29 19:50:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-25 15:20:09 +02:00
|
|
|
#[get("/")]
|
|
|
|
fn index(conn: DbConn, user: Option<User>) -> Template {
|
|
|
|
paginated_index(conn, user, Page::first())
|
|
|
|
}
|
|
|
|
|
2018-07-27 19:05:36 +02:00
|
|
|
#[get("/admin")]
|
|
|
|
fn admin(conn: DbConn, admin: Admin) -> Template {
|
|
|
|
Template::render("instance/admin", json!({
|
|
|
|
"account": admin.0,
|
|
|
|
"instance": Instance::get_local(&*conn),
|
|
|
|
"errors": null,
|
|
|
|
"form": null
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(FromForm, Validate, Serialize)]
|
|
|
|
struct InstanceSettingsForm {
|
|
|
|
#[validate(length(min = "1"))]
|
|
|
|
name: String,
|
|
|
|
open_registrations: bool,
|
|
|
|
short_description: String,
|
|
|
|
long_description: String,
|
|
|
|
#[validate(length(min = "1"))]
|
|
|
|
default_license: String
|
|
|
|
}
|
|
|
|
|
|
|
|
#[post("/admin", data = "<form>")]
|
|
|
|
fn update_settings(conn: DbConn, admin: Admin, form: LenientForm<InstanceSettingsForm>) -> Result<Redirect, Template> {
|
|
|
|
let form = form.get();
|
|
|
|
form.validate()
|
|
|
|
.map(|_| {
|
|
|
|
let instance = Instance::get_local(&*conn).unwrap();
|
|
|
|
instance.update(&*conn,
|
|
|
|
form.name.clone(),
|
|
|
|
form.open_registrations,
|
|
|
|
form.short_description.clone(),
|
|
|
|
form.long_description.clone());
|
|
|
|
Redirect::to(uri!(admin))
|
|
|
|
})
|
|
|
|
.map_err(|e| Template::render("instance/admin", json!({
|
|
|
|
"account": admin.0,
|
|
|
|
"instance": Instance::get_local(&*conn),
|
|
|
|
"errors": e.inner(),
|
|
|
|
"form": form
|
|
|
|
})))
|
|
|
|
}
|
|
|
|
|
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();
|
2018-06-21 18:00:37 +02:00
|
|
|
match instance.received(&*conn, act) {
|
|
|
|
Ok(_) => String::new(),
|
|
|
|
Err(e) => {
|
|
|
|
println!("Shared inbox error: {}\n{}", e.cause(), e.backtrace());
|
|
|
|
format!("Error: {}", e.cause())
|
|
|
|
}
|
|
|
|
}
|
2018-05-13 19:39:18 +02:00
|
|
|
}
|
2018-06-10 21:33:42 +02:00
|
|
|
|
|
|
|
#[get("/nodeinfo")]
|
2018-06-16 19:39:22 +02:00
|
|
|
fn nodeinfo(conn: DbConn) -> Json<serde_json::Value> {
|
2018-06-10 21:33:42 +02:00
|
|
|
Json(json!({
|
|
|
|
"version": "2.0",
|
|
|
|
"software": {
|
|
|
|
"name": "Plume",
|
|
|
|
"version": "0.1.0"
|
|
|
|
},
|
|
|
|
"protocols": ["activitypub"],
|
|
|
|
"services": {
|
|
|
|
"inbound": [],
|
|
|
|
"outbound": []
|
|
|
|
},
|
|
|
|
"openRegistrations": true,
|
|
|
|
"usage": {
|
|
|
|
"users": {
|
|
|
|
"total": User::count_local(&*conn)
|
|
|
|
},
|
|
|
|
"localPosts": Post::count_local(&*conn),
|
|
|
|
"localComments": Comment::count_local(&*conn)
|
|
|
|
},
|
|
|
|
"metadata": {}
|
|
|
|
}))
|
|
|
|
}
|