atagen

macltok

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

Blame: src/engine/watchers.rs

411bc1c78b65atagen2026-10-22 07:39 UTC1
use std::{
411bc1c78b65atagen2026-10-22 07:39 UTC2
    collections::{HashSet, VecDeque},
411bc1c78b65atagen2026-10-22 07:39 UTC3
    sync::Arc,
411bc1c78b65atagen2026-10-22 07:39 UTC4
    time::Duration,
411bc1c78b65atagen2026-10-22 07:39 UTC5
};
31ce2453c82fatagen2026-09-24 13:07 UTC6
31ce2453c82fatagen2026-09-24 13:07 UTC7
use futures_util::StreamExt;
31ce2453c82fatagen2026-09-24 13:07 UTC8
use matrix_sdk::{
411bc1c78b65atagen2026-10-22 07:39 UTC9
    Client,
31ce2453c82fatagen2026-09-24 13:07 UTC10
    ruma::events::{AnyMessageLikeEventContent, room::message::MessageType},
0bfa3f46f0beatagen2026-09-26 09:12 UTC11
    send_queue::{LocalEchoContent, RoomSendQueueUpdate, SendQueueUpdate},
31ce2453c82fatagen2026-09-24 13:07 UTC12
};
17e042fbc331atagen2026-10-08 12:39 UTC13
use matrix_sdk_base::RoomInfoNotableUpdateReasons;
31ce2453c82fatagen2026-09-24 13:07 UTC14
use matrix_sdk_ui::sync_service::{State as SyncState, SyncService};
17e042fbc331atagen2026-10-08 12:39 UTC15
use ruma::{
17e042fbc331atagen2026-10-08 12:39 UTC16
    OwnedRoomId, OwnedTransactionId,
9bd34e25bb86atagen2026-10-24 07:23 UTC17
    events::{AnyGlobalAccountDataEvent, AnyRoomAccountDataEvent, AnySyncStateEvent},
17e042fbc331atagen2026-10-08 12:39 UTC18
    serde::Raw,
17e042fbc331atagen2026-10-08 12:39 UTC19
    time::Instant,
17e042fbc331atagen2026-10-08 12:39 UTC20
};
411bc1c78b65atagen2026-10-22 07:39 UTC21
use tokio::sync::{Notify, broadcast::error::RecvError, watch};
31ce2453c82fatagen2026-09-24 13:07 UTC22
f12377bce9b9atagen2026-09-26 12:32 UTC23
use super::executor::TaskSet;
31ce2453c82fatagen2026-09-24 13:07 UTC24
use crate::{
17e042fbc331atagen2026-10-08 12:39 UTC25
    ConnectionState, Event, UploadOutcome,
17e042fbc331atagen2026-10-08 12:39 UTC26
    encryption::encryption_state,
17e042fbc331atagen2026-10-08 12:39 UTC27
    error::classify_sdk_error,
17e042fbc331atagen2026-10-08 12:39 UTC28
    normalise::{ENABLED_ROOMS, ROOM_PACKS, USER_PACK},
9bd34e25bb86atagen2026-10-24 07:23 UTC29
    space_order,
f12377bce9b9atagen2026-09-26 12:32 UTC30
    transport::EventSender,
31ce2453c82fatagen2026-09-24 13:07 UTC31
};
31ce2453c82fatagen2026-09-24 13:07 UTC32
31ce2453c82fatagen2026-09-24 13:07 UTC33
const SYNC_RESTART_MIN: Duration = Duration::from_secs(1);
31ce2453c82fatagen2026-09-24 13:07 UTC34
const SYNC_RESTART_MAX: Duration = Duration::from_secs(60);
31ce2453c82fatagen2026-09-24 13:07 UTC35
const SEND_QUEUE_RETRY: Duration = Duration::from_secs(5);
5a52012c9b1datagen2026-09-24 23:45 UTC36
const SYNC_STABLE: Duration = Duration::from_secs(30);
5a52012c9b1datagen2026-09-24 23:45 UTC37
5a52012c9b1datagen2026-09-24 23:45 UTC38
pub(super) async fn room_subscriptions(
5a52012c9b1datagen2026-09-24 23:45 UTC39
    sync: Arc<SyncService>,
5a52012c9b1datagen2026-09-24 23:45 UTC40
    mut rooms: watch::Receiver<Vec<ruma::OwnedRoomId>>,
5a52012c9b1datagen2026-09-24 23:45 UTC41
) {
5a52012c9b1datagen2026-09-24 23:45 UTC42
    while rooms.changed().await.is_ok() {
5a52012c9b1datagen2026-09-24 23:45 UTC43
        let room_ids = rooms.borrow_and_update().clone();
5a52012c9b1datagen2026-09-24 23:45 UTC44
        let room_ids = room_ids.iter().map(AsRef::as_ref).collect::<Vec<_>>();
5a52012c9b1datagen2026-09-24 23:45 UTC45
        sync.room_list_service().subscribe_to_rooms(&room_ids).await;
5a52012c9b1datagen2026-09-24 23:45 UTC46
    }
5a52012c9b1datagen2026-09-24 23:45 UTC47
}
31ce2453c82fatagen2026-09-24 13:07 UTC48
31ce2453c82fatagen2026-09-24 13:07 UTC49
/// `states` must be taken before sync starts, a late subscriber never hears the transition it missed
31ce2453c82fatagen2026-09-24 13:07 UTC50
pub(super) async fn connection(
31ce2453c82fatagen2026-09-24 13:07 UTC51
    mut states: impl futures_util::Stream<Item = SyncState> + Unpin,
31ce2453c82fatagen2026-09-24 13:07 UTC52
    sync: Arc<SyncService>,
31ce2453c82fatagen2026-09-24 13:07 UTC53
    client: Client,
31ce2453c82fatagen2026-09-24 13:07 UTC54
    events: EventSender,
31ce2453c82fatagen2026-09-24 13:07 UTC55
    connection: watch::Sender<ConnectionState>,
31ce2453c82fatagen2026-09-24 13:07 UTC56
) {
31ce2453c82fatagen2026-09-24 13:07 UTC57
    let mut backoff = SYNC_RESTART_MIN;
5a52012c9b1datagen2026-09-24 23:45 UTC58
    let mut online_since: Option<Instant> = None;
31ce2453c82fatagen2026-09-24 13:07 UTC59
    while let Some(state) = states.next().await {
5a52012c9b1datagen2026-09-24 23:45 UTC60
        // only a run that lasted counts as recovered, or a server that answers /versions
5a52012c9b1datagen2026-09-24 23:45 UTC61
        // but refuses sync would have us bounce between offline and online forever
5a52012c9b1datagen2026-09-24 23:45 UTC62
        let stable = online_since
5a52012c9b1datagen2026-09-24 23:45 UTC63
            .take()
5a52012c9b1datagen2026-09-24 23:45 UTC64
            .map(|since| since.elapsed() >= SYNC_STABLE);
5a52012c9b1datagen2026-09-24 23:45 UTC65
        if stable == Some(true) {
5a52012c9b1datagen2026-09-24 23:45 UTC66
            backoff = SYNC_RESTART_MIN;
5a52012c9b1datagen2026-09-24 23:45 UTC67
        }
31ce2453c82fatagen2026-09-24 13:07 UTC68
        let (state, restart) = match state {
5a52012c9b1datagen2026-09-24 23:45 UTC69
            // only ever us stopping it for a restart, nothing a client needs to hear
5a52012c9b1datagen2026-09-24 23:45 UTC70
            SyncState::Idle => continue,
31ce2453c82fatagen2026-09-24 13:07 UTC71
            SyncState::Running => {
5a52012c9b1datagen2026-09-24 23:45 UTC72
                online_since = Some(Instant::now());
31ce2453c82fatagen2026-09-24 13:07 UTC73
                // the sdk parks a room queue on any send error and never unparks it
31ce2453c82fatagen2026-09-24 13:07 UTC74
                client.send_queue().set_enabled(true).await;
31ce2453c82fatagen2026-09-24 13:07 UTC75
                (ConnectionState::Online, false)
31ce2453c82fatagen2026-09-24 13:07 UTC76
            }
5a52012c9b1datagen2026-09-24 23:45 UTC77
            SyncState::Offline => (ConnectionState::Offline, stable == Some(false)),
31ce2453c82fatagen2026-09-24 13:07 UTC78
            SyncState::Terminated => (ConnectionState::Offline, true),
31ce2453c82fatagen2026-09-24 13:07 UTC79
            SyncState::Error(error) => {
31ce2453c82fatagen2026-09-24 13:07 UTC80
                tracing::warn!(%error, "sync service reported an error");
31ce2453c82fatagen2026-09-24 13:07 UTC81
                (ConnectionState::Offline, true)
31ce2453c82fatagen2026-09-24 13:07 UTC82
            }
31ce2453c82fatagen2026-09-24 13:07 UTC83
        };
31ce2453c82fatagen2026-09-24 13:07 UTC84
        if *connection.borrow() != state {
31ce2453c82fatagen2026-09-24 13:07 UTC85
            connection.send_replace(state);
31ce2453c82fatagen2026-09-24 13:07 UTC86
            if events.send(Event::ConnectionChanged(state)).await.is_err() {
31ce2453c82fatagen2026-09-24 13:07 UTC87
                return;
31ce2453c82fatagen2026-09-24 13:07 UTC88
            }
31ce2453c82fatagen2026-09-24 13:07 UTC89
        }
31ce2453c82fatagen2026-09-24 13:07 UTC90
        if restart {
5a52012c9b1datagen2026-09-24 23:45 UTC91
            sync.stop().await;
1fe480c16a3catagen2026-09-26 08:44 UTC92
            matrix_sdk::sleep::sleep(backoff).await;
31ce2453c82fatagen2026-09-24 13:07 UTC93
            backoff = (backoff * 2).min(SYNC_RESTART_MAX);
31ce2453c82fatagen2026-09-24 13:07 UTC94
            sync.start().await;
31ce2453c82fatagen2026-09-24 13:07 UTC95
        }
31ce2453c82fatagen2026-09-24 13:07 UTC96
    }
31ce2453c82fatagen2026-09-24 13:07 UTC97
}
31ce2453c82fatagen2026-09-24 13:07 UTC98
31ce2453c82fatagen2026-09-24 13:07 UTC99
/// a recoverable failure while we're online is a hiccup, so give the room another go
31ce2453c82fatagen2026-09-24 13:07 UTC100
pub(super) async fn send_queue_errors(
31ce2453c82fatagen2026-09-24 13:07 UTC101
    client: Client,
31ce2453c82fatagen2026-09-24 13:07 UTC102
    mut errors: tokio::sync::broadcast::Receiver<matrix_sdk::send_queue::SendQueueRoomError>,
31ce2453c82fatagen2026-09-24 13:07 UTC103
    online: watch::Receiver<ConnectionState>,
31ce2453c82fatagen2026-09-24 13:07 UTC104
) {
1fe480c16a3catagen2026-09-26 08:44 UTC105
    let mut retries = TaskSet::new();
31ce2453c82fatagen2026-09-24 13:07 UTC106
    loop {
31ce2453c82fatagen2026-09-24 13:07 UTC107
        let error = tokio::select! {
31ce2453c82fatagen2026-09-24 13:07 UTC108
            error = errors.recv() => error,
31ce2453c82fatagen2026-09-24 13:07 UTC109
            Some(_) = retries.join_next(), if !retries.is_empty() => continue,
31ce2453c82fatagen2026-09-24 13:07 UTC110
        };
31ce2453c82fatagen2026-09-24 13:07 UTC111
        match error {
31ce2453c82fatagen2026-09-24 13:07 UTC112
            Ok(error) if error.is_recoverable => {
31ce2453c82fatagen2026-09-24 13:07 UTC113
                let client = client.clone();
31ce2453c82fatagen2026-09-24 13:07 UTC114
                let online = online.clone();
31ce2453c82fatagen2026-09-24 13:07 UTC115
                retries.spawn(async move {
1fe480c16a3catagen2026-09-26 08:44 UTC116
                    matrix_sdk::sleep::sleep(SEND_QUEUE_RETRY).await;
31ce2453c82fatagen2026-09-24 13:07 UTC117
                    if *online.borrow() == ConnectionState::Online
31ce2453c82fatagen2026-09-24 13:07 UTC118
                        && let Some(room) = client.get_room(&error.room_id)
31ce2453c82fatagen2026-09-24 13:07 UTC119
                    {
31ce2453c82fatagen2026-09-24 13:07 UTC120
                        room.send_queue().set_enabled(true);
31ce2453c82fatagen2026-09-24 13:07 UTC121
                    }
31ce2453c82fatagen2026-09-24 13:07 UTC122
                });
31ce2453c82fatagen2026-09-24 13:07 UTC123
            }
31ce2453c82fatagen2026-09-24 13:07 UTC124
            Ok(_) | Err(RecvError::Lagged(_)) => {}
31ce2453c82fatagen2026-09-24 13:07 UTC125
            Err(RecvError::Closed) => return,
31ce2453c82fatagen2026-09-24 13:07 UTC126
        }
31ce2453c82fatagen2026-09-24 13:07 UTC127
    }
31ce2453c82fatagen2026-09-24 13:07 UTC128
}
31ce2453c82fatagen2026-09-24 13:07 UTC129
7c26fad34822atagen2026-09-26 09:41 UTC130
/// the one place upload events leave from, so an upload is never reported after it
411bc1c78b65atagen2026-10-22 07:39 UTC131
/// ended. the client-wide channel exists before any upload does. it holds 32 updates and
411bc1c78b65atagen2026-10-22 07:39 UTC132
/// progress ticks share it, so this keeps draining while the event stream is full
0bfa3f46f0beatagen2026-09-26 09:12 UTC133
pub(super) async fn uploads(
5a52012c9b1datagen2026-09-24 23:45 UTC134
    client: Client,
0bfa3f46f0beatagen2026-09-26 09:12 UTC135
    mut updates: tokio::sync::broadcast::Receiver<SendQueueUpdate>,
5a52012c9b1datagen2026-09-24 23:45 UTC136
    events: EventSender,
5a52012c9b1datagen2026-09-24 23:45 UTC137
) {
0bfa3f46f0beatagen2026-09-26 09:12 UTC138
    let (_, mut rooms) = client.rooms_stream();
411bc1c78b65atagen2026-10-22 07:39 UTC139
    let mut tracker = UploadTracker::new(attachment_echoes(&client).await);
411bc1c78b65atagen2026-10-22 07:39 UTC140
    let mut pending = VecDeque::new();
5a52012c9b1datagen2026-09-24 23:45 UTC141
    loop {
411bc1c78b65atagen2026-10-22 07:39 UTC142
        let reports: Vec<Event> = tokio::select! {
411bc1c78b65atagen2026-10-22 07:39 UTC143
            biased;
0bfa3f46f0beatagen2026-09-26 09:12 UTC144
            update = updates.recv() => match update {
7c26fad34822atagen2026-09-26 09:41 UTC145
                Ok(update) => tracker.update(update).into_iter().collect(),
0bfa3f46f0beatagen2026-09-26 09:12 UTC146
                Err(RecvError::Lagged(skipped)) => {
7cf348521204atagen2026-09-26 12:28 UTC147
                    tracing::warn!(skipped, "upload tracker lagged");
7cf348521204atagen2026-09-26 12:28 UTC148
                    tracker.resnapshot(attachment_echoes(&client).await)
0bfa3f46f0beatagen2026-09-26 09:12 UTC149
                }
0bfa3f46f0beatagen2026-09-26 09:12 UTC150
                Err(RecvError::Closed) => return,
0bfa3f46f0beatagen2026-09-26 09:12 UTC151
            },
0bfa3f46f0beatagen2026-09-26 09:12 UTC152
            update = rooms.next() => {
0bfa3f46f0beatagen2026-09-26 09:12 UTC153
                if update.is_none() {
0bfa3f46f0beatagen2026-09-26 09:12 UTC154
                    return;
0bfa3f46f0beatagen2026-09-26 09:12 UTC155
                }
7c26fad34822atagen2026-09-26 09:41 UTC156
                tracker.finish_where(
7c26fad34822atagen2026-09-26 09:41 UTC157
                    |(room_id, _)| client.get_room(room_id).is_none(),
7c26fad34822atagen2026-09-26 09:41 UTC158
                    &UploadOutcome::RoomForgotten,
7c26fad34822atagen2026-09-26 09:41 UTC159
                )
31ce2453c82fatagen2026-09-24 13:07 UTC160
            }
411bc1c78b65atagen2026-10-22 07:39 UTC161
            permit = events.reserve(), if !pending.is_empty() => {
411bc1c78b65atagen2026-10-22 07:39 UTC162
                let Ok(permit) = permit else { return };
411bc1c78b65atagen2026-10-22 07:39 UTC163
                permit.send(pending.pop_front().expect("pending was checked"));
411bc1c78b65atagen2026-10-22 07:39 UTC164
                continue;
411bc1c78b65atagen2026-10-22 07:39 UTC165
            }
31ce2453c82fatagen2026-09-24 13:07 UTC166
        };
7c26fad34822atagen2026-09-26 09:41 UTC167
        for event in reports {
411bc1c78b65atagen2026-10-22 07:39 UTC168
            queue_upload_event(&mut pending, event);
411bc1c78b65atagen2026-10-22 07:39 UTC169
        }
411bc1c78b65atagen2026-10-22 07:39 UTC170
    }
411bc1c78b65atagen2026-10-22 07:39 UTC171
}
411bc1c78b65atagen2026-10-22 07:39 UTC172
411bc1c78b65atagen2026-10-22 07:39 UTC173
/// a progress tick replaces a waiting one for the same upload, unless something else about
411bc1c78b65atagen2026-10-22 07:39 UTC174
/// that upload is queued after it
411bc1c78b65atagen2026-10-22 07:39 UTC175
fn queue_upload_event(pending: &mut VecDeque<Event>, event: Event) {
411bc1c78b65atagen2026-10-22 07:39 UTC176
    if matches!(event, Event::MediaUploadProgress { .. })
411bc1c78b65atagen2026-10-22 07:39 UTC177
        && let Some(waiting) = pending
411bc1c78b65atagen2026-10-22 07:39 UTC178
            .iter_mut()
411bc1c78b65atagen2026-10-22 07:39 UTC179
            .rev()
411bc1c78b65atagen2026-10-22 07:39 UTC180
            .find(|waiting| upload_of(waiting) == upload_of(&event))
411bc1c78b65atagen2026-10-22 07:39 UTC181
        && matches!(waiting, Event::MediaUploadProgress { .. })
411bc1c78b65atagen2026-10-22 07:39 UTC182
    {
411bc1c78b65atagen2026-10-22 07:39 UTC183
        *waiting = event;
411bc1c78b65atagen2026-10-22 07:39 UTC184
        return;
411bc1c78b65atagen2026-10-22 07:39 UTC185
    }
411bc1c78b65atagen2026-10-22 07:39 UTC186
    pending.push_back(event);
411bc1c78b65atagen2026-10-22 07:39 UTC187
}
411bc1c78b65atagen2026-10-22 07:39 UTC188
411bc1c78b65atagen2026-10-22 07:39 UTC189
fn upload_of(event: &Event) -> Option<(&OwnedRoomId, &OwnedTransactionId)> {
411bc1c78b65atagen2026-10-22 07:39 UTC190
    match event {
411bc1c78b65atagen2026-10-22 07:39 UTC191
        Event::MediaUploadProgress {
411bc1c78b65atagen2026-10-22 07:39 UTC192
            room_id,
411bc1c78b65atagen2026-10-22 07:39 UTC193
            transaction_id,
411bc1c78b65atagen2026-10-22 07:39 UTC194
            ..
411bc1c78b65atagen2026-10-22 07:39 UTC195
        }
411bc1c78b65atagen2026-10-22 07:39 UTC196
        | Event::MediaUploadFailed {
411bc1c78b65atagen2026-10-22 07:39 UTC197
            room_id,
411bc1c78b65atagen2026-10-22 07:39 UTC198
            transaction_id,
411bc1c78b65atagen2026-10-22 07:39 UTC199
            ..
0bfa3f46f0beatagen2026-09-26 09:12 UTC200
        }
411bc1c78b65atagen2026-10-22 07:39 UTC201
        | Event::MediaUploadFinished {
411bc1c78b65atagen2026-10-22 07:39 UTC202
            room_id,
411bc1c78b65atagen2026-10-22 07:39 UTC203
            transaction_id,
411bc1c78b65atagen2026-10-22 07:39 UTC204
            ..
411bc1c78b65atagen2026-10-22 07:39 UTC205
        } => Some((room_id, transaction_id)),
411bc1c78b65atagen2026-10-22 07:39 UTC206
        _ => None,
0bfa3f46f0beatagen2026-09-26 09:12 UTC207
    }
0bfa3f46f0beatagen2026-09-26 09:12 UTC208
}
0bfa3f46f0beatagen2026-09-26 09:12 UTC209
0bfa3f46f0beatagen2026-09-26 09:12 UTC210
type Upload = (OwnedRoomId, OwnedTransactionId);
0bfa3f46f0beatagen2026-09-26 09:12 UTC211
411bc1c78b65atagen2026-10-22 07:39 UTC212
/// the sdk sends progress ticks from a detached task, so a tick can land after its
411bc1c78b65atagen2026-10-22 07:39 UTC213
/// upload's end. it trails by one task hop, so a short memory of ends covers it
411bc1c78b65atagen2026-10-22 07:39 UTC214
const REMEMBERED_ENDS: usize = 256;
411bc1c78b65atagen2026-10-22 07:39 UTC215
411bc1c78b65atagen2026-10-22 07:39 UTC216
/// everything reported gets exactly one `MediaUploadFinished` and nothing after it
7c26fad34822atagen2026-09-26 09:41 UTC217
struct UploadTracker {
7c26fad34822atagen2026-09-26 09:41 UTC218
    live: HashSet<Upload>,
7c26fad34822atagen2026-09-26 09:41 UTC219
    ended: HashSet<Upload>,
411bc1c78b65atagen2026-10-22 07:39 UTC220
    ended_order: VecDeque<Upload>,
0bfa3f46f0beatagen2026-09-26 09:12 UTC221
}
0bfa3f46f0beatagen2026-09-26 09:12 UTC222
7c26fad34822atagen2026-09-26 09:41 UTC223
impl UploadTracker {
411bc1c78b65atagen2026-10-22 07:39 UTC224
    fn new(live: HashSet<Upload>) -> Self {
411bc1c78b65atagen2026-10-22 07:39 UTC225
        Self {
411bc1c78b65atagen2026-10-22 07:39 UTC226
            live,
411bc1c78b65atagen2026-10-22 07:39 UTC227
            ended: HashSet::new(),
411bc1c78b65atagen2026-10-22 07:39 UTC228
            ended_order: VecDeque::new(),
411bc1c78b65atagen2026-10-22 07:39 UTC229
        }
411bc1c78b65atagen2026-10-22 07:39 UTC230
    }
411bc1c78b65atagen2026-10-22 07:39 UTC231
7c26fad34822atagen2026-09-26 09:41 UTC232
    fn update(&mut self, update: SendQueueUpdate) -> Option<Event> {
7c26fad34822atagen2026-09-26 09:41 UTC233
        let room_id = update.room_id;
7c26fad34822atagen2026-09-26 09:41 UTC234
        let (transaction_id, outcome) = match update.update {
7c26fad34822atagen2026-09-26 09:41 UTC235
            RoomSendQueueUpdate::NewLocalEvent(echo) => {
7c26fad34822atagen2026-09-26 09:41 UTC236
                let upload = (room_id, echo.transaction_id);
7c26fad34822atagen2026-09-26 09:41 UTC237
                if is_attachment_echo(&echo.content) && !self.ended.contains(&upload) {
7c26fad34822atagen2026-09-26 09:41 UTC238
                    self.live.insert(upload);
7c26fad34822atagen2026-09-26 09:41 UTC239
                }
0bfa3f46f0beatagen2026-09-26 09:12 UTC240
                return None;
0bfa3f46f0beatagen2026-09-26 09:12 UTC241
            }
411bc1c78b65atagen2026-10-22 07:39 UTC242
            RoomSendQueueUpdate::MediaUpload {
411bc1c78b65atagen2026-10-22 07:39 UTC243
                related_to,
411bc1c78b65atagen2026-10-22 07:39 UTC244
                file,
411bc1c78b65atagen2026-10-22 07:39 UTC245
                progress,
411bc1c78b65atagen2026-10-22 07:39 UTC246
                ..
411bc1c78b65atagen2026-10-22 07:39 UTC247
            } => {
411bc1c78b65atagen2026-10-22 07:39 UTC248
                let upload = (room_id, related_to);
411bc1c78b65atagen2026-10-22 07:39 UTC249
                if self.ended.contains(&upload) {
411bc1c78b65atagen2026-10-22 07:39 UTC250
                    return None;
411bc1c78b65atagen2026-10-22 07:39 UTC251
                }
411bc1c78b65atagen2026-10-22 07:39 UTC252
                self.live.insert(upload.clone());
411bc1c78b65atagen2026-10-22 07:39 UTC253
                let (room_id, transaction_id) = upload;
411bc1c78b65atagen2026-10-22 07:39 UTC254
                return Some(Event::MediaUploadProgress {
411bc1c78b65atagen2026-10-22 07:39 UTC255
                    room_id,
411bc1c78b65atagen2026-10-22 07:39 UTC256
                    transaction_id,
411bc1c78b65atagen2026-10-22 07:39 UTC257
                    uploaded_bytes: progress.current as u64,
411bc1c78b65atagen2026-10-22 07:39 UTC258
                    total_bytes: progress.total as u64,
411bc1c78b65atagen2026-10-22 07:39 UTC259
                    complete: file.is_some() && progress.current == progress.total,
411bc1c78b65atagen2026-10-22 07:39 UTC260
                });
411bc1c78b65atagen2026-10-22 07:39 UTC261
            }
7c26fad34822atagen2026-09-26 09:41 UTC262
            RoomSendQueueUpdate::SendError {
0bfa3f46f0beatagen2026-09-26 09:12 UTC263
                transaction_id,
7c26fad34822atagen2026-09-26 09:41 UTC264
                error,
7c26fad34822atagen2026-09-26 09:41 UTC265
                is_recoverable,
7c26fad34822atagen2026-09-26 09:41 UTC266
            } => {
7c26fad34822atagen2026-09-26 09:41 UTC267
                let upload = (room_id, transaction_id);
7c26fad34822atagen2026-09-26 09:41 UTC268
                if !self.live.contains(&upload) {
7c26fad34822atagen2026-09-26 09:41 UTC269
                    return None;
7c26fad34822atagen2026-09-26 09:41 UTC270
                }
7c26fad34822atagen2026-09-26 09:41 UTC271
                let (room_id, transaction_id) = upload;
7c26fad34822atagen2026-09-26 09:41 UTC272
                return Some(Event::MediaUploadFailed {
7c26fad34822atagen2026-09-26 09:41 UTC273
                    room_id,
7c26fad34822atagen2026-09-26 09:41 UTC274
                    transaction_id,
7c26fad34822atagen2026-09-26 09:41 UTC275
                    kind: classify_sdk_error(&error),
7c26fad34822atagen2026-09-26 09:41 UTC276
                    will_retry: is_recoverable,
7c26fad34822atagen2026-09-26 09:41 UTC277
                });
7c26fad34822atagen2026-09-26 09:41 UTC278
            }
7c26fad34822atagen2026-09-26 09:41 UTC279
            RoomSendQueueUpdate::SentEvent {
7c26fad34822atagen2026-09-26 09:41 UTC280
                transaction_id,
7c26fad34822atagen2026-09-26 09:41 UTC281
                event_id,
7c26fad34822atagen2026-09-26 09:41 UTC282
            } => (transaction_id, UploadOutcome::Sent { event_id }),
7c26fad34822atagen2026-09-26 09:41 UTC283
            RoomSendQueueUpdate::CancelledLocalEvent { transaction_id } => {
7c26fad34822atagen2026-09-26 09:41 UTC284
                (transaction_id, UploadOutcome::Cancelled)
7c26fad34822atagen2026-09-26 09:41 UTC285
            }
7c26fad34822atagen2026-09-26 09:41 UTC286
            _ => return None,
7c26fad34822atagen2026-09-26 09:41 UTC287
        };
411bc1c78b65atagen2026-10-22 07:39 UTC288
        self.finish((room_id, transaction_id), outcome)
7c26fad34822atagen2026-09-26 09:41 UTC289
    }
7c26fad34822atagen2026-09-26 09:41 UTC290
7c26fad34822atagen2026-09-26 09:41 UTC291
    fn resnapshot(&mut self, current: HashSet<Upload>) -> Vec<Event> {
7c26fad34822atagen2026-09-26 09:41 UTC292
        let reports = self.finish_where(|upload| !current.contains(upload), &UploadOutcome::Missed);
7c26fad34822atagen2026-09-26 09:41 UTC293
        let ended = &self.ended;
7c26fad34822atagen2026-09-26 09:41 UTC294
        self.live
7c26fad34822atagen2026-09-26 09:41 UTC295
            .extend(current.into_iter().filter(|upload| !ended.contains(upload)));
7c26fad34822atagen2026-09-26 09:41 UTC296
        reports
7c26fad34822atagen2026-09-26 09:41 UTC297
    }
7c26fad34822atagen2026-09-26 09:41 UTC298
7c26fad34822atagen2026-09-26 09:41 UTC299
    fn finish_where(
7c26fad34822atagen2026-09-26 09:41 UTC300
        &mut self,
7c26fad34822atagen2026-09-26 09:41 UTC301
        ends: impl Fn(&Upload) -> bool,
7c26fad34822atagen2026-09-26 09:41 UTC302
        outcome: &UploadOutcome,
7c26fad34822atagen2026-09-26 09:41 UTC303
    ) -> Vec<Event> {
7c26fad34822atagen2026-09-26 09:41 UTC304
        let ending = self
7c26fad34822atagen2026-09-26 09:41 UTC305
            .live
7c26fad34822atagen2026-09-26 09:41 UTC306
            .iter()
7c26fad34822atagen2026-09-26 09:41 UTC307
            .filter(|upload| ends(upload))
7c26fad34822atagen2026-09-26 09:41 UTC308
            .cloned()
7c26fad34822atagen2026-09-26 09:41 UTC309
            .collect::<Vec<_>>();
7c26fad34822atagen2026-09-26 09:41 UTC310
        ending
7c26fad34822atagen2026-09-26 09:41 UTC311
            .into_iter()
411bc1c78b65atagen2026-10-22 07:39 UTC312
            .filter_map(|upload| self.finish(upload, outcome.clone()))
7c26fad34822atagen2026-09-26 09:41 UTC313
            .collect()
7c26fad34822atagen2026-09-26 09:41 UTC314
    }
7c26fad34822atagen2026-09-26 09:41 UTC315
411bc1c78b65atagen2026-10-22 07:39 UTC316
    /// an end for an upload we never saw is remembered too, its ticks may still be coming
411bc1c78b65atagen2026-10-22 07:39 UTC317
    fn finish(&mut self, upload: Upload, outcome: UploadOutcome) -> Option<Event> {
411bc1c78b65atagen2026-10-22 07:39 UTC318
        if self.ended.insert(upload.clone()) {
411bc1c78b65atagen2026-10-22 07:39 UTC319
            self.ended_order.push_back(upload.clone());
411bc1c78b65atagen2026-10-22 07:39 UTC320
            if self.ended_order.len() > REMEMBERED_ENDS
411bc1c78b65atagen2026-10-22 07:39 UTC321
                && let Some(oldest) = self.ended_order.pop_front()
411bc1c78b65atagen2026-10-22 07:39 UTC322
            {
411bc1c78b65atagen2026-10-22 07:39 UTC323
                self.ended.remove(&oldest);
411bc1c78b65atagen2026-10-22 07:39 UTC324
            }
411bc1c78b65atagen2026-10-22 07:39 UTC325
        }
7c26fad34822atagen2026-09-26 09:41 UTC326
        if !self.live.remove(&upload) {
7c26fad34822atagen2026-09-26 09:41 UTC327
            return None;
31ce2453c82fatagen2026-09-24 13:07 UTC328
        }
7c26fad34822atagen2026-09-26 09:41 UTC329
        let (room_id, transaction_id) = upload;
7c26fad34822atagen2026-09-26 09:41 UTC330
        Some(Event::MediaUploadFinished {
7c26fad34822atagen2026-09-26 09:41 UTC331
            room_id,
7c26fad34822atagen2026-09-26 09:41 UTC332
            transaction_id,
7c26fad34822atagen2026-09-26 09:41 UTC333
            outcome,
7c26fad34822atagen2026-09-26 09:41 UTC334
        })
31ce2453c82fatagen2026-09-24 13:07 UTC335
    }
0bfa3f46f0beatagen2026-09-26 09:12 UTC336
}
0bfa3f46f0beatagen2026-09-26 09:12 UTC337
7c26fad34822atagen2026-09-26 09:41 UTC338
async fn attachment_echoes(client: &Client) -> HashSet<Upload> {
7c26fad34822atagen2026-09-26 09:41 UTC339
    let Ok(echoes) = client.send_queue().local_echoes().await else {
7c26fad34822atagen2026-09-26 09:41 UTC340
        return HashSet::new();
7c26fad34822atagen2026-09-26 09:41 UTC341
    };
7c26fad34822atagen2026-09-26 09:41 UTC342
    echoes
0bfa3f46f0beatagen2026-09-26 09:12 UTC343
        .into_iter()
7c26fad34822atagen2026-09-26 09:41 UTC344
        .flat_map(|(room_id, echoes)| {
7c26fad34822atagen2026-09-26 09:41 UTC345
            echoes
7c26fad34822atagen2026-09-26 09:41 UTC346
                .into_iter()
7c26fad34822atagen2026-09-26 09:41 UTC347
                .filter(|echo| is_attachment_echo(&echo.content))
7c26fad34822atagen2026-09-26 09:41 UTC348
                .map(move |echo| (room_id.clone(), echo.transaction_id))
0bfa3f46f0beatagen2026-09-26 09:12 UTC349
        })
0bfa3f46f0beatagen2026-09-26 09:12 UTC350
        .collect()
31ce2453c82fatagen2026-09-24 13:07 UTC351
}
31ce2453c82fatagen2026-09-24 13:07 UTC352
31ce2453c82fatagen2026-09-24 13:07 UTC353
fn is_attachment_echo(content: &LocalEchoContent) -> bool {
31ce2453c82fatagen2026-09-24 13:07 UTC354
    let LocalEchoContent::Event {
31ce2453c82fatagen2026-09-24 13:07 UTC355
        serialized_event, ..
31ce2453c82fatagen2026-09-24 13:07 UTC356
    } = content
31ce2453c82fatagen2026-09-24 13:07 UTC357
    else {
31ce2453c82fatagen2026-09-24 13:07 UTC358
        return false;
31ce2453c82fatagen2026-09-24 13:07 UTC359
    };
31ce2453c82fatagen2026-09-24 13:07 UTC360
    matches!(
31ce2453c82fatagen2026-09-24 13:07 UTC361
        serialized_event.deserialize(),
31ce2453c82fatagen2026-09-24 13:07 UTC362
        Ok(AnyMessageLikeEventContent::RoomMessage(content))
31ce2453c82fatagen2026-09-24 13:07 UTC363
            if matches!(
31ce2453c82fatagen2026-09-24 13:07 UTC364
                content.msgtype,
31ce2453c82fatagen2026-09-24 13:07 UTC365
                MessageType::Audio(_) | MessageType::File(_) | MessageType::Image(_) | MessageType::Video(_)
31ce2453c82fatagen2026-09-24 13:07 UTC366
            )
31ce2453c82fatagen2026-09-24 13:07 UTC367
    )
31ce2453c82fatagen2026-09-24 13:07 UTC368
}
31ce2453c82fatagen2026-09-24 13:07 UTC369
31ce2453c82fatagen2026-09-24 13:07 UTC370
pub(super) async fn encryption(client: Client, events: EventSender) {
31ce2453c82fatagen2026-09-24 13:07 UTC371
    let Some(device_id) = client.device_id().map(ToOwned::to_owned) else {
31ce2453c82fatagen2026-09-24 13:07 UTC372
        tracing::warn!("encryption watcher stopped because the client has no device id");
31ce2453c82fatagen2026-09-24 13:07 UTC373
        return;
31ce2453c82fatagen2026-09-24 13:07 UTC374
    };
31ce2453c82fatagen2026-09-24 13:07 UTC375
    let recovery = client.encryption().recovery();
31ce2453c82fatagen2026-09-24 13:07 UTC376
    let backups = client.encryption().backups();
31ce2453c82fatagen2026-09-24 13:07 UTC377
    let mut recovery_stream = recovery.state_stream();
31ce2453c82fatagen2026-09-24 13:07 UTC378
    let mut backup_stream = backups.state_stream();
31ce2453c82fatagen2026-09-24 13:07 UTC379
    let mut verification_stream = client.encryption().verification_state();
31ce2453c82fatagen2026-09-24 13:07 UTC380
    let mut recovery_state = recovery.state();
31ce2453c82fatagen2026-09-24 13:07 UTC381
    let mut backup_state = backups.state();
31ce2453c82fatagen2026-09-24 13:07 UTC382
    let mut verification_state = verification_stream.get();
31ce2453c82fatagen2026-09-24 13:07 UTC383
    loop {
31ce2453c82fatagen2026-09-24 13:07 UTC384
        let state = encryption_state(
31ce2453c82fatagen2026-09-24 13:07 UTC385
            device_id.clone(),
31ce2453c82fatagen2026-09-24 13:07 UTC386
            recovery_state,
31ce2453c82fatagen2026-09-24 13:07 UTC387
            verification_state,
31ce2453c82fatagen2026-09-24 13:07 UTC388
            backup_state,
31ce2453c82fatagen2026-09-24 13:07 UTC389
        );
31ce2453c82fatagen2026-09-24 13:07 UTC390
        if events.send(Event::EncryptionChanged(state)).await.is_err() {
31ce2453c82fatagen2026-09-24 13:07 UTC391
            return;
31ce2453c82fatagen2026-09-24 13:07 UTC392
        }
31ce2453c82fatagen2026-09-24 13:07 UTC393
        tokio::select! {
31ce2453c82fatagen2026-09-24 13:07 UTC394
            Some(update) = recovery_stream.next() => recovery_state = update,
31ce2453c82fatagen2026-09-24 13:07 UTC395
            Some(update) = verification_stream.next() => verification_state = update,
31ce2453c82fatagen2026-09-24 13:07 UTC396
            Some(update) = backup_stream.next() => {
31ce2453c82fatagen2026-09-24 13:07 UTC397
                backup_state = update.unwrap_or_else(|_| backups.state());
31ce2453c82fatagen2026-09-24 13:07 UTC398
            }
31ce2453c82fatagen2026-09-24 13:07 UTC399
            else => return,
31ce2453c82fatagen2026-09-24 13:07 UTC400
        }
31ce2453c82fatagen2026-09-24 13:07 UTC401
    }
31ce2453c82fatagen2026-09-24 13:07 UTC402
}
31ce2453c82fatagen2026-09-24 13:07 UTC403
fa5b4e09943aatagen2026-10-24 09:33 UTC404
/// space orders, pins and power levels all feed summaries without raising a notable room info update
31ce2453c82fatagen2026-09-24 13:07 UTC405
pub(super) async fn rooms(client: Client, events: EventSender) {
31ce2453c82fatagen2026-09-24 13:07 UTC406
    let (_, mut rooms) = client.rooms_stream();
31ce2453c82fatagen2026-09-24 13:07 UTC407
    let mut info = client.room_info_notable_update_receiver();
fa5b4e09943aatagen2026-10-24 09:33 UTC408
    let changed = Arc::new(Notify::new());
fa5b4e09943aatagen2026-10-24 09:33 UTC409
    let handlers = [
fa5b4e09943aatagen2026-10-24 09:33 UTC410
        client.add_event_handler({
fa5b4e09943aatagen2026-10-24 09:33 UTC411
            let changed = changed.clone();
fa5b4e09943aatagen2026-10-24 09:33 UTC412
            move |event: Raw<AnyRoomAccountDataEvent>| {
fa5b4e09943aatagen2026-10-24 09:33 UTC413
                let changed = changed.clone();
fa5b4e09943aatagen2026-10-24 09:33 UTC414
                async move {
fa5b4e09943aatagen2026-10-24 09:33 UTC415
                    if event_type(&event).as_deref() == Some(space_order::EVENT_TYPE) {
fa5b4e09943aatagen2026-10-24 09:33 UTC416
                        changed.notify_one();
fa5b4e09943aatagen2026-10-24 09:33 UTC417
                    }
9bd34e25bb86atagen2026-10-24 07:23 UTC418
                }
9bd34e25bb86atagen2026-10-24 07:23 UTC419
            }
fa5b4e09943aatagen2026-10-24 09:33 UTC420
        }),
fa5b4e09943aatagen2026-10-24 09:33 UTC421
        client.add_event_handler({
fa5b4e09943aatagen2026-10-24 09:33 UTC422
            let changed = changed.clone();
fa5b4e09943aatagen2026-10-24 09:33 UTC423
            move |event: Raw<AnySyncStateEvent>| {
fa5b4e09943aatagen2026-10-24 09:33 UTC424
                let changed = changed.clone();
fa5b4e09943aatagen2026-10-24 09:33 UTC425
                async move {
fa5b4e09943aatagen2026-10-24 09:33 UTC426
                    if matches!(
fa5b4e09943aatagen2026-10-24 09:33 UTC427
                        event_type(&event).as_deref(),
fa5b4e09943aatagen2026-10-24 09:33 UTC428
                        Some("m.room.pinned_events" | "m.room.power_levels")
fa5b4e09943aatagen2026-10-24 09:33 UTC429
                    ) {
fa5b4e09943aatagen2026-10-24 09:33 UTC430
                        changed.notify_one();
fa5b4e09943aatagen2026-10-24 09:33 UTC431
                    }
fa5b4e09943aatagen2026-10-24 09:33 UTC432
                }
fa5b4e09943aatagen2026-10-24 09:33 UTC433
            }
fa5b4e09943aatagen2026-10-24 09:33 UTC434
        }),
fa5b4e09943aatagen2026-10-24 09:33 UTC435
    ];
31ce2453c82fatagen2026-09-24 13:07 UTC436
    loop {
31ce2453c82fatagen2026-09-24 13:07 UTC437
        if events.send(Event::RoomsChanged).await.is_err() {
9bd34e25bb86atagen2026-10-24 07:23 UTC438
            break;
31ce2453c82fatagen2026-09-24 13:07 UTC439
        }
31ce2453c82fatagen2026-09-24 13:07 UTC440
        tokio::select! {
9bd34e25bb86atagen2026-10-24 07:23 UTC441
            update = rooms.next() => if update.is_none() { break },
9bd34e25bb86atagen2026-10-24 07:23 UTC442
            update = info.recv() => if matches!(update, Err(RecvError::Closed)) { break },
fa5b4e09943aatagen2026-10-24 09:33 UTC443
            () = changed.notified() => {}
31ce2453c82fatagen2026-09-24 13:07 UTC444
        }
31ce2453c82fatagen2026-09-24 13:07 UTC445
        // a sync touching fifty rooms is one invalidation, not fifty
31ce2453c82fatagen2026-09-24 13:07 UTC446
        while let Ok(_) | Err(tokio::sync::broadcast::error::TryRecvError::Lagged(_)) =
31ce2453c82fatagen2026-09-24 13:07 UTC447
            info.try_recv()
31ce2453c82fatagen2026-09-24 13:07 UTC448
        {}
31ce2453c82fatagen2026-09-24 13:07 UTC449
    }
fa5b4e09943aatagen2026-10-24 09:33 UTC450
    for handler in handlers {
fa5b4e09943aatagen2026-10-24 09:33 UTC451
        client.remove_event_handler(handler);
fa5b4e09943aatagen2026-10-24 09:33 UTC452
    }
31ce2453c82fatagen2026-09-24 13:07 UTC453
}
31ce2453c82fatagen2026-09-24 13:07 UTC454
17e042fbc331atagen2026-10-08 12:39 UTC455
/// packs live in room state and account data, and leaving a room takes its packs with it
17e042fbc331atagen2026-10-08 12:39 UTC456
pub(super) async fn image_packs(client: Client, events: EventSender) {
17e042fbc331atagen2026-10-08 12:39 UTC457
    let changed = Arc::new(Notify::new());
17e042fbc331atagen2026-10-08 12:39 UTC458
    let handlers = [
17e042fbc331atagen2026-10-08 12:39 UTC459
        client.add_event_handler({
17e042fbc331atagen2026-10-08 12:39 UTC460
            let changed = changed.clone();
17e042fbc331atagen2026-10-08 12:39 UTC461
            move |event: Raw<AnySyncStateEvent>| {
17e042fbc331atagen2026-10-08 12:39 UTC462
                let changed = changed.clone();
17e042fbc331atagen2026-10-08 12:39 UTC463
                async move {
17e042fbc331atagen2026-10-08 12:39 UTC464
                    if event_type(&event).as_deref() == Some(ROOM_PACKS) {
17e042fbc331atagen2026-10-08 12:39 UTC465
                        changed.notify_one();
17e042fbc331atagen2026-10-08 12:39 UTC466
                    }
17e042fbc331atagen2026-10-08 12:39 UTC467
                }
17e042fbc331atagen2026-10-08 12:39 UTC468
            }
17e042fbc331atagen2026-10-08 12:39 UTC469
        }),
17e042fbc331atagen2026-10-08 12:39 UTC470
        client.add_event_handler({
17e042fbc331atagen2026-10-08 12:39 UTC471
            let changed = changed.clone();
17e042fbc331atagen2026-10-08 12:39 UTC472
            move |event: Raw<AnyGlobalAccountDataEvent>| {
17e042fbc331atagen2026-10-08 12:39 UTC473
                let changed = changed.clone();
17e042fbc331atagen2026-10-08 12:39 UTC474
                async move {
17e042fbc331atagen2026-10-08 12:39 UTC475
                    if matches!(
17e042fbc331atagen2026-10-08 12:39 UTC476
                        event_type(&event).as_deref(),
17e042fbc331atagen2026-10-08 12:39 UTC477
                        Some(USER_PACK | ENABLED_ROOMS)
17e042fbc331atagen2026-10-08 12:39 UTC478
                    ) {
17e042fbc331atagen2026-10-08 12:39 UTC479
                        changed.notify_one();
17e042fbc331atagen2026-10-08 12:39 UTC480
                    }
17e042fbc331atagen2026-10-08 12:39 UTC481
                }
17e042fbc331atagen2026-10-08 12:39 UTC482
            }
17e042fbc331atagen2026-10-08 12:39 UTC483
        }),
17e042fbc331atagen2026-10-08 12:39 UTC484
    ];
17e042fbc331atagen2026-10-08 12:39 UTC485
    let mut info = client.room_info_notable_update_receiver();
17e042fbc331atagen2026-10-08 12:39 UTC486
    loop {
17e042fbc331atagen2026-10-08 12:39 UTC487
        tokio::select! {
17e042fbc331atagen2026-10-08 12:39 UTC488
            () = changed.notified() => {}
17e042fbc331atagen2026-10-08 12:39 UTC489
            update = info.recv() => match update {
17e042fbc331atagen2026-10-08 12:39 UTC490
                Ok(update) if !update.reasons.contains(RoomInfoNotableUpdateReasons::MEMBERSHIP) => {
17e042fbc331atagen2026-10-08 12:39 UTC491
                    continue;
17e042fbc331atagen2026-10-08 12:39 UTC492
                }
17e042fbc331atagen2026-10-08 12:39 UTC493
                Ok(_) | Err(RecvError::Lagged(_)) => {}
17e042fbc331atagen2026-10-08 12:39 UTC494
                Err(RecvError::Closed) => break,
17e042fbc331atagen2026-10-08 12:39 UTC495
            },
17e042fbc331atagen2026-10-08 12:39 UTC496
        }
17e042fbc331atagen2026-10-08 12:39 UTC497
        if events.send(Event::ImagePacksChanged).await.is_err() {
17e042fbc331atagen2026-10-08 12:39 UTC498
            break;
17e042fbc331atagen2026-10-08 12:39 UTC499
        }
17e042fbc331atagen2026-10-08 12:39 UTC500
    }
17e042fbc331atagen2026-10-08 12:39 UTC501
    for handler in handlers {
17e042fbc331atagen2026-10-08 12:39 UTC502
        client.remove_event_handler(handler);
17e042fbc331atagen2026-10-08 12:39 UTC503
    }
17e042fbc331atagen2026-10-08 12:39 UTC504
}
17e042fbc331atagen2026-10-08 12:39 UTC505
17e042fbc331atagen2026-10-08 12:39 UTC506
fn event_type<T>(event: &Raw<T>) -> Option<String> {
17e042fbc331atagen2026-10-08 12:39 UTC507
    event.get_field("type").ok().flatten()
17e042fbc331atagen2026-10-08 12:39 UTC508
}
17e042fbc331atagen2026-10-08 12:39 UTC509
31ce2453c82fatagen2026-09-24 13:07 UTC510
pub(super) async fn notification_settings(client: Client, events: EventSender) {
31ce2453c82fatagen2026-09-24 13:07 UTC511
    let settings = client.notification_settings().await;
31ce2453c82fatagen2026-09-24 13:07 UTC512
    let mut changes = settings.subscribe_to_changes();
31ce2453c82fatagen2026-09-24 13:07 UTC513
    while let Ok(()) | Err(RecvError::Lagged(_)) = changes.recv().await {
0bfa3f46f0beatagen2026-09-26 09:12 UTC514
        // room summaries carry the effective mode, and push rule changes never touch room info
0bfa3f46f0beatagen2026-09-26 09:12 UTC515
        for event in [Event::NotificationSettingsChanged, Event::RoomsChanged] {
0bfa3f46f0beatagen2026-09-26 09:12 UTC516
            if events.send(event).await.is_err() {
0bfa3f46f0beatagen2026-09-26 09:12 UTC517
                return;
0bfa3f46f0beatagen2026-09-26 09:12 UTC518
            }
31ce2453c82fatagen2026-09-24 13:07 UTC519
        }
31ce2453c82fatagen2026-09-24 13:07 UTC520
    }
31ce2453c82fatagen2026-09-24 13:07 UTC521
}
7c26fad34822atagen2026-09-26 09:41 UTC522
7c26fad34822atagen2026-09-26 09:41 UTC523
#[cfg(test)]
7c26fad34822atagen2026-09-26 09:41 UTC524
mod tests {
411bc1c78b65atagen2026-10-22 07:39 UTC525
    use matrix_sdk::send_queue::{AbstractProgress, RoomSendQueueUpdate, SendQueueUpdate};
7c26fad34822atagen2026-09-26 09:41 UTC526
    use ruma::{OwnedTransactionId, event_id, owned_room_id};
7c26fad34822atagen2026-09-26 09:41 UTC527
411bc1c78b65atagen2026-10-22 07:39 UTC528
    use super::{HashSet, Upload, UploadTracker};
7c26fad34822atagen2026-09-26 09:41 UTC529
    use crate::{Event, UploadOutcome};
7c26fad34822atagen2026-09-26 09:41 UTC530
7cf348521204atagen2026-09-26 12:28 UTC531
    fn upload() -> Upload {
7cf348521204atagen2026-09-26 12:28 UTC532
        (
7c26fad34822atagen2026-09-26 09:41 UTC533
            owned_room_id!("!room:localhost"),
7c26fad34822atagen2026-09-26 09:41 UTC534
            OwnedTransactionId::from("upload"),
7cf348521204atagen2026-09-26 12:28 UTC535
        )
7cf348521204atagen2026-09-26 12:28 UTC536
    }
7cf348521204atagen2026-09-26 12:28 UTC537
411bc1c78b65atagen2026-10-22 07:39 UTC538
    fn update(update: RoomSendQueueUpdate) -> SendQueueUpdate {
411bc1c78b65atagen2026-10-22 07:39 UTC539
        SendQueueUpdate {
411bc1c78b65atagen2026-10-22 07:39 UTC540
            room_id: upload().0,
411bc1c78b65atagen2026-10-22 07:39 UTC541
            update,
7cf348521204atagen2026-09-26 12:28 UTC542
        }
7cf348521204atagen2026-09-26 12:28 UTC543
    }
7cf348521204atagen2026-09-26 12:28 UTC544
411bc1c78b65atagen2026-10-22 07:39 UTC545
    fn tick() -> SendQueueUpdate {
411bc1c78b65atagen2026-10-22 07:39 UTC546
        update(RoomSendQueueUpdate::MediaUpload {
411bc1c78b65atagen2026-10-22 07:39 UTC547
            related_to: upload().1,
411bc1c78b65atagen2026-10-22 07:39 UTC548
            file: None,
411bc1c78b65atagen2026-10-22 07:39 UTC549
            index: 0,
411bc1c78b65atagen2026-10-22 07:39 UTC550
            progress: AbstractProgress {
411bc1c78b65atagen2026-10-22 07:39 UTC551
                current: 1,
411bc1c78b65atagen2026-10-22 07:39 UTC552
                total: 2,
411bc1c78b65atagen2026-10-22 07:39 UTC553
            },
411bc1c78b65atagen2026-10-22 07:39 UTC554
        })
7cf348521204atagen2026-09-26 12:28 UTC555
    }
7cf348521204atagen2026-09-26 12:28 UTC556
411bc1c78b65atagen2026-10-22 07:39 UTC557
    fn sent() -> SendQueueUpdate {
411bc1c78b65atagen2026-10-22 07:39 UTC558
        update(RoomSendQueueUpdate::SentEvent {
411bc1c78b65atagen2026-10-22 07:39 UTC559
            transaction_id: upload().1,
411bc1c78b65atagen2026-10-22 07:39 UTC560
            event_id: event_id!("$sent:localhost").to_owned(),
411bc1c78b65atagen2026-10-22 07:39 UTC561
        })
7cf348521204atagen2026-09-26 12:28 UTC562
    }
7cf348521204atagen2026-09-26 12:28 UTC563
7cf348521204atagen2026-09-26 12:28 UTC564
    fn finished(event: Option<Event>) -> Option<UploadOutcome> {
7cf348521204atagen2026-09-26 12:28 UTC565
        match event? {
7cf348521204atagen2026-09-26 12:28 UTC566
            Event::MediaUploadFinished { outcome, .. } => Some(outcome),
7cf348521204atagen2026-09-26 12:28 UTC567
            _ => None,
7cf348521204atagen2026-09-26 12:28 UTC568
        }
7cf348521204atagen2026-09-26 12:28 UTC569
    }
7c26fad34822atagen2026-09-26 09:41 UTC570
7cf348521204atagen2026-09-26 12:28 UTC571
    #[test]
411bc1c78b65atagen2026-10-22 07:39 UTC572
    fn a_tick_trailing_its_end_says_nothing() {
411bc1c78b65atagen2026-10-22 07:39 UTC573
        let mut tracker = UploadTracker::new(HashSet::new());
411bc1c78b65atagen2026-10-22 07:39 UTC574
        assert!(tracker.update(tick()).is_some());
411bc1c78b65atagen2026-10-22 07:39 UTC575
        assert!(matches!(
411bc1c78b65atagen2026-10-22 07:39 UTC576
            finished(tracker.update(sent())),
411bc1c78b65atagen2026-10-22 07:39 UTC577
            Some(UploadOutcome::Sent { .. })
411bc1c78b65atagen2026-10-22 07:39 UTC578
        ));
411bc1c78b65atagen2026-10-22 07:39 UTC579
        assert!(tracker.update(tick()).is_none());
411bc1c78b65atagen2026-10-22 07:39 UTC580
        assert!(tracker.live.is_empty());
411bc1c78b65atagen2026-10-22 07:39 UTC581
    }
411bc1c78b65atagen2026-10-22 07:39 UTC582
411bc1c78b65atagen2026-10-22 07:39 UTC583
    #[test]
7cf348521204atagen2026-09-26 12:28 UTC584
    fn a_stale_tick_after_a_missed_end_changes_nothing() {
411bc1c78b65atagen2026-10-22 07:39 UTC585
        let mut tracker = UploadTracker::new(HashSet::new());
411bc1c78b65atagen2026-10-22 07:39 UTC586
        assert!(tracker.update(tick()).is_some());
7c26fad34822atagen2026-09-26 09:41 UTC587
        let missed = tracker.resnapshot(HashSet::new());
7c26fad34822atagen2026-09-26 09:41 UTC588
        assert!(matches!(
7c26fad34822atagen2026-09-26 09:41 UTC589
            missed.as_slice(),
7c26fad34822atagen2026-09-26 09:41 UTC590
            [Event::MediaUploadFinished {
7c26fad34822atagen2026-09-26 09:41 UTC591
                outcome: UploadOutcome::Missed,
7c26fad34822atagen2026-09-26 09:41 UTC592
                ..
7c26fad34822atagen2026-09-26 09:41 UTC593
            }]
7c26fad34822atagen2026-09-26 09:41 UTC594
        ));
411bc1c78b65atagen2026-10-22 07:39 UTC595
        assert!(tracker.update(tick()).is_none());
411bc1c78b65atagen2026-10-22 07:39 UTC596
        assert!(tracker.update(sent()).is_none());
411bc1c78b65atagen2026-10-22 07:39 UTC597
        assert!(tracker.live.is_empty());
7c26fad34822atagen2026-09-26 09:41 UTC598
    }
7c26fad34822atagen2026-09-26 09:41 UTC599
}