Add addSavedNotificationSound.

This commit is contained in:
levlam 2022-04-14 22:39:25 +03:00
parent 178d6414dd
commit 42c6280321
7 changed files with 127 additions and 2 deletions

View File

@ -5184,6 +5184,9 @@ getSavedNotificationSound notification_sound_id:int64 = NotificationSounds;
//@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 Adds a new notification sound to the list of saved notification sounds. The new notification sound is added to the top of the list. If it is already in the list, it is position isn't changed @sound Notification sound file to add
addSavedNotificationSound sound:InputFile = NotificationSound;
//@description Removes a notification sound from the list of saved notification sounds @notification_sound_id Identifier of the notification sound
removeSavedNotificationSound notification_sound_id:int64 = Ok;

View File

@ -7,6 +7,7 @@
#include "td/telegram/NotificationSettingsManager.h"
#include "td/telegram/AccessRights.h"
#include "td/telegram/AudiosManager.h"
#include "td/telegram/AuthManager.h"
#include "td/telegram/ContactsManager.h"
#include "td/telegram/DocumentsManager.h"
@ -729,6 +730,106 @@ void NotificationSettingsManager::send_save_ringtone_query(
->send(ringtone_file_id, file_view.remote_location().as_input_document(), unsave);
}
void NotificationSettingsManager::add_saved_ringtone(td_api::object_ptr<td_api::InputFile> &&input_file,
Promise<td_api::object_ptr<td_api::notificationSound>> &&promise) {
TRY_STATUS_PROMISE(promise, G()->close_status());
if (!are_saved_ringtones_loaded_) {
reload_saved_ringtones(PromiseCreator::lambda([actor_id = actor_id(this), input_file = std::move(input_file),
promise = std::move(promise)](Result<Unit> &&result) mutable {
if (result.is_error()) {
return promise.set_error(result.move_as_error());
}
send_closure(actor_id, &NotificationSettingsManager::add_saved_ringtone, std::move(input_file),
std::move(promise));
}));
return;
}
auto r_file_id = td_->file_manager_->get_input_file_id(FileType::Ringtone, input_file, DialogId(), false, false);
if (r_file_id.is_error()) {
// TODO promise.set_error(r_file_id.move_as_error());
return promise.set_error(Status::Error(400, r_file_id.error().message()));
}
FileId file_id = r_file_id.ok();
auto file_view = td_->file_manager_->get_file_view(file_id);
CHECK(!file_view.empty());
if (file_view.has_remote_location()) {
CHECK(file_view.remote_location().is_document());
if (file_view.main_remote_location().is_web()) {
return promise.set_error(Status::Error(400, "Can't use web document as notification sound"));
}
FileId ringtone_file_id = file_view.file_id();
auto file_type = file_view.get_type();
if (file_type != FileType::Ringtone) {
if (file_type != FileType::Audio && file_type != FileType::VoiceNote) {
return promise.set_error(Status::Error(400, "Unsupported file specified"));
}
auto &remote = file_view.main_remote_location();
ringtone_file_id = td_->file_manager_->register_remote(
FullRemoteFileLocation(FileType::Ringtone, remote.get_id(), remote.get_access_hash(), remote.get_dc_id(),
remote.get_file_reference().str()),
FileLocationSource::FromServer, DialogId(), file_view.size(), file_view.expected_size(),
file_view.suggested_path());
}
if (file_type != FileType::VoiceNote) {
for (auto &saved_ringtone_file_id : saved_ringtone_file_ids_) {
if (ringtone_file_id == saved_ringtone_file_id) {
return promise.set_value(td_->audios_manager_->get_notification_sound_object(ringtone_file_id));
}
}
}
send_save_ringtone_query(
file_view.file_id(), false,
PromiseCreator::lambda(
[actor_id = actor_id(this), file_id = ringtone_file_id, promise = std::move(promise)](
Result<telegram_api::object_ptr<telegram_api::account_SavedRingtone>> &&result) mutable {
if (result.is_error()) {
promise.set_error(result.move_as_error());
} else {
send_closure(actor_id, &NotificationSettingsManager::on_add_saved_ringtone, file_id,
result.move_as_ok(), std::move(promise));
}
}));
return;
}
promise.set_error(Status::Error(400, "Unsupported"));
}
void NotificationSettingsManager::on_add_saved_ringtone(
FileId file_id, telegram_api::object_ptr<telegram_api::account_SavedRingtone> &&saved_ringtone,
Promise<td_api::object_ptr<td_api::notificationSound>> &&promise) {
TRY_STATUS_PROMISE(promise, G()->close_status());
if (saved_ringtone != nullptr && saved_ringtone->get_id() == telegram_api::account_savedRingtoneConverted::ID) {
auto ringtone = move_tl_object_as<telegram_api::account_savedRingtoneConverted>(saved_ringtone);
TRY_RESULT_PROMISE_ASSIGN(promise, file_id, get_ringtone(std::move(ringtone->document_)));
} else {
for (auto &saved_ringtone_file_id : saved_ringtone_file_ids_) {
if (file_id == saved_ringtone_file_id) {
return promise.set_value(td_->audios_manager_->get_notification_sound_object(file_id));
}
}
if (saved_ringtone == nullptr) {
return promise.set_error(Status::Error(500, "Failed to find saved notification sound"));
}
}
reload_saved_ringtones(PromiseCreator::lambda([actor_id = actor_id(this), file_id,
promise = std::move(promise)](Result<Unit> &&result) mutable {
if (result.is_error()) {
promise.set_error(result.move_as_error());
} else {
send_closure(actor_id, &NotificationSettingsManager::on_add_saved_ringtone, file_id, nullptr, std::move(promise));
}
}));
}
void NotificationSettingsManager::remove_saved_ringtone(int64 ringtone_id, Promise<Unit> &&promise) {
if (!are_saved_ringtones_loaded_) {
reload_saved_ringtones(std::move(promise));
@ -761,6 +862,8 @@ void NotificationSettingsManager::remove_saved_ringtone(int64 ringtone_id, Promi
}
void NotificationSettingsManager::on_remove_saved_ringtone(int64 ringtone_id, Promise<Unit> &&promise) {
TRY_STATUS_PROMISE(promise, G()->close_status());
CHECK(are_saved_ringtones_loaded_);
for (auto it = saved_ringtone_file_ids_.begin(); it != saved_ringtone_file_ids_.begin(); ++it) {

View File

@ -46,6 +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 add_saved_ringtone(td_api::object_ptr<td_api::InputFile> &&input_file,
Promise<td_api::object_ptr<td_api::notificationSound>> &&promise);
FileId get_saved_ringtone(int64 ringtone_id, Promise<Unit> &&promise);
vector<FileId> get_saved_ringtones(Promise<Unit> &&promise);
@ -102,6 +105,10 @@ class NotificationSettingsManager final : public Actor {
Result<FileId> get_ringtone(telegram_api::object_ptr<telegram_api::Document> &&ringtone) const;
void on_add_saved_ringtone(FileId file_id,
telegram_api::object_ptr<telegram_api::account_SavedRingtone> &&saved_ringtone,
Promise<td_api::object_ptr<td_api::notificationSound>> &&promise);
void on_remove_saved_ringtone(int64 ringtone_id, Promise<Unit> &&promise);
void on_reload_saved_ringtones(Result<telegram_api::object_ptr<telegram_api::account_SavedRingtones>> &&result);

View File

@ -7156,6 +7156,12 @@ void Td::on_request(uint64 id, const td_api::getSavedNotificationSounds &request
CREATE_NO_ARGS_REQUEST(GetSavedNotificationSoundsRequest);
}
void Td::on_request(uint64 id, td_api::addSavedNotificationSound &request) {
CHECK_IS_USER();
CREATE_REQUEST_PROMISE();
notification_settings_manager_->add_saved_ringtone(std::move(request.sound_), std::move(promise));
}
void Td::on_request(uint64 id, const td_api::removeSavedNotificationSound &request) {
CHECK_IS_USER();
CREATE_REQUEST(RemoveSavedNotificationSoundRequest, request.notification_sound_id_);

View File

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

View File

@ -4496,6 +4496,10 @@ class CliClient final : public Actor {
send_request(td_api::make_object<td_api::getSavedNotificationSound>(notification_sound_id));
} else if (op == "gnss") {
send_request(td_api::make_object<td_api::getSavedNotificationSounds>());
} else if (op == "asns") {
string sound;
get_args(args, sound);
send_request(td_api::make_object<td_api::addSavedNotificationSound>(as_input_file(sound)));
} else if (op == "rns") {
int64 notification_sound_id;
get_args(args, notification_sound_id);

View File

@ -2885,8 +2885,8 @@ void FileManager::cancel_upload(FileId file_id) {
static bool is_document_type(FileType type) {
return type == FileType::Document || type == FileType::Sticker || type == FileType::Audio ||
type == FileType::Animation || type == FileType::Background || type == FileType::DocumentAsFile ||
type == FileType::Ringtone;
type == FileType::Animation || type == FileType::VoiceNote || type == FileType::Background ||
type == FileType::DocumentAsFile || type == FileType::Ringtone;
}
static bool is_background_type(FileType type) {