| 1 | use std::{fmt, sync::Arc}; |
| 2 | |
| 3 | use imbl::Vector; |
| 4 | use ruma::{ |
| 5 | OwnedDeviceId, OwnedEventId, OwnedMxcUri, OwnedRoomAliasId, OwnedRoomId, OwnedRoomOrAliasId, |
| 6 | OwnedServerName, OwnedTransactionId, OwnedUserId, |
| 7 | events::{ |
| 8 | AnyGlobalAccountDataEventContent, AnyRoomAccountDataEventContent, AnySyncTimelineEvent, |
| 9 | GlobalAccountDataEventType, RoomAccountDataEventType, |
| 10 | room::member::StrippedRoomMemberEvent, |
| 11 | }, |
| 12 | serde::Raw, |
| 13 | }; |
| 14 | #[cfg(feature = "serde")] |
| 15 | use serde::{Deserialize, Serialize}; |
| 16 | |
| 17 | /// every command answers exactly once through `ClientHandle::send` or `execute`. |
| 18 | /// the event stream only ever carries state |
| 19 | #[derive(Debug)] |
| 20 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 21 | #[non_exhaustive] |
| 22 | pub enum Command { |
| 23 | Account(AccountCommand), |
| 24 | Room(RoomCommand), |
| 25 | Timeline(TimelineCommand), |
| 26 | Media(MediaCommand), |
| 27 | Encryption(EncryptionCommand), |
| 28 | Notification(NotificationCommand), |
| 29 | Lifecycle(LifecycleCommand), |
| 30 | } |
| 31 | |
| 32 | #[derive(Debug)] |
| 33 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 34 | #[non_exhaustive] |
| 35 | pub enum AccountCommand { |
| 36 | Logout, |
| 37 | SetAccountData { |
| 38 | event_type: GlobalAccountDataEventType, |
| 39 | content: Raw<AnyGlobalAccountDataEventContent>, |
| 40 | }, |
| 41 | SetDisplayName(Option<String>), |
| 42 | SetAvatar(Option<OwnedMxcUri>), |
| 43 | /// uploads the image and points the profile at it, same byte cap as any upload |
| 44 | UploadAvatar { |
| 45 | mimetype: String, |
| 46 | bytes: Vec<u8>, |
| 47 | }, |
| 48 | RenameDevice { |
| 49 | device_id: OwnedDeviceId, |
| 50 | display_name: String, |
| 51 | }, |
| 52 | DeleteDevices { |
| 53 | device_ids: Vec<OwnedDeviceId>, |
| 54 | auth: Option<crate::InteractiveAuth>, |
| 55 | }, |
| 56 | } |
| 57 | |
| 58 | #[derive(Clone, Debug)] |
| 59 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 60 | #[non_exhaustive] |
| 61 | pub enum RoomCommand { |
| 62 | /// the snapshot turns up as `Event::TimelineReset`. opening an open room sends a fresh one |
| 63 | Open(OwnedRoomId), |
| 64 | Close(OwnedRoomId), |
| 65 | Join { |
| 66 | room: OwnedRoomOrAliasId, |
| 67 | via: Vec<OwnedServerName>, |
| 68 | }, |
| 69 | Leave(OwnedRoomId), |
| 70 | Forget(OwnedRoomId), |
| 71 | AcceptInvite(OwnedRoomId), |
| 72 | RejectInvite(OwnedRoomId), |
| 73 | InviteUser { |
| 74 | room_id: OwnedRoomId, |
| 75 | user_id: OwnedUserId, |
| 76 | }, |
| 77 | KickUser { |
| 78 | room_id: OwnedRoomId, |
| 79 | user_id: OwnedUserId, |
| 80 | reason: Option<String>, |
| 81 | }, |
| 82 | BanUser { |
| 83 | room_id: OwnedRoomId, |
| 84 | user_id: OwnedUserId, |
| 85 | reason: Option<String>, |
| 86 | }, |
| 87 | UnbanUser { |
| 88 | room_id: OwnedRoomId, |
| 89 | user_id: OwnedUserId, |
| 90 | reason: Option<String>, |
| 91 | }, |
| 92 | SetPowerLevels { |
| 93 | room_id: OwnedRoomId, |
| 94 | changes: PowerLevelChanges, |
| 95 | }, |
| 96 | SetUserPowerLevel { |
| 97 | room_id: OwnedRoomId, |
| 98 | user_id: OwnedUserId, |
| 99 | level: i64, |
| 100 | }, |
| 101 | Create(CreateRoomOptions), |
| 102 | /// hands back the existing dm if there is one |
| 103 | CreateDirect(OwnedUserId), |
| 104 | CancelSend { |
| 105 | room_id: OwnedRoomId, |
| 106 | transaction_id: OwnedTransactionId, |
| 107 | }, |
| 108 | RetrySend { |
| 109 | room_id: OwnedRoomId, |
| 110 | transaction_id: OwnedTransactionId, |
| 111 | }, |
| 112 | SetTyping { |
| 113 | room_id: OwnedRoomId, |
| 114 | typing: bool, |
| 115 | }, |
| 116 | /// the key work a send into an encrypted room would otherwise start with, done now. |
| 117 | /// nothing in a plain room, and a second ask joins the first |
| 118 | PrepareToSend(OwnedRoomId), |
| 119 | SetAccountData { |
| 120 | room_id: OwnedRoomId, |
| 121 | event_type: RoomAccountDataEventType, |
| 122 | content: Raw<AnyRoomAccountDataEventContent>, |
| 123 | }, |
| 124 | } |
| 125 | |
| 126 | #[derive(Clone, Debug, Default, Eq, PartialEq)] |
| 127 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 128 | #[non_exhaustive] |
| 129 | pub struct CreateRoomOptions { |
| 130 | pub name: Option<String>, |
| 131 | pub topic: Option<String>, |
| 132 | pub alias_localpart: Option<String>, |
| 133 | pub invite: Vec<OwnedUserId>, |
| 134 | pub direct: bool, |
| 135 | pub visibility: RoomVisibility, |
| 136 | pub preset: Option<RoomPreset>, |
| 137 | } |
| 138 | |
| 139 | impl CreateRoomOptions { |
| 140 | #[must_use] |
| 141 | pub fn with_name(mut self, name: impl Into<String>) -> Self { |
| 142 | self.name = Some(name.into()); |
| 143 | self |
| 144 | } |
| 145 | |
| 146 | #[must_use] |
| 147 | pub fn with_topic(mut self, topic: impl Into<String>) -> Self { |
| 148 | self.topic = Some(topic.into()); |
| 149 | self |
| 150 | } |
| 151 | |
| 152 | #[must_use] |
| 153 | pub fn with_alias_localpart(mut self, localpart: impl Into<String>) -> Self { |
| 154 | self.alias_localpart = Some(localpart.into()); |
| 155 | self |
| 156 | } |
| 157 | |
| 158 | #[must_use] |
| 159 | pub fn with_invite(mut self, invite: impl IntoIterator<Item = OwnedUserId>) -> Self { |
| 160 | self.invite = invite.into_iter().collect(); |
| 161 | self |
| 162 | } |
| 163 | |
| 164 | #[must_use] |
| 165 | pub const fn with_direct(mut self, direct: bool) -> Self { |
| 166 | self.direct = direct; |
| 167 | self |
| 168 | } |
| 169 | |
| 170 | #[must_use] |
| 171 | pub const fn with_visibility(mut self, visibility: RoomVisibility) -> Self { |
| 172 | self.visibility = visibility; |
| 173 | self |
| 174 | } |
| 175 | |
| 176 | #[must_use] |
| 177 | pub const fn with_preset(mut self, preset: RoomPreset) -> Self { |
| 178 | self.preset = Some(preset); |
| 179 | self |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | #[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] |
| 184 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 185 | pub enum RoomVisibility { |
| 186 | #[default] |
| 187 | Private, |
| 188 | Public, |
| 189 | } |
| 190 | |
| 191 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 192 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 193 | pub enum RoomPreset { |
| 194 | PrivateChat, |
| 195 | TrustedPrivateChat, |
| 196 | PublicChat, |
| 197 | } |
| 198 | |
| 199 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 200 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 201 | #[non_exhaustive] |
| 202 | pub enum TimelineCommand { |
| 203 | OpenThread { |
| 204 | room_id: OwnedRoomId, |
| 205 | root_event_id: OwnedEventId, |
| 206 | }, |
| 207 | CloseThread { |
| 208 | room_id: OwnedRoomId, |
| 209 | root_event_id: OwnedEventId, |
| 210 | }, |
| 211 | /// Re-emit an authoritative reset for an open room or thread without |
| 212 | /// closing it. The reset arrives before this command's terminal result. |
| 213 | Resync { |
| 214 | timeline_id: TimelineId, |
| 215 | }, |
| 216 | Paginate { |
| 217 | timeline_id: TimelineId, |
| 218 | limit: u16, |
| 219 | }, |
| 220 | Send { |
| 221 | timeline_id: TimelineId, |
| 222 | content: MessageContent, |
| 223 | }, |
| 224 | Edit { |
| 225 | timeline_id: TimelineId, |
| 226 | target: EventRef, |
| 227 | content: MessageEditContent, |
| 228 | }, |
| 229 | /// redacting your own unsent message just cancels it |
| 230 | Redact { |
| 231 | timeline_id: TimelineId, |
| 232 | target: EventRef, |
| 233 | reason: Option<String>, |
| 234 | }, |
| 235 | ToggleReaction { |
| 236 | timeline_id: TimelineId, |
| 237 | target: EventRef, |
| 238 | key: String, |
| 239 | }, |
| 240 | /// works on a closed room too, it just can't refuse to move the marker backwards |
| 241 | MarkRead { |
| 242 | timeline_id: TimelineId, |
| 243 | event_id: OwnedEventId, |
| 244 | }, |
| 245 | } |
| 246 | |
| 247 | impl TimelineCommand { |
| 248 | #[must_use] |
| 249 | pub const fn room_id(&self) -> &OwnedRoomId { |
| 250 | match self { |
| 251 | Self::OpenThread { room_id, .. } | Self::CloseThread { room_id, .. } => room_id, |
| 252 | Self::Resync { timeline_id } |
| 253 | | Self::Paginate { timeline_id, .. } |
| 254 | | Self::Send { timeline_id, .. } |
| 255 | | Self::Edit { timeline_id, .. } |
| 256 | | Self::Redact { timeline_id, .. } |
| 257 | | Self::ToggleReaction { timeline_id, .. } |
| 258 | | Self::MarkRead { timeline_id, .. } => timeline_id.room_id(), |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | #[must_use] |
| 263 | pub const fn timeline_id(&self) -> Option<&TimelineId> { |
| 264 | match self { |
| 265 | Self::OpenThread { .. } | Self::CloseThread { .. } => None, |
| 266 | Self::Resync { timeline_id } |
| 267 | | Self::Paginate { timeline_id, .. } |
| 268 | | Self::Send { timeline_id, .. } |
| 269 | | Self::Edit { timeline_id, .. } |
| 270 | | Self::Redact { timeline_id, .. } |
| 271 | | Self::ToggleReaction { timeline_id, .. } |
| 272 | | Self::MarkRead { timeline_id, .. } => Some(timeline_id), |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | #[derive(Clone, Debug, Eq, Hash, PartialEq)] |
| 278 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 279 | #[non_exhaustive] |
| 280 | pub enum TimelineId { |
| 281 | Room(OwnedRoomId), |
| 282 | Thread { |
| 283 | room_id: OwnedRoomId, |
| 284 | root_event_id: OwnedEventId, |
| 285 | }, |
| 286 | } |
| 287 | |
| 288 | impl TimelineId { |
| 289 | #[must_use] |
| 290 | pub const fn room_id(&self) -> &OwnedRoomId { |
| 291 | match self { |
| 292 | Self::Room(room_id) | Self::Thread { room_id, .. } => room_id, |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | #[must_use] |
| 297 | pub fn thread(room_id: OwnedRoomId, root_event_id: OwnedEventId) -> Self { |
| 298 | Self::Thread { |
| 299 | room_id, |
| 300 | root_event_id, |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | #[must_use] |
| 305 | pub fn thread_root(&self) -> Option<&OwnedEventId> { |
| 306 | match self { |
| 307 | Self::Room(_) => None, |
| 308 | Self::Thread { root_event_id, .. } => Some(root_event_id), |
| 309 | } |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | impl From<OwnedRoomId> for TimelineId { |
| 314 | fn from(room_id: OwnedRoomId) -> Self { |
| 315 | Self::Room(room_id) |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 320 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 321 | #[non_exhaustive] |
| 322 | pub enum MediaCommand { |
| 323 | /// succeeds once the send queue has it. progress and failure arrive as events |
| 324 | Upload(AttachmentUpload), |
| 325 | } |
| 326 | |
| 327 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 328 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 329 | #[non_exhaustive] |
| 330 | pub struct AttachmentUpload { |
| 331 | pub timeline_id: TimelineId, |
| 332 | pub filename: String, |
| 333 | pub mimetype: String, |
| 334 | pub bytes: Vec<u8>, |
| 335 | pub caption: Option<String>, |
| 336 | pub details: Option<UploadDetails>, |
| 337 | pub thumbnail: Option<UploadThumbnail>, |
| 338 | } |
| 339 | |
| 340 | impl AttachmentUpload { |
| 341 | #[must_use] |
| 342 | pub fn new( |
| 343 | timeline_id: impl Into<TimelineId>, |
| 344 | filename: impl Into<String>, |
| 345 | mimetype: impl Into<String>, |
| 346 | bytes: Vec<u8>, |
| 347 | ) -> Self { |
| 348 | Self { |
| 349 | timeline_id: timeline_id.into(), |
| 350 | filename: filename.into(), |
| 351 | mimetype: mimetype.into(), |
| 352 | bytes, |
| 353 | caption: None, |
| 354 | details: None, |
| 355 | thumbnail: None, |
| 356 | } |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 361 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 362 | #[non_exhaustive] |
| 363 | pub enum UploadDetails { |
| 364 | Image { |
| 365 | width: Option<u32>, |
| 366 | height: Option<u32>, |
| 367 | blurhash: Option<String>, |
| 368 | }, |
| 369 | Video { |
| 370 | width: Option<u32>, |
| 371 | height: Option<u32>, |
| 372 | duration_ms: Option<u64>, |
| 373 | blurhash: Option<String>, |
| 374 | }, |
| 375 | Audio { |
| 376 | duration_ms: Option<u64>, |
| 377 | }, |
| 378 | Voice { |
| 379 | duration_ms: u64, |
| 380 | waveform: Vec<u16>, |
| 381 | }, |
| 382 | File, |
| 383 | } |
| 384 | |
| 385 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 386 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 387 | #[non_exhaustive] |
| 388 | pub struct UploadThumbnail { |
| 389 | pub bytes: Vec<u8>, |
| 390 | pub mimetype: String, |
| 391 | pub width: u32, |
| 392 | pub height: u32, |
| 393 | } |
| 394 | |
| 395 | impl UploadThumbnail { |
| 396 | #[must_use] |
| 397 | pub fn new(bytes: Vec<u8>, mimetype: impl Into<String>, width: u32, height: u32) -> Self { |
| 398 | Self { |
| 399 | bytes, |
| 400 | mimetype: mimetype.into(), |
| 401 | width, |
| 402 | height, |
| 403 | } |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | #[derive(Eq, PartialEq)] |
| 408 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 409 | #[non_exhaustive] |
| 410 | pub enum EncryptionCommand { |
| 411 | Recover(String), |
| 412 | CreateKeyBackup, |
| 413 | EnableKeyBackup, |
| 414 | DisableKeyBackup { delete_from_server: bool }, |
| 415 | RecoverKeyBackup(String), |
| 416 | StartVerification(VerificationTarget), |
| 417 | Verify(VerificationAction), |
| 418 | } |
| 419 | |
| 420 | impl fmt::Debug for EncryptionCommand { |
| 421 | fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 422 | match self { |
| 423 | Self::Recover(_) => formatter.debug_tuple("Recover").field(&"...").finish(), |
| 424 | Self::CreateKeyBackup => formatter.write_str("CreateKeyBackup"), |
| 425 | Self::EnableKeyBackup => formatter.write_str("EnableKeyBackup"), |
| 426 | Self::DisableKeyBackup { delete_from_server } => formatter |
| 427 | .debug_struct("DisableKeyBackup") |
| 428 | .field("delete_from_server", delete_from_server) |
| 429 | .finish(), |
| 430 | Self::RecoverKeyBackup(_) => formatter |
| 431 | .debug_tuple("RecoverKeyBackup") |
| 432 | .field(&"...") |
| 433 | .finish(), |
| 434 | Self::StartVerification(target) => formatter |
| 435 | .debug_tuple("StartVerification") |
| 436 | .field(target) |
| 437 | .finish(), |
| 438 | Self::Verify(action) => formatter.debug_tuple("Verify").field(action).finish(), |
| 439 | } |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 444 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 445 | #[non_exhaustive] |
| 446 | pub enum NotificationCommand { |
| 447 | SetRoomMode { |
| 448 | room_id: OwnedRoomId, |
| 449 | mode: RoomNotificationMode, |
| 450 | }, |
| 451 | ResetRoomMode(OwnedRoomId), |
| 452 | SetDefaultMode { |
| 453 | encrypted: bool, |
| 454 | one_to_one: bool, |
| 455 | mode: RoomNotificationMode, |
| 456 | }, |
| 457 | AddKeyword(String), |
| 458 | RemoveKeyword(String), |
| 459 | } |
| 460 | |
| 461 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 462 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 463 | #[non_exhaustive] |
| 464 | pub enum LifecycleCommand { |
| 465 | Shutdown, |
| 466 | } |
| 467 | |
| 468 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 469 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 470 | #[non_exhaustive] |
| 471 | pub enum CommandOutput { |
| 472 | Done, |
| 473 | Room(OwnedRoomId), |
| 474 | } |
| 475 | |
| 476 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 477 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 478 | #[non_exhaustive] |
| 479 | pub enum MessageContent { |
| 480 | Text { |
| 481 | content: RichText, |
| 482 | mentions: MessageMentions, |
| 483 | }, |
| 484 | Emote { |
| 485 | content: RichText, |
| 486 | mentions: MessageMentions, |
| 487 | }, |
| 488 | Reply { |
| 489 | event_id: OwnedEventId, |
| 490 | content: RichText, |
| 491 | mentions: MessageMentions, |
| 492 | notify_replied_to: bool, |
| 493 | }, |
| 494 | } |
| 495 | |
| 496 | impl MessageContent { |
| 497 | #[must_use] |
| 498 | pub fn text(body: impl Into<String>) -> Self { |
| 499 | Self::Text { |
| 500 | content: RichText::plain(body), |
| 501 | mentions: MessageMentions::default(), |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | #[must_use] |
| 506 | pub fn html(body: impl Into<String>, formatted_body: impl Into<String>) -> Self { |
| 507 | Self::Text { |
| 508 | content: RichText::html(body, formatted_body), |
| 509 | mentions: MessageMentions::default(), |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | #[must_use] |
| 514 | pub fn emote(content: RichText, mentions: MessageMentions) -> Self { |
| 515 | Self::Emote { content, mentions } |
| 516 | } |
| 517 | |
| 518 | #[must_use] |
| 519 | pub fn reply(event_id: OwnedEventId, content: RichText, mentions: MessageMentions) -> Self { |
| 520 | Self::Reply { |
| 521 | event_id, |
| 522 | content, |
| 523 | mentions, |
| 524 | notify_replied_to: true, |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | #[must_use] |
| 529 | pub fn reply_without_notification( |
| 530 | event_id: OwnedEventId, |
| 531 | content: RichText, |
| 532 | mentions: MessageMentions, |
| 533 | ) -> Self { |
| 534 | Self::Reply { |
| 535 | event_id, |
| 536 | content, |
| 537 | mentions, |
| 538 | notify_replied_to: false, |
| 539 | } |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 544 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 545 | #[non_exhaustive] |
| 546 | pub enum MessageEditContent { |
| 547 | Text { |
| 548 | content: RichText, |
| 549 | mentions: MessageMentions, |
| 550 | }, |
| 551 | Emote { |
| 552 | content: RichText, |
| 553 | mentions: MessageMentions, |
| 554 | }, |
| 555 | } |
| 556 | |
| 557 | impl MessageEditContent { |
| 558 | #[must_use] |
| 559 | pub fn text(body: impl Into<String>) -> Self { |
| 560 | Self::Text { |
| 561 | content: RichText::plain(body), |
| 562 | mentions: MessageMentions::default(), |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | #[must_use] |
| 567 | pub fn html(body: impl Into<String>, formatted_body: impl Into<String>) -> Self { |
| 568 | Self::Text { |
| 569 | content: RichText::html(body, formatted_body), |
| 570 | mentions: MessageMentions::default(), |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | #[must_use] |
| 575 | pub fn emote(content: RichText, mentions: MessageMentions) -> Self { |
| 576 | Self::Emote { content, mentions } |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | #[derive(Clone, Debug, Default, Eq, PartialEq)] |
| 581 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 582 | #[non_exhaustive] |
| 583 | pub struct RichText { |
| 584 | pub body: String, |
| 585 | pub formatted_body: Option<String>, |
| 586 | } |
| 587 | |
| 588 | impl RichText { |
| 589 | #[must_use] |
| 590 | pub fn plain(body: impl Into<String>) -> Self { |
| 591 | Self { |
| 592 | body: body.into(), |
| 593 | formatted_body: None, |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | #[must_use] |
| 598 | pub fn html(body: impl Into<String>, formatted_body: impl Into<String>) -> Self { |
| 599 | Self { |
| 600 | body: body.into(), |
| 601 | formatted_body: Some(formatted_body.into()), |
| 602 | } |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | #[derive(Clone, Debug, Default, Eq, PartialEq)] |
| 607 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 608 | #[non_exhaustive] |
| 609 | pub struct MessageMentions { |
| 610 | pub user_ids: Vec<OwnedUserId>, |
| 611 | pub room: bool, |
| 612 | } |
| 613 | |
| 614 | impl MessageMentions { |
| 615 | #[must_use] |
| 616 | pub fn with_user_ids(user_ids: impl IntoIterator<Item = OwnedUserId>) -> Self { |
| 617 | Self { |
| 618 | user_ids: user_ids.into_iter().collect(), |
| 619 | room: false, |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | #[must_use] |
| 624 | pub fn room() -> Self { |
| 625 | Self { |
| 626 | user_ids: Vec::new(), |
| 627 | room: true, |
| 628 | } |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 633 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 634 | #[non_exhaustive] |
| 635 | pub enum Event { |
| 636 | ConnectionChanged(ConnectionState), |
| 637 | Authenticated { |
| 638 | user_id: OwnedUserId, |
| 639 | }, |
| 640 | /// the tokens rotated. with a `SessionStore` the record is already saved by now, |
| 641 | /// without one persist a fresh `ClientHandle::session_record()` atomically |
| 642 | SessionTokensChanged, |
| 643 | /// the rotated tokens didn't reach the `SessionStore`, so the saved record may not |
| 644 | /// restore. the engine tries again on the next rotation |
| 645 | SessionSaveFailed { |
| 646 | message: String, |
| 647 | }, |
| 648 | /// the server stopped honouring our token and the engine stops. a soft logout |
| 649 | /// names the device to log in again as, keeping the store. otherwise the record |
| 650 | /// and the store are both dead |
| 651 | SessionAccessRejected { |
| 652 | soft_logout: Option<OwnedDeviceId>, |
| 653 | }, |
| 654 | NotificationSettingsChanged, |
| 655 | VerificationTargetChanged(VerificationTarget), |
| 656 | RoomsChanged, |
| 657 | /// replaces whatever you had for this timeline. every diff after it applies to it |
| 658 | TimelineReset { |
| 659 | timeline_id: TimelineId, |
| 660 | items: Vector<TimelineItem>, |
| 661 | }, |
| 662 | TimelineChanged { |
| 663 | timeline_id: TimelineId, |
| 664 | diffs: Vec<TimelineDiff>, |
| 665 | }, |
| 666 | HistoryChanged { |
| 667 | timeline_id: TimelineId, |
| 668 | state: HistoryState, |
| 669 | }, |
| 670 | /// the timeline was closed, or its room was left, and nothing more comes for it |
| 671 | /// until another `TimelineReset` |
| 672 | TimelineClosed { |
| 673 | timeline_id: TimelineId, |
| 674 | }, |
| 675 | /// a room that stops being watched says so with an empty `users` |
| 676 | TypingChanged(TypingState), |
| 677 | MediaUploadProgress { |
| 678 | room_id: OwnedRoomId, |
| 679 | transaction_id: OwnedTransactionId, |
| 680 | uploaded_bytes: u64, |
| 681 | total_bytes: u64, |
| 682 | complete: bool, |
| 683 | }, |
| 684 | /// not the end of it. `will_retry` means the send queue has another go by itself, |
| 685 | /// otherwise it waits for `retry_send` or `cancel_send` |
| 686 | MediaUploadFailed { |
| 687 | room_id: OwnedRoomId, |
| 688 | transaction_id: OwnedTransactionId, |
| 689 | kind: crate::ErrorKind, |
| 690 | will_retry: bool, |
| 691 | }, |
| 692 | /// an upload that turned up in a progress or failure event gets exactly one of |
| 693 | /// these while the engine runs, and nothing about it after. stopping ends |
| 694 | /// nothing, `Stopped` covers whatever is still going and the sdk resumes it next run |
| 695 | MediaUploadFinished { |
| 696 | room_id: OwnedRoomId, |
| 697 | transaction_id: OwnedTransactionId, |
| 698 | outcome: UploadOutcome, |
| 699 | }, |
| 700 | EncryptionChanged(EncryptionState), |
| 701 | VerificationChanged(VerificationState), |
| 702 | /// logout went through and the engine stops. delete the record and the store |
| 703 | LoggedOut, |
| 704 | Failed(EngineFailure), |
| 705 | /// always the last event. keep the saved record unless `LoggedOut` or a |
| 706 | /// hard `SessionAccessRejected` came first. commands still in flight answer `Interrupted` |
| 707 | Stopped, |
| 708 | } |
| 709 | |
| 710 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 711 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 712 | #[non_exhaustive] |
| 713 | pub enum UploadOutcome { |
| 714 | Sent { |
| 715 | event_id: OwnedEventId, |
| 716 | }, |
| 717 | Cancelled, |
| 718 | /// its room was forgotten, and the queue with it |
| 719 | RoomForgotten, |
| 720 | /// the sdk's update channel overflowed while it ended, so whether it was sent is unknown |
| 721 | Missed, |
| 722 | } |
| 723 | |
| 724 | /// why `Engine::run` came back, when nothing went wrong |
| 725 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 726 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 727 | #[non_exhaustive] |
| 728 | pub enum Termination { |
| 729 | ShutDown, |
| 730 | /// every `ClientHandle` was dropped, so nobody could have asked for anything else |
| 731 | HandlesDropped, |
| 732 | LoggedOut, |
| 733 | /// the server rejected our token for good |
| 734 | AccessRevoked, |
| 735 | /// the server wants this device to log in again, and its e2ee keys live in the store |
| 736 | SoftLoggedOut { |
| 737 | device_id: OwnedDeviceId, |
| 738 | }, |
| 739 | } |
| 740 | |
| 741 | impl Termination { |
| 742 | #[must_use] |
| 743 | pub const fn retain(&self) -> Retain { |
| 744 | match self { |
| 745 | Self::ShutDown | Self::HandlesDropped => Retain::RecordAndStore, |
| 746 | Self::SoftLoggedOut { .. } => Retain::Store, |
| 747 | Self::LoggedOut | Self::AccessRevoked => Retain::Nothing, |
| 748 | } |
| 749 | } |
| 750 | } |
| 751 | |
| 752 | /// what's still worth keeping of the saved record and the store |
| 753 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 754 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 755 | pub enum Retain { |
| 756 | RecordAndStore, |
| 757 | /// keep the store and a record naming the device to log in again as |
| 758 | Store, |
| 759 | Nothing, |
| 760 | } |
| 761 | |
| 762 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 763 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 764 | #[non_exhaustive] |
| 765 | pub enum ConnectionState { |
| 766 | Idle, |
| 767 | Connecting, |
| 768 | Online, |
| 769 | Offline, |
| 770 | Stopped, |
| 771 | } |
| 772 | |
| 773 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 774 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 775 | #[non_exhaustive] |
| 776 | pub struct RoomSummary { |
| 777 | pub room_id: OwnedRoomId, |
| 778 | pub name: String, |
| 779 | pub membership: RoomMembership, |
| 780 | pub unread_messages: u64, |
| 781 | pub unread_mentions: u64, |
| 782 | /// unread events the push rules said to notify about, which is neither of the above |
| 783 | pub unread_notifications: u64, |
| 784 | pub encrypted: bool, |
| 785 | pub space: bool, |
| 786 | pub direct: bool, |
| 787 | pub favourite: bool, |
| 788 | pub low_priority: bool, |
| 789 | pub avatar: Option<OwnedMxcUri>, |
| 790 | pub latest_event_timestamp_ms: Option<u64>, |
| 791 | pub latest_event: Option<LatestEvent>, |
| 792 | pub notification_mode: Option<RoomNotificationMode>, |
| 793 | pub notification_mode_custom: bool, |
| 794 | pub topic: Option<String>, |
| 795 | pub canonical_alias: Option<OwnedRoomAliasId>, |
| 796 | pub joined_members: u64, |
| 797 | pub invited_members: u64, |
| 798 | /// who a nameless room gets named after |
| 799 | pub heroes: Vec<RoomHero>, |
| 800 | /// set once the room has been upgraded, this is where everyone went |
| 801 | pub successor: Option<OwnedRoomId>, |
| 802 | pub inviter: Option<OwnedUserId>, |
| 803 | /// joined spaces that list this room as a child |
| 804 | pub parent_spaces: Vec<OwnedRoomId>, |
| 805 | /// for a space, the children it lists, joined or not |
| 806 | pub children: Vec<OwnedRoomId>, |
| 807 | } |
| 808 | |
| 809 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 810 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 811 | #[non_exhaustive] |
| 812 | pub struct RoomHero { |
| 813 | pub user_id: OwnedUserId, |
| 814 | pub display_name: Option<String>, |
| 815 | pub avatar: Option<OwnedMxcUri>, |
| 816 | } |
| 817 | |
| 818 | /// enough for a room list preview line. `kind` and `body` are only set for messages, |
| 819 | /// and an edit shows its new text |
| 820 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 821 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 822 | #[non_exhaustive] |
| 823 | pub struct LatestEvent { |
| 824 | pub sender: Option<OwnedUserId>, |
| 825 | pub kind: Option<MessageKind>, |
| 826 | pub body: Option<String>, |
| 827 | pub timestamp_ms: Option<u64>, |
| 828 | pub send_state: SendState, |
| 829 | } |
| 830 | |
| 831 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 832 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 833 | #[non_exhaustive] |
| 834 | pub struct Profile { |
| 835 | pub user_id: OwnedUserId, |
| 836 | pub display_name: Option<String>, |
| 837 | pub avatar: Option<OwnedMxcUri>, |
| 838 | } |
| 839 | |
| 840 | /// one room in a space's hierarchy, as the server describes it, which may be a room you're not in |
| 841 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 842 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 843 | #[non_exhaustive] |
| 844 | pub struct SpaceChild { |
| 845 | pub room_id: OwnedRoomId, |
| 846 | pub name: String, |
| 847 | pub topic: Option<String>, |
| 848 | pub avatar: Option<OwnedMxcUri>, |
| 849 | pub canonical_alias: Option<OwnedRoomAliasId>, |
| 850 | pub joined_members: u64, |
| 851 | pub space: bool, |
| 852 | pub children_count: u64, |
| 853 | pub membership: Option<RoomMembership>, |
| 854 | pub via: Vec<OwnedServerName>, |
| 855 | pub suggested: bool, |
| 856 | pub world_readable: Option<bool>, |
| 857 | pub guest_can_join: bool, |
| 858 | } |
| 859 | |
| 860 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 861 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 862 | #[non_exhaustive] |
| 863 | pub enum RoomNotificationMode { |
| 864 | AllMessages, |
| 865 | MentionsAndKeywordsOnly, |
| 866 | Mute, |
| 867 | } |
| 868 | |
| 869 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 870 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 871 | #[non_exhaustive] |
| 872 | pub struct RoomNotificationSettings { |
| 873 | pub mode: RoomNotificationMode, |
| 874 | pub custom: bool, |
| 875 | } |
| 876 | |
| 877 | #[derive(Clone, Debug)] |
| 878 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 879 | #[non_exhaustive] |
| 880 | pub enum PushNotificationStatus { |
| 881 | Event(Box<PushNotification>), |
| 882 | NotFound, |
| 883 | FilteredOut, |
| 884 | Redacted, |
| 885 | } |
| 886 | |
| 887 | #[derive(Clone, Debug)] |
| 888 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 889 | #[non_exhaustive] |
| 890 | pub struct PushNotification { |
| 891 | pub event_id: OwnedEventId, |
| 892 | pub event: PushNotificationEvent, |
| 893 | pub sender: PushNotificationSender, |
| 894 | pub room: PushNotificationRoom, |
| 895 | pub noisy: Option<bool>, |
| 896 | pub has_mention: Option<bool>, |
| 897 | pub thread_id: Option<OwnedEventId>, |
| 898 | } |
| 899 | |
| 900 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 901 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 902 | #[non_exhaustive] |
| 903 | pub struct PushNotificationSender { |
| 904 | pub user_id: OwnedUserId, |
| 905 | pub display_name: Option<String>, |
| 906 | pub avatar_url: Option<OwnedMxcUri>, |
| 907 | pub name_ambiguous: bool, |
| 908 | } |
| 909 | |
| 910 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 911 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 912 | #[non_exhaustive] |
| 913 | pub struct PushNotificationRoom { |
| 914 | pub room_id: OwnedRoomId, |
| 915 | pub name: String, |
| 916 | pub avatar_url: Option<OwnedMxcUri>, |
| 917 | pub canonical_alias: Option<OwnedRoomAliasId>, |
| 918 | pub topic: Option<String>, |
| 919 | pub encrypted: Option<bool>, |
| 920 | pub direct: bool, |
| 921 | pub joined_members_count: u64, |
| 922 | pub service_members: Vec<OwnedUserId>, |
| 923 | pub active_service_members_count: u64, |
| 924 | pub space: bool, |
| 925 | } |
| 926 | |
| 927 | #[derive(Clone, Debug)] |
| 928 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 929 | #[non_exhaustive] |
| 930 | pub enum PushNotificationEvent { |
| 931 | Timeline(Raw<AnySyncTimelineEvent>), |
| 932 | Invite(Raw<StrippedRoomMemberEvent>), |
| 933 | } |
| 934 | |
| 935 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 936 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 937 | #[non_exhaustive] |
| 938 | pub struct RoomPowerLevels { |
| 939 | pub ban: i64, |
| 940 | pub invite: i64, |
| 941 | pub kick: i64, |
| 942 | pub redact: i64, |
| 943 | pub events_default: i64, |
| 944 | pub state_default: i64, |
| 945 | pub users_default: i64, |
| 946 | pub room_notification: i64, |
| 947 | pub users: Vec<(OwnedUserId, i64)>, |
| 948 | pub events: Vec<(String, i64)>, |
| 949 | /// in rooms from version 12 the creators outrank everyone and aren't listed in `users` |
| 950 | pub creators: Vec<OwnedUserId>, |
| 951 | } |
| 952 | |
| 953 | #[derive(Clone, Debug, Default, Eq, PartialEq)] |
| 954 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 955 | #[non_exhaustive] |
| 956 | pub struct PowerLevelChanges { |
| 957 | pub ban: Option<i64>, |
| 958 | pub invite: Option<i64>, |
| 959 | pub kick: Option<i64>, |
| 960 | pub redact: Option<i64>, |
| 961 | pub events_default: Option<i64>, |
| 962 | pub state_default: Option<i64>, |
| 963 | pub users_default: Option<i64>, |
| 964 | pub room_notification: Option<i64>, |
| 965 | pub room_name: Option<i64>, |
| 966 | pub room_avatar: Option<i64>, |
| 967 | pub room_topic: Option<i64>, |
| 968 | pub space_child: Option<i64>, |
| 969 | pub beacon: Option<i64>, |
| 970 | pub beacon_info: Option<i64>, |
| 971 | } |
| 972 | |
| 973 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 974 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 975 | #[non_exhaustive] |
| 976 | pub enum RoomMembership { |
| 977 | Joined, |
| 978 | Invited, |
| 979 | Left, |
| 980 | Knocked, |
| 981 | Banned, |
| 982 | } |
| 983 | |
| 984 | #[derive(Clone, Debug, Default, Eq, PartialEq)] |
| 985 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 986 | #[non_exhaustive] |
| 987 | pub struct RoomListFilter { |
| 988 | pub membership: Option<RoomMembership>, |
| 989 | pub category: Option<RoomCategory>, |
| 990 | pub favourite: Option<bool>, |
| 991 | pub unread_only: bool, |
| 992 | pub search: Option<String>, |
| 993 | } |
| 994 | |
| 995 | impl RoomListFilter { |
| 996 | #[must_use] |
| 997 | pub fn with_membership(mut self, membership: RoomMembership) -> Self { |
| 998 | self.membership = Some(membership); |
| 999 | self |
| 1000 | } |
| 1001 | |
| 1002 | #[must_use] |
| 1003 | pub fn with_category(mut self, category: RoomCategory) -> Self { |
| 1004 | self.category = Some(category); |
| 1005 | self |
| 1006 | } |
| 1007 | |
| 1008 | #[must_use] |
| 1009 | pub fn with_favourite(mut self, favourite: bool) -> Self { |
| 1010 | self.favourite = Some(favourite); |
| 1011 | self |
| 1012 | } |
| 1013 | |
| 1014 | #[must_use] |
| 1015 | pub const fn with_unread_only(mut self, unread_only: bool) -> Self { |
| 1016 | self.unread_only = unread_only; |
| 1017 | self |
| 1018 | } |
| 1019 | |
| 1020 | #[must_use] |
| 1021 | pub fn with_search(mut self, search: impl Into<String>) -> Self { |
| 1022 | self.search = Some(search.into()); |
| 1023 | self |
| 1024 | } |
| 1025 | |
| 1026 | #[must_use] |
| 1027 | pub fn matches(&self, room: &RoomSummary) -> bool { |
| 1028 | self.matcher().matches(room) |
| 1029 | } |
| 1030 | |
| 1031 | /// lowercases the search once, for filtering a whole list |
| 1032 | #[must_use] |
| 1033 | pub fn matcher(&self) -> RoomMatcher<'_> { |
| 1034 | RoomMatcher { |
| 1035 | filter: self, |
| 1036 | search: self.search.as_deref().map(str::to_lowercase), |
| 1037 | } |
| 1038 | } |
| 1039 | } |
| 1040 | |
| 1041 | #[derive(Debug)] |
| 1042 | pub struct RoomMatcher<'a> { |
| 1043 | filter: &'a RoomListFilter, |
| 1044 | search: Option<String>, |
| 1045 | } |
| 1046 | |
| 1047 | impl RoomMatcher<'_> { |
| 1048 | #[must_use] |
| 1049 | pub fn matches(&self, room: &RoomSummary) -> bool { |
| 1050 | let filter = self.filter; |
| 1051 | filter |
| 1052 | .membership |
| 1053 | .is_none_or(|membership| room.membership == membership) |
| 1054 | && filter.category.is_none_or(|category| match category { |
| 1055 | RoomCategory::People => room.direct && !room.space, |
| 1056 | RoomCategory::Rooms => !room.direct && !room.space, |
| 1057 | RoomCategory::Spaces => room.space, |
| 1058 | }) |
| 1059 | && filter |
| 1060 | .favourite |
| 1061 | .is_none_or(|favourite| room.favourite == favourite) |
| 1062 | && (!filter.unread_only || room.unread_messages > 0 || room.unread_mentions > 0) |
| 1063 | && self.search.as_ref().is_none_or(|search| { |
| 1064 | room.name.to_lowercase().contains(search) |
| 1065 | || room.room_id.as_str().to_lowercase().contains(search) |
| 1066 | }) |
| 1067 | } |
| 1068 | } |
| 1069 | |
| 1070 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 1071 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1072 | #[non_exhaustive] |
| 1073 | pub enum RoomCategory { |
| 1074 | People, |
| 1075 | Rooms, |
| 1076 | Spaces, |
| 1077 | } |
| 1078 | |
| 1079 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 1080 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1081 | #[non_exhaustive] |
| 1082 | pub enum MemberFilter { |
| 1083 | Active, |
| 1084 | Joined, |
| 1085 | Invited, |
| 1086 | Banned, |
| 1087 | Left, |
| 1088 | All, |
| 1089 | } |
| 1090 | |
| 1091 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 1092 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1093 | #[non_exhaustive] |
| 1094 | pub enum MemberMembership { |
| 1095 | Joined, |
| 1096 | Invited, |
| 1097 | Left, |
| 1098 | Knocked, |
| 1099 | Banned, |
| 1100 | Custom(String), |
| 1101 | } |
| 1102 | |
| 1103 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 1104 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1105 | #[non_exhaustive] |
| 1106 | pub struct MemberSummary { |
| 1107 | pub user_id: OwnedUserId, |
| 1108 | pub name: String, |
| 1109 | pub display_name: Option<String>, |
| 1110 | pub avatar: Option<OwnedMxcUri>, |
| 1111 | pub membership: MemberMembership, |
| 1112 | pub own: bool, |
| 1113 | pub name_ambiguous: bool, |
| 1114 | pub ignored: bool, |
| 1115 | pub service: bool, |
| 1116 | } |
| 1117 | |
| 1118 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 1119 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1120 | #[non_exhaustive] |
| 1121 | pub struct OwnDevice { |
| 1122 | pub device_id: OwnedDeviceId, |
| 1123 | pub display_name: Option<String>, |
| 1124 | pub last_seen_ip: Option<String>, |
| 1125 | pub last_seen_timestamp_ms: Option<u64>, |
| 1126 | pub current: bool, |
| 1127 | } |
| 1128 | |
| 1129 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 1130 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1131 | #[non_exhaustive] |
| 1132 | pub enum TimelineItem { |
| 1133 | Event(Box<TimelineEvent>), |
| 1134 | DayDivider { timestamp_ms: u64 }, |
| 1135 | ReadMarker, |
| 1136 | TimelineStart, |
| 1137 | } |
| 1138 | |
| 1139 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 1140 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1141 | #[non_exhaustive] |
| 1142 | pub enum TimelineDiff { |
| 1143 | Append { values: Vec<TimelineItem> }, |
| 1144 | Clear, |
| 1145 | PushFront { value: TimelineItem }, |
| 1146 | PushBack { value: TimelineItem }, |
| 1147 | PopFront, |
| 1148 | PopBack, |
| 1149 | Insert { index: usize, value: TimelineItem }, |
| 1150 | Set { index: usize, value: TimelineItem }, |
| 1151 | Remove { index: usize }, |
| 1152 | Truncate { length: usize }, |
| 1153 | Reset { values: Vec<TimelineItem> }, |
| 1154 | } |
| 1155 | |
| 1156 | #[derive(Clone, Copy, Debug, Eq, PartialEq, thiserror::Error)] |
| 1157 | #[error("timeline diff is incompatible with the current {length}-item snapshot")] |
| 1158 | pub struct TimelineDiffError { |
| 1159 | pub length: usize, |
| 1160 | } |
| 1161 | |
| 1162 | impl TimelineDiff { |
| 1163 | /// Apply a diff only when its indices match the current snapshot. |
| 1164 | /// On error the snapshot is unchanged; request `TimelineCommand::Resync` |
| 1165 | /// and ignore further diffs until the corresponding `TimelineReset`. |
| 1166 | pub fn try_apply(self, items: &mut Vector<TimelineItem>) -> Result<(), TimelineDiffError> { |
| 1167 | let length = items.len(); |
| 1168 | if self.length_after(length).is_none() { |
| 1169 | return Err(TimelineDiffError { length }); |
| 1170 | } |
| 1171 | self.apply(items); |
| 1172 | Ok(()) |
| 1173 | } |
| 1174 | |
| 1175 | /// a whole `TimelineChanged` or none of it. whether a diff fits depends only on the |
| 1176 | /// length before it, so the batch is checked against lengths alone before anything |
| 1177 | /// moves. the error carries the length the snapshot would have had at the misfit |
| 1178 | pub fn try_apply_all( |
| 1179 | diffs: Vec<Self>, |
| 1180 | items: &mut Vector<TimelineItem>, |
| 1181 | ) -> Result<(), TimelineDiffError> { |
| 1182 | diffs.iter().try_fold(items.len(), |length, diff| { |
| 1183 | diff.length_after(length) |
| 1184 | .ok_or(TimelineDiffError { length }) |
| 1185 | })?; |
| 1186 | for diff in diffs { |
| 1187 | diff.apply(items); |
| 1188 | } |
| 1189 | Ok(()) |
| 1190 | } |
| 1191 | |
| 1192 | fn length_after(&self, length: usize) -> Option<usize> { |
| 1193 | match self { |
| 1194 | Self::Append { values } => Some(length + values.len()), |
| 1195 | Self::Clear => Some(0), |
| 1196 | Self::PushFront { .. } | Self::PushBack { .. } => Some(length + 1), |
| 1197 | Self::PopFront | Self::PopBack => length.checked_sub(1), |
| 1198 | Self::Insert { index, .. } => (*index <= length).then_some(length + 1), |
| 1199 | Self::Set { index, .. } => (*index < length).then_some(length), |
| 1200 | Self::Remove { index } => (*index < length).then(|| length - 1), |
| 1201 | Self::Truncate { length: target } => (*target <= length).then_some(*target), |
| 1202 | Self::Reset { values } => Some(values.len()), |
| 1203 | } |
| 1204 | } |
| 1205 | |
| 1206 | fn apply(self, items: &mut Vector<TimelineItem>) { |
| 1207 | match self { |
| 1208 | Self::Append { values } => items.extend(values), |
| 1209 | Self::Clear => items.clear(), |
| 1210 | Self::PushFront { value } => items.push_front(value), |
| 1211 | Self::PushBack { value } => items.push_back(value), |
| 1212 | Self::PopFront => { |
| 1213 | items.pop_front(); |
| 1214 | } |
| 1215 | Self::PopBack => { |
| 1216 | items.pop_back(); |
| 1217 | } |
| 1218 | Self::Insert { index, value } => items.insert(index, value), |
| 1219 | Self::Set { index, value } => { |
| 1220 | items.set(index, value); |
| 1221 | } |
| 1222 | Self::Remove { index } => { |
| 1223 | items.remove(index); |
| 1224 | } |
| 1225 | Self::Truncate { length } => items.truncate(length), |
| 1226 | Self::Reset { values } => *items = values.into_iter().collect(), |
| 1227 | } |
| 1228 | } |
| 1229 | } |
| 1230 | |
| 1231 | #[cfg(test)] |
| 1232 | mod tests { |
| 1233 | use imbl::{Vector, vector}; |
| 1234 | |
| 1235 | use super::{TimelineDiff, TimelineItem}; |
| 1236 | |
| 1237 | fn item(timestamp_ms: u64) -> TimelineItem { |
| 1238 | TimelineItem::DayDivider { timestamp_ms } |
| 1239 | } |
| 1240 | |
| 1241 | #[test] |
| 1242 | fn timeline_diff_try_apply_handles_every_variant() { |
| 1243 | let mut items = vector![item(1), item(2)]; |
| 1244 | |
| 1245 | TimelineDiff::Append { |
| 1246 | values: vec![item(3)], |
| 1247 | } |
| 1248 | .try_apply(&mut items) |
| 1249 | .unwrap(); |
| 1250 | assert_eq!(items, vector![item(1), item(2), item(3)]); |
| 1251 | |
| 1252 | TimelineDiff::Clear.try_apply(&mut items).unwrap(); |
| 1253 | assert!(items.is_empty()); |
| 1254 | |
| 1255 | TimelineDiff::PushFront { value: item(2) } |
| 1256 | .try_apply(&mut items) |
| 1257 | .unwrap(); |
| 1258 | TimelineDiff::PushBack { value: item(3) } |
| 1259 | .try_apply(&mut items) |
| 1260 | .unwrap(); |
| 1261 | assert_eq!(items, vector![item(2), item(3)]); |
| 1262 | |
| 1263 | TimelineDiff::PopFront.try_apply(&mut items).unwrap(); |
| 1264 | TimelineDiff::PopBack.try_apply(&mut items).unwrap(); |
| 1265 | assert!(items.is_empty()); |
| 1266 | |
| 1267 | TimelineDiff::Reset { |
| 1268 | values: vec![item(1), item(3)], |
| 1269 | } |
| 1270 | .try_apply(&mut items) |
| 1271 | .unwrap(); |
| 1272 | TimelineDiff::Insert { |
| 1273 | index: 1, |
| 1274 | value: item(2), |
| 1275 | } |
| 1276 | .try_apply(&mut items) |
| 1277 | .unwrap(); |
| 1278 | assert_eq!(items, vector![item(1), item(2), item(3)]); |
| 1279 | |
| 1280 | TimelineDiff::Set { |
| 1281 | index: 1, |
| 1282 | value: item(4), |
| 1283 | } |
| 1284 | .try_apply(&mut items) |
| 1285 | .unwrap(); |
| 1286 | assert_eq!(items, vector![item(1), item(4), item(3)]); |
| 1287 | |
| 1288 | TimelineDiff::Remove { index: 1 } |
| 1289 | .try_apply(&mut items) |
| 1290 | .unwrap(); |
| 1291 | assert_eq!(items, vector![item(1), item(3)]); |
| 1292 | |
| 1293 | TimelineDiff::Truncate { length: 1 } |
| 1294 | .try_apply(&mut items) |
| 1295 | .unwrap(); |
| 1296 | assert_eq!(items, vector![item(1)]); |
| 1297 | } |
| 1298 | |
| 1299 | #[test] |
| 1300 | fn invalid_timeline_diff_leaves_snapshot_unchanged() { |
| 1301 | let invalid = [ |
| 1302 | TimelineDiff::Insert { |
| 1303 | index: 3, |
| 1304 | value: item(3), |
| 1305 | }, |
| 1306 | TimelineDiff::Set { |
| 1307 | index: 2, |
| 1308 | value: item(3), |
| 1309 | }, |
| 1310 | TimelineDiff::Remove { index: 2 }, |
| 1311 | TimelineDiff::Truncate { length: 3 }, |
| 1312 | ]; |
| 1313 | for diff in invalid { |
| 1314 | let mut items = vector![item(1), item(2)]; |
| 1315 | assert_eq!(diff.try_apply(&mut items).unwrap_err().length, 2); |
| 1316 | assert_eq!(items, vector![item(1), item(2)]); |
| 1317 | } |
| 1318 | for diff in [TimelineDiff::PopFront, TimelineDiff::PopBack] { |
| 1319 | let mut items = Vector::new(); |
| 1320 | assert_eq!(diff.try_apply(&mut items).unwrap_err().length, 0); |
| 1321 | assert!(items.is_empty()); |
| 1322 | } |
| 1323 | } |
| 1324 | |
| 1325 | #[test] |
| 1326 | fn a_batch_that_goes_wrong_halfway_changes_nothing() { |
| 1327 | let mut items = vector![item(1), item(2)]; |
| 1328 | let batch = vec![ |
| 1329 | TimelineDiff::PushBack { value: item(3) }, |
| 1330 | TimelineDiff::PushBack { value: item(4) }, |
| 1331 | TimelineDiff::Remove { index: 0 }, |
| 1332 | TimelineDiff::Set { |
| 1333 | index: 3, |
| 1334 | value: item(5), |
| 1335 | }, |
| 1336 | ]; |
| 1337 | assert_eq!( |
| 1338 | TimelineDiff::try_apply_all(batch, &mut items) |
| 1339 | .unwrap_err() |
| 1340 | .length, |
| 1341 | 3 |
| 1342 | ); |
| 1343 | assert_eq!(items, vector![item(1), item(2)]); |
| 1344 | |
| 1345 | let batch = vec![ |
| 1346 | TimelineDiff::PushBack { value: item(3) }, |
| 1347 | TimelineDiff::Remove { index: 0 }, |
| 1348 | TimelineDiff::Set { |
| 1349 | index: 1, |
| 1350 | value: item(4), |
| 1351 | }, |
| 1352 | ]; |
| 1353 | TimelineDiff::try_apply_all(batch, &mut items).unwrap(); |
| 1354 | assert_eq!(items, vector![item(2), item(4)]); |
| 1355 | } |
| 1356 | } |
| 1357 | |
| 1358 | #[derive(Clone, Debug, Eq, Hash, PartialEq)] |
| 1359 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1360 | pub struct TimelineItemId(String); |
| 1361 | |
| 1362 | impl TimelineItemId { |
| 1363 | pub(crate) fn new(value: String) -> Self { |
| 1364 | Self(value) |
| 1365 | } |
| 1366 | |
| 1367 | #[must_use] |
| 1368 | pub fn as_str(&self) -> &str { |
| 1369 | &self.0 |
| 1370 | } |
| 1371 | } |
| 1372 | |
| 1373 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 1374 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1375 | #[non_exhaustive] |
| 1376 | pub struct TimelineEvent { |
| 1377 | pub item_id: TimelineItemId, |
| 1378 | pub event_id: Option<OwnedEventId>, |
| 1379 | pub transaction_id: Option<OwnedTransactionId>, |
| 1380 | pub sender: Sender, |
| 1381 | pub timestamp_ms: u64, |
| 1382 | pub own: bool, |
| 1383 | pub send_state: SendState, |
| 1384 | pub content: TimelineContent, |
| 1385 | pub reply_to: Option<ReplyReference>, |
| 1386 | pub thread_root: Option<OwnedEventId>, |
| 1387 | pub thread_summary: Option<ThreadSummary>, |
| 1388 | pub reactions: Vec<Reaction>, |
| 1389 | pub read_by: Vec<OwnedUserId>, |
| 1390 | pub editable: bool, |
| 1391 | } |
| 1392 | |
| 1393 | impl TimelineEvent { |
| 1394 | /// what to hand edit, redact and react. works before the server has echoed it back |
| 1395 | #[must_use] |
| 1396 | pub fn target(&self) -> Option<EventRef> { |
| 1397 | self.event_id |
| 1398 | .clone() |
| 1399 | .map(EventRef::Remote) |
| 1400 | .or_else(|| self.transaction_id.clone().map(EventRef::Local)) |
| 1401 | } |
| 1402 | } |
| 1403 | |
| 1404 | #[derive(Clone, Debug, Eq, Hash, PartialEq)] |
| 1405 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1406 | #[non_exhaustive] |
| 1407 | pub enum EventRef { |
| 1408 | Remote(OwnedEventId), |
| 1409 | /// a local echo, by the transaction id it was queued under |
| 1410 | Local(OwnedTransactionId), |
| 1411 | } |
| 1412 | |
| 1413 | impl From<OwnedEventId> for EventRef { |
| 1414 | fn from(event_id: OwnedEventId) -> Self { |
| 1415 | Self::Remote(event_id) |
| 1416 | } |
| 1417 | } |
| 1418 | |
| 1419 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 1420 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1421 | #[non_exhaustive] |
| 1422 | pub struct Sender { |
| 1423 | pub user_id: OwnedUserId, |
| 1424 | pub display_name: Option<String>, |
| 1425 | pub avatar: Option<OwnedMxcUri>, |
| 1426 | /// someone else in the room goes by the same name, so show the user id too |
| 1427 | pub name_ambiguous: bool, |
| 1428 | } |
| 1429 | |
| 1430 | impl Sender { |
| 1431 | #[must_use] |
| 1432 | pub fn name(&self) -> &str { |
| 1433 | self.display_name |
| 1434 | .as_deref() |
| 1435 | .unwrap_or_else(|| self.user_id.as_str()) |
| 1436 | } |
| 1437 | } |
| 1438 | |
| 1439 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 1440 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1441 | #[non_exhaustive] |
| 1442 | pub enum TimelineContent { |
| 1443 | Message(Message), |
| 1444 | Sticker { |
| 1445 | body: String, |
| 1446 | media: MediaDescriptor, |
| 1447 | }, |
| 1448 | Poll { |
| 1449 | question: String, |
| 1450 | }, |
| 1451 | Redacted, |
| 1452 | UnableToDecrypt(UtdCause), |
| 1453 | Membership { |
| 1454 | user_id: OwnedUserId, |
| 1455 | display_name: Option<String>, |
| 1456 | change: MembershipChange, |
| 1457 | }, |
| 1458 | Profile { |
| 1459 | user_id: OwnedUserId, |
| 1460 | display_name: Option<Change<Option<String>>>, |
| 1461 | avatar: Option<Change<Option<OwnedMxcUri>>>, |
| 1462 | }, |
| 1463 | /// `None` means it was cleared, or the event was redacted |
| 1464 | RoomName(Option<String>), |
| 1465 | RoomTopic(Option<String>), |
| 1466 | RoomAvatar(Option<OwnedMxcUri>), |
| 1467 | EncryptionEnabled, |
| 1468 | RoomCreated, |
| 1469 | RoomUpgraded { |
| 1470 | successor: OwnedRoomId, |
| 1471 | }, |
| 1472 | OtherState { |
| 1473 | event_type: String, |
| 1474 | state_key: String, |
| 1475 | }, |
| 1476 | Other { |
| 1477 | event_type: String, |
| 1478 | }, |
| 1479 | } |
| 1480 | |
| 1481 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 1482 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1483 | #[non_exhaustive] |
| 1484 | pub struct Message { |
| 1485 | pub kind: MessageKind, |
| 1486 | pub body: String, |
| 1487 | /// only for `MessageKind::Location` |
| 1488 | pub geo_uri: Option<String>, |
| 1489 | pub formatted_body: Option<String>, |
| 1490 | pub mentions: Option<MessageMentions>, |
| 1491 | pub edited: bool, |
| 1492 | pub media: Option<MediaDescriptor>, |
| 1493 | } |
| 1494 | |
| 1495 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 1496 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1497 | #[non_exhaustive] |
| 1498 | pub enum MessageKind { |
| 1499 | Text, |
| 1500 | Emote, |
| 1501 | Notice, |
| 1502 | ServerNotice, |
| 1503 | Image, |
| 1504 | Video, |
| 1505 | Audio, |
| 1506 | File, |
| 1507 | Location, |
| 1508 | VerificationRequest, |
| 1509 | Other(String), |
| 1510 | } |
| 1511 | |
| 1512 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 1513 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1514 | #[non_exhaustive] |
| 1515 | pub enum MembershipChange { |
| 1516 | Joined, |
| 1517 | Left, |
| 1518 | Banned, |
| 1519 | Unbanned, |
| 1520 | Kicked, |
| 1521 | KickedAndBanned, |
| 1522 | Invited, |
| 1523 | InvitationAccepted, |
| 1524 | InvitationRejected, |
| 1525 | InvitationRevoked, |
| 1526 | Knocked, |
| 1527 | KnockAccepted, |
| 1528 | KnockRetracted, |
| 1529 | KnockDenied, |
| 1530 | /// nothing a person would call a change, or something the sdk couldn't make sense of |
| 1531 | Other, |
| 1532 | } |
| 1533 | |
| 1534 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 1535 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1536 | pub struct Change<T> { |
| 1537 | pub old: T, |
| 1538 | pub new: T, |
| 1539 | } |
| 1540 | |
| 1541 | /// why a message couldn't be read, worth telling the user since some of these they can fix |
| 1542 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 1543 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1544 | #[non_exhaustive] |
| 1545 | pub enum UtdCause { |
| 1546 | Unknown, |
| 1547 | SentBeforeWeJoined, |
| 1548 | VerificationViolation, |
| 1549 | UnsignedDevice, |
| 1550 | UnknownDevice, |
| 1551 | HistoricalMessageAndBackupIsDisabled, |
| 1552 | WithheldForUnverifiedOrInsecureDevice, |
| 1553 | WithheldBySender, |
| 1554 | HistoricalMessageAndDeviceIsUnverified, |
| 1555 | } |
| 1556 | |
| 1557 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 1558 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1559 | #[non_exhaustive] |
| 1560 | pub struct ThreadSummary { |
| 1561 | pub reply_count: u32, |
| 1562 | pub latest_event_id: Option<OwnedEventId>, |
| 1563 | pub latest_event_timestamp_ms: Option<u64>, |
| 1564 | } |
| 1565 | |
| 1566 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 1567 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1568 | #[non_exhaustive] |
| 1569 | pub enum SendState { |
| 1570 | Sent, |
| 1571 | Sending, |
| 1572 | /// `will_retry` means macltok has it in hand. otherwise it's wedged until you |
| 1573 | /// `retry_send` or `cancel_send`, so that's when to show a retry button |
| 1574 | Failed { |
| 1575 | will_retry: bool, |
| 1576 | }, |
| 1577 | } |
| 1578 | |
| 1579 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 1580 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1581 | #[non_exhaustive] |
| 1582 | pub struct ReplyReference { |
| 1583 | pub event_id: OwnedEventId, |
| 1584 | pub details: ReplyDetails, |
| 1585 | } |
| 1586 | |
| 1587 | /// the replied-to event. macltok asks for it when it isn't loaded, `Pending` then turns into one of the others |
| 1588 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 1589 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1590 | #[non_exhaustive] |
| 1591 | pub enum ReplyDetails { |
| 1592 | Pending, |
| 1593 | Ready { |
| 1594 | sender: Sender, |
| 1595 | content: Box<TimelineContent>, |
| 1596 | timestamp_ms: u64, |
| 1597 | }, |
| 1598 | Unavailable, |
| 1599 | } |
| 1600 | |
| 1601 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 1602 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1603 | #[non_exhaustive] |
| 1604 | pub struct Reaction { |
| 1605 | pub key: String, |
| 1606 | pub count: usize, |
| 1607 | pub own: bool, |
| 1608 | pub senders: Vec<OwnedUserId>, |
| 1609 | } |
| 1610 | |
| 1611 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 1612 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1613 | #[non_exhaustive] |
| 1614 | pub struct MediaDescriptor { |
| 1615 | pub id: MediaId, |
| 1616 | pub kind: MediaKind, |
| 1617 | pub filename: String, |
| 1618 | pub caption: Option<String>, |
| 1619 | pub mimetype: Option<String>, |
| 1620 | pub size_bytes: Option<u64>, |
| 1621 | pub width: Option<u64>, |
| 1622 | pub height: Option<u64>, |
| 1623 | pub duration_ms: Option<u64>, |
| 1624 | pub blurhash: Option<String>, |
| 1625 | /// the uploader sent a thumbnail, so `MediaSize::Thumbnail` won't need the server to resize |
| 1626 | pub has_thumbnail: bool, |
| 1627 | } |
| 1628 | |
| 1629 | #[derive(Clone, Debug, Eq, Hash, PartialEq)] |
| 1630 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1631 | pub struct MediaId(String); |
| 1632 | |
| 1633 | impl MediaId { |
| 1634 | pub(crate) fn new(value: String) -> Self { |
| 1635 | Self(value) |
| 1636 | } |
| 1637 | |
| 1638 | #[must_use] |
| 1639 | pub fn as_str(&self) -> &str { |
| 1640 | &self.0 |
| 1641 | } |
| 1642 | } |
| 1643 | |
| 1644 | /// media ids are opaque but they do have to survive a trip through a browser. a made up one just fails `MediaUnknown` |
| 1645 | impl std::str::FromStr for MediaId { |
| 1646 | type Err = std::convert::Infallible; |
| 1647 | |
| 1648 | fn from_str(value: &str) -> Result<Self, Self::Err> { |
| 1649 | Ok(Self(value.to_owned())) |
| 1650 | } |
| 1651 | } |
| 1652 | |
| 1653 | impl From<String> for MediaId { |
| 1654 | fn from(value: String) -> Self { |
| 1655 | Self(value) |
| 1656 | } |
| 1657 | } |
| 1658 | |
| 1659 | impl std::fmt::Display for MediaId { |
| 1660 | fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 1661 | self.0.fmt(formatter) |
| 1662 | } |
| 1663 | } |
| 1664 | |
| 1665 | #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] |
| 1666 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1667 | #[non_exhaustive] |
| 1668 | pub enum MediaSize { |
| 1669 | Full, |
| 1670 | /// a hint. an uploader's own thumbnail wins over the server resizing anything |
| 1671 | Thumbnail { |
| 1672 | width: u32, |
| 1673 | height: u32, |
| 1674 | }, |
| 1675 | } |
| 1676 | |
| 1677 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 1678 | #[non_exhaustive] |
| 1679 | pub struct Media { |
| 1680 | pub bytes: Arc<[u8]>, |
| 1681 | pub mimetype: String, |
| 1682 | } |
| 1683 | |
| 1684 | /// where media lives, for a host that fetches and decrypts it itself |
| 1685 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 1686 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1687 | #[non_exhaustive] |
| 1688 | pub enum MediaLocation { |
| 1689 | File(MediaFile), |
| 1690 | /// a resize of plaintext, so never encrypted. plenty of servers refuse some, and `media` |
| 1691 | /// then fetches `fallback` instead, so should you |
| 1692 | ServerThumbnail { |
| 1693 | url: String, |
| 1694 | fallback: MediaFile, |
| 1695 | }, |
| 1696 | /// still in the send queue with no url yet. `media` has the bytes |
| 1697 | Local, |
| 1698 | } |
| 1699 | |
| 1700 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 1701 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1702 | #[non_exhaustive] |
| 1703 | pub struct MediaFile { |
| 1704 | pub url: String, |
| 1705 | pub mimetype: Option<String>, |
| 1706 | pub declared_size: Option<u64>, |
| 1707 | pub encryption: Option<AttachmentEncryption>, |
| 1708 | } |
| 1709 | |
| 1710 | /// AES-256-CTR, the only attachment cipher matrix has |
| 1711 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 1712 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1713 | #[non_exhaustive] |
| 1714 | pub struct AttachmentEncryption { |
| 1715 | /// the JWK `k`, unpadded url-safe base64 |
| 1716 | pub key: String, |
| 1717 | /// unpadded base64, the initial counter block |
| 1718 | pub iv: String, |
| 1719 | /// unpadded base64 SHA-256 of the whole ciphertext, so a partial read goes unchecked |
| 1720 | pub sha256: String, |
| 1721 | } |
| 1722 | |
| 1723 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 1724 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1725 | #[non_exhaustive] |
| 1726 | pub enum MediaKind { |
| 1727 | Image, |
| 1728 | Video, |
| 1729 | Audio, |
| 1730 | File, |
| 1731 | Sticker, |
| 1732 | } |
| 1733 | |
| 1734 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 1735 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1736 | #[non_exhaustive] |
| 1737 | pub struct TypingState { |
| 1738 | pub room_id: OwnedRoomId, |
| 1739 | pub users: Vec<OwnedUserId>, |
| 1740 | } |
| 1741 | |
| 1742 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 1743 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1744 | #[non_exhaustive] |
| 1745 | pub enum HistoryState { |
| 1746 | Idle, |
| 1747 | Loading, |
| 1748 | StartReached, |
| 1749 | Failed(String), |
| 1750 | } |
| 1751 | |
| 1752 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 1753 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1754 | #[non_exhaustive] |
| 1755 | pub struct EncryptionState { |
| 1756 | pub device_id: OwnedDeviceId, |
| 1757 | pub device_verified: bool, |
| 1758 | pub recovery: RecoveryState, |
| 1759 | pub key_backup: KeyBackupState, |
| 1760 | } |
| 1761 | |
| 1762 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 1763 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1764 | #[non_exhaustive] |
| 1765 | pub enum KeyBackupState { |
| 1766 | Unknown, |
| 1767 | Creating, |
| 1768 | Enabling, |
| 1769 | Resuming, |
| 1770 | Downloading, |
| 1771 | Enabled, |
| 1772 | Disabling, |
| 1773 | } |
| 1774 | |
| 1775 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 1776 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1777 | #[non_exhaustive] |
| 1778 | pub enum RecoveryState { |
| 1779 | Unknown, |
| 1780 | Disabled, |
| 1781 | Incomplete, |
| 1782 | Enabled, |
| 1783 | } |
| 1784 | |
| 1785 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 1786 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1787 | #[non_exhaustive] |
| 1788 | pub enum VerificationAction { |
| 1789 | /// take up a request another device sent, which arrives as `VerificationState::Incoming` |
| 1790 | Accept, |
| 1791 | Confirm, |
| 1792 | Reject, |
| 1793 | Cancel, |
| 1794 | } |
| 1795 | |
| 1796 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 1797 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1798 | #[non_exhaustive] |
| 1799 | pub enum VerificationTarget { |
| 1800 | OwnIdentity, |
| 1801 | User(OwnedUserId), |
| 1802 | Device { |
| 1803 | user_id: OwnedUserId, |
| 1804 | device_id: OwnedDeviceId, |
| 1805 | }, |
| 1806 | } |
| 1807 | |
| 1808 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 1809 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1810 | #[non_exhaustive] |
| 1811 | pub enum VerificationState { |
| 1812 | Waiting, |
| 1813 | /// someone else asked. `Accept` to go ahead, `Cancel` to turn them down |
| 1814 | Incoming { |
| 1815 | user_id: OwnedUserId, |
| 1816 | device_id: Option<OwnedDeviceId>, |
| 1817 | }, |
| 1818 | Comparison { |
| 1819 | emojis: Vec<(String, String)>, |
| 1820 | decimals: (u16, u16, u16), |
| 1821 | }, |
| 1822 | Confirmed, |
| 1823 | Done, |
| 1824 | Cancelled(String), |
| 1825 | Failed(VerificationFailure), |
| 1826 | } |
| 1827 | |
| 1828 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 1829 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1830 | #[non_exhaustive] |
| 1831 | pub enum VerificationFailure { |
| 1832 | RequestAcceptance(String), |
| 1833 | SasStartup(String), |
| 1834 | SasAcceptance(String), |
| 1835 | RequestStreamEnded, |
| 1836 | SasStreamEnded, |
| 1837 | } |
| 1838 | |
| 1839 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 1840 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1841 | #[non_exhaustive] |
| 1842 | pub struct EngineFailure { |
| 1843 | pub kind: EngineErrorKind, |
| 1844 | pub message: String, |
| 1845 | } |
| 1846 | |
| 1847 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 1848 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1849 | #[non_exhaustive] |
| 1850 | pub enum EngineErrorKind { |
| 1851 | EventReceiverClosed, |
| 1852 | Watcher, |
| 1853 | Other, |
| 1854 | } |