Skip to main content

matrix_sdk/send_queue/
mod.rs

1// Copyright 2024 The Matrix.org Foundation C.I.C.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! A send queue facility to serializing queuing and sending of messages.
16//!
17//! # [`Room`] send queue
18//!
19//! Each room gets its own [`RoomSendQueue`], that's available by calling
20//! [`Room::send_queue()`]. The first time this method is called, it will spawn
21//! a background task that's used to actually send events, in the order they
22//! were passed from calls to [`RoomSendQueue::send()`].
23//!
24//! This queue tries to simplify error management around sending events, using
25//! [`RoomSendQueue::send`] or [`RoomSendQueue::send_raw`]: by default, it will retry to send the
26//! same event a few times, before automatically disabling itself, and emitting
27//! a notification that can be listened to with the global send queue (see
28//! paragraph below) or using [`RoomSendQueue::subscribe()`].
29//!
30//! Requests are sent in the order they were queued. A request that failed with
31//! an unrecoverable error is marked as "wedged", and blocks all the requests
32//! queued after it (in the same room) from being sent, so events are never sent
33//! out of order; the queue resumes when the wedged request is retried (with
34//! [`SendHandle::unwedge`]) or removed (with [`SendHandle::abort`]).
35//!
36//! It is possible to control whether a single room is enabled using
37//! [`RoomSendQueue::set_enabled()`].
38//!
39//! # Global [`SendQueue`] object
40//!
41//! The [`Client::send_queue()`] method returns an API object allowing to
42//! control all the room send queues:
43//!
44//! - enable/disable them all at once with [`SendQueue::set_enabled()`].
45//! - get notifications about send errors with [`SendQueue::subscribe_errors`].
46//! - reload all unsent events that had been persisted in storage using
47//!   [`SendQueue::respawn_tasks_for_rooms_with_unsent_requests()`]. It is
48//!   recommended to call this method during initialization of a client,
49//!   otherwise persisted unsent events will only be re-sent after the send
50//!   queue for the given room has been reopened for the first time.
51//!
52//! # Send handle
53//!
54//! Just after queuing a request to send something, a [`SendHandle`] is
55//! returned, allowing manipulating the inflight request.
56//!
57//! For a send handle for an event, it's possible to edit the event / abort
58//! sending it. If it was still in the queue (i.e. not sent yet, or not being
59//! sent), then such an action would happen locally (i.e. in the database).
60//! Otherwise, it is "too late": the background task may be sending
61//! the event already, or has sent it; in that case, the edit/aborting must
62//! happen as an actual event materializing this, on the server. To accomplish
63//! this, the send queue may send such an event, using the dependency system
64//! described below.
65//!
66//! # Dependency system
67//!
68//! The send queue includes a simple dependency system, where a
69//! [`QueuedRequest`] can have zero or more dependents in the form of
70//! [`DependentQueuedRequest`]. A dependent queued request can have at most one
71//! depended-upon (parent) queued request.
72//!
73//! This allows implementing deferred edits/redacts, as hinted to in the
74//! previous section.
75//!
76//! ## Media upload
77//!
78//! This dependency system also allows uploading medias, since the media's
79//! *content* must be uploaded before we send the media *event* that describes
80//! it.
81//!
82//! In the simplest case, that is, a media file and its event must be sent (i.e.
83//! no thumbnails):
84//!
85//! - The file's content is immediately cached in the
86//!   [`matrix_sdk_base::event_cache::store::EventCacheStore`], using an MXC ID
87//!   that is temporary and designates a local URI without any possible doubt.
88//! - An initial media event is created and uses this temporary MXC ID, and
89//!   propagated as a local echo for an event.
90//! - A [`QueuedRequest`] is pushed to upload the file's media
91//!   ([`QueuedRequestKind::MediaUpload`]).
92//! - A [`DependentQueuedRequest`] is pushed to finish the upload
93//!   ([`DependentQueuedRequestKind::FinishUpload`]).
94//!
95//! What is expected to happen, if all goes well, is the following:
96//!
97//! - the media is uploaded to the media homeserver, which returns the final MXC
98//!   ID.
99//! - when marking the upload request as sent, the MXC ID is injected (as a
100//!   [`matrix_sdk_base::store::SentRequestKey`]) into the dependent request
101//!   [`DependentQueuedRequestKind::FinishUpload`] created in the last step
102//!   above.
103//! - next time the send queue handles dependent queries, it'll see this one is
104//!   ready to be sent, and it will transform it into an event queued request
105//!   ([`QueuedRequestKind::Event`]), with the event created in the local echo
106//!   before, updated with the MXC ID returned from the server.
107//! - this updated local echo is also propagated as an edit of the local echo to
108//!   observers, who get the final version with the final MXC IDs at this point
109//!   too.
110//! - then the event is sent normally, as any event sent with the send queue.
111//!
112//! When there is a thumbnail, things behave similarly, with some tweaks:
113//!
114//! - the thumbnail's content is also stored into the cache store immediately,
115//! - the thumbnail is sent first as an [`QueuedRequestKind::MediaUpload`]
116//!   request,
117//! - the file upload is pushed as a dependent request of kind
118//!   [`DependentQueuedRequestKind::UploadFileOrThumbnail`] (this variant keeps
119//!   the file's key used to look it up in the cache store).
120//! - the media event is then sent as a dependent request as described in the
121//!   previous section.
122//!
123//! What's expected to happen is thus the following:
124//!
125//! - After the thumbnail has been uploaded, the dependent query will retrieve
126//!   the final MXC ID returned by the homeserver for the thumbnail, and store
127//!   it into the [`QueuedRequestKind::MediaUpload`]'s `thumbnail_source` field,
128//!   allowing to remember the thumbnail MXC ID when it's time to finish the
129//!   upload later.
130//! - The dependent request is morphed into another
131//!   [`QueuedRequestKind::MediaUpload`], for the file itself.
132//!
133//! The rest of the process is then similar to that of uploading a file without
134//! a thumbnail. The only difference is that there's a thumbnail source (MXC ID)
135//! remembered and fixed up into the media event, just before sending it.
136
137use std::{
138    collections::{BTreeMap, HashMap},
139    future::IntoFuture,
140    str::FromStr as _,
141    sync::{
142        Arc, OnceLock, RwLock,
143        atomic::{AtomicBool, Ordering},
144    },
145    time::Duration,
146};
147
148use eyeball::SharedObservable;
149#[cfg(feature = "e2e-encryption")]
150use matrix_sdk_base::crypto::{OlmError, SessionRecipientCollectionError};
151#[cfg(feature = "unstable-msc4274")]
152use matrix_sdk_base::store::FinishGalleryItemInfo;
153use matrix_sdk_base::{
154    RoomState, StoreError,
155    cross_process_lock::CrossProcessLockError,
156    deserialized_responses::{EncryptionInfo, TimelineEvent},
157    event_cache::store::EventCacheStoreError,
158    media::{MediaRequestParameters, store::MediaStoreError},
159    store::{
160        ChildTransactionId, DependentQueuedRequest, DependentQueuedRequestKind, DynStateStore,
161        FinishUploadThumbnailInfo, QueueWedgeError, QueuedRequest, QueuedRequestKind,
162        SentMediaInfo, SentRequestKey, SerializableEventContent,
163    },
164    task_monitor::BackgroundTaskHandle,
165};
166use matrix_sdk_common::{boxed_into_future, locks::Mutex as SyncMutex};
167use mime::Mime;
168#[cfg(feature = "unstable-msc4354")]
169use ruma::events::sticky::StickyDurationMs;
170use ruma::{
171    EventId, MilliSecondsSinceUnixEpoch, OwnedEventId, OwnedRoomId, OwnedTransactionId, RoomId,
172    TransactionId, UInt, UserId,
173    api::error::ErrorKind,
174    events::{
175        AnyMessageLikeEventContent, Mentions, MessageLikeEventContent as _, TimelineEventType,
176        reaction::ReactionEventContent,
177        relation::Annotation,
178        room::{
179            MediaSource,
180            message::{FormattedBody, RoomMessageEventContent},
181        },
182    },
183    serde::Raw,
184};
185use tokio::sync::{Mutex, Notify, OwnedMutexGuard, broadcast, oneshot};
186use tracing::{debug, error, info, instrument, trace, warn};
187
188#[cfg(feature = "unstable-msc4354")]
189use crate::utils::sticky_duration_ms;
190use crate::{
191    Client, Media, Room, SessionChange, TransmissionProgress,
192    client::WeakClient,
193    config::RequestConfig,
194    error::RetryKind,
195    room::{WeakRoom, edit::EditedContent},
196};
197
198mod progress;
199mod upload;
200
201pub use progress::AbstractProgress;
202
203/// How long to wait before retrying, when loading the next request to send
204/// failed against the state store.
205const STORE_ERROR_BACKOFF: Duration = Duration::from_millis(250);
206
207/// A client-wide send queue, for all the rooms known by a client.
208pub struct SendQueue {
209    client: Client,
210}
211
212#[cfg(not(tarpaulin_include))]
213impl std::fmt::Debug for SendQueue {
214    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
215        f.debug_struct("SendQueue").finish_non_exhaustive()
216    }
217}
218
219impl SendQueue {
220    pub(super) fn new(client: Client) -> Self {
221        Self { client }
222    }
223
224    /// Reload all the rooms which had unsent requests, and respawn tasks for
225    /// those rooms.
226    pub async fn respawn_tasks_for_rooms_with_unsent_requests(&self) {
227        if !self.is_enabled() {
228            return;
229        }
230
231        let room_ids =
232            self.client.state_store().load_rooms_with_unsent_requests().await.unwrap_or_else(
233                |err| {
234                    warn!("error when loading rooms with unsent requests: {err}");
235                    Vec::new()
236                },
237            );
238
239        // Getting the [`RoomSendQueue`] is sufficient to spawn the task if
240        // needs be.
241        for room_id in room_ids {
242            if let Some(room) = self.client.get_room(&room_id) {
243                let _ = self.for_room(room);
244            }
245        }
246    }
247
248    /// Watches for the session getting a working access token again and
249    /// unwedges the requests that were wedged because it didn't have one.
250    async fn session_change_task(client: WeakClient) {
251        let Some(strong_client) = client.get() else { return };
252        let mut session_changes = strong_client.subscribe_to_session_changes();
253        // Don't keep the client alive just by listening.
254        drop(strong_client);
255
256        loop {
257            match session_changes.recv().await {
258                Ok(SessionChange::TokensRefreshed) => {
259                    let Some(client) = client.get() else { break };
260                    client.send_queue().unwedge_requests_with_expired_token().await;
261                }
262
263                Ok(SessionChange::UnknownToken(_)) => {}
264
265                // A missed `TokensRefreshed` would leave requests wedged for good, while
266                // a spurious unwedge only costs a retry that gets wedged again.
267                Err(broadcast::error::RecvError::Lagged(num)) => {
268                    warn!(num, "missed some session changes, unwedging just in case");
269                    let Some(client) = client.get() else { break };
270                    client.send_queue().unwedge_requests_with_expired_token().await;
271                }
272
273                Err(broadcast::error::RecvError::Closed) => break,
274            }
275        }
276    }
277
278    /// Unwedge every request that was wedged because the access token had been
279    /// rejected, in every room that has unsent requests.
280    async fn unwedge_requests_with_expired_token(&self) {
281        let room_ids = match self.client.state_store().load_rooms_with_unsent_requests().await {
282            Ok(room_ids) => room_ids,
283            Err(err) => {
284                warn!("error when loading rooms with unsent requests: {err}");
285                return;
286            }
287        };
288
289        for room_id in room_ids {
290            let Some(room) = self.client.get_room(&room_id) else { continue };
291            self.for_room(room).unwedge_requests_with_expired_token().await;
292        }
293    }
294
295    /// Tiny helper to get the send queue's global context from the [`Client`].
296    #[inline(always)]
297    fn data(&self) -> &SendQueueData {
298        &self.client.inner.send_queue_data
299    }
300
301    /// Get or create a new send queue for a given room, and insert it into our
302    /// memoized rooms mapping.
303    pub(crate) fn for_room(&self, room: Room) -> RoomSendQueue {
304        let data = self.data();
305
306        data.session_change_task.get_or_init(|| {
307            let client = self.client.downgrade();
308            self.client
309                .task_monitor()
310                .spawn_infinite_task("send_queue_session_change", Self::session_change_task(client))
311                .abort_on_drop()
312        });
313
314        let mut map = data.rooms.write().unwrap();
315
316        let room_id = room.room_id();
317        if let Some(room_q) = map.get(room_id).cloned() {
318            return room_q;
319        }
320
321        let owned_room_id = room_id.to_owned();
322        let room_q = RoomSendQueue::new(
323            self.is_enabled(),
324            data.global_update_sender.clone(),
325            data.error_sender.clone(),
326            data.is_dropping.clone(),
327            &self.client,
328            owned_room_id.clone(),
329            data.report_media_upload_progress.clone(),
330        );
331
332        map.insert(owned_room_id, room_q.clone());
333
334        room_q
335    }
336
337    /// Enable or disable the send queue for the entire client, i.e. all rooms.
338    ///
339    /// If we're disabling the queue, and requests were being sent, they're not
340    /// aborted, and will continue until a status resolves (error responses will
341    /// keep the events in the buffer of events to send later). The disablement
342    /// will happen before the next request is sent.
343    ///
344    /// This may wake up background tasks and resume sending of requests in the
345    /// background.
346    pub async fn set_enabled(&self, enabled: bool) {
347        debug!(?enabled, "setting global send queue enablement");
348
349        self.data().globally_enabled.store(enabled, Ordering::SeqCst);
350
351        // Wake up individual rooms we already know about.
352        for room in self.data().rooms.read().unwrap().values() {
353            room.set_enabled(enabled);
354        }
355
356        // Reload some extra rooms that might not have been awaken yet, but
357        // could have requests from previous sessions.
358        self.respawn_tasks_for_rooms_with_unsent_requests().await;
359    }
360
361    /// Returns whether the send queue is enabled, at a client-wide granularity.
362    pub fn is_enabled(&self) -> bool {
363        self.data().globally_enabled.load(Ordering::SeqCst)
364    }
365
366    /// Enable or disable progress reporting for media uploads.
367    pub fn enable_upload_progress(&self, enabled: bool) {
368        self.data().report_media_upload_progress.store(enabled, Ordering::SeqCst);
369    }
370
371    /// Subscribe to all updates for all rooms.
372    ///
373    /// Use [`RoomSendQueue::subscribe`] to subscribe to update for a
374    /// _specific room_.
375    pub fn subscribe(&self) -> broadcast::Receiver<SendQueueUpdate> {
376        self.data().global_update_sender.subscribe()
377    }
378
379    /// Get local echoes from all room send queues.
380    pub async fn local_echoes(
381        &self,
382    ) -> Result<BTreeMap<OwnedRoomId, Vec<LocalEcho>>, RoomSendQueueError> {
383        let room_ids =
384            self.client.state_store().load_rooms_with_unsent_requests().await.unwrap_or_else(
385                |err| {
386                    warn!("error when loading rooms with unsent requests: {err}");
387                    Vec::new()
388                },
389            );
390
391        let mut local_echoes: BTreeMap<OwnedRoomId, Vec<LocalEcho>> = BTreeMap::new();
392
393        for room_id in room_ids {
394            if let Some(room) = self.client.get_room(&room_id) {
395                let queue = self.for_room(room);
396                local_echoes
397                    .insert(room_id.to_owned(), queue.inner.queue.local_echoes(&queue).await?);
398            }
399        }
400
401        Ok(local_echoes)
402    }
403
404    /// A subscriber to the enablement status (enabled or disabled) of the send
405    /// queue, along with useful errors.
406    pub fn subscribe_errors(&self) -> broadcast::Receiver<SendQueueRoomError> {
407        self.data().error_sender.subscribe()
408    }
409}
410
411/// Metadata about a thumbnail needed when pushing media uploads to the send
412/// queue.
413#[derive(Clone, Debug)]
414struct QueueThumbnailInfo {
415    /// Metadata about the thumbnail needed when finishing a media upload.
416    finish_upload_thumbnail_info: FinishUploadThumbnailInfo,
417
418    /// The parameters for the request to retrieve the thumbnail data.
419    media_request_parameters: MediaRequestParameters,
420
421    /// The thumbnail's mime type.
422    content_type: Mime,
423
424    /// The thumbnail's file size in bytes.
425    file_size: usize,
426}
427
428/// A specific room's send queue ran into an error.
429#[derive(Clone, Debug)]
430pub struct SendQueueRoomError {
431    /// For which room is the send queue failing?
432    pub room_id: OwnedRoomId,
433
434    /// The error the room has ran into, when trying to send a request.
435    pub error: Arc<crate::Error>,
436
437    /// Whether the error is considered recoverable or not.
438    ///
439    /// An error that's recoverable will disable the room's send queue, while an
440    /// unrecoverable error will be parked, until it's retried or aborted.
441    pub is_recoverable: bool,
442}
443
444impl Client {
445    /// Returns a [`SendQueue`] that handles sending, retrying and not
446    /// forgetting about requests that are to be sent.
447    pub fn send_queue(&self) -> SendQueue {
448        SendQueue::new(self.clone())
449    }
450}
451
452pub(super) struct SendQueueData {
453    /// Mapping of room to their unique send queue.
454    rooms: RwLock<BTreeMap<OwnedRoomId, RoomSendQueue>>,
455
456    /// Is the whole mechanism enabled or disabled?
457    ///
458    /// This is only kept in memory to initialize new room queues with an
459    /// initial enablement state.
460    globally_enabled: AtomicBool,
461
462    /// Global sender to send [`SendQueueUpdate`].
463    ///
464    /// See [`SendQueue::subscribe`].
465    global_update_sender: broadcast::Sender<SendQueueUpdate>,
466
467    /// Global error updates for the send queue.
468    error_sender: broadcast::Sender<SendQueueRoomError>,
469
470    /// Are we currently dropping the Client?
471    is_dropping: Arc<AtomicBool>,
472
473    /// Will media upload progress be reported via send queue updates?
474    report_media_upload_progress: Arc<AtomicBool>,
475
476    /// Task watching for the session getting a working access token again.
477    session_change_task: OnceLock<BackgroundTaskHandle>,
478}
479
480impl SendQueueData {
481    /// Create the data for a send queue, in the given enabled state.
482    pub fn new(globally_enabled: bool) -> Self {
483        let (global_update_sender, _) = broadcast::channel(32);
484        let (error_sender, _) = broadcast::channel(32);
485
486        Self {
487            rooms: Default::default(),
488            globally_enabled: AtomicBool::new(globally_enabled),
489            global_update_sender,
490            error_sender,
491            is_dropping: Arc::new(false.into()),
492            report_media_upload_progress: Arc::new(false.into()),
493            session_change_task: OnceLock::new(),
494        }
495    }
496}
497
498impl Drop for SendQueueData {
499    fn drop(&mut self) {
500        // Mark the whole send queue as shutting down, then wake up all the room
501        // queues so they're stopped too.
502        debug!("globally dropping the send queue");
503        self.is_dropping.store(true, Ordering::SeqCst);
504
505        let rooms = self.rooms.read().unwrap();
506        for room in rooms.values() {
507            room.inner.notifier.notify_one();
508        }
509    }
510}
511
512impl Room {
513    /// Returns the [`RoomSendQueue`] for this specific room.
514    pub fn send_queue(&self) -> RoomSendQueue {
515        self.client.send_queue().for_room(self.clone())
516    }
517}
518
519/// A per-room send queue.
520///
521/// This is cheap to clone.
522#[derive(Clone)]
523pub struct RoomSendQueue {
524    inner: Arc<RoomSendQueueInner>,
525}
526
527#[cfg(not(tarpaulin_include))]
528impl std::fmt::Debug for RoomSendQueue {
529    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
530        f.debug_struct("RoomSendQueue").finish_non_exhaustive()
531    }
532}
533
534impl RoomSendQueue {
535    fn new(
536        globally_enabled: bool,
537        global_update_sender: broadcast::Sender<SendQueueUpdate>,
538        global_error_sender: broadcast::Sender<SendQueueRoomError>,
539        is_dropping: Arc<AtomicBool>,
540        client: &Client,
541        room_id: OwnedRoomId,
542        report_media_upload_progress: Arc<AtomicBool>,
543    ) -> Self {
544        let (update_sender, _) = broadcast::channel(32);
545
546        let queue = QueueStorage::new(client.downgrade(), room_id.clone());
547        let notifier = Arc::new(Notify::new());
548
549        let weak_room = WeakRoom::new(client.downgrade(), room_id);
550        let locally_enabled = Arc::new(AtomicBool::new(globally_enabled));
551
552        let task = client.task_monitor().spawn_infinite_task(
553            "send_queue",
554            Self::sending_task(
555                weak_room.clone(),
556                queue.clone(),
557                notifier.clone(),
558                global_update_sender.clone(),
559                update_sender.clone(),
560                locally_enabled.clone(),
561                global_error_sender,
562                is_dropping,
563                report_media_upload_progress,
564            ),
565        );
566
567        Self {
568            inner: Arc::new(RoomSendQueueInner {
569                room: weak_room,
570                global_update_sender,
571                update_sender,
572                _task: task,
573                queue,
574                notifier,
575                locally_enabled,
576            }),
577        }
578    }
579
580    /// Queues a raw event for sending it to this room.
581    ///
582    /// This immediately returns, and will push the event to be sent into a
583    /// queue, handled in the background.
584    ///
585    /// Callers are expected to consume [`RoomSendQueueUpdate`] via calling the
586    /// [`Self::subscribe()`] method to get updates about the sending of that
587    /// event.
588    ///
589    /// By default, if sending failed on the first attempt, it will be retried a
590    /// few times. If sending failed after those retries, the entire client's
591    /// sending queue will be disabled, and it will need to be manually
592    /// re-enabled by the caller (e.g. after network is back, or when something
593    /// has been done about the faulty requests).
594    pub fn send_raw(
595        &self,
596        content: Raw<AnyMessageLikeEventContent>,
597        event_type: String,
598    ) -> SendRawEvent<'_> {
599        SendRawEvent {
600            queue: self,
601            content: SerializableEventContent::from_raw(content, event_type),
602            #[cfg(feature = "unstable-msc4354")]
603            sticky_duration: None,
604        }
605    }
606
607    /// Queues an already serialized event for sending it to this room.
608    async fn send_serialized(
609        &self,
610        content: SerializableEventContent,
611        #[cfg(feature = "unstable-msc4354")] sticky_duration: Option<StickyDurationMs>,
612    ) -> Result<SendHandle, RoomSendQueueError> {
613        let Some(room) = self.inner.room.get() else {
614            return Err(RoomSendQueueError::RoomDisappeared);
615        };
616        if room.state() != RoomState::Joined {
617            return Err(RoomSendQueueError::RoomNotJoined);
618        }
619
620        let request = QueuedRequestKind::Event {
621            content: content.clone(),
622            #[cfg(feature = "unstable-msc4354")]
623            sticky_duration,
624        };
625
626        let created_at = MilliSecondsSinceUnixEpoch::now();
627        let transaction_id = self.inner.queue.push(request, created_at).await?;
628        trace!(%transaction_id, "manager sends a raw event to the background task");
629
630        self.inner.notifier.notify_one();
631
632        let send_handle = SendHandle {
633            room: self.clone(),
634            transaction_id: transaction_id.clone(),
635            media_handles: vec![],
636            created_at,
637        };
638
639        self.send_update(RoomSendQueueUpdate::NewLocalEvent(LocalEcho {
640            transaction_id,
641            content: LocalEchoContent::Event {
642                serialized_event: content,
643                send_handle: send_handle.clone(),
644                send_error: None,
645            },
646        }));
647
648        Ok(send_handle)
649    }
650
651    /// Queues an event for sending it to this room.
652    ///
653    /// This immediately returns, and will push the event to be sent into a
654    /// queue, handled in the background.
655    ///
656    /// Callers are expected to consume [`RoomSendQueueUpdate`] via calling the
657    /// [`Self::subscribe()`] method to get updates about the sending of that
658    /// event.
659    ///
660    /// By default, if sending failed on the first attempt, it will be retried a
661    /// few times. If sending failed after those retries, the entire client's
662    /// sending queue will be disabled, and it will need to be manually
663    /// re-enabled by the caller (e.g. after network is back, or when something
664    /// has been done about the faulty requests).
665    pub fn send(&self, content: AnyMessageLikeEventContent) -> SendEvent<'_> {
666        SendEvent {
667            queue: self,
668            content,
669            extra_content: None,
670            #[cfg(feature = "unstable-msc4354")]
671            sticky_duration: None,
672        }
673    }
674
675    /// Queues a redaction of another event for sending it to this room.
676    ///
677    /// This immediately returns, and will push the redaction to be sent into a
678    /// queue, handled in the background.
679    ///
680    /// Callers are expected to consume [`RoomSendQueueUpdate`] via calling the
681    /// [`Self::subscribe()`] method to get updates about the sending of that
682    /// redaction.
683    ///
684    /// By default, if sending failed on the first attempt, it will be retried a
685    /// few times. If sending failed after those retries, the entire client's
686    /// sending queue will be disabled, and it will need to be manually
687    /// re-enabled by the caller (e.g. after network is back, or when something
688    /// has been done about the faulty requests).
689    pub async fn redact(
690        &self,
691        redacts: OwnedEventId,
692        reason: Option<&str>,
693    ) -> Result<SendHandle, RoomSendQueueError> {
694        let Some(room) = self.inner.room.get() else {
695            return Err(RoomSendQueueError::RoomDisappeared);
696        };
697        if room.state() != RoomState::Joined {
698            return Err(RoomSendQueueError::RoomNotJoined);
699        }
700
701        let request = QueuedRequestKind::Redaction {
702            redacts: redacts.clone(),
703            reason: reason.map(str::to_owned),
704        };
705
706        let created_at = MilliSecondsSinceUnixEpoch::now();
707        let transaction_id = self.inner.queue.push(request, created_at).await?;
708        trace!(%transaction_id, "manager sends a redaction event to the background task");
709
710        self.inner.notifier.notify_one();
711
712        let send_handle = SendHandle {
713            room: self.clone(),
714            transaction_id: transaction_id.clone(),
715            media_handles: vec![],
716            created_at,
717        };
718
719        self.send_update(RoomSendQueueUpdate::NewLocalEvent(LocalEcho {
720            transaction_id,
721            content: LocalEchoContent::Redaction {
722                redacts,
723                reason: reason.map(str::to_owned),
724                send_handle: send_handle.clone(),
725                send_error: None,
726            },
727        }));
728
729        Ok(send_handle)
730    }
731
732    /// Returns the current local requests as well as a receiver to listen to
733    /// the send queue updates, as defined in [`RoomSendQueueUpdate`].
734    ///
735    /// Use [`SendQueue::subscribe`] to subscribe to update for _all rooms_ with
736    /// a single receiver.
737    pub async fn subscribe(
738        &self,
739    ) -> Result<(Vec<LocalEcho>, broadcast::Receiver<RoomSendQueueUpdate>), RoomSendQueueError>
740    {
741        let local_echoes = self.inner.queue.local_echoes(self).await?;
742
743        Ok((local_echoes, self.inner.update_sender.subscribe()))
744    }
745
746    /// A task that must be spawned in the async runtime, running in the
747    /// background for each room that has a send queue.
748    ///
749    /// It only progresses forward: nothing can be cancelled at any point, which
750    /// makes the implementation not overly complicated to follow.
751    #[allow(clippy::too_many_arguments)]
752    #[instrument(skip_all, fields(room_id = %room.room_id()))]
753    async fn sending_task(
754        room: WeakRoom,
755        queue: QueueStorage,
756        notifier: Arc<Notify>,
757        global_update_sender: broadcast::Sender<SendQueueUpdate>,
758        update_sender: broadcast::Sender<RoomSendQueueUpdate>,
759        locally_enabled: Arc<AtomicBool>,
760        global_error_sender: broadcast::Sender<SendQueueRoomError>,
761        is_dropping: Arc<AtomicBool>,
762        report_media_upload_progress: Arc<AtomicBool>,
763    ) {
764        trace!("spawned the sending task");
765
766        let room_id = room.room_id();
767
768        loop {
769            // A request to shut down should be preferred above everything else.
770            if is_dropping.load(Ordering::SeqCst) {
771                trace!("shutting down!");
772                break;
773            }
774
775            // Try to apply dependent requests now; those applying to previously
776            // failed attempts (local echoes) would succeed now.
777            let mut new_updates = Vec::new();
778            if let Err(err) = queue.apply_dependent_requests(&mut new_updates).await {
779                warn!("errors when applying dependent requests: {err}");
780            }
781
782            for up in new_updates {
783                send_update(&global_update_sender, &update_sender, room_id, up);
784            }
785
786            if !locally_enabled.load(Ordering::SeqCst) {
787                trace!("not enabled, sleeping");
788                // Wait for an explicit wakeup.
789                notifier.notified().await;
790                continue;
791            }
792
793            let (queued_request, cancel_upload_rx) = match queue.peek_next_to_send().await {
794                Ok(Some(request)) => request,
795
796                Ok(None) => {
797                    trace!("queue is empty or blocked on a wedged request, sleeping");
798                    // Wait for an explicit wakeup.
799                    notifier.notified().await;
800                    continue;
801                }
802
803                Err(err) => {
804                    warn!("error when loading next request to send: {err}");
805                    // Don't hammer a failing store; back off a bit before
806                    // retrying.
807                    matrix_sdk_common::sleep::sleep(STORE_ERROR_BACKOFF).await;
808                    continue;
809                }
810            };
811
812            let txn_id = queued_request.transaction_id.clone();
813            trace!(txn_id = %txn_id, "received a request to send!");
814
815            let Some(room) = room.get() else {
816                if is_dropping.load(Ordering::SeqCst) {
817                    break;
818                }
819                error!("the weak room couldn't be upgraded but we're not shutting down?");
820                continue;
821            };
822
823            // If this is a media/gallery upload, prepare the following:
824            //
825            // - transaction id for the related media event request,
826            // - progress metadata to feed the final media upload progress
827            // - an observable to watch the media upload progress.
828            let (related_txn_id, media_upload_progress_info, http_progress) =
829                if let QueuedRequestKind::MediaUpload {
830                    cache_key,
831                    thumbnail_source,
832                    #[cfg(feature = "unstable-msc4274")]
833                    accumulated,
834                    related_to,
835                    ..
836                } = &queued_request.kind
837                {
838                    // Prepare to watch and communicate the request's progress
839                    // for media uploads, if it has been requested.
840                    let (media_upload_progress_info, http_progress) =
841                        if report_media_upload_progress.load(Ordering::SeqCst) {
842                            let media_upload_progress_info =
843                                RoomSendQueue::create_media_upload_progress_info(
844                                    &queued_request.transaction_id,
845                                    related_to,
846                                    cache_key,
847                                    thumbnail_source.as_ref(),
848                                    #[cfg(feature = "unstable-msc4274")]
849                                    accumulated,
850                                    &room,
851                                    &queue,
852                                )
853                                .await;
854
855                            let progress = RoomSendQueue::create_media_upload_progress_observable(
856                                &media_upload_progress_info,
857                                related_to,
858                                &update_sender,
859                            );
860
861                            (Some(media_upload_progress_info), Some(progress))
862                        } else {
863                            Default::default()
864                        };
865
866                    (Some(related_to.clone()), media_upload_progress_info, http_progress)
867                } else {
868                    Default::default()
869                };
870
871            match Self::handle_request(&room, queued_request, cancel_upload_rx, http_progress).await
872            {
873                Ok((Some(parent_key), encryption_info)) => match queue
874                    .mark_as_sent(&txn_id, parent_key.clone())
875                    .await
876                {
877                    Ok(()) => match parent_key {
878                        SentRequestKey::Event { event_id, event, event_type } => {
879                            send_update(
880                                &global_update_sender,
881                                &update_sender,
882                                room_id,
883                                RoomSendQueueUpdate::SentEvent {
884                                    transaction_id: txn_id,
885                                    event_id: event_id.clone(),
886                                },
887                            );
888
889                            // The event has been sent to the server and the
890                            // server has received it. Yepee! Now, we usually
891                            // wait on the server to give us back the event via
892                            // the sync.
893                            //
894                            // Problem: sometimes the network lags, can be down,
895                            // or the server may be slow; well, anything can
896                            // happen.
897                            //
898                            // It results in a weird situation where the user
899                            // sees its event being sent, then disappears before
900                            // it's received again from the server.
901                            //
902                            // To avoid this situation, we eagerly save the
903                            // event in the Event Cache. It's similar to what
904                            // would happen if the event was echoed back from
905                            // the server via the sync, but we avoid any network
906                            // issues. The Event Cache is smart enough to
907                            // deduplicate events based on the event ID, so it's
908                            // safe to do that.
909                            //
910                            // If this little feature fails, it MUST NOT stop
911                            // the Send Queue. Any errors are logged, but the
912                            // Send Queue will continue as if everything
913                            // happened successfully. This feature is not
914                            // considered “crucial”.
915                            if let Some(event_cache) = room.client().event_cache()
916                                && let Ok((room_event_cache, _drop_handles)) =
917                                    event_cache.room(room.room_id()).await
918                            {
919                                let event = build_timeline_event(
920                                    &event_id,
921                                    MilliSecondsSinceUnixEpoch::now().get(),
922                                    room.client().user_id().expect("Client must be logged-in"),
923                                    &event_type,
924                                    event.into_json(),
925                                );
926
927                                let timeline_event = match serde_json::value::to_raw_value(&event)
928                                    .map(Raw::from_json)
929                                {
930                                    Ok(event) => match encryption_info {
931                                        #[cfg(feature = "e2e-encryption")]
932                                        Some(encryption_info) => {
933                                            use matrix_sdk_base::deserialized_responses::DecryptedRoomEvent;
934                                            let decrypted_event = DecryptedRoomEvent {
935                                                event: event.cast_unchecked(),
936                                                encryption_info: Arc::new(encryption_info),
937                                                unsigned_encryption_info: None,
938                                            };
939                                            Some(TimelineEvent::from_decrypted(
940                                                decrypted_event,
941                                                None,
942                                            ))
943                                        }
944                                        _ => Some(TimelineEvent::from_plaintext(event)),
945                                    },
946                                    Err(err) => {
947                                        error!(
948                                            ?err,
949                                            "Failed to build the (sync) event before the saving in the Event Cache"
950                                        );
951                                        None
952                                    }
953                                };
954
955                                // In case of an error, just log the error but
956                                // don't stop the Send Queue. This feature is
957                                // not crucial.
958                                if let Some(timeline_event) = timeline_event
959                                    && let Err(err) = room_event_cache
960                                        .insert_sent_event_from_send_queue(timeline_event)
961                                        .await
962                                {
963                                    error!(
964                                        ?err,
965                                        "Failed to save the sent event in the Event Cache"
966                                    );
967                                }
968                            } else {
969                                info!(
970                                    "Cannot insert the sent event in the Event Cache because \
971                                    either the room no longer exists, or the Room Event Cache cannot be retrieved"
972                                );
973                            }
974                        }
975
976                        SentRequestKey::Media(sent_media_info) => {
977                            // Generate some final progress information, even if
978                            // incremental progress wasn't requested.
979                            let index =
980                                media_upload_progress_info.as_ref().map_or(0, |info| info.index);
981                            let progress = media_upload_progress_info
982                                .as_ref()
983                                .map(|info| {
984                                    AbstractProgress { current: info.bytes, total: info.bytes }
985                                        + info.offsets
986                                })
987                                .unwrap_or(AbstractProgress { current: 1, total: 1 });
988
989                            // Purposefully don't use `send_update` here,
990                            // because we don't want to notify the global
991                            // listeners about an upload progress update.
992                            let _ = update_sender.send(RoomSendQueueUpdate::MediaUpload {
993                                related_to: related_txn_id.as_ref().unwrap_or(&txn_id).clone(),
994                                file: Some(sent_media_info.file),
995                                index,
996                                progress,
997                            });
998                        }
999
1000                        SentRequestKey::Redaction { event_id, redacts, reason } => {
1001                            send_update(
1002                                &global_update_sender,
1003                                &update_sender,
1004                                room_id,
1005                                RoomSendQueueUpdate::SentEvent {
1006                                    transaction_id: txn_id,
1007                                    event_id: event_id.clone(),
1008                                },
1009                            );
1010
1011                            // The redaction event has been sent to the server
1012                            // and the server has received it. It's safe to
1013                            // cache the event now to avoid any inconsistencies
1014                            // until the server sends down the remote echo via
1015                            // the sync.
1016                            if let Some(event_cache) = room.client().event_cache()
1017                                && let Ok((room_event_cache, _drop_handles)) =
1018                                    event_cache.room(room.room_id()).await
1019                            {
1020                                let content_field_redacts = room.version().is_some_and(|id| {
1021                                    id.rules()
1022                                        .is_some_and(|rules| rules.redaction.content_field_redacts)
1023                                });
1024
1025                                let event = build_redaction_event(
1026                                    &event_id,
1027                                    MilliSecondsSinceUnixEpoch::now().get(),
1028                                    room.client().user_id().expect("Client must be logged-in"),
1029                                    &redacts,
1030                                    reason.as_deref(),
1031                                    content_field_redacts,
1032                                );
1033
1034                                let timeline_event = match serde_json::value::to_raw_value(&event)
1035                                    .map(Raw::from_json)
1036                                {
1037                                    Ok(event) => Some(TimelineEvent::from_plaintext(event)),
1038                                    Err(err) => {
1039                                        error!(
1040                                            ?err,
1041                                            "Failed to build the (sync) redaction event before the saving in the Event Cache"
1042                                        );
1043                                        None
1044                                    }
1045                                };
1046
1047                                // In case of an error, just log the error but
1048                                // don't stop the Send Queue. This feature is
1049                                // not crucial.
1050                                if let Some(timeline_event) = timeline_event
1051                                    && let Err(err) = room_event_cache
1052                                        .insert_sent_event_from_send_queue(timeline_event)
1053                                        .await
1054                                {
1055                                    error!(
1056                                        ?err,
1057                                        "Failed to save the sent redaction event in the Event Cache"
1058                                    );
1059                                }
1060                            } else {
1061                                info!(
1062                                    "Cannot insert the sent redaction event in the Event Cache because \
1063                                    either the room no longer exists, or the Room Event Cache cannot be retrieved"
1064                                );
1065                            }
1066                        }
1067                    },
1068
1069                    Err(err) => {
1070                        warn!("unable to mark queued request as sent: {err}");
1071                    }
1072                },
1073
1074                Ok((None, _)) => {
1075                    debug!("Request has been aborted while running, continuing.");
1076                }
1077
1078                // The server already has this exact reaction from us (sent from
1079                // another device, or by an earlier attempt it no longer
1080                // remembers), so there's nothing left to do. Wedging would
1081                // block the room for good: every retry gets the same answer.
1082                Err(err)
1083                    if err.client_api_error_kind() == Some(&ErrorKind::DuplicateAnnotation) =>
1084                {
1085                    debug!(txn_id = %txn_id, "the server already has this reaction, dropping it");
1086
1087                    queue.mark_as_not_being_sent(&txn_id).await;
1088
1089                    match queue.cancel_event(&txn_id, None).await {
1090                        Ok(true) => send_update(
1091                            &global_update_sender,
1092                            &update_sender,
1093                            room_id,
1094                            RoomSendQueueUpdate::CancelledLocalEvent { transaction_id: txn_id },
1095                        ),
1096
1097                        // Removed meanwhile (aborted before we got here);
1098                        // nothing to report.
1099                        Ok(false) => {}
1100
1101                        Err(storage_error) => {
1102                            // Still queued, so it would be sent again right
1103                            // away, over and over: stop the queue instead.
1104                            error!(
1105                                txn_id = %txn_id,
1106                                "couldn't drop the duplicate reaction, disabling the queue: {storage_error}"
1107                            );
1108                            locally_enabled.store(false, Ordering::SeqCst);
1109                        }
1110                    }
1111                }
1112
1113                Err(err) => {
1114                    let is_recoverable = match err {
1115                        crate::Error::Http(ref http_err) => {
1116                            // All transient errors are recoverable.
1117                            matches!(
1118                                http_err.retry_kind(),
1119                                RetryKind::Transient { .. } | RetryKind::NetworkFailure
1120                            )
1121                        }
1122
1123                        // `ConcurrentRequestFailed` typically happens because
1124                        // of an HTTP failure; since we don't get the underlying
1125                        // error, be lax and consider it recoverable, and let
1126                        // observers decide to retry it or not. At some point
1127                        // we'll get the actual underlying error.
1128                        crate::Error::ConcurrentRequestFailed => true,
1129
1130                        // As of 2024-06-27, all other error types are considered unrecoverable.
1131                        _ => false,
1132                    };
1133
1134                    if is_recoverable {
1135                        // Disable the queue for this room; there's nothing else
1136                        // blocking it, and whatever caused the failure is
1137                        // likely to affect the next requests too.
1138                        locally_enabled.store(false, Ordering::SeqCst);
1139
1140                        warn!(txn_id = %txn_id, error = ?err, "Recoverable error when sending request: {err}, disabling send queue");
1141
1142                        // In this case, we intentionally keep the request in
1143                        // the queue, but mark it as not being sent anymore.
1144                        queue.mark_as_not_being_sent(&txn_id).await;
1145
1146                        // Let observers know about a failure _after_ we've
1147                        // marked the item as not being sent anymore. Otherwise,
1148                        // there's a possible race where a caller might try to
1149                        // remove an item, while it's still marked as being
1150                        // sent, resulting in a cancellation failure.
1151                    } else {
1152                        warn!(txn_id = %txn_id, error = ?err, "Unrecoverable error when sending request: {err}");
1153
1154                        // Mark the request as wedged, so it's not picked at any
1155                        // future point; it will also block subsequent requests
1156                        // in the same room from being sent, until it's unwedged
1157                        // or removed, so as to preserve ordering.
1158                        if let Err(storage_error) =
1159                            queue.mark_as_wedged(&txn_id, QueueWedgeError::from(&err)).await
1160                        {
1161                            // Nothing recorded the wedge, so the request would
1162                            // be picked up and sent again right away, over and
1163                            // over; disabling the queue is the only brake left.
1164                            error!(
1165                                "unable to mark request as wedged, disabling the queue: {storage_error}"
1166                            );
1167                            locally_enabled.store(false, Ordering::SeqCst);
1168                        }
1169                    }
1170
1171                    let error = Arc::new(err);
1172
1173                    let _ = global_error_sender.send(SendQueueRoomError {
1174                        room_id: room_id.to_owned(),
1175                        error: error.clone(),
1176                        is_recoverable,
1177                    });
1178
1179                    send_update(
1180                        &global_update_sender,
1181                        &update_sender,
1182                        room_id,
1183                        RoomSendQueueUpdate::SendError {
1184                            transaction_id: related_txn_id.unwrap_or(txn_id),
1185                            error,
1186                            is_recoverable,
1187                        },
1188                    );
1189                }
1190            }
1191        }
1192
1193        info!("exited sending task");
1194    }
1195
1196    /// Handles a single request and returns the [`SentRequestKey`] on success
1197    /// (unless the request was cancelled, in which case it'll return `None`).
1198    async fn handle_request(
1199        room: &Room,
1200        request: QueuedRequest,
1201        cancel_upload_rx: Option<oneshot::Receiver<()>>,
1202        progress: Option<SharedObservable<TransmissionProgress>>,
1203    ) -> Result<(Option<SentRequestKey>, Option<EncryptionInfo>), crate::Error> {
1204        match request.kind {
1205            QueuedRequestKind::Event {
1206                content,
1207                #[cfg(feature = "unstable-msc4354")]
1208                sticky_duration,
1209            } => {
1210                let (event, event_type) = content.into_raw();
1211
1212                let future = room
1213                    .send_raw(&event_type, &event)
1214                    .with_transaction_id(&request.transaction_id)
1215                    .with_request_config(RequestConfig::short_retry());
1216
1217                #[cfg(feature = "unstable-msc4354")]
1218                let future = match sticky_duration {
1219                    Some(duration) => {
1220                        future.with_sticky_duration(Duration::from_millis(duration.get().into()))
1221                    }
1222                    None => future,
1223                };
1224
1225                let result = future.await?;
1226
1227                trace!(txn_id = %request.transaction_id, event_id = %result.response.event_id, "event successfully sent");
1228
1229                Ok((
1230                    Some(SentRequestKey::Event {
1231                        event_id: result.response.event_id,
1232                        event,
1233                        event_type,
1234                    }),
1235                    result.encryption_info,
1236                ))
1237            }
1238
1239            QueuedRequestKind::MediaUpload {
1240                content_type,
1241                cache_key,
1242                thumbnail_source,
1243                related_to: relates_to,
1244                #[cfg(feature = "unstable-msc4274")]
1245                accumulated,
1246            } => {
1247                trace!(%relates_to, "uploading media related to event");
1248
1249                let fut = async move {
1250                    let data = room
1251                        .client()
1252                        .media_store()
1253                        .lock()
1254                        .await?
1255                        .get_media_content(&cache_key)
1256                        .await?
1257                        .ok_or(crate::Error::SendQueueWedgeError(Box::new(
1258                            QueueWedgeError::MissingMediaContent,
1259                        )))?;
1260
1261                    let mime = Mime::from_str(&content_type).map_err(|_| {
1262                        crate::Error::SendQueueWedgeError(Box::new(
1263                            QueueWedgeError::InvalidMimeType { mime_type: content_type.clone() },
1264                        ))
1265                    })?;
1266
1267                    #[cfg(feature = "e2e-encryption")]
1268                    let media_source = if room.latest_encryption_state().await?.is_encrypted() {
1269                        trace!("upload will be encrypted (encrypted room)");
1270
1271                        let mut cursor = std::io::Cursor::new(data);
1272                        let mut req = room
1273                            .client
1274                            .upload_encrypted_file(&mut cursor)
1275                            .with_request_config(RequestConfig::short_retry());
1276                        if let Some(progress) = progress {
1277                            req = req.with_send_progress_observable(progress);
1278                        }
1279                        let encrypted_file = req.await?;
1280
1281                        MediaSource::Encrypted(Box::new(encrypted_file))
1282                    } else {
1283                        trace!("upload will be in clear text (room without encryption)");
1284
1285                        let request_config = RequestConfig::short_retry()
1286                            .timeout(Media::reasonable_upload_timeout(&data));
1287                        let mut req =
1288                            room.client().media().upload(&mime, data, Some(request_config));
1289                        if let Some(progress) = progress {
1290                            req = req.with_send_progress_observable(progress);
1291                        }
1292                        let res = req.await?;
1293
1294                        MediaSource::Plain(res.content_uri)
1295                    };
1296
1297                    #[cfg(not(feature = "e2e-encryption"))]
1298                    let media_source = {
1299                        let request_config = RequestConfig::short_retry()
1300                            .timeout(Media::reasonable_upload_timeout(&data));
1301                        let mut req =
1302                            room.client().media().upload(&mime, data, Some(request_config));
1303                        if let Some(progress) = progress {
1304                            req = req.with_send_progress_observable(progress);
1305                        }
1306                        let res = req.await?;
1307                        MediaSource::Plain(res.content_uri)
1308                    };
1309
1310                    let uri = match &media_source {
1311                        MediaSource::Plain(uri) => uri,
1312                        MediaSource::Encrypted(encrypted_file) => &encrypted_file.url,
1313                    };
1314                    trace!(%relates_to, mxc_uri = %uri, "media successfully uploaded");
1315
1316                    Ok((
1317                        Some(SentRequestKey::Media(SentMediaInfo {
1318                            file: media_source,
1319                            thumbnail: thumbnail_source,
1320                            #[cfg(feature = "unstable-msc4274")]
1321                            accumulated,
1322                        })),
1323                        None,
1324                    ))
1325                };
1326
1327                let wait_for_cancel = async move {
1328                    if let Some(rx) = cancel_upload_rx {
1329                        rx.await
1330                    } else {
1331                        std::future::pending().await
1332                    }
1333                };
1334
1335                tokio::select! {
1336                    biased;
1337
1338                    _ = wait_for_cancel => {
1339                        Ok((None, None))
1340                    }
1341
1342                    res = fut => {
1343                        res
1344                    }
1345                }
1346            }
1347
1348            QueuedRequestKind::Redaction { redacts, reason } => {
1349                let result = room
1350                    .redact(&redacts, reason.as_deref(), Some(request.transaction_id.clone()))
1351                    .await?;
1352
1353                trace!(txn_id = %request.transaction_id, event_id = %result.event_id, "redaction successfully sent");
1354
1355                Ok((
1356                    Some(SentRequestKey::Redaction { event_id: result.event_id, redacts, reason }),
1357                    None,
1358                ))
1359            }
1360        }
1361    }
1362
1363    /// Returns whether the room is enabled, at the room level.
1364    pub fn is_enabled(&self) -> bool {
1365        self.inner.locally_enabled.load(Ordering::SeqCst)
1366    }
1367
1368    /// Set the locally enabled flag for this room queue.
1369    pub fn set_enabled(&self, enabled: bool) {
1370        self.inner.locally_enabled.store(enabled, Ordering::SeqCst);
1371
1372        // No need to wake a task to tell it it's been disabled, so only notify
1373        // if we're re-enabling the queue.
1374        if enabled {
1375            self.inner.notifier.notify_one();
1376        }
1377    }
1378
1379    /// Send an update on the room send queue channel, and on the global send
1380    /// queue channel, i.e. it sends a [`RoomSendQueueUpdate`] and a
1381    /// [`SendQueueUpdate`].
1382    fn send_update(&self, update: RoomSendQueueUpdate) {
1383        let _ = self.inner.update_sender.send(update.clone());
1384        let _ = self
1385            .inner
1386            .global_update_sender
1387            .send(SendQueueUpdate { room_id: self.inner.room.room_id().to_owned(), update });
1388    }
1389
1390    /// Clear the wedged status of every request that was wedged because the
1391    /// access token had been rejected.
1392    async fn unwedge_requests_with_expired_token(&self) {
1393        let transaction_ids = match self.inner.queue.requests_with_expired_token().await {
1394            Ok(transaction_ids) => transaction_ids,
1395            Err(err) => {
1396                warn!("error when looking for requests wedged by an expired token: {err}");
1397                return;
1398            }
1399        };
1400
1401        for transaction_id in transaction_ids {
1402            if let Err(err) = self.unwedge_request(&transaction_id).await {
1403                warn!(%transaction_id, "error when unwedging a request: {err}");
1404            }
1405        }
1406    }
1407
1408    /// Clear a request's wedged status and wake the queue up so it's tried
1409    /// again.
1410    async fn unwedge_request(
1411        &self,
1412        transaction_id: &TransactionId,
1413    ) -> Result<(), RoomSendQueueError> {
1414        self.inner
1415            .queue
1416            .mark_as_unwedged(transaction_id)
1417            .await
1418            .map_err(RoomSendQueueError::StorageError)?;
1419
1420        // Wake up the queue, in case the room was asleep before unwedging the
1421        // request.
1422        self.inner.notifier.notify_one();
1423
1424        self.send_update(RoomSendQueueUpdate::RetryEvent {
1425            transaction_id: transaction_id.to_owned(),
1426        });
1427
1428        Ok(())
1429    }
1430}
1431
1432fn send_update(
1433    global_update_sender: &broadcast::Sender<SendQueueUpdate>,
1434    update_sender: &broadcast::Sender<RoomSendQueueUpdate>,
1435    room_id: &RoomId,
1436    update: RoomSendQueueUpdate,
1437) {
1438    let _ = update_sender.send(update.clone());
1439    let _ = global_update_sender.send(SendQueueUpdate { room_id: room_id.to_owned(), update });
1440}
1441
1442impl From<&crate::Error> for QueueWedgeError {
1443    fn from(value: &crate::Error) -> Self {
1444        match value {
1445            #[cfg(feature = "e2e-encryption")]
1446            crate::Error::OlmError(error) => match &**error {
1447                OlmError::SessionRecipientCollectionError(error) => match error {
1448                    SessionRecipientCollectionError::VerifiedUserHasUnsignedDevice(user_map) => {
1449                        QueueWedgeError::InsecureDevices { user_device_map: user_map.clone() }
1450                    }
1451
1452                    SessionRecipientCollectionError::VerifiedUserChangedIdentity(users) => {
1453                        QueueWedgeError::IdentityViolations { users: users.clone() }
1454                    }
1455
1456                    SessionRecipientCollectionError::CrossSigningNotSetup
1457                    | SessionRecipientCollectionError::SendingFromUnverifiedDevice => {
1458                        QueueWedgeError::CrossVerificationRequired
1459                    }
1460                },
1461                _ => QueueWedgeError::GenericApiError { msg: value.to_string() },
1462            },
1463
1464            // Flatten errors of `Self` type.
1465            crate::Error::SendQueueWedgeError(error) => *error.clone(),
1466
1467            crate::Error::Http(error) if error.is_rejected_token() => {
1468                QueueWedgeError::ExpiredAccessToken
1469            }
1470
1471            _ => QueueWedgeError::GenericApiError { msg: value.to_string() },
1472        }
1473    }
1474}
1475
1476struct RoomSendQueueInner {
1477    /// The room which this send queue relates to.
1478    room: WeakRoom,
1479
1480    /// Global sender to send [`SendQueueUpdate`].
1481    ///
1482    /// See [`SendQueue::subscribe`].
1483    global_update_sender: broadcast::Sender<SendQueueUpdate>,
1484
1485    /// Broadcaster for notifications about the statuses of requests to be sent.
1486    ///
1487    /// Can be subscribed to from the outside.
1488    ///
1489    /// See [`RoomSendQueue::subscribe`].
1490    update_sender: broadcast::Sender<RoomSendQueueUpdate>,
1491
1492    /// Queue of requests that are either to be sent, or being sent.
1493    ///
1494    /// When a request has been sent to the server, it is removed from that
1495    /// queue _after_ being sent. That way, we will retry sending upon failure,
1496    /// in the same order requests have been inserted in the first place.
1497    queue: QueueStorage,
1498
1499    /// A notifier that's updated any time common data is touched (stopped or
1500    /// enabled statuses), or the associated room [`QueueStorage`].
1501    notifier: Arc<Notify>,
1502
1503    /// Should the room process new requests or not (because e.g. it might be
1504    /// running off the network)?
1505    locally_enabled: Arc<AtomicBool>,
1506
1507    /// Handle to the actual sending task. Unused, but kept alive along this
1508    /// data structure.
1509    _task: BackgroundTaskHandle,
1510}
1511
1512/// Information about a request being sent right this moment.
1513struct BeingSentInfo {
1514    /// Transaction id of the thing being sent.
1515    transaction_id: OwnedTransactionId,
1516
1517    /// For an upload request, a trigger to cancel the upload before it
1518    /// completes.
1519    cancel_upload: Option<oneshot::Sender<()>>,
1520}
1521
1522impl BeingSentInfo {
1523    /// Aborts the upload, if a trigger is available.
1524    ///
1525    /// Consumes the object because the sender is a oneshot and will be consumed
1526    /// upon sending.
1527    fn cancel_upload(self) -> bool {
1528        if let Some(cancel_upload) = self.cancel_upload {
1529            let _ = cancel_upload.send(());
1530            true
1531        } else {
1532            false
1533        }
1534    }
1535}
1536
1537/// A specialized lock that guards both against the state store and the
1538/// [`Self::being_sent`] data.
1539#[derive(Clone)]
1540struct StoreLock {
1541    /// Reference to the client, to get access to the underlying store.
1542    client: WeakClient,
1543
1544    /// The one queued request that is being sent at the moment, along with
1545    /// associated data that can be useful to act upon it.
1546    ///
1547    /// Also used as the lock to access the state store.
1548    being_sent: Arc<Mutex<Option<BeingSentInfo>>>,
1549}
1550
1551impl StoreLock {
1552    /// Gets a hold of the locked store and [`Self::being_sent`] pair.
1553    async fn lock(&self) -> StoreLockGuard {
1554        StoreLockGuard {
1555            client: self.client.clone(),
1556            being_sent: self.being_sent.clone().lock_owned().await,
1557        }
1558    }
1559}
1560
1561/// A lock guard obtained through locking with [`StoreLock`]. `being_sent` data.
1562struct StoreLockGuard {
1563    /// Reference to the client, to get access to the underlying store.
1564    client: WeakClient,
1565
1566    /// The one queued request that is being sent at the moment, along with
1567    /// associated data that can be useful to act upon it.
1568    being_sent: OwnedMutexGuard<Option<BeingSentInfo>>,
1569}
1570
1571impl StoreLockGuard {
1572    /// Get a client from the locked state, useful to get a handle on a store.
1573    fn client(&self) -> Result<Client, RoomSendQueueStorageError> {
1574        self.client.get().ok_or(RoomSendQueueStorageError::ClientShuttingDown)
1575    }
1576}
1577
1578#[derive(Clone)]
1579struct QueueStorage {
1580    /// A lock to make sure the state store is only accessed once at a time, to
1581    /// make some store operations atomic.
1582    store: StoreLock,
1583
1584    /// To which room is this storage related.
1585    room_id: OwnedRoomId,
1586
1587    /// In-memory mapping of media transaction IDs to thumbnail sizes for the
1588    /// purpose of progress reporting.
1589    ///
1590    /// The keys are the transaction IDs for sending the media or gallery event
1591    /// after all uploads have finished. This allows us to easily clean up the
1592    /// cache after the event was sent.
1593    ///
1594    /// For media uploads, the value vector will always have a single element.
1595    ///
1596    /// For galleries, some gallery items might not have a thumbnail while
1597    /// others do. Since we access the thumbnails by their index within the
1598    /// gallery, the vector needs to hold optional usize's.
1599    thumbnail_file_sizes: Arc<SyncMutex<HashMap<OwnedTransactionId, Vec<Option<usize>>>>>,
1600}
1601
1602impl QueueStorage {
1603    /// Default priority for a queued request.
1604    const LOW_PRIORITY: usize = 0;
1605
1606    /// High priority for a queued request that must be handled before others.
1607    const HIGH_PRIORITY: usize = 10;
1608
1609    /// Create a new queue for queuing requests to be sent later.
1610    fn new(client: WeakClient, room: OwnedRoomId) -> Self {
1611        Self {
1612            room_id: room,
1613            store: StoreLock { client, being_sent: Default::default() },
1614            thumbnail_file_sizes: Default::default(),
1615        }
1616    }
1617
1618    /// Push a new event to be sent in the queue, with a default priority of 0.
1619    ///
1620    /// Returns the transaction id chosen to identify the request.
1621    async fn push(
1622        &self,
1623        request: QueuedRequestKind,
1624        created_at: MilliSecondsSinceUnixEpoch,
1625    ) -> Result<OwnedTransactionId, RoomSendQueueStorageError> {
1626        let transaction_id = TransactionId::new();
1627
1628        self.store
1629            .lock()
1630            .await
1631            .client()?
1632            .state_store()
1633            .save_send_queue_request(
1634                &self.room_id,
1635                transaction_id.clone(),
1636                created_at,
1637                request,
1638                Self::LOW_PRIORITY,
1639            )
1640            .await?;
1641
1642        Ok(transaction_id)
1643    }
1644
1645    /// Peeks the next request to be sent, marking it as being sent.
1646    ///
1647    /// It is required to call [`Self::mark_as_sent`] after it's been
1648    /// effectively sent.
1649    async fn peek_next_to_send(
1650        &self,
1651    ) -> Result<Option<(QueuedRequest, Option<oneshot::Receiver<()>>)>, RoomSendQueueStorageError>
1652    {
1653        let mut guard = self.store.lock().await;
1654        let queued_requests =
1655            guard.client()?.state_store().load_send_queue_requests(&self.room_id).await?;
1656
1657        // Only ever consider the head of the queue: requests must be sent in
1658        // the order they were queued, so a wedged request (which failed to be
1659        // sent with an unrecoverable error) blocks all the requests queued
1660        // after it. Otherwise, messages would be sent out of order, until the
1661        // wedged request is either manually unwedged or removed (both of which
1662        // will wake up the sending task).
1663        if let Some(request) = queued_requests.first().filter(|queued| !queued.is_wedged()) {
1664            let (cancel_upload_tx, cancel_upload_rx) =
1665                if matches!(request.kind, QueuedRequestKind::MediaUpload { .. }) {
1666                    let (tx, rx) = oneshot::channel();
1667                    (Some(tx), Some(rx))
1668                } else {
1669                    Default::default()
1670                };
1671
1672            let prev = guard.being_sent.replace(BeingSentInfo {
1673                transaction_id: request.transaction_id.clone(),
1674                cancel_upload: cancel_upload_tx,
1675            });
1676
1677            if let Some(prev) = prev {
1678                error!(
1679                    prev_txn = ?prev.transaction_id,
1680                    "a previous request was still active while picking a new one"
1681                );
1682            }
1683
1684            Ok(Some((request.clone(), cancel_upload_rx)))
1685        } else {
1686            Ok(None)
1687        }
1688    }
1689
1690    /// Marks a request popped with [`Self::peek_next_to_send`] and identified
1691    /// with the given transaction id as not being sent anymore, so it can be
1692    /// removed from the queue later.
1693    async fn mark_as_not_being_sent(&self, transaction_id: &TransactionId) {
1694        let was_being_sent = self.store.lock().await.being_sent.take();
1695
1696        let prev_txn = was_being_sent.as_ref().map(|info| info.transaction_id.as_ref());
1697        if prev_txn != Some(transaction_id) {
1698            error!(prev_txn = ?prev_txn, "previous active request didn't match that we expect (after transient error)");
1699        }
1700    }
1701
1702    /// Marks a request popped with [`Self::peek_next_to_send`] and identified
1703    /// with the given transaction id as being wedged (and not being sent
1704    /// anymore), so it can be removed from the queue later.
1705    async fn mark_as_wedged(
1706        &self,
1707        transaction_id: &TransactionId,
1708        reason: QueueWedgeError,
1709    ) -> Result<(), RoomSendQueueStorageError> {
1710        // Keep the lock until we're done touching the storage.
1711        let mut guard = self.store.lock().await;
1712        let was_being_sent = guard.being_sent.take();
1713
1714        let prev_txn = was_being_sent.as_ref().map(|info| info.transaction_id.as_ref());
1715        if prev_txn != Some(transaction_id) {
1716            error!(
1717                ?prev_txn,
1718                "previous active request didn't match that we expect (after permanent error)",
1719            );
1720        }
1721
1722        Ok(guard
1723            .client()?
1724            .state_store()
1725            .update_send_queue_request_status(&self.room_id, transaction_id, Some(reason))
1726            .await?)
1727    }
1728
1729    /// Marks a request identified with the given transaction id as being now
1730    /// unwedged and adds it back to the queue.
1731    async fn mark_as_unwedged(
1732        &self,
1733        transaction_id: &TransactionId,
1734    ) -> Result<(), RoomSendQueueStorageError> {
1735        Ok(self
1736            .store
1737            .lock()
1738            .await
1739            .client()?
1740            .state_store()
1741            .update_send_queue_request_status(&self.room_id, transaction_id, None)
1742            .await?)
1743    }
1744
1745    /// The transaction ids of the requests that were wedged because the
1746    /// access token had been rejected.
1747    async fn requests_with_expired_token(
1748        &self,
1749    ) -> Result<Vec<OwnedTransactionId>, RoomSendQueueStorageError> {
1750        Ok(self
1751            .store
1752            .lock()
1753            .await
1754            .client()?
1755            .state_store()
1756            .load_send_queue_requests(&self.room_id)
1757            .await?
1758            .into_iter()
1759            .filter(|request| matches!(request.error, Some(QueueWedgeError::ExpiredAccessToken)))
1760            .map(|request| request.transaction_id)
1761            .collect())
1762    }
1763
1764    /// Marks a request pushed with [`Self::push`] and identified with the given
1765    /// transaction id as sent, by removing it from the local queue.
1766    async fn mark_as_sent(
1767        &self,
1768        transaction_id: &TransactionId,
1769        parent_key: SentRequestKey,
1770    ) -> Result<(), RoomSendQueueStorageError> {
1771        // Keep the lock until we're done touching the storage.
1772        let mut guard = self.store.lock().await;
1773        let was_being_sent = guard.being_sent.take();
1774
1775        let prev_txn = was_being_sent.as_ref().map(|info| info.transaction_id.as_ref());
1776        if prev_txn != Some(transaction_id) {
1777            error!(
1778                ?prev_txn,
1779                "previous active request didn't match that we expect (after successful send)",
1780            );
1781        }
1782
1783        let client = guard.client()?;
1784        let store = client.state_store();
1785
1786        // Update all dependent requests.
1787        store
1788            .mark_dependent_queued_requests_as_ready(&self.room_id, transaction_id, parent_key)
1789            .await?;
1790
1791        let removed = store.remove_send_queue_request(&self.room_id, transaction_id).await?;
1792
1793        if !removed {
1794            warn!(txn_id = %transaction_id, "request marked as sent was missing from storage");
1795        }
1796
1797        self.thumbnail_file_sizes.lock().remove(transaction_id);
1798
1799        Ok(())
1800    }
1801
1802    /// Cancel a sending command for an event that has been sent with
1803    /// [`Self::push`] with the given transaction id.
1804    ///
1805    /// Returns whether the given transaction has been effectively removed. If
1806    /// false, this either means that the transaction id was unrelated to this
1807    /// queue, or that the request was sent before we cancelled it.
1808    async fn cancel_event(
1809        &self,
1810        transaction_id: &TransactionId,
1811        reason: Option<String>,
1812    ) -> Result<bool, RoomSendQueueStorageError> {
1813        let guard = self.store.lock().await;
1814
1815        if guard.being_sent.as_ref().map(|info| info.transaction_id.as_ref())
1816            == Some(transaction_id)
1817        {
1818            // Save the intent to redact the event.
1819            guard
1820                .client()?
1821                .state_store()
1822                .save_dependent_queued_request(
1823                    &self.room_id,
1824                    transaction_id,
1825                    ChildTransactionId::new(),
1826                    MilliSecondsSinceUnixEpoch::now(),
1827                    DependentQueuedRequestKind::RedactEventWithReason { reason },
1828                )
1829                .await?;
1830
1831            return Ok(true);
1832        }
1833
1834        let removed = guard
1835            .client()?
1836            .state_store()
1837            .remove_send_queue_request(&self.room_id, transaction_id)
1838            .await?;
1839
1840        self.thumbnail_file_sizes.lock().remove(transaction_id);
1841
1842        Ok(removed)
1843    }
1844
1845    /// Replace an event that has been sent with [`Self::push`] with the given
1846    /// transaction id, before it's been actually sent.
1847    ///
1848    /// Returns whether the given transaction has been effectively edited. If
1849    /// false, this either means that the transaction id was unrelated to this
1850    /// queue, or that the request was sent before we edited it.
1851    async fn replace_event(
1852        &self,
1853        transaction_id: &TransactionId,
1854        serializable: SerializableEventContent,
1855    ) -> Result<bool, RoomSendQueueStorageError> {
1856        let guard = self.store.lock().await;
1857        let client = guard.client()?;
1858        let store = client.state_store();
1859
1860        // Only an event the user composed has content to replace: a redaction
1861        // or a reaction has nothing to put the new content into.
1862        if !store.load_send_queue_requests(&self.room_id).await?.iter().any(|request| {
1863            request.transaction_id == transaction_id && is_own_event_request(request)
1864        }) {
1865            return Ok(false);
1866        }
1867
1868        if guard.being_sent.as_ref().map(|info| info.transaction_id.as_ref())
1869            == Some(transaction_id)
1870        {
1871            // Save the intent to edit the associated event.
1872            store
1873                .save_dependent_queued_request(
1874                    &self.room_id,
1875                    transaction_id,
1876                    ChildTransactionId::new(),
1877                    MilliSecondsSinceUnixEpoch::now(),
1878                    DependentQueuedRequestKind::EditEvent { new_content: serializable },
1879                )
1880                .await?;
1881
1882            return Ok(true);
1883        }
1884
1885        let request = QueuedRequestKind::Event {
1886            content: serializable,
1887            #[cfg(feature = "unstable-msc4354")]
1888            sticky_duration: self.sticky_duration_of(store, transaction_id).await?,
1889        };
1890
1891        let edited =
1892            store.update_send_queue_request(&self.room_id, transaction_id, request).await?;
1893
1894        Ok(edited)
1895    }
1896
1897    /// The sticky duration of the queued event `transaction_id`, if it is
1898    /// queued and sticky.
1899    #[cfg(feature = "unstable-msc4354")]
1900    async fn sticky_duration_of(
1901        &self,
1902        store: &DynStateStore,
1903        transaction_id: &TransactionId,
1904    ) -> Result<Option<StickyDurationMs>, RoomSendQueueStorageError> {
1905        let sticky_duration = store
1906            .load_send_queue_requests(&self.room_id)
1907            .await?
1908            .into_iter()
1909            .find(|request| request.transaction_id == *transaction_id)
1910            .and_then(|request| match request.kind {
1911                QueuedRequestKind::Event { sticky_duration, .. } => sticky_duration,
1912                _ => None,
1913            });
1914
1915        Ok(sticky_duration)
1916    }
1917
1918    /// Push requests (and dependents) to upload a media.
1919    ///
1920    /// See the module-level description for details of the whole processus.
1921    #[allow(clippy::too_many_arguments)]
1922    async fn push_media(
1923        &self,
1924        event: RoomMessageEventContent,
1925        content_type: Mime,
1926        send_event_txn: OwnedTransactionId,
1927        created_at: MilliSecondsSinceUnixEpoch,
1928        upload_file_txn: OwnedTransactionId,
1929        file_media_request: MediaRequestParameters,
1930        thumbnail: Option<QueueThumbnailInfo>,
1931        extra_content: Option<serde_json::Map<String, serde_json::Value>>,
1932    ) -> Result<(), RoomSendQueueStorageError> {
1933        let guard = self.store.lock().await;
1934        let client = guard.client()?;
1935        let store = client.state_store();
1936
1937        // There's only a single media to be sent, so it has at most one
1938        // thumbnail.
1939        let thumbnail_file_sizes = vec![thumbnail.as_ref().map(|t| t.file_size)];
1940
1941        let thumbnail_info = self
1942            .push_thumbnail_and_media_uploads(
1943                store,
1944                &content_type,
1945                send_event_txn.clone(),
1946                created_at,
1947                upload_file_txn.clone(),
1948                file_media_request,
1949                thumbnail,
1950            )
1951            .await?;
1952
1953        // Push the dependent request for the event itself.
1954        store
1955            .save_dependent_queued_request(
1956                &self.room_id,
1957                &upload_file_txn,
1958                send_event_txn.clone().into(),
1959                created_at,
1960                DependentQueuedRequestKind::FinishUpload {
1961                    local_echo: Box::new(event),
1962                    file_upload: upload_file_txn.clone(),
1963                    thumbnail_info,
1964                    extra_content,
1965                },
1966            )
1967            .await?;
1968
1969        self.thumbnail_file_sizes.lock().insert(send_event_txn, thumbnail_file_sizes);
1970
1971        Ok(())
1972    }
1973
1974    /// Push requests (and dependents) to upload a gallery.
1975    ///
1976    /// See the module-level description for details of the whole processus.
1977    #[cfg(feature = "unstable-msc4274")]
1978    #[allow(clippy::too_many_arguments)]
1979    async fn push_gallery(
1980        &self,
1981        event: RoomMessageEventContent,
1982        send_event_txn: OwnedTransactionId,
1983        created_at: MilliSecondsSinceUnixEpoch,
1984        item_queue_infos: Vec<GalleryItemQueueInfo>,
1985        extra_content: Option<serde_json::Map<String, serde_json::Value>>,
1986    ) -> Result<(), RoomSendQueueStorageError> {
1987        let guard = self.store.lock().await;
1988        let client = guard.client()?;
1989        let store = client.state_store();
1990
1991        let mut finish_item_infos = Vec::with_capacity(item_queue_infos.len());
1992        let mut thumbnail_file_sizes = Vec::with_capacity(item_queue_infos.len());
1993
1994        let Some((first, rest)) = item_queue_infos.split_first() else {
1995            return Ok(());
1996        };
1997
1998        let GalleryItemQueueInfo {
1999            content_type,
2000            upload_file_txn,
2001            file_media_request,
2002            thumbnail,
2003            extra_content: item_extra_content,
2004        } = first;
2005
2006        let thumbnail_info = self
2007            .push_thumbnail_and_media_uploads(
2008                store,
2009                content_type,
2010                send_event_txn.clone(),
2011                created_at,
2012                upload_file_txn.clone(),
2013                file_media_request.clone(),
2014                thumbnail.clone(),
2015            )
2016            .await?;
2017
2018        finish_item_infos.push(FinishGalleryItemInfo {
2019            file_upload: upload_file_txn.clone(),
2020            thumbnail_info,
2021            extra_content: item_extra_content.clone(),
2022        });
2023        thumbnail_file_sizes.push(thumbnail.as_ref().map(|t| t.file_size));
2024
2025        let mut last_upload_file_txn = upload_file_txn.clone();
2026
2027        for item_queue_info in rest {
2028            let GalleryItemQueueInfo {
2029                content_type,
2030                upload_file_txn,
2031                file_media_request,
2032                thumbnail,
2033                extra_content: item_extra_content,
2034            } = item_queue_info;
2035
2036            let thumbnail_info = if let Some(QueueThumbnailInfo {
2037                finish_upload_thumbnail_info: thumbnail_info,
2038                media_request_parameters: thumbnail_media_request,
2039                content_type: thumbnail_content_type,
2040                ..
2041            }) = thumbnail
2042            {
2043                let upload_thumbnail_txn = thumbnail_info.txn.clone();
2044
2045                // Save the thumbnail upload request as a dependent request of
2046                // the last file upload.
2047                store
2048                    .save_dependent_queued_request(
2049                        &self.room_id,
2050                        &last_upload_file_txn,
2051                        upload_thumbnail_txn.clone().into(),
2052                        created_at,
2053                        DependentQueuedRequestKind::UploadFileOrThumbnail {
2054                            content_type: thumbnail_content_type.to_string(),
2055                            cache_key: thumbnail_media_request.clone(),
2056                            related_to: send_event_txn.clone(),
2057                            parent_is_thumbnail_upload: false,
2058                        },
2059                    )
2060                    .await?;
2061
2062                last_upload_file_txn = upload_thumbnail_txn;
2063
2064                Some(thumbnail_info)
2065            } else {
2066                None
2067            };
2068
2069            // Save the file upload as a dependent request of the previous
2070            // upload.
2071            store
2072                .save_dependent_queued_request(
2073                    &self.room_id,
2074                    &last_upload_file_txn,
2075                    upload_file_txn.clone().into(),
2076                    created_at,
2077                    DependentQueuedRequestKind::UploadFileOrThumbnail {
2078                        content_type: content_type.to_string(),
2079                        cache_key: file_media_request.clone(),
2080                        related_to: send_event_txn.clone(),
2081                        parent_is_thumbnail_upload: thumbnail.is_some(),
2082                    },
2083                )
2084                .await?;
2085
2086            finish_item_infos.push(FinishGalleryItemInfo {
2087                file_upload: upload_file_txn.clone(),
2088                thumbnail_info: thumbnail_info.cloned(),
2089                extra_content: item_extra_content.clone(),
2090            });
2091            thumbnail_file_sizes.push(thumbnail.as_ref().map(|t| t.file_size));
2092
2093            last_upload_file_txn = upload_file_txn.clone();
2094        }
2095
2096        // Push the request for the event itself as a dependent request of the
2097        // last file upload.
2098        store
2099            .save_dependent_queued_request(
2100                &self.room_id,
2101                &last_upload_file_txn,
2102                send_event_txn.clone().into(),
2103                created_at,
2104                DependentQueuedRequestKind::FinishGallery {
2105                    local_echo: Box::new(event),
2106                    item_infos: finish_item_infos,
2107                    extra_content,
2108                },
2109            )
2110            .await?;
2111
2112        self.thumbnail_file_sizes.lock().insert(send_event_txn, thumbnail_file_sizes);
2113
2114        Ok(())
2115    }
2116
2117    /// If a thumbnail exists, pushes a [`QueuedRequestKind::MediaUpload`] to
2118    /// upload it and a [`DependentQueuedRequestKind::UploadFileOrThumbnail`] to
2119    /// upload the media itself. Otherwise, pushes a
2120    /// [`QueuedRequestKind::MediaUpload`] to upload the media directly.
2121    #[allow(clippy::too_many_arguments)]
2122    async fn push_thumbnail_and_media_uploads(
2123        &self,
2124        store: &DynStateStore,
2125        content_type: &Mime,
2126        send_event_txn: OwnedTransactionId,
2127        created_at: MilliSecondsSinceUnixEpoch,
2128        upload_file_txn: OwnedTransactionId,
2129        file_media_request: MediaRequestParameters,
2130        thumbnail: Option<QueueThumbnailInfo>,
2131    ) -> Result<Option<FinishUploadThumbnailInfo>, RoomSendQueueStorageError> {
2132        if let Some(QueueThumbnailInfo {
2133            finish_upload_thumbnail_info: thumbnail_info,
2134            media_request_parameters: thumbnail_media_request,
2135            content_type: thumbnail_content_type,
2136            ..
2137        }) = thumbnail
2138        {
2139            let upload_thumbnail_txn = thumbnail_info.txn.clone();
2140
2141            // Save the thumbnail upload request.
2142            store
2143                .save_send_queue_request(
2144                    &self.room_id,
2145                    upload_thumbnail_txn.clone(),
2146                    created_at,
2147                    QueuedRequestKind::MediaUpload {
2148                        content_type: thumbnail_content_type.to_string(),
2149                        cache_key: thumbnail_media_request,
2150                        thumbnail_source: None, // the thumbnail has no thumbnails :)
2151                        related_to: send_event_txn.clone(),
2152                        #[cfg(feature = "unstable-msc4274")]
2153                        accumulated: vec![],
2154                    },
2155                    Self::LOW_PRIORITY,
2156                )
2157                .await?;
2158
2159            // Save the file upload request as a dependent request of the
2160            // thumbnail upload.
2161            store
2162                .save_dependent_queued_request(
2163                    &self.room_id,
2164                    &upload_thumbnail_txn,
2165                    upload_file_txn.into(),
2166                    created_at,
2167                    DependentQueuedRequestKind::UploadFileOrThumbnail {
2168                        content_type: content_type.to_string(),
2169                        cache_key: file_media_request,
2170                        related_to: send_event_txn,
2171                        parent_is_thumbnail_upload: true,
2172                    },
2173                )
2174                .await?;
2175
2176            Ok(Some(thumbnail_info))
2177        } else {
2178            // Save the file upload as its own request, not a dependent one.
2179            store
2180                .save_send_queue_request(
2181                    &self.room_id,
2182                    upload_file_txn,
2183                    created_at,
2184                    QueuedRequestKind::MediaUpload {
2185                        content_type: content_type.to_string(),
2186                        cache_key: file_media_request,
2187                        thumbnail_source: None,
2188                        related_to: send_event_txn,
2189                        #[cfg(feature = "unstable-msc4274")]
2190                        accumulated: vec![],
2191                    },
2192                    Self::LOW_PRIORITY,
2193                )
2194                .await?;
2195
2196            Ok(None)
2197        }
2198    }
2199
2200    /// Reacts to the given local echo of an event.
2201    #[instrument(skip(self))]
2202    async fn react(
2203        &self,
2204        transaction_id: &TransactionId,
2205        key: String,
2206        created_at: MilliSecondsSinceUnixEpoch,
2207    ) -> Result<Option<ChildTransactionId>, RoomSendQueueStorageError> {
2208        let guard = self.store.lock().await;
2209        let client = guard.client()?;
2210        let store = client.state_store();
2211
2212        let requests = store.load_send_queue_requests(&self.room_id).await?;
2213
2214        // If the target event has been already sent, or isn't something that
2215        // can be reacted to in the first place, abort immediately.
2216        if !requests
2217            .iter()
2218            .any(|item| item.transaction_id == transaction_id && is_own_event_request(item))
2219        {
2220            // We didn't find it as a queued request; try to find it as a
2221            // dependent queued request.
2222            let dependent_requests = store.load_dependent_queued_requests(&self.room_id).await?;
2223            if !dependent_requests
2224                .into_iter()
2225                .filter_map(|item| item.is_own_event().then_some(item.own_transaction_id))
2226                .any(|child_txn| *child_txn == *transaction_id)
2227            {
2228                // We didn't find it as either a request or a dependent request,
2229                // abort.
2230                return Ok(None);
2231            }
2232        }
2233
2234        // Record the dependent request.
2235        let reaction_txn_id = ChildTransactionId::new();
2236        store
2237            .save_dependent_queued_request(
2238                &self.room_id,
2239                transaction_id,
2240                reaction_txn_id.clone(),
2241                created_at,
2242                DependentQueuedRequestKind::ReactEvent { key },
2243            )
2244            .await?;
2245
2246        Ok(Some(reaction_txn_id))
2247    }
2248
2249    /// Returns a list of the local echoes, that is, all the requests that we're
2250    /// about to send but that haven't been sent yet (or are being sent).
2251    async fn local_echoes(
2252        &self,
2253        room: &RoomSendQueue,
2254    ) -> Result<Vec<LocalEcho>, RoomSendQueueStorageError> {
2255        let guard = self.store.lock().await;
2256        let client = guard.client()?;
2257        let store = client.state_store();
2258
2259        let queued_requests = store.load_send_queue_requests(&self.room_id).await?;
2260
2261        // Media upload requests aren't returned as echoes themselves (the media
2262        // event, represented as a dependent request, is), so carry their send
2263        // errors over to the dependent request's echo: a wedged upload wedges
2264        // the media event.
2265        let mut media_upload_errors: HashMap<OwnedTransactionId, QueueWedgeError> = queued_requests
2266            .iter()
2267            .filter_map(|queued| match queued.kind {
2268                QueuedRequestKind::MediaUpload { .. } => {
2269                    queued.error.clone().map(|error| (queued.transaction_id.clone(), error))
2270                }
2271                _ => None,
2272            })
2273            .collect();
2274
2275        let local_requests = queued_requests.into_iter().filter_map(|queued| {
2276            Some(LocalEcho {
2277                transaction_id: queued.transaction_id.clone(),
2278                content: match queued.kind {
2279                    QueuedRequestKind::Event { content, .. } => LocalEchoContent::Event {
2280                        serialized_event: content,
2281                        send_handle: SendHandle {
2282                            room: room.clone(),
2283                            transaction_id: queued.transaction_id,
2284                            media_handles: vec![],
2285                            created_at: queued.created_at,
2286                        },
2287                        send_error: queued.error,
2288                    },
2289
2290                    QueuedRequestKind::MediaUpload { .. } => {
2291                        // Don't return uploaded medias as their own things; the
2292                        // accompanying event represented as a dependent request
2293                        // should be sufficient.
2294                        return None;
2295                    }
2296
2297                    QueuedRequestKind::Redaction { redacts, reason } => {
2298                        LocalEchoContent::Redaction {
2299                            redacts,
2300                            reason,
2301                            send_handle: SendHandle {
2302                                room: room.clone(),
2303                                transaction_id: queued.transaction_id,
2304                                media_handles: vec![],
2305                                created_at: queued.created_at,
2306                            },
2307                            send_error: queued.error,
2308                        }
2309                    }
2310                },
2311            })
2312        });
2313
2314        let dependent_requests = store.load_dependent_queued_requests(&self.room_id).await?;
2315
2316        let reactions_and_medias =
2317            dependent_requests.into_iter().filter_map(|dep| match dep.kind {
2318                DependentQueuedRequestKind::EditEvent { .. }
2319                | DependentQueuedRequestKind::RedactEventWithReason { .. } => {
2320                    // TODO: reflect local edits/redacts too?
2321                    None
2322                }
2323
2324                DependentQueuedRequestKind::ReactEvent { key } => Some(LocalEcho {
2325                    transaction_id: dep.own_transaction_id.clone().into(),
2326                    content: LocalEchoContent::React {
2327                        key,
2328                        send_handle: SendHandle {
2329                            room: room.clone(),
2330                            transaction_id: dep.own_transaction_id.into(),
2331                            media_handles: vec![],
2332                            created_at: dep.created_at,
2333                        },
2334                        applies_to: dep.parent_transaction_id,
2335                    },
2336                }),
2337
2338                DependentQueuedRequestKind::UploadFileOrThumbnail { .. } => {
2339                    // Don't reflect these: only the associated event is
2340                    // interesting to observers.
2341                    None
2342                }
2343
2344                DependentQueuedRequestKind::FinishUpload {
2345                    local_echo,
2346                    file_upload,
2347                    thumbnail_info,
2348                    extra_content,
2349                } => {
2350                    let upload_thumbnail_txn = thumbnail_info.map(|info| info.txn);
2351
2352                    // If one of the uploads wedged, the media event is wedged
2353                    // too.
2354                    let send_error = media_upload_errors.remove(&file_upload).or_else(|| {
2355                        upload_thumbnail_txn
2356                            .as_ref()
2357                            .and_then(|txn| media_upload_errors.remove(&**txn))
2358                    });
2359
2360                    // Materialize as an event local echo.
2361                    Some(LocalEcho {
2362                        transaction_id: dep.own_transaction_id.clone().into(),
2363                        content: LocalEchoContent::Event {
2364                            serialized_event: upload::merge_extra_content(
2365                                SerializableEventContent::new(&(*local_echo).into()).ok()?,
2366                                extra_content,
2367                            )
2368                            .ok()?,
2369                            send_handle: SendHandle {
2370                                room: room.clone(),
2371                                transaction_id: dep.own_transaction_id.into(),
2372                                media_handles: vec![MediaHandles {
2373                                    upload_thumbnail_txn,
2374                                    upload_file_txn: file_upload,
2375                                }],
2376                                created_at: dep.created_at,
2377                            },
2378                            send_error,
2379                        },
2380                    })
2381                }
2382
2383                #[cfg(feature = "unstable-msc4274")]
2384                DependentQueuedRequestKind::FinishGallery {
2385                    local_echo,
2386                    item_infos,
2387                    extra_content,
2388                } => {
2389                    // Materialize as an event local echo.
2390                    self.create_gallery_local_echo(
2391                        dep.own_transaction_id,
2392                        room,
2393                        dep.created_at,
2394                        local_echo,
2395                        item_infos,
2396                        extra_content,
2397                        &mut media_upload_errors,
2398                    )
2399                }
2400            });
2401
2402        Ok(local_requests.chain(reactions_and_medias).collect())
2403    }
2404
2405    /// Create a local echo for a gallery event.
2406    #[cfg(feature = "unstable-msc4274")]
2407    #[allow(clippy::too_many_arguments)]
2408    fn create_gallery_local_echo(
2409        &self,
2410        transaction_id: ChildTransactionId,
2411        room: &RoomSendQueue,
2412        created_at: MilliSecondsSinceUnixEpoch,
2413        local_echo: Box<RoomMessageEventContent>,
2414        item_infos: Vec<FinishGalleryItemInfo>,
2415        extra_content: Option<serde_json::Map<String, serde_json::Value>>,
2416        media_upload_errors: &mut HashMap<OwnedTransactionId, QueueWedgeError>,
2417    ) -> Option<LocalEcho> {
2418        // If any of the uploads wedged, the gallery event is wedged too.
2419        let send_error = item_infos.iter().find_map(|i| {
2420            media_upload_errors.remove(&i.file_upload).or_else(|| {
2421                i.thumbnail_info.as_ref().and_then(|info| media_upload_errors.remove(&*info.txn))
2422            })
2423        });
2424
2425        Some(LocalEcho {
2426            transaction_id: transaction_id.clone().into(),
2427            content: LocalEchoContent::Event {
2428                serialized_event: upload::merge_gallery_extra_content(
2429                    SerializableEventContent::new(&(*local_echo).into()).ok()?,
2430                    extra_content,
2431                    item_infos.iter().map(|item| item.extra_content.clone()),
2432                )
2433                .ok()?,
2434                send_handle: SendHandle {
2435                    room: room.clone(),
2436                    transaction_id: transaction_id.into(),
2437                    media_handles: item_infos
2438                        .into_iter()
2439                        .map(|i| MediaHandles {
2440                            upload_thumbnail_txn: i.thumbnail_info.map(|info| info.txn),
2441                            upload_file_txn: i.file_upload,
2442                        })
2443                        .collect(),
2444                    created_at,
2445                },
2446                send_error,
2447            },
2448        })
2449    }
2450
2451    /// Try to apply a single dependent request, whether it's local or remote.
2452    ///
2453    /// This swallows errors that would retrigger every time if we retried
2454    /// applying the dependent request: invalid edit content, etc.
2455    ///
2456    /// Returns true if the dependent request has been sent (or should not be
2457    /// retried later).
2458    #[instrument(skip_all)]
2459    async fn try_apply_single_dependent_request(
2460        &self,
2461        client: &Client,
2462        dependent_request: DependentQueuedRequest,
2463        new_updates: &mut Vec<RoomSendQueueUpdate>,
2464    ) -> Result<bool, RoomSendQueueError> {
2465        let store = client.state_store();
2466
2467        let parent_key = dependent_request.parent_key;
2468
2469        match dependent_request.kind {
2470            DependentQueuedRequestKind::EditEvent { new_content } => {
2471                if let Some(parent_key) = parent_key {
2472                    let Some(event_id) = parent_key.into_event_id() else {
2473                        return Err(RoomSendQueueError::StorageError(
2474                            RoomSendQueueStorageError::InvalidParentKey,
2475                        ));
2476                    };
2477
2478                    // The parent event has been sent, so send an edit event.
2479                    let room = client
2480                        .get_room(&self.room_id)
2481                        .ok_or(RoomSendQueueError::RoomDisappeared)?;
2482
2483                    // Check the event is one we know how to edit with an edit
2484                    // event.
2485
2486                    // Fields the typed content doesn't know about, like an
2487                    // attachment's extra content, to put back in the edit.
2488                    let mut unknown_fields = None;
2489
2490                    // It must be deserializable…
2491                    let edited_content = match new_content.deserialize() {
2492                        Ok(AnyMessageLikeEventContent::RoomMessage(c)) => {
2493                            unknown_fields = upload::unknown_fields(&new_content, &c);
2494
2495                            // Assume no relationships.
2496                            EditedContent::RoomMessage(c.into())
2497                        }
2498
2499                        Ok(AnyMessageLikeEventContent::UnstablePollStart(c)) => {
2500                            let poll_start = c.poll_start().clone();
2501                            EditedContent::PollStart {
2502                                fallback_text: poll_start.question.text.clone(),
2503                                new_content: poll_start,
2504                            }
2505                        }
2506
2507                        Ok(c) => {
2508                            warn!("Unsupported edit content type: {:?}", c.event_type());
2509                            return Ok(true);
2510                        }
2511
2512                        Err(err) => {
2513                            warn!("Unable to deserialize: {err}");
2514                            return Ok(true);
2515                        }
2516                    };
2517
2518                    let edit_event = match room.make_edit_event(&event_id, edited_content).await {
2519                        Ok(e) => e,
2520                        Err(err) => {
2521                            warn!("couldn't create edited event: {err}");
2522                            return Ok(true);
2523                        }
2524                    };
2525
2526                    let mut edit_json = serde_json::to_value(&edit_event)
2527                        .map_err(RoomSendQueueStorageError::JsonSerialization)?;
2528
2529                    // Only in the new content: the top level is what push rules
2530                    // see, and an edit mustn't notify again.
2531                    if let Some(serde_json::Value::Object(mut unknown)) = unknown_fields
2532                        && let Some(edited) = edit_json.get_mut("m.new_content")
2533                    {
2534                        // The edit has its own relation and new content.
2535                        unknown.remove("m.relates_to");
2536                        unknown.remove("m.new_content");
2537                        upload::restore_unknown_fields(edited, unknown.into());
2538                    }
2539
2540                    // Queue the edit event in the send queue 🧠.
2541                    let serializable = SerializableEventContent::from_raw(
2542                        Raw::from_json(
2543                            serde_json::value::to_raw_value(&edit_json)
2544                                .map_err(RoomSendQueueStorageError::JsonSerialization)?,
2545                        ),
2546                        edit_event.event_type().to_string(),
2547                    );
2548
2549                    store
2550                        .save_send_queue_request(
2551                            &self.room_id,
2552                            dependent_request.own_transaction_id.into(),
2553                            dependent_request.created_at,
2554                            serializable.into(),
2555                            Self::HIGH_PRIORITY,
2556                        )
2557                        .await
2558                        .map_err(RoomSendQueueStorageError::StateStoreError)?;
2559                } else {
2560                    // The parent event is still local; update the local echo.
2561                    let parent_transaction_id = &dependent_request.parent_transaction_id;
2562
2563                    let request = QueuedRequestKind::Event {
2564                        content: new_content,
2565                        #[cfg(feature = "unstable-msc4354")]
2566                        sticky_duration: self
2567                            .sticky_duration_of(store, parent_transaction_id)
2568                            .await?,
2569                    };
2570
2571                    let edited = store
2572                        .update_send_queue_request(&self.room_id, parent_transaction_id, request)
2573                        .await
2574                        .map_err(RoomSendQueueStorageError::StateStoreError)?;
2575
2576                    if !edited {
2577                        warn!("missing local echo upon dependent edit");
2578                    }
2579                }
2580            }
2581
2582            DependentQueuedRequestKind::RedactEventWithReason { reason } => {
2583                if let Some(parent_key) = parent_key {
2584                    let Some(event_id) = parent_key.into_event_id() else {
2585                        return Err(RoomSendQueueError::StorageError(
2586                            RoomSendQueueStorageError::InvalidParentKey,
2587                        ));
2588                    };
2589
2590                    // The parent event has been sent; send a redaction.
2591                    let room = client
2592                        .get_room(&self.room_id)
2593                        .ok_or(RoomSendQueueError::RoomDisappeared)?;
2594
2595                    // Ideally we'd use the send queue to send the redaction,
2596                    // but the protocol has changed the shape of a
2597                    // room.redaction after v11, so keep it simple and try once
2598                    // here.
2599
2600                    if let Err(err) = room
2601                        .redact(
2602                            &event_id,
2603                            reason.as_deref(),
2604                            Some(dependent_request.own_transaction_id.into()),
2605                        )
2606                        .await
2607                    {
2608                        warn!("error when sending a redact for {event_id}: {err}");
2609                        return Ok(false);
2610                    }
2611                } else {
2612                    // The parent event is still local (sending must have
2613                    // failed); redact the local echo.
2614                    let removed = store
2615                        .remove_send_queue_request(
2616                            &self.room_id,
2617                            &dependent_request.parent_transaction_id,
2618                        )
2619                        .await
2620                        .map_err(RoomSendQueueStorageError::StateStoreError)?;
2621
2622                    if !removed {
2623                        warn!("missing local echo upon dependent redact");
2624                    }
2625                }
2626            }
2627
2628            DependentQueuedRequestKind::ReactEvent { key } => {
2629                if let Some(parent_key) = parent_key {
2630                    let Some(parent_event_id) = parent_key.into_event_id() else {
2631                        return Err(RoomSendQueueError::StorageError(
2632                            RoomSendQueueStorageError::InvalidParentKey,
2633                        ));
2634                    };
2635
2636                    // Queue the reaction event in the send queue 🧠.
2637                    let react_event =
2638                        ReactionEventContent::new(Annotation::new(parent_event_id, key)).into();
2639                    let serializable = SerializableEventContent::from_raw(
2640                        Raw::new(&react_event)
2641                            .map_err(RoomSendQueueStorageError::JsonSerialization)?,
2642                        react_event.event_type().to_string(),
2643                    );
2644
2645                    store
2646                        .save_send_queue_request(
2647                            &self.room_id,
2648                            dependent_request.own_transaction_id.into(),
2649                            dependent_request.created_at,
2650                            serializable.into(),
2651                            Self::HIGH_PRIORITY,
2652                        )
2653                        .await
2654                        .map_err(RoomSendQueueStorageError::StateStoreError)?;
2655                } else {
2656                    // Not applied yet, we should retry later => false.
2657                    return Ok(false);
2658                }
2659            }
2660
2661            DependentQueuedRequestKind::UploadFileOrThumbnail {
2662                content_type,
2663                cache_key,
2664                related_to,
2665                parent_is_thumbnail_upload,
2666            } => {
2667                let Some(parent_key) = parent_key else {
2668                    // Not finished yet, we should retry later => false.
2669                    return Ok(false);
2670                };
2671                self.handle_dependent_file_or_thumbnail_upload(
2672                    client,
2673                    dependent_request.own_transaction_id.into(),
2674                    parent_key,
2675                    content_type,
2676                    cache_key,
2677                    related_to,
2678                    parent_is_thumbnail_upload,
2679                )
2680                .await?;
2681            }
2682
2683            DependentQueuedRequestKind::FinishUpload {
2684                local_echo,
2685                file_upload,
2686                thumbnail_info,
2687                extra_content,
2688            } => {
2689                let Some(parent_key) = parent_key else {
2690                    // Not finished yet, we should retry later => false.
2691                    return Ok(false);
2692                };
2693                self.handle_dependent_finish_upload(
2694                    client,
2695                    dependent_request.own_transaction_id.into(),
2696                    parent_key,
2697                    *local_echo,
2698                    file_upload,
2699                    thumbnail_info,
2700                    extra_content,
2701                    new_updates,
2702                )
2703                .await?;
2704            }
2705
2706            #[cfg(feature = "unstable-msc4274")]
2707            DependentQueuedRequestKind::FinishGallery { local_echo, item_infos, extra_content } => {
2708                let Some(parent_key) = parent_key else {
2709                    // Not finished yet, we should retry later => false.
2710                    return Ok(false);
2711                };
2712                self.handle_dependent_finish_gallery_upload(
2713                    client,
2714                    dependent_request.own_transaction_id.into(),
2715                    parent_key,
2716                    *local_echo,
2717                    item_infos,
2718                    extra_content,
2719                    new_updates,
2720                )
2721                .await?;
2722            }
2723        }
2724
2725        Ok(true)
2726    }
2727
2728    #[instrument(skip(self))]
2729    async fn apply_dependent_requests(
2730        &self,
2731        new_updates: &mut Vec<RoomSendQueueUpdate>,
2732    ) -> Result<(), RoomSendQueueError> {
2733        let guard = self.store.lock().await;
2734
2735        let client = guard.client()?;
2736        let store = client.state_store();
2737
2738        let dependent_requests = store
2739            .load_dependent_queued_requests(&self.room_id)
2740            .await
2741            .map_err(RoomSendQueueStorageError::StateStoreError)?;
2742
2743        let num_initial_dependent_requests = dependent_requests.len();
2744        if num_initial_dependent_requests == 0 {
2745            // Returning early here avoids a bit of useless logging.
2746            return Ok(());
2747        }
2748
2749        let canonicalized_dependent_requests = canonicalize_dependent_requests(&dependent_requests);
2750
2751        // Get rid of the all non-canonical dependent events.
2752        for original in &dependent_requests {
2753            if !canonicalized_dependent_requests
2754                .iter()
2755                .any(|canonical| canonical.own_transaction_id == original.own_transaction_id)
2756            {
2757                store
2758                    .remove_dependent_queued_request(&self.room_id, &original.own_transaction_id)
2759                    .await
2760                    .map_err(RoomSendQueueStorageError::StateStoreError)?;
2761            }
2762        }
2763
2764        let mut num_dependent_requests = canonicalized_dependent_requests.len();
2765
2766        debug!(
2767            num_dependent_requests,
2768            num_initial_dependent_requests, "starting handling of dependent requests"
2769        );
2770
2771        for dependent in canonicalized_dependent_requests {
2772            let dependent_id = dependent.own_transaction_id.clone();
2773
2774            match self.try_apply_single_dependent_request(&client, dependent, new_updates).await {
2775                Ok(should_remove) => {
2776                    if should_remove {
2777                        // The dependent request has been successfully applied,
2778                        // forget about it.
2779                        store
2780                            .remove_dependent_queued_request(&self.room_id, &dependent_id)
2781                            .await
2782                            .map_err(RoomSendQueueStorageError::StateStoreError)?;
2783
2784                        num_dependent_requests -= 1;
2785                    }
2786                }
2787
2788                Err(err) => {
2789                    warn!("error when applying single dependent request: {err}");
2790                }
2791            }
2792        }
2793
2794        debug!(
2795            leftover_dependent_requests = num_dependent_requests,
2796            "stopped handling dependent request"
2797        );
2798
2799        Ok(())
2800    }
2801
2802    /// Remove a single dependent request from storage.
2803    async fn remove_dependent_send_queue_request(
2804        &self,
2805        dependent_event_id: &ChildTransactionId,
2806    ) -> Result<bool, RoomSendQueueStorageError> {
2807        Ok(self
2808            .store
2809            .lock()
2810            .await
2811            .client()?
2812            .state_store()
2813            .remove_dependent_queued_request(&self.room_id, dependent_event_id)
2814            .await?)
2815    }
2816}
2817
2818#[cfg(feature = "unstable-msc4274")]
2819/// Metadata needed for pushing gallery item uploads onto the send queue.
2820struct GalleryItemQueueInfo {
2821    content_type: Mime,
2822    upload_file_txn: OwnedTransactionId,
2823    file_media_request: MediaRequestParameters,
2824    thumbnail: Option<QueueThumbnailInfo>,
2825    extra_content: Option<serde_json::Map<String, serde_json::Value>>,
2826}
2827
2828/// The content of a local echo.
2829#[derive(Clone, Debug)]
2830pub enum LocalEchoContent {
2831    /// The local echo contains an actual event ready to display.
2832    Event {
2833        /// Content of the event itself (along with its type) that we are about
2834        /// to send.
2835        serialized_event: SerializableEventContent,
2836        /// A handle to manipulate the sending of the associated event.
2837        send_handle: SendHandle,
2838        /// Whether trying to send this local echo failed in the past with an
2839        /// unrecoverable error (see [`SendQueueRoomError::is_recoverable`]).
2840        send_error: Option<QueueWedgeError>,
2841    },
2842
2843    /// A local echo has been reacted to.
2844    React {
2845        /// The key with which the local echo has been reacted to.
2846        key: String,
2847        /// A handle to manipulate the sending of the reaction.
2848        send_handle: SendHandle,
2849        /// The local echo which has been reacted to.
2850        applies_to: OwnedTransactionId,
2851    },
2852
2853    /// A local echo of a redaction event.
2854    Redaction {
2855        /// The ID of the redacted event.
2856        redacts: OwnedEventId,
2857        /// The reason for the event being redacted.
2858        reason: Option<String>,
2859        /// A handle to manipulate the sending of the associated event.
2860        send_handle: SendHandle,
2861        /// Whether trying to send this local echo failed in the past with an
2862        /// unrecoverable error (see [`SendQueueRoomError::is_recoverable`]).
2863        send_error: Option<QueueWedgeError>,
2864    },
2865}
2866
2867/// A local representation for a request that hasn't been sent yet to the user's
2868/// homeserver.
2869#[derive(Clone, Debug)]
2870pub struct LocalEcho {
2871    /// Transaction id used to identify the associated request.
2872    pub transaction_id: OwnedTransactionId,
2873    /// The content for the local echo.
2874    pub content: LocalEchoContent,
2875}
2876
2877/// An update to a room send queue, observable with
2878/// [`RoomSendQueue::subscribe`].
2879#[derive(Clone, Debug)]
2880pub enum RoomSendQueueUpdate {
2881    /// A new local event is being sent.
2882    ///
2883    /// There's been a user query to create this event. It is being sent to the
2884    /// server.
2885    NewLocalEvent(LocalEcho),
2886
2887    /// A local event that hadn't been sent to the server yet has been cancelled
2888    /// before sending.
2889    CancelledLocalEvent {
2890        /// Transaction id used to identify this event.
2891        transaction_id: OwnedTransactionId,
2892    },
2893
2894    /// A local event's content has been replaced with something else.
2895    ReplacedLocalEvent {
2896        /// Transaction id used to identify this event.
2897        transaction_id: OwnedTransactionId,
2898
2899        /// The new content replacing the previous one.
2900        new_content: SerializableEventContent,
2901    },
2902
2903    /// An error happened when an event was being sent.
2904    ///
2905    /// The event has not been removed from the queue. A recoverable error
2906    /// disables the room's send queue, which must then be manually re-enabled;
2907    /// an unrecoverable one wedges the request, which blocks its room's queue
2908    /// until the request is unwedged or aborted.
2909    SendError {
2910        /// Transaction id used to identify this event.
2911        transaction_id: OwnedTransactionId,
2912        /// Error received while sending the event.
2913        error: Arc<crate::Error>,
2914        /// Whether the error is considered recoverable or not.
2915        ///
2916        /// An error that's recoverable will disable the room's send queue,
2917        /// while an unrecoverable error will be parked, until it's retried or
2918        /// aborted.
2919        is_recoverable: bool,
2920    },
2921
2922    /// The event has been unwedged and sending is now being retried.
2923    RetryEvent {
2924        /// Transaction id used to identify this event.
2925        transaction_id: OwnedTransactionId,
2926    },
2927
2928    /// The event has been sent to the server, and the query returned
2929    /// successfully.
2930    SentEvent {
2931        /// Transaction id used to identify this event.
2932        transaction_id: OwnedTransactionId,
2933        /// Received event id from the send response.
2934        event_id: OwnedEventId,
2935    },
2936
2937    /// A media upload (consisting of a file and possibly a thumbnail) has made
2938    /// progress.
2939    MediaUpload {
2940        /// The media event this uploaded media relates to.
2941        related_to: OwnedTransactionId,
2942
2943        /// The final media source for the file if it has finished uploading.
2944        file: Option<MediaSource>,
2945
2946        /// The index of the media within the transaction. A file and its
2947        /// thumbnail share the same index. Will always be 0 for non-gallery
2948        /// media uploads.
2949        index: u64,
2950
2951        /// The combined upload progress across the file and, if existing, its
2952        /// thumbnail. For gallery uploads, the progress is reported per indexed
2953        /// gallery item.
2954        progress: AbstractProgress,
2955    },
2956}
2957
2958/// A [`RoomSendQueueUpdate`] with an associated [`OwnedRoomId`].
2959///
2960/// This is used by [`SendQueue::subscribe`] to get a single channel to receive
2961/// updates for all [`RoomSendQueue`]s.
2962#[derive(Clone, Debug)]
2963pub struct SendQueueUpdate {
2964    /// The room where the update happened.
2965    pub room_id: OwnedRoomId,
2966
2967    /// The update for this room.
2968    pub update: RoomSendQueueUpdate,
2969}
2970
2971/// An error triggered by the send queue module.
2972#[derive(Debug, thiserror::Error)]
2973pub enum RoomSendQueueError {
2974    /// The room isn't in the joined state.
2975    #[error("the room isn't in the joined state")]
2976    RoomNotJoined,
2977
2978    /// The room is missing from the client.
2979    ///
2980    /// This happens only whenever the client is shutting down.
2981    #[error("the room is now missing from the client")]
2982    RoomDisappeared,
2983
2984    /// Error coming from storage.
2985    #[error(transparent)]
2986    StorageError(#[from] RoomSendQueueStorageError),
2987
2988    /// The attachment event failed to be created.
2989    #[error("the attachment event could not be created")]
2990    FailedToCreateAttachment,
2991
2992    /// The target of an [`RoomSendQueue::edit_with_attachment`] can't be
2993    /// edited with a new attachment.
2994    #[error(transparent)]
2995    Edit(#[from] crate::room::edit::EditError),
2996
2997    /// The gallery contains no items.
2998    #[cfg(feature = "unstable-msc4274")]
2999    #[error("the gallery contains no items")]
3000    EmptyGallery,
3001
3002    /// The gallery event failed to be created.
3003    #[cfg(feature = "unstable-msc4274")]
3004    #[error("the gallery event could not be created")]
3005    FailedToCreateGallery,
3006}
3007
3008/// An error triggered by the send queue storage.
3009#[derive(Debug, thiserror::Error)]
3010pub enum RoomSendQueueStorageError {
3011    /// Error caused by the state store.
3012    #[error(transparent)]
3013    StateStoreError(#[from] StoreError),
3014
3015    /// Error caused by the event cache store.
3016    #[error(transparent)]
3017    EventCacheStoreError(#[from] EventCacheStoreError),
3018
3019    /// Error caused by the event cache store.
3020    #[error(transparent)]
3021    MediaStoreError(#[from] MediaStoreError),
3022
3023    /// Error caused when attempting to get a handle on the event cache store.
3024    #[error(transparent)]
3025    LockError(#[from] CrossProcessLockError),
3026
3027    /// Error caused when (de)serializing into/from json.
3028    #[error(transparent)]
3029    JsonSerialization(#[from] serde_json::Error),
3030
3031    /// A parent key was expected to be of a certain type, and it was another
3032    /// type instead.
3033    #[error("a dependent event had an invalid parent key type")]
3034    InvalidParentKey,
3035
3036    /// The client is shutting down.
3037    #[error("The client is shutting down.")]
3038    ClientShuttingDown,
3039
3040    /// An operation not implemented on a send handle.
3041    #[error("This operation is not implemented for media uploads")]
3042    OperationNotImplementedYet,
3043
3044    /// Trying to edit a media caption for something that's not a media.
3045    #[error("Can't edit a media caption when the underlying event isn't a media")]
3046    InvalidMediaCaptionEdit,
3047}
3048
3049/// Extra transaction IDs useful during an upload.
3050#[derive(Clone, Debug)]
3051struct MediaHandles {
3052    /// Transaction id used when uploading the thumbnail.
3053    ///
3054    /// Optional because a media can be uploaded without a thumbnail.
3055    upload_thumbnail_txn: Option<OwnedTransactionId>,
3056
3057    /// Transaction id used when uploading the media itself.
3058    upload_file_txn: OwnedTransactionId,
3059}
3060
3061/// Future returned by [`RoomSendQueue::send`].
3062#[allow(missing_debug_implementations)]
3063pub struct SendEvent<'a> {
3064    queue: &'a RoomSendQueue,
3065    content: AnyMessageLikeEventContent,
3066    extra_content: Option<serde_json::Map<String, serde_json::Value>>,
3067    #[cfg(feature = "unstable-msc4354")]
3068    sticky_duration: Option<StickyDurationMs>,
3069}
3070
3071impl<'a> SendEvent<'a> {
3072    /// Merge additional top-level fields into the outgoing event's content.
3073    ///
3074    /// The event's own fields take precedence on conflicts.
3075    pub fn with_extra_content(
3076        mut self,
3077        extra_content: serde_json::Map<String, serde_json::Value>,
3078    ) -> Self {
3079        self.extra_content = Some(extra_content);
3080        self
3081    }
3082
3083    /// Send the event as a sticky event for `duration`, clamped to one hour.
3084    ///
3085    /// Note that if the homeserver doesn't support sticky events, it will
3086    /// ignore the duration and send the event unsticky. Server support can
3087    /// be checked with [`Client::supports_sticky_events`].
3088    #[cfg(feature = "unstable-msc4354")]
3089    pub fn with_sticky_duration(mut self, duration: Duration) -> Self {
3090        self.sticky_duration = Some(sticky_duration_ms(duration));
3091        self
3092    }
3093}
3094
3095impl<'a> IntoFuture for SendEvent<'a> {
3096    type Output = Result<SendHandle, RoomSendQueueError>;
3097    boxed_into_future!(extra_bounds: 'a);
3098
3099    fn into_future(self) -> Self::IntoFuture {
3100        Box::pin(async move {
3101            let serialized = upload::merge_extra_content(
3102                SerializableEventContent::new(&self.content)
3103                    .map_err(RoomSendQueueStorageError::JsonSerialization)?,
3104                self.extra_content,
3105            )?;
3106            self.queue
3107                .send_serialized(
3108                    serialized,
3109                    #[cfg(feature = "unstable-msc4354")]
3110                    self.sticky_duration,
3111                )
3112                .await
3113        })
3114    }
3115}
3116
3117/// Future returned by [`RoomSendQueue::send_raw`].
3118#[allow(missing_debug_implementations)]
3119pub struct SendRawEvent<'a> {
3120    queue: &'a RoomSendQueue,
3121    content: SerializableEventContent,
3122    #[cfg(feature = "unstable-msc4354")]
3123    sticky_duration: Option<StickyDurationMs>,
3124}
3125
3126impl SendRawEvent<'_> {
3127    /// Send the event as a sticky event for `duration`, clamped to one hour.
3128    ///
3129    /// Note that if the homeserver doesn't support sticky events, it will
3130    /// ignore the duration and send the event unsticky. Server support can
3131    /// be checked with [`Client::supports_sticky_events`].
3132    #[cfg(feature = "unstable-msc4354")]
3133    pub fn with_sticky_duration(mut self, duration: Duration) -> Self {
3134        self.sticky_duration = Some(sticky_duration_ms(duration));
3135        self
3136    }
3137}
3138
3139impl<'a> IntoFuture for SendRawEvent<'a> {
3140    type Output = Result<SendHandle, RoomSendQueueError>;
3141    boxed_into_future!(extra_bounds: 'a);
3142
3143    fn into_future(self) -> Self::IntoFuture {
3144        Box::pin(async move {
3145            self.queue
3146                .send_serialized(
3147                    self.content,
3148                    #[cfg(feature = "unstable-msc4354")]
3149                    self.sticky_duration,
3150                )
3151                .await
3152        })
3153    }
3154}
3155
3156/// A handle to manipulate an event that was scheduled to be sent to a room.
3157///
3158/// The event may be a room message, a media upload, a redaction or a reaction;
3159/// [`Self::edit`] and [`Self::react`] only apply to the first two, and return
3160/// `false`/`None` for the others.
3161#[derive(Clone, Debug)]
3162pub struct SendHandle {
3163    /// Link to the send queue used to send this request.
3164    room: RoomSendQueue,
3165
3166    /// Transaction id used for the sent request.
3167    ///
3168    /// If this is a media upload, this is the "main" transaction id, i.e. the
3169    /// one used to send the event, and that will be seen by observers.
3170    transaction_id: OwnedTransactionId,
3171
3172    /// Additional handles for a media upload.
3173    media_handles: Vec<MediaHandles>,
3174
3175    /// The time at which the event to be sent has been created.
3176    pub created_at: MilliSecondsSinceUnixEpoch,
3177}
3178
3179impl SendHandle {
3180    /// Creates a new [`SendHandle`].
3181    #[cfg(test)]
3182    pub(crate) fn new(
3183        room: RoomSendQueue,
3184        transaction_id: OwnedTransactionId,
3185        created_at: MilliSecondsSinceUnixEpoch,
3186    ) -> Self {
3187        Self { room, transaction_id, media_handles: vec![], created_at }
3188    }
3189
3190    /// Returns the [`TransactionId`] used for sending the associated event.
3191    pub fn transaction_id(&self) -> &TransactionId {
3192        &self.transaction_id
3193    }
3194
3195    fn nyi_for_uploads(&self) -> Result<(), RoomSendQueueStorageError> {
3196        if !self.media_handles.is_empty() {
3197            Err(RoomSendQueueStorageError::OperationNotImplementedYet)
3198        } else {
3199            Ok(())
3200        }
3201    }
3202
3203    /// Aborts the sending of the event, if it wasn't sent yet.
3204    ///
3205    /// Returns true if the sending could be aborted, false if not (i.e. the
3206    /// event had already been sent).
3207    pub async fn abort(&self) -> Result<bool, RoomSendQueueStorageError> {
3208        self.abort_with_reason(None).await
3209    }
3210
3211    /// Aborts the sending of the event, if it wasn't sent yet, with an optional
3212    /// reason.
3213    ///
3214    /// If the event was being sent when the abort was requested and the send
3215    /// succeeds, the event is redacted server-side; the given reason is applied
3216    /// to that redaction. It is unused in every other case (the local echo is
3217    /// simply dropped).
3218    ///
3219    /// Returns true if the sending could be aborted, false if not (i.e. the
3220    /// event had already been sent).
3221    #[instrument(skip(self), fields(room_id = %self.room.inner.room.room_id(), txn_id = %self.transaction_id))]
3222    pub async fn abort_with_reason(
3223        &self,
3224        reason: Option<String>,
3225    ) -> Result<bool, RoomSendQueueStorageError> {
3226        trace!("received an abort request");
3227
3228        let queue = &self.room.inner.queue;
3229
3230        for handles in &self.media_handles {
3231            if queue.abort_upload(&self.transaction_id, handles).await? {
3232                // Wake up the queue, in case it was blocked on this request
3233                // being wedged.
3234                self.room.inner.notifier.notify_one();
3235
3236                // Propagate a cancelled update.
3237                self.room.send_update(RoomSendQueueUpdate::CancelledLocalEvent {
3238                    transaction_id: self.transaction_id.clone(),
3239                });
3240
3241                return Ok(true);
3242            }
3243
3244            // If it failed, it means the sending of the event is not a
3245            // dependent request anymore. Fall back to the regular code path
3246            // below, that handles aborting sending of an event.
3247        }
3248
3249        // A reaction is queued as a dependent request of the event it applies
3250        // to, so it has no entry in the main queue as long as that
3251        // event hasn't been sent.
3252        let aborted =
3253            queue.remove_dependent_send_queue_request(&self.transaction_id.clone().into()).await?
3254                || queue.cancel_event(&self.transaction_id, reason).await?;
3255
3256        if aborted {
3257            trace!("successful abort");
3258
3259            // Wake up the queue, in case it was blocked on this request being
3260            // wedged.
3261            self.room.inner.notifier.notify_one();
3262
3263            // Propagate a cancelled update too.
3264            self.room.send_update(RoomSendQueueUpdate::CancelledLocalEvent {
3265                transaction_id: self.transaction_id.clone(),
3266            });
3267
3268            Ok(true)
3269        } else {
3270            debug!("local echo didn't exist anymore, can't abort");
3271            Ok(false)
3272        }
3273    }
3274
3275    /// Edits the content of a local echo with a raw event content.
3276    ///
3277    /// Returns true if the event to be sent was replaced, false if not (i.e.
3278    /// the event had already been sent).
3279    ///
3280    /// This method should not be used for editing sticky events. Sticky events
3281    /// are collected in an ephemeral map. Entries in that map need to be
3282    /// updated by sending a replacing sticky event with updated content,
3283    /// not by using an `m.replace` relation. Nevertheless, this method
3284    /// applies edits of an _unsent_ sticky event on the queued event
3285    /// directly because it is safe to do so. If, however, the event has
3286    /// already been sent, the edit will be sent as an unsticky event.
3287    #[instrument(skip(self, new_content), fields(room_id = %self.room.inner.room.room_id(), txn_id = %self.transaction_id))]
3288    pub async fn edit_raw(
3289        &self,
3290        new_content: Raw<AnyMessageLikeEventContent>,
3291        event_type: String,
3292    ) -> Result<bool, RoomSendQueueStorageError> {
3293        trace!("received an edit request");
3294        self.nyi_for_uploads()?;
3295
3296        let serializable = SerializableEventContent::from_raw(new_content, event_type);
3297
3298        if self.room.inner.queue.replace_event(&self.transaction_id, serializable.clone()).await? {
3299            trace!("successful edit");
3300
3301            // Wake up the queue, in case the room was asleep before the edit.
3302            self.room.inner.notifier.notify_one();
3303
3304            // Propagate a replaced update too.
3305            self.room.send_update(RoomSendQueueUpdate::ReplacedLocalEvent {
3306                transaction_id: self.transaction_id.clone(),
3307                new_content: serializable,
3308            });
3309
3310            Ok(true)
3311        } else {
3312            debug!("local echo doesn't exist anymore, can't edit");
3313            Ok(false)
3314        }
3315    }
3316
3317    /// Edits the content of a local echo with an event content.
3318    ///
3319    /// Returns true if the event to be sent was replaced, false if not (i.e.
3320    /// the event had already been sent).
3321    ///
3322    /// This method should not be used for editing sticky events. Sticky events
3323    /// are collected in an ephemeral map. Entries in that map need to be
3324    /// updated by sending a replacing sticky event with updated content,
3325    /// not by using an `m.replace` relation. Nevertheless, this method
3326    /// applies edits of an _unsent_ sticky event on the queued event
3327    /// directly because it is safe to do so. If, however, the event has
3328    /// already been sent, the edit will be sent as an unsticky event.
3329    pub async fn edit(
3330        &self,
3331        new_content: AnyMessageLikeEventContent,
3332    ) -> Result<bool, RoomSendQueueStorageError> {
3333        self.edit_raw(
3334            Raw::new(&new_content).map_err(RoomSendQueueStorageError::JsonSerialization)?,
3335            new_content.event_type().to_string(),
3336        )
3337        .await
3338    }
3339
3340    /// Edits the content of a local echo with a media caption.
3341    ///
3342    /// Will fail if the event to be sent, represented by this send handle,
3343    /// wasn't a media.
3344    pub async fn edit_media_caption(
3345        &self,
3346        caption: Option<String>,
3347        formatted_caption: Option<FormattedBody>,
3348        mentions: Option<Mentions>,
3349    ) -> Result<bool, RoomSendQueueStorageError> {
3350        if let Some(new_content) = self
3351            .room
3352            .inner
3353            .queue
3354            .edit_media_caption(&self.transaction_id, caption, formatted_caption, mentions)
3355            .await?
3356        {
3357            trace!("successful edit of media caption");
3358
3359            // Wake up the queue, in case the room was asleep before the edit.
3360            self.room.inner.notifier.notify_one();
3361
3362            // Propagate a replaced update too.
3363            self.room.send_update(RoomSendQueueUpdate::ReplacedLocalEvent {
3364                transaction_id: self.transaction_id.clone(),
3365                new_content,
3366            });
3367
3368            Ok(true)
3369        } else {
3370            debug!("local echo doesn't exist anymore, can't edit media caption");
3371            Ok(false)
3372        }
3373    }
3374
3375    /// Unwedge the local echo associated to this [`SendHandle`] and try to
3376    /// resend it.
3377    pub async fn unwedge(&self) -> Result<(), RoomSendQueueError> {
3378        let room = &self.room.inner;
3379
3380        // If we have media handles, try to unwedge them.
3381        //
3382        // It's fine to always do it to _all_ the transaction IDs at once,
3383        // because only one of the three requests will be active at the same
3384        // time, i.e. only one entry will be updated in the store. The other two
3385        // are either done, or dependent requests.
3386
3387        for handles in &self.media_handles {
3388            room.queue
3389                .mark_as_unwedged(&handles.upload_file_txn)
3390                .await
3391                .map_err(RoomSendQueueError::StorageError)?;
3392
3393            if let Some(txn) = &handles.upload_thumbnail_txn {
3394                room.queue.mark_as_unwedged(txn).await.map_err(RoomSendQueueError::StorageError)?;
3395            }
3396        }
3397
3398        self.room.unwedge_request(&self.transaction_id).await
3399    }
3400
3401    /// Send a reaction to the event as soon as it's sent.
3402    ///
3403    /// If returning `Ok(None)`; this means the reaction couldn't be sent
3404    /// because the event is already a remote one.
3405    #[instrument(skip(self), fields(room_id = %self.room.inner.room.room_id(), txn_id = %self.transaction_id))]
3406    pub async fn react(
3407        &self,
3408        key: String,
3409    ) -> Result<Option<SendHandle>, RoomSendQueueStorageError> {
3410        trace!("received an intent to react");
3411
3412        let created_at = MilliSecondsSinceUnixEpoch::now();
3413        if let Some(reaction_txn_id) =
3414            self.room.inner.queue.react(&self.transaction_id, key.clone(), created_at).await?
3415        {
3416            trace!("successfully queued react");
3417
3418            // Wake up the queue, in case the room was asleep before the
3419            // sending.
3420            self.room.inner.notifier.notify_one();
3421
3422            // Propagate a new local event.
3423            let send_handle = SendHandle {
3424                room: self.room.clone(),
3425                transaction_id: reaction_txn_id.clone().into(),
3426                media_handles: vec![],
3427                created_at,
3428            };
3429
3430            self.room.send_update(RoomSendQueueUpdate::NewLocalEvent(LocalEcho {
3431                // Note: we do want to use the `txn_id` we're going to use for
3432                // the reaction, not the one for the event we're reacting to.
3433                transaction_id: reaction_txn_id.into(),
3434                content: LocalEchoContent::React {
3435                    key,
3436                    send_handle: send_handle.clone(),
3437                    applies_to: self.transaction_id.clone(),
3438                },
3439            }));
3440
3441            Ok(Some(send_handle))
3442        } else {
3443            debug!("local echo doesn't exist anymore, can't react");
3444            Ok(None)
3445        }
3446    }
3447}
3448
3449/// Whether a queued request is an event the user composed, as opposed to a
3450/// redaction or a reaction.
3451///
3452/// A reaction is queued as a dependent request at first, but graduates into a
3453/// request of its own, under the same transaction id, once the event it
3454/// applies to has been sent; so being an event is not enough to tell the two
3455/// apart.
3456fn is_own_event_request(request: &QueuedRequest) -> bool {
3457    request.as_event().is_some_and(|content| {
3458        TimelineEventType::from(content.raw().1) != TimelineEventType::Reaction
3459    })
3460}
3461
3462/// From a given source of [`DependentQueuedRequest`], return only the most
3463/// meaningful, i.e. the ones that wouldn't be overridden after applying the
3464/// others.
3465fn canonicalize_dependent_requests(
3466    dependent: &[DependentQueuedRequest],
3467) -> Vec<DependentQueuedRequest> {
3468    let mut by_txn = HashMap::<OwnedTransactionId, Vec<&DependentQueuedRequest>>::new();
3469
3470    for d in dependent {
3471        let prevs = by_txn.entry(d.parent_transaction_id.clone()).or_default();
3472
3473        if prevs.iter().any(|prev| {
3474            matches!(prev.kind, DependentQueuedRequestKind::RedactEventWithReason { .. })
3475        }) {
3476            // The parent event has already been flagged for redaction, don't
3477            // consider the other dependent events.
3478            continue;
3479        }
3480
3481        match &d.kind {
3482            DependentQueuedRequestKind::EditEvent { .. } => {
3483                // Replace any previous edit with this one.
3484                if let Some(prev_edit) = prevs
3485                    .iter_mut()
3486                    .find(|prev| matches!(prev.kind, DependentQueuedRequestKind::EditEvent { .. }))
3487                {
3488                    *prev_edit = d;
3489                } else {
3490                    prevs.insert(0, d);
3491                }
3492            }
3493
3494            DependentQueuedRequestKind::UploadFileOrThumbnail { .. }
3495            | DependentQueuedRequestKind::FinishUpload { .. }
3496            | DependentQueuedRequestKind::ReactEvent { .. } => {
3497                // These requests can't be canonicalized, push them as is.
3498                prevs.push(d);
3499            }
3500
3501            #[cfg(feature = "unstable-msc4274")]
3502            DependentQueuedRequestKind::FinishGallery { .. } => {
3503                // This request can't be canonicalized, push it as is.
3504                prevs.push(d);
3505            }
3506
3507            DependentQueuedRequestKind::RedactEventWithReason { .. } => {
3508                // Remove every other dependent action.
3509                prevs.clear();
3510                prevs.push(d);
3511            }
3512        }
3513    }
3514
3515    by_txn.into_values().flat_map(|entries| entries.into_iter().cloned()).collect()
3516}
3517
3518fn build_timeline_event(
3519    event_id: &EventId,
3520    now: UInt,
3521    sender: &UserId,
3522    event_type: &str,
3523    content: Box<serde_json::value::RawValue>,
3524) -> serde_json::Value {
3525    serde_json::json!({
3526        "event_id": event_id,
3527        "origin_server_ts": now,
3528        "sender": sender,
3529        "type": event_type,
3530        "content": content
3531    })
3532}
3533
3534fn build_redaction_event(
3535    event_id: &EventId,
3536    now: UInt,
3537    sender: &UserId,
3538    redacts: &EventId,
3539    reason: Option<&str>,
3540    content_field_redacts: bool,
3541) -> serde_json::Value {
3542    let mut event = serde_json::json!({
3543        "event_id": event_id,
3544        "origin_server_ts": now,
3545        "sender": sender,
3546        "type": TimelineEventType::RoomRedaction,
3547        "content": {},
3548    });
3549
3550    if content_field_redacts {
3551        event["content"]["redacts"] = redacts.to_string().into();
3552    } else {
3553        event["redacts"] = redacts.to_string().into();
3554    }
3555
3556    if let Some(reason) = reason {
3557        event["content"]["reason"] = reason.into();
3558    }
3559
3560    event
3561}
3562
3563#[cfg(all(test, not(target_family = "wasm")))]
3564mod tests {
3565    use std::{assert_matches, sync::Arc, time::Duration};
3566
3567    use matrix_sdk_base::store::{
3568        ChildTransactionId, DependentQueuedRequest, DependentQueuedRequestKind,
3569        SerializableEventContent,
3570    };
3571    use matrix_sdk_test::{JoinedRoomBuilder, SyncResponseBuilder, async_test};
3572    use ruma::{
3573        MilliSecondsSinceUnixEpoch, TransactionId, UInt,
3574        api::error::UnknownTokenErrorData,
3575        event_id,
3576        events::{AnyMessageLikeEventContent, room::message::RoomMessageEventContent},
3577        room_id, user_id,
3578    };
3579    use strass::assert_let;
3580
3581    use super::{
3582        RoomSendQueueUpdate, build_redaction_event, build_timeline_event,
3583        canonicalize_dependent_requests,
3584    };
3585    use crate::{
3586        SessionChange,
3587        test_utils::{logged_in_client, mocks::MatrixMockServer},
3588    };
3589
3590    #[test]
3591    fn test_canonicalize_dependent_events_created_at() {
3592        // Test to ensure the created_at field is being serialized and retrieved
3593        // correctly.
3594        let txn = TransactionId::new();
3595        let created_at = MilliSecondsSinceUnixEpoch::now();
3596
3597        let edit = DependentQueuedRequest {
3598            own_transaction_id: ChildTransactionId::new(),
3599            parent_transaction_id: txn.clone(),
3600            kind: DependentQueuedRequestKind::EditEvent {
3601                new_content: SerializableEventContent::new(
3602                    &RoomMessageEventContent::text_plain("edit").into(),
3603                )
3604                .unwrap(),
3605            },
3606            parent_key: None,
3607            created_at,
3608        };
3609
3610        let res = canonicalize_dependent_requests(&[edit]);
3611
3612        assert_eq!(res.len(), 1);
3613        assert_let!(DependentQueuedRequestKind::EditEvent { new_content } = &res[0].kind);
3614        assert_let!(
3615            AnyMessageLikeEventContent::RoomMessage(msg) = new_content.deserialize().unwrap()
3616        );
3617        assert_eq!(msg.body(), "edit");
3618        assert_eq!(res[0].parent_transaction_id, txn);
3619        assert_eq!(res[0].created_at, created_at);
3620    }
3621
3622    #[async_test]
3623    async fn test_client_no_cycle_with_send_queue() {
3624        for enabled in [true, false] {
3625            let client = logged_in_client(None).await;
3626            let weak_client = client.downgrade();
3627
3628            {
3629                let mut sync_response_builder = SyncResponseBuilder::new();
3630
3631                let room_id = room_id!("!a:b.c");
3632
3633                // Make sure the client knows about the room.
3634                client
3635                    .base_client()
3636                    .receive_sync_response(
3637                        sync_response_builder
3638                            .add_joined_room(JoinedRoomBuilder::new(room_id))
3639                            .build_sync_response(),
3640                    )
3641                    .await
3642                    .unwrap();
3643
3644                let room = client.get_room(room_id).unwrap();
3645                let q = room.send_queue();
3646
3647                let _watcher = q.subscribe().await;
3648
3649                client.send_queue().set_enabled(enabled).await;
3650            }
3651
3652            drop(client);
3653
3654            // Give a bit of time for background tasks to die.
3655            tokio::time::sleep(Duration::from_millis(500)).await;
3656
3657            // The weak client must be the last reference to the client now.
3658            let client = weak_client.get();
3659            assert!(
3660                client.is_none(),
3661                "too many strong references to the client: {}",
3662                Arc::strong_count(&client.unwrap().inner)
3663            );
3664        }
3665    }
3666
3667    #[test]
3668    fn test_canonicalize_dependent_events_smoke_test() {
3669        // Smoke test: canonicalizing a single dependent event returns it.
3670        let txn = TransactionId::new();
3671
3672        let edit = DependentQueuedRequest {
3673            own_transaction_id: ChildTransactionId::new(),
3674            parent_transaction_id: txn.clone(),
3675            kind: DependentQueuedRequestKind::EditEvent {
3676                new_content: SerializableEventContent::new(
3677                    &RoomMessageEventContent::text_plain("edit").into(),
3678                )
3679                .unwrap(),
3680            },
3681            parent_key: None,
3682            created_at: MilliSecondsSinceUnixEpoch::now(),
3683        };
3684        let res = canonicalize_dependent_requests(&[edit]);
3685
3686        assert_eq!(res.len(), 1);
3687        assert_matches!(&res[0].kind, DependentQueuedRequestKind::EditEvent { .. });
3688        assert_eq!(res[0].parent_transaction_id, txn);
3689        assert!(res[0].parent_key.is_none());
3690    }
3691
3692    #[test]
3693    fn test_canonicalize_dependent_events_redaction_preferred() {
3694        // A redaction is preferred over any other kind of dependent event.
3695        let txn = TransactionId::new();
3696
3697        let mut inputs = Vec::with_capacity(100);
3698        let redact = DependentQueuedRequest {
3699            own_transaction_id: ChildTransactionId::new(),
3700            parent_transaction_id: txn.clone(),
3701            kind: DependentQueuedRequestKind::RedactEventWithReason { reason: None },
3702            parent_key: None,
3703            created_at: MilliSecondsSinceUnixEpoch::now(),
3704        };
3705
3706        let edit = DependentQueuedRequest {
3707            own_transaction_id: ChildTransactionId::new(),
3708            parent_transaction_id: txn.clone(),
3709            kind: DependentQueuedRequestKind::EditEvent {
3710                new_content: SerializableEventContent::new(
3711                    &RoomMessageEventContent::text_plain("edit").into(),
3712                )
3713                .unwrap(),
3714            },
3715            parent_key: None,
3716            created_at: MilliSecondsSinceUnixEpoch::now(),
3717        };
3718
3719        inputs.push({
3720            let mut edit = edit.clone();
3721            edit.own_transaction_id = ChildTransactionId::new();
3722            edit
3723        });
3724
3725        inputs.push(redact);
3726
3727        for _ in 0..98 {
3728            let mut edit = edit.clone();
3729            edit.own_transaction_id = ChildTransactionId::new();
3730            inputs.push(edit);
3731        }
3732
3733        let res = canonicalize_dependent_requests(&inputs);
3734
3735        assert_eq!(res.len(), 1);
3736        assert_matches!(&res[0].kind, DependentQueuedRequestKind::RedactEventWithReason { .. });
3737        assert_eq!(res[0].parent_transaction_id, txn);
3738    }
3739
3740    #[test]
3741    fn test_canonicalize_dependent_events_last_edit_preferred() {
3742        let parent_txn = TransactionId::new();
3743
3744        // The latest edit of a list is always preferred.
3745        let inputs = (0..10)
3746            .map(|i| DependentQueuedRequest {
3747                own_transaction_id: ChildTransactionId::new(),
3748                parent_transaction_id: parent_txn.clone(),
3749                kind: DependentQueuedRequestKind::EditEvent {
3750                    new_content: SerializableEventContent::new(
3751                        &RoomMessageEventContent::text_plain(format!("edit{i}")).into(),
3752                    )
3753                    .unwrap(),
3754                },
3755                parent_key: None,
3756                created_at: MilliSecondsSinceUnixEpoch::now(),
3757            })
3758            .collect::<Vec<_>>();
3759
3760        let txn = inputs[9].parent_transaction_id.clone();
3761
3762        let res = canonicalize_dependent_requests(&inputs);
3763
3764        assert_eq!(res.len(), 1);
3765        assert_let!(DependentQueuedRequestKind::EditEvent { new_content } = &res[0].kind);
3766        assert_let!(
3767            AnyMessageLikeEventContent::RoomMessage(msg) = new_content.deserialize().unwrap()
3768        );
3769        assert_eq!(msg.body(), "edit9");
3770        assert_eq!(res[0].parent_transaction_id, txn);
3771    }
3772
3773    #[test]
3774    fn test_canonicalize_multiple_local_echoes() {
3775        let txn1 = TransactionId::new();
3776        let txn2 = TransactionId::new();
3777
3778        let child1 = ChildTransactionId::new();
3779        let child2 = ChildTransactionId::new();
3780
3781        let inputs = vec![
3782            // This one pertains to txn1.
3783            DependentQueuedRequest {
3784                own_transaction_id: child1.clone(),
3785                kind: DependentQueuedRequestKind::RedactEventWithReason { reason: None },
3786                parent_transaction_id: txn1.clone(),
3787                parent_key: None,
3788                created_at: MilliSecondsSinceUnixEpoch::now(),
3789            },
3790            // This one pertains to txn2.
3791            DependentQueuedRequest {
3792                own_transaction_id: child2,
3793                kind: DependentQueuedRequestKind::EditEvent {
3794                    new_content: SerializableEventContent::new(
3795                        &RoomMessageEventContent::text_plain("edit").into(),
3796                    )
3797                    .unwrap(),
3798                },
3799                parent_transaction_id: txn2.clone(),
3800                parent_key: None,
3801                created_at: MilliSecondsSinceUnixEpoch::now(),
3802            },
3803        ];
3804
3805        let res = canonicalize_dependent_requests(&inputs);
3806
3807        // The canonicalization shouldn't depend per event id.
3808        assert_eq!(res.len(), 2);
3809
3810        for dependent in res {
3811            if dependent.own_transaction_id == child1 {
3812                assert_eq!(dependent.parent_transaction_id, txn1);
3813                assert_matches!(
3814                    dependent.kind,
3815                    DependentQueuedRequestKind::RedactEventWithReason { .. }
3816                );
3817            } else {
3818                assert_eq!(dependent.parent_transaction_id, txn2);
3819                assert_matches!(dependent.kind, DependentQueuedRequestKind::EditEvent { .. });
3820            }
3821        }
3822    }
3823
3824    #[test]
3825    fn test_canonicalize_reactions_after_edits() {
3826        // Sending reactions should happen after edits to a given event.
3827        let txn = TransactionId::new();
3828
3829        let react_id = ChildTransactionId::new();
3830        let react = DependentQueuedRequest {
3831            own_transaction_id: react_id.clone(),
3832            kind: DependentQueuedRequestKind::ReactEvent { key: "🧠".to_owned() },
3833            parent_transaction_id: txn.clone(),
3834            parent_key: None,
3835            created_at: MilliSecondsSinceUnixEpoch::now(),
3836        };
3837
3838        let edit_id = ChildTransactionId::new();
3839        let edit = DependentQueuedRequest {
3840            own_transaction_id: edit_id.clone(),
3841            kind: DependentQueuedRequestKind::EditEvent {
3842                new_content: SerializableEventContent::new(
3843                    &RoomMessageEventContent::text_plain("edit").into(),
3844                )
3845                .unwrap(),
3846            },
3847            parent_transaction_id: txn,
3848            parent_key: None,
3849            created_at: MilliSecondsSinceUnixEpoch::now(),
3850        };
3851
3852        let res = canonicalize_dependent_requests(&[react, edit]);
3853
3854        assert_eq!(res.len(), 2);
3855        assert_eq!(res[0].own_transaction_id, edit_id);
3856        assert_eq!(res[1].own_transaction_id, react_id);
3857    }
3858
3859    #[async_test]
3860    async fn test_request_wedged_by_a_rejected_token_is_unwedged_on_lag() {
3861        let mock = MatrixMockServer::new().await;
3862        let client = mock.client_builder().build().await;
3863        let room = mock.sync_joined_room(&client, room_id!("!a:b.c")).await;
3864
3865        let q = room.send_queue();
3866        let (_, mut watch) = q.subscribe().await.unwrap();
3867
3868        mock.mock_room_state_encryption().plain().mount().await;
3869        mock.mock_room_send().error_unknown_token(false).mock_once().mount().await;
3870        mock.mock_room_send().ok(event_id!("$42")).mock_once().mount().await;
3871
3872        q.send(RoomMessageEventContent::text_plain("hello").into()).await.unwrap();
3873
3874        assert_let!(Ok(RoomSendQueueUpdate::NewLocalEvent(_)) = watch.recv().await);
3875        assert_let!(
3876            Ok(RoomSendQueueUpdate::SendError { is_recoverable: false, .. }) = watch.recv().await
3877        );
3878
3879        // The token gets refreshed, then rejected again before the send queue
3880        // gets to see the refresh, so all it sees is a lag.
3881        let sender = &client.auth_ctx().session_change_sender;
3882        sender.send(SessionChange::TokensRefreshed).unwrap();
3883        sender.send(SessionChange::UnknownToken(UnknownTokenErrorData::new())).unwrap();
3884
3885        // Which is still enough for the request to be retried.
3886        assert_let!(Ok(RoomSendQueueUpdate::RetryEvent { .. }) = watch.recv().await);
3887        assert_let!(Ok(RoomSendQueueUpdate::SentEvent { .. }) = watch.recv().await);
3888    }
3889
3890    #[test]
3891    fn test_build_timeline_event() {
3892        let content = serde_json::value::to_raw_value(&serde_json::json!({
3893            "msgtype": "m.text",
3894            "body": "some text",
3895        }))
3896        .unwrap();
3897
3898        let now = UInt::new(1_700_000_000_000).unwrap();
3899
3900        let event = build_timeline_event(
3901            event_id!("$123"),
3902            now,
3903            user_id!("@bob:matrix.org"),
3904            "m.room.message",
3905            content,
3906        );
3907
3908        assert_eq!(event["event_id"], "$123");
3909        assert_eq!(event["origin_server_ts"].as_u64(), Some(now.into()));
3910        assert_eq!(event["sender"], "@bob:matrix.org");
3911        assert_eq!(event["type"], "m.room.message");
3912        assert_eq!(event["content"]["msgtype"], "m.text");
3913        assert_eq!(event["content"]["body"], "some text");
3914    }
3915
3916    #[test]
3917    fn test_build_redaction_event_no_content_field_redacts() {
3918        let now = UInt::new(1_700_000_000_000).unwrap();
3919
3920        let event = build_redaction_event(
3921            event_id!("$123"),
3922            now,
3923            user_id!("@alice:matrix.org"),
3924            event_id!("$1234"),
3925            Some("reason"),
3926            false,
3927        );
3928
3929        assert_eq!(event["event_id"], "$123");
3930        assert_eq!(event["origin_server_ts"].as_u64(), Some(now.into()));
3931        assert_eq!(event["sender"], "@alice:matrix.org");
3932        assert_eq!(event["type"], "m.room.redaction");
3933        assert_eq!(event["redacts"], "$1234");
3934        assert_eq!(event["content"]["reason"], "reason");
3935    }
3936
3937    #[test]
3938    fn test_build_redaction_event_content_field_redacts() {
3939        let now = UInt::new(1_700_000_000_000).unwrap();
3940
3941        let event = build_redaction_event(
3942            event_id!("$123"),
3943            now,
3944            user_id!("@alice:matrix.org"),
3945            event_id!("$1234"),
3946            Some("reason"),
3947            true,
3948        );
3949
3950        assert_eq!(event["event_id"], "$123");
3951        assert_eq!(event["origin_server_ts"].as_u64(), Some(now.into()));
3952        assert_eq!(event["sender"], "@alice:matrix.org");
3953        assert_eq!(event["type"], "m.room.redaction");
3954        assert_eq!(event["content"]["redacts"], "$1234");
3955        assert_eq!(event["content"]["reason"], "reason");
3956    }
3957}