use std::fmt;
use sew_proto::e2ee::{CallKeysMessage, ReadyCallKeys};
use crate::bridge::KeyEvent;
use crate::ids::{
Consent, Epoch, FenceId, LkIdentity, MemberId, Millis, RecordingId, RequestId, SignId, Slot,
};
use crate::wire::{ApiFailure, KeyReply, KeyRequest};
#[non_exhaustive]
pub enum Input {
Server(CallKeysMessage),
Reply(RequestId, Result<KeyReply, ApiFailure>),
Installed {
fence: FenceId,
},
DeviceSignature {
request: SignId,
sig: [u8; 64],
},
GatewayResumed,
GatewayReidentified {
gateway_session: String,
},
Ready(ReadyCallKeys),
Recording {
recording_id: Option<RecordingId>,
my_consent: Consent,
},
SdkMissingKey {
identity: LkIdentity,
since: Millis,
},
SdkUndecryptable {
identity: LkIdentity,
since: Millis,
failures: u32,
reason: UndecryptReason,
missing_index: Option<Slot>,
},
SdkDecryptOk {
identity: LkIdentity,
},
SenderSlotObserved {
observed: Slot,
},
EngineMisbehaviour {
kind: EngineMisbehaviour,
identity: Option<LkIdentity>,
},
MediaParticipants(Vec<LkIdentity>),
VoiceMembers(Vec<MemberId>),
}
impl fmt::Debug for Input {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Input::Server(m) => write!(f, "Server({})", m.kind()),
Input::Reply(id, r) => write!(
f,
"Reply({id}, {})",
match r {
Ok(k) => k.kind(),
Err(_) => "failure",
}
),
Input::Installed { fence } => write!(f, "Installed({fence})"),
Input::DeviceSignature { request, .. } => write!(f, "DeviceSignature({request})"),
Input::GatewayResumed => f.write_str("GatewayResumed"),
Input::GatewayReidentified { .. } => f.write_str("GatewayReidentified"),
Input::Ready(_) => f.write_str("Ready"),
Input::Recording {
recording_id,
my_consent,
} => write!(f, "Recording({recording_id:?}, {my_consent:?})"),
Input::SdkMissingKey { identity, since } => {
write!(f, "SdkMissingKey({identity}, {since})")
}
Input::SdkUndecryptable {
identity, reason, ..
} => write!(f, "SdkUndecryptable({identity}, {reason:?})"),
Input::SdkDecryptOk { identity } => write!(f, "SdkDecryptOk({identity})"),
Input::SenderSlotObserved { observed } => write!(f, "SenderSlotObserved({observed})"),
Input::EngineMisbehaviour { kind, .. } => write!(f, "EngineMisbehaviour({kind:?})"),
Input::MediaParticipants(v) => write!(f, "MediaParticipants({})", v.len()),
Input::VoiceMembers(v) => write!(f, "VoiceMembers({})", v.len()),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum UndecryptReason {
InvalidKey,
MissingKey,
}
impl UndecryptReason {
pub fn as_str(self) -> &'static str {
match self {
UndecryptReason::InvalidKey => "invalid_key",
UndecryptReason::MissingKey => "missing_key",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum EngineMisbehaviour {
UnencryptedTrack,
SifTrailer,
EncryptionDowngrade,
}
#[non_exhaustive]
pub enum Output {
Send(RequestId, KeyRequest),
Key(KeyEvent),
NeedDeviceSignature {
request: SignId,
message: Vec<u8>,
},
CheckSenderSlot {
expected: Slot,
},
Status(E2eeStatus),
Roster,
}
impl fmt::Debug for Output {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Output::Send(id, r) => write!(f, "Send({id}, {})", r.kind()),
Output::Key(ev) => write!(f, "Key({} {:?} {:?})", ev.kind(), ev.identity(), ev.slot()),
Output::NeedDeviceSignature { request, .. } => {
write!(f, "NeedDeviceSignature({request})")
}
Output::CheckSenderSlot { expected } => write!(f, "CheckSenderSlot({expected})"),
Output::Status(s) => write!(f, "Status({s:?})"),
Output::Roster => f.write_str("Roster"),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum E2eeStatus {
Preparing,
Joining {
since: Millis,
},
Active {
epoch: Epoch,
since: Millis,
},
Securing {
since: Millis,
show_after_ms: u64,
},
Recovering {
reason: &'static str,
since: Millis,
},
DeviceNotEnrolled,
ServerIdentityChanged,
Unsupported {
reason: String,
},
Failed {
reason: &'static str,
},
Left {
reason: LeftReason,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum LeftReason {
Left,
Removed(sew_proto::e2ee::RemoveReason),
Failed(&'static str),
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum SessionState {
Joining,
Active {
epoch: Epoch,
transition: Transition,
},
Resyncing {
reason: &'static str,
since: Millis,
},
Resetting {
new_group_id: Vec<u8>,
since: Millis,
},
Left {
reason: LeftReason,
},
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum Transition {
Idle,
Committing {
epoch_from: Epoch,
},
Installed {
epoch: Epoch,
fence: FenceId,
},
Ready {
epoch: Epoch,
since: Millis,
},
}