mirror of
https://github.com/telegramdesktop/tdesktop
synced 2025-03-21 02:41:34 +00:00
Save self to local storage map.
This commit is contained in:
parent
0c8709ca5f
commit
34665cd6da
@ -323,7 +323,8 @@ namespace App {
|
||||
if (data->loadedStatus == PeerData::NotLoaded) {
|
||||
data->loadedStatus = PeerData::MinimalLoaded;
|
||||
}
|
||||
} else if (data->loadedStatus != PeerData::FullLoaded) {
|
||||
} else if (data->loadedStatus != PeerData::FullLoaded
|
||||
&& (!data->isSelf() || !data->phone().isEmpty())) {
|
||||
data->loadedStatus = PeerData::FullLoaded;
|
||||
}
|
||||
|
||||
|
@ -23,6 +23,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
||||
#include "window/section_widget.h"
|
||||
#include "chat_helpers/tabbed_selector.h"
|
||||
#include "boxes/send_files_box.h"
|
||||
#include "observer_peer.h"
|
||||
|
||||
namespace {
|
||||
|
||||
@ -296,6 +297,24 @@ AuthSession::AuthSession(const MTPUser &user)
|
||||
_api->requestTermsUpdate();
|
||||
_api->requestFullPeer(_user);
|
||||
|
||||
crl::on_main(this, [=] {
|
||||
using Flag = Notify::PeerUpdate::Flag;
|
||||
const auto events = Flag::NameChanged
|
||||
| Flag::UsernameChanged
|
||||
| Flag::PhotoChanged
|
||||
| Flag::AboutChanged
|
||||
| Flag::UserPhoneChanged;
|
||||
subscribe(
|
||||
Notify::PeerUpdated(),
|
||||
Notify::PeerUpdatedHandler(
|
||||
events,
|
||||
[=](const Notify::PeerUpdate &update) {
|
||||
if (update.peer == _user) {
|
||||
Local::writeSelf();
|
||||
}
|
||||
}));
|
||||
});
|
||||
|
||||
Window::Theme::Background()->start();
|
||||
}
|
||||
|
||||
|
@ -115,7 +115,6 @@ void PeerData::updateNameDelayed(
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
++nameVersion;
|
||||
name = newName;
|
||||
nameText.setText(st::msgNameStyle, name, Ui::NameTextOptions());
|
||||
|
@ -67,6 +67,7 @@ Messenger *Messenger::InstancePointer() {
|
||||
struct Messenger::Private {
|
||||
UserId authSessionUserId = 0;
|
||||
QByteArray authSessionUserSerialized;
|
||||
int32 authSessionUserStreamVersion = 0;
|
||||
std::unique_ptr<AuthSessionSettings> storedAuthSession;
|
||||
MTP::Instance::Config mtpConfig;
|
||||
MTP::AuthKeysList mtpKeysToDestroy;
|
||||
@ -372,12 +373,19 @@ QByteArray Messenger::serializeMtpAuthorization() const {
|
||||
|
||||
void Messenger::setAuthSessionUserId(UserId userId) {
|
||||
Expects(!authSession());
|
||||
|
||||
_private->authSessionUserId = userId;
|
||||
}
|
||||
|
||||
void Messenger::setAuthSessionFromStorage(std::unique_ptr<AuthSessionSettings> data) {
|
||||
void Messenger::setAuthSessionFromStorage(
|
||||
std::unique_ptr<AuthSessionSettings> data,
|
||||
QByteArray &&selfSerialized,
|
||||
int32 selfStreamVersion) {
|
||||
Expects(!authSession());
|
||||
|
||||
_private->storedAuthSession = std::move(data);
|
||||
_private->authSessionUserSerialized = std::move(selfSerialized);
|
||||
_private->authSessionUserStreamVersion = selfStreamVersion;
|
||||
}
|
||||
|
||||
AuthSessionSettings *Messenger::getAuthSessionSettings() {
|
||||
@ -469,7 +477,9 @@ void Messenger::startMtp() {
|
||||
MTPstring(), // restriction_reason
|
||||
MTPstring(), // bot_inline_placeholder
|
||||
MTPstring())); // lang_code
|
||||
//Local::readUser(base::take(_private->authSessionUserSerialized));
|
||||
Local::readSelf(
|
||||
base::take(_private->authSessionUserSerialized),
|
||||
base::take(_private->authSessionUserStreamVersion));
|
||||
}
|
||||
if (_private->storedAuthSession) {
|
||||
if (_authSession) {
|
||||
@ -485,6 +495,11 @@ void Messenger::startMtp() {
|
||||
if (!Core::UpdaterDisabled()) {
|
||||
Core::UpdateChecker().setMtproto(mtp());
|
||||
}
|
||||
|
||||
if (_authSession) {
|
||||
// Skip all pending self updates so that we won't Local::writeSelf.
|
||||
Notify::peerUpdatedSendDelayed();
|
||||
}
|
||||
}
|
||||
|
||||
void Messenger::destroyMtpKeys(MTP::AuthKeysList &&keys) {
|
||||
|
@ -115,7 +115,9 @@ public:
|
||||
void setMtpKey(MTP::DcId dcId, const MTP::AuthKey::Data &keyData);
|
||||
void setAuthSessionUserId(UserId userId);
|
||||
void setAuthSessionFromStorage(
|
||||
std::unique_ptr<AuthSessionSettings> data);
|
||||
std::unique_ptr<AuthSessionSettings> data,
|
||||
QByteArray &&selfSerialized,
|
||||
int32 selfStreamVersion);
|
||||
AuthSessionSettings *getAuthSessionSettings();
|
||||
|
||||
// Serialization.
|
||||
|
@ -513,6 +513,7 @@ enum { // Local Storage Keys
|
||||
lskFavedStickers = 0x12, // no data
|
||||
lskExportSettings = 0x13, // no data
|
||||
lskBackground = 0x14, // no data
|
||||
lskSelfSerialized = 0x15, // serialized self
|
||||
};
|
||||
|
||||
enum {
|
||||
@ -2115,6 +2116,7 @@ ReadMapState _readMap(const QByteArray &pass) {
|
||||
}
|
||||
LOG(("App Info: reading encrypted map..."));
|
||||
|
||||
QByteArray selfSerialized;
|
||||
DraftsMap draftsMap, draftCursorsMap;
|
||||
DraftsNotReadMap draftsNotReadMap;
|
||||
quint64 locationsKey = 0, reportSpamStatusesKey = 0, trustedBotsKey = 0;
|
||||
@ -2138,6 +2140,9 @@ ReadMapState _readMap(const QByteArray &pass) {
|
||||
draftsNotReadMap.insert(p, true);
|
||||
}
|
||||
} break;
|
||||
case lskSelfSerialized: {
|
||||
map.stream >> selfSerialized;
|
||||
} break;
|
||||
case lskDraftPosition: {
|
||||
quint32 count = 0;
|
||||
map.stream >> count;
|
||||
@ -2256,7 +2261,10 @@ ReadMapState _readMap(const QByteArray &pass) {
|
||||
_readUserSettings();
|
||||
_readMtpData();
|
||||
|
||||
Messenger::Instance().setAuthSessionFromStorage(std::move(StoredAuthSessionCache));
|
||||
Messenger::Instance().setAuthSessionFromStorage(
|
||||
std::move(StoredAuthSessionCache),
|
||||
std::move(selfSerialized),
|
||||
_oldMapVersion);
|
||||
|
||||
LOG(("Map read time: %1").arg(getms() - ms));
|
||||
if (_oldSettingsVersion < AppVersion) {
|
||||
@ -2298,6 +2306,27 @@ void _writeMap(WriteMapWhen when) {
|
||||
map.writeData(_passKeyEncrypted);
|
||||
|
||||
uint32 mapSize = 0;
|
||||
const auto self = [] {
|
||||
if (!AuthSession::Exists()) {
|
||||
return QByteArray();
|
||||
}
|
||||
const auto self = Auth().user();
|
||||
if (self->phone().isEmpty()) {
|
||||
return QByteArray();
|
||||
}
|
||||
auto result = QByteArray();
|
||||
result.reserve(Serialize::peerSize(self)
|
||||
+ Serialize::stringSize(self->about()));
|
||||
{
|
||||
QBuffer buffer(&result);
|
||||
buffer.open(QIODevice::WriteOnly);
|
||||
QDataStream stream(&buffer);
|
||||
Serialize::writePeer(stream, self);
|
||||
stream << self->about();
|
||||
}
|
||||
return result;
|
||||
}();
|
||||
if (!self.isEmpty()) mapSize += sizeof(quint32) + Serialize::bytearraySize(self);
|
||||
if (!_draftsMap.isEmpty()) mapSize += sizeof(quint32) * 2 + _draftsMap.size() * sizeof(quint64) * 2;
|
||||
if (!_draftCursorsMap.isEmpty()) mapSize += sizeof(quint32) * 2 + _draftCursorsMap.size() * sizeof(quint64) * 2;
|
||||
if (_locationsKey) mapSize += sizeof(quint32) + sizeof(quint64);
|
||||
@ -2316,7 +2345,9 @@ void _writeMap(WriteMapWhen when) {
|
||||
if (_exportSettingsKey) mapSize += sizeof(quint32) + sizeof(quint64);
|
||||
|
||||
EncryptedDescriptor mapData(mapSize);
|
||||
|
||||
if (!self.isEmpty()) {
|
||||
mapData.stream << quint32(lskSelfSerialized) << self;
|
||||
}
|
||||
if (!_draftsMap.isEmpty()) {
|
||||
mapData.stream << quint32(lskDraft) << quint32(_draftsMap.size());
|
||||
for (DraftsMap::const_iterator i = _draftsMap.cbegin(), e = _draftsMap.cend(); i != e; ++i) {
|
||||
@ -4040,211 +4071,6 @@ bool copyThemeColorsToPalette(const QString &path) {
|
||||
return Window::Theme::CopyColorsToPalette(path, themeContent);
|
||||
}
|
||||
|
||||
uint32 _peerSize(not_null<PeerData*> peer) {
|
||||
uint32 result = sizeof(quint64)
|
||||
+ sizeof(quint64)
|
||||
+ Serialize::storageImageLocationSize(peer->userpicLocation());
|
||||
if (peer->isUser()) {
|
||||
UserData *user = peer->asUser();
|
||||
|
||||
// first + last + phone + username + access
|
||||
result += Serialize::stringSize(user->firstName) + Serialize::stringSize(user->lastName) + Serialize::stringSize(user->phone()) + Serialize::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 += Serialize::stringSize(chat->name) + sizeof(qint32) + sizeof(qint32) + sizeof(qint32) + sizeof(qint32) + sizeof(qint32) + sizeof(quint32) + Serialize::stringSize(chat->inviteLink());
|
||||
} else if (peer->isChannel()) {
|
||||
ChannelData *channel = peer->asChannel();
|
||||
|
||||
// name + access + date + version + old forbidden + flags + inviteLink
|
||||
result += Serialize::stringSize(channel->name) + sizeof(quint64) + sizeof(qint32) + sizeof(qint32) + sizeof(qint32) + sizeof(quint32) + Serialize::stringSize(channel->inviteLink());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void _writePeer(QDataStream &stream, PeerData *peer) {
|
||||
stream << quint64(peer->id) << quint64(peer->userpicPhotoId());
|
||||
Serialize::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) {
|
||||
const auto botInlinePlaceholder = user->botInfo
|
||||
? user->botInfo->inlinePlaceholder
|
||||
: QString();
|
||||
stream << botInlinePlaceholder;
|
||||
}
|
||||
const auto contactSerialized = [&] {
|
||||
switch (user->contactStatus()) {
|
||||
case UserData::ContactStatus::Contact: return 1;
|
||||
case UserData::ContactStatus::CanAdd: return 0;
|
||||
case UserData::ContactStatus::PhoneUnknown: return -1;
|
||||
}
|
||||
Unexpected("contactStatus in _writePeer()");
|
||||
}();
|
||||
stream
|
||||
<< qint32(user->onlineTill)
|
||||
<< qint32(contactSerialized)
|
||||
<< qint32(user->botInfo ? user->botInfo->version : -1);
|
||||
} else if (const auto chat = peer->asChat()) {
|
||||
stream
|
||||
<< chat->name
|
||||
<< qint32(chat->count)
|
||||
<< qint32(chat->date)
|
||||
<< qint32(chat->version)
|
||||
<< 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)
|
||||
<< qint32(channel->version)
|
||||
<< 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;
|
||||
}
|
||||
|
||||
auto photoLoc = Serialize::readStorageImageLocation(
|
||||
streamAppVersion,
|
||||
stream);
|
||||
|
||||
PeerData *result = App::peerLoaded(peerId);
|
||||
bool wasLoaded = (result != nullptr);
|
||||
if (!wasLoaded) {
|
||||
result = App::peer(peerId);
|
||||
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;
|
||||
|
||||
const auto showPhone = !isServiceUser(user->id)
|
||||
&& (user->id != Auth().userPeerId())
|
||||
&& (contact <= 0);
|
||||
const auto pname = (showPhone && !phone.isEmpty())
|
||||
? App::formatPhone(phone)
|
||||
: QString();
|
||||
|
||||
if (!wasLoaded) {
|
||||
user->setPhone(phone);
|
||||
user->setName(first, last, pname, username);
|
||||
|
||||
user->setFlags(MTPDuser::Flags::from_raw(flags));
|
||||
user->setAccessHash(access);
|
||||
user->onlineTill = onlineTill;
|
||||
user->setContactStatus((contact > 0)
|
||||
? UserData::ContactStatus::Contact
|
||||
: (contact == 0)
|
||||
? UserData::ContactStatus::CanAdd
|
||||
: UserData::ContactStatus::PhoneUnknown);
|
||||
user->setBotInfoVersion(botInfoVersion);
|
||||
if (!inlinePlaceholder.isEmpty() && user->botInfo) {
|
||||
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);
|
||||
}
|
||||
if (!wasLoaded) {
|
||||
chat->setName(name);
|
||||
chat->count = count;
|
||||
chat->date = date;
|
||||
chat->version = version;
|
||||
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;
|
||||
if (oldForbidden) {
|
||||
flags |= quint32(MTPDchannel_ClientFlag::f_forbidden);
|
||||
}
|
||||
if (!wasLoaded) {
|
||||
channel->setName(name, QString());
|
||||
channel->access = access;
|
||||
channel->date = date;
|
||||
channel->version = version;
|
||||
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));
|
||||
}
|
||||
}
|
||||
if (!wasLoaded) {
|
||||
result->setUserpic(
|
||||
photoId,
|
||||
photoLoc,
|
||||
photoLoc.isNull() ? ImagePtr() : ImagePtr(photoLoc));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void writeRecentHashtagsAndBots() {
|
||||
if (!_working()) return;
|
||||
|
||||
@ -4278,7 +4104,7 @@ void writeRecentHashtagsAndBots() {
|
||||
}
|
||||
}
|
||||
for (auto i = bots.cbegin(), e = bots.cend(); i != e; ++i) {
|
||||
size += _peerSize(*i);
|
||||
size += Serialize::peerSize(*i);
|
||||
}
|
||||
|
||||
EncryptedDescriptor data(size);
|
||||
@ -4291,7 +4117,7 @@ void writeRecentHashtagsAndBots() {
|
||||
}
|
||||
data.stream << quint32(botsCnt);
|
||||
for (auto i = bots.cbegin(), e = bots.cend(); i != e; ++i) {
|
||||
_writePeer(data.stream, *i);
|
||||
Serialize::writePeer(data.stream, *i);
|
||||
}
|
||||
FileWriteDescriptor file(_recentHashtagsAndBotsKey);
|
||||
file.writeEncrypted(data);
|
||||
@ -4342,7 +4168,7 @@ void readRecentHashtagsAndBots() {
|
||||
if (botsCount) {
|
||||
bots.reserve(botsCount);
|
||||
for (auto i = 0; i < botsCount; ++i) {
|
||||
const auto peer = _readPeer(
|
||||
const auto peer = Serialize::readPeer(
|
||||
hashtags.version,
|
||||
hashtags.stream);
|
||||
if (!peer) {
|
||||
@ -4593,13 +4419,13 @@ void writeSavedPeers() {
|
||||
}
|
||||
quint32 size = sizeof(quint32);
|
||||
for (SavedPeers::const_iterator i = saved.cbegin(); i != saved.cend(); ++i) {
|
||||
size += _peerSize(i.key()) + Serialize::dateTimeSize();
|
||||
size += Serialize::peerSize(i.key()) + Serialize::dateTimeSize();
|
||||
}
|
||||
|
||||
EncryptedDescriptor data(size);
|
||||
data.stream << quint32(saved.size());
|
||||
for (SavedPeers::const_iterator i = saved.cbegin(); i != saved.cend(); ++i) {
|
||||
_writePeer(data.stream, i.key());
|
||||
Serialize::writePeer(data.stream, i.key());
|
||||
data.stream << i.value();
|
||||
}
|
||||
|
||||
@ -4632,7 +4458,7 @@ void readSavedPeers() {
|
||||
QList<PeerData*> peers;
|
||||
peers.reserve(count);
|
||||
for (uint32 i = 0; i < count; ++i) {
|
||||
const auto peer = _readPeer(saved.version, saved.stream);
|
||||
const auto peer = Serialize::readPeer(saved.version, saved.stream);
|
||||
if (!peer) break;
|
||||
|
||||
QDateTime t;
|
||||
@ -4676,6 +4502,25 @@ void writeReportSpamStatuses() {
|
||||
_writeReportSpamStatuses();
|
||||
}
|
||||
|
||||
void writeSelf() {
|
||||
_mapChanged = true;
|
||||
_writeMap();
|
||||
}
|
||||
|
||||
void readSelf(const QByteArray &serialized, int32 streamVersion) {
|
||||
QDataStream stream(serialized);
|
||||
const auto self = Serialize::readPeer(streamVersion, stream);
|
||||
if (!self || !self->isSelf() || self != Auth().user()) {
|
||||
return;
|
||||
}
|
||||
|
||||
QString about;
|
||||
stream >> about;
|
||||
if (_checkStreamStatus(stream)) {
|
||||
self->asUser()->setAbout(about);
|
||||
}
|
||||
}
|
||||
|
||||
void writeTrustedBots() {
|
||||
if (!_working()) return;
|
||||
|
||||
|
@ -156,6 +156,9 @@ void readSavedPeers();
|
||||
|
||||
void writeReportSpamStatuses();
|
||||
|
||||
void writeSelf();
|
||||
void readSelf(const QByteArray &serialized, int32 streamVersion);
|
||||
|
||||
void makeBotTrusted(UserData *bot);
|
||||
bool isBotTrusted(UserData *bot);
|
||||
|
||||
|
@ -7,6 +7,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
||||
*/
|
||||
#include "storage/serialize_common.h"
|
||||
|
||||
#include "auth_session.h"
|
||||
|
||||
namespace Serialize {
|
||||
|
||||
void writeStorageImageLocation(
|
||||
@ -53,4 +55,209 @@ int storageImageLocationSize(const StorageImageLocation &location) {
|
||||
+ bytearraySize(location.fileReference());
|
||||
}
|
||||
|
||||
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) {
|
||||
const auto botInlinePlaceholder = user->botInfo
|
||||
? user->botInfo->inlinePlaceholder
|
||||
: QString();
|
||||
stream << botInlinePlaceholder;
|
||||
}
|
||||
const auto contactSerialized = [&] {
|
||||
switch (user->contactStatus()) {
|
||||
case UserData::ContactStatus::Contact: return 1;
|
||||
case UserData::ContactStatus::CanAdd: return 0;
|
||||
case UserData::ContactStatus::PhoneUnknown: return -1;
|
||||
}
|
||||
Unexpected("contactStatus in _writePeer()");
|
||||
}();
|
||||
stream
|
||||
<< qint32(user->onlineTill)
|
||||
<< qint32(contactSerialized)
|
||||
<< qint32(user->botInfo ? user->botInfo->version : -1);
|
||||
} else if (const auto chat = peer->asChat()) {
|
||||
stream
|
||||
<< chat->name
|
||||
<< qint32(chat->count)
|
||||
<< qint32(chat->date)
|
||||
<< qint32(chat->version)
|
||||
<< 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)
|
||||
<< qint32(channel->version)
|
||||
<< 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;
|
||||
}
|
||||
|
||||
auto photoLoc = readStorageImageLocation(
|
||||
streamAppVersion,
|
||||
stream);
|
||||
|
||||
PeerData *result = App::peerLoaded(peerId);
|
||||
bool wasLoaded = (result != nullptr);
|
||||
if (!wasLoaded) {
|
||||
result = App::peer(peerId);
|
||||
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;
|
||||
|
||||
const auto showPhone = !isServiceUser(user->id)
|
||||
&& (user->id != Auth().userPeerId())
|
||||
&& (contact <= 0);
|
||||
const auto pname = (showPhone && !phone.isEmpty())
|
||||
? App::formatPhone(phone)
|
||||
: QString();
|
||||
|
||||
if (!wasLoaded) {
|
||||
user->setPhone(phone);
|
||||
user->setName(first, last, pname, username);
|
||||
|
||||
user->setFlags(MTPDuser::Flags::from_raw(flags));
|
||||
user->setAccessHash(access);
|
||||
user->onlineTill = onlineTill;
|
||||
user->setContactStatus((contact > 0)
|
||||
? UserData::ContactStatus::Contact
|
||||
: (contact == 0)
|
||||
? UserData::ContactStatus::CanAdd
|
||||
: UserData::ContactStatus::PhoneUnknown);
|
||||
user->setBotInfoVersion(botInfoVersion);
|
||||
if (!inlinePlaceholder.isEmpty() && user->botInfo) {
|
||||
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);
|
||||
}
|
||||
if (!wasLoaded) {
|
||||
chat->setName(name);
|
||||
chat->count = count;
|
||||
chat->date = date;
|
||||
chat->version = version;
|
||||
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;
|
||||
if (oldForbidden) {
|
||||
flags |= quint32(MTPDchannel_ClientFlag::f_forbidden);
|
||||
}
|
||||
if (!wasLoaded) {
|
||||
channel->setName(name, QString());
|
||||
channel->access = access;
|
||||
channel->date = date;
|
||||
channel->version = version;
|
||||
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));
|
||||
}
|
||||
}
|
||||
if (!wasLoaded) {
|
||||
result->setUserpic(
|
||||
photoId,
|
||||
photoLoc,
|
||||
photoLoc.isNull() ? ImagePtr() : ImagePtr(photoLoc));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
} // namespace Serialize
|
||||
|
@ -107,4 +107,8 @@ inline MTP::AuthKey::Data read<MTP::AuthKey::Data>(QDataStream &stream) {
|
||||
return result;
|
||||
}
|
||||
|
||||
uint32 peerSize(not_null<PeerData*> peer);
|
||||
void writePeer(QDataStream &stream, PeerData *peer);
|
||||
PeerData *readPeer(int streamAppVersion, QDataStream &stream);
|
||||
|
||||
} // namespace Serialize
|
||||
|
Loading…
Reference in New Issue
Block a user