2022-10-25 00:22:04 +02:00
|
|
|
//
|
2022-12-31 22:28:08 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
2022-10-25 00:22:04 +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/ScopeNotificationSettings.h"
|
|
|
|
|
|
|
|
#include "td/telegram/Global.h"
|
|
|
|
|
|
|
|
#include <limits>
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
2023-06-09 14:08:00 +02:00
|
|
|
telegram_api::object_ptr<telegram_api::inputPeerNotifySettings>
|
|
|
|
ScopeNotificationSettings::get_input_peer_notify_settings() const {
|
|
|
|
int32 flags = telegram_api::inputPeerNotifySettings::MUTE_UNTIL_MASK |
|
|
|
|
telegram_api::inputPeerNotifySettings::SHOW_PREVIEWS_MASK;
|
|
|
|
if (sound != nullptr) {
|
|
|
|
flags |= telegram_api::inputPeerNotifySettings::SOUND_MASK;
|
|
|
|
}
|
|
|
|
return telegram_api::make_object<telegram_api::inputPeerNotifySettings>(
|
|
|
|
flags, show_preview, false, mute_until, get_input_notification_sound(sound), false, false, nullptr);
|
|
|
|
}
|
|
|
|
|
2022-10-25 00:22:04 +02:00
|
|
|
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<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> &¬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<telegram_api::peerNotifySettings> &&settings,
|
|
|
|
bool old_disable_pinned_message_notifications,
|
|
|
|
bool old_disable_mention_notifications) {
|
|
|
|
if (settings == nullptr) {
|
|
|
|
return ScopeNotificationSettings();
|
|
|
|
}
|
2022-12-18 20:15:57 +01:00
|
|
|
auto mute_until = settings->mute_until_;
|
|
|
|
if (mute_until <= G()->unix_time()) {
|
|
|
|
mute_until = 0;
|
|
|
|
}
|
|
|
|
auto show_preview = settings->show_previews_;
|
2022-10-25 00:22:04 +02:00
|
|
|
return {mute_until, get_notification_sound(settings.get()), show_preview, old_disable_pinned_message_notifications,
|
|
|
|
old_disable_mention_notifications};
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|