Add td_api::getSavedNotificationSounds.

This commit is contained in:
levlam 2022-04-13 22:14:40 +03:00
parent 7fff2d2e40
commit 1f18008cc6
9 changed files with 113 additions and 36 deletions

View File

@ -3159,6 +3159,18 @@ notificationGroupTypeSecretChat = NotificationGroupType;
notificationGroupTypeCalls = NotificationGroupType;
//@description Describes a notification sound in MP3 format
//@id Unique identifier of the notification sound
//@duration Duration of the sound, in seconds
//@title Title of the notification sound
//@data Arbitrary data, defined while the sound was uploaded
//@sound File containing the sound
notificationSound id:int64 duration:int32 title:string data:string sound:file = NotificationSound;
//@description Contains a list of notification sounds @notification_sounds A list of notification sounds
notificationSounds notification_sounds:vector<notificationSound> = NotificationSounds;
//@description Contains information about a notification @id Unique persistent identifier of this notification @date Notification date
//@is_silent True, if the notification was initially silent @type Notification type
notification id:int32 date:int32 is_silent:Bool type:NotificationType = Notification;
@ -5166,6 +5178,10 @@ getChatAdministrators chat_id:int53 = ChatAdministrators;
clearAllDraftMessages exclude_secret_chats:Bool = Ok;
//@description Returns list of saved notification sounds. If a sound isn't in the list, then default sound needs to be used
getSavedNotificationSounds = NotificationSounds;
//@description Returns list of chats with non-default notification settings
//@scope If specified, only chats from the scope will be returned; pass null to return chats from all scopes
//@compare_sound Pass true to include in the response chats with only non-default sound

View File

