separate account_data service methods for room vs global events

Previously we were mashing everything together as RoomAccountDataEvent,
even the global events. This technically worked, because of the hidden
custom fields on the ruma event types, but it's confusing and easy to
mess up. Separate methods with appropriate types are preferable.
This commit is contained in:
Olivia Lee 2025-03-23 02:08:38 -07:00
parent 6897f0ba34
commit 66210bc32d
No known key found for this signature in database
GPG key ID: 54D568A15B9CD1F9
16 changed files with 349 additions and 361 deletions

View file

@ -1,7 +1,10 @@
use std::collections::HashMap;
use ruma::{
events::{AnyEphemeralRoomEvent, RoomAccountDataEventType},
events::{
AnyGlobalAccountDataEvent, AnyRoomAccountDataEvent,
GlobalAccountDataEventType, RoomAccountDataEventType,
},
serde::Raw,
RoomId, UserId,
};
@ -23,42 +26,104 @@ impl Service {
}
}
/// Places one event in the account data of the user and removes the
/// Places one event in the global account data of the user and removes the
/// previous entry.
#[tracing::instrument(skip(self, room_id, user_id, event_type, data))]
pub(crate) fn update(
#[tracing::instrument(skip(self, user_id, event))]
pub(crate) fn update_global(
&self,
room_id: Option<&RoomId>,
user_id: &UserId,
event_type: RoomAccountDataEventType,
data: &serde_json::Value,
event_type: &GlobalAccountDataEventType,
event: &Raw<AnyGlobalAccountDataEvent>,
) -> Result<()> {
self.db.update(room_id, user_id, event_type, data)
self.db.update(None, user_id, &event_type.to_string(), event.json())
}
/// Searches the account data for a specific kind.
#[tracing::instrument(skip(self, room_id, user_id, event_type))]
pub(crate) fn get(
/// Places one event in the room account data of the user and removes the
/// previous entry for that room.
#[tracing::instrument(skip(self, room_id, user_id, event))]
pub(crate) fn update_room(
&self,
room_id: Option<&RoomId>,
room_id: &RoomId,
user_id: &UserId,
event_type: RoomAccountDataEventType,
) -> Result<Option<Box<serde_json::value::RawValue>>> {
self.db.get(room_id, user_id, event_type)
event_type: &RoomAccountDataEventType,
event: &Raw<AnyRoomAccountDataEvent>,
) -> Result<()> {
self.db.update(
Some(room_id),
user_id,
&event_type.to_string(),
event.json(),
)
}
/// Returns all changes to the account data that happened after `since`.
/// Searches the global account data for a specific kind.
#[tracing::instrument(skip(self, user_id, event_type))]
pub(crate) fn get_global(
&self,
user_id: &UserId,
event_type: &GlobalAccountDataEventType,
) -> Result<Option<Raw<AnyGlobalAccountDataEvent>>> {
Ok(self
.db
.get(None, user_id, &event_type.to_string())?
.map(Raw::from_json))
}
/// Searches the room account data for a specific kind.
#[tracing::instrument(skip(self, room_id, user_id, event_type))]
pub(crate) fn get_room(
&self,
room_id: &RoomId,
user_id: &UserId,
event_type: &RoomAccountDataEventType,
) -> Result<Option<Raw<AnyRoomAccountDataEvent>>> {
Ok(self
.db
.get(Some(room_id), user_id, &event_type.to_string())?
.map(Raw::from_json))
}
/// Returns all changes to global account data that happened after `since`.
///
/// When there have been multiple changes to the same event type, returned
/// map contains the most recent value.
#[tracing::instrument(skip(self, user_id, since))]
pub(crate) fn global_changes_since(
&self,
user_id: &UserId,
since: u64,
) -> Result<
HashMap<GlobalAccountDataEventType, Raw<AnyGlobalAccountDataEvent>>,
> {
Ok(self
.db
.changes_since(None, user_id, since)?
.into_iter()
.map(|(event_type, event)| {
(event_type.into(), Raw::from_json(event))
})
.collect())
}
/// Returns all changes to room account data that happened after `since`.
///
/// When there have been multiple changes to the same event type, returned
/// map contains the most recent value.
#[tracing::instrument(skip(self, room_id, user_id, since))]
pub(crate) fn changes_since(
pub(crate) fn room_changes_since(
&self,
room_id: Option<&RoomId>,
user_id: &UserId,
room_id: &RoomId,
since: u64,
) -> Result<HashMap<RoomAccountDataEventType, Raw<AnyEphemeralRoomEvent>>>
) -> Result<HashMap<RoomAccountDataEventType, Raw<AnyRoomAccountDataEvent>>>
{
self.db.changes_since(room_id, user_id, since)
Ok(self
.db
.changes_since(Some(room_id), user_id, since)?
.into_iter()
.map(|(event_type, event)| {
(event_type.into(), Raw::from_json(event))
})
.collect())
}
}