From edbeeef64095aca3cb57dca56a4ec28f3715ec8b Mon Sep 17 00:00:00 2001 From: Bat Date: Sun, 10 Jun 2018 18:55:08 +0100 Subject: [PATCH] Add a dashboard --- src/activity_pub/inbox.rs | 1 - src/main.rs | 2 ++ src/models/blogs.rs | 10 +++++++++- src/routes/user.rs | 17 ++++++++++++++++- templates/base.tera | 1 + templates/users/dashboard.tera | 25 +++++++++++++++++++++++++ 6 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 templates/users/dashboard.tera diff --git a/src/activity_pub/inbox.rs b/src/activity_pub/inbox.rs index ae40667..294fd1f 100644 --- a/src/activity_pub/inbox.rs +++ b/src/activity_pub/inbox.rs @@ -1,6 +1,5 @@ use activitypub::{ Actor, - actor::Person, activity::{Accept, Announce, Create, Follow, Like, Undo}, object::Note }; diff --git a/src/main.rs b/src/main.rs index 5ffbe60..382833c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -104,6 +104,8 @@ fn main() { routes::user::me, routes::user::details, + routes::user::dashboard, + routes::user::dashboard_auth, routes::user::followers, routes::user::edit, routes::user::edit_auth, diff --git a/src/models/blogs.rs b/src/models/blogs.rs index fbec62e..cf8bd07 100644 --- a/src/models/blogs.rs +++ b/src/models/blogs.rs @@ -7,7 +7,7 @@ use reqwest::{ use serde_json; use url::Url; use chrono::NaiveDateTime; -use diesel::{self, QueryDsl, RunQueryDsl, ExpressionMethods, PgConnection}; +use diesel::{self, QueryDsl, RunQueryDsl, ExpressionMethods, PgConnection, dsl::any}; use openssl::{ hash::MessageDigest, pkey::{PKey, Private}, @@ -71,6 +71,14 @@ impl Blog { .into_iter().nth(0) } + pub fn find_for_author(conn: &PgConnection, author_id: i32) -> Vec { + use schema::blog_authors; + let author_ids = blog_authors::table.filter(blog_authors::author_id.eq(author_id)).select(blog_authors::blog_id); + blogs::table.filter(blogs::id.eq(any(author_ids))) + .load::(conn) + .expect("Couldn't load blogs ") + } + pub fn find_by_name(conn: &PgConnection, name: String, instance_id: i32) -> Option { blogs::table.filter(blogs::actor_id.eq(name)) .filter(blogs::instance_id.eq(instance_id)) diff --git a/src/routes/user.rs b/src/routes/user.rs index eab7c5e..49023c6 100644 --- a/src/routes/user.rs +++ b/src/routes/user.rs @@ -15,6 +15,7 @@ use activity_pub::{ }; use db_conn::DbConn; use models::{ + blogs::Blog, follows, instance::Instance, posts::Post, @@ -74,6 +75,20 @@ fn details(name: String, conn: DbConn, account: Option) -> Template { })) } +#[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 { + utils::requires_login("You need to be logged in order to access your dashboard", "/dashboard") +} + #[get("/@//follow")] fn follow(name: String, conn: DbConn, user: User) -> Redirect { let target = User::find_by_fqn(&*conn, name.clone()).unwrap(); @@ -91,7 +106,7 @@ fn follow(name: String, conn: DbConn, user: User) -> Redirect { #[get("/@//follow", rank = 2)] fn follow_auth(name: String) -> Flash { - utils::requires_login("You need to belogged in order to follow someone", &format!("/@/{}/follow", name)) + utils::requires_login("You need to be logged in order to follow someone", &format!("/@/{}/follow", name)) } #[get("/@//followers", rank = 2)] diff --git a/templates/base.tera b/templates/base.tera index 768242c..491ff4b 100644 --- a/templates/base.tera +++ b/templates/base.tera @@ -16,6 +16,7 @@