@ -44,6 +44,22 @@ tl_object_ptr<td_api::audio> AudiosManager::get_audio_object(FileId file_id) con
td_->file_manager_->get_file_object(file_id));
}
td_api::object_ptr<td_api::notificationSound> AudiosManager::get_notification_sound_object(FileId file_id) const {
CHECK(file_id.is_valid());
auto it = audios_.find(file_id);
CHECK(it != audios_.end());
auto audio = it->second.get();
CHECK(audio != nullptr);
auto file_view = td_->file_manager_->get_file_view(file_id);
CHECK(!file_view.empty());
CHECK(file_view.get_type() == FileType::Ringtone);
CHECK(file_view.has_remote_location());
auto document_id = file_view.remote_location().get_id();
return td_api::make_object<td_api::notificationSound>(document_id, audio->duration, audio->title, audio->performer,
td_->file_manager_->get_file_object(file_id));
}
FileId AudiosManager::on_get_audio(unique_ptr<Audio> new_audio, bool replace) {
auto file_id = new_audio->file_id;
CHECK(file_id.is_valid());

View File

@ -28,6 +28,8 @@ class AudiosManager {
tl_object_ptr<td_api::audio> get_audio_object(FileId file_id) const;
td_api::object_ptr<td_api::notificationSound> get_notification_sound_object(FileId file_id) const;
void create_audio(FileId file_id, string minithumbnail, PhotoSize thumbnail, string file_name, string mime_type,
int32 duration, string title, string performer, bool replace);

View File

@ -398,7 +398,7 @@ void NotificationSettingsManager::on_scope_unmute_timeout_callback(void *notific
}
void NotificationSettingsManager::timeout_expired() {
reload_ringtones(Promise<Unit>());
reload_saved_ringtones(Promise<Unit>());
}
int32 NotificationSettingsManager::get_scope_mute_until(NotificationSettingsScope scope) const {
@ -624,6 +624,16 @@ bool NotificationSettingsManager::is_active() const {
return !G()->close_flag() && td_->auth_manager_->is_authorized() && !td_->auth_manager_->is_bot();
}
vector<FileId> NotificationSettingsManager::get_saved_ringtones(Promise<Unit> &&promise) {
if (!are_saved_ringtones_loaded_) {
reload_saved_ringtones(std::move(promise));
return {};
}
promise.set_value(Unit());
return saved_ringtone_file_ids_;
}
Result<FileId> NotificationSettingsManager::get_ringtone(
telegram_api::object_ptr<telegram_api::Document> &&ringtone) const {
int32 document_id = ringtone->get_id();
@ -641,47 +651,49 @@ Result<FileId> NotificationSettingsManager::get_ringtone(
return parsed_document.file_id;
}
void NotificationSettingsManager::reload_ringtones(Promise<Unit> &&promise) {
void NotificationSettingsManager::reload_saved_ringtones(Promise<Unit> &&promise) {
if (!is_active()) {
return;
}
reload_ringtone_queries_.push_back(std::move(promise));
if (reload_ringtone_queries_.size() == 1) {
reload_saved_ringtone_queries_.push_back(std::move(promise));
if (reload_saved_ringtone_queries_.size() == 1) {
auto query_promise = PromiseCreator::lambda(
[actor_id = actor_id(this)](Result<telegram_api::object_ptr<telegram_api::account_SavedRingtones>> &&result) {
send_closure(actor_id, &NotificationSettingsManager::on_reload_ringtones, std::move(result));
send_closure(actor_id, &NotificationSettingsManager::on_reload_saved_ringtones, std::move(result));
});
td_->create_handler<GetSavedRingtonesQuery>(std::move(query_promise))->send(ringtone_hash_);
td_->create_handler<GetSavedRingtonesQuery>(std::move(query_promise))->send(saved_ringtone_hash_);
}
}
void NotificationSettingsManager::on_reload_ringtones(
void NotificationSettingsManager::on_reload_saved_ringtones(
Result<telegram_api::object_ptr<telegram_api::account_SavedRingtones>> &&result) {
if (!is_active()) {
set_promises(reload_ringtone_queries_);
are_saved_ringtones_loaded_ = true;
set_promises(reload_saved_ringtone_queries_);
return;
}
if (result.is_error()) {
fail_promises(reload_ringtone_queries_, result.move_as_error());
fail_promises(reload_saved_ringtone_queries_, result.move_as_error());
set_timeout_in(Random::fast(60, 120));
return;
}
set_timeout_in(Random::fast(3600, 4800));
auto ringtones_ptr = result.move_as_ok();
auto constructor_id = ringtones_ptr->get_id();
auto saved_ringtones_ptr = result.move_as_ok();
auto constructor_id = saved_ringtones_ptr->get_id();
if (constructor_id == telegram_api::account_savedRingtonesNotModified::ID) {
set_promises(reload_ringtone_queries_);
are_saved_ringtones_loaded_ = true;
set_promises(reload_saved_ringtone_queries_);
return;
}
CHECK(constructor_id == telegram_api::account_savedRingtones::ID);
auto ringtones = move_tl_object_as<telegram_api::account_savedRingtones>(ringtones_ptr);
auto saved_ringtones = move_tl_object_as<telegram_api::account_savedRingtones>(saved_ringtones_ptr);
auto new_hash = ringtones->hash_;
vector<FileId> new_ringtone_file_ids;
auto new_hash = saved_ringtones->hash_;
vector<FileId> new_saved_ringtone_file_ids;
for (auto &ringtone : ringtones->ringtones_) {
for (auto &ringtone : saved_ringtones->ringtones_) {
auto r_ringtone = get_ringtone(std::move(ringtone));
if (r_ringtone.is_error()) {
LOG(ERROR) << r_ringtone.error().message();
@ -689,15 +701,16 @@ void NotificationSettingsManager::on_reload_ringtones(
continue;
}
new_ringtone_file_ids.push_back(r_ringtone.move_as_ok());
new_saved_ringtone_file_ids.push_back(r_ringtone.move_as_ok());
}
bool need_update = new_ringtone_file_ids != ringtone_file_ids_;
if (need_update || ringtone_hash_ != new_hash) {
ringtone_hash_ = new_hash;
ringtone_file_ids_ = std::move(new_ringtone_file_ids);
bool need_update = new_saved_ringtone_file_ids != saved_ringtone_file_ids_;
if (need_update || saved_ringtone_hash_ != new_hash) {
saved_ringtone_hash_ = new_hash;
saved_ringtone_file_ids_ = std::move(new_saved_ringtone_file_ids);
}
set_promises(reload_ringtone_queries_);
are_saved_ringtones_loaded_ = true;
set_promises(reload_saved_ringtone_queries_);
}
void NotificationSettingsManager::send_get_dialog_notification_settings_query(DialogId dialog_id,
@ -839,9 +852,9 @@ void NotificationSettingsManager::after_get_difference() {
send_get_scope_notification_settings_query(NotificationSettingsScope::Channel, Promise<>());
}
if (td_->is_online() && !are_ringtones_reloaded_) {
are_ringtones_reloaded_ = true;
reload_ringtones(Auto());
if (td_->is_online() && !are_saved_ringtones_reloaded_) {
are_saved_ringtones_reloaded_ = true;
reload_saved_ringtones(Auto());
}
}

View File

@ -46,7 +46,9 @@ class NotificationSettingsManager final : public Actor {
void on_update_scope_notify_settings(NotificationSettingsScope scope,
tl_object_ptr<telegram_api::peerNotifySettings> &&peer_notify_settings);
void reload_ringtones(Promise<Unit> &&promise);
vector<FileId> get_saved_ringtones(Promise<Unit> &&promise);
void reload_saved_ringtones(Promise<Unit> &&promise);
void send_get_dialog_notification_settings_query(DialogId dialog_id, Promise<Unit> &&promise);
@ -93,7 +95,7 @@ class NotificationSettingsManager final : public Actor {
Result<FileId> get_ringtone(telegram_api::object_ptr<telegram_api::Document> &&ringtone) const;
void on_reload_ringtones(Result<telegram_api::object_ptr<telegram_api::account_SavedRingtones>> &&result);
void on_reload_saved_ringtones(Result<telegram_api::object_ptr<telegram_api::account_SavedRingtones>> &&result);
ScopeNotificationSettings *get_scope_notification_settings(NotificationSettingsScope scope);
@ -124,7 +126,8 @@ class NotificationSettingsManager final : public Actor {
ActorShared<> parent_;
bool is_inited_ = false;
bool are_ringtones_reloaded_ = false;
bool are_saved_ringtones_loaded_ = false;
bool are_saved_ringtones_reloaded_ = false;
ScopeNotificationSettings users_notification_settings_;
ScopeNotificationSettings chats_notification_settings_;
@ -132,10 +135,10 @@ class NotificationSettingsManager final : public Actor {
MultiTimeout scope_unmute_timeout_{"ScopeUnmuteTimeout"};
int64 ringtone_hash_ = 0;
vector<FileId> ringtone_file_ids_;
int64 saved_ringtone_hash_ = 0;
vector<FileId> saved_ringtone_file_ids_;
vector<Promise<Unit>> reload_ringtone_queries_;
vector<Promise<Unit>> reload_saved_ringtone_queries_;
FlatHashMap<DialogId, vector<Promise<Unit>>, DialogIdHash> get_dialog_notification_settings_queries_;
};

View File

@ -2555,10 +2555,9 @@ class GetSavedAnimationsRequest final : public RequestActor<> {
}
void do_send_result() final {
send_result(
make_tl_object<td_api::animations>(transform(std::move(animation_ids_), [td_ = td_](FileId animation_id) {
return td_->animations_manager_->get_animation_object(animation_id);
})));
send_result(make_tl_object<td_api::animations>(transform(animation_ids_, [td = td_](FileId animation_id) {
return td->animations_manager_->get_animation_object(animation_id);
})));
}
public:
@ -2594,6 +2593,25 @@ class RemoveSavedAnimationRequest final : public RequestOnceActor {
}
};
class GetSavedNotificationSoundsRequest final : public RequestActor<> {
vector<FileId> ringtone_file_ids_;
void do_run(Promise<Unit> &&promise) final {
ringtone_file_ids_ = td_->notification_settings_manager_->get_saved_ringtones(std::move(promise));
}
void do_send_result() final {
send_result(td_api::make_object<td_api::notificationSounds>(
transform(ringtone_file_ids_, [td = td_](FileId ringtone_file_id) {
return td->audios_manager_->get_notification_sound_object(ringtone_file_id);
})));
}
public:
GetSavedNotificationSoundsRequest(ActorShared<Td> td, uint64 request_id) : RequestActor(std::move(td), request_id) {
}
};
class GetInlineQueryResultsRequest final : public RequestOnceActor {
UserId bot_user_id_;
DialogId dialog_id_;
@ -7095,6 +7113,11 @@ void Td::on_request(uint64 id, td_api::removeSavedAnimation &request) {
CREATE_REQUEST(RemoveSavedAnimationRequest, std::move(request.animation_));
}
void Td::on_request(uint64 id, const td_api::getSavedNotificationSounds &request) {
CHECK_IS_USER();
CREATE_NO_ARGS_REQUEST(GetSavedNotificationSoundsRequest);
}
void Td::on_request(uint64 id, const td_api::getChatNotificationSettingsExceptions &request) {
CHECK_IS_USER();
bool filter_scope = false;

View File

@ -1104,6 +1104,8 @@ class Td final : public Actor {
void on_request(uint64 id, td_api::removeFavoriteSticker &request);
void on_request(uint64 id, const td_api::getSavedNotificationSounds &request);
void on_request(uint64 id, const td_api::getChatNotificationSettingsExceptions &request);
void on_request(uint64 id, const td_api::getScopeNotificationSettings &request);

View File

@ -3313,7 +3313,7 @@ void UpdatesManager::on_update(tl_object_ptr<telegram_api::updatePendingJoinRequ
}
void UpdatesManager::on_update(tl_object_ptr<telegram_api::updateSavedRingtones> update, Promise<Unit> &&promise) {
td_->notification_settings_manager_->reload_ringtones(std::move(promise));
td_->notification_settings_manager_->reload_saved_ringtones(std::move(promise));
}
// unsupported updates

View File

@ -4490,6 +4490,8 @@ class CliClient final : public Actor {
int64 profile_photo_id;
get_args(args, profile_photo_id);
send_request(td_api::make_object<td_api::deleteProfilePhoto>(profile_photo_id));
} else if (op == "gsnss") {
send_request(td_api::make_object<td_api::getSavedNotificationSounds>());
} else if (op == "gcnse" || op == "gcnses") {
send_request(td_api::make_object<td_api::getChatNotificationSettingsExceptions>(
get_notification_settings_scope(args), op == "gcnses"));