api: get_existing_msg_ids()

This API allows to check if the message with
given ID exists and distinguish between
message not existing and database error.
It might also be faster than
checking messages one by one
if multiple messages need to be checked
because of using a single SQL transaction.
This commit is contained in:
link2xt
2025-11-22 01:12:35 +00:00
committed by l
parent 6514b4ca7f
commit c9c362d5ff
3 changed files with 66 additions and 2 deletions

View File

@@ -1868,6 +1868,33 @@ pub async fn markseen_msgs(context: &Context, msg_ids: Vec<MsgId>) -> Result<()>
Ok(())
}
/// Checks if the messages with given IDs exist.
///
/// Returns IDs of existing messages.
pub async fn get_existing_msg_ids(context: &Context, ids: &[MsgId]) -> Result<Vec<MsgId>> {
let query_only = true;
let res = context
.sql
.transaction_ex(query_only, |transaction| {
let mut res: Vec<MsgId> = Vec::new();
for id in ids {
if transaction.query_one(
"SELECT COUNT(*) > 0 FROM msgs WHERE id=? AND chat_id!=3",
(id,),
|row| {
let exists: bool = row.get(0)?;
Ok(exists)
},
)? {
res.push(*id);
}
}
Ok(res)
})
.await?;
Ok(res)
}
pub(crate) async fn update_msg_state(
context: &Context,
msg_id: MsgId,