atagen

macltok

public
Star0
Watch0
2c7ca08237507e839b498f639987316309e3b843
3
ssh://brewery@pls.lobotomise.me:33/atagen/macltok.git
src/transport.rsRust1152 lines36.0 KB
1
use std::{
2
    collections::HashSet,
3
    fmt,
4
    future::Future,
5
    pin::Pin,
6
    sync::{Arc, Mutex},
7
    task::{Context, Poll},
8
};
9
10
use matrix_sdk::{Client, RoomMemberships};
11
use matrix_sdk_ui::{
12
    notification_client::{
13
        NotificationClient, NotificationProcessSetup, NotificationStatus, RawNotificationEvent,
14
    },
15
    spaces::{SpaceRoomList, SpaceService, room_list::SpaceRoomListPaginationState},
16
    sync_service::SyncService,
17
};
18
use ruma::{
19
    EventId, OwnedDeviceId, OwnedEventId, OwnedMxcUri, OwnedRoomId, OwnedRoomOrAliasId,
20
    OwnedServerName, OwnedTransactionId, OwnedUserId, RoomId, UserId,
21
    events::{
22
        AnyGlobalAccountDataEventContent, AnyRoomAccountDataEvent, AnyRoomAccountDataEventContent,
23
        GlobalAccountDataEventType, RoomAccountDataEventType,
24
    },
25
    serde::Raw,
26
};
27
use tokio::sync::{OnceCell, mpsc, oneshot, watch};
28
29
use crate::{
30
    AccountCommand, AttachmentUpload, Command, CommandOutput, ConnectionState, CreateRoomOptions,
31
    EncryptionCommand, EncryptionState, Error, ErrorKind, Event, EventRef, ImagePacks,
32
    InteractiveAuth, InteractiveAuthOutcome, LifecycleCommand, LinkPreview, Media, MediaCommand,
33
    MediaId, MediaLocation, MediaSize, MemberFilter, MemberSummary, MessageContent,
34
    MessageEditContent, NotificationCommand, OwnDevice, PowerLevelChanges, Profile,
35
    PushNotification, PushNotificationEvent, PushNotificationStatus, RoomCommand, RoomListFilter,
36
    RoomNotificationMode, RoomNotificationSettings, RoomPowerLevels, RoomSummary, SpaceChild,
37
    TimelineCommand, TimelineId, VerificationAction, VerificationTarget,
38
    encryption::encryption_state,
39
    engine::{MediaService, StoreLock},
40
    model::{PushNotificationRoom, PushNotificationSender},
41
    normalise::{image_packs, member_summary, room_summaries, space_child, url_previews_allowed},
42
};
43
44
#[derive(Clone)]
45
pub struct ClientHandle {
46
    requests: mpsc::Sender<Request>,
47
    client: Client,
48
    connection: watch::Receiver<ConnectionState>,
49
    notification_client: Arc<OnceCell<NotificationClient>>,
50
    sync_service: Arc<SyncService>,
51
    media: Arc<MediaService>,
52
    spaces: Arc<OnceCell<SpaceService>>,
53
    members_checked: Arc<Mutex<HashSet<OwnedRoomId>>>,
54
    _store_lock: Option<Arc<StoreLock>>,
55
}
56
57
impl fmt::Debug for ClientHandle {
58
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
59
        formatter
60
            .debug_struct("ClientHandle")
61
            .finish_non_exhaustive()
62
    }
63
}
64
65
/// a command's answer. drop it if you don't care, the command still runs
66
#[derive(Debug)]
67
#[must_use = "dropping it is fine but then you'll never know"]
68
pub struct Pending(oneshot::Receiver<Result<CommandOutput, Error>>);
69
70
impl Future for Pending {
71
    type Output = Result<CommandOutput, Error>;
72
73
    fn poll(mut self: Pin<&mut Self>, context: &mut Context<'_>) -> Poll<Self::Output> {
74
        Pin::new(&mut self.0)
75
            .poll(context)
76
            .map(|result| result.unwrap_or_else(|_| Err(Error::interrupted())))
77
    }
78
}
79
80
impl ClientHandle {
81
    /// current credentials, it holds secrets. the engine saves it for you unless the
82
    /// session was built `without_persistence`
83
    #[cfg(any(feature = "sqlite", feature = "indexeddb"))]
84
    pub fn session_record(&self) -> Result<crate::SessionRecord, crate::AccountError> {
85
        crate::account::session_record(&self.client)
86
    }
87
88
    /// the bearer token. it rotates on every refresh so ask again after a 401 rather than
89
    /// keeping it, and keep it in memory and away from all but the homeserver's media endpoints
90
    pub fn access_token(&self) -> Option<String> {
91
        self.client.access_token()
92
    }
93
94
    /// queue a command, waiting for room in the queue if there isn't any
95
    pub async fn send(&self, command: Command) -> Result<Pending, Error> {
96
        let (request, pending) = Request::new(command);
97
        self.requests
98
            .send(request)
99
            .await
100
            .map_err(|_| Error::interrupted())?;
101
        Ok(pending)
102
    }
103
104
    /// queue a command or fail with `QueueFull` right now
105
    pub fn try_send(&self, command: Command) -> Result<Pending, Error> {
106
        let (request, pending) = Request::new(command);
107
        self.requests
108
            .try_send(request)
109
            .map_err(|error| match error {
110
                mpsc::error::TrySendError::Full(_) => {
111
                    Error::new(ErrorKind::QueueFull, "the command queue is full")
112
                }
113
                mpsc::error::TrySendError::Closed(_) => Error::interrupted(),
114
            })?;
115
        Ok(pending)
116
    }
117
118
    /// queue a command and wait for its answer. timeline sends answer once the
119
    /// sdk send queue has them, delivery shows up later as the item's send state
120
    pub async fn execute(&self, command: Command) -> Result<CommandOutput, Error> {
121
        self.send(command).await?.await
122
    }
123
124
    async fn run(&self, command: Command) -> Result<(), Error> {
125
        self.execute(command).await.map(drop)
126
    }
127
128
    async fn run_for_room(&self, command: RoomCommand) -> Result<OwnedRoomId, Error> {
129
        match self.execute(Command::Room(command)).await? {
130
            CommandOutput::Room(room_id) => Ok(room_id),
131
            output => Err(Error::new(
132
                ErrorKind::Other,
133
                format!("expected a room but got {output:?}"),
134
            )),
135
        }
136
    }
137
138
    pub async fn shutdown(&self) -> Result<(), Error> {
139
        self.run(Command::Lifecycle(LifecycleCommand::Shutdown))
140
            .await
141
    }
142
143
    pub async fn logout(&self) -> Result<(), Error> {
144
        self.run(Command::Account(AccountCommand::Logout)).await
145
    }
146
147
    pub async fn delete_devices(
148
        &self,
149
        device_ids: Vec<OwnedDeviceId>,
150
        auth: Option<InteractiveAuth>,
151
    ) -> Result<InteractiveAuthOutcome, Error> {
152
        match self
153
            .run(Command::Account(AccountCommand::DeleteDevices {
154
                device_ids,
155
                auth,
156
            }))
157
            .await
158
        {
159
            Ok(()) => Ok(InteractiveAuthOutcome::Completed),
160
            Err(Error {
161
                challenge: Some(challenge),
162
                ..
163
            }) => Ok(InteractiveAuthOutcome::Challenge(*challenge)),
164
            Err(error) => Err(error),
165
        }
166
    }
167
168
    pub async fn rename_device(
169
        &self,
170
        device_id: OwnedDeviceId,
171
        display_name: impl Into<String>,
172
    ) -> Result<(), Error> {
173
        self.run(Command::Account(AccountCommand::RenameDevice {
174
            device_id,
175
            display_name: display_name.into(),
176
        }))
177
        .await
178
    }
179
180
    pub async fn set_global_account_data(
181
        &self,
182
        event_type: GlobalAccountDataEventType,
183
        content: Raw<AnyGlobalAccountDataEventContent>,
184
    ) -> Result<(), Error> {
185
        self.run(Command::Account(AccountCommand::SetAccountData {
186
            event_type,
187
            content,
188
        }))
189
        .await
190
    }
191
192
    pub async fn set_room_account_data(
193
        &self,
194
        room_id: OwnedRoomId,
195
        event_type: RoomAccountDataEventType,
196
        content: Raw<AnyRoomAccountDataEventContent>,
197
    ) -> Result<(), Error> {
198
        self.run(Command::Room(RoomCommand::SetAccountData {
199
            room_id,
200
            event_type,
201
            content,
202
        }))
203
        .await
204
    }
205
206
    pub async fn set_room_notification_mode(
207
        &self,
208
        room_id: OwnedRoomId,
209
        mode: RoomNotificationMode,
210
    ) -> Result<(), Error> {
211
        self.run(Command::Notification(NotificationCommand::SetRoomMode {
212
            room_id,
213
            mode,
214
        }))
215
        .await
216
    }
217
218
    pub async fn reset_room_notification_mode(&self, room_id: OwnedRoomId) -> Result<(), Error> {
219
        self.run(Command::Notification(NotificationCommand::ResetRoomMode(
220
            room_id,
221
        )))
222
        .await
223
    }
224
225
    pub async fn set_default_notification_mode(
226
        &self,
227
        encrypted: bool,
228
        one_to_one: bool,
229
        mode: RoomNotificationMode,
230
    ) -> Result<(), Error> {
231
        self.run(Command::Notification(NotificationCommand::SetDefaultMode {
232
            encrypted,
233
            one_to_one,
234
            mode,
235
        }))
236
        .await
237
    }
238
239
    pub async fn add_notification_keyword(&self, keyword: impl Into<String>) -> Result<(), Error> {
240
        self.run(Command::Notification(NotificationCommand::AddKeyword(
241
            keyword.into(),
242
        )))
243
        .await
244
    }
245
246
    pub async fn remove_notification_keyword(
247
        &self,
248
        keyword: impl Into<String>,
249
    ) -> Result<(), Error> {
250
        self.run(Command::Notification(NotificationCommand::RemoveKeyword(
251
            keyword.into(),
252
        )))
253
        .await
254
    }
255
256
    pub async fn open_room(&self, room_id: OwnedRoomId) -> Result<(), Error> {
257
        self.run(Command::Room(RoomCommand::Open(room_id))).await
258
    }
259
260
    pub async fn close_room(&self, room_id: OwnedRoomId) -> Result<(), Error> {
261
        self.run(Command::Room(RoomCommand::Close(room_id))).await
262
    }
263
264
    pub async fn open_thread(
265
        &self,
266
        room_id: OwnedRoomId,
267
        root_event_id: OwnedEventId,
268
    ) -> Result<(), Error> {
269
        self.run(Command::Timeline(TimelineCommand::OpenThread {
270
            room_id,
271
            root_event_id,
272
        }))
273
        .await
274
    }
275
276
    pub async fn close_thread(
277
        &self,
278
        room_id: OwnedRoomId,
279
        root_event_id: OwnedEventId,
280
    ) -> Result<(), Error> {
281
        self.run(Command::Timeline(TimelineCommand::CloseThread {
282
            room_id,
283
            root_event_id,
284
        }))
285
        .await
286
    }
287
288
    pub async fn resync(&self, timeline_id: TimelineId) -> Result<(), Error> {
289
        self.run(Command::Timeline(TimelineCommand::Resync { timeline_id }))
290
            .await
291
    }
292
293
    pub async fn paginate(&self, timeline_id: TimelineId, limit: u16) -> Result<(), Error> {
294
        self.run(Command::Timeline(TimelineCommand::Paginate {
295
            timeline_id,
296
            limit,
297
        }))
298
        .await
299
    }
300
301
    pub async fn send_message(
302
        &self,
303
        timeline_id: TimelineId,
304
        content: MessageContent,
305
    ) -> Result<(), Error> {
306
        self.run(Command::Timeline(TimelineCommand::Send {
307
            timeline_id,
308
            content,
309
        }))
310
        .await
311
    }
312
313
    pub async fn edit_message(
314
        &self,
315
        timeline_id: TimelineId,
316
        target: impl Into<EventRef>,
317
        content: MessageEditContent,
318
    ) -> Result<(), Error> {
319
        self.run(Command::Timeline(TimelineCommand::Edit {
320
            timeline_id,
321
            target: target.into(),
322
            content,
323
        }))
324
        .await
325
    }
326
327
    pub async fn redact(
328
        &self,
329
        timeline_id: TimelineId,
330
        target: impl Into<EventRef>,
331
        reason: Option<String>,
332
    ) -> Result<(), Error> {
333
        self.run(Command::Timeline(TimelineCommand::Redact {
334
            timeline_id,
335
            target: target.into(),
336
            reason,
337
        }))
338
        .await
339
    }
340
341
    pub async fn toggle_reaction(
342
        &self,
343
        timeline_id: TimelineId,
344
        target: impl Into<EventRef>,
345
        key: impl Into<String>,
346
        shortcode: Option<String>,
347
    ) -> Result<(), Error> {
348
        self.run(Command::Timeline(TimelineCommand::ToggleReaction {
349
            timeline_id,
350
            target: target.into(),
351
            key: key.into(),
352
            shortcode,
353
        }))
354
        .await
355
    }
356
357
    pub async fn mark_read(
358
        &self,
359
        timeline_id: TimelineId,
360
        event_id: OwnedEventId,
361
    ) -> Result<(), Error> {
362
        self.run(Command::Timeline(TimelineCommand::MarkRead {
363
            timeline_id,
364
            event_id,
365
        }))
366
        .await
367
    }
368
369
    pub async fn set_typing(&self, room_id: OwnedRoomId, typing: bool) -> Result<(), Error> {
370
        self.run(Command::Room(RoomCommand::SetTyping { room_id, typing }))
371
            .await
372
    }
373
374
    pub async fn prepare_to_send(&self, room_id: OwnedRoomId) -> Result<(), Error> {
375
        self.run(Command::Room(RoomCommand::PrepareToSend(room_id)))
376
            .await
377
    }
378
379
    pub async fn join_room(
380
        &self,
381
        room: OwnedRoomOrAliasId,
382
        via: Vec<OwnedServerName>,
383
    ) -> Result<OwnedRoomId, Error> {
384
        self.run_for_room(RoomCommand::Join { room, via }).await
385
    }
386
387
    pub async fn leave_room(&self, room_id: OwnedRoomId) -> Result<(), Error> {
388
        self.run(Command::Room(RoomCommand::Leave(room_id))).await
389
    }
390
391
    pub async fn forget_room(&self, room_id: OwnedRoomId) -> Result<(), Error> {
392
        self.run(Command::Room(RoomCommand::Forget(room_id))).await
393
    }
394
395
    pub async fn accept_invite(&self, room_id: OwnedRoomId) -> Result<(), Error> {
396
        self.run(Command::Room(RoomCommand::AcceptInvite(room_id)))
397
            .await
398
    }
399
400
    pub async fn reject_invite(&self, room_id: OwnedRoomId) -> Result<(), Error> {
401
        self.run(Command::Room(RoomCommand::RejectInvite(room_id)))
402
            .await
403
    }
404
405
    pub async fn invite_user(
406
        &self,
407
        room_id: OwnedRoomId,
408
        user_id: OwnedUserId,
409
    ) -> Result<(), Error> {
410
        self.run(Command::Room(RoomCommand::InviteUser { room_id, user_id }))
411
            .await
412
    }
413
414
    pub async fn kick_user(
415
        &self,
416
        room_id: OwnedRoomId,
417
        user_id: OwnedUserId,
418
        reason: Option<String>,
419
    ) -> Result<(), Error> {
420
        self.run(Command::Room(RoomCommand::KickUser {
421
            room_id,
422
            user_id,
423
            reason,
424
        }))
425
        .await
426
    }
427
428
    pub async fn ban_user(
429
        &self,
430
        room_id: OwnedRoomId,
431
        user_id: OwnedUserId,
432
        reason: Option<String>,
433
    ) -> Result<(), Error> {
434
        self.run(Command::Room(RoomCommand::BanUser {
435
            room_id,
436
            user_id,
437
            reason,
438
        }))
439
        .await
440
    }
441
442
    pub async fn unban_user(
443
        &self,
444
        room_id: OwnedRoomId,
445
        user_id: OwnedUserId,
446
        reason: Option<String>,
447
    ) -> Result<(), Error> {
448
        self.run(Command::Room(RoomCommand::UnbanUser {
449
            room_id,
450
            user_id,
451
            reason,
452
        }))
453
        .await
454
    }
455
456
    pub async fn set_power_levels(
457
        &self,
458
        room_id: OwnedRoomId,
459
        changes: PowerLevelChanges,
460
    ) -> Result<(), Error> {
461
        self.run(Command::Room(RoomCommand::SetPowerLevels {
462
            room_id,
463
            changes,
464
        }))
465
        .await
466
    }
467
468
    pub async fn set_user_power_level(
469
        &self,
470
        room_id: OwnedRoomId,
471
        user_id: OwnedUserId,
472
        level: i64,
473
    ) -> Result<(), Error> {
474
        self.run(Command::Room(RoomCommand::SetUserPowerLevel {
475
            room_id,
476
            user_id,
477
            level,
478
        }))
479
        .await
480
    }
481
482
    pub async fn create_room(&self, options: CreateRoomOptions) -> Result<OwnedRoomId, Error> {
483
        self.run_for_room(RoomCommand::Create(options)).await
484
    }
485
486
    /// hands back the existing dm with them if there is one
487
    pub async fn create_direct(&self, user_id: OwnedUserId) -> Result<OwnedRoomId, Error> {
488
        self.run_for_room(RoomCommand::CreateDirect(user_id)).await
489
    }
490
491
    pub async fn cancel_send(
492
        &self,
493
        room_id: OwnedRoomId,
494
        transaction_id: OwnedTransactionId,
495
    ) -> Result<(), Error> {
496
        self.run(Command::Room(RoomCommand::CancelSend {
497
            room_id,
498
            transaction_id,
499
        }))
500
        .await
501
    }
502
503
    pub async fn retry_send(
504
        &self,
505
        room_id: OwnedRoomId,
506
        transaction_id: OwnedTransactionId,
507
    ) -> Result<(), Error> {
508
        self.run(Command::Room(RoomCommand::RetrySend {
509
            room_id,
510
            transaction_id,
511
        }))
512
        .await
513
    }
514
515
    pub async fn upload(&self, upload: AttachmentUpload) -> Result<(), Error> {
516
        self.run(Command::Media(MediaCommand::Upload(upload))).await
517
    }
518
519
    pub async fn recover(&self, recovery_key: impl Into<String>) -> Result<(), Error> {
520
        self.run(Command::Encryption(EncryptionCommand::Recover(
521
            recovery_key.into(),
522
        )))
523
        .await
524
    }
525
526
    pub async fn create_key_backup(&self) -> Result<(), Error> {
527
        self.run(Command::Encryption(EncryptionCommand::CreateKeyBackup))
528
            .await
529
    }
530
531
    pub async fn enable_key_backup(&self) -> Result<(), Error> {
532
        self.run(Command::Encryption(EncryptionCommand::EnableKeyBackup))
533
            .await
534
    }
535
536
    pub async fn disable_key_backup(&self, delete_from_server: bool) -> Result<(), Error> {
537
        self.run(Command::Encryption(EncryptionCommand::DisableKeyBackup {
538
            delete_from_server,
539
        }))
540
        .await
541
    }
542
543
    pub async fn recover_key_backup(&self, recovery_key: impl Into<String>) -> Result<(), Error> {
544
        self.run(Command::Encryption(EncryptionCommand::RecoverKeyBackup(
545
            recovery_key.into(),
546
        )))
547
        .await
548
    }
549
550
    pub async fn start_verification(&self, target: VerificationTarget) -> Result<(), Error> {
551
        self.run(Command::Encryption(EncryptionCommand::StartVerification(
552
            target,
553
        )))
554
        .await
555
    }
556
557
    pub async fn verify(&self, action: VerificationAction) -> Result<(), Error> {
558
        self.run(Command::Encryption(EncryptionCommand::Verify(action)))
559
            .await
560
    }
561
562
    /// bytes for an avatar, or for media a retained timeline showed you. concurrent asks share
563
    /// one download
564
    pub async fn media(&self, media_id: &MediaId, size: MediaSize) -> Result<Media, Error> {
565
        self.media.timeline_media(media_id, size).await
566
    }
567
568
    /// where `media` would fetch from, for a host streaming big files itself. the byte cap
569
    /// doesn't apply since nothing is buffered
570
    pub async fn media_source(
571
        &self,
572
        media_id: &MediaId,
573
        size: MediaSize,
574
    ) -> Result<MediaLocation, Error> {
575
        self.media.media_location(media_id, size).await
576
    }
577
578
    /// the space graph builds on first use and then keeps itself current
579
    async fn spaces(&self) -> &SpaceService {
580
        self.spaces
581
            .get_or_init(|| SpaceService::new(self.client.clone()))
582
            .await
583
    }
584
585
    pub async fn rooms(&self) -> Vec<RoomSummary> {
586
        room_summaries(&self.client, self.spaces().await).await
587
    }
588
589
    pub async fn image_packs(&self) -> Result<ImagePacks, Error> {
590
        image_packs(&self.client).await
591
    }
592
593
    /// the homeserver's preview of a page, `None` when it has none. the homeserver sees every
594
    /// url asked about, so a host decides whether encrypted rooms ask at all
595
    pub async fn url_preview(&self, url: &str) -> Result<Option<LinkPreview>, Error> {
596
        self.media.url_preview(url).await
597
    }
598
599
    /// whether the room and the user's settings let link previews show here
600
    pub async fn url_previews_allowed(&self, room_id: &RoomId) -> Result<bool, Error> {
601
        url_previews_allowed(&self.room(room_id)?).await
602
    }
603
604
    /// browse a space the way the server sees it, rooms you haven't joined included
605
    pub async fn space_hierarchy(&self, space_id: OwnedRoomId) -> SpaceHierarchy {
606
        SpaceHierarchy(SpaceRoomList::new(self.client.clone(), space_id).await)
607
    }
608
609
    pub async fn own_profile(&self) -> Result<Profile, Error> {
610
        let user_id = self
611
            .client
612
            .user_id()
613
            .ok_or_else(|| Error::new(ErrorKind::Unauthenticated, "the client is not logged in"))?
614
            .to_owned();
615
        let response = self
616
            .client
617
            .account()
618
            .fetch_user_profile()
619
            .await
620
            .map_err(|source| Error::from_sdk("fetching the profile failed", source))?;
621
        profile(user_id, &response)
622
    }
623
624
    pub async fn user_profile(&self, user_id: &UserId) -> Result<Profile, Error> {
625
        let response = self
626
            .client
627
            .account()
628
            .fetch_user_profile_of(user_id)
629
            .await
630
            .map_err(|source| Error::from_sdk("fetching the profile failed", source))?;
631
        profile(user_id.to_owned(), &response)
632
    }
633
634
    pub async fn set_display_name(&self, name: Option<String>) -> Result<(), Error> {
635
        self.run(Command::Account(AccountCommand::SetDisplayName(name)))
636
            .await
637
    }
638
639
    pub async fn set_avatar(&self, avatar: Option<OwnedMxcUri>) -> Result<(), Error> {
640
        self.run(Command::Account(AccountCommand::SetAvatar(avatar)))
641
            .await
642
    }
643
644
    pub async fn upload_avatar(
645
        &self,
646
        mimetype: impl Into<String>,
647
        bytes: Vec<u8>,
648
    ) -> Result<(), Error> {
649
        self.run(Command::Account(AccountCommand::UploadAvatar {
650
            mimetype: mimetype.into(),
651
            bytes,
652
        }))
653
        .await
654
    }
655
656
    pub async fn rooms_filtered(&self, filter: &RoomListFilter) -> Vec<RoomSummary> {
657
        let matcher = filter.matcher();
658
        self.rooms()
659
            .await
660
            .into_iter()
661
            .filter(|room| matcher.matches(room))
662
            .collect()
663
    }
664
665
    pub async fn devices(&self) -> Result<Vec<OwnDevice>, Error> {
666
        let current_device_id = self.client.device_id();
667
        Ok(self
668
            .client
669
            .devices()
670
            .await
671
            .map_err(|source| Error::from_http("listing devices failed", source))?
672
            .devices
673
            .into_iter()
674
            .map(|device| OwnDevice {
675
                current: current_device_id == Some(device.device_id.as_ref()),
676
                device_id: device.device_id,
677
                display_name: device.display_name,
678
                last_seen_ip: device.last_seen_ip,
679
                last_seen_timestamp_ms: device.last_seen_ts.map(|timestamp| timestamp.get().into()),
680
            })
681
            .collect())
682
    }
683
684
    fn room(&self, room_id: &RoomId) -> Result<matrix_sdk::Room, Error> {
685
        self.client
686
            .get_room(room_id)
687
            .ok_or_else(Error::room_unknown)
688
    }
689
690
    pub async fn room_notification_settings(
691
        &self,
692
        room_id: &RoomId,
693
    ) -> Result<Option<RoomNotificationSettings>, Error> {
694
        let room = self.room(room_id)?;
695
        let custom = room.user_defined_notification_mode().await;
696
        let mode = match custom {
697
            Some(mode) => mode,
698
            None => match room.notification_mode().await {
699
                Some(mode) => mode,
700
                None => return Ok(None),
701
            },
702
        };
703
        Ok(Some(RoomNotificationSettings {
704
            mode: crate::normalise::notification_mode(mode),
705
            custom: custom.is_some(),
706
        }))
707
    }
708
709
    pub async fn notification_keywords(&self) -> Vec<String> {
710
        self.client
711
            .notification_settings()
712
            .await
713
            .enabled_keywords()
714
            .await
715
            .into_iter()
716
            .collect()
717
    }
718
719
    pub async fn resolve_push_notification(
720
        &self,
721
        room_id: &RoomId,
722
        event_id: &EventId,
723
    ) -> Result<PushNotificationStatus, Error> {
724
        let notification_failed = |source| {
725
            Error::with_source(
726
                ErrorKind::Other,
727
                "resolving the push notification failed",
728
                source,
729
            )
730
        };
731
        let client = self
732
            .notification_client
733
            .get_or_try_init(|| async {
734
                NotificationClient::new(
735
                    self.client.clone(),
736
                    NotificationProcessSetup::SingleProcess {
737
                        sync_service: self.sync_service.clone(),
738
                    },
739
                )
740
                .await
741
            })
742
            .await
743
            .map_err(notification_failed)?;
744
        let status = client
745
            .get_notification(room_id, event_id)
746
            .await
747
            .map_err(notification_failed)?;
748
        Ok(normalise_push_notification(room_id, event_id, status))
749
    }
750
751
    pub async fn members(
752
        &self,
753
        room_id: &RoomId,
754
        filter: MemberFilter,
755
    ) -> Result<Vec<MemberSummary>, Error> {
756
        let room = self.room(room_id)?;
757
        // stores have been seen with members_synced persisted over a partial
758
        // roster, and the sdk never asks /members again once it's set
759
        if self.members_checked.lock().unwrap().insert(room_id.to_owned()) {
760
            room.mark_members_missing();
761
        }
762
        list_members(&room, filter).await
763
    }
764
765
    /// drop the cached roster for a room and fetch /members again
766
    pub async fn refresh_members(
767
        &self,
768
        room_id: &RoomId,
769
        filter: MemberFilter,
770
    ) -> Result<Vec<MemberSummary>, Error> {
771
        let room = self.room(room_id)?;
772
        room.mark_members_missing();
773
        self.members_checked.lock().unwrap().insert(room_id.to_owned());
774
        list_members(&room, filter).await
775
    }
776
777
    pub async fn room_power_levels(&self, room_id: &RoomId) -> Result<RoomPowerLevels, Error> {
778
        let levels = self
779
            .room(room_id)?
780
            .power_levels()
781
            .await
782
            .map_err(|source| Error::from_sdk("reading power levels failed", source.into()))?;
783
        Ok(RoomPowerLevels {
784
            ban: levels.ban.into(),
785
            invite: levels.invite.into(),
786
            kick: levels.kick.into(),
787
            redact: levels.redact.into(),
788
            events_default: levels.events_default.into(),
789
            state_default: levels.state_default.into(),
790
            users_default: levels.users_default.into(),
791
            room_notification: levels.notifications.room.into(),
792
            users: levels
793
                .users
794
                .into_iter()
795
                .map(|(user_id, level)| (user_id, level.into()))
796
                .collect(),
797
            events: levels
798
                .events
799
                .into_iter()
800
                .map(|(event_type, level)| (event_type.to_string(), level.into()))
801
                .collect(),
802
            creators: levels
803
                .rules
804
                .privileged_creators
805
                .into_iter()
806
                .flatten()
807
                .collect(),
808
        })
809
    }
810
811
    pub async fn pending_send_ids(
812
        &self,
813
        room_id: &RoomId,
814
    ) -> Result<Vec<OwnedTransactionId>, Error> {
815
        let (echoes, _) = self
816
            .room(room_id)?
817
            .send_queue()
818
            .subscribe()
819
            .await
820
            .map_err(|source| {
821
                Error::with_source(ErrorKind::Store, "reading the send queue failed", source)
822
            })?;
823
        Ok(echoes
824
            .into_iter()
825
            .filter(|echo| {
826
                matches!(
827
                    echo.content,
828
                    matrix_sdk::send_queue::LocalEchoContent::Event { .. }
829
                )
830
            })
831
            .map(|echo| echo.transaction_id)
832
            .collect())
833
    }
834
835
    pub async fn global_account_data(
836
        &self,
837
        event_type: GlobalAccountDataEventType,
838
    ) -> Result<Option<Raw<AnyGlobalAccountDataEventContent>>, Error> {
839
        self.client
840
            .account()
841
            .account_data_raw(event_type)
842
            .await
843
            .map_err(|source| Error::from_sdk("reading account data failed", source))
844
    }
845
846
    pub async fn room_account_data(
847
        &self,
848
        room_id: &RoomId,
849
        event_type: RoomAccountDataEventType,
850
    ) -> Result<Option<Raw<AnyRoomAccountDataEvent>>, Error> {
851
        self.room(room_id)?
852
            .account_data(event_type)
853
            .await
854
            .map_err(|source| Error::from_sdk("reading room account data failed", source))
855
    }
856
857
    #[must_use]
858
    pub fn connection_state(&self) -> watch::Receiver<ConnectionState> {
859
        self.connection.clone()
860
    }
861
862
    #[must_use]
863
    pub fn encryption_state(&self) -> Option<EncryptionState> {
864
        let device_id = self.client.device_id()?.to_owned();
865
        let encryption = self.client.encryption();
866
        Some(encryption_state(
867
            device_id,
868
            encryption.recovery().state(),
869
            encryption.verification_state().get(),
870
            encryption.backups().state(),
871
        ))
872
    }
873
}
874
875
/// one space's hierarchy, a page at a time. hold on to it while you browse, it remembers where it got to
876
pub struct SpaceHierarchy(SpaceRoomList);
877
878
impl fmt::Debug for SpaceHierarchy {
879
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
880
        formatter
881
            .debug_struct("SpaceHierarchy")
882
            .finish_non_exhaustive()
883
    }
884
}
885
886
impl SpaceHierarchy {
887
    /// fetches the next page. a no-op once the end is reached or while a page is in flight
888
    pub async fn paginate(&self) -> Result<(), Error> {
889
        self.0.paginate().await.map_err(|source| {
890
            Error::with_source(
891
                ErrorKind::Server,
892
                "fetching the space hierarchy failed",
893
                source,
894
            )
895
        })
896
    }
897
898
    /// everything fetched so far, the space itself excluded
899
    pub async fn rooms(&self) -> Vec<SpaceChild> {
900
        self.0.rooms().await.into_iter().map(space_child).collect()
901
    }
902
903
    #[must_use]
904
    pub fn end_reached(&self) -> bool {
905
        matches!(
906
            self.0.pagination_state(),
907
            SpaceRoomListPaginationState::Idle { end_reached: true }
908
        )
909
    }
910
}
911
912
fn profile(
913
    user_id: OwnedUserId,
914
    response: &ruma::api::client::profile::get_profile::v3::Response,
915
) -> Result<Profile, Error> {
916
    use ruma::api::client::profile::{AvatarUrl, DisplayName};
917
    let malformed = |source| {
918
        Error::with_source(
919
            ErrorKind::Server,
920
            "the server sent a malformed profile",
921
            source,
922
        )
923
    };
924
    Ok(Profile {
925
        user_id,
926
        display_name: response.get_static::<DisplayName>().map_err(malformed)?,
927
        avatar: response
928
            .get_static::<AvatarUrl>()
929
            .map_err(malformed)?
930
            .as_deref()
931
            .map(MediaId::plain),
932
    })
933
}
934
935
#[derive(Debug)]
936
pub(crate) struct Request {
937
    pub(crate) command: Command,
938
    pub(crate) reply: Responder,
939
}
940
941
impl Request {
942
    fn new(command: Command) -> (Self, Pending) {
943
        let (reply, pending) = oneshot::channel();
944
        (
945
            Self {
946
                command,
947
                reply: Responder(reply),
948
            },
949
            Pending(pending),
950
        )
951
    }
952
}
953
954
/// whoever holds this owes the caller an answer. dropping it answers `Interrupted`
955
#[derive(Debug)]
956
pub(crate) struct Responder(oneshot::Sender<Result<CommandOutput, Error>>);
957
958
impl Responder {
959
    pub(crate) fn send(self, result: Result<CommandOutput, Error>) {
960
        if let Err(error) = &result {
961
            tracing::debug!(%error, kind = ?error.kind, "command failed");
962
        }
963
        let _ = self.0.send(result);
964
    }
965
966
    pub(crate) fn done(self, result: Result<(), Error>) {
967
        self.send(result.map(|()| CommandOutput::Done));
968
    }
969
}
970
971
#[derive(Debug)]
972
pub struct EventStream {
973
    events: mpsc::Receiver<Event>,
974
}
975
976
impl EventStream {
977
    pub async fn recv(&mut self) -> Option<Event> {
978
        self.events.recv().await
979
    }
980
981
    pub fn try_recv(&mut self) -> Result<Event, mpsc::error::TryRecvError> {
982
        self.events.try_recv()
983
    }
984
}
985
986
impl futures_util::Stream for EventStream {
987
    type Item = Event;
988
989
    fn poll_next(mut self: Pin<&mut Self>, context: &mut Context<'_>) -> Poll<Option<Self::Item>> {
990
        self.events.poll_recv(context)
991
    }
992
}
993
994
#[derive(Debug)]
995
pub(crate) struct DriverPort {
996
    requests: mpsc::Receiver<Request>,
997
    events: mpsc::Sender<Event>,
998
    connection: watch::Sender<ConnectionState>,
999
}
1000
1001
#[derive(Clone, Debug)]
1002
pub(crate) struct EventSender(mpsc::Sender<Event>);
1003
1004
impl EventSender {
1005
    pub(crate) async fn send(&self, event: Event) -> Result<(), SendEventError> {
1006
        self.0.send(event).await.map_err(|_| SendEventError)
1007
    }
1008
1009
    pub(crate) async fn reserve(&self) -> Result<mpsc::Permit<'_, Event>, SendEventError> {
1010
        self.0.reserve().await.map_err(|_| SendEventError)
1011
    }
1012
1013
    /// resolves once every `EventStream` is gone
1014
    pub(crate) async fn closed(&self) {
1015
        self.0.closed().await;
1016
    }
1017
}
1018
1019
impl DriverPort {
1020
    pub(crate) fn event_sender(&self) -> EventSender {
1021
        EventSender(self.events.clone())
1022
    }
1023
1024
    pub(crate) fn connection_sender(&self) -> watch::Sender<ConnectionState> {
1025
        self.connection.clone()
1026
    }
1027
1028
    pub(crate) async fn recv(&mut self) -> Option<Request> {
1029
        self.requests.recv().await
1030
    }
1031
1032
    pub(crate) fn try_recv(&mut self) -> Option<Request> {
1033
        self.requests.try_recv().ok()
1034
    }
1035
1036
    pub(crate) fn close(&mut self) {
1037
        self.requests.close();
1038
    }
1039
}
1040
1041
#[derive(Clone, Copy, Debug, Eq, PartialEq, thiserror::Error)]
1042
#[error("the event receiver has closed")]
1043
pub(crate) struct SendEventError;
1044
1045
#[must_use]
1046
pub(crate) fn transport_pair(
1047
    client: Client,
1048
    sync_service: Arc<SyncService>,
1049
    media: Arc<MediaService>,
1050
    store_lock: Option<Arc<StoreLock>>,
1051
    command_capacity: usize,
1052
    event_capacity: usize,
1053
) -> (ClientHandle, DriverPort, EventStream) {
1054
    let (request_tx, request_rx) = mpsc::channel(command_capacity);
1055
    let (event_tx, event_rx) = mpsc::channel(event_capacity);
1056
    let (connection_tx, connection_rx) = watch::channel(ConnectionState::Idle);
1057
    (
1058
        ClientHandle {
1059
            requests: request_tx,
1060
            client,
1061
            connection: connection_rx,
1062
            notification_client: Arc::new(OnceCell::new()),
1063
            sync_service,
1064
            media,
1065
            spaces: Arc::new(OnceCell::new()),
1066
            members_checked: Arc::default(),
1067
            _store_lock: store_lock,
1068
        },
1069
        DriverPort {
1070
            requests: request_rx,
1071
            events: event_tx,
1072
            connection: connection_tx,
1073
        },
1074
        EventStream { events: event_rx },
1075
    )
1076
}
1077
1078
async fn list_members(
1079
    room: &matrix_sdk::Room,
1080
    filter: MemberFilter,
1081
) -> Result<Vec<MemberSummary>, Error> {
1082
    let memberships = match filter {
1083
        MemberFilter::Active => RoomMemberships::ACTIVE,
1084
        MemberFilter::Joined => RoomMemberships::JOIN,
1085
        MemberFilter::Invited => RoomMemberships::INVITE,
1086
        MemberFilter::Banned => RoomMemberships::BAN,
1087
        MemberFilter::Left => RoomMemberships::LEAVE,
1088
        // An empty mask is the SDK's unfiltered wildcard: it matches every
1089
        // membership, including custom states that `all()` would exclude.
1090
        MemberFilter::All => RoomMemberships::empty(),
1091
    };
1092
    Ok(room
1093
        .members(memberships)
1094
        .await
1095
        .map_err(|source| Error::from_sdk("listing members failed", source))?
1096
        .into_iter()
1097
        .map(member_summary)
1098
        .collect())
1099
}
1100
1101
fn normalise_push_notification(
1102
    room_id: &RoomId,
1103
    event_id: &EventId,
1104
    status: NotificationStatus,
1105
) -> PushNotificationStatus {
1106
    match status {
1107
        NotificationStatus::Event(item) => {
1108
            let event = match &item.raw_event {
1109
                RawNotificationEvent::Timeline(event) => {
1110
                    PushNotificationEvent::Timeline(event.clone())
1111
                }
1112
                RawNotificationEvent::Invite(event) => PushNotificationEvent::Invite(event.clone()),
1113
            };
1114
            PushNotificationStatus::Event(Box::new(PushNotification {
1115
                event_id: event_id.to_owned(),
1116
                event,
1117
                sender: PushNotificationSender {
1118
                    user_id: item.event.sender().to_owned(),
1119
                    display_name: item.sender_display_name,
1120
                    avatar_url: item.sender_avatar_url.as_deref().map(MediaId::plain),
1121
                    name_ambiguous: item.is_sender_name_ambiguous,
1122
                },
1123
                room: PushNotificationRoom {
1124
                    room_id: room_id.to_owned(),
1125
                    name: item.room_computed_display_name,
1126
                    avatar_url: item.room_avatar_url.as_deref().map(MediaId::plain),
1127
                    canonical_alias: item
1128
                        .room_canonical_alias
1129
                        .as_deref()
1130
                        .and_then(|alias| alias.parse().ok()),
1131
                    topic: item.room_topic,
1132
                    encrypted: item.is_room_encrypted,
1133
                    direct: item.room_is_dm,
1134
                    joined_members_count: item.joined_members_count,
1135
                    service_members: item
1136
                        .service_members
1137
                        .into_iter()
1138
                        .filter_map(|user_id| user_id.parse().ok())
1139
                        .collect(),
1140
                    active_service_members_count: item.active_service_members_count,
1141
                    space: item.is_space,
1142
                },
1143
                noisy: item.is_noisy,
1144
                has_mention: item.has_mention,
1145
                thread_id: item.thread_id,
1146
            }))
1147
        }
1148
        NotificationStatus::EventNotFound => PushNotificationStatus::NotFound,
1149
        NotificationStatus::EventFilteredOut => PushNotificationStatus::FilteredOut,
1150
        NotificationStatus::EventRedacted => PushNotificationStatus::Redacted,
1151
    }
1152
}