BlueGit

ict rusty-teams

Files

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

outgoing.rs

chore: cargo fmt the workspace
metis Daniel Däschle · pushed by metis · 2 weeks ago
a6592765 History
outgoing.rs 3.5 KiB Download Raw
1use std::sync::Arc;
2
3use chrono::{DateTime, Utc};
4use gpui_kit::Image;
5use serde::{Deserialize, Serialize};
6use teams_core::{
7 AnnouncementBanner, Draft, FileKind, FileReference, HostedImage, LinkPreview, MentionInput,
8 MessageExtras, OBJECT_MARK, UploadedFile,
9};
10
11use store::MessageImportance;
12
13use crate::remote_image::RemoteImage;
14
15#[derive(Debug, Clone, PartialEq, Eq)]
16pub struct InlineImage {
17 pub name: String,
18 pub image: Arc<Image>,
19 pub dimensions: Option<(u32, u32)>,
20}
21
22/// One object in the text: uploaded bytes or a GIF or sticker the message links to.
23#[derive(Debug, Clone, PartialEq, Eq)]
24pub enum OutgoingImage {
25 Inline(InlineImage),
26 Remote(RemoteImage),
27}
28
29#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
30pub struct OutgoingFile {
31 pub name: String,
32 pub size: u64,
33 pub kind: FileKind,
34 pub reference: FileReference,
35 pub uploaded: Option<UploadedFile>,
36}
37
38#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
39pub struct ReplyPreview {
40 pub message_id: String,
41 pub author: String,
42 pub excerpt: String,
43}
44
45#[derive(Debug, Clone, PartialEq, Eq)]
46pub struct EditPreview {
47 pub message_id: String,
48 pub excerpt: String,
49 pub scheduled: Option<DateTime<Utc>>,
50}
51
52#[derive(Debug, Clone, PartialEq, Eq)]
53pub struct Outgoing {
54 pub draft: Draft,
55 pub subject: Option<String>,
56 pub mentions: Vec<MentionInput>,
57 pub reply: Option<ReplyPreview>,
58 pub edit: Option<EditPreview>,
59 pub images: Vec<OutgoingImage>,
60 pub files: Vec<OutgoingFile>,
61 pub link_preview: Option<LinkPreview>,
62 pub importance: MessageImportance,
63 pub banner: Option<AnnouncementBanner>,
64}
65
66impl Outgoing {
67 /// Without list and quote markers, for previews.
68 pub fn text(&self) -> String {
69 self.draft.plain_text()
70 }
71
72 /// The body before mentions become `<at>` tags.
73 pub fn html(&self) -> String {
74 let mut html = String::new();
75 let mut objects = self.images.iter();
76 let mut uploaded = 0;
77 for character in self.draft.to_html().chars() {
78 if character != OBJECT_MARK {
79 html.push(character);
80 continue;
81 }
82 match objects.next() {
83 Some(OutgoingImage::Inline(_)) => {
84 uploaded += 1;
85 html.push_str(&format!(
86 "<img src=\"../hostedContents/{uploaded}/$value\">"
87 ));
88 }
89 Some(OutgoingImage::Remote(remote)) => html.push_str(&remote.html()),
90 None => {}
91 }
92 }
93 teams_core::emoticon_tags(&html, crate::emoticons::send_tag)
94 }
95
96 pub fn has_attachments(&self) -> bool {
97 !self.images.is_empty() || !self.files.is_empty()
98 }
99
100 pub fn extras(&self) -> MessageExtras {
101 MessageExtras {
102 importance: self.importance.stored(),
103 kept: Vec::new(),
104 images: self
105 .images
106 .iter()
107 .filter_map(|outgoing| match outgoing {
108 OutgoingImage::Inline(inline) => Some(HostedImage {
109 content_type: inline.image.format.mime_type().to_owned(),
110 bytes: Arc::new(inline.image.bytes.clone()),
111 }),
112 OutgoingImage::Remote(_) => None,
113 })
114 .collect(),
115 files: self
116 .files
117 .iter()
118 .map(|file| file.reference.clone())
119 .collect(),
120 }
121 }
122}