mirror of
https://github.com/Blah-IM/blahrs.git
synced 2025-05-01 00:31:09 +00:00
feat(types): add optional schemars support
This commit is contained in:
parent
ee85112fb6
commit
ea69062a6d
7 changed files with 134 additions and 2 deletions
43
Cargo.lock
generated
43
Cargo.lock
generated
|
@ -273,6 +273,7 @@ dependencies = [
|
||||||
"mock_instant",
|
"mock_instant",
|
||||||
"rand",
|
"rand",
|
||||||
"rusqlite",
|
"rusqlite",
|
||||||
|
"schemars",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_jcs",
|
"serde_jcs",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
|
@ -697,6 +698,12 @@ version = "1.0.9"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "59f8e79d1fbf76bdfbde321e902714bf6c49df88a7dda6fc682fc2979226962d"
|
checksum = "59f8e79d1fbf76bdfbde321e902714bf6c49df88a7dda6fc682fc2979226962d"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "dyn-clone"
|
||||||
|
version = "1.0.17"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "0d6ef0072f8a535281e4876be788938b528e9a1d43900b82c2569af7da799125"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "ed25519"
|
name = "ed25519"
|
||||||
version = "2.2.3"
|
version = "2.2.3"
|
||||||
|
@ -1931,6 +1938,31 @@ dependencies = [
|
||||||
"windows-sys 0.59.0",
|
"windows-sys 0.59.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "schemars"
|
||||||
|
version = "0.8.21"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "09c024468a378b7e36765cd36702b7a90cc3cba11654f6685c8f233408e89e92"
|
||||||
|
dependencies = [
|
||||||
|
"dyn-clone",
|
||||||
|
"schemars_derive",
|
||||||
|
"serde",
|
||||||
|
"serde_json",
|
||||||
|
"url",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "schemars_derive"
|
||||||
|
version = "0.8.21"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b1eee588578aff73f856ab961cd2f79e36bc45d7ded33a7562adba4667aecc0e"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"serde_derive_internals",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "scopeguard"
|
name = "scopeguard"
|
||||||
version = "1.2.0"
|
version = "1.2.0"
|
||||||
|
@ -2012,6 +2044,17 @@ dependencies = [
|
||||||
"syn",
|
"syn",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "serde_derive_internals"
|
||||||
|
version = "0.29.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "18d26a20a969b9e3fdf2fc2d9f21eda6c40e2de84c9408bb5d3b05d499aae711"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "serde_jcs"
|
name = "serde_jcs"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
|
|
@ -6,6 +6,7 @@ edition = "2021"
|
||||||
[features]
|
[features]
|
||||||
default = []
|
default = []
|
||||||
unsafe_use_mock_instant_for_testing = ["dep:mock_instant"]
|
unsafe_use_mock_instant_for_testing = ["dep:mock_instant"]
|
||||||
|
schemars = ["dep:schemars"]
|
||||||
|
|
||||||
[[bench]]
|
[[bench]]
|
||||||
name = "crypto_ops"
|
name = "crypto_ops"
|
||||||
|
@ -27,6 +28,11 @@ serde_with = "3"
|
||||||
thiserror = "1"
|
thiserror = "1"
|
||||||
url = { version = "2", features = ["serde"] }
|
url = { version = "2", features = ["serde"] }
|
||||||
|
|
||||||
|
[dependencies.schemars]
|
||||||
|
version = "0.8"
|
||||||
|
optional = true
|
||||||
|
features = ["url"]
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
criterion = "0.5"
|
criterion = "0.5"
|
||||||
ed25519-dalek = { version = "2", features = ["rand_core"] }
|
ed25519-dalek = { version = "2", features = ["rand_core"] }
|
||||||
|
|
|
@ -12,6 +12,7 @@ use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
/// User pubkey pair to uniquely identity a user.
|
/// User pubkey pair to uniquely identity a user.
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
|
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
|
||||||
pub struct UserKey {
|
pub struct UserKey {
|
||||||
/// The identity key (`id_key`).
|
/// The identity key (`id_key`).
|
||||||
pub id_key: PubKey,
|
pub id_key: PubKey,
|
||||||
|
@ -24,6 +25,8 @@ pub struct UserKey {
|
||||||
#[serde(transparent)]
|
#[serde(transparent)]
|
||||||
pub struct PubKey(#[serde(with = "hex::serde")] pub [u8; PUBLIC_KEY_LENGTH]);
|
pub struct PubKey(#[serde(with = "hex::serde")] pub [u8; PUBLIC_KEY_LENGTH]);
|
||||||
|
|
||||||
|
impl_json_schema_as!(PubKey => String);
|
||||||
|
|
||||||
impl FromStr for PubKey {
|
impl FromStr for PubKey {
|
||||||
type Err = hex::FromHexError;
|
type Err = hex::FromHexError;
|
||||||
|
|
||||||
|
@ -59,14 +62,21 @@ impl From<&VerifyingKey> for PubKey {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
|
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
|
||||||
#[serde(deny_unknown_fields)]
|
#[serde(deny_unknown_fields)]
|
||||||
pub struct Signed<T> {
|
pub struct Signed<T> {
|
||||||
#[serde(with = "hex::serde")]
|
// Workaround: https://github.com/GREsau/schemars/issues/89
|
||||||
|
#[serde(
|
||||||
|
serialize_with = "hex::serde::serialize",
|
||||||
|
deserialize_with = "hex::serde::deserialize"
|
||||||
|
)]
|
||||||
|
#[cfg_attr(feature = "schemars", schemars(with = "String"))]
|
||||||
pub sig: [u8; SIGNATURE_LENGTH],
|
pub sig: [u8; SIGNATURE_LENGTH],
|
||||||
pub signee: Signee<T>,
|
pub signee: Signee<T>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
|
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
|
||||||
#[serde(deny_unknown_fields)]
|
#[serde(deny_unknown_fields)]
|
||||||
pub struct Signee<T> {
|
pub struct Signee<T> {
|
||||||
pub nonce: u32,
|
pub nonce: u32,
|
||||||
|
|
|
@ -12,6 +12,7 @@ use crate::{PubKey, Signed};
|
||||||
/// User identity description structure.
|
/// User identity description structure.
|
||||||
// TODO: Revise and shrink duplicates (pubkey fields).
|
// TODO: Revise and shrink duplicates (pubkey fields).
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
|
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
|
||||||
pub struct UserIdentityDesc {
|
pub struct UserIdentityDesc {
|
||||||
/// User primary identity key, only for signing action keys.
|
/// User primary identity key, only for signing action keys.
|
||||||
pub id_key: PubKey,
|
pub id_key: PubKey,
|
||||||
|
@ -93,6 +94,7 @@ impl UserIdentityDesc {
|
||||||
|
|
||||||
/// Description of an action key.
|
/// Description of an action key.
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
|
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
|
||||||
#[serde(tag = "typ", rename = "user_act_key")]
|
#[serde(tag = "typ", rename = "user_act_key")]
|
||||||
pub struct UserActKeyDesc {
|
pub struct UserActKeyDesc {
|
||||||
/// Per-device action key for signing msgs.
|
/// Per-device action key for signing msgs.
|
||||||
|
@ -105,6 +107,7 @@ pub struct UserActKeyDesc {
|
||||||
|
|
||||||
/// User profile describing their non-cryptographic metadata.
|
/// User profile describing their non-cryptographic metadata.
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
|
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
|
||||||
#[serde(tag = "typ", rename = "user_profile")]
|
#[serde(tag = "typ", rename = "user_profile")]
|
||||||
pub struct UserProfile {
|
pub struct UserProfile {
|
||||||
/// Preferred chat servers ordered by decreasing preference, for starting private chats.
|
/// Preferred chat servers ordered by decreasing preference, for starting private chats.
|
||||||
|
@ -122,6 +125,8 @@ pub struct UserProfile {
|
||||||
#[serde(try_from = "Url")]
|
#[serde(try_from = "Url")]
|
||||||
pub struct IdUrl(Url);
|
pub struct IdUrl(Url);
|
||||||
|
|
||||||
|
impl_json_schema_as!(IdUrl => Url);
|
||||||
|
|
||||||
impl fmt::Display for IdUrl {
|
impl fmt::Display for IdUrl {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
self.0.fmt(f)
|
self.0.fmt(f)
|
||||||
|
|
|
@ -6,6 +6,31 @@ pub use url;
|
||||||
pub use crypto::{get_timestamp, PubKey, SignExt, Signed, Signee, UserKey};
|
pub use crypto::{get_timestamp, PubKey, SignExt, Signed, Signee, UserKey};
|
||||||
pub use msg::Id;
|
pub use msg::Id;
|
||||||
|
|
||||||
|
#[cfg(not(feature = "schemars"))]
|
||||||
|
macro_rules! impl_json_schema_as {
|
||||||
|
($($tt:tt)*) => {};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Workaround: https://github.com/GREsau/schemars/issues/267
|
||||||
|
#[cfg(feature = "schemars")]
|
||||||
|
macro_rules! impl_json_schema_as {
|
||||||
|
($ty:ident => $as_ty:ty) => {
|
||||||
|
impl schemars::JsonSchema for $ty {
|
||||||
|
fn schema_name() -> String {
|
||||||
|
stringify!($ty).into()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn schema_id() -> std::borrow::Cow<'static, str> {
|
||||||
|
concat!(module_path!(), "::", stringify!($ty)).into()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
|
||||||
|
gen.subschema_for::<$as_ty>()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
pub mod crypto;
|
pub mod crypto;
|
||||||
pub mod identity;
|
pub mod identity;
|
||||||
pub mod msg;
|
pub mod msg;
|
||||||
|
|
|
@ -18,6 +18,8 @@ use crate::{PubKey, Signed};
|
||||||
#[serde(transparent)]
|
#[serde(transparent)]
|
||||||
pub struct Id(#[serde_as(as = "DisplayFromStr")] pub i64);
|
pub struct Id(#[serde_as(as = "DisplayFromStr")] pub i64);
|
||||||
|
|
||||||
|
impl_json_schema_as!(Id => String);
|
||||||
|
|
||||||
impl fmt::Display for Id {
|
impl fmt::Display for Id {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
self.0.fmt(f)
|
self.0.fmt(f)
|
||||||
|
@ -39,6 +41,7 @@ impl Id {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
|
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
|
||||||
pub struct WithMsgId<T> {
|
pub struct WithMsgId<T> {
|
||||||
pub cid: Id,
|
pub cid: Id,
|
||||||
#[serde(flatten)]
|
#[serde(flatten)]
|
||||||
|
@ -53,6 +56,7 @@ impl<T> WithMsgId<T> {
|
||||||
|
|
||||||
/// Register a user on a chat server.
|
/// Register a user on a chat server.
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
|
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
|
||||||
#[serde(tag = "typ", rename = "user_register")]
|
#[serde(tag = "typ", rename = "user_register")]
|
||||||
pub struct UserRegisterPayload {
|
pub struct UserRegisterPayload {
|
||||||
/// The normalized server URL to register on.
|
/// The normalized server URL to register on.
|
||||||
|
@ -70,6 +74,7 @@ pub struct UserRegisterPayload {
|
||||||
|
|
||||||
/// The server-specific challenge data for registration.
|
/// The server-specific challenge data for registration.
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
|
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
|
||||||
#[serde(rename_all = "snake_case")]
|
#[serde(rename_all = "snake_case")]
|
||||||
pub enum UserRegisterChallengeResponse {
|
pub enum UserRegisterChallengeResponse {
|
||||||
/// Proof of work challenge containing the same nonce from server challenge request.
|
/// Proof of work challenge containing the same nonce from server challenge request.
|
||||||
|
@ -82,6 +87,7 @@ pub enum UserRegisterChallengeResponse {
|
||||||
|
|
||||||
// FIXME: `deny_unknown_fields` breaks this.
|
// FIXME: `deny_unknown_fields` breaks this.
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
|
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
|
||||||
#[serde(tag = "typ", rename = "chat")]
|
#[serde(tag = "typ", rename = "chat")]
|
||||||
pub struct ChatPayload {
|
pub struct ChatPayload {
|
||||||
pub rich_text: RichText,
|
pub rich_text: RichText,
|
||||||
|
@ -93,6 +99,8 @@ pub struct ChatPayload {
|
||||||
#[serde(transparent)]
|
#[serde(transparent)]
|
||||||
pub struct RichText(pub Vec<RichTextPiece>);
|
pub struct RichText(pub Vec<RichTextPiece>);
|
||||||
|
|
||||||
|
impl_json_schema_as!(RichText => Vec<RichTextPieceRaw>);
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
pub struct RichTextPiece {
|
pub struct RichTextPiece {
|
||||||
pub attrs: TextAttrs,
|
pub attrs: TextAttrs,
|
||||||
|
@ -112,8 +120,9 @@ impl Serialize for RichTextPiece {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The protocol representation of `RichTextPiece`.
|
/// The representation on wire of `RichTextPiece`.
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
|
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
|
||||||
|
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
|
||||||
#[serde(untagged)]
|
#[serde(untagged)]
|
||||||
enum RichTextPieceRaw {
|
enum RichTextPieceRaw {
|
||||||
Text(String),
|
Text(String),
|
||||||
|
@ -157,6 +166,7 @@ impl<'de> Deserialize<'de> for RichText {
|
||||||
|
|
||||||
// TODO: This protocol format is quite large. Could use bitflags for database.
|
// TODO: This protocol format is quite large. Could use bitflags for database.
|
||||||
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
|
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
|
||||||
pub struct TextAttrs {
|
pub struct TextAttrs {
|
||||||
#[serde(default, rename = "b", skip_serializing_if = "is_default")]
|
#[serde(default, rename = "b", skip_serializing_if = "is_default")]
|
||||||
pub bold: bool,
|
pub bold: bool,
|
||||||
|
@ -280,6 +290,7 @@ pub type SignedChatMsg = Signed<ChatPayload>;
|
||||||
pub type SignedChatMsgWithId = WithMsgId<SignedChatMsg>;
|
pub type SignedChatMsgWithId = WithMsgId<SignedChatMsg>;
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
|
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
|
||||||
#[serde(tag = "typ")]
|
#[serde(tag = "typ")]
|
||||||
pub enum CreateRoomPayload {
|
pub enum CreateRoomPayload {
|
||||||
#[serde(rename = "create_room")]
|
#[serde(rename = "create_room")]
|
||||||
|
@ -290,6 +301,7 @@ pub enum CreateRoomPayload {
|
||||||
|
|
||||||
/// Multi-user room.
|
/// Multi-user room.
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
|
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
|
||||||
pub struct CreateGroup {
|
pub struct CreateGroup {
|
||||||
pub attrs: RoomAttrs,
|
pub attrs: RoomAttrs,
|
||||||
pub title: String,
|
pub title: String,
|
||||||
|
@ -297,11 +309,13 @@ pub struct CreateGroup {
|
||||||
|
|
||||||
/// Peer-to-peer chat room with exactly two symmetric users.
|
/// Peer-to-peer chat room with exactly two symmetric users.
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
|
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
|
||||||
pub struct CreatePeerChat {
|
pub struct CreatePeerChat {
|
||||||
pub peer: PubKey,
|
pub peer: PubKey,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
|
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
|
||||||
#[serde(tag = "typ", rename = "delete_room")]
|
#[serde(tag = "typ", rename = "delete_room")]
|
||||||
pub struct DeleteRoomPayload {
|
pub struct DeleteRoomPayload {
|
||||||
pub room: Id,
|
pub room: Id,
|
||||||
|
@ -311,6 +325,7 @@ pub struct DeleteRoomPayload {
|
||||||
/// 1. Sorted by userkeys.
|
/// 1. Sorted by userkeys.
|
||||||
/// 2. No duplicated users.
|
/// 2. No duplicated users.
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
|
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
|
||||||
|
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
|
||||||
#[serde(try_from = "Vec<RoomMember>")]
|
#[serde(try_from = "Vec<RoomMember>")]
|
||||||
pub struct RoomMemberList(pub Vec<RoomMember>);
|
pub struct RoomMemberList(pub Vec<RoomMember>);
|
||||||
|
|
||||||
|
@ -336,6 +351,7 @@ impl TryFrom<Vec<RoomMember>> for RoomMemberList {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
|
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
|
||||||
pub struct RoomMember {
|
pub struct RoomMember {
|
||||||
pub permission: MemberPermission,
|
pub permission: MemberPermission,
|
||||||
pub user: PubKey,
|
pub user: PubKey,
|
||||||
|
@ -345,11 +361,13 @@ pub struct RoomMember {
|
||||||
///
|
///
|
||||||
/// TODO: Should we use JWT here instead?
|
/// TODO: Should we use JWT here instead?
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
|
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
|
||||||
#[serde(tag = "typ", rename = "auth")]
|
#[serde(tag = "typ", rename = "auth")]
|
||||||
pub struct AuthPayload {}
|
pub struct AuthPayload {}
|
||||||
|
|
||||||
// FIXME: Remove this.
|
// FIXME: Remove this.
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
|
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
|
||||||
// `typ` is provided by `RoomAdminOp`.
|
// `typ` is provided by `RoomAdminOp`.
|
||||||
pub struct RoomAdminPayload {
|
pub struct RoomAdminPayload {
|
||||||
#[serde(flatten)]
|
#[serde(flatten)]
|
||||||
|
@ -357,6 +375,7 @@ pub struct RoomAdminPayload {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
|
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
|
||||||
#[serde(tag = "typ", rename_all = "snake_case", rename = "remove_member")]
|
#[serde(tag = "typ", rename_all = "snake_case", rename = "remove_member")]
|
||||||
pub struct RemoveMemberPayload {
|
pub struct RemoveMemberPayload {
|
||||||
pub room: Id,
|
pub room: Id,
|
||||||
|
@ -366,6 +385,7 @@ pub struct RemoveMemberPayload {
|
||||||
|
|
||||||
// TODO: Maybe disallow adding other user without consent?
|
// TODO: Maybe disallow adding other user without consent?
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
|
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
|
||||||
#[serde(tag = "typ", rename_all = "snake_case", rename = "add_member")]
|
#[serde(tag = "typ", rename_all = "snake_case", rename = "add_member")]
|
||||||
pub struct AddMemberPayload {
|
pub struct AddMemberPayload {
|
||||||
pub room: Id,
|
pub room: Id,
|
||||||
|
@ -374,6 +394,7 @@ pub struct AddMemberPayload {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
|
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
|
||||||
#[serde(tag = "typ", rename_all = "snake_case", rename = "update_member")]
|
#[serde(tag = "typ", rename_all = "snake_case", rename = "update_member")]
|
||||||
pub struct UpdateMemberPayload {
|
pub struct UpdateMemberPayload {
|
||||||
pub room: Id,
|
pub room: Id,
|
||||||
|
@ -382,6 +403,7 @@ pub struct UpdateMemberPayload {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
|
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
|
||||||
#[serde(untagged)]
|
#[serde(untagged)]
|
||||||
pub enum RoomAdminOp {
|
pub enum RoomAdminOp {
|
||||||
AddMember(AddMemberPayload),
|
AddMember(AddMemberPayload),
|
||||||
|
@ -437,6 +459,10 @@ impl_serde_for_bitflags!(ServerPermission);
|
||||||
impl_serde_for_bitflags!(MemberPermission);
|
impl_serde_for_bitflags!(MemberPermission);
|
||||||
impl_serde_for_bitflags!(RoomAttrs);
|
impl_serde_for_bitflags!(RoomAttrs);
|
||||||
|
|
||||||
|
impl_json_schema_as!(ServerPermission => i32);
|
||||||
|
impl_json_schema_as!(MemberPermission => i32);
|
||||||
|
impl_json_schema_as!(RoomAttrs => i32);
|
||||||
|
|
||||||
#[cfg(feature = "rusqlite")]
|
#[cfg(feature = "rusqlite")]
|
||||||
mod sql_impl {
|
mod sql_impl {
|
||||||
use ed25519_dalek::{VerifyingKey, PUBLIC_KEY_LENGTH};
|
use ed25519_dalek::{VerifyingKey, PUBLIC_KEY_LENGTH};
|
||||||
|
|
|
@ -10,6 +10,7 @@ use crate::PubKey;
|
||||||
|
|
||||||
/// The response object returned as body on HTTP error status.
|
/// The response object returned as body on HTTP error status.
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
|
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
|
||||||
pub struct ErrorResponse<S = String> {
|
pub struct ErrorResponse<S = String> {
|
||||||
/// The error object.
|
/// The error object.
|
||||||
pub error: ErrorObject<S>,
|
pub error: ErrorObject<S>,
|
||||||
|
@ -18,6 +19,7 @@ pub struct ErrorResponse<S = String> {
|
||||||
/// The response object of `/_blah/user/me` endpoint on HTTP error status.
|
/// The response object of `/_blah/user/me` endpoint on HTTP error status.
|
||||||
/// It contains additional registration information.
|
/// It contains additional registration information.
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
|
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
|
||||||
pub struct ErrorResponseWithChallenge<S = String> {
|
pub struct ErrorResponseWithChallenge<S = String> {
|
||||||
/// The error object.
|
/// The error object.
|
||||||
pub error: ErrorObject<S>,
|
pub error: ErrorObject<S>,
|
||||||
|
@ -29,10 +31,14 @@ pub struct ErrorResponseWithChallenge<S = String> {
|
||||||
|
|
||||||
/// The error object.
|
/// The error object.
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
|
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
|
||||||
pub struct ErrorObject<S = String> {
|
pub struct ErrorObject<S = String> {
|
||||||
/// A machine-readable error code string.
|
/// A machine-readable error code string.
|
||||||
|
#[cfg_attr(feature = "schemars", schemars(with = "String"))]
|
||||||
pub code: S,
|
pub code: S,
|
||||||
|
|
||||||
/// A human-readable error message.
|
/// A human-readable error message.
|
||||||
|
#[cfg_attr(feature = "schemars", schemars(with = "String"))]
|
||||||
pub message: S,
|
pub message: S,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -50,6 +56,7 @@ impl<S: fmt::Display + fmt::Debug> std::error::Error for ErrorObject<S> {}
|
||||||
/// It may contains extra fields and clients should ignore them for future compatibility.
|
/// It may contains extra fields and clients should ignore them for future compatibility.
|
||||||
/// Chat Servers can also include any custom fields here as long they have a `_` prefix.
|
/// Chat Servers can also include any custom fields here as long they have a `_` prefix.
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
|
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
|
||||||
pub struct ServerMetadata {
|
pub struct ServerMetadata {
|
||||||
/// A server-defined version string indicating its implementation name and the version.
|
/// A server-defined version string indicating its implementation name and the version.
|
||||||
///
|
///
|
||||||
|
@ -60,6 +67,7 @@ pub struct ServerMetadata {
|
||||||
///
|
///
|
||||||
/// It is expected to be a public accessible maybe-compressed tarball link without
|
/// It is expected to be a public accessible maybe-compressed tarball link without
|
||||||
/// access control.
|
/// access control.
|
||||||
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||||
pub src_url: Option<Url>,
|
pub src_url: Option<Url>,
|
||||||
|
|
||||||
/// The server capabilities set.
|
/// The server capabilities set.
|
||||||
|
@ -67,6 +75,7 @@ pub struct ServerMetadata {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
|
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
|
||||||
pub struct ServerCapabilities {
|
pub struct ServerCapabilities {
|
||||||
/// Whether registration is open to public.
|
/// Whether registration is open to public.
|
||||||
pub allow_public_register: bool,
|
pub allow_public_register: bool,
|
||||||
|
@ -74,6 +83,7 @@ pub struct ServerCapabilities {
|
||||||
|
|
||||||
/// Registration challenge information.
|
/// Registration challenge information.
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
|
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
|
||||||
#[serde(rename_all = "snake_case")]
|
#[serde(rename_all = "snake_case")]
|
||||||
pub enum UserRegisterChallenge {
|
pub enum UserRegisterChallenge {
|
||||||
/// Proof-of-work (PoW) challenge.
|
/// Proof-of-work (PoW) challenge.
|
||||||
|
@ -86,6 +96,7 @@ pub enum UserRegisterChallenge {
|
||||||
|
|
||||||
/// Response to list rooms.
|
/// Response to list rooms.
|
||||||
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
|
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
|
||||||
pub struct RoomList {
|
pub struct RoomList {
|
||||||
/// Result list of rooms.
|
/// Result list of rooms.
|
||||||
pub rooms: Vec<RoomMetadata>,
|
pub rooms: Vec<RoomMetadata>,
|
||||||
|
@ -96,6 +107,7 @@ pub struct RoomList {
|
||||||
|
|
||||||
/// The metadata of a room.
|
/// The metadata of a room.
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
|
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
|
||||||
pub struct RoomMetadata {
|
pub struct RoomMetadata {
|
||||||
/// Room id.
|
/// Room id.
|
||||||
pub rid: Id,
|
pub rid: Id,
|
||||||
|
@ -127,6 +139,7 @@ pub struct RoomMetadata {
|
||||||
|
|
||||||
/// Response to list room msgs.
|
/// Response to list room msgs.
|
||||||
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
|
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
|
||||||
pub struct RoomMsgs {
|
pub struct RoomMsgs {
|
||||||
/// Result list of msgs ordered in reverse of server-received time.
|
/// Result list of msgs ordered in reverse of server-received time.
|
||||||
pub msgs: Vec<SignedChatMsgWithId>,
|
pub msgs: Vec<SignedChatMsgWithId>,
|
||||||
|
@ -137,6 +150,7 @@ pub struct RoomMsgs {
|
||||||
|
|
||||||
/// Response to list room members.
|
/// Response to list room members.
|
||||||
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
|
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
|
||||||
pub struct RoomMemberList {
|
pub struct RoomMemberList {
|
||||||
/// Result list of members.
|
/// Result list of members.
|
||||||
pub members: Vec<RoomMember>,
|
pub members: Vec<RoomMember>,
|
||||||
|
@ -147,6 +161,7 @@ pub struct RoomMemberList {
|
||||||
|
|
||||||
/// The description of a room member.
|
/// The description of a room member.
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
|
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
|
||||||
pub struct RoomMember {
|
pub struct RoomMember {
|
||||||
/// The identity key of the member user.
|
/// The identity key of the member user.
|
||||||
pub id_key: PubKey,
|
pub id_key: PubKey,
|
||||||
|
@ -159,6 +174,7 @@ pub struct RoomMember {
|
||||||
|
|
||||||
/// A server-to-client event.
|
/// A server-to-client event.
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
|
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
|
||||||
#[serde(rename_all = "snake_case")]
|
#[serde(rename_all = "snake_case")]
|
||||||
pub enum ServerEvent {
|
pub enum ServerEvent {
|
||||||
/// A message from a joined room.
|
/// A message from a joined room.
|
||||||
|
@ -170,5 +186,6 @@ pub enum ServerEvent {
|
||||||
|
|
||||||
/// A client-to-server event.
|
/// A client-to-server event.
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
|
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
|
||||||
#[serde(rename_all = "snake_case")]
|
#[serde(rename_all = "snake_case")]
|
||||||
pub enum ClientEvent {}
|
pub enum ClientEvent {}
|
||||||
|
|
Loading…
Add table
Reference in a new issue