Move notification settings conversion to NotificationSettings.cpp.

GitOrigin-RevId: ac4dcc19183f1ffeba6ebae70a3c9049a9eee415
This commit is contained in:
levlam 2019-02-01 15:47:27 +03:00
parent 591576e6a4
commit bdfd8f4523
3 changed files with 76 additions and 51 deletions

View File

@ -12939,38 +12939,13 @@ tl_object_ptr<telegram_api::InputNotifyPeer> MessagesManager::get_input_notify_p
Status MessagesManager::set_dialog_notification_settings(
DialogId dialog_id, tl_object_ptr<td_api::chatNotificationSettings> &&notification_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");
}
auto current_settings = get_dialog_notification_settings(dialog_id, false);
if (current_settings == nullptr) {
return Status::Error(6, "Wrong chat identifier specified");
}
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;
}
DialogNotificationSettings new_settings(
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_, current_settings->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_);
TRY_RESULT(new_settings, ::td::get_dialog_notification_settings(std::move(notification_settings),
current_settings->silent_send_message));
if (update_dialog_notification_settings(dialog_id, current_settings, new_settings)) {
update_dialog_notification_settings_on_server(dialog_id, false);
}
@ -12979,30 +12954,7 @@ Status MessagesManager::set_dialog_notification_settings(
Status MessagesManager::set_scope_notification_settings(
NotificationSettingsScope scope, tl_object_ptr<td_api::scopeNotificationSettings> &&notification_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");
}
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;
}
ScopeNotificationSettings new_settings(mute_until, std::move(notification_settings->sound_),
notification_settings->show_preview_,
notification_settings->disable_pinned_message_notifications_,
notification_settings->disable_mention_notifications_);
TRY_RESULT(new_settings, ::td::get_scope_notification_settings(std::move(notification_settings)));
if (update_scope_notification_settings(scope, get_scope_notification_settings(scope), new_settings)) {
update_scope_notification_settings_on_server(scope, 0);
}

View File

@ -7,9 +7,12 @@
#include "td/telegram/NotificationSettings.h"
#include "td/telegram/Global.h"
#include "td/telegram/misc.h"
#include "td/utils/logging.h"
#include <limits>
namespace td {
StringBuilder &operator<<(StringBuilder &string_builder, const DialogNotificationSettings &notification_settings) {
@ -112,6 +115,70 @@ NotificationSettingsScope get_notification_settings_scope(
}
}
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 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> &&notification_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_);
}
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,

View File

@ -100,6 +100,12 @@ telegram_api::object_ptr<telegram_api::InputNotifyPeer> get_input_notify_peer(No
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,