acter/api/
client.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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
use acter_core::{
    client::CoreClient,
    executor::Executor,
    models::AnyActerModel,
    referencing::{
        ExecuteReference, IndexKey, ModelParam, ObjectListIndex, RoomParam, SectionIndex,
    },
    store::Store,
    templates::Engine,
    CustomAuthSession, RestoreToken,
};
use anyhow::{Context, Result};
use base64ct::{Base64UrlUnpadded, Encoding};
use core::time::Duration;
use derive_builder::Builder;
use futures::{
    future::join_all,
    stream::{Stream, StreamExt},
};
use matrix_sdk::ruma::{EventId, ServerName};
use matrix_sdk::{room::Room as SdkRoom, Client as SdkClient};
use matrix_sdk_base::{
    media::{MediaRequestParameters, UniqueKey},
    ruma::{
        device_id, events::room::MediaSource, OwnedDeviceId, OwnedMxcUri, OwnedRoomAliasId,
        OwnedRoomId, OwnedRoomOrAliasId, OwnedServerName, OwnedUserId, RoomAliasId, RoomId,
        RoomOrAliasId, UserId,
    },
    RoomStateFilter,
};
use matrix_sdk_ui::eyeball_im::{ObservableVector, Vector};
use std::{borrow::Cow, io::Write, ops::Deref, path::PathBuf, str::FromStr, sync::Arc};
use tokio::{
    sync::{broadcast::Receiver, RwLock},
    time,
};
use tokio_stream::wrappers::BroadcastStream;
use tracing::{error, trace};

use crate::{Account, Convo, OptionString, Room, Space, ThumbnailSize, RUNTIME};

use super::{
    api::FfiBuffer, device::DeviceController, invitation::InvitationController,
    typing::TypingController, verification::VerificationController, VecStringBuilder,
};

mod models;
mod sync;
mod url_preview;
pub use sync::{HistoryLoadState, SyncState};
pub use url_preview::LocalUrlPreview;

#[derive(Default, Builder, Debug)]
pub struct ClientState {
    #[builder(default)]
    pub is_guest: bool,

    #[builder(default)]
    pub has_first_synced: bool,

    #[builder(default)]
    pub is_syncing: bool,

    #[builder(default)]
    pub should_stop_syncing: bool,

    #[builder(default)]
    pub db_passphrase: Option<String>,
}

#[derive(Clone, Debug)]
pub struct Client {
    pub(crate) core: CoreClient,
    pub(crate) state: Arc<RwLock<ClientState>>,
    pub(crate) invitation_controller: InvitationController,
    pub(crate) verification_controller: VerificationController,
    pub(crate) device_controller: DeviceController,
    pub(crate) typing_controller: TypingController,
    pub spaces: Arc<RwLock<ObservableVector<Space>>>,
    pub convos: Arc<RwLock<ObservableVector<Convo>>>,
}

impl Deref for Client {
    type Target = SdkClient;
    fn deref(&self) -> &SdkClient {
        self.core.client()
    }
}

// internal API
impl Client {
    pub(crate) async fn source_binary(
        &self,
        source: MediaSource,
        thumb_size: Option<Box<ThumbnailSize>>,
    ) -> Result<FfiBuffer<u8>> {
        // any variable in self can’t be called directly in spawn
        let client = self.core.client().clone();
        let format = ThumbnailSize::parse_into_media_format(thumb_size);
        let request = MediaRequestParameters { source, format };
        trace!(?request, "tasked to get source binary");
        RUNTIME
            .spawn(async move {
                let buf = client.media().get_media_content(&request, true).await?;
                Ok(FfiBuffer::new(buf))
            })
            .await?
    }

    pub(crate) async fn source_binary_tmp_path(
        &self,
        source: MediaSource,
        thumb_size: Option<Box<ThumbnailSize>>,
        tmp_path: String,
        file_suffix: &str,
    ) -> Result<String> {
        // any variable in self can’t be called directly in spawn
        let client = self.core.client().clone();
        let format = ThumbnailSize::parse_into_media_format(thumb_size);
        let request = MediaRequestParameters { source, format };
        let path = PathBuf::from(tmp_path).join(format!(
            "{}.{file_suffix}",
            Base64UrlUnpadded::encode_string(request.unique_key().as_bytes())
        ));
        trace!(
            ?request,
            ?path,
            "tasked to get source binary and store to file"
        );
        if !path.exists() {
            // only download if the temp isn’t already there.
            let target_path = path.clone();
            RUNTIME
                .spawn(async move {
                    let data = client.media().get_media_content(&request, true).await?;
                    let mut file = std::fs::File::create(target_path)?;
                    file.write_all(&data)?;
                    anyhow::Ok(())
                })
                .await?;
        }

        path.to_str()
            .map(|s| s.to_string())
            .context("Path was generated from strings. Must be string")
    }

