Hide articles on public pages

Only show them in the dashboard
This commit is contained in:
Bat 2018-09-10 20:06:00 +01:00
parent 8879935925
commit 10da8f31b6
4 changed files with 40 additions and 3 deletions

View File

@ -67,6 +67,7 @@ impl Post {
let ids = tags::table.filter(tags::tag.eq(tag)).select(tags::post_id); let ids = tags::table.filter(tags::tag.eq(tag)).select(tags::post_id);
posts::table.filter(posts::id.eq(any(ids))) posts::table.filter(posts::id.eq(any(ids)))
.filter(posts::published.eq(true))
.order(posts::creation_date.desc()) .order(posts::creation_date.desc())
.offset(min.into()) .offset(min.into())
.limit((max - min).into()) .limit((max - min).into())
@ -78,6 +79,7 @@ impl Post {
use schema::tags; use schema::tags;
let ids = tags::table.filter(tags::tag.eq(tag)).select(tags::post_id); let ids = tags::table.filter(tags::tag.eq(tag)).select(tags::post_id);
posts::table.filter(posts::id.eq(any(ids))) posts::table.filter(posts::id.eq(any(ids)))
.filter(posts::published.eq(true))
.count() .count()
.get_result(conn) .get_result(conn)
.expect("Error counting posts by tag") .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_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); 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))) posts::table.filter(posts::id.eq(any(local_posts_id)))
.filter(posts::published.eq(true))
.load::<Post>(conn) .load::<Post>(conn)
.expect("Couldn't load local posts") .expect("Couldn't load local posts")
.len() .len()
} }
pub fn count(conn: &PgConnection) -> i64 { 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<Post> { pub fn get_recents(conn: &PgConnection, limit: i64) -> Vec<Post> {
posts::table.order(posts::creation_date.desc()) posts::table.order(posts::creation_date.desc())
.filter(posts::published.eq(true))
.limit(limit) .limit(limit)
.load::<Post>(conn) .load::<Post>(conn)
.expect("Error loading recent posts") .expect("Error loading recent posts")
@ -110,6 +114,7 @@ impl Post {
let posts = PostAuthor::belonging_to(author).select(post_authors::post_id); let posts = PostAuthor::belonging_to(author).select(post_authors::post_id);
posts::table.filter(posts::id.eq(any(posts))) posts::table.filter(posts::id.eq(any(posts)))
.filter(posts::published.eq(true))
.order(posts::creation_date.desc()) .order(posts::creation_date.desc())
.limit(limit) .limit(limit)
.load::<Post>(conn) .load::<Post>(conn)
@ -118,6 +123,7 @@ impl Post {
pub fn get_recents_for_blog(conn: &PgConnection, blog: &Blog, limit: i64) -> Vec<Post> { pub fn get_recents_for_blog(conn: &PgConnection, blog: &Blog, limit: i64) -> Vec<Post> {
posts::table.filter(posts::blog_id.eq(blog.id)) posts::table.filter(posts::blog_id.eq(blog.id))
.filter(posts::published.eq(true))
.order(posts::creation_date.desc()) .order(posts::creation_date.desc())
.limit(limit) .limit(limit)
.load::<Post>(conn) .load::<Post>(conn)
@ -126,12 +132,14 @@ impl Post {
pub fn get_for_blog(conn: &PgConnection, blog:&Blog) -> Vec<Post> { pub fn get_for_blog(conn: &PgConnection, blog:&Blog) -> Vec<Post> {
posts::table.filter(posts::blog_id.eq(blog.id)) posts::table.filter(posts::blog_id.eq(blog.id))
.filter(posts::published.eq(true))
.load::<Post>(conn) .load::<Post>(conn)
.expect("Error loading posts for blog") .expect("Error loading posts for blog")
} }
pub fn blog_page(conn: &PgConnection, blog: &Blog, (min, max): (i32, i32)) -> Vec<Post> { pub fn blog_page(conn: &PgConnection, blog: &Blog, (min, max): (i32, i32)) -> Vec<Post> {
posts::table.filter(posts::blog_id.eq(blog.id)) posts::table.filter(posts::blog_id.eq(blog.id))
.filter(posts::published.eq(true))
.order(posts::creation_date.desc()) .order(posts::creation_date.desc())
.offset(min.into()) .offset(min.into())
.limit((max - 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) /// 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<Post> { pub fn get_recents_page(conn: &PgConnection, (min, max): (i32, i32)) -> Vec<Post> {
posts::table.order(posts::creation_date.desc()) posts::table.order(posts::creation_date.desc())
.filter(posts::published.eq(true))
.offset(min.into()) .offset(min.into())
.limit((max - min).into()) .limit((max - min).into())
.load::<Post>(conn) .load::<Post>(conn)
@ -155,6 +164,7 @@ impl Post {
let blog_ids = blogs::table.filter(blogs::instance_id.eq(instance_id)).select(blogs::id); let blog_ids = blogs::table.filter(blogs::instance_id.eq(instance_id)).select(blogs::id);
posts::table.order(posts::creation_date.desc()) posts::table.order(posts::creation_date.desc())
.filter(posts::published.eq(true))
.filter(posts::blog_id.eq(any(blog_ids))) .filter(posts::blog_id.eq(any(blog_ids)))
.offset(min.into()) .offset(min.into())
.limit((max - min).into()) .limit((max - min).into())
@ -169,6 +179,7 @@ impl Post {
.select(post_authors::post_id); .select(post_authors::post_id);
posts::table.order(posts::creation_date.desc()) posts::table.order(posts::creation_date.desc())
.filter(posts::published.eq(true))
.filter(posts::id.eq(any(post_ids))) .filter(posts::id.eq(any(post_ids)))
.offset(min.into()) .offset(min.into())
.limit((max - min).into()) .limit((max - min).into())
@ -176,6 +187,17 @@ impl Post {
.expect("Error loading user feed page") .expect("Error loading user feed page")
} }
pub fn drafts_by_author(conn: &PgConnection, author: &User) -> Vec<Post> {
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::<Post>(conn)
.expect("Error listing drafts")
}
pub fn get_authors(&self, conn: &PgConnection) -> Vec<User> { pub fn get_authors(&self, conn: &PgConnection) -> Vec<User> {
use schema::users; use schema::users;
use schema::post_authors; use schema::post_authors;

View File

@ -373,7 +373,10 @@ impl User {
use schema::posts; use schema::posts;
use schema::post_authors; use schema::post_authors;
let posts_by_self = PostAuthor::belonging_to(self).select(post_authors::post_id); 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::<Post>(conn).unwrap(); let posts = posts::table
.filter(posts::published.eq(true))
.filter(posts::id.eq(any(posts_by_self)))
.load::<Post>(conn).unwrap();
posts.into_iter().map(|p| { posts.into_iter().map(|p| {
serde_json::to_value(p.create_activity(conn)).unwrap() serde_json::to_value(p.create_activity(conn)).unwrap()
}).collect::<Vec<serde_json::Value>>() }).collect::<Vec<serde_json::Value>>()

View File

@ -105,7 +105,8 @@ fn dashboard(user: User, conn: DbConn) -> Template {
let blogs = Blog::find_for_author(&*conn, user.id); let blogs = Blog::find_for_author(&*conn, user.id);
Template::render("users/dashboard", json!({ Template::render("users/dashboard", json!({
"account": user.to_json(&*conn), "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::<Vec<serde_json::Value>>(),
})) }))
} }

View File

@ -23,6 +23,17 @@
<a class="button" href="/blogs/new">{{ "Start a new blog" | _ }}</a> <a class="button" href="/blogs/new">{{ "Start a new blog" | _ }}</a>
</section> </section>
{% if drafts | length > 0 %}
<section>
<h2>{{ "Your Drafts" | _ }}</h2>
<div class="cards">
{% for draft in drafts %}
{{ macros::post_card(article=draft) }}
{% endfor %}
</div>
</section>
{% endif %}
<section> <section>
<h2>{{ "Your media" | _ }}</h2> <h2>{{ "Your media" | _ }}</h2>
<a class="button" href="/medias">{{ "Go to your gallery" | _ }}</a> <a class="button" href="/medias">{{ "Go to your gallery" | _ }}</a>