2022-09-07 13:25:51 +02:00
|
|
|
mod data;
|
|
|
|
|
|
|
|
pub use data::Data;
|
|
|
|
|
2020-06-05 18:19:26 +02:00
|
|
|
use ruma::{
|
2022-09-07 13:25:51 +02:00
|
|
|
api::client::{
|
|
|
|
error::ErrorKind,
|
|
|
|
},
|
2022-04-06 21:31:29 +02:00
|
|
|
events::{AnyEphemeralRoomEvent, RoomAccountDataEventType},
|
2020-12-04 18:16:17 -05:00
|
|
|
serde::Raw,
|
2022-09-07 13:25:51 +02:00
|
|
|
signatures::CanonicalJsonValue,
|
|
|
|
DeviceId, RoomId, UserId,
|
2020-06-05 18:19:26 +02:00
|
|
|
};
|
2020-07-29 14:28:40 -04:00
|
|
|
use serde::{de::DeserializeOwned, Serialize};
|
2022-02-03 13:24:04 +01:00
|
|
|
use std::{collections::HashMap, sync::Arc};
|
2022-09-07 13:25:51 +02:00
|
|
|
use tracing::error;
|
|
|
|
|
|
|
|
use crate::{service::*, services, utils, Error, Result};
|
|
|
|
|
2022-10-05 12:45:54 +02:00
|
|
|
pub struct Service {
|
|
|
|
db: Box<dyn 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 {
|
2020-05-03 17:25:31 +02:00
|
|
|
/// Places one event in the account data of the user and removes the previous entry.
|
2022-09-06 23:15:09 +02:00
|
|
|
#[tracing::instrument(skip(self, room_id, user_id, event_type, data))]
|
2020-07-26 22:33:20 +02:00
|
|
|
pub fn update<T: Serialize>(
|
2020-05-03 17:25:31 +02:00
|
|
|
&self,
|
|
|
|
room_id: Option<&RoomId>,
|
|
|
|
user_id: &UserId,
|
2022-04-06 21:31:29 +02:00
|
|
|
event_type: RoomAccountDataEventType,
|
2020-07-28 14:02:29 +02:00
|
|
|
data: &T,
|
2020-05-03 17:25:31 +02:00
|
|
|
) -> Result<()> {
|
2022-10-05 09:34:25 +02:00
|
|
|
self.db.update(room_id, user_id, event_type, data)
|
2020-05-03 17:25:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Searches the account data for a specific kind.
|
2022-10-05 09:34:25 +02:00
|
|
|
#[tracing::instrument(skip(self, room_id, user_id, event_type))]
|
2020-07-26 22:33:20 +02:00
|
|
|
pub fn get<T: DeserializeOwned>(
|
2020-05-03 17:25:31 +02:00
|
|
|
&self,
|
|
|
|
room_id: Option<&RoomId>,
|
|
|
|
user_id: &UserId,
|
2022-10-05 09:34:25 +02:00
|
|
|
event_type: RoomAccountDataEventType,
|
2020-07-26 22:33:20 +02:00
|
|
|
) -> Result<Option<T>> {
|
2022-10-05 09:34:25 +02:00
|
|
|
self.db.get(room_id, user_id, event_type)
|
2020-05-03 17:25:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns all changes to the account data that happened after `since`.
|
2021-07-29 08:36:01 +02:00
|
|
|
#[tracing::instrument(skip(self, room_id, user_id, since))]
|
2020-05-03 17:25:31 +02:00
|
|
|
pub fn changes_since(
|
|
|
|
&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
|
|
|
}
|
|
|
|
}
|