Merge pull request #1592 from LemmyNet/feature/mark_post_as_read

Add show_read_posts filter. Fixes #1561
This commit is contained in:
Dessalines 2021-04-26 10:57:40 -04:00 committed by GitHub
commit 1eb7e41674
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 104 additions and 57 deletions

View File

@ -240,6 +240,7 @@ impl Perform for SaveUserSettings {
default_listing_type, default_listing_type,
lang: data.lang.to_owned(), lang: data.lang.to_owned(),
show_avatars: data.show_avatars, show_avatars: data.show_avatars,
show_read_posts: data.show_read_posts,
send_notifications_to_email: data.send_notifications_to_email, send_notifications_to_email: data.send_notifications_to_email,
}; };

View File

@ -6,6 +6,7 @@ use lemmy_api_common::{
check_downvotes_enabled, check_downvotes_enabled,
get_local_user_view_from_jwt, get_local_user_view_from_jwt,
is_mod_or_admin, is_mod_or_admin,
mark_post_as_read,
post::*, post::*,
}; };
use lemmy_apub::{ApubLikeableType, ApubObjectType}; use lemmy_apub::{ApubLikeableType, ApubObjectType};
@ -69,6 +70,9 @@ impl Perform for CreatePostLike {
.await?; .await?;
} }
// Mark the post as read
mark_post_as_read(person_id, post_id, context.pool()).await?;
let post_id = data.post_id; let post_id = data.post_id;
let person_id = local_user_view.person.id; let person_id = local_user_view.person.id;
let post_view = blocking(context.pool(), move |conn| { let post_view = blocking(context.pool(), move |conn| {
@ -269,6 +273,9 @@ impl Perform for SavePost {
}) })
.await??; .await??;
// Mark the post as read
mark_post_as_read(person_id, post_id, context.pool()).await?;
Ok(PostResponse { post_view }) Ok(PostResponse { post_view })
} }
} }

View File

