mirror of
https://github.com/chatmail/core.git
synced 2026-05-07 17:06:35 +03:00
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 22e5bf8571.
This commit is contained in:
@@ -98,19 +98,26 @@ impl MsgId {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Updates the message download state. Returns `Ok` if the message doesn't exist anymore.
|
||||||
pub(crate) async fn update_download_state(
|
pub(crate) async fn update_download_state(
|
||||||
self,
|
self,
|
||||||
context: &Context,
|
context: &Context,
|
||||||
download_state: DownloadState,
|
download_state: DownloadState,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let msg = Message::load_from_db(context, self).await?;
|
if context
|
||||||
context
|
|
||||||
.sql
|
.sql
|
||||||
.execute(
|
.execute(
|
||||||
"UPDATE msgs SET download_state=? WHERE id=?;",
|
"UPDATE msgs SET download_state=? WHERE id=?;",
|
||||||
(download_state, self),
|
(download_state, self),
|
||||||
)
|
)
|
||||||
.await?;
|
.await?
|
||||||
|
== 0
|
||||||
|
{
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
let Some(msg) = Message::load_from_db_optional(context, self).await? else {
|
||||||
|
return Ok(());
|
||||||
|
};
|
||||||
context.emit_event(EventType::MsgsChanged {
|
context.emit_event(EventType::MsgsChanged {
|
||||||
chat_id: msg.chat_id,
|
chat_id: msg.chat_id,
|
||||||
msg_id: self,
|
msg_id: self,
|
||||||
@@ -322,11 +329,17 @@ mod tests {
|
|||||||
DownloadState::InProgress,
|
DownloadState::InProgress,
|
||||||
DownloadState::Failure,
|
DownloadState::Failure,
|
||||||
DownloadState::Done,
|
DownloadState::Done,
|
||||||
|
DownloadState::Done,
|
||||||
] {
|
] {
|
||||||
msg_id.update_download_state(&t, *s).await?;
|
msg_id.update_download_state(&t, *s).await?;
|
||||||
let msg = Message::load_from_db(&t, msg_id).await?;
|
let msg = Message::load_from_db(&t, msg_id).await?;
|
||||||
assert_eq!(msg.download_state(), *s);
|
assert_eq!(msg.download_state(), *s);
|
||||||
}
|
}
|
||||||
|
msg_id.delete_from_db(&t).await?;
|
||||||
|
// Nothing to do is ok.
|
||||||
|
msg_id
|
||||||
|
.update_download_state(&t, DownloadState::Done)
|
||||||
|
.await?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user