2023-10-06 22:35:27 -07:00
|
|
|
//! # Room Messages
|
2022-12-29 18:00:59 -08:00
|
|
|
use std::borrow::Cow;
|
|
|
|
use std::cmp::{Ord, Ordering, PartialOrd};
|
|
|
|
use std::collections::hash_map::DefaultHasher;
|
2023-10-15 18:12:39 -07:00
|
|
|
use std::collections::hash_set;
|
2022-12-29 18:00:59 -08:00
|
|
|
use std::collections::BTreeMap;
|
2024-08-18 01:31:42 -07:00
|
|
|
use std::convert::{TryFrom, TryInto};
|
2024-08-01 06:02:42 +03:00
|
|
|
use std::fmt::{self, Display};
|
2022-12-29 18:00:59 -08:00
|
|
|
use std::hash::{Hash, Hasher};
|
2024-03-09 00:47:05 -08:00
|
|
|
use std::ops::{Deref, DerefMut};
|
2022-12-29 18:00:59 -08:00
|
|
|
|
2024-08-30 09:08:12 -07:00
|
|
|
use chrono::{DateTime, Local as LocalTz};
|
2024-05-26 01:16:04 +02:00
|
|
|
use humansize::{format_size, DECIMAL};
|
2024-03-02 15:00:29 -08:00
|
|
|
use serde_json::json;
|
2022-12-29 18:00:59 -08:00
|
|
|
use unicode_width::UnicodeWidthStr;
|
|
|
|
|
|
|
|
use matrix_sdk::ruma::{
|
2023-01-13 17:53:54 -08:00
|
|
|
events::{
|
2024-03-09 00:47:05 -08:00
|
|
|
relation::Thread,
|
2023-01-13 17:53:54 -08:00
|
|
|
room::{
|
2023-03-13 16:43:04 -07:00
|
|
|
encrypted::{
|
|
|
|
OriginalRoomEncryptedEvent,
|
|
|
|
RedactedRoomEncryptedEvent,
|
|
|
|
RoomEncryptedEvent,
|
|
|
|
},
|
2023-01-13 17:53:54 -08:00
|
|
|
message::{
|
2023-01-23 17:08:11 -08:00
|
|
|
FormattedBody,
|
|
|
|
MessageFormat,
|
2023-01-13 17:53:54 -08:00
|
|
|
MessageType,
|
|
|
|
OriginalRoomMessageEvent,
|
|
|
|
RedactedRoomMessageEvent,
|
2023-01-23 17:08:11 -08:00
|
|
|
Relation,
|
2023-01-13 17:53:54 -08:00
|
|
|
RoomMessageEvent,
|
|
|
|
RoomMessageEventContent,
|
|
|
|
},
|
|
|
|
redaction::SyncRoomRedactionEvent,
|
|
|
|
},
|
2024-03-02 15:00:29 -08:00
|
|
|
RedactContent,
|
2023-03-13 16:43:04 -07:00
|
|
|
RedactedUnsigned,
|
2022-12-29 18:00:59 -08:00
|
|
|
},
|
2023-01-26 15:40:16 -08:00
|
|
|
EventId,
|
2022-12-29 18:00:59 -08:00
|
|
|
MilliSecondsSinceUnixEpoch,
|
|
|
|
OwnedEventId,
|
|
|
|
OwnedUserId,
|
2023-01-13 17:53:54 -08:00
|
|
|
RoomVersionId,
|
2022-12-29 18:00:59 -08:00
|
|
|
UInt,
|
|
|
|
};
|
|
|
|
|
2024-02-27 21:21:05 -08:00
|
|
|
use ratatui::{
|
2023-01-06 16:56:28 -08:00
|
|
|
style::{Modifier as StyleModifier, Style},
|
2023-01-23 17:08:11 -08:00
|
|
|
symbols::line::THICK_VERTICAL,
|
2023-07-01 08:58:48 +01:00
|
|
|
text::{Line, Span, Text},
|
2022-12-29 18:00:59 -08:00
|
|
|
};
|
|
|
|
|
2024-02-27 21:21:05 -08:00
|
|
|
use modalkit::editing::cursor::Cursor;
|
|
|
|
use modalkit::prelude::*;
|
2023-11-16 08:36:22 -08:00
|
|
|
use ratatui_image::protocol::Protocol;
|
2022-12-29 18:00:59 -08:00
|
|
|
|
2023-11-16 08:36:22 -08:00
|
|
|
use crate::config::ImagePreviewSize;
|
2023-01-06 16:56:28 -08:00
|
|
|
use crate::{
|
2024-03-06 23:49:35 -08:00
|
|
|
base::RoomInfo,
|
2023-01-06 16:56:28 -08:00
|
|
|
config::ApplicationSettings,
|
2023-01-23 17:08:11 -08:00
|
|
|
message::html::{parse_matrix_html, StyleTree},
|
2024-03-24 00:35:10 +01:00
|
|
|
util::{replace_emojis_in_str, space, space_span, take_width, wrapped_text},
|
2023-01-06 16:56:28 -08:00
|
|
|
};
|
2022-12-29 18:00:59 -08:00
|
|
|
|
2024-08-07 22:49:54 -07:00
|
|
|
mod compose;
|
2023-01-23 17:08:11 -08:00
|
|
|
mod html;
|
|
|
|
mod printer;
|
|
|
|
|
2024-08-07 22:49:54 -07:00
|
|
|
pub use self::compose::text_to_message;
|
|
|
|
|
2022-12-29 18:00:59 -08:00
|
|
|
pub type MessageKey = (MessageTimeStamp, OwnedEventId);
|
2024-03-09 00:47:05 -08:00
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct Messages(BTreeMap<MessageKey, Message>);
|
|
|
|
|
|
|
|
impl Deref for Messages {
|
|
|
|
type Target = BTreeMap<MessageKey, Message>;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DerefMut for Messages {
|
|
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
|
|
&mut self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Messages {
|
|
|
|
pub fn insert_message(&mut self, key: MessageKey, msg: impl Into<Message>) {
|
|
|
|
let event_id = key.1.clone();
|
|
|
|
let msg = msg.into();
|
|
|
|
|
|
|
|
self.0.insert(key, msg);
|
|
|
|
|
|
|
|
// Remove any echo.
|
|
|
|
let key = (MessageTimeStamp::LocalEcho, event_id);
|
|
|
|
let _ = self.0.remove(&key);
|
|
|
|
}
|
|
|
|
}
|
2022-12-29 18:00:59 -08:00
|
|
|
|
2023-01-26 15:40:16 -08:00
|
|
|
const fn span_static(s: &'static str) -> Span<'static> {
|
|
|
|
Span {
|
|
|
|
content: Cow::Borrowed(s),
|
|
|
|
style: Style {
|
|
|
|
fg: None,
|
|
|
|
bg: None,
|
|
|
|
add_modifier: StyleModifier::empty(),
|
|
|
|
sub_modifier: StyleModifier::empty(),
|
2023-07-01 08:58:48 +01:00
|
|
|
underline_color: None,
|
2023-01-26 15:40:16 -08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-29 18:07:00 -08:00
|
|
|
const BOLD_STYLE: Style = Style {
|
|
|
|
fg: None,
|
|
|
|
bg: None,
|
|
|
|
add_modifier: StyleModifier::BOLD,
|
|
|
|
sub_modifier: StyleModifier::empty(),
|
2023-07-01 08:58:48 +01:00
|
|
|
underline_color: None,
|
2023-01-29 18:07:00 -08:00
|
|
|
};
|
|
|
|
|
2022-12-29 18:00:59 -08:00
|
|
|
const TIME_GUTTER: usize = 12;
|
2023-01-26 15:40:16 -08:00
|
|
|
const READ_GUTTER: usize = 5;
|
2022-12-29 18:00:59 -08:00
|
|
|
const MIN_MSG_LEN: usize = 30;
|
|
|
|
|
2023-01-26 15:40:16 -08:00
|
|
|
const TIME_GUTTER_EMPTY: &str = " ";
|
|
|
|
const TIME_GUTTER_EMPTY_SPAN: Span<'static> = span_static(TIME_GUTTER_EMPTY);
|
2022-12-29 18:00:59 -08:00
|
|
|
|
2024-08-18 01:31:42 -07:00
|
|
|
const USIZE_TOO_SMALL: bool = usize::BITS < u64::BITS;
|
|
|
|
|
|
|
|
/// Convert the [u64] hash to [usize] as needed.
|
|
|
|
fn hash_finish_usize(hasher: DefaultHasher) -> Option<usize> {
|
|
|
|
if USIZE_TOO_SMALL {
|
|
|
|
(hasher.finish() % usize::MAX as u64).try_into().ok()
|
|
|
|
} else {
|
|
|
|
hasher.finish().try_into().ok()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Hash an [EventId] into a [usize].
|
|
|
|
fn hash_event_id(event_id: &EventId) -> Option<usize> {
|
|
|
|
let mut hasher = DefaultHasher::new();
|
|
|
|
event_id.hash(&mut hasher);
|
|
|
|
hash_finish_usize(hasher)
|
|
|
|
}
|
|
|
|
|
2024-03-23 19:41:05 -07:00
|
|
|
/// Before the image is loaded, already display a placeholder frame of the image size.
|
|
|
|
fn placeholder_frame(
|
|
|
|
text: Option<&str>,
|
|
|
|
outer_width: usize,
|
|
|
|
image_preview_size: &ImagePreviewSize,
|
|
|
|
) -> Option<String> {
|
|
|
|
let ImagePreviewSize { width, height } = image_preview_size;
|
|
|
|
if outer_width < *width || (*width < 2 || *height < 2) {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
let mut placeholder = "\u{230c}".to_string();
|
|
|
|
placeholder.push_str(&" ".repeat(width - 2));
|
|
|
|
placeholder.push_str("\u{230d}\n");
|
|
|
|
if *height > 2 {
|
|
|
|
if let Some(text) = text {
|
|
|
|
if text.width() <= width - 2 {
|
|
|
|
placeholder.push(' ');
|
|
|
|
placeholder.push_str(text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
placeholder.push_str(&"\n".repeat(height - 2));
|
|
|
|
placeholder.push('\u{230e}');
|
|
|
|
placeholder.push_str(&" ".repeat(width - 2));
|
|
|
|
placeholder.push_str("\u{230f}\n");
|
|
|
|
Some(placeholder)
|
|
|
|
}
|
|
|
|
|
2023-01-29 18:07:00 -08:00
|
|
|
#[inline]
|
|
|
|
fn millis_to_datetime(ms: UInt) -> DateTime<LocalTz> {
|
|
|
|
let time = i64::from(ms) / 1000;
|
2024-08-30 09:08:12 -07:00
|
|
|
let time = DateTime::from_timestamp(time, 0).unwrap_or_default();
|
|
|
|
time.into()
|
2023-01-29 18:07:00 -08:00
|
|
|
}
|
|
|
|
|
2022-12-29 18:00:59 -08:00
|
|
|
#[derive(thiserror::Error, Debug)]
|
|
|
|
pub enum TimeStampIntError {
|
|
|
|
#[error("Integer conversion error: {0}")]
|
|
|
|
IntError(#[from] std::num::TryFromIntError),
|
|
|
|
|
|
|
|
#[error("UInt conversion error: {0}")]
|
|
|
|
UIntError(<UInt as TryFrom<u64>>::Error),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
|
|
|
pub enum MessageTimeStamp {
|
|
|
|
OriginServer(UInt),
|
|
|
|
LocalEcho,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MessageTimeStamp {
|
2023-01-29 18:07:00 -08:00
|
|
|
fn as_datetime(&self) -> DateTime<LocalTz> {
|
2022-12-29 18:00:59 -08:00
|
|
|
match self {
|
2023-01-29 18:07:00 -08:00
|
|
|
MessageTimeStamp::OriginServer(ms) => millis_to_datetime(*ms),
|
|
|
|
MessageTimeStamp::LocalEcho => LocalTz::now(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn same_day(&self, other: &Self) -> bool {
|
|
|
|
let dt1 = self.as_datetime();
|
|
|
|
let dt2 = other.as_datetime();
|
|
|
|
|
|
|
|
dt1.date_naive() == dt2.date_naive()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn show_date(&self) -> Option<Span> {
|
|
|
|
let time = self.as_datetime().format("%A, %B %d %Y").to_string();
|
|
|
|
|
|
|
|
Span::styled(time, BOLD_STYLE).into()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn show_time(&self) -> Option<Span> {
|
|
|
|
match self {
|
|
|
|
MessageTimeStamp::OriginServer(ms) => {
|
|
|
|
let time = millis_to_datetime(*ms).format("%T");
|
2023-01-30 13:51:32 -08:00
|
|
|
let time = format!(" [{time}]");
|
2022-12-29 18:00:59 -08:00
|
|
|
|
|
|
|
Span::raw(time).into()
|
|
|
|
},
|
|
|
|
MessageTimeStamp::LocalEcho => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_local_echo(&self) -> bool {
|
|
|
|
matches!(self, MessageTimeStamp::LocalEcho)
|
|
|
|
}
|
2023-01-12 21:20:32 -08:00
|
|
|
|
|
|
|
pub fn as_millis(&self) -> Option<MilliSecondsSinceUnixEpoch> {
|
|
|
|
match self {
|
|
|
|
MessageTimeStamp::OriginServer(ms) => MilliSecondsSinceUnixEpoch(*ms).into(),
|
|
|
|
MessageTimeStamp::LocalEcho => None,
|
|
|
|
}
|
|
|
|
}
|
2022-12-29 18:00:59 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Ord for MessageTimeStamp {
|
|
|
|
fn cmp(&self, other: &Self) -> Ordering {
|
|
|
|
match (self, other) {
|
|
|
|
(MessageTimeStamp::OriginServer(_), MessageTimeStamp::LocalEcho) => Ordering::Less,
|
|
|
|
(MessageTimeStamp::OriginServer(a), MessageTimeStamp::OriginServer(b)) => a.cmp(b),
|
|
|
|
(MessageTimeStamp::LocalEcho, MessageTimeStamp::OriginServer(_)) => Ordering::Greater,
|
|
|
|
(MessageTimeStamp::LocalEcho, MessageTimeStamp::LocalEcho) => Ordering::Equal,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialOrd for MessageTimeStamp {
|
|
|
|
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
2024-02-27 18:36:09 -08:00
|
|
|
Some(self.cmp(other))
|
2022-12-29 18:00:59 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-29 18:07:00 -08:00
|
|
|
impl From<UInt> for MessageTimeStamp {
|
|
|
|
fn from(millis: UInt) -> Self {
|
|
|
|
MessageTimeStamp::OriginServer(millis)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-29 18:00:59 -08:00
|
|
|
impl From<MilliSecondsSinceUnixEpoch> for MessageTimeStamp {
|
|
|
|
fn from(millis: MilliSecondsSinceUnixEpoch) -> Self {
|
|
|
|
MessageTimeStamp::OriginServer(millis.0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<&MessageTimeStamp> for usize {
|
|
|
|
type Error = TimeStampIntError;
|
|
|
|
|
|
|
|
fn try_from(ts: &MessageTimeStamp) -> Result<Self, Self::Error> {
|
|
|
|
let n = match ts {
|
|
|
|
MessageTimeStamp::LocalEcho => 0,
|
|
|
|
MessageTimeStamp::OriginServer(u) => usize::try_from(u64::from(*u))?,
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<usize> for MessageTimeStamp {
|
|
|
|
type Error = TimeStampIntError;
|
|
|
|
|
|
|
|
fn try_from(u: usize) -> Result<Self, Self::Error> {
|
|
|
|
if u == 0 {
|
|
|
|
Ok(MessageTimeStamp::LocalEcho)
|
|
|
|
} else {
|
|
|
|
let n = u64::try_from(u)?;
|
|
|
|
let n = UInt::try_from(n).map_err(TimeStampIntError::UIntError)?;
|
|
|
|
|
2023-01-29 18:07:00 -08:00
|
|
|
Ok(MessageTimeStamp::from(n))
|
2022-12-29 18:00:59 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Default, Eq, PartialEq)]
|
|
|
|
pub struct MessageCursor {
|
|
|
|
/// When timestamp is None, the corner is determined by moving backwards from
|
|
|
|
/// the most recently received message.
|
|
|
|
pub timestamp: Option<MessageKey>,
|
|
|
|
|
|
|
|
/// A row within the [Text] representation of a [Message].
|
|
|
|
pub text_row: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MessageCursor {
|
|
|
|
pub fn new(timestamp: MessageKey, text_row: usize) -> Self {
|
|
|
|
MessageCursor { timestamp: Some(timestamp), text_row }
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get a cursor that refers to the most recent message.
|
|
|
|
pub fn latest() -> Self {
|
|
|
|
MessageCursor::default()
|
|
|
|
}
|
|
|
|
|
2024-03-09 00:47:05 -08:00
|
|
|
pub fn to_key<'a>(&'a self, thread: &'a Messages) -> Option<&'a MessageKey> {
|
2022-12-29 18:00:59 -08:00
|
|
|
if let Some(ref key) = self.timestamp {
|
|
|
|
Some(key)
|
|
|
|
} else {
|
2024-03-09 00:47:05 -08:00
|
|
|
Some(thread.last_key_value()?.0)
|
2022-12-29 18:00:59 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-09 00:47:05 -08:00
|
|
|
pub fn from_cursor(cursor: &Cursor, thread: &Messages) -> Option<Self> {
|
2024-08-18 01:31:42 -07:00
|
|
|
let ev_hash = cursor.get_x();
|
2022-12-29 18:00:59 -08:00
|
|
|
let ev_term = OwnedEventId::try_from("$").ok()?;
|
|
|
|
|
|
|
|
let ts_start = MessageTimeStamp::try_from(cursor.get_y()).ok()?;
|
|
|
|
let start = (ts_start, ev_term);
|
|
|
|
|
2024-03-09 00:47:05 -08:00
|
|
|
for ((ts, event_id), _) in thread.range(&start..) {
|
2024-08-18 01:31:42 -07:00
|
|
|
if hash_event_id(event_id)? == ev_hash {
|
2024-03-09 00:47:05 -08:00
|
|
|
return Self::from((*ts, event_id.clone())).into();
|
2022-12-29 18:00:59 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if ts > &ts_start {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-09 00:47:05 -08:00
|
|
|
// If we can't find the cursor, then go to the nearest timestamp.
|
|
|
|
thread
|
|
|
|
.range(start..)
|
|
|
|
.next()
|
|
|
|
.map(|((ts, ev), _)| Self::from((*ts, ev.clone())))
|
2022-12-29 18:00:59 -08:00
|
|
|
}
|
|
|
|
|
2024-03-09 00:47:05 -08:00
|
|
|
pub fn to_cursor(&self, thread: &Messages) -> Option<Cursor> {
|
|
|
|
let (ts, event_id) = self.to_key(thread)?;
|
2022-12-29 18:00:59 -08:00
|
|
|
|
2024-08-18 01:31:42 -07:00
|
|
|
let y = usize::try_from(ts).ok()?;
|
|
|
|
let x = hash_event_id(event_id)?;
|
2022-12-29 18:00:59 -08:00
|
|
|
|
|
|
|
Cursor::new(y, x).into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Option<MessageKey>> for MessageCursor {
|
|
|
|
fn from(key: Option<MessageKey>) -> Self {
|
|
|
|
MessageCursor { timestamp: key, text_row: 0 }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<MessageKey> for MessageCursor {
|
|
|
|
fn from(key: MessageKey) -> Self {
|
|
|
|
MessageCursor { timestamp: Some(key), text_row: 0 }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Ord for MessageCursor {
|
|
|
|
fn cmp(&self, other: &Self) -> Ordering {
|
|
|
|
match (&self.timestamp, &other.timestamp) {
|
|
|
|
(None, None) => self.text_row.cmp(&other.text_row),
|
|
|
|
(None, Some(_)) => Ordering::Greater,
|
|
|
|
(Some(_), None) => Ordering::Less,
|
|
|
|
(Some(st), Some(ot)) => {
|
|
|
|
let pcmp = st.cmp(ot);
|
|
|
|
let tcmp = self.text_row.cmp(&other.text_row);
|
|
|
|
|
|
|
|
pcmp.then(tcmp)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialOrd for MessageCursor {
|
|
|
|
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
2024-02-27 18:36:09 -08:00
|
|
|
Some(self.cmp(other))
|
2022-12-29 18:00:59 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-02 15:00:29 -08:00
|
|
|
fn redaction_reason(ev: &SyncRoomRedactionEvent) -> Option<&str> {
|
|
|
|
let SyncRoomRedactionEvent::Original(ev) = ev else {
|
|
|
|
return None;
|
|
|
|
};
|
|
|
|
|
|
|
|
return ev.content.reason.as_deref();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn redaction_unsigned(ev: SyncRoomRedactionEvent) -> RedactedUnsigned {
|
|
|
|
let reason = redaction_reason(&ev);
|
|
|
|
let redacted_because = json!({
|
|
|
|
"content": {
|
|
|
|
"reason": reason
|
|
|
|
},
|
|
|
|
"event_id": ev.event_id(),
|
|
|
|
"sender": ev.sender(),
|
|
|
|
"origin_server_ts": ev.origin_server_ts(),
|
|
|
|
"unsigned": {},
|
|
|
|
});
|
|
|
|
RedactedUnsigned::new(serde_json::from_value(redacted_because).unwrap())
|
|
|
|
}
|
|
|
|
|
2022-12-29 18:00:59 -08:00
|
|
|
#[derive(Clone)]
|
2023-01-12 21:20:32 -08:00
|
|
|
pub enum MessageEvent {
|
2023-03-13 16:43:04 -07:00
|
|
|
EncryptedOriginal(Box<OriginalRoomEncryptedEvent>),
|
|
|
|
EncryptedRedacted(Box<RedactedRoomEncryptedEvent>),
|
2023-01-12 21:20:32 -08:00
|
|
|
Original(Box<OriginalRoomMessageEvent>),
|
|
|
|
Redacted(Box<RedactedRoomMessageEvent>),
|
2023-01-26 15:40:16 -08:00
|
|
|
Local(OwnedEventId, Box<RoomMessageEventContent>),
|
2022-12-29 18:00:59 -08:00
|
|
|
}
|
|
|
|
|
2023-01-12 21:20:32 -08:00
|
|
|
impl MessageEvent {
|
2023-01-26 15:40:16 -08:00
|
|
|
pub fn event_id(&self) -> &EventId {
|
|
|
|
match self {
|
2023-03-13 16:43:04 -07:00
|
|
|
MessageEvent::EncryptedOriginal(ev) => ev.event_id.as_ref(),
|
|
|
|
MessageEvent::EncryptedRedacted(ev) => ev.event_id.as_ref(),
|
2023-01-26 15:40:16 -08:00
|
|
|
MessageEvent::Original(ev) => ev.event_id.as_ref(),
|
|
|
|
MessageEvent::Redacted(ev) => ev.event_id.as_ref(),
|
|
|
|
MessageEvent::Local(event_id, _) => event_id.as_ref(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-21 14:02:42 -07:00
|
|
|
pub fn content(&self) -> Option<&RoomMessageEventContent> {
|
|
|
|
match self {
|
|
|
|
MessageEvent::EncryptedOriginal(_) => None,
|
|
|
|
MessageEvent::Original(ev) => Some(&ev.content),
|
|
|
|
MessageEvent::EncryptedRedacted(_) => None,
|
|
|
|
MessageEvent::Redacted(_) => None,
|
|
|
|
MessageEvent::Local(_, content) => Some(content),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_emote(&self) -> bool {
|
|
|
|
matches!(
|
|
|
|
self.content(),
|
|
|
|
Some(RoomMessageEventContent { msgtype: MessageType::Emote(_), .. })
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-01-23 17:08:11 -08:00
|
|
|
pub fn body(&self) -> Cow<'_, str> {
|
2022-12-29 18:00:59 -08:00
|
|
|
match self {
|
2023-03-13 16:43:04 -07:00
|
|
|
MessageEvent::EncryptedOriginal(_) => "[Unable to decrypt message]".into(),
|
2023-01-23 17:08:11 -08:00
|
|
|
MessageEvent::Original(ev) => body_cow_content(&ev.content),
|
2023-03-13 16:43:04 -07:00
|
|
|
MessageEvent::EncryptedRedacted(ev) => body_cow_reason(&ev.unsigned),
|
|
|
|
MessageEvent::Redacted(ev) => body_cow_reason(&ev.unsigned),
|
2023-01-26 15:40:16 -08:00
|
|
|
MessageEvent::Local(_, content) => body_cow_content(content),
|
2023-01-23 17:08:11 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn html(&self) -> Option<StyleTree> {
|
|
|
|
let content = match self {
|
2023-03-13 16:43:04 -07:00
|
|
|
MessageEvent::EncryptedOriginal(_) => return None,
|
|
|
|
MessageEvent::EncryptedRedacted(_) => return None,
|
2023-01-23 17:08:11 -08:00
|
|
|
MessageEvent::Original(ev) => &ev.content,
|
|
|
|
MessageEvent::Redacted(_) => return None,
|
2023-01-26 15:40:16 -08:00
|
|
|
MessageEvent::Local(_, content) => content,
|
2023-01-23 17:08:11 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
if let MessageType::Text(content) = &content.msgtype {
|
|
|
|
if let Some(FormattedBody { format: MessageFormat::Html, body }) = &content.formatted {
|
|
|
|
Some(parse_matrix_html(body.as_str()))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
2022-12-29 18:00:59 -08:00
|
|
|
}
|
|
|
|
}
|
2023-01-13 17:53:54 -08:00
|
|
|
|
2023-03-22 21:25:37 -07:00
|
|
|
fn redact(&mut self, redaction: SyncRoomRedactionEvent, version: &RoomVersionId) {
|
2023-01-13 17:53:54 -08:00
|
|
|
match self {
|
2023-03-13 16:43:04 -07:00
|
|
|
MessageEvent::EncryptedOriginal(_) => return,
|
|
|
|
MessageEvent::EncryptedRedacted(_) => return,
|
2023-01-13 17:53:54 -08:00
|
|
|
MessageEvent::Redacted(_) => return,
|
2023-01-26 15:40:16 -08:00
|
|
|
MessageEvent::Local(_, _) => return,
|
2023-01-13 17:53:54 -08:00
|
|
|
MessageEvent::Original(ev) => {
|
2024-03-02 15:00:29 -08:00
|
|
|
let redacted = RedactedRoomMessageEvent {
|
|
|
|
content: ev.content.clone().redact(version),
|
|
|
|
event_id: ev.event_id.clone(),
|
|
|
|
sender: ev.sender.clone(),
|
|
|
|
origin_server_ts: ev.origin_server_ts,
|
|
|
|
room_id: ev.room_id.clone(),
|
|
|
|
unsigned: redaction_unsigned(redaction),
|
|
|
|
};
|
2023-01-13 17:53:54 -08:00
|
|
|
*self = MessageEvent::Redacted(Box::new(redacted));
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2022-12-29 18:00:59 -08:00
|
|
|
}
|
|
|
|
|
2024-05-26 01:16:04 +02:00
|
|
|
/// Macro rule converting a File / Image / Audio / Video to its text content with the shape:
|
|
|
|
/// `[Attached <type>: <content>[ (<human readable file size>)]]`
|
|
|
|
macro_rules! display_file_to_text {
|
|
|
|
( $msgtype:ident, $content:expr ) => {
|
|
|
|
return Cow::Owned(format!(
|
|
|
|
"[Attached {}: {}{}]",
|
|
|
|
stringify!($msgtype),
|
|
|
|
$content.body,
|
|
|
|
$content
|
|
|
|
.info
|
|
|
|
.as_ref()
|
|
|
|
.map(|info| {
|
|
|
|
info.size
|
|
|
|
.map(|s| format!(" ({})", format_size(u64::from(s), DECIMAL)))
|
|
|
|
.unwrap_or_else(String::new)
|
|
|
|
})
|
|
|
|
.unwrap_or_else(String::new)
|
|
|
|
))
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-01-23 17:08:11 -08:00
|
|
|
fn body_cow_content(content: &RoomMessageEventContent) -> Cow<'_, str> {
|
2023-01-12 21:20:32 -08:00
|
|
|
let s = match &content.msgtype {
|
2023-01-23 17:08:11 -08:00
|
|
|
MessageType::Text(content) => content.body.as_str(),
|
|
|
|
MessageType::VerificationRequest(_) => "[Verification Request]",
|
2023-01-12 21:20:32 -08:00
|
|
|
MessageType::Emote(content) => content.body.as_ref(),
|
|
|
|
MessageType::Notice(content) => content.body.as_str(),
|
|
|
|
MessageType::ServerNotice(content) => content.body.as_str(),
|
|
|
|
|
|
|
|
MessageType::Audio(content) => {
|
2024-05-26 01:16:04 +02:00
|
|
|
display_file_to_text!(Audio, content);
|
2023-01-12 21:20:32 -08:00
|
|
|
},
|
|
|
|
MessageType::File(content) => {
|
2024-05-26 01:16:04 +02:00
|
|
|
display_file_to_text!(File, content);
|
2023-01-12 21:20:32 -08:00
|
|
|
},
|
|
|
|
MessageType::Image(content) => {
|
2024-05-26 01:16:04 +02:00
|
|
|
display_file_to_text!(Image, content);
|
2023-01-12 21:20:32 -08:00
|
|
|
},
|
|
|
|
MessageType::Video(content) => {
|
2024-05-26 01:16:04 +02:00
|
|
|
display_file_to_text!(Video, content);
|
2023-01-12 21:20:32 -08:00
|
|
|
},
|
|
|
|
_ => {
|
2024-08-07 22:49:54 -07:00
|
|
|
match content.msgtype() {
|
|
|
|
// Just show the body text for the special Element messages.
|
|
|
|
"nic.custom.confetti" |
|
|
|
|
"nic.custom.fireworks" |
|
|
|
|
"io.element.effect.hearts" |
|
|
|
|
"io.element.effect.rainfall" |
|
|
|
|
"io.element.effect.snowfall" |
|
|
|
|
"io.element.effects.space_invaders" => content.body(),
|
|
|
|
other => {
|
|
|
|
return Cow::Owned(format!("[Unknown message type: {other:?}]"));
|
|
|
|
},
|
|
|
|
}
|
2023-01-12 21:20:32 -08:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
Cow::Borrowed(s)
|
|
|
|
}
|
|
|
|
|
2023-03-13 16:43:04 -07:00
|
|
|
fn body_cow_reason(unsigned: &RedactedUnsigned) -> Cow<'_, str> {
|
2024-03-02 15:00:29 -08:00
|
|
|
let reason = unsigned.redacted_because.content.reason.as_ref();
|
2023-03-13 16:43:04 -07:00
|
|
|
|
|
|
|
if let Some(r) = reason {
|
|
|
|
Cow::Owned(format!("[Redacted: {r:?}]"))
|
|
|
|
} else {
|
|
|
|
Cow::Borrowed("[Redacted]")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-26 15:40:16 -08:00
|
|
|
enum MessageColumns {
|
|
|
|
/// Four columns: sender, message, timestamp, read receipts.
|
|
|
|
Four,
|
|
|
|
|
|
|
|
/// Three columns: sender, message, timestamp.
|
|
|
|
Three,
|
|
|
|
|
|
|
|
/// Two columns: sender, message.
|
|
|
|
Two,
|
|
|
|
|
|
|
|
/// One column: message with sender on line before the message.
|
|
|
|
One,
|
|
|
|
}
|
|
|
|
|
2024-03-23 19:41:05 -07:00
|
|
|
impl MessageColumns {
|
|
|
|
fn user_gutter_width(&self, settings: &ApplicationSettings) -> u16 {
|
|
|
|
if let MessageColumns::One = self {
|
|
|
|
0
|
|
|
|
} else {
|
|
|
|
settings.tunables.user_gutter_width as u16
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-26 15:40:16 -08:00
|
|
|
struct MessageFormatter<'a> {
|
|
|
|
settings: &'a ApplicationSettings,
|
2023-01-29 18:07:00 -08:00
|
|
|
|
|
|
|
/// How many columns to print.
|
2023-01-26 15:40:16 -08:00
|
|
|
cols: MessageColumns,
|
2023-01-29 18:07:00 -08:00
|
|
|
|
|
|
|
/// The full, original width.
|
|
|
|
orig: usize,
|
|
|
|
|
|
|
|
/// The width that the message contents need to fill.
|
2023-01-26 15:40:16 -08:00
|
|
|
fill: usize,
|
2023-01-29 18:07:00 -08:00
|
|
|
|
|
|
|
/// The formatted Span for the message sender.
|
2023-01-26 15:40:16 -08:00
|
|
|
user: Option<Span<'a>>,
|
2023-01-29 18:07:00 -08:00
|
|
|
|
|
|
|
/// The time the message was sent.
|
2023-01-26 15:40:16 -08:00
|
|
|
time: Option<Span<'a>>,
|
2023-01-29 18:07:00 -08:00
|
|
|
|
|
|
|
/// The date the message was sent.
|
|
|
|
date: Option<Span<'a>>,
|
|
|
|
|
|
|
|
/// Iterator over the users who have read up to this message.
|
2023-10-15 18:12:39 -07:00
|
|
|
read: Option<hash_set::Iter<'a, OwnedUserId>>,
|
2023-01-23 17:08:11 -08:00
|
|
|
}
|
|
|
|
|
2023-01-26 15:40:16 -08:00
|
|
|
impl<'a> MessageFormatter<'a> {
|
2023-01-23 17:08:11 -08:00
|
|
|
fn width(&self) -> usize {
|
2023-01-26 15:40:16 -08:00
|
|
|
self.fill
|
2023-01-23 17:08:11 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2023-07-01 08:58:48 +01:00
|
|
|
fn push_spans(&mut self, prev_line: Line<'a>, style: Style, text: &mut Text<'a>) {
|
2023-01-29 18:07:00 -08:00
|
|
|
if let Some(date) = self.date.take() {
|
|
|
|
let len = date.content.as_ref().len();
|
|
|
|
let padding = self.orig.saturating_sub(len);
|
|
|
|
let leading = space_span(padding / 2, Style::default());
|
|
|
|
let trailing = space_span(padding.saturating_sub(padding / 2), Style::default());
|
|
|
|
|
2023-07-01 08:58:48 +01:00
|
|
|
text.lines.push(Line::from(vec![leading, date, trailing]));
|
2023-01-29 18:07:00 -08:00
|
|
|
}
|
|
|
|
|
2024-03-24 02:54:26 +01:00
|
|
|
let user_gutter_empty_span =
|
|
|
|
space_span(self.settings.tunables.user_gutter_width, Style::default());
|
|
|
|
|
2023-01-26 15:40:16 -08:00
|
|
|
match self.cols {
|
|
|
|
MessageColumns::Four => {
|
|
|
|
let settings = self.settings;
|
2024-03-24 02:54:26 +01:00
|
|
|
let user = self.user.take().unwrap_or(user_gutter_empty_span);
|
2023-01-26 15:40:16 -08:00
|
|
|
let time = self.time.take().unwrap_or(TIME_GUTTER_EMPTY_SPAN);
|
|
|
|
|
|
|
|
let mut line = vec![user];
|
2023-07-01 08:58:48 +01:00
|
|
|
line.extend(prev_line.spans);
|
2023-01-26 15:40:16 -08:00
|
|
|
line.push(time);
|
|
|
|
|
|
|
|
// Show read receipts.
|
|
|
|
let user_char =
|
|
|
|
|user: &'a OwnedUserId| -> Span<'a> { settings.get_user_char_span(user) };
|
2023-10-15 18:12:39 -07:00
|
|
|
let mut read = self.read.iter_mut().flatten();
|
2023-01-26 15:40:16 -08:00
|
|
|
|
2023-10-15 18:12:39 -07:00
|
|
|
let a = read.next().map(user_char).unwrap_or_else(|| Span::raw(" "));
|
|
|
|
let b = read.next().map(user_char).unwrap_or_else(|| Span::raw(" "));
|
|
|
|
let c = read.next().map(user_char).unwrap_or_else(|| Span::raw(" "));
|
2023-01-26 15:40:16 -08:00
|
|
|
|
|
|
|
line.push(Span::raw(" "));
|
|
|
|
line.push(c);
|
|
|
|
line.push(b);
|
|
|
|
line.push(a);
|
|
|
|
line.push(Span::raw(" "));
|
|
|
|
|
2023-07-01 08:58:48 +01:00
|
|
|
text.lines.push(Line::from(line))
|
2023-01-26 15:40:16 -08:00
|
|
|
},
|
|
|
|
MessageColumns::Three => {
|
2024-03-24 02:54:26 +01:00
|
|
|
let user = self.user.take().unwrap_or(user_gutter_empty_span);
|
2023-01-26 15:40:16 -08:00
|
|
|
let time = self.time.take().unwrap_or_else(|| Span::from(""));
|
2023-01-23 17:08:11 -08:00
|
|
|
|
|
|
|
let mut line = vec![user];
|
2023-07-01 08:58:48 +01:00
|
|
|
line.extend(prev_line.spans);
|
2023-01-23 17:08:11 -08:00
|
|
|
line.push(time);
|
|
|
|
|
2023-07-01 08:58:48 +01:00
|
|
|
text.lines.push(Line::from(line))
|
2023-01-23 17:08:11 -08:00
|
|
|
},
|
2023-01-26 15:40:16 -08:00
|
|
|
MessageColumns::Two => {
|
2024-03-24 02:54:26 +01:00
|
|
|
let user = self.user.take().unwrap_or(user_gutter_empty_span);
|
2023-01-23 17:08:11 -08:00
|
|
|
let mut line = vec![user];
|
2023-07-01 08:58:48 +01:00
|
|
|
line.extend(prev_line.spans);
|
2023-01-23 17:08:11 -08:00
|
|
|
|
2023-07-01 08:58:48 +01:00
|
|
|
text.lines.push(Line::from(line));
|
2023-01-23 17:08:11 -08:00
|
|
|
},
|
2023-01-26 15:40:16 -08:00
|
|
|
MessageColumns::One => {
|
|
|
|
if let Some(user) = self.user.take() {
|
2023-07-01 08:58:48 +01:00
|
|
|
text.lines.push(Line::from(vec![user]));
|
2023-01-23 17:08:11 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
let leading = space_span(2, style);
|
|
|
|
let mut line = vec![leading];
|
2023-07-01 08:58:48 +01:00
|
|
|
line.extend(prev_line.spans);
|
2023-01-23 17:08:11 -08:00
|
|
|
|
2023-07-01 08:58:48 +01:00
|
|
|
text.lines.push(Line::from(line));
|
2023-01-23 17:08:11 -08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn push_text(&mut self, append: Text<'a>, style: Style, text: &mut Text<'a>) {
|
|
|
|
for line in append.lines.into_iter() {
|
|
|
|
self.push_spans(line, style, text);
|
|
|
|
}
|
|
|
|
}
|
2024-03-23 19:20:06 -07:00
|
|
|
|
|
|
|
fn push_in_reply(
|
|
|
|
&mut self,
|
|
|
|
msg: &'a Message,
|
|
|
|
style: Style,
|
|
|
|
text: &mut Text<'a>,
|
|
|
|
info: &'a RoomInfo,
|
|
|
|
) {
|
|
|
|
let width = self.width();
|
|
|
|
let w = width.saturating_sub(2);
|
|
|
|
let shortcodes = self.settings.tunables.message_shortcode_display;
|
2024-03-23 19:41:05 -07:00
|
|
|
let (mut replied, _) = msg.show_msg(w, style, true, shortcodes);
|
2024-03-23 19:20:06 -07:00
|
|
|
let mut sender = msg.sender_span(info, self.settings);
|
|
|
|
let sender_width = UnicodeWidthStr::width(sender.content.as_ref());
|
|
|
|
let trailing = w.saturating_sub(sender_width + 1);
|
|
|
|
|
|
|
|
sender.style = sender.style.patch(style);
|
|
|
|
|
|
|
|
self.push_spans(
|
|
|
|
Line::from(vec![
|
|
|
|
Span::styled(" ", style),
|
|
|
|
Span::styled(THICK_VERTICAL, style),
|
|
|
|
sender,
|
|
|
|
Span::styled(":", style),
|
|
|
|
space_span(trailing, style),
|
|
|
|
]),
|
|
|
|
style,
|
|
|
|
text,
|
|
|
|
);
|
|
|
|
|
|
|
|
for line in replied.lines.iter_mut() {
|
|
|
|
line.spans.insert(0, Span::styled(THICK_VERTICAL, style));
|
|
|
|
line.spans.insert(0, Span::styled(" ", style));
|
|
|
|
}
|
|
|
|
|
|
|
|
self.push_text(replied, style, text);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn push_reactions(&mut self, counts: Vec<(&'a str, usize)>, style: Style, text: &mut Text<'a>) {
|
|
|
|
let mut emojis = printer::TextPrinter::new(self.width(), style, false, false);
|
|
|
|
let mut reactions = 0;
|
|
|
|
|
|
|
|
for (key, count) in counts {
|
|
|
|
if reactions != 0 {
|
|
|
|
emojis.push_str(" ", style);
|
|
|
|
}
|
|
|
|
|
|
|
|
let name = if self.settings.tunables.reaction_shortcode_display {
|
|
|
|
if let Some(emoji) = emojis::get(key) {
|
|
|
|
if let Some(short) = emoji.shortcode() {
|
|
|
|
short
|
|
|
|
} else {
|
|
|
|
// No ASCII shortcode name to show.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else if key.chars().all(|c| c.is_ascii_alphanumeric()) {
|
|
|
|
key
|
|
|
|
} else {
|
|
|
|
// Not an Emoji or a printable ASCII string.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
key
|
|
|
|
};
|
|
|
|
|
|
|
|
emojis.push_str("[", style);
|
|
|
|
emojis.push_str(name, style);
|
|
|
|
emojis.push_str(" ", style);
|
|
|
|
emojis.push_span_nobreak(Span::styled(count.to_string(), style));
|
|
|
|
emojis.push_str("]", style);
|
|
|
|
|
|
|
|
reactions += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if reactions > 0 {
|
|
|
|
self.push_text(emojis.finish(), style, text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn push_thread_reply_count(&mut self, len: usize, text: &mut Text<'a>) {
|
|
|
|
if len == 0 {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have threaded replies to this message, show how many.
|
|
|
|
let plural = len != 1;
|
|
|
|
let style = Style::default();
|
|
|
|
let mut threaded =
|
|
|
|
printer::TextPrinter::new(self.width(), style, false, false).literal(true);
|
|
|
|
let len = Span::styled(len.to_string(), style.add_modifier(StyleModifier::BOLD));
|
|
|
|
threaded.push_str(" \u{2937} ", style);
|
|
|
|
threaded.push_span_nobreak(len);
|
|
|
|
if plural {
|
|
|
|
threaded.push_str(" replies in thread", style);
|
|
|
|
} else {
|
|
|
|
threaded.push_str(" reply in thread", style);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.push_text(threaded.finish(), style, text);
|
|
|
|
}
|
2023-01-23 17:08:11 -08:00
|
|
|
}
|
|
|
|
|
2023-11-16 08:36:22 -08:00
|
|
|
pub enum ImageStatus {
|
|
|
|
None,
|
|
|
|
Downloading(ImagePreviewSize),
|
|
|
|
Loaded(Box<dyn Protocol>),
|
|
|
|
Error(String),
|
|
|
|
}
|
|
|
|
|
2022-12-29 18:00:59 -08:00
|
|
|
pub struct Message {
|
2023-01-12 21:20:32 -08:00
|
|
|
pub event: MessageEvent,
|
2022-12-29 18:00:59 -08:00
|
|
|
pub sender: OwnedUserId,
|
|
|
|
pub timestamp: MessageTimeStamp,
|
2023-01-10 19:59:30 -08:00
|
|
|
pub downloaded: bool,
|
2023-01-23 17:08:11 -08:00
|
|
|
pub html: Option<StyleTree>,
|
2023-11-16 08:36:22 -08:00
|
|
|
pub image_preview: ImageStatus,
|
2022-12-29 18:00:59 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Message {
|
2023-01-12 21:20:32 -08:00
|
|
|
pub fn new(event: MessageEvent, sender: OwnedUserId, timestamp: MessageTimeStamp) -> Self {
|
2023-01-23 17:08:11 -08:00
|
|
|
let html = event.html();
|
|
|
|
let downloaded = false;
|
|
|
|
|
2023-11-16 08:36:22 -08:00
|
|
|
Message {
|
|
|
|
event,
|
|
|
|
sender,
|
|
|
|
timestamp,
|
|
|
|
downloaded,
|
|
|
|
html,
|
|
|
|
image_preview: ImageStatus::None,
|
|
|
|
}
|
2022-12-29 18:00:59 -08:00
|
|
|
}
|
|
|
|
|
2023-01-23 17:08:11 -08:00
|
|
|
pub fn reply_to(&self) -> Option<OwnedEventId> {
|
|
|
|
let content = match &self.event {
|
2023-03-13 16:43:04 -07:00
|
|
|
MessageEvent::EncryptedOriginal(_) => return None,
|
|
|
|
MessageEvent::EncryptedRedacted(_) => return None,
|
2023-01-26 15:40:16 -08:00
|
|
|
MessageEvent::Local(_, content) => content,
|
2023-01-23 17:08:11 -08:00
|
|
|
MessageEvent::Original(ev) => &ev.content,
|
|
|
|
MessageEvent::Redacted(_) => return None,
|
|
|
|
};
|
2023-01-10 19:59:30 -08:00
|
|
|
|
2024-03-09 00:47:05 -08:00
|
|
|
match &content.relates_to {
|
|
|
|
Some(Relation::Reply { in_reply_to }) => Some(in_reply_to.event_id.clone()),
|
|
|
|
Some(Relation::Thread(Thread {
|
|
|
|
in_reply_to: Some(in_reply_to),
|
|
|
|
is_falling_back: false,
|
|
|
|
..
|
|
|
|
})) => Some(in_reply_to.event_id.clone()),
|
|
|
|
Some(_) | None => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn thread_root(&self) -> Option<OwnedEventId> {
|
|
|
|
let content = match &self.event {
|
|
|
|
MessageEvent::EncryptedOriginal(_) => return None,
|
|
|
|
MessageEvent::EncryptedRedacted(_) => return None,
|
|
|
|
MessageEvent::Local(_, content) => content,
|
|
|
|
MessageEvent::Original(ev) => &ev.content,
|
|
|
|
MessageEvent::Redacted(_) => return None,
|
|
|
|
};
|
|
|
|
|
|
|
|
match &content.relates_to {
|
|
|
|
Some(Relation::Thread(Thread {
|
|
|
|
event_id,
|
|
|
|
in_reply_to: Some(in_reply_to),
|
|
|
|
is_falling_back: true,
|
|
|
|
..
|
|
|
|
})) if event_id == &in_reply_to.event_id => Some(event_id.clone()),
|
|
|
|
Some(_) | None => None,
|
2023-01-10 19:59:30 -08:00
|
|
|
}
|
2023-01-23 17:08:11 -08:00
|
|
|
}
|
2023-01-10 19:59:30 -08:00
|
|
|
|
2024-02-28 06:52:24 +00:00
|
|
|
fn get_render_style(&self, selected: bool, settings: &ApplicationSettings) -> Style {
|
2022-12-29 18:00:59 -08:00
|
|
|
let mut style = Style::default();
|
|
|
|
|
|
|
|
if selected {
|
|
|
|
style = style.add_modifier(StyleModifier::REVERSED)
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.timestamp.is_local_echo() {
|
|
|
|
style = style.add_modifier(StyleModifier::ITALIC);
|
|
|
|
}
|
|
|
|
|
2024-02-28 06:52:24 +00:00
|
|
|
if settings.tunables.message_user_color {
|
2024-04-02 08:42:27 -07:00
|
|
|
let color = settings.get_user_color(&self.sender);
|
2024-02-28 06:52:24 +00:00
|
|
|
style = style.fg(color);
|
|
|
|
}
|
|
|
|
|
2023-01-23 17:08:11 -08:00
|
|
|
return style;
|
|
|
|
}
|
|
|
|
|
2023-01-26 15:40:16 -08:00
|
|
|
fn get_render_format<'a>(
|
|
|
|
&'a self,
|
2023-01-23 17:08:11 -08:00
|
|
|
prev: Option<&Message>,
|
|
|
|
width: usize,
|
2023-01-26 15:40:16 -08:00
|
|
|
info: &'a RoomInfo,
|
|
|
|
settings: &'a ApplicationSettings,
|
|
|
|
) -> MessageFormatter<'a> {
|
2023-01-29 18:07:00 -08:00
|
|
|
let orig = width;
|
|
|
|
let date = match &prev {
|
|
|
|
Some(prev) if prev.timestamp.same_day(&self.timestamp) => None,
|
|
|
|
_ => self.timestamp.show_date(),
|
|
|
|
};
|
2024-03-24 02:54:26 +01:00
|
|
|
let user_gutter = settings.tunables.user_gutter_width;
|
2023-01-29 18:07:00 -08:00
|
|
|
|
2024-03-24 02:54:26 +01:00
|
|
|
if user_gutter + TIME_GUTTER + READ_GUTTER + MIN_MSG_LEN <= width &&
|
2023-01-26 15:40:16 -08:00
|
|
|
settings.tunables.read_receipt_display
|
|
|
|
{
|
|
|
|
let cols = MessageColumns::Four;
|
2024-03-24 02:54:26 +01:00
|
|
|
let fill = width - user_gutter - TIME_GUTTER - READ_GUTTER;
|
2023-07-06 23:15:58 -07:00
|
|
|
let user = self.show_sender(prev, true, info, settings);
|
2023-01-29 18:07:00 -08:00
|
|
|
let time = self.timestamp.show_time();
|
2023-10-15 18:12:39 -07:00
|
|
|
let read = info.event_receipts.get(self.event.event_id()).map(|read| read.iter());
|
2023-01-26 15:40:16 -08:00
|
|
|
|
2023-01-29 18:07:00 -08:00
|
|
|
MessageFormatter { settings, cols, orig, fill, user, date, time, read }
|
2024-03-24 02:54:26 +01:00
|
|
|
} else if user_gutter + TIME_GUTTER + MIN_MSG_LEN <= width {
|
2023-01-26 15:40:16 -08:00
|
|
|
let cols = MessageColumns::Three;
|
2024-03-24 02:54:26 +01:00
|
|
|
let fill = width - user_gutter - TIME_GUTTER;
|
2023-07-06 23:15:58 -07:00
|
|
|
let user = self.show_sender(prev, true, info, settings);
|
2023-01-29 18:07:00 -08:00
|
|
|
let time = self.timestamp.show_time();
|
2023-10-15 18:12:39 -07:00
|
|
|
let read = None;
|
2022-12-29 18:00:59 -08:00
|
|
|
|
2023-01-29 18:07:00 -08:00
|
|
|
MessageFormatter { settings, cols, orig, fill, user, date, time, read }
|
2024-03-24 02:54:26 +01:00
|
|
|
} else if user_gutter + MIN_MSG_LEN <= width {
|
2023-01-26 15:40:16 -08:00
|
|
|
let cols = MessageColumns::Two;
|
2024-03-24 02:54:26 +01:00
|
|
|
let fill = width - user_gutter;
|
2023-07-06 23:15:58 -07:00
|
|
|
let user = self.show_sender(prev, true, info, settings);
|
2023-01-26 15:40:16 -08:00
|
|
|
let time = None;
|
2023-10-15 18:12:39 -07:00
|
|
|
let read = None;
|
2022-12-29 18:00:59 -08:00
|
|
|
|
2023-01-29 18:07:00 -08:00
|
|
|
MessageFormatter { settings, cols, orig, fill, user, date, time, read }
|
2023-01-23 17:08:11 -08:00
|
|
|
} else {
|
2023-01-26 15:40:16 -08:00
|
|
|
let cols = MessageColumns::One;
|
|
|
|
let fill = width.saturating_sub(2);
|
2023-07-06 23:15:58 -07:00
|
|
|
let user = self.show_sender(prev, false, info, settings);
|
2023-01-26 15:40:16 -08:00
|
|
|
let time = None;
|
2023-10-15 18:12:39 -07:00
|
|
|
let read = None;
|
2022-12-29 18:00:59 -08:00
|
|
|
|
2023-01-29 18:07:00 -08:00
|
|
|
MessageFormatter { settings, cols, orig, fill, user, date, time, read }
|
2023-01-23 17:08:11 -08:00
|
|
|
}
|
|
|
|
}
|
2022-12-29 18:00:59 -08:00
|
|
|
|
2024-03-23 19:41:05 -07:00
|
|
|
/// Render the message as a [Text] object for the terminal.
|
|
|
|
///
|
|
|
|
/// This will also get the image preview Protocol with an x/y offset.
|
|
|
|
pub fn show_with_preview<'a>(
|
2023-01-23 17:08:11 -08:00
|
|
|
&'a self,
|
|
|
|
prev: Option<&Message>,
|
|
|
|
selected: bool,
|
|
|
|
vwctx: &ViewportContext<MessageCursor>,
|
|
|
|
info: &'a RoomInfo,
|
2023-01-26 15:40:16 -08:00
|
|
|
settings: &'a ApplicationSettings,
|
2024-03-23 19:41:05 -07:00
|
|
|
) -> (Text<'a>, Option<(&dyn Protocol, u16, u16)>) {
|
2023-01-23 17:08:11 -08:00
|
|
|
let width = vwctx.get_width();
|
|
|
|
|
2024-02-28 06:52:24 +00:00
|
|
|
let style = self.get_render_style(selected, settings);
|
2023-01-26 15:40:16 -08:00
|
|
|
let mut fmt = self.get_render_format(prev, width, info, settings);
|
2024-04-23 23:30:01 -07:00
|
|
|
let mut text = Text::default();
|
2023-01-23 17:08:11 -08:00
|
|
|
let width = fmt.width();
|
|
|
|
|
|
|
|
// Show the message that this one replied to, if any.
|
2024-03-09 00:47:05 -08:00
|
|
|
let reply = self
|
|
|
|
.reply_to()
|
|
|
|
.or_else(|| self.thread_root())
|
|
|
|
.and_then(|e| info.get_event(&e));
|
2023-01-23 17:08:11 -08:00
|
|
|
|
|
|
|
if let Some(r) = &reply {
|
2024-03-23 19:20:06 -07:00
|
|
|
fmt.push_in_reply(r, style, &mut text, info);
|
2023-01-23 17:08:11 -08:00
|
|
|
}
|
2022-12-29 18:00:59 -08:00
|
|
|
|
2023-01-23 17:08:11 -08:00
|
|
|
// Now show the message contents, and the inlined reply if we couldn't find it above.
|
2024-03-23 19:41:05 -07:00
|
|
|
let (msg, proto) = self.show_msg(
|
2024-03-24 00:35:10 +01:00
|
|
|
width,
|
|
|
|
style,
|
|
|
|
reply.is_some(),
|
|
|
|
settings.tunables.message_shortcode_display,
|
|
|
|
);
|
2024-03-23 19:41:05 -07:00
|
|
|
|
|
|
|
// Given our text so far, determine the image offset.
|
|
|
|
let proto = proto.map(|p| {
|
|
|
|
let y_off = text.lines.len() as u16;
|
|
|
|
let x_off = fmt.cols.user_gutter_width(settings);
|
2024-04-13 18:47:08 -04:00
|
|
|
// Adjust y_off by 1 if a date was printed before the message to account for the extra line.
|
|
|
|
let y_off = if fmt.date.is_some() { y_off + 1 } else { y_off };
|
2024-03-23 19:41:05 -07:00
|
|
|
(p, x_off, y_off)
|
|
|
|
});
|
|
|
|
|
2023-01-23 17:08:11 -08:00
|
|
|
fmt.push_text(msg, style, &mut text);
|
2022-12-29 18:00:59 -08:00
|
|
|
|
2023-01-23 17:08:11 -08:00
|
|
|
if text.lines.is_empty() {
|
|
|
|
// If there was nothing in the body, just show an empty message.
|
|
|
|
fmt.push_spans(space_span(width, style).into(), style, &mut text);
|
|
|
|
}
|
2022-12-29 18:00:59 -08:00
|
|
|
|
2023-02-09 17:53:33 -08:00
|
|
|
if settings.tunables.reaction_display {
|
2024-03-23 19:20:06 -07:00
|
|
|
let reactions = info.get_reactions(self.event.event_id());
|
|
|
|
fmt.push_reactions(reactions, style, &mut text);
|
2023-02-09 17:53:33 -08:00
|
|
|
}
|
|
|
|
|
2024-03-23 19:20:06 -07:00
|
|
|
if let Some(thread) = info.get_thread(Some(self.event.event_id())) {
|
|
|
|
fmt.push_thread_reply_count(thread.len(), &mut text);
|
2024-03-09 00:47:05 -08:00
|
|
|
}
|
|
|
|
|
2024-03-23 19:41:05 -07:00
|
|
|
(text, proto)
|
2023-01-23 17:08:11 -08:00
|
|
|
}
|
|
|
|
|
2024-03-23 19:41:05 -07:00
|
|
|
pub fn show<'a>(
|
|
|
|
&'a self,
|
|
|
|
prev: Option<&Message>,
|
|
|
|
selected: bool,
|
|
|
|
vwctx: &ViewportContext<MessageCursor>,
|
|
|
|
info: &'a RoomInfo,
|
|
|
|
settings: &'a ApplicationSettings,
|
|
|
|
) -> Text<'a> {
|
|
|
|
self.show_with_preview(prev, selected, vwctx, info, settings).0
|
|
|
|
}
|
|
|
|
|
|
|
|
fn show_msg(
|
2024-03-24 00:35:10 +01:00
|
|
|
&self,
|
|
|
|
width: usize,
|
|
|
|
style: Style,
|
|
|
|
hide_reply: bool,
|
|
|
|
emoji_shortcodes: bool,
|
2024-03-23 19:41:05 -07:00
|
|
|
) -> (Text, Option<&dyn Protocol>) {
|
2023-01-23 17:08:11 -08:00
|
|
|
if let Some(html) = &self.html {
|
2024-03-23 19:41:05 -07:00
|
|
|
(html.to_text(width, style, hide_reply, emoji_shortcodes), None)
|
2023-01-23 17:08:11 -08:00
|
|
|
} else {
|
|
|
|
let mut msg = self.event.body();
|
2024-03-24 00:35:10 +01:00
|
|
|
if emoji_shortcodes {
|
|
|
|
msg = Cow::Owned(replace_emojis_in_str(msg.as_ref()));
|
|
|
|
}
|
2022-12-29 18:00:59 -08:00
|
|
|
|
2023-01-23 17:08:11 -08:00
|
|
|
if self.downloaded {
|
|
|
|
msg.to_mut().push_str(" \u{2705}");
|
2022-12-29 18:00:59 -08:00
|
|
|
}
|
2023-01-23 17:08:11 -08:00
|
|
|
|
2024-03-23 19:41:05 -07:00
|
|
|
let mut proto = None;
|
|
|
|
let placeholder = match &self.image_preview {
|
2023-11-16 08:36:22 -08:00
|
|
|
ImageStatus::None => None,
|
|
|
|
ImageStatus::Downloading(image_preview_size) => {
|
2024-03-23 19:41:05 -07:00
|
|
|
placeholder_frame(Some("Downloading..."), width, image_preview_size)
|
2023-11-16 08:36:22 -08:00
|
|
|
},
|
|
|
|
ImageStatus::Loaded(backend) => {
|
2024-03-23 19:41:05 -07:00
|
|
|
proto = Some(backend.as_ref());
|
|
|
|
placeholder_frame(None, width, &backend.rect().into())
|
2023-11-16 08:36:22 -08:00
|
|
|
},
|
|
|
|
ImageStatus::Error(err) => Some(format!("[Image error: {err}]\n")),
|
2024-03-23 19:41:05 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(placeholder) = placeholder {
|
2023-11-16 08:36:22 -08:00
|
|
|
msg.to_mut().insert_str(0, &placeholder);
|
|
|
|
}
|
|
|
|
|
2024-03-23 19:41:05 -07:00
|
|
|
(wrapped_text(msg, width, style), proto)
|
2022-12-29 18:00:59 -08:00
|
|
|
}
|
2023-01-23 17:08:11 -08:00
|
|
|
}
|
2022-12-29 18:00:59 -08:00
|
|
|
|
2023-07-06 23:15:58 -07:00
|
|
|
fn sender_span<'a>(
|
|
|
|
&'a self,
|
|
|
|
info: &'a RoomInfo,
|
|
|
|
settings: &'a ApplicationSettings,
|
|
|
|
) -> Span<'a> {
|
|
|
|
settings.get_user_span(self.sender.as_ref(), info)
|
2022-12-29 18:00:59 -08:00
|
|
|
}
|
|
|
|
|
2023-07-06 23:15:58 -07:00
|
|
|
fn show_sender<'a>(
|
|
|
|
&'a self,
|
2023-01-06 16:56:28 -08:00
|
|
|
prev: Option<&Message>,
|
|
|
|
align_right: bool,
|
2023-07-06 23:15:58 -07:00
|
|
|
info: &'a RoomInfo,
|
|
|
|
settings: &'a ApplicationSettings,
|
|
|
|
) -> Option<Span<'a>> {
|
2023-01-29 18:07:00 -08:00
|
|
|
if let Some(prev) = prev {
|
2023-03-21 14:02:42 -07:00
|
|
|
if self.sender == prev.sender &&
|
|
|
|
self.timestamp.same_day(&prev.timestamp) &&
|
|
|
|
!self.event.is_emote()
|
|
|
|
{
|
2023-01-29 18:07:00 -08:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
}
|
2023-01-06 16:56:28 -08:00
|
|
|
|
2023-07-06 23:15:58 -07:00
|
|
|
let Span { content, style } = self.sender_span(info, settings);
|
2024-03-24 02:54:26 +01:00
|
|
|
let user_gutter = settings.tunables.user_gutter_width;
|
|
|
|
let ((truncated, width), _) = take_width(content, user_gutter - 2);
|
|
|
|
let padding = user_gutter - 2 - width;
|
2022-12-29 18:00:59 -08:00
|
|
|
|
|
|
|
let sender = if align_right {
|
2025-02-17 22:22:16 -05:00
|
|
|
format!("{}{} ", space(padding), truncated)
|
2022-12-29 18:00:59 -08:00
|
|
|
} else {
|
2025-02-17 22:22:16 -05:00
|
|
|
format!("{}{} ", truncated, space(padding))
|
2022-12-29 18:00:59 -08:00
|
|
|
};
|
|
|
|
|
2023-01-23 17:08:11 -08:00
|
|
|
Span::styled(sender, style).into()
|
2022-12-29 18:00:59 -08:00
|
|
|
}
|
2023-03-22 21:25:37 -07:00
|
|
|
|
|
|
|
pub fn redact(&mut self, redaction: SyncRoomRedactionEvent, version: &RoomVersionId) {
|
|
|
|
self.event.redact(redaction, version);
|
|
|
|
self.html = None;
|
|
|
|
}
|
2022-12-29 18:00:59 -08:00
|
|
|
}
|
|
|
|
|
2023-03-13 15:18:53 -07:00
|
|
|
impl From<RoomEncryptedEvent> for Message {
|
|
|
|
fn from(event: RoomEncryptedEvent) -> Self {
|
|
|
|
let timestamp = event.origin_server_ts().into();
|
|
|
|
let user_id = event.sender().to_owned();
|
2023-03-13 16:43:04 -07:00
|
|
|
let content = match event {
|
|
|
|
RoomEncryptedEvent::Original(ev) => MessageEvent::EncryptedOriginal(ev.into()),
|
|
|
|
RoomEncryptedEvent::Redacted(ev) => MessageEvent::EncryptedRedacted(ev.into()),
|
|
|
|
};
|
2023-03-13 15:18:53 -07:00
|
|
|
|
|
|
|
Message::new(content, user_id, timestamp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-12 21:20:32 -08:00
|
|
|
impl From<OriginalRoomMessageEvent> for Message {
|
|
|
|
fn from(event: OriginalRoomMessageEvent) -> Self {
|
|
|
|
let timestamp = event.origin_server_ts.into();
|
|
|
|
let user_id = event.sender.clone();
|
|
|
|
let content = MessageEvent::Original(event.into());
|
2022-12-29 18:00:59 -08:00
|
|
|
|
2023-01-12 21:20:32 -08:00
|
|
|
Message::new(content, user_id, timestamp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<RedactedRoomMessageEvent> for Message {
|
|
|
|
fn from(event: RedactedRoomMessageEvent) -> Self {
|
|
|
|
let timestamp = event.origin_server_ts.into();
|
|
|
|
let user_id = event.sender.clone();
|
|
|
|
let content = MessageEvent::Redacted(event.into());
|
|
|
|
|
|
|
|
Message::new(content, user_id, timestamp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<RoomMessageEvent> for Message {
|
|
|
|
fn from(event: RoomMessageEvent) -> Self {
|
|
|
|
match event {
|
|
|
|
RoomMessageEvent::Original(ev) => ev.into(),
|
|
|
|
RoomMessageEvent::Redacted(ev) => ev.into(),
|
2022-12-29 18:00:59 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-01 06:02:42 +03:00
|
|
|
impl Display for Message {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "{}", self.event.body())
|
2022-12-29 18:00:59 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
pub mod tests {
|
2024-05-26 01:16:04 +02:00
|
|
|
use matrix_sdk::ruma::events::room::{
|
|
|
|
message::{
|
|
|
|
AudioInfo,
|
|
|
|
AudioMessageEventContent,
|
|
|
|
FileInfo,
|
|
|
|
FileMessageEventContent,
|
|
|
|
ImageMessageEventContent,
|
|
|
|
VideoInfo,
|
|
|
|
VideoMessageEventContent,
|
|
|
|
},
|
|
|
|
ImageInfo,
|
|
|
|
};
|
|
|
|
|
2022-12-29 18:00:59 -08:00
|
|
|
use super::*;
|
|
|
|
use crate::tests::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_mc_cmp() {
|
|
|
|
let mc1 = MessageCursor::from(MSG1_KEY.clone());
|
|
|
|
let mc2 = MessageCursor::from(MSG2_KEY.clone());
|
|
|
|
let mc3 = MessageCursor::from(MSG3_KEY.clone());
|
|
|
|
let mc4 = MessageCursor::from(MSG4_KEY.clone());
|
|
|
|
let mc5 = MessageCursor::from(MSG5_KEY.clone());
|
|
|
|
|
|
|
|
// Everything is equal to itself.
|
|
|
|
assert_eq!(mc1.cmp(&mc1), Ordering::Equal);
|
|
|
|
assert_eq!(mc2.cmp(&mc2), Ordering::Equal);
|
|
|
|
assert_eq!(mc3.cmp(&mc3), Ordering::Equal);
|
|
|
|
assert_eq!(mc4.cmp(&mc4), Ordering::Equal);
|
|
|
|
assert_eq!(mc5.cmp(&mc5), Ordering::Equal);
|
|
|
|
|
|
|
|
// Local echo is always greater than an origin server timestamp.
|
|
|
|
assert_eq!(mc1.cmp(&mc2), Ordering::Greater);
|
|
|
|
assert_eq!(mc1.cmp(&mc3), Ordering::Greater);
|
|
|
|
assert_eq!(mc1.cmp(&mc4), Ordering::Greater);
|
|
|
|
assert_eq!(mc1.cmp(&mc5), Ordering::Greater);
|
|
|
|
|
|
|
|
// mc2 is the smallest timestamp.
|
|
|
|
assert_eq!(mc2.cmp(&mc1), Ordering::Less);
|
|
|
|
assert_eq!(mc2.cmp(&mc3), Ordering::Less);
|
|
|
|
assert_eq!(mc2.cmp(&mc4), Ordering::Less);
|
|
|
|
assert_eq!(mc2.cmp(&mc5), Ordering::Less);
|
|
|
|
|
|
|
|
// mc3 should be less than mc4 because of its event ID.
|
|
|
|
assert_eq!(mc3.cmp(&mc1), Ordering::Less);
|
|
|
|
assert_eq!(mc3.cmp(&mc2), Ordering::Greater);
|
|
|
|
assert_eq!(mc3.cmp(&mc4), Ordering::Less);
|
|
|
|
assert_eq!(mc3.cmp(&mc5), Ordering::Less);
|
|
|
|
|
|
|
|
// mc4 should be greater than mc3 because of its event ID.
|
|
|
|
assert_eq!(mc4.cmp(&mc1), Ordering::Less);
|
|
|
|
assert_eq!(mc4.cmp(&mc2), Ordering::Greater);
|
|
|
|
assert_eq!(mc4.cmp(&mc3), Ordering::Greater);
|
|
|
|
assert_eq!(mc4.cmp(&mc5), Ordering::Less);
|
|
|
|
|
|
|
|
// mc5 is the greatest OriginServer timestamp.
|
|
|
|
assert_eq!(mc5.cmp(&mc1), Ordering::Less);
|
|
|
|
assert_eq!(mc5.cmp(&mc2), Ordering::Greater);
|
|
|
|
assert_eq!(mc5.cmp(&mc3), Ordering::Greater);
|
|
|
|
assert_eq!(mc5.cmp(&mc4), Ordering::Greater);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_mc_to_key() {
|
2024-03-09 00:47:05 -08:00
|
|
|
let messages = mock_messages();
|
2022-12-29 18:00:59 -08:00
|
|
|
let mc1 = MessageCursor::from(MSG1_KEY.clone());
|
|
|
|
let mc2 = MessageCursor::from(MSG2_KEY.clone());
|
|
|
|
let mc3 = MessageCursor::from(MSG3_KEY.clone());
|
|
|
|
let mc4 = MessageCursor::from(MSG4_KEY.clone());
|
|
|
|
let mc5 = MessageCursor::from(MSG5_KEY.clone());
|
|
|
|
let mc6 = MessageCursor::latest();
|
|
|
|
|
2024-03-09 00:47:05 -08:00
|
|
|
let k1 = mc1.to_key(&messages).unwrap();
|
|
|
|
let k2 = mc2.to_key(&messages).unwrap();
|
|
|
|
let k3 = mc3.to_key(&messages).unwrap();
|
|
|
|
let k4 = mc4.to_key(&messages).unwrap();
|
|
|
|
let k5 = mc5.to_key(&messages).unwrap();
|
|
|
|
let k6 = mc6.to_key(&messages).unwrap();
|
2022-12-29 18:00:59 -08:00
|
|
|
|
|
|
|
// These should all be equal to their MSGN_KEYs.
|
|
|
|
assert_eq!(k1, &MSG1_KEY.clone());
|
|
|
|
assert_eq!(k2, &MSG2_KEY.clone());
|
|
|
|
assert_eq!(k3, &MSG3_KEY.clone());
|
|
|
|
assert_eq!(k4, &MSG4_KEY.clone());
|
|
|
|
assert_eq!(k5, &MSG5_KEY.clone());
|
|
|
|
|
|
|
|
// MessageCursor::latest() turns into the largest key (our local echo message).
|
|
|
|
assert_eq!(k6, &MSG1_KEY.clone());
|
|
|
|
|
|
|
|
// MessageCursor::latest() fails to convert for a room w/o messages.
|
2024-03-09 00:47:05 -08:00
|
|
|
let messages_empty = Messages::default();
|
|
|
|
assert_eq!(mc6.to_key(&messages_empty), None);
|
2022-12-29 18:00:59 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_mc_to_from_cursor() {
|
2024-03-09 00:47:05 -08:00
|
|
|
let messages = mock_messages();
|
2022-12-29 18:00:59 -08:00
|
|
|
let mc1 = MessageCursor::from(MSG1_KEY.clone());
|
|
|
|
let mc2 = MessageCursor::from(MSG2_KEY.clone());
|
|
|
|
let mc3 = MessageCursor::from(MSG3_KEY.clone());
|
|
|
|
let mc4 = MessageCursor::from(MSG4_KEY.clone());
|
|
|
|
let mc5 = MessageCursor::from(MSG5_KEY.clone());
|
|
|
|
let mc6 = MessageCursor::latest();
|
|
|
|
|
|
|
|
let identity = |mc: &MessageCursor| {
|
2024-03-09 00:47:05 -08:00
|
|
|
let c = mc.to_cursor(&messages).unwrap();
|
2022-12-29 18:00:59 -08:00
|
|
|
|
2024-03-09 00:47:05 -08:00
|
|
|
MessageCursor::from_cursor(&c, &messages).unwrap()
|
2022-12-29 18:00:59 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
// These should all convert to a Cursor and back to the original value.
|
|
|
|
assert_eq!(identity(&mc1), mc1);
|
|
|
|
assert_eq!(identity(&mc2), mc2);
|
|
|
|
assert_eq!(identity(&mc3), mc3);
|
|
|
|
assert_eq!(identity(&mc4), mc4);
|
|
|
|
assert_eq!(identity(&mc5), mc5);
|
|
|
|
|
|
|
|
// MessageCursor::latest() should point at the most recent message after conversion.
|
|
|
|
assert_eq!(identity(&mc6), mc1);
|
|
|
|
}
|
2023-04-06 16:10:48 -07:00
|
|
|
|
2024-01-28 07:35:07 +00:00
|
|
|
#[test]
|
|
|
|
fn test_placeholder_frame() {
|
|
|
|
fn pretty_frame_test(str: &str) -> Option<String> {
|
|
|
|
Some(str[1..].to_string())
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(
|
2024-03-23 19:41:05 -07:00
|
|
|
placeholder_frame(None, 4, &ImagePreviewSize { width: 4, height: 4 }),
|
2024-01-28 07:35:07 +00:00
|
|
|
pretty_frame_test(
|
|
|
|
r#"
|
|
|
|
⌌ ⌍
|
|
|
|
|
|
|
|
|
|
|
|
⌎ ⌏
|
|
|
|
"#
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2024-03-23 19:41:05 -07:00
|
|
|
assert_eq!(placeholder_frame(None, 2, &ImagePreviewSize { width: 4, height: 4 }), None);
|
|
|
|
assert_eq!(placeholder_frame(None, 4, &ImagePreviewSize { width: 1, height: 4 }), None);
|
2024-01-28 07:35:07 +00:00
|
|
|
|
2024-03-23 19:41:05 -07:00
|
|
|
assert_eq!(placeholder_frame(None, 4, &ImagePreviewSize { width: 4, height: 1 }), None);
|
2024-01-28 07:35:07 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
2024-03-23 19:41:05 -07:00
|
|
|
placeholder_frame(Some("OK"), 4, &ImagePreviewSize { width: 4, height: 4 }),
|
2024-01-28 07:35:07 +00:00
|
|
|
pretty_frame_test(
|
|
|
|
r#"
|
|
|
|
⌌ ⌍
|
|
|
|
OK
|
|
|
|
|
|
|
|
⌎ ⌏
|
|
|
|
"#
|
|
|
|
)
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2024-03-23 19:41:05 -07:00
|
|
|
placeholder_frame(Some("idontfit"), 4, &ImagePreviewSize { width: 4, height: 4 }),
|
2024-01-28 07:35:07 +00:00
|
|
|
pretty_frame_test(
|
|
|
|
r#"
|
|
|
|
⌌ ⌍
|
|
|
|
|
|
|
|
|
|
|
|
⌎ ⌏
|
|
|
|
"#
|
|
|
|
)
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2024-03-23 19:41:05 -07:00
|
|
|
placeholder_frame(Some("OK"), 4, &ImagePreviewSize { width: 4, height: 2 }),
|
2024-01-28 07:35:07 +00:00
|
|
|
pretty_frame_test(
|
|
|
|
r#"
|
|
|
|
⌌ ⌍
|
|
|
|
⌎ ⌏
|
|
|
|
"#
|
|
|
|
)
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2024-03-23 19:41:05 -07:00
|
|
|
placeholder_frame(Some("OK"), 4, &ImagePreviewSize { width: 2, height: 3 }),
|
2024-01-28 07:35:07 +00:00
|
|
|
pretty_frame_test(
|
|
|
|
r#"
|
|
|
|
⌌⌍
|
|
|
|
|
|
|
|
⌎⌏
|
|
|
|
"#
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2024-05-26 01:16:04 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_display_attachment_size() {
|
|
|
|
assert_eq!(
|
|
|
|
body_cow_content(&RoomMessageEventContent::new(MessageType::Image(
|
|
|
|
ImageMessageEventContent::plain(
|
|
|
|
"Alt text".to_string(),
|
|
|
|
"mxc://matrix.org/jDErsDugkNlfavzLTjJNUKAH".into()
|
|
|
|
)
|
2024-08-01 06:02:42 +03:00
|
|
|
.info(Some(Box::default()))
|
2024-05-26 01:16:04 +02:00
|
|
|
))),
|
|
|
|
"[Attached Image: Alt text]".to_string()
|
|
|
|
);
|
|
|
|
|
|
|
|
let mut info = ImageInfo::default();
|
|
|
|
info.size = Some(442630_u32.into());
|
|
|
|
assert_eq!(
|
|
|
|
body_cow_content(&RoomMessageEventContent::new(MessageType::Image(
|
|
|
|
ImageMessageEventContent::plain(
|
|
|
|
"Alt text".to_string(),
|
|
|
|
"mxc://matrix.org/jDErsDugkNlfavzLTjJNUKAH".into()
|
|
|
|
)
|
|
|
|
.info(Some(Box::new(info)))
|
|
|
|
))),
|
|
|
|
"[Attached Image: Alt text (442.63 kB)]".to_string()
|
|
|
|
);
|
|
|
|
|
|
|
|
let mut info = ImageInfo::default();
|
|
|
|
info.size = Some(12_u32.into());
|
|
|
|
assert_eq!(
|
|
|
|
body_cow_content(&RoomMessageEventContent::new(MessageType::Image(
|
|
|
|
ImageMessageEventContent::plain(
|
|
|
|
"Alt text".to_string(),
|
|
|
|
"mxc://matrix.org/jDErsDugkNlfavzLTjJNUKAH".into()
|
|
|
|
)
|
|
|
|
.info(Some(Box::new(info)))
|
|
|
|
))),
|
|
|
|
"[Attached Image: Alt text (12 B)]".to_string()
|
|
|
|
);
|
|
|
|
|
|
|
|
let mut info = AudioInfo::default();
|
|
|
|
info.size = Some(4294967295_u32.into());
|
|
|
|
assert_eq!(
|
|
|
|
body_cow_content(&RoomMessageEventContent::new(MessageType::Audio(
|
|
|
|
AudioMessageEventContent::plain(
|
|
|
|
"Alt text".to_string(),
|
|
|
|
"mxc://matrix.org/jDErsDugkNlfavzLTjJNUKAH".into()
|
|
|
|
)
|
|
|
|
.info(Some(Box::new(info)))
|
|
|
|
))),
|
|
|
|
"[Attached Audio: Alt text (4.29 GB)]".to_string()
|
|
|
|
);
|
|
|
|
|
|
|
|
let mut info = FileInfo::default();
|
|
|
|
info.size = Some(4426300_u32.into());
|
|
|
|
assert_eq!(
|
|
|
|
body_cow_content(&RoomMessageEventContent::new(MessageType::File(
|
|
|
|
FileMessageEventContent::plain(
|
|
|
|
"Alt text".to_string(),
|
|
|
|
"mxc://matrix.org/jDErsDugkNlfavzLTjJNUKAH".into()
|
|
|
|
)
|
|
|
|
.info(Some(Box::new(info)))
|
|
|
|
))),
|
|
|
|
"[Attached File: Alt text (4.43 MB)]".to_string()
|
|
|
|
);
|
|
|
|
|
|
|
|
let mut info = VideoInfo::default();
|
|
|
|
info.size = Some(44000_u32.into());
|
|
|
|
assert_eq!(
|
|
|
|
body_cow_content(&RoomMessageEventContent::new(MessageType::Video(
|
|
|
|
VideoMessageEventContent::plain(
|
|
|
|
"Alt text".to_string(),
|
|
|
|
"mxc://matrix.org/jDErsDugkNlfavzLTjJNUKAH".into()
|
|
|
|
)
|
|
|
|
.info(Some(Box::new(info)))
|
|
|
|
))),
|
|
|
|
"[Attached Video: Alt text (44 kB)]".to_string()
|
|
|
|
);
|
|
|
|
}
|
2022-12-29 18:00:59 -08:00
|
|
|
}
|