From 33a203d56e6e7f4b3798a5e1276a6a0bdf96e5bd Mon Sep 17 00:00:00 2001 From: link2xt Date: Wed, 27 Sep 2023 19:30:19 +0000 Subject: [PATCH] fix: initialise last_msg_id to the highest known row id Otherwise existing bots migrating to get_next_msgs() are trying to process all the messages they have in the database. --- src/context.rs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/context.rs b/src/context.rs index d1300f7fa..d7aac82d1 100644 --- a/src/context.rs +++ b/src/context.rs @@ -814,7 +814,22 @@ impl Context { pub async fn get_next_msgs(&self) -> Result> { let last_msg_id = match self.get_config(Config::LastMsgId).await? { Some(s) => MsgId::new(s.parse()?), - None => MsgId::new_unset(), + None => { + // If `last_msg_id` is not set yet, + // subtract 1 from the last id, + // so a single message is returned and can + // be marked as seen. + self.sql + .query_row( + "SELECT IFNULL((SELECT MAX(id) - 1 FROM msgs), 0)", + (), + |row| { + let msg_id: MsgId = row.get(0)?; + Ok(msg_id) + }, + ) + .await? + } }; let list = self