BlueGit

ict rusty-teams

Files

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

outgoing.rs

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