1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
use derive_builder::Builder;
use derive_getters::Getters;
use matrix_sdk_base::ruma::events::{
    macros::EventContent,
    room::message::{
        AudioMessageEventContent, FileMessageEventContent, ImageMessageEventContent,
        LocationMessageEventContent, MessageType, VideoMessageEventContent,
    },
};
use serde::{Deserialize, Serialize};

use super::{BelongsTo, Update};
use crate::Result;

// if you change the order of these enum variables, enum value will change and parsing of old content will fail
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(untagged)]
pub enum FallbackAttachmentContent {
    /// An image
    Image(ImageMessageEventContent),
    /// A video message.
    Video(VideoMessageEventContent),
    /// An audio message.
    Audio(AudioMessageEventContent),
    /// A file message.
    File(FileMessageEventContent),
    /// A location
    Location(LocationMessageEventContent),
}

impl FallbackAttachmentContent {
    pub fn name(&self) -> Option<String> {
        match self {
            FallbackAttachmentContent::Image(ImageMessageEventContent { filename, .. }) => {
                filename.clone()
            }
            FallbackAttachmentContent::Video(VideoMessageEventContent { filename, .. }) => {
                filename.clone()
            }
            FallbackAttachmentContent::Audio(AudioMessageEventContent { filename, .. }) => {
                filename.clone()
            }
            FallbackAttachmentContent::File(FileMessageEventContent { filename, .. }) => {
                filename.clone()
            }
            FallbackAttachmentContent::Location(LocationMessageEventContent { body, .. }) => {
                Some(body.clone())
            }
        }
    }
    pub fn type_str(&self) -> String {
        match self {
            FallbackAttachmentContent::Image(_) => "image".to_owned(),
            FallbackAttachmentContent::Video(_) => "video".to_owned(),
            FallbackAttachmentContent::Audio(_) => "audio".to_owned(),
            FallbackAttachmentContent::File(_) => "file".to_owned(),
            FallbackAttachmentContent::Location(_) => "location".to_owned(),
        }
    }

    pub fn image(&self) -> Option<ImageMessageEventContent> {
        match self {
            FallbackAttachmentContent::Image(content) => Some(content.clone()),
            _ => None,
        }
    }

    pub fn video(&self) -> Option<VideoMessageEventContent> {
        match self {
            FallbackAttachmentContent::Video(content) => Some(content.clone()),
            _ => None,
        }
    }

    pub fn audio(&self) -> Option<AudioMessageEventContent> {
        match self {
            FallbackAttachmentContent::Audio(content) => Some(content.clone()),
            _ => None,
        }
    }

    pub fn file(&self) -> Option<FileMessageEventContent> {
        match self {
            FallbackAttachmentContent::File(content) => Some(content.clone()),
            _ => None,
        }
    }

    pub fn location(&self) -> Option<LocationMessageEventContent> {
        match self {
            FallbackAttachmentContent::Location(content) => Some(content.clone()),
            _ => None,
        }
    }
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct LinkAttachmentContent {
    /// A short name for the given link
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,

    /// The actual Link / URL / URI
    pub link: String,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(tag = "type")]
pub enum AttachmentContent {
    /// An image attachment.
    Image(ImageMessageEventContent),
    /// A video attachment.
    Video(VideoMessageEventContent),
    /// An audio attachment.
    Audio(AudioMessageEventContent),
    /// A file attachment
    File(FileMessageEventContent),
    /// A location attachment.
    Location(LocationMessageEventContent),
    /// A link attachment.
    Link(LinkAttachmentContent),
    /// Backwards-compatible fallback support for previous untagged version
    /// only for reading existing events.
    #[serde(untagged)]
    Fallback(FallbackAttachmentContent),
}

impl TryFrom<MessageType> for AttachmentContent {
    type Error = ();

