Add td_api::setReactionNotificationSettings.

This commit is contained in:
levlam 2024-04-25 02:13:45 +03:00
parent 7d0ccce6b4
commit 4b078cb36c
10 changed files with 157 additions and 1 deletions

View File

@ -8955,7 +8955,10 @@ getScopeNotificationSettings scope:NotificationSettingsScope = ScopeNotification
//@description Changes notification settings for chats of a given type @scope Types of chats for which to change the notification settings @notification_settings The new notification settings for the given scope
setScopeNotificationSettings scope:NotificationSettingsScope notification_settings:scopeNotificationSettings = Ok;
//@description Resets all notification settings to their default values. By default, all chats are unmuted and message previews are shown
//@description Changes notification settings for reactions @notification_settings The new notification settings for reactions
setReactionNotificationSettings notification_settings:reactionNotificationSettings = Ok;
//@description Resets all chat and scope notification settings to their default values. By default, all chats are unmuted and message previews are shown
resetAllNotificationSettings = Ok;

View File

@ -499,6 +499,41 @@ class UpdateScopeNotifySettingsQuery final : public Td::ResultHandler {
}
};
class SetReactionsNotifySettingsQuery final : public Td::ResultHandler {
Promise<Unit> promise_;
public:
explicit SetReactionsNotifySettingsQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
}
void send(const ReactionNotificationSettings &settings) {
send_query(G()->net_query_creator().create(
telegram_api::account_setReactionsNotifySettings(settings.get_input_reactions_notify_settings())));
}
void on_result(BufferSlice packet) final {
auto result_ptr = fetch_result<telegram_api::account_setReactionsNotifySettings>(packet);
if (result_ptr.is_error()) {
return on_error(result_ptr.move_as_error());
}
auto ptr = result_ptr.move_as_ok();
LOG(INFO) << "Receive result for SetReactionsNotifySettingsQuery: " << to_string(ptr);
promise_.set_value(Unit());
}
void on_error(Status status) final {
LOG(INFO) << "Receive error for set reaction notification settings: " << status;
if (!td_->auth_manager_->is_bot()) {
// trying to repair notification settings
td_->notification_settings_manager_->send_get_reaction_notification_settings_query(Promise<>());
}
promise_.set_error(std::move(status));
}
};
class ResetNotifySettingsQuery final : public Td::ResultHandler {
Promise<Unit> promise_;
@ -1608,6 +1643,61 @@ void NotificationSettingsManager::reset_notify_settings(Promise<Unit> &&promise)
td_->create_handler<ResetNotifySettingsQuery>(std::move(promise))->send();
}
Status NotificationSettingsManager::set_reaction_notification_settings(
ReactionNotificationSettings &&notification_settings) {
CHECK(!td_->auth_manager_->is_bot());
notification_settings.update_default_notification_sound(reaction_notification_settings_);
if (notification_settings == reaction_notification_settings_) {
have_reaction_notification_settings_ = true;
return Status::OK();
}
VLOG(notifications) << "Update reaction notification settings from " << reaction_notification_settings_ << " to "
<< notification_settings;
reaction_notification_settings_ = std::move(notification_settings);
have_reaction_notification_settings_ = true;
save_reaction_notification_settings();
send_closure(G()->td(), &Td::send_update, get_update_reaction_notification_settings_object());
update_reaction_notification_settings_on_server(0);
return Status::OK();
}
class NotificationSettingsManager::UpdateReactionNotificationSettingsOnServerLogEvent {
public:
template <class StorerT>
void store(StorerT &storer) const {
BEGIN_STORE_FLAGS();
END_STORE_FLAGS();
}
template <class ParserT>
void parse(ParserT &parser) {
BEGIN_PARSE_FLAGS();
END_PARSE_FLAGS();
}
};
uint64 NotificationSettingsManager::save_update_reaction_notification_settings_on_server_log_event() {
UpdateReactionNotificationSettingsOnServerLogEvent log_event;
return binlog_add(G()->td_db()->get_binlog(), LogEvent::HandlerType::UpdateReactionNotificationSettingsOnServer,
get_log_event_storer(log_event));
}
void NotificationSettingsManager::update_reaction_notification_settings_on_server(uint64 log_event_id) {
CHECK(!td_->auth_manager_->is_bot());
if (log_event_id == 0) {
log_event_id = save_update_reaction_notification_settings_on_server_log_event();
}
LOG(INFO) << "Update reaction notification settings on server with log_event " << log_event_id;
td_->create_handler<SetReactionsNotifySettingsQuery>(get_erase_log_event_promise(log_event_id))
->send(reaction_notification_settings_);
}
void NotificationSettingsManager::get_notify_settings_exceptions(NotificationSettingsScope scope, bool filter_scope,
bool compare_sound, Promise<Unit> &&promise) {
td_->create_handler<GetNotifySettingsExceptionsQuery>(std::move(promise))->send(scope, filter_scope, compare_sound);
@ -1632,6 +1722,13 @@ void NotificationSettingsManager::on_binlog_events(vector<BinlogEvent> &&events)
update_scope_notification_settings_on_server(log_event.scope_, event.id_);
break;
}
case LogEvent::HandlerType::UpdateReactionNotificationSettingsOnServer: {
UpdateReactionNotificationSettingsOnServerLogEvent log_event;
log_event_parse(log_event, event.get_data()).ensure();
update_reaction_notification_settings_on_server(event.id_);
break;
}
default:
LOG(FATAL) << "Unsupported log event type " << event.type_;
}

View File

