Skip to main content

tuwunel_service/account_data/
mod.rs

1//! User and room account data storage.
2//!
3//! The service stores raw account data events behind monotonic update counters and maintains a
4//! secondary index by room, user, and event type. Its streams expose changes for incremental sync.
5
6mod direct;
7mod push_rules;
8mod room_tags;
9#[cfg(test)]
10mod tests;
11
12use std::sync::Arc;
13
14use futures::{Stream, StreamExt, TryFutureExt, pin_mut};
15use ruma::{
16	OwnedUserId, RoomId, UserId,
17	events::{
18		AnyGlobalAccountDataEvent, AnyRawAccountDataEvent, AnyRoomAccountDataEvent,
19		GlobalAccountDataEventType, RoomAccountDataEventType,
20	},
21	push::{RuleKind, Ruleset},
22	serde::Raw,
23};
24use serde::Deserialize;
25use serde_json::json;
26use tuwunel_core::{
27	Err, Result, at, err, implement,
28	utils::{MutexMap, ReadyExt, TryReadyExt, result::LogErr, stream::TryIgnore},
29};
30use tuwunel_database::{Deserialized, Handle, Ignore, Interfix, Json, Map};
31
32/// Maximum number of push rules one account may hold.
33///
34/// The ruleset is a single account-data blob rewritten in full on every
35/// mutation and matched against every event for every local recipient, so the
36/// count bounds the write cost and the per-event matching work alike.
37pub const MAX_RULES: usize = 10_000;
38
39/// Longest rule ID stored, in bytes.
40///
41/// Room IDs reach 255 bytes and are routinely used as rule IDs, so the ceiling
42/// sits above that rather than at it.
43pub const MAX_RULE_ID_BYTES: usize = 300;
44
45/// Largest match and action data stored for one rule, in bytes.
46///
47/// Rule IDs are excluded and bounded separately by [`MAX_RULE_ID_BYTES`].
48pub const MAX_RULE_BYTES: usize = 1024;
49
50/// Stores and queries global and room-scoped account data.
51///
52/// Updates commit the secondary pointer and new event blob in one transaction while retaining
53/// monotonic counters for sync. Typed accessors deserialize the complete account data event
54/// envelope.
55pub struct Service {
56	services: Arc<crate::services::OnceServices>,
57	db: Data,
58	/// Serializes each user's updates and erases, from the first read to the
59	/// commit.
60	///
61	/// A superseded revision is keyed by a count that only the pointer names, so
62	/// replacing it needs that read; skipping orphans on read instead would cost
63	/// every sync. An erase holds the lock across its scans, so no update lands
64	/// between a scan and the commit. An entry lives only while a writer for the
65	/// user holds or awaits the lock.
66	updating: MutexMap<OwnedUserId, ()>,
67}
68
69struct Data {
70	roomuserdataid_accountdata: Arc<Map>,
71	roomusertype_roomuserdataid: Arc<Map>,
72}
73
74impl crate::Service for Service {
75	fn build(args: &crate::Args<'_>) -> Result<Arc<Self>> {
76		Ok(Arc::new(Self {
77			services: args.services.clone(),
78			db: Data {
79				roomuserdataid_accountdata: args.db["roomuserdataid_accountdata"].clone(),
80				roomusertype_roomuserdataid: args.db["roomusertype_roomuserdataid"].clone(),
81			},
82			updating: MutexMap::new(),
83		}))
84	}
85
86	fn name(&self) -> &str { crate::service::make_name(std::module_path!()) }
87}
88
89/// Whether a ruleset has room for the rule with the given kind and ID.
90///
91/// A rule replacing one already present adds nothing, so only an unseen ID is
92/// held against [`MAX_RULES`]. The ID is bounded here rather than at each call
93/// site because a room ID serves as the rule ID for room rules and carries no
94/// length of its own once arbitrary-length identifiers are accepted.
95#[must_use]
96pub fn admits_rule(ruleset: &Ruleset, kind: RuleKind, rule_id: &str) -> bool {
97	rule_id.len() <= MAX_RULE_ID_BYTES
98		&& (ruleset.get(kind, rule_id).is_some()
99			|| ruleset.iter().take(MAX_RULES).count() < MAX_RULES)
100}
101
102/// Stores an account data event and replaces its previous revision.
103///
104/// The input must contain complete `type` and `content` fields. A new global counter value keys the
105/// event blob, secondary pointer, and removal of the superseded blob in one transaction. One user's
106/// updates take turns, so each replaces the revision the previous one stored.
107///
108/// # Panics
109///
110/// Panics when dispatching the global sequence number fails.
111#[implement(Service)]
112pub async fn update(
113	&self,
114	room_id: Option<&RoomId>,
115	user_id: &UserId,
116	event_type: RoomAccountDataEventType,
117	data: &serde_json::Value,
118) -> Result {
119	if data.get("type").is_none() || data.get("content").is_none() {
120		return Err!(Request(InvalidParam("Account data doesn't have all required fields.")));
121	}
122
123	// Taken before the count, so an update holds no sequence number of its own while it waits.
124	let _updating = self.updating.lock(user_id).await;
125
126	let key = (room_id, user_id, &event_type);
127	let prev = self
128		.db
129		.roomusertype_roomuserdataid
130		.qry(&key)
131		.await;
132
133	let count = self.services.globals.next_count();
134	let roomuserdataid = (room_id, user_id, *count, &event_type);
135
136	let mut txn = self.services.db.txn();
137
138	txn.put(&self.db.roomuserdataid_accountdata, roomuserdataid, Json(data));
139	txn.put(&self.db.roomusertype_roomuserdataid, key, roomuserdataid);
140
141	if let Ok(prev) = prev {
142		txn.del_raw(&self.db.roomuserdataid_accountdata, prev);
143	}
144
145	txn.execute();
146
147	Ok(())
148}
149
150/// Replaces an account data event with an MSC3391 tombstone.
151///
152/// Delta sync surfaces the empty content so clients can apply the deletion. Zero-token V3 and V5
153/// sync responses and client account-data GET routes treat the tombstone as absent.
154///
155/// # Panics
156///
157/// Panics when dispatching the global sequence number fails.
158#[implement(Service)]
159pub async fn delete(
160	&self,
161	room_id: Option<&RoomId>,
162	user_id: &UserId,
163	event_type: RoomAccountDataEventType,
164) -> Result {
165	let tombstone = json!({
166		"type": event_type.to_string(),
167		"content": {},
168	});
169
170	self.update(room_id, user_id, event_type, &tombstone)
171		.await
172}
173
174/// Searches the global account data for a specific kind.
175///
176/// Global data is stored under no room, so one kind can be held once globally
177/// and once in every room without collision. The record is the whole
178/// `{type, content}` event, so `T` names the event rather than its content.
179#[implement(Service)]
180pub async fn get_global<T>(&self, user_id: &UserId, kind: GlobalAccountDataEventType) -> Result<T>
181where
182	T: for<'de> Deserialize<'de>,
183{
184	self.get_raw(None, user_id, &kind.to_string())
185		.await
186		.deserialized()
187}
188
189/// Searches the room account data for a specific kind.
190///
191/// The room scopes the lookup, so one kind may hold different data in each of
192/// them. The record is the whole `{type, content}` event, so `T` names the
193/// event rather than its content.
194#[implement(Service)]
195pub async fn get_room<T>(
196	&self,
197	room_id: &RoomId,
198	user_id: &UserId,
199	kind: RoomAccountDataEventType,
200) -> Result<T>
201where
202	T: for<'de> Deserialize<'de>,
203{
204	self.get_raw(Some(room_id), user_id, &kind.to_string())
205		.await
206		.deserialized()
207}
208
209/// Loads a stored account data event without deserializing it.
210///
211/// The optional room ID selects room-scoped or global account data. The lookup resolves the
212/// current secondary index entry before borrowing the raw event from storage.
213#[implement(Service)]
214pub async fn get_raw(
215	&self,
216	room_id: Option<&RoomId>,
217	user_id: &UserId,
218	kind: &str,
219) -> Result<Handle<'_>> {
220	let key = (room_id, user_id, kind.to_owned());
221	self.db
222		.roomusertype_roomuserdataid
223		.qry(&key)
224		.and_then(|roomuserdataid| {
225			self.db
226				.roomuserdataid_accountdata
227				.get(&roomuserdataid)
228		})
229		.await
230}
231
232/// Streams account data changes after a counter value.
233///
234/// The optional upper bound is inclusive. Cursor and decoding failures are logged and omitted from
235/// this convenience stream.
236#[implement(Service)]
237pub fn changes_since<'a>(
238	&'a self,
239	room_id: Option<&'a RoomId>,
240	user_id: &'a UserId,
241	since: u64,
242	to: Option<u64>,
243) -> impl Stream<Item = AnyRawAccountDataEvent> + Send + 'a {
244	self.changes_since_fallible(room_id, user_id, since, to)
245		.map(LogErr::log_err)
246		.ignore_err()
247}
248
249/// Returns bounded account-data changes without suppressing failures.
250///
251/// The lower bound is exclusive and the optional upper bound is inclusive.
252/// Cursor, decode, and deserialization failures remain in the stream for an
253/// atomic caller to handle.
254#[implement(Service)]
255pub fn changes_since_fallible<'a>(
256	&'a self,
257	room_id: Option<&'a RoomId>,
258	user_id: &'a UserId,
259	since: u64,
260	to: Option<u64>,
261) -> impl Stream<Item = Result<AnyRawAccountDataEvent>> + Send + 'a {
262	type Key<'a> = (Option<&'a RoomId>, &'a UserId, u64, Ignore);
263
264	// Skip the data that's exactly at since, because we sent that last time
265	let first_possible = (room_id, user_id, since.saturating_add(1));
266
267	self.db
268		.roomuserdataid_accountdata
269		.stream_from(&first_possible)
270		.ready_try_take_while(move |((room_id_, user_id_, count, _), _): &(Key<'_>, _)| {
271			Ok(room_id == *room_id_ && user_id == *user_id_ && to.is_none_or(|to| *count <= to))
272		})
273		.ready_and_then(move |(_, v)| {
274			match room_id {
275				| Some(_) => serde_json::from_slice::<Raw<AnyRoomAccountDataEvent>>(v)
276					.map(AnyRawAccountDataEvent::Room),
277				| None => serde_json::from_slice::<Raw<AnyGlobalAccountDataEvent>>(v)
278					.map(AnyRawAccountDataEvent::Global),
279			}
280			.map_err(|e| err!(Database("Database contains invalid account data: {e}")))
281		})
282}
283
284/// Erases a user's account data within one MSC4025 namespace.
285///
286/// An absent room selects global data, while a room selects only that room's data. Rows yielded
287/// successfully from both prefix scans are deleted together; scan errors are skipped. The user's
288/// update lock spans the scans and the commit, so an update lands wholly before or after the
289/// erase.
290#[implement(Service)]
291pub async fn erase_user(&self, user_id: &UserId, room_id: Option<&RoomId>) {
292	let _updating = self.updating.lock(user_id).await;
293
294	self.erase_rows(user_id, room_id).await;
295}
296
297#[implement(Service)]
298async fn erase_rows(&self, user_id: &UserId, room_id: Option<&RoomId>) {
299	let prefix = (room_id, user_id, Interfix);
300	let mut txn = self.services.db.txn();
301
302	self.db
303		.roomuserdataid_accountdata
304		.keys_prefix_raw(&prefix)
305		.ignore_err()
306		.ready_for_each(|key| txn.del_raw(&self.db.roomuserdataid_accountdata, key))
307		.await;
308
309	self.db
310		.roomusertype_roomuserdataid
311		.keys_prefix_raw(&prefix)
312		.ignore_err()
313		.ready_for_each(|key| txn.del_raw(&self.db.roomusertype_roomuserdataid, key))
314		.await;
315
316	txn.execute();
317}
318
319/// Returns the latest account data counter at or below an optional bound.
320///
321/// The lookup is scoped to one user and either global data or a single room. An empty scope produces
322/// a not-found request error.
323#[implement(Service)]
324pub async fn last_count<'a>(
325	&'a self,
326	room_id: Option<&'a RoomId>,
327	user_id: &'a UserId,
328	upper: Option<u64>,
329) -> Result<u64> {
330	type Key<'a> = (Option<&'a RoomId>, &'a UserId, u64, Ignore);
331
332	let upper = upper.unwrap_or(u64::MAX);
333	let key = (room_id, user_id, upper, Interfix);
334	let keys = self
335		.db
336		.roomuserdataid_accountdata
337		.rev_keys_from(&key)
338		.ignore_err()
339		.ready_take_while(move |(room_id_, user_id_, ..): &Key<'_>| {
340			room_id == *room_id_ && user_id == *user_id_
341		})
342		.map(at!(2));
343
344	pin_mut!(keys);
345	keys.next()
346		.await
347		.ok_or_else(|| err!(Request(NotFound("No account data found."))))
348}