2019-05-14 01:03:05 +02:00
|
|
|
//
|
2021-01-01 13:57:46 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021
|
2019-05-14 01:03:05 +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/AutoDownloadSettings.h"
|
|
|
|
|
|
|
|
#include "td/telegram/Global.h"
|
|
|
|
#include "td/telegram/net/NetQueryCreator.h"
|
|
|
|
#include "td/telegram/Td.h"
|
|
|
|
#include "td/telegram/telegram_api.h"
|
|
|
|
|
2019-05-14 16:26:13 +02:00
|
|
|
#include "td/utils/buffer.h"
|
2019-05-14 01:03:05 +02:00
|
|
|
#include "td/utils/logging.h"
|
2019-05-14 16:26:13 +02:00
|
|
|
#include "td/utils/Status.h"
|
2019-05-14 01:03:05 +02:00
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
static td_api::object_ptr<td_api::autoDownloadSettings> convert_auto_download_settings(
|
|
|
|
const telegram_api::object_ptr<telegram_api::autoDownloadSettings> &settings) {
|
|
|
|
CHECK(settings != nullptr);
|
|
|
|
auto flags = settings->flags_;
|
|
|
|
auto disabled = (flags & telegram_api::autoDownloadSettings::DISABLED_MASK) != 0;
|
|
|
|
auto video_preload_large = (flags & telegram_api::autoDownloadSettings::VIDEO_PRELOAD_LARGE_MASK) != 0;
|
|
|
|
auto audio_preload_next = (flags & telegram_api::autoDownloadSettings::AUDIO_PRELOAD_NEXT_MASK) != 0;
|
|
|
|
auto phonecalls_less_data = (flags & telegram_api::autoDownloadSettings::PHONECALLS_LESS_DATA_MASK) != 0;
|
|
|
|
return td_api::make_object<td_api::autoDownloadSettings>(
|
2019-12-21 22:40:37 +01:00
|
|
|
!disabled, settings->photo_size_max_, settings->video_size_max_, settings->file_size_max_,
|
|
|
|
settings->video_upload_maxbitrate_, video_preload_large, audio_preload_next, phonecalls_less_data);
|
2019-05-14 01:03:05 +02:00
|
|
|
}
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class GetAutoDownloadSettingsQuery final : public Td::ResultHandler {
|
2019-05-14 01:03:05 +02:00
|
|
|
Promise<td_api::object_ptr<td_api::autoDownloadSettingsPresets>> promise_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit GetAutoDownloadSettingsQuery(Promise<td_api::object_ptr<td_api::autoDownloadSettingsPresets>> &&promise)
|
|
|
|
: promise_(std::move(promise)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void send() {
|
2020-03-15 22:17:11 +01:00
|
|
|
send_query(G()->net_query_creator().create(telegram_api::account_getAutoDownloadSettings()));
|
2019-05-14 01:03:05 +02:00
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void on_result(uint64 id, BufferSlice packet) final {
|
2019-05-14 01:03:05 +02:00
|
|
|
auto result_ptr = fetch_result<telegram_api::account_getAutoDownloadSettings>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
|
|
|
return on_error(id, result_ptr.move_as_error());
|
|
|
|
}
|
|
|
|
|
|
|
|
auto settings = result_ptr.move_as_ok();
|
|
|
|
promise_.set_value(td_api::make_object<td_api::autoDownloadSettingsPresets>(
|
|
|
|
convert_auto_download_settings(settings->low_), convert_auto_download_settings(settings->medium_),
|
|
|
|
convert_auto_download_settings(settings->high_)));
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void on_error(uint64 id, Status status) final {
|
2019-05-14 01:03:05 +02:00
|
|
|
promise_.set_error(std::move(status));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
telegram_api::object_ptr<telegram_api::autoDownloadSettings> get_input_auto_download_settings(
|
|
|
|
const AutoDownloadSettings &settings) {
|
|
|
|
int32 flags = 0;
|
|
|
|
if (!settings.is_enabled) {
|
|
|
|
flags |= telegram_api::autoDownloadSettings::DISABLED_MASK;
|
|
|
|
}
|
|
|
|
if (settings.preload_large_videos) {
|
|
|
|
flags |= telegram_api::autoDownloadSettings::VIDEO_PRELOAD_LARGE_MASK;
|
|
|
|
}
|
|
|
|
if (settings.preload_next_audio) {
|
|
|
|
flags |= telegram_api::autoDownloadSettings::AUDIO_PRELOAD_NEXT_MASK;
|
|
|
|
}
|
|
|
|
if (settings.use_less_data_for_calls) {
|
|
|
|
flags |= telegram_api::autoDownloadSettings::PHONECALLS_LESS_DATA_MASK;
|
|
|
|
}
|
|
|
|
return telegram_api::make_object<telegram_api::autoDownloadSettings>(
|
|
|
|
flags, false /*ignored*/, false /*ignored*/, false /*ignored*/, false /*ignored*/, settings.max_photo_file_size,
|
2019-12-25 18:59:19 +01:00
|
|
|
settings.max_video_file_size, settings.max_other_file_size, settings.video_upload_bitrate);
|
2019-05-14 01:03:05 +02:00
|
|
|
}
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class SaveAutoDownloadSettingsQuery final : public Td::ResultHandler {
|
2019-05-14 01:03:05 +02:00
|
|
|
Promise<Unit> promise_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit SaveAutoDownloadSettingsQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void send(NetType type, const AutoDownloadSettings &settings) {
|
|
|
|
int32 flags = 0;
|
|
|
|
if (type == NetType::MobileRoaming) {
|
|
|
|
flags |= telegram_api::account_saveAutoDownloadSettings::LOW_MASK;
|
|
|
|
}
|
|
|
|
if (type == NetType::WiFi) {
|
|
|
|
flags |= telegram_api::account_saveAutoDownloadSettings::HIGH_MASK;
|
|
|
|
}
|
2020-03-15 22:17:11 +01:00
|
|
|
send_query(G()->net_query_creator().create(telegram_api::account_saveAutoDownloadSettings(
|
|
|
|
flags, false /*ignored*/, false /*ignored*/, get_input_auto_download_settings(settings))));
|
2019-05-14 01:03:05 +02:00
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void on_result(uint64 id, BufferSlice packet) final {
|
2019-05-14 01:03:05 +02:00
|
|
|
auto result_ptr = fetch_result<telegram_api::account_saveAutoDownloadSettings>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
|
|
|
return on_error(id, result_ptr.move_as_error());
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(INFO) << "SaveAutoDownloadSettingsQuery returned " << result_ptr.ok();
|
|
|
|
promise_.set_value(Unit());
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void on_error(uint64 id, Status status) final {
|
2019-05-14 01:03:05 +02:00
|
|
|
promise_.set_error(std::move(status));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
AutoDownloadSettings get_auto_download_settings(const td_api::object_ptr<td_api::autoDownloadSettings> &settings) {
|
|
|
|
CHECK(settings != nullptr);
|
|
|
|
AutoDownloadSettings result;
|
2020-05-23 21:27:24 +02:00
|
|
|
result.max_photo_file_size = -1;
|
|
|
|
result.max_video_file_size = -1;
|
|
|
|
result.max_other_file_size = -1;
|
2019-12-25 18:59:19 +01:00
|
|
|
result.video_upload_bitrate = settings->video_upload_bitrate_;
|
2020-05-23 21:27:24 +02:00
|
|
|
result.is_enabled = false;
|
|
|
|
result.preload_large_videos = false;
|
|
|
|
result.preload_next_audio = false;
|
2019-05-14 01:03:05 +02:00
|
|
|
result.use_less_data_for_calls = settings->use_less_data_for_calls_;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void get_auto_download_settings_presets(Td *td,
|
|
|
|
Promise<td_api::object_ptr<td_api::autoDownloadSettingsPresets>> &&promise) {
|
|
|
|
td->create_handler<GetAutoDownloadSettingsQuery>(std::move(promise))->send();
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_auto_download_settings(Td *td, NetType type, AutoDownloadSettings settings, Promise<Unit> &&promise) {
|
|
|
|
td->create_handler<SaveAutoDownloadSettingsQuery>(std::move(promise))->send(type, settings);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|