2022-09-07 13:25:51 +02:00
|
|
|
mod data;
|
|
|
|
|
2024-05-09 15:59:08 -07:00
|
|
|
use std::{collections::HashMap, sync::Arc};
|
2022-09-07 13:25:51 +02:00
|
|
|
|
2024-05-27 03:17:20 +00:00
|
|
|
use conduit::{Result, Server};
|
|
|
|
use data::Data;
|
2024-06-28 22:51:39 +00:00
|
|
|
use database::Database;
|
2020-06-05 18:19:26 +02:00
|
|
|
use ruma::{
|
2022-04-06 21:31:29 +02:00
|
|
|
events::{AnyEphemeralRoomEvent, RoomAccountDataEventType},
|
2022-10-08 13:03:07 +02:00
|
|
|
serde::Raw,
|
|
|
|
RoomId, UserId,
|
2020-06-05 18:19:26 +02:00
|
|
|
};
|
2022-10-05 20:41:05 +02:00
|
|
|
|
2024-05-09 15:59:08 -07:00
|
|
|
pub struct Service {
|
2024-05-27 03:17:20 +00:00
|
|
|
db: Data,
|
2022-09-07 13:25:51 +02:00
|
|
|
}
|
2021-06-08 18:10:00 +02:00
|
|
|
|
2022-10-05 12:45:54 +02:00
|
|
|
impl Service {
|
2024-06-28 22:51:39 +00:00
|
|
|
pub fn build(_server: &Arc<Server>, db: &Arc<Database>) -> Result<Self> {
|
2024-05-27 03:17:20 +00:00
|
|
|
Ok(Self {
|
|
|
|
db: Data::new(db),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-03 17:25:31 +02:00
|
|
|
/// Places one event in the account data of the user and removes the
|
|
|
|
/// previous entry.
|
2024-05-27 03:17:20 +00:00
|
|
|
#[allow(clippy::needless_pass_by_value)]
|
2024-05-09 15:59:08 -07:00
|
|
|
pub fn update(
|
2022-04-06 21:31:29 +02:00
|
|
|
&self, room_id: Option<&RoomId>, user_id: &UserId, event_type: RoomAccountDataEventType,
|
2022-10-05 15:33:57 +02:00
|
|
|
data: &serde_json::Value,
|
2020-05-03 17:25:31 +02:00
|
|
|
) -> Result<()> {
|
2024-05-27 03:17:20 +00:00
|
|
|
self.db.update(room_id, user_id, &event_type, data)
|
2020-05-03 17:25:31 +02:00
|
|
|
}
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2020-05-03 17:25:31 +02:00
|
|
|
/// Searches the account data for a specific kind.
|
2024-05-27 03:17:20 +00:00
|
|
|
#[allow(clippy::needless_pass_by_value)]
|
2024-05-09 15:59:08 -07:00
|
|
|
pub fn get(
|
2022-10-05 09:34:25 +02:00
|
|
|
&self, room_id: Option<&RoomId>, user_id: &UserId, event_type: RoomAccountDataEventType,
|
2022-10-05 15:33:57 +02:00
|
|
|
) -> Result<Option<Box<serde_json::value::RawValue>>> {
|
2024-05-27 03:17:20 +00:00
|
|
|
self.db.get(room_id, user_id, &event_type)
|
2020-05-03 17:25:31 +02:00
|
|
|
}
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2020-05-03 17:25:31 +02:00
|
|
|
/// Returns all changes to the account data that happened after `since`.
|
2024-05-27 03:17:20 +00:00
|
|
|
#[tracing::instrument(skip_all, name = "since")]
|
2024-05-09 15:59:08 -07:00
|
|
|
pub fn changes_since(
|
2020-05-03 17:25:31 +02:00
|
|
|
&self, room_id: Option<&RoomId>, user_id: &UserId, since: u64,
|
2022-04-06 21:31:29 +02:00
|
|
|
) -> Result<HashMap<RoomAccountDataEventType, Raw<AnyEphemeralRoomEvent>>> {
|
2022-10-05 09:34:25 +02:00
|
|
|
self.db.changes_since(room_id, user_id, since)
|
2020-05-03 17:25:31 +02:00
|
|
|
}
|
|
|
|
}
|