atagen

macltok

public
Star0
Watch0
6ed8c0ef3a2a5cab4ea62b9694d6b5fa2c60f3df
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
dc609022bb53atagen2026-10-08 12:39 UTC36
/// the sdk's own pre-send work, run early. a send landing mid-share waits on the
dc609022bb53atagen2026-10-08 12:39 UTC37
/// sdk's per-room share lock instead of starting another (forked sdk, see design-decisions)
38c4caecdb3datagen2026-09-26 13:35 UTC38
async fn prepare(client: Client, room_id: OwnedRoomId) -> Result<(), Error> {
38c4caecdb3datagen2026-09-26 13:35 UTC39
    let room = client.get_room(&room_id).ok_or_else(Error::room_unknown)?;
dc609022bb53atagen2026-10-08 12:39 UTC40
    room.prepare_encryption()
dc609022bb53atagen2026-10-08 12:39 UTC41
        .await
dc609022bb53atagen2026-10-08 12:39 UTC42
        .map_err(|source| Error::from_sdk("preparing room encryption failed", source))
38c4caecdb3datagen2026-09-26 13:35 UTC43
}