2016-04-09 05:57:55 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2016-04-09 05:57:55 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2016-04-09 05:57:55 +00:00
|
|
|
*/
|
2017-03-04 10:23:56 +00:00
|
|
|
#include "storage/serialize_common.h"
|
2016-04-09 05:57:55 +00:00
|
|
|
|
2019-07-24 11:45:24 +00:00
|
|
|
#include "main/main_session.h"
|
2019-01-04 11:09:48 +00:00
|
|
|
#include "data/data_channel.h"
|
|
|
|
#include "data/data_chat.h"
|
|
|
|
#include "data/data_user.h"
|
2019-01-18 12:27:37 +00:00
|
|
|
#include "data/data_session.h"
|
2018-10-23 09:44:42 +00:00
|
|
|
#include "ui/image/image.h"
|
2019-09-13 06:06:02 +00:00
|
|
|
#include "app.h"
|
2018-09-11 14:36:09 +00:00
|
|
|
|
2016-04-09 05:57:55 +00:00
|
|
|
namespace Serialize {
|
2019-03-22 14:19:43 +00:00
|
|
|
namespace {
|
2016-04-09 05:57:55 +00:00
|
|
|
|
2019-03-22 14:19:43 +00:00
|
|
|
constexpr auto kModernImageLocationTag = std::numeric_limits<qint32>::min();
|
2016-04-09 05:57:55 +00:00
|
|
|
|
2019-03-22 14:19:43 +00:00
|
|
|
} // namespace
|
|
|
|
|
2019-08-23 13:52:59 +00:00
|
|
|
void writeColor(QDataStream &stream, const QColor &color) {
|
|
|
|
stream << (quint32(uchar(color.red()))
|
|
|
|
| (quint32(uchar(color.green())) << 8)
|
|
|
|
| (quint32(uchar(color.blue())) << 16)
|
|
|
|
| (quint32(uchar(color.alpha())) << 24));
|
|
|
|
}
|
|
|
|
|
|
|
|
QColor readColor(QDataStream &stream) {
|
|
|
|
auto value = quint32();
|
|
|
|
stream >> value;
|
|
|
|
return QColor(
|
|
|
|
int(value & 0xFFU),
|
|
|
|
int((value >> 8) & 0xFFU),
|
|
|
|
int((value >> 16) & 0xFFU),
|
|
|
|
int((value >> 24) & 0xFFU));
|
|
|
|
}
|
|
|
|
|
2019-03-22 14:19:43 +00:00
|
|
|
std::optional<StorageImageLocation> readLegacyStorageImageLocationOrTag(
|
2018-07-13 16:49:46 +00:00
|
|
|
int streamAppVersion,
|
|
|
|
QDataStream &stream) {
|
2016-04-09 05:57:55 +00:00
|
|
|
qint32 width, height, dc, local;
|
|
|
|
quint64 volume, secret;
|
2018-07-13 16:49:46 +00:00
|
|
|
QByteArray fileReference;
|
2019-03-22 14:19:43 +00:00
|
|
|
stream >> width;
|
|
|
|
if (width == kModernImageLocationTag) {
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
stream >> height >> dc >> volume >> local >> secret;
|
2018-07-16 19:43:44 +00:00
|
|
|
if (streamAppVersion >= 1003013) {
|
2018-07-13 16:49:46 +00:00
|
|
|
stream >> fileReference;
|
|
|
|
}
|
2019-03-22 14:19:43 +00:00
|
|
|
if (stream.status() != QDataStream::Ok) {
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
2018-07-13 16:49:46 +00:00
|
|
|
return StorageImageLocation(
|
2019-03-22 14:19:43 +00:00
|
|
|
StorageFileLocation(
|
|
|
|
dc,
|
|
|
|
UserId(0),
|
|
|
|
MTP_inputFileLocation(
|
|
|
|
MTP_long(volume),
|
|
|
|
MTP_int(local),
|
|
|
|
MTP_long(secret),
|
|
|
|
MTP_bytes(fileReference))),
|
2018-07-13 16:49:46 +00:00
|
|
|
width,
|
2019-03-22 14:19:43 +00:00
|
|
|
height);
|
2016-04-09 05:57:55 +00:00
|
|
|
}
|
|
|
|
|
2018-07-13 16:49:46 +00:00
|
|
|
int storageImageLocationSize(const StorageImageLocation &location) {
|
2019-03-22 14:19:43 +00:00
|
|
|
// Modern image location tag + (size + content) of the serialization.
|
|
|
|
return sizeof(qint32) * 2 + location.serializeSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
void writeStorageImageLocation(
|
|
|
|
QDataStream &stream,
|
|
|
|
const StorageImageLocation &location) {
|
|
|
|
stream << kModernImageLocationTag << location.serialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<StorageImageLocation> readStorageImageLocation(
|
|
|
|
int streamAppVersion,
|
|
|
|
QDataStream &stream) {
|
|
|
|
const auto legacy = readLegacyStorageImageLocationOrTag(
|
|
|
|
streamAppVersion,
|
|
|
|
stream);
|
|
|
|
if (legacy) {
|
|
|
|
return legacy;
|
|
|
|
}
|
|
|
|
auto serialized = QByteArray();
|
|
|
|
stream >> serialized;
|
|
|
|
return (stream.status() == QDataStream::Ok)
|
|
|
|
? StorageImageLocation::FromSerialized(serialized)
|
|
|
|
: std::nullopt;
|
2016-04-09 05:57:55 +00:00
|
|
|
}
|
|
|
|
|
2018-09-11 14:36:09 +00:00
|
|
|
uint32 peerSize(not_null<PeerData*> peer) {
|
|
|
|
uint32 result = sizeof(quint64)
|
|
|
|
+ sizeof(quint64)
|
|
|
|
+ storageImageLocationSize(peer->userpicLocation());
|
|
|
|
if (peer->isUser()) {
|
|
|
|
UserData *user = peer->asUser();
|
|
|
|
|
|
|
|
// first + last + phone + username + access
|
|
|
|
result += stringSize(user->firstName) + stringSize(user->lastName) + stringSize(user->phone()) + stringSize(user->username) + sizeof(quint64);
|
|
|
|
|
|
|
|
// flags
|
|
|
|
if (AppVersion >= 9012) {
|
|
|
|
result += sizeof(qint32);
|
|
|
|
}
|
|
|
|
|
|
|
|
// onlineTill + contact + botInfoVersion
|
|
|
|
result += sizeof(qint32) + sizeof(qint32) + sizeof(qint32);
|
|
|
|
} else if (peer->isChat()) {
|
|
|
|
ChatData *chat = peer->asChat();
|
|
|
|
|
|
|
|
// name + count + date + version + admin + old forbidden + left + inviteLink
|
|
|
|
result += stringSize(chat->name) + sizeof(qint32) + sizeof(qint32) + sizeof(qint32) + sizeof(qint32) + sizeof(qint32) + sizeof(quint32) + stringSize(chat->inviteLink());
|
|
|
|
} else if (peer->isChannel()) {
|
|
|
|
ChannelData *channel = peer->asChannel();
|
|
|
|
|
|
|
|
// name + access + date + version + old forbidden + flags + inviteLink
|
|
|
|
result += stringSize(channel->name) + sizeof(quint64) + sizeof(qint32) + sizeof(qint32) + sizeof(qint32) + sizeof(quint32) + stringSize(channel->inviteLink());
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void writePeer(QDataStream &stream, PeerData *peer) {
|
|
|
|
stream << quint64(peer->id) << quint64(peer->userpicPhotoId());
|
|
|
|
writeStorageImageLocation(stream, peer->userpicLocation());
|
|
|
|
if (const auto user = peer->asUser()) {
|
|
|
|
stream
|
|
|
|
<< user->firstName
|
|
|
|
<< user->lastName
|
|
|
|
<< user->phone()
|
|
|
|
<< user->username
|
|
|
|
<< quint64(user->accessHash());
|
|
|
|
if (AppVersion >= 9012) {
|
|
|
|
stream << qint32(user->flags());
|
|
|
|
}
|
|
|
|
if (AppVersion >= 9016) {
|
2019-07-18 08:51:11 +00:00
|
|
|
const auto botInlinePlaceholder = user->isBot()
|
2018-09-11 14:36:09 +00:00
|
|
|
? user->botInfo->inlinePlaceholder
|
|
|
|
: QString();
|
|
|
|
stream << botInlinePlaceholder;
|
|
|
|
}
|
|
|
|
stream
|
|
|
|
<< qint32(user->onlineTill)
|
2019-06-06 16:48:42 +00:00
|
|
|
<< qint32(user->isContact() ? 1 : 0)
|
2019-07-18 08:51:11 +00:00
|
|
|
<< qint32(user->isBot() ? user->botInfo->version : -1);
|
2018-09-11 14:36:09 +00:00
|
|
|
} else if (const auto chat = peer->asChat()) {
|
|
|
|
stream
|
|
|
|
<< chat->name
|
|
|
|
<< qint32(chat->count)
|
|
|
|
<< qint32(chat->date)
|
2019-01-13 08:03:34 +00:00
|
|
|
<< qint32(chat->version())
|
2018-09-11 14:36:09 +00:00
|
|
|
<< qint32(chat->creator)
|
|
|
|
<< qint32(0)
|
|
|
|
<< quint32(chat->flags())
|
|
|
|
<< chat->inviteLink();
|
|
|
|
} else if (const auto channel = peer->asChannel()) {
|
|
|
|
stream
|
|
|
|
<< channel->name
|
|
|
|
<< quint64(channel->access)
|
|
|
|
<< qint32(channel->date)
|
2019-01-13 08:03:34 +00:00
|
|
|
<< qint32(channel->version())
|
2018-09-11 14:36:09 +00:00
|
|
|
<< qint32(0)
|
|
|
|
<< quint32(channel->flags())
|
|
|
|
<< channel->inviteLink();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PeerData *readPeer(int streamAppVersion, QDataStream &stream) {
|
|
|
|
quint64 peerId = 0, photoId = 0;
|
|
|
|
stream >> peerId >> photoId;
|
|
|
|
if (!peerId) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-03-22 14:19:43 +00:00
|
|
|
const auto userpic = readStorageImageLocation(streamAppVersion, stream);
|
|
|
|
auto userpicAccessHash = uint64(0);
|
|
|
|
if (!userpic) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-09-11 14:36:09 +00:00
|
|
|
|
2019-03-22 14:19:43 +00:00
|
|
|
const auto loaded = Auth().data().peerLoaded(peerId);
|
|
|
|
const auto result = loaded ? loaded : Auth().data().peer(peerId).get();
|
|
|
|
if (!loaded) {
|
2018-09-11 14:36:09 +00:00
|
|
|
result->loadedStatus = PeerData::FullLoaded;
|
|
|
|
}
|
|
|
|
if (const auto user = result->asUser()) {
|
|
|
|
QString first, last, phone, username, inlinePlaceholder;
|
|
|
|
quint64 access;
|
|
|
|
qint32 flags = 0, onlineTill, contact, botInfoVersion;
|
|
|
|
stream >> first >> last >> phone >> username >> access;
|
|
|
|
if (streamAppVersion >= 9012) {
|
|
|
|
stream >> flags;
|
|
|
|
}
|
|
|
|
if (streamAppVersion >= 9016) {
|
|
|
|
stream >> inlinePlaceholder;
|
|
|
|
}
|
|
|
|
stream >> onlineTill >> contact >> botInfoVersion;
|
|
|
|
|
2019-03-22 14:19:43 +00:00
|
|
|
userpicAccessHash = access;
|
|
|
|
|
2019-03-12 10:36:33 +00:00
|
|
|
const auto showPhone = !user->isServiceUser()
|
2018-09-11 14:36:09 +00:00
|
|
|
&& (user->id != Auth().userPeerId())
|
|
|
|
&& (contact <= 0);
|
|
|
|
const auto pname = (showPhone && !phone.isEmpty())
|
|
|
|
? App::formatPhone(phone)
|
|
|
|
: QString();
|
|
|
|
|
2019-03-22 14:19:43 +00:00
|
|
|
if (!loaded) {
|
2018-09-11 14:36:09 +00:00
|
|
|
user->setPhone(phone);
|
|
|
|
user->setName(first, last, pname, username);
|
|
|
|
|
|
|
|
user->setFlags(MTPDuser::Flags::from_raw(flags));
|
|
|
|
user->setAccessHash(access);
|
|
|
|
user->onlineTill = onlineTill;
|
2019-06-06 16:48:42 +00:00
|
|
|
user->setIsContact(contact == 1);
|
2018-09-11 14:36:09 +00:00
|
|
|
user->setBotInfoVersion(botInfoVersion);
|
2019-07-18 08:51:11 +00:00
|
|
|
if (!inlinePlaceholder.isEmpty() && user->isBot()) {
|
2018-09-11 14:36:09 +00:00
|
|
|
user->botInfo->inlinePlaceholder = inlinePlaceholder;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (user->id == Auth().userPeerId()) {
|
|
|
|
user->input = MTP_inputPeerSelf();
|
|
|
|
user->inputUser = MTP_inputUserSelf();
|
|
|
|
} else {
|
|
|
|
user->input = MTP_inputPeerUser(MTP_int(peerToUser(user->id)), MTP_long(user->accessHash()));
|
|
|
|
user->inputUser = MTP_inputUser(MTP_int(peerToUser(user->id)), MTP_long(user->accessHash()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (const auto chat = result->asChat()) {
|
|
|
|
QString name, inviteLink;
|
|
|
|
qint32 count, date, version, creator, oldForbidden;
|
|
|
|
quint32 flagsData, flags;
|
|
|
|
stream >> name >> count >> date >> version >> creator >> oldForbidden >> flagsData >> inviteLink;
|
|
|
|
|
|
|
|
if (streamAppVersion >= 9012) {
|
|
|
|
flags = flagsData;
|
|
|
|
} else {
|
|
|
|
// flagsData was haveLeft
|
|
|
|
flags = (flagsData == 1)
|
|
|
|
? MTPDchat::Flags(MTPDchat::Flag::f_left)
|
|
|
|
: MTPDchat::Flags(0);
|
|
|
|
}
|
|
|
|
if (oldForbidden) {
|
|
|
|
flags |= quint32(MTPDchat_ClientFlag::f_forbidden);
|
|
|
|
}
|
2019-03-22 14:19:43 +00:00
|
|
|
if (!loaded) {
|
2018-09-11 14:36:09 +00:00
|
|
|
chat->setName(name);
|
|
|
|
chat->count = count;
|
|
|
|
chat->date = date;
|
2019-01-13 08:03:34 +00:00
|
|
|
|
|
|
|
// We don't save participants, admin status and banned rights.
|
|
|
|
// So we don't restore the version field, info is still unknown.
|
|
|
|
chat->setVersion(0);
|
|
|
|
|
2018-09-11 14:36:09 +00:00
|
|
|
chat->creator = creator;
|
|
|
|
chat->setFlags(MTPDchat::Flags::from_raw(flags));
|
|
|
|
chat->setInviteLink(inviteLink);
|
|
|
|
|
|
|
|
chat->input = MTP_inputPeerChat(MTP_int(peerToChat(chat->id)));
|
|
|
|
chat->inputChat = MTP_int(peerToChat(chat->id));
|
|
|
|
}
|
|
|
|
} else if (const auto channel = result->asChannel()) {
|
|
|
|
QString name, inviteLink;
|
|
|
|
quint64 access;
|
|
|
|
qint32 date, version, oldForbidden;
|
|
|
|
quint32 flags;
|
|
|
|
stream >> name >> access >> date >> version >> oldForbidden >> flags >> inviteLink;
|
2019-03-22 14:19:43 +00:00
|
|
|
|
|
|
|
userpicAccessHash = access;
|
|
|
|
|
2018-09-11 14:36:09 +00:00
|
|
|
if (oldForbidden) {
|
|
|
|
flags |= quint32(MTPDchannel_ClientFlag::f_forbidden);
|
|
|
|
}
|
2019-03-22 14:19:43 +00:00
|
|
|
if (!loaded) {
|
2018-09-11 14:36:09 +00:00
|
|
|
channel->setName(name, QString());
|
|
|
|
channel->access = access;
|
|
|
|
channel->date = date;
|
2019-01-13 08:03:34 +00:00
|
|
|
|
|
|
|
// We don't save participants, admin status and banned rights.
|
|
|
|
// So we don't restore the version field, info is still unknown.
|
|
|
|
channel->setVersion(0);
|
|
|
|
|
2018-09-11 14:36:09 +00:00
|
|
|
channel->setFlags(MTPDchannel::Flags::from_raw(flags));
|
|
|
|
channel->setInviteLink(inviteLink);
|
|
|
|
|
|
|
|
channel->input = MTP_inputPeerChannel(MTP_int(peerToChannel(channel->id)), MTP_long(access));
|
|
|
|
channel->inputChannel = MTP_inputChannel(MTP_int(peerToChannel(channel->id)), MTP_long(access));
|
|
|
|
}
|
|
|
|
}
|
2019-03-22 14:19:43 +00:00
|
|
|
if (!loaded) {
|
|
|
|
using LocationType = StorageFileLocation::Type;
|
|
|
|
const auto location = (userpic->valid()
|
|
|
|
&& userpic->type() == LocationType::Legacy)
|
|
|
|
? userpic->convertToModern(
|
|
|
|
LocationType::PeerPhoto,
|
|
|
|
result->id,
|
|
|
|
userpicAccessHash)
|
|
|
|
: *userpic;
|
|
|
|
result->setUserpic(photoId, location, Images::Create(location));
|
2018-09-11 14:36:09 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-10-07 09:02:44 +00:00
|
|
|
QString peekUserPhone(int streamAppVersion, QDataStream &stream) {
|
|
|
|
quint64 peerId = 0, photoId = 0;
|
|
|
|
stream >> peerId >> photoId;
|
2018-11-13 16:03:58 +00:00
|
|
|
DEBUG_LOG(("peekUserPhone.id: %1").arg(peerId));
|
2019-03-22 14:19:43 +00:00
|
|
|
if (!peerId
|
|
|
|
|| !peerIsUser(peerId)
|
|
|
|
|| !readStorageImageLocation(streamAppVersion, stream)) {
|
2018-10-07 09:02:44 +00:00
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString first, last, phone;
|
|
|
|
stream >> first >> last >> phone;
|
2018-11-13 16:03:58 +00:00
|
|
|
DEBUG_LOG(("peekUserPhone.data: %1 %2 %3"
|
|
|
|
).arg(first).arg(last).arg(phone));
|
2018-10-07 09:02:44 +00:00
|
|
|
return phone;
|
|
|
|
}
|
|
|
|
|
2016-04-09 05:57:55 +00:00
|
|
|
} // namespace Serialize
|