BlueGit

ict rusty-teams

Files

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

outgoing.rs

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