]> Untitled Git - lemmy.git/commitdiff
Add show_read_posts filter. Fixes #1561
authorDessalines <tyhou13@gmx.com>
Sat, 24 Apr 2021 22:26:50 +0000 (18:26 -0400)
committerDessalines <tyhou13@gmx.com>
Sat, 24 Apr 2021 22:26:50 +0000 (18:26 -0400)
17 files changed:
crates/api/src/local_user.rs
crates/api/src/post.rs
crates/api/src/site.rs
crates/api_common/src/lib.rs
crates/api_common/src/person.rs
crates/api_crud/src/post/create.rs
crates/api_crud/src/post/read.rs
crates/api_crud/src/user/create.rs
crates/api_crud/src/user/read.rs
crates/db_queries/src/source/local_user.rs
crates/db_queries/src/source/post.rs
crates/db_schema/src/schema.rs
crates/db_schema/src/source/local_user.rs
crates/db_views/src/post_view.rs
crates/routes/src/feeds.rs
migrations/2021-04-24-174047_add_show_read_post_setting/down.sql [new file with mode: 0644]
migrations/2021-04-24-174047_add_show_read_post_setting/up.sql [new file with mode: 0644]

index b273b58208c888b56dc98b9322e92ec67b248709..c0ca8e32e8f460ca0364999495345d02ca92ec18 100644 (file)
@@ -240,6 +240,7 @@ impl Perform for SaveUserSettings {
       default_listing_type,
       lang: data.lang.to_owned(),
       show_avatars: data.show_avatars,
+      show_read_posts: data.show_read_posts,
       send_notifications_to_email: data.send_notifications_to_email,
     };
 
index 34c9c4116a1bc09bd113cbe70cd2250496cf71da..f0e4d91faced00535d55aabd64e9c8a97e909762 100644 (file)
@@ -6,6 +6,7 @@ use lemmy_api_common::{
   check_downvotes_enabled,
   get_local_user_view_from_jwt,
   is_mod_or_admin,
+  mark_post_as_read,
   post::*,
 };
 use lemmy_apub::{ApubLikeableType, ApubObjectType};
@@ -69,6 +70,9 @@ impl Perform for CreatePostLike {
         .await?;
     }
 
+    // Mark the post as read
+    mark_post_as_read(person_id, post_id, context.pool()).await?;
+
     let post_id = data.post_id;
     let person_id = local_user_view.person.id;
     let post_view = blocking(context.pool(), move |conn| {
@@ -269,6 +273,9 @@ impl Perform for SavePost {
     })
     .await??;
 
+    // Mark the post as read
+    mark_post_as_read(person_id, post_id, context.pool()).await?;
+
     Ok(PostResponse { post_view })
   }
 }
index aae400ca7464188a48ef8778567280ac4b5cc140..fa8dd5e4c76e9cd0bb00fbd02455287780c86b39 100644 (file)
@@ -11,6 +11,7 @@ use lemmy_api_common::{
   site::*,
   user_show_bot_accounts,
   user_show_nsfw,
+  user_show_read_posts,
 };
 use lemmy_apub::fetcher::search::search_by_apub_id;
 use lemmy_db_queries::{source::site::Site_, Crud, SearchType, SortType};
