diff --git a/CMakeLists.txt b/CMakeLists.txt index 421ded394..6d98676c8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -320,6 +320,7 @@ set(TDLIB_SOURCE td/telegram/DialogId.cpp td/telegram/DialogInviteLink.cpp td/telegram/DialogLocation.cpp + td/telegram/DialogNotificationSettings.cpp td/telegram/DialogParticipant.cpp td/telegram/DialogParticipantFilter.cpp td/telegram/DialogSource.cpp @@ -403,7 +404,7 @@ set(TDLIB_SOURCE td/telegram/net/SessionMultiProxy.cpp td/telegram/NewPasswordState.cpp td/telegram/NotificationManager.cpp - td/telegram/NotificationSettings.cpp + td/telegram/NotificationSettingsScope.cpp td/telegram/NotificationSettingsManager.cpp td/telegram/NotificationSound.cpp td/telegram/NotificationType.cpp @@ -424,6 +425,7 @@ set(TDLIB_SOURCE td/telegram/ReplyMarkup.cpp td/telegram/ReportReason.cpp td/telegram/RestrictionReason.cpp + td/telegram/ScopeNotificationSettings.cpp td/telegram/SecretChatActor.cpp td/telegram/SecretChatDb.cpp td/telegram/SecretChatsManager.cpp @@ -535,6 +537,7 @@ set(TDLIB_SOURCE td/telegram/DialogInviteLink.h td/telegram/DialogListId.h td/telegram/DialogLocation.h + td/telegram/DialogNotificationSettings.h td/telegram/DialogParticipant.h td/telegram/DialogParticipantFilter.h td/telegram/DialogSource.h @@ -647,7 +650,7 @@ set(TDLIB_SOURCE td/telegram/NotificationGroupType.h td/telegram/NotificationId.h td/telegram/NotificationManager.h - td/telegram/NotificationSettings.h + td/telegram/NotificationSettingsScope.h td/telegram/NotificationSettingsManager.h td/telegram/NotificationSound.h td/telegram/NotificationSoundType.h @@ -675,6 +678,7 @@ set(TDLIB_SOURCE td/telegram/RequestActor.h td/telegram/RestrictionReason.h td/telegram/ScheduledServerMessageId.h + td/telegram/ScopeNotificationSettings.h td/telegram/SecretChatActor.h td/telegram/SecretChatId.h td/telegram/SecretChatDb.h @@ -723,6 +727,7 @@ set(TDLIB_SOURCE td/telegram/AudiosManager.hpp td/telegram/AuthManager.hpp td/telegram/BackgroundType.hpp + td/telegram/DialogNotificationSettings.hpp td/telegram/DialogFilter.hpp td/telegram/Dimensions.hpp td/telegram/Document.hpp @@ -742,7 +747,6 @@ set(TDLIB_SOURCE td/telegram/MessageReaction.hpp td/telegram/MessageReplyInfo.hpp td/telegram/MinChannel.hpp - td/telegram/NotificationSettings.hpp td/telegram/OrderInfo.hpp td/telegram/Photo.hpp td/telegram/PhotoSize.hpp @@ -751,6 +755,7 @@ set(TDLIB_SOURCE td/telegram/PollManager.hpp td/telegram/PremiumGiftOption.hpp td/telegram/ReplyMarkup.hpp + td/telegram/ScopeNotificationSettings.hpp td/telegram/SecureValue.hpp td/telegram/SendCodeHelper.hpp td/telegram/StickerSetId.hpp diff --git a/td/telegram/DialogNotificationSettings.cpp b/td/telegram/DialogNotificationSettings.cpp new file mode 100644 index 000000000..fc1d6d398 --- /dev/null +++ b/td/telegram/DialogNotificationSettings.cpp @@ -0,0 +1,102 @@ +// +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022 +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +#include "td/telegram/DialogNotificationSettings.h" + +#include "td/telegram/Global.h" + +#include + +namespace td { + +StringBuilder &operator<<(StringBuilder &string_builder, const DialogNotificationSettings ¬ification_settings) { + return string_builder << "[" << notification_settings.mute_until << ", " << notification_settings.sound << ", " + << notification_settings.show_preview << ", " << notification_settings.silent_send_message + << ", " << notification_settings.disable_pinned_message_notifications << ", " + << notification_settings.disable_mention_notifications << ", " + << notification_settings.use_default_mute_until << ", " + << notification_settings.use_default_show_preview << ", " + << notification_settings.use_default_disable_pinned_message_notifications << ", " + << notification_settings.use_default_disable_mention_notifications << ", " + << notification_settings.is_synchronized << "]"; +} + +td_api::object_ptr get_chat_notification_settings_object( + const DialogNotificationSettings *notification_settings) { + CHECK(notification_settings != nullptr); + return td_api::make_object( + notification_settings->use_default_mute_until, max(0, notification_settings->mute_until - G()->unix_time()), + is_notification_sound_default(notification_settings->sound), + get_notification_sound_ringtone_id(notification_settings->sound), notification_settings->use_default_show_preview, + notification_settings->show_preview, notification_settings->use_default_disable_pinned_message_notifications, + notification_settings->disable_pinned_message_notifications, + notification_settings->use_default_disable_mention_notifications, + notification_settings->disable_mention_notifications); +} + +static int32 get_mute_until(int32 mute_for) { + if (mute_for <= 0) { + return 0; + } + + const int32 MAX_PRECISE_MUTE_FOR = 366 * 86400; + int32 current_time = G()->unix_time(); + if (mute_for > MAX_PRECISE_MUTE_FOR || mute_for >= std::numeric_limits::max() - current_time) { + return std::numeric_limits::max(); + } + return mute_for + current_time; +} + +Result get_dialog_notification_settings( + td_api::object_ptr &¬ification_settings, bool old_silent_send_message) { + if (notification_settings == nullptr) { + return Status::Error(400, "New notification settings must be non-empty"); + } + + int32 mute_until = + notification_settings->use_default_mute_for_ ? 0 : get_mute_until(notification_settings->mute_for_); + return DialogNotificationSettings( + notification_settings->use_default_mute_for_, mute_until, + get_notification_sound(notification_settings->use_default_sound_, notification_settings->sound_id_), + notification_settings->use_default_show_preview_, notification_settings->show_preview_, old_silent_send_message, + notification_settings->use_default_disable_pinned_message_notifications_, + notification_settings->disable_pinned_message_notifications_, + notification_settings->use_default_disable_mention_notifications_, + notification_settings->disable_mention_notifications_); +} + +DialogNotificationSettings get_dialog_notification_settings(tl_object_ptr &&settings, + bool old_use_default_disable_pinned_message_notifications, + bool old_disable_pinned_message_notifications, + bool old_use_default_disable_mention_notifications, + bool old_disable_mention_notifications) { + if (settings == nullptr) { + return DialogNotificationSettings(); + } + bool use_default_mute_until = (settings->flags_ & telegram_api::peerNotifySettings::MUTE_UNTIL_MASK) == 0; + bool use_default_show_preview = (settings->flags_ & telegram_api::peerNotifySettings::SHOW_PREVIEWS_MASK) == 0; + auto mute_until = use_default_mute_until || settings->mute_until_ <= G()->unix_time() ? 0 : settings->mute_until_; + bool silent_send_message = + (settings->flags_ & telegram_api::peerNotifySettings::SILENT_MASK) == 0 ? false : settings->silent_; + return {use_default_mute_until, + mute_until, + get_notification_sound(settings.get()), + use_default_show_preview, + settings->show_previews_, + silent_send_message, + old_use_default_disable_pinned_message_notifications, + old_disable_pinned_message_notifications, + old_use_default_disable_mention_notifications, + old_disable_mention_notifications}; +} + +bool are_default_dialog_notification_settings(const DialogNotificationSettings &settings, bool compare_sound) { + return settings.use_default_mute_until && (!compare_sound || is_notification_sound_default(settings.sound)) && + settings.use_default_show_preview && settings.use_default_disable_pinned_message_notifications && + settings.use_default_disable_mention_notifications; +} + +} // namespace td diff --git a/td/telegram/NotificationSettings.h b/td/telegram/DialogNotificationSettings.h similarity index 61% rename from td/telegram/NotificationSettings.h rename to td/telegram/DialogNotificationSettings.h index 0fc0d6170..3222f2a8a 100644 --- a/td/telegram/NotificationSettings.h +++ b/td/telegram/DialogNotificationSettings.h @@ -55,68 +55,20 @@ class DialogNotificationSettings { } }; -enum class NotificationSettingsScope : int32 { Private, Group, Channel }; - -class ScopeNotificationSettings { - public: - int32 mute_until = 0; - unique_ptr sound; - bool show_preview = true; - bool is_synchronized = false; - - // local settings - bool disable_pinned_message_notifications = false; - bool disable_mention_notifications = false; - - ScopeNotificationSettings() = default; - - ScopeNotificationSettings(int32 mute_until, unique_ptr &&sound, bool show_preview, - bool disable_pinned_message_notifications, bool disable_mention_notifications) - : mute_until(mute_until) - , sound(std::move(sound)) - , show_preview(show_preview) - , is_synchronized(true) - , disable_pinned_message_notifications(disable_pinned_message_notifications) - , disable_mention_notifications(disable_mention_notifications) { - } -}; - StringBuilder &operator<<(StringBuilder &string_builder, const DialogNotificationSettings ¬ification_settings); -StringBuilder &operator<<(StringBuilder &string_builder, NotificationSettingsScope scope); - -StringBuilder &operator<<(StringBuilder &string_builder, const ScopeNotificationSettings ¬ification_settings); - -td_api::object_ptr get_notification_settings_scope_object( - NotificationSettingsScope scope); - td_api::object_ptr get_chat_notification_settings_object( const DialogNotificationSettings *notification_settings); -td_api::object_ptr get_scope_notification_settings_object( - const ScopeNotificationSettings *notification_settings); - -telegram_api::object_ptr get_input_notify_peer(NotificationSettingsScope scope); - -NotificationSettingsScope get_notification_settings_scope( - const td_api::object_ptr &scope); - Result get_dialog_notification_settings( td_api::object_ptr &¬ification_settings, bool old_silent_send_message); -Result get_scope_notification_settings( - td_api::object_ptr &¬ification_settings); - DialogNotificationSettings get_dialog_notification_settings(tl_object_ptr &&settings, bool old_use_default_disable_pinned_message_notifications, bool old_disable_pinned_message_notifications, bool old_use_default_disable_mention_notifications, bool old_disable_mention_notifications); -ScopeNotificationSettings get_scope_notification_settings(tl_object_ptr &&settings, - bool old_disable_pinned_message_notifications, - bool old_disable_mention_notifications); - bool are_default_dialog_notification_settings(const DialogNotificationSettings &settings, bool compare_sound); } // namespace td diff --git a/td/telegram/NotificationSettings.hpp b/td/telegram/DialogNotificationSettings.hpp similarity index 65% rename from td/telegram/NotificationSettings.hpp rename to td/telegram/DialogNotificationSettings.hpp index 2b66bf1dc..23be246f5 100644 --- a/td/telegram/NotificationSettings.hpp +++ b/td/telegram/DialogNotificationSettings.hpp @@ -6,8 +6,8 @@ // #pragma once +#include "td/telegram/DialogNotificationSettings.h" #include "td/telegram/Global.h" -#include "td/telegram/NotificationSettings.h" #include "td/telegram/NotificationSound.h" #include "td/utils/tl_helpers.h" @@ -86,58 +86,4 @@ void parse(DialogNotificationSettings ¬ification_settings, ParserT &parser) { } } -template -void store(const ScopeNotificationSettings ¬ification_settings, StorerT &storer) { - bool is_muted = notification_settings.mute_until != 0 && notification_settings.mute_until > G()->unix_time(); - bool has_sound = notification_settings.sound != nullptr; - bool has_ringtone_support = true; - BEGIN_STORE_FLAGS(); - STORE_FLAG(is_muted); - STORE_FLAG(has_sound); - STORE_FLAG(notification_settings.show_preview); - STORE_FLAG(false); - STORE_FLAG(notification_settings.is_synchronized); - STORE_FLAG(notification_settings.disable_pinned_message_notifications); - STORE_FLAG(notification_settings.disable_mention_notifications); - STORE_FLAG(has_ringtone_support); - END_STORE_FLAGS(); - if (is_muted) { - store(notification_settings.mute_until, storer); - } - if (has_sound) { - store(notification_settings.sound, storer); - } -} - -template -void parse(ScopeNotificationSettings ¬ification_settings, ParserT &parser) { - bool is_muted; - bool has_sound; - bool silent_send_message_ignored; - bool has_ringtone_support; - BEGIN_PARSE_FLAGS(); - PARSE_FLAG(is_muted); - PARSE_FLAG(has_sound); - PARSE_FLAG(notification_settings.show_preview); - PARSE_FLAG(silent_send_message_ignored); - PARSE_FLAG(notification_settings.is_synchronized); - PARSE_FLAG(notification_settings.disable_pinned_message_notifications); - PARSE_FLAG(notification_settings.disable_mention_notifications); - PARSE_FLAG(has_ringtone_support); - END_PARSE_FLAGS(); - (void)silent_send_message_ignored; - if (is_muted) { - parse(notification_settings.mute_until, parser); - } - if (has_sound) { - if (has_ringtone_support) { - parse_notification_sound(notification_settings.sound, parser); - } else { - string sound; - parse(sound, parser); - notification_settings.sound = get_legacy_notification_sound(sound); - } - } -} - } // namespace td diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index 534b14212..1b97f958f 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -17,6 +17,7 @@ #include "td/telegram/DialogFilter.h" #include "td/telegram/DialogFilter.hpp" #include "td/telegram/DialogLocation.h" +#include "td/telegram/DialogNotificationSettings.hpp" #include "td/telegram/DownloadManager.h" #include "td/telegram/DraftMessage.h" #include "td/telegram/DraftMessage.hpp" @@ -45,7 +46,6 @@ #include "td/telegram/net/NetQuery.h" #include "td/telegram/NotificationGroupType.h" #include "td/telegram/NotificationManager.h" -#include "td/telegram/NotificationSettings.hpp" #include "td/telegram/NotificationSettingsManager.h" #include "td/telegram/NotificationSound.h" #include "td/telegram/NotificationType.h" diff --git a/td/telegram/MessagesManager.h b/td/telegram/MessagesManager.h index 7b18079b7..93ec24da3 100644 --- a/td/telegram/MessagesManager.h +++ b/td/telegram/MessagesManager.h @@ -17,6 +17,7 @@ #include "td/telegram/DialogId.h" #include "td/telegram/DialogListId.h" #include "td/telegram/DialogLocation.h" +#include "td/telegram/DialogNotificationSettings.h" #include "td/telegram/DialogParticipant.h" #include "td/telegram/DialogSource.h" #include "td/telegram/EncryptedFile.h" @@ -44,7 +45,7 @@ #include "td/telegram/NotificationGroupKey.h" #include "td/telegram/NotificationGroupType.h" #include "td/telegram/NotificationId.h" -#include "td/telegram/NotificationSettings.h" +#include "td/telegram/NotificationSettingsScope.h" #include "td/telegram/Photo.h" #include "td/telegram/RecentDialogList.h" #include "td/telegram/ReplyMarkup.h" diff --git a/td/telegram/NotificationSettings.cpp b/td/telegram/NotificationSettings.cpp deleted file mode 100644 index 7ee1962d2..000000000 --- a/td/telegram/NotificationSettings.cpp +++ /dev/null @@ -1,221 +0,0 @@ -// -// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022 -// -// Distributed under the Boost Software License, Version 1.0. (See accompanying -// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -// -#include "td/telegram/NotificationSettings.h" - -#include "td/telegram/Global.h" - -#include "td/utils/common.h" - -#include - -namespace td { - -StringBuilder &operator<<(StringBuilder &string_builder, const DialogNotificationSettings ¬ification_settings) { - return string_builder << "[" << notification_settings.mute_until << ", " << notification_settings.sound << ", " - << notification_settings.show_preview << ", " << notification_settings.silent_send_message - << ", " << notification_settings.disable_pinned_message_notifications << ", " - << notification_settings.disable_mention_notifications << ", " - << notification_settings.use_default_mute_until << ", " - << notification_settings.use_default_show_preview << ", " - << notification_settings.use_default_disable_pinned_message_notifications << ", " - << notification_settings.use_default_disable_mention_notifications << ", " - << notification_settings.is_synchronized << "]"; -} - -StringBuilder &operator<<(StringBuilder &string_builder, NotificationSettingsScope scope) { - switch (scope) { - case NotificationSettingsScope::Private: - return string_builder << "notification settings for private chats"; - case NotificationSettingsScope::Group: - return string_builder << "notification settings for group chats"; - case NotificationSettingsScope::Channel: - return string_builder << "notification settings for channel chats"; - default: - UNREACHABLE(); - return string_builder; - } -} - -StringBuilder &operator<<(StringBuilder &string_builder, const ScopeNotificationSettings ¬ification_settings) { - return string_builder << "[" << notification_settings.mute_until << ", " << notification_settings.sound << ", " - << notification_settings.show_preview << ", " << notification_settings.is_synchronized << ", " - << notification_settings.disable_pinned_message_notifications << ", " - << notification_settings.disable_mention_notifications << "]"; -} - -td_api::object_ptr get_notification_settings_scope_object( - NotificationSettingsScope scope) { - switch (scope) { - case NotificationSettingsScope::Private: - return td_api::make_object(); - case NotificationSettingsScope::Group: - return td_api::make_object(); - case NotificationSettingsScope::Channel: - return td_api::make_object(); - default: - UNREACHABLE(); - return nullptr; - } -} - -td_api::object_ptr get_chat_notification_settings_object( - const DialogNotificationSettings *notification_settings) { - CHECK(notification_settings != nullptr); - return td_api::make_object( - notification_settings->use_default_mute_until, max(0, notification_settings->mute_until - G()->unix_time()), - is_notification_sound_default(notification_settings->sound), - get_notification_sound_ringtone_id(notification_settings->sound), notification_settings->use_default_show_preview, - notification_settings->show_preview, notification_settings->use_default_disable_pinned_message_notifications, - notification_settings->disable_pinned_message_notifications, - notification_settings->use_default_disable_mention_notifications, - notification_settings->disable_mention_notifications); -} - -td_api::object_ptr get_scope_notification_settings_object( - const ScopeNotificationSettings *notification_settings) { - CHECK(notification_settings != nullptr); - return td_api::make_object( - max(0, notification_settings->mute_until - G()->unix_time()), - get_notification_sound_ringtone_id(notification_settings->sound), notification_settings->show_preview, - notification_settings->disable_pinned_message_notifications, - notification_settings->disable_mention_notifications); -} - -telegram_api::object_ptr get_input_notify_peer(NotificationSettingsScope scope) { - switch (scope) { - case NotificationSettingsScope::Private: - return telegram_api::make_object(); - case NotificationSettingsScope::Group: - return telegram_api::make_object(); - case NotificationSettingsScope::Channel: - return telegram_api::make_object(); - default: - return nullptr; - } -} - -NotificationSettingsScope get_notification_settings_scope( - const td_api::object_ptr &scope) { - CHECK(scope != nullptr); - switch (scope->get_id()) { - case td_api::notificationSettingsScopePrivateChats::ID: - return NotificationSettingsScope::Private; - case td_api::notificationSettingsScopeGroupChats::ID: - return NotificationSettingsScope::Group; - case td_api::notificationSettingsScopeChannelChats::ID: - return NotificationSettingsScope::Channel; - default: - UNREACHABLE(); - return NotificationSettingsScope::Private; - } -} - -static int32 get_mute_until(int32 mute_for) { - if (mute_for <= 0) { - return 0; - } - - const int32 MAX_PRECISE_MUTE_FOR = 366 * 86400; - int32 current_time = G()->unix_time(); - if (mute_for > MAX_PRECISE_MUTE_FOR || mute_for >= std::numeric_limits::max() - current_time) { - return std::numeric_limits::max(); - } - return mute_for + current_time; -} - -Result get_dialog_notification_settings( - td_api::object_ptr &¬ification_settings, bool old_silent_send_message) { - if (notification_settings == nullptr) { - return Status::Error(400, "New notification settings must be non-empty"); - } - - int32 mute_until = - notification_settings->use_default_mute_for_ ? 0 : get_mute_until(notification_settings->mute_for_); - return DialogNotificationSettings( - notification_settings->use_default_mute_for_, mute_until, - get_notification_sound(notification_settings->use_default_sound_, notification_settings->sound_id_), - notification_settings->use_default_show_preview_, notification_settings->show_preview_, old_silent_send_message, - notification_settings->use_default_disable_pinned_message_notifications_, - notification_settings->disable_pinned_message_notifications_, - notification_settings->use_default_disable_mention_notifications_, - notification_settings->disable_mention_notifications_); -} - -Result get_scope_notification_settings( - td_api::object_ptr &¬ification_settings) { - if (notification_settings == nullptr) { - return Status::Error(400, "New notification settings must be non-empty"); - } - - auto mute_until = get_mute_until(notification_settings->mute_for_); - return ScopeNotificationSettings(mute_until, get_notification_sound(false, notification_settings->sound_id_), - notification_settings->show_preview_, - notification_settings->disable_pinned_message_notifications_, - notification_settings->disable_mention_notifications_); -} - -static unique_ptr get_peer_notification_sound( - tl_object_ptr &settings) { - telegram_api::NotificationSound *sound = -#if TD_ANDROID - settings->android_sound_.get(); -#elif TD_DARWIN_IOS || TD_DARWIN_TV_OS || TD_DARWIN_WATCH_OS - settings->ios_sound_.get(); -#else - settings->other_sound_.get(); -#endif - return get_notification_sound(sound); -} - -DialogNotificationSettings get_dialog_notification_settings(tl_object_ptr &&settings, - bool old_use_default_disable_pinned_message_notifications, - bool old_disable_pinned_message_notifications, - bool old_use_default_disable_mention_notifications, - bool old_disable_mention_notifications) { - if (settings == nullptr) { - return DialogNotificationSettings(); - } - bool use_default_mute_until = (settings->flags_ & telegram_api::peerNotifySettings::MUTE_UNTIL_MASK) == 0; - bool use_default_show_preview = (settings->flags_ & telegram_api::peerNotifySettings::SHOW_PREVIEWS_MASK) == 0; - auto mute_until = use_default_mute_until || settings->mute_until_ <= G()->unix_time() ? 0 : settings->mute_until_; - bool silent_send_message = - (settings->flags_ & telegram_api::peerNotifySettings::SILENT_MASK) == 0 ? false : settings->silent_; - return {use_default_mute_until, - mute_until, - get_peer_notification_sound(settings), - use_default_show_preview, - settings->show_previews_, - silent_send_message, - old_use_default_disable_pinned_message_notifications, - old_disable_pinned_message_notifications, - old_use_default_disable_mention_notifications, - old_disable_mention_notifications}; -} - -ScopeNotificationSettings get_scope_notification_settings(tl_object_ptr &&settings, - bool old_disable_pinned_message_notifications, - bool old_disable_mention_notifications) { - if (settings == nullptr) { - return ScopeNotificationSettings(); - } - auto mute_until = (settings->flags_ & telegram_api::peerNotifySettings::MUTE_UNTIL_MASK) == 0 || - settings->mute_until_ <= G()->unix_time() - ? 0 - : settings->mute_until_; - auto show_preview = - (settings->flags_ & telegram_api::peerNotifySettings::SHOW_PREVIEWS_MASK) == 0 ? false : settings->show_previews_; - return {mute_until, get_peer_notification_sound(settings), show_preview, old_disable_pinned_message_notifications, - old_disable_mention_notifications}; -} - -bool are_default_dialog_notification_settings(const DialogNotificationSettings &settings, bool compare_sound) { - return settings.use_default_mute_until && (!compare_sound || is_notification_sound_default(settings.sound)) && - settings.use_default_show_preview && settings.use_default_disable_pinned_message_notifications && - settings.use_default_disable_mention_notifications; -} - -} // namespace td diff --git a/td/telegram/NotificationSettingsManager.cpp b/td/telegram/NotificationSettingsManager.cpp index e39528bb0..70ebae3bc 100644 --- a/td/telegram/NotificationSettingsManager.cpp +++ b/td/telegram/NotificationSettingsManager.cpp @@ -22,9 +22,9 @@ #include "td/telegram/logevent/LogEventHelper.h" #include "td/telegram/MessagesManager.h" #include "td/telegram/NotificationManager.h" -#include "td/telegram/NotificationSettings.hpp" #include "td/telegram/NotificationSound.h" #include "td/telegram/OptionManager.h" +#include "td/telegram/ScopeNotificationSettings.hpp" #include "td/telegram/Td.h" #include "td/telegram/TdDb.h" #include "td/telegram/telegram_api.h" diff --git a/td/telegram/NotificationSettingsManager.h b/td/telegram/NotificationSettingsManager.h index 3b02f3be1..14712d16f 100644 --- a/td/telegram/NotificationSettingsManager.h +++ b/td/telegram/NotificationSettingsManager.h @@ -7,9 +7,11 @@ #pragma once #include "td/telegram/DialogId.h" +#include "td/telegram/DialogNotificationSettings.h" #include "td/telegram/files/FileId.h" #include "td/telegram/files/FileSourceId.h" -#include "td/telegram/NotificationSettings.h" +#include "td/telegram/NotificationSettingsScope.h" +#include "td/telegram/ScopeNotificationSettings.h" #include "td/telegram/td_api.h" #include "td/telegram/telegram_api.h" diff --git a/td/telegram/NotificationSettingsScope.cpp b/td/telegram/NotificationSettingsScope.cpp new file mode 100644 index 000000000..ea1d68bed --- /dev/null +++ b/td/telegram/NotificationSettingsScope.cpp @@ -0,0 +1,69 @@ +// +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022 +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +#include "td/telegram/NotificationSettingsScope.h" + +namespace td { + +StringBuilder &operator<<(StringBuilder &string_builder, NotificationSettingsScope scope) { + switch (scope) { + case NotificationSettingsScope::Private: + return string_builder << "notification settings for private chats"; + case NotificationSettingsScope::Group: + return string_builder << "notification settings for group chats"; + case NotificationSettingsScope::Channel: + return string_builder << "notification settings for channel chats"; + default: + UNREACHABLE(); + return string_builder; + } +} + +td_api::object_ptr get_notification_settings_scope_object( + NotificationSettingsScope scope) { + switch (scope) { + case NotificationSettingsScope::Private: + return td_api::make_object(); + case NotificationSettingsScope::Group: + return td_api::make_object(); + case NotificationSettingsScope::Channel: + return td_api::make_object(); + default: + UNREACHABLE(); + return nullptr; + } +} + +telegram_api::object_ptr get_input_notify_peer(NotificationSettingsScope scope) { + switch (scope) { + case NotificationSettingsScope::Private: + return telegram_api::make_object(); + case NotificationSettingsScope::Group: + return telegram_api::make_object(); + case NotificationSettingsScope::Channel: + return telegram_api::make_object(); + default: + return nullptr; + } +} + +NotificationSettingsScope get_notification_settings_scope( + const td_api::object_ptr &scope) { + CHECK(scope != nullptr); + switch (scope->get_id()) { + case td_api::notificationSettingsScopePrivateChats::ID: + return NotificationSettingsScope::Private; + case td_api::notificationSettingsScopeGroupChats::ID: + return NotificationSettingsScope::Group; + case td_api::notificationSettingsScopeChannelChats::ID: + return NotificationSettingsScope::Channel; + default: + UNREACHABLE(); + return NotificationSettingsScope::Private; + } +} + +} // namespace td diff --git a/td/telegram/NotificationSettingsScope.h b/td/telegram/NotificationSettingsScope.h new file mode 100644 index 000000000..039176efa --- /dev/null +++ b/td/telegram/NotificationSettingsScope.h @@ -0,0 +1,29 @@ +// +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022 +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +#pragma once + +#include "td/telegram/td_api.h" +#include "td/telegram/telegram_api.h" + +#include "td/utils/common.h" +#include "td/utils/StringBuilder.h" + +namespace td { + +enum class NotificationSettingsScope : int32 { Private, Group, Channel }; + +StringBuilder &operator<<(StringBuilder &string_builder, NotificationSettingsScope scope); + +td_api::object_ptr get_notification_settings_scope_object( + NotificationSettingsScope scope); + +telegram_api::object_ptr get_input_notify_peer(NotificationSettingsScope scope); + +NotificationSettingsScope get_notification_settings_scope( + const td_api::object_ptr &scope); + +} // namespace td diff --git a/td/telegram/NotificationSound.cpp b/td/telegram/NotificationSound.cpp index c4ae2ad16..10abbfc10 100644 --- a/td/telegram/NotificationSound.cpp +++ b/td/telegram/NotificationSound.cpp @@ -223,7 +223,7 @@ unique_ptr get_notification_sound(bool use_default_sound, int return td::make_unique(ringtone_id); } -unique_ptr get_notification_sound(telegram_api::NotificationSound *notification_sound) { +static unique_ptr get_notification_sound(telegram_api::NotificationSound *notification_sound) { if (notification_sound == nullptr) { return nullptr; } @@ -251,6 +251,19 @@ unique_ptr get_notification_sound(telegram_api::NotificationS } } +unique_ptr get_notification_sound(telegram_api::peerNotifySettings *settings) { + CHECK(settings != nullptr); + telegram_api::NotificationSound *sound = +#if TD_ANDROID + settings->android_sound_.get(); +#elif TD_DARWIN_IOS || TD_DARWIN_TV_OS || TD_DARWIN_WATCH_OS + settings->ios_sound_.get(); +#else + settings->other_sound_.get(); +#endif + return get_notification_sound(sound); +} + telegram_api::object_ptr get_input_notification_sound( const unique_ptr ¬ification_sound) { if (notification_sound == nullptr) { diff --git a/td/telegram/NotificationSound.h b/td/telegram/NotificationSound.h index b7414681e..72db725cd 100644 --- a/td/telegram/NotificationSound.h +++ b/td/telegram/NotificationSound.h @@ -57,7 +57,7 @@ unique_ptr get_legacy_notification_sound(const string &sound) unique_ptr get_notification_sound(bool use_default_sound, int64 ringtone_id); -unique_ptr get_notification_sound(telegram_api::NotificationSound *notification_sound); +unique_ptr get_notification_sound(telegram_api::peerNotifySettings *settings); telegram_api::object_ptr get_input_notification_sound( const unique_ptr ¬ification_sound); diff --git a/td/telegram/ScopeNotificationSettings.cpp b/td/telegram/ScopeNotificationSettings.cpp new file mode 100644 index 000000000..eb2ba9636 --- /dev/null +++ b/td/telegram/ScopeNotificationSettings.cpp @@ -0,0 +1,74 @@ +// +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022 +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +#include "td/telegram/ScopeNotificationSettings.h" + +#include "td/telegram/Global.h" + +#include + +namespace td { + +StringBuilder &operator<<(StringBuilder &string_builder, const ScopeNotificationSettings ¬ification_settings) { + return string_builder << "[" << notification_settings.mute_until << ", " << notification_settings.sound << ", " + << notification_settings.show_preview << ", " << notification_settings.is_synchronized << ", " + << notification_settings.disable_pinned_message_notifications << ", " + << notification_settings.disable_mention_notifications << "]"; +} + +td_api::object_ptr get_scope_notification_settings_object( + const ScopeNotificationSettings *notification_settings) { + CHECK(notification_settings != nullptr); + return td_api::make_object( + max(0, notification_settings->mute_until - G()->unix_time()), + get_notification_sound_ringtone_id(notification_settings->sound), notification_settings->show_preview, + notification_settings->disable_pinned_message_notifications, + notification_settings->disable_mention_notifications); +} + +static int32 get_mute_until(int32 mute_for) { + if (mute_for <= 0) { + return 0; + } + + const int32 MAX_PRECISE_MUTE_FOR = 366 * 86400; + int32 current_time = G()->unix_time(); + if (mute_for > MAX_PRECISE_MUTE_FOR || mute_for >= std::numeric_limits::max() - current_time) { + return std::numeric_limits::max(); + } + return mute_for + current_time; +} + +Result get_scope_notification_settings( + td_api::object_ptr &¬ification_settings) { + if (notification_settings == nullptr) { + return Status::Error(400, "New notification settings must be non-empty"); + } + + auto mute_until = get_mute_until(notification_settings->mute_for_); + return ScopeNotificationSettings(mute_until, get_notification_sound(false, notification_settings->sound_id_), + notification_settings->show_preview_, + notification_settings->disable_pinned_message_notifications_, + notification_settings->disable_mention_notifications_); +} + +ScopeNotificationSettings get_scope_notification_settings(tl_object_ptr &&settings, + bool old_disable_pinned_message_notifications, + bool old_disable_mention_notifications) { + if (settings == nullptr) { + return ScopeNotificationSettings(); + } + auto mute_until = (settings->flags_ & telegram_api::peerNotifySettings::MUTE_UNTIL_MASK) == 0 || + settings->mute_until_ <= G()->unix_time() + ? 0 + : settings->mute_until_; + auto show_preview = + (settings->flags_ & telegram_api::peerNotifySettings::SHOW_PREVIEWS_MASK) == 0 ? false : settings->show_previews_; + return {mute_until, get_notification_sound(settings.get()), show_preview, old_disable_pinned_message_notifications, + old_disable_mention_notifications}; +} + +} // namespace td diff --git a/td/telegram/ScopeNotificationSettings.h b/td/telegram/ScopeNotificationSettings.h new file mode 100644 index 000000000..d1942ec84 --- /dev/null +++ b/td/telegram/ScopeNotificationSettings.h @@ -0,0 +1,55 @@ +// +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022 +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +#pragma once + +#include "td/telegram/NotificationSound.h" +#include "td/telegram/td_api.h" +#include "td/telegram/telegram_api.h" + +#include "td/utils/common.h" +#include "td/utils/Status.h" +#include "td/utils/StringBuilder.h" + +namespace td { + +class ScopeNotificationSettings { + public: + int32 mute_until = 0; + unique_ptr sound; + bool show_preview = true; + bool is_synchronized = false; + + // local settings + bool disable_pinned_message_notifications = false; + bool disable_mention_notifications = false; + + ScopeNotificationSettings() = default; + + ScopeNotificationSettings(int32 mute_until, unique_ptr &&sound, bool show_preview, + bool disable_pinned_message_notifications, bool disable_mention_notifications) + : mute_until(mute_until) + , sound(std::move(sound)) + , show_preview(show_preview) + , is_synchronized(true) + , disable_pinned_message_notifications(disable_pinned_message_notifications) + , disable_mention_notifications(disable_mention_notifications) { + } +}; + +StringBuilder &operator<<(StringBuilder &string_builder, const ScopeNotificationSettings ¬ification_settings); + +td_api::object_ptr get_scope_notification_settings_object( + const ScopeNotificationSettings *notification_settings); + +Result get_scope_notification_settings( + td_api::object_ptr &¬ification_settings); + +ScopeNotificationSettings get_scope_notification_settings(tl_object_ptr &&settings, + bool old_disable_pinned_message_notifications, + bool old_disable_mention_notifications); + +} // namespace td diff --git a/td/telegram/ScopeNotificationSettings.hpp b/td/telegram/ScopeNotificationSettings.hpp new file mode 100644 index 000000000..3aee6e418 --- /dev/null +++ b/td/telegram/ScopeNotificationSettings.hpp @@ -0,0 +1,71 @@ +// +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022 +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +#pragma once + +#include "td/telegram/Global.h" +#include "td/telegram/NotificationSound.h" +#include "td/telegram/ScopeNotificationSettings.h" + +#include "td/utils/tl_helpers.h" + +namespace td { + +template +void store(const ScopeNotificationSettings ¬ification_settings, StorerT &storer) { + bool is_muted = notification_settings.mute_until != 0 && notification_settings.mute_until > G()->unix_time(); + bool has_sound = notification_settings.sound != nullptr; + bool has_ringtone_support = true; + BEGIN_STORE_FLAGS(); + STORE_FLAG(is_muted); + STORE_FLAG(has_sound); + STORE_FLAG(notification_settings.show_preview); + STORE_FLAG(false); + STORE_FLAG(notification_settings.is_synchronized); + STORE_FLAG(notification_settings.disable_pinned_message_notifications); + STORE_FLAG(notification_settings.disable_mention_notifications); + STORE_FLAG(has_ringtone_support); + END_STORE_FLAGS(); + if (is_muted) { + store(notification_settings.mute_until, storer); + } + if (has_sound) { + store(notification_settings.sound, storer); + } +} + +template +void parse(ScopeNotificationSettings ¬ification_settings, ParserT &parser) { + bool is_muted; + bool has_sound; + bool silent_send_message_ignored; + bool has_ringtone_support; + BEGIN_PARSE_FLAGS(); + PARSE_FLAG(is_muted); + PARSE_FLAG(has_sound); + PARSE_FLAG(notification_settings.show_preview); + PARSE_FLAG(silent_send_message_ignored); + PARSE_FLAG(notification_settings.is_synchronized); + PARSE_FLAG(notification_settings.disable_pinned_message_notifications); + PARSE_FLAG(notification_settings.disable_mention_notifications); + PARSE_FLAG(has_ringtone_support); + END_PARSE_FLAGS(); + (void)silent_send_message_ignored; + if (is_muted) { + parse(notification_settings.mute_until, parser); + } + if (has_sound) { + if (has_ringtone_support) { + parse_notification_sound(notification_settings.sound, parser); + } else { + string sound; + parse(sound, parser); + notification_settings.sound = get_legacy_notification_sound(sound); + } + } +} + +} // namespace td diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 3dcdef956..c0eee8685 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -88,8 +88,8 @@ #include "td/telegram/NotificationGroupId.h" #include "td/telegram/NotificationId.h" #include "td/telegram/NotificationManager.h" -#include "td/telegram/NotificationSettings.h" #include "td/telegram/NotificationSettingsManager.h" +#include "td/telegram/NotificationSettingsScope.h" #include "td/telegram/OptionManager.h" #include "td/telegram/PasswordManager.h" #include "td/telegram/Payments.h" @@ -102,6 +102,7 @@ #include "td/telegram/PublicDialogType.h" #include "td/telegram/ReportReason.h" #include "td/telegram/RequestActor.h" +#include "td/telegram/ScopeNotificationSettings.h" #include "td/telegram/SecretChatId.h" #include "td/telegram/SecretChatsManager.h" #include "td/telegram/SecureManager.h" diff --git a/td/telegram/UpdatesManager.cpp b/td/telegram/UpdatesManager.cpp index 57ff36a1f..284232648 100644 --- a/td/telegram/UpdatesManager.cpp +++ b/td/telegram/UpdatesManager.cpp @@ -34,8 +34,8 @@ #include "td/telegram/net/DcOptions.h" #include "td/telegram/net/NetQuery.h" #include "td/telegram/NotificationManager.h" -#include "td/telegram/NotificationSettings.h" #include "td/telegram/NotificationSettingsManager.h" +#include "td/telegram/NotificationSettingsScope.h" #include "td/telegram/OptionManager.h" #include "td/telegram/OrderInfo.h" #include "td/telegram/PollId.h"