Use data_peer_id in export.

This commit is contained in:
John Preston 2021-04-09 20:47:28 +04:00
parent a100048cce
commit 4625e7613b
7 changed files with 202 additions and 175 deletions

View File

@ -49,26 +49,22 @@ QString PreparePhotoFileName(int index, TimeId date) {
} // namespace
PeerId UserPeerId(int32 userId) {
return kUserPeerIdShift | uint32(userId);
}
PeerId ChatPeerId(int32 chatId) {
return kChatPeerIdShift | uint32(chatId);
}
int32 BarePeerId(PeerId peerId) {
return int32(peerId & 0xFFFFFFFFULL);
}
int PeerColorIndex(int32 bareId) {
const auto index = std::abs(bareId) % 7;
int PeerColorIndex(BareId bareId) {
const auto index = bareId % 7;
const int map[] = { 0, 7, 4, 1, 6, 3, 5 };
return map[index];
}
int StringBarePeerId(const Utf8String &data) {
auto result = 0xFF;
BareId PeerToBareId(PeerId peerId) {
return (peerId.value & PeerId::kChatTypeMask);
}
int PeerColorIndex(PeerId peerId) {
return PeerColorIndex(PeerToBareId(peerId));
}
BareId StringBarePeerId(const Utf8String &data) {
auto result = BareId(0xFF);
for (const auto ch : data) {
result *= 239;
result += ch;
@ -98,22 +94,8 @@ int DomainApplicationId(const Utf8String &data) {
return 0x1000 + StringBarePeerId(data);
}
bool IsChatPeerId(PeerId peerId) {
return (peerId & kChatPeerIdShift) == kChatPeerIdShift;
}
bool IsUserPeerId(PeerId peerId) {
return (peerId & kUserPeerIdShift) == kUserPeerIdShift;
}
PeerId ParsePeerId(const MTPPeer &data) {
return data.match([](const MTPDpeerUser &data) {
return UserPeerId(data.vuser_id().v);
}, [](const MTPDpeerChat &data) {
return ChatPeerId(data.vchat_id().v);
}, [](const MTPDpeerChannel &data) {
return ChatPeerId(data.vchannel_id().v);
});
return peerFromMTP(data);
}
Utf8String ParseString(const MTPstring &data) {
@ -516,7 +498,7 @@ Venue ParseVenue(const MTPDmessageMediaVenue &data) {
return result;
}
Game ParseGame(const MTPGame &data, int32 botId) {
Game ParseGame(const MTPGame &data, UserId botId) {
return data.match([&](const MTPDgame &data) {
auto result = Game();
result.id = data.vid().v;
@ -687,16 +669,20 @@ ContactInfo ParseContactInfo(const MTPUser &data) {
int ContactColorIndex(const ContactInfo &data) {
if (data.userId != 0) {
return PeerColorIndex(data.userId);
return PeerColorIndex(data.userId.bare);
}
return PeerColorIndex(StringBarePeerId(data.phoneNumber));
}
PeerId User::id() const {
return UserId(bareId);
}
User ParseUser(const MTPUser &data) {
auto result = User();
result.info = ParseContactInfo(data);
data.match([&](const MTPDuser &data) {
result.id = data.vid().v;
result.bareId = data.vid().v;
if (const auto username = data.vusername()) {
result.username = ParseString(*username);
}
@ -715,8 +701,8 @@ User ParseUser(const MTPUser &data) {
return result;
}
std::map<int32, User> ParseUsersList(const MTPVector<MTPUser> &data) {
auto result = std::map<int32, User>();
std::map<UserId, User> ParseUsersList(const MTPVector<MTPUser> &data) {
auto result = std::map<UserId, User>();
for (const auto &user : data.v) {
auto parsed = ParseUser(user);
result.emplace(parsed.info.userId, std::move(parsed));
@ -724,12 +710,18 @@ std::map<int32, User> ParseUsersList(const MTPVector<MTPUser> &data) {
return result;
}
PeerId Chat::id() const {
return (isBroadcast || isSupergroup)
? PeerId(ChannelId(bareId))
: ChatId(bareId);
}
Chat ParseChat(const MTPChat &data) {
auto result = Chat();
data.match([&](const MTPDchat &data) {
result.id = data.vid().v;
result.bareId = data.vid().v;
result.title = ParseString(data.vtitle());
result.input = MTP_inputPeerChat(MTP_int(result.id));
result.input = MTP_inputPeerChat(MTP_int(result.bareId)); // #TODO ids
if (const auto migratedTo = data.vmigrated_to()) {
result.migratedToChannelId = migratedTo->match(
[](const MTPDinputChannel &data) {
@ -737,14 +729,14 @@ Chat ParseChat(const MTPChat &data) {
}, [](auto&&) { return 0; });
}
}, [&](const MTPDchatEmpty &data) {
result.id = data.vid().v;
result.input = MTP_inputPeerChat(MTP_int(result.id));
result.bareId = data.vid().v;
result.input = MTP_inputPeerChat(MTP_int(result.bareId)); // #TODO ids
}, [&](const MTPDchatForbidden &data) {
result.id = data.vid().v;
result.bareId = data.vid().v;
result.title = ParseString(data.vtitle());
result.input = MTP_inputPeerChat(MTP_int(result.id));
result.input = MTP_inputPeerChat(MTP_int(result.bareId)); // #TODO ids
}, [&](const MTPDchannel &data) {
result.id = data.vid().v;
result.bareId = data.vid().v;
result.isBroadcast = data.is_broadcast();
result.isSupergroup = data.is_megagroup();
result.title = ParseString(data.vtitle());
@ -752,25 +744,25 @@ Chat ParseChat(const MTPChat &data) {
result.username = ParseString(*username);
}
result.input = MTP_inputPeerChannel(
MTP_int(result.id),
MTP_int(result.bareId), // #TODO ids
MTP_long(data.vaccess_hash().value_or_empty()));
}, [&](const MTPDchannelForbidden &data) {
result.id = data.vid().v;
result.bareId = data.vid().v;
result.isBroadcast = data.is_broadcast();
result.isSupergroup = data.is_megagroup();
result.title = ParseString(data.vtitle());
result.input = MTP_inputPeerChannel(
MTP_int(result.id),
result.input = MTP_inputPeerChannel( // #TODO ids
MTP_int(result.bareId),
data.vaccess_hash());
});
return result;
}
std::map<int32, Chat> ParseChatsList(const MTPVector<MTPChat> &data) {
auto result = std::map<int32, Chat>();
std::map<PeerId, Chat> ParseChatsList(const MTPVector<MTPChat> &data) {
auto result = std::map<PeerId, Chat>();
for (const auto &chat : data.v) {
auto parsed = ParseChat(chat);
result.emplace(parsed.id, std::move(parsed));
result.emplace(parsed.id(), std::move(parsed));
}
return result;
}
@ -799,9 +791,9 @@ const Chat *Peer::chat() const {
PeerId Peer::id() const {
if (const auto user = this->user()) {
return UserPeerId(user->info.userId);
return peerFromUser(user->info.userId);
} else if (const auto chat = this->chat()) {
return ChatPeerId(chat->id);
return chat->id();
}
Unexpected("Variant in Peer::id.");
}
@ -835,29 +827,31 @@ std::map<PeerId, Peer> ParsePeersLists(
for (const auto &user : users.v) {
auto parsed = ParseUser(user);
result.emplace(
UserPeerId(parsed.info.userId),
PeerId(parsed.info.userId),
Peer{ std::move(parsed) });
}
for (const auto &chat : chats.v) {
auto parsed = ParseChat(chat);
result.emplace(ChatPeerId(parsed.id), Peer{ std::move(parsed) });
result.emplace(parsed.id(), Peer{ std::move(parsed) });
}
return result;
}
User EmptyUser(int32 userId) {
return ParseUser(MTP_userEmpty(MTP_int(userId)));
User EmptyUser(UserId userId) {
return ParseUser(MTP_userEmpty(MTP_int(userId.bare))); // #TODO ids
}
Chat EmptyChat(int32 chatId) {
return ParseChat(MTP_chatEmpty(MTP_int(chatId)));
Chat EmptyChat(ChatId chatId) {
return ParseChat(MTP_chatEmpty(MTP_int(chatId.bare))); // #TODO ids
}
Peer EmptyPeer(PeerId peerId) {
if (IsUserPeerId(peerId)) {
return Peer{ EmptyUser(BarePeerId(peerId)) };
} else if (IsChatPeerId(peerId)) {
return Peer{ EmptyChat(BarePeerId(peerId)) };
if (peerIsUser(peerId)) {
return Peer{ EmptyUser(peerToUser(peerId)) };
} else if (peerIsChat(peerId)) {
return Peer{ EmptyChat(peerToChat(peerId)) };
} else if (peerIsChannel(peerId)) {
return Peer{ EmptyChat(peerToChat(peerId)) };
}
Unexpected("PeerId in EmptyPeer.");
}
@ -1177,9 +1171,6 @@ Message ParseMessage(
result.selfId = context.selfPeerId;
result.peerId = ParsePeerId(data.vpeer_id());
const auto fromId = data.vfrom_id();
if (IsChatPeerId(result.peerId)) {
result.chatId = BarePeerId(result.peerId);
}
if (fromId) {
result.fromId = ParsePeerId(*fromId);
} else {
@ -1247,11 +1238,11 @@ Message ParseMessage(
result.viaBotId = viaBotId->v;
}
if (const auto media = data.vmedia()) {
context.botId = (result.viaBotId
context.botId = result.viaBotId
? result.viaBotId
: IsUserPeerId(result.forwardedFromId)
? BarePeerId(result.forwardedFromId)
: result.fromId);
: peerIsUser(result.forwardedFromId)
? peerToUser(result.forwardedFromId)
: peerToUser(result.fromId);
result.media = ParseMedia(
context,
*media,
@ -1278,16 +1269,17 @@ Message ParseMessage(
return result;
}
std::map<uint64, Message> ParseMessagesList(
std::map<MessageId, Message> ParseMessagesList(
PeerId selfId,
const MTPVector<MTPMessage> &data,
const QString &mediaFolder) {
auto context = ParseMediaContext{ .selfPeerId = selfId };
auto result = std::map<uint64, Message>();
auto result = std::map<MessageId, Message>();
for (const auto &message : data.v) {
auto parsed = ParseMessage(context, message, mediaFolder);
const auto shift = uint64(uint32(parsed.chatId)) << 32;
result.emplace(shift | uint32(parsed.id), std::move(parsed));
result.emplace(
MessageId{ peerToChannel(parsed.peerId), parsed.id },
std::move(parsed));
}
return result;
}
@ -1439,7 +1431,7 @@ SessionsList ParseSessionsList(const MTPaccount_Authorizations &data) {
WebSession ParseWebSession(
const MTPWebAuthorization &data,
const std::map<int32, User> &users) {
const std::map<UserId, User> &users) {
return data.match([&](const MTPDwebAuthorization &data) {
auto result = WebSession();
const auto i = users.find(data.vbot_id().v);
@ -1555,11 +1547,10 @@ DialogsInfo ParseDialogsInfo(const MTPmessages_Dialogs &data) {
: 0;
}
info.topMessageId = fields.vtop_message().v;
const auto shift = IsChatPeerId(info.peerId)
? (uint64(uint32(BarePeerId(info.peerId))) << 32)
: 0;
const auto messageIt = messages.find(
shift | uint32(info.topMessageId));
const auto messageIt = messages.find(MessageId{
peerToChannel(info.peerId),
info.topMessageId,
});
if (messageIt != end(messages)) {
const auto &message = messageIt->second;
info.topMessageDate = message.date;
@ -1575,7 +1566,7 @@ DialogInfo DialogInfoFromUser(const User &data) {
result.input = (Peer{ data }).input();
result.name = data.info.firstName;
result.lastName = data.info.lastName;
result.peerId = UserPeerId(data.info.userId);
result.peerId = data.id();
result.topMessageDate = 0;
result.topMessageId = 0;
result.type = DialogTypeFromUser(data);
@ -1587,7 +1578,7 @@ DialogInfo DialogInfoFromChat(const Chat &data) {
auto result = DialogInfo();
result.input = data.input;
result.name = data.title;
result.peerId = ChatPeerId(data.id);
result.peerId = data.id();
result.topMessageDate = 0;
result.topMessageId = 0;
result.type = DialogTypeFromChat(data);
@ -1613,17 +1604,17 @@ DialogsInfo ParseDialogsInfo(
const MTPVector<MTPUser> &data) {
const auto singleId = singlePeer.match(
[](const MTPDinputPeerUser &data) {
return data.vuser_id().v;
return UserId(data.vuser_id());
}, [](const MTPDinputPeerSelf &data) {
return 0;
}, [](const auto &data) -> int {
return UserId();
}, [](const auto &data) -> UserId {
Unexpected("Single peer type in ParseDialogsInfo(users).");
});
auto result = DialogsInfo();
result.chats.reserve(data.v.size());
for (const auto &single : data.v) {
const auto userId = single.match([&](const auto &data) {
return data.vid().v;
return peerFromUser(data.vid());
});
if (userId != singleId
&& (singleId != 0
@ -1642,20 +1633,24 @@ DialogsInfo ParseDialogsInfo(
const MTPmessages_Chats &data) {
const auto singleId = singlePeer.match(
[](const MTPDinputPeerChat &data) {
return data.vchat_id().v;
return peerFromChat(data.vchat_id());
}, [](const MTPDinputPeerChannel &data) {
return data.vchannel_id().v;
}, [](const auto &data) -> int {
return peerFromChannel(data.vchannel_id());
}, [](const auto &data) -> PeerId {
Unexpected("Single peer type in ParseDialogsInfo(chats).");
});
auto result = DialogsInfo();
data.match([&](const auto &data) { //MTPDmessages_chats &data) {
result.chats.reserve(data.vchats().v.size());
for (const auto &single : data.vchats().v) {
const auto chatId = single.match([&](const auto &data) {
return data.vid().v;
const auto peerId = single.match([](const MTPDchannel &data) {
return peerFromChannel(data.vid());
}, [](const MTPDchannelForbidden &data) {
return peerFromChannel(data.vid());
}, [](const auto &data) {
return peerFromChat(data.vid());
});
if (chatId != singleId) {
if (peerId != singleId) {
continue;
}
const auto chat = ParseChat(single);
@ -1677,8 +1672,8 @@ bool AddMigrateFromSlice(
const auto good = to.migratedFromInput.match([](
const MTPDinputPeerEmpty &) {
return true;
}, [&](const MTPDinputPeerChat & data) {
return (ChatPeerId(data.vchat_id().v) == from.peerId);
}, [&](const MTPDinputPeerChat &data) {
return (peerFromChat(data.vchat_id()) == from.peerId);
}, [](auto&&) { return false; });
if (!good) {
return false;

View File

@ -10,6 +10,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "scheme.h"
#include "base/optional.h"
#include "base/variant.h"
#include "data/data_peer_id.h"
#include <QtCore/QSize>
#include <QtCore/QString>
@ -22,14 +23,10 @@ struct Settings;
namespace Data {
using Utf8String = QByteArray;
using PeerId = uint64;
PeerId UserPeerId(int32 userId);
PeerId ChatPeerId(int32 chatId);
int32 BarePeerId(PeerId peerId);
bool IsChatPeerId(PeerId peerId);
bool IsUserPeerId(PeerId peerId);
int PeerColorIndex(int32 bareId);
int PeerColorIndex(BareId bareId);
BareId PeerToBareId(PeerId peerId);
int PeerColorIndex(PeerId peerId);
int ApplicationColorIndex(int applicationId);
int DomainApplicationId(const Utf8String &data);
@ -102,7 +99,7 @@ QString WriteImageThumb(
const QString &postfix = "_thumb");
struct ContactInfo {
int32 userId = 0;
UserId userId = 0;
Utf8String firstName;
Utf8String lastName;
Utf8String phoneNumber;
@ -169,7 +166,7 @@ struct Game {
Utf8String title;
Utf8String description;
int32 botId = 0;
UserId botId = 0;
};
struct Invoice {
@ -204,9 +201,11 @@ UserpicsSlice ParseUserpicsSlice(
int baseIndex);
struct User {
PeerId id() const;
BareId bareId = 0;
ContactInfo info;
Utf8String username;
int32 id;
bool isBot = false;
bool isSelf = false;
bool isReplies = false;
@ -217,11 +216,13 @@ struct User {
};
User ParseUser(const MTPUser &data);
std::map<int32, User> ParseUsersList(const MTPVector<MTPUser> &data);
std::map<UserId, User> ParseUsersList(const MTPVector<MTPUser> &data);
struct Chat {
int32 id = 0;
int32 migratedToChannelId = 0;
PeerId id() const;
BareId bareId = 0;
ChannelId migratedToChannelId = 0;
Utf8String title;
Utf8String username;
bool isBroadcast = false;
@ -231,7 +232,7 @@ struct Chat {
};
Chat ParseChat(const MTPChat &data);
std::map<int32, Chat> ParseChatsList(const MTPVector<MTPChat> &data);
std::map<PeerId, Chat> ParseChatsList(const MTPVector<MTPChat> &data);
struct Peer {
PeerId id() const;
@ -336,7 +337,7 @@ struct ParseMediaContext {
int videos = 0;
int files = 0;
int contacts = 0;
int32 botId = 0;
UserId botId = 0;
};
Media ParseMedia(
@ -347,7 +348,7 @@ Media ParseMedia(
struct ActionChatCreate {
Utf8String title;
std::vector<int32> userIds;
std::vector<UserId> userIds;
};
struct ActionChatEditTitle {
@ -362,15 +363,15 @@ struct ActionChatDeletePhoto {
};
struct ActionChatAddUser {
std::vector<int32> userIds;
std::vector<UserId> userIds;
};
struct ActionChatDeleteUser {
int32 userId = 0;
UserId userId = 0;
};
struct ActionChatJoinedByLink {
int32 inviterId = 0;
UserId inviterId = 0;
};
struct ActionChannelCreate {
@ -378,12 +379,12 @@ struct ActionChannelCreate {
};
struct ActionChatMigrateTo {
int32 channelId = 0;
ChannelId channelId = 0;
};
struct ActionChannelMigrateFrom {
Utf8String title;
int32 chatId = 0;
ChatId chatId = 0;
};
struct ActionPinMessage {
@ -463,7 +464,7 @@ struct ActionGroupCall {
};
struct ActionInviteToGroupCall {
std::vector<int32> userIds;
std::vector<UserId> userIds;
};
struct ActionSetMessagesTTL {
@ -537,9 +538,33 @@ struct TextPart {
Utf8String additional;
};
struct MessageId {
ChannelId channelId;
int32 msgId = 0;
};
inline bool operator==(MessageId a, MessageId b) {
return (a.channelId == b.channelId) && (a.msgId == b.msgId);
}
inline bool operator!=(MessageId a, MessageId b) {
return !(a == b);
}
inline bool operator<(MessageId a, MessageId b) {
return (a.channelId < b.channelId)
|| (a.channelId == b.channelId && a.msgId < b.msgId);
}
inline bool operator>(MessageId a, MessageId b) {
return (b < a);
}
inline bool operator<=(MessageId a, MessageId b) {
return !(b < a);
}
inline bool operator>=(MessageId a, MessageId b) {
return !(a < b);
}
struct Message {
int32 id = 0;
int32 chatId = 0;
TimeId date = 0;
TimeId edited = 0;
PeerId fromId = 0;
@ -552,7 +577,7 @@ struct Message {
bool showForwardedAsOriginal = false;
PeerId savedFromChatId = 0;
Utf8String signature;
int32 viaBotId = 0;
UserId viaBotId = 0;
int32 replyToMsgId = 0;
PeerId replyToPeerId = 0;
std::vector<TextPart> text;
@ -576,7 +601,7 @@ Message ParseMessage(
ParseMediaContext &context,
const MTPMessage &data,
const QString &mediaFolder);
std::map<uint64, Message> ParseMessagesList(
std::map<MessageId, Message> ParseMessagesList(
PeerId selfId,
const MTPVector<MTPMessage> &data,
const QString &mediaFolder);
@ -604,7 +629,7 @@ struct DialogInfo {
PeerId peerId = 0;
MTPInputPeer migratedFromInput = MTP_inputPeerEmpty();
int32 migratedToChannelId = 0;
ChannelId migratedToChannelId = 0;
// User messages splits which contained that dialog.
std::vector<int> splits;

View File

@ -184,7 +184,7 @@ struct ApiWrap::ChatsProcess {
Data::DialogsInfo info;
int processedCount = 0;
std::map<Data::PeerId, int> indexByPeer;
std::map<PeerId, int> indexByPeer;
};
struct ApiWrap::LeftChannelsProcess : ChatsProcess {
@ -651,7 +651,7 @@ void ApiWrap::startMainSession(FnMut<void()> done) {
for (const auto &user : result.v) {
user.match([&](const MTPDuser &data) {
if (data.is_self()) {
_selfId = data.vid().v;
_selfId.emplace(data.vid());
}
}, [&](const MTPDuserEmpty&) {
});
@ -950,7 +950,7 @@ void ApiWrap::requestMessages(
Expects(_selfId.has_value());
_chatProcess = std::make_unique<ChatProcess>();
_chatProcess->context.selfPeerId = Data::UserPeerId(*_selfId);
_chatProcess->context.selfPeerId = peerFromUser(*_selfId);
_chatProcess->info = info;
_chatProcess->start = std::move(start);
_chatProcess->fileProgress = std::move(progress);
@ -1342,7 +1342,7 @@ void ApiWrap::appendChatsSlice(
for (auto &info : filtered) {
const auto nextIndex = to.size();
if (info.migratedToChannelId) {
const auto toPeerId = Data::ChatPeerId(info.migratedToChannelId);
const auto toPeerId = PeerId(info.migratedToChannelId);
const auto i = process.indexByPeer.find(toPeerId);
if (i != process.indexByPeer.end()
&& Data::AddMigrateFromSlice(
@ -1915,7 +1915,7 @@ void ApiWrap::filePartExtractReference(
Expects(_selfId.has_value());
auto context = Data::ParseMediaContext();
context.selfPeerId = Data::UserPeerId(*_selfId);
context.selfPeerId = peerFromUser(*_selfId);
const auto messages = Data::ParseMessagesSlice(
context,
data.vmessages(),

View File

@ -8,6 +8,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#pragma once
#include "mtproto/mtproto_concurrent_sender.h"
#include "data/data_peer_id.h"
namespace Export {
namespace Data {
@ -211,7 +212,7 @@ private:
MTP::ConcurrentSender _mtp;
std::optional<uint64> _takeoutId;
std::optional<int32> _selfId;
std::optional<UserId> _selfId;
Output::Stats *_stats = nullptr;
std::unique_ptr<Settings> _settings;

View File

@ -131,7 +131,7 @@ Stats AbstractWriter::produceTestExample(
topUser.rating = 0.5;
auto topChat = Data::TopPeer();
auto chat = Data::Chat();
chat.id = counter();
chat.bareId = counter();
chat.title = "Group chat";
auto peerChat = Data::Peer{ chat };
topChat.peer = peerChat;
@ -148,7 +148,7 @@ Stats AbstractWriter::produceTestExample(
topBot.peer = peerBot;
topBot.rating = 0.125;
auto peers = std::map<Data::PeerId, Data::Peer>();
auto peers = std::map<PeerId, Data::Peer>();
peers.emplace(peerUser.id(), peerUser);
peers.emplace(peerBot.id(), peerBot);
peers.emplace(peerChat.id(), peerChat);
@ -197,7 +197,7 @@ Stats AbstractWriter::produceTestExample(
message.edited = date();
static auto count = 0;
if (++count % 3 == 0) {
message.forwardedFromId = Data::UserPeerId(user.info.userId);
message.forwardedFromId = peerFromUser(user.info.userId);
message.forwardedDate = date();
} else if (count % 3 == 2) {
message.forwardedFromName = "Test hidden forward";
@ -357,14 +357,14 @@ Stats AbstractWriter::produceTestExample(
sliceChat1.list.push_back([&] {
auto message = serviceMessage();
auto action = Data::ActionChatMigrateTo();
action.channelId = chat.id;
action.channelId = ChannelId(chat.bareId);
message.action.content = action;
return message;
}());
sliceChat1.list.push_back([&] {
auto message = serviceMessage();
auto action = Data::ActionChannelMigrateFrom();
action.chatId = chat.id;
action.chatId = ChatId(chat.bareId);
action.title = "Supergroup now";
message.action.content = action;
return message;

View File

@ -340,7 +340,6 @@ struct UserpicData {
class PeersMap {
public:
using PeerId = Data::PeerId;
using Peer = Data::Peer;
using User = Data::User;
using Chat = Data::Chat;
@ -348,15 +347,14 @@ public:
PeersMap(const std::map<PeerId, Peer> &data);
const Peer &peer(PeerId peerId) const;
const User &user(int32 userId) const;
const Chat &chat(int32 chatId) const;
const User &user(UserId userId) const;
QByteArray wrapPeerName(PeerId peerId) const;
QByteArray wrapUserName(int32 userId) const;
QByteArray wrapUserNames(const std::vector<int32> &data) const;
QByteArray wrapUserName(UserId userId) const;
QByteArray wrapUserNames(const std::vector<UserId> &data) const;
private:
const std::map<Data::PeerId, Data::Peer> &_data;
const std::map<PeerId, Data::Peer> &_data;
};
@ -380,22 +378,14 @@ auto PeersMap::peer(PeerId peerId) const -> const Peer & {
return empty;
}
auto PeersMap::user(int32 userId) const -> const User & {
if (const auto result = peer(Data::UserPeerId(userId)).user()) {
auto PeersMap::user(UserId userId) const -> const User & {
if (const auto result = peer(peerFromUser(userId)).user()) {
return *result;
}
static auto empty = User();
return empty;
}
auto PeersMap::chat(int32 chatId) const -> const Chat & {
if (const auto result = peer(Data::ChatPeerId(chatId)).chat()) {
return *result;
}
static auto empty = Chat();
return empty;
}
QByteArray PeersMap::wrapPeerName(PeerId peerId) const {
const auto result = peer(peerId).name();
return result.isEmpty()
@ -403,14 +393,14 @@ QByteArray PeersMap::wrapPeerName(PeerId peerId) const {
: SerializeString(result);
}
QByteArray PeersMap::wrapUserName(int32 userId) const {
QByteArray PeersMap::wrapUserName(UserId userId) const {
const auto result = user(userId).name();
return result.isEmpty()
? QByteArray("Deleted Account")
: SerializeString(result);
}
QByteArray PeersMap::wrapUserNames(const std::vector<int32> &data) const {
QByteArray PeersMap::wrapUserNames(const std::vector<UserId> &data) const {
auto list = std::vector<QByteArray>();
for (const auto userId : data) {
list.push_back(wrapUserName(userId));
@ -469,12 +459,12 @@ struct HtmlWriter::MessageInfo {
Service,
Default,
};
int32 id = 0;
int id = 0;
Type type = Type::Service;
Data::PeerId fromId = 0;
int32 viaBotId = 0;
PeerId fromId = 0;
UserId viaBotId = 0;
TimeId date = 0;
Data::PeerId forwardedFromId = 0;
PeerId forwardedFromId = 0;
QString forwardedFromName;
bool forwarded = false;
bool showForwardedAsOriginal = false;
@ -895,8 +885,7 @@ QByteArray HtmlWriter::Wrap::pushServiceMessage(
result.append(popTag());
if (photo) {
auto userpic = UserpicData();
userpic.colorIndex = Data::PeerColorIndex(
Data::BarePeerId(dialog.peerId));
userpic.colorIndex = Data::PeerColorIndex(dialog.peerId);
userpic.firstName = dialog.name;
userpic.lastName = dialog.lastName;
userpic.pixelSize = kServiceMessagePhotoSize;
@ -1136,7 +1125,7 @@ auto HtmlWriter::Wrap::pushMessage(
auto forwardedUserpic = UserpicData();
if (message.forwarded) {
forwardedUserpic.colorIndex = message.forwardedFromId
? PeerColorIndex(BarePeerId(message.forwardedFromId))
? PeerColorIndex(message.forwardedFromId)
: PeerColorIndex(message.id);
forwardedUserpic.pixelSize = kHistoryUserpicSize;
if (message.forwardedFromId) {
@ -1151,7 +1140,7 @@ auto HtmlWriter::Wrap::pushMessage(
if (message.showForwardedAsOriginal) {
userpic = forwardedUserpic;
} else {
userpic.colorIndex = PeerColorIndex(BarePeerId(fromPeerId));
userpic.colorIndex = PeerColorIndex(fromPeerId);
userpic.pixelSize = kHistoryUserpicSize;
FillUserpicNames(userpic, peers.peer(fromPeerId));
}
@ -1805,7 +1794,7 @@ bool HtmlWriter::Wrap::forwardedNeedsWrap(
} else if (!message.forwardedFromId
|| message.forwardedFromId != previous->forwardedFromId) {
return true;
} else if (Data::IsChatPeerId(message.forwardedFromId)) {
} else if (!peerIsUser(message.forwardedFromId)) {
return true;
} else if (abs(message.forwardedDate - previous->forwardedDate)
> kJoinWithinSeconds) {
@ -2203,7 +2192,7 @@ Result HtmlWriter::writeFrequentContacts(const Data::ContactsList &data) {
return {};
}();
auto userpic = UserpicData{
Data::PeerColorIndex(Data::BarePeerId(top.peer.id())),
Data::PeerColorIndex(top.peer.id()),
kEntryUserpicSize
};
userpic.firstName = name;
@ -2558,7 +2547,7 @@ Result HtmlWriter::writeDialogEnd() {
auto userpic = UserpicData{
((_dialog.type == Type::Self || _dialog.type == Type::Replies)
? kSavedMessagesColorIndex
: Data::PeerColorIndex(Data::BarePeerId(_dialog.peerId))),
: Data::PeerColorIndex(_dialog.peerId)),
kEntryUserpicSize
};
userpic.firstName = NameString(_dialog);

View File

@ -217,7 +217,7 @@ QByteArray FormatFilePath(const Data::File &file) {
QByteArray SerializeMessage(
Context &context,
const Data::Message &message,
const std::map<Data::PeerId, Data::Peer> &peers,
const std::map<PeerId, Data::Peer> &peers,
const QString &internalLinksDomain) {
using namespace Data;
@ -235,20 +235,13 @@ QByteArray SerializeMessage(
static auto empty = Peer{ User() };
return empty;
};
const auto user = [&](int32 userId) -> const User& {
if (const auto result = peer(UserPeerId(userId)).user()) {
const auto user = [&](UserId userId) -> const User& {
if (const auto result = peer(userId).user()) {
return *result;
}
static auto empty = User();
return empty;
};
const auto chat = [&](int32 chatId) -> const Chat& {
if (const auto result = peer(ChatPeerId(chatId)).chat()) {
return *result;
}
static auto empty = Chat();
return empty;
};
auto values = std::vector<std::pair<QByteArray, QByteArray>>{
{ "id", NumberToString(message.id) },
@ -280,6 +273,25 @@ QByteArray SerializeMessage(
const auto push = [&](const QByteArray &key, const auto &value) {
if constexpr (std::is_arithmetic_v<std::decay_t<decltype(value)>>) {
pushBare(key, Data::NumberToString(value));
} else if constexpr (std::is_same_v<
std::decay_t<decltype(value)>,
PeerId>) {
if (const auto chat = peerToChat(value)) {
pushBare(
key,
SerializeString("chat"
+ Data::NumberToString(chat.bare)));
} else if (const auto channel = peerToChannel(value)) {
pushBare(
key,
SerializeString("channel"
+ Data::NumberToString(channel.bare)));
} else {
pushBare(
key,
SerializeString("user"
+ Data::NumberToString(peerToUser(value).bare)));
}
} else {
const auto wrapped = QByteArray(value);
if (!wrapped.isEmpty()) {
@ -290,7 +302,7 @@ QByteArray SerializeMessage(
const auto wrapPeerName = [&](PeerId peerId) {
return StringAllowNull(peer(peerId).name());
};
const auto wrapUserName = [&](int32 userId) {
const auto wrapUserName = [&](UserId userId) {
return StringAllowNull(user(userId).name());
};
const auto pushFrom = [&](const QByteArray &label = "from") {
@ -309,7 +321,7 @@ QByteArray SerializeMessage(
}
};
const auto pushUserNames = [&](
const std::vector<int32> &data,
const std::vector<UserId> &data,
const QByteArray &label = "members") {
auto list = std::vector<QByteArray>();
for (const auto userId : data) {
@ -716,7 +728,7 @@ Result JsonWriter::writePersonal(const Data::PersonalInfo &data) {
return _output->writeBlock(
prepareObjectItemStart("personal_information")
+ SerializeObject(_context, {
{ "user_id", Data::NumberToString(data.user.id) },
{ "user_id", Data::NumberToString(data.user.bareId) },
{ "first_name", SerializeString(info.firstName) },
{ "last_name", SerializeString(info.lastName) },
{
@ -822,7 +834,12 @@ Result JsonWriter::writeSavedContacts(const Data::ContactsList &data) {
}));
} else {
block.append(SerializeObject(_context, {
{ "user_id", Data::NumberToString(contact.userId) },
{
"user_id",
(contact.userId
? Data::NumberToString(contact.userId.bare)
: QByteArray())
},
{ "first_name", SerializeString(contact.firstName) },
{ "last_name", SerializeString(contact.lastName) },
{
@ -867,7 +884,7 @@ Result JsonWriter::writeFrequentContacts(const Data::ContactsList &data) {
}();
block.append(prepareArrayItemStart());
block.append(SerializeObject(_context, {
{ "id", Data::NumberToString(top.peer.id()) },
{ "id", Data::NumberToString(Data::PeerToBareId(top.peer.id())) },
{ "category", SerializeString(category) },
{ "type", SerializeString(type) },
{ "name", StringAllowNull(top.peer.name()) },
@ -1066,7 +1083,7 @@ Result JsonWriter::writeDialogStart(const Data::DialogInfo &data) {
block.append(prepareObjectItemStart("type")
+ StringAllowNull(TypeString(data.type)));
block.append(prepareObjectItemStart("id")
+ Data::NumberToString(data.peerId));
+ Data::NumberToString(Data::PeerToBareId(data.peerId)));
block.append(prepareObjectItemStart("messages"));
block.append(pushNesting(Context::kArray));
return _output->writeBlock(block);