acter/api/
common.rs

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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
use acter_core::events::{
    rsvp::RsvpStatus, ColorizeBuilder, DisplayBuilder, ObjRefBuilder, Position,
};
use anyhow::{Context, Result};
use core::time::Duration;
use matrix_sdk::{HttpError, RumaApiError};
use matrix_sdk_base::{
    media::{MediaFormat, MediaThumbnailSettings},
    ruma::{
        api::{client::error::ErrorBody, error::FromHttpResponseError},
        events::room::{
            message::UrlPreview as RumaUrlPreview, MediaSource as SdkMediaSource,
            ThumbnailInfo as SdkThumbnailInfo,
        },
        MilliSecondsSinceUnixEpoch, OwnedDeviceId, OwnedEventId, OwnedUserId, UInt,
    },
    ComposerDraft, ComposerDraftType,
};
use serde::{Deserialize, Serialize};
use std::{ops::Deref, str::FromStr};
use tracing::error;

use super::api::FfiBuffer;
use super::RefDetails;

pub fn duration_from_secs(secs: u64) -> Duration {
    Duration::from_secs(secs)
}

pub struct OptionString {
    text: Option<String>,
}

impl OptionString {
    pub(crate) fn new(text: Option<String>) -> Self {
        OptionString { text }
    }

    pub fn text(&self) -> Option<String> {
        self.text.clone()
    }
}

impl From<Option<String>> for OptionString {
    fn from(text: Option<String>) -> Self {
        OptionString { text }
    }
}

pub struct OptionBuffer {
    pub(crate) data: Option<Vec<u8>>,
}

impl OptionBuffer {
    pub(crate) fn new(data: Option<Vec<u8>>) -> Self {
        OptionBuffer { data }
    }

    pub fn data(&self) -> Option<FfiBuffer<u8>> {
        self.data.clone().map(FfiBuffer::new)
    }
}

pub struct OptionRsvpStatus {
    pub(crate) status: Option<RsvpStatus>,
}

impl OptionRsvpStatus {
    pub(crate) fn new(status: Option<RsvpStatus>) -> Self {
        OptionRsvpStatus { status }
    }

    pub fn status(&self) -> Option<RsvpStatus> {
        self.status.clone()
    }

    pub fn status_str(&self) -> Option<String> {
        self.status.as_ref().map(ToString::to_string)
    }
}
#[derive(Clone)]
pub struct OptionComposeDraft {
    draft: Option<ComposeDraft>,
}

impl OptionComposeDraft {
    pub(crate) fn new(draft: Option<ComposeDraft>) -> Self {
        OptionComposeDraft { draft }
    }

    pub fn draft(&self) -> Option<ComposeDraft> {
        self.draft.clone()
    }
}

pub struct MediaSource {
    pub(crate) inner: SdkMediaSource,
}

impl From<&SdkMediaSource> for MediaSource {
    fn from(value: &SdkMediaSource) -> Self {
        MediaSource {
            inner: value.clone(),
        }
    }
}

impl MediaSource {
    pub fn url(&self) -> String {
        match self.inner.clone() {
            SdkMediaSource::Plain(url) => url.to_string(),
            SdkMediaSource::Encrypted(file) => file.url.to_string(),
        }
    }
}

#[derive(Clone)]
pub struct ThumbnailInfo {
    pub(crate) inner: SdkThumbnailInfo,
}

impl From<&SdkThumbnailInfo> for ThumbnailInfo {
    fn from(value: &SdkThumbnailInfo) -> Self {
        ThumbnailInfo {
            inner: value.clone(),
        }
    }
}

impl ThumbnailInfo {
    pub fn mimetype(&self) -> Option<String> {
        self.inner.mimetype.clone()
    }

    pub fn size(&self) -> Option<u64> {
        self.inner.size.map(Into::into)
    }

    pub fn width(&self) -> Option<u64> {
        self.inner.width.map(Into::into)
    }

    pub fn height(&self) -> Option<u64> {
        self.inner.height.map(Into::into)
    }
}

pub struct UrlPreview(pub(crate) RumaUrlPreview);

