BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / pending_rows.rs

pending_rows.rs

pending_rows.rs 3.8 KiB Download Raw
1use chrono::{DateTime, Local, Utc};
2use teams_core::{FileCard, QuoteInfo};
3
4use crate::render::{Block, layout_blocks};
5use crate::rows::{Delivery, LocalImage, MessageRow, Receipt, Series};
6use crate::views::attachment_tray::OutgoingImage;
7use crate::views::composer::Outgoing;
8
9fn pending_blocks(outgoing: &Outgoing) -> Vec<Block> {
10 let quote = outgoing.reply.as_ref().map(|reply| {
11 teams_core::quote_html(&QuoteInfo {
12 message_id: reply.message_id.clone(),
13 preview: reply.excerpt.clone(),
14 sender_name: Some(reply.author.clone()),
15 })
16 });
17 if quote.is_none() && outgoing.draft.is_blank() && outgoing.images.is_empty() {
18 return Vec::new();
19 }
20 let html = quote.unwrap_or_default() + &outgoing.html();
21 layout_blocks(&teams_core::html_to_spans(&html))
22}
23
24pub fn pending_row(
25 key: String,
26 conversation_id: String,
27 my_user_id: Option<String>,
28 created_at: DateTime<Utc>,
29 outgoing: &Outgoing,
30 delivery: Delivery,
31) -> MessageRow {
32 MessageRow {
33 key,
34 conversation_id,
35 author: "You".to_owned(),
36 sender_id: my_user_id,
37 application_id: None,
38 created_at,
39 series: Series::default(),
40 time: created_at.with_timezone(&Local).format("%H:%M").to_string(),
41 day_header: None,
42 blocks: pending_blocks(outgoing),
43 edited: false,
44 deleted: false,
45 reactions: Vec::new(),
46 images: outgoing
47 .images
48 .iter()
49 .filter_map(|image| match image {
50 OutgoingImage::Remote(remote) => Some(remote.image_ref()),
51 OutgoingImage::Inline(_) => None,
52 })
53 .collect(),
54 local_images: outgoing
55 .images
56 .iter()
57 .filter_map(|image| match image {
58 OutgoingImage::Inline(inline) => Some(LocalImage {
59 image: inline.image.clone(),
60 size: inline.dimensions,
61 }),
62 OutgoingImage::Remote(_) => None,
63 })
64 .collect(),
65 adaptive_cards: Vec::new(),
66 link_preview: outgoing.link_preview.clone(),
67 meeting_link: teams_core::meeting_link_in_html(&outgoing.html()),
68 files: outgoing
69 .files
70 .iter()
71 .map(|file| FileCard {
72 name: file.name.clone(),
73 kind: file.kind,
74 content_type: None,
75 size: Some(file.size),
76 open_url: file.reference.content_url.clone(),
77 })
78 .collect(),
79 subject: outgoing.subject.clone(),
80 new_marker: false,
81 reply_root: None,
82 delivery,
83 receipt: Receipt::Hidden,
84 own: true,
85 forwarded: false,
86 translation: None,
87 call: None,
88 }
89}
90
91#[cfg(test)]
92mod tests {
93 use teams_core::Draft;
94
95 use super::*;
96 use crate::views::composer::ReplyPreview;
97
98 #[test]
99 fn a_pending_reply_shows_its_quote_before_the_server_echo() {
100 let outgoing = Outgoing {
101 draft: Draft::plain("agreed"),
102 subject: None,
103 mentions: Vec::new(),
104 reply: Some(ReplyPreview {
105 message_id: "message-1".to_owned(),
106 author: "Grace".to_owned(),
107 excerpt: "earlier".to_owned(),
108 }),
109 edit: None,
110 images: Vec::new(),
111 files: Vec::new(),
112 link_preview: None,
113 };
114 let row = pending_row(
115 "key".to_owned(),
116 "chat".to_owned(),
117 None,
118 Utc::now(),
119 &outgoing,
120 Delivery::Sending,
121 );
122 assert!(matches!(row.blocks.first(), Some(Block::Reply(_))));
123 assert_eq!(row.blocks.len(), 2);
124 }
125}