@@ -141,6 +142,7 @@ impl Perform for Search {
 
     let show_nsfw = user_show_nsfw(&local_user_view);
     let show_bot_accounts = user_show_bot_accounts(&local_user_view);
+    let show_read_posts = user_show_read_posts(&local_user_view);
 
     let person_id = local_user_view.map(|u| u.person.id);
 
@@ -166,6 +168,7 @@ impl Perform for Search {
             .sort(&sort)
             .show_nsfw(show_nsfw)
             .show_bot_accounts(show_bot_accounts)
+            .show_read_posts(show_read_posts)
             .community_id(community_id)
             .community_name(community_name)
             .my_person_id(person_id)
@@ -218,6 +221,7 @@ impl Perform for Search {
             .sort(&sort)
             .show_nsfw(show_nsfw)
             .show_bot_accounts(show_bot_accounts)
+            .show_read_posts(show_read_posts)
             .community_id(community_id)
             .community_name(community_name)
             .my_person_id(person_id)
@@ -276,6 +280,7 @@ impl Perform for Search {
             .sort(&sort)
             .show_nsfw(show_nsfw)
             .show_bot_accounts(show_bot_accounts)
+            .show_read_posts(show_read_posts)
             .my_person_id(person_id)
             .community_id(community_id)
             .community_name(community_name)
index 1a644b7d4d775745de63bb1d14fe1c604b9e54cb..47f4752dd489acaafe3c0f82831fb4f87501d7e0 100644 (file)
@@ -14,6 +14,7 @@ use lemmy_db_queries::{
   },
   Crud,
   DbPool,
+  Readable,
 };
 use lemmy_db_schema::{
   source::{
@@ -21,7 +22,7 @@ use lemmy_db_schema::{
     community::{Community, CommunityModerator},
     person::Person,
     person_mention::{PersonMention, PersonMentionForm},
-    post::Post,
+    post::{Post, PostRead, PostReadForm},
     site::Site,
   },
   CommunityId,
@@ -252,12 +253,34 @@ pub fn user_show_nsfw(local_user_view: &Option<LocalUserView>) -> bool {
   }
 }
 
+/// A helper method for showing read posts
+pub fn user_show_read_posts(local_user_view: &Option<LocalUserView>) -> bool {
+  match local_user_view {
+    Some(uv) => uv.to_owned().local_user.show_read_posts,
+    None => true,
+  }
+}
+
 pub async fn get_post(post_id: PostId, pool: &DbPool) -> Result<Post, LemmyError> {
   blocking(pool, move |conn| Post::read(conn, post_id))
     .await?
     .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(
   jwt: &str,
   pool: &DbPool,
index 5473a482017459fd124fae95b956459a5e159d8b..f8fbe5d0d1812d749f0e262c8f844d0bc1d6bf83 100644 (file)
@@ -62,6 +62,7 @@ pub struct SaveUserSettings {
   pub send_notifications_to_email: Option<bool>,
   pub bot_account: Option<bool>,
   pub show_bot_accounts: Option<bool>,
+  pub show_read_posts: Option<bool>,
   pub auth: String,
 }
 
index 3a2311a768e751b51ce115c70753f7f2a78f9a89..835518a14b517569f687b4f5eb440c32ad11d675 100644 (file)
@@ -1,6 +1,12 @@
 use crate::PerformCrud;
 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_db_queries::{source::post::Post_, Crud, Likeable};
 use lemmy_db_schema::source::post::*;
@@ -81,9 +87,11 @@ impl PerformCrud for CreatePost {
       .await?;
 
     // 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 {
-      post_id: inserted_post.id,
-      person_id: local_user_view.person.id,
+      post_id,
+      person_id,
       score: 1,
     };
 
@@ -92,6 +100,9 @@ impl PerformCrud for CreatePost {
       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
       .send_like(&local_user_view.person, context)
       .await?;
index d2231d24b5e2cfe5ad6838a186ded5f74a807538..d44f029a0973b07a801fe88969fe4e0707d3b158 100644 (file)
@@ -3,9 +3,11 @@ use actix_web::web::Data;
 use lemmy_api_common::{
   blocking,
   get_local_user_view_from_jwt_opt,
+  mark_post_as_read,
   post::*,
   user_show_bot_accounts,
   user_show_nsfw,
+  user_show_read_posts,
 };
 use lemmy_db_queries::{ListingType, SortType};
 use lemmy_db_views::{
@@ -43,6 +45,11 @@ impl PerformCrud for GetPost {
     .await?
     .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 comments = blocking(context.pool(), move |conn| {
       CommentQueryBuilder::create(conn)
@@ -100,6 +107,7 @@ impl PerformCrud for GetPosts {
 
     let show_nsfw = user_show_nsfw(&local_user_view);
     let show_bot_accounts = user_show_bot_accounts(&local_user_view);
+    let show_read_posts = user_show_read_posts(&local_user_view);
 
     let type_ = ListingType::from_str(&data.type_)?;
     let sort = SortType::from_str(&data.sort)?;
@@ -116,6 +124,7 @@ impl PerformCrud for GetPosts {
         .sort(&sort)
         .show_nsfw(show_nsfw)
         .show_bot_accounts(show_bot_accounts)
+        .show_read_posts(show_read_posts)
         .community_id(community_id)
         .community_name(community_name)
         .saved_only(saved_only)
index 0975555617d7d4f64d6102e5ccd1fe8c1a7c031c..d7aae65f024d80dd41f43b41a21ddb895b5cad3f 100644 (file)
@@ -130,6 +130,7 @@ impl PerformCrud for Register {
       lang: Some("browser".into()),
       show_avatars: Some(true),
       show_scores: Some(true),
+      show_read_posts: Some(true),
       send_notifications_to_email: Some(false),
     };
 
index 24ac2f61e3610ea2d55543a756a7d3b74510c743..e0e3808c17ced23885055b17c034fe1caae0fbf2 100644 (file)
@@ -6,6 +6,7 @@ use lemmy_api_common::{
   person::*,
   user_show_bot_accounts,
   user_show_nsfw,
+  user_show_read_posts,
 };
 use lemmy_db_queries::{source::person::Person_, SortType};
 use lemmy_db_schema::source::person::*;
@@ -33,6 +34,7 @@ impl PerformCrud for GetPersonDetails {
 
     let show_nsfw = user_show_nsfw(&local_user_view);
     let show_bot_accounts = user_show_bot_accounts(&local_user_view);
+    let show_read_posts = user_show_read_posts(&local_user_view);
 
     let sort = SortType::from_str(&data.sort)?;
 
@@ -72,6 +74,7 @@ impl PerformCrud for GetPersonDetails {
         .sort(&sort)
         .show_nsfw(show_nsfw)
         .show_bot_accounts(show_bot_accounts)
+        .show_read_posts(show_read_posts)
         .saved_only(saved_only)
         .community_id(community_id)
         .my_person_id(person_id)
index 43a5c7727565fd60a66833a116569c777915f159..2cd347e5cba7533ca72900c303c688908bda5c34 100644 (file)
@@ -26,6 +26,7 @@ mod safe_settings_type {
     validator_time,
     show_bot_accounts,
     show_scores,
+    show_read_posts,
   );
 
   impl ToSafeSettings for LocalUser {
@@ -47,6 +48,7 @@ mod safe_settings_type {
         validator_time,
         show_bot_accounts,
         show_scores,
+        show_read_posts,
       )
     }
   }
index 0205dc954063836878fd78eb8e74b9e6e8c15e13..9f2d2f9b396c162dbf239bcf02c1e74f2fc613ff 100644 (file)
@@ -243,6 +243,9 @@ impl Readable<PostReadForm> for PostRead {
     use lemmy_db_schema::schema::post_read::dsl::*;
     insert_into(post_read)
       .values(post_read_form)
+      .on_conflict((post_id, person_id))
+      .do_update()
+      .set(post_read_form)
       .get_result::<Self>(conn)
   }
 
index a700839818a8b3ae406e0c0334b73c0094a5ad67..4a52b310d4efe243ebd954b9396a3f135d3b99f1 100644 (file)
@@ -155,6 +155,7 @@ table! {
         validator_time -> Timestamp,
         show_bot_accounts -> Bool,
         show_scores -> Bool,
+        show_read_posts -> Bool,
     }
 }
 
index 64afb49a425b5e2c34eec5ed618120aac0a08b48..77e0c0ceb437aba4c738a66fa0945a300e6cd470 100644 (file)
@@ -18,6 +18,7 @@ pub struct LocalUser {
   pub validator_time: chrono::NaiveDateTime,
   pub show_bot_accounts: bool,
   pub show_scores: bool,
+  pub show_read_posts: bool,
 }
 
 // TODO redo these, check table defaults
@@ -36,6 +37,7 @@ pub struct LocalUserForm {
   pub send_notifications_to_email: Option<bool>,
   pub show_bot_accounts: Option<bool>,
   pub show_scores: Option<bool>,
+  pub show_read_posts: Option<bool>,
 }
 
 /// A local user view that removes password encrypted
@@ -55,4 +57,5 @@ pub struct LocalUserSettings {
   pub validator_time: chrono::NaiveDateTime,
   pub show_bot_accounts: bool,
   pub show_scores: bool,
+  pub show_read_posts: bool,
 }
index 8248bfd292d19c585b360088250f1a764ba39fce..8a533412f7da77abfbbf2925dcf0afcac427c11e 100644 (file)
@@ -165,8 +165,8 @@ pub struct PostQueryBuilder<'a> {
   url_search: Option<String>,
   show_nsfw: bool,
   show_bot_accounts: bool,
+  show_read_posts: bool,
   saved_only: bool,
-  unread_only: bool,
   page: Option<i64>,
   limit: Option<i64>,
 }
@@ -185,8 +185,8 @@ impl<'a> PostQueryBuilder<'a> {
       url_search: None,
       show_nsfw: true,
       show_bot_accounts: true,
+      show_read_posts: true,
       saved_only: false,
-      unread_only: false,
       page: None,
       limit: None,
     }
@@ -242,6 +242,11 @@ impl<'a> PostQueryBuilder<'a> {
     self
   }
 
+  pub fn show_read_posts(mut self, show_read_posts: bool) -> Self {
+    self.show_read_posts = show_read_posts;
+    self
+  }
+
   pub fn saved_only(mut self, saved_only: bool) -> Self {
     self.saved_only = saved_only;
     self
@@ -362,13 +367,12 @@ impl<'a> PostQueryBuilder<'a> {
       query = query.filter(person::bot_account.eq(false));
     };
 
-    // TODO  These two might be wrong
     if self.saved_only {
       query = query.filter(post_saved::id.is_not_null());
     };
 
-    if self.unread_only {
-      query = query.filter(post_read::id.is_not_null());
+    if !self.show_read_posts {
+      query = query.filter(post_read::id.is_null());
     };
 
     query = match self.sort {
index 21263fd610e1aa69639e7150ba468b571bf6d714..2ed586737f3e9e688d3eb36119a09af08bd37ebe 100644 (file)
@@ -231,11 +231,13 @@ fn get_feed_front(
   let local_user = LocalUser::read(&conn, local_user_id)?;
   let person_id = local_user.person_id;
   let show_bot_accounts = local_user.show_bot_accounts;
+  let show_read_posts = local_user.show_read_posts;
 
   let posts = PostQueryBuilder::create(&conn)
     .listing_type(&ListingType::Subscribed)
     .my_person_id(person_id)
     .show_bot_accounts(show_bot_accounts)
+    .show_read_posts(show_read_posts)
     .sort(sort_type)
     .list()?;
 
diff --git a/migrations/2021-04-24-174047_add_show_read_post_setting/down.sql b/migrations/2021-04-24-174047_add_show_read_post_setting/down.sql
new file mode 100644 (file)
index 0000000..2aa2751
--- /dev/null
@@ -0,0 +1 @@
+alter table local_user drop column show_read_posts;
diff --git a/migrations/2021-04-24-174047_add_show_read_post_setting/up.sql b/migrations/2021-04-24-174047_add_show_read_post_setting/up.sql
new file mode 100644 (file)
index 0000000..74b5398
--- /dev/null
@@ -0,0 +1 @@
+alter table local_user add column show_read_posts boolean default true not null;