| 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 | target: EventRef, |
| 223 | content: MessageEditContent, |
| 224 | }, |
| 225 | /// redacting your own unsent message just cancels it |
| 226 | Redact { |
| 227 | timeline_id: TimelineId, |
| 228 | target: EventRef, |
| 229 | reason: Option<String>, |
| 230 | }, |
| 231 | ToggleReaction { |
| 232 | timeline_id: TimelineId, |
| 233 | target: EventRef, |
| 234 | key: String, |
| 235 | }, |
| 236 | /// works on a closed room too, it just can't refuse to move the marker backwards |
| 237 | MarkRead { |
| 238 | timeline_id: TimelineId, |
| 239 | event_id: OwnedEventId, |
| 240 | }, |
| 241 | } |
| 242 | |
| 243 | impl TimelineCommand { |
| 244 | #[must_use] |
| 245 | pub const fn room_id(&self) -> &OwnedRoomId { |
| 246 | match self { |
| 247 | Self::OpenThread { room_id, .. } | Self::CloseThread { room_id, .. } => room_id, |
| 248 | Self::Resync { timeline_id } |
| 249 | | Self::Paginate { timeline_id, .. } |
| 250 | | Self::Send { timeline_id, .. } |
| 251 | | Self::Edit { timeline_id, .. } |
| 252 | | Self::Redact { timeline_id, .. } |
| 253 | | Self::ToggleReaction { timeline_id, .. } |
| 254 | | Self::MarkRead { timeline_id, .. } => timeline_id.room_id(), |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | #[must_use] |
| 259 | pub const fn timeline_id(&self) -> Option<&TimelineId> { |
| 260 | match self { |
| 261 | Self::OpenThread { .. } | Self::CloseThread { .. } => None, |
| 262 | Self::Resync { timeline_id } |
| 263 | | Self::Paginate { timeline_id, .. } |
| 264 | | Self::Send { timeline_id, .. } |
| 265 | | Self::Edit { timeline_id, .. } |
| 266 | | Self::Redact { timeline_id, .. } |
| 267 | | Self::ToggleReaction { timeline_id, .. } |
| 268 | | Self::MarkRead { timeline_id, .. } => Some(timeline_id), |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | #[derive(Clone, Debug, Eq, Hash, PartialEq)] |
| 274 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 275 | #[non_exhaustive] |
| 276 | pub enum TimelineId { |
| 277 | Room(OwnedRoomId), |
| 278 | Thread { |
| 279 | room_id: OwnedRoomId, |
| 280 | root_event_id: OwnedEventId, |
| 281 | }, |
| 282 | } |
| 283 | |
| 284 | impl TimelineId { |
| 285 | #[must_use] |
| 286 | pub const fn room_id(&self) -> &OwnedRoomId { |
| 287 | match self { |
| 288 | Self::Room(room_id) | Self::Thread { room_id, .. } => room_id, |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | #[must_use] |
| 293 | pub fn thread(room_id: OwnedRoomId, root_event_id: OwnedEventId) -> Self { |
| 294 | Self::Thread { |
| 295 | room_id, |
| 296 | root_event_id, |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | #[must_use] |
| 301 | pub fn thread_root(&self) -> Option<&OwnedEventId> { |
| 302 | match self { |
| 303 | Self::Room(_) => None, |
| 304 | Self::Thread { root_event_id, .. } => Some(root_event_id), |
| 305 | } |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | impl From<OwnedRoomId> for TimelineId { |
| 310 | fn from(room_id: OwnedRoomId) -> Self { |
| 311 | Self::Room(room_id) |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 316 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 317 | #[non_exhaustive] |
| 318 | pub enum MediaCommand { |
| 319 | /// succeeds once the send queue has it. progress and failure arrive as events |
| 320 | Upload(AttachmentUpload), |
| 321 | } |
| 322 | |
| 323 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 324 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 325 | pub struct AttachmentUpload { |
| 326 | pub timeline_id: TimelineId, |
| 327 | pub filename: String, |
| 328 | pub mimetype: String, |
| 329 | pub bytes: Vec<u8>, |
| 330 | pub caption: Option<String>, |
| 331 | pub details: Option<UploadDetails>, |
| 332 | pub thumbnail: Option<UploadThumbnail>, |
| 333 | } |
| 334 | |
| 335 | impl AttachmentUpload { |
| 336 | #[must_use] |
| 337 | pub fn new( |
| 338 | timeline_id: impl Into<TimelineId>, |
| 339 | filename: impl Into<String>, |
| 340 | mimetype: impl Into<String>, |
| 341 | bytes: Vec<u8>, |
| 342 | ) -> Self { |
| 343 | Self { |
| 344 | timeline_id: timeline_id.into(), |
| 345 | filename: filename.into(), |
| 346 | mimetype: mimetype.into(), |
| 347 | bytes, |
| 348 | caption: None, |
| 349 | details: None, |
| 350 | thumbnail: None, |
| 351 | } |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 356 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 357 | #[non_exhaustive] |
| 358 | pub enum UploadDetails { |
| 359 | Image { |
| 360 | width: Option<u32>, |
| 361 | height: Option<u32>, |
| 362 | blurhash: Option<String>, |
| 363 | }, |
| 364 | Video { |
| 365 | width: Option<u32>, |
| 366 | height: Option<u32>, |
| 367 | duration_ms: Option<u64>, |
| 368 | blurhash: Option<String>, |
| 369 | }, |
| 370 | Audio { |
| 371 | duration_ms: Option<u64>, |
| 372 | }, |
| 373 | Voice { |
| 374 | duration_ms: u64, |
| 375 | waveform: Vec<u16>, |
| 376 | }, |
| 377 | File, |
| 378 | } |
| 379 | |
| 380 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 381 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 382 | #[non_exhaustive] |
| 383 | pub struct UploadThumbnail { |
| 384 | pub bytes: Vec<u8>, |
| 385 | pub mimetype: String, |
| 386 | pub width: u32, |
| 387 | pub height: u32, |
| 388 | } |
| 389 | |
| 390 | impl UploadThumbnail { |
| 391 | #[must_use] |
| 392 | pub fn new(bytes: Vec<u8>, mimetype: impl Into<String>, width: u32, height: u32) -> Self { |
| 393 | Self { |
| 394 | bytes, |
| 395 | mimetype: mimetype.into(), |
| 396 | width, |
| 397 | height, |
| 398 | } |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | #[derive(Eq, PartialEq)] |
| 403 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 404 | #[non_exhaustive] |
| 405 | pub enum EncryptionCommand { |
| 406 | Recover(String), |
| 407 | CreateKeyBackup, |
| 408 | EnableKeyBackup, |
| 409 | DisableKeyBackup { delete_from_server: bool }, |
| 410 | RecoverKeyBackup(String), |
| 411 | StartVerification(VerificationTarget), |
| 412 | Verify(VerificationAction), |
| 413 | } |
| 414 | |
| 415 | impl fmt::Debug for EncryptionCommand { |
| 416 | fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 417 | match self { |
| 418 | Self::Recover(_) => formatter.debug_tuple("Recover").field(&"...").finish(), |
| 419 | Self::CreateKeyBackup => formatter.write_str("CreateKeyBackup"), |
| 420 | Self::EnableKeyBackup => formatter.write_str("EnableKeyBackup"), |
| 421 | Self::DisableKeyBackup { delete_from_server } => formatter |
| 422 | .debug_struct("DisableKeyBackup") |
| 423 | .field("delete_from_server", delete_from_server) |
| 424 | .finish(), |
| 425 | Self::RecoverKeyBackup(_) => formatter |
| 426 | .debug_tuple("RecoverKeyBackup") |
| 427 | .field(&"...") |
| 428 | .finish(), |
| 429 | Self::StartVerification(target) => formatter |
| 430 | .debug_tuple("StartVerification") |
| 431 | .field(target) |
| 432 | .finish(), |
| 433 | Self::Verify(action) => formatter.debug_tuple("Verify").field(action).finish(), |
| 434 | } |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 439 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 440 | #[non_exhaustive] |
| 441 | pub enum NotificationCommand { |
| 442 | SetRoomMode { |
| 443 | room_id: OwnedRoomId, |
| 444 | mode: RoomNotificationMode, |
| 445 | }, |
| 446 | ResetRoomMode(OwnedRoomId), |
| 447 | SetDefaultMode { |
| 448 | encrypted: bool, |
| 449 | one_to_one: bool, |
| 450 | mode: RoomNotificationMode, |
| 451 | }, |
| 452 | AddKeyword(String), |
| 453 | RemoveKeyword(String), |
| 454 | } |
| 455 | |
| 456 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 457 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 458 | #[non_exhaustive] |
| 459 | pub enum LifecycleCommand { |
| 460 | Shutdown, |
| 461 | } |
| 462 | |
| 463 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 464 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 465 | #[non_exhaustive] |
| 466 | pub enum CommandOutput { |
| 467 | Done, |
| 468 | Room(OwnedRoomId), |
| 469 | } |
| 470 | |
| 471 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 472 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 473 | #[non_exhaustive] |
| 474 | pub enum MessageContent { |
| 475 | Text { |
| 476 | content: RichText, |
| 477 | mentions: MessageMentions, |
| 478 | }, |
| 479 | Emote { |
| 480 | content: RichText, |
| 481 | mentions: MessageMentions, |
| 482 | }, |
| 483 | Reply { |
| 484 | event_id: OwnedEventId, |
| 485 | content: RichText, |
| 486 | mentions: MessageMentions, |
| 487 | notify_replied_to: bool, |
| 488 | }, |
| 489 | } |
| 490 | |
| 491 | impl MessageContent { |
| 492 | #[must_use] |
| 493 | pub fn text(body: impl Into<String>) -> Self { |
| 494 | Self::Text { |
| 495 | content: RichText::plain(body), |
| 496 | mentions: MessageMentions::default(), |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | #[must_use] |
| 501 | pub fn html(body: impl Into<String>, formatted_body: impl Into<String>) -> Self { |
| 502 | Self::Text { |
| 503 | content: RichText::html(body, formatted_body), |
| 504 | mentions: MessageMentions::default(), |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | #[must_use] |
| 509 | pub fn emote(content: RichText, mentions: MessageMentions) -> Self { |
| 510 | Self::Emote { content, mentions } |
| 511 | } |
| 512 | |
| 513 | #[must_use] |
| 514 | pub fn reply(event_id: OwnedEventId, content: RichText, mentions: MessageMentions) -> Self { |
| 515 | Self::Reply { |
| 516 | event_id, |
| 517 | content, |
| 518 | mentions, |
| 519 | notify_replied_to: true, |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | #[must_use] |
| 524 | pub fn reply_without_notification( |
| 525 | event_id: OwnedEventId, |
| 526 | content: RichText, |
| 527 | mentions: MessageMentions, |
| 528 | ) -> Self { |
| 529 | Self::Reply { |
| 530 | event_id, |
| 531 | content, |
| 532 | mentions, |
| 533 | notify_replied_to: false, |
| 534 | } |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 539 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 540 | #[non_exhaustive] |
| 541 | pub enum MessageEditContent { |
| 542 | Text { |
| 543 | content: RichText, |
| 544 | mentions: MessageMentions, |
| 545 | }, |
| 546 | Emote { |
| 547 | content: RichText, |
| 548 | mentions: MessageMentions, |
| 549 | }, |
| 550 | } |
| 551 | |
| 552 | impl MessageEditContent { |
| 553 | #[must_use] |
| 554 | pub fn text(body: impl Into<String>) -> Self { |
| 555 | Self::Text { |
| 556 | content: RichText::plain(body), |
| 557 | mentions: MessageMentions::default(), |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | #[must_use] |
| 562 | pub fn html(body: impl Into<String>, formatted_body: impl Into<String>) -> Self { |
| 563 | Self::Text { |
| 564 | content: RichText::html(body, formatted_body), |
| 565 | mentions: MessageMentions::default(), |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | #[must_use] |
| 570 | pub fn emote(content: RichText, mentions: MessageMentions) -> Self { |
| 571 | Self::Emote { content, mentions } |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | #[derive(Clone, Debug, Default, Eq, PartialEq)] |
| 576 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 577 | #[non_exhaustive] |
| 578 | pub struct RichText { |
| 579 | pub body: String, |
| 580 | pub formatted_body: Option<String>, |
| 581 | } |
| 582 | |
| 583 | impl RichText { |
| 584 | #[must_use] |
| 585 | pub fn plain(body: impl Into<String>) -> Self { |
| 586 | Self { |
| 587 | body: body.into(), |
| 588 | formatted_body: None, |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | #[must_use] |
| 593 | pub fn html(body: impl Into<String>, formatted_body: impl Into<String>) -> Self { |
| 594 | Self { |
| 595 | body: body.into(), |
| 596 | formatted_body: Some(formatted_body.into()), |
| 597 | } |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | #[derive(Clone, Debug, Default, Eq, PartialEq)] |
| 602 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 603 | #[non_exhaustive] |
| 604 | pub struct MessageMentions { |
| 605 | pub user_ids: Vec<OwnedUserId>, |
| 606 | pub room: bool, |
| 607 | } |
| 608 | |
| 609 | impl MessageMentions { |
| 610 | #[must_use] |
| 611 | pub fn with_user_ids(user_ids: impl IntoIterator<Item = OwnedUserId>) -> Self { |
| 612 | Self { |
| 613 | user_ids: user_ids.into_iter().collect(), |
| 614 | room: false, |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | #[must_use] |
| 619 | pub fn room() -> Self { |
| 620 | Self { |
| 621 | user_ids: Vec::new(), |
| 622 | room: true, |
| 623 | } |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 628 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 629 | #[non_exhaustive] |
| 630 | pub enum Event { |
| 631 | ConnectionChanged(ConnectionState), |
| 632 | Authenticated { |
| 633 | user_id: OwnedUserId, |
| 634 | }, |
| 635 | /// Refresh tokens changed. Persist a fresh `ClientHandle::session_record()` |
| 636 | /// atomically; a record captured before this event may no longer restore. |
| 637 | SessionTokensChanged, |
| 638 | /// the server stopped honouring our token and the engine stops. on a soft |
| 639 | /// logout keep the store and log in again with the same device, otherwise |
| 640 | /// the record and the store are both dead |
| 641 | SessionAccessRejected { |
| 642 | soft_logout: bool, |
| 643 | }, |
| 644 | NotificationSettingsChanged, |
| 645 | VerificationTargetChanged(VerificationTarget), |
| 646 | RoomsChanged, |
| 647 | /// replaces whatever you had for this timeline. every diff after it applies to it |
| 648 | TimelineReset { |
| 649 | timeline_id: TimelineId, |
| 650 | items: Vec<TimelineItem>, |
| 651 | }, |
| 652 | TimelineChanged { |
| 653 | timeline_id: TimelineId, |
| 654 | diffs: Vec<TimelineDiff>, |
| 655 | }, |
| 656 | HistoryChanged { |
| 657 | timeline_id: TimelineId, |
| 658 | state: HistoryState, |
| 659 | }, |
| 660 | TypingChanged(TypingState), |
| 661 | MediaUploadProgress { |
| 662 | room_id: OwnedRoomId, |
| 663 | transaction_id: OwnedTransactionId, |
| 664 | uploaded_bytes: u64, |
| 665 | total_bytes: u64, |
| 666 | complete: bool, |
| 667 | }, |
| 668 | MediaUploadFailed { |
| 669 | room_id: OwnedRoomId, |
| 670 | transaction_id: OwnedTransactionId, |
| 671 | kind: crate::ErrorKind, |
| 672 | recoverable: bool, |
| 673 | }, |
| 674 | EncryptionChanged(EncryptionState), |
| 675 | VerificationChanged(VerificationState), |
| 676 | /// logout went through and the engine stops. delete the record and the store |
| 677 | LoggedOut, |
| 678 | Failed(EngineFailure), |
| 679 | /// always the last event. keep the saved record unless `LoggedOut` or a |
| 680 | /// hard `SessionAccessRejected` came first. commands still in flight get no result |
| 681 | Stopped, |
| 682 | } |
| 683 | |
| 684 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 685 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 686 | #[non_exhaustive] |
| 687 | pub enum ConnectionState { |
| 688 | Idle, |
| 689 | Connecting, |
| 690 | Online, |
| 691 | Offline, |
| 692 | Stopped, |
| 693 | } |
| 694 | |
| 695 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 696 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 697 | #[non_exhaustive] |
| 698 | pub struct RoomSummary { |
| 699 | pub room_id: OwnedRoomId, |
| 700 | pub name: String, |
| 701 | pub membership: RoomMembership, |
| 702 | pub unread_messages: u64, |
| 703 | pub unread_mentions: u64, |
| 704 | pub encrypted: bool, |
| 705 | pub space: bool, |
| 706 | pub direct: bool, |
| 707 | pub favourite: bool, |
| 708 | pub low_priority: bool, |
| 709 | pub avatar: Option<OwnedMxcUri>, |
| 710 | pub latest_event_timestamp_ms: Option<u64>, |
| 711 | pub latest_event: Option<LatestEvent>, |
| 712 | pub notification_mode: Option<RoomNotificationMode>, |
| 713 | pub notification_mode_custom: bool, |
| 714 | pub topic: Option<String>, |
| 715 | pub canonical_alias: Option<OwnedRoomAliasId>, |
| 716 | pub joined_members: u64, |
| 717 | pub invited_members: u64, |
| 718 | /// who a nameless room gets named after |
| 719 | pub heroes: Vec<RoomHero>, |
| 720 | /// set once the room has been upgraded, this is where everyone went |
| 721 | pub successor: Option<OwnedRoomId>, |
| 722 | pub inviter: Option<OwnedUserId>, |
| 723 | /// joined spaces that list this room as a child |
| 724 | pub parent_spaces: Vec<OwnedRoomId>, |
| 725 | /// for a space, the children it lists, joined or not |
| 726 | pub children: Vec<OwnedRoomId>, |
| 727 | } |
| 728 | |
| 729 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 730 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 731 | #[non_exhaustive] |
| 732 | pub struct RoomHero { |
| 733 | pub user_id: OwnedUserId, |
| 734 | pub display_name: Option<String>, |
| 735 | pub avatar: Option<OwnedMxcUri>, |
| 736 | } |
| 737 | |
| 738 | /// enough for a room list preview line. `body` is empty for anything that isn't a plain message |
| 739 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 740 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 741 | #[non_exhaustive] |
| 742 | pub struct LatestEvent { |
| 743 | pub sender: Option<OwnedUserId>, |
| 744 | pub body: Option<String>, |
| 745 | pub timestamp_ms: Option<u64>, |
| 746 | pub send_state: SendState, |
| 747 | } |
| 748 | |
| 749 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 750 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 751 | #[non_exhaustive] |
| 752 | pub struct Profile { |
| 753 | pub user_id: OwnedUserId, |
| 754 | pub display_name: Option<String>, |
| 755 | pub avatar: Option<OwnedMxcUri>, |
| 756 | } |
| 757 | |
| 758 | /// one room in a space's hierarchy, as the server describes it, which may be a room you're not in |
| 759 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 760 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 761 | #[non_exhaustive] |
| 762 | pub struct SpaceChild { |
| 763 | pub room_id: OwnedRoomId, |
| 764 | pub name: String, |
| 765 | pub topic: Option<String>, |
| 766 | pub avatar: Option<OwnedMxcUri>, |
| 767 | pub canonical_alias: Option<OwnedRoomAliasId>, |
| 768 | pub joined_members: u64, |
| 769 | pub space: bool, |
| 770 | pub children_count: u64, |
| 771 | pub membership: Option<RoomMembership>, |
| 772 | pub via: Vec<OwnedServerName>, |
| 773 | pub suggested: bool, |
| 774 | pub world_readable: Option<bool>, |
| 775 | pub guest_can_join: bool, |
| 776 | } |
| 777 | |
| 778 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 779 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 780 | #[non_exhaustive] |
| 781 | pub enum RoomNotificationMode { |
| 782 | AllMessages, |
| 783 | MentionsAndKeywordsOnly, |
| 784 | Mute, |
| 785 | } |
| 786 | |
| 787 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 788 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 789 | #[non_exhaustive] |
| 790 | pub struct RoomNotificationSettings { |
| 791 | pub mode: RoomNotificationMode, |
| 792 | pub custom: bool, |
| 793 | } |
| 794 | |
| 795 | #[derive(Clone, Debug)] |
| 796 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 797 | #[non_exhaustive] |
| 798 | pub enum PushNotificationStatus { |
| 799 | Event(Box<PushNotification>), |
| 800 | NotFound, |
| 801 | FilteredOut, |
| 802 | Redacted, |
| 803 | } |
| 804 | |
| 805 | #[derive(Clone, Debug)] |
| 806 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 807 | #[non_exhaustive] |
| 808 | pub struct PushNotification { |
| 809 | pub event_id: OwnedEventId, |
| 810 | pub event: PushNotificationEvent, |
| 811 | pub sender: PushNotificationSender, |
| 812 | pub room: PushNotificationRoom, |
| 813 | pub noisy: Option<bool>, |
| 814 | pub has_mention: Option<bool>, |
| 815 | pub thread_id: Option<OwnedEventId>, |
| 816 | } |
| 817 | |
| 818 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 819 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 820 | #[non_exhaustive] |
| 821 | pub struct PushNotificationSender { |
| 822 | pub user_id: OwnedUserId, |
| 823 | pub display_name: Option<String>, |
| 824 | pub avatar_url: Option<OwnedMxcUri>, |
| 825 | pub name_ambiguous: bool, |
| 826 | } |
| 827 | |
| 828 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 829 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 830 | #[non_exhaustive] |
| 831 | pub struct PushNotificationRoom { |
| 832 | pub room_id: OwnedRoomId, |
| 833 | pub name: String, |
| 834 | pub avatar_url: Option<OwnedMxcUri>, |
| 835 | pub canonical_alias: Option<OwnedRoomAliasId>, |
| 836 | pub topic: Option<String>, |
| 837 | pub encrypted: Option<bool>, |
| 838 | pub direct: bool, |
| 839 | pub joined_members_count: u64, |
| 840 | pub service_members: Vec<OwnedUserId>, |
| 841 | pub active_service_members_count: u64, |
| 842 | pub space: bool, |
| 843 | } |
| 844 | |
| 845 | #[derive(Clone, Debug)] |
| 846 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 847 | #[non_exhaustive] |
| 848 | pub enum PushNotificationEvent { |
| 849 | Timeline(Raw<AnySyncTimelineEvent>), |
| 850 | Invite(Raw<StrippedRoomMemberEvent>), |
| 851 | } |
| 852 | |
| 853 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 854 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 855 | #[non_exhaustive] |
| 856 | pub struct RoomPowerLevels { |
| 857 | pub ban: i64, |
| 858 | pub invite: i64, |
| 859 | pub kick: i64, |
| 860 | pub redact: i64, |
| 861 | pub events_default: i64, |
| 862 | pub state_default: i64, |
| 863 | pub users_default: i64, |
| 864 | pub room_notification: i64, |
| 865 | pub users: Vec<(OwnedUserId, i64)>, |
| 866 | pub events: Vec<(String, i64)>, |
| 867 | } |
| 868 | |
| 869 | #[derive(Clone, Debug, Default, Eq, PartialEq)] |
| 870 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 871 | #[non_exhaustive] |
| 872 | pub struct PowerLevelChanges { |
| 873 | pub ban: Option<i64>, |
| 874 | pub invite: Option<i64>, |
| 875 | pub kick: Option<i64>, |
| 876 | pub redact: Option<i64>, |
| 877 | pub events_default: Option<i64>, |
| 878 | pub state_default: Option<i64>, |
| 879 | pub users_default: Option<i64>, |
| 880 | pub room_notification: Option<i64>, |
| 881 | pub room_name: Option<i64>, |
| 882 | pub room_avatar: Option<i64>, |
| 883 | pub room_topic: Option<i64>, |
| 884 | pub space_child: Option<i64>, |
| 885 | pub beacon: Option<i64>, |
| 886 | pub beacon_info: Option<i64>, |
| 887 | } |
| 888 | |
| 889 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 890 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 891 | #[non_exhaustive] |
| 892 | pub enum RoomMembership { |
| 893 | Joined, |
| 894 | Invited, |
| 895 | Left, |
| 896 | Knocked, |
| 897 | Banned, |
| 898 | } |
| 899 | |
| 900 | #[derive(Clone, Debug, Default, Eq, PartialEq)] |
| 901 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 902 | #[non_exhaustive] |
| 903 | pub struct RoomListFilter { |
| 904 | pub membership: Option<RoomMembership>, |
| 905 | pub category: Option<RoomCategory>, |
| 906 | pub favourite: Option<bool>, |
| 907 | pub unread_only: bool, |
| 908 | pub search: Option<String>, |
| 909 | } |
| 910 | |
| 911 | impl RoomListFilter { |
| 912 | #[must_use] |
| 913 | pub fn with_membership(mut self, membership: RoomMembership) -> Self { |
| 914 | self.membership = Some(membership); |
| 915 | self |
| 916 | } |
| 917 | |
| 918 | #[must_use] |
| 919 | pub fn with_category(mut self, category: RoomCategory) -> Self { |
| 920 | self.category = Some(category); |
| 921 | self |
| 922 | } |
| 923 | |
| 924 | #[must_use] |
| 925 | pub fn with_favourite(mut self, favourite: bool) -> Self { |
| 926 | self.favourite = Some(favourite); |
| 927 | self |
| 928 | } |
| 929 | |
| 930 | #[must_use] |
| 931 | pub const fn with_unread_only(mut self, unread_only: bool) -> Self { |
| 932 | self.unread_only = unread_only; |
| 933 | self |
| 934 | } |
| 935 | |
| 936 | #[must_use] |
| 937 | pub fn with_search(mut self, search: impl Into<String>) -> Self { |
| 938 | self.search = Some(search.into()); |
| 939 | self |
| 940 | } |
| 941 | |
| 942 | #[must_use] |
| 943 | pub fn matches(&self, room: &RoomSummary) -> bool { |
| 944 | self.matcher().matches(room) |
| 945 | } |
| 946 | |
| 947 | /// lowercases the search once, for filtering a whole list |
| 948 | #[must_use] |
| 949 | pub fn matcher(&self) -> RoomMatcher<'_> { |
| 950 | RoomMatcher { |
| 951 | filter: self, |
| 952 | search: self.search.as_deref().map(str::to_lowercase), |
| 953 | } |
| 954 | } |
| 955 | } |
| 956 | |
| 957 | #[derive(Debug)] |
| 958 | pub struct RoomMatcher<'a> { |
| 959 | filter: &'a RoomListFilter, |
| 960 | search: Option<String>, |
| 961 | } |
| 962 | |
| 963 | impl RoomMatcher<'_> { |
| 964 | #[must_use] |
| 965 | pub fn matches(&self, room: &RoomSummary) -> bool { |
| 966 | let filter = self.filter; |
| 967 | filter |
| 968 | .membership |
| 969 | .is_none_or(|membership| room.membership == membership) |
| 970 | && filter.category.is_none_or(|category| match category { |
| 971 | RoomCategory::People => room.direct && !room.space, |
| 972 | RoomCategory::Rooms => !room.direct && !room.space, |
| 973 | RoomCategory::Spaces => room.space, |
| 974 | }) |
| 975 | && filter |
| 976 | .favourite |
| 977 | .is_none_or(|favourite| room.favourite == favourite) |
| 978 | && (!filter.unread_only || room.unread_messages > 0 || room.unread_mentions > 0) |
| 979 | && self.search.as_ref().is_none_or(|search| { |
| 980 | room.name.to_lowercase().contains(search) |
| 981 | || room.room_id.as_str().to_lowercase().contains(search) |
| 982 | }) |
| 983 | } |
| 984 | } |
| 985 | |
| 986 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 987 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 988 | pub enum RoomCategory { |
| 989 | People, |
| 990 | Rooms, |
| 991 | Spaces, |
| 992 | } |
| 993 | |
| 994 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 995 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 996 | pub enum MemberFilter { |
| 997 | Active, |
| 998 | Joined, |
| 999 | Invited, |
| 1000 | All, |
| 1001 | } |
| 1002 | |
| 1003 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 1004 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1005 | #[non_exhaustive] |
| 1006 | pub enum MemberMembership { |
| 1007 | Joined, |
| 1008 | Invited, |
| 1009 | Left, |
| 1010 | Knocked, |
| 1011 | Banned, |
| 1012 | Custom(String), |
| 1013 | } |
| 1014 | |
| 1015 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 1016 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1017 | #[non_exhaustive] |
| 1018 | pub struct MemberSummary { |
| 1019 | pub user_id: OwnedUserId, |
| 1020 | pub name: String, |
| 1021 | pub display_name: Option<String>, |
| 1022 | pub avatar: Option<OwnedMxcUri>, |
| 1023 | pub membership: MemberMembership, |
| 1024 | pub own: bool, |
| 1025 | pub name_ambiguous: bool, |
| 1026 | pub ignored: bool, |
| 1027 | pub service: bool, |
| 1028 | } |
| 1029 | |
| 1030 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 1031 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1032 | #[non_exhaustive] |
| 1033 | pub struct OwnDevice { |
| 1034 | pub device_id: OwnedDeviceId, |
| 1035 | pub display_name: Option<String>, |
| 1036 | pub last_seen_ip: Option<String>, |
| 1037 | pub last_seen_timestamp_ms: Option<u64>, |
| 1038 | pub current: bool, |
| 1039 | } |
| 1040 | |
| 1041 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 1042 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1043 | #[non_exhaustive] |
| 1044 | pub enum TimelineItem { |
| 1045 | Event(Box<TimelineEvent>), |
| 1046 | DayDivider { timestamp_ms: u64 }, |
| 1047 | ReadMarker, |
| 1048 | TimelineStart, |
| 1049 | } |
| 1050 | |
| 1051 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 1052 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1053 | #[non_exhaustive] |
| 1054 | pub enum TimelineDiff { |
| 1055 | Append { values: Vec<TimelineItem> }, |
| 1056 | Clear, |
| 1057 | PushFront { value: TimelineItem }, |
| 1058 | PushBack { value: TimelineItem }, |
| 1059 | PopFront, |
| 1060 | PopBack, |
| 1061 | Insert { index: usize, value: TimelineItem }, |
| 1062 | Set { index: usize, value: TimelineItem }, |
| 1063 | Remove { index: usize }, |
| 1064 | Truncate { length: usize }, |
| 1065 | Reset { values: Vec<TimelineItem> }, |
| 1066 | } |
| 1067 | |
| 1068 | #[derive(Clone, Copy, Debug, Eq, PartialEq, thiserror::Error)] |
| 1069 | #[error("timeline diff is incompatible with the current {length}-item snapshot")] |
| 1070 | pub struct TimelineDiffError { |
| 1071 | pub length: usize, |
| 1072 | } |
| 1073 | |
| 1074 | impl TimelineDiff { |
| 1075 | /// Apply a diff only when its indices match the current snapshot. |
| 1076 | /// On error the snapshot is unchanged; request `TimelineCommand::Resync` |
| 1077 | /// and ignore further diffs until the corresponding `TimelineReset`. |
| 1078 | pub fn try_apply(self, items: &mut Vec<TimelineItem>) -> Result<(), TimelineDiffError> { |
| 1079 | let length = items.len(); |
| 1080 | match self { |
| 1081 | Self::Append { values } => items.extend(values), |
| 1082 | Self::Clear => items.clear(), |
| 1083 | Self::PushFront { value } => items.insert(0, value), |
| 1084 | Self::PushBack { value } => items.push(value), |
| 1085 | Self::PopFront if length == 0 => return Err(TimelineDiffError { length }), |
| 1086 | Self::PopFront => { |
| 1087 | items.remove(0); |
| 1088 | } |
| 1089 | Self::PopBack if length == 0 => return Err(TimelineDiffError { length }), |
| 1090 | Self::PopBack => { |
| 1091 | items.pop(); |
| 1092 | } |
| 1093 | Self::Insert { index, .. } if index > length => { |
| 1094 | return Err(TimelineDiffError { length }); |
| 1095 | } |
| 1096 | Self::Insert { index, value } => items.insert(index, value), |
| 1097 | Self::Set { index, .. } if index >= length => return Err(TimelineDiffError { length }), |
| 1098 | Self::Set { index, value } => items[index] = value, |
| 1099 | Self::Remove { index } if index >= length => return Err(TimelineDiffError { length }), |
| 1100 | Self::Remove { index } => { |
| 1101 | items.remove(index); |
| 1102 | } |
| 1103 | Self::Truncate { length: target } if target > length => { |
| 1104 | return Err(TimelineDiffError { length }); |
| 1105 | } |
| 1106 | Self::Truncate { length } => items.truncate(length), |
| 1107 | Self::Reset { values } => *items = values, |
| 1108 | } |
| 1109 | Ok(()) |
| 1110 | } |
| 1111 | } |
| 1112 | |
| 1113 | #[cfg(test)] |
| 1114 | mod tests { |
| 1115 | use super::{TimelineDiff, TimelineItem}; |
| 1116 | |
| 1117 | fn item(timestamp_ms: u64) -> TimelineItem { |
| 1118 | TimelineItem::DayDivider { timestamp_ms } |
| 1119 | } |
| 1120 | |
| 1121 | #[test] |
| 1122 | fn timeline_diff_try_apply_handles_every_variant() { |
| 1123 | let mut items = vec![item(1), item(2)]; |
| 1124 | |
| 1125 | TimelineDiff::Append { |
| 1126 | values: vec![item(3)], |
| 1127 | } |
| 1128 | .try_apply(&mut items) |
| 1129 | .unwrap(); |
| 1130 | assert_eq!(items, vec![item(1), item(2), item(3)]); |
| 1131 | |
| 1132 | TimelineDiff::Clear.try_apply(&mut items).unwrap(); |
| 1133 | assert!(items.is_empty()); |
| 1134 | |
| 1135 | TimelineDiff::PushFront { value: item(2) } |
| 1136 | .try_apply(&mut items) |
| 1137 | .unwrap(); |
| 1138 | TimelineDiff::PushBack { value: item(3) } |
| 1139 | .try_apply(&mut items) |
| 1140 | .unwrap(); |
| 1141 | assert_eq!(items, vec![item(2), item(3)]); |
| 1142 | |
| 1143 | TimelineDiff::PopFront.try_apply(&mut items).unwrap(); |
| 1144 | TimelineDiff::PopBack.try_apply(&mut items).unwrap(); |
| 1145 | assert!(items.is_empty()); |
| 1146 | |
| 1147 | TimelineDiff::Reset { |
| 1148 | values: vec![item(1), item(3)], |
| 1149 | } |
| 1150 | .try_apply(&mut items) |
| 1151 | .unwrap(); |
| 1152 | TimelineDiff::Insert { |
| 1153 | index: 1, |
| 1154 | value: item(2), |
| 1155 | } |
| 1156 | .try_apply(&mut items) |
| 1157 | .unwrap(); |
| 1158 | assert_eq!(items, vec![item(1), item(2), item(3)]); |
| 1159 | |
| 1160 | TimelineDiff::Set { |
| 1161 | index: 1, |
| 1162 | value: item(4), |
| 1163 | } |
| 1164 | .try_apply(&mut items) |
| 1165 | .unwrap(); |
| 1166 | assert_eq!(items, vec![item(1), item(4), item(3)]); |
| 1167 | |
| 1168 | TimelineDiff::Remove { index: 1 } |
| 1169 | .try_apply(&mut items) |
| 1170 | .unwrap(); |
| 1171 | assert_eq!(items, vec![item(1), item(3)]); |
| 1172 | |
| 1173 | TimelineDiff::Truncate { length: 1 } |
| 1174 | .try_apply(&mut items) |
| 1175 | .unwrap(); |
| 1176 | assert_eq!(items, vec![item(1)]); |
| 1177 | } |
| 1178 | |
| 1179 | #[test] |
| 1180 | fn invalid_timeline_diff_leaves_snapshot_unchanged() { |
| 1181 | let invalid = [ |
| 1182 | TimelineDiff::Insert { |
| 1183 | index: 3, |
| 1184 | value: item(3), |
| 1185 | }, |
| 1186 | TimelineDiff::Set { |
| 1187 | index: 2, |
| 1188 | value: item(3), |
| 1189 | }, |
| 1190 | TimelineDiff::Remove { index: 2 }, |
| 1191 | TimelineDiff::Truncate { length: 3 }, |
| 1192 | ]; |
| 1193 | for diff in invalid { |
| 1194 | let mut items = vec![item(1), item(2)]; |
| 1195 | assert_eq!(diff.try_apply(&mut items).unwrap_err().length, 2); |
| 1196 | assert_eq!(items, vec![item(1), item(2)]); |
| 1197 | } |
| 1198 | for diff in [TimelineDiff::PopFront, TimelineDiff::PopBack] { |
| 1199 | let mut items = Vec::new(); |
| 1200 | assert_eq!(diff.try_apply(&mut items).unwrap_err().length, 0); |
| 1201 | assert!(items.is_empty()); |
| 1202 | } |
| 1203 | } |
| 1204 | } |
| 1205 | |
| 1206 | #[derive(Clone, Debug, Eq, Hash, PartialEq)] |
| 1207 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1208 | pub struct TimelineItemId(String); |
| 1209 | |
| 1210 | impl TimelineItemId { |
| 1211 | pub(crate) fn new(value: String) -> Self { |
| 1212 | Self(value) |
| 1213 | } |
| 1214 | |
| 1215 | #[must_use] |
| 1216 | pub fn as_str(&self) -> &str { |
| 1217 | &self.0 |
| 1218 | } |
| 1219 | } |
| 1220 | |
| 1221 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 1222 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1223 | #[non_exhaustive] |
| 1224 | pub struct TimelineEvent { |
| 1225 | pub item_id: TimelineItemId, |
| 1226 | pub event_id: Option<OwnedEventId>, |
| 1227 | pub transaction_id: Option<OwnedTransactionId>, |
| 1228 | pub sender: Sender, |
| 1229 | pub timestamp_ms: u64, |
| 1230 | pub own: bool, |
| 1231 | pub send_state: SendState, |
| 1232 | pub content: TimelineContent, |
| 1233 | pub reply_to: Option<ReplyReference>, |
| 1234 | pub thread_root: Option<OwnedEventId>, |
| 1235 | pub thread_summary: Option<ThreadSummary>, |
| 1236 | pub reactions: Vec<Reaction>, |
| 1237 | pub read_by: Vec<OwnedUserId>, |
| 1238 | pub editable: bool, |
| 1239 | } |
| 1240 | |
| 1241 | impl TimelineEvent { |
| 1242 | /// what to hand edit, redact and react. works before the server has echoed it back |
| 1243 | #[must_use] |
| 1244 | pub fn target(&self) -> Option<EventRef> { |
| 1245 | self.event_id |
| 1246 | .clone() |
| 1247 | .map(EventRef::Remote) |
| 1248 | .or_else(|| self.transaction_id.clone().map(EventRef::Local)) |
| 1249 | } |
| 1250 | } |
| 1251 | |
| 1252 | #[derive(Clone, Debug, Eq, Hash, PartialEq)] |
| 1253 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1254 | #[non_exhaustive] |
| 1255 | pub enum EventRef { |
| 1256 | Remote(OwnedEventId), |
| 1257 | /// a local echo, by the transaction id it was queued under |
| 1258 | Local(OwnedTransactionId), |
| 1259 | } |
| 1260 | |
| 1261 | impl From<OwnedEventId> for EventRef { |
| 1262 | fn from(event_id: OwnedEventId) -> Self { |
| 1263 | Self::Remote(event_id) |
| 1264 | } |
| 1265 | } |
| 1266 | |
| 1267 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 1268 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1269 | #[non_exhaustive] |
| 1270 | pub struct Sender { |
| 1271 | pub user_id: OwnedUserId, |
| 1272 | pub display_name: Option<String>, |
| 1273 | pub avatar: Option<OwnedMxcUri>, |
| 1274 | /// someone else in the room goes by the same name, so show the user id too |
| 1275 | pub name_ambiguous: bool, |
| 1276 | } |
| 1277 | |
| 1278 | impl Sender { |
| 1279 | #[must_use] |
| 1280 | pub fn name(&self) -> &str { |
| 1281 | self.display_name |
| 1282 | .as_deref() |
| 1283 | .unwrap_or_else(|| self.user_id.as_str()) |
| 1284 | } |
| 1285 | } |
| 1286 | |
| 1287 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 1288 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1289 | #[non_exhaustive] |
| 1290 | pub enum TimelineContent { |
| 1291 | Message(Message), |
| 1292 | Sticker { |
| 1293 | body: String, |
| 1294 | media: MediaDescriptor, |
| 1295 | }, |
| 1296 | Poll { |
| 1297 | question: String, |
| 1298 | }, |
| 1299 | Redacted, |
| 1300 | UnableToDecrypt(UtdCause), |
| 1301 | Membership { |
| 1302 | user_id: OwnedUserId, |
| 1303 | display_name: Option<String>, |
| 1304 | change: MembershipChange, |
| 1305 | }, |
| 1306 | Profile { |
| 1307 | user_id: OwnedUserId, |
| 1308 | display_name: Option<Change<Option<String>>>, |
| 1309 | avatar: Option<Change<Option<OwnedMxcUri>>>, |
| 1310 | }, |
| 1311 | /// `None` means it was cleared |
| 1312 | RoomName(Option<String>), |
| 1313 | RoomTopic(Option<String>), |
| 1314 | RoomAvatar(Option<OwnedMxcUri>), |
| 1315 | EncryptionEnabled, |
| 1316 | RoomCreated, |
| 1317 | RoomUpgraded { |
| 1318 | successor: OwnedRoomId, |
| 1319 | }, |
| 1320 | OtherState { |
| 1321 | event_type: String, |
| 1322 | state_key: String, |
| 1323 | }, |
| 1324 | Other { |
| 1325 | event_type: String, |
| 1326 | }, |
| 1327 | } |
| 1328 | |
| 1329 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 1330 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1331 | #[non_exhaustive] |
| 1332 | pub struct Message { |
| 1333 | pub kind: MessageKind, |
| 1334 | pub body: String, |
| 1335 | pub formatted_body: Option<String>, |
| 1336 | pub mentions: Option<MessageMentions>, |
| 1337 | pub edited: bool, |
| 1338 | pub media: Option<MediaDescriptor>, |
| 1339 | } |
| 1340 | |
| 1341 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 1342 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1343 | #[non_exhaustive] |
| 1344 | pub enum MessageKind { |
| 1345 | Text, |
| 1346 | Emote, |
| 1347 | Notice, |
| 1348 | ServerNotice, |
| 1349 | Image, |
| 1350 | Video, |
| 1351 | Audio, |
| 1352 | File, |
| 1353 | Location, |
| 1354 | VerificationRequest, |
| 1355 | Other(String), |
| 1356 | } |
| 1357 | |
| 1358 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 1359 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1360 | #[non_exhaustive] |
| 1361 | pub enum MembershipChange { |
| 1362 | Joined, |
| 1363 | Left, |
| 1364 | Banned, |
| 1365 | Unbanned, |
| 1366 | Kicked, |
| 1367 | KickedAndBanned, |
| 1368 | Invited, |
| 1369 | InvitationAccepted, |
| 1370 | InvitationRejected, |
| 1371 | InvitationRevoked, |
| 1372 | Knocked, |
| 1373 | KnockAccepted, |
| 1374 | KnockRetracted, |
| 1375 | KnockDenied, |
| 1376 | /// nothing a person would call a change, or something the sdk couldn't make sense of |
| 1377 | Other, |
| 1378 | } |
| 1379 | |
| 1380 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 1381 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1382 | pub struct Change<T> { |
| 1383 | pub old: T, |
| 1384 | pub new: T, |
| 1385 | } |
| 1386 | |
| 1387 | /// why a message couldn't be read, worth telling the user since some of these they can fix |
| 1388 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 1389 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1390 | #[non_exhaustive] |
| 1391 | pub enum UtdCause { |
| 1392 | Unknown, |
| 1393 | SentBeforeWeJoined, |
| 1394 | VerificationViolation, |
| 1395 | UnsignedDevice, |
| 1396 | UnknownDevice, |
| 1397 | HistoricalMessageAndBackupIsDisabled, |
| 1398 | WithheldForUnverifiedOrInsecureDevice, |
| 1399 | WithheldBySender, |
| 1400 | HistoricalMessageAndDeviceIsUnverified, |
| 1401 | } |
| 1402 | |
| 1403 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 1404 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1405 | #[non_exhaustive] |
| 1406 | pub struct ThreadSummary { |
| 1407 | pub reply_count: u32, |
| 1408 | pub latest_event_id: Option<OwnedEventId>, |
| 1409 | pub latest_event_timestamp_ms: Option<u64>, |
| 1410 | } |
| 1411 | |
| 1412 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 1413 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1414 | #[non_exhaustive] |
| 1415 | pub enum SendState { |
| 1416 | Sent, |
| 1417 | Queued, |
| 1418 | Sending, |
| 1419 | Failed { retriable: bool }, |
| 1420 | } |
| 1421 | |
| 1422 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 1423 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1424 | #[non_exhaustive] |
| 1425 | pub struct ReplyReference { |
| 1426 | pub event_id: OwnedEventId, |
| 1427 | pub details: ReplyDetails, |
| 1428 | } |
| 1429 | |
| 1430 | /// the replied-to event. macltok asks for it when it isn't loaded, `Pending` then turns into one of the others |
| 1431 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 1432 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1433 | #[non_exhaustive] |
| 1434 | pub enum ReplyDetails { |
| 1435 | Pending, |
| 1436 | Ready { |
| 1437 | sender: Sender, |
| 1438 | content: Box<TimelineContent>, |
| 1439 | timestamp_ms: u64, |
| 1440 | }, |
| 1441 | Unavailable, |
| 1442 | } |
| 1443 | |
| 1444 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 1445 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1446 | #[non_exhaustive] |
| 1447 | pub struct Reaction { |
| 1448 | pub key: String, |
| 1449 | pub count: usize, |
| 1450 | pub own: bool, |
| 1451 | pub senders: Vec<OwnedUserId>, |
| 1452 | } |
| 1453 | |
| 1454 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 1455 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1456 | #[non_exhaustive] |
| 1457 | pub struct MediaDescriptor { |
| 1458 | pub id: MediaId, |
| 1459 | pub kind: MediaKind, |
| 1460 | pub filename: String, |
| 1461 | pub caption: Option<String>, |
| 1462 | pub mimetype: Option<String>, |
| 1463 | pub size_bytes: Option<u64>, |
| 1464 | pub width: Option<u64>, |
| 1465 | pub height: Option<u64>, |
| 1466 | } |
| 1467 | |
| 1468 | #[derive(Clone, Debug, Eq, Hash, PartialEq)] |
| 1469 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1470 | pub struct MediaId(String); |
| 1471 | |
| 1472 | impl MediaId { |
| 1473 | pub(crate) fn new(value: String) -> Self { |
| 1474 | Self(value) |
| 1475 | } |
| 1476 | |
| 1477 | #[must_use] |
| 1478 | pub fn as_str(&self) -> &str { |
| 1479 | &self.0 |
| 1480 | } |
| 1481 | } |
| 1482 | |
| 1483 | impl std::fmt::Display for MediaId { |
| 1484 | fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 1485 | self.0.fmt(formatter) |
| 1486 | } |
| 1487 | } |
| 1488 | |
| 1489 | #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] |
| 1490 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1491 | pub enum MediaSize { |
| 1492 | Full, |
| 1493 | /// a hint. an uploader's own thumbnail wins over the server resizing anything |
| 1494 | Thumbnail { |
| 1495 | width: u32, |
| 1496 | height: u32, |
| 1497 | }, |
| 1498 | } |
| 1499 | |
| 1500 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 1501 | #[non_exhaustive] |
| 1502 | pub struct Media { |
| 1503 | pub bytes: Arc<[u8]>, |
| 1504 | pub mimetype: String, |
| 1505 | } |
| 1506 | |
| 1507 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 1508 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1509 | #[non_exhaustive] |
| 1510 | pub enum MediaKind { |
| 1511 | Image, |
| 1512 | Video, |
| 1513 | Audio, |
| 1514 | File, |
| 1515 | Sticker, |
| 1516 | } |
| 1517 | |
| 1518 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 1519 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1520 | #[non_exhaustive] |
| 1521 | pub struct TypingState { |
| 1522 | pub room_id: OwnedRoomId, |
| 1523 | pub users: Vec<OwnedUserId>, |
| 1524 | } |
| 1525 | |
| 1526 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 1527 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1528 | #[non_exhaustive] |
| 1529 | pub enum HistoryState { |
| 1530 | Idle, |
| 1531 | Loading, |
| 1532 | StartReached, |
| 1533 | Failed(String), |
| 1534 | } |
| 1535 | |
| 1536 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 1537 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1538 | #[non_exhaustive] |
| 1539 | pub struct EncryptionState { |
| 1540 | pub device_id: OwnedDeviceId, |
| 1541 | pub device_verified: bool, |
| 1542 | pub recovery: RecoveryState, |
| 1543 | pub key_backup: KeyBackupState, |
| 1544 | } |
| 1545 | |
| 1546 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 1547 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1548 | #[non_exhaustive] |
| 1549 | pub enum KeyBackupState { |
| 1550 | Unknown, |
| 1551 | Creating, |
| 1552 | Enabling, |
| 1553 | Resuming, |
| 1554 | Downloading, |
| 1555 | Enabled, |
| 1556 | Disabling, |
| 1557 | } |
| 1558 | |
| 1559 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 1560 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1561 | #[non_exhaustive] |
| 1562 | pub enum RecoveryState { |
| 1563 | Unknown, |
| 1564 | Disabled, |
| 1565 | Incomplete, |
| 1566 | Enabled, |
| 1567 | } |
| 1568 | |
| 1569 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 1570 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1571 | pub enum VerificationAction { |
| 1572 | Confirm, |
| 1573 | Reject, |
| 1574 | Cancel, |
| 1575 | } |
| 1576 | |
| 1577 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 1578 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1579 | #[non_exhaustive] |
| 1580 | pub enum VerificationTarget { |
| 1581 | OwnIdentity, |
| 1582 | User(OwnedUserId), |
| 1583 | Device { |
| 1584 | user_id: OwnedUserId, |
| 1585 | device_id: OwnedDeviceId, |
| 1586 | }, |
| 1587 | } |
| 1588 | |
| 1589 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 1590 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1591 | #[non_exhaustive] |
| 1592 | pub enum VerificationState { |
| 1593 | Waiting, |
| 1594 | Comparison { |
| 1595 | emojis: Vec<(String, String)>, |
| 1596 | decimals: (u16, u16, u16), |
| 1597 | }, |
| 1598 | Confirmed, |
| 1599 | Done, |
| 1600 | Cancelled(String), |
| 1601 | Failed(VerificationFailure), |
| 1602 | } |
| 1603 | |
| 1604 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 1605 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1606 | #[non_exhaustive] |
| 1607 | pub enum VerificationFailure { |
| 1608 | RequestAcceptance(String), |
| 1609 | SasStartup(String), |
| 1610 | SasAcceptance(String), |
| 1611 | RequestStreamEnded, |
| 1612 | SasStreamEnded, |
| 1613 | } |
| 1614 | |
| 1615 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 1616 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1617 | #[non_exhaustive] |
| 1618 | pub struct EngineFailure { |
| 1619 | pub kind: EngineErrorKind, |
| 1620 | pub message: String, |
| 1621 | } |
| 1622 | |
| 1623 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 1624 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 1625 | #[non_exhaustive] |
| 1626 | pub enum EngineErrorKind { |
| 1627 | EventReceiverClosed, |
| 1628 | Watcher, |
| 1629 | Other, |
| 1630 | } |