BlueGit

ict rusty-teams

Files

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

outgoing.rs

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