Skip to content

Commit 8acf391

Browse files
committed
refactor: MsgId::update_download_state: Don't fail if the message doesn't exist anymore
If a race happens and the message disappears, there's just nothing to do and no sense to fail. Follow-up to 22e5bf8.
1 parent aacea2d commit 8acf391

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

src/download.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,19 +98,26 @@ impl MsgId {
9898
Ok(())
9999
}
100100

101+
/// Updates the message download state. Returns `Ok` if the message doesn't exist anymore.
101102
pub(crate) async fn update_download_state(
102103
self,
103104
context: &Context,
104105
download_state: DownloadState,
105106
) -> Result<()> {
106-
let msg = Message::load_from_db(context, self).await?;
107-
context
107+
if context
108108
.sql
109109
.execute(
110110
"UPDATE msgs SET download_state=? WHERE id=?;",
111111
(download_state, self),
112112
)
113-
.await?;
113+
.await?
114+
== 0
115+
{
116+
return Ok(());
117+
}
118+
let Some(msg) = Message::load_from_db_optional(context, self).await? else {
119+
return Ok(());
120+
};
114121
context.emit_event(EventType::MsgsChanged {
115122
chat_id: msg.chat_id,
116123
msg_id: self,
@@ -322,11 +329,17 @@ mod tests {
322329
DownloadState::InProgress,
323330
DownloadState::Failure,
324331
DownloadState::Done,
332+
DownloadState::Done,
325333
] {
326334
msg_id.update_download_state(&t, *s).await?;
327335
let msg = Message::load_from_db(&t, msg_id).await?;
328336
assert_eq!(msg.download_state(), *s);
329337
}
338+
msg_id.delete_from_db(&t).await?;
339+
// Nothing to do is ok.
340+
msg_id
341+
.update_download_state(&t, DownloadState::Done)
342+
.await?;
330343

331344
Ok(())
332345
}

0 commit comments

Comments
 (0)