Skip to content

Commit 8a7498a

Browse files
committed
fix: handle the case when account does not exist in get_existing_msg_ids()
If account is removed, this means the messages are removed as well. We do not reuse account IDs, so the account will not reappear.
1 parent c41a69e commit 8a7498a

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

deltachat-jsonrpc/src/api.rs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,14 @@ impl CommandApi {
121121
}
122122
}
123123

124+
async fn get_context_opt(&self, id: u32) -> Option<deltachat::context::Context> {
125+
self.accounts.read().await.get_account(id)
126+
}
127+
124128
async fn get_context(&self, id: u32) -> Result<deltachat::context::Context> {
125-
let sc = self
126-
.accounts
127-
.read()
129+
self.get_context_opt(id)
128130
.await
129-
.get_account(id)
130-
.ok_or_else(|| anyhow!("account with id {id} not found"))?;
131-
Ok(sc)
131+
.ok_or_else(|| anyhow!("account with id {id} not found"))
132132
}
133133

134134
async fn with_state<F, T>(&self, id: u32, with_state: F) -> T
@@ -1307,13 +1307,18 @@ impl CommandApi {
13071307
///
13081308
/// Returns IDs of existing messages.
13091309
async fn get_existing_msg_ids(&self, account_id: u32, msg_ids: Vec<u32>) -> Result<Vec<u32>> {
1310-
let context = self.get_context(account_id).await?;
1311-
let msg_ids: Vec<MsgId> = msg_ids.into_iter().map(MsgId::new).collect();
1312-
let existing_msg_ids = get_existing_msg_ids(&context, &msg_ids).await?;
1313-
Ok(existing_msg_ids
1314-
.into_iter()
1315-
.map(|msg_id| msg_id.to_u32())
1316-
.collect())
1310+
if let Some(context) = self.get_context_opt(account_id).await {
1311+
let msg_ids: Vec<MsgId> = msg_ids.into_iter().map(MsgId::new).collect();
1312+
let existing_msg_ids = get_existing_msg_ids(&context, &msg_ids).await?;
1313+
Ok(existing_msg_ids
1314+
.into_iter()
1315+
.map(|msg_id| msg_id.to_u32())
1316+
.collect())
1317+
} else {
1318+
// Account does not exist, so messages do not exist either,
1319+
// but this is not an error.
1320+
Ok(Vec::new())
1321+
}
13171322
}
13181323

13191324
async fn get_message_list_items(

0 commit comments

Comments
 (0)