Skip to main content

tuwunel_admin/query/
presence.rs

1use clap::Subcommand;
2use futures::StreamExt;
3use ruma::OwnedUserId;
4use tuwunel_core::Result;
5
6use crate::Context;
7
8#[derive(Debug, Subcommand)]
9/// All the getters and iterators from src/service/presence/
10pub(crate) enum PresenceCommand {
11	/// - Returns the latest presence event for the given user.
12	GetPresence {
13		/// Full user ID
14		user_id: OwnedUserId,
15	},
16
17	/// - Iterator of the most recent presence updates that happened after the
18	///   event with id `since`.
19	PresenceSince {
20		/// UNIX timestamp since (u64)
21		since: u64,
22
23		/// Upper-bound of since
24		to: Option<u64>,
25	},
26}
27
28/// All the getters and iterators from src/service/presence/
29pub(super) async fn process(subcommand: PresenceCommand, context: &Context<'_>) -> Result {
30	let services = context.services;
31
32	match subcommand {
33		| PresenceCommand::GetPresence { user_id } => {
34			let timer = tokio::time::Instant::now();
35			let results = services.presence.get_presence(&user_id).await;
36			let query_time = timer.elapsed();
37
38			write!(context, "Query completed in {query_time:?}:\n\n```rs\n{results:#?}\n```")
39		},
40		| PresenceCommand::PresenceSince { since, to } => {
41			let timer = tokio::time::Instant::now();
42			let results: Vec<(_, _, _)> = services
43				.presence
44				.presence_since(since, to)
45				.map(|(user_id, count, bytes)| (user_id.to_owned(), count, bytes.to_vec()))
46				.collect()
47				.await;
48			let query_time = timer.elapsed();
49
50			write!(context, "Query completed in {query_time:?}:\n\n```rs\n{results:#?}\n```")
51		},
52	}
53	.await
54}