use jmap_server::{BackendSetError, JmapBackend, JmapObject, SetObject};
use jmap_scene_types::{
Orientation, Position, SceneAsset, SceneAvatar, SceneCapability, SceneInteractionEvent,
SceneObject, SceneRegion, SceneRegionEvent,
};
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum BindingProperty {
ChatId,
SpaceId,
ChannelId,
ActiveCallId,
}
impl BindingProperty {
pub fn as_str(self) -> &'static str {
match self {
Self::ChatId => "chatId",
Self::SpaceId => "spaceId",
Self::ChannelId => "channelId",
Self::ActiveCallId => "activeCallId",
}
}
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BindingTarget {
Usable,
Missing,
Inaccessible,
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Default)]
pub enum AvatarView {
#[default]
Full,
Redacted(AvatarRedaction),
Hidden,
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Default)]
pub struct AvatarRedaction {
pub position: Option<Position>,
pub orientation: Option<Orientation>,
pub world_state_keys: Vec<String>,
pub region_state_keys: Vec<String>,
}
impl AvatarRedaction {
pub fn apply(&self, avatar: &mut SceneAvatar) {
if let Some(p) = self.position {
avatar.position = p;
}
if let Some(o) = self.orientation {
avatar.orientation = o;
}
if let Some(ws) = &mut avatar.world_state {
for k in &self.world_state_keys {
ws.remove(k);
}
}
if let Some(Some(rs)) = &mut avatar.region_state {
for k in &self.region_state_keys {
rs.remove(k);
}
}
}
pub fn withheld(&self) -> Vec<&'static str> {
[
("position", self.position.is_some()),
("orientation", self.orientation.is_some()),
("worldState", !self.world_state_keys.is_empty()),
("regionState", !self.region_state_keys.is_empty()),
]
.into_iter()
.filter_map(|(name, on)| on.then_some(name))
.collect()
}
}
pub trait SceneBackend: JmapBackend {
fn scene_capability(
&self,
caller: &Self::CallerCtx,
account_id: &jmap_types::Id,
) -> impl std::future::Future<Output = Result<SceneCapability, Self::Error>> + Send;
fn blob_size(
&self,
caller: &Self::CallerCtx,
account_id: &jmap_types::Id,
blob_id: &jmap_types::Id,
) -> impl std::future::Future<Output = Result<Option<u64>, Self::Error>> + Send;
fn create_object<O, F>(
&self,
caller: &Self::CallerCtx,
account_id: &jmap_types::Id,
create_id: &str,
build: F,
) -> impl std::future::Future<Output = Result<(jmap_types::Id, O), BackendSetError<Self::Error>>>
+ Send
where
O: SetObject + Send + Sync,
F: FnOnce(&jmap_types::Id) -> O + Send;
fn update_object<O: SetObject + Send + Sync>(
&self,
caller: &Self::CallerCtx,
account_id: &jmap_types::Id,
id: &jmap_types::Id,
patch: O::Patch,
) -> impl std::future::Future<Output = Result<Option<O>, BackendSetError<Self::Error>>> + Send;
fn destroy_object<O: SetObject + Send + Sync>(
&self,
caller: &Self::CallerCtx,
account_id: &jmap_types::Id,
id: &jmap_types::Id,
) -> impl std::future::Future<Output = Result<(), BackendSetError<Self::Error>>> + Send;
fn may_modify_asset(
&self,
caller: &Self::CallerCtx,
account_id: &jmap_types::Id,
asset: &SceneAsset,
) -> impl std::future::Future<Output = Result<bool, Self::Error>> + Send {
let _ = (caller, account_id, asset);
async { Ok(true) }
}
fn has_capability(
&self,
caller: &Self::CallerCtx,
account_id: &jmap_types::Id,
uri: &str,
) -> bool;
fn region_binding(
&self,
caller: &Self::CallerCtx,
account_id: &jmap_types::Id,
property: BindingProperty,
id: &jmap_types::Id,
) -> impl std::future::Future<Output = Result<BindingTarget, Self::Error>> + Send {
let _ = (caller, account_id, property, id);
async { Ok(BindingTarget::Usable) }
}
fn channel_in_space(
&self,
caller: &Self::CallerCtx,
account_id: &jmap_types::Id,
channel_id: &jmap_types::Id,
space_id: &jmap_types::Id,
) -> impl std::future::Future<Output = Result<bool, Self::Error>> + Send {
let _ = (caller, account_id, channel_id, space_id);
async { Ok(true) }
}
fn may_update_region(
&self,
caller: &Self::CallerCtx,
account_id: &jmap_types::Id,
region: &SceneRegion,
) -> impl std::future::Future<Output = Result<bool, Self::Error>> + Send {
let _ = (caller, account_id, region);
async { Ok(true) }
}
fn may_destroy_region(
&self,
caller: &Self::CallerCtx,
account_id: &jmap_types::Id,
region: &SceneRegion,
) -> impl std::future::Future<Output = Result<bool, Self::Error>> + Send {
let _ = (caller, account_id, region);
async { Ok(true) }
}
fn may_modify_object(
&self,
caller: &Self::CallerCtx,
account_id: &jmap_types::Id,
object: &SceneObject,
) -> impl std::future::Future<Output = Result<bool, Self::Error>> + Send {
let _ = (caller, account_id, object);
async { Ok(true) }
}
fn caller_user_id(
&self,
caller: &Self::CallerCtx,
account_id: &jmap_types::Id,
) -> impl std::future::Future<Output = Result<String, Self::Error>> + Send;
fn avatar_display_name(
&self,
caller: &Self::CallerCtx,
account_id: &jmap_types::Id,
user_id: &str,
) -> impl std::future::Future<Output = Result<String, Self::Error>> + Send {
let _ = (caller, account_id);
let name = user_id.to_owned();
async { Ok(name) }
}
fn reserved_display_names(
&self,
caller: &Self::CallerCtx,
account_id: &jmap_types::Id,
) -> impl std::future::Future<Output = Result<Vec<String>, Self::Error>> + Send {
let _ = (caller, account_id);
async { Ok(Vec::new()) }
}
fn may_enter_region(
&self,
caller: &Self::CallerCtx,
account_id: &jmap_types::Id,
region: &SceneRegion,
) -> impl std::future::Future<Output = Result<bool, Self::Error>> + Send {
let _ = (caller, account_id, region);
async { Ok(true) }
}
fn may_eject_avatar(
&self,
caller: &Self::CallerCtx,
account_id: &jmap_types::Id,
avatar: &SceneAvatar,
) -> impl std::future::Future<Output = Result<bool, Self::Error>> + Send {
let _ = (caller, account_id, avatar);
async { Ok(true) }
}
fn may_access_region(
&self,
caller: &Self::CallerCtx,
account_id: &jmap_types::Id,
region_id: &jmap_types::Id,
) -> impl std::future::Future<Output = Result<bool, Self::Error>> + Send {
let _ = (caller, account_id, region_id);
async { Ok(true) }
}
fn is_object_visible(
&self,
caller: &Self::CallerCtx,
account_id: &jmap_types::Id,
object: &SceneObject,
) -> impl std::future::Future<Output = Result<bool, Self::Error>> + Send {
let _ = (caller, account_id, object);
async { Ok(true) }
}
fn is_user_blocked(
&self,
caller: &Self::CallerCtx,
user_id: &str,
) -> impl std::future::Future<Output = Result<bool, Self::Error>> + Send {
let _ = (caller, user_id);
async { Ok(false) }
}
fn may_see_region_avatars(
&self,
caller: &Self::CallerCtx,
account_id: &jmap_types::Id,
region_id: &jmap_types::Id,
) -> impl std::future::Future<Output = Result<bool, Self::Error>> + Send {
let _ = (caller, account_id, region_id);
async { Ok(true) }
}
fn avatar_view(
&self,
caller: &Self::CallerCtx,
account_id: &jmap_types::Id,
avatar: &SceneAvatar,
) -> impl std::future::Future<Output = Result<AvatarView, Self::Error>> + Send {
let _ = (caller, account_id, avatar);
async { Ok(AvatarView::Full) }
}
fn supports_observe(&self, caller: &Self::CallerCtx) -> bool {
let _ = caller;
false
}
fn may_observe_region(
&self,
caller: &Self::CallerCtx,
account_id: &jmap_types::Id,
region: &SceneRegion,
) -> impl std::future::Future<Output = Result<bool, Self::Error>> + Send {
let _ = (caller, account_id, region);
async { Ok(true) }
}
fn observer_accounts(
&self,
caller: &Self::CallerCtx,
own_account_id: &jmap_types::Id,
) -> impl std::future::Future<Output = Result<Vec<jmap_types::Id>, Self::Error>> + Send {
let _ = caller;
let own = vec![own_account_id.clone()];
async { Ok(own) }
}
fn set_observing(
&self,
caller: &Self::CallerCtx,
account_id: &jmap_types::Id,
region_id: &jmap_types::Id,
observing: bool,
) -> impl std::future::Future<Output = Result<(), Self::Error>> + Send {
let _ = (caller, account_id, region_id, observing);
async { Ok(()) }
}
fn accept_interaction(
&self,
caller: &Self::CallerCtx,
account_id: &jmap_types::Id,
event: &SceneInteractionEvent,
) -> impl std::future::Future<Output = Result<bool, Self::Error>> + Send {
let _ = (caller, account_id, event);
async { Ok(true) }
}
fn accept_region_event(
&self,
caller: &Self::CallerCtx,
account_id: &jmap_types::Id,
event: &SceneRegionEvent,
) -> impl std::future::Future<Output = Result<bool, Self::Error>> + Send {
let _ = (caller, account_id, event);
async { Ok(true) }
}
fn supports_type<O: JmapObject>(
&self,
caller: &Self::CallerCtx,
account_id: &jmap_types::Id,
) -> bool;
}