dtransform mute chat to use relative durations

instead of absolute timestamps
This commit is contained in:
Simon Laux
2020-02-08 12:45:06 +01:00
parent 07d698f8dc
commit 6d80b3675a
5 changed files with 46 additions and 14 deletions

View File

@@ -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);

View File

@@ -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]

View File

@@ -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
}

View File

@@ -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)

View File

@@ -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)
}
}
}
}
}