2
0
mirror of https://github.com/kotatogram/kotatogram-desktop synced 2025-08-31 14:45:14 +00:00

Simplest polls data export.

This commit is contained in:
John Preston
2018-12-23 00:08:15 +04:00
parent 93c8e9aa1f
commit ef1d38462f
6 changed files with 153 additions and 2 deletions

View File

@@ -621,6 +621,7 @@ private:
[[nodiscard]] QByteArray pushPhotoMedia(
const Data::Photo &data,
const QString &basePath);
[[nodiscard]] QByteArray pushPoll(const Data::Poll &data);
File _file;
bool _closed = false;
@@ -1232,6 +1233,8 @@ QByteArray HtmlWriter::Wrap::pushMedia(
} else if (const auto photo = base::get_if<Data::Photo>(&content)) {
Assert(!message.media.ttl);
return pushPhotoMedia(*photo, basePath);
} else if (const auto poll = base::get_if<Data::Poll>(&content)) {
return pushPoll(*poll);
}
Assert(!content.has_value());
return QByteArray();
@@ -1503,6 +1506,54 @@ QByteArray HtmlWriter::Wrap::pushPhotoMedia(
return result;
}
QByteArray HtmlWriter::Wrap::pushPoll(const Data::Poll &data) {
using namespace Data;
auto result = pushDiv("media_wrap clearfix");
result.append(pushDiv("media_poll"));
result.append(pushDiv("question bold"));
result.append(SerializeString(data.question));
result.append(popTag());
result.append(pushDiv("details"));
if (data.closed) {
result.append(SerializeString("Final results"));
} else {
result.append(SerializeString("Anonymous poll"));
}
result.append(popTag());
const auto votes = [](int count) {
if (count > 1) {
return NumberToString(count) + " votes";
} else if (count > 0) {
return NumberToString(count) + " vote";
}
return QByteArray("No votes");
};
const auto details = [&](const Poll::Answer &answer) {
if (!answer.votes) {
return QByteArray("");
} else if (!answer.my) {
return " <span class=\"details\">"
+ votes(answer.votes)
+ "</span>";
}
return " <span class=\"details\">"
+ votes(answer.votes)
+ ", chosen vote</span>";
};
for (const auto &answer : data.answers) {
result.append(pushDiv("answer"));
result.append("- " + SerializeString(answer.text) + details(answer));
result.append(popTag());
}
result.append(pushDiv("total details "));
result.append(votes(data.totalVotes));
result.append(popTag());
result.append(popTag());
result.append(popTag());
return result;
}
MediaData HtmlWriter::Wrap::prepareMediaData(
const Data::Message &message,
const QString &basePath,
@@ -1658,6 +1709,7 @@ MediaData HtmlWriter::Wrap::prepareMediaData(
result.title = data.title;
result.description = data.description;
result.status = Data::FormatMoneyAmount(data.amount, data.currency);
}, [](const Poll &data) {
}, [](const UnsupportedMedia &data) {
Unexpected("Unsupported message.");
}, [](std::nullopt_t) {});

View File

@@ -574,6 +574,29 @@ QByteArray SerializeMessage(
? NumberToString(data.receiptMsgId)
: QByteArray()) }
}));
}, [&](const Poll &data) {
context.nesting.push_back(Context::kObject);
const auto answers = ranges::view::all(
data.answers
) | ranges::view::transform([&](const Poll::Answer &answer) {
context.nesting.push_back(Context::kArray);
auto result = SerializeObject(context, {
{ "text", SerializeString(answer.text) },
{ "voters", NumberToString(answer.votes) },
{ "chosen", answer.my ? "true" : "false" },
});
context.nesting.pop_back();
return result;
}) | ranges::to_vector;
const auto serialized = SerializeArray(context, answers);
context.nesting.pop_back();
pushBare("poll", SerializeObject(context, {
{ "question", SerializeString(data.question) },
{ "closed", data.closed ? "true" : "false" },
{ "total_voters", NumberToString(data.totalVotes) },
{ "answers", serialized }
}));
}, [](const UnsupportedMedia &data) {
Unexpected("Unsupported message.");
}, [](std::nullopt_t) {});

View File

@@ -436,6 +436,19 @@ QByteArray SerializeMessage(
? "ID-" + NumberToString(data.receiptMsgId)
: QByteArray()) }
}));
}, [&](const Poll &data) {
push("Poll", SerializeKeyValue({
{ "Question", data.question },
{ "Closed", data.closed ? QByteArray("Yes") : QByteArray() },
{ "Votes", NumberToString(data.totalVotes) },
}));
for (const auto &answer : data.answers) {
push("Answer", SerializeKeyValue({
{ "Text", answer.text },
{ "Votes", NumberToString(answer.votes) },
{ "Chosen", answer.my ? QByteArray("Yes") : QByteArray() }
}));
}
}, [](const UnsupportedMedia &data) {
Unexpected("Unsupported message.");
}, [](std::nullopt_t) {});