2018-06-10 13:13:07 +02:00
|
|
|
use activitypub::{
|
2018-07-26 22:23:53 +02:00
|
|
|
activity::{Create, Follow},
|
|
|
|
collection::OrderedCollection,
|
|
|
|
object::Article
|
2018-05-16 20:20:44 +02:00
|
|
|
};
|
2018-09-01 22:08:26 +02:00
|
|
|
use atom_syndication::{Entry, FeedBuilder};
|
2018-07-26 17:51:41 +02:00
|
|
|
use rocket::{
|
|
|
|
request::LenientForm,
|
2018-09-01 22:08:26 +02:00
|
|
|
response::{Redirect, Flash, Content},
|
|
|
|
http::ContentType
|
2018-06-07 10:39:22 +02:00
|
|
|
};
|
2018-04-24 14:31:02 +02:00
|
|
|
use rocket_contrib::Template;
|
2018-05-01 13:48:19 +02:00
|
|
|
use serde_json;
|
2018-06-29 14:56:00 +02:00
|
|
|
use validator::{Validate, ValidationError};
|
2018-09-03 20:53:20 +02:00
|
|
|
use workerpool::thunk::*;
|
2018-04-22 20:13:12 +02:00
|
|
|
|
2018-06-23 18:36:11 +02:00
|
|
|
use plume_common::activity_pub::{
|
2018-07-11 17:30:01 +02:00
|
|
|
ActivityStream, broadcast, Id, IntoId, ApRequest,
|
2018-07-26 22:23:53 +02:00
|
|
|
inbox::{FromActivity, Notify}
|
2018-05-19 09:39:59 +02:00
|
|
|
};
|
2018-06-23 18:36:11 +02:00
|
|
|
use plume_common::utils;
|
|
|
|
use plume_models::{
|
2018-06-10 19:55:08 +02:00
|
|
|
blogs::Blog,
|
2018-06-23 18:36:11 +02:00
|
|
|
db_conn::DbConn,
|
2018-05-19 09:39:59 +02:00
|
|
|
follows,
|
|
|
|
instance::Instance,
|
|
|
|
posts::Post,
|
2018-05-24 11:45:36 +02:00
|
|
|
reshares::Reshare,
|
2018-05-19 09:39:59 +02:00
|
|
|
users::*
|
|
|
|
};
|
2018-06-23 18:36:11 +02:00
|
|
|
use inbox::Inbox;
|
2018-07-25 15:50:29 +02:00
|
|
|
use routes::Page;
|
2018-09-03 20:53:20 +02:00
|
|
|
use Worker;
|
2018-04-22 20:13:12 +02:00
|
|
|
|
2018-04-23 11:52:44 +02:00
|
|
|
#[get("/me")]
|
2018-06-18 19:28:28 +02:00
|
|
|
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))),
|
|
|
|
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-09-03 20:53:20 +02:00
|
|
|
fn details(name: String, conn: DbConn, account: Option<User>, worker: Worker, fecth_articles_conn: DbConn, fecth_followers_conn: DbConn, update_conn: DbConn) -> Template {
|
2018-09-03 15:59:02 +02:00
|
|
|
may_fail!(account.map(|a| a.to_json(&*conn)), User::find_by_fqn(&*conn, name), "Couldn't find requested user", |user| {
|
2018-06-18 19:28:28 +02:00
|
|
|
let recents = Post::get_recents_for_author(&*conn, &user, 6);
|
|
|
|
let reshares = Reshare::get_recents_for_author(&*conn, &user, 6);
|
|
|
|
let user_id = user.id.clone();
|
|
|
|
let n_followers = user.get_followers(&*conn).len();
|
2018-05-12 18:55:25 +02:00
|
|
|
|
2018-07-26 22:59:41 +02:00
|
|
|
if !user.get_instance(&*conn).local {
|
2018-07-27 12:53:21 +02:00
|
|
|
// Fetch new articles
|
2018-07-26 22:59:41 +02:00
|
|
|
let user_clone = user.clone();
|
|
|
|
worker.execute(Thunk::of(move || {
|
|
|
|
for create_act in user_clone.fetch_outbox::<Create>() {
|
|
|
|
match create_act.create_props.object_object::<Article>() {
|
|
|
|
Ok(article) => {
|
2018-07-27 12:53:21 +02:00
|
|
|
Post::from_activity(&*fecth_articles_conn, article, user_clone.clone().into_id());
|
2018-07-26 22:59:41 +02:00
|
|
|
println!("Fetched article from remote user");
|
|
|
|
}
|
|
|
|
Err(e) => println!("Error while fetching articles in background: {:?}", e)
|
2018-07-26 22:23:53 +02:00
|
|
|
}
|
|
|
|
}
|
2018-07-26 22:59:41 +02:00
|
|
|
}));
|
2018-07-27 12:53:21 +02:00
|
|
|
|
|
|
|
// Fetch followers
|
|
|
|
let user_clone = user.clone();
|
|
|
|
worker.execute(Thunk::of(move || {
|
|
|
|
for user_id in user_clone.fetch_followers_ids() {
|
|
|
|
let follower = User::find_by_ap_url(&*fecth_followers_conn, user_id.clone())
|
|
|
|
.unwrap_or_else(|| User::fetch_from_url(&*fecth_followers_conn, user_id).expect("Couldn't fetch follower"));
|
|
|
|
follows::Follow::insert(&*fecth_followers_conn, follows::NewFollow {
|
|
|
|
follower_id: follower.id,
|
|
|
|
following_id: user_clone.id
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}));
|
2018-09-03 20:53:20 +02:00
|
|
|
|
|
|
|
// Update profile information if needed
|
|
|
|
let user_clone = user.clone();
|
|
|
|
if user.needs_update() {
|
|
|
|
worker.execute(Thunk::of(move || {
|
|
|
|
user_clone.refetch(&*update_conn);
|
|
|
|
}))
|
|
|
|
}
|
2018-07-26 22:59:41 +02:00
|
|
|
}
|
2018-07-26 22:23:53 +02:00
|
|
|
|
2018-06-18 19:28:28 +02:00
|
|
|
Template::render("users/details", json!({
|
2018-07-20 17:59:16 +02:00
|
|
|
"user": user.to_json(&*conn),
|
2018-06-18 19:28:28 +02:00
|
|
|
"instance_url": user.get_instance(&*conn).public_domain,
|
|
|
|
"is_remote": user.instance_id != Instance::local_id(&*conn),
|
|
|
|
"follows": account.clone().map(|x| x.is_following(&*conn, user.id)).unwrap_or(false),
|
2018-09-03 15:59:02 +02:00
|
|
|
"account": account.clone().map(|a| a.to_json(&*conn)),
|
2018-06-18 19:28:28 +02:00
|
|
|
"recents": recents.into_iter().map(|p| p.to_json(&*conn)).collect::<Vec<serde_json::Value>>(),
|
|
|
|
"reshares": reshares.into_iter().map(|r| r.get_post(&*conn).unwrap().to_json(&*conn)).collect::<Vec<serde_json::Value>>(),
|
|
|
|
"is_self": account.map(|a| a.id == user_id).unwrap_or(false),
|
|
|
|
"n_followers": n_followers
|
|
|
|
}))
|
|
|
|
})
|
2018-04-22 20:13:12 +02:00
|
|
|
}
|
|
|
|
|
2018-06-10 19:55:08 +02:00
|
|
|
#[get("/dashboard")]
|
|
|
|
fn dashboard(user: User, conn: DbConn) -> Template {
|
|
|
|
let blogs = Blog::find_for_author(&*conn, user.id);
|
|
|
|
Template::render("users/dashboard", json!({
|
2018-09-03 15:59:02 +02:00
|
|
|
"account": user.to_json(&*conn),
|
2018-06-10 19:55:08 +02:00
|
|
|
"blogs": blogs
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/dashboard", rank = 2)]
|
|
|
|
fn dashboard_auth() -> Flash<Redirect> {
|
2018-06-19 23:20:27 +02:00
|
|
|
utils::requires_login("You need to be logged in order to access your dashboard", uri!(dashboard))
|
2018-06-10 19:55:08 +02:00
|
|
|
}
|
|
|
|
|
2018-05-01 21:57:30 +02:00
|
|
|
#[get("/@/<name>/follow")]
|
2018-09-03 20:53:20 +02:00
|
|
|
fn follow(name: String, conn: DbConn, user: User, worker: Worker) -> Redirect {
|
2018-05-01 21:57:30 +02:00
|
|
|
let target = User::find_by_fqn(&*conn, name.clone()).unwrap();
|
2018-09-04 12:37:58 +02:00
|
|
|
if let Some(follow) = follows::Follow::find(&*conn, user.id, target.id) {
|
|
|
|
let delete_act = follow.delete(&*conn);
|
|
|
|
worker.execute(Thunk::of(move || broadcast(&user, delete_act, vec![target])));
|
|
|
|
} else {
|
|
|
|
let f = follows::Follow::insert(&*conn, follows::NewFollow {
|
|
|
|
follower_id: user.id,
|
|
|
|
following_id: target.id,
|
|
|
|
ap_url: format!("{}/follow/{}", user.ap_url, target.ap_url),
|
|
|
|
});
|
|
|
|
f.notify(&*conn);
|
2018-06-20 23:51:47 +02:00
|
|
|
|
2018-09-04 12:37:58 +02:00
|
|
|
let act = f.into_activity(&*conn);
|
|
|
|
worker.execute(Thunk::of(move || broadcast(&user, act, vec![target])));
|
|
|
|
}
|
2018-06-19 23:20:27 +02:00
|
|
|
Redirect::to(uri!(details: name = name))
|
2018-05-01 21:57:30 +02:00
|
|
|
}
|
|
|
|
|
2018-06-07 10:39:22 +02:00
|
|
|
#[get("/@/<name>/follow", rank = 2)]
|
|
|
|
fn follow_auth(name: String) -> Flash<Redirect> {
|
2018-06-19 23:20:27 +02:00
|
|
|
utils::requires_login("You need to be logged in order to follow someone", uri!(follow: name = name))
|
2018-06-07 10:39:22 +02:00
|
|
|
}
|
|
|
|
|
2018-07-25 15:50:29 +02:00
|
|
|
#[get("/@/<name>/followers?<page>")]
|
|
|
|
fn followers_paginated(name: String, conn: DbConn, account: Option<User>, page: Page) -> Template {
|
2018-09-03 15:59:02 +02:00
|
|
|
may_fail!(account.map(|a| a.to_json(&*conn)), User::find_by_fqn(&*conn, name.clone()), "Couldn't find requested user", |user| {
|
2018-06-18 19:28:28 +02:00
|
|
|
let user_id = user.id.clone();
|
2018-07-25 15:50:29 +02:00
|
|
|
let followers_count = user.get_followers(&*conn).len();
|
2018-06-15 15:08:38 +02:00
|
|
|
|
2018-06-18 19:28:28 +02:00
|
|
|
Template::render("users/followers", json!({
|
2018-07-20 17:59:16 +02:00
|
|
|
"user": user.to_json(&*conn),
|
2018-06-18 19:28:28 +02:00
|
|
|
"instance_url": user.get_instance(&*conn).public_domain,
|
|
|
|
"is_remote": user.instance_id != Instance::local_id(&*conn),
|
|
|
|
"follows": account.clone().map(|x| x.is_following(&*conn, user.id)).unwrap_or(false),
|
2018-07-25 15:50:29 +02:00
|
|
|
"followers": user.get_followers_page(&*conn, page.limits()).into_iter().map(|f| f.to_json(&*conn)).collect::<Vec<serde_json::Value>>(),
|
2018-09-03 15:59:02 +02:00
|
|
|
"account": account.clone().map(|a| a.to_json(&*conn)),
|
2018-06-18 19:28:28 +02:00
|
|
|
"is_self": account.map(|a| a.id == user_id).unwrap_or(false),
|
2018-07-25 15:50:29 +02:00
|
|
|
"n_followers": followers_count,
|
|
|
|
"page": page.page,
|
|
|
|
"n_pages": Page::total(followers_count as i32)
|
2018-06-18 19:28:28 +02:00
|
|
|
}))
|
|
|
|
})
|
2018-05-13 13:53:58 +02:00
|
|
|
}
|
|
|
|
|
2018-07-25 15:50:29 +02:00
|
|
|
#[get("/@/<name>/followers", rank = 2)]
|
|
|
|
fn followers(name: String, conn: DbConn, account: Option<User>) -> Template {
|
|
|
|
followers_paginated(name, conn, account, Page::first())
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-11 17:30:01 +02:00
|
|
|
#[get("/@/<name>", rank = 1)]
|
|
|
|
fn activity_details(name: String, conn: DbConn, _ap: ApRequest) -> ActivityStream<CustomPerson> {
|
2018-05-01 13:48:19 +02:00
|
|
|
let user = User::find_local(&*conn, name).unwrap();
|
2018-06-21 19:09:18 +02:00
|
|
|
ActivityStream::new(user.into_activity(&*conn))
|
2018-04-23 17:09:05 +02:00
|
|
|
}
|
|
|
|
|
2018-04-22 20:13:12 +02:00
|
|
|
#[get("/users/new")]
|
2018-09-03 15:59:02 +02:00
|
|
|
fn new(user: Option<User>, conn: DbConn) -> Template {
|
2018-05-10 22:31:52 +02:00
|
|
|
Template::render("users/new", json!({
|
2018-09-03 18:05:45 +02:00
|
|
|
"enabled": Instance::get_local(&*conn).map(|i| i.open_registrations).unwrap_or(true),
|
2018-09-03 15:59:02 +02:00
|
|
|
"account": user.map(|u| u.to_json(&*conn)),
|
2018-07-06 19:29:36 +02:00
|
|
|
"errors": null,
|
|
|
|
"form": null
|
2018-05-10 22:31:52 +02:00
|
|
|
}))
|
2018-04-22 20:13:12 +02:00
|
|
|
}
|
|
|
|
|
2018-05-12 17:30:14 +02:00
|
|
|
#[get("/@/<name>/edit")]
|
2018-09-03 15:59:02 +02:00
|
|
|
fn edit(name: String, user: User, conn: DbConn) -> Option<Template> {
|
2018-05-12 17:30:14 +02:00
|
|
|
if user.username == name && !name.contains("@") {
|
|
|
|
Some(Template::render("users/edit", json!({
|
2018-09-03 15:59:02 +02:00
|
|
|
"account": user.to_json(&*conn)
|
2018-05-12 17:30:14 +02:00
|
|
|
})))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-07 10:39:22 +02:00
|
|
|
#[get("/@/<name>/edit", rank = 2)]
|
|
|
|
fn edit_auth(name: String) -> Flash<Redirect> {
|
2018-06-19 23:20:27 +02:00
|
|
|
utils::requires_login("You need to be logged in order to edit your profile", uri!(edit: name = name))
|
2018-06-07 10:39:22 +02:00
|
|
|
}
|
|
|
|
|
2018-05-12 17:30:14 +02:00
|
|
|
#[derive(FromForm)]
|
|
|
|
struct UpdateUserForm {
|
|
|
|
display_name: Option<String>,
|
|
|
|
email: Option<String>,
|
|
|
|
summary: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[put("/@/<_name>/edit", data = "<data>")]
|
2018-06-24 18:58:57 +02:00
|
|
|
fn update(_name: String, conn: DbConn, user: User, data: LenientForm<UpdateUserForm>) -> Redirect {
|
2018-05-12 17:30:14 +02:00
|
|
|
user.update(&*conn,
|
|
|
|
data.get().display_name.clone().unwrap_or(user.display_name.to_string()).to_string(),
|
|
|
|
data.get().email.clone().unwrap_or(user.email.clone().unwrap()).to_string(),
|
|
|
|
data.get().summary.clone().unwrap_or(user.summary.to_string())
|
|
|
|
);
|
2018-06-19 23:20:27 +02:00
|
|
|
Redirect::to(uri!(me))
|
2018-05-12 17:30:14 +02:00
|
|
|
}
|
|
|
|
|
2018-07-06 19:29:36 +02:00
|
|
|
#[derive(FromForm, Serialize, Validate)]
|
2018-07-07 22:51:48 +02:00
|
|
|
#[validate(schema(function = "passwords_match", skip_on_field_errors = "false", message = "Passwords are not matching"))]
|
2018-04-22 20:13:12 +02:00
|
|
|
struct NewUserForm {
|
2018-07-06 19:29:36 +02:00
|
|
|
#[validate(length(min = "1", message = "Username can't be empty"))]
|
2018-04-22 20:13:12 +02:00
|
|
|
username: String,
|
2018-07-06 19:29:36 +02:00
|
|
|
#[validate(email(message = "Invalid email"))]
|
2018-04-22 20:13:12 +02:00
|
|
|
email: String,
|
2018-07-06 19:29:36 +02:00
|
|
|
#[validate(length(min = "8", message = "Password should be at least 8 characters long"))]
|
2018-04-22 20:13:12 +02:00
|
|
|
password: String,
|
2018-07-06 19:29:36 +02:00
|
|
|
#[validate(length(min = "8", message = "Password should be at least 8 characters long"))]
|
2018-04-22 20:13:12 +02:00
|
|
|
password_confirmation: String
|
|
|
|
}
|
|
|
|
|
2018-06-29 14:56:00 +02:00
|
|
|
fn passwords_match(form: &NewUserForm) -> Result<(), ValidationError> {
|
|
|
|
if form.password != form.password_confirmation {
|
|
|
|
Err(ValidationError::new("password_match"))
|
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-22 20:13:12 +02:00
|
|
|
#[post("/users/new", data = "<data>")]
|
2018-07-06 11:51:19 +02:00
|
|
|
fn create(conn: DbConn, data: LenientForm<NewUserForm>) -> Result<Redirect, Template> {
|
2018-09-03 18:05:45 +02:00
|
|
|
if !Instance::get_local(&*conn).map(|i| i.open_registrations).unwrap_or(true) {
|
|
|
|
return Ok(Redirect::to(uri!(new))); // Actually, it is an error
|
|
|
|
}
|
|
|
|
|
2018-04-22 20:13:12 +02:00
|
|
|
let form = data.get();
|
2018-07-06 11:51:19 +02:00
|
|
|
form.validate()
|
|
|
|
.map(|_| {
|
|
|
|
NewUser::new_local(
|
|
|
|
&*conn,
|
|
|
|
form.username.to_string(),
|
|
|
|
form.username.to_string(),
|
|
|
|
false,
|
|
|
|
String::from(""),
|
|
|
|
form.email.to_string(),
|
|
|
|
User::hash_pass(form.password.to_string())
|
|
|
|
).update_boxes(&*conn);
|
|
|
|
Redirect::to(uri!(super::session::new))
|
|
|
|
})
|
|
|
|
.map_err(|e| Template::render("users/new", json!({
|
2018-07-06 19:29:36 +02:00
|
|
|
"errors": e.inner(),
|
|
|
|
"form": form
|
2018-07-06 11:51:19 +02:00
|
|
|
})))
|
2018-04-22 20:13:12 +02:00
|
|
|
}
|
2018-04-29 20:01:42 +02:00
|
|
|
|
|
|
|
#[get("/@/<name>/outbox")]
|
2018-05-16 20:20:44 +02:00
|
|
|
fn outbox(name: String, conn: DbConn) -> ActivityStream<OrderedCollection> {
|
2018-05-01 13:48:19 +02:00
|
|
|
let user = User::find_local(&*conn, name).unwrap();
|
2018-04-29 20:01:42 +02:00
|
|
|
user.outbox(&*conn)
|
|
|
|
}
|
2018-05-01 16:00:29 +02:00
|
|
|
|
|
|
|
#[post("/@/<name>/inbox", data = "<data>")]
|
|
|
|
fn inbox(name: String, conn: DbConn, data: String) -> String {
|
|
|
|
let user = User::find_local(&*conn, name).unwrap();
|
|
|
|
let act: serde_json::Value = serde_json::from_str(&data[..]).unwrap();
|
2018-06-21 18:00:37 +02:00
|
|
|
match user.received(&*conn, act) {
|
|
|
|
Ok(_) => String::new(),
|
|
|
|
Err(e) => {
|
|
|
|
println!("User inbox error: {}\n{}", e.cause(), e.backtrace());
|
|
|
|
format!("Error: {}", e.cause())
|
|
|
|
}
|
|
|
|
}
|
2018-05-01 16:00:29 +02:00
|
|
|
}
|
2018-05-04 15:13:55 +02:00
|
|
|
|
2018-07-11 17:30:01 +02:00
|
|
|
#[get("/@/<name>/followers")]
|
|
|
|
fn ap_followers(name: String, conn: DbConn, _ap: ApRequest) -> ActivityStream<OrderedCollection> {
|
2018-05-04 15:13:55 +02:00
|
|
|
let user = User::find_local(&*conn, name).unwrap();
|
2018-06-21 19:09:18 +02:00
|
|
|
let followers = user.get_followers(&*conn).into_iter().map(|f| Id::new(f.ap_url)).collect::<Vec<Id>>();
|
|
|
|
|
|
|
|
let mut coll = OrderedCollection::default();
|
2018-07-27 12:53:21 +02:00
|
|
|
coll.object_props.set_id_string(user.followers_endpoint).expect("Follower collection: id error");
|
2018-06-21 19:09:18 +02:00
|
|
|
coll.collection_props.set_total_items_u64(followers.len() as u64).expect("Follower collection: totalItems error");
|
|
|
|
coll.collection_props.set_items_link_vec(followers).expect("Follower collection: items error");
|
|
|
|
ActivityStream::new(coll)
|
2018-05-04 15:13:55 +02:00
|
|
|
}
|
2018-09-01 22:08:26 +02:00
|
|
|
|
|
|
|
#[get("/@/<name>/atom.xml")]
|
|
|
|
fn atom_feed(name: String, conn: DbConn) -> Content<String> {
|
|
|
|
let author = User::find_by_fqn(&*conn, name.clone()).expect("Unable to find author");
|
|
|
|
let feed = FeedBuilder::default()
|
|
|
|
.title(author.display_name.clone())
|
|
|
|
.id(Instance::get_local(&*conn).unwrap().compute_box("~", name, "atom.xml"))
|
|
|
|
.entries(Post::get_recents_for_author(&*conn, &author, 15)
|
|
|
|
.into_iter()
|
|
|
|
.map(|p| super::post_to_atom(p, &*conn))
|
|
|
|
.collect::<Vec<Entry>>())
|
|
|
|
.build()
|
|
|
|
.expect("Error building Atom feed");
|
|
|
|
Content(ContentType::new("application", "atom+xml"), feed.to_string())
|
|
|
|
}
|