use web_time::SystemTime;
use crate::{
BinPtr, EventId, ObjectId, Query, QueryId, Session, SessionRef, SessionToken, TypeId,
Updatedness,
};
use std::{
collections::{BTreeMap, HashMap, HashSet},
sync::Arc,
};
#[derive(
Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, serde::Deserialize, serde::Serialize,
)]
pub struct RequestId(pub u64);
#[derive(Debug, serde::Deserialize, serde::Serialize)]
pub struct ClientMessage {
pub request_id: RequestId,
pub request: Arc<Request>,
}
#[derive(Debug, serde::Deserialize, serde::Serialize)]
pub enum Request {
SetToken(SessionToken),
RenameSession(String),
CurrentSession,
ListSessions,
Logout,
DisconnectSession(SessionRef),
GetTime,
Get {
object_ids: HashMap<ObjectId, Option<Updatedness>>,
subscribe: bool,
},
AlreadyHave {
object_ids: HashMap<ObjectId, Updatedness>,
},
Query {
query_id: QueryId,
type_id: TypeId,
query: Arc<Query>,
only_updated_since: Option<Updatedness>,
subscribe: bool,
},
GetBinaries(HashSet<BinPtr>),
Unsubscribe(HashSet<ObjectId>),
UnsubscribeQuery(QueryId),
Upload(Upload),
UploadBinaries(usize), }
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
pub enum Upload {
Object {
object_id: ObjectId,
type_id: TypeId,
created_at: EventId,
snapshot_version: i32,
object: Arc<serde_json::Value>,
subscribe: bool,
},
Event {
object_id: ObjectId,
type_id: TypeId,
event_id: EventId,
event: Arc<serde_json::Value>,
subscribe: bool,
},
}
#[derive(Debug, serde::Deserialize, serde::Serialize)]
pub enum ServerMessage {
Response {
request_id: RequestId,
response: ResponsePart,
last_response: bool,
},
Updates(Updates),
}
#[derive(Debug, serde::Deserialize, serde::Serialize)]
pub struct Updates {
pub data: Vec<Arc<Update>>,
pub now_have_all_until: Updatedness,
}
#[derive(Debug, serde::Deserialize, serde::Serialize)]
pub enum ResponsePart {
Success,
Error(crate::SerializableError),
Sessions(Vec<Session>),
CurrentTime(SystemTime),
Objects {
data: Vec<MaybeObject>,
now_have_all_until: Option<Updatedness>,
},
Binaries(usize),
}
#[derive(Debug, serde::Deserialize, serde::Serialize)]
pub enum MaybeObject {
AlreadySubscribed(ObjectId),
NotYetSubscribed(ObjectData),
}
#[derive(Debug, serde::Deserialize, serde::Serialize)]
pub struct ObjectData {
pub object_id: ObjectId,
pub type_id: TypeId,
pub creation_snapshot: Option<(EventId, i32, Arc<serde_json::Value>)>,
pub events: BTreeMap<EventId, Arc<serde_json::Value>>,
pub now_have_all_until: Updatedness,
}
impl ObjectData {
pub fn into_updates(self) -> Vec<Arc<Update>> {
let mut res =
Vec::with_capacity(self.events.len() + self.creation_snapshot.is_some() as usize);
if let Some((created_at, snapshot_version, data)) = self.creation_snapshot {
res.push(Arc::new(Update {
object_id: self.object_id,
data: UpdateData::Creation {
type_id: self.type_id,
created_at,
snapshot_version,
data,
},
}));
}
for (event_id, data) in self.events.into_iter() {
res.push(Arc::new(Update {
object_id: self.object_id,
data: UpdateData::Event {
type_id: self.type_id,
event_id,
data,
},
}));
}
res
}
}
#[derive(Debug, serde::Deserialize, serde::Serialize)]
pub struct Update {
pub object_id: ObjectId,
pub data: UpdateData,
}
#[derive(Debug, serde::Deserialize, serde::Serialize)]
pub enum UpdateData {
Creation {
type_id: TypeId,
created_at: EventId,
snapshot_version: i32,
data: Arc<serde_json::Value>,
},
Event {
type_id: TypeId,
event_id: EventId,
data: Arc<serde_json::Value>,
},
LostReadRights,
}