1
0
Fork 0
mirror of https://forgejo.ellis.link/continuwuation/continuwuity.git synced 2025-07-27 18:28:31 +00:00
continuwuity/src/service/account_data/mod.rs

54 lines
1.5 KiB
Rust
Raw Normal View History

2022-09-07 13:25:51 +02:00
mod data;
pub(crate) use data::Data;
2022-09-07 13:25:51 +02:00
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
use std::collections::HashMap;
2022-09-07 13:25:51 +02:00
2022-10-08 13:03:07 +02:00
use crate::Result;
2022-09-07 13:25:51 +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-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 {
/// Places one event in the account data of the user and removes the previous entry.
#[tracing::instrument(skip(self, room_id, user_id, event_type, data))]
2022-10-05 15:33:57 +02:00
pub fn update(
&self,
room_id: Option<&RoomId>,
user_id: &UserId,
2022-04-06 21:31:29 +02:00
event_type: RoomAccountDataEventType,
2022-10-05 15:33:57 +02:00
data: &serde_json::Value,
) -> Result<()> {
2022-10-05 09:34:25 +02:00
self.db.update(room_id, user_id, event_type, data)
}
/// 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))]
2022-10-05 15:33:57 +02:00
pub fn get(
&self,
room_id: Option<&RoomId>,
user_id: &UserId,
2022-10-05 09:34:25 +02:00
event_type: RoomAccountDataEventType,
2022-10-05 15:33:57 +02:00
) -> Result<Option<Box<serde_json::value::RawValue>>> {
2022-10-05 09:34:25 +02:00
self.db.get(room_id, user_id, event_type)
}
/// Returns all changes to the account data that happened after `since`.
#[tracing::instrument(skip(self, room_id, user_id, since))]
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)
}
}