    pub async fn join_room(
        &self,
        room_id_or_alias: String,
        server_names: Box<VecStringBuilder>,
    ) -> Result<Room> {
        let parsed = RoomOrAliasId::parse(room_id_or_alias)?;
        let servers = (*server_names)
            .0
            .into_iter()
            .map(ServerName::parse)
            .collect::<Result<Vec<OwnedServerName>, matrix_sdk::IdParseError>>()?;

        self.join_room_typed(parsed, servers).await
    }
    pub async fn join_room_typed(
        &self,
        room_id_or_alias: OwnedRoomOrAliasId,
        server_names: Vec<OwnedServerName>,
    ) -> Result<Room> {
        let core = self.core.clone();
        RUNTIME
            .spawn(async move {
                let joined = core
                    .client()
                    .join_room_by_id_or_alias(&room_id_or_alias, server_names.as_slice())
                    .await?;
                Ok(Room::new(core.clone(), joined))
            })
            .await?
    }
}

// external API
impl Client {
    pub async fn new(client: SdkClient, state: ClientState) -> Result<Self> {
        let core = CoreClient::new(client.clone()).await?;
        let mut cl = Client {
            core: core.clone(),
            state: Arc::new(RwLock::new(state)),
            spaces: Default::default(),
            convos: Default::default(),
            invitation_controller: InvitationController::new(core.clone()),
            verification_controller: VerificationController::new(),
            device_controller: DeviceController::new(client),
            typing_controller: TypingController::new(),
        };
        cl.load_from_cache().await;
        cl.setup_handlers();
        Ok(cl)
    }

    async fn load_from_cache(&self) {
        let (spaces, chats) = self.get_spaces_and_chats().await;
        // FIXME for a lack of a better system, we just sort by room-id
        let mut space_types: Vector<Space> = spaces
            .into_iter()
            .map(|r| Space::new(self.clone(), r))
            .collect();
        space_types.sort();

        self.spaces.write().await.append(space_types);
        let mut values = join_all(chats.into_iter().map(|r| Convo::new(self.clone(), r))).await;
        values.sort();
        self.convos.write().await.append(values.into());
    }

    async fn get_spaces_and_chats(&self) -> (Vec<Room>, Vec<Room>) {
        let client = self.core.clone();
        // only include items we are ourselves are currently joined in
        self.rooms_filtered(RoomStateFilter::JOINED)
            .into_iter()
            .fold(
                (Vec::new(), Vec::new()),
                move |(mut spaces, mut convos), room| {
                    let inner = Room::new(client.clone(), room);

                    if inner.is_space() {
                        spaces.push(inner);
                    } else {
                        convos.push(inner);
                    }
                    (spaces, convos)
                },
            )
    }

    pub async fn resolve_room_alias(&self, alias_id: OwnedRoomAliasId) -> Result<OwnedRoomId> {
        let client = self.core.client().clone();
        RUNTIME
            .spawn(async move {
                let response = client.resolve_room_alias(&alias_id).await?;
                anyhow::Ok(response.room_id)
            })
            .await?
    }

    pub fn store(&self) -> &Store {
        self.core.store()
    }

    pub fn executor(&self) -> &Executor {
        self.core.executor()
    }

    pub async fn template_engine(&self, template: &str) -> Result<Engine> {
        let engine = self.core.template_engine(template).await?;
        Ok(engine)
    }

    /// Is this a guest account?
    pub fn is_guest(&self) -> bool {
        match self.state.try_read() {
            Ok(r) => r.is_guest,
            Err(e) => false,
        }
    }

    pub async fn restore_token(&self) -> Result<String> {
        let session = self.session().context("Missing session")?;
        let homeurl = self.homeserver();
        let (is_guest, db_passphrase) = {
            let state = self.state.try_read()?;
            (state.is_guest, state.db_passphrase.clone())
        };
        let result = RestoreToken::serialized(
            CustomAuthSession {
                user_id: session.meta().user_id.clone(),
                device_id: session.meta().device_id.clone(),
                access_token: session.access_token().to_string(),
            },
            homeurl,
            is_guest,
            db_passphrase,
        )?;
        Ok(result)
    }

    // pub async fn get_mxcuri_media(&self, uri: String) -> Result<Vec<u8>> {
    //     let client = self.core.clone();
    //     RUNTIME.spawn(async move {
    //         let user_id = client.user_id().await.context("You must be logged in to do that")?;
    //         Ok(user_id.to_string())
    //     }).await?
    // }

