2018-11-30 12:49:30 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop application for the Telegram messaging service.
|
|
|
|
|
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
|
|
|
*/
|
|
|
|
#include "data/data_auto_download.h"
|
|
|
|
|
|
|
|
#include "data/data_peer.h"
|
|
|
|
#include "data/data_photo.h"
|
|
|
|
#include "data/data_document.h"
|
|
|
|
|
2019-09-04 07:19:15 +00:00
|
|
|
#include <QtCore/QBuffer>
|
|
|
|
|
2018-11-30 12:49:30 +00:00
|
|
|
namespace Data {
|
|
|
|
namespace AutoDownload {
|
|
|
|
namespace {
|
|
|
|
|
2022-05-10 14:22:28 +00:00
|
|
|
constexpr auto kDefaultMaxSize = 8 * int64(1024 * 1024);
|
|
|
|
constexpr auto kDefaultAutoPlaySize = 50 * int64(1024 * 1024);
|
2019-12-24 10:09:04 +00:00
|
|
|
constexpr auto kVersion1 = char(1);
|
|
|
|
constexpr auto kVersion = char(2);
|
2018-11-30 12:49:30 +00:00
|
|
|
|
|
|
|
template <typename Enum>
|
|
|
|
auto enums_view(int from, int till) {
|
2021-03-13 12:12:08 +00:00
|
|
|
using namespace ranges::views;
|
2018-11-30 12:49:30 +00:00
|
|
|
return ints(from, till) | transform([](int index) {
|
|
|
|
return static_cast<Enum>(index);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Enum>
|
|
|
|
auto enums_view(int till) {
|
|
|
|
return enums_view<Enum>(0, till);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetDefaultsForSource(Full &data, Source source) {
|
|
|
|
data.setBytesLimit(source, Type::Photo, kDefaultMaxSize);
|
|
|
|
data.setBytesLimit(source, Type::VoiceMessage, kDefaultMaxSize);
|
2019-12-24 10:09:04 +00:00
|
|
|
data.setBytesLimit(
|
|
|
|
source,
|
|
|
|
Type::AutoPlayVideoMessage,
|
|
|
|
kDefaultAutoPlaySize);
|
|
|
|
data.setBytesLimit(source, Type::AutoPlayGIF, kDefaultAutoPlaySize);
|
2018-12-26 05:45:37 +00:00
|
|
|
const auto channelsFileLimit = (source == Source::Channel)
|
|
|
|
? 0
|
|
|
|
: kDefaultMaxSize;
|
|
|
|
data.setBytesLimit(source, Type::File, channelsFileLimit);
|
2019-12-24 10:09:04 +00:00
|
|
|
data.setBytesLimit(source, Type::AutoPlayVideo, kDefaultAutoPlaySize);
|
2018-12-26 05:45:37 +00:00
|
|
|
data.setBytesLimit(source, Type::Music, channelsFileLimit);
|
2018-11-30 12:49:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const Full &Defaults() {
|
|
|
|
static auto Result = [] {
|
|
|
|
auto result = Full::FullDisabled();
|
|
|
|
for (const auto source : enums_view<Source>(kSourcesCount)) {
|
|
|
|
SetDefaultsForSource(result, source);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}();
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
Source SourceFromPeer(not_null<PeerData*> peer) {
|
|
|
|
if (peer->isUser()) {
|
|
|
|
return Source::User;
|
|
|
|
} else if (peer->isChat() || peer->isMegagroup()) {
|
|
|
|
return Source::Group;
|
|
|
|
} else {
|
|
|
|
return Source::Channel;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-24 10:09:04 +00:00
|
|
|
Type AutoPlayTypeFromDocument(not_null<DocumentData*> document) {
|
|
|
|
return document->isVideoFile()
|
|
|
|
? Type::AutoPlayVideo
|
|
|
|
: document->isVideoMessage()
|
|
|
|
? Type::AutoPlayVideoMessage
|
|
|
|
: Type::AutoPlayGIF;
|
2018-11-30 12:49:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2022-05-10 14:22:28 +00:00
|
|
|
void Single::setBytesLimit(int64 bytesLimit) {
|
2018-11-30 12:49:30 +00:00
|
|
|
Expects(bytesLimit >= 0 && bytesLimit <= kMaxBytesLimit);
|
|
|
|
|
2022-05-10 14:22:28 +00:00
|
|
|
_limit = int32(uint32(bytesLimit));
|
|
|
|
|
|
|
|
Ensures(hasValue());
|
2018-11-30 12:49:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Single::hasValue() const {
|
2022-05-10 14:22:28 +00:00
|
|
|
return (_limit != -1);
|
2018-11-30 12:49:30 +00:00
|
|
|
}
|
|
|
|
|
2022-05-10 14:22:28 +00:00
|
|
|
bool Single::shouldDownload(int64 fileSize) const {
|
2018-11-30 12:49:30 +00:00
|
|
|
Expects(hasValue());
|
|
|
|
|
2022-05-10 14:22:28 +00:00
|
|
|
const auto realLimit = bytesLimit();
|
|
|
|
return (realLimit > 0) && (fileSize <= realLimit);
|
2018-11-30 12:49:30 +00:00
|
|
|
}
|
|
|
|
|
2022-05-10 14:22:28 +00:00
|
|
|
int64 Single::bytesLimit() const {
|
2018-11-30 12:49:30 +00:00
|
|
|
Expects(hasValue());
|
|
|
|
|
2022-05-10 14:22:28 +00:00
|
|
|
return uint32(_limit);
|
2018-11-30 12:49:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
qint32 Single::serialize() const {
|
|
|
|
return _limit;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Single::setFromSerialized(qint32 serialized) {
|
2022-05-10 14:22:28 +00:00
|
|
|
auto realLimit = quint32(serialized);
|
|
|
|
if (serialized != -1 && int64(realLimit) > kMaxBytesLimit) {
|
2018-11-30 12:49:30 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
_limit = serialized;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Single &Set::single(Type type) const {
|
|
|
|
Expects(static_cast<int>(type) >= 0
|
|
|
|
&& static_cast<int>(type) < kTypesCount);
|
|
|
|
|
|
|
|
return _data[static_cast<int>(type)];
|
|
|
|
}
|
|
|
|
|
|
|
|
Single &Set::single(Type type) {
|
|
|
|
return const_cast<Single&>(static_cast<const Set*>(this)->single(type));
|
|
|
|
}
|
|
|
|
|
2022-05-10 14:22:28 +00:00
|
|
|
void Set::setBytesLimit(Type type, int64 bytesLimit) {
|
2018-11-30 12:49:30 +00:00
|
|
|
single(type).setBytesLimit(bytesLimit);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Set::hasValue(Type type) const {
|
|
|
|
return single(type).hasValue();
|
|
|
|
}
|
|
|
|
|
2022-05-10 14:22:28 +00:00
|
|
|
bool Set::shouldDownload(Type type, int64 fileSize) const {
|
2018-11-30 12:49:30 +00:00
|
|
|
return single(type).shouldDownload(fileSize);
|
|
|
|
}
|
|
|
|
|
2022-05-10 14:22:28 +00:00
|
|
|
int64 Set::bytesLimit(Type type) const {
|
2018-11-30 12:49:30 +00:00
|
|
|
return single(type).bytesLimit();
|
|
|
|
}
|
|
|
|
|
|
|
|
qint32 Set::serialize(Type type) const {
|
|
|
|
return single(type).serialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Set::setFromSerialized(Type type, qint32 serialized) {
|
|
|
|
if (static_cast<int>(type) < 0
|
|
|
|
|| static_cast<int>(type) >= kTypesCount) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return single(type).setFromSerialized(serialized);
|
|
|
|
}
|
|
|
|
|
|
|
|
const Set &Full::set(Source source) const {
|
|
|
|
Expects(static_cast<int>(source) >= 0
|
|
|
|
&& static_cast<int>(source) < kSourcesCount);
|
|
|
|
|
|
|
|
return _data[static_cast<int>(source)];
|
|
|
|
}
|
|
|
|
|
|
|
|
Set &Full::set(Source source) {
|
|
|
|
return const_cast<Set&>(static_cast<const Full*>(this)->set(source));
|
|
|
|
}
|
|
|
|
|
|
|
|
const Set &Full::setOrDefault(Source source, Type type) const {
|
|
|
|
const auto &my = set(source);
|
|
|
|
const auto &result = my.hasValue(type) ? my : Defaults().set(source);
|
|
|
|
|
|
|
|
Ensures(result.hasValue(type));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-05-10 14:22:28 +00:00
|
|
|
void Full::setBytesLimit(Source source, Type type, int64 bytesLimit) {
|
2018-11-30 12:49:30 +00:00
|
|
|
set(source).setBytesLimit(type, bytesLimit);
|
|
|
|
}
|
|
|
|
|
2022-05-10 14:22:28 +00:00
|
|
|
bool Full::shouldDownload(Source source, Type type, int64 fileSize) const {
|
2019-12-20 12:56:39 +00:00
|
|
|
if (ranges::find(kStreamedTypes, type) != end(kStreamedTypes)) {
|
2019-03-01 13:11:47 +00:00
|
|
|
// With streaming we disable autodownload and hide them in Settings.
|
|
|
|
return false;
|
|
|
|
}
|
2018-11-30 12:49:30 +00:00
|
|
|
return setOrDefault(source, type).shouldDownload(type, fileSize);
|
|
|
|
}
|
|
|
|
|
2022-05-10 14:22:28 +00:00
|
|
|
int64 Full::bytesLimit(Source source, Type type) const {
|
2018-11-30 12:49:30 +00:00
|
|
|
return setOrDefault(source, type).bytesLimit(type);
|
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray Full::serialize() const {
|
|
|
|
auto result = QByteArray();
|
|
|
|
auto size = sizeof(qint8);
|
|
|
|
size += kSourcesCount * kTypesCount * sizeof(qint32);
|
|
|
|
result.reserve(size);
|
|
|
|
{
|
|
|
|
auto buffer = QBuffer(&result);
|
|
|
|
buffer.open(QIODevice::WriteOnly);
|
|
|
|
auto stream = QDataStream(&buffer);
|
|
|
|
stream << qint8(kVersion);
|
|
|
|
for (const auto source : enums_view<Source>(kSourcesCount)) {
|
|
|
|
for (const auto type : enums_view<Type>(kTypesCount)) {
|
|
|
|
stream << set(source).serialize(type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Full::setFromSerialized(const QByteArray &serialized) {
|
|
|
|
if (serialized.isEmpty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto stream = QDataStream(serialized);
|
|
|
|
auto version = qint8();
|
|
|
|
stream >> version;
|
|
|
|
if (stream.status() != QDataStream::Ok) {
|
|
|
|
return false;
|
2019-12-24 10:09:04 +00:00
|
|
|
} else if (version != kVersion && version != kVersion1) {
|
2018-11-30 12:49:30 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
auto temp = Full();
|
|
|
|
for (const auto source : enums_view<Source>(kSourcesCount)) {
|
|
|
|
for (const auto type : enums_view<Type>(kTypesCount)) {
|
|
|
|
auto value = qint32();
|
|
|
|
stream >> value;
|
|
|
|
if (!temp.set(source).setFromSerialized(type, value)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-24 10:09:04 +00:00
|
|
|
if (version == kVersion1) {
|
|
|
|
for (const auto source : enums_view<Source>(kSourcesCount)) {
|
|
|
|
for (const auto type : kAutoPlayTypes) {
|
|
|
|
temp.setBytesLimit(source, type, std::max(
|
|
|
|
temp.bytesLimit(source, type),
|
|
|
|
kDefaultAutoPlaySize));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-11-30 12:49:30 +00:00
|
|
|
_data = temp._data;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Full Full::FullDisabled() {
|
|
|
|
auto result = Full();
|
|
|
|
for (const auto source : enums_view<Source>(kSourcesCount)) {
|
|
|
|
for (const auto type : enums_view<Type>(kTypesCount)) {
|
|
|
|
result.setBytesLimit(source, type, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Should(
|
|
|
|
const Full &data,
|
2019-12-26 14:14:35 +00:00
|
|
|
Source source,
|
2018-11-30 12:49:30 +00:00
|
|
|
not_null<DocumentData*> document) {
|
2020-05-22 06:59:32 +00:00
|
|
|
if (document->sticker() || document->isGifv()) {
|
2018-11-30 12:49:30 +00:00
|
|
|
return true;
|
2019-12-26 14:14:35 +00:00
|
|
|
} else if (document->isVoiceMessage()
|
|
|
|
|| document->isVideoMessage()
|
|
|
|
|| document->isSong()
|
|
|
|
|| document->isVideoFile()) {
|
|
|
|
return false;
|
2018-11-30 12:49:30 +00:00
|
|
|
}
|
2019-12-26 14:14:35 +00:00
|
|
|
return data.shouldDownload(source, Type::File, document->size);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Should(
|
|
|
|
const Full &data,
|
|
|
|
not_null<PeerData*> peer,
|
|
|
|
not_null<DocumentData*> document) {
|
|
|
|
return Should(data, SourceFromPeer(peer), document);
|
2018-11-30 12:49:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Should(
|
|
|
|
const Full &data,
|
|
|
|
not_null<DocumentData*> document) {
|
|
|
|
if (document->sticker()) {
|
|
|
|
return true;
|
|
|
|
}
|
2019-12-26 14:14:35 +00:00
|
|
|
return Should(data, Source::User, document)
|
|
|
|
|| Should(data, Source::Group, document)
|
|
|
|
|| Should(data, Source::Channel, document);
|
2018-11-30 12:49:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Should(
|
|
|
|
const Full &data,
|
|
|
|
not_null<PeerData*> peer,
|
2020-05-25 14:16:04 +00:00
|
|
|
not_null<PhotoData*> photo) {
|
2018-11-30 12:49:30 +00:00
|
|
|
return data.shouldDownload(
|
|
|
|
SourceFromPeer(peer),
|
|
|
|
Type::Photo,
|
2020-05-25 14:16:04 +00:00
|
|
|
photo->imageByteSize(PhotoSize::Large));
|
2018-11-30 12:49:30 +00:00
|
|
|
}
|
|
|
|
|
2019-12-24 10:09:04 +00:00
|
|
|
bool ShouldAutoPlay(
|
|
|
|
const Full &data,
|
|
|
|
not_null<PeerData*> peer,
|
|
|
|
not_null<DocumentData*> document) {
|
2022-01-20 12:58:32 +00:00
|
|
|
return document->sticker() || data.shouldDownload(
|
2019-12-24 10:09:04 +00:00
|
|
|
SourceFromPeer(peer),
|
|
|
|
AutoPlayTypeFromDocument(document),
|
|
|
|
document->size);
|
|
|
|
}
|
|
|
|
|
2020-07-03 18:57:30 +00:00
|
|
|
bool ShouldAutoPlay(
|
|
|
|
const Full &data,
|
|
|
|
not_null<PeerData*> peer,
|
|
|
|
not_null<PhotoData*> photo) {
|
|
|
|
const auto source = SourceFromPeer(peer);
|
2022-06-03 11:58:02 +00:00
|
|
|
const auto size = photo->videoByteSize(PhotoSize::Large);
|
2020-07-03 18:57:30 +00:00
|
|
|
return photo->hasVideo()
|
|
|
|
&& (data.shouldDownload(source, Type::AutoPlayGIF, size)
|
|
|
|
|| data.shouldDownload(source, Type::AutoPlayVideo, size)
|
|
|
|
|| data.shouldDownload(source, Type::AutoPlayVideoMessage, size));
|
|
|
|
}
|
|
|
|
|
2019-12-24 10:09:04 +00:00
|
|
|
Full WithDisabledAutoPlay(const Full &data) {
|
|
|
|
auto result = data;
|
|
|
|
for (const auto source : enums_view<Source>(kSourcesCount)) {
|
|
|
|
for (const auto type : kAutoPlayTypes) {
|
|
|
|
result.setBytesLimit(source, type, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-11-30 12:49:30 +00:00
|
|
|
} // namespace AutoDownload
|
|
|
|
} // namespace Data
|