BlueGit

ict rusty-teams

Files

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

pending_rows.rs

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