| 1 | use std::{fmt, sync::Arc}; |
| 2 | |
| 3 | use ruma::{ |
| 4 | OwnedDeviceId, OwnedEventId, OwnedMxcUri, OwnedRoomAliasId, OwnedRoomId, OwnedRoomOrAliasId, |
| 5 | OwnedServerName, OwnedTransactionId, OwnedUserId, |
| 6 | events::{ |
| 7 | AnyGlobalAccountDataEventContent, AnyRoomAccountDataEventContent, AnySyncTimelineEvent, |
| 8 | GlobalAccountDataEventType, RoomAccountDataEventType, |
| 9 | room::member::StrippedRoomMemberEvent, |
| 10 | }, |
| 11 | serde::Raw, |
| 12 | }; |
| 13 | #[cfg(feature = "serde")] |
| 14 | use serde::{Deserialize, Serialize}; |
| 15 | |
| 16 | /// every command answers exactly once through `ClientHandle::send` or `execute`. |
| 17 | /// the event stream only ever carries state |
| 18 | #[derive(Debug)] |
| 19 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 20 | #[non_exhaustive] |
| 21 | pub enum Command { |
| 22 | Account(AccountCommand), |
| 23 | Room(RoomCommand), |
| 24 | Timeline(TimelineCommand), |
| 25 | Media(MediaCommand), |
| 26 | Encryption(EncryptionCommand), |
| 27 | Notification(NotificationCommand), |
| 28 | Lifecycle(LifecycleCommand), |
| 29 | } |
| 30 | |
| 31 | #[derive(Debug)] |
| 32 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 33 | #[non_exhaustive] |
| 34 | pub enum AccountCommand { |
| 35 | Logout, |
| 36 | SetAccountData { |
| 37 | event_type: GlobalAccountDataEventType, |
| 38 | content: Raw<AnyGlobalAccountDataEventContent>, |
| 39 | }, |
| 40 | SetDisplayName(Option<String>), |
| 41 | SetAvatar(Option<OwnedMxcUri>), |
| 42 | /// uploads the image and points the profile at it, same byte cap as any upload |
| 43 | UploadAvatar { |
| 44 | mimetype: String, |
| 45 | bytes: Vec<u8>, |
| 46 | }, |
| 47 | RenameDevice { |
| 48 | device_id: OwnedDeviceId, |
| 49 | display_name: String, |
| 50 | }, |
| 51 | DeleteDevices { |
| 52 | device_ids: Vec<OwnedDeviceId>, |
| 53 | auth: Option<crate::InteractiveAuth>, |
| 54 | }, |
| 55 | } |
| 56 | |
| 57 | #[derive(Clone, Debug)] |
| 58 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 59 | #[non_exhaustive] |
| 60 | pub enum RoomCommand { |
| 61 | /// the snapshot turns up as `Event::TimelineReset`. opening an open room sends a fresh one |
| 62 | Open(OwnedRoomId), |
| 63 | Close(OwnedRoomId), |
| 64 | Join { |
| 65 | room: OwnedRoomOrAliasId, |
| 66 | via: Vec<OwnedServerName>, |
| 67 | }, |
| 68 | Leave(OwnedRoomId), |
| 69 | Forget(OwnedRoomId), |
| 70 | AcceptInvite(OwnedRoomId), |
| 71 | RejectInvite(OwnedRoomId), |
| 72 | InviteUser { |
| 73 | room_id: OwnedRoomId, |
| 74 | user_id: OwnedUserId, |
| 75 | }, |
| 76 | KickUser { |
| 77 | room_id: OwnedRoomId, |
| 78 | user_id: OwnedUserId, |
| 79 | reason: Option<String>, |
| 80 | }, |
| 81 | BanUser { |
| 82 | room_id: OwnedRoomId, |
| 83 | user_id: OwnedUserId, |
| 84 | reason: Option<String>, |
| 85 | }, |
| 86 | UnbanUser { |
| 87 | room_id: OwnedRoomId, |
| 88 | user_id: OwnedUserId, |
| 89 | reason: Option<String>, |
| 90 | }, |
| 91 | SetPowerLevels { |
| 92 | room_id: OwnedRoomId, |
| 93 | changes: PowerLevelChanges, |
| 94 | }, |
| 95 | SetUserPowerLevel { |
| 96 | room_id: OwnedRoomId, |
| 97 | user_id: OwnedUserId, |
| 98 | level: i64, |
| 99 | }, |
| 100 | Create(CreateRoomOptions), |
| 101 | /// hands back the existing dm if there is one |
| 102 | CreateDirect(OwnedUserId), |
| 103 | CancelSend { |
| 104 | room_id: OwnedRoomId, |
| 105 | transaction_id: OwnedTransactionId, |
| 106 | }, |
| 107 | RetrySend { |
| 108 | room_id: OwnedRoomId, |
| 109 | transaction_id: OwnedTransactionId, |
| 110 | }, |
| 111 | SetTyping { |
| 112 | room_id: OwnedRoomId, |
| 113 | typing: bool, |
| 114 | }, |
| 115 | SetAccountData { |
| 116 | room_id: OwnedRoomId, |
| 117 | event_type: RoomAccountDataEventType, |
| 118 | content: Raw<AnyRoomAccountDataEventContent>, |
| 119 | }, |
| 120 | } |
| 121 | |
| 122 | #[derive(Clone, Debug, Default, Eq, PartialEq)] |
| 123 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 124 | #[non_exhaustive] |
| 125 | pub struct CreateRoomOptions { |
| 126 | pub name: Option<String>, |
| 127 | pub topic: Option<String>, |
| 128 | pub alias_localpart: Option<String>, |
| 129 | pub invite: Vec<OwnedUserId>, |
| 130 | pub direct: bool, |
| 131 | pub visibility: RoomVisibility, |
| 132 | pub preset: Option<RoomPreset>, |
| 133 | } |
| 134 | |
| 135 | impl CreateRoomOptions { |
| 136 | #[must_use] |
| 137 | pub fn with_name(mut self, name: impl Into<String>) -> Self { |
| 138 | self.name = Some(name.into()); |
| 139 | self |
| 140 | } |
| 141 | |
| 142 | #[must_use] |
| 143 | pub fn with_topic(mut self, topic: impl Into<String>) -> Self { |
| 144 | self.topic = Some(topic.into()); |
| 145 | self |
| 146 | } |
| 147 | |
| 148 | #[must_use] |
| 149 | pub fn with_alias_localpart(mut self, localpart: impl Into<String>) -> Self { |
| 150 | self.alias_localpart = Some(localpart.into()); |
| 151 | self |
| 152 | } |
| 153 | |
| 154 | #[must_use] |
| 155 | pub fn with_invite(mut self, invite: impl IntoIterator<Item = OwnedUserId>) -> Self { |
| 156 | self.invite = invite.into_iter().collect(); |
| 157 | self |
| 158 | } |
| 159 | |
| 160 | #[must_use] |
| 161 | pub const fn with_direct(mut self, direct: bool) -> Self { |
| 162 | self.direct = direct; |
| 163 | self |
| 164 | } |
| 165 | |
| 166 | #[must_use] |
| 167 | pub const fn with_visibility(mut self, visibility: RoomVisibility) -> Self { |
| 168 | self.visibility = visibility; |
| 169 | self |
| 170 | } |
| 171 | |
| 172 | #[must_use] |
| 173 | pub const fn with_preset(mut self, preset: RoomPreset) -> Self { |
| 174 | self.preset = Some(preset); |
| 175 | self |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | #[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] |
| 180 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 181 | pub enum RoomVisibility { |
| 182 | #[default] |
| 183 | Private, |
| 184 | Public, |
| 185 | } |
| 186 | |
| 187 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 188 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 189 | pub enum RoomPreset { |
| 190 | PrivateChat, |
| 191 | TrustedPrivateChat, |
| 192 | PublicChat, |
| 193 | } |
| 194 | |
| 195 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 196 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 197 | #[non_exhaustive] |
| 198 | pub enum TimelineCommand { |
| 199 | OpenThread { |
| 200 | room_id: OwnedRoomId, |
| 201 | root_event_id: OwnedEventId, |
| 202 | }, |
| 203 | CloseThread { |
| 204 | room_id: OwnedRoomId, |
| 205 | root_event_id: OwnedEventId, |
| 206 | }, |
| 207 | /// Re-emit an authoritative reset for an open room or thread without |
| 208 | /// closing it. The reset arrives before this command's terminal result. |
| 209 | Resync { |
| 210 | timeline_id: TimelineId, |
| 211 | }, |
| 212 | Paginate { |
| 213 | timeline_id: TimelineId, |
| 214 | limit: u16, |
| 215 | }, |
| 216 | Send { |
| 217 | timeline_id: TimelineId, |
| 218 | content: MessageContent, |
| 219 | }, |
| 220 | Edit { |
| 221 | timeline_id: TimelineId, |
| 222 | event_id: OwnedEventId, |
| 223 | content: MessageEditContent, |
| 224 | }, |
| 225 | Redact { |
| 226 | timeline_id: TimelineId, |
| 227 | event_id: OwnedEventId, |
| 228 | reason: Option<String>, |
| 229 | }, |
| 230 | ToggleReaction { |
| 231 | timeline_id: TimelineId, |
| 232 | event_id: OwnedEventId, |
| 233 | key: String, |
| 234 | }, |
| 235 | /// works on a closed room too, it just can't refuse to move the marker backwards |
| 236 | MarkRead { |
| 237 | timeline_id: TimelineId, |
| 238 | event_id: OwnedEventId, |
| 239 | }, |
| 240 | } |
| 241 | |
| 242 | impl TimelineCommand { |
| 243 | #[must_use] |
| 244 | pub const fn room_id(&self) -> &OwnedRoomId { |
| 245 | match self { |
| 246 | Self::OpenThread { room_id, .. } | Self::CloseThread { room_id, .. } => room_id, |
| 247 | Self::Resync { timeline_id } |
| 248 | | Self::Paginate { timeline_id, .. } |
| 249 | | Self::Send { timeline_id, .. } |
| 250 | | Self::Edit { timeline_id, .. } |
| 251 | | Self::Redact { timeline_id, .. } |
| 252 | | Self::ToggleReaction { timeline_id, .. } |
| 253 | | Self::MarkRead { timeline_id, .. } => timeline_id.room_id(), |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | #[must_use] |
| 258 | pub const fn timeline_id(&self) -> Option<&TimelineId> { |
| 259 | match self { |
| 260 | Self::OpenThread { .. } | Self::CloseThread { .. } => None, |
| 261 | Self::Resync { timeline_id } |
| 262 | | Self::Paginate { timeline_id, .. } |
| 263 | | Self::Send { timeline_id, .. } |
| 264 | | Self::Edit { timeline_id, .. } |
| 265 | | Self::Redact { timeline_id, .. } |
| 266 | | Self::ToggleReaction { timeline_id, .. } |
| 267 | | Self::MarkRead { timeline_id, .. } => Some(timeline_id), |
| 268 | } |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | #[derive(Clone, Debug, Eq, Hash, PartialEq)] |
| 273 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 274 | #[non_exhaustive] |
| 275 | pub enum TimelineId { |
| 276 | Room(OwnedRoomId), |
| 277 | Thread { |
| 278 | room_id: OwnedRoomId, |
| 279 | root_event_id: OwnedEventId, |
| 280 | }, |
| 281 | } |
| 282 | |
| 283 | impl TimelineId { |
| 284 | #[must_use] |
| 285 | pub const fn room_id(&self) -> &OwnedRoomId { |
| 286 | match self { |
| 287 | Self::Room(room_id) | Self::Thread { room_id, .. } => room_id, |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | #[must_use] |
| 292 | pub fn thread(room_id: OwnedRoomId, root_event_id: OwnedEventId) -> Self { |
| 293 | Self::Thread { |
| 294 | room_id, |
| 295 | root_event_id, |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | #[must_use] |
| 300 | pub fn thread_root(&self) -> Option<&OwnedEventId> { |
| 301 | match self { |
| 302 | Self::Room(_) => None, |
| 303 | Self::Thread { root_event_id, .. } => Some(root_event_id), |
| 304 | } |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | impl From<OwnedRoomId> for TimelineId { |
| 309 | fn from(room_id: OwnedRoomId) -> Self { |
| 310 | Self::Room(room_id) |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 315 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 316 | #[non_exhaustive] |
| 317 | pub enum MediaCommand { |
| 318 | /// succeeds once the send queue has it. progress and failure arrive as events |
| 319 | Upload(AttachmentUpload), |
| 320 | } |
| 321 | |
| 322 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 323 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 324 | pub struct AttachmentUpload { |
| 325 | pub timeline_id: TimelineId, |
| 326 | pub filename: String, |
| 327 | pub mimetype: String, |
| 328 | pub bytes: Vec<u8>, |
| 329 | pub caption: Option<String>, |
| 330 | pub details: Option<UploadDetails>, |
| 331 | pub thumbnail: Option<UploadThumbnail>, |
| 332 | } |
| 333 | |
| 334 | impl AttachmentUpload { |
| 335 | #[must_use] |
| 336 | pub fn new( |
| 337 | timeline_id: impl Into<TimelineId>, |
| 338 | filename: impl Into<String>, |
| 339 | mimetype: impl Into<String>, |
| 340 | bytes: Vec<u8>, |
| 341 | ) -> Self { |
| 342 | Self { |
| 343 | timeline_id: timeline_id.into(), |
| 344 | filename: filename.into(), |
| 345 | mimetype: mimetype.into(), |
| 346 | bytes, |
| 347 | caption: None, |
| 348 | details: None, |
| 349 | thumbnail: None, |
| 350 | } |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 355 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 356 | #[non_exhaustive] |
| 357 | pub enum UploadDetails { |
| 358 | Image { |
| 359 | width: Option<u32>, |
| 360 | height: Option<u32>, |
| 361 | blurhash: Option<String>, |
| 362 | }, |
| 363 | Video { |
| 364 | width: Option<u32>, |
| 365 | height: Option<u32>, |
| 366 | duration_ms: Option<u64>, |
| 367 | blurhash: Option<String>, |
| 368 | }, |
| 369 | Audio { |
| 370 | duration_ms: Option<u64>, |
| 371 | }, |
| 372 | Voice { |
| 373 | duration_ms: u64, |
| 374 | waveform: Vec<u16>, |
| 375 | }, |
| 376 | File, |
| 377 | } |
| 378 | |
| 379 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 380 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 381 | #[non_exhaustive] |
| 382 | pub struct UploadThumbnail { |
| 383 | pub bytes: Vec<u8>, |
| 384 | pub mimetype: String, |
| 385 | pub width: u32, |
| 386 | pub height: u32, |
| 387 | } |
| 388 | |
| 389 | impl UploadThumbnail { |
| 390 | #[must_use] |
| 391 | pub fn new(bytes: Vec<u8>, mimetype: impl Into<String>, width: u32, height: u32) -> Self { |
| 392 | Self { |
| 393 | bytes, |
| 394 | mimetype: mimetype.into(), |
| 395 | width, |
| 396 | height, |
| 397 | } |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | #[derive(Eq, PartialEq)] |
| 402 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 403 | #[non_exhaustive] |
| 404 | pub enum EncryptionCommand { |
| 405 | Recover(String), |
| 406 | CreateKeyBackup, |
| 407 | EnableKeyBackup, |
| 408 | DisableKeyBackup { delete_from_server: bool }, |
| 409 | RecoverKeyBackup(String), |
| 410 | StartVerification(VerificationTarget), |
| 411 | Verify(VerificationAction), |
| 412 | } |
| 413 | |
| 414 | impl fmt::Debug for EncryptionCommand { |
| 415 | fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 416 | match self { |
| 417 | Self::Recover(_) => formatter.debug_tuple("Recover").field(&"...").finish(), |
| 418 | Self::CreateKeyBackup => formatter.write_str("CreateKeyBackup"), |
| 419 | Self::EnableKeyBackup => formatter.write_str("EnableKeyBackup"), |
| 420 | Self::DisableKeyBackup { delete_from_server } => formatter |
| 421 | .debug_struct("DisableKeyBackup") |
| 422 | .field("delete_from_server", delete_from_server) |
| 423 | .finish(), |
| 424 | Self::RecoverKeyBackup(_) => formatter |
| 425 | .debug_tuple("RecoverKeyBackup") |
| 426 | .field(&"...") |
| 427 | .finish(), |
| 428 | Self::StartVerification(target) => formatter |
| 429 | .debug_tuple("StartVerification") |
| 430 | .field(target) |
| 431 | .finish(), |
| 432 | Self::Verify(action) => formatter.debug_tuple("Verify").field(action).finish(), |
| 433 | } |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 438 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 439 | #[non_exhaustive] |
| 440 | pub enum NotificationCommand { |
| 441 | SetRoomMode { |
| 442 | room_id: OwnedRoomId, |
| 443 | mode: RoomNotificationMode, |
| 444 | }, |
| 445 | ResetRoomMode(OwnedRoomId), |
| 446 | SetDefaultMode { |
| 447 | encrypted: bool, |
| 448 | one_to_one: bool, |
| 449 | mode: RoomNotificationMode, |
| 450 | }, |
| 451 | AddKeyword(String), |
| 452 | RemoveKeyword(String), |
| 453 | } |
| 454 | |
| 455 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 456 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 457 | #[non_exhaustive] |
| 458 | pub enum LifecycleCommand { |
| 459 | Shutdown, |
| 460 | } |
| 461 | |
| 462 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 463 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 464 | #[non_exhaustive] |
| 465 | pub enum CommandOutput { |
| 466 | Done, |
| 467 | Room(OwnedRoomId), |
| 468 | } |
| 469 | |
| 470 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 471 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 472 | #[non_exhaustive] |
| 473 | pub enum MessageContent { |
| 474 | Text { |
| 475 | content: RichText, |
| 476 | mentions: MessageMentions, |
| 477 | }, |
| 478 | Emote { |
| 479 | content: RichText, |
| 480 | mentions: MessageMentions, |
| 481 | }, |
| 482 | Reply { |
| 483 | event_id: OwnedEventId, |
| 484 | content: RichText, |
| 485 | mentions: MessageMentions, |
| 486 | notify_replied_to: bool, |
| 487 | }, |
| 488 | } |
| 489 | |
| 490 | impl MessageContent { |
| 491 | #[must_use] |
| 492 | pub fn text(body: impl Into<String>) -> Self { |
| 493 | Self::Text { |
| 494 | content: RichText::plain(body), |
| 495 | mentions: MessageMentions::default(), |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | #[must_use] |
| 500 | pub fn html(body: impl Into<String>, formatted_body: impl Into<String>) -> Self { |
| 501 | Self::Text { |
| 502 | content: RichText::html(body, formatted_body), |
| 503 | mentions: MessageMentions::default(), |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | #[must_use] |
| 508 | pub fn emote(content: RichText, mentions: MessageMentions) -> Self { |
| 509 | Self::Emote { content, mentions } |
| 510 | } |
| 511 | |
| 512 | #[must_use] |
| 513 | pub fn reply(event_id: OwnedEventId, content: RichText, mentions: MessageMentions) -> Self { |
| 514 | Self::Reply { |
| 515 | event_id, |
| 516 | content, |
| 517 | mentions, |
| 518 | notify_replied_to: true, |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | #[must_use] |
| 523 | pub fn reply_without_notification( |
| 524 | event_id: OwnedEventId, |
| 525 | content: RichText, |
| 526 | mentions: MessageMentions, |
| 527 | ) -> Self { |
| 528 | Self::Reply { |
| 529 | event_id, |
| 530 | content, |
| 531 | mentions, |
| 532 | notify_replied_to: false, |
| 533 | } |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 538 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 539 | #[non_exhaustive] |
| 540 | pub enum MessageEditContent { |
| 541 | Text { |
| 542 | content: RichText, |
| 543 | mentions: MessageMentions, |
| 544 | }, |
| 545 | Emote { |
| 546 | content: RichText, |
| 547 | mentions: MessageMentions, |
| 548 | }, |
| 549 | } |
| 550 | |
| 551 | impl MessageEditContent { |
| 552 | #[must_use] |
| 553 | pub fn text(body: impl Into<String>) -> Self { |
| 554 | Self::Text { |
| 555 | content: RichText::plain(body), |
| 556 | mentions: MessageMentions::default(), |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | #[must_use] |
| 561 | pub fn html(body: impl Into<String>, formatted_body: impl Into<String>) -> Self { |
| 562 | Self::Text { |
| 563 | content: RichText::html(body, formatted_body), |
| 564 | mentions: MessageMentions::default(), |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | #[must_use] |
| 569 | pub fn emote(content: RichText, mentions: MessageMentions) -> Self { |
| 570 | Self::Emote { content, mentions } |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | #[derive(Clone, Debug, Default, Eq, PartialEq)] |
| 575 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 576 | #[non_exhaustive] |
| 577 | pub struct RichText { |
| 578 | pub body: String, |
| 579 | pub formatted_body: Option<String>, |
| 580 | } |
| 581 | |
| 582 | impl RichText { |
| 583 | #[must_use] |
| 584 | pub fn plain(body: impl Into<String>) -> Self { |
| 585 | Self { |
| 586 | body: body.into(), |
| 587 | formatted_body: None, |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | #[must_use] |
| 592 | pub fn html(body: impl Into<String>, formatted_body: impl Into<String>) -> Self { |
| 593 | Self { |
| 594 | body: body.into(), |
| 595 | formatted_body: Some(formatted_body.into()), |
| 596 | } |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | #[derive(Clone, Debug, Default, Eq, PartialEq)] |
| 601 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 602 | #[non_exhaustive] |
| 603 | pub struct MessageMentions { |
| 604 | pub user_ids: Vec<OwnedUserId>, |
| 605 | pub room: bool, |
| 606 | } |
| 607 | |
| 608 | impl MessageMentions { |
| 609 | #[must_use] |
| 610 | pub fn with_user_ids(user_ids: impl IntoIterator<Item = OwnedUserId>) -> Self { |
| 611 | Self { |
| 612 | user_ids: user_ids.into_iter().collect(), |
| 613 | room: false, |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | #[must_use] |
| 618 | pub fn room() -> Self { |
| 619 | Self { |
| 620 | user_ids: Vec::new(), |
| 621 | room: true, |
| 622 | } |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 627 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 628 | #[non_exhaustive] |
| 629 | pub enum Event { |
| 630 | ConnectionChanged(ConnectionState), |
| 631 | Authenticated { |
| 632 | user_id: OwnedUserId, |
| 633 | }, |
| 634 | /// Refresh tokens changed. Persist a fresh `ClientHandle::session_record()` |
| 635 | /// atomically; a record captured before this event may no longer restore. |
| 636 | SessionTokensChanged, |
| 637 | /// the server stopped honouring our token and the engine stops. on a soft |
| 638 | /// logout keep the store and log in again with the same device, otherwise |
| 639 | /// the record and the store are both dead |
| 640 | SessionAccessRejected { |
| 641 | soft_logout: bool, |
| 642 | }, |
| 643 | NotificationSettingsChanged, |
| 644 | VerificationTargetChanged(VerificationTarget), |
| 645 | RoomsChanged, |
| 646 | /// replaces whatever you had for this timeline. every diff after it applies to it |
| 647 | TimelineReset { |
| 648 | timeline_id: TimelineId, |
| 649 | items: Vec<TimelineItem>, |
| 650 | }, |
| 651 | TimelineChanged { |
| 652 | timeline_id: TimelineId, |
| 653 | diffs: Vec<TimelineDiff>, |
| 654 | }, |
| 655 | HistoryChanged { |
| 656 | timeline_id: TimelineId, |
| 657 | state: HistoryState, |
| 658 | }, |
| 659 | TypingChanged(TypingState), |
| 660 | MediaUploadProgress { |
| 661 | room_id: OwnedRoomId, |
| 662 | transaction_id: OwnedTransactionId, |
| 663 | uploaded_bytes: u64, |
| 664 | total_bytes: u64, |
| 665 | complete: bool, |
| 666 | }, |
| 667 | MediaUploadFailed { |
| 668 | room_id: OwnedRoomId, |
| 669 | transaction_id: OwnedTransactionId, |
| 670 | kind: crate::ErrorKind, |
| 671 | recoverable: bool, |
| 672 | }, |
| 673 | EncryptionChanged(EncryptionState), |
| 674 | VerificationChanged(VerificationState), |
| 675 | /// logout went through and the engine stops. delete the record and the store |
| 676 | LoggedOut, |
| 677 | Failed(EngineFailure), |
| 678 | /// always the last event. keep the saved record unless `LoggedOut` or a |
| 679 | /// hard `SessionAccessRejected` came first. commands still in flight get no result |
| 680 | Stopped, |
| 681 | } |
| 682 | |
| 683 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 684 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 685 | #[non_exhaustive] |
| 686 | pub enum ConnectionState { |
| 687 | Idle, |
| 688 | Connecting, |
| 689 | Online, |
| 690 | Offline, |
| 691 | Stopped, |
| 692 | } |
| 693 | |
| 694 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 695 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 696 | #[non_exhaustive] |
| 697 | pub struct RoomSummary { |
| 698 | pub room_id: OwnedRoomId, |
| 699 | pub name: String, |
| 700 | pub membership: RoomMembership, |
| 701 | pub unread_messages: u64, |
| 702 | pub unread_mentions: u64, |
| 703 | pub encrypted: bool, |
| 704 | pub space: bool, |
| 705 | pub direct: bool, |
| 706 | pub favourite: bool, |
| 707 | pub low_priority: bool, |
| 708 | pub avatar: Option<OwnedMxcUri>, |
| 709 | pub latest_event_timestamp_ms: Option<u64>, |
| 710 | pub latest_event: Option<LatestEvent>, |
| 711 | pub notification_mode: Option<RoomNotificationMode>, |
| 712 | pub notification_mode_custom: bool, |
| 713 | pub topic: Option<String>, |
| 714 | pub canonical_alias: Option<OwnedRoomAliasId>, |
| 715 | pub joined_members: u64, |
| 716 | pub invited_members: u64, |
| 717 | /// who a nameless room gets named after |
| 718 | pub heroes: Vec<RoomHero>, |
| 719 | /// set once the room has been upgraded, this is where everyone went |
| 720 | pub successor: Option<OwnedRoomId>, |
| 721 | pub inviter: Option<OwnedUserId>, |
| 722 | /// joined spaces that list this room as a child |
| 723 | pub parent_spaces: Vec<OwnedRoomId>, |
| 724 | /// for a space, the children it lists, joined or not |
| 725 | pub children: Vec<OwnedRoomId>, |
| 726 | } |
| 727 | |
| 728 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 729 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 730 | #[non_exhaustive] |
| 731 | pub struct RoomHero { |
| 732 | pub user_id: OwnedUserId, |
| 733 | pub display_name: Option<String>, |
| 734 | pub avatar: Option<OwnedMxcUri>, |
| 735 | } |
| 736 | |
| 737 | /// enough for a room list preview line. `body` is empty for anything that isn't a plain message |
| 738 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 739 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 740 | #[non_exhaustive] |
| 741 | pub struct LatestEvent { |
| 742 | pub sender: Option<OwnedUserId>, |
| 743 | pub body: Option<String>, |
| 744 | pub timestamp_ms: Option<u64>, |
| 745 | pub send_state: SendState, |
| 746 | } |
| 747 | |
| 748 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 749 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 750 | #[non_exhaustive] |
| 751 | pub struct Profile { |
| 752 | pub user_id: OwnedUserId, |
| 753 | pub display_name: Option<String>, |
| 754 | pub avatar: Option<OwnedMxcUri>, |
| 755 | } |
| 756 | |
| 757 | /// one room in a space's hierarchy, as the server describes it, which may be a room you're not in |
| 758 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 759 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 760 | #[non_exhaustive] |
| 761 | pub struct SpaceChild { |
| 762 | pub room_id: OwnedRoomId, |
| 763 | pub name: String, |
| 764 | pub topic: Option<String>, |
| 765 | pub avatar: Option<OwnedMxcUri>, |
| 766 | pub canonical_alias: Option<OwnedRoomAliasId>, |
| 767 | pub joined_members: u64, |
| 768 | pub space: bool, |
| 769 | pub children_count: u64, |
| 770 | pub membership: Option<RoomMembership>, |
| 771 | pub via: Vec<OwnedServerName>, |
| 772 | pub suggested: bool, |
| 773 | pub world_readable: Option<bool>, |
| 774 | pub guest_can_join: bool, |
| 775 | } |
| 776 | |
| 777 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 778 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 779 | #[non_exhaustive] |
| 780 | pub enum RoomNotificationMode { |
| 781 | AllMessages, |
| 782 | MentionsAndKeywordsOnly, |
| 783 | Mute, |
| 784 | } |
| 785 | |
| 786 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 787 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 788 | #[non_exhaustive] |
| 789 | pub struct RoomNotificationSettings { |
| 790 | pub mode: RoomNotificationMode, |
| 791 | pub custom: bool, |
| 792 | } |
| 793 | |
| 794 | #[derive(Clone, Debug)] |
| 795 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 796 | #[non_exhaustive] |
| 797 | pub enum PushNotificationStatus { |
| 798 | Event(Box<PushNotification>), |
| 799 | NotFound, |
| 800 | FilteredOut, |
| 801 | Redacted, |
| 802 | } |
| 803 | |
| 804 | #[derive(Clone, Debug)] |
| 805 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 806 | #[non_exhaustive] |
| 807 | pub struct PushNotification { |
| 808 | pub event_id: OwnedEventId, |
| 809 | pub event: PushNotificationEvent, |
| 810 | pub sender: PushNotificationSender, |
| 811 | pub room: PushNotificationRoom, |
| 812 | pub noisy: Option<bool>, |
| 813 | pub has_mention: Option<bool>, |
| 814 | pub thread_id: Option<OwnedEventId>, |
| 815 | } |
| 816 | |
| 817 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 818 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 819 | #[non_exhaustive] |
| 820 | pub struct PushNotificationSender { |
| 821 | pub user_id: OwnedUserId, |
| 822 | pub display_name: Option<String>, |
| 823 | pub avatar_url: Option<OwnedMxcUri>, |
| 824 | pub name_ambiguous: bool, |
| 825 | } |
| 826 | |
| 827 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 828 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 829 | #[non_exhaustive] |
| 830 | pub struct PushNotificationRoom { |
| 831 | pub room_id: OwnedRoomId, |
| 832 | pub name: String, |
| 833 | pub avatar_url: Option<OwnedMxcUri>, |
| 834 | pub canonical_alias: Option<OwnedRoomAliasId>, |
| 835 | pub topic: Option<String>, |
| 836 | pub encrypted: Option<bool>, |
| 837 | pub direct: bool, |
| 838 | pub joined_members_count: u64, |
| 839 | pub service_members: Vec<OwnedUserId>, |
| 840 | pub active_service_members_count: u64, |
| 841 | pub space: bool, |
| 842 | } |
| 843 | |
| 844 | #[derive(Clone, Debug)] |
| 845 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 846 | #[non_exhaustive] |
| 847 | pub enum PushNotificationEvent { |
| 848 | Timeline(Raw<AnySyncTimelineEvent>), |
| 849 | Invite(Raw<StrippedRoomMemberEvent>), |
| 850 | } |
| 851 | |
| 852 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 853 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 854 | #[non_exhaustive] |
| 855 | pub struct RoomPowerLevels { |
| 856 | pub ban: i64, |
| 857 | pub invite: i64, |
| 858 | pub kick: i64, |
| 859 | pub redact: i64, |
| 860 | pub events_default: i64, |
| 861 | pub state_default: i64, |
| 862 | pub users_default: i64, |
| 863 | pub room_notification: i64, |
| 864 | pub users: Vec<(OwnedUserId, i64)>, |
| 865 | pub events: Vec<(String, i64)>, |
| 866 | } |
| 867 | |
| 868 | #[derive(Clone, Debug, Default, Eq, PartialEq)] |
| 869 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 870 | #[non_exhaustive] |
| 871 | pub struct PowerLevelChanges { |
| 872 | pub ban: Option<i64>, |
| 873 | pub invite: Option<i64>, |
| 874 | pub kick: Option<i64>, |
| 875 | pub redact: Option<i64>, |
| 876 | pub events_default: Option<i64>, |
| 877 | pub state_default: Option<i64>, |
| 878 | pub users_default: Option<i64>, |
| 879 | pub room_notification: Option<i64>, |
| 880 | pub room_name: Option<i64>, |
| 881 | pub room_avatar: Option<i64>, |
| 882 | pub room_topic: Option<i64>, |
| 883 | pub space_child: Option<i64>, |
| 884 | pub beacon: Option<i64>, |
| 885 | pub beacon_info: Option<i64>, |
| 886 | } |
| 887 | |
| 888 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 889 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 890 | #[non_exhaustive] |
| 891 | pub enum RoomMembership { |
| 892 | Joined, |
| 893 | Invited, |
| 894 | Left, |
| 895 | Knocked, |
| 896 | Banned, |
| 897 | } |
| 898 | |
| 899 | #[derive(Clone, Debug, Default, Eq, PartialEq)] |
| 900 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 901 | #[non_exhaustive] |
| 902 | pub struct RoomListFilter { |
| 903 | pub membership: Option<RoomMembership>, |
| 904 | pub category: Option<RoomCategory>, |
| 905 | pub favourite: Option<bool>, |
| 906 | pub unread_only: bool, |
| 907 | pub search: Option<String>, |
| 908 | } |
| 909 | |
| 910 | impl RoomListFilter { |
| 911 | #[must_use] |
| 912 | pub fn with_membership(mut self, membership: RoomMembership) -> Self { |
| 913 | self.membership = Some(membership); |
| 914 | self |
| 915 | } |
| 916 | |
| 917 | #[must_use] |
| 918 | pub fn with_category(mut self, category: RoomCategory) -> Self { |
| 919 | self.category = Some(category); |
| 920 | self |
| 921 | } |
| 922 | |
| 923 | #[must_use] |
| 924 | pub fn with_favourite(mut self, favourite: bool) -> Self { |
| 925 | self.favourite = Some(favourite); |
| 926 | self |
| 927 | } |
| 928 | |
| 929 | #[must_use] |
| 930 | pub const fn with_unread_only(mut self, unread_only: bool) -> Self { |
| 931 | self.unread_only = unread_only; |
| 932 | self |
| 933 | } |
| 934 | |
| 935 | #[must_use] |
| 936 | pub fn with_search(mut self, search: impl Into<String>) -> Self { |
| 937 | self.search = Some(search.into()); |
| 938 | self |
| 939 | } |
| 940 | |
| 941 | #[must_use] |
| 942 | pub fn matches(&self, room: &RoomSummary) -> bool { |
| 943 | self.matcher().matches(room) |
| 944 | } |
| 945 | |
| 946 | /// lowercases the search once, for filtering a whole list |
| 947 | #[must_use] |
| 948 | pub fn matcher(&self) -> RoomMatcher<'_> { |
| 949 | RoomMatcher { |
| 950 | filter: self, |
| 951 | search: self.search.as_deref().map(str::to_lowercase), |
| 952 | } |
| 953 | } |
| 954 | } |
| 955 | |
| 956 | #[derive(Debug)] |
| 957 | pub struct RoomMatcher<'a> { |
| 958 | filter: &'a RoomListFilter, |
| 959 | search: Option<String>, |
| 960 | } |
| 961 | |
| 962 | impl RoomMatcher<'_> { |
| 963 | #[must_use] |
| 964 | pub fn matches(&self, room: &RoomSummary) -> bool { |
| 965 | let filter = self.filter; |
| 966 | filter |
| 967 | .membership |
| 968 | .is_none_or(|membership| room.membership == membership) |
| 969 | && filter.category.is_none_or(|category| match category { |
| 970 | RoomCategory::People => room.direct && !room.space, |
| 971 | RoomCategory::Rooms => !room.direct && !room.space, |
| 972 | RoomCategory::Spaces => room.space, |
| 973 | }) |
| 974 | && filter |
| 975 | .favourite |
| 976 | .is_none_or(|favourite| room.favourite == favourite) |
| 977 | && (!filter.unread_only || room.unread_messages > 0 || room.unread_mentions > 0) |
| 978 | && self.search.as_ref().is_none_or(|search| { |
| 979 | room.name.to_lowercase().contains(search) |
| 980 | || room.room_id.as_str().to_lowercase().contains(search) |
| 981 | }) |
| 982 | } |
| 983 | } |
| 984 | |
| 985 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 986 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 987 | pub enum RoomCategory { |
| 988 | People, |
| 989 | Rooms, |
| 990 | Spaces, |
| 991 | } |
| 992 | |
| 993 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 994 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 995 | pub enum MemberFilter { |
| 996 | Active, |
| 997 | Joined, |
| 998 | Invited, |
| 999 | All, |
| 1000 | } |
| 1001 | |
| 1002 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 1003 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1004 | #[non_exhaustive] |
| 1005 | pub enum MemberMembership { |
| 1006 | Joined, |
| 1007 | Invited, |
| 1008 | Left, |
| 1009 | Knocked, |
| 1010 | Banned, |
| 1011 | Custom(String), |
| 1012 | } |
| 1013 | |
| 1014 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 1015 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1016 | #[non_exhaustive] |
| 1017 | pub struct MemberSummary { |
| 1018 | pub user_id: OwnedUserId, |
| 1019 | pub name: String, |
| 1020 | pub display_name: Option<String>, |
| 1021 | pub avatar: Option<OwnedMxcUri>, |
| 1022 | pub membership: MemberMembership, |
| 1023 | pub own: bool, |
| 1024 | pub name_ambiguous: bool, |
| 1025 | pub ignored: bool, |
| 1026 | pub service: bool, |
| 1027 | } |
| 1028 | |
| 1029 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 1030 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1031 | #[non_exhaustive] |
| 1032 | pub struct OwnDevice { |
| 1033 | pub device_id: OwnedDeviceId, |
| 1034 | pub display_name: Option<String>, |
| 1035 | pub last_seen_ip: Option<String>, |
| 1036 | pub last_seen_timestamp_ms: Option<u64>, |
| 1037 | pub current: bool, |
| 1038 | } |
| 1039 | |
| 1040 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 1041 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1042 | #[non_exhaustive] |
| 1043 | pub enum TimelineItem { |
| 1044 | Message(Box<TimelineMessage>), |
| 1045 | DayDivider { timestamp_ms: u64 }, |
| 1046 | ReadMarker, |
| 1047 | TimelineStart, |
| 1048 | } |
| 1049 | |
| 1050 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 1051 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1052 | #[non_exhaustive] |
| 1053 | pub enum TimelineDiff { |
| 1054 | Append { values: Vec<TimelineItem> }, |
| 1055 | Clear, |
| 1056 | PushFront { value: TimelineItem }, |
| 1057 | PushBack { value: TimelineItem }, |
| 1058 | PopFront, |
| 1059 | PopBack, |
| 1060 | Insert { index: usize, value: TimelineItem }, |
| 1061 | Set { index: usize, value: TimelineItem }, |
| 1062 | Remove { index: usize }, |
| 1063 | Truncate { length: usize }, |
| 1064 | Reset { values: Vec<TimelineItem> }, |
| 1065 | } |
| 1066 | |
| 1067 | #[derive(Clone, Copy, Debug, Eq, PartialEq, thiserror::Error)] |
| 1068 | #[error("timeline diff is incompatible with the current {length}-item snapshot")] |
| 1069 | pub struct TimelineDiffError { |
| 1070 | pub length: usize, |
| 1071 | } |
| 1072 | |
| 1073 | impl TimelineDiff { |
| 1074 | /// Apply a diff only when its indices match the current snapshot. |
| 1075 | /// On error the snapshot is unchanged; request `TimelineCommand::Resync` |
| 1076 | /// and ignore further diffs until the corresponding `TimelineReset`. |
| 1077 | pub fn try_apply(self, items: &mut Vec<TimelineItem>) -> Result<(), TimelineDiffError> { |
| 1078 | let length = items.len(); |
| 1079 | match self { |
| 1080 | Self::Append { values } => items.extend(values), |
| 1081 | Self::Clear => items.clear(), |
| 1082 | Self::PushFront { value } => items.insert(0, value), |
| 1083 | Self::PushBack { value } => items.push(value), |
| 1084 | Self::PopFront if length == 0 => return Err(TimelineDiffError { length }), |
| 1085 | Self::PopFront => { |
| 1086 | items.remove(0); |
| 1087 | } |
| 1088 | Self::PopBack if length == 0 => return Err(TimelineDiffError { length }), |
| 1089 | Self::PopBack => { |
| 1090 | items.pop(); |
| 1091 | } |
| 1092 | Self::Insert { index, .. } if index > length => { |
| 1093 | return Err(TimelineDiffError { length }); |
| 1094 | } |
| 1095 | Self::Insert { index, value } => items.insert(index, value), |
| 1096 | Self::Set { index, .. } if index >= length => return Err(TimelineDiffError { length }), |
| 1097 | Self::Set { index, value } => items[index] = value, |
| 1098 | Self::Remove { index } if index >= length => return Err(TimelineDiffError { length }), |
| 1099 | Self::Remove { index } => { |
| 1100 | items.remove(index); |
| 1101 | } |
| 1102 | Self::Truncate { length: target } if target > length => { |
| 1103 | return Err(TimelineDiffError { length }); |
| 1104 | } |
| 1105 | Self::Truncate { length } => items.truncate(length), |
| 1106 | Self::Reset { values } => *items = values, |
| 1107 | } |
| 1108 | Ok(()) |
| 1109 | } |
| 1110 | } |
| 1111 | |
| 1112 | #[cfg(test)] |
| 1113 | mod tests { |
| 1114 | use super::{TimelineDiff, TimelineItem}; |
| 1115 | |
| 1116 | fn item(timestamp_ms: u64) -> TimelineItem { |
| 1117 | TimelineItem::DayDivider { timestamp_ms } |
| 1118 | } |
| 1119 | |
| 1120 | #[test] |
| 1121 | fn timeline_diff_try_apply_handles_every_variant() { |
| 1122 | let mut items = vec![item(1), item(2)]; |
| 1123 | |
| 1124 | TimelineDiff::Append { |
| 1125 | values: vec![item(3)], |
| 1126 | } |
| 1127 | .try_apply(&mut items) |
| 1128 | .unwrap(); |
| 1129 | assert_eq!(items, vec![item(1), item(2), item(3)]); |
| 1130 | |
| 1131 | TimelineDiff::Clear.try_apply(&mut items).unwrap(); |
| 1132 | assert!(items.is_empty()); |
| 1133 | |
| 1134 | TimelineDiff::PushFront { value: item(2) } |
| 1135 | .try_apply(&mut items) |
| 1136 | .unwrap(); |
| 1137 | TimelineDiff::PushBack { value: item(3) } |
| 1138 | .try_apply(&mut items) |
| 1139 | .unwrap(); |
| 1140 | assert_eq!(items, vec![item(2), item(3)]); |
| 1141 | |
| 1142 | TimelineDiff::PopFront.try_apply(&mut items).unwrap(); |
| 1143 | TimelineDiff::PopBack.try_apply(&mut items).unwrap(); |
| 1144 | assert!(items.is_empty()); |
| 1145 | |
| 1146 | TimelineDiff::Reset { |
| 1147 | values: vec![item(1), item(3)], |
| 1148 | } |
| 1149 | .try_apply(&mut items) |
| 1150 | .unwrap(); |
| 1151 | TimelineDiff::Insert { |
| 1152 | index: 1, |
| 1153 | value: item(2), |
| 1154 | } |
| 1155 | .try_apply(&mut items) |
| 1156 | .unwrap(); |
| 1157 | assert_eq!(items, vec![item(1), item(2), item(3)]); |
| 1158 | |
| 1159 | TimelineDiff::Set { |
| 1160 | index: 1, |
| 1161 | value: item(4), |
| 1162 | } |
| 1163 | .try_apply(&mut items) |
| 1164 | .unwrap(); |
| 1165 | assert_eq!(items, vec![item(1), item(4), item(3)]); |
| 1166 | |
| 1167 | TimelineDiff::Remove { index: 1 } |
| 1168 | .try_apply(&mut items) |
| 1169 | .unwrap(); |
| 1170 | assert_eq!(items, vec![item(1), item(3)]); |
| 1171 | |
| 1172 | TimelineDiff::Truncate { length: 1 } |
| 1173 | .try_apply(&mut items) |
| 1174 | .unwrap(); |
| 1175 | assert_eq!(items, vec![item(1)]); |
| 1176 | } |
| 1177 | |
| 1178 | #[test] |
| 1179 | fn invalid_timeline_diff_leaves_snapshot_unchanged() { |
| 1180 | let invalid = [ |
| 1181 | TimelineDiff::Insert { |
| 1182 | index: 3, |
| 1183 | value: item(3), |
| 1184 | }, |
| 1185 | TimelineDiff::Set { |
| 1186 | index: 2, |
| 1187 | value: item(3), |
| 1188 | }, |
| 1189 | TimelineDiff::Remove { index: 2 }, |
| 1190 | TimelineDiff::Truncate { length: 3 }, |
| 1191 | ]; |
| 1192 | for diff in invalid { |
| 1193 | let mut items = vec![item(1), item(2)]; |
| 1194 | assert_eq!(diff.try_apply(&mut items).unwrap_err().length, 2); |
| 1195 | assert_eq!(items, vec![item(1), item(2)]); |
| 1196 | } |
| 1197 | for diff in [TimelineDiff::PopFront, TimelineDiff::PopBack] { |
| 1198 | let mut items = Vec::new(); |
| 1199 | assert_eq!(diff.try_apply(&mut items).unwrap_err().length, 0); |
| 1200 | assert!(items.is_empty()); |
| 1201 | } |
| 1202 | } |
| 1203 | } |
| 1204 | |
| 1205 | #[derive(Clone, Debug, Eq, Hash, PartialEq)] |
| 1206 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1207 | pub struct TimelineItemId(String); |
| 1208 | |
| 1209 | impl TimelineItemId { |
| 1210 | pub(crate) fn new(value: String) -> Self { |
| 1211 | Self(value) |
| 1212 | } |
| 1213 | |
| 1214 | #[must_use] |
| 1215 | pub fn as_str(&self) -> &str { |
| 1216 | &self.0 |
| 1217 | } |
| 1218 | } |
| 1219 | |
| 1220 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 1221 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1222 | #[non_exhaustive] |
| 1223 | pub struct TimelineMessage { |
| 1224 | pub item_id: TimelineItemId, |
| 1225 | pub event_id: Option<OwnedEventId>, |
| 1226 | pub transaction_id: Option<OwnedTransactionId>, |
| 1227 | pub sender_id: OwnedUserId, |
| 1228 | pub sender_name: String, |
| 1229 | pub body: String, |
| 1230 | pub formatted_body: Option<String>, |
| 1231 | pub mentions: Option<MessageMentions>, |
| 1232 | pub thread_root: Option<OwnedEventId>, |
| 1233 | pub thread_summary: Option<ThreadSummary>, |
| 1234 | pub timestamp_ms: u64, |
| 1235 | pub own: bool, |
| 1236 | pub send_state: SendState, |
| 1237 | pub edited: bool, |
| 1238 | pub unable_to_decrypt: bool, |
| 1239 | pub media: Option<MediaDescriptor>, |
| 1240 | pub reply_to: Option<ReplyReference>, |
| 1241 | pub reactions: Vec<Reaction>, |
| 1242 | pub read_by: Vec<OwnedUserId>, |
| 1243 | } |
| 1244 | |
| 1245 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 1246 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1247 | #[non_exhaustive] |
| 1248 | pub struct ThreadSummary { |
| 1249 | pub reply_count: u32, |
| 1250 | pub latest_event_id: Option<OwnedEventId>, |
| 1251 | pub latest_event_timestamp_ms: Option<u64>, |
| 1252 | } |
| 1253 | |
| 1254 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 1255 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1256 | #[non_exhaustive] |
| 1257 | pub enum SendState { |
| 1258 | Sent, |
| 1259 | Queued, |
| 1260 | Sending, |
| 1261 | Failed { retriable: bool }, |
| 1262 | } |
| 1263 | |
| 1264 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 1265 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1266 | #[non_exhaustive] |
| 1267 | pub struct ReplyReference { |
| 1268 | pub event_id: OwnedEventId, |
| 1269 | pub sender_id: Option<OwnedUserId>, |
| 1270 | pub sender_name: Option<String>, |
| 1271 | } |
| 1272 | |
| 1273 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 1274 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1275 | #[non_exhaustive] |
| 1276 | pub struct Reaction { |
| 1277 | pub key: String, |
| 1278 | pub count: usize, |
| 1279 | pub own: bool, |
| 1280 | pub senders: Vec<OwnedUserId>, |
| 1281 | } |
| 1282 | |
| 1283 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 1284 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1285 | #[non_exhaustive] |
| 1286 | pub struct MediaDescriptor { |
| 1287 | pub id: MediaId, |
| 1288 | pub kind: MediaKind, |
| 1289 | pub filename: String, |
| 1290 | pub caption: Option<String>, |
| 1291 | pub mimetype: Option<String>, |
| 1292 | pub size_bytes: Option<u64>, |
| 1293 | pub width: Option<u64>, |
| 1294 | pub height: Option<u64>, |
| 1295 | } |
| 1296 | |
| 1297 | #[derive(Clone, Debug, Eq, Hash, PartialEq)] |
| 1298 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1299 | pub struct MediaId(String); |
| 1300 | |
| 1301 | impl MediaId { |
| 1302 | pub(crate) fn new(value: String) -> Self { |
| 1303 | Self(value) |
| 1304 | } |
| 1305 | |
| 1306 | #[must_use] |
| 1307 | pub fn as_str(&self) -> &str { |
| 1308 | &self.0 |
| 1309 | } |
| 1310 | } |
| 1311 | |
| 1312 | impl std::fmt::Display for MediaId { |
| 1313 | fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 1314 | self.0.fmt(formatter) |
| 1315 | } |
| 1316 | } |
| 1317 | |
| 1318 | #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] |
| 1319 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1320 | pub enum MediaSize { |
| 1321 | Full, |
| 1322 | /// a hint. an uploader's own thumbnail wins over the server resizing anything |
| 1323 | Thumbnail { |
| 1324 | width: u32, |
| 1325 | height: u32, |
| 1326 | }, |
| 1327 | } |
| 1328 | |
| 1329 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 1330 | #[non_exhaustive] |
| 1331 | pub struct Media { |
| 1332 | pub bytes: Arc<[u8]>, |
| 1333 | pub mimetype: String, |
| 1334 | } |
| 1335 | |
| 1336 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 1337 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1338 | #[non_exhaustive] |
| 1339 | pub enum MediaKind { |
| 1340 | Image, |
| 1341 | Video, |
| 1342 | Audio, |
| 1343 | File, |
| 1344 | Sticker, |
| 1345 | } |
| 1346 | |
| 1347 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 1348 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1349 | #[non_exhaustive] |
| 1350 | pub struct TypingState { |
| 1351 | pub room_id: OwnedRoomId, |
| 1352 | pub users: Vec<OwnedUserId>, |
| 1353 | } |
| 1354 | |
| 1355 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 1356 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1357 | #[non_exhaustive] |
| 1358 | pub enum HistoryState { |
| 1359 | Idle, |
| 1360 | Loading, |
| 1361 | StartReached, |
| 1362 | Failed(String), |
| 1363 | } |
| 1364 | |
| 1365 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 1366 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1367 | #[non_exhaustive] |
| 1368 | pub struct EncryptionState { |
| 1369 | pub device_id: OwnedDeviceId, |
| 1370 | pub device_verified: bool, |
| 1371 | pub recovery: RecoveryState, |
| 1372 | pub key_backup: KeyBackupState, |
| 1373 | } |
| 1374 | |
| 1375 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 1376 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1377 | #[non_exhaustive] |
| 1378 | pub enum KeyBackupState { |
| 1379 | Unknown, |
| 1380 | Creating, |
| 1381 | Enabling, |
| 1382 | Resuming, |
| 1383 | Downloading, |
| 1384 | Enabled, |
| 1385 | Disabling, |
| 1386 | } |
| 1387 | |
| 1388 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 1389 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1390 | #[non_exhaustive] |
| 1391 | pub enum RecoveryState { |
| 1392 | Unknown, |
| 1393 | Disabled, |
| 1394 | Incomplete, |
| 1395 | Enabled, |
| 1396 | } |
| 1397 | |
| 1398 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 1399 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1400 | pub enum VerificationAction { |
| 1401 | Confirm, |
| 1402 | Reject, |
| 1403 | Cancel, |
| 1404 | } |
| 1405 | |
| 1406 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 1407 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1408 | #[non_exhaustive] |
| 1409 | pub enum VerificationTarget { |
| 1410 | OwnIdentity, |
| 1411 | User(OwnedUserId), |
| 1412 | Device { |
| 1413 | user_id: OwnedUserId, |
| 1414 | device_id: OwnedDeviceId, |
| 1415 | }, |
| 1416 | } |
| 1417 | |
| 1418 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 1419 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1420 | #[non_exhaustive] |
| 1421 | pub enum VerificationState { |
| 1422 | Waiting, |
| 1423 | Comparison { |
| 1424 | emojis: Vec<(String, String)>, |
| 1425 | decimals: (u16, u16, u16), |
| 1426 | }, |
| 1427 | Confirmed, |
| 1428 | Done, |
| 1429 | Cancelled(String), |
| 1430 | Failed(VerificationFailure), |
| 1431 | } |
| 1432 | |
| 1433 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 1434 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1435 | #[non_exhaustive] |
| 1436 | pub enum VerificationFailure { |
| 1437 | RequestAcceptance(String), |
| 1438 | SasStartup(String), |
| 1439 | SasAcceptance(String), |
| 1440 | RequestStreamEnded, |
| 1441 | SasStreamEnded, |
| 1442 | } |
| 1443 | |
| 1444 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 1445 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1446 | #[non_exhaustive] |
| 1447 | pub struct EngineFailure { |
| 1448 | pub kind: EngineErrorKind, |
| 1449 | pub message: String, |
| 1450 | } |
| 1451 | |
| 1452 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 1453 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1454 | #[non_exhaustive] |
| 1455 | pub enum EngineErrorKind { |
| 1456 | EventReceiverClosed, |
| 1457 | Watcher, |
| 1458 | Other, |
| 1459 | } |