    pub async fn upload_media(&self, uri: String) -> Result<OwnedMxcUri> {
        let client = self.core.client().clone();
        let path = PathBuf::from(uri);

        RUNTIME
            .spawn(async move {
                let guess = mime_guess::from_path(path.clone());
                let content_type = guess.first().context("don’t know mime type")?;
                let buf = std::fs::read(path)?;
                let response = client.media().upload(&content_type, buf, None).await?;
                Ok(response.content_uri)
            })
            .await?
    }

    pub fn user_id(&self) -> Result<OwnedUserId> {
        self.core
            .client()
            .user_id()
            .context("You must be logged in to do that")
            .map(|x| x.to_owned())
    }

    fn user_id_ref(&self) -> Option<&UserId> {
        self.core.client().user_id()
    }

    pub async fn room(&self, room_id_or_alias: String) -> Result<Room> {
        let id_or_alias = RoomOrAliasId::parse(room_id_or_alias)?;
        self.room_typed(&id_or_alias).await
    }

    // ***_typed fn accepts rust-typed input, not string-based one
    async fn room_typed(&self, room_id_or_alias: &RoomOrAliasId) -> Result<Room> {
        if room_id_or_alias.is_room_id() {
            let room_id = RoomId::parse(room_id_or_alias.as_str())?;
            let room = self.room_by_id_typed(&room_id)?;
            return Ok(Room::new(self.core.clone(), room));
        }

        let room_alias = RoomAliasId::parse(room_id_or_alias.as_str())?;
        self.room_by_alias_typed(&room_alias).await
    }

    // ***_typed fn accepts rust-typed input, not string-based one
    pub fn room_by_id_typed(&self, room_id: &RoomId) -> Result<SdkRoom> {
        self.core
            .client()
            .get_room(room_id)
            .context("Room not found")
    }

    pub async fn wait_for_room(&self, room_id: String, timeout: Option<u8>) -> Result<bool> {
        let executor = self.core.executor().clone();
        let room_id = RoomId::parse(room_id)?;

        let mut subscription = executor.subscribe(ExecuteReference::Room(room_id.clone()));
        if self.room_by_id_typed(&room_id).is_ok() {
            return Ok(true);
        }

        RUNTIME
            .spawn(async move {
                let waiter = subscription.recv();
                if let Some(tm) = timeout {
                    time::timeout(Duration::from_secs(tm as u64), waiter).await??;
                } else {
                    waiter.await?;
                }
                Ok(true)
            })
            .await?
    }

    // ***_typed fn accepts rust-typed input, not string-based one
    async fn room_by_alias_typed(&self, room_alias: &RoomAliasId) -> Result<Room> {
        let client = self.core.client();
        for r in client.rooms() {
            // looping locally first
            if let Some(con_alias) = r.canonical_alias() {
                if con_alias == room_alias {
                    return Ok(Room::new(self.core.clone(), r));
                }
            }
            for alt_alias in r.alt_aliases() {
                if alt_alias == room_alias {
                    return Ok(Room::new(self.core.clone(), r));
                }
            }
        }
        // nothing found, try remote:
        let response = client.resolve_room_alias(room_alias).await?;
        let room = self.room_by_id_typed(&response.room_id)?;
        Ok(Room::new(self.core.clone(), room))
    }

    pub fn dm_with_user(&self, user_id: String) -> Result<OptionString> {
        let user_id = UserId::parse(user_id)?;
        let room_id = self
            .core
            .client()
            .get_dm_room(&user_id)
            .map(|x| x.room_id().to_string());
        Ok(OptionString::new(room_id))
    }

    pub fn subscribe_section_stream(&self, section: String) -> Result<impl Stream<Item = bool>> {
        let index = SectionIndex::from_str(&section)?;
        Ok(BroadcastStream::new(self.subscribe(index)).map(|_| true))
    }

    pub fn subscribe_model_stream(&self, key: String) -> Result<impl Stream<Item = bool>> {
        let model_id = EventId::parse(key)?;
        Ok(BroadcastStream::new(self.subscribe(model_id)).map(|_| true))
    }

    pub fn subscribe_model_param_stream(
        &self,
        key: String,
        param: String,
    ) -> Result<impl Stream<Item = bool>> {
        let model_id = EventId::parse(key)?;
        let param = ModelParam::from_str(&param)?;
        Ok(
            BroadcastStream::new(self.subscribe(ExecuteReference::ModelParam(model_id, param)))
                .map(|_| true),
        )
    }

