Plume/src/routes/user.rs

245 lines
8.5 KiB
Rust
Raw Normal View History

2018-06-10 13:13:07 +02:00
use activitypub::{
2018-05-16 20:20:44 +02:00
activity::Follow,
collection::OrderedCollection
};
use rocket::{request::Form,
response::{Redirect, Flash}
};
2018-04-24 14:31:02 +02:00
use rocket_contrib::Template;
2018-05-01 13:48:19 +02:00
use serde_json;
2018-04-22 20:13:12 +02:00
2018-05-19 09:39:59 +02:00
use activity_pub::{
activity_pub, ActivityPub, ActivityStream, context, broadcast, Id, IntoId,
2018-05-19 09:53:51 +02:00
inbox::Inbox,
actor::Actor
2018-05-19 09:39:59 +02:00
};
2018-04-22 20:13:12 +02:00
use db_conn::DbConn;
2018-05-19 09:39:59 +02:00
use models::{
2018-06-10 19:55:08 +02:00
blogs::Blog,
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::*
};
use utils;
2018-04-22 20:13:12 +02:00
2018-04-23 11:52:44 +02:00
#[get("/me")]
fn me(user: Option<User>) -> Result<Redirect,Flash<Redirect>> {
match user {
Some(user) => Ok(Redirect::to(format!("/@/{}/", user.username).as_ref())),
None => Err(utils::requires_login("", "/me"))
}
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-05-10 22:31:52 +02:00
fn details(name: String, conn: DbConn, account: Option<User>) -> Template {
2018-05-01 13:48:19 +02:00
let user = User::find_by_fqn(&*conn, name).unwrap();
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-01 13:48:19 +02:00
Template::render("users/details", json!({
"user": serde_json::to_value(user.clone()).unwrap(),
"instance_url": user.get_instance(&*conn).public_domain,
"is_remote": user.instance_id != Instance::local_id(&*conn),
2018-06-13 20:06:14 +02:00
"follows": account.clone().map(|x| x.is_following(&*conn, user.id)).unwrap_or(false),
"account": account,
"recents": recents.into_iter().map(|p| {
json!({
"post": p,
"author": ({
let author = &p.get_authors(&*conn)[0];
let mut json = serde_json::to_value(author).unwrap();
json["fqn"] = serde_json::Value::String(author.get_fqn(&*conn));
json
}),
2018-05-12 19:59:38 +02:00
"url": format!("/~/{}/{}/", p.get_blog(&*conn).actor_id, p.slug),
2018-05-24 11:45:36 +02:00
"date": p.creation_date.timestamp()
})
}).collect::<Vec<serde_json::Value>>(),
"reshares": reshares.into_iter().map(|r| {
let p = r.get_post(&*conn).unwrap();
json!({
"post": p,
"author": ({
let author = &p.get_authors(&*conn)[0];
let mut json = serde_json::to_value(author).unwrap();
json["fqn"] = serde_json::Value::String(author.get_fqn(&*conn));
json
}),
"url": format!("/~/{}/{}/", p.get_blog(&*conn).actor_id, p.slug),
"date": p.creation_date.timestamp()
})
}).collect::<Vec<serde_json::Value>>(),
"is_self": account.map(|a| a.id == user_id).unwrap_or(false),
"n_followers": n_followers
2018-05-01 13:48:19 +02:00
}))
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!({
"account": user,
"blogs": blogs
}))
}
#[get("/dashboard", rank = 2)]
fn dashboard_auth() -> Flash<Redirect> {
utils::requires_login("You need to be logged in order to access your dashboard", "/dashboard")
}
2018-05-01 21:57:30 +02:00
#[get("/@/<name>/follow")]
fn follow(name: String, conn: DbConn, user: User) -> Redirect {
let target = User::find_by_fqn(&*conn, name.clone()).unwrap();
2018-05-16 20:20:44 +02:00
follows::Follow::insert(&*conn, follows::NewFollow {
2018-05-01 21:57:30 +02:00
follower_id: user.id,
following_id: target.id
});
2018-05-19 00:04:30 +02:00
let mut act = Follow::default();
2018-06-10 13:13:07 +02:00
act.follow_props.set_actor_link::<Id>(user.clone().into_id()).unwrap();
act.follow_props.set_object_object(user.into_activity(&*conn)).unwrap();
2018-05-19 00:04:30 +02:00
act.object_props.set_id_string(format!("{}/follow/{}", user.ap_url, target.ap_url)).unwrap();
2018-05-16 20:20:44 +02:00
broadcast(&*conn, &user, act, vec![target]);
Redirect::to(format!("/@/{}/", name).as_ref())
2018-05-01 21:57:30 +02:00
}
#[get("/@/<name>/follow", rank = 2)]
fn follow_auth(name: String) -> Flash<Redirect> {
2018-06-10 19:55:08 +02:00
utils::requires_login("You need to be logged in order to follow someone", &format!("/@/{}/follow", name))
}
#[get("/@/<name>/followers", rank = 2)]
fn followers(name: String, conn: DbConn, account: Option<User>) -> Template {
let user = User::find_by_fqn(&*conn, name.clone()).unwrap();
let user_id = user.id.clone();
2018-06-15 15:08:38 +02:00
Template::render("users/followers", json!({
"user": serde_json::to_value(user.clone()).unwrap(),
"instance_url": user.get_instance(&*conn).public_domain,
"is_remote": user.instance_id != Instance::local_id(&*conn),
2018-06-13 20:06:14 +02:00
"follows": account.clone().map(|x| x.is_following(&*conn, user.id)).unwrap_or(false),
"followers": user.get_followers(&*conn).into_iter().map(|f| {
let fqn = f.get_fqn(&*conn);
let mut json = serde_json::to_value(f).unwrap();
json["fqn"] = serde_json::Value::String(fqn);
json
}).collect::<Vec<serde_json::Value>>(),
"account": account,
"is_self": account.map(|a| a.id == user_id).unwrap_or(false)
}))
}
2018-04-23 17:09:05 +02:00
#[get("/@/<name>", format = "application/activity+json", rank = 1)]
fn activity_details(name: String, conn: DbConn) -> ActivityPub {
2018-05-01 13:48:19 +02:00
let user = User::find_local(&*conn, name).unwrap();
2018-04-24 14:31:02 +02:00
user.as_activity_pub(&*conn)
2018-04-23 17:09:05 +02:00
}
2018-04-22 20:13:12 +02:00
#[get("/users/new")]
2018-05-10 22:31:52 +02:00
fn new(user: Option<User>) -> Template {
Template::render("users/new", json!({
"account": user
}))
2018-04-22 20:13:12 +02:00
}
2018-05-12 17:30:14 +02:00
#[get("/@/<name>/edit")]
fn edit(name: String, user: User) -> Option<Template> {
if user.username == name && !name.contains("@") {
Some(Template::render("users/edit", json!({
"account": user
})))
} else {
None
}
}
#[get("/@/<name>/edit", rank = 2)]
fn edit_auth(name: String) -> Flash<Redirect> {
utils::requires_login("You need to be logged in order to edit your profile", &format!("/@/{}/edit", name))
}
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>")]
fn update(_name: String, conn: DbConn, user: User, data: Form<UpdateUserForm>) -> Redirect {
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())
);
Redirect::to("/me")
}
2018-04-22 20:13:12 +02:00
#[derive(FromForm)]
struct NewUserForm {
username: String,
email: String,
password: String,
password_confirmation: String
}
#[post("/users/new", data = "<data>")]
fn create(conn: DbConn, data: Form<NewUserForm>) -> Result<Redirect, String> {
2018-04-22 20:13:12 +02:00
let inst = Instance::get_local(&*conn).unwrap();
let form = data.get();
if form.username.clone().len() < 1 {
Err(String::from("Username is required"))
} else if form.email.clone().len() < 1 {
Err(String::from("Email is required"))
} else if form.password.clone().len() < 8 {
Err(String::from("Password should be at least 8 characters long"))
} else if form.password == form.password_confirmation {
User::insert(&*conn, NewUser::new_local(
form.username.to_string(),
form.username.to_string(),
!inst.has_admin(&*conn),
String::from(""),
form.email.to_string(),
User::hash_pass(form.password.to_string()),
inst.id
)).update_boxes(&*conn);
Ok(Redirect::to(format!("/@/{}/", data.get().username).as_str()))
} else {
Err(String::from("Passwords don't match"))
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();
user.received(&*conn, act);
String::from("")
}
2018-05-04 15:13:55 +02:00
#[get("/@/<name>/followers", format = "application/activity+json")]
fn ap_followers(name: String, conn: DbConn) -> ActivityPub {
2018-05-04 15:13:55 +02:00
let user = User::find_local(&*conn, name).unwrap();
let followers = user.get_followers(&*conn).into_iter().map(|f| f.compute_id(&*conn)).collect::<Vec<String>>();
let json = json!({
"@context": context(),
"id": user.compute_box(&*conn, "followers"),
"type": "OrderedCollection",
"totalItems": followers.len(),
"orderedItems": followers
});
activity_pub(json)
}