Skip to main content

tuwunel_admin/query/
account_data.rs

1use clap::Subcommand;
2use futures::StreamExt;
3use ruma::{OwnedRoomId, OwnedUserId};
4use tuwunel_core::Result;
5use tuwunel_database::Deserialized;
6
7use crate::{admin_command, admin_command_dispatch};
8
9#[admin_command_dispatch]
10#[derive(Debug, Subcommand)]
11/// All the getters and iterators from src/service/account_data/
12pub(crate) enum AccountDataCommand {
13	/// - Returns all changes to the account data that happened after `since`.
14	ChangesSince {
15		/// Full user ID
16		user_id: OwnedUserId,
17		/// UNIX timestamp since (u64)
18		since: u64,
19		/// Optional room ID of the account data
20		room_id: Option<OwnedRoomId>,
21	},
22
23	/// - Searches the account data for a specific kind.
24	AccountDataGet {
25		/// Full user ID
26		user_id: OwnedUserId,
27		/// Account data event type
28		kind: String,
29		/// Optional room ID of the account data
30		room_id: Option<OwnedRoomId>,
31	},
32}
33
34#[admin_command]
35async fn changes_since(
36	&self,
37	user_id: OwnedUserId,
38	since: u64,
39	room_id: Option<OwnedRoomId>,
40) -> Result {
41	let timer = tokio::time::Instant::now();
42	let results: Vec<_> = self
43		.services
44		.account_data
45		.changes_since(room_id.as_deref(), &user_id, since, None)
46		.collect()
47		.await;
48	let query_time = timer.elapsed();
49
50	self.write_str(&format!("Query completed in {query_time:?}:\n\n```rs\n{results:?}\n```"))
51		.await
52}
53
54#[admin_command]
55async fn account_data_get(
56	&self,
57	user_id: OwnedUserId,
58	kind: String,
59	room_id: Option<OwnedRoomId>,
60) -> Result {
61	let timer = tokio::time::Instant::now();
62	let results: serde_json::Value = self
63		.services
64		.account_data
65		.get_raw(room_id.as_deref(), &user_id, &kind)
66		.await
67		.deserialized()?;
68	let query_time = timer.elapsed();
69
70	self.write_str(&format!("Query completed in {query_time:?}:\n\n```rs\n{results:#?}\n```"))
71		.await
72}