2018-10-20 11:04:20 +02:00
|
|
|
use rocket::{request::LenientForm, response::{status, Redirect}};
|
2018-12-06 18:54:16 +01:00
|
|
|
use rocket_contrib::json::Json;
|
|
|
|
use rocket_i18n::I18n;
|
2018-05-12 14:56:38 +02:00
|
|
|
use serde_json;
|
2018-12-06 18:54:16 +01:00
|
|
|
use validator::{Validate, ValidationErrors};
|
2018-04-22 15:35:37 +02:00
|
|
|
|
2018-10-06 10:06:06 +02:00
|
|
|
use plume_common::activity_pub::sign::{Signable,
|
|
|
|
verify_http_headers};
|
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,
|
2019-01-05 22:30:28 +01:00
|
|
|
Error,
|
2018-10-03 20:48:25 +02:00
|
|
|
headers::Headers,
|
2018-05-19 09:39:59 +02:00
|
|
|
posts::Post,
|
|
|
|
users::User,
|
2018-09-14 20:25:16 +02:00
|
|
|
safe_string::SafeString,
|
2018-05-19 09:39:59 +02:00
|
|
|
instance::*
|
|
|
|
};
|
2018-12-22 18:27:21 +01:00
|
|
|
use inbox::{Inbox, SignedJson};
|
2018-12-29 09:36:07 +01:00
|
|
|
use routes::{Page, errors::ErrorPage};
|
2018-12-06 18:54:16 +01:00
|
|
|
use template_utils::Ructe;
|
2018-12-02 17:37:51 +01:00
|
|
|
use Searcher;
|
2018-04-22 15:35:37 +02:00
|
|
|
|
2018-09-05 19:03:02 +02:00
|
|
|
#[get("/")]
|
2018-12-29 09:36:07 +01:00
|
|
|
pub fn index(conn: DbConn, user: Option<User>, intl: I18n) -> Result<Ructe, ErrorPage> {
|
|
|
|
let inst = Instance::get_local(&*conn)?;
|
|
|
|
let federated = Post::get_recents_page(&*conn, Page::default().limits())?;
|
|
|
|
let local = Post::get_instance_page(&*conn, inst.id, Page::default().limits())?;
|
|
|
|
let user_feed = user.clone().and_then(|user| {
|
2019-02-26 13:13:00 +01:00
|
|
|
let followed = user.get_followed(&*conn).ok()?;
|
2018-12-29 09:36:07 +01:00
|
|
|
let mut in_feed = followed.into_iter().map(|u| u.id).collect::<Vec<i32>>();
|
|
|
|
in_feed.push(user.id);
|
|
|
|
Post::user_feed_page(&*conn, in_feed, Page::default().limits()).ok()
|
|
|
|
});
|
2018-05-12 14:56:38 +02:00
|
|
|
|
2018-12-29 09:36:07 +01:00
|
|
|
Ok(render!(instance::index(
|
|
|
|
&(&*conn, &intl.catalog, user),
|
|
|
|
inst,
|
|
|
|
User::count_local(&*conn)?,
|
|
|
|
Post::count_local(&*conn)?,
|
|
|
|
local,
|
|
|
|
federated,
|
|
|
|
user_feed
|
|
|
|
)))
|
2018-04-29 19:50:46 +02:00
|
|
|
}
|
|
|
|
|
2018-09-04 21:56:27 +02:00
|
|
|
#[get("/local?<page>")]
|
2018-12-29 09:36:07 +01:00
|
|
|
pub fn local(conn: DbConn, user: Option<User>, page: Option<Page>, intl: I18n) -> Result<Ructe, ErrorPage> {
|
2018-12-13 22:20:19 +01:00
|
|
|
let page = page.unwrap_or_default();
|
2018-12-29 09:36:07 +01:00
|
|
|
let instance = Instance::get_local(&*conn)?;
|
|
|
|
let articles = Post::get_instance_page(&*conn, instance.id, page.limits())?;
|
|
|
|
Ok(render!(instance::local(
|
2018-12-06 18:54:16 +01:00
|
|
|
&(&*conn, &intl.catalog, user),
|
|
|
|
instance,
|
|
|
|
articles,
|
|
|
|
page.0,
|
2018-12-29 09:36:07 +01:00
|
|
|
Page::total(Post::count_local(&*conn)? as i32)
|
|
|
|
)))
|
2018-09-04 21:56:27 +02:00
|
|
|
}
|
|
|
|
|
2018-09-05 16:21:50 +02:00
|
|
|
#[get("/feed?<page>")]
|
2018-12-29 09:36:07 +01:00
|
|
|
pub fn feed(conn: DbConn, user: User, page: Option<Page>, intl: I18n) -> Result<Ructe, ErrorPage> {
|
2018-12-13 22:20:19 +01:00
|
|
|
let page = page.unwrap_or_default();
|
2019-02-26 13:13:00 +01:00
|
|
|
let followed = user.get_followed(&*conn)?;
|
2018-10-06 14:41:23 +02:00
|
|
|
let mut in_feed = followed.into_iter().map(|u| u.id).collect::<Vec<i32>>();
|
|
|
|
in_feed.push(user.id);
|
2018-12-29 09:36:07 +01:00
|
|
|
let articles = Post::user_feed_page(&*conn, in_feed, page.limits())?;
|
|
|
|
Ok(render!(instance::feed(
|
2018-12-06 18:54:16 +01:00
|
|
|
&(&*conn, &intl.catalog, Some(user)),
|
|
|
|
articles,
|
|
|
|
page.0,
|
2018-12-29 09:36:07 +01:00
|
|
|
Page::total(Post::count_local(&*conn)? as i32)
|
|
|
|
)))
|
2018-09-05 16:37:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/federated?<page>")]
|
2018-12-29 09:36:07 +01:00
|
|
|
pub fn federated(conn: DbConn, user: Option<User>, page: Option<Page>, intl: I18n) -> Result<Ructe, ErrorPage> {
|
2018-12-13 22:20:19 +01:00
|
|
|
let page = page.unwrap_or_default();
|
2018-12-29 09:36:07 +01:00
|
|
|
let articles = Post::get_recents_page(&*conn, page.limits())?;
|
|
|
|
Ok(render!(instance::federated(
|
2018-12-06 18:54:16 +01:00
|
|
|
&(&*conn, &intl.catalog, user),
|
|
|
|
articles,
|
|
|
|
page.0,
|
2018-12-29 09:36:07 +01:00
|
|
|
Page::total(Post::count_local(&*conn)? as i32)
|
|
|
|
)))
|
2018-09-05 16:21:50 +02:00
|
|
|
}
|
|
|
|
|
2018-07-27 19:05:36 +02:00
|
|
|
#[get("/admin")]
|
2018-12-29 09:36:07 +01:00
|
|
|
pub fn admin(conn: DbConn, admin: Admin, intl: I18n) -> Result<Ructe, ErrorPage> {
|
|
|
|
let local_inst = Instance::get_local(&*conn)?;
|
|
|
|
Ok(render!(instance::admin(
|
2018-12-06 18:54:16 +01:00
|
|
|
&(&*conn, &intl.catalog, Some(admin.0)),
|
|
|
|
local_inst.clone(),
|
|
|
|
InstanceSettingsForm {
|
|
|
|
name: local_inst.name.clone(),
|
|
|
|
open_registrations: local_inst.open_registrations,
|
|
|
|
short_description: local_inst.short_description,
|
|
|
|
long_description: local_inst.long_description,
|
|
|
|
default_license: local_inst.default_license,
|
|
|
|
},
|
|
|
|
ValidationErrors::default()
|
2018-12-29 09:36:07 +01:00
|
|
|
)))
|
2018-07-27 19:05:36 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 18:54:16 +01:00
|
|
|
#[derive(Clone, FromForm, Validate, Serialize)]
|
|
|
|
pub struct InstanceSettingsForm {
|
2018-07-27 19:05:36 +02:00
|
|
|
#[validate(length(min = "1"))]
|
2018-12-06 18:54:16 +01:00
|
|
|
pub name: String,
|
|
|
|
pub open_registrations: bool,
|
|
|
|
pub short_description: SafeString,
|
|
|
|
pub long_description: SafeString,
|
2018-07-27 19:05:36 +02:00
|
|
|
#[validate(length(min = "1"))]
|
2018-12-06 18:54:16 +01:00
|
|
|
pub default_license: String
|
2018-07-27 19:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[post("/admin", data = "<form>")]
|
2018-12-06 18:54:16 +01:00
|
|
|
pub fn update_settings(conn: DbConn, admin: Admin, form: LenientForm<InstanceSettingsForm>, intl: I18n) -> Result<Redirect, Ructe> {
|
2018-07-27 19:05:36 +02:00
|
|
|
form.validate()
|
2018-12-29 09:36:07 +01:00
|
|
|
.and_then(|_| {
|
|
|
|
let instance = Instance::get_local(&*conn).expect("instance::update_settings: local instance error");
|
2018-07-27 19:05:36 +02:00
|
|
|
instance.update(&*conn,
|
|
|
|
form.name.clone(),
|
|
|
|
form.open_registrations,
|
2018-09-14 18:24:27 +02:00
|
|
|
form.short_description.clone(),
|
2018-12-29 09:36:07 +01:00
|
|
|
form.long_description.clone()).expect("instance::update_settings: save error");
|
|
|
|
Ok(Redirect::to(uri!(admin)))
|
2018-07-27 19:05:36 +02:00
|
|
|
})
|
2018-12-29 09:36:07 +01:00
|
|
|
.or_else(|e| {
|
|
|
|
let local_inst = Instance::get_local(&*conn).expect("instance::update_settings: local instance error");
|
|
|
|
Err(render!(instance::admin(
|
2018-12-06 18:54:16 +01:00
|
|
|
&(&*conn, &intl.catalog, Some(admin.0)),
|
|
|
|
local_inst,
|
|
|
|
form.clone(),
|
|
|
|
e
|
2018-12-29 09:36:07 +01:00
|
|
|
)))
|
2018-12-06 18:54:16 +01:00
|
|
|
})
|
2018-07-27 19:05:36 +02:00
|
|
|
}
|
|
|
|
|
2018-09-08 20:54:09 +02:00
|
|
|
#[get("/admin/instances?<page>")]
|
2018-12-29 09:36:07 +01:00
|
|
|
pub fn admin_instances(admin: Admin, conn: DbConn, page: Option<Page>, intl: I18n) -> Result<Ructe, ErrorPage> {
|
2018-12-13 22:20:19 +01:00
|
|
|
let page = page.unwrap_or_default();
|
2018-12-29 09:36:07 +01:00
|
|
|
let instances = Instance::page(&*conn, page.limits())?;
|
|
|
|
Ok(render!(instance::list(
|
2018-12-06 18:54:16 +01:00
|
|
|
&(&*conn, &intl.catalog, Some(admin.0)),
|
2018-12-29 09:36:07 +01:00
|
|
|
Instance::get_local(&*conn)?,
|
2018-12-06 18:54:16 +01:00
|
|
|
instances,
|
|
|
|
page.0,
|
2018-12-29 09:36:07 +01:00
|
|
|
Page::total(Instance::count(&*conn)? as i32)
|
|
|
|
)))
|
2018-09-08 20:54:09 +02:00
|
|
|
}
|
|
|
|
|
2018-09-19 19:13:07 +02:00
|
|
|
#[post("/admin/instances/<id>/block")]
|
2018-12-29 09:36:07 +01:00
|
|
|
pub fn toggle_block(_admin: Admin, conn: DbConn, id: i32) -> Result<Redirect, ErrorPage> {
|
|
|
|
if let Ok(inst) = Instance::get(&*conn, id) {
|
|
|
|
inst.toggle_block(&*conn)?;
|
2018-09-08 21:07:55 +02:00
|
|
|
}
|
|
|
|
|
2018-12-29 09:36:07 +01:00
|
|
|
Ok(Redirect::to(uri!(admin_instances: page = _)))
|
2018-09-09 12:25:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/admin/users?<page>")]
|
2018-12-29 09:36:07 +01:00
|
|
|
pub fn admin_users(admin: Admin, conn: DbConn, page: Option<Page>, intl: I18n) -> Result<Ructe, ErrorPage> {
|
2018-12-13 22:20:19 +01:00
|
|
|
let page = page.unwrap_or_default();
|
2018-12-29 09:36:07 +01:00
|
|
|
Ok(render!(instance::users(
|
2018-12-06 18:54:16 +01:00
|
|
|
&(&*conn, &intl.catalog, Some(admin.0)),
|
2018-12-29 09:36:07 +01:00
|
|
|
User::get_local_page(&*conn, page.limits())?,
|
2018-12-06 18:54:16 +01:00
|
|
|
page.0,
|
2018-12-29 09:36:07 +01:00
|
|
|
Page::total(User::count_local(&*conn)? as i32)
|
|
|
|
)))
|
2018-09-09 12:25:55 +02:00
|
|
|
}
|
|
|
|
|
2018-09-19 19:13:07 +02:00
|
|
|
#[post("/admin/users/<id>/ban")]
|
2018-12-29 09:36:07 +01:00
|
|
|
pub fn ban(_admin: Admin, conn: DbConn, id: i32, searcher: Searcher) -> Result<Redirect, ErrorPage> {
|
|
|
|
if let Ok(u) = User::get(&*conn, id) {
|
|
|
|
u.delete(&*conn, &searcher)?;
|
2018-11-26 10:21:52 +01:00
|
|
|
}
|
2018-12-29 09:36:07 +01:00
|
|
|
Ok(Redirect::to(uri!(admin_users: page = _)))
|
2018-09-09 12:25:55 +02:00
|
|
|
}
|
|
|
|
|
2018-05-13 19:39:18 +02:00
|
|
|
#[post("/inbox", data = "<data>")]
|
2018-12-22 18:27:21 +01:00
|
|
|
pub fn shared_inbox(conn: DbConn, data: SignedJson<serde_json::Value>, headers: Headers, searcher: Searcher) -> Result<String, status::BadRequest<&'static str>> {
|
|
|
|
let act = data.1.into_inner();
|
2019-01-05 22:30:28 +01:00
|
|
|
let sig = data.0;
|
2018-09-08 23:05:48 +02:00
|
|
|
|
|
|
|
let activity = act.clone();
|
|
|
|
let actor_id = activity["actor"].as_str()
|
2018-10-20 11:04:20 +02:00
|
|
|
.or_else(|| activity["actor"]["id"].as_str()).ok_or(status::BadRequest(Some("Missing actor id for activity")))?;
|
2018-10-03 09:31:38 +02:00
|
|
|
|
2018-11-26 10:21:52 +01:00
|
|
|
let actor = User::from_url(&conn, actor_id).expect("instance::shared_inbox: user error");
|
2019-01-05 22:30:28 +01:00
|
|
|
if !verify_http_headers(&actor, &headers.0, &sig).is_secure() &&
|
2018-10-06 10:06:06 +02:00
|
|
|
!act.clone().verify(&actor) {
|
2019-01-05 22:30:28 +01:00
|
|
|
// maybe we just know an old key?
|
|
|
|
actor.refetch(&conn).and_then(|_| User::get(&conn, actor.id))
|
|
|
|
.and_then(|u| if verify_http_headers(&u, &headers.0, &sig).is_secure() ||
|
|
|
|
act.clone().verify(&u) {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(Error::Signature)
|
|
|
|
})
|
|
|
|
.map_err(|_| {
|
|
|
|
println!("Rejected invalid activity supposedly from {}, with headers {:?}", actor.username, headers.0);
|
|
|
|
status::BadRequest(Some("Invalid signature"))})?;
|
2018-10-03 09:31:38 +02:00
|
|
|
}
|
|
|
|
|
2018-12-29 09:36:07 +01:00
|
|
|
if Instance::is_blocked(&*conn, actor_id).map_err(|_| status::BadRequest(Some("Can't tell if instance is blocked")))? {
|
2018-10-20 11:04:20 +02:00
|
|
|
return Ok(String::new());
|
2018-09-08 23:05:48 +02:00
|
|
|
}
|
2018-10-20 11:04:20 +02:00
|
|
|
let instance = Instance::get_local(&*conn).expect("instance::shared_inbox: local instance not found error");
|
2018-12-02 17:37:51 +01:00
|
|
|
Ok(match instance.received(&*conn, &searcher, act) {
|
2018-06-21 18:00:37 +02:00
|
|
|
Ok(_) => String::new(),
|
|
|
|
Err(e) => {
|
2018-09-08 01:11:27 +02:00
|
|
|
println!("Shared inbox error: {}\n{}", e.as_fail(), e.backtrace());
|
|
|
|
format!("Error: {}", e.as_fail())
|
2018-06-21 18:00:37 +02:00
|
|
|
}
|
2018-10-20 11:04:20 +02:00
|
|
|
})
|
2018-05-13 19:39:18 +02:00
|
|
|
}
|
2018-06-10 21:33:42 +02:00
|
|
|
|
2019-02-17 13:42:59 +01:00
|
|
|
#[get("/nodeinfo/<version>")]
|
|
|
|
pub fn nodeinfo(conn: DbConn, version: String) -> Result<Json<serde_json::Value>, ErrorPage> {
|
2019-03-12 17:00:23 +01:00
|
|
|
if version != "2.0" && version != "2.1" {
|
2019-02-17 13:42:59 +01:00
|
|
|
return Err(ErrorPage::from(Error::NotFound));
|
|
|
|
}
|
|
|
|
|
|
|
|
let local_inst = Instance::get_local(&*conn)?;
|
|
|
|
let mut doc = json!({
|
|
|
|
"version": version,
|
2018-06-10 21:33:42 +02:00
|
|
|
"software": {
|
2019-02-17 13:42:59 +01:00
|
|
|
"name": env!("CARGO_PKG_NAME"),
|
|
|
|
"version": env!("CARGO_PKG_VERSION"),
|
2018-06-10 21:33:42 +02:00
|
|
|
},
|
|
|
|
"protocols": ["activitypub"],
|
|
|
|
"services": {
|
|
|
|
"inbound": [],
|
|
|
|
"outbound": []
|
|
|
|
},
|
2019-02-17 13:42:59 +01:00
|
|
|
"openRegistrations": local_inst.open_registrations,
|
2018-06-10 21:33:42 +02:00
|
|
|
"usage": {
|
|
|
|
"users": {
|
2018-12-29 09:36:07 +01:00
|
|
|
"total": User::count_local(&*conn)?
|
2018-06-10 21:33:42 +02:00
|
|
|
},
|
2018-12-29 09:36:07 +01:00
|
|
|
"localPosts": Post::count_local(&*conn)?,
|
|
|
|
"localComments": Comment::count_local(&*conn)?
|
2018-06-10 21:33:42 +02:00
|
|
|
},
|
2019-02-17 13:42:59 +01:00
|
|
|
"metadata": {
|
|
|
|
"nodeName": local_inst.name,
|
|
|
|
"nodeDescription": local_inst.short_description
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if version == "2.1" {
|
|
|
|
doc["software"]["repository"] = json!(env!("CARGO_PKG_REPOSITORY"));
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Json(doc))
|
2018-06-10 21:33:42 +02:00
|
|
|
}
|
2018-09-01 18:39:40 +02:00
|
|
|
|
|
|
|
#[get("/about")]
|
2018-12-29 09:36:07 +01:00
|
|
|
pub fn about(user: Option<User>, conn: DbConn, intl: I18n) -> Result<Ructe, ErrorPage> {
|
|
|
|
Ok(render!(instance::about(
|
2018-12-06 18:54:16 +01:00
|
|
|
&(&*conn, &intl.catalog, user),
|
2018-12-29 09:36:07 +01:00
|
|
|
Instance::get_local(&*conn)?,
|
|
|
|
Instance::get_local(&*conn)?.main_admin(&*conn)?,
|
|
|
|
User::count_local(&*conn)?,
|
|
|
|
Post::count_local(&*conn)?,
|
|
|
|
Instance::count(&*conn)? - 1
|
|
|
|
)))
|
2018-09-01 18:39:40 +02:00
|
|
|
}
|
2018-09-10 16:08:22 +02:00
|
|
|
|
|
|
|
#[get("/manifest.json")]
|
2018-12-29 09:36:07 +01:00
|
|
|
pub fn web_manifest(conn: DbConn) -> Result<Json<serde_json::Value>, ErrorPage> {
|
|
|
|
let instance = Instance::get_local(&*conn)?;
|
|
|
|
Ok(Json(json!({
|
2018-09-10 16:08:22 +02:00
|
|
|
"name": &instance.name,
|
|
|
|
"description": &instance.short_description,
|
|
|
|
"start_url": String::from("/"),
|
|
|
|
"scope": String::from("/"),
|
|
|
|
"display": String::from("standalone"),
|
|
|
|
"background_color": String::from("#f4f4f4"),
|
2018-10-09 20:38:01 +02:00
|
|
|
"theme_color": String::from("#7765e3"),
|
2019-01-24 13:16:48 +01:00
|
|
|
"categories": [String::from("social")],
|
2018-10-09 20:38:01 +02:00
|
|
|
"icons": [
|
|
|
|
{
|
|
|
|
"src": "/static/icons/trwnh/feather/plumeFeather48.png",
|
|
|
|
"sizes": "48x48",
|
|
|
|
"type": "image/png"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"src": "/static/icons/trwnh/feather/plumeFeather72.png",
|
|
|
|
"sizes": "72x72",
|
|
|
|
"type": "image/png"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"src": "/static/icons/trwnh/feather/plumeFeather96.png",
|
|
|
|
"sizes": "96x96",
|
|
|
|
"type": "image/png"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"src": "/static/icons/trwnh/feather/plumeFeather144.png",
|
|
|
|
"sizes": "144x144",
|
|
|
|
"type": "image/png"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"src": "/static/icons/trwnh/feather/plumeFeather160.png",
|
|
|
|
"sizes": "160x160",
|
|
|
|
"type": "image/png"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"src": "/static/icons/trwnh/feather/plumeFeather192.png",
|
|
|
|
"sizes": "192x192",
|
|
|
|
"type": "image/png"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"src": "/static/icons/trwnh/feather/plumeFeather256.png",
|
|
|
|
"sizes": "256x256",
|
|
|
|
"type": "image/png"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"src": "/static/icons/trwnh/feather/plumeFeather512.png",
|
|
|
|
"sizes": "512x512",
|
|
|
|
"type": "image/png"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"src": "/static/icons/trwnh/feather/plumeFeather.svg"
|
|
|
|
}
|
|
|
|
]
|
2018-12-29 09:36:07 +01:00
|
|
|
})))
|
2018-09-10 16:08:22 +02:00
|
|
|
}
|