Skip to main content

arminius_lib_sync/syncv4/execute/
mod.rs

1// SPDX-FileCopyrightText: 2026 BWI GmbH
2//
3// SPDX-License-Identifier: Apache-2.0
4
5//! Contains logic to execute a [plan::SyncV4Plan] in order to produce a [Persisted].
6
7mod fetch;
8mod persist;
9
10use std::collections::{BTreeMap, HashMap};
11
12use arminius_lib_db::TransactedDb;
13use ruma::{OwnedRoomId, api::client::sync::sync_events};
14
15use crate::syncv4::{error::SyncV4Error, plan};
16
17/// Final structure after we've both fetched data and stored new state.
18// TODO: Add extensions data
19#[derive(Debug)]
20pub struct Persisted {
21	/// Next `pos` to be used by the client in their next request.
22	pub(crate) pos: String,
23
24	/// The counts of rooms matched by each list.
25	pub(crate) lists_counts: HashMap<String, usize>,
26
27	/// All the rooms data to returned for the current request.
28	pub(crate) rooms: BTreeMap<OwnedRoomId, sync_events::v5::response::Room>,
29}
30
31/// Execute the given [plan::SyncV4Plan] which contains actual data to fetch.
32pub(super) async fn execute(
33	mut transaction: TransactedDb<'_>,
34	plan: plan::SyncV4Plan,
35) -> Result<Persisted, SyncV4Error> {
36	let fetched = fetch::fetch(&mut transaction, &plan).await?;
37
38	persist::persist(transaction, plan, Some(fetched)).await
39}
40
41/// Execute the given empty [plan::SyncV4Plan] to produce an empty response.
42pub(super) async fn persist_without_position(
43	transaction: TransactedDb<'_>,
44	plan: plan::SyncV4Plan,
45) -> Result<Persisted, SyncV4Error> {
46	persist::persist(transaction, plan, None).await
47}