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 Draft, FileKind, FileReference, HostedImage, LinkPreview, MentionInput, MessageExtras,
8 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}
64
65impl Outgoing {
66 /// Without list and quote markers, for previews.
67 pub fn text(&self) -> String {
68 self.draft.plain_text()
69 }
70
71 /// The body before mentions become `<at>` tags.
72 pub fn html(&self) -> String {
73 let mut html = String::new();
74 let mut objects = self.images.iter();
75 let mut uploaded = 0;
76 for character in self.draft.to_html().chars() {
77 if character != OBJECT_MARK {
78 html.push(character);
79 continue;
80 }
81 match objects.next() {
82 Some(OutgoingImage::Inline(_)) => {
83 uploaded += 1;
84 html.push_str(&format!(
85 "<img src=\"../hostedContents/{uploaded}/$value\">"
86 ));
87 }
88 Some(OutgoingImage::Remote(remote)) => html.push_str(&remote.html()),
89 None => {}
90 }
91 }
92 teams_core::emoticon_tags(&html, crate::emoticons::send_tag)
93 }
94
95 pub fn has_attachments(&self) -> bool {
96 !self.images.is_empty() || !self.files.is_empty()
97 }
98
99 pub fn extras(&self) -> MessageExtras {
100 MessageExtras {
101 importance: self.importance.stored(),
102 kept: Vec::new(),
103 images: self
104 .images
105 .iter()
106 .filter_map(|outgoing| match outgoing {
107 OutgoingImage::Inline(inline) => Some(HostedImage {
108 content_type: inline.image.format.mime_type().to_owned(),
109 bytes: Arc::new(inline.image.bytes.clone()),
110 }),
111 OutgoingImage::Remote(_) => None,
112 })
113 .collect(),
114 files: self
115 .files
116 .iter()
117 .map(|file| file.reference.clone())
118 .collect(),
119 }
120 }
121}