arminius_lib_model/
syncv4.rs1mod tests;
8
9use std::collections::{HashMap, HashSet};
10
11use ruma::{api::client::sync::sync_events, events::StateEventType};
12
13use crate::types::StreamId;
14
15#[derive(Debug, Clone, Default)]
18pub struct RoomConfig {
19 pub timeline_limit: i64,
21 pub required_state: Vec<(StateEventType, String)>,
23}
24
25impl From<sync_events::v5::request::RoomDetails> for RoomConfig {
27 fn from(room_config: sync_events::v5::request::RoomDetails) -> Self {
28 Self {
29 timeline_limit: room_config.timeline_limit.into(),
30 required_state: room_config.required_state,
31 }
32 }
33}
34
35impl From<sync_events::v5::request::RoomSubscription> for RoomConfig {
37 fn from(room_config: sync_events::v5::request::RoomSubscription) -> Self {
38 Self {
39 timeline_limit: room_config.timeline_limit.into(),
40 required_state: room_config.required_state,
41 }
42 }
43}
44
45impl RoomConfig {
47 pub fn combine(&mut self, other: &Self) {
54 self.timeline_limit = self.timeline_limit.max(other.timeline_limit);
55
56 let mut other_required_state = other.required_state.clone();
57 self.required_state.append(&mut other_required_state);
58
59 self.required_state = self
60 .required_state
61 .iter()
62 .fold(
63 HashMap::<StateEventType, HashSet<String>>::new(),
64 |mut the_map, (state_event_type, state_key)| {
65 if the_map
66 .get(&StateEventType::from("*"))
67 .is_some_and(|the_state_keys| the_state_keys.get("*").is_some())
68 || state_event_type.to_string() == "*" && state_key == "*"
69 {
70 return vec![(StateEventType::from("*"), ["*".to_owned()].into())]
71 .into_iter()
72 .collect();
73 };
74
75 if state_key == "*" {
76 the_map.insert(state_event_type.clone(), [state_key.clone()].into());
78 return the_map;
79 }
80
81 if the_map
84 .get(&StateEventType::from("*"))
85 .is_some_and(|keys| keys.contains(state_key))
86 {
87 return the_map;
88 }
89 let keys = the_map.entry(state_event_type.clone()).or_default();
90 if !keys.contains("*") {
91 keys.insert(state_key.clone());
92 }
93
94 the_map
95 },
96 )
97 .into_iter()
98 .fold(vec![], |mut the_list, (state_event_type, keys)| {
99 keys.into_iter().for_each(|key| the_list.push((state_event_type.clone(), key)));
100 the_list
101 });
102 }
103}
104
105#[derive(Debug, Clone, PartialEq, Eq)]
109pub enum StreamUpperBound {
110 Live,
113 Previously(StreamId),
120}