1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
//! The protocol between the server and backends. //! //! ## Example Session //! //! ```json //! P: {"backend_name": "Example", "backend_version": [0, 0, 1], "protocol_version": [0, 1, 0]} //! S: {"sequence_number": 0, "body": {"type": "RoomLookup", "value": "#general"}} //! P: {"sequence_number": 0, "body": {"type": "RoomID", "value": "#general"}} //! S: {"sequence_number": 1, "body": {"type": "RoomJoin", "value": "#general"}} //! P: {"sequence_number": 1, "body": {"type": "Success", "value": null}} //! S: {"sequence_number": 2, "body": {"type": "MessageSend", "value": {"recipient": "#general", "attachments": [], "content": {"type": "Text", "value: "Hello, world!"}, "extra": null}}} //! P: {"sequence_number": 2, "body": {"type": "MessageID", "value": "test"}} //! ``` //! //! TODO: //! - Work out how emotes should work //! - EmoteID? Store emotes by hash? //! - Users #![deny( bad_style, bare_trait_objects, const_err, dead_code, improper_ctypes, legacy_directory_ownership, missing_debug_implementations, missing_docs, no_mangle_generic_items, non_shorthand_field_patterns, overflowing_literals, path_statements, patterns_in_fns_without_body, plugin_as_library, private_in_public, safe_extern_statics, trivial_casts, trivial_numeric_casts, unconditional_recursion, // unions_with_drop_fields, unsafe_code, unused, unused_allocation, unused_comparisons, unused_extern_crates, unused_import_braces, unused_parens, unused_qualifications, unused_results, while_true )] pub mod serde; use chrono::{DateTime, Utc}; use mime::Mime; use serde_derive::{Deserialize, Serialize}; use serde_json::Value as Json; use std::{ error::Error, fmt::{Display, Formatter}, }; use sval::Value; /// Backend sends this to the server when it starts. #[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize, Value)] #[sval(derive_from = "serde")] pub struct InitInfo { /// The name of the backend. pub backend_name: String, /// The version of the backend. pub backend_version: Version, /// The version of the protocol. This is version `0.1.0`. pub protocol_version: Version, } /// The version of the backend or protocol. #[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize, Value)] #[sval(derive_from = "serde")] pub struct Version(pub u32, pub u32, pub u32); /// A name for a message on a service. This should uniquely identify the message, even if it gets edited. #[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize, Value)] #[sval(derive_from = "serde")] pub struct MessageID(pub String); /// A name for a room on a service. This should uniquely identify a room through renames if possible. #[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize, Value)] #[sval(derive_from = "serde")] pub struct RoomID(pub String); /// A name for a user on a service. This should uniquely identify a user through renames if possible. #[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize, Value)] #[sval(derive_from = "serde")] pub struct UserID(pub String); /// A RoomID or UserID. #[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize, Value)] #[serde(untagged)] #[sval(derive_from = "serde")] pub enum RoomIDOrUserID { /// A RoomID. Room(RoomID), /// A UserID. User(UserID), } /// A message sent from a user to another user or a room. #[derive(Clone, Debug, Deserialize, PartialEq, Serialize, Value)] #[sval(derive_from = "serde")] pub struct Message { /// The ID of the message. pub id: MessageID, /// The sending User. pub sender: UserID, /// The Room sent to, or the User who was DM'd. pub recipient: RoomIDOrUserID, /// Attachments sent with the message. pub attachments: Vec<MessageAttachment>, /// The body of the message. pub content: MessageContent, /// The time the message was created. #[serde(with = "crate::serde::unix_ms")] pub create_time: DateTime<Utc>, /// The time the message was last edited. #[serde(with = "crate::serde::unix_ms")] pub edit_time: DateTime<Utc>, /// Extra backend-specific data. #[serde(default)] pub extra: Json, } /// A message sent from a user to another user or a room. #[derive(Clone, Debug, Deserialize, PartialEq, Serialize, Value)] #[sval(derive_from = "serde")] pub struct NewMessage { /// The Room to send to, or the User to DM. pub recipient: RoomIDOrUserID, /// Attachments sent with the message. pub attachments: Vec<MessageAttachment>, /// The body of the message. pub content: MessageContent, /// Extra backend-specific data. #[serde(default)] pub extra: Json, } /// MIME-typed data attached to a message. #[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize, Value)] #[sval(derive_from = "serde")] pub struct MessageAttachment { /// The mime type of a message. #[serde(with = "crate::serde::mime")] pub mime: Mime, /// The contents of the attachment. #[serde(with = "crate::serde::base64")] pub data: Vec<u8>, } /// The contents of a message. #[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize, Value)] #[serde(tag = "type", content = "value")] #[sval(derive_from = "serde")] pub enum MessageContent { /// Displays the enclosed content in bold. Bold(Box<MessageContent>), /// Concatenates the enclosed content. Concat(Vec<MessageContent>), /// Displays the enclosed content crossed out. Crossout(Box<MessageContent>), /// An emote. /// /// **TODO**: How to do these? Just make them cached images? Emote(String), /// Displays the enclosed content in italics. Italic(Box<MessageContent>), /// A link to a message. MessageLink(MessageID), /// A link to a room. RoomLink(RoomID), /// Plain text. Text(String), /// A link to a resource by URL. UrlLink(String), /// Displays the enclosed content with an underline. Underline(Box<MessageContent>), /// A link to a user. UserLink(UserID), } /// The information corresponding to a room. #[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize, Value)] #[sval(derive_from = "serde")] pub struct Room { /// The ID of the room. pub id: RoomID, /// The room which is the parent of this room. pub parent: Option<RoomID>, /// The name of the room. pub name: String, /// Whether the room can be sent to. pub sendable: bool, } /// A request to create a new room with the given properties. #[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize, Value)] #[sval(derive_from = "serde")] pub struct NewRoom { /// The room which is the parent of this room. pub parent: Option<RoomID>, /// The name of the room. pub name: String, /// Whether the room can be sent to. pub sendable: bool, } /// Information sent from the backend to the server. #[derive(Clone, Debug, Deserialize, PartialEq, Serialize, Value)] #[serde(tag = "type", content = "value")] #[sval(derive_from = "serde")] pub enum Update { /// Notification that a room was created or edited. RoomUpsert(Room), /// Notification that a room was deleted. RoomDelete(RoomID), /// Notification that a message was created or edited. MessageUpsert(Message), /// Notification that a message was deleted. MessageDelete(MessageID), } /// A request as sent to the backend. #[derive(Clone, Debug, Deserialize, PartialEq, Serialize, Value)] #[sval(derive_from = "serde")] pub struct Request { /// The sequence number of the request. Two requests with the same sequence number may not be /// in flight at the same time. pub sequence_number: u32, /// The contents of the request. pub body: RequestBody, } /// A response as sent from the backend. #[derive(Clone, Debug, Deserialize, PartialEq, Serialize, Value)] #[sval(derive_from = "serde")] pub struct Response { /// The sequence number, which must match the sequence number in the request. pub sequence_number: u32, /// The contents of the response. pub body: ResponseBody, } /// A Response or Update. #[derive(Clone, Debug, Deserialize, PartialEq, Serialize, Value)] #[serde(untagged)] #[sval(derive_from = "serde")] pub enum ResponseOrUpdate { /// A Response. Response(Response), /// An Update. Update(Update), } /// A request made to a server. #[derive(Clone, Debug, Deserialize, PartialEq, Serialize, Value)] #[serde(tag = "type", content = "value")] #[sval(derive_from = "serde")] pub enum RequestBody { /// A request to get some number of messages earlier in history than the given one. /// /// The only valid non-error response is a `ResponseBody::Success`. MessageGetBefore(MessageID), /// A request to get information about a message by ID. /// /// The only valid non-error response is a `ResponseBody::Message`. MessageGet(MessageID), /// A request to send a message. /// /// The only valid non-error response is a `ResponseBody::MessageID`. MessageSend(NewMessage), /// A request to get information about a room by ID. /// /// The only valid non-error response is a `ResponseBody::Room`. RoomGet(RoomID), /// A request to create a room. /// /// The only valid non-error response is a `ResponseBody::RoomID`. RoomCreate(NewRoom), /// A request to get the ID of a named room. /// /// The only valid non-error response is a `ResponseBody::RoomID`. RoomLookup(String), /// A request to join a room. /// /// The only valid non-error response is a `ResponseBody::Success`. RoomJoin(RoomID), /// A request to leave a room. /// /// The only valid non-error response is a `ResponseBody::Success`. RoomLeave(RoomID), } /// The response to a request. #[derive(Clone, Debug, Deserialize, PartialEq, Serialize, Value)] #[serde(tag = "type", content = "value")] #[sval(derive_from = "serde")] pub enum ResponseBody { /// The request succeeded without returning a response. Success, /// The request succeeded, resulting in a message. Message(Message), /// The request succeeded, resulting in a room. Room(Room), /// The request succeeded, resulting in a message ID. MessageID(MessageID), /// The request succeeded, resulting in a room ID. RoomID(RoomID), /// The request failed. Error(ResponseError), } /// An error with a request. #[derive(Clone, Debug, Deserialize, PartialEq, Serialize, Value)] #[sval(derive_from = "serde")] pub struct ResponseError { /// An error message. pub message: String, /// Additional information to be logged for debugging, but not necessarily shown to the user. #[serde(default)] pub debug_info: Json, /// Whether the error can be resolved by retrying the request. Note that this typically implies /// that the action was idempotent; see https://www.tedinski.com/2019/02/20/idempotence.html. #[serde(default)] pub retry: bool, } impl Display for ResponseError { fn fmt(&self, fmt: &mut Formatter) -> Result<(), std::fmt::Error> { fmt.write_str(&self.message) } } impl Error for ResponseError { fn description(&self) -> &str { &self.message } }