2018-12-09 18:43:34 +01:00
|
|
|
use activitypub::{activity::Create, collection::OrderedCollection};
|
2018-09-01 22:08:26 +02:00
|
|
|
use atom_syndication::{Entry, FeedBuilder};
|
2018-07-26 17:51:41 +02:00
|
|
|
use rocket::{
|
2018-11-24 12:44:17 +01:00
|
|
|
http::{ContentType, Cookies},
|
2018-07-26 17:51:41 +02:00
|
|
|
request::LenientForm,
|
2018-11-24 12:44:17 +01:00
|
|
|
response::{status, Content, Flash, Redirect},
|
2018-06-07 10:39:22 +02:00
|
|
|
};
|
2018-12-06 18:54:16 +01:00
|
|
|
use rocket_i18n::I18n;
|
2018-05-01 13:48:19 +02:00
|
|
|
use serde_json;
|
2018-12-29 09:36:07 +01:00
|
|
|
use std::{borrow::Cow, collections::HashMap};
|
2018-12-06 18:54:16 +01:00
|
|
|
use validator::{Validate, ValidationError, ValidationErrors};
|
2018-04-22 20:13:12 +02:00
|
|
|
|
2018-12-22 18:27:21 +01:00
|
|
|
use inbox::{Inbox, SignedJson};
|
2018-06-23 18:36:11 +02:00
|
|
|
use plume_common::activity_pub::{
|
2018-11-24 12:44:17 +01:00
|
|
|
broadcast,
|
|
|
|
inbox::{Deletable, FromActivity, Notify},
|
|
|
|
sign::{verify_http_headers, Signable},
|
|
|
|
ActivityStream, ApRequest, Id, IntoId,
|
2018-05-19 09:39:59 +02:00
|
|
|
};
|
2018-06-23 18:36:11 +02:00
|
|
|
use plume_common::utils;
|
|
|
|
use plume_models::{
|
2018-12-29 09:36:07 +01:00
|
|
|
Error,
|
2018-12-09 18:43:34 +01:00
|
|
|
blogs::Blog, db_conn::DbConn, follows, headers::Headers, instance::Instance, posts::{LicensedArticle, Post},
|
2018-11-24 12:44:17 +01:00
|
|
|
reshares::Reshare, users::*,
|
2018-05-19 09:39:59 +02:00
|
|
|
};
|
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-09-03 20:53:20 +02:00
|
|
|
use Worker;
|
2018-12-02 17:37:51 +01:00
|
|
|
use Searcher;
|
2018-04-22 20:13:12 +02:00
|
|
|
|
2018-04-23 11:52:44 +02:00
|
|
|
#[get("/me")]
|
2018-12-06 18:54:16 +01:00
|
|
|
pub fn me(user: Option<User>) -> Result<Redirect, Flash<Redirect>> {
|
2018-06-07 10:39:22 +02:00
|
|
|
match user {
|
2018-06-19 23:20:27 +02:00
|
|
|
Some(user) => Ok(Redirect::to(uri!(details: name = user.username))),
|
2018-11-26 10:21:52 +01:00
|
|
|
None => Err(utils::requires_login("", uri!(me))),
|
2018-06-07 10:39:22 +02:00
|
|
|
}
|
2018-04-23 11:52:44 +02:00
|
|
|
}
|
2018-04-22 20:13:12 +02:00
|
|
|
|
2018-04-23 17:09:05 +02:00
|
|
|
#[get("/@/<name>", rank = 2)]
|
2018-12-06 18:54:16 +01:00
|
|
|
pub fn details(
|
2018-11-24 12:44:17 +01:00
|
|
|
name: String,
|
|
|
|
conn: DbConn,
|
|
|
|
account: Option<User>,
|
|
|
|
worker: Worker,
|
2018-12-02 17:37:51 +01:00
|
|
|
fetch_articles_conn: DbConn,
|
|
|
|
fetch_followers_conn: DbConn,
|
2018-11-24 12:44:17 +01:00
|
|
|
update_conn: DbConn,
|
2018-12-06 18:54:16 +01:00
|
|
|
intl: I18n,
|
2018-12-02 17:37:51 +01:00
|
|
|
searcher: Searcher,
|
2018-12-29 09:36:07 +01:00
|
|
|
) -> Result<Ructe, ErrorPage> {
|
|
|
|
let user = User::find_by_fqn(&*conn, &name)?;
|
|
|
|
let recents = Post::get_recents_for_author(&*conn, &user, 6)?;
|
|
|
|
let reshares = Reshare::get_recents_for_author(&*conn, &user, 6)?;
|
2018-12-06 18:54:16 +01:00
|
|
|
|
2018-12-29 09:36:07 +01:00
|
|
|
if !user.get_instance(&*conn)?.local {
|
2018-12-06 18:54:16 +01:00
|
|
|
// Fetch new articles
|
|
|
|
let user_clone = user.clone();
|
|
|
|
let searcher = searcher.clone();
|
|
|
|
worker.execute(move || {
|
2018-12-29 09:36:07 +01:00
|
|
|
for create_act in user_clone.fetch_outbox::<Create>().expect("Remote user: outbox couldn't be fetched") {
|
2018-12-09 18:43:34 +01:00
|
|
|
match create_act.create_props.object_object::<LicensedArticle>() {
|
2018-12-06 18:54:16 +01:00
|
|
|
Ok(article) => {
|
|
|
|
Post::from_activity(
|
|
|
|
&(&*fetch_articles_conn, &searcher),
|
|
|
|
article,
|
|
|
|
user_clone.clone().into_id(),
|
2018-12-29 09:36:07 +01:00
|
|
|
).expect("Article from remote user couldn't be saved");
|
2018-12-06 18:54:16 +01:00
|
|
|
println!("Fetched article from remote user");
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
println!("Error while fetching articles in background: {:?}", e)
|
2018-11-24 12:44:17 +01:00
|
|
|
}
|
|
|
|
}
|
2018-09-03 20:53:20 +02:00
|
|
|
}
|
2018-12-06 18:54:16 +01:00
|
|
|
});
|
2018-07-26 22:23:53 +02:00
|
|
|
|
2018-12-06 18:54:16 +01:00
|
|
|
// Fetch followers
|
|
|
|
let user_clone = user.clone();
|
|
|
|
worker.execute(move || {
|
2018-12-29 09:36:07 +01:00
|
|
|
for user_id in user_clone.fetch_followers_ids().expect("Remote user: fetching followers error") {
|
2019-03-04 21:35:03 +01:00
|
|
|
let follower = User::from_url(&*fetch_followers_conn, &user_id).expect("user::details: Couldn't fetch follower");
|
2018-12-06 18:54:16 +01:00
|
|
|
follows::Follow::insert(
|
|
|
|
&*fetch_followers_conn,
|
|
|
|
follows::NewFollow {
|
|
|
|
follower_id: follower.id,
|
|
|
|
following_id: user_clone.id,
|
2019-03-04 21:35:03 +01:00
|
|
|
ap_url: String::new(),
|
2018-12-06 18:54:16 +01:00
|
|
|
},
|
2018-12-29 09:36:07 +01:00
|
|
|
).expect("Couldn't save follower for remote user");
|
2018-12-06 18:54:16 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Update profile information if needed
|
|
|
|
let user_clone = user.clone();
|
|
|
|
if user.needs_update() {
|
|
|
|
worker.execute(move || {
|
2018-12-29 09:36:07 +01:00
|
|
|
user_clone.refetch(&*update_conn).expect("Couldn't update user info");
|
2018-12-06 18:54:16 +01:00
|
|
|
});
|
2018-11-24 12:44:17 +01:00
|
|
|
}
|
2018-12-06 18:54:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(render!(users::details(
|
|
|
|
&(&*conn, &intl.catalog, account.clone()),
|
|
|
|
user.clone(),
|
2018-12-29 09:36:07 +01:00
|
|
|
account.and_then(|x| x.is_following(&*conn, user.id).ok()).unwrap_or(false),
|
|
|
|
user.instance_id != Instance::get_local(&*conn)?.id,
|
|
|
|
user.get_instance(&*conn)?.public_domain,
|
2018-12-06 18:54:16 +01:00
|
|
|
recents,
|
2018-12-29 09:36:07 +01:00
|
|
|
reshares.into_iter().filter_map(|r| r.get_post(&*conn).ok()).collect()
|
2018-12-06 18:54:16 +01:00
|
|
|
)))
|
2018-04-22 20:13:12 +02:00
|
|
|
}
|
|
|
|
|
2018-06-10 19:55:08 +02:00
|
|
|
#[get("/dashboard")]
|
2018-12-29 09:36:07 +01:00
|
|
|
pub fn dashboard(user: User, conn: DbConn, intl: I18n) -> Result<Ructe, ErrorPage> {
|
|
|
|
let blogs = Blog::find_for_author(&*conn, &user)?;
|
|
|
|
Ok(render!(users::dashboard(
|
2018-12-06 18:54:16 +01:00
|
|
|
&(&*conn, &intl.catalog, Some(user.clone())),
|
|
|
|
blogs,
|
2018-12-29 09:36:07 +01:00
|
|
|
Post::drafts_by_author(&*conn, &user)?
|
|
|
|
)))
|
2018-06-10 19:55:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/dashboard", rank = 2)]
|
2018-12-06 18:54:16 +01:00
|
|
|
pub fn dashboard_auth(i18n: I18n) -> Flash<Redirect> {
|
2018-09-08 01:11:27 +02:00
|
|
|
utils::requires_login(
|
2019-02-02 15:23:50 +01:00
|
|
|
&i18n!(i18n.catalog, "You need to be logged in order to access your dashboard"),
|
2018-11-26 10:21:52 +01:00
|
|
|
uri!(dashboard),
|
2018-09-08 01:11:27 +02:00
|
|
|
)
|
2018-06-10 19:55:08 +02:00
|
|
|
}
|
|
|
|
|
2018-09-19 19:13:07 +02:00
|
|
|
#[post("/@/<name>/follow")]
|
2018-12-29 09:36:07 +01:00
|
|
|
pub fn follow(name: String, conn: DbConn, user: User, worker: Worker) -> Result<Redirect, ErrorPage> {
|
2018-11-26 10:21:52 +01:00
|
|
|
let target = User::find_by_fqn(&*conn, &name)?;
|
2018-12-29 09:36:07 +01:00
|
|
|
if let Ok(follow) = follows::Follow::find(&*conn, user.id, target.id) {
|
|
|
|
let delete_act = follow.delete(&*conn)?;
|
2018-12-02 17:37:51 +01:00
|
|
|
worker.execute(move || {
|
2018-11-24 12:44:17 +01:00
|
|
|
broadcast(&user, delete_act, vec![target])
|
2018-12-02 17:37:51 +01:00
|
|
|
});
|
2018-09-04 12:37:58 +02:00
|
|
|
} else {
|
2018-11-24 12:44:17 +01:00
|
|
|
let f = follows::Follow::insert(
|
|
|
|
&*conn,
|
|
|
|
follows::NewFollow {
|
|
|
|
follower_id: user.id,
|
|
|
|
following_id: target.id,
|
2019-03-04 21:35:03 +01:00
|
|
|
ap_url: String::new(),
|
2018-11-24 12:44:17 +01:00
|
|
|
},
|
2018-12-29 09:36:07 +01:00
|
|
|
)?;
|
|
|
|
f.notify(&*conn)?;
|
2018-06-20 23:51:47 +02:00
|
|
|
|
2018-12-29 09:36:07 +01:00
|
|
|
let act = f.to_activity(&*conn)?;
|
2018-12-02 17:37:51 +01:00
|
|
|
worker.execute(move || broadcast(&user, act, vec![target]));
|
2018-09-04 12:37:58 +02:00
|
|
|
}
|
2018-12-29 09:36:07 +01:00
|
|
|
Ok(Redirect::to(uri!(details: name = name)))
|
2018-05-01 21:57:30 +02:00
|
|
|
}
|
|
|
|
|
2018-09-19 19:13:07 +02:00
|
|
|
#[post("/@/<name>/follow", rank = 2)]
|
2018-12-06 18:54:16 +01:00
|
|
|
pub fn follow_auth(name: String, i18n: I18n) -> Flash<Redirect> {
|
2018-09-08 01:11:27 +02:00
|
|
|
utils::requires_login(
|
2019-02-26 13:13:00 +01:00
|
|
|
&i18n!(i18n.catalog, "You need to be logged in order to subscribe to someone"),
|
2018-11-26 10:21:52 +01:00
|
|
|
uri!(follow: name = name),
|
2018-09-08 01:11:27 +02:00
|
|
|
)
|
2018-06-07 10:39:22 +02:00
|
|
|
}
|
|
|
|
|
2018-12-23 11:12:15 +01:00
|
|
|
#[get("/@/<name>/followers?<page>", rank = 2)]
|
2018-12-29 09:36:07 +01:00
|
|
|
pub fn followers(name: String, conn: DbConn, account: 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 user = User::find_by_fqn(&*conn, &name)?;
|
|
|
|
let followers_count = user.count_followers(&*conn)?;
|
2018-12-06 18:54:16 +01:00
|
|
|
|
|
|
|
Ok(render!(users::followers(
|
|
|
|
&(&*conn, &intl.catalog, account.clone()),
|
|
|
|
user.clone(),
|
2018-12-29 09:36:07 +01:00
|
|
|
account.and_then(|x| x.is_following(&*conn, user.id).ok()).unwrap_or(false),
|
|
|
|
user.instance_id != Instance::get_local(&*conn)?.id,
|
|
|
|
user.get_instance(&*conn)?.public_domain,
|
|
|
|
user.get_followers_page(&*conn, page.limits())?,
|
2018-12-06 18:54:16 +01:00
|
|
|
page.0,
|
|
|
|
Page::total(followers_count as i32)
|
|
|
|
)))
|
2018-05-13 13:53:58 +02:00
|
|
|
}
|
|
|
|
|
2019-02-26 13:13:00 +01:00
|
|
|
#[get("/@/<name>/followed?<page>", rank = 2)]
|
|
|
|
pub fn followed(name: String, conn: DbConn, account: Option<User>, page: Option<Page>, intl: I18n) -> Result<Ructe, ErrorPage> {
|
|
|
|
let page = page.unwrap_or_default();
|
|
|
|
let user = User::find_by_fqn(&*conn, &name)?;
|
|
|
|
let followed_count = user.count_followed(&*conn)?;
|
|
|
|
|
|
|
|
Ok(render!(users::followed(
|
|
|
|
&(&*conn, &intl.catalog, account.clone()),
|
|
|
|
user.clone(),
|
|
|
|
account.and_then(|x| x.is_following(&*conn, user.id).ok()).unwrap_or(false),
|
|
|
|
user.instance_id != Instance::get_local(&*conn)?.id,
|
|
|
|
user.get_instance(&*conn)?.public_domain,
|
|
|
|
user.get_followed_page(&*conn, page.limits())?,
|
|
|
|
page.0,
|
|
|
|
Page::total(followed_count as i32)
|
|
|
|
)))
|
|
|
|
}
|
|
|
|
|
2018-07-11 17:30:01 +02:00
|
|
|
#[get("/@/<name>", rank = 1)]
|
2018-12-06 18:54:16 +01:00
|
|
|
pub fn activity_details(
|
2018-11-24 12:44:17 +01:00
|
|
|
name: String,
|
|
|
|
conn: DbConn,
|
|
|
|
_ap: ApRequest,
|
|
|
|
) -> Option<ActivityStream<CustomPerson>> {
|
2018-12-29 09:36:07 +01:00
|
|
|
let user = User::find_local(&*conn, &name).ok()?;
|
|
|
|
Some(ActivityStream::new(user.to_activity(&*conn).ok()?))
|
2018-04-23 17:09:05 +02:00
|
|
|
}
|
|
|
|
|
2018-04-22 20:13:12 +02:00
|
|
|
#[get("/users/new")]
|
2018-12-29 09:36:07 +01:00
|
|
|
pub fn new(user: Option<User>, conn: DbConn, intl: I18n) -> Result<Ructe, ErrorPage> {
|
|
|
|
Ok(render!(users::new(
|
2018-12-06 18:54:16 +01:00
|
|
|
&(&*conn, &intl.catalog, user),
|
2018-12-29 09:36:07 +01:00
|
|
|
Instance::get_local(&*conn)?.open_registrations,
|
2018-12-06 18:54:16 +01:00
|
|
|
&NewUserForm::default(),
|
|
|
|
ValidationErrors::default()
|
2018-12-29 09:36:07 +01:00
|
|
|
)))
|
2018-04-22 20:13:12 +02:00
|
|
|
}
|
|
|
|
|
2018-05-12 17:30:14 +02:00
|
|
|
#[get("/@/<name>/edit")]
|
2018-12-29 09:36:07 +01:00
|
|
|
pub fn edit(name: String, user: User, conn: DbConn, intl: I18n) -> Result<Ructe, ErrorPage> {
|
2018-11-26 10:21:52 +01:00
|
|
|
if user.username == name && !name.contains('@') {
|
2018-12-29 09:36:07 +01:00
|
|
|
Ok(render!(users::edit(
|
2018-12-06 18:54:16 +01:00
|
|
|
&(&*conn, &intl.catalog, Some(user.clone())),
|
|
|
|
UpdateUserForm {
|
|
|
|
display_name: user.display_name.clone(),
|
|
|
|
email: user.email.clone().unwrap_or_default(),
|
|
|
|
summary: user.summary.to_string(),
|
|
|
|
},
|
|
|
|
ValidationErrors::default()
|
|
|
|
)))
|
2018-05-12 17:30:14 +02:00
|
|
|
} else {
|
2018-12-29 09:36:07 +01:00
|
|
|
Err(Error::Unauthorized)?
|
2018-05-12 17:30:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-07 10:39:22 +02:00
|
|
|
#[get("/@/<name>/edit", rank = 2)]
|
2018-12-06 18:54:16 +01:00
|
|
|
pub fn edit_auth(name: String, i18n: I18n) -> Flash<Redirect> {
|
2018-09-08 01:11:27 +02:00
|
|
|
utils::requires_login(
|
2019-02-02 15:23:50 +01:00
|
|
|
&i18n!(i18n.catalog, "You need to be logged in order to edit your profile"),
|
2018-11-26 10:21:52 +01:00
|
|
|
uri!(edit: name = name),
|
2018-09-08 01:11:27 +02:00
|
|
|
)
|
2018-06-07 10:39:22 +02:00
|
|
|
}
|
|
|
|
|
2018-05-12 17:30:14 +02:00
|
|
|
#[derive(FromForm)]
|
2018-12-06 18:54:16 +01:00
|
|
|
pub struct UpdateUserForm {
|
|
|
|
pub display_name: String,
|
|
|
|
pub email: String,
|
|
|
|
pub summary: String,
|
2018-05-12 17:30:14 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 18:54:16 +01:00
|
|
|
#[put("/@/<_name>/edit", data = "<form>")]
|
2018-12-29 09:36:07 +01:00
|
|
|
pub fn update(_name: String, conn: DbConn, user: User, form: LenientForm<UpdateUserForm>) -> Result<Redirect, ErrorPage> {
|
2018-11-24 12:44:17 +01:00
|
|
|
user.update(
|
|
|
|
&*conn,
|
2018-12-06 18:54:16 +01:00
|
|
|
if !form.display_name.is_empty() { form.display_name.clone() } else { user.display_name.clone() },
|
|
|
|
if !form.email.is_empty() { form.email.clone() } else { user.email.clone().unwrap_or_default() },
|
|
|
|
if !form.summary.is_empty() { form.summary.clone() } else { user.summary.to_string() },
|
2018-12-29 09:36:07 +01:00
|
|
|
)?;
|
|
|
|
Ok(Redirect::to(uri!(me)))
|
2018-05-12 17:30:14 +02:00
|
|
|
}
|
|
|
|
|
2018-09-19 19:13:07 +02:00
|
|
|
#[post("/@/<name>/delete")]
|
2018-12-29 09:36:07 +01:00
|
|
|
pub fn delete(name: String, conn: DbConn, user: User, mut cookies: Cookies, searcher: Searcher) -> Result<Redirect, ErrorPage> {
|
2018-11-26 10:21:52 +01:00
|
|
|
let account = User::find_by_fqn(&*conn, &name)?;
|
2018-09-09 21:49:24 +02:00
|
|
|
if user.id == account.id {
|
2018-12-29 09:36:07 +01:00
|
|
|
account.delete(&*conn, &searcher)?;
|
2018-09-09 21:49:24 +02:00
|
|
|
|
2018-12-29 09:36:07 +01:00
|
|
|
if let Some(cookie) = cookies.get_private(AUTH_COOKIE) {
|
|
|
|
cookies.remove_private(cookie);
|
|
|
|
}
|
2018-09-09 21:49:24 +02:00
|
|
|
|
2018-12-29 09:36:07 +01:00
|
|
|
Ok(Redirect::to(uri!(super::instance::index)))
|
2018-09-09 21:49:24 +02:00
|
|
|
} else {
|
2018-12-29 09:36:07 +01:00
|
|
|
Ok(Redirect::to(uri!(edit: name = name)))
|
2018-09-09 21:49:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 18:54:16 +01:00
|
|
|
#[derive(Default, FromForm, Serialize, Validate)]
|
2018-11-24 12:44:17 +01:00
|
|
|
#[validate(
|
|
|
|
schema(
|
|
|
|
function = "passwords_match",
|
|
|
|
skip_on_field_errors = "false",
|
|
|
|
message = "Passwords are not matching"
|
|
|
|
)
|
|
|
|
)]
|
2018-12-06 18:54:16 +01:00
|
|
|
pub struct NewUserForm {
|
2018-12-02 12:43:03 +01:00
|
|
|
#[validate(length(min = "1", message = "Username can't be empty"),
|
|
|
|
custom( function = "validate_username", message = "User name is not allowed to contain any of < > & @ ' or \""))]
|
2018-12-06 18:54:16 +01:00
|
|
|
pub username: String,
|
2018-07-06 19:29:36 +02:00
|
|
|
#[validate(email(message = "Invalid email"))]
|
2018-12-06 18:54:16 +01:00
|
|
|
pub email: String,
|
2018-11-24 12:44:17 +01:00
|
|
|
#[validate(
|
|
|
|
length(
|
|
|
|
min = "8",
|
|
|
|
message = "Password should be at least 8 characters long"
|
|
|
|
)
|
|
|
|
)]
|
2018-12-06 18:54:16 +01:00
|
|
|
pub password: String,
|
2018-11-24 12:44:17 +01:00
|
|
|
#[validate(
|
|
|
|
length(
|
|
|
|
min = "8",
|
|
|
|
message = "Password should be at least 8 characters long"
|
|
|
|
)
|
|
|
|
)]
|
2018-12-06 18:54:16 +01:00
|
|
|
pub password_confirmation: String,
|
2018-04-22 20:13:12 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 18:54:16 +01:00
|
|
|
pub fn passwords_match(form: &NewUserForm) -> Result<(), ValidationError> {
|
2018-06-29 14:56:00 +02:00
|
|
|
if form.password != form.password_confirmation {
|
|
|
|
Err(ValidationError::new("password_match"))
|
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 18:54:16 +01:00
|
|
|
pub fn validate_username(username: &str) -> Result<(), ValidationError> {
|
2018-12-25 18:00:21 +01:00
|
|
|
if username.contains(&['<', '>', '&', '@', '\'', '"', ' ', '\n', '\t'][..]) {
|
2018-12-02 12:43:03 +01:00
|
|
|
Err(ValidationError::new("username_illegal_char"))
|
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-29 09:36:07 +01:00
|
|
|
fn to_validation(_: Error) -> ValidationErrors {
|
|
|
|
let mut errors = ValidationErrors::new();
|
|
|
|
errors.add("", ValidationError {
|
|
|
|
code: Cow::from("server_error"),
|
|
|
|
message: Some(Cow::from("An unknown error occured")),
|
|
|
|
params: HashMap::new()
|
|
|
|
});
|
|
|
|
errors
|
|
|
|
}
|
|
|
|
|
2018-12-06 18:54:16 +01:00
|
|
|
#[post("/users/new", data = "<form>")]
|
|
|
|
pub fn create(conn: DbConn, form: LenientForm<NewUserForm>, intl: I18n) -> Result<Redirect, Ructe> {
|
|
|
|
if !Instance::get_local(&*conn)
|
2018-11-24 12:44:17 +01:00
|
|
|
.map(|i| i.open_registrations)
|
|
|
|
.unwrap_or(true)
|
|
|
|
{
|
2018-09-03 18:05:45 +02:00
|
|
|
return Ok(Redirect::to(uri!(new))); // Actually, it is an error
|
|
|
|
}
|
|
|
|
|
2018-12-25 18:00:21 +01:00
|
|
|
let mut form = form.into_inner();
|
|
|
|
form.username = form.username.trim().to_owned();
|
|
|
|
form.email = form.email.trim().to_owned();
|
2018-07-06 11:51:19 +02:00
|
|
|
form.validate()
|
2018-12-29 09:36:07 +01:00
|
|
|
.and_then(|_| {
|
2018-11-24 12:44:17 +01:00
|
|
|
NewUser::new_local(
|
2018-07-06 11:51:19 +02:00
|
|
|
&*conn,
|
|
|
|
form.username.to_string(),
|
|
|
|
form.username.to_string(),
|
|
|
|
false,
|
2018-11-26 10:21:52 +01:00
|
|
|
"",
|
2018-07-06 11:51:19 +02:00
|
|
|
form.email.to_string(),
|
2018-12-29 09:36:07 +01:00
|
|
|
User::hash_pass(&form.password).map_err(to_validation)?,
|
2019-03-04 21:35:03 +01:00
|
|
|
).map_err(to_validation)?;
|
2018-12-29 09:36:07 +01:00
|
|
|
Ok(Redirect::to(uri!(super::session::new: m = _)))
|
2018-07-06 11:51:19 +02:00
|
|
|
})
|
2018-12-07 21:00:12 +01:00
|
|
|
.map_err(|err| {
|
2018-12-06 18:54:16 +01:00
|
|
|
render!(users::new(
|
|
|
|
&(&*conn, &intl.catalog, None),
|
|
|
|
Instance::get_local(&*conn).map(|i| i.open_registrations).unwrap_or(true),
|
2018-12-25 18:00:21 +01:00
|
|
|
&form,
|
2018-12-06 18:54:16 +01:00
|
|
|
err
|
|
|
|
))
|
2018-11-24 12:44:17 +01:00
|
|
|
})
|
2018-04-22 20:13:12 +02:00
|
|
|
}
|
2018-04-29 20:01:42 +02:00
|
|
|
|
|
|
|
#[get("/@/<name>/outbox")]
|
2018-12-06 18:54:16 +01:00
|
|
|
pub fn outbox(name: String, conn: DbConn) -> Option<ActivityStream<OrderedCollection>> {
|
2018-12-29 09:36:07 +01:00
|
|
|
let user = User::find_local(&*conn, &name).ok()?;
|
|
|
|
user.outbox(&*conn).ok()
|
2018-04-29 20:01:42 +02:00
|
|
|
}
|
2018-05-01 16:00:29 +02:00
|
|
|
|
|
|
|
#[post("/@/<name>/inbox", data = "<data>")]
|
2018-12-06 18:54:16 +01:00
|
|
|
pub fn inbox(
|
2018-11-24 12:44:17 +01:00
|
|
|
name: String,
|
|
|
|
conn: DbConn,
|
2018-12-22 18:27:21 +01:00
|
|
|
data: SignedJson<serde_json::Value>,
|
2018-11-24 12:44:17 +01:00
|
|
|
headers: Headers,
|
2018-12-02 17:37:51 +01:00
|
|
|
searcher: Searcher,
|
2018-11-24 12:44:17 +01:00
|
|
|
) -> Result<String, Option<status::BadRequest<&'static str>>> {
|
2018-12-29 09:36:07 +01:00
|
|
|
let user = User::find_local(&*conn, &name).map_err(|_| None)?;
|
2018-12-22 18:27:21 +01:00
|
|
|
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();
|
2018-11-24 12:44:17 +01:00
|
|
|
let actor_id = activity["actor"]
|
|
|
|
.as_str()
|
|
|
|
.or_else(|| activity["actor"]["id"].as_str())
|
|
|
|
.ok_or(Some(status::BadRequest(Some(
|
|
|
|
"Missing actor id for activity",
|
|
|
|
))))?;
|
2018-10-03 20:48:25 +02:00
|
|
|
|
2018-11-26 10:21:52 +01:00
|
|
|
let actor = User::from_url(&conn, actor_id).expect("user::inbox: user error");
|
2019-01-05 22:30:28 +01:00
|
|
|
if !verify_http_headers(&actor, &headers.0, &sig).is_secure()
|
2018-11-24 12:44:17 +01: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(|actor| if verify_http_headers(&actor, &headers.0, &sig).is_secure()
|
|
|
|
|| act.clone().verify(&actor)
|
|
|
|
{
|
|
|
|
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 20:48:25 +02:00
|
|
|
}
|
|
|
|
|
2018-12-29 09:36:07 +01:00
|
|
|
if Instance::is_blocked(&*conn, actor_id).map_err(|_| None)? {
|
2018-10-20 11:04:20 +02:00
|
|
|
return Ok(String::new());
|
2018-09-08 23:05:48 +02:00
|
|
|
}
|
2018-12-02 17:37:51 +01:00
|
|
|
Ok(match user.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!("User 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-01 16:00:29 +02:00
|
|
|
}
|
2018-05-04 15:13:55 +02:00
|
|
|
|
2018-12-23 11:12:15 +01:00
|
|
|
#[get("/@/<name>/followers", rank = 1)]
|
2018-12-06 18:54:16 +01:00
|
|
|
pub fn ap_followers(
|
2018-11-24 12:44:17 +01:00
|
|
|
name: String,
|
|
|
|
conn: DbConn,
|
|
|
|
_ap: ApRequest,
|
|
|
|
) -> Option<ActivityStream<OrderedCollection>> {
|
2018-12-29 09:36:07 +01:00
|
|
|
let user = User::find_local(&*conn, &name).ok()?;
|
2018-11-24 12:44:17 +01:00
|
|
|
let followers = user
|
2018-12-29 09:36:07 +01:00
|
|
|
.get_followers(&*conn).ok()?
|
2018-11-24 12:44:17 +01:00
|
|
|
.into_iter()
|
|
|
|
.map(|f| Id::new(f.ap_url))
|
|
|
|
.collect::<Vec<Id>>();
|
2018-06-21 19:09:18 +02:00
|
|
|
|
|
|
|
let mut coll = OrderedCollection::default();
|
2018-11-24 12:44:17 +01:00
|
|
|
coll.object_props
|
2018-12-29 09:36:07 +01:00
|
|
|
.set_id_string(user.followers_endpoint).ok()?;
|
2018-11-24 12:44:17 +01:00
|
|
|
coll.collection_props
|
2018-12-29 09:36:07 +01:00
|
|
|
.set_total_items_u64(followers.len() as u64).ok()?;
|
2018-11-24 12:44:17 +01:00
|
|
|
coll.collection_props
|
2018-12-29 09:36:07 +01:00
|
|
|
.set_items_link_vec(followers).ok()?;
|
2018-10-20 11:04:20 +02:00
|
|
|
Some(ActivityStream::new(coll))
|
2018-05-04 15:13:55 +02:00
|
|
|
}
|
2018-09-01 22:08:26 +02:00
|
|
|
|
|
|
|
#[get("/@/<name>/atom.xml")]
|
2018-12-06 18:54:16 +01:00
|
|
|
pub fn atom_feed(name: String, conn: DbConn) -> Option<Content<String>> {
|
2018-12-29 09:36:07 +01:00
|
|
|
let author = User::find_by_fqn(&*conn, &name).ok()?;
|
2018-09-01 22:08:26 +02:00
|
|
|
let feed = FeedBuilder::default()
|
|
|
|
.title(author.display_name.clone())
|
2018-11-24 12:44:17 +01:00
|
|
|
.id(Instance::get_local(&*conn)
|
|
|
|
.unwrap()
|
2018-11-26 10:21:52 +01:00
|
|
|
.compute_box("~", &name, "atom.xml"))
|
2018-11-24 12:44:17 +01:00
|
|
|
.entries(
|
2018-12-29 09:36:07 +01:00
|
|
|
Post::get_recents_for_author(&*conn, &author, 15).ok()?
|
2018-11-24 12:44:17 +01:00
|
|
|
.into_iter()
|
|
|
|
.map(|p| super::post_to_atom(p, &*conn))
|
|
|
|
.collect::<Vec<Entry>>(),
|
|
|
|
)
|
2018-09-01 22:08:26 +02:00
|
|
|
.build()
|
2018-10-20 11:04:20 +02:00
|
|
|
.expect("user::atom_feed: Error building Atom feed");
|
2018-11-24 12:44:17 +01:00
|
|
|
Some(Content(
|
|
|
|
ContentType::new("application", "atom+xml"),
|
|
|
|
feed.to_string(),
|
|
|
|
))
|
2018-09-01 22:08:26 +02:00
|
|
|
}
|