2024-11-19 03:08:09 +00:00
|
|
|
use std::sync::Arc;
|
2022-09-07 13:25:51 +02:00
|
|
|
|
2024-08-08 17:18:30 +00:00
|
|
|
use conduit::{
|
2024-11-19 03:08:09 +00:00
|
|
|
err, implement,
|
|
|
|
utils::{result::LogErr, stream::TryIgnore, ReadyExt},
|
|
|
|
Err, Result,
|
2024-08-08 17:18:30 +00:00
|
|
|
};
|
2024-11-19 03:08:09 +00:00
|
|
|
use database::{Deserialized, Handle, Interfix, Json, Map};
|
|
|
|
use futures::{Stream, StreamExt, TryFutureExt};
|
2020-06-05 18:19:26 +02:00
|
|
|
use ruma::{
|
2024-10-02 07:57:18 +00:00
|
|
|
events::{
|
|
|
|
AnyGlobalAccountDataEvent, AnyRawAccountDataEvent, AnyRoomAccountDataEvent, GlobalAccountDataEventType,
|
|
|
|
RoomAccountDataEventType,
|
|
|
|
},
|
2024-08-08 17:18:30 +00:00
|
|
|
serde::Raw,
|
2022-10-08 13:03:07 +02:00
|
|
|
RoomId, UserId,
|
2020-06-05 18:19:26 +02:00
|
|
|
};
|
2024-10-02 07:57:18 +00:00
|
|
|
use serde::Deserialize;
|
2024-08-08 17:18:30 +00:00
|
|
|
|
|
|
|
use crate::{globals, Dep};
|
2022-10-05 20:41:05 +02:00
|
|
|
|
2024-05-09 15:59:08 -07:00
|
|
|
pub struct Service {
|
2024-08-08 17:18:30 +00:00
|
|
|
services: Services,
|
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
|
|
|
|
2024-08-08 17:18:30 +00:00
|
|
|
struct Data {
|
|
|
|
roomuserdataid_accountdata: Arc<Map>,
|
|
|
|
roomusertype_roomuserdataid: Arc<Map>,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Services {
|
|
|
|
globals: Dep<globals::Service>,
|
|
|
|
}
|
|
|
|
|
2024-07-04 03:26:19 +00:00
|
|
|
impl crate::Service for Service {
|
|
|
|
fn build(args: crate::Args<'_>) -> Result<Arc<Self>> {
|
|
|
|
Ok(Arc::new(Self {
|
2024-08-08 17:18:30 +00:00
|
|
|
services: Services {
|
|
|
|
globals: args.depend::<globals::Service>("globals"),
|
|
|
|
},
|
|
|
|
db: Data {
|
|
|
|
roomuserdataid_accountdata: args.db["roomuserdataid_accountdata"].clone(),
|
|
|
|
roomusertype_roomuserdataid: args.db["roomusertype_roomuserdataid"].clone(),
|
|
|
|
},
|
2024-07-04 03:26:19 +00:00
|
|
|
}))
|
2024-05-27 03:17:20 +00:00
|
|
|
}
|
|
|
|
|
2024-07-04 03:26:19 +00:00
|
|
|
fn name(&self) -> &str { crate::service::make_name(std::module_path!()) }
|
|
|
|
}
|
|
|
|
|
2024-08-08 17:18:30 +00:00
|
|
|
/// Places one event in the account data of the user and removes the
|
|
|
|
/// previous entry.
|
|
|
|
#[allow(clippy::needless_pass_by_value)]
|
|
|
|
#[implement(Service)]
|
|
|
|
pub async fn update(
|
|
|
|
&self, room_id: Option<&RoomId>, user_id: &UserId, event_type: RoomAccountDataEventType, data: &serde_json::Value,
|
|
|
|
) -> Result<()> {
|
|
|
|
if data.get("type").is_none() || data.get("content").is_none() {
|
|
|
|
return Err!(Request(InvalidParam("Account data doesn't have all required fields.")));
|
2020-05-03 17:25:31 +02:00
|
|
|
}
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2024-10-07 17:54:27 +00:00
|
|
|
let count = self.services.globals.next_count().unwrap();
|
|
|
|
let roomuserdataid = (room_id, user_id, count, &event_type);
|
2024-08-08 17:18:30 +00:00
|
|
|
self.db
|
2024-10-07 17:54:27 +00:00
|
|
|
.roomuserdataid_accountdata
|
|
|
|
.put(roomuserdataid, Json(data));
|
|
|
|
|
|
|
|
let key = (room_id, user_id, &event_type);
|
|
|
|
let prev = self.db.roomusertype_roomuserdataid.qry(&key).await;
|
|
|
|
self.db.roomusertype_roomuserdataid.put(key, roomuserdataid);
|
2024-08-08 17:18:30 +00:00
|
|
|
|
|
|
|
// Remove old entry
|
|
|
|
if let Ok(prev) = prev {
|
|
|
|
self.db.roomuserdataid_accountdata.remove(&prev);
|
2020-05-03 17:25:31 +02:00
|
|
|
}
|
2024-08-08 17:18:30 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-10-02 07:57:18 +00:00
|
|
|
/// Searches the room account data for a specific kind.
|
2024-08-08 17:18:30 +00:00
|
|
|
#[implement(Service)]
|
2024-10-02 07:57:18 +00:00
|
|
|
pub async fn get_global<T>(&self, user_id: &UserId, kind: GlobalAccountDataEventType) -> Result<T>
|
|
|
|
where
|
|
|
|
T: for<'de> Deserialize<'de>,
|
|
|
|
{
|
|
|
|
self.get_raw(None, user_id, &kind.to_string())
|
|
|
|
.await
|
|
|
|
.deserialized()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Searches the global account data for a specific kind.
|
|
|
|
#[implement(Service)]
|
|
|
|
pub async fn get_room<T>(&self, room_id: &RoomId, user_id: &UserId, kind: RoomAccountDataEventType) -> Result<T>
|
|
|
|
where
|
|
|
|
T: for<'de> Deserialize<'de>,
|
|
|
|
{
|
|
|
|
self.get_raw(Some(room_id), user_id, &kind.to_string())
|
|
|
|
.await
|
|
|
|
.deserialized()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[implement(Service)]
|
|
|
|
pub async fn get_raw(&self, room_id: Option<&RoomId>, user_id: &UserId, kind: &str) -> Result<Handle<'_>> {
|
|
|
|
let key = (room_id, user_id, kind.to_owned());
|
2024-08-08 17:18:30 +00:00
|
|
|
self.db
|
|
|
|
.roomusertype_roomuserdataid
|
|
|
|
.qry(&key)
|
2024-09-29 13:13:09 +00:00
|
|
|
.and_then(|roomuserdataid| self.db.roomuserdataid_accountdata.get(&roomuserdataid))
|
2024-08-08 17:18:30 +00:00
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns all changes to the account data that happened after `since`.
|
|
|
|
#[implement(Service)]
|
2024-11-19 03:08:09 +00:00
|
|
|
pub fn changes_since<'a>(
|
|
|
|
&'a self, room_id: Option<&'a RoomId>, user_id: &'a UserId, since: u64,
|
|
|
|
) -> impl Stream<Item = AnyRawAccountDataEvent> + Send + 'a {
|
|
|
|
let prefix = (room_id, user_id, Interfix);
|
2024-11-28 05:54:34 +00:00
|
|
|
let prefix = database::serialize_key(prefix).expect("failed to serialize prefix");
|
2024-08-08 17:18:30 +00:00
|
|
|
|
|
|
|
// Skip the data that's exactly at since, because we sent that last time
|
2024-11-19 03:08:09 +00:00
|
|
|
let first_possible = (room_id, user_id, since.saturating_add(1));
|
2024-08-08 17:18:30 +00:00
|
|
|
|
|
|
|
self.db
|
|
|
|
.roomuserdataid_accountdata
|
2024-11-19 03:08:09 +00:00
|
|
|
.stream_from_raw(&first_possible)
|
2024-08-08 17:18:30 +00:00
|
|
|
.ignore_err()
|
|
|
|
.ready_take_while(move |(k, _)| k.starts_with(&prefix))
|
2024-11-19 03:08:09 +00:00
|
|
|
.map(move |(_, v)| {
|
|
|
|
match room_id {
|
|
|
|
Some(_) => serde_json::from_slice::<Raw<AnyRoomAccountDataEvent>>(v).map(AnyRawAccountDataEvent::Room),
|
|
|
|
None => serde_json::from_slice::<Raw<AnyGlobalAccountDataEvent>>(v).map(AnyRawAccountDataEvent::Global),
|
|
|
|
}
|
|
|
|
.map_err(|e| err!(Database("Database contains invalid account data: {e}")))
|
|
|
|
.log_err()
|
2024-08-08 17:18:30 +00:00
|
|
|
})
|
|
|
|
.ignore_err()
|
2020-05-03 17:25:31 +02:00
|
|
|
}
|