diff --git a/deltachat-ffi/deltachat.h b/deltachat-ffi/deltachat.h index 4e8b307d8..634eab881 100644 --- a/deltachat-ffi/deltachat.h +++ b/deltachat-ffi/deltachat.h @@ -1587,11 +1587,11 @@ int dc_set_chat_profile_image (dc_context_t* context, uint32_t ch * * @memberof dc_context_t * @param chat_id The chat ID to set the mute duration. - * @param duration The duration (0 for no mute, -1 for forever mute, >0 unix timestamp it until it should be unmuted again) + * @param duration The duration (0 for no mute, -1 for forever mute, everything else is is the relative mute duration from now in seconds) * @param context The context as created by dc_context_new(). * @return 1=success, 0=error */ -int dc_set_chat_muted (dc_context_t* context, uint32_t chat_id, int64_t duration); +int dc_chat_set_mute_duration (dc_context_t* context, uint32_t chat_id, int64_t duration); // handle messages @@ -2950,7 +2950,7 @@ int dc_chat_is_muted (const dc_chat_t* chat); * * @memberof dc_chat_t * @param chat The chat object. - * @return 0=not muted, -1=forever muted, (x>0)=unix timestamp until mute is lifted + * @return 0=not muted, -1=forever muted, (x>0)=remaining seconds until the mute is lifted */ int64_t dc_chat_get_mute_duration (const dc_chat_t* chat); diff --git a/deltachat-ffi/src/lib.rs b/deltachat-ffi/src/lib.rs index 4be954a9e..ae69b3a28 100644 --- a/deltachat-ffi/src/lib.rs +++ b/deltachat-ffi/src/lib.rs @@ -25,6 +25,7 @@ use libc::uintptr_t; use num_traits::{FromPrimitive, ToPrimitive}; use deltachat::chat::ChatId; +use deltachat::chat::MuteDuration; use deltachat::constants::DC_MSG_ID_LAST_SPECIAL; use deltachat::contact::Contact; use deltachat::context::Context; @@ -33,6 +34,9 @@ use deltachat::message::MsgId; use deltachat::stock::StockMessage; use deltachat::*; +mod tools; +use crate::tools::time; + mod dc_array; mod string; @@ -1408,19 +1412,26 @@ pub unsafe extern "C" fn dc_set_chat_profile_image( } #[no_mangle] -pub unsafe extern "C" fn dc_set_chat_muted( +pub unsafe extern "C" fn dc_chat_set_mute_duration( context: *mut dc_context_t, chat_id: u32, duration: i64, ) -> libc::c_int { if context.is_null() || chat_id <= constants::DC_CHAT_ID_LAST_SPECIAL as u32 { - eprintln!("ignoring careless call to dc_set_chat_muted()"); + eprintln!("ignoring careless call to dc_chat_set_mute_duration()"); return 0; } + + let muteDuration = match duration { + 0 => MuteDuration::NotMuted, + -1 => MuteDuration::Forever, + _ => MuteDuration::MutedUntilTimestamp(time() + duration), + }; + let ffi_context = &*context; ffi_context .with_inner(|ctx| { - chat::set_muted(ctx, chat_id, chat::MuteDuration::deserialize(duration)) + chat::set_muted(ctx, chat_id, muteDuration) .map(|_| 1) .unwrap_or_log_default(ctx, "Failed to set mute duration") }) @@ -2514,11 +2525,18 @@ pub unsafe extern "C" fn dc_chat_is_muted(chat: *mut dc_chat_t) -> libc::c_int { #[no_mangle] pub unsafe extern "C" fn dc_chat_get_mute_duration(chat: *mut dc_chat_t) -> i64 { if chat.is_null() { - eprintln!("ignoring careless call to dc_chat_is_muted()"); + eprintln!("ignoring careless call to dc_chat_get_mute_duration()"); return 0; } let ffi_chat = &*chat; - ffi_chat.chat.mute_duration.serialize() as i64 + if !ffi_chat.chat.is_muted() { + return 0; + } + match ffi_chat.chat.mute_duration { + MuteDuration::NotMuted => 0, + MuteDuration::Forever => -1, + MuteDuration::MutedUntilTimestamp(timestamp) => timestamp - time(), + } } #[no_mangle] diff --git a/deltachat-ffi/src/tools.rs b/deltachat-ffi/src/tools.rs new file mode 100644 index 000000000..e7f7e8bb7 --- /dev/null +++ b/deltachat-ffi/src/tools.rs @@ -0,0 +1,8 @@ +use std::time::SystemTime; + +pub(crate) fn time() -> i64 { + SystemTime::now() + .duration_since(SystemTime::UNIX_EPOCH) + .unwrap_or_default() + .as_secs() as i64 +} diff --git a/python/src/deltachat/chat.py b/python/src/deltachat/chat.py index 525c358f0..0b5ff3e40 100644 --- a/python/src/deltachat/chat.py +++ b/python/src/deltachat/chat.py @@ -105,20 +105,20 @@ class Chat(object): :returns: """ if duration is None: - timestamp = -1 + mute_duration = -1 else: - timestamp = int(time.time()) + duration - return bool(lib.dc_set_chat_muted(self._dc_context, self.id, timestamp)) + mute_duration = duration + return bool(lib.dc_chat_set_mute_duration(self._dc_context, self.id, mute_duration)) def unmute(self): """ unmutes the chat :returns: """ - return bool(lib.dc_set_chat_muted(self._dc_context, self.id, 0)) + return bool(lib.dc_chat_set_mute_duration(self._dc_context, self.id, 0)) def get_mute_duration(self): - """ mutes the chat + """ Returns the number of seconds until the mute of this chat is lifted. :param duration: :returns: Returns the number of seconds the chat is still muted for. (0 for not muted, -1 forever muted) diff --git a/src/chat.rs b/src/chat.rs index 741f7627e..a1abb68dc 100644 --- a/src/chat.rs +++ b/src/chat.rs @@ -1938,7 +1938,13 @@ impl MuteDuration { match value { 0 => MuteDuration::NotMuted, -1 => MuteDuration::Forever, - _ => MuteDuration::MutedUntilTimestamp(value), + _ => { + if value <= time() { + MuteDuration::NotMuted + } else { + MuteDuration::MutedUntilTimestamp(value) + } + } } } }