matrix_ui_serializable/models/
events.rs

1use matrix_sdk::ruma::{MilliSecondsSinceUnixEpoch, OwnedDeviceId, OwnedRoomId};
2use serde::{Deserialize, Serialize};
3
4// Listen to events
5#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
6pub enum ListenEvent {
7    RoomCreated,
8    VerificationResult,
9    MatrixUpdateCurrentActiveRoom,
10}
11
12#[derive(Debug, Clone, Serialize, Deserialize)]
13#[serde(rename_all = "camelCase")]
14pub struct MatrixVerificationResponse {
15    pub confirmed: bool,
16}
17
18#[derive(Debug, Clone, Serialize, Deserialize)]
19#[serde(rename_all = "camelCase")]
20pub struct MatrixRoomStoreCreatedRequest {
21    pub id: String,
22    pub message: String,
23}
24
25#[derive(Debug, Clone, Serialize, Deserialize)]
26#[serde(rename_all = "camelCase")]
27pub struct MatrixUpdateCurrentActiveRoom {
28    // if the frontend sends null then it will be None
29    pub room_id: Option<OwnedRoomId>,
30    pub room_name: Option<String>,
31}
32
33// Emit events
34
35#[derive(Debug, Clone)]
36pub enum EmitEvent {
37    RoomCreate(MatrixRoomStoreCreateRequest),
38    VerificationStart(MatrixVerificationEmojis),
39    ToastNotification(ToastNotificationRequest),
40    OsNotification(OsNotificationRequest),
41}
42
43#[derive(Debug, Clone, Serialize)]
44#[serde(rename_all = "camelCase")]
45pub struct MatrixVerificationEmojis {
46    emojis: String,
47}
48
49impl MatrixVerificationEmojis {
50    pub fn new(emojis: String) -> Self {
51        Self { emojis }
52    }
53}
54
55#[derive(Debug, Clone, Serialize, Deserialize)]
56#[serde(rename_all = "camelCase")]
57pub struct MatrixRoomStoreCreateRequest {
58    id: String,
59}
60
61impl MatrixRoomStoreCreateRequest {
62    pub fn new(id: String) -> Self {
63        Self { id }
64    }
65}
66
67#[derive(Debug, Clone, Serialize, Deserialize)]
68#[serde(rename_all = "camelCase")]
69pub struct ToastNotificationRequest {
70    message: String,
71    description: Option<String>,
72    variant: ToastNotificationVariant,
73}
74
75#[derive(Debug, Clone, Serialize, Deserialize)]
76#[serde(rename_all = "camelCase", rename_all_fields = "camelCase")]
77pub enum ToastNotificationVariant {
78    Default,
79    Description,
80    Success,
81    Info,
82    Warning,
83    Error,
84}
85
86impl ToastNotificationRequest {
87    pub fn new(
88        message: String,
89        description: Option<String>,
90        variant: ToastNotificationVariant,
91    ) -> Self {
92        if description.is_some() {
93            // If there is a description, force the description variant.
94            Self {
95                message,
96                description,
97                variant: ToastNotificationVariant::Description,
98            }
99        } else {
100            Self {
101                message,
102                description: None,
103                variant,
104            }
105        }
106    }
107}
108
109#[derive(Debug, Clone, Serialize, Deserialize)]
110#[serde(rename_all = "camelCase")]
111pub struct OsNotificationRequest {
112    pub summary: String,
113    pub body: Option<String>,
114}
115
116impl OsNotificationRequest {
117    pub fn new(summary: String, body: Option<String>) -> Self {
118        Self { summary, body }
119    }
120}
121
122// Channel events
123
124#[derive(Clone, Serialize)]
125#[serde(
126    rename_all = "camelCase",
127    rename_all_fields = "camelCase",
128    tag = "event",
129    content = "data"
130)]
131pub enum MediaStreamEvent {
132    Started,
133    Chunk {
134        data: Vec<u8>,
135        chunk_size: usize,
136        bytes_received: usize,
137    },
138    Finished {
139        total_bytes: usize,
140    },
141    Error {
142        message: String,
143    },
144}
145
146// Commands
147#[derive(Debug, Clone, Serialize)]
148#[serde(rename_all = "camelCase")]
149pub struct FrontendDevice {
150    pub device_id: OwnedDeviceId,
151    pub is_verified: bool,
152    pub is_verified_with_cross_signing: bool,
153    pub display_name: Option<String>,
154    pub registration_date: MilliSecondsSinceUnixEpoch,
155    pub guessed_type: DeviceGuessedType,
156    pub is_current_device: bool,
157}
158
159#[derive(Debug, Clone, Serialize)]
160#[serde(rename_all = "camelCase", rename_all_fields = "camelCase")]
161pub enum DeviceGuessedType {
162    Android,
163    Ios,
164    Web,
165    Desktop,
166    Unknown,
167}