    fn try_from(value: MessageType) -> std::prelude::v1::Result<Self, Self::Error> {
        Ok(match value {
            MessageType::Image(content) => AttachmentContent::Image(content),
            MessageType::Video(content) => AttachmentContent::Video(content),
            MessageType::Audio(content) => AttachmentContent::Audio(content),
            MessageType::File(content) => AttachmentContent::File(content),
            MessageType::Location(content) => AttachmentContent::Location(content),
            _ => return Err(()),
        })
    }
}

impl AttachmentContent {
    pub fn name(&self) -> Option<String> {
        match self {
            AttachmentContent::Image(ImageMessageEventContent { filename, .. }) => filename.clone(),
            AttachmentContent::Video(VideoMessageEventContent { filename, .. }) => filename.clone(),
            AttachmentContent::Audio(AudioMessageEventContent { filename, .. }) => filename.clone(),
            AttachmentContent::File(FileMessageEventContent { filename, .. }) => filename.clone(),
            AttachmentContent::Location(LocationMessageEventContent { body, .. }) => {
                Some(body.clone())
            }
            AttachmentContent::Link(LinkAttachmentContent { name, .. }) => name.clone(),
            AttachmentContent::Fallback(f) => f.name(),
        }
    }

    pub fn type_str(&self) -> String {
        match self {
            AttachmentContent::Image(_) => "image".to_owned(),
            AttachmentContent::Video(_) => "video".to_owned(),
            AttachmentContent::Audio(_) => "audio".to_owned(),
            AttachmentContent::File(_) => "file".to_owned(),
            AttachmentContent::Location(_) => "location".to_owned(),
            AttachmentContent::Link(_) => "link".to_owned(),
            AttachmentContent::Fallback(f) => f.type_str(),
        }
    }

    pub fn image(&self) -> Option<ImageMessageEventContent> {
        match self {
            AttachmentContent::Image(content) => Some(content.clone()),
            AttachmentContent::Fallback(f) => f.image(),
            _ => None,
        }
    }

    pub fn video(&self) -> Option<VideoMessageEventContent> {
        match self {
            AttachmentContent::Video(content) => Some(content.clone()),
            AttachmentContent::Fallback(f) => f.video(),
            _ => None,
        }
    }

    pub fn audio(&self) -> Option<AudioMessageEventContent> {
        match self {
            AttachmentContent::Audio(content) => Some(content.clone()),
            AttachmentContent::Fallback(f) => f.audio(),
            _ => None,
        }
    }

    pub fn file(&self) -> Option<FileMessageEventContent> {
        match self {
            AttachmentContent::File(content) => Some(content.clone()),
            AttachmentContent::Fallback(f) => f.file(),
            _ => None,
        }
    }

    pub fn location(&self) -> Option<LocationMessageEventContent> {
        match self {
            AttachmentContent::Location(content) => Some(content.clone()),
            AttachmentContent::Fallback(f) => f.location(),
            _ => None,
        }
    }

    pub fn link(&self) -> Option<String> {
        if let AttachmentContent::Link(LinkAttachmentContent { link, .. }) = self {
            Some(link.clone())
        } else {
            None
        }
    }
}

/// Attachment Event
#[derive(Clone, Debug, Deserialize, Serialize, EventContent, Builder, Getters)]
#[ruma_event(type = "global.acter.dev.attachment", kind = MessageLike)]
#[builder(name = "AttachmentBuilder", derive(Debug))]
pub struct AttachmentEventContent {
    #[builder(setter(into))]
    #[serde(rename = "m.relates_to")]
    pub on: BelongsTo,

    pub content: AttachmentContent,
}

/// The Attachment Update Event
#[derive(Clone, Debug, Deserialize, Serialize, EventContent, Builder)]
#[ruma_event(type = "global.acter.dev.attachment.update", kind = MessageLike)]
#[builder(name = "AttachmentUpdateBuilder", derive(Debug))]
pub struct AttachmentUpdateEventContent {
    #[builder(setter(into))]
    #[serde(rename = "m.relates_to")]
    pub attachment: Update,

    pub content: AttachmentContent,
}

impl AttachmentUpdateEventContent {
    pub fn apply(&self, task: &mut AttachmentEventContent) -> Result<bool> {
        task.content = self.content.clone();
        Ok(true)
    }
}