Skip to content

Commit 9575ee9

Browse files
committed
timeline queue: unify updating event send state into a single place
1 parent e49d629 commit 9575ee9

File tree

1 file changed

+57
-51
lines changed
  • crates/matrix-sdk-ui/src/timeline

1 file changed

+57
-51
lines changed

crates/matrix-sdk-ui/src/timeline/queue.rs

Lines changed: 57 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use matrix_sdk::{
2727
Room,
2828
};
2929
use matrix_sdk_base::RoomState;
30-
use ruma::{events::AnyMessageLikeEventContent, OwnedTransactionId};
30+
use ruma::{events::AnyMessageLikeEventContent, OwnedEventId, OwnedTransactionId};
3131
use tokio::{select, sync::mpsc::Receiver};
3232
use tracing::{debug, error, info, instrument, trace, warn};
3333

@@ -57,13 +57,13 @@ pub(super) async fn send_queued_messages(
5757

5858
loop {
5959
select! {
60-
result = &mut send_task => {
60+
send_result = &mut send_task => {
6161
trace!("SendMessageTask finished");
6262

6363
send_task.reset();
6464

6565
handle_send_result(
66-
result,
66+
send_result,
6767
&mut send_task,
6868
&mut queue,
6969
&timeline,
@@ -129,7 +129,7 @@ async fn send_or_queue_msg(
129129
return;
130130
}
131131

132-
send_task.start(room, timeline.clone(), msg);
132+
send_task.start(room, msg);
133133
}
134134

135135
async fn handle_send_result(
@@ -139,16 +139,23 @@ async fn handle_send_result(
139139
timeline: &TimelineInner,
140140
) {
141141
match send_result {
142-
SendMessageResult::Success { room } => {
142+
SendMessageResult::Success { event_id, txn_id } => {
143+
timeline.update_event_send_state(&txn_id, EventSendState::Sent { event_id }).await;
144+
143145
// Event was successfully sent, move on to the next queued event.
144146
if let Some(msg) = queue.pop_front() {
145-
send_task.start(room, timeline.clone(), msg);
147+
send_task.start(timeline.room().clone(), msg);
146148
}
147149
}
148150

149-
SendMessageResult::SendingFailed => {
150-
// Timeline items are marked as failed / cancelled in this case.
151-
//
151+
SendMessageResult::SendingFailed { send_error, txn_id } => {
152+
timeline
153+
.update_event_send_state(
154+
&txn_id,
155+
EventSendState::SendingFailed { error: Arc::new(send_error) },
156+
)
157+
.await;
158+
152159
// Clear the queue and wait for the user to explicitly retry (which will
153160
// re-append the to-be-sent events in the queue).
154161
queue.clear();
@@ -157,36 +164,41 @@ async fn handle_send_result(
157164
SendMessageResult::TaskError { join_error, txn_id } => {
158165
error!("Message-sending task failed: {join_error}");
159166

167+
timeline
168+
.update_event_send_state(
169+
&txn_id,
170+
EventSendState::SendingFailed {
171+
// FIXME: Probably not exactly right
172+
error: Arc::new(matrix_sdk::Error::InconsistentState),
173+
},
174+
)
175+
.await;
176+
160177
// See above comment in the `SendingFailed` arm.
161178
queue.clear();
162-
163-
let send_state = EventSendState::SendingFailed {
164-
// FIXME: Probably not exactly right
165-
error: Arc::new(matrix_sdk::Error::InconsistentState),
166-
};
167-
168-
timeline.update_event_send_state(&txn_id, send_state).await;
169179
}
170180
}
171181
}
172182

173183
/// Result of [`SendMessageTask`].
174184
enum SendMessageResult {
175-
/// The message was sent successfully, and the local echo was updated to
176-
/// indicate this.
185+
/// The message was sent successfully.
177186
Success {
178-
/// The joined room object, used to start sending of the next message
179-
/// in the queue, if it isn't empty.
180-
room: Room,
187+
/// The event id returned by the server.
188+
event_id: OwnedEventId,
189+
/// The transaction ID of the message that was being sent by the task.
190+
txn_id: OwnedTransactionId,
181191
},
182192

183-
/// Sending failed, and the local echo was updated to indicate this.
184-
SendingFailed,
193+
/// Sending failed.
194+
SendingFailed {
195+
/// The reason of the sending failure.
196+
send_error: matrix_sdk::Error,
197+
/// The transaction ID of the message that was being sent by the task.
198+
txn_id: OwnedTransactionId,
199+
},
185200

186201
/// The [`SendMessageTask`] failed, likely due to a panic.
187-
///
188-
/// This means that the timeline item was likely not updated yet, which thus
189-
/// becomes the responsibility of the code observing this result.
190202
TaskError {
191203
/// The error with which the task failed.
192204
join_error: JoinError,
@@ -206,7 +218,7 @@ enum SendMessageTask {
206218
/// The transaction ID of the message that is being sent.
207219
txn_id: OwnedTransactionId,
208220
/// Handle to the task itself.
209-
task: JoinHandle<Option<Room>>,
221+
task: JoinHandle<SendMessageResult>,
210222
},
211223
}
212224

@@ -218,22 +230,20 @@ impl SendMessageTask {
218230

219231
/// Spawns a task sending the message to the room, and updating the timeline
220232
/// once the result has been processed.
221-
fn start(&mut self, room: Room, timeline: TimelineInner, msg: LocalMessage) {
233+
fn start(&mut self, room: Room, msg: LocalMessage) {
222234
debug!("Spawning message-sending task");
223235

224236
let txn_id = msg.txn_id.clone();
225237

226238
let task = spawn(async move {
227-
let result = room.send(msg.content).with_transaction_id(&msg.txn_id).await;
228-
229-
let (room, send_state) = match result {
230-
Ok(response) => (Some(room), EventSendState::Sent { event_id: response.event_id }),
231-
Err(error) => (None, EventSendState::SendingFailed { error: Arc::new(error) }),
232-
};
233-
234-
timeline.update_event_send_state(&msg.txn_id, send_state).await;
235-
236-
room
239+
match room.send(msg.content).with_transaction_id(&msg.txn_id).await {
240+
Ok(response) => {
241+
SendMessageResult::Success { event_id: response.event_id, txn_id: msg.txn_id }
242+
}
243+
Err(error) => {
244+
SendMessageResult::SendingFailed { send_error: error, txn_id: msg.txn_id }
245+
}
246+
}
237247
});
238248

239249
*self = Self::Running { txn_id, task };
@@ -251,20 +261,16 @@ impl Future for SendMessageTask {
251261
match &mut *self {
252262
SendMessageTask::Idle => Poll::Pending,
253263

254-
SendMessageTask::Running { txn_id, task: join_handle } => {
255-
Pin::new(join_handle).poll(cx).map(|result| {
256-
let txn_id = mem::replace(txn_id, OwnedTransactionId::from(""));
257-
if txn_id.as_str().is_empty() {
258-
warn!("SendMessageTask polled after returning Poll::Ready!");
259-
}
260-
261-
match result {
262-
Ok(Some(room)) => SendMessageResult::Success { room },
263-
Ok(None) => SendMessageResult::SendingFailed,
264-
Err(join_error) => SendMessageResult::TaskError { join_error, txn_id },
265-
}
264+
SendMessageTask::Running { txn_id, task } => Pin::new(task).poll(cx).map(|result| {
265+
let txn_id = mem::replace(txn_id, OwnedTransactionId::from(""));
266+
if txn_id.as_str().is_empty() {
267+
warn!("SendMessageTask polled after returning Poll::Ready!");
268+
}
269+
result.unwrap_or_else(|error| SendMessageResult::TaskError {
270+
join_error: error,
271+
txn_id,
266272
})
267-
}
273+
}),
268274
}
269275
}
270276
}

0 commit comments

Comments
 (0)