use jmap_scene_types::{
ObjectEventKind, SCENE_EVENT_TYPES, SceneAvatar, SceneEvent, SceneInteractionEvent,
SceneObject, SceneStreamEnable,
};
use jmap_types::{Id, JmapError};
use crate::SceneBackend;
use crate::helpers::is_visible;
use crate::region::JMAP_CHAT_URI;
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RegionScope {
All,
Only(Vec<Id>),
}
impl RegionScope {
pub fn covers(&self, region_id: &Id) -> bool {
match self {
RegionScope::All => true,
RegionScope::Only(ids) => ids.contains(region_id),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SceneStreamFilter {
region_ids: RegionScope,
event_types: Option<Vec<&'static str>>,
}
impl SceneStreamFilter {
pub fn region_ids(&self) -> &RegionScope {
&self.region_ids
}
pub fn includes(&self, event_type: &str) -> bool {
self.event_types
.as_ref()
.is_none_or(|types| types.contains(&event_type))
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct SceneEphemeralSubscription {
active: Option<SceneStreamFilter>,
}
impl SceneEphemeralSubscription {
pub fn replace_with(&mut self, enable: &SceneStreamEnable) -> Result<(), JmapError> {
let event_types = match &enable.event_types {
None => None,
Some(requested) => {
let recognized: Vec<&'static str> = SCENE_EVENT_TYPES
.iter()
.copied()
.filter(|t| requested.iter().any(|r| r.as_str() == *t))
.collect();
if recognized.is_empty() {
return Err(JmapError::invalid_arguments(
"eventTypes contains no recognized values",
));
}
Some(recognized)
}
};
self.active = Some(SceneStreamFilter {
region_ids: enable
.region_ids
.clone()
.map_or(RegionScope::All, RegionScope::Only),
event_types,
});
Ok(())
}
pub fn filter(&self) -> Option<&SceneStreamFilter> {
self.active.as_ref()
}
}
pub async fn should_deliver<B: SceneBackend>(
backend: &B,
caller: &B::CallerCtx,
hosting_account_id: &Id,
recipient_account_id: &Id,
subscription: &SceneEphemeralSubscription,
event: &SceneEvent,
) -> Result<bool, B::Error> {
let Some(filter) = subscription.filter() else {
return Ok(false);
};
if !filter.includes(event.type_name()) {
return Ok(false);
}
let (event_account, region_id, object, origin) = match event {
SceneEvent::Avatar(e) => (&e.account_id, &e.region_id, None, Some(e.user_id.as_str())),
SceneEvent::Object(e) => (
&e.account_id,
&e.region_id,
Some((&e.object_id, e.event == ObjectEventKind::Destroyed)),
e.updated_by.as_deref(),
),
SceneEvent::Interaction(e) => (
&e.account_id,
&e.region_id,
Some((&e.object_id, false)),
Some(e.user_id.as_str()),
),
_ => return Ok(false),
};
if event_account != hosting_account_id
|| !filter.region_ids().covers(region_id)
|| !backend
.may_access_region(caller, hosting_account_id, region_id)
.await?
{
return Ok(false);
}
let me = backend.caller_user_id(caller, hosting_account_id).await?;
if !has_active_avatar(backend, caller, hosting_account_id, &me, region_id).await? {
return Ok(false);
}
if matches!(event, SceneEvent::Avatar(_))
&& !backend
.may_see_region_avatars(caller, hosting_account_id, region_id)
.await?
{
return Ok(false);
}
if let Some((object_id, destroyed)) = object {
let visible = match fetch_object(backend, caller, hosting_account_id, object_id).await? {
Some(o) => {
o.region_id == *region_id
&& is_visible(backend, caller, hosting_account_id, &o).await?
}
None => destroyed,
};
if !visible {
return Ok(false);
}
}
Ok(match origin {
Some(user) if backend.has_capability(caller, recipient_account_id, JMAP_CHAT_URI) => {
!backend.is_user_blocked(caller, user).await?
}
_ => true,
})
}
pub async fn validate_interaction<B: SceneBackend>(
backend: &B,
caller: &B::CallerCtx,
account_id: &Id,
event: &SceneInteractionEvent,
) -> Result<Option<SceneInteractionEvent>, B::Error> {
if event.account_id != *account_id {
return Ok(None);
}
let me = backend.caller_user_id(caller, account_id).await?;
if !has_active_avatar(backend, caller, account_id, &me, &event.region_id).await? {
return Ok(None);
}
let interactable = fetch_object(backend, caller, account_id, &event.object_id)
.await?
.is_some_and(|o| o.region_id == event.region_id && o.interactable);
Ok(interactable.then(|| {
let mut relayed = event.clone();
relayed.user_id = me;
relayed
}))
}
pub(crate) async fn has_active_avatar<B: SceneBackend>(
backend: &B,
caller: &B::CallerCtx,
account_id: &Id,
user_id: &str,
region_id: &Id,
) -> Result<bool, B::Error> {
let (avatars, _) = backend
.get_objects::<SceneAvatar>(caller, account_id, None, None)
.await?;
Ok(avatars
.iter()
.any(|a| a.region_id == *region_id && a.user_id == user_id && a.left_at.is_none()))
}
async fn fetch_object<B: SceneBackend>(
backend: &B,
caller: &B::CallerCtx,
account_id: &Id,
object_id: &Id,
) -> Result<Option<SceneObject>, B::Error> {
let (objects, _) = backend
.get_objects::<SceneObject>(
caller,
account_id,
Some(std::slice::from_ref(object_id)),
None,
)
.await?;
Ok(objects.into_iter().next())
}