Skip to content

Commit

Permalink
refactor: factor out insert_tombstone
Browse files Browse the repository at this point in the history
  • Loading branch information
link2xt committed Nov 28, 2023
1 parent 37d2aaf commit 4287a4d
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions src/receive_imf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,22 @@ pub async fn receive_imf(
receive_imf_inner(context, &rfc724_mid, imf_raw, seen, None, false).await
}

/// Inserts a tombstone into `msgs` table
/// to prevent downloading the same message in the future.
///
/// Returns tombstone database row ID.
async fn insert_tombstone(context: &Context, rfc724_mid: &str) -> Result<MsgId> {
let row_id = context
.sql
.insert(
"INSERT INTO msgs(rfc724_mid, chat_id) VALUES (?,?)",
(rfc724_mid, DC_CHAT_ID_TRASH),
)
.await?;
let msg_id = MsgId::new(u32::try_from(row_id)?);
Ok(msg_id)
}

/// Receive a message and add it to the database.
///
/// Returns an error on database failure or if the message is broken,
Expand Down Expand Up @@ -137,14 +153,7 @@ pub(crate) async fn receive_imf_inner(
return Ok(None);
}

let row_id = context
.sql
.insert(
"INSERT INTO msgs(rfc724_mid, chat_id) VALUES (?,?)",
(rfc724_mid, DC_CHAT_ID_TRASH),
)
.await?;
let msg_ids = vec![MsgId::new(u32::try_from(row_id)?)];
let msg_ids = vec![insert_tombstone(context, rfc724_mid).await?];

return Ok(Some(ReceivedMsg {
chat_id: DC_CHAT_ID_TRASH,
Expand Down

0 comments on commit 4287a4d

Please sign in to comment.