2024-07-27 00:11:41 +00:00
|
|
|
use clap::Subcommand;
|
|
|
|
|
use conduit::Result;
|
2024-08-08 17:18:30 +00:00
|
|
|
use futures::StreamExt;
|
2024-07-27 00:11:41 +00:00
|
|
|
use ruma::{events::room::message::RoomMessageEventContent, UserId};
|
2024-04-20 14:44:31 -04:00
|
|
|
|
2024-07-27 00:11:41 +00:00
|
|
|
use crate::Command;
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Subcommand)]
|
|
|
|
|
/// All the getters and iterators from src/database/key_value/presence.rs
|
|
|
|
|
pub(crate) enum PresenceCommand {
|
|
|
|
|
/// - Returns the latest presence event for the given user.
|
|
|
|
|
GetPresence {
|
|
|
|
|
/// Full user ID
|
|
|
|
|
user_id: Box<UserId>,
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/// - Iterator of the most recent presence updates that happened after the
|
|
|
|
|
/// event with id `since`.
|
|
|
|
|
PresenceSince {
|
|
|
|
|
/// UNIX timestamp since (u64)
|
|
|
|
|
since: u64,
|
|
|
|
|
},
|
|
|
|
|
}
|
2024-04-20 14:44:31 -04:00
|
|
|
|
2024-04-20 17:02:24 -04:00
|
|
|
/// All the getters and iterators in key_value/presence.rs
|
2024-07-27 00:11:41 +00:00
|
|
|
pub(super) async fn process(subcommand: PresenceCommand, context: &Command<'_>) -> Result<RoomMessageEventContent> {
|
|
|
|
|
let services = context.services;
|
|
|
|
|
|
2024-04-20 14:44:31 -04:00
|
|
|
match subcommand {
|
2024-07-27 00:11:41 +00:00
|
|
|
PresenceCommand::GetPresence {
|
2024-04-20 14:44:31 -04:00
|
|
|
user_id,
|
|
|
|
|
} => {
|
|
|
|
|
let timer = tokio::time::Instant::now();
|
2024-08-08 17:18:30 +00:00
|
|
|
let results = services.presence.db.get_presence(&user_id).await;
|
2024-04-20 14:44:31 -04:00
|
|
|
let query_time = timer.elapsed();
|
|
|
|
|
|
2024-06-17 10:12:44 +00:00
|
|
|
Ok(RoomMessageEventContent::notice_markdown(format!(
|
|
|
|
|
"Query completed in {query_time:?}:\n\n```rs\n{results:#?}\n```"
|
|
|
|
|
)))
|
2024-04-20 14:44:31 -04:00
|
|
|
},
|
2024-07-27 00:11:41 +00:00
|
|
|
PresenceCommand::PresenceSince {
|
2024-04-20 14:44:31 -04:00
|
|
|
since,
|
|
|
|
|
} => {
|
|
|
|
|
let timer = tokio::time::Instant::now();
|
2024-07-27 00:11:41 +00:00
|
|
|
let results = services.presence.db.presence_since(since);
|
2024-08-08 17:18:30 +00:00
|
|
|
let presence_since: Vec<(_, _, _)> = results.collect().await;
|
2024-07-01 20:35:39 +00:00
|
|
|
let query_time = timer.elapsed();
|
2024-04-20 14:44:31 -04:00
|
|
|
|
2024-06-17 10:12:44 +00:00
|
|
|
Ok(RoomMessageEventContent::notice_markdown(format!(
|
|
|
|
|
"Query completed in {query_time:?}:\n\n```rs\n{presence_since:#?}\n```"
|
|
|
|
|
)))
|
2024-04-20 14:44:31 -04:00
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|