2019-03-20 17:56:17 +01:00
|
|
|
use rocket::{
|
2020-01-12 19:41:35 +01:00
|
|
|
request::{Form, FormItems, FromForm, LenientForm},
|
2019-04-30 12:04:25 +02:00
|
|
|
response::{status, Flash, Redirect},
|
2019-03-20 17:56:17 +01:00
|
|
|
};
|
2018-12-06 18:54:16 +01:00
|
|
|
use rocket_contrib::json::Json;
|
|
|
|
use rocket_i18n::I18n;
|
2019-09-13 12:28:36 +02:00
|
|
|
use scheduled_thread_pool::ScheduledThreadPool;
|
|
|
|
use std::str::FromStr;
|
2018-12-06 18:54:16 +01:00
|
|
|
use validator::{Validate, ValidationErrors};
|
2018-04-22 15:35:37 +02:00
|
|
|
|
2020-01-21 07:02:03 +01:00
|
|
|
use crate::inbox;
|
|
|
|
use crate::routes::{errors::ErrorPage, rocket_uri_macro_static_files, Page, RespondOrRedirect};
|
|
|
|
use crate::template_utils::{IntoContext, Ructe};
|
2019-04-28 19:01:41 +02:00
|
|
|
use plume_common::activity_pub::{broadcast, inbox::FromId};
|
2018-06-23 18:36:11 +02:00
|
|
|
use plume_models::{
|
2019-09-13 12:28:36 +02:00
|
|
|
admin::*,
|
2020-01-12 19:41:35 +01:00
|
|
|
blocklisted_emails::*,
|
2019-09-13 12:28:36 +02:00
|
|
|
comments::Comment,
|
|
|
|
db_conn::DbConn,
|
|
|
|
headers::Headers,
|
|
|
|
instance::*,
|
|
|
|
posts::Post,
|
|
|
|
safe_string::SafeString,
|
2019-10-07 19:08:20 +02:00
|
|
|
timeline::Timeline,
|
2019-09-13 12:28:36 +02:00
|
|
|
users::{Role, User},
|
|
|
|
Connection, Error, PlumeRocket, CONFIG,
|
2018-05-19 09:39:59 +02:00
|
|
|
};
|
2018-04-22 15:35:37 +02:00
|
|
|
|
2018-09-05 19:03:02 +02:00
|
|
|
#[get("/")]
|
2021-01-30 13:44:29 +01:00
|
|
|
pub fn index(conn: DbConn, rockets: PlumeRocket) -> Result<Ructe, ErrorPage> {
|
2019-05-10 22:59:34 +02:00
|
|
|
let inst = Instance::get_local()?;
|
2019-10-07 19:08:20 +02:00
|
|
|
let timelines = Timeline::list_all_for_user(&conn, rockets.user.clone().map(|u| u.id))?
|
|
|
|
.into_iter()
|
|
|
|
.filter_map(|t| {
|
|
|
|
if let Ok(latest) = t.get_latest(&conn, 12) {
|
|
|
|
Some((t, latest))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect();
|
2018-05-12 14:56:38 +02:00
|
|
|
|
2018-12-29 09:36:07 +01:00
|
|
|
Ok(render!(instance::index(
|
2021-01-30 13:44:29 +01:00
|
|
|
&(&conn, &rockets).to_context(),
|
2018-12-29 09:36:07 +01:00
|
|
|
inst,
|
2021-01-30 13:44:29 +01:00
|
|
|
User::count_local(&conn)?,
|
|
|
|
Post::count_local(&conn)?,
|
2019-10-07 19:08:20 +02:00
|
|
|
timelines
|
2018-12-29 09:36:07 +01:00
|
|
|
)))
|
2018-09-05 16:21:50 +02:00
|
|
|
}
|
|
|
|
|
2018-07-27 19:05:36 +02:00
|
|
|
#[get("/admin")]
|
2021-01-30 13:44:29 +01:00
|
|
|
pub fn admin(_admin: Admin, conn: DbConn, rockets: PlumeRocket) -> Result<Ructe, ErrorPage> {
|
2019-05-10 22:59:34 +02:00
|
|
|
let local_inst = Instance::get_local()?;
|
2018-12-29 09:36:07 +01:00
|
|
|
Ok(render!(instance::admin(
|
2021-01-30 13:44:29 +01:00
|
|
|
&(&conn, &rockets).to_context(),
|
2018-12-06 18:54:16 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-09-13 12:28:36 +02:00
|
|
|
#[get("/admin", rank = 2)]
|
2021-01-30 13:44:29 +01:00
|
|
|
pub fn admin_mod(_mod: Moderator, conn: DbConn, rockets: PlumeRocket) -> Ructe {
|
|
|
|
render!(instance::admin_mod(&(&conn, &rockets).to_context()))
|
2019-09-13 12:28:36 +02:00
|
|
|
}
|
|
|
|
|
2019-03-12 19:40:54 +01:00
|
|
|
#[derive(Clone, FromForm, Validate)]
|
2018-12-06 18:54:16 +01:00
|
|
|
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"))]
|
2019-03-20 17:56:17 +01:00
|
|
|
pub default_license: String,
|
2018-07-27 19:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[post("/admin", data = "<form>")]
|
2019-03-20 17:56:17 +01:00
|
|
|
pub fn update_settings(
|
2019-04-30 12:04:25 +02:00
|
|
|
_admin: Admin,
|
2019-03-20 17:56:17 +01:00
|
|
|
form: LenientForm<InstanceSettingsForm>,
|
2021-01-30 13:44:29 +01:00
|
|
|
conn: DbConn,
|
2019-04-30 12:04:25 +02:00
|
|
|
rockets: PlumeRocket,
|
2019-06-14 09:33:30 +02:00
|
|
|
) -> RespondOrRedirect {
|
|
|
|
if let Err(e) = form.validate() {
|
|
|
|
let local_inst =
|
|
|
|
Instance::get_local().expect("instance::update_settings: local instance error");
|
|
|
|
render!(instance::admin(
|
2021-01-30 13:44:29 +01:00
|
|
|
&(&conn, &rockets).to_context(),
|
2019-06-14 09:33:30 +02:00
|
|
|
local_inst,
|
|
|
|
form.clone(),
|
|
|
|
e
|
|
|
|
))
|
|
|
|
.into()
|
|
|
|
} else {
|
|
|
|
let instance =
|
|
|
|
Instance::get_local().expect("instance::update_settings: local instance error");
|
|
|
|
instance
|
|
|
|
.update(
|
2021-01-30 13:44:29 +01:00
|
|
|
&*conn,
|
2019-06-14 09:33:30 +02:00
|
|
|
form.name.clone(),
|
|
|
|
form.open_registrations,
|
|
|
|
form.short_description.clone(),
|
|
|
|
form.long_description.clone(),
|
2019-08-27 21:47:45 +02:00
|
|
|
form.default_license.clone(),
|
2019-06-14 09:33:30 +02:00
|
|
|
)
|
|
|
|
.expect("instance::update_settings: save error");
|
|
|
|
Flash::success(
|
|
|
|
Redirect::to(uri!(admin)),
|
|
|
|
i18n!(rockets.intl.catalog, "Instance settings have been saved."),
|
|
|
|
)
|
|
|
|
.into()
|
|
|
|
}
|
2018-07-27 19:05:36 +02:00
|
|
|
}
|
|
|
|
|
2018-09-08 20:54:09 +02:00
|
|
|
#[get("/admin/instances?<page>")]
|
2019-03-20 17:56:17 +01:00
|
|
|
pub fn admin_instances(
|
2019-09-13 12:28:36 +02:00
|
|
|
_mod: Moderator,
|
2019-03-20 17:56:17 +01:00
|
|
|
page: Option<Page>,
|
2021-01-30 13:44:29 +01:00
|
|
|
conn: DbConn,
|
2019-04-30 12:04:25 +02:00
|
|
|
rockets: PlumeRocket,
|
2019-03-20 17:56:17 +01:00
|
|
|
) -> Result<Ructe, ErrorPage> {
|
2018-12-13 22:20:19 +01:00
|
|
|
let page = page.unwrap_or_default();
|
2021-01-30 13:44:29 +01:00
|
|
|
let instances = Instance::page(&conn, page.limits())?;
|
2018-12-29 09:36:07 +01:00
|
|
|
Ok(render!(instance::list(
|
2021-01-30 13:44:29 +01:00
|
|
|
&(&conn, &rockets).to_context(),
|
2019-05-10 22:59:34 +02:00
|
|
|
Instance::get_local()?,
|
2018-12-06 18:54:16 +01:00
|
|
|
instances,
|
|
|
|
page.0,
|
2021-01-30 13:44:29 +01:00
|
|
|
Page::total(Instance::count(&conn)? as i32)
|
2018-12-29 09:36:07 +01:00
|
|
|
)))
|
2018-09-08 20:54:09 +02:00
|
|
|
}
|
|
|
|
|
2018-09-19 19:13:07 +02:00
|
|
|
#[post("/admin/instances/<id>/block")]
|
2019-04-30 12:04:25 +02:00
|
|
|
pub fn toggle_block(
|
2019-09-13 12:28:36 +02:00
|
|
|
_mod: Moderator,
|
2019-04-30 12:04:25 +02:00
|
|
|
conn: DbConn,
|
|
|
|
id: i32,
|
|
|
|
intl: I18n,
|
|
|
|
) -> Result<Flash<Redirect>, ErrorPage> {
|
2021-01-30 13:44:29 +01:00
|
|
|
let inst = Instance::get(&conn, id)?;
|
2019-04-30 12:04:25 +02:00
|
|
|
let message = if inst.blocked {
|
2019-05-29 13:26:37 +02:00
|
|
|
i18n!(intl.catalog, "{} has been unblocked."; &inst.name)
|
2019-04-30 12:04:25 +02:00
|
|
|
} else {
|
2019-05-29 13:26:37 +02:00
|
|
|
i18n!(intl.catalog, "{} has been blocked."; &inst.name)
|
2019-04-30 12:04:25 +02:00
|
|
|
};
|
2018-09-08 21:07:55 +02:00
|
|
|
|
2021-01-30 13:44:29 +01:00
|
|
|
inst.toggle_block(&conn)?;
|
2019-04-30 12:04:25 +02:00
|
|
|
Ok(Flash::success(
|
|
|
|
Redirect::to(uri!(admin_instances: page = _)),
|
|
|
|
message,
|
|
|
|
))
|
2018-09-09 12:25:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/admin/users?<page>")]
|
2019-03-20 17:56:17 +01:00
|
|
|
pub fn admin_users(
|
2019-09-13 12:28:36 +02:00
|
|
|
_mod: Moderator,
|
2019-03-20 17:56:17 +01:00
|
|
|
page: Option<Page>,
|
2021-01-30 13:44:29 +01:00
|
|
|
conn: DbConn,
|
2019-04-30 12:04:25 +02:00
|
|
|
rockets: PlumeRocket,
|
2019-03-20 17:56:17 +01:00
|
|
|
) -> 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(
|
2021-01-30 13:44:29 +01:00
|
|
|
&(&conn, &rockets).to_context(),
|
|
|
|
User::get_local_page(&conn, page.limits())?,
|
2018-12-06 18:54:16 +01:00
|
|
|
page.0,
|
2021-01-30 13:44:29 +01:00
|
|
|
Page::total(User::count_local(&conn)? as i32)
|
2018-12-29 09:36:07 +01:00
|
|
|
)))
|
2018-09-09 12:25:55 +02:00
|
|
|
}
|
2020-01-12 19:41:35 +01:00
|
|
|
pub struct BlocklistEmailDeletion {
|
|
|
|
ids: Vec<i32>,
|
|
|
|
}
|
|
|
|
impl<'f> FromForm<'f> for BlocklistEmailDeletion {
|
|
|
|
type Error = ();
|
|
|
|
fn from_form(items: &mut FormItems<'f>, _strict: bool) -> Result<BlocklistEmailDeletion, ()> {
|
|
|
|
let mut c: BlocklistEmailDeletion = BlocklistEmailDeletion { ids: Vec::new() };
|
|
|
|
for item in items {
|
|
|
|
let key = item.key.parse::<i32>();
|
|
|
|
if let Ok(i) = key {
|
|
|
|
c.ids.push(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#[post("/admin/emails/delete", data = "<form>")]
|
|
|
|
pub fn delete_email_blocklist(
|
|
|
|
_mod: Moderator,
|
|
|
|
form: Form<BlocklistEmailDeletion>,
|
2021-01-30 13:44:29 +01:00
|
|
|
conn: DbConn,
|
2020-01-12 19:41:35 +01:00
|
|
|
rockets: PlumeRocket,
|
|
|
|
) -> Result<Flash<Redirect>, ErrorPage> {
|
2021-01-30 13:44:29 +01:00
|
|
|
BlocklistedEmail::delete_entries(&conn, form.0.ids)?;
|
2020-01-12 19:41:35 +01:00
|
|
|
Ok(Flash::success(
|
|
|
|
Redirect::to(uri!(admin_email_blocklist: page = None)),
|
|
|
|
i18n!(rockets.intl.catalog, "Blocks deleted"),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[post("/admin/emails/new", data = "<form>")]
|
|
|
|
pub fn add_email_blocklist(
|
|
|
|
_mod: Moderator,
|
|
|
|
form: LenientForm<NewBlocklistedEmail>,
|
2021-01-30 13:44:29 +01:00
|
|
|
conn: DbConn,
|
2020-01-12 19:41:35 +01:00
|
|
|
rockets: PlumeRocket,
|
|
|
|
) -> Result<Flash<Redirect>, ErrorPage> {
|
2021-01-30 13:44:29 +01:00
|
|
|
let result = BlocklistedEmail::insert(&conn, form.0);
|
2020-10-06 05:17:30 +02:00
|
|
|
|
|
|
|
if let Err(Error::Db(_)) = result {
|
|
|
|
Ok(Flash::error(
|
|
|
|
Redirect::to(uri!(admin_email_blocklist: page = None)),
|
2020-12-01 00:38:58 +01:00
|
|
|
i18n!(rockets.intl.catalog, "Email already blocked"),
|
2020-10-06 05:17:30 +02:00
|
|
|
))
|
|
|
|
} else {
|
|
|
|
Ok(Flash::success(
|
|
|
|
Redirect::to(uri!(admin_email_blocklist: page = None)),
|
|
|
|
i18n!(rockets.intl.catalog, "Email Blocked"),
|
|
|
|
))
|
|
|
|
}
|
2020-01-12 19:41:35 +01:00
|
|
|
}
|
|
|
|
#[get("/admin/emails?<page>")]
|
|
|
|
pub fn admin_email_blocklist(
|
|
|
|
_mod: Moderator,
|
|
|
|
page: Option<Page>,
|
2021-01-30 13:44:29 +01:00
|
|
|
conn: DbConn,
|
2020-01-12 19:41:35 +01:00
|
|
|
rockets: PlumeRocket,
|
|
|
|
) -> Result<Ructe, ErrorPage> {
|
|
|
|
let page = page.unwrap_or_default();
|
|
|
|
Ok(render!(instance::emailblocklist(
|
2021-01-30 13:44:29 +01:00
|
|
|
&(&conn, &rockets).to_context(),
|
|
|
|
BlocklistedEmail::page(&conn, page.limits())?,
|
2020-01-12 19:41:35 +01:00
|
|
|
page.0,
|
2021-01-30 13:44:29 +01:00
|
|
|
Page::total(BlocklistedEmail::count(&conn)? as i32)
|
2020-01-12 19:41:35 +01:00
|
|
|
)))
|
|
|
|
}
|
2018-09-09 12:25:55 +02:00
|
|
|
|
2019-09-13 12:28:36 +02:00
|
|
|
/// A structure to handle forms that are a list of items on which actions are applied.
|
|
|
|
///
|
|
|
|
/// This is for instance the case of the user list in the administration.
|
|
|
|
pub struct MultiAction<T>
|
|
|
|
where
|
|
|
|
T: FromStr,
|
|
|
|
{
|
|
|
|
ids: Vec<i32>,
|
|
|
|
action: T,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'f, T> FromForm<'f> for MultiAction<T>
|
|
|
|
where
|
|
|
|
T: FromStr,
|
|
|
|
{
|
|
|
|
type Error = ();
|
|
|
|
|
2020-01-21 07:02:03 +01:00
|
|
|
fn from_form(items: &mut FormItems<'_>, _strict: bool) -> Result<Self, Self::Error> {
|
2019-09-13 12:28:36 +02:00
|
|
|
let (ids, act) = items.fold((vec![], None), |(mut ids, act), item| {
|
|
|
|
let (name, val) = item.key_value_decoded();
|
|
|
|
|
|
|
|
if name == "action" {
|
|
|
|
(ids, T::from_str(&val).ok())
|
|
|
|
} else if let Ok(id) = name.parse::<i32>() {
|
|
|
|
ids.push(id);
|
|
|
|
(ids, act)
|
|
|
|
} else {
|
|
|
|
(ids, act)
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if let Some(act) = act {
|
|
|
|
Ok(MultiAction { ids, action: act })
|
|
|
|
} else {
|
|
|
|
Err(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub enum UserActions {
|
|
|
|
Admin,
|
|
|
|
RevokeAdmin,
|
|
|
|
Moderator,
|
|
|
|
RevokeModerator,
|
|
|
|
Ban,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromStr for UserActions {
|
|
|
|
type Err = ();
|
|
|
|
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
|
|
match s {
|
|
|
|
"admin" => Ok(UserActions::Admin),
|
|
|
|
"un-admin" => Ok(UserActions::RevokeAdmin),
|
|
|
|
"moderator" => Ok(UserActions::Moderator),
|
|
|
|
"un-moderator" => Ok(UserActions::RevokeModerator),
|
|
|
|
"ban" => Ok(UserActions::Ban),
|
|
|
|
_ => Err(()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[post("/admin/users/edit", data = "<form>")]
|
|
|
|
pub fn edit_users(
|
|
|
|
moderator: Moderator,
|
|
|
|
form: LenientForm<MultiAction<UserActions>>,
|
2021-01-30 13:44:29 +01:00
|
|
|
conn: DbConn,
|
2019-09-13 12:28:36 +02:00
|
|
|
rockets: PlumeRocket,
|
|
|
|
) -> Result<Flash<Redirect>, ErrorPage> {
|
|
|
|
// you can't change your own rights
|
|
|
|
if form.ids.contains(&moderator.0.id) {
|
|
|
|
return Ok(Flash::error(
|
|
|
|
Redirect::to(uri!(admin_users: page = _)),
|
|
|
|
i18n!(rockets.intl.catalog, "You can't change your own rights."),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
// moderators can't grant or revoke admin rights
|
|
|
|
if !moderator.0.is_admin() {
|
|
|
|
match form.action {
|
|
|
|
UserActions::Admin | UserActions::RevokeAdmin => {
|
|
|
|
return Ok(Flash::error(
|
|
|
|
Redirect::to(uri!(admin_users: page = _)),
|
|
|
|
i18n!(
|
|
|
|
rockets.intl.catalog,
|
|
|
|
"You are not allowed to take this action."
|
|
|
|
),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let worker = &*rockets.worker;
|
|
|
|
match form.action {
|
|
|
|
UserActions::Admin => {
|
|
|
|
for u in form.ids.clone() {
|
2021-01-30 13:44:29 +01:00
|
|
|
User::get(&conn, u)?.set_role(&conn, Role::Admin)?;
|
2019-09-13 12:28:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
UserActions::Moderator => {
|
|
|
|
for u in form.ids.clone() {
|
2021-01-30 13:44:29 +01:00
|
|
|
User::get(&conn, u)?.set_role(&conn, Role::Moderator)?;
|
2019-09-13 12:28:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
UserActions::RevokeAdmin | UserActions::RevokeModerator => {
|
|
|
|
for u in form.ids.clone() {
|
2021-01-30 13:44:29 +01:00
|
|
|
User::get(&conn, u)?.set_role(&conn, Role::Normal)?;
|
2019-09-13 12:28:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
UserActions::Ban => {
|
|
|
|
for u in form.ids.clone() {
|
2021-01-30 13:44:29 +01:00
|
|
|
ban(u, &conn, worker)?;
|
2019-09-13 12:28:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Flash::success(
|
|
|
|
Redirect::to(uri!(admin_users: page = _)),
|
|
|
|
i18n!(rockets.intl.catalog, "Done."),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2021-01-07 14:51:46 +01:00
|
|
|
fn ban(id: i32, conn: &Connection, worker: &ScheduledThreadPool) -> Result<(), ErrorPage> {
|
2019-09-13 12:28:36 +02:00
|
|
|
let u = User::get(&*conn, id)?;
|
2021-01-07 14:51:46 +01:00
|
|
|
u.delete(&*conn)?;
|
2019-05-10 22:59:34 +02:00
|
|
|
if Instance::get_local()
|
2019-04-30 12:04:25 +02:00
|
|
|
.map(|i| u.instance_id == i.id)
|
|
|
|
.unwrap_or(false)
|
|
|
|
{
|
2020-01-12 19:41:35 +01:00
|
|
|
BlocklistedEmail::insert(
|
2021-11-27 23:53:13 +01:00
|
|
|
conn,
|
2020-01-12 19:41:35 +01:00
|
|
|
NewBlocklistedEmail {
|
|
|
|
email_address: u.email.clone().unwrap(),
|
|
|
|
note: "Banned".to_string(),
|
|
|
|
notify_user: false,
|
|
|
|
notification_text: "".to_owned(),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.unwrap();
|
2019-09-13 12:28:36 +02:00
|
|
|
let target = User::one_by_instance(&*conn)?;
|
|
|
|
let delete_act = u.delete_activity(&*conn)?;
|
2021-01-11 21:27:52 +01:00
|
|
|
worker.execute(move || broadcast(&u, delete_act, target, CONFIG.proxy().cloned()));
|
2018-11-26 10:21:52 +01:00
|
|
|
}
|
2019-04-30 12:04:25 +02:00
|
|
|
|
2019-09-13 12:28:36 +02:00
|
|
|
Ok(())
|
2018-09-09 12:25:55 +02:00
|
|
|
}
|
|
|
|
|
2018-05-13 19:39:18 +02:00
|
|
|
#[post("/inbox", data = "<data>")]
|
2019-03-20 17:56:17 +01:00
|
|
|
pub fn shared_inbox(
|
2021-01-30 13:44:29 +01:00
|
|
|
conn: DbConn,
|
2019-04-17 19:31:47 +02:00
|
|
|
data: inbox::SignedJson<serde_json::Value>,
|
2020-01-21 07:02:03 +01:00
|
|
|
headers: Headers<'_>,
|
2019-03-20 17:56:17 +01:00
|
|
|
) -> Result<String, status::BadRequest<&'static str>> {
|
2021-01-30 13:44:29 +01:00
|
|
|
inbox::handle_incoming(conn, data, headers)
|
2018-05-13 19:39:18 +02:00
|
|
|
}
|
2018-06-10 21:33:42 +02:00
|
|
|
|
2019-04-17 22:09:07 +02:00
|
|
|
#[get("/remote_interact?<target>")]
|
2021-01-30 13:44:29 +01:00
|
|
|
pub fn interact(conn: DbConn, user: Option<User>, target: String) -> Option<Redirect> {
|
|
|
|
if User::find_by_fqn(&conn, &target).is_ok() {
|
2019-04-17 22:09:07 +02:00
|
|
|
return Some(Redirect::to(uri!(super::user::details: name = target)));
|
|
|
|
}
|
|
|
|
|
2021-01-30 13:44:29 +01:00
|
|
|
if let Ok(post) = Post::from_id(&conn, &target, None, CONFIG.proxy()) {
|
2021-01-15 17:13:45 +01:00
|
|
|
return Some(Redirect::to(uri!(
|
2021-01-30 13:44:29 +01:00
|
|
|
super::posts::details: blog = post.get_blog(&conn).expect("Can't retrieve blog").fqn,
|
2021-01-15 17:13:45 +01:00
|
|
|
slug = &post.slug,
|
|
|
|
responding_to = _
|
|
|
|
)));
|
2019-04-17 22:09:07 +02:00
|
|
|
}
|
|
|
|
|
2021-01-30 13:44:29 +01:00
|
|
|
if let Ok(comment) = Comment::from_id(&conn, &target, None, CONFIG.proxy()) {
|
|
|
|
if comment.can_see(&conn, user.as_ref()) {
|
|
|
|
let post = comment.get_post(&conn).expect("Can't retrieve post");
|
2019-04-17 22:09:07 +02:00
|
|
|
return Some(Redirect::to(uri!(
|
2021-01-30 13:44:29 +01:00
|
|
|
super::posts::details: blog =
|
|
|
|
post.get_blog(&conn).expect("Can't retrieve blog").fqn,
|
2019-04-17 22:09:07 +02:00
|
|
|
slug = &post.slug,
|
|
|
|
responding_to = comment.id
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2019-05-10 22:59:34 +02:00
|
|
|
let local_inst = Instance::get_local()?;
|
2019-02-17 13:42:59 +01:00
|
|
|
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": {
|
2021-01-30 13:44:29 +01:00
|
|
|
"total": User::count_local(&conn)?
|
2018-06-10 21:33:42 +02:00
|
|
|
},
|
2021-01-30 13:44:29 +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")]
|
2021-01-30 13:44:29 +01:00
|
|
|
pub fn about(conn: DbConn, rockets: PlumeRocket) -> Result<Ructe, ErrorPage> {
|
2018-12-29 09:36:07 +01:00
|
|
|
Ok(render!(instance::about(
|
2021-01-30 13:44:29 +01:00
|
|
|
&(&conn, &rockets).to_context(),
|
2019-05-10 22:59:34 +02:00
|
|
|
Instance::get_local()?,
|
2021-01-30 13:44:29 +01:00
|
|
|
Instance::get_local()?.main_admin(&conn)?,
|
|
|
|
User::count_local(&conn)?,
|
|
|
|
Post::count_local(&conn)?,
|
|
|
|
Instance::count(&conn)? - 1
|
2018-12-29 09:36:07 +01:00
|
|
|
)))
|
2018-09-01 18:39:40 +02:00
|
|
|
}
|
2018-09-10 16:08:22 +02:00
|
|
|
|
2019-05-18 14:09:51 +02:00
|
|
|
#[get("/privacy")]
|
2021-01-30 13:44:29 +01:00
|
|
|
pub fn privacy(conn: DbConn, rockets: PlumeRocket) -> Ructe {
|
|
|
|
render!(instance::privacy(&(&conn, &rockets).to_context()))
|
2019-05-18 14:09:51 +02:00
|
|
|
}
|
|
|
|
|
2018-09-10 16:08:22 +02:00
|
|
|
#[get("/manifest.json")]
|
2019-05-10 22:59:34 +02:00
|
|
|
pub fn web_manifest() -> Result<Json<serde_json::Value>, ErrorPage> {
|
|
|
|
let instance = Instance::get_local()?;
|
2018-12-29 09:36:07 +01:00
|
|
|
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")],
|
2019-03-21 11:51:41 +01:00
|
|
|
"icons": CONFIG.logo.other.iter()
|
|
|
|
.map(|i| i.with_prefix(&uri!(static_files: file = "").to_string()))
|
|
|
|
.collect::<Vec<_>>()
|
2018-12-29 09:36:07 +01:00
|
|
|
})))
|
2018-09-10 16:08:22 +02:00
|
|
|
}
|