atagen

macltok

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

Blame: src/engine/watchers.rs

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