BlueGit

ict rusty-teams

Files

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

pending_rows.rs

d39bdfed History
pending_rows.rs 3.7 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.with_timezone(&Local).format("%H:%M").to_string(),
40 day_header: None,
41 blocks: pending_blocks(outgoing),
42 edited: false,
43 deleted: false,
44 reactions: Vec::new(),
45 images: outgoing
46 .images
47 .iter()
48 .filter_map(|image| match image {
49 OutgoingImage::Remote(remote) => Some(remote.image_ref()),
50 OutgoingImage::Inline(_) => None,
51 })
52 .collect(),
53 local_images: outgoing
54 .images
55 .iter()
56 .filter_map(|image| match image {
57 OutgoingImage::Inline(inline) => Some(LocalImage {
58 image: inline.image.clone(),
59 size: inline.dimensions,
60 }),
61 OutgoingImage::Remote(_) => None,
62 })
63 .collect(),
64 adaptive_cards: Vec::new(),
65 link_preview: outgoing.link_preview.clone(),
66 meeting_link: teams_core::meeting_link_in_html(&outgoing.html()),
67 files: outgoing
68 .files
69 .iter()
70 .map(|file| FileCard {
71 name: file.name.clone(),
72 kind: file.kind,
73 content_type: None,
74 size: Some(file.size),
75 open_url: file.reference.content_url.clone(),
76 })
77 .collect(),
78 subject: outgoing.subject.clone(),
79 new_marker: false,
80 reply_root: None,
81 delivery,
82 receipt: Receipt::Hidden,
83 own: true,
84 forwarded: false,
85 translation: None,
86 call: None,
87 }
88}
89
90#[cfg(test)]
91mod tests {
92 use teams_core::Draft;
93
94 use super::*;
95 use crate::outgoing::ReplyPreview;
96
97 #[test]
98 fn a_pending_reply_shows_its_quote_before_the_server_echo() {
99 let outgoing = Outgoing {
100 draft: Draft::plain("agreed"),
101 subject: None,
102 mentions: Vec::new(),
103 reply: Some(ReplyPreview {
104 message_id: "message-1".to_owned(),
105 author: "Grace".to_owned(),
106 excerpt: "earlier".to_owned(),
107 }),
108 edit: None,
109 images: Vec::new(),
110 files: Vec::new(),
111 link_preview: None,
112 };
113 let row = pending_row(
114 "key".to_owned(),
115 "chat".to_owned(),
116 None,
117 Utc::now(),
118 &outgoing,
119 Delivery::Sending,
120 );
121 assert!(matches!(row.blocks.first(), Some(Block::Reply { .. })));
122 assert_eq!(row.blocks.len(), 2);
123 }
124}