impl UrlPreview {
    pub fn from(prev: &RumaUrlPreview) -> Self {
        Self(prev.clone())
    }
    pub fn new(prev: RumaUrlPreview) -> Self {
        Self(prev)
    }
    pub fn url(&self) -> Option<String> {
        self.0.url.clone()
    }
    pub fn title(&self) -> Option<String> {
        self.0.title.clone()
    }
    pub fn description(&self) -> Option<String> {
        self.0.description.clone()
    }

    pub fn has_image(&self) -> bool {
        false // not yet supported
              // !self.0.image.is_none()
    }
    pub fn image_source(&self) -> Option<MediaSource> {
        None // not yet support
             // self.0.image.as_ref().map(|image| MediaSource {
             //     inner: match image.source {
             //         PreviewImageSource::EncryptedImage(e) => SdkMediaSource::Encrypted(e.clone()),
             //         PreviewImageSource::Url(u) => SdkMediaSource::Plain(u.clone()),
             //     },
             // })
    }
}

#[derive(Clone)]
pub struct ComposeDraft {
    inner: ComposerDraft,
}

impl ComposeDraft {
    pub fn new(
        plain_text: String,
        html_text: Option<String>,
        msg_type: String,
        event_id: Option<OwnedEventId>,
    ) -> Self {
        let m_type = msg_type.clone();
        let draft_type = match (m_type.as_str(), event_id) {
            ("new", None) => ComposerDraftType::NewMessage,
            ("edit", Some(id)) => ComposerDraftType::Edit { event_id: id },
            ("reply", Some(id)) => ComposerDraftType::Reply { event_id: id },
            _ => ComposerDraftType::NewMessage,
        };

        ComposeDraft {
            inner: ComposerDraft {
                plain_text,
                html_text,
                draft_type,
            },
        }
    }

    pub fn inner(&self) -> ComposerDraft {
        self.inner.clone()
    }

    pub fn plain_text(&self) -> String {
        self.inner.plain_text.clone()
    }

    pub fn html_text(&self) -> Option<String> {
        self.inner.html_text.clone()
    }

    // only valid for reply and edit drafts
    pub fn event_id(&self) -> Option<String> {
        match &(self.inner.draft_type) {
            ComposerDraftType::Edit { event_id } => Some(event_id.to_string()),
            ComposerDraftType::Reply { event_id } => Some(event_id.to_string()),
            ComposerDraftType::NewMessage => None,
        }
    }

    pub fn draft_type(&self) -> String {
        match &(self.inner.draft_type) {
            ComposerDraftType::NewMessage => "new".to_string(),
            ComposerDraftType::Edit { event_id } => "edit".to_string(),
            ComposerDraftType::Reply { event_id } => "reply".to_string(),
        }
    }
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ReactionRecord {
    sender_id: OwnedUserId,
    timestamp: MilliSecondsSinceUnixEpoch,
    sent_by_me: bool,
}

impl ReactionRecord {
    pub(crate) fn new(
        sender_id: OwnedUserId,
        timestamp: MilliSecondsSinceUnixEpoch,
        sent_by_me: bool,
    ) -> Self {
        ReactionRecord {
            sender_id,
            timestamp,
            sent_by_me,
        }
    }

    pub fn sender_id(&self) -> OwnedUserId {
        self.sender_id.clone()
    }

    pub fn sent_by_me(&self) -> bool {
        self.sent_by_me
    }

    pub fn timestamp(&self) -> u64 {
        self.timestamp.get().into()
    }
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct DeviceRecord {
    device_id: OwnedDeviceId,
    display_name: Option<String>,
    last_seen_ts: Option<MilliSecondsSinceUnixEpoch>,
    last_seen_ip: Option<String>,
    is_verified: bool,
    is_active: bool,
    is_me: bool,
}

impl DeviceRecord {
    pub(crate) fn new(
        device_id: OwnedDeviceId,
        display_name: Option<String>,
        last_seen_ts: Option<MilliSecondsSinceUnixEpoch>,
        last_seen_ip: Option<String>,
        is_verified: bool,
        is_active: bool,
        is_me: bool,
    ) -> Self {
        DeviceRecord {
            device_id,
            display_name,
            last_seen_ts,
            last_seen_ip,
            is_verified,
            is_active,
            is_me,
        }
    }

