From b6c6a63a39595553d7b5277d65d01160b617f30c Mon Sep 17 00:00:00 2001 From: link2xt Date: Sun, 14 Apr 2024 18:01:07 +0000 Subject: [PATCH] refactor: do not check for is_trash() in get_last_reaction_if_newer_than() `Message::load_from_db_optional` does not return trashed messages anymore. --- src/reaction.rs | 73 ++++++++++++++++++++++++------------------------- 1 file changed, 36 insertions(+), 37 deletions(-) diff --git a/src/reaction.rs b/src/reaction.rs index cd0dce152..6bbf94de1 100644 --- a/src/reaction.rs +++ b/src/reaction.rs @@ -338,44 +338,43 @@ impl Chat { context: &Context, timestamp: i64, ) -> Result> { - if let Some(reaction_timestamp) = self.param.get_i64(Param::LastReactionTimestamp) { - if reaction_timestamp > timestamp { - let reaction_msg_id = MsgId::new( - self.param - .get_int(Param::LastReactionMsgId) - .unwrap_or_default() as u32, - ); - // The message reacted to may be deleted physically (`load_from_db()` fails) or marked as a tombstone (`is_trash()`). - // These are no errors as `Param::LastReaction*` are just weak pointers. - // Instead, just return `Ok(None)` and let the caller create another summary. - if let Some(reaction_msg) = - Message::load_from_db_optional(context, reaction_msg_id).await? - { - if !reaction_msg.chat_id.is_trash() { - let reaction_contact_id = ContactId::new( - self.param - .get_int(Param::LastReactionContactId) - .unwrap_or_default() as u32, - ); - if let Some(reaction) = context - .sql - .query_row_optional( - r#"SELECT reaction FROM reactions WHERE msg_id=? AND contact_id=?"#, - (reaction_msg.id, reaction_contact_id), - |row| { - let reaction: String = row.get(0)?; - Ok(reaction) - }, - ) - .await? - { - return Ok(Some((reaction_msg, reaction_contact_id, reaction))); - } - } - } - } + if self + .param + .get_i64(Param::LastReactionTimestamp) + .filter(|&reaction_timestamp| reaction_timestamp > timestamp) + .is_none() + { + return Ok(None); + }; + let reaction_msg_id = MsgId::new( + self.param + .get_int(Param::LastReactionMsgId) + .unwrap_or_default() as u32, + ); + let Some(reaction_msg) = Message::load_from_db_optional(context, reaction_msg_id).await? + else { + // The message reacted to may be deleted. + // These are no errors as `Param::LastReaction*` are just weak pointers. + // Instead, just return `Ok(None)` and let the caller create another summary. + return Ok(None); + }; + let reaction_contact_id = ContactId::new( + self.param + .get_int(Param::LastReactionContactId) + .unwrap_or_default() as u32, + ); + if let Some(reaction) = context + .sql + .query_get_value( + "SELECT reaction FROM reactions WHERE msg_id=? AND contact_id=?", + (reaction_msg.id, reaction_contact_id), + ) + .await? + { + Ok(Some((reaction_msg, reaction_contact_id, reaction))) + } else { + Ok(None) } - Ok(None) } }