@ -107,6 +107,8 @@ class NotificationSettingsManager final : public Actor {
td_api::object_ptr<td_api::scopeNotificationSettings> &&notification_settings)
TD_WARN_UNUSED_RESULT;
Status set_reaction_notification_settings(ReactionNotificationSettings &&notification_settings) TD_WARN_UNUSED_RESULT;
void reset_scope_notification_settings();
void reset_notify_settings(Promise<Unit> &&promise);
@ -124,6 +126,7 @@ class NotificationSettingsManager final : public Actor {
private:
class UpdateScopeNotificationSettingsOnServerLogEvent;
class UpdateReactionNotificationSettingsOnServerLogEvent;
class RingtoneListLogEvent;
@ -204,6 +207,10 @@ class NotificationSettingsManager final : public Actor {
void save_reaction_notification_settings() const;
uint64 save_update_reaction_notification_settings_on_server_log_event();
void update_reaction_notification_settings_on_server(uint64 log_event_id);
Td *td_;
ActorShared<> parent_;

View File

@ -54,6 +54,12 @@ ReactionNotificationSettings::get_input_reactions_notify_settings() const {
get_input_notification_sound(sound_, true), show_preview_);
}
void ReactionNotificationSettings::update_default_notification_sound(const ReactionNotificationSettings &other) {
if (is_notification_sound_default(sound_) && is_notification_sound_default(other.sound_)) {
sound_ = dup_notification_sound(other.sound_);
}
}
bool operator==(const ReactionNotificationSettings &lhs, const ReactionNotificationSettings &rhs) {
return lhs.message_reactions_ == rhs.message_reactions_ && lhs.story_reactions_ == rhs.story_reactions_ &&
are_equivalent_notification_sounds(lhs.sound_, rhs.sound_) && lhs.show_preview_ == rhs.show_preview_;

View File

@ -41,6 +41,8 @@ class ReactionNotificationSettings {
telegram_api::object_ptr<telegram_api::reactionsNotifySettings> get_input_reactions_notify_settings() const;
void update_default_notification_sound(const ReactionNotificationSettings &other);
template <class StorerT>
void store(StorerT &storer) const;

View File

@ -129,6 +129,7 @@
#include "td/telegram/PublicDialogType.h"
#include "td/telegram/QuickReplyManager.h"
#include "td/telegram/ReactionManager.h"
#include "td/telegram/ReactionNotificationSettings.h"
#include "td/telegram/ReactionType.h"
#include "td/telegram/ReportReason.h"
#include "td/telegram/RequestActor.h"
@ -8748,6 +8749,12 @@ void Td::on_request(uint64 id, td_api::setScopeNotificationSettings &request) {
get_notification_settings_scope(request.scope_), std::move(request.notification_settings_)));
}
void Td::on_request(uint64 id, td_api::setReactionNotificationSettings &request) {
CHECK_IS_USER();
answer_ok_query(id, notification_settings_manager_->set_reaction_notification_settings(
ReactionNotificationSettings(std::move(request.notification_settings_))));
}
void Td::on_request(uint64 id, const td_api::resetAllNotificationSettings &request) {
CHECK_IS_USER();
messages_manager_->reset_all_notification_settings();

View File

@ -1633,6 +1633,8 @@ class Td final : public Actor {
void on_request(uint64 id, td_api::setScopeNotificationSettings &request);
void on_request(uint64 id, td_api::setReactionNotificationSettings &request);
void on_request(uint64 id, const td_api::resetAllNotificationSettings &request);
void on_request(uint64 id, const td_api::removeChatActionBar &request);

View File

@ -137,6 +137,7 @@ Status init_binlog(Binlog &binlog, string path, BinlogKeyValue<Binlog> &binlog_p
events.to_story_manager.push_back(event.clone());
break;
case LogEvent::HandlerType::UpdateScopeNotificationSettingsOnServer:
case LogEvent::HandlerType::UpdateReactionNotificationSettingsOnServer:
events.to_notification_settings_manager.push_back(event.clone());
break;
case LogEvent::HandlerType::AddMessagePushNotification:

View File

@ -1239,6 +1239,27 @@ class CliClient final : public Actor {
}
}
struct ReactionNotificationSource {
string source;
operator td_api::object_ptr<td_api::ReactionNotificationSource>() const {
if (source == "none" || source == "n") {
return td_api::make_object<td_api::reactionNotificationSourceNone>();
}
if (source == "contacts" || source == "c") {
return td_api::make_object<td_api::reactionNotificationSourceContacts>();
}
if (source == "all" || source == "a") {
return td_api::make_object<td_api::reactionNotificationSourceAll>();
}
return nullptr;
}
};
void get_args(string &args, ReactionNotificationSource &arg) const {
arg.source = trim(args);
}
struct PrivacyRules {
string rules_str;
@ -6502,6 +6523,15 @@ class CliClient final : public Actor {
as_chat_id(chat_id), as_message_id(message_id), std::move(settings)));
}
}
} else if (op == "srns") {
ReactionNotificationSource message_reactions;
ReactionNotificationSource story_reactions;
int64 sound_id;
bool show_preview;
get_args(args, message_reactions, story_reactions, sound_id, show_preview);
send_request(td_api::make_object<td_api::setReactionNotificationSettings>(
td_api::make_object<td_api::reactionNotificationSettings>(message_reactions, story_reactions, sound_id,
show_preview)));
} else if (op == "rans") {
send_request(td_api::make_object<td_api::resetAllNotificationSettings>());
} else if (op == "rn") {

View File

@ -105,6 +105,7 @@ class LogEvent {
ToggleDialogIsTranslatableOnServer = 0x126,
ToggleDialogViewAsMessagesOnServer = 0x127,
SendQuickReplyShortcutMessages = 0x128,
UpdateReactionNotificationSettingsOnServer = 0x129,
GetChannelDifference = 0x140,
AddMessagePushNotification = 0x200,
EditMessagePushNotification = 0x201,