2018-09-29 02:29:57 +02:00
|
|
|
//
|
2018-12-31 23:02:34 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2019
|
2018-09-29 02:29:57 +02:00
|
|
|
//
|
|
|
|
// 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"
|
2019-02-01 13:47:27 +01:00
|
|
|
#include "td/telegram/misc.h"
|
2018-09-29 02:29:57 +02:00
|
|
|
|
|
|
|
#include "td/utils/logging.h"
|
|
|
|
|
2019-02-01 13:47:27 +01:00
|
|
|
#include <limits>
|
|
|
|
|
2018-09-29 02:29:57 +02:00
|
|
|
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
|
2019-01-10 18:54:46 +01:00
|
|
|
<< ", " << notification_settings.disable_pinned_message_notifications << ", "
|
|
|
|
<< notification_settings.disable_mention_notifications << ", "
|
2019-01-10 01:47:33 +01:00
|
|
|
<< notification_settings.use_default_mute_until << ", "
|
2018-09-29 02:29:57 +02:00
|
|
|
<< notification_settings.use_default_sound << ", "
|
|
|
|
<< notification_settings.use_default_show_preview << ", "
|
2019-01-10 18:54:46 +01:00
|
|
|
<< notification_settings.use_default_disable_pinned_message_notifications << ", "
|
|
|
|
<< notification_settings.use_default_disable_mention_notifications << ", "
|
2018-09-29 02:29:57 +02:00
|
|
|
<< 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";
|
2019-01-29 12:40:56 +01:00
|
|
|
case NotificationSettingsScope::Channel:
|
|
|
|
return string_builder << "notification settings for channel chats";
|
2018-09-29 02:29:57 +02:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return string_builder;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
StringBuilder &operator<<(StringBuilder &string_builder, const ScopeNotificationSettings ¬ification_settings) {
|
|
|
|
return string_builder << "[" << notification_settings.mute_until << ", " << notification_settings.sound << ", "
|
2019-01-10 01:47:33 +01:00
|
|
|
<< notification_settings.show_preview << ", " << notification_settings.is_synchronized << ", "
|
2019-01-13 20:28:25 +01:00
|
|
|
<< notification_settings.disable_pinned_message_notifications << ", "
|
2019-01-10 18:54:46 +01:00
|
|
|
<< notification_settings.disable_mention_notifications << "]";
|
2018-09-29 02:29:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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>();
|
2019-01-29 12:40:56 +01:00
|
|
|
case NotificationSettingsScope::Channel:
|
|
|
|
return td_api::make_object<td_api::notificationSettingsScopeChannelChats>();
|
2018-09-29 02:29:57 +02:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
td_api::object_ptr<td_api::chatNotificationSettings> get_chat_notification_settings_object(
|
|
|
|
const DialogNotificationSettings *notification_settings) {
|
2019-01-10 01:47:33 +01:00
|
|
|
CHECK(notification_settings != nullptr);
|
2018-09-29 02:29:57 +02:00
|
|
|
return td_api::make_object<td_api::chatNotificationSettings>(
|
|
|
|
notification_settings->use_default_mute_until, max(0, notification_settings->mute_until - G()->unix_time()),
|
|
|
|
notification_settings->use_default_sound, notification_settings->sound,
|
2019-01-10 01:47:33 +01:00
|
|
|
notification_settings->use_default_show_preview, notification_settings->show_preview,
|
2019-01-10 18:54:46 +01:00
|
|
|
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);
|
2018-09-29 02:29:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
td_api::object_ptr<td_api::scopeNotificationSettings> get_scope_notification_settings_object(
|
|
|
|
const ScopeNotificationSettings *notification_settings) {
|
2019-01-10 01:47:33 +01:00
|
|
|
CHECK(notification_settings != nullptr);
|
2018-09-29 02:29:57 +02:00
|
|
|
return td_api::make_object<td_api::scopeNotificationSettings>(
|
|
|
|
max(0, notification_settings->mute_until - G()->unix_time()), notification_settings->sound,
|
2019-01-10 18:54:46 +01:00
|
|
|
notification_settings->show_preview, notification_settings->disable_pinned_message_notifications,
|
|
|
|
notification_settings->disable_mention_notifications);
|
2018-09-29 02:29:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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>();
|
2019-01-29 12:40:56 +01:00
|
|
|
case NotificationSettingsScope::Channel:
|
|
|
|
return telegram_api::make_object<telegram_api::inputNotifyBroadcasts>();
|
2018-09-29 02:29:57 +02:00
|
|
|
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;
|
2019-01-29 12:40:56 +01:00
|
|
|
case td_api::notificationSettingsScopeChannelChats::ID:
|
|
|
|
return NotificationSettingsScope::Channel;
|
2018-09-29 02:29:57 +02:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return NotificationSettingsScope::Private;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-01 13:47:27 +01:00
|
|
|
Result<DialogNotificationSettings> get_dialog_notification_settings(
|
|
|
|
td_api::object_ptr<td_api::chatNotificationSettings> &¬ification_settings, bool old_silent_send_message) {
|
|
|
|
if (notification_settings == nullptr) {
|
|
|
|
return Status::Error(400, "New notification settings must not be empty");
|
|
|
|
}
|
|
|
|
if (!clean_input_string(notification_settings->sound_)) {
|
|
|
|
return Status::Error(400, "Notification settings sound must be encoded in UTF-8");
|
|
|
|
}
|
|
|
|
if (notification_settings->sound_.empty()) {
|
|
|
|
notification_settings->sound_ = "default";
|
|
|
|
}
|
|
|
|
|
|
|
|
int32 current_time = G()->unix_time();
|
|
|
|
if (notification_settings->mute_for_ > std::numeric_limits<int32>::max() - current_time) {
|
|
|
|
notification_settings->mute_for_ = std::numeric_limits<int32>::max() - current_time;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32 mute_until;
|
|
|
|
if (notification_settings->use_default_mute_for_ || notification_settings->mute_for_ <= 0) {
|
|
|
|
mute_until = 0;
|
|
|
|
} else {
|
|
|
|
mute_until = notification_settings->mute_for_ + current_time;
|
|
|
|
}
|
|
|
|
|
|
|
|
return DialogNotificationSettings(notification_settings->use_default_mute_for_, mute_until,
|
|
|
|
notification_settings->use_default_sound_, std::move(notification_settings->sound_),
|
|
|
|
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> &¬ification_settings) {
|
|
|
|
if (notification_settings == nullptr) {
|
|
|
|
return Status::Error(400, "New notification settings must not be empty");
|
|
|
|
}
|
|
|
|
if (!clean_input_string(notification_settings->sound_)) {
|
|
|
|
return Status::Error(400, "Notification settings sound must be encoded in UTF-8");
|
|
|
|
}
|
|
|
|
if (notification_settings->sound_.empty()) {
|
|
|
|
notification_settings->sound_ = "default";
|
|
|
|
}
|
|
|
|
|
|
|
|
int32 current_time = G()->unix_time();
|
|
|
|
if (notification_settings->mute_for_ > std::numeric_limits<int32>::max() - current_time) {
|
|
|
|
notification_settings->mute_for_ = std::numeric_limits<int32>::max() - current_time;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32 mute_until;
|
|
|
|
if (notification_settings->mute_for_ <= 0) {
|
|
|
|
mute_until = 0;
|
|
|
|
} else {
|
|
|
|
mute_until = notification_settings->mute_for_ + current_time;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ScopeNotificationSettings(mute_until, std::move(notification_settings->sound_),
|
|
|
|
notification_settings->show_preview_,
|
|
|
|
notification_settings->disable_pinned_message_notifications_,
|
|
|
|
notification_settings->disable_mention_notifications_);
|
|
|
|
}
|
|
|
|
|
2019-01-10 01:47:33 +01:00
|
|
|
DialogNotificationSettings get_dialog_notification_settings(tl_object_ptr<telegram_api::peerNotifySettings> &&settings,
|
2019-01-10 18:54:46 +01:00
|
|
|
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) {
|
2018-09-29 02:29:57 +02:00
|
|
|
bool use_default_mute_until = (settings->flags_ & telegram_api::peerNotifySettings::MUTE_UNTIL_MASK) == 0;
|
|
|
|
bool use_default_sound = (settings->flags_ & telegram_api::peerNotifySettings::SOUND_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_;
|
2019-02-01 13:16:00 +01:00
|
|
|
auto sound = std::move(settings->sound_);
|
|
|
|
if (sound.empty()) {
|
|
|
|
sound = "default";
|
|
|
|
}
|
2018-09-29 02:29:57 +02:00
|
|
|
bool silent_send_message =
|
|
|
|
(settings->flags_ & telegram_api::peerNotifySettings::SILENT_MASK) == 0 ? false : settings->silent_;
|
2019-01-10 01:47:33 +01:00
|
|
|
return {use_default_mute_until,
|
|
|
|
mute_until,
|
|
|
|
use_default_sound,
|
2019-02-01 13:16:00 +01:00
|
|
|
std::move(sound),
|
2019-01-10 01:47:33 +01:00
|
|
|
use_default_show_preview,
|
|
|
|
settings->show_previews_,
|
|
|
|
silent_send_message,
|
2019-01-10 18:54:46 +01:00
|
|
|
old_use_default_disable_pinned_message_notifications,
|
|
|
|
old_disable_pinned_message_notifications,
|
|
|
|
old_use_default_disable_mention_notifications,
|
|
|
|
old_disable_mention_notifications};
|
2018-09-29 02:29:57 +02:00
|
|
|
}
|
|
|
|
|
2019-01-10 01:47:33 +01:00
|
|
|
ScopeNotificationSettings get_scope_notification_settings(tl_object_ptr<telegram_api::peerNotifySettings> &&settings,
|
2019-01-10 18:54:46 +01:00
|
|
|
bool old_disable_pinned_message_notifications,
|
|
|
|
bool old_disable_mention_notifications) {
|
2018-09-29 02:29:57 +02:00
|
|
|
auto mute_until = (settings->flags_ & telegram_api::peerNotifySettings::MUTE_UNTIL_MASK) == 0 ||
|
|
|
|
settings->mute_until_ <= G()->unix_time()
|
|
|
|
? 0
|
|
|
|
: settings->mute_until_;
|
2019-02-01 13:16:00 +01:00
|
|
|
auto sound = std::move(settings->sound_);
|
|
|
|
if (sound.empty()) {
|
|
|
|
sound = "default";
|
|
|
|
}
|
2018-09-29 02:29:57 +02:00
|
|
|
auto show_preview =
|
|
|
|
(settings->flags_ & telegram_api::peerNotifySettings::SHOW_PREVIEWS_MASK) == 0 ? false : settings->show_previews_;
|
2019-01-10 18:54:46 +01:00
|
|
|
return {mute_until, std::move(sound), show_preview, old_disable_pinned_message_notifications,
|
|
|
|
old_disable_mention_notifications};
|
2018-09-29 02:29:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|