From a5c09cdf207711620f61e6efe645a2b20f6ec6c2 Mon Sep 17 00:00:00 2001 From: "B. Petersen" Date: Tue, 15 Nov 2022 01:13:41 +0100 Subject: [PATCH] use ByContact enum also for ephemeral stock strings --- src/ephemeral.rs | 36 ++++---- src/receive_imf.rs | 12 ++- src/stock_str.rs | 215 ++++++++++++++++++++++++++++++--------------- 3 files changed, 176 insertions(+), 87 deletions(-) diff --git a/src/ephemeral.rs b/src/ephemeral.rs index 18e1be385..c10fd3150 100644 --- a/src/ephemeral.rs +++ b/src/ephemeral.rs @@ -83,6 +83,7 @@ use crate::message::{Message, MessageState, MsgId, Viewtype}; use crate::mimeparser::SystemMessage; use crate::sql::{self, params_iter}; use crate::stock_str; +use crate::stock_str::ByContact; use crate::tools::{duration_to_str, time}; use std::cmp::max; @@ -204,7 +205,7 @@ impl ChatId { } self.inner_set_ephemeral_timer(context, timer).await?; let mut msg = Message::new(Viewtype::Text); - msg.text = Some(stock_ephemeral_timer_changed(context, timer, ContactId::SELF).await); + msg.text = Some(stock_ephemeral_timer_changed(context, timer, ByContact::SelfName).await); msg.param.set_cmd(SystemMessage::EphemeralTimerChanged); if let Err(err) = send_msg(context, self, &mut msg).await { error!( @@ -220,7 +221,7 @@ impl ChatId { pub(crate) async fn stock_ephemeral_timer_changed( context: &Context, timer: Timer, - from_id: ContactId, + from_id: ByContact, ) -> String { match timer { Timer::Disabled => stock_str::msg_ephemeral_timer_disabled(context, from_id).await, @@ -637,7 +638,12 @@ mod tests { let context = TestContext::new().await; assert_eq!( - stock_ephemeral_timer_changed(&context, Timer::Disabled, ContactId::SELF).await, + stock_ephemeral_timer_changed( + &context, + Timer::Disabled, + ByContact::YouOrName(ContactId::SELF) + ) + .await, "You disabled message deletion timer." ); @@ -645,7 +651,7 @@ mod tests { stock_ephemeral_timer_changed( &context, Timer::Enabled { duration: 1 }, - ContactId::SELF + ByContact::YouOrName(ContactId::SELF) ) .await, "You set message deletion timer to 1 s." @@ -654,7 +660,7 @@ mod tests { stock_ephemeral_timer_changed( &context, Timer::Enabled { duration: 30 }, - ContactId::SELF + ByContact::YouOrName(ContactId::SELF) ) .await, "You set message deletion timer to 30 s." @@ -663,7 +669,7 @@ mod tests { stock_ephemeral_timer_changed( &context, Timer::Enabled { duration: 60 }, - ContactId::SELF + ByContact::YouOrName(ContactId::SELF) ) .await, "You set message deletion timer to 1 minute." @@ -672,7 +678,7 @@ mod tests { stock_ephemeral_timer_changed( &context, Timer::Enabled { duration: 90 }, - ContactId::SELF + ByContact::YouOrName(ContactId::SELF) ) .await, "You set message deletion timer to 1.5 minutes." @@ -681,7 +687,7 @@ mod tests { stock_ephemeral_timer_changed( &context, Timer::Enabled { duration: 30 * 60 }, - ContactId::SELF + ByContact::YouOrName(ContactId::SELF) ) .await, "You set message deletion timer to 30 minutes." @@ -690,7 +696,7 @@ mod tests { stock_ephemeral_timer_changed( &context, Timer::Enabled { duration: 60 * 60 }, - ContactId::SELF + ByContact::YouOrName(ContactId::SELF) ) .await, "You set message deletion timer to 1 hour." @@ -699,7 +705,7 @@ mod tests { stock_ephemeral_timer_changed( &context, Timer::Enabled { duration: 5400 }, - ContactId::SELF + ByContact::YouOrName(ContactId::SELF) ) .await, "You set message deletion timer to 1.5 hours." @@ -710,7 +716,7 @@ mod tests { Timer::Enabled { duration: 2 * 60 * 60 }, - ContactId::SELF + ByContact::YouOrName(ContactId::SELF) ) .await, "You set message deletion timer to 2 hours." @@ -721,7 +727,7 @@ mod tests { Timer::Enabled { duration: 24 * 60 * 60 }, - ContactId::SELF + ByContact::YouOrName(ContactId::SELF) ) .await, "You set message deletion timer to 1 day." @@ -732,7 +738,7 @@ mod tests { Timer::Enabled { duration: 2 * 24 * 60 * 60 }, - ContactId::SELF + ByContact::YouOrName(ContactId::SELF) ) .await, "You set message deletion timer to 2 days." @@ -743,7 +749,7 @@ mod tests { Timer::Enabled { duration: 7 * 24 * 60 * 60 }, - ContactId::SELF + ByContact::YouOrName(ContactId::SELF) ) .await, "You set message deletion timer to 1 week." @@ -754,7 +760,7 @@ mod tests { Timer::Enabled { duration: 4 * 7 * 24 * 60 * 60 }, - ContactId::SELF + ByContact::YouOrName(ContactId::SELF) ) .await, "You set message deletion timer to 4 weeks." diff --git a/src/receive_imf.rs b/src/receive_imf.rs index a2df684fc..2a6b4cbf0 100644 --- a/src/receive_imf.rs +++ b/src/receive_imf.rs @@ -934,7 +934,12 @@ async fn add_parts( chat::add_info_msg( context, chat_id, - &stock_ephemeral_timer_changed(context, ephemeral_timer, from_id).await, + &stock_ephemeral_timer_changed( + context, + ephemeral_timer, + ByContact::YouOrName(from_id), + ) + .await, sort_timestamp, ) .await?; @@ -949,7 +954,10 @@ async fn add_parts( } if mime_parser.is_system_message == SystemMessage::EphemeralTimerChanged { - better_msg = Some(stock_ephemeral_timer_changed(context, ephemeral_timer, from_id).await); + better_msg = Some( + stock_ephemeral_timer_changed(context, ephemeral_timer, ByContact::YouOrName(from_id)) + .await, + ); // Do not delete the system message itself. // diff --git a/src/stock_str.rs b/src/stock_str.rs index 5db005e4d..bea880ada 100644 --- a/src/stock_str.rs +++ b/src/stock_str.rs @@ -944,14 +944,21 @@ pub(crate) async fn failed_sending_to(context: &Context, name: impl AsRef) /// Stock string: `Message deletion timer is disabled.`. pub(crate) async fn msg_ephemeral_timer_disabled( context: &Context, - by_contact: ContactId, + by_contact: ByContact, ) -> String { - if by_contact == ContactId::SELF { - translated(context, StockMessage::MsgYouDisabledEphemeralTimer).await - } else { - translated(context, StockMessage::MsgEphemeralTimerDisabledBy) + match by_contact { + ByContact::YouOrName(by_contact) => { + if by_contact == ContactId::SELF { + translated(context, StockMessage::MsgYouDisabledEphemeralTimer).await + } else { + translated(context, StockMessage::MsgEphemeralTimerDisabledBy) + .await + .replace1(by_contact.get_stock_name(context).await) + } + } + ByContact::SelfName => translated(context, StockMessage::MsgEphemeralTimerDisabledBy) .await - .replace1(by_contact.get_stock_name(context).await) + .replace1(context.get_config_self_name().await), } } @@ -959,61 +966,97 @@ pub(crate) async fn msg_ephemeral_timer_disabled( pub(crate) async fn msg_ephemeral_timer_enabled( context: &Context, timer: impl AsRef, - by_contact: ContactId, + by_contact: ByContact, ) -> String { - if by_contact == ContactId::SELF { - translated(context, StockMessage::MsgYouEnabledEphemeralTimer) + match by_contact { + ByContact::YouOrName(by_contact) => { + if by_contact == ContactId::SELF { + translated(context, StockMessage::MsgYouEnabledEphemeralTimer) + .await + .replace1(timer) + } else { + translated(context, StockMessage::MsgEphemeralTimerEnabledBy) + .await + .replace1(timer) + .replace2(by_contact.get_stock_name(context).await) + } + } + ByContact::SelfName => translated(context, StockMessage::MsgEphemeralTimerEnabledBy) .await .replace1(timer) - } else { - translated(context, StockMessage::MsgEphemeralTimerEnabledBy) - .await - .replace1(timer) - .replace2(by_contact.get_stock_name(context).await) + .replace2(context.get_config_self_name().await), } } /// Stock string: `Message deletion timer is set to 1 minute.`. -pub(crate) async fn msg_ephemeral_timer_minute(context: &Context, by_contact: ContactId) -> String { - if by_contact == ContactId::SELF { - translated(context, StockMessage::MsgYouEphemeralTimerMinute).await - } else { - translated(context, StockMessage::MsgEphemeralTimerMinuteBy) +pub(crate) async fn msg_ephemeral_timer_minute(context: &Context, by_contact: ByContact) -> String { + match by_contact { + ByContact::YouOrName(by_contact) => { + if by_contact == ContactId::SELF { + translated(context, StockMessage::MsgYouEphemeralTimerMinute).await + } else { + translated(context, StockMessage::MsgEphemeralTimerMinuteBy) + .await + .replace1(by_contact.get_stock_name(context).await) + } + } + ByContact::SelfName => translated(context, StockMessage::MsgEphemeralTimerMinuteBy) .await - .replace1(by_contact.get_stock_name(context).await) + .replace1(context.get_config_self_name().await), } } /// Stock string: `Message deletion timer is set to 1 hour.`. -pub(crate) async fn msg_ephemeral_timer_hour(context: &Context, by_contact: ContactId) -> String { - if by_contact == ContactId::SELF { - translated(context, StockMessage::MsgYouEphemeralTimerHour).await - } else { - translated(context, StockMessage::MsgEphemeralTimerHourBy) +pub(crate) async fn msg_ephemeral_timer_hour(context: &Context, by_contact: ByContact) -> String { + match by_contact { + ByContact::YouOrName(by_contact) => { + if by_contact == ContactId::SELF { + translated(context, StockMessage::MsgYouEphemeralTimerHour).await + } else { + translated(context, StockMessage::MsgEphemeralTimerHourBy) + .await + .replace1(by_contact.get_stock_name(context).await) + } + } + ByContact::SelfName => translated(context, StockMessage::MsgEphemeralTimerHourBy) .await - .replace1(by_contact.get_stock_name(context).await) + .replace1(context.get_config_self_name().await), } } /// Stock string: `Message deletion timer is set to 1 day.`. -pub(crate) async fn msg_ephemeral_timer_day(context: &Context, by_contact: ContactId) -> String { - if by_contact == ContactId::SELF { - translated(context, StockMessage::MsgYouEphemeralTimerDay).await - } else { - translated(context, StockMessage::MsgEphemeralTimerDayBy) +pub(crate) async fn msg_ephemeral_timer_day(context: &Context, by_contact: ByContact) -> String { + match by_contact { + ByContact::YouOrName(by_contact) => { + if by_contact == ContactId::SELF { + translated(context, StockMessage::MsgYouEphemeralTimerDay).await + } else { + translated(context, StockMessage::MsgEphemeralTimerDayBy) + .await + .replace1(by_contact.get_stock_name(context).await) + } + } + ByContact::SelfName => translated(context, StockMessage::MsgEphemeralTimerDayBy) .await - .replace1(by_contact.get_stock_name(context).await) + .replace1(context.get_config_self_name().await), } } /// Stock string: `Message deletion timer is set to 1 week.`. -pub(crate) async fn msg_ephemeral_timer_week(context: &Context, by_contact: ContactId) -> String { - if by_contact == ContactId::SELF { - translated(context, StockMessage::MsgYouEphemeralTimerWeek).await - } else { - translated(context, StockMessage::MsgEphemeralTimerWeekBy) +pub(crate) async fn msg_ephemeral_timer_week(context: &Context, by_contact: ByContact) -> String { + match by_contact { + ByContact::YouOrName(by_contact) => { + if by_contact == ContactId::SELF { + translated(context, StockMessage::MsgYouEphemeralTimerWeek).await + } else { + translated(context, StockMessage::MsgEphemeralTimerWeekBy) + .await + .replace1(by_contact.get_stock_name(context).await) + } + } + ByContact::SelfName => translated(context, StockMessage::MsgEphemeralTimerWeekBy) .await - .replace1(by_contact.get_stock_name(context).await) + .replace1(context.get_config_self_name().await), } } @@ -1095,17 +1138,25 @@ pub(crate) async fn delete_server_turned_off(context: &Context) -> String { pub(crate) async fn msg_ephemeral_timer_minutes( context: &Context, minutes: impl AsRef, - by_contact: ContactId, + by_contact: ByContact, ) -> String { - if by_contact == ContactId::SELF { - translated(context, StockMessage::MsgYouEphemeralTimerMinutes) + match by_contact { + ByContact::YouOrName(by_contact) => { + if by_contact == ContactId::SELF { + translated(context, StockMessage::MsgYouEphemeralTimerMinutes) + .await + .replace1(minutes) + } else { + translated(context, StockMessage::MsgEphemeralTimerMinutesBy) + .await + .replace1(minutes) + .replace2(by_contact.get_stock_name(context).await) + } + } + ByContact::SelfName => translated(context, StockMessage::MsgEphemeralTimerMinutesBy) .await .replace1(minutes) - } else { - translated(context, StockMessage::MsgEphemeralTimerMinutesBy) - .await - .replace1(minutes) - .replace2(by_contact.get_stock_name(context).await) + .replace2(context.get_config_self_name().await), } } @@ -1113,17 +1164,25 @@ pub(crate) async fn msg_ephemeral_timer_minutes( pub(crate) async fn msg_ephemeral_timer_hours( context: &Context, hours: impl AsRef, - by_contact: ContactId, + by_contact: ByContact, ) -> String { - if by_contact == ContactId::SELF { - translated(context, StockMessage::MsgYouEphemeralTimerHours) + match by_contact { + ByContact::YouOrName(by_contact) => { + if by_contact == ContactId::SELF { + translated(context, StockMessage::MsgYouEphemeralTimerHours) + .await + .replace1(hours) + } else { + translated(context, StockMessage::MsgEphemeralTimerHoursBy) + .await + .replace1(hours) + .replace2(by_contact.get_stock_name(context).await) + } + } + ByContact::SelfName => translated(context, StockMessage::MsgEphemeralTimerHoursBy) .await .replace1(hours) - } else { - translated(context, StockMessage::MsgEphemeralTimerHoursBy) - .await - .replace1(hours) - .replace2(by_contact.get_stock_name(context).await) + .replace2(context.get_config_self_name().await), } } @@ -1131,17 +1190,25 @@ pub(crate) async fn msg_ephemeral_timer_hours( pub(crate) async fn msg_ephemeral_timer_days( context: &Context, days: impl AsRef, - by_contact: ContactId, + by_contact: ByContact, ) -> String { - if by_contact == ContactId::SELF { - translated(context, StockMessage::MsgYouEphemeralTimerDays) + match by_contact { + ByContact::YouOrName(by_contact) => { + if by_contact == ContactId::SELF { + translated(context, StockMessage::MsgYouEphemeralTimerDays) + .await + .replace1(days) + } else { + translated(context, StockMessage::MsgEphemeralTimerDaysBy) + .await + .replace1(days) + .replace2(by_contact.get_stock_name(context).await) + } + } + ByContact::SelfName => translated(context, StockMessage::MsgEphemeralTimerDaysBy) .await .replace1(days) - } else { - translated(context, StockMessage::MsgEphemeralTimerDaysBy) - .await - .replace1(days) - .replace2(by_contact.get_stock_name(context).await) + .replace2(context.get_config_self_name().await), } } @@ -1149,17 +1216,25 @@ pub(crate) async fn msg_ephemeral_timer_days( pub(crate) async fn msg_ephemeral_timer_weeks( context: &Context, weeks: impl AsRef, - by_contact: ContactId, + by_contact: ByContact, ) -> String { - if by_contact == ContactId::SELF { - translated(context, StockMessage::MsgYouEphemeralTimerWeeks) + match by_contact { + ByContact::YouOrName(by_contact) => { + if by_contact == ContactId::SELF { + translated(context, StockMessage::MsgYouEphemeralTimerWeeks) + .await + .replace1(weeks) + } else { + translated(context, StockMessage::MsgEphemeralTimerWeeksBy) + .await + .replace1(weeks) + .replace2(by_contact.get_stock_name(context).await) + } + } + ByContact::SelfName => translated(context, StockMessage::MsgEphemeralTimerWeeksBy) .await .replace1(weeks) - } else { - translated(context, StockMessage::MsgEphemeralTimerWeeksBy) - .await - .replace1(weeks) - .replace2(by_contact.get_stock_name(context).await) + .replace2(context.get_config_self_name().await), } }