From 3013eec5794ff6816da735e5d27c15eabe4c6143 Mon Sep 17 00:00:00 2001 From: Bat Date: Mon, 3 Sep 2018 14:59:02 +0100 Subject: [PATCH] Redesign menu items --- src/routes/blogs.rs | 10 +++++----- src/routes/comments.rs | 2 +- src/routes/errors.rs | 7 +++++-- src/routes/instance.rs | 8 ++++---- src/routes/medias.rs | 8 ++++---- src/routes/notifications.rs | 2 +- src/routes/posts.rs | 14 +++++++------- src/routes/session.rs | 10 +++++----- src/routes/user.rs | 18 +++++++++--------- static/main.css | 26 +++++++++++++++++++++++++- templates/base.html.tera | 22 +++++++++++++++++----- templates/users/header.html.tera | 2 +- 12 files changed, 84 insertions(+), 45 deletions(-) diff --git a/src/routes/blogs.rs b/src/routes/blogs.rs index f4d783ae..cc99964e 100644 --- a/src/routes/blogs.rs +++ b/src/routes/blogs.rs @@ -24,14 +24,14 @@ use routes::Page; #[get("/~/?", rank = 2)] fn paginated_details(name: String, conn: DbConn, user: Option, page: Page) -> Template { - may_fail!(user, Blog::find_by_fqn(&*conn, name), "Requested blog couldn't be found", |blog| { + may_fail!(user.map(|u| u.to_json(&*conn)), Blog::find_by_fqn(&*conn, name), "Requested blog couldn't be found", |blog| { let posts = Post::blog_page(&*conn, &blog, page.limits()); let articles = Post::get_for_blog(&*conn, &blog); let authors = &blog.list_authors(&*conn); Template::render("blogs/details", json!({ "blog": &blog.to_json(&*conn), - "account": user, + "account": user.clone().map(|u| u.to_json(&*conn)), "is_author": user.map(|x| x.is_author_in(&*conn, blog.clone())), "posts": posts.into_iter().map(|p| p.to_json(&*conn)).collect::>(), "authors": authors.into_iter().map(|u| u.to_json(&*conn)).collect::>(), @@ -55,9 +55,9 @@ fn activity_details(name: String, conn: DbConn, _ap: ApRequest) -> ActivityStrea } #[get("/blogs/new")] -fn new(user: User) -> Template { +fn new(user: User, conn: DbConn) -> Template { Template::render("blogs/new", json!({ - "account": user, + "account": user.to_json(&*conn), "errors": null, "form": null })) @@ -119,7 +119,7 @@ fn create(conn: DbConn, data: LenientForm, user: User) -> Result, us "has_liked": user.has_liked(&*conn, &post), "n_reshares": post.get_reshares(&*conn).len(), "has_reshared": user.has_reshared(&*conn, &post), - "account": user, + "account": user.to_json(&*conn), "date": &post.creation_date.timestamp(), "previous": form.responding_to.map(|r| Comment::get(&*conn, r).expect("Error retrieving previous comment").to_json(&*conn, &vec![])), "user_fqn": user.get_fqn(&*conn), diff --git a/src/routes/errors.rs b/src/routes/errors.rs index ee17982d..5f9f3f5c 100644 --- a/src/routes/errors.rs +++ b/src/routes/errors.rs @@ -1,23 +1,26 @@ use rocket_contrib::Template; use rocket::Request; use rocket::request::FromRequest; +use plume_models::db_conn::DbConn; use plume_models::users::User; #[catch(404)] fn not_found(req: &Request) -> Template { + let conn = req.guard::().expect("404: DbConn error"); let user = User::from_request(req).succeeded(); Template::render("errors/404", json!({ "error_message": "Page not found", - "account": user + "account": user.map(|u| u.to_json(&*conn)) })) } #[catch(500)] fn server_error(req: &Request) -> Template { + let conn = req.guard::().expect("500: DbConn error"); let user = User::from_request(req).succeeded(); Template::render("errors/500", json!({ "error_message": "Server error", - "account": user + "account": user.map(|u| u.to_json(&*conn)) })) } diff --git a/src/routes/instance.rs b/src/routes/instance.rs index b4f395f6..2d666084 100644 --- a/src/routes/instance.rs +++ b/src/routes/instance.rs @@ -23,7 +23,7 @@ fn paginated_index(conn: DbConn, user: Option, page: Page) -> Template { Template::render("instance/index", json!({ "instance": inst, - "account": user, + "account": user.map(|u| u.to_json(&*conn)), "recents": recents.into_iter().map(|p| p.to_json(&*conn)).collect::>(), "page": page.page, "n_pages": Page::total(Post::count(&*conn) as i32), @@ -47,7 +47,7 @@ fn index(conn: DbConn, user: Option) -> Template { #[get("/admin")] fn admin(conn: DbConn, admin: Admin) -> Template { Template::render("instance/admin", json!({ - "account": admin.0, + "account": admin.0.to_json(&*conn), "instance": Instance::get_local(&*conn), "errors": null, "form": null @@ -79,7 +79,7 @@ fn update_settings(conn: DbConn, admin: Admin, form: LenientForm Json { #[get("/about")] fn about(user: User, conn: DbConn) -> Template { Template::render("instance/about", json!({ - "account": user, + "account": user.to_json(&*conn), "instance": Instance::get_local(&*conn), "admin": Instance::get_local(&*conn).map(|i| i.main_admin(&*conn).to_json(&*conn)), "version": "0.1.0", diff --git a/src/routes/medias.rs b/src/routes/medias.rs index 5a9ee85b..fa9197d4 100644 --- a/src/routes/medias.rs +++ b/src/routes/medias.rs @@ -10,15 +10,15 @@ use plume_models::{db_conn::DbConn, medias::*, users::User}; fn list(user: User, conn: DbConn) -> Template { let medias = Media::for_user(&*conn, user.id); Template::render("medias/index", json!({ - "account": user, + "account": user.to_json(&*conn), "medias": medias.into_iter().map(|m| m.to_json(&*conn)).collect::>() })) } #[get("/medias/new")] -fn new(user: User) -> Template { +fn new(user: User, conn: DbConn) -> Template { Template::render("medias/new", json!({ - "account": user, + "account": user.to_json(&*conn), "form": {}, "errors": {} })) @@ -92,7 +92,7 @@ fn read(data: &SavedData) -> String { fn details(id: i32, user: User, conn: DbConn) -> Template { let media = Media::get(&*conn, id); Template::render("medias/details", json!({ - "account": user, + "account": user.to_json(&*conn), "media": media.map(|m| m.to_json(&*conn)) })) } diff --git a/src/routes/notifications.rs b/src/routes/notifications.rs index 64def8da..08e3df6c 100644 --- a/src/routes/notifications.rs +++ b/src/routes/notifications.rs @@ -8,7 +8,7 @@ use routes::Page; #[get("/notifications?")] fn paginated_notifications(conn: DbConn, user: User, page: Page) -> Template { Template::render("notifications/index", json!({ - "account": user, + "account": user.to_json(&*conn), "notifications": Notification::page_for_user(&*conn, &user, page.limits()).into_iter().map(|n| n.to_json(&*conn)).collect::>(), "page": page.page, "n_pages": Page::total(Notification::find_for_user(&*conn, &user).len() as i32) diff --git a/src/routes/posts.rs b/src/routes/posts.rs index f047ec30..44376b89 100644 --- a/src/routes/posts.rs +++ b/src/routes/posts.rs @@ -35,8 +35,8 @@ fn details(blog: String, slug: String, conn: DbConn, user: Option) -> Temp #[get("/~//?")] fn details_response(blog: String, slug: String, conn: DbConn, user: Option, query: Option) -> Template { - may_fail!(user, Blog::find_by_fqn(&*conn, blog), "Couldn't find this blog", |blog| { - may_fail!(user, Post::find_by_slug(&*conn, slug, blog.id), "Couldn't find this post", |post| { + may_fail!(user.map(|u| u.to_json(&*conn)), Blog::find_by_fqn(&*conn, blog), "Couldn't find this blog", |blog| { + may_fail!(user.map(|u| u.to_json(&*conn)), Post::find_by_slug(&*conn, slug, blog.id), "Couldn't find this post", |post| { let comments = Comment::list_by_post(&*conn, post.id); let comms = comments.clone(); @@ -53,7 +53,7 @@ fn details_response(blog: String, slug: String, conn: DbConn, user: Option "has_liked": user.clone().map(|u| u.has_liked(&*conn, &post)).unwrap_or(false), "n_reshares": post.get_reshares(&*conn).len(), "has_reshared": user.clone().map(|u| u.has_reshared(&*conn, &post)).unwrap_or(false), - "account": &user, + "account": &user.clone().map(|u| u.to_json(&*conn)), "date": &post.creation_date.timestamp(), "previous": query.and_then(|q| q.responding_to.map(|r| Comment::get(&*conn, r).expect("Error retrieving previous comment").to_json(&*conn, &vec![]))), "user_fqn": user.clone().map(|u| u.get_fqn(&*conn)).unwrap_or(String::new()), @@ -86,7 +86,7 @@ fn new(blog: String, user: User, conn: DbConn) -> Template { })) } else { Template::render("posts/new", json!({ - "account": user, + "account": user.to_json(&*conn), "instance": Instance::get_local(&*conn), "errors": null, "form": null @@ -118,7 +118,7 @@ fn create(blog_name: String, data: LenientForm, user: User, conn: D let blog = Blog::find_by_fqn(&*conn, blog_name.to_string()).unwrap(); let form = data.get(); let slug = form.title.to_string().to_kebab_case(); - + let mut errors = match form.validate() { Ok(_) => ValidationErrors::new(), Err(e) => e @@ -170,8 +170,8 @@ fn create(blog_name: String, data: LenientForm, user: User, conn: D } } else { Err(Template::render("posts/new", json!({ - "account": user, - "instance": Instance::get_local(&*conn), + "account": user.to_json(&*conn), + "instance": Instance::get_local(&*conn), "errors": errors.inner(), "form": form }))) diff --git a/src/routes/session.rs b/src/routes/session.rs index d6e73b32..bcc3c6b0 100644 --- a/src/routes/session.rs +++ b/src/routes/session.rs @@ -13,10 +13,10 @@ use plume_models::{ }; #[get("/login")] -fn new(user: Option) -> Template { +fn new(user: Option, conn: DbConn) -> Template { Template::render("session/login", json!({ - "account": user, - "errors": null, + "account": user.map(|u| u.to_json(&*conn)), + "errors": null, "form": null })) } @@ -27,9 +27,9 @@ struct Message { } #[get("/login?")] -fn new_message(user: Option, message: Message) -> Template { +fn new_message(user: Option, message: Message, conn: DbConn) -> Template { Template::render("session/login", json!({ - "account": user, + "account": user.map(|u| u.to_json(&*conn)), "message": message.m, "errors": null, "form": null diff --git a/src/routes/user.rs b/src/routes/user.rs index 2246c05f..451dfb4c 100644 --- a/src/routes/user.rs +++ b/src/routes/user.rs @@ -42,7 +42,7 @@ fn me(user: Option) -> Result> { #[get("/@/", rank = 2)] fn details<'r>(name: String, conn: DbConn, account: Option, worker: State>>, fecth_articles_conn: DbConn, fecth_followers_conn: DbConn) -> Template { - may_fail!(account, User::find_by_fqn(&*conn, name), "Couldn't find requested user", |user| { + may_fail!(account.map(|a| a.to_json(&*conn)), User::find_by_fqn(&*conn, name), "Couldn't find requested user", |user| { 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(); @@ -82,7 +82,7 @@ fn details<'r>(name: String, conn: DbConn, account: Option, worker: State< "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), - "account": account, + "account": account.clone().map(|a| a.to_json(&*conn)), "recents": recents.into_iter().map(|p| p.to_json(&*conn)).collect::>(), "reshares": reshares.into_iter().map(|r| r.get_post(&*conn).unwrap().to_json(&*conn)).collect::>(), "is_self": account.map(|a| a.id == user_id).unwrap_or(false), @@ -95,7 +95,7 @@ fn details<'r>(name: String, conn: DbConn, account: Option, worker: State< fn dashboard(user: User, conn: DbConn) -> Template { let blogs = Blog::find_for_author(&*conn, user.id); Template::render("users/dashboard", json!({ - "account": user, + "account": user.to_json(&*conn), "blogs": blogs })) } @@ -132,7 +132,7 @@ fn follow_auth(name: String) -> Flash { #[get("/@//followers?")] fn followers_paginated(name: String, conn: DbConn, account: Option, page: Page) -> Template { - may_fail!(account, User::find_by_fqn(&*conn, name.clone()), "Couldn't find requested user", |user| { + may_fail!(account.map(|a| a.to_json(&*conn)), User::find_by_fqn(&*conn, name.clone()), "Couldn't find requested user", |user| { let user_id = user.id.clone(); let followers_count = user.get_followers(&*conn).len(); @@ -142,7 +142,7 @@ fn followers_paginated(name: String, conn: DbConn, account: Option, page: "is_remote": user.instance_id != Instance::local_id(&*conn), "follows": account.clone().map(|x| x.is_following(&*conn, user.id)).unwrap_or(false), "followers": user.get_followers_page(&*conn, page.limits()).into_iter().map(|f| f.to_json(&*conn)).collect::>(), - "account": account, + "account": account.clone().map(|a| a.to_json(&*conn)), "is_self": account.map(|a| a.id == user_id).unwrap_or(false), "n_followers": followers_count, "page": page.page, @@ -164,19 +164,19 @@ fn activity_details(name: String, conn: DbConn, _ap: ApRequest) -> ActivityStrea } #[get("/users/new")] -fn new(user: Option) -> Template { +fn new(user: Option, conn: DbConn) -> Template { Template::render("users/new", json!({ - "account": user, + "account": user.map(|u| u.to_json(&*conn)), "errors": null, "form": null })) } #[get("/@//edit")] -fn edit(name: String, user: User) -> Option