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.
This commit is contained in:
link2xt
2023-09-27 19:30:19 +00:00
parent a19811f379
commit 33a203d56e

View File

@@ -814,7 +814,22 @@ impl Context {
pub async fn get_next_msgs(&self) -> Result<Vec<MsgId>> {
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