Split NotificationSettings.h.

This commit is contained in:
levlam 2022-10-25 01:22:04 +03:00
parent ac76e8604c
commit 88ffdb62e7
18 changed files with 434 additions and 335 deletions

View File

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

View File

@ -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 <limits>
namespace td {
StringBuilder &operator<<(StringBuilder &string_builder, const DialogNotificationSettings &notification_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<td_api::chatNotificationSettings> get_chat_notification_settings_object(
const DialogNotificationSettings *notification_settings) {
CHECK(notification_settings != nullptr);
return td_api::make_object<td_api::chatNotificationSettings>(
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<int32>::max() - current_time) {
return std::numeric_limits<int32>::max();
}
return mute_for + current_time;
}
Result<DialogNotificationSettings> get_dialog_notification_settings(
td_api::object_ptr<td_api::chatNotificationSettings> &&notification_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<telegram_api::peerNotifySettings> &&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

View File

@ -55,68 +55,20 @@ class DialogNotificationSettings {
}
};
enum class NotificationSettingsScope : int32 { Private, Group, Channel };
class ScopeNotificationSettings {
public:
int32 mute_until = 0;
unique_ptr<NotificationSound> 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<NotificationSound> &&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 &notification_settings);
StringBuilder &operator<<(StringBuilder &string_builder, NotificationSettingsScope scope);
StringBuilder &operator<<(StringBuilder &string_builder, const ScopeNotificationSettings &notification_settings);
td_api::object_ptr<td_api::NotificationSettingsScope> get_notification_settings_scope_object(
NotificationSettingsScope scope);
td_api::object_ptr<td_api::chatNotificationSettings> get_chat_notification_settings_object(
const DialogNotificationSettings *notification_settings);
td_api::object_ptr<td_api::scopeNotificationSettings> get_scope_notification_settings_object(
const ScopeNotificationSettings *notification_settings);
telegram_api::object_ptr<telegram_api::InputNotifyPeer> get_input_notify_peer(NotificationSettingsScope scope);
NotificationSettingsScope get_notification_settings_scope(
const td_api::object_ptr<td_api::NotificationSettingsScope> &scope);
Result<DialogNotificationSettings> get_dialog_notification_settings(
td_api::object_ptr<td_api::chatNotificationSettings> &&notification_settings, bool old_silent_send_message);
Result<ScopeNotificationSettings> get_scope_notification_settings(
td_api::object_ptr<td_api::scopeNotificationSettings> &&notification_settings);
DialogNotificationSettings get_dialog_notification_settings(tl_object_ptr<telegram_api::peerNotifySettings> &&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<telegram_api::peerNotifySettings> &&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

View File

@ -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 &notification_settings, ParserT &parser) {
}
}
template <class StorerT>
void store(const ScopeNotificationSettings &notification_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 <class ParserT>
void parse(ScopeNotificationSettings &notification_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

View File

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

View File

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

View File

@ -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 <limits>
namespace td {
StringBuilder &operator<<(StringBuilder &string_builder, const DialogNotificationSettings &notification_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 &notification_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<td_api::NotificationSettingsScope> get_notification_settings_scope_object(
NotificationSettingsScope scope) {
switch (scope) {
case NotificationSettingsScope::Private:
return td_api::make_object<td_api::notificationSettingsScopePrivateChats>();
case NotificationSettingsScope::Group:
return td_api::make_object<td_api::notificationSettingsScopeGroupChats>();
case NotificationSettingsScope::Channel:
return td_api::make_object<td_api::notificationSettingsScopeChannelChats>();
default:
UNREACHABLE();
return nullptr;
}
}
td_api::object_ptr<td_api::chatNotificationSettings> get_chat_notification_settings_object(
const DialogNotificationSettings *notification_settings) {
CHECK(notification_settings != nullptr);
return td_api::make_object<td_api::chatNotificationSettings>(
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<td_api::scopeNotificationSettings> get_scope_notification_settings_object(
const ScopeNotificationSettings *notification_settings) {
CHECK(notification_settings != nullptr);
return td_api::make_object<td_api::scopeNotificationSettings>(
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<telegram_api::InputNotifyPeer> get_input_notify_peer(NotificationSettingsScope scope) {
switch (scope) {
case NotificationSettingsScope::Private:
return telegram_api::make_object<telegram_api::inputNotifyUsers>();
case NotificationSettingsScope::Group:
return telegram_api::make_object<telegram_api::inputNotifyChats>();
case NotificationSettingsScope::Channel:
return telegram_api::make_object<telegram_api::inputNotifyBroadcasts>();
default:
return nullptr;
}
}
NotificationSettingsScope get_notification_settings_scope(
const td_api::object_ptr<td_api::NotificationSettingsScope> &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<int32>::max() - current_time) {
return std::numeric_limits<int32>::max();
}
return mute_for + current_time;
}
Result<DialogNotificationSettings> get_dialog_notification_settings(
td_api::object_ptr<td_api::chatNotificationSettings> &&notification_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<ScopeNotificationSettings> get_scope_notification_settings(
td_api::object_ptr<td_api::scopeNotificationSettings> &&notification_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<NotificationSound> get_peer_notification_sound(
tl_object_ptr<telegram_api::peerNotifySettings> &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<telegram_api::peerNotifySettings> &&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<telegram_api::peerNotifySettings> &&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

View File

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

View File

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

View File

@ -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<td_api::NotificationSettingsScope> get_notification_settings_scope_object(
NotificationSettingsScope scope) {
switch (scope) {
case NotificationSettingsScope::Private:
return td_api::make_object<td_api::notificationSettingsScopePrivateChats>();
case NotificationSettingsScope::Group:
return td_api::make_object<td_api::notificationSettingsScopeGroupChats>();
case NotificationSettingsScope::Channel:
return td_api::make_object<td_api::notificationSettingsScopeChannelChats>();
default:
UNREACHABLE();
return nullptr;
}
}
telegram_api::object_ptr<telegram_api::InputNotifyPeer> get_input_notify_peer(NotificationSettingsScope scope) {
switch (scope) {
case NotificationSettingsScope::Private:
return telegram_api::make_object<telegram_api::inputNotifyUsers>();
case NotificationSettingsScope::Group:
return telegram_api::make_object<telegram_api::inputNotifyChats>();
case NotificationSettingsScope::Channel:
return telegram_api::make_object<telegram_api::inputNotifyBroadcasts>();
default:
return nullptr;
}
}
NotificationSettingsScope get_notification_settings_scope(
const td_api::object_ptr<td_api::NotificationSettingsScope> &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

View File

@ -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<td_api::NotificationSettingsScope> get_notification_settings_scope_object(
NotificationSettingsScope scope);
telegram_api::object_ptr<telegram_api::InputNotifyPeer> get_input_notify_peer(NotificationSettingsScope scope);
NotificationSettingsScope get_notification_settings_scope(
const td_api::object_ptr<td_api::NotificationSettingsScope> &scope);
} // namespace td

View File

@ -223,7 +223,7 @@ unique_ptr<NotificationSound> get_notification_sound(bool use_default_sound, int
return td::make_unique<NotificationSoundRingtone>(ringtone_id);
}
unique_ptr<NotificationSound> get_notification_sound(telegram_api::NotificationSound *notification_sound) {
static unique_ptr<NotificationSound> get_notification_sound(telegram_api::NotificationSound *notification_sound) {
if (notification_sound == nullptr) {
return nullptr;
}
@ -251,6 +251,19 @@ unique_ptr<NotificationSound> get_notification_sound(telegram_api::NotificationS
}
}
unique_ptr<NotificationSound> 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<telegram_api::NotificationSound> get_input_notification_sound(
const unique_ptr<NotificationSound> &notification_sound) {
if (notification_sound == nullptr) {

View File

@ -57,7 +57,7 @@ unique_ptr<NotificationSound> get_legacy_notification_sound(const string &sound)
unique_ptr<NotificationSound> get_notification_sound(bool use_default_sound, int64 ringtone_id);
unique_ptr<NotificationSound> get_notification_sound(telegram_api::NotificationSound *notification_sound);
unique_ptr<NotificationSound> get_notification_sound(telegram_api::peerNotifySettings *settings);
telegram_api::object_ptr<telegram_api::NotificationSound> get_input_notification_sound(
const unique_ptr<NotificationSound> &notification_sound);

View File

@ -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 <limits>
namespace td {
StringBuilder &operator<<(StringBuilder &string_builder, const ScopeNotificationSettings &notification_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<td_api::scopeNotificationSettings> get_scope_notification_settings_object(
const ScopeNotificationSettings *notification_settings) {
CHECK(notification_settings != nullptr);
return td_api::make_object<td_api::scopeNotificationSettings>(
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<int32>::max() - current_time) {
return std::numeric_limits<int32>::max();
}
return mute_for + current_time;
}
Result<ScopeNotificationSettings> get_scope_notification_settings(
td_api::object_ptr<td_api::scopeNotificationSettings> &&notification_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<telegram_api::peerNotifySettings> &&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

View File

@ -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<NotificationSound> 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<NotificationSound> &&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 &notification_settings);
td_api::object_ptr<td_api::scopeNotificationSettings> get_scope_notification_settings_object(
const ScopeNotificationSettings *notification_settings);
Result<ScopeNotificationSettings> get_scope_notification_settings(
td_api::object_ptr<td_api::scopeNotificationSettings> &&notification_settings);
ScopeNotificationSettings get_scope_notification_settings(tl_object_ptr<telegram_api::peerNotifySettings> &&settings,
bool old_disable_pinned_message_notifications,
bool old_disable_mention_notifications);
} // namespace td

View File

@ -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 <class StorerT>
void store(const ScopeNotificationSettings &notification_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 <class ParserT>
void parse(ScopeNotificationSettings &notification_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

View File

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

View File

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