use event content in account_data service api instead of full events

This eliminates the possibility of passing an event that has a
mismatching type, reducing the space of possible invalid states.
This commit is contained in:
Olivia Lee 2025-03-23 14:45:33 -07:00
parent 66210bc32d
commit b82458a460
No known key found for this signature in database
GPG key ID: 54D568A15B9CD1F9
15 changed files with 277 additions and 240 deletions

View file

@ -2,14 +2,16 @@ use std::collections::HashMap;
use ruma::{
events::{
AnyGlobalAccountDataEvent, AnyRoomAccountDataEvent,
AnyGlobalAccountDataEvent, AnyGlobalAccountDataEventContent,
AnyRoomAccountDataEvent, AnyRoomAccountDataEventContent,
GlobalAccountDataEventType, RoomAccountDataEventType,
},
serde::Raw,
RoomId, UserId,
};
use serde::{Deserialize, Serialize};
use crate::Result;
use crate::{Error, Result};
mod data;
@ -19,6 +21,78 @@ pub(crate) struct Service {
pub(crate) db: &'static dyn Data,
}
pub(crate) fn raw_global_event_to_parts(
event: &Raw<AnyGlobalAccountDataEvent>,
) -> serde_json::Result<(
GlobalAccountDataEventType,
Raw<AnyGlobalAccountDataEventContent>,
)> {
#[derive(Deserialize)]
struct Parts {
#[serde(rename = "type")]
event_type: GlobalAccountDataEventType,
content: Raw<AnyGlobalAccountDataEventContent>,
}
let parts = event.deserialize_as::<Parts>()?;
Ok((parts.event_type, parts.content))
}
pub(crate) fn raw_global_event_from_parts(
event_type: &GlobalAccountDataEventType,
content: &Raw<AnyGlobalAccountDataEventContent>,
) -> Raw<AnyGlobalAccountDataEvent> {
#[derive(Serialize)]
struct Parts<'a> {
#[serde(rename = "type")]
event_type: &'a GlobalAccountDataEventType,
content: &'a Raw<AnyGlobalAccountDataEventContent>,
}
Raw::new(&Parts {
event_type,
content,
})
.expect("json serialization should always succeed")
.cast::<AnyGlobalAccountDataEvent>()
}
pub(crate) fn raw_room_event_to_parts(
event: &Raw<AnyRoomAccountDataEvent>,
) -> serde_json::Result<(
RoomAccountDataEventType,
Raw<AnyRoomAccountDataEventContent>,
)> {
#[derive(Deserialize)]
struct Parts {
#[serde(rename = "type")]
event_type: RoomAccountDataEventType,
content: Raw<AnyRoomAccountDataEventContent>,
}
let parts = event.deserialize_as::<Parts>()?;
Ok((parts.event_type, parts.content))
}
pub(crate) fn raw_room_event_from_parts(
event_type: &RoomAccountDataEventType,
content: &Raw<AnyRoomAccountDataEventContent>,
) -> Raw<AnyRoomAccountDataEvent> {
#[derive(Serialize)]
struct Parts<'a> {
#[serde(rename = "type")]
event_type: &'a RoomAccountDataEventType,
content: &'a Raw<AnyRoomAccountDataEventContent>,
}
Raw::new(&Parts {
event_type,
content,
})
.expect("json serialization should always succeed")
.cast::<AnyRoomAccountDataEvent>()
}
impl Service {
pub(crate) fn new(db: &'static dyn Data) -> Self {
Self {
@ -28,26 +102,28 @@ impl Service {
/// Places one event in the global account data of the user and removes the
/// previous entry.
#[tracing::instrument(skip(self, user_id, event))]
#[tracing::instrument(skip(self, user_id, content))]
pub(crate) fn update_global(
&self,
user_id: &UserId,
event_type: &GlobalAccountDataEventType,
event: &Raw<AnyGlobalAccountDataEvent>,
content: &Raw<AnyGlobalAccountDataEventContent>,
) -> Result<()> {
let event = raw_global_event_from_parts(event_type, content);
self.db.update(None, user_id, &event_type.to_string(), event.json())
}
/// 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))]
#[tracing::instrument(skip(self, room_id, user_id, content))]
pub(crate) fn update_room(
&self,
room_id: &RoomId,
user_id: &UserId,
event_type: &RoomAccountDataEventType,
event: &Raw<AnyRoomAccountDataEvent>,
content: &Raw<AnyRoomAccountDataEventContent>,
) -> Result<()> {
let event = raw_room_event_from_parts(event_type, content);
self.db.update(
Some(room_id),
user_id,
@ -62,11 +138,17 @@ impl Service {
&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))
) -> Result<Option<Raw<AnyGlobalAccountDataEventContent>>> {
let Some(event) =
self.db.get(None, user_id, &event_type.to_string())?
else {
return Ok(None);
};
let event = Raw::<AnyGlobalAccountDataEvent>::from_json(event);
let (_, content) = raw_global_event_to_parts(&event).map_err(|_| {
Error::bad_database("Invalid account data event in db.")
})?;
Ok(Some(content))
}
/// Searches the room account data for a specific kind.
@ -76,11 +158,17 @@ impl Service {
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))
) -> Result<Option<Raw<AnyRoomAccountDataEventContent>>> {
let Some(event) =
self.db.get(Some(room_id), user_id, &event_type.to_string())?
else {
return Ok(None);
};
let event = Raw::<AnyRoomAccountDataEvent>::from_json(event);
let (_, content) = raw_room_event_to_parts(&event).map_err(|_| {
Error::bad_database("Invalid account data event in db.")
})?;
Ok(Some(content))
}
/// Returns all changes to global account data that happened after `since`.
@ -93,16 +181,21 @@ impl Service {
user_id: &UserId,
since: u64,
) -> Result<
HashMap<GlobalAccountDataEventType, Raw<AnyGlobalAccountDataEvent>>,
HashMap<
GlobalAccountDataEventType,
Raw<AnyGlobalAccountDataEventContent>,
>,
> {
Ok(self
.db
self.db
.changes_since(None, user_id, since)?
.into_iter()
.map(|(event_type, event)| {
(event_type.into(), Raw::from_json(event))
.into_values()
.map(|event| {
let event = Raw::<AnyGlobalAccountDataEvent>::from_json(event);
raw_global_event_to_parts(&event).map_err(|_| {
Error::bad_database("Invalid account data event in db")
})
})
.collect())
.collect()
}
/// Returns all changes to room account data that happened after `since`.
@ -115,15 +208,18 @@ impl Service {
user_id: &UserId,
room_id: &RoomId,
since: u64,
) -> Result<HashMap<RoomAccountDataEventType, Raw<AnyRoomAccountDataEvent>>>
{
Ok(self
.db
) -> Result<
HashMap<RoomAccountDataEventType, Raw<AnyRoomAccountDataEventContent>>,
> {
self.db
.changes_since(Some(room_id), user_id, since)?
.into_iter()
.map(|(event_type, event)| {
(event_type.into(), Raw::from_json(event))
.into_values()
.map(|event| {
let event = Raw::<AnyRoomAccountDataEvent>::from_json(event);
raw_room_event_to_parts(&event).map_err(|_| {
Error::bad_database("Invalid account data event in db")
})
})
.collect())
.collect()
}
}