use std::borrow::Cow;
use gtk::{glib, prelude::*};
use ruma::{OwnedRoomId, OwnedUserId, RoomId, UserId};
use crate::{
session::model::VerificationKey,
utils::matrix::{MatrixIdUri, MatrixRoomIdUri},
};
#[derive(Debug)]
pub enum AppIntent {
WithSession(SessionIntent),
ShowMatrixId(MatrixIdUri),
}
impl From<SessionIntent> for AppIntent {
fn from(value: SessionIntent) -> Self {
Self::WithSession(value)
}
}
impl From<MatrixIdUri> for AppIntent {
fn from(value: MatrixIdUri) -> Self {
Self::ShowMatrixId(value)
}
}
#[derive(Debug)]
pub enum SessionIntent {
ShowRoom(ShowRoomPayload),
ShowIdentityVerification(ShowIdentityVerificationPayload),
JoinRoom(JoinRoomPayload),
ShowUser(ShowUserPayload),
}
impl SessionIntent {
pub fn with_matrix_uri(session_id: String, matrix_uri: MatrixIdUri) -> Self {
match matrix_uri {
MatrixIdUri::Room(room_uri) => Self::JoinRoom(JoinRoomPayload {
session_id,
room_uri,
}),
MatrixIdUri::User(user_id) => Self::ShowUser(ShowUserPayload {
session_id,
user_id,
}),
MatrixIdUri::Event(event_uri) => Self::JoinRoom(JoinRoomPayload {
session_id,
room_uri: event_uri.room_uri,
}),
}
}
pub fn session_id(&self) -> &str {
match self {
Self::ShowRoom(p) => &p.session_id,
Self::ShowIdentityVerification(p) => &p.session_id,
Self::JoinRoom(p) => &p.session_id,
Self::ShowUser(p) => &p.session_id,
}
}
}
#[derive(Debug)]
pub struct ShowRoomPayload {
pub session_id: String,
pub room_id: OwnedRoomId,
}
impl StaticVariantType for ShowRoomPayload {
fn static_variant_type() -> Cow<'static, glib::VariantTy> {
<(String, String)>::static_variant_type()
}
}
impl ToVariant for ShowRoomPayload {
fn to_variant(&self) -> glib::Variant {
(&self.session_id, self.room_id.as_str()).to_variant()
}
}
impl FromVariant for ShowRoomPayload {
fn from_variant(variant: &glib::Variant) -> Option<Self> {
let (session_id, room_id) = variant.get::<(String, String)>()?;
let room_id = RoomId::parse(room_id).ok()?;
Some(Self {
session_id,
room_id,
})
}
}
#[derive(Debug)]
pub struct ShowIdentityVerificationPayload {
pub session_id: String,
pub key: VerificationKey,
}
impl StaticVariantType for ShowIdentityVerificationPayload {
fn static_variant_type() -> Cow<'static, glib::VariantTy> {
<(String, String, String)>::static_variant_type()
}
}
impl ToVariant for ShowIdentityVerificationPayload {
fn to_variant(&self) -> glib::Variant {
(
&self.session_id,
self.key.user_id.as_str(),
&self.key.flow_id,
)
.to_variant()
}
}
impl FromVariant for ShowIdentityVerificationPayload {
fn from_variant(variant: &glib::Variant) -> Option<Self> {
let (session_id, user_id, flow_id) = variant.get::<(String, String, String)>()?;
let user_id = UserId::parse(user_id).ok()?;
Some(Self {
session_id,
key: VerificationKey::new(user_id, flow_id),
})
}
}
#[derive(Debug)]
pub struct JoinRoomPayload {
pub session_id: String,
pub room_uri: MatrixRoomIdUri,
}
#[derive(Debug)]
pub struct ShowUserPayload {
pub session_id: String,
pub user_id: OwnedUserId,
}