Commit afe892c7fe
Unregistered key
Layout: unified · split
src/base.rs +28 −3
| @@ -214,6 +214,8 @@ pub type AsyncProgramStore = Arc<AsyncMutex<ProgramStore>>; | ||
| 214 | 214 | |
| 215 | 215 | pub type IambResult<T> = UIResult<T, IambInfo>; |
| 216 | 216 | |
| 217 | pub type Receipts = HashMap<OwnedEventId, Vec<OwnedUserId>>; | |
| 218 | ||
| 217 | 219 | #[derive(thiserror::Error, Debug)] |
| 218 | 220 | pub enum IambError { |
| 219 | 221 | #[error("Invalid user identifier: {0}")] |
| @@ -286,6 +288,9 @@ pub struct RoomInfo { | ||
| 286 | 288 | pub keys: HashMap<OwnedEventId, MessageKey>, |
| 287 | 289 | pub messages: Messages, |
| 288 | 290 | |
| 291 | pub receipts: HashMap<OwnedEventId, Vec<OwnedUserId>>, | |
| 292 | pub read_till: Option<OwnedEventId>, | |
| 293 | ||
| 289 | 294 | pub fetch_id: RoomFetchStatus, |
| 290 | 295 | pub fetch_last: Option<Instant>, |
| 291 | 296 | pub users_typing: Option<(Instant, Vec<OwnedUserId>)>, |
| @@ -316,7 +321,7 @@ impl RoomInfo { | ||
| 316 | 321 | MessageEvent::Original(orig) => { |
| 317 | 322 | orig.content = *new_content; |
| 318 | 323 | }, |
| 319 | MessageEvent::Local(content) => { | |
| 324 | MessageEvent::Local(_, content) => { | |
| 320 | 325 | *content = new_content; |
| 321 | 326 | }, |
| 322 | 327 | MessageEvent::Redacted(_) => { |
| @@ -364,7 +369,7 @@ impl RoomInfo { | ||
| 364 | 369 | } |
| 365 | 370 | } |
| 366 | 371 | |
| 367 | fn get_typing_spans(&self, settings: &ApplicationSettings) -> Spans { | |
| 372 | fn get_typing_spans<'a>(&'a self, settings: &'a ApplicationSettings) -> Spans<'a> { | |
| 368 | 373 | let typers = self.get_typers(); |
| 369 | 374 | let n = typers.len(); |
| 370 | 375 | |
| @@ -454,6 +459,26 @@ impl ChatStore { | ||
| 454 | 459 | .unwrap_or_else(|| "Untitled Matrix Room".to_string()) |
| 455 | 460 | } |
| 456 | 461 | |
| 462 | pub async fn set_receipts(&mut self, receipts: Vec<(OwnedRoomId, Receipts)>) { | |
| 463 | let mut updates = vec![]; | |
| 464 | ||
| 465 | for (room_id, receipts) in receipts.into_iter() { | |
| 466 | if let Some(info) = self.rooms.get_mut(&room_id) { | |
| 467 | info.receipts = receipts; | |
| 468 | ||
| 469 | if let Some(read_till) = info.read_till.take() { | |
| 470 | updates.push((room_id, read_till)); | |
| 471 | } | |
| 472 | } | |
| 473 | } | |
| 474 | ||
| 475 | for (room_id, read_till) in updates.into_iter() { | |
| 476 | if let Some(room) = self.worker.client.get_joined_room(&room_id) { | |
| 477 | let _ = room.read_receipt(read_till.as_ref()).await; | |
| 478 | } | |
| 479 | } | |
| 480 | } | |
| 481 | ||
| 457 | 482 | pub fn mark_for_load(&mut self, room_id: OwnedRoomId) { |
| 458 | 483 | self.need_load.insert(room_id); |
| 459 | 484 | } |
| @@ -588,7 +613,7 @@ impl ApplicationInfo for IambInfo { | ||
| 588 | 613 | #[cfg(test)] |
| 589 | 614 | pub mod tests { |
| 590 | 615 | use super::*; |
| 591 | use crate::config::{user_style, user_style_from_color}; | |
| 616 | use crate::config::user_style_from_color; | |
| 592 | 617 | use crate::tests::*; |
| 593 | 618 | use modalkit::tui::style::Color; |
| 594 | 619 | |
src/config.rs +55 −33
| @@ -1,3 +1,4 @@ | ||
| 1 | use std::borrow::Cow; | |
| 1 | 2 | use std::collections::hash_map::DefaultHasher; |
| 2 | 3 | use std::collections::HashMap; |
| 3 | 4 | use std::fmt; |
| @@ -52,10 +53,6 @@ pub fn user_style_from_color(color: Color) -> Style { | ||
| 52 | 53 | Style::default().fg(color).add_modifier(StyleModifier::BOLD) |
| 53 | 54 | } |
| 54 | 55 | |
| 55 | pub fn user_style(user: &str) -> Style { | |
| 56 | user_style_from_color(user_color(user)) | |
| 57 | } | |
| 58 | ||
| 59 | 56 | fn is_profile_char(c: char) -> bool { |
| 60 | 57 | c.is_ascii_alphanumeric() || c == '.' || c == '-' |
| 61 | 58 | } |
| @@ -181,14 +178,18 @@ fn merge_users(a: Option<UserOverrides>, b: Option<UserOverrides>) -> Option<Use | ||
| 181 | 178 | |
| 182 | 179 | #[derive(Clone)] |
| 183 | 180 | pub struct TunableValues { |
| 184 | pub typing_notice: bool, | |
| 181 | pub read_receipt_send: bool, | |
| 182 | pub read_receipt_display: bool, | |
| 183 | pub typing_notice_send: bool, | |
| 185 | 184 | pub typing_notice_display: bool, |
| 186 | 185 | pub users: UserOverrides, |
| 187 | 186 | } |
| 188 | 187 | |
| 189 | 188 | #[derive(Clone, Default, Deserialize)] |
| 190 | 189 | pub struct Tunables { |
| 191 | pub typing_notice: Option<bool>, | |
| 190 | pub read_receipt_send: Option<bool>, | |
| 191 | pub read_receipt_display: Option<bool>, | |
| 192 | pub typing_notice_send: Option<bool>, | |
| 192 | 193 | pub typing_notice_display: Option<bool>, |
| 193 | 194 | pub users: Option<UserOverrides>, |
| 194 | 195 | } |
| @@ -196,7 +197,9 @@ pub struct Tunables { | ||
| 196 | 197 | impl Tunables { |
| 197 | 198 | fn merge(self, other: Self) -> Self { |
| 198 | 199 | Tunables { |
| 199 | typing_notice: self.typing_notice.or(other.typing_notice), | |
| 200 | read_receipt_send: self.read_receipt_send.or(other.read_receipt_send), | |
| 201 | read_receipt_display: self.read_receipt_display.or(other.read_receipt_display), | |
| 202 | typing_notice_send: self.typing_notice_send.or(other.typing_notice_send), | |
| 200 | 203 | typing_notice_display: self.typing_notice_display.or(other.typing_notice_display), |
| 201 | 204 | users: merge_users(self.users, other.users), |
| 202 | 205 | } |
| @@ -204,8 +207,10 @@ impl Tunables { | ||
| 204 | 207 | |
| 205 | 208 | fn values(self) -> TunableValues { |
| 206 | 209 | TunableValues { |
| 207 | typing_notice: self.typing_notice.unwrap_or(true), | |
| 208 | typing_notice_display: self.typing_notice.unwrap_or(true), | |
| 210 | read_receipt_send: self.read_receipt_send.unwrap_or(true), | |
| 211 | read_receipt_display: self.read_receipt_display.unwrap_or(true), | |
| 212 | typing_notice_send: self.typing_notice_send.unwrap_or(true), | |
| 213 | typing_notice_display: self.typing_notice_display.unwrap_or(true), | |
| 209 | 214 | users: self.users.unwrap_or_default(), |
| 210 | 215 | } |
| 211 | 216 | } |
| @@ -374,24 +379,41 @@ impl ApplicationSettings { | ||
| 374 | 379 | Ok(settings) |
| 375 | 380 | } |
| 376 | 381 | |
| 382 | pub fn get_user_char_span<'a>(&self, user_id: &'a UserId) -> Span<'a> { | |
| 383 | let (color, c) = self | |
| 384 | .tunables | |
| 385 | .users | |
| 386 | .get(user_id) | |
| 387 | .map(|user| { | |
| 388 | ( | |
| 389 | user.color.as_ref().map(|c| c.0), | |
| 390 | user.name.as_ref().and_then(|s| s.chars().next()), | |
| 391 | ) | |
| 392 | }) | |
| 393 | .unwrap_or_default(); | |
| 394 | ||
| 395 | let color = color.unwrap_or_else(|| user_color(user_id.as_str())); | |
| 396 | let style = user_style_from_color(color); | |
| 397 | ||
| 398 | let c = c.unwrap_or_else(|| user_id.localpart().chars().next().unwrap_or(' ')); | |
| 399 | ||
| 400 | Span::styled(String::from(c), style) | |
| 401 | } | |
| 402 | ||
| 377 | 403 | pub fn get_user_span<'a>(&self, user_id: &'a UserId) -> Span<'a> { |
| 378 | if let Some(user) = self.tunables.users.get(user_id) { | |
| 379 | let color = if let Some(UserColor(c)) = user.color { | |
| 380 | c | |
| 381 | } else { | |
| 382 | user_color(user_id.as_str()) | |
| 383 | }; | |
| 384 | ||
| 385 | let style = user_style_from_color(color); | |
| 386 | ||
| 387 | if let Some(name) = &user.name { | |
| 388 | Span::styled(name.clone(), style) | |
| 389 | } else { | |
| 390 | Span::styled(user_id.as_str(), style) | |
| 391 | } | |
| 392 | } else { | |
| 393 | Span::styled(user_id.as_str(), user_style(user_id.as_str())) | |
| 394 | } | |
| 404 | let (color, name) = self | |
| 405 | .tunables | |
| 406 | .users | |
| 407 | .get(user_id) | |
| 408 | .map(|user| (user.color.as_ref().map(|c| c.0), user.name.clone().map(Cow::Owned))) | |
| 409 | .unwrap_or_default(); | |
| 410 | ||
| 411 | let user_id = user_id.as_str(); | |
| 412 | let color = color.unwrap_or_else(|| user_color(user_id)); | |
| 413 | let style = user_style_from_color(color); | |
| 414 | let name = name.unwrap_or(Cow::Borrowed(user_id)); | |
| 415 | ||
| 416 | Span::styled(name, style) | |
| 395 | 417 | } |
| 396 | 418 | } |
| 397 | 419 | |
| @@ -461,22 +483,22 @@ mod tests { | ||
| 461 | 483 | #[test] |
| 462 | 484 | fn test_parse_tunables() { |
| 463 | 485 | let res: Tunables = serde_json::from_str("{}").unwrap(); |
| 464 | assert_eq!(res.typing_notice, None); | |
| 486 | assert_eq!(res.typing_notice_send, None); | |
| 465 | 487 | assert_eq!(res.typing_notice_display, None); |
| 466 | 488 | assert_eq!(res.users, None); |
| 467 | 489 | |
| 468 | let res: Tunables = serde_json::from_str("{\"typing_notice\": true}").unwrap(); | |
| 469 | assert_eq!(res.typing_notice, Some(true)); | |
| 490 | let res: Tunables = serde_json::from_str("{\"typing_notice_send\": true}").unwrap(); | |
| 491 | assert_eq!(res.typing_notice_send, Some(true)); | |
| 470 | 492 | assert_eq!(res.typing_notice_display, None); |
| 471 | 493 | assert_eq!(res.users, None); |
| 472 | 494 | |
| 473 | let res: Tunables = serde_json::from_str("{\"typing_notice\": false}").unwrap(); | |
| 474 | assert_eq!(res.typing_notice, Some(false)); | |
| 495 | let res: Tunables = serde_json::from_str("{\"typing_notice_send\": false}").unwrap(); | |
| 496 | assert_eq!(res.typing_notice_send, Some(false)); | |
| 475 | 497 | assert_eq!(res.typing_notice_display, None); |
| 476 | 498 | assert_eq!(res.users, None); |
| 477 | 499 | |
| 478 | 500 | let res: Tunables = serde_json::from_str("{\"users\": {}}").unwrap(); |
| 479 | assert_eq!(res.typing_notice, None); | |
| 501 | assert_eq!(res.typing_notice_send, None); | |
| 480 | 502 | assert_eq!(res.typing_notice_display, None); |
| 481 | 503 | assert_eq!(res.users, Some(HashMap::new())); |
| 482 | 504 | |
| @@ -484,7 +506,7 @@ mod tests { | ||
| 484 | 506 | "{\"users\": {\"@a:b.c\": {\"color\": \"black\", \"name\": \"Tim\"}}}", |
| 485 | 507 | ) |
| 486 | 508 | .unwrap(); |
| 487 | assert_eq!(res.typing_notice, None); | |
| 509 | assert_eq!(res.typing_notice_send, None); | |
| 488 | 510 | assert_eq!(res.typing_notice_display, None); |
| 489 | 511 | let users = vec![(user_id!("@a:b.c").to_owned(), UserDisplayTunables { |
| 490 | 512 | color: Some(UserColor(Color::Black)), |
src/message/mod.rs +123 −45
| @@ -4,6 +4,7 @@ use std::collections::hash_map::DefaultHasher; | ||
| 4 | 4 | use std::collections::BTreeMap; |
| 5 | 5 | use std::convert::TryFrom; |
| 6 | 6 | use std::hash::{Hash, Hasher}; |
| 7 | use std::slice::Iter; | |
| 7 | 8 | |
| 8 | 9 | use chrono::{DateTime, NaiveDateTime, Utc}; |
| 9 | 10 | use unicode_width::UnicodeWidthStr; |
| @@ -25,6 +26,7 @@ use matrix_sdk::ruma::{ | ||
| 25 | 26 | }, |
| 26 | 27 | Redact, |
| 27 | 28 | }, |
| 29 | EventId, | |
| 28 | 30 | MilliSecondsSinceUnixEpoch, |
| 29 | 31 | OwnedEventId, |
| 30 | 32 | OwnedUserId, |
| @@ -54,20 +56,28 @@ pub type MessageFetchResult = IambResult<(Option<String>, Vec<RoomMessageEvent>) | ||
| 54 | 56 | pub type MessageKey = (MessageTimeStamp, OwnedEventId); |
| 55 | 57 | pub type Messages = BTreeMap<MessageKey, Message>; |
| 56 | 58 | |
| 59 | const fn span_static(s: &'static str) -> Span<'static> { | |
| 60 | Span { | |
| 61 | content: Cow::Borrowed(s), | |
| 62 | style: Style { | |
| 63 | fg: None, | |
| 64 | bg: None, | |
| 65 | add_modifier: StyleModifier::empty(), | |
| 66 | sub_modifier: StyleModifier::empty(), | |
| 67 | }, | |
| 68 | } | |
| 69 | } | |
| 70 | ||
| 57 | 71 | const USER_GUTTER: usize = 30; |
| 58 | 72 | const TIME_GUTTER: usize = 12; |
| 73 | const READ_GUTTER: usize = 5; | |
| 59 | 74 | const MIN_MSG_LEN: usize = 30; |
| 60 | 75 | |
| 61 | 76 | const USER_GUTTER_EMPTY: &str = " "; |
| 62 | const USER_GUTTER_EMPTY_SPAN: Span<'static> = Span { | |
| 63 | content: Cow::Borrowed(USER_GUTTER_EMPTY), | |
| 64 | style: Style { | |
| 65 | fg: None, | |
| 66 | bg: None, | |
| 67 | add_modifier: StyleModifier::empty(), | |
| 68 | sub_modifier: StyleModifier::empty(), | |
| 69 | }, | |
| 70 | }; | |
| 77 | const USER_GUTTER_EMPTY_SPAN: Span<'static> = span_static(USER_GUTTER_EMPTY); | |
| 78 | ||
| 79 | const TIME_GUTTER_EMPTY: &str = " "; | |
| 80 | const TIME_GUTTER_EMPTY_SPAN: Span<'static> = span_static(TIME_GUTTER_EMPTY); | |
| 71 | 81 | |
| 72 | 82 | #[derive(thiserror::Error, Debug)] |
| 73 | 83 | pub enum TimeStampIntError { |
| @@ -271,10 +281,18 @@ impl PartialOrd for MessageCursor { | ||
| 271 | 281 | pub enum MessageEvent { |
| 272 | 282 | Original(Box<OriginalRoomMessageEvent>), |
| 273 | 283 | Redacted(Box<RedactedRoomMessageEvent>), |
| 274 | Local(Box<RoomMessageEventContent>), | |
| 284 | Local(OwnedEventId, Box<RoomMessageEventContent>), | |
| 275 | 285 | } |
| 276 | 286 | |
| 277 | 287 | impl MessageEvent { |
| 288 | pub fn event_id(&self) -> &EventId { | |
| 289 | match self { | |
| 290 | MessageEvent::Original(ev) => ev.event_id.as_ref(), | |
| 291 | MessageEvent::Redacted(ev) => ev.event_id.as_ref(), | |
| 292 | MessageEvent::Local(event_id, _) => event_id.as_ref(), | |
| 293 | } | |
| 294 | } | |
| 295 | ||
| 278 | 296 | pub fn body(&self) -> Cow<'_, str> { |
| 279 | 297 | match self { |
| 280 | 298 | MessageEvent::Original(ev) => body_cow_content(&ev.content), |
| @@ -292,7 +310,7 @@ impl MessageEvent { | ||
| 292 | 310 | Cow::Borrowed("[Redacted]") |
| 293 | 311 | } |
| 294 | 312 | }, |
| 295 | MessageEvent::Local(content) => body_cow_content(content), | |
| 313 | MessageEvent::Local(_, content) => body_cow_content(content), | |
| 296 | 314 | } |
| 297 | 315 | } |
| 298 | 316 | |
| @@ -300,7 +318,7 @@ impl MessageEvent { | ||
| 300 | 318 | let content = match self { |
| 301 | 319 | MessageEvent::Original(ev) => &ev.content, |
| 302 | 320 | MessageEvent::Redacted(_) => return None, |
| 303 | MessageEvent::Local(content) => content, | |
| 321 | MessageEvent::Local(_, content) => content, | |
| 304 | 322 | }; |
| 305 | 323 | |
| 306 | 324 | if let MessageType::Text(content) = &content.msgtype { |
| @@ -317,7 +335,7 @@ impl MessageEvent { | ||
| 317 | 335 | pub fn redact(&mut self, redaction: SyncRoomRedactionEvent, version: &RoomVersionId) { |
| 318 | 336 | match self { |
| 319 | 337 | MessageEvent::Redacted(_) => return, |
| 320 | MessageEvent::Local(_) => return, | |
| 338 | MessageEvent::Local(_, _) => return, | |
| 321 | 339 | MessageEvent::Original(ev) => { |
| 322 | 340 | let redacted = ev.clone().redact(redaction, version); |
| 323 | 341 | *self = MessageEvent::Redacted(Box::new(redacted)); |
| @@ -354,27 +372,65 @@ fn body_cow_content(content: &RoomMessageEventContent) -> Cow<'_, str> { | ||
| 354 | 372 | Cow::Borrowed(s) |
| 355 | 373 | } |
| 356 | 374 | |
| 357 | enum MessageColumns<'a> { | |
| 358 | Three(usize, Option<Span<'a>>, Option<Span<'a>>), | |
| 359 | Two(usize, Option<Span<'a>>), | |
| 360 | One(usize, Option<Span<'a>>), | |
| 375 | enum MessageColumns { | |
| 376 | /// Four columns: sender, message, timestamp, read receipts. | |
| 377 | Four, | |
| 378 | ||
| 379 | /// Three columns: sender, message, timestamp. | |
| 380 | Three, | |
| 381 | ||
| 382 | /// Two columns: sender, message. | |
| 383 | Two, | |
| 384 | ||
| 385 | /// One column: message with sender on line before the message. | |
| 386 | One, | |
| 361 | 387 | } |
| 362 | 388 | |
| 363 | impl<'a> MessageColumns<'a> { | |
| 389 | struct MessageFormatter<'a> { | |
| 390 | settings: &'a ApplicationSettings, | |
| 391 | cols: MessageColumns, | |
| 392 | fill: usize, | |
| 393 | user: Option<Span<'a>>, | |
| 394 | time: Option<Span<'a>>, | |
| 395 | read: Iter<'a, OwnedUserId>, | |
| 396 | } | |
| 397 | ||
| 398 | impl<'a> MessageFormatter<'a> { | |
| 364 | 399 | fn width(&self) -> usize { |
| 365 | match self { | |
| 366 | MessageColumns::Three(fill, _, _) => *fill, | |
| 367 | MessageColumns::Two(fill, _) => *fill, | |
| 368 | MessageColumns::One(fill, _) => *fill, | |
| 369 | } | |
| 400 | self.fill | |
| 370 | 401 | } |
| 371 | 402 | |
| 372 | 403 | #[inline] |
| 373 | 404 | fn push_spans(&mut self, spans: Spans<'a>, style: Style, text: &mut Text<'a>) { |
| 374 | match self { | |
| 375 | MessageColumns::Three(_, user, time) => { | |
| 376 | let user = user.take().unwrap_or(USER_GUTTER_EMPTY_SPAN); | |
| 377 | let time = time.take().unwrap_or_else(|| Span::from("")); | |
| 405 | match self.cols { | |
| 406 | MessageColumns::Four => { | |
| 407 | let settings = self.settings; | |
| 408 | let user = self.user.take().unwrap_or(USER_GUTTER_EMPTY_SPAN); | |
| 409 | let time = self.time.take().unwrap_or(TIME_GUTTER_EMPTY_SPAN); | |
| 410 | ||
| 411 | let mut line = vec![user]; | |
| 412 | line.extend(spans.0); | |
| 413 | line.push(time); | |
| 414 | ||
| 415 | // Show read receipts. | |
| 416 | let user_char = | |
| 417 | |user: &'a OwnedUserId| -> Span<'a> { settings.get_user_char_span(user) }; | |
| 418 | ||
| 419 | let a = self.read.next().map(user_char).unwrap_or_else(|| Span::raw(" ")); | |
| 420 | let b = self.read.next().map(user_char).unwrap_or_else(|| Span::raw(" ")); | |
| 421 | let c = self.read.next().map(user_char).unwrap_or_else(|| Span::raw(" ")); | |
| 422 | ||
| 423 | line.push(Span::raw(" ")); | |
| 424 | line.push(c); | |
| 425 | line.push(b); | |
| 426 | line.push(a); | |
| 427 | line.push(Span::raw(" ")); | |
| 428 | ||
| 429 | text.lines.push(Spans(line)) | |
| 430 | }, | |
| 431 | MessageColumns::Three => { | |
| 432 | let user = self.user.take().unwrap_or(USER_GUTTER_EMPTY_SPAN); | |
| 433 | let time = self.time.take().unwrap_or_else(|| Span::from("")); | |
| 378 | 434 | |
| 379 | 435 | let mut line = vec![user]; |
| 380 | 436 | line.extend(spans.0); |
| @@ -382,15 +438,15 @@ impl<'a> MessageColumns<'a> { | ||
| 382 | 438 | |
| 383 | 439 | text.lines.push(Spans(line)) |
| 384 | 440 | }, |
| 385 | MessageColumns::Two(_, opt) => { | |
| 386 | let user = opt.take().unwrap_or(USER_GUTTER_EMPTY_SPAN); | |
| 441 | MessageColumns::Two => { | |
| 442 | let user = self.user.take().unwrap_or(USER_GUTTER_EMPTY_SPAN); | |
| 387 | 443 | let mut line = vec![user]; |
| 388 | 444 | line.extend(spans.0); |
| 389 | 445 | |
| 390 | 446 | text.lines.push(Spans(line)); |
| 391 | 447 | }, |
| 392 | MessageColumns::One(_, opt) => { | |
| 393 | if let Some(user) = opt.take() { | |
| 448 | MessageColumns::One => { | |
| 449 | if let Some(user) = self.user.take() { | |
| 394 | 450 | text.lines.push(Spans(vec![user])); |
| 395 | 451 | } |
| 396 | 452 | |
| @@ -428,7 +484,7 @@ impl Message { | ||
| 428 | 484 | |
| 429 | 485 | pub fn reply_to(&self) -> Option<OwnedEventId> { |
| 430 | 486 | let content = match &self.event { |
| 431 | MessageEvent::Local(content) => content, | |
| 487 | MessageEvent::Local(_, content) => content, | |
| 432 | 488 | MessageEvent::Original(ev) => &ev.content, |
| 433 | 489 | MessageEvent::Redacted(_) => return None, |
| 434 | 490 | }; |
| @@ -454,28 +510,50 @@ impl Message { | ||
| 454 | 510 | return style; |
| 455 | 511 | } |
| 456 | 512 | |
| 457 | fn get_render_format( | |
| 458 | &self, | |
| 513 | fn get_render_format<'a>( | |
| 514 | &'a self, | |
| 459 | 515 | prev: Option<&Message>, |
| 460 | 516 | width: usize, |
| 461 | settings: &ApplicationSettings, | |
| 462 | ) -> MessageColumns { | |
| 463 | if USER_GUTTER + TIME_GUTTER + MIN_MSG_LEN <= width { | |
| 464 | let lw = width - USER_GUTTER - TIME_GUTTER; | |
| 517 | info: &'a RoomInfo, | |
| 518 | settings: &'a ApplicationSettings, | |
| 519 | ) -> MessageFormatter<'a> { | |
| 520 | if USER_GUTTER + TIME_GUTTER + READ_GUTTER + MIN_MSG_LEN <= width && | |
| 521 | settings.tunables.read_receipt_display | |
| 522 | { | |
| 523 | let cols = MessageColumns::Four; | |
| 524 | let fill = width - USER_GUTTER - TIME_GUTTER - READ_GUTTER; | |
| 525 | let user = self.show_sender(prev, true, settings); | |
| 526 | let time = self.timestamp.show(); | |
| 527 | let read = match info.receipts.get(self.event.event_id()) { | |
| 528 | Some(read) => read.iter(), | |
| 529 | None => [].iter(), | |
| 530 | }; | |
| 531 | ||
| 532 | MessageFormatter { settings, cols, fill, user, time, read } | |
| 533 | } else if USER_GUTTER + TIME_GUTTER + MIN_MSG_LEN <= width { | |
| 534 | let cols = MessageColumns::Three; | |
| 535 | let fill = width - USER_GUTTER - TIME_GUTTER; | |
| 465 | 536 | let user = self.show_sender(prev, true, settings); |
| 466 | 537 | let time = self.timestamp.show(); |
| 538 | let read = [].iter(); | |
| 467 | 539 | |
| 468 | MessageColumns::Three(lw, user, time) | |
| 540 | MessageFormatter { settings, cols, fill, user, time, read } | |
| 469 | 541 | } else if USER_GUTTER + MIN_MSG_LEN <= width { |
| 470 | let lw = width - USER_GUTTER; | |
| 542 | let cols = MessageColumns::Two; | |
| 543 | let fill = width - USER_GUTTER; | |
| 471 | 544 | let user = self.show_sender(prev, true, settings); |
| 545 | let time = None; | |
| 546 | let read = [].iter(); | |
| 472 | 547 | |
| 473 | MessageColumns::Two(lw, user) | |
| 548 | MessageFormatter { settings, cols, fill, user, time, read } | |
| 474 | 549 | } else { |
| 475 | let lw = width.saturating_sub(2); | |
| 550 | let cols = MessageColumns::One; | |
| 551 | let fill = width.saturating_sub(2); | |
| 476 | 552 | let user = self.show_sender(prev, false, settings); |
| 553 | let time = None; | |
| 554 | let read = [].iter(); | |
| 477 | 555 | |
| 478 | MessageColumns::One(lw, user) | |
| 556 | MessageFormatter { settings, cols, fill, user, time, read } | |
| 479 | 557 | } |
| 480 | 558 | } |
| 481 | 559 | |
| @@ -485,12 +563,12 @@ impl Message { | ||
| 485 | 563 | selected: bool, |
| 486 | 564 | vwctx: &ViewportContext<MessageCursor>, |
| 487 | 565 | info: &'a RoomInfo, |
| 488 | settings: &ApplicationSettings, | |
| 566 | settings: &'a ApplicationSettings, | |
| 489 | 567 | ) -> Text<'a> { |
| 490 | 568 | let width = vwctx.get_width(); |
| 491 | 569 | |
| 492 | 570 | let style = self.get_render_style(selected); |
| 493 | let mut fmt = self.get_render_format(prev, width, settings); | |
| 571 | let mut fmt = self.get_render_format(prev, width, info, settings); | |
| 494 | 572 | let mut text = Text { lines: vec![] }; |
| 495 | 573 | let width = fmt.width(); |
| 496 | 574 | |
src/tests.rs +15 −4
| @@ -15,13 +15,15 @@ use matrix_sdk::ruma::{ | ||
| 15 | 15 | }; |
| 16 | 16 | |
| 17 | 17 | use lazy_static::lazy_static; |
| 18 | use modalkit::tui::style::Color; | |
| 18 | use modalkit::tui::style::{Color, Style}; | |
| 19 | 19 | use tokio::sync::mpsc::unbounded_channel; |
| 20 | 20 | use url::Url; |
| 21 | 21 | |
| 22 | 22 | use crate::{ |
| 23 | 23 | base::{ChatStore, ProgramStore, RoomFetchStatus, RoomInfo}, |
| 24 | 24 | config::{ |
| 25 | user_color, | |
| 26 | user_style_from_color, | |
| 25 | 27 | ApplicationSettings, |
| 26 | 28 | DirectoryValues, |
| 27 | 29 | ProfileConfig, |
| @@ -60,6 +62,10 @@ lazy_static! { | ||
| 60 | 62 | pub static ref MSG5_KEY: MessageKey = (OriginServer(UInt::new(8).unwrap()), MSG5_EVID.clone()); |
| 61 | 63 | } |
| 62 | 64 | |
| 65 | pub fn user_style(user: &str) -> Style { | |
| 66 | user_style_from_color(user_color(user)) | |
| 67 | } | |
| 68 | ||
| 63 | 69 | pub fn mock_room1_message( |
| 64 | 70 | content: RoomMessageEventContent, |
| 65 | 71 | sender: OwnedUserId, |
| @@ -82,7 +88,7 @@ pub fn mock_room1_message( | ||
| 82 | 88 | |
| 83 | 89 | pub fn mock_message1() -> Message { |
| 84 | 90 | let content = RoomMessageEventContent::text_plain("writhe"); |
| 85 | let content = MessageEvent::Local(content.into()); | |
| 91 | let content = MessageEvent::Local(MSG1_EVID.clone(), content.into()); | |
| 86 | 92 | |
| 87 | 93 | Message::new(content, TEST_USER1.clone(), MSG1_KEY.0) |
| 88 | 94 | } |
| @@ -138,10 +144,13 @@ pub fn mock_messages() -> Messages { | ||
| 138 | 144 | pub fn mock_room() -> RoomInfo { |
| 139 | 145 | RoomInfo { |
| 140 | 146 | name: Some("Watercooler Discussion".into()), |
| 147 | tags: None, | |
| 141 | 148 | |
| 142 | 149 | keys: mock_keys(), |
| 143 | 150 | messages: mock_messages(), |
| 144 | tags: None, | |
| 151 | ||
| 152 | receipts: HashMap::new(), | |
| 153 | read_till: None, | |
| 145 | 154 | |
| 146 | 155 | fetch_id: RoomFetchStatus::NotStarted, |
| 147 | 156 | fetch_last: None, |
| @@ -159,7 +168,9 @@ pub fn mock_dirs() -> DirectoryValues { | ||
| 159 | 168 | |
| 160 | 169 | pub fn mock_tunables() -> TunableValues { |
| 161 | 170 | TunableValues { |
| 162 | typing_notice: true, | |
| 171 | read_receipt_send: true, | |
| 172 | read_receipt_display: true, | |
| 173 | typing_notice_send: true, | |
| 163 | 174 | typing_notice_display: true, |
| 164 | 175 | users: vec![(TEST_USER5.clone(), UserDisplayTunables { |
| 165 | 176 | color: Some(UserColor(Color::Black)), |
src/windows/room/chat.rs +5 −7
| @@ -241,7 +241,7 @@ impl ChatState { | ||
| 241 | 241 | |
| 242 | 242 | let ev = match &msg.event { |
| 243 | 243 | MessageEvent::Original(ev) => &ev.content, |
| 244 | MessageEvent::Local(ev) => ev.deref(), | |
| 244 | MessageEvent::Local(_, ev) => ev.deref(), | |
| 245 | 245 | _ => { |
| 246 | 246 | let msg = "Cannot edit a redacted message"; |
| 247 | 247 | let err = UIError::Failure(msg.into()); |
| @@ -275,9 +275,7 @@ impl ChatState { | ||
| 275 | 275 | |
| 276 | 276 | let event_id = match &msg.event { |
| 277 | 277 | MessageEvent::Original(ev) => ev.event_id.clone(), |
| 278 | MessageEvent::Local(_) => { | |
| 279 | self.scrollback.get_key(info).ok_or(IambError::NoSelectedMessage)?.1 | |
| 280 | }, | |
| 278 | MessageEvent::Local(event_id, _) => event_id.clone(), | |
| 281 | 279 | MessageEvent::Redacted(_) => { |
| 282 | 280 | let msg = ""; |
| 283 | 281 | let err = UIError::Failure(msg.into()); |
| @@ -378,8 +376,8 @@ impl ChatState { | ||
| 378 | 376 | |
| 379 | 377 | if show_echo { |
| 380 | 378 | let user = store.application.settings.profile.user_id.clone(); |
| 381 | let key = (MessageTimeStamp::LocalEcho, event_id); | |
| 382 | let msg = MessageEvent::Local(msg.into()); | |
| 379 | let key = (MessageTimeStamp::LocalEcho, event_id.clone()); | |
| 380 | let msg = MessageEvent::Local(event_id, msg.into()); | |
| 383 | 381 | let msg = Message::new(msg, user, MessageTimeStamp::LocalEcho); |
| 384 | 382 | info.messages.insert(key, msg); |
| 385 | 383 | } |
| @@ -415,7 +413,7 @@ impl ChatState { | ||
| 415 | 413 | return; |
| 416 | 414 | } |
| 417 | 415 | |
| 418 | if !store.application.settings.tunables.typing_notice { | |
| 416 | if !store.application.settings.tunables.typing_notice_send { | |
| 419 | 417 | return; |
| 420 | 418 | } |
| 421 | 419 | |
src/windows/room/scrollback.rs +7 −1
| @@ -1260,7 +1260,13 @@ impl<'a> StatefulWidget for Scrollback<'a> { | ||
| 1260 | 1260 | y += 1; |
| 1261 | 1261 | } |
| 1262 | 1262 | |
| 1263 | let first_key = info.messages.first_key_value().map(|f| f.0.clone()); | |
| 1263 | if settings.tunables.read_receipt_send && state.cursor.timestamp.is_none() { | |
| 1264 | // If the cursor is at the last message, then update the read marker. | |
| 1265 | info.read_till = info.messages.last_key_value().map(|(k, _)| k.1.clone()); | |
| 1266 | } | |
| 1267 | ||
| 1268 | // Check whether we should load older messages for this room. | |
| 1269 | let first_key = info.messages.first_key_value().map(|(k, _)| k.clone()); | |
| 1264 | 1270 | if first_key == state.viewctx.corner.timestamp { |
| 1265 | 1271 | // If the top of the screen is the older message, load more. |
| 1266 | 1272 | self.store.application.mark_for_load(state.room_id.clone()); |
src/worker.rs +38 −2
| @@ -57,7 +57,7 @@ use matrix_sdk::{ | ||
| 57 | 57 | use modalkit::editing::action::{EditInfo, InfoMessage, UIError}; |
| 58 | 58 | |
| 59 | 59 | use crate::{ |
| 60 | base::{AsyncProgramStore, IambError, IambResult, VerifyAction}, | |
| 60 | base::{AsyncProgramStore, IambError, IambResult, Receipts, VerifyAction}, | |
| 61 | 61 | message::MessageFetchResult, |
| 62 | 62 | ApplicationSettings, |
| 63 | 63 | }; |
| @@ -99,6 +99,29 @@ fn oneshot<T>() -> (ClientReply<T>, ClientResponse<T>) { | ||
| 99 | 99 | return (reply, response); |
| 100 | 100 | } |
| 101 | 101 | |
| 102 | async fn update_receipts(client: &Client) -> Vec<(OwnedRoomId, Receipts)> { | |
| 103 | let mut rooms = vec![]; | |
| 104 | ||
| 105 | for room in client.joined_rooms() { | |
| 106 | if let Ok(users) = room.active_members_no_sync().await { | |
| 107 | let mut receipts = Receipts::new(); | |
| 108 | ||
| 109 | for member in users { | |
| 110 | let res = room.user_read_receipt(member.user_id()).await; | |
| 111 | ||
| 112 | if let Ok(Some((event_id, _))) = res { | |
| 113 | let user_id = member.user_id().to_owned(); | |
| 114 | receipts.entry(event_id).or_default().push(user_id); | |
| 115 | } | |
| 116 | } | |
| 117 | ||
| 118 | rooms.push((room.room_id().to_owned(), receipts)); | |
| 119 | } | |
| 120 | } | |
| 121 | ||
| 122 | return rooms; | |
| 123 | } | |
| 124 | ||
| 102 | 125 | pub type FetchedRoom = (MatrixRoom, DisplayName, Option<Tags>); |
| 103 | 126 | |
| 104 | 127 | pub enum WorkerTask { |
| @@ -454,7 +477,7 @@ impl ClientWorker { | ||
| 454 | 477 | } |
| 455 | 478 | |
| 456 | 479 | async fn init(&mut self, store: AsyncProgramStore) { |
| 457 | self.client.add_event_handler_context(store); | |
| 480 | self.client.add_event_handler_context(store.clone()); | |
| 458 | 481 | |
| 459 | 482 | let _ = self.client.add_event_handler( |
| 460 | 483 | |ev: SyncTypingEvent, room: MatrixRoom, store: Ctx<AsyncProgramStore>| { |
| @@ -661,6 +684,19 @@ impl ClientWorker { | ||
| 661 | 684 | }, |
| 662 | 685 | ); |
| 663 | 686 | |
| 687 | let client = self.client.clone(); | |
| 688 | let _ = tokio::spawn(async move { | |
| 689 | // Update the displayed read receipts ever 5 seconds. | |
| 690 | let mut interval = tokio::time::interval(Duration::from_secs(5)); | |
| 691 | ||
| 692 | loop { | |
| 693 | interval.tick().await; | |
| 694 | ||
| 695 | let receipts = update_receipts(&client).await; | |
| 696 | store.lock().await.application.set_receipts(receipts).await; | |
| 697 | } | |
| 698 | }); | |
| 699 | ||
| 664 | 700 | self.initialized = true; |
| 665 | 701 | } |
| 666 | 702 | |