    pub fn subscribe_model_objects_stream(
        &self,
        key: String,
        sublist: String,
    ) -> Result<impl Stream<Item = bool>> {
        let model_id = EventId::parse(key)?;
        let param = ObjectListIndex::from_str(&sublist)?;
        Ok(
            BroadcastStream::new(self.subscribe(ExecuteReference::Index(IndexKey::ObjectList(
                model_id, param,
            ))))
            .map(|_| true),
        )
    }

    pub fn subscribe_room_stream(&self, key: String) -> Result<impl Stream<Item = bool>> {
        let model_id = RoomId::parse(key)?;
        Ok(BroadcastStream::new(self.subscribe(model_id)).map(|_| true))
    }

    pub fn subscribe_room_param_stream(
        &self,
        key: String,
        param: String,
    ) -> Result<impl Stream<Item = bool>> {
        let model_id = RoomId::parse(key)?;
        let param = RoomParam::from_str(&param)?;
        Ok(
            BroadcastStream::new(self.subscribe(ExecuteReference::RoomParam(model_id, param)))
                .map(|_| true),
        )
    }

    pub fn subscribe_room_section_stream(
        &self,
        key: String,
        section: String,
    ) -> Result<impl Stream<Item = bool>> {
        let index = IndexKey::RoomSection(RoomId::parse(key)?, SectionIndex::from_str(&section)?);
        Ok(BroadcastStream::new(self.subscribe(ExecuteReference::Index(index))).map(|_| true))
    }

    pub fn subscribe_event_type_stream(&self, key: String) -> Result<impl Stream<Item = bool>> {
        Ok(
            BroadcastStream::new(self.subscribe(ExecuteReference::ModelType(Cow::Owned(key))))
                .map(|_| true),
        )
    }

    pub fn subscribe_account_data_stream(&self, key: String) -> Result<impl Stream<Item = bool>> {
        Ok(
            BroadcastStream::new(self.subscribe(ExecuteReference::AccountData(Cow::Owned(key))))
                .map(|_| true),
        )
    }

    pub fn subscribe_room_account_data_stream(
        &self,
        room_id: String,
        key: String,
    ) -> Result<impl Stream<Item = bool>> {
        Ok(
            BroadcastStream::new(self.subscribe(ExecuteReference::RoomAccountData(
                RoomId::parse(room_id)?,
                Cow::Owned(key),
            )))
            .map(|_| true),
        )
    }

    pub fn subscribe<K: Into<ExecuteReference>>(&self, key: K) -> Receiver<()> {
        self.executor().subscribe(key)
    }

    pub async fn wait_for(&self, key: String, timeout: Option<u8>) -> Result<AnyActerModel> {
        let executor = self.core.executor().clone();

        RUNTIME
            .spawn(async move {
                let model_id = EventId::parse(key)?;
                let waiter = executor.wait_for(model_id);
                let Some(tm) = timeout else {
                    return Ok(waiter.await?);
                };
                Ok(time::timeout(Duration::from_secs(tm as u64), waiter).await??)
            })
            .await?
    }

    pub fn account(&self) -> Result<Account> {
        let account = self.core.client().account();
        let user_id = self.user_id()?;
        Ok(Account::new(account, user_id, self.clone()))
    }

    pub fn device_id(&self) -> Result<OwnedDeviceId> {
        self.core
            .client()
            .device_id()
            .context("DeviceId not found")
            .map(|x| x.to_owned())
    }

    pub async fn verified_device(&self, dev_id: String) -> Result<bool> {
        let client = self.core.client().clone();
        let user_id = self.user_id()?;
        RUNTIME
            .spawn(async move {
                client
                    .encryption()
                    .get_device(&user_id, device_id!(dev_id.as_str()))
                    .await?
                    .context("Unable to find device")
                    .map(|x| x.is_verified())
            })
            .await?
    }

    pub async fn logout(&mut self) -> Result<bool> {
        if let Ok(mut w) = self.state.try_write() {
            w.should_stop_syncing = true;
        }
        let client = self.core.client().clone();

        self.invitation_controller.remove_event_handler();
        self.verification_controller
            .remove_to_device_event_handler(&client);
        self.verification_controller
            .remove_sync_event_handler(&client);
        self.typing_controller.remove_event_handler(&client);

        RUNTIME
            .spawn(async move {
                match client.matrix_auth().logout().await {
                    Ok(resp) => Ok(true),
                    Err(e) => {
                        error!("logout error: {:?}", e);
                        Ok(false)
                    }
                }
            })
            .await?
    }
}