From 51133ce0d6a490834b599719677b33d5370f699f Mon Sep 17 00:00:00 2001 From: Alexander Krotov Date: Wed, 1 Jan 2020 23:10:47 +0100 Subject: [PATCH] Add autodelete timer to the database --- src/chat.rs | 31 ++++++++++++++++++++++++++++++- src/sql.rs | 8 ++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/chat.rs b/src/chat.rs index 5c409dbbe..2a9b5cef5 100644 --- a/src/chat.rs +++ b/src/chat.rs @@ -663,6 +663,7 @@ impl Chat { profile_image: self.get_profile_image(context).unwrap_or_else(PathBuf::new), draft, is_muted: self.is_muted(), + autodelete_timer: get_autodelete_timer(context, self.id), }) } @@ -1022,6 +1023,8 @@ pub struct ChatInfo { // - [ ] lastUpdated, // - [ ] freshMessageCounter, // - [ ] email + /// Automatic message deletion timer. + pub autodelete_timer: u32, } /// Create a chat from a message ID. @@ -2567,6 +2570,31 @@ pub(crate) fn add_info_msg(context: &Context, chat_id: ChatId, text: impl AsRef< }); } +/// Get autodelete timer value in seconds. +pub fn get_autodelete_timer(context: &Context, chat_id: ChatId) -> u32 { + context + .sql + .query_get_value( + context, + "SELECT autodelete_timer FROM chats WHERE id=?;", + params![chat_id], + ) + .unwrap_or_default() +} + +/// Set autodelete timer value in seconds. +/// +/// If timer value is 0, disable autodelete timer. +pub fn set_autodelete_timer(context: &Context, chat_id: ChatId, timer: u32) -> Result<(), Error> { + context.sql.execute( + "UPDATE chats + SET autodelete_timer=? + WHERE id=?;", + params![timer, chat_id], + )?; + Ok(()) +} + #[cfg(test)] mod tests { use super::*; @@ -2597,7 +2625,8 @@ mod tests { "color": 15895624, "profile_image": "", "draft": "", - "is_muted": false + "is_muted": false, + "autodelete_timer": 0 } "#; diff --git a/src/sql.rs b/src/sql.rs index c5927aa04..1bcc19679 100644 --- a/src/sql.rs +++ b/src/sql.rs @@ -902,6 +902,14 @@ fn open( sql.execute("UPDATE chats SET grpid='' WHERE type=100", NO_PARAMS)?; sql.set_raw_config_int(context, "dbversion", 63)?; } + if dbversion < 64 { + info!(context, "[migration] v64"); + sql.execute( + "ALTER TABLE chats ADD COLUMN autodelete_timer INTEGER DEFAULT 0;", + NO_PARAMS, + )?; + sql.set_raw_config_int(context, "dbversion", 64)?; + } // (2) updates that require high-level objects // (the structure is complete now and all objects are usable)