mirror of
https://github.com/chatmail/core.git
synced 2026-04-27 02:16:29 +03:00
Improve formatting of ephemeral timer change messages
This adds support for more timer values and better fallback for unknown values. For example, 90 seconds is formatted as "1.5 minutes".
This commit is contained in:
@@ -5312,9 +5312,9 @@ void dc_event_unref(dc_event_t* event);
|
||||
/// Used in status messages.
|
||||
#define DC_STR_EPHEMERAL_WEEK 80
|
||||
|
||||
/// "Message deletion timer is set to 4 weeks."
|
||||
/// DEPRECATED
|
||||
///
|
||||
/// Used in status messages.
|
||||
/// DC_STR_EPHEMERAL_WEEKS is used instead.
|
||||
#define DC_STR_EPHEMERAL_FOUR_WEEKS 81
|
||||
|
||||
/// "Video chat invitation"
|
||||
@@ -5376,6 +5376,34 @@ void dc_event_unref(dc_event_t* event);
|
||||
/// Used as device message text.
|
||||
#define DC_STR_SERVER_TURNED_OFF 92
|
||||
|
||||
/// "Message deletion timer is set to %1$s minutes."
|
||||
///
|
||||
/// Used in status messages.
|
||||
//
|
||||
/// `%1$s` will be replaced by the number of minutes (alwasy >1) the timer is set to.
|
||||
#define DC_STR_EPHEMERAL_MINUTES 93
|
||||
|
||||
/// "Message deletion timer is set to %1$s hours."
|
||||
///
|
||||
/// Used in status messages.
|
||||
//
|
||||
/// `%1$s` will be replaced by the number of hours (always >1) the timer is set to.
|
||||
#define DC_STR_EPHEMERAL_HOURS 94
|
||||
|
||||
/// "Message deletion timer is set to %1$s days."
|
||||
///
|
||||
/// Used in status messages.
|
||||
//
|
||||
/// `%1$s` will be replaced by the number of days (always >1) the timer is set to.
|
||||
#define DC_STR_EPHEMERAL_DAYS 95
|
||||
|
||||
/// "Message deletion timer is set to %1$s weeks."
|
||||
///
|
||||
/// Used in status messages.
|
||||
//
|
||||
/// `%1$s` will be replaced by the number of weeks (always >1) the timer is set to.
|
||||
#define DC_STR_EPHEMERAL_WEEKS 96
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
128
src/ephemeral.rs
128
src/ephemeral.rs
@@ -212,21 +212,89 @@ pub(crate) async fn stock_ephemeral_timer_changed(
|
||||
timer: Timer,
|
||||
from_id: u32,
|
||||
) -> String {
|
||||
let stock_message = match timer {
|
||||
Timer::Disabled => StockMessage::MsgEphemeralTimerDisabled,
|
||||
match timer {
|
||||
Timer::Disabled => {
|
||||
context
|
||||
.stock_system_msg(
|
||||
StockMessage::MsgEphemeralTimerDisabled,
|
||||
timer.to_string(),
|
||||
"",
|
||||
from_id,
|
||||
)
|
||||
.await
|
||||
}
|
||||
Timer::Enabled { duration } => match duration {
|
||||
60 => StockMessage::MsgEphemeralTimerMinute,
|
||||
3600 => StockMessage::MsgEphemeralTimerHour,
|
||||
86400 => StockMessage::MsgEphemeralTimerDay,
|
||||
604_800 => StockMessage::MsgEphemeralTimerWeek,
|
||||
2_419_200 => StockMessage::MsgEphemeralTimerFourWeeks,
|
||||
_ => StockMessage::MsgEphemeralTimerEnabled,
|
||||
0..=59 => {
|
||||
context
|
||||
.stock_system_msg(
|
||||
StockMessage::MsgEphemeralTimerEnabled,
|
||||
timer.to_string(),
|
||||
"",
|
||||
from_id,
|
||||
)
|
||||
.await
|
||||
}
|
||||
60 => {
|
||||
context
|
||||
.stock_system_msg(StockMessage::MsgEphemeralTimerMinute, "", "", from_id)
|
||||
.await
|
||||
}
|
||||
61..=3599 => {
|
||||
context
|
||||
.stock_system_msg(
|
||||
StockMessage::MsgEphemeralTimerMinutes,
|
||||
format!("{}", (f64::from(duration) / 6.0).round() / 10.0),
|
||||
"",
|
||||
from_id,
|
||||
)
|
||||
.await
|
||||
}
|
||||
3600 => {
|
||||
context
|
||||
.stock_system_msg(StockMessage::MsgEphemeralTimerHour, "", "", from_id)
|
||||
.await
|
||||
}
|
||||
3601..=86399 => {
|
||||
context
|
||||
.stock_system_msg(
|
||||
StockMessage::MsgEphemeralTimerHours,
|
||||
format!("{}", (f64::from(duration) / 360.0).round() / 10.0),
|
||||
"",
|
||||
from_id,
|
||||
)
|
||||
.await
|
||||
}
|
||||
86400 => {
|
||||
context
|
||||
.stock_system_msg(StockMessage::MsgEphemeralTimerDay, "", "", from_id)
|
||||
.await
|
||||
}
|
||||
86401..=604_799 => {
|
||||
context
|
||||
.stock_system_msg(
|
||||
StockMessage::MsgEphemeralTimerDays,
|
||||
format!("{}", (f64::from(duration) / 8640.0).round() / 10.0),
|
||||
"",
|
||||
from_id,
|
||||
)
|
||||
.await
|
||||
}
|
||||
604_800 => {
|
||||
{ context.stock_system_msg(StockMessage::MsgEphemeralTimerWeek, "", "", from_id) }
|
||||
.await
|
||||
}
|
||||
_ => {
|
||||
context
|
||||
.stock_system_msg(
|
||||
StockMessage::MsgEphemeralTimerWeeks,
|
||||
format!("{}", (f64::from(duration) / 60480.0).round() / 10.0),
|
||||
"",
|
||||
from_id,
|
||||
)
|
||||
.await
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
context
|
||||
.stock_system_msg(stock_message, timer.to_string(), "", from_id)
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
||||
impl MsgId {
|
||||
@@ -494,10 +562,33 @@ mod tests {
|
||||
stock_ephemeral_timer_changed(&context, Timer::Enabled { duration: 60 }, 0).await,
|
||||
"Message deletion timer is set to 1 minute."
|
||||
);
|
||||
assert_eq!(
|
||||
stock_ephemeral_timer_changed(&context, Timer::Enabled { duration: 90 }, 0).await,
|
||||
"Message deletion timer is set to 1.5 minutes."
|
||||
);
|
||||
assert_eq!(
|
||||
stock_ephemeral_timer_changed(&context, Timer::Enabled { duration: 30 * 60 }, 0).await,
|
||||
"Message deletion timer is set to 30 minutes."
|
||||
);
|
||||
assert_eq!(
|
||||
stock_ephemeral_timer_changed(&context, Timer::Enabled { duration: 60 * 60 }, 0).await,
|
||||
"Message deletion timer is set to 1 hour."
|
||||
);
|
||||
assert_eq!(
|
||||
stock_ephemeral_timer_changed(&context, Timer::Enabled { duration: 5400 }, 0).await,
|
||||
"Message deletion timer is set to 1.5 hours."
|
||||
);
|
||||
assert_eq!(
|
||||
stock_ephemeral_timer_changed(
|
||||
&context,
|
||||
Timer::Enabled {
|
||||
duration: 2 * 60 * 60
|
||||
},
|
||||
0
|
||||
)
|
||||
.await,
|
||||
"Message deletion timer is set to 2 hours."
|
||||
);
|
||||
assert_eq!(
|
||||
stock_ephemeral_timer_changed(
|
||||
&context,
|
||||
@@ -509,6 +600,17 @@ mod tests {
|
||||
.await,
|
||||
"Message deletion timer is set to 1 day."
|
||||
);
|
||||
assert_eq!(
|
||||
stock_ephemeral_timer_changed(
|
||||
&context,
|
||||
Timer::Enabled {
|
||||
duration: 2 * 24 * 60 * 60
|
||||
},
|
||||
0
|
||||
)
|
||||
.await,
|
||||
"Message deletion timer is set to 2 days."
|
||||
);
|
||||
assert_eq!(
|
||||
stock_ephemeral_timer_changed(
|
||||
&context,
|
||||
|
||||
15
src/stock.rs
15
src/stock.rs
@@ -206,9 +206,6 @@ pub enum StockMessage {
|
||||
#[strum(props(fallback = "Message deletion timer is set to 1 week."))]
|
||||
MsgEphemeralTimerWeek = 80,
|
||||
|
||||
#[strum(props(fallback = "Message deletion timer is set to 4 weeks."))]
|
||||
MsgEphemeralTimerFourWeeks = 81,
|
||||
|
||||
#[strum(props(fallback = "Video chat invitation"))]
|
||||
VideochatInvitation = 82,
|
||||
|
||||
@@ -255,6 +252,18 @@ pub enum StockMessage {
|
||||
Settings → \"Chats and Media\" → \"Delete messages from server\" to continue using it."
|
||||
))]
|
||||
DeleteServerTurnedOff = 92,
|
||||
|
||||
#[strum(props(fallback = "Message deletion timer is set to %1$s minutes."))]
|
||||
MsgEphemeralTimerMinutes = 93,
|
||||
|
||||
#[strum(props(fallback = "Message deletion timer is set to %1$s hours."))]
|
||||
MsgEphemeralTimerHours = 94,
|
||||
|
||||
#[strum(props(fallback = "Message deletion timer is set to %1$s days."))]
|
||||
MsgEphemeralTimerDays = 95,
|
||||
|
||||
#[strum(props(fallback = "Message deletion timer is set to %1$s weeks."))]
|
||||
MsgEphemeralTimerWeeks = 96,
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
Reference in New Issue
Block a user