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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
use derive_builder::Builder;
use derive_getters::Getters;
use matrix_sdk_base::ruma::events::macros::EventContent;
use matrix_sdk_base::ruma::events::room::message::{
    AudioMessageEventContent, FileMessageEventContent, ImageMessageEventContent,
    LocationMessageEventContent, TextMessageEventContent, VideoMessageEventContent,
};
use serde::{Deserialize, Serialize};

use super::{Colorize, ObjRef, Update};
use crate::{util::deserialize_some, 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 FallbackNewsContent {
    /// An image message.
    Image(ImageMessageEventContent),
    /// A video message.
    Video(VideoMessageEventContent),
    /// A text message.
    Text(TextMessageEventContent),
    /// An audio message.
    Audio(AudioMessageEventContent),
    /// A file message.
    File(FileMessageEventContent),
    /// A location message.
    Location(LocationMessageEventContent),
}

impl FallbackNewsContent {
    pub fn type_str(&self) -> String {
        match self {
            FallbackNewsContent::Audio(_) => "audio".to_owned(),
            FallbackNewsContent::File(_) => "file".to_owned(),
            FallbackNewsContent::Image(_) => "image".to_owned(),
            FallbackNewsContent::Location(_) => "location".to_owned(),
            FallbackNewsContent::Text(_) => "text".to_owned(),
            FallbackNewsContent::Video(_) => "video".to_owned(),
        }
    }

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

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

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

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

    pub fn text(&self) -> Option<TextMessageEventContent> {
        match self {
            FallbackNewsContent::Text(content) => Some(content.clone()),
            _ => None,
        }
    }

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

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

impl NewsContent {
    pub fn type_str(&self) -> String {
        match self {
            NewsContent::File(_) => "file".to_owned(),
            NewsContent::Image(_) => "image".to_owned(),
            NewsContent::Location(_) => "location".to_owned(),
            NewsContent::Text(_) => "text".to_owned(),
            NewsContent::Audio(_) => "audio".to_owned(),
            NewsContent::Video(_) => "video".to_owned(),
            NewsContent::Fallback(f) => f.type_str(),
        }
    }
    pub fn text_str(&self) -> String {
        match self {
            NewsContent::Image(ImageMessageEventContent { body, .. })
            | NewsContent::Fallback(FallbackNewsContent::Image(ImageMessageEventContent {
                body,
                ..
            }))
            | NewsContent::File(FileMessageEventContent { body, .. })
            | NewsContent::Fallback(FallbackNewsContent::File(FileMessageEventContent {
                body,
                ..
            }))
            | NewsContent::Location(LocationMessageEventContent { body, .. })
            | NewsContent::Fallback(FallbackNewsContent::Location(LocationMessageEventContent {
                body,
                ..
            }))
            | NewsContent::Video(VideoMessageEventContent { body, .. })
            | NewsContent::Fallback(FallbackNewsContent::Video(VideoMessageEventContent {
                body,
                ..
            }))
            | NewsContent::Audio(AudioMessageEventContent { body, .. })
            | NewsContent::Fallback(FallbackNewsContent::Audio(AudioMessageEventContent {
                body,
                ..
            })) => body.clone(),

            NewsContent::Text(TextMessageEventContent {
                formatted, body, ..
            })
            | NewsContent::Fallback(FallbackNewsContent::Text(TextMessageEventContent {
                formatted,
                body,
                ..
            })) => {
                if let Some(formatted) = formatted {
                    formatted.body.clone()
                } else {
                    body.clone()
                }
            }
        }
    }

    pub fn text(&self) -> Option<TextMessageEventContent> {
        match self {
            NewsContent::Text(body) => Some(body.clone()),
            NewsContent::Fallback(f) => f.text(),
            _ => None,
        }
    }

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

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

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

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

    pub fn location(&self) -> Option<LocationMessageEventContent> {
        match self {
            NewsContent::Location(content) => Some(content.clone()),
            NewsContent::Fallback(f) => f.location(),
            _ => None,
        }
    }
}
/// A news slide represents one full-sized slide of news
#[derive(Clone, Debug, Builder, Deserialize, Getters, Serialize)]
pub struct NewsSlide {
    /// A slide must contain some news-worthy content
    #[serde(flatten)]
    pub content: NewsContent,

    /// A slide may optionally contain references to other items
    #[builder(default)]
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub references: Vec<ObjRef>,

    /// You can define custom background and foreground colors
    #[builder(default)]
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        deserialize_with = "deserialize_some"
    )]
    pub colors: Option<Colorize>,
}

/// The payload for our news creation event.
#[derive(Clone, Debug, Builder, Deserialize, Serialize, Getters, EventContent)]
#[ruma_event(type = "global.acter.dev.news", kind = MessageLike)]
#[builder(name = "NewsEntryBuilder", derive(Debug))]
pub struct NewsEntryEventContent {
    /// A news entry may have one or more slides of news
    /// which are scrolled through horizontally
    pub slides: Vec<NewsSlide>,
}

/// The payload for our news update event.
#[derive(Clone, Debug, Builder, Deserialize, Serialize, EventContent)]
#[ruma_event(type = "global.acter.dev.news.update", kind = MessageLike)]
#[builder(name = "NewsEntryUpdateBuilder", derive(Debug))]
pub struct NewsEntryUpdateEventContent {
    #[builder(setter(into))]
    #[serde(rename = "m.relates_to")]
    pub news_entry: Update,

    /// A news entry may have one or more slides of news
    /// which are scrolled through horizontally
    #[builder(default)]
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        deserialize_with = "deserialize_some"
    )]
    pub slides: Option<Vec<NewsSlide>>,
}

impl NewsEntryUpdateEventContent {
    pub fn apply(&self, task: &mut NewsEntryEventContent) -> Result<bool> {
        let mut updated = false;
        if let Some(slides) = &self.slides {
            task.slides.clone_from(slides);
            updated = true;
        }
        Ok(updated)
    }
}