Skip to main content

arminius_lib_sync/syncv4/gather/
mod.rs

1// SPDX-FileCopyrightText: 2026 BWI GmbH
2//
3// SPDX-License-Identifier: Apache-2.0
4
5//! Contains the logic to **gather** everything that will be needed to handle a sync v4 request.
6
7use std::collections::HashMap;
8
9use arminius_lib_db_sync::syncv4::{RoomInfo, RoomState, TransactedSyncV4Queries};
10use ruma::OwnedRoomId;
11
12use crate::syncv4::{connect::ResolvedConnection, error::SyncV4Error};
13
14/// Contains all the inputs needed to handle a sync v4 request.
15#[derive(Debug)]
16pub struct SyncV4Inputs {
17	/// The connection of the current request.
18	pub(crate) connection: ResolvedConnection,
19
20	/// Map of all the rooms the user is or has been a member of to their information.
21	pub(crate) user_rooms: HashMap<OwnedRoomId, RoomInfo>,
22
23	/// The loaded per-room state.
24	pub(crate) rooms_states: HashMap<OwnedRoomId, RoomState>,
25}
26
27/// Gathers everything, from the requester's point of view, that will be needed to handle the
28/// request (See [SyncV4Inputs]).
29pub(super) async fn gather<T: TransactedSyncV4Queries>(
30	transaction: &mut T,
31	connection: ResolvedConnection,
32) -> Result<SyncV4Inputs, SyncV4Error> {
33	let user_rooms = transaction.fetch_user_room_info(&connection.key.user_id).await?;
34
35	let rooms_states = match connection.previous_stored_position() {
36		Some(prev_position) => {
37			let events_stream_statuses =
38				transaction.fetch_events_room_streams(prev_position).await?;
39
40			// TODO: Combine the room configs
41			let rooms_states: HashMap<OwnedRoomId, RoomState> = events_stream_statuses
42				.into_iter()
43				.map(|(room_id, events_stream_status)| {
44					(room_id, RoomState { events_stream_upper_bound: events_stream_status })
45				})
46				.collect();
47
48			rooms_states
49		}
50		None => HashMap::new(),
51	};
52
53	Ok(SyncV4Inputs { connection, user_rooms, rooms_states })
54}