mirror of
https://github.com/chatmail/core.git
synced 2026-05-06 16:36:59 +03:00
dtransform mute chat to use relative durations
instead of absolute timestamps
This commit is contained in:
@@ -1587,11 +1587,11 @@ int dc_set_chat_profile_image (dc_context_t* context, uint32_t ch
|
|||||||
*
|
*
|
||||||
* @memberof dc_context_t
|
* @memberof dc_context_t
|
||||||
* @param chat_id The chat ID to set the mute duration.
|
* @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().
|
* @param context The context as created by dc_context_new().
|
||||||
* @return 1=success, 0=error
|
* @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
|
// handle messages
|
||||||
|
|
||||||
@@ -2950,7 +2950,7 @@ int dc_chat_is_muted (const dc_chat_t* chat);
|
|||||||
*
|
*
|
||||||
* @memberof dc_chat_t
|
* @memberof dc_chat_t
|
||||||
* @param chat The chat object.
|
* @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);
|
int64_t dc_chat_get_mute_duration (const dc_chat_t* chat);
|
||||||
|
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ use libc::uintptr_t;
|
|||||||
use num_traits::{FromPrimitive, ToPrimitive};
|
use num_traits::{FromPrimitive, ToPrimitive};
|
||||||
|
|
||||||
use deltachat::chat::ChatId;
|
use deltachat::chat::ChatId;
|
||||||
|
use deltachat::chat::MuteDuration;
|
||||||
use deltachat::constants::DC_MSG_ID_LAST_SPECIAL;
|
use deltachat::constants::DC_MSG_ID_LAST_SPECIAL;
|
||||||
use deltachat::contact::Contact;
|
use deltachat::contact::Contact;
|
||||||
use deltachat::context::Context;
|
use deltachat::context::Context;
|
||||||
@@ -33,6 +34,9 @@ use deltachat::message::MsgId;
|
|||||||
use deltachat::stock::StockMessage;
|
use deltachat::stock::StockMessage;
|
||||||
use deltachat::*;
|
use deltachat::*;
|
||||||
|
|
||||||
|
mod tools;
|
||||||
|
use crate::tools::time;
|
||||||
|
|
||||||
mod dc_array;
|
mod dc_array;
|
||||||
|
|
||||||
mod string;
|
mod string;
|
||||||
@@ -1408,19 +1412,26 @@ pub unsafe extern "C" fn dc_set_chat_profile_image(
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[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,
|
context: *mut dc_context_t,
|
||||||
chat_id: u32,
|
chat_id: u32,
|
||||||
duration: i64,
|
duration: i64,
|
||||||
) -> libc::c_int {
|
) -> libc::c_int {
|
||||||
if context.is_null() || chat_id <= constants::DC_CHAT_ID_LAST_SPECIAL as u32 {
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let muteDuration = match duration {
|
||||||
|
0 => MuteDuration::NotMuted,
|
||||||
|
-1 => MuteDuration::Forever,
|
||||||
|
_ => MuteDuration::MutedUntilTimestamp(time() + duration),
|
||||||
|
};
|
||||||
|
|
||||||
let ffi_context = &*context;
|
let ffi_context = &*context;
|
||||||
ffi_context
|
ffi_context
|
||||||
.with_inner(|ctx| {
|
.with_inner(|ctx| {
|
||||||
chat::set_muted(ctx, chat_id, chat::MuteDuration::deserialize(duration))
|
chat::set_muted(ctx, chat_id, muteDuration)
|
||||||
.map(|_| 1)
|
.map(|_| 1)
|
||||||
.unwrap_or_log_default(ctx, "Failed to set mute duration")
|
.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]
|
#[no_mangle]
|
||||||
pub unsafe extern "C" fn dc_chat_get_mute_duration(chat: *mut dc_chat_t) -> i64 {
|
pub unsafe extern "C" fn dc_chat_get_mute_duration(chat: *mut dc_chat_t) -> i64 {
|
||||||
if chat.is_null() {
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
let ffi_chat = &*chat;
|
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]
|
#[no_mangle]
|
||||||
|
|||||||
8
deltachat-ffi/src/tools.rs
Normal file
8
deltachat-ffi/src/tools.rs
Normal 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
|
||||||
|
}
|
||||||
@@ -105,20 +105,20 @@ class Chat(object):
|
|||||||
:returns:
|
:returns:
|
||||||
"""
|
"""
|
||||||
if duration is None:
|
if duration is None:
|
||||||
timestamp = -1
|
mute_duration = -1
|
||||||
else:
|
else:
|
||||||
timestamp = int(time.time()) + duration
|
mute_duration = duration
|
||||||
return bool(lib.dc_set_chat_muted(self._dc_context, self.id, timestamp))
|
return bool(lib.dc_chat_set_mute_duration(self._dc_context, self.id, mute_duration))
|
||||||
|
|
||||||
def unmute(self):
|
def unmute(self):
|
||||||
""" unmutes the chat
|
""" unmutes the chat
|
||||||
|
|
||||||
:returns:
|
: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):
|
def get_mute_duration(self):
|
||||||
""" mutes the chat
|
""" Returns the number of seconds until the mute of this chat is lifted.
|
||||||
|
|
||||||
:param duration:
|
:param duration:
|
||||||
:returns: Returns the number of seconds the chat is still muted for. (0 for not muted, -1 forever muted)
|
:returns: Returns the number of seconds the chat is still muted for. (0 for not muted, -1 forever muted)
|
||||||
|
|||||||
@@ -1938,7 +1938,13 @@ impl MuteDuration {
|
|||||||
match value {
|
match value {
|
||||||
0 => MuteDuration::NotMuted,
|
0 => MuteDuration::NotMuted,
|
||||||
-1 => MuteDuration::Forever,
|
-1 => MuteDuration::Forever,
|
||||||
_ => MuteDuration::MutedUntilTimestamp(value),
|
_ => {
|
||||||
|
if value <= time() {
|
||||||
|
MuteDuration::NotMuted
|
||||||
|
} else {
|
||||||
|
MuteDuration::MutedUntilTimestamp(value)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user