    pub fn device_id(&self) -> OwnedDeviceId {
        self.device_id.clone()
    }

    pub fn display_name(&self) -> Option<String> {
        self.display_name.clone()
    }

    pub fn last_seen_ts(&self) -> Option<u64> {
        self.last_seen_ts.map(|x| x.get().into())
    }

    pub fn last_seen_ip(&self) -> Option<String> {
        self.last_seen_ip.clone()
    }

    pub fn is_verified(&self) -> bool {
        self.is_verified
    }

    pub fn is_me(&self) -> bool {
        self.is_me
    }

    pub fn is_active(&self) -> bool {
        self.is_active
    }
}

#[derive(Clone, Debug)]
pub struct ThumbnailSize {
    width: UInt,
    height: UInt,
}

impl ThumbnailSize {
    pub(crate) fn new(width: u64, height: u64) -> Result<Self> {
        let width = UInt::new(width).context("invalid thumbnail width")?;
        let height = UInt::new(height).context("invalid thumbnail height")?;
        Ok(ThumbnailSize { width, height })
    }

    pub(crate) fn width(&self) -> UInt {
        self.width
    }

    pub(crate) fn height(&self) -> UInt {
        self.height
    }

    pub fn parse_into_media_format(thumb_size: Option<Box<ThumbnailSize>>) -> MediaFormat {
        match thumb_size {
            Some(thumb_size) => MediaFormat::from(thumb_size),
            None => MediaFormat::File,
        }
    }
}

impl From<Box<ThumbnailSize>> for MediaFormat {
    fn from(val: Box<ThumbnailSize>) -> Self {
        MediaFormat::Thumbnail(MediaThumbnailSettings::new(val.width, val.height))
    }
}

pub fn new_thumb_size(width: u64, height: u64) -> Result<ThumbnailSize> {
    ThumbnailSize::new(width, height)
}

pub fn new_colorize_builder(
    color: Option<u32>,
    background: Option<u32>,
    link: Option<u32>,
) -> Result<ColorizeBuilder> {
    let mut builder = ColorizeBuilder::default();
    if let Some(color) = color {
        builder.color(color);
    }
    if let Some(background) = background {
        builder.background(background);
    }
    if let Some(link) = link {
        builder.link(link);
    }
    Ok(builder)
}

pub fn new_obj_ref_builder(
    position: Option<String>,
    reference: Box<RefDetails>,
) -> Result<ObjRefBuilder> {
    if let Some(p) = position {
        let p = Position::from_str(&p)?;
        Ok(ObjRefBuilder::new(Some(p), (*reference).deref().clone()))
    } else {
        Ok(ObjRefBuilder::new(None, (*reference).deref().clone()))
    }
}

pub fn clearify_error(err: matrix_sdk::Error) -> anyhow::Error {
    if let matrix_sdk::Error::Http(HttpError::Api(api_error)) = &err {
        match api_error {
            FromHttpResponseError::Deserialization(des) => {
                return anyhow::anyhow!("Deserialization failed: {des}");
            }
            FromHttpResponseError::Server(inner) => match inner {
                RumaApiError::ClientApi(error) => {
                    if let ErrorBody::Standard { kind, message } = &error.body {
                        return anyhow::anyhow!("{message:?} [{kind:?}]");
                    }
                    return anyhow::anyhow!("{0:?} [{1}]", error.body, error.status_code);
                }
                RumaApiError::Uiaa(uiaa_error) => {
                    if let Some(err) = &uiaa_error.auth_error {
                        return anyhow::anyhow!("{:?} [{:?}]", err.message, err.kind);
                    }
                    error!(?uiaa_error, "Other UIAA response");
                    return anyhow::anyhow!("Unsupported User Interaction needed.");
                }
                RumaApiError::Other(err) => {
                    return anyhow::anyhow!("{:?} [{:?}]", err.body, err.status_code);
                }
            },
            _ => {}
        }
    }
    err.into()
}

pub fn new_display_builder() -> DisplayBuilder {
    DisplayBuilder::default()
}