Skip to content

Commit

Permalink
fix: do not handle empty messages
Browse files Browse the repository at this point in the history
  • Loading branch information
boxdot committed Feb 3, 2024
1 parent fbe6148 commit 0db2e54
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 30 deletions.
15 changes: 11 additions & 4 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down
49 changes: 25 additions & 24 deletions src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<Message> {
Some(Message {
from_id: quote.author_aci?.parse().ok()?,
Expand All @@ -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()
}
}
4 changes: 2 additions & 2 deletions src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(());
};

Expand Down

0 comments on commit 0db2e54

Please sign in to comment.