From 10da8f31b6948175c5e7a84f7b6143c271f98d9f Mon Sep 17 00:00:00 2001 From: Bat Date: Mon, 10 Sep 2018 20:06:00 +0100 Subject: [PATCH] Hide articles on public pages Only show them in the dashboard --- plume-models/src/posts.rs | 24 +++++++++++++++++++++++- plume-models/src/users.rs | 5 ++++- src/routes/user.rs | 3 ++- templates/users/dashboard.html.tera | 11 +++++++++++ 4 files changed, 40 insertions(+), 3 deletions(-) diff --git a/plume-models/src/posts.rs b/plume-models/src/posts.rs index e4bd1563..3d1f6219 100644 --- a/plume-models/src/posts.rs +++ b/plume-models/src/posts.rs @@ -67,6 +67,7 @@ impl Post { let ids = tags::table.filter(tags::tag.eq(tag)).select(tags::post_id); posts::table.filter(posts::id.eq(any(ids))) + .filter(posts::published.eq(true)) .order(posts::creation_date.desc()) .offset(min.into()) .limit((max - min).into()) @@ -78,6 +79,7 @@ impl Post { use schema::tags; let ids = tags::table.filter(tags::tag.eq(tag)).select(tags::post_id); posts::table.filter(posts::id.eq(any(ids))) + .filter(posts::published.eq(true)) .count() .get_result(conn) .expect("Error counting posts by tag") @@ -89,17 +91,19 @@ impl Post { let local_authors = users::table.filter(users::instance_id.eq(Instance::local_id(conn))).select(users::id); let local_posts_id = post_authors::table.filter(post_authors::author_id.eq(any(local_authors))).select(post_authors::post_id); posts::table.filter(posts::id.eq(any(local_posts_id))) + .filter(posts::published.eq(true)) .load::(conn) .expect("Couldn't load local posts") .len() } pub fn count(conn: &PgConnection) -> i64 { - posts::table.count().get_result(conn).expect("Couldn't count posts") + posts::table.filter(posts::published.eq(true)).count().get_result(conn).expect("Couldn't count posts") } pub fn get_recents(conn: &PgConnection, limit: i64) -> Vec { posts::table.order(posts::creation_date.desc()) + .filter(posts::published.eq(true)) .limit(limit) .load::(conn) .expect("Error loading recent posts") @@ -110,6 +114,7 @@ impl Post { let posts = PostAuthor::belonging_to(author).select(post_authors::post_id); posts::table.filter(posts::id.eq(any(posts))) + .filter(posts::published.eq(true)) .order(posts::creation_date.desc()) .limit(limit) .load::(conn) @@ -118,6 +123,7 @@ impl Post { pub fn get_recents_for_blog(conn: &PgConnection, blog: &Blog, limit: i64) -> Vec { posts::table.filter(posts::blog_id.eq(blog.id)) + .filter(posts::published.eq(true)) .order(posts::creation_date.desc()) .limit(limit) .load::(conn) @@ -126,12 +132,14 @@ impl Post { pub fn get_for_blog(conn: &PgConnection, blog:&Blog) -> Vec { posts::table.filter(posts::blog_id.eq(blog.id)) + .filter(posts::published.eq(true)) .load::(conn) .expect("Error loading posts for blog") } pub fn blog_page(conn: &PgConnection, blog: &Blog, (min, max): (i32, i32)) -> Vec { posts::table.filter(posts::blog_id.eq(blog.id)) + .filter(posts::published.eq(true)) .order(posts::creation_date.desc()) .offset(min.into()) .limit((max - min).into()) @@ -142,6 +150,7 @@ impl Post { /// Give a page of all the recent posts known to this instance (= federated timeline) pub fn get_recents_page(conn: &PgConnection, (min, max): (i32, i32)) -> Vec { posts::table.order(posts::creation_date.desc()) + .filter(posts::published.eq(true)) .offset(min.into()) .limit((max - min).into()) .load::(conn) @@ -155,6 +164,7 @@ impl Post { let blog_ids = blogs::table.filter(blogs::instance_id.eq(instance_id)).select(blogs::id); posts::table.order(posts::creation_date.desc()) + .filter(posts::published.eq(true)) .filter(posts::blog_id.eq(any(blog_ids))) .offset(min.into()) .limit((max - min).into()) @@ -169,6 +179,7 @@ impl Post { .select(post_authors::post_id); posts::table.order(posts::creation_date.desc()) + .filter(posts::published.eq(true)) .filter(posts::id.eq(any(post_ids))) .offset(min.into()) .limit((max - min).into()) @@ -176,6 +187,17 @@ impl Post { .expect("Error loading user feed page") } + pub fn drafts_by_author(conn: &PgConnection, author: &User) -> Vec { + use schema::post_authors; + + let posts = PostAuthor::belonging_to(author).select(post_authors::post_id); + posts::table.order(posts::creation_date.desc()) + .filter(posts::published.eq(false)) + .filter(posts::id.eq(any(posts))) + .load::(conn) + .expect("Error listing drafts") + } + pub fn get_authors(&self, conn: &PgConnection) -> Vec { use schema::users; use schema::post_authors; diff --git a/plume-models/src/users.rs b/plume-models/src/users.rs index 6148fc5e..9cfad4e3 100644 --- a/plume-models/src/users.rs +++ b/plume-models/src/users.rs @@ -373,7 +373,10 @@ impl User { use schema::posts; use schema::post_authors; let posts_by_self = PostAuthor::belonging_to(self).select(post_authors::post_id); - let posts = posts::table.filter(posts::id.eq(any(posts_by_self))).load::(conn).unwrap(); + let posts = posts::table + .filter(posts::published.eq(true)) + .filter(posts::id.eq(any(posts_by_self))) + .load::(conn).unwrap(); posts.into_iter().map(|p| { serde_json::to_value(p.create_activity(conn)).unwrap() }).collect::>() diff --git a/src/routes/user.rs b/src/routes/user.rs index be61d7ee..3deb2574 100644 --- a/src/routes/user.rs +++ b/src/routes/user.rs @@ -105,7 +105,8 @@ fn dashboard(user: User, conn: DbConn) -> Template { let blogs = Blog::find_for_author(&*conn, user.id); Template::render("users/dashboard", json!({ "account": user.to_json(&*conn), - "blogs": blogs + "blogs": blogs, + "drafts": Post::drafts_by_author(&*conn, &user).into_iter().map(|a| a.to_json(&*conn)).collect::>(), })) } diff --git a/templates/users/dashboard.html.tera b/templates/users/dashboard.html.tera index 1e0e2c52..bb3456a6 100644 --- a/templates/users/dashboard.html.tera +++ b/templates/users/dashboard.html.tera @@ -23,6 +23,17 @@ {{ "Start a new blog" | _ }} + {% if drafts | length > 0 %} +
+

{{ "Your Drafts" | _ }}

+
+ {% for draft in drafts %} + {{ macros::post_card(article=draft) }} + {% endfor %} +
+
+ {% endif %} +

{{ "Your media" | _ }}

{{ "Go to your gallery" | _ }}