atagen

macltok

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

Blame: src/engine/prepare.rs

38c4caecdb3datagen2026-09-26 13:35 UTC1
use std::collections::HashMap;
38c4caecdb3datagen2026-09-26 13:35 UTC2
38c4caecdb3datagen2026-09-26 13:35 UTC3
use futures_util::{
38c4caecdb3datagen2026-09-26 13:35 UTC4
    FutureExt,
38c4caecdb3datagen2026-09-26 13:35 UTC5
    future::{Shared, WeakShared},
38c4caecdb3datagen2026-09-26 13:35 UTC6
};
38c4caecdb3datagen2026-09-26 13:35 UTC7
use matrix_sdk::Client;
38c4caecdb3datagen2026-09-26 13:35 UTC8
use ruma::OwnedRoomId;
38c4caecdb3datagen2026-09-26 13:35 UTC9
38c4caecdb3datagen2026-09-26 13:35 UTC10
use super::executor::EngineBoxFuture;
38c4caecdb3datagen2026-09-26 13:35 UTC11
use crate::Error;
38c4caecdb3datagen2026-09-26 13:35 UTC12
38c4caecdb3datagen2026-09-26 13:35 UTC13
type Flight = Shared<EngineBoxFuture<'static, Result<(), Error>>>;
38c4caecdb3datagen2026-09-26 13:35 UTC14
38c4caecdb3datagen2026-09-26 13:35 UTC15
/// one warm-up per room at a time, later asks join the one in flight
38c4caecdb3datagen2026-09-26 13:35 UTC16
#[derive(Default)]
38c4caecdb3datagen2026-09-26 13:35 UTC17
pub(super) struct Preparations(
38c4caecdb3datagen2026-09-26 13:35 UTC18
    HashMap<OwnedRoomId, WeakShared<EngineBoxFuture<'static, Result<(), Error>>>>,
38c4caecdb3datagen2026-09-26 13:35 UTC19
);
38c4caecdb3datagen2026-09-26 13:35 UTC20
38c4caecdb3datagen2026-09-26 13:35 UTC21
impl Preparations {
38c4caecdb3datagen2026-09-26 13:35 UTC22
    pub(super) fn join(&mut self, client: &Client, room_id: OwnedRoomId) -> Flight {
38c4caecdb3datagen2026-09-26 13:35 UTC23
        self.0.retain(|_, flight| flight.upgrade().is_some());
38c4caecdb3datagen2026-09-26 13:35 UTC24
        if let Some(flight) = self.0.get(&room_id).and_then(WeakShared::upgrade) {
38c4caecdb3datagen2026-09-26 13:35 UTC25
            return flight;
38c4caecdb3datagen2026-09-26 13:35 UTC26
        }
38c4caecdb3datagen2026-09-26 13:35 UTC27
        let work: EngineBoxFuture<'static, _> = Box::pin(prepare(client.clone(), room_id.clone()));
38c4caecdb3datagen2026-09-26 13:35 UTC28
        let flight = work.shared();
38c4caecdb3datagen2026-09-26 13:35 UTC29
        if let Some(weak) = flight.downgrade() {
38c4caecdb3datagen2026-09-26 13:35 UTC30
            self.0.insert(room_id, weak);
38c4caecdb3datagen2026-09-26 13:35 UTC31
        }
38c4caecdb3datagen2026-09-26 13:35 UTC32
        flight
38c4caecdb3datagen2026-09-26 13:35 UTC33
    }
38c4caecdb3datagen2026-09-26 13:35 UTC34
}
38c4caecdb3datagen2026-09-26 13:35 UTC35
38c4caecdb3datagen2026-09-26 13:35 UTC36
/// the part of the sdk's pre-send encryption work it lets us reach. syncing the members
38c4caecdb3datagen2026-09-26 13:35 UTC37
/// has the sdk track them all, and its own background `/keys/query` then catches up
38c4caecdb3datagen2026-09-26 13:35 UTC38
/// their devices. the one time key claim and the room key share stay on the send path
38c4caecdb3datagen2026-09-26 13:35 UTC39
async fn prepare(client: Client, room_id: OwnedRoomId) -> Result<(), Error> {
38c4caecdb3datagen2026-09-26 13:35 UTC40
    let room = client.get_room(&room_id).ok_or_else(Error::room_unknown)?;
38c4caecdb3datagen2026-09-26 13:35 UTC41
    if room.encryption_state().is_encrypted() && !room.are_members_synced() {
38c4caecdb3datagen2026-09-26 13:35 UTC42
        room.sync_members()
38c4caecdb3datagen2026-09-26 13:35 UTC43
            .await
38c4caecdb3datagen2026-09-26 13:35 UTC44
            .map_err(|source| Error::from_sdk("syncing room members failed", source))?;
38c4caecdb3datagen2026-09-26 13:35 UTC45
    }
38c4caecdb3datagen2026-09-26 13:35 UTC46
    Ok(())
38c4caecdb3datagen2026-09-26 13:35 UTC47
}