Skip to content

Commit

Permalink
Improved code style in newest code from export submodule.
Browse files Browse the repository at this point in the history
  • Loading branch information
23rd committed Sep 30, 2024
1 parent a970fe9 commit 4582e61
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 25 deletions.
22 changes: 13 additions & 9 deletions Telegram/SourceFiles/export/data/export_data_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -370,8 +370,10 @@ std::vector<Reaction> ParseReactions(const MTPMessageReactions &data) {
for (const auto &single : data.data().vresults().v) {
auto reaction = ParseReaction(single.data().vreaction());
reaction.count = single.data().vcount().v;
auto id = Reaction::Id(reaction);
auto const &[_, inserted] = reactionsMap.try_emplace(id, reaction);
const auto id = Reaction::Id(reaction);
auto const &[_, inserted] = reactionsMap.try_emplace(
id,
std::move(reaction));
if (inserted) {
reactionsOrder.push_back(id);
}
Expand All @@ -380,8 +382,10 @@ std::vector<Reaction> ParseReactions(const MTPMessageReactions &data) {
if (const auto list = data.data().vrecent_reactions()) {
for (const auto &single : list->v) {
auto reaction = ParseReaction(single.data().vreaction());
auto id = Reaction::Id(reaction);
auto const &[it, inserted] = reactionsMap.try_emplace(id, reaction);
const auto id = Reaction::Id(reaction);
auto const &[it, inserted] = reactionsMap.try_emplace(
id,
std::move(reaction));
if (inserted) {
reactionsOrder.push_back(id);
}
Expand All @@ -392,11 +396,11 @@ std::vector<Reaction> ParseReactions(const MTPMessageReactions &data) {
}
}
}
std::vector<Reaction> results;
for (const auto &id : reactionsOrder) {
results.push_back(reactionsMap[id]);
}
return results;
return ranges::views::all(
reactionsOrder
) | ranges::views::transform([&](const Utf8String &id) {
return reactionsMap.at(id);
}) | ranges::to_vector;
}

Utf8String FillLeft(const Utf8String &data, int length, char filler) {
Expand Down
6 changes: 3 additions & 3 deletions Telegram/SourceFiles/export/data/export_data_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -709,10 +709,10 @@ struct Reaction {
CustomEmoji,
Paid,
};

static Utf8String TypeToString(const Reaction &);

static Utf8String Id(const Reaction &);
[[nodiscard]] static Utf8String TypeToString(const Reaction &);

[[nodiscard]] static Utf8String Id(const Reaction &);

struct Recent {
PeerId peerId = 0;
Expand Down
8 changes: 3 additions & 5 deletions Telegram/SourceFiles/export/export_api_wrap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1826,9 +1826,7 @@ std::optional<QByteArray> ApiWrap::getCustomEmoji(QByteArray &data) {
file,
{ .customEmojiId = id },
fileProgress,
[=](const QString &path) {
loadMessageEmojiDone(id, path);
});
[=](const QString &path) { loadMessageEmojiDone(id, path); });
if (!ready) {
return std::nullopt;
}
Expand All @@ -1850,7 +1848,7 @@ bool ApiWrap::messageCustomEmojiReady(Data::Message &message) {
if (part.type == Data::TextPart::Type::CustomEmoji) {
auto data = getCustomEmoji(part.additional);
if (data.has_value()) {
part.additional = *data;
part.additional = base::take(*data);
} else {
return false;
}
Expand All @@ -1860,7 +1858,7 @@ bool ApiWrap::messageCustomEmojiReady(Data::Message &message) {
if (reaction.type == Data::Reaction::Type::CustomEmoji) {
auto data = getCustomEmoji(reaction.documentId);
if (data.has_value()) {
reaction.documentId = *data;
reaction.documentId = base::take(*data);
} else {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion Telegram/SourceFiles/export/export_api_wrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ class ApiWrap {
void resolveCustomEmoji();
void loadMessagesFiles(Data::MessagesSlice &&slice);
void loadNextMessageFile();
std::optional<QByteArray> getCustomEmoji(QByteArray &data);
[[nodiscard]] std::optional<QByteArray> getCustomEmoji(QByteArray &data);
bool messageCustomEmojiReady(Data::Message &message);
bool loadMessageFileProgress(FileProgress value);
void loadMessageFileDone(const QString &relativePath);
Expand Down
9 changes: 5 additions & 4 deletions Telegram/SourceFiles/export/output/export_output_html.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1556,9 +1556,9 @@ auto HtmlWriter::Wrap::pushMessage(
if (!message.reactions.empty()) {
block.append(pushDiv("reactions"));
for (const auto &reaction : message.reactions) {
QByteArray reactionClass = "reaction";
auto reactionClass = QByteArray("reaction");
for (const auto &recent : reaction.recent) {
auto peer = peers.peer(recent.peerId);
const auto peer = peers.peer(recent.peerId);
if (peer.user() && peer.user()->isSelf) {
reactionClass += " active";
break;
Expand Down Expand Up @@ -1594,7 +1594,7 @@ auto HtmlWriter::Wrap::pushMessage(
{ "class", "userpics" },
}));
for (const auto &recent : reaction.recent) {
auto peer = peers.peer(recent.peerId);
const auto peer = peers.peer(recent.peerId);
block.append(pushUserpic(UserpicData({
.colorIndex = peer.colorIndex(),
.pixelSize = 20,
Expand All @@ -1609,7 +1609,8 @@ auto HtmlWriter::Wrap::pushMessage(
}
block.append(popTag());
}
if (reaction.recent.empty() || reaction.count > reaction.recent.size()) {
if (reaction.recent.empty()
|| (reaction.count > reaction.recent.size())) {
block.append(pushTag("div", {
{ "class", "count" },
}));
Expand Down
11 changes: 8 additions & 3 deletions Telegram/SourceFiles/export/output/export_output_json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -918,7 +918,9 @@ QByteArray SerializeMessage(
if (!message.reactions.empty()) {
const auto serializeReaction = [&](const Reaction &reaction) {
context.nesting.push_back(Context::kObject);
const auto guard = gsl::finally([&] { context.nesting.pop_back(); });
const auto guard = gsl::finally([&] {
context.nesting.pop_back();
});

auto pairs = std::vector<std::pair<QByteArray, QByteArray>>();
pairs.push_back({
Expand Down Expand Up @@ -948,9 +950,12 @@ QByteArray SerializeMessage(
context.nesting.push_back(Context::kArray);
const auto recents = ranges::views::all(
reaction.recent
) | ranges::views::transform([&](const Reaction::Recent &recent) {
) | ranges::views::transform([&](
const Reaction::Recent &recent) {
context.nesting.push_back(Context::kArray);
const auto guard = gsl::finally([&] { context.nesting.pop_back(); });
const auto guard = gsl::finally([&] {
context.nesting.pop_back();
});
return SerializeObject(context, {
{ "from", wrapPeerName(recent.peerId) },
{ "from_id", wrapPeerId(recent.peerId) },
Expand Down

0 comments on commit 4582e61

Please sign in to comment.