2022-08-14 13:38:21 +02:00
|
|
|
mod data;
|
2022-12-18 06:37:03 +01:00
|
|
|
use std::{collections::HashMap, sync::Arc};
|
2022-09-06 23:15:09 +02:00
|
|
|
|
2022-08-14 13:38:21 +02:00
|
|
|
pub use data::Data;
|
2022-10-05 20:34:31 +02:00
|
|
|
use ruma::{events::StateEventType, EventId, RoomId};
|
2022-08-14 13:38:21 +02:00
|
|
|
|
2022-10-05 20:34:31 +02:00
|
|
|
use crate::{PduEvent, Result};
|
2022-08-14 13:38:21 +02:00
|
|
|
|
2022-10-05 12:45:54 +02:00
|
|
|
pub struct Service {
|
2022-10-08 13:02:52 +02:00
|
|
|
pub db: &'static dyn Data,
|
2022-08-14 13:38:21 +02:00
|
|
|
}
|
|
|
|
|
2022-10-05 12:45:54 +02:00
|
|
|
impl Service {
|
2020-08-26 11:15:52 -04:00
|
|
|
/// Builds a StateMap by iterating over all keys that start
|
|
|
|
/// with state_hash, this gives the full state for the given state_hash.
|
2022-12-18 06:37:03 +01:00
|
|
|
#[tracing::instrument(skip(self))]
|
|
|
|
pub async fn state_full_ids(&self, shortstatehash: u64) -> Result<HashMap<u64, Arc<EventId>>> {
|
2022-10-05 20:33:55 +02:00
|
|
|
self.db.state_full_ids(shortstatehash).await
|
2021-03-17 22:30:25 +01:00
|
|
|
}
|
|
|
|
|
2022-06-18 16:38:41 +02:00
|
|
|
pub async fn state_full(
|
2021-03-17 22:30:25 +01:00
|
|
|
&self,
|
|
|
|
shortstatehash: u64,
|
2022-04-06 21:31:29 +02:00
|
|
|
) -> Result<HashMap<(StateEventType, String), Arc<PduEvent>>> {
|
2022-10-05 20:33:55 +02:00
|
|
|
self.db.state_full(shortstatehash).await
|
2020-08-06 08:29:59 -04:00
|
|
|
}
|
|
|
|
|
2020-09-17 14:44:47 +02:00
|
|
|
/// Returns a single PDU from `room_id` with key (`event_type`, `state_key`).
|
2021-02-28 12:41:03 +01:00
|
|
|
#[tracing::instrument(skip(self))]
|
2021-04-11 21:01:27 +02:00
|
|
|
pub fn state_get_id(
|
2020-09-17 14:44:47 +02:00
|
|
|
&self,
|
2021-03-17 22:30:25 +01:00
|
|
|
shortstatehash: u64,
|
2022-04-06 21:31:29 +02:00
|
|
|
event_type: &StateEventType,
|
2020-09-17 14:44:47 +02:00
|
|
|
state_key: &str,
|
2021-08-26 17:58:32 -04:00
|
|
|
) -> Result<Option<Arc<EventId>>> {
|
2022-08-14 13:38:21 +02:00
|
|
|
self.db.state_get_id(shortstatehash, event_type, state_key)
|
2020-08-06 08:29:59 -04:00
|
|
|
}
|
|
|
|
|
2021-04-11 21:01:27 +02:00
|
|
|
/// Returns a single PDU from `room_id` with key (`event_type`, `state_key`).
|
|
|
|
pub fn state_get(
|
|
|
|
&self,
|
|
|
|
shortstatehash: u64,
|
2022-04-06 21:31:29 +02:00
|
|
|
event_type: &StateEventType,
|
2021-04-11 21:01:27 +02:00
|
|
|
state_key: &str,
|
2021-06-30 09:52:01 +02:00
|
|
|
) -> Result<Option<Arc<PduEvent>>> {
|
2022-10-05 18:36:12 +02:00
|
|
|
self.db.state_get(shortstatehash, event_type, state_key)
|
2021-04-11 21:01:27 +02:00
|
|
|
}
|
|
|
|
|
2021-01-16 16:37:20 -05:00
|
|
|
/// Returns the state hash for this pdu.
|
2021-03-17 22:30:25 +01:00
|
|
|
pub fn pdu_shortstatehash(&self, event_id: &EventId) -> Result<Option<u64>> {
|
2022-08-14 13:38:21 +02:00
|
|
|
self.db.pdu_shortstatehash(event_id)
|
2020-09-17 14:44:47 +02:00
|
|
|
}
|
2020-08-06 08:29:59 -04:00
|
|
|
|
2020-05-03 17:25:31 +02:00
|
|
|
/// Returns the full room state.
|
2021-02-28 12:41:03 +01:00
|
|
|
#[tracing::instrument(skip(self))]
|
2022-06-18 16:38:41 +02:00
|
|
|
pub async fn room_state_full(
|
2020-12-31 08:40:49 -05:00
|
|
|
&self,
|
|
|
|
room_id: &RoomId,
|
2022-04-06 21:31:29 +02:00
|
|
|
) -> Result<HashMap<(StateEventType, String), Arc<PduEvent>>> {
|
2022-10-05 20:33:55 +02:00
|
|
|
self.db.room_state_full(room_id).await
|
2020-05-03 17:25:31 +02:00
|
|
|
}
|
|
|
|
|
2021-04-11 21:01:27 +02:00
|
|
|
/// Returns a single PDU from `room_id` with key (`event_type`, `state_key`).
|
|
|
|
#[tracing::instrument(skip(self))]
|
|
|
|
pub fn room_state_get_id(
|
|
|
|
&self,
|
|
|
|
room_id: &RoomId,
|
2022-04-06 21:31:29 +02:00
|
|
|
event_type: &StateEventType,
|
2021-04-11 21:01:27 +02:00
|
|
|
state_key: &str,
|
2021-08-26 17:58:32 -04:00
|
|
|
) -> Result<Option<Arc<EventId>>> {
|
2022-08-14 13:38:21 +02:00
|
|
|
self.db.room_state_get_id(room_id, event_type, state_key)
|
2021-04-11 21:01:27 +02:00
|
|
|
}
|
|
|
|
|
2020-08-26 11:15:52 -04:00
|
|
|
/// Returns a single PDU from `room_id` with key (`event_type`, `state_key`).
|
2021-02-28 12:41:03 +01:00
|
|
|
#[tracing::instrument(skip(self))]
|
2020-06-12 13:18:25 +02:00
|
|
|
pub fn room_state_get(
|
|
|
|
&self,
|
|
|
|
room_id: &RoomId,
|
2022-04-06 21:31:29 +02:00
|
|
|
event_type: &StateEventType,
|
2020-06-12 13:18:25 +02:00
|
|
|
state_key: &str,
|
2021-06-30 09:52:01 +02:00
|
|
|
) -> Result<Option<Arc<PduEvent>>> {
|
2022-08-14 13:38:21 +02:00
|
|
|
self.db.room_state_get(room_id, event_type, state_key)
|
2020-06-12 13:18:25 +02:00
|
|
|
}
|
2022-08-14 13:38:21 +02:00
|
|
|
}
|