@ -9,8 +9,6 @@ use lemmy_api_common::{
get_local_user_view_from_jwt_opt, get_local_user_view_from_jwt_opt,
is_admin, is_admin,
site::*, site::*,
user_show_bot_accounts,
user_show_nsfw,
}; };
use lemmy_apub::fetcher::search::search_by_apub_id; use lemmy_apub::fetcher::search::search_by_apub_id;
use lemmy_db_queries::{ use lemmy_db_queries::{
@ -145,8 +143,13 @@ impl Perform for Search {
let local_user_view = get_local_user_view_from_jwt_opt(&data.auth, context.pool()).await?; let local_user_view = get_local_user_view_from_jwt_opt(&data.auth, context.pool()).await?;
let show_nsfw = user_show_nsfw(&local_user_view); let show_nsfw = local_user_view.as_ref().map(|t| t.local_user.show_nsfw);
let show_bot_accounts = user_show_bot_accounts(&local_user_view); let show_bot_accounts = local_user_view
.as_ref()
.map(|t| t.local_user.show_bot_accounts);
let show_read_posts = local_user_view
.as_ref()
.map(|t| t.local_user.show_read_posts);
let person_id = local_user_view.map(|u| u.person.id); let person_id = local_user_view.map(|u| u.person.id);
@ -173,6 +176,7 @@ impl Perform for Search {
.sort(sort) .sort(sort)
.show_nsfw(show_nsfw) .show_nsfw(show_nsfw)
.show_bot_accounts(show_bot_accounts) .show_bot_accounts(show_bot_accounts)
.show_read_posts(show_read_posts)
.listing_type(listing_type) .listing_type(listing_type)
.community_id(community_id) .community_id(community_id)
.community_name(community_name) .community_name(community_name)
@ -236,6 +240,7 @@ impl Perform for Search {
.sort(sort) .sort(sort)
.show_nsfw(show_nsfw) .show_nsfw(show_nsfw)
.show_bot_accounts(show_bot_accounts) .show_bot_accounts(show_bot_accounts)
.show_read_posts(show_read_posts)
.listing_type(listing_type) .listing_type(listing_type)
.community_id(community_id) .community_id(community_id)
.community_name(community_name) .community_name(community_name)
@ -307,6 +312,7 @@ impl Perform for Search {
.sort(sort) .sort(sort)
.show_nsfw(show_nsfw) .show_nsfw(show_nsfw)
.show_bot_accounts(show_bot_accounts) .show_bot_accounts(show_bot_accounts)
.show_read_posts(show_read_posts)
.listing_type(listing_type) .listing_type(listing_type)
.my_person_id(person_id) .my_person_id(person_id)
.community_id(community_id) .community_id(community_id)

View File

@ -14,6 +14,7 @@ use lemmy_db_queries::{
}, },
Crud, Crud,
DbPool, DbPool,
Readable,
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
source::{ source::{
@ -21,7 +22,7 @@ use lemmy_db_schema::{
community::{Community, CommunityModerator}, community::{Community, CommunityModerator},
person::Person, person::Person,
person_mention::{PersonMention, PersonMentionForm}, person_mention::{PersonMention, PersonMentionForm},
post::Post, post::{Post, PostRead, PostReadForm},
site::Site, site::Site,
}, },
CommunityId, CommunityId,
@ -236,28 +237,26 @@ pub fn is_admin(local_user_view: &LocalUserView) -> Result<(), LemmyError> {
Ok(()) Ok(())
} }
/// A helper method for showing the bot account
pub fn user_show_bot_accounts(local_user_view: &Option<LocalUserView>) -> bool {
match local_user_view {
Some(uv) => uv.to_owned().local_user.show_bot_accounts,
None => true,
}
}
/// A helper method for showing nsfw
pub fn user_show_nsfw(local_user_view: &Option<LocalUserView>) -> bool {
match &local_user_view {
Some(uv) => uv.local_user.show_nsfw,
None => false,
}
}
pub async fn get_post(post_id: PostId, pool: &DbPool) -> Result<Post, LemmyError> { pub async fn get_post(post_id: PostId, pool: &DbPool) -> Result<Post, LemmyError> {
blocking(pool, move |conn| Post::read(conn, post_id)) blocking(pool, move |conn| Post::read(conn, post_id))
.await? .await?
.map_err(|_| ApiError::err("couldnt_find_post").into()) .map_err(|_| ApiError::err("couldnt_find_post").into())
} }
pub async fn mark_post_as_read(
person_id: PersonId,
post_id: PostId,
pool: &DbPool,
) -> Result<PostRead, LemmyError> {
let post_read_form = PostReadForm { post_id, person_id };
blocking(pool, move |conn| {
PostRead::mark_as_read(conn, &post_read_form)
})
.await?
.map_err(|_| ApiError::err("couldnt_mark_post_as_read").into())
}
pub async fn get_local_user_view_from_jwt( pub async fn get_local_user_view_from_jwt(
jwt: &str, jwt: &str,
pool: &DbPool, pool: &DbPool,

View File

@ -62,6 +62,7 @@ pub struct SaveUserSettings {
pub send_notifications_to_email: Option<bool>, pub send_notifications_to_email: Option<bool>,
pub bot_account: Option<bool>, pub bot_account: Option<bool>,
pub show_bot_accounts: Option<bool>, pub show_bot_accounts: Option<bool>,
pub show_read_posts: Option<bool>,
pub auth: String, pub auth: String,
} }

View File

@ -1,11 +1,6 @@
use crate::PerformCrud; use crate::PerformCrud;
use actix_web::web::Data; use actix_web::web::Data;
use lemmy_api_common::{ use lemmy_api_common::{blocking, comment::*, get_local_user_view_from_jwt_opt};
blocking,
comment::*,
get_local_user_view_from_jwt_opt,
user_show_bot_accounts,
};
use lemmy_db_queries::{from_opt_str_to_opt_enum, ListingType, SortType}; use lemmy_db_queries::{from_opt_str_to_opt_enum, ListingType, SortType};
use lemmy_db_views::comment_view::CommentQueryBuilder; use lemmy_db_views::comment_view::CommentQueryBuilder;
use lemmy_utils::{ApiError, ConnectionId, LemmyError}; use lemmy_utils::{ApiError, ConnectionId, LemmyError};
@ -23,7 +18,9 @@ impl PerformCrud for GetComments {
let data: &GetComments = &self; let data: &GetComments = &self;
let local_user_view = get_local_user_view_from_jwt_opt(&data.auth, context.pool()).await?; let local_user_view = get_local_user_view_from_jwt_opt(&data.auth, context.pool()).await?;
let show_bot_accounts = user_show_bot_accounts(&local_user_view); let show_bot_accounts = local_user_view
.as_ref()
.map(|t| t.local_user.show_bot_accounts);
let person_id = local_user_view.map(|u| u.person.id); let person_id = local_user_view.map(|u| u.person.id);
let sort: Option<SortType> = from_opt_str_to_opt_enum(&data.sort); let sort: Option<SortType> = from_opt_str_to_opt_enum(&data.sort);

View File

@ -1,6 +1,12 @@
use crate::PerformCrud; use crate::PerformCrud;
use actix_web::web::Data; use actix_web::web::Data;
use lemmy_api_common::{blocking, check_community_ban, get_local_user_view_from_jwt, post::*}; use lemmy_api_common::{
blocking,
check_community_ban,
get_local_user_view_from_jwt,
mark_post_as_read,
post::*,
};
use lemmy_apub::{generate_apub_endpoint, ApubLikeableType, ApubObjectType, EndpointType}; use lemmy_apub::{generate_apub_endpoint, ApubLikeableType, ApubObjectType, EndpointType};
use lemmy_db_queries::{source::post::Post_, Crud, Likeable}; use lemmy_db_queries::{source::post::Post_, Crud, Likeable};
use lemmy_db_schema::source::post::*; use lemmy_db_schema::source::post::*;
@ -81,9 +87,11 @@ impl PerformCrud for CreatePost {
.await?; .await?;
// They like their own post by default // They like their own post by default
let person_id = local_user_view.person.id;
let post_id = inserted_post.id;
let like_form = PostLikeForm { let like_form = PostLikeForm {
post_id: inserted_post.id, post_id,
person_id: local_user_view.person.id, person_id,
score: 1, score: 1,
}; };
@ -92,6 +100,9 @@ impl PerformCrud for CreatePost {
return Err(ApiError::err("couldnt_like_post").into()); return Err(ApiError::err("couldnt_like_post").into());
} }
// Mark the post as read
mark_post_as_read(person_id, post_id, context.pool()).await?;
updated_post updated_post
.send_like(&local_user_view.person, context) .send_like(&local_user_view.person, context)
.await?; .await?;

View File

@ -1,12 +1,6 @@
use crate::PerformCrud; use crate::PerformCrud;
use actix_web::web::Data; use actix_web::web::Data;
use lemmy_api_common::{ use lemmy_api_common::{blocking, get_local_user_view_from_jwt_opt, mark_post_as_read, post::*};
blocking,
get_local_user_view_from_jwt_opt,
post::*,
user_show_bot_accounts,
user_show_nsfw,
};
use lemmy_db_queries::{from_opt_str_to_opt_enum, ListingType, SortType}; use lemmy_db_queries::{from_opt_str_to_opt_enum, ListingType, SortType};
use lemmy_db_views::{ use lemmy_db_views::{
comment_view::CommentQueryBuilder, comment_view::CommentQueryBuilder,
@ -31,8 +25,9 @@ impl PerformCrud for GetPost {
let data: &GetPost = &self; let data: &GetPost = &self;
let local_user_view = get_local_user_view_from_jwt_opt(&data.auth, context.pool()).await?; let local_user_view = get_local_user_view_from_jwt_opt(&data.auth, context.pool()).await?;
let show_bot_accounts = user_show_bot_accounts(&local_user_view); let show_bot_accounts = local_user_view
.as_ref()
.map(|t| t.local_user.show_bot_accounts);
let person_id = local_user_view.map(|u| u.person.id); let person_id = local_user_view.map(|u| u.person.id);
let id = data.id; let id = data.id;
@ -42,6 +37,11 @@ impl PerformCrud for GetPost {
.await? .await?
.map_err(|_| ApiError::err("couldnt_find_post"))?; .map_err(|_| ApiError::err("couldnt_find_post"))?;
// Mark the post as read
if let Some(person_id) = person_id {
mark_post_as_read(person_id, id, context.pool()).await?;
}
let id = data.id; let id = data.id;
let comments = blocking(context.pool(), move |conn| { let comments = blocking(context.pool(), move |conn| {
CommentQueryBuilder::create(conn) CommentQueryBuilder::create(conn)
@ -97,8 +97,13 @@ impl PerformCrud for GetPosts {
let person_id = local_user_view.to_owned().map(|l| l.person.id); let person_id = local_user_view.to_owned().map(|l| l.person.id);
let show_nsfw = user_show_nsfw(&local_user_view); let show_nsfw = local_user_view.as_ref().map(|t| t.local_user.show_nsfw);
let show_bot_accounts = user_show_bot_accounts(&local_user_view); let show_bot_accounts = local_user_view
.as_ref()
.map(|t| t.local_user.show_bot_accounts);
let show_read_posts = local_user_view
.as_ref()
.map(|t| t.local_user.show_read_posts);
let sort: Option<SortType> = from_opt_str_to_opt_enum(&data.sort); let sort: Option<SortType> = from_opt_str_to_opt_enum(&data.sort);
let listing_type: Option<ListingType> = from_opt_str_to_opt_enum(&data.type_); let listing_type: Option<ListingType> = from_opt_str_to_opt_enum(&data.type_);
@ -115,6 +120,7 @@ impl PerformCrud for GetPosts {
.sort(sort) .sort(sort)
.show_nsfw(show_nsfw) .show_nsfw(show_nsfw)
.show_bot_accounts(show_bot_accounts) .show_bot_accounts(show_bot_accounts)
.show_read_posts(show_read_posts)
.community_id(community_id) .community_id(community_id)
.community_name(community_name) .community_name(community_name)
.saved_only(saved_only) .saved_only(saved_only)

View File

@ -130,6 +130,7 @@ impl PerformCrud for Register {
lang: Some("browser".into()), lang: Some("browser".into()),
show_avatars: Some(true), show_avatars: Some(true),
show_scores: Some(true), show_scores: Some(true),
show_read_posts: Some(true),
send_notifications_to_email: Some(false), send_notifications_to_email: Some(false),
}; };

View File

@ -1,12 +1,6 @@
use crate::PerformCrud; use crate::PerformCrud;
use actix_web::web::Data; use actix_web::web::Data;
use lemmy_api_common::{ use lemmy_api_common::{blocking, get_local_user_view_from_jwt_opt, person::*};
blocking,
get_local_user_view_from_jwt_opt,
person::*,
user_show_bot_accounts,
user_show_nsfw,
};
use lemmy_db_queries::{from_opt_str_to_opt_enum, source::person::Person_, SortType}; use lemmy_db_queries::{from_opt_str_to_opt_enum, source::person::Person_, SortType};
use lemmy_db_schema::source::person::*; use lemmy_db_schema::source::person::*;
use lemmy_db_views::{comment_view::CommentQueryBuilder, post_view::PostQueryBuilder}; use lemmy_db_views::{comment_view::CommentQueryBuilder, post_view::PostQueryBuilder};
@ -30,8 +24,13 @@ impl PerformCrud for GetPersonDetails {
let data: &GetPersonDetails = &self; let data: &GetPersonDetails = &self;
let local_user_view = get_local_user_view_from_jwt_opt(&data.auth, context.pool()).await?; let local_user_view = get_local_user_view_from_jwt_opt(&data.auth, context.pool()).await?;
let show_nsfw = user_show_nsfw(&local_user_view); let show_nsfw = local_user_view.as_ref().map(|t| t.local_user.show_nsfw);
let show_bot_accounts = user_show_bot_accounts(&local_user_view); let show_bot_accounts = local_user_view
.as_ref()
.map(|t| t.local_user.show_bot_accounts);
let show_read_posts = local_user_view
.as_ref()
.map(|t| t.local_user.show_read_posts);
let sort: Option<SortType> = from_opt_str_to_opt_enum(&data.sort); let sort: Option<SortType> = from_opt_str_to_opt_enum(&data.sort);
@ -71,6 +70,7 @@ impl PerformCrud for GetPersonDetails {
.sort(sort) .sort(sort)
.show_nsfw(show_nsfw) .show_nsfw(show_nsfw)
.show_bot_accounts(show_bot_accounts) .show_bot_accounts(show_bot_accounts)
.show_read_posts(show_read_posts)
.saved_only(saved_only) .saved_only(saved_only)
.community_id(community_id) .community_id(community_id)
.my_person_id(person_id) .my_person_id(person_id)

View File

@ -26,6 +26,7 @@ mod safe_settings_type {
validator_time, validator_time,
show_bot_accounts, show_bot_accounts,
show_scores, show_scores,
show_read_posts,
); );
impl ToSafeSettings for LocalUser { impl ToSafeSettings for LocalUser {
@ -47,6 +48,7 @@ mod safe_settings_type {
validator_time, validator_time,
show_bot_accounts, show_bot_accounts,
show_scores, show_scores,
show_read_posts,
) )
} }
} }

View File

@ -243,6 +243,9 @@ impl Readable<PostReadForm> for PostRead {
use lemmy_db_schema::schema::post_read::dsl::*; use lemmy_db_schema::schema::post_read::dsl::*;
insert_into(post_read) insert_into(post_read)
.values(post_read_form) .values(post_read_form)
.on_conflict((post_id, person_id))
.do_update()
.set(post_read_form)
.get_result::<Self>(conn) .get_result::<Self>(conn)
} }

View File

@ -155,6 +155,7 @@ table! {
validator_time -> Timestamp, validator_time -> Timestamp,
show_bot_accounts -> Bool, show_bot_accounts -> Bool,
show_scores -> Bool, show_scores -> Bool,
show_read_posts -> Bool,
} }
} }

View File

@ -18,6 +18,7 @@ pub struct LocalUser {
pub validator_time: chrono::NaiveDateTime, pub validator_time: chrono::NaiveDateTime,
pub show_bot_accounts: bool, pub show_bot_accounts: bool,
pub show_scores: bool, pub show_scores: bool,
pub show_read_posts: bool,
} }
// TODO redo these, check table defaults // TODO redo these, check table defaults
@ -36,6 +37,7 @@ pub struct LocalUserForm {
pub send_notifications_to_email: Option<bool>, pub send_notifications_to_email: Option<bool>,
pub show_bot_accounts: Option<bool>, pub show_bot_accounts: Option<bool>,
pub show_scores: Option<bool>, pub show_scores: Option<bool>,
pub show_read_posts: Option<bool>,
} }
/// A local user view that removes password encrypted /// A local user view that removes password encrypted
@ -55,4 +57,5 @@ pub struct LocalUserSettings {
pub validator_time: chrono::NaiveDateTime, pub validator_time: chrono::NaiveDateTime,
pub show_bot_accounts: bool, pub show_bot_accounts: bool,
pub show_scores: bool, pub show_scores: bool,
pub show_read_posts: bool,
} }

View File

@ -165,8 +165,8 @@ pub struct PostQueryBuilder<'a> {
url_search: Option<String>, url_search: Option<String>,
show_nsfw: Option<bool>, show_nsfw: Option<bool>,
show_bot_accounts: Option<bool>, show_bot_accounts: Option<bool>,
show_read_posts: Option<bool>,
saved_only: Option<bool>, saved_only: Option<bool>,
unread_only: Option<bool>,
page: Option<i64>, page: Option<i64>,
limit: Option<i64>, limit: Option<i64>,
} }
@ -185,8 +185,8 @@ impl<'a> PostQueryBuilder<'a> {
url_search: None, url_search: None,
show_nsfw: None, show_nsfw: None,
show_bot_accounts: None, show_bot_accounts: None,
show_read_posts: None,
saved_only: None, saved_only: None,
unread_only: None,
page: None, page: None,
limit: None, limit: None,
} }
@ -242,6 +242,11 @@ impl<'a> PostQueryBuilder<'a> {
self self
} }
pub fn show_read_posts<T: MaybeOptional<bool>>(mut self, show_read_posts: T) -> Self {
self.show_read_posts = show_read_posts.get_optional();
self
}
pub fn saved_only<T: MaybeOptional<bool>>(mut self, saved_only: T) -> Self { pub fn saved_only<T: MaybeOptional<bool>>(mut self, saved_only: T) -> Self {
self.saved_only = saved_only.get_optional(); self.saved_only = saved_only.get_optional();
self self
@ -364,12 +369,12 @@ impl<'a> PostQueryBuilder<'a> {
query = query.filter(person::bot_account.eq(false)); query = query.filter(person::bot_account.eq(false));
}; };
if self.saved_only.unwrap_or(false) { if !self.show_read_posts.unwrap_or(true) {
query = query.filter(post_saved::id.is_not_null()); query = query.filter(post_read::id.is_null());
}; };
if self.unread_only.unwrap_or(false) { if self.saved_only.unwrap_or(false) {
query = query.filter(post_read::id.is_not_null()); query = query.filter(post_saved::id.is_not_null());
}; };
query = match self.sort.unwrap_or(SortType::Hot) { query = match self.sort.unwrap_or(SortType::Hot) {

View File

@ -230,11 +230,13 @@ fn get_feed_front(
let local_user = LocalUser::read(&conn, local_user_id)?; let local_user = LocalUser::read(&conn, local_user_id)?;
let person_id = local_user.person_id; let person_id = local_user.person_id;
let show_bot_accounts = local_user.show_bot_accounts; let show_bot_accounts = local_user.show_bot_accounts;
let show_read_posts = local_user.show_read_posts;
let posts = PostQueryBuilder::create(&conn) let posts = PostQueryBuilder::create(&conn)
.listing_type(ListingType::Subscribed) .listing_type(ListingType::Subscribed)
.my_person_id(person_id) .my_person_id(person_id)
.show_bot_accounts(show_bot_accounts) .show_bot_accounts(show_bot_accounts)
.show_read_posts(show_read_posts)
.sort(*sort_type) .sort(*sort_type)
.list()?; .list()?;

View File

@ -0,0 +1 @@
alter table local_user drop column show_read_posts;

View File

@ -0,0 +1 @@
alter table local_user add column show_read_posts boolean default true not null;