2019-10-30 11:22:28 +01:00
|
|
|
use activitypub::{
|
|
|
|
activity::Create,
|
|
|
|
collection::{OrderedCollection, OrderedCollectionPage},
|
|
|
|
};
|
2019-08-21 00:42:04 +02:00
|
|
|
use diesel::SaveChangesDsl;
|
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};
|
2021-01-11 00:38:41 +01:00
|
|
|
use tracing::{info, warn};
|
2018-12-06 18:54:16 +01:00
|
|
|
use validator::{Validate, ValidationError, ValidationErrors};
|
2018-04-22 20:13:12 +02:00
|
|
|
|
2020-01-21 07:02:03 +01:00
|
|
|
use crate::inbox;
|
|
|
|
use crate::routes::{errors::ErrorPage, Page, RemoteForm, RespondOrRedirect};
|
|
|
|
use crate::template_utils::{IntoContext, Ructe};
|
2019-04-17 19:31:47 +02:00
|
|
|
use plume_common::activity_pub::{broadcast, inbox::FromId, ActivityStream, ApRequest, Id};
|
2018-06-23 18:36:11 +02:00
|
|
|
use plume_common::utils;
|
|
|
|
use plume_models::{
|
2019-03-20 17:56:17 +01:00
|
|
|
blogs::Blog,
|
|
|
|
db_conn::DbConn,
|
|
|
|
follows,
|
|
|
|
headers::Headers,
|
2019-04-17 19:31:47 +02:00
|
|
|
inbox::inbox as local_inbox,
|
2019-03-20 17:56:17 +01:00
|
|
|
instance::Instance,
|
2019-08-21 00:42:04 +02:00
|
|
|
medias::Media,
|
2019-03-20 17:56:17 +01:00
|
|
|
posts::{LicensedArticle, Post},
|
|
|
|
reshares::Reshare,
|
2019-08-21 00:42:04 +02:00
|
|
|
safe_string::SafeString,
|
2019-03-20 17:56:17 +01:00
|
|
|
users::*,
|
2019-04-17 19:31:47 +02:00
|
|
|
Error, PlumeRocket,
|
2018-05-19 09:39:59 +02:00
|
|
|
};
|
2018-04-22 20:13:12 +02:00
|
|
|
|
2018-04-23 11:52:44 +02:00
|
|
|
#[get("/me")]
|
2019-06-14 09:33:30 +02:00
|
|
|
pub fn me(user: Option<User>) -> RespondOrRedirect {
|
2018-06-07 10:39:22 +02:00
|
|
|
match user {
|
2019-06-14 09:33:30 +02:00
|
|
|
Some(user) => Redirect::to(uri!(details: name = user.username)).into(),
|
|
|
|
None => utils::requires_login("", uri!(me)).into(),
|
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,
|
2019-03-19 14:37:56 +01:00
|
|
|
rockets: PlumeRocket,
|
2019-04-17 19:31:47 +02:00
|
|
|
fetch_rockets: PlumeRocket,
|
|
|
|
fetch_followers_rockets: PlumeRocket,
|
2018-11-24 12:44:17 +01:00
|
|
|
update_conn: DbConn,
|
2018-12-29 09:36:07 +01:00
|
|
|
) -> Result<Ructe, ErrorPage> {
|
2019-04-17 19:31:47 +02:00
|
|
|
let conn = &*rockets.conn;
|
|
|
|
let user = User::find_by_fqn(&rockets, &name)?;
|
2018-12-29 09:36:07 +01:00
|
|
|
let recents = Post::get_recents_for_author(&*conn, &user, 6)?;
|
|
|
|
let reshares = Reshare::get_recents_for_author(&*conn, &user, 6)?;
|
2019-04-30 12:04:25 +02:00
|
|
|
let worker = &rockets.worker;
|
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();
|
|
|
|
worker.execute(move || {
|
2019-03-20 17:56:17 +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) => {
|
2019-04-17 19:31:47 +02:00
|
|
|
Post::from_activity(&fetch_rockets, article)
|
|
|
|
.expect("Article from remote user couldn't be saved");
|
2021-01-05 13:41:14 +01:00
|
|
|
info!("Fetched article from remote user");
|
2018-12-06 18:54:16 +01:00
|
|
|
}
|
2021-01-05 13:41:14 +01:00
|
|
|
Err(e) => warn!("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 || {
|
2019-03-20 17:56:17 +01:00
|
|
|
for user_id in user_clone
|
|
|
|
.fetch_followers_ids()
|
|
|
|
.expect("Remote user: fetching followers error")
|
|
|
|
{
|
2019-04-17 19:31:47 +02:00
|
|
|
let follower = User::from_id(&fetch_followers_rockets, &user_id, None)
|
2019-03-20 17:56:17 +01:00
|
|
|
.expect("user::details: Couldn't fetch follower");
|
2018-12-06 18:54:16 +01:00
|
|
|
follows::Follow::insert(
|
2019-04-17 19:31:47 +02:00
|
|
|
&*fetch_followers_rockets.conn,
|
2018-12-06 18:54:16 +01:00
|
|
|
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
|
|
|
},
|
2019-03-20 17:56:17 +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 || {
|
2019-03-20 17:56:17 +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(
|
2019-04-30 12:04:25 +02:00
|
|
|
&rockets.to_context(),
|
2018-12-06 18:54:16 +01:00
|
|
|
user.clone(),
|
2019-04-30 12:04:25 +02:00
|
|
|
rockets
|
|
|
|
.user
|
|
|
|
.clone()
|
2019-03-20 17:56:17 +01:00
|
|
|
.and_then(|x| x.is_following(&*conn, user.id).ok())
|
|
|
|
.unwrap_or(false),
|
2019-05-10 22:59:34 +02:00
|
|
|
user.instance_id != Instance::get_local()?.id,
|
2018-12-29 09:36:07 +01:00
|
|
|
user.get_instance(&*conn)?.public_domain,
|
2018-12-06 18:54:16 +01:00
|
|
|
recents,
|
2019-03-20 17:56:17 +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")]
|
2019-04-30 12:04:25 +02:00
|
|
|
pub fn dashboard(user: User, rockets: PlumeRocket) -> Result<Ructe, ErrorPage> {
|
|
|
|
let blogs = Blog::find_for_author(&*rockets.conn, &user)?;
|
2018-12-29 09:36:07 +01:00
|
|
|
Ok(render!(users::dashboard(
|
2019-04-30 12:04:25 +02:00
|
|
|
&rockets.to_context(),
|
2018-12-06 18:54:16 +01:00
|
|
|
blogs,
|
2019-04-30 12:04:25 +02:00
|
|
|
Post::drafts_by_author(&*rockets.conn, &user)?
|
2018-12-29 09:36:07 +01:00
|
|
|
)))
|
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-03-20 17:56:17 +01:00
|
|
|
&i18n!(
|
|
|
|
i18n.catalog,
|
2019-04-01 20:09:29 +02:00
|
|
|
"To access your dashboard, you need to be logged in"
|
2019-03-20 17:56:17 +01:00
|
|
|
),
|
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")]
|
2019-04-30 12:04:25 +02:00
|
|
|
pub fn follow(
|
|
|
|
name: String,
|
|
|
|
user: User,
|
|
|
|
rockets: PlumeRocket,
|
|
|
|
) -> Result<Flash<Redirect>, ErrorPage> {
|
2019-04-17 19:31:47 +02:00
|
|
|
let conn = &*rockets.conn;
|
|
|
|
let target = User::find_by_fqn(&rockets, &name)?;
|
2019-04-30 12:04:25 +02:00
|
|
|
let message = if let Ok(follow) = follows::Follow::find(&*conn, user.id, target.id) {
|
2019-04-17 19:31:47 +02:00
|
|
|
let delete_act = follow.build_undo(&*conn)?;
|
|
|
|
local_inbox(
|
|
|
|
&rockets,
|
|
|
|
serde_json::to_value(&delete_act).map_err(Error::from)?,
|
|
|
|
)?;
|
|
|
|
|
2019-04-30 12:04:25 +02:00
|
|
|
let msg = i18n!(rockets.intl.catalog, "You are no longer following {}."; target.name());
|
2019-04-17 19:31:47 +02:00
|
|
|
rockets
|
|
|
|
.worker
|
|
|
|
.execute(move || broadcast(&user, delete_act, vec![target]));
|
2019-04-30 12:04:25 +02:00
|
|
|
msg
|
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)?;
|
2019-04-30 12:04:25 +02:00
|
|
|
let msg = i18n!(rockets.intl.catalog, "You are now following {}."; target.name());
|
2019-04-17 19:31:47 +02:00
|
|
|
rockets
|
|
|
|
.worker
|
|
|
|
.execute(move || broadcast(&user, act, vec![target]));
|
2019-04-30 12:04:25 +02:00
|
|
|
msg
|
|
|
|
};
|
|
|
|
Ok(Flash::success(
|
|
|
|
Redirect::to(uri!(details: name = name)),
|
|
|
|
message,
|
|
|
|
))
|
2018-05-01 21:57:30 +02:00
|
|
|
}
|
|
|
|
|
2019-04-17 22:09:07 +02:00
|
|
|
#[post("/@/<name>/follow", data = "<remote_form>", rank = 2)]
|
|
|
|
pub fn follow_not_connected(
|
|
|
|
rockets: PlumeRocket,
|
|
|
|
name: String,
|
|
|
|
remote_form: Option<LenientForm<RemoteForm>>,
|
|
|
|
i18n: I18n,
|
2019-06-14 09:33:30 +02:00
|
|
|
) -> Result<RespondOrRedirect, ErrorPage> {
|
2019-04-17 22:09:07 +02:00
|
|
|
let target = User::find_by_fqn(&rockets, &name)?;
|
|
|
|
if let Some(remote_form) = remote_form {
|
2019-05-25 20:23:45 +02:00
|
|
|
if let Some(uri) = User::fetch_remote_interact_uri(&remote_form)
|
2019-04-17 22:09:07 +02:00
|
|
|
.ok()
|
|
|
|
.and_then(|uri| {
|
2020-05-18 20:18:07 +02:00
|
|
|
Some(uri.replace(
|
|
|
|
"{uri}",
|
|
|
|
&format!(
|
2019-04-17 22:09:07 +02:00
|
|
|
"{}@{}",
|
|
|
|
target.fqn,
|
|
|
|
target.get_instance(&rockets.conn).ok()?.public_domain
|
2020-05-18 20:18:07 +02:00
|
|
|
),
|
|
|
|
))
|
2019-04-17 22:09:07 +02:00
|
|
|
})
|
|
|
|
{
|
2019-06-14 09:33:30 +02:00
|
|
|
Ok(Redirect::to(uri).into())
|
2019-04-17 22:09:07 +02:00
|
|
|
} else {
|
|
|
|
let mut err = ValidationErrors::default();
|
|
|
|
err.add("remote",
|
|
|
|
ValidationError {
|
|
|
|
code: Cow::from("invalid_remote"),
|
|
|
|
message: Some(Cow::from(i18n!(&i18n.catalog, "Couldn't obtain enough information about your account. Please make sure your username is correct."))),
|
|
|
|
params: HashMap::new(),
|
|
|
|
},
|
|
|
|
);
|
2019-06-14 09:33:30 +02:00
|
|
|
Ok(Flash::new(
|
2019-04-17 22:09:07 +02:00
|
|
|
render!(users::follow_remote(
|
2019-04-30 12:04:25 +02:00
|
|
|
&rockets.to_context(),
|
2019-04-17 22:09:07 +02:00
|
|
|
target,
|
|
|
|
super::session::LoginForm::default(),
|
|
|
|
ValidationErrors::default(),
|
|
|
|
remote_form.clone(),
|
|
|
|
err
|
|
|
|
)),
|
|
|
|
"callback",
|
|
|
|
uri!(follow: name = name).to_string(),
|
2019-06-14 09:33:30 +02:00
|
|
|
)
|
|
|
|
.into())
|
2019-04-17 22:09:07 +02:00
|
|
|
}
|
|
|
|
} else {
|
2019-06-14 09:33:30 +02:00
|
|
|
Ok(Flash::new(
|
2019-04-17 22:09:07 +02:00
|
|
|
render!(users::follow_remote(
|
2019-04-30 12:04:25 +02:00
|
|
|
&rockets.to_context(),
|
2019-04-17 22:09:07 +02:00
|
|
|
target,
|
|
|
|
super::session::LoginForm::default(),
|
|
|
|
ValidationErrors::default(),
|
|
|
|
#[allow(clippy::map_clone)]
|
|
|
|
remote_form.map(|x| x.clone()).unwrap_or_default(),
|
|
|
|
ValidationErrors::default()
|
|
|
|
)),
|
|
|
|
"callback",
|
|
|
|
uri!(follow: name = name).to_string(),
|
2019-06-14 09:33:30 +02:00
|
|
|
)
|
|
|
|
.into())
|
2019-04-17 22:09:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/@/<name>/follow?local", 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-03-20 17:56:17 +01:00
|
|
|
&i18n!(
|
|
|
|
i18n.catalog,
|
2019-04-01 20:09:29 +02:00
|
|
|
"To subscribe to someone, you need to be logged in"
|
2019-03-20 17:56:17 +01:00
|
|
|
),
|
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)]
|
2019-03-20 17:56:17 +01:00
|
|
|
pub fn followers(
|
|
|
|
name: String,
|
|
|
|
page: Option<Page>,
|
2019-04-17 19:31:47 +02:00
|
|
|
rockets: PlumeRocket,
|
2019-03-20 17:56:17 +01:00
|
|
|
) -> Result<Ructe, ErrorPage> {
|
2019-04-17 19:31:47 +02:00
|
|
|
let conn = &*rockets.conn;
|
2018-12-13 22:20:19 +01:00
|
|
|
let page = page.unwrap_or_default();
|
2019-04-17 19:31:47 +02:00
|
|
|
let user = User::find_by_fqn(&rockets, &name)?;
|
2018-12-29 09:36:07 +01:00
|
|
|
let followers_count = user.count_followers(&*conn)?;
|
2018-12-06 18:54:16 +01:00
|
|
|
|
|
|
|
Ok(render!(users::followers(
|
2019-04-30 12:04:25 +02:00
|
|
|
&rockets.to_context(),
|
2018-12-06 18:54:16 +01:00
|
|
|
user.clone(),
|
2019-04-17 19:31:47 +02:00
|
|
|
rockets
|
|
|
|
.user
|
2019-04-30 12:04:25 +02:00
|
|
|
.clone()
|
2019-03-20 17:56:17 +01:00
|
|
|
.and_then(|x| x.is_following(&*conn, user.id).ok())
|
|
|
|
.unwrap_or(false),
|
2019-05-10 22:59:34 +02:00
|
|
|
user.instance_id != Instance::get_local()?.id,
|
2018-12-29 09:36:07 +01:00
|
|
|
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)]
|
2019-03-20 17:56:17 +01:00
|
|
|
pub fn followed(
|
|
|
|
name: String,
|
|
|
|
page: Option<Page>,
|
2019-04-17 19:31:47 +02:00
|
|
|
rockets: PlumeRocket,
|
2019-03-20 17:56:17 +01:00
|
|
|
) -> Result<Ructe, ErrorPage> {
|
2019-04-17 19:31:47 +02:00
|
|
|
let conn = &*rockets.conn;
|
2019-02-26 13:13:00 +01:00
|
|
|
let page = page.unwrap_or_default();
|
2019-04-17 19:31:47 +02:00
|
|
|
let user = User::find_by_fqn(&rockets, &name)?;
|
2019-04-30 12:04:25 +02:00
|
|
|
let followed_count = user.count_followed(conn)?;
|
2019-02-26 13:13:00 +01:00
|
|
|
|
|
|
|
Ok(render!(users::followed(
|
2019-04-30 12:04:25 +02:00
|
|
|
&rockets.to_context(),
|
2019-02-26 13:13:00 +01:00
|
|
|
user.clone(),
|
2019-04-17 19:31:47 +02:00
|
|
|
rockets
|
|
|
|
.user
|
2019-04-30 12:04:25 +02:00
|
|
|
.clone()
|
|
|
|
.and_then(|x| x.is_following(conn, user.id).ok())
|
2019-03-20 17:56:17 +01:00
|
|
|
.unwrap_or(false),
|
2019-05-10 22:59:34 +02:00
|
|
|
user.instance_id != Instance::get_local()?.id,
|
2019-04-30 12:04:25 +02:00
|
|
|
user.get_instance(conn)?.public_domain,
|
|
|
|
user.get_followed_page(conn, page.limits())?,
|
2019-02-26 13:13:00 +01:00
|
|
|
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,
|
2019-04-17 19:31:47 +02:00
|
|
|
rockets: PlumeRocket,
|
2018-11-24 12:44:17 +01:00
|
|
|
_ap: ApRequest,
|
|
|
|
) -> Option<ActivityStream<CustomPerson>> {
|
2019-04-17 19:31:47 +02:00
|
|
|
let user = User::find_by_fqn(&rockets, &name).ok()?;
|
|
|
|
Some(ActivityStream::new(user.to_activity(&*rockets.conn).ok()?))
|
2018-04-23 17:09:05 +02:00
|
|
|
}
|
|
|
|
|
2018-04-22 20:13:12 +02:00
|
|
|
#[get("/users/new")]
|
2019-04-30 12:04:25 +02:00
|
|
|
pub fn new(rockets: PlumeRocket) -> Result<Ructe, ErrorPage> {
|
2018-12-29 09:36:07 +01:00
|
|
|
Ok(render!(users::new(
|
2019-04-30 12:04:25 +02:00
|
|
|
&rockets.to_context(),
|
2019-05-10 22:59:34 +02:00
|
|
|
Instance::get_local()?.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")]
|
2019-04-30 12:04:25 +02:00
|
|
|
pub fn edit(name: String, user: User, rockets: PlumeRocket) -> 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(
|
2019-04-30 12:04:25 +02:00
|
|
|
&rockets.to_context(),
|
2018-12-06 18:54:16 +01:00
|
|
|
UpdateUserForm {
|
|
|
|
display_name: user.display_name.clone(),
|
|
|
|
email: user.email.clone().unwrap_or_default(),
|
2019-08-21 00:42:04 +02:00
|
|
|
summary: user.summary.clone(),
|
|
|
|
theme: user.preferred_theme,
|
|
|
|
hide_custom_css: user.hide_custom_css,
|
2018-12-06 18:54:16 +01:00
|
|
|
},
|
|
|
|
ValidationErrors::default()
|
|
|
|
)))
|
2018-05-12 17:30:14 +02:00
|
|
|
} else {
|
2020-01-19 12:52:32 +01:00
|
|
|
Err(Error::Unauthorized.into())
|
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-03-20 17:56:17 +01:00
|
|
|
&i18n!(
|
|
|
|
i18n.catalog,
|
2019-04-01 20:09:29 +02:00
|
|
|
"To edit your profile, you need to be logged in"
|
2019-03-20 17:56:17 +01:00
|
|
|
),
|
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,
|
2019-08-21 00:42:04 +02:00
|
|
|
pub theme: Option<String>,
|
|
|
|
pub hide_custom_css: bool,
|
2018-05-12 17:30:14 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 18:54:16 +01:00
|
|
|
#[put("/@/<_name>/edit", data = "<form>")]
|
2019-03-20 17:56:17 +01:00
|
|
|
pub fn update(
|
|
|
|
_name: String,
|
|
|
|
conn: DbConn,
|
2019-08-21 00:42:04 +02:00
|
|
|
mut user: User,
|
2019-03-20 17:56:17 +01:00
|
|
|
form: LenientForm<UpdateUserForm>,
|
2019-04-30 12:04:25 +02:00
|
|
|
intl: I18n,
|
|
|
|
) -> Result<Flash<Redirect>, ErrorPage> {
|
2019-08-21 00:42:04 +02:00
|
|
|
user.display_name = form.display_name.clone();
|
|
|
|
user.email = Some(form.email.clone());
|
|
|
|
user.summary = form.summary.clone();
|
|
|
|
user.summary_html = SafeString::new(
|
|
|
|
&utils::md_to_html(
|
|
|
|
&form.summary,
|
|
|
|
None,
|
|
|
|
false,
|
|
|
|
Some(Media::get_media_processor(&conn, vec![&user])),
|
|
|
|
)
|
|
|
|
.0,
|
|
|
|
);
|
2020-04-12 17:36:00 +02:00
|
|
|
user.preferred_theme = form
|
|
|
|
.theme
|
|
|
|
.clone()
|
|
|
|
.and_then(|t| if &t == "" { None } else { Some(t) });
|
2019-08-21 00:42:04 +02:00
|
|
|
user.hide_custom_css = form.hide_custom_css;
|
|
|
|
let _: User = user.save_changes(&*conn).map_err(Error::from)?;
|
|
|
|
|
2019-04-30 12:04:25 +02:00
|
|
|
Ok(Flash::success(
|
|
|
|
Redirect::to(uri!(me)),
|
2019-05-14 12:54:16 +02:00
|
|
|
i18n!(intl.catalog, "Your profile has been updated."),
|
2019-04-30 12:04:25 +02:00
|
|
|
))
|
2018-05-12 17:30:14 +02:00
|
|
|
}
|
|
|
|
|
2018-09-19 19:13:07 +02:00
|
|
|
#[post("/@/<name>/delete")]
|
2019-03-20 17:56:17 +01:00
|
|
|
pub fn delete(
|
|
|
|
name: String,
|
|
|
|
user: User,
|
2020-01-21 07:02:03 +01:00
|
|
|
mut cookies: Cookies<'_>,
|
2019-04-17 19:31:47 +02:00
|
|
|
rockets: PlumeRocket,
|
2019-04-30 12:04:25 +02:00
|
|
|
) -> Result<Flash<Redirect>, ErrorPage> {
|
2019-04-17 19:31:47 +02:00
|
|
|
let account = User::find_by_fqn(&rockets, &name)?;
|
2018-09-09 21:49:24 +02:00
|
|
|
if user.id == account.id {
|
2019-04-17 19:31:47 +02:00
|
|
|
account.delete(&*rockets.conn, &rockets.searcher)?;
|
2018-09-09 21:49:24 +02:00
|
|
|
|
2019-04-28 19:01:41 +02:00
|
|
|
let target = User::one_by_instance(&*rockets.conn)?;
|
|
|
|
let delete_act = account.delete_activity(&*rockets.conn)?;
|
|
|
|
rockets
|
|
|
|
.worker
|
|
|
|
.execute(move || broadcast(&account, delete_act, target));
|
|
|
|
|
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
|
|
|
|
2019-04-30 12:04:25 +02:00
|
|
|
Ok(Flash::success(
|
|
|
|
Redirect::to(uri!(super::instance::index)),
|
2019-05-14 12:54:16 +02:00
|
|
|
i18n!(rockets.intl.catalog, "Your account has been deleted."),
|
2019-04-30 12:04:25 +02:00
|
|
|
))
|
2018-09-09 21:49:24 +02:00
|
|
|
} else {
|
2019-04-30 12:04:25 +02:00
|
|
|
Ok(Flash::error(
|
|
|
|
Redirect::to(uri!(edit: name = name)),
|
|
|
|
i18n!(
|
|
|
|
rockets.intl.catalog,
|
|
|
|
"You can't delete someone else's account."
|
|
|
|
),
|
|
|
|
))
|
2018-09-09 21:49:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-12 19:40:54 +01:00
|
|
|
#[derive(Default, FromForm, Validate)]
|
2019-03-20 17:56: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-11-24 12:44:17 +01:00
|
|
|
#[validate(
|
2019-03-20 17:56:17 +01:00
|
|
|
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-11-24 12:44:17 +01:00
|
|
|
)
|
|
|
|
)]
|
2019-03-20 17:56:17 +01:00
|
|
|
pub username: String,
|
|
|
|
#[validate(email(message = "Invalid email"))]
|
|
|
|
pub email: String,
|
|
|
|
#[validate(length(min = "8", message = "Password should be at least 8 characters long"))]
|
2018-12-06 18:54:16 +01:00
|
|
|
pub password: String,
|
2019-03-20 17:56: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(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-12 19:41:35 +01:00
|
|
|
fn to_validation(x: Error) -> ValidationErrors {
|
2018-12-29 09:36:07 +01:00
|
|
|
let mut errors = ValidationErrors::new();
|
2020-01-12 19:41:35 +01:00
|
|
|
if let Error::Blocklisted(show, msg) = x {
|
|
|
|
if show {
|
|
|
|
errors.add(
|
|
|
|
"email",
|
|
|
|
ValidationError {
|
|
|
|
code: Cow::from("blocklisted"),
|
|
|
|
message: Some(Cow::from(msg)),
|
|
|
|
params: HashMap::new(),
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-03-20 17:56:17 +01:00
|
|
|
errors.add(
|
|
|
|
"",
|
|
|
|
ValidationError {
|
|
|
|
code: Cow::from("server_error"),
|
|
|
|
message: Some(Cow::from("An unknown error occured")),
|
|
|
|
params: HashMap::new(),
|
|
|
|
},
|
|
|
|
);
|
2018-12-29 09:36:07 +01:00
|
|
|
errors
|
|
|
|
}
|
|
|
|
|
2018-12-06 18:54:16 +01:00
|
|
|
#[post("/users/new", data = "<form>")]
|
2019-04-30 12:04:25 +02:00
|
|
|
pub fn create(
|
|
|
|
form: LenientForm<NewUserForm>,
|
|
|
|
rockets: PlumeRocket,
|
|
|
|
) -> Result<Flash<Redirect>, Ructe> {
|
|
|
|
let conn = &*rockets.conn;
|
2019-05-10 22:59:34 +02:00
|
|
|
if !Instance::get_local()
|
2018-11-24 12:44:17 +01:00
|
|
|
.map(|i| i.open_registrations)
|
|
|
|
.unwrap_or(true)
|
|
|
|
{
|
2019-04-30 12:04:25 +02:00
|
|
|
return Ok(Flash::error(
|
|
|
|
Redirect::to(uri!(new)),
|
|
|
|
i18n!(
|
|
|
|
rockets.intl.catalog,
|
|
|
|
"Registrations are closed on this instance."
|
|
|
|
),
|
|
|
|
)); // Actually, it is an error
|
2018-09-03 18:05:45 +02:00
|
|
|
}
|
|
|
|
|
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(
|
2019-04-30 12:04:25 +02:00
|
|
|
conn,
|
2018-07-06 11:51:19 +02:00
|
|
|
form.username.to_string(),
|
|
|
|
form.username.to_string(),
|
2019-09-13 12:28:36 +02:00
|
|
|
Role::Normal,
|
2018-11-26 10:21:52 +01:00
|
|
|
"",
|
2018-07-06 11:51:19 +02:00
|
|
|
form.email.to_string(),
|
2020-10-04 12:18:22 +02:00
|
|
|
Some(User::hash_pass(&form.password).map_err(to_validation)?),
|
2020-01-12 19:41:35 +01:00
|
|
|
).map_err(to_validation)?;
|
2019-04-30 12:04:25 +02:00
|
|
|
Ok(Flash::success(
|
|
|
|
Redirect::to(uri!(super::session::new: m = _)),
|
|
|
|
i18n!(
|
|
|
|
rockets.intl.catalog,
|
2019-05-14 12:54:16 +02:00
|
|
|
"Your account has been created. Now you just need to log in, before you can use it."
|
2019-04-30 12:04:25 +02:00
|
|
|
),
|
|
|
|
))
|
2018-07-06 11:51:19 +02:00
|
|
|
})
|
2019-03-20 17:56:17 +01:00
|
|
|
.map_err(|err| {
|
2018-12-06 18:54:16 +01:00
|
|
|
render!(users::new(
|
2019-04-30 12:04:25 +02:00
|
|
|
&rockets.to_context(),
|
2019-05-10 22:59:34 +02:00
|
|
|
Instance::get_local()
|
2019-03-20 17:56:17 +01:00
|
|
|
.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")]
|
2019-04-17 19:31:47 +02:00
|
|
|
pub fn outbox(name: String, rockets: PlumeRocket) -> Option<ActivityStream<OrderedCollection>> {
|
|
|
|
let user = User::find_by_fqn(&rockets, &name).ok()?;
|
|
|
|
user.outbox(&*rockets.conn).ok()
|
2018-04-29 20:01:42 +02:00
|
|
|
}
|
2019-10-30 11:22:28 +01:00
|
|
|
#[get("/@/<name>/outbox?<page>")]
|
|
|
|
pub fn outbox_page(
|
|
|
|
name: String,
|
|
|
|
page: Page,
|
|
|
|
rockets: PlumeRocket,
|
|
|
|
) -> Option<ActivityStream<OrderedCollectionPage>> {
|
|
|
|
let user = User::find_by_fqn(&rockets, &name).ok()?;
|
|
|
|
user.outbox_page(&*rockets.conn, page.limits()).ok()
|
|
|
|
}
|
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,
|
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-04-17 19:31:47 +02:00
|
|
|
rockets: PlumeRocket,
|
|
|
|
) -> Result<String, status::BadRequest<&'static str>> {
|
|
|
|
User::find_by_fqn(&rockets, &name).map_err(|_| status::BadRequest(Some("User not found")))?;
|
|
|
|
inbox::handle_incoming(rockets, data, headers)
|
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,
|
2019-04-17 19:31:47 +02:00
|
|
|
rockets: PlumeRocket,
|
2018-11-24 12:44:17 +01:00
|
|
|
_ap: ApRequest,
|
|
|
|
) -> Option<ActivityStream<OrderedCollection>> {
|
2019-04-17 19:31:47 +02:00
|
|
|
let user = User::find_by_fqn(&rockets, &name).ok()?;
|
2018-11-24 12:44:17 +01:00
|
|
|
let followers = user
|
2019-04-17 19:31:47 +02:00
|
|
|
.get_followers(&*rockets.conn)
|
2019-03-20 17:56:17 +01:00
|
|
|
.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
|
2019-03-20 17:56:17 +01:00
|
|
|
.set_id_string(user.followers_endpoint)
|
|
|
|
.ok()?;
|
2018-11-24 12:44:17 +01:00
|
|
|
coll.collection_props
|
2019-03-20 17:56:17 +01:00
|
|
|
.set_total_items_u64(followers.len() as u64)
|
|
|
|
.ok()?;
|
|
|
|
coll.collection_props.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")]
|
2019-04-17 19:31:47 +02:00
|
|
|
pub fn atom_feed(name: String, rockets: PlumeRocket) -> Option<Content<String>> {
|
|
|
|
let conn = &*rockets.conn;
|
|
|
|
let author = User::find_by_fqn(&rockets, &name).ok()?;
|
2020-05-04 15:28:52 +02:00
|
|
|
let entries = Post::get_recents_for_author(conn, &author, 15).ok()?;
|
|
|
|
let uri = Instance::get_local()
|
|
|
|
.ok()?
|
|
|
|
.compute_box("@", &name, "atom.xml");
|
|
|
|
let title = &author.display_name;
|
|
|
|
let default_updated = &author.creation_date;
|
|
|
|
let feed = super::build_atom_feed(entries, &uri, title, default_updated, conn);
|
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
|
|
|
}
|