From 0db2e540ae4ffa9fd5f9838e078b5bd34505ee47 Mon Sep 17 00:00:00 2001 From: boxdot Date: Sat, 3 Feb 2024 19:17:40 +0100 Subject: [PATCH] fix: do not handle empty messages --- src/app.rs | 15 +++++++++++---- src/data.rs | 49 +++++++++++++++++++++++++------------------------ src/handlers.rs | 4 ++-- 3 files changed, 38 insertions(+), 30 deletions(-) diff --git a/src/app.rs b/src/app.rs index 1378071..754768f 100644 --- a/src/app.rs +++ b/src/app.rs @@ -12,7 +12,7 @@ use crate::storage::{MessageId, Storage}; use crate::util::{self, LazyRegex, StatefulList, ATTACHMENT_REGEX, URL_REGEX}; use std::io::Cursor; -use anyhow::{anyhow, bail, Context as _}; +use anyhow::{anyhow, Context as _}; use arboard::Clipboard; use chrono::{DateTime, Utc}; use crossterm::event::{KeyCode, KeyEvent, KeyModifiers}; @@ -32,7 +32,7 @@ use presage::proto::{ use presage::proto::{AttachmentPointer, DataMessage, ReceiptMessage, SyncMessage, TypingMessage}; use regex_automata::Regex; use tokio::sync::mpsc; -use tracing::{error, info, warn}; +use tracing::{debug, error, info, warn}; use uuid::Uuid; use std::borrow::Cow; @@ -526,25 +526,32 @@ impl App { .context("sync message with destination without profile key")? .try_into() .map_err(|_| anyhow!("invalid profile key"))?; - let destination_uuid = Uuid::parse_str(&destination_uuid).unwrap(); + let destination_uuid = destination_uuid.parse()?; let name = self.name_by_id(destination_uuid); self.ensure_user_is_known(destination_uuid, profile_key) .await; self.ensure_contact_channel_exists(destination_uuid, &name) .await } else { - bail!("message without a group context and without a destination uuid"); + debug!("dropping a sync message not attached to a channel"); + return Ok(()); }; add_emoji_from_sticker(&mut body, sticker); let quote = quote.and_then(Message::from_quote).map(Box::new); let attachments = self.save_attachments(attachment_pointers).await; let body_ranges = body_ranges.into_iter().filter_map(BodyRange::from_proto); + let message = Message { quote, ..Message::new(user_id, body, body_ranges, timestamp, attachments) }; + if message.is_empty() { + debug!("dropping empty message"); + return Ok(()); + } + (channel_idx, message) } // Incoming direct/group message diff --git a/src/data.rs b/src/data.rs index f167eb6..fd38640 100644 --- a/src/data.rs +++ b/src/data.rs @@ -172,29 +172,6 @@ pub struct Message { pub(crate) edited: bool, } -impl Message { - pub(crate) fn text(from_id: Uuid, arrived_at: u64, message: String) -> Self { - Self { - from_id, - message: Some(message), - arrived_at, - quote: Default::default(), - attachments: Default::default(), - reactions: Default::default(), - receipt: Default::default(), - body_ranges: Default::default(), - send_failed: Default::default(), - edit: Default::default(), - edited: Default::default(), - } - } - - /// Returns whether this message is an edit of an another message - pub(crate) fn is_edit(&self) -> bool { - self.edit.is_some() - } -} - #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub(crate) struct BodyRange { pub(crate) start: u16, @@ -305,6 +282,22 @@ impl Message { } } + pub(crate) fn text(from_id: Uuid, arrived_at: u64, message: String) -> Self { + Self { + from_id, + message: Some(message), + arrived_at, + quote: Default::default(), + attachments: Default::default(), + reactions: Default::default(), + receipt: Default::default(), + body_ranges: Default::default(), + send_failed: Default::default(), + edit: Default::default(), + edited: Default::default(), + } + } + pub fn from_quote(quote: Quote) -> Option { Some(Message { from_id: quote.author_aci?.parse().ok()?, @@ -325,7 +318,15 @@ impl Message { }) } + /// Returns whether this message is an edit of an another message + pub(crate) fn is_edit(&self) -> bool { + self.edit.is_some() + } + pub fn is_empty(&self) -> bool { - self.message.is_none() && self.attachments.is_empty() && self.reactions.is_empty() + self.message.is_none() + && self.attachments.is_empty() + && self.reactions.is_empty() + && self.quote.is_none() } } diff --git a/src/handlers.rs b/src/handlers.rs index ca6c28c..f0de444 100644 --- a/src/handlers.rs +++ b/src/handlers.rs @@ -2,7 +2,7 @@ use anyhow::Context; use presage::libsignal_service::content::Metadata; use presage::proto::sync_message::Sent; use presage::proto::{DataMessage, EditMessage, SyncMessage}; -use tracing::warn; +use tracing::debug; use uuid::Uuid; use crate::app::App; @@ -16,7 +16,7 @@ impl App { sync_message: SyncMessage, ) -> anyhow::Result<()> { let Some(channel_id) = sync_message.channel_id() else { - warn!("dropping a sync message not attached to a channel"); + debug!("dropping a sync message not attached to a channel"); return Ok(()); };