atagen

macltok

public
Star0
Watch0
62fe32759529d8db8bc39738189399cea0f4511d
3
ssh://brewery@pls.lobotomise.me:33/atagen/macltok.git

Blame: src/engine/watchers.rs

1fe480c16a3catagen2026-09-26 08:44 UTC1
use std::{sync::Arc, time::Duration};
31ce2453c82fatagen2026-09-24 13:07 UTC2
31ce2453c82fatagen2026-09-24 13:07 UTC3
use futures_util::StreamExt;
31ce2453c82fatagen2026-09-24 13:07 UTC4
use matrix_sdk::{
31ce2453c82fatagen2026-09-24 13:07 UTC5
    Client, Room, SessionChange,
31ce2453c82fatagen2026-09-24 13:07 UTC6
    ruma::events::{AnyMessageLikeEventContent, room::message::MessageType},
31ce2453c82fatagen2026-09-24 13:07 UTC7
    send_queue::{LocalEchoContent, RoomSendQueueUpdate},
31ce2453c82fatagen2026-09-24 13:07 UTC8
};
31ce2453c82fatagen2026-09-24 13:07 UTC9
use matrix_sdk_ui::sync_service::{State as SyncState, SyncService};
1fe480c16a3catagen2026-09-26 08:44 UTC10
use ruma::time::Instant;
1fe480c16a3catagen2026-09-26 08:44 UTC11
use tokio::sync::{Notify, broadcast::error::RecvError, watch};
31ce2453c82fatagen2026-09-24 13:07 UTC12
1fe480c16a3catagen2026-09-26 08:44 UTC13
use super::executor::TaskSet;
31ce2453c82fatagen2026-09-24 13:07 UTC14
use crate::{
31ce2453c82fatagen2026-09-24 13:07 UTC15
    ConnectionState, Event, encryption::encryption_state, error::classify_sdk_error,
31ce2453c82fatagen2026-09-24 13:07 UTC16
    transport::EventSender,
31ce2453c82fatagen2026-09-24 13:07 UTC17
};
31ce2453c82fatagen2026-09-24 13:07 UTC18
31ce2453c82fatagen2026-09-24 13:07 UTC19
const SYNC_RESTART_MIN: Duration = Duration::from_secs(1);
31ce2453c82fatagen2026-09-24 13:07 UTC20
const SYNC_RESTART_MAX: Duration = Duration::from_secs(60);
31ce2453c82fatagen2026-09-24 13:07 UTC21
const SEND_QUEUE_RETRY: Duration = Duration::from_secs(5);
5a52012c9b1datagen2026-09-24 23:45 UTC22
const SYNC_STABLE: Duration = Duration::from_secs(30);
5a52012c9b1datagen2026-09-24 23:45 UTC23
5a52012c9b1datagen2026-09-24 23:45 UTC24
pub(super) async fn room_subscriptions(
5a52012c9b1datagen2026-09-24 23:45 UTC25
    sync: Arc<SyncService>,
5a52012c9b1datagen2026-09-24 23:45 UTC26
    mut rooms: watch::Receiver<Vec<ruma::OwnedRoomId>>,
5a52012c9b1datagen2026-09-24 23:45 UTC27
) {
5a52012c9b1datagen2026-09-24 23:45 UTC28
    while rooms.changed().await.is_ok() {
5a52012c9b1datagen2026-09-24 23:45 UTC29
        let room_ids = rooms.borrow_and_update().clone();
5a52012c9b1datagen2026-09-24 23:45 UTC30
        let room_ids = room_ids.iter().map(AsRef::as_ref).collect::<Vec<_>>();
5a52012c9b1datagen2026-09-24 23:45 UTC31
        sync.room_list_service().subscribe_to_rooms(&room_ids).await;
5a52012c9b1datagen2026-09-24 23:45 UTC32
    }
5a52012c9b1datagen2026-09-24 23:45 UTC33
}
31ce2453c82fatagen2026-09-24 13:07 UTC34
31ce2453c82fatagen2026-09-24 13:07 UTC35
pub(super) async fn session(
31ce2453c82fatagen2026-09-24 13:07 UTC36
    mut changes: tokio::sync::broadcast::Receiver<SessionChange>,
31ce2453c82fatagen2026-09-24 13:07 UTC37
    events: EventSender,
31ce2453c82fatagen2026-09-24 13:07 UTC38
    stop: Arc<Notify>,
31ce2453c82fatagen2026-09-24 13:07 UTC39
) {
31ce2453c82fatagen2026-09-24 13:07 UTC40
    loop {
31ce2453c82fatagen2026-09-24 13:07 UTC41
        let event = match changes.recv().await {
31ce2453c82fatagen2026-09-24 13:07 UTC42
            Ok(SessionChange::TokensRefreshed) => Event::SessionTokensChanged,
31ce2453c82fatagen2026-09-24 13:07 UTC43
            Ok(SessionChange::UnknownToken(data)) => {
31ce2453c82fatagen2026-09-24 13:07 UTC44
                let _ = events
31ce2453c82fatagen2026-09-24 13:07 UTC45
                    .send(Event::SessionAccessRejected {
31ce2453c82fatagen2026-09-24 13:07 UTC46
                        soft_logout: data.soft_logout,
31ce2453c82fatagen2026-09-24 13:07 UTC47
                    })
31ce2453c82fatagen2026-09-24 13:07 UTC48
                    .await;
31ce2453c82fatagen2026-09-24 13:07 UTC49
                stop.notify_one();
31ce2453c82fatagen2026-09-24 13:07 UTC50
                return;
31ce2453c82fatagen2026-09-24 13:07 UTC51
            }
31ce2453c82fatagen2026-09-24 13:07 UTC52
            // a missed refresh costs one needless save, a missed rejection comes round again
31ce2453c82fatagen2026-09-24 13:07 UTC53
            Err(RecvError::Lagged(_)) => Event::SessionTokensChanged,
31ce2453c82fatagen2026-09-24 13:07 UTC54
            Err(RecvError::Closed) => return,
31ce2453c82fatagen2026-09-24 13:07 UTC55
        };
31ce2453c82fatagen2026-09-24 13:07 UTC56
        if events.send(event).await.is_err() {
31ce2453c82fatagen2026-09-24 13:07 UTC57
            return;
31ce2453c82fatagen2026-09-24 13:07 UTC58
        }
31ce2453c82fatagen2026-09-24 13:07 UTC59
    }
31ce2453c82fatagen2026-09-24 13:07 UTC60
}
31ce2453c82fatagen2026-09-24 13:07 UTC61
31ce2453c82fatagen2026-09-24 13:07 UTC62
/// `states` must be taken before sync starts, a late subscriber never hears the transition it missed
31ce2453c82fatagen2026-09-24 13:07 UTC63
pub(super) async fn connection(
31ce2453c82fatagen2026-09-24 13:07 UTC64
    mut states: impl futures_util::Stream<Item = SyncState> + Unpin,
31ce2453c82fatagen2026-09-24 13:07 UTC65
    sync: Arc<SyncService>,
31ce2453c82fatagen2026-09-24 13:07 UTC66
    client: Client,
31ce2453c82fatagen2026-09-24 13:07 UTC67
    events: EventSender,
31ce2453c82fatagen2026-09-24 13:07 UTC68
    connection: watch::Sender<ConnectionState>,
31ce2453c82fatagen2026-09-24 13:07 UTC69
) {
31ce2453c82fatagen2026-09-24 13:07 UTC70
    let mut backoff = SYNC_RESTART_MIN;
5a52012c9b1datagen2026-09-24 23:45 UTC71
    let mut online_since: Option<Instant> = None;
31ce2453c82fatagen2026-09-24 13:07 UTC72
    while let Some(state) = states.next().await {
5a52012c9b1datagen2026-09-24 23:45 UTC73
        // only a run that lasted counts as recovered, or a server that answers /versions
5a52012c9b1datagen2026-09-24 23:45 UTC74
        // but refuses sync would have us bounce between offline and online forever
5a52012c9b1datagen2026-09-24 23:45 UTC75
        let stable = online_since
5a52012c9b1datagen2026-09-24 23:45 UTC76
            .take()
5a52012c9b1datagen2026-09-24 23:45 UTC77
            .map(|since| since.elapsed() >= SYNC_STABLE);
5a52012c9b1datagen2026-09-24 23:45 UTC78
        if stable == Some(true) {
5a52012c9b1datagen2026-09-24 23:45 UTC79
            backoff = SYNC_RESTART_MIN;
5a52012c9b1datagen2026-09-24 23:45 UTC80
        }
31ce2453c82fatagen2026-09-24 13:07 UTC81
        let (state, restart) = match state {
5a52012c9b1datagen2026-09-24 23:45 UTC82
            // only ever us stopping it for a restart, nothing a client needs to hear
5a52012c9b1datagen2026-09-24 23:45 UTC83
            SyncState::Idle => continue,
31ce2453c82fatagen2026-09-24 13:07 UTC84
            SyncState::Running => {
5a52012c9b1datagen2026-09-24 23:45 UTC85
                online_since = Some(Instant::now());
31ce2453c82fatagen2026-09-24 13:07 UTC86
                // the sdk parks a room queue on any send error and never unparks it
31ce2453c82fatagen2026-09-24 13:07 UTC87
                client.send_queue().set_enabled(true).await;
31ce2453c82fatagen2026-09-24 13:07 UTC88
                (ConnectionState::Online, false)
31ce2453c82fatagen2026-09-24 13:07 UTC89
            }
5a52012c9b1datagen2026-09-24 23:45 UTC90
            SyncState::Offline => (ConnectionState::Offline, stable == Some(false)),
31ce2453c82fatagen2026-09-24 13:07 UTC91
            SyncState::Terminated => (ConnectionState::Offline, true),
31ce2453c82fatagen2026-09-24 13:07 UTC92
            SyncState::Error(error) => {
31ce2453c82fatagen2026-09-24 13:07 UTC93
                tracing::warn!(%error, "sync service reported an error");
31ce2453c82fatagen2026-09-24 13:07 UTC94
                (ConnectionState::Offline, true)
31ce2453c82fatagen2026-09-24 13:07 UTC95
            }
31ce2453c82fatagen2026-09-24 13:07 UTC96
        };
31ce2453c82fatagen2026-09-24 13:07 UTC97
        if *connection.borrow() != state {
31ce2453c82fatagen2026-09-24 13:07 UTC98
            connection.send_replace(state);
31ce2453c82fatagen2026-09-24 13:07 UTC99
            if events.send(Event::ConnectionChanged(state)).await.is_err() {
31ce2453c82fatagen2026-09-24 13:07 UTC100
                return;
31ce2453c82fatagen2026-09-24 13:07 UTC101
            }
31ce2453c82fatagen2026-09-24 13:07 UTC102
        }
31ce2453c82fatagen2026-09-24 13:07 UTC103
        if restart {
5a52012c9b1datagen2026-09-24 23:45 UTC104
            sync.stop().await;
1fe480c16a3catagen2026-09-26 08:44 UTC105
            matrix_sdk::sleep::sleep(backoff).await;
31ce2453c82fatagen2026-09-24 13:07 UTC106
            backoff = (backoff * 2).min(SYNC_RESTART_MAX);
31ce2453c82fatagen2026-09-24 13:07 UTC107
            sync.start().await;
31ce2453c82fatagen2026-09-24 13:07 UTC108
        }
31ce2453c82fatagen2026-09-24 13:07 UTC109
    }
31ce2453c82fatagen2026-09-24 13:07 UTC110
}
31ce2453c82fatagen2026-09-24 13:07 UTC111
31ce2453c82fatagen2026-09-24 13:07 UTC112
/// a recoverable failure while we're online is a hiccup, so give the room another go
31ce2453c82fatagen2026-09-24 13:07 UTC113
pub(super) async fn send_queue_errors(
31ce2453c82fatagen2026-09-24 13:07 UTC114
    client: Client,
31ce2453c82fatagen2026-09-24 13:07 UTC115
    mut errors: tokio::sync::broadcast::Receiver<matrix_sdk::send_queue::SendQueueRoomError>,
31ce2453c82fatagen2026-09-24 13:07 UTC116
    online: watch::Receiver<ConnectionState>,
31ce2453c82fatagen2026-09-24 13:07 UTC117
) {
1fe480c16a3catagen2026-09-26 08:44 UTC118
    let mut retries = TaskSet::new();
31ce2453c82fatagen2026-09-24 13:07 UTC119
    loop {
31ce2453c82fatagen2026-09-24 13:07 UTC120
        let error = tokio::select! {
31ce2453c82fatagen2026-09-24 13:07 UTC121
            error = errors.recv() => error,
31ce2453c82fatagen2026-09-24 13:07 UTC122
            Some(_) = retries.join_next(), if !retries.is_empty() => continue,
31ce2453c82fatagen2026-09-24 13:07 UTC123
        };
31ce2453c82fatagen2026-09-24 13:07 UTC124
        match error {
31ce2453c82fatagen2026-09-24 13:07 UTC125
            Ok(error) if error.is_recoverable => {
31ce2453c82fatagen2026-09-24 13:07 UTC126
                let client = client.clone();
31ce2453c82fatagen2026-09-24 13:07 UTC127
                let online = online.clone();
31ce2453c82fatagen2026-09-24 13:07 UTC128
                retries.spawn(async move {
1fe480c16a3catagen2026-09-26 08:44 UTC129
                    matrix_sdk::sleep::sleep(SEND_QUEUE_RETRY).await;
31ce2453c82fatagen2026-09-24 13:07 UTC130
                    if *online.borrow() == ConnectionState::Online
31ce2453c82fatagen2026-09-24 13:07 UTC131
                        && let Some(room) = client.get_room(&error.room_id)
31ce2453c82fatagen2026-09-24 13:07 UTC132
                    {
31ce2453c82fatagen2026-09-24 13:07 UTC133
                        room.send_queue().set_enabled(true);
31ce2453c82fatagen2026-09-24 13:07 UTC134
                    }
31ce2453c82fatagen2026-09-24 13:07 UTC135
                });
31ce2453c82fatagen2026-09-24 13:07 UTC136
            }
31ce2453c82fatagen2026-09-24 13:07 UTC137
            Ok(_) | Err(RecvError::Lagged(_)) => {}
31ce2453c82fatagen2026-09-24 13:07 UTC138
            Err(RecvError::Closed) => return,
31ce2453c82fatagen2026-09-24 13:07 UTC139
        }
31ce2453c82fatagen2026-09-24 13:07 UTC140
    }
31ce2453c82fatagen2026-09-24 13:07 UTC141
}
31ce2453c82fatagen2026-09-24 13:07 UTC142
31ce2453c82fatagen2026-09-24 13:07 UTC143
/// upload progress only goes out on the room's own channel, never the client-wide one
31ce2453c82fatagen2026-09-24 13:07 UTC144
pub(super) async fn room_uploads(room: Room, events: EventSender, ready: watch::Sender<bool>) {
31ce2453c82fatagen2026-09-24 13:07 UTC145
    let room_id = room.room_id().to_owned();
31ce2453c82fatagen2026-09-24 13:07 UTC146
    let mut updates = match room.send_queue().subscribe().await {
31ce2453c82fatagen2026-09-24 13:07 UTC147
        Ok((_, updates)) => updates,
31ce2453c82fatagen2026-09-24 13:07 UTC148
        Err(error) => {
31ce2453c82fatagen2026-09-24 13:07 UTC149
            tracing::warn!(%room_id, %error, "reading the media send queue failed");
31ce2453c82fatagen2026-09-24 13:07 UTC150
            return;
31ce2453c82fatagen2026-09-24 13:07 UTC151
        }
31ce2453c82fatagen2026-09-24 13:07 UTC152
    };
31ce2453c82fatagen2026-09-24 13:07 UTC153
    ready.send_replace(true);
31ce2453c82fatagen2026-09-24 13:07 UTC154
    loop {
31ce2453c82fatagen2026-09-24 13:07 UTC155
        let update = match updates.recv().await {
31ce2453c82fatagen2026-09-24 13:07 UTC156
            Ok(update) => update,
5a52012c9b1datagen2026-09-24 23:45 UTC157
            // a missed tick is fine, the next one says where it's got to
5a52012c9b1datagen2026-09-24 23:45 UTC158
            Err(RecvError::Lagged(_)) => continue,
5a52012c9b1datagen2026-09-24 23:45 UTC159
            Err(RecvError::Closed) => return,
5a52012c9b1datagen2026-09-24 23:45 UTC160
        };
5a52012c9b1datagen2026-09-24 23:45 UTC161
        let RoomSendQueueUpdate::MediaUpload {
5a52012c9b1datagen2026-09-24 23:45 UTC162
            related_to,
5a52012c9b1datagen2026-09-24 23:45 UTC163
            file,
5a52012c9b1datagen2026-09-24 23:45 UTC164
            progress,
5a52012c9b1datagen2026-09-24 23:45 UTC165
            ..
5a52012c9b1datagen2026-09-24 23:45 UTC166
        } = update
5a52012c9b1datagen2026-09-24 23:45 UTC167
        else {
5a52012c9b1datagen2026-09-24 23:45 UTC168
            continue;
5a52012c9b1datagen2026-09-24 23:45 UTC169
        };
5a52012c9b1datagen2026-09-24 23:45 UTC170
        let event = Event::MediaUploadProgress {
5a52012c9b1datagen2026-09-24 23:45 UTC171
            room_id: room_id.clone(),
5a52012c9b1datagen2026-09-24 23:45 UTC172
            transaction_id: related_to,
5a52012c9b1datagen2026-09-24 23:45 UTC173
            uploaded_bytes: progress.current as u64,
5a52012c9b1datagen2026-09-24 23:45 UTC174
            total_bytes: progress.total as u64,
5a52012c9b1datagen2026-09-24 23:45 UTC175
            complete: file.is_some() && progress.current == progress.total,
5a52012c9b1datagen2026-09-24 23:45 UTC176
        };
5a52012c9b1datagen2026-09-24 23:45 UTC177
        if events.send(event).await.is_err() {
5a52012c9b1datagen2026-09-24 23:45 UTC178
            return;
5a52012c9b1datagen2026-09-24 23:45 UTC179
        }
5a52012c9b1datagen2026-09-24 23:45 UTC180
    }
5a52012c9b1datagen2026-09-24 23:45 UTC181
}
5a52012c9b1datagen2026-09-24 23:45 UTC182
5a52012c9b1datagen2026-09-24 23:45 UTC183
/// failures do reach the client-wide channel, and this one exists before any upload does
5a52012c9b1datagen2026-09-24 23:45 UTC184
pub(super) async fn upload_failures(
5a52012c9b1datagen2026-09-24 23:45 UTC185
    client: Client,
5a52012c9b1datagen2026-09-24 23:45 UTC186
    mut updates: tokio::sync::broadcast::Receiver<matrix_sdk::send_queue::SendQueueUpdate>,
5a52012c9b1datagen2026-09-24 23:45 UTC187
    events: EventSender,
5a52012c9b1datagen2026-09-24 23:45 UTC188
) {
5a52012c9b1datagen2026-09-24 23:45 UTC189
    loop {
5a52012c9b1datagen2026-09-24 23:45 UTC190
        let update = match updates.recv().await {
5a52012c9b1datagen2026-09-24 23:45 UTC191
            Ok(update) => update,
31ce2453c82fatagen2026-09-24 13:07 UTC192
            Err(RecvError::Lagged(skipped)) => {
5a52012c9b1datagen2026-09-24 23:45 UTC193
                tracing::warn!(skipped, "upload failure watcher lagged");
31ce2453c82fatagen2026-09-24 13:07 UTC194
                continue;
31ce2453c82fatagen2026-09-24 13:07 UTC195
            }
31ce2453c82fatagen2026-09-24 13:07 UTC196
            Err(RecvError::Closed) => return,
31ce2453c82fatagen2026-09-24 13:07 UTC197
        };
5a52012c9b1datagen2026-09-24 23:45 UTC198
        let RoomSendQueueUpdate::SendError {
5a52012c9b1datagen2026-09-24 23:45 UTC199
            transaction_id,
5a52012c9b1datagen2026-09-24 23:45 UTC200
            error,
5a52012c9b1datagen2026-09-24 23:45 UTC201
            is_recoverable,
5a52012c9b1datagen2026-09-24 23:45 UTC202
        } = update.update
5a52012c9b1datagen2026-09-24 23:45 UTC203
        else {
5a52012c9b1datagen2026-09-24 23:45 UTC204
            continue;
5a52012c9b1datagen2026-09-24 23:45 UTC205
        };
5a52012c9b1datagen2026-09-24 23:45 UTC206
        let Some(room) = client.get_room(&update.room_id) else {
5a52012c9b1datagen2026-09-24 23:45 UTC207
            continue;
5a52012c9b1datagen2026-09-24 23:45 UTC208
        };
5a52012c9b1datagen2026-09-24 23:45 UTC209
        let is_attachment = room
5a52012c9b1datagen2026-09-24 23:45 UTC210
            .send_queue()
5a52012c9b1datagen2026-09-24 23:45 UTC211
            .subscribe()
5a52012c9b1datagen2026-09-24 23:45 UTC212
            .await
5a52012c9b1datagen2026-09-24 23:45 UTC213
            .is_ok_and(|(echoes, _)| {
5a52012c9b1datagen2026-09-24 23:45 UTC214
                echoes.iter().any(|echo| {
5a52012c9b1datagen2026-09-24 23:45 UTC215
                    echo.transaction_id == transaction_id && is_attachment_echo(&echo.content)
5a52012c9b1datagen2026-09-24 23:45 UTC216
                })
5a52012c9b1datagen2026-09-24 23:45 UTC217
            });
5a52012c9b1datagen2026-09-24 23:45 UTC218
        if !is_attachment {
5a52012c9b1datagen2026-09-24 23:45 UTC219
            continue;
5a52012c9b1datagen2026-09-24 23:45 UTC220
        }
5a52012c9b1datagen2026-09-24 23:45 UTC221
        let event = Event::MediaUploadFailed {
5a52012c9b1datagen2026-09-24 23:45 UTC222
            room_id: update.room_id,
5a52012c9b1datagen2026-09-24 23:45 UTC223
            transaction_id,
5a52012c9b1datagen2026-09-24 23:45 UTC224
            kind: classify_sdk_error(&error),
5a52012c9b1datagen2026-09-24 23:45 UTC225
            recoverable: is_recoverable,
31ce2453c82fatagen2026-09-24 13:07 UTC226
        };
31ce2453c82fatagen2026-09-24 13:07 UTC227
        if events.send(event).await.is_err() {
31ce2453c82fatagen2026-09-24 13:07 UTC228
            return;
31ce2453c82fatagen2026-09-24 13:07 UTC229
        }
31ce2453c82fatagen2026-09-24 13:07 UTC230
    }
31ce2453c82fatagen2026-09-24 13:07 UTC231
}
31ce2453c82fatagen2026-09-24 13:07 UTC232
31ce2453c82fatagen2026-09-24 13:07 UTC233
fn is_attachment_echo(content: &LocalEchoContent) -> bool {
31ce2453c82fatagen2026-09-24 13:07 UTC234
    let LocalEchoContent::Event {
31ce2453c82fatagen2026-09-24 13:07 UTC235
        serialized_event, ..
31ce2453c82fatagen2026-09-24 13:07 UTC236
    } = content
31ce2453c82fatagen2026-09-24 13:07 UTC237
    else {
31ce2453c82fatagen2026-09-24 13:07 UTC238
        return false;
31ce2453c82fatagen2026-09-24 13:07 UTC239
    };
31ce2453c82fatagen2026-09-24 13:07 UTC240
    matches!(
31ce2453c82fatagen2026-09-24 13:07 UTC241
        serialized_event.deserialize(),
31ce2453c82fatagen2026-09-24 13:07 UTC242
        Ok(AnyMessageLikeEventContent::RoomMessage(content))
31ce2453c82fatagen2026-09-24 13:07 UTC243
            if matches!(
31ce2453c82fatagen2026-09-24 13:07 UTC244
                content.msgtype,
31ce2453c82fatagen2026-09-24 13:07 UTC245
                MessageType::Audio(_) | MessageType::File(_) | MessageType::Image(_) | MessageType::Video(_)
31ce2453c82fatagen2026-09-24 13:07 UTC246
            )
31ce2453c82fatagen2026-09-24 13:07 UTC247
    )
31ce2453c82fatagen2026-09-24 13:07 UTC248
}
31ce2453c82fatagen2026-09-24 13:07 UTC249
31ce2453c82fatagen2026-09-24 13:07 UTC250
pub(super) async fn encryption(client: Client, events: EventSender) {
31ce2453c82fatagen2026-09-24 13:07 UTC251
    let Some(device_id) = client.device_id().map(ToOwned::to_owned) else {
31ce2453c82fatagen2026-09-24 13:07 UTC252
        tracing::warn!("encryption watcher stopped because the client has no device id");
31ce2453c82fatagen2026-09-24 13:07 UTC253
        return;
31ce2453c82fatagen2026-09-24 13:07 UTC254
    };
31ce2453c82fatagen2026-09-24 13:07 UTC255
    let recovery = client.encryption().recovery();
31ce2453c82fatagen2026-09-24 13:07 UTC256
    let backups = client.encryption().backups();
31ce2453c82fatagen2026-09-24 13:07 UTC257
    let mut recovery_stream = recovery.state_stream();
31ce2453c82fatagen2026-09-24 13:07 UTC258
    let mut backup_stream = backups.state_stream();
31ce2453c82fatagen2026-09-24 13:07 UTC259
    let mut verification_stream = client.encryption().verification_state();
31ce2453c82fatagen2026-09-24 13:07 UTC260
    let mut recovery_state = recovery.state();
31ce2453c82fatagen2026-09-24 13:07 UTC261
    let mut backup_state = backups.state();
31ce2453c82fatagen2026-09-24 13:07 UTC262
    let mut verification_state = verification_stream.get();
31ce2453c82fatagen2026-09-24 13:07 UTC263
    loop {
31ce2453c82fatagen2026-09-24 13:07 UTC264
        let state = encryption_state(
31ce2453c82fatagen2026-09-24 13:07 UTC265
            device_id.clone(),
31ce2453c82fatagen2026-09-24 13:07 UTC266
            recovery_state,
31ce2453c82fatagen2026-09-24 13:07 UTC267
            verification_state,
31ce2453c82fatagen2026-09-24 13:07 UTC268
            backup_state,
31ce2453c82fatagen2026-09-24 13:07 UTC269
        );
31ce2453c82fatagen2026-09-24 13:07 UTC270
        if events.send(Event::EncryptionChanged(state)).await.is_err() {
31ce2453c82fatagen2026-09-24 13:07 UTC271
            return;
31ce2453c82fatagen2026-09-24 13:07 UTC272
        }
31ce2453c82fatagen2026-09-24 13:07 UTC273
        tokio::select! {
31ce2453c82fatagen2026-09-24 13:07 UTC274
            Some(update) = recovery_stream.next() => recovery_state = update,
31ce2453c82fatagen2026-09-24 13:07 UTC275
            Some(update) = verification_stream.next() => verification_state = update,
31ce2453c82fatagen2026-09-24 13:07 UTC276
            Some(update) = backup_stream.next() => {
31ce2453c82fatagen2026-09-24 13:07 UTC277
                backup_state = update.unwrap_or_else(|_| backups.state());
31ce2453c82fatagen2026-09-24 13:07 UTC278
            }
31ce2453c82fatagen2026-09-24 13:07 UTC279
            else => return,
31ce2453c82fatagen2026-09-24 13:07 UTC280
        }
31ce2453c82fatagen2026-09-24 13:07 UTC281
    }
31ce2453c82fatagen2026-09-24 13:07 UTC282
}
31ce2453c82fatagen2026-09-24 13:07 UTC283
31ce2453c82fatagen2026-09-24 13:07 UTC284
pub(super) async fn rooms(client: Client, events: EventSender) {
31ce2453c82fatagen2026-09-24 13:07 UTC285
    let (_, mut rooms) = client.rooms_stream();
31ce2453c82fatagen2026-09-24 13:07 UTC286
    let mut info = client.room_info_notable_update_receiver();
31ce2453c82fatagen2026-09-24 13:07 UTC287
    loop {
31ce2453c82fatagen2026-09-24 13:07 UTC288
        if events.send(Event::RoomsChanged).await.is_err() {
31ce2453c82fatagen2026-09-24 13:07 UTC289
            return;
31ce2453c82fatagen2026-09-24 13:07 UTC290
        }
31ce2453c82fatagen2026-09-24 13:07 UTC291
        tokio::select! {
31ce2453c82fatagen2026-09-24 13:07 UTC292
            update = rooms.next() => if update.is_none() { return },
31ce2453c82fatagen2026-09-24 13:07 UTC293
            update = info.recv() => if matches!(update, Err(RecvError::Closed)) { return },
31ce2453c82fatagen2026-09-24 13:07 UTC294
        }
31ce2453c82fatagen2026-09-24 13:07 UTC295
        // a sync touching fifty rooms is one invalidation, not fifty
31ce2453c82fatagen2026-09-24 13:07 UTC296
        while let Ok(_) | Err(tokio::sync::broadcast::error::TryRecvError::Lagged(_)) =
31ce2453c82fatagen2026-09-24 13:07 UTC297
            info.try_recv()
31ce2453c82fatagen2026-09-24 13:07 UTC298
        {}
31ce2453c82fatagen2026-09-24 13:07 UTC299
    }
31ce2453c82fatagen2026-09-24 13:07 UTC300
}
31ce2453c82fatagen2026-09-24 13:07 UTC301
31ce2453c82fatagen2026-09-24 13:07 UTC302
pub(super) async fn notification_settings(client: Client, events: EventSender) {
31ce2453c82fatagen2026-09-24 13:07 UTC303
    let settings = client.notification_settings().await;
31ce2453c82fatagen2026-09-24 13:07 UTC304
    let mut changes = settings.subscribe_to_changes();
31ce2453c82fatagen2026-09-24 13:07 UTC305
    while let Ok(()) | Err(RecvError::Lagged(_)) = changes.recv().await {
31ce2453c82fatagen2026-09-24 13:07 UTC306
        if events
31ce2453c82fatagen2026-09-24 13:07 UTC307
            .send(Event::NotificationSettingsChanged)
31ce2453c82fatagen2026-09-24 13:07 UTC308
            .await
31ce2453c82fatagen2026-09-24 13:07 UTC309
            .is_err()
31ce2453c82fatagen2026-09-24 13:07 UTC310
        {
31ce2453c82fatagen2026-09-24 13:07 UTC311
            return;
31ce2453c82fatagen2026-09-24 13:07 UTC312
        }
31ce2453c82fatagen2026-09-24 13:07 UTC313
    }
31ce2453c82fatagen2026-09-24 13:07 UTC314
}