From 7929e7760268ef2fa247f56489313cb0980fb7b6 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Tue, 26 Mar 2024 10:46:37 -0400 Subject: [PATCH] Fixing issue with comment replies wrongly marked as read. (#4567) * Fixing issue with comment replies wrongly marked as read. - Fixes #4566 * Elaborating on a comment. --- crates/api_crud/src/comment/create.rs | 10 ++++-- crates/db_schema/src/impls/comment_reply.rs | 33 ++++++++++++++------ crates/db_schema/src/impls/person_mention.rs | 22 ++++++------- 3 files changed, 41 insertions(+), 24 deletions(-) diff --git a/crates/api_crud/src/comment/create.rs b/crates/api_crud/src/comment/create.rs index 9269ec382..acb386c60 100644 --- a/crates/api_crud/src/comment/create.rs +++ b/crates/api_crud/src/comment/create.rs @@ -164,10 +164,15 @@ pub async fn create_comment( ) .await?; - // If its a reply, mark the parent as read + // If we're responding to a comment where we're the recipient, + // (ie we're the grandparent, or the recipient of the parent comment_reply), + // then mark the parent as read. + // Then we don't have to do it manually after we respond to a comment. if let Some(parent) = parent_opt { + let person_id = local_user_view.person.id; let parent_id = parent.id; - let comment_reply = CommentReply::read_by_comment(&mut context.pool(), parent_id).await; + let comment_reply = + CommentReply::read_by_comment_and_person(&mut context.pool(), parent_id, person_id).await; if let Ok(reply) = comment_reply { CommentReply::update( &mut context.pool(), @@ -179,7 +184,6 @@ pub async fn create_comment( } // If the parent has PersonMentions mark them as read too - let person_id = local_user_view.person.id; let person_mention = PersonMention::read_by_comment_and_person(&mut context.pool(), parent_id, person_id).await; if let Ok(mention) = person_mention { diff --git a/crates/db_schema/src/impls/comment_reply.rs b/crates/db_schema/src/impls/comment_reply.rs index 40c99e045..e6d0915e9 100644 --- a/crates/db_schema/src/impls/comment_reply.rs +++ b/crates/db_schema/src/impls/comment_reply.rs @@ -1,6 +1,6 @@ use crate::{ newtypes::{CommentId, CommentReplyId, PersonId}, - schema::comment_reply::dsl::{comment_id, comment_reply, read, recipient_id}, + schema::comment_reply, source::comment_reply::{CommentReply, CommentReplyInsertForm, CommentReplyUpdateForm}, traits::Crud, utils::{get_conn, DbPool}, @@ -22,9 +22,9 @@ impl Crud for CommentReply { // since the return here isnt utilized, we dont need to do an update // but get_result doesnt return the existing row here - insert_into(comment_reply) + insert_into(comment_reply::table) .values(comment_reply_form) - .on_conflict((recipient_id, comment_id)) + .on_conflict((comment_reply::recipient_id, comment_reply::comment_id)) .do_update() .set(comment_reply_form) .get_result::(conn) @@ -37,7 +37,7 @@ impl Crud for CommentReply { comment_reply_form: &Self::UpdateForm, ) -> Result { let conn = &mut get_conn(pool).await?; - diesel::update(comment_reply.find(comment_reply_id)) + diesel::update(comment_reply::table.find(comment_reply_id)) .set(comment_reply_form) .get_result::(conn) .await @@ -51,11 +51,11 @@ impl CommentReply { ) -> Result, Error> { let conn = &mut get_conn(pool).await?; diesel::update( - comment_reply - .filter(recipient_id.eq(for_recipient_id)) - .filter(read.eq(false)), + comment_reply::table + .filter(comment_reply::recipient_id.eq(for_recipient_id)) + .filter(comment_reply::read.eq(false)), ) - .set(read.eq(true)) + .set(comment_reply::read.eq(true)) .get_results::(conn) .await } @@ -65,8 +65,21 @@ impl CommentReply { for_comment_id: CommentId, ) -> Result { let conn = &mut get_conn(pool).await?; - comment_reply - .filter(comment_id.eq(for_comment_id)) + comment_reply::table + .filter(comment_reply::comment_id.eq(for_comment_id)) + .first::(conn) + .await + } + + pub async fn read_by_comment_and_person( + pool: &mut DbPool<'_>, + for_comment_id: CommentId, + for_recipient_id: PersonId, + ) -> Result { + let conn = &mut get_conn(pool).await?; + comment_reply::table + .filter(comment_reply::comment_id.eq(for_comment_id)) + .filter(comment_reply::recipient_id.eq(for_recipient_id)) .first::(conn) .await } diff --git a/crates/db_schema/src/impls/person_mention.rs b/crates/db_schema/src/impls/person_mention.rs index 9882e662c..5524f87ee 100644 --- a/crates/db_schema/src/impls/person_mention.rs +++ b/crates/db_schema/src/impls/person_mention.rs @@ -1,6 +1,6 @@ use crate::{ newtypes::{CommentId, PersonId, PersonMentionId}, - schema::person_mention::dsl::{comment_id, person_mention, read, recipient_id}, + schema::person_mention, source::person_mention::{PersonMention, PersonMentionInsertForm, PersonMentionUpdateForm}, traits::Crud, utils::{get_conn, DbPool}, @@ -21,9 +21,9 @@ impl Crud for PersonMention { let conn = &mut get_conn(pool).await?; // since the return here isnt utilized, we dont need to do an update // but get_result doesnt return the existing row here - insert_into(person_mention) + insert_into(person_mention::table) .values(person_mention_form) - .on_conflict((recipient_id, comment_id)) + .on_conflict((person_mention::recipient_id, person_mention::comment_id)) .do_update() .set(person_mention_form) .get_result::(conn) @@ -36,7 +36,7 @@ impl Crud for PersonMention { person_mention_form: &Self::UpdateForm, ) -> Result { let conn = &mut get_conn(pool).await?; - diesel::update(person_mention.find(person_mention_id)) + diesel::update(person_mention::table.find(person_mention_id)) .set(person_mention_form) .get_result::(conn) .await @@ -50,11 +50,11 @@ impl PersonMention { ) -> Result, Error> { let conn = &mut get_conn(pool).await?; diesel::update( - person_mention - .filter(recipient_id.eq(for_recipient_id)) - .filter(read.eq(false)), + person_mention::table + .filter(person_mention::recipient_id.eq(for_recipient_id)) + .filter(person_mention::read.eq(false)), ) - .set(read.eq(true)) + .set(person_mention::read.eq(true)) .get_results::(conn) .await } @@ -65,9 +65,9 @@ impl PersonMention { for_recipient_id: PersonId, ) -> Result { let conn = &mut get_conn(pool).await?; - person_mention - .filter(comment_id.eq(for_comment_id)) - .filter(recipient_id.eq(for_recipient_id)) + person_mention::table + .filter(person_mention::comment_id.eq(for_comment_id)) + .filter(person_mention::recipient_id.eq(for_recipient_id)) .first::(conn) .await }