2018-12-18 05:43:11 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop application for the Telegram messaging service.
|
|
|
|
|
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
|
|
|
*/
|
|
|
|
#include "data/data_poll.h"
|
|
|
|
|
2018-12-24 17:03:53 +00:00
|
|
|
#include "apiwrap.h"
|
|
|
|
#include "auth_session.h"
|
|
|
|
|
2018-12-18 05:43:11 +00:00
|
|
|
namespace {
|
|
|
|
|
2019-02-19 06:57:53 +00:00
|
|
|
constexpr auto kShortPollTimeout = 30 * crl::time(1000);
|
2018-12-24 17:03:53 +00:00
|
|
|
|
2018-12-18 05:43:11 +00:00
|
|
|
const PollAnswer *AnswerByOption(
|
|
|
|
const std::vector<PollAnswer> &list,
|
|
|
|
const QByteArray &option) {
|
|
|
|
const auto i = ranges::find(
|
|
|
|
list,
|
|
|
|
option,
|
|
|
|
[](const PollAnswer &a) { return a.option; });
|
|
|
|
return (i != end(list)) ? &*i : nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
PollAnswer *AnswerByOption(
|
|
|
|
std::vector<PollAnswer> &list,
|
|
|
|
const QByteArray &option) {
|
|
|
|
return const_cast<PollAnswer*>(AnswerByOption(
|
|
|
|
std::as_const(list),
|
|
|
|
option));
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
PollData::PollData(PollId id) : id(id) {
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PollData::applyChanges(const MTPDpoll &poll) {
|
|
|
|
Expects(poll.vid.v == id);
|
|
|
|
|
|
|
|
const auto newQuestion = qs(poll.vquestion);
|
|
|
|
const auto newClosed = poll.is_closed();
|
|
|
|
auto newAnswers = ranges::view::all(
|
|
|
|
poll.vanswers.v
|
|
|
|
) | ranges::view::transform([](const MTPPollAnswer &data) {
|
|
|
|
return data.match([](const MTPDpollAnswer &answer) {
|
|
|
|
auto result = PollAnswer();
|
|
|
|
result.option = answer.voption.v;
|
|
|
|
result.text = qs(answer.vtext);
|
|
|
|
return result;
|
|
|
|
});
|
2018-12-25 14:17:02 +00:00
|
|
|
}) | ranges::view::take(
|
|
|
|
kMaxOptions
|
|
|
|
) | ranges::to_vector;
|
2018-12-18 05:43:11 +00:00
|
|
|
|
|
|
|
const auto changed1 = (question != newQuestion)
|
|
|
|
|| (closed != newClosed);
|
|
|
|
const auto changed2 = (answers != newAnswers);
|
|
|
|
if (!changed1 && !changed2) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (changed1) {
|
|
|
|
question = newQuestion;
|
|
|
|
closed = newClosed;
|
|
|
|
}
|
|
|
|
if (changed2) {
|
|
|
|
std::swap(answers, newAnswers);
|
|
|
|
for (const auto &old : newAnswers) {
|
|
|
|
if (const auto current = answerByOption(old.option)) {
|
|
|
|
current->votes = old.votes;
|
|
|
|
current->chosen = old.chosen;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-18 13:56:38 +00:00
|
|
|
++version;
|
2018-12-18 05:43:11 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PollData::applyResults(const MTPPollResults &results) {
|
|
|
|
return results.match([&](const MTPDpollResults &results) {
|
2019-02-19 06:57:53 +00:00
|
|
|
lastResultsUpdate = crl::now();
|
2018-12-25 14:17:02 +00:00
|
|
|
|
2018-12-18 05:43:11 +00:00
|
|
|
const auto newTotalVoters = results.has_total_voters()
|
|
|
|
? results.vtotal_voters.v
|
|
|
|
: totalVoters;
|
|
|
|
auto changed = (newTotalVoters != totalVoters);
|
|
|
|
if (results.has_results()) {
|
|
|
|
for (const auto &result : results.vresults.v) {
|
|
|
|
if (applyResultToAnswers(result, results.is_min())) {
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-25 14:17:02 +00:00
|
|
|
if (!changed) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-12-18 05:43:11 +00:00
|
|
|
totalVoters = newTotalVoters;
|
2018-12-25 14:17:02 +00:00
|
|
|
++version;
|
2018-12-18 05:43:11 +00:00
|
|
|
return changed;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-02-19 06:57:53 +00:00
|
|
|
void PollData::checkResultsReload(not_null<HistoryItem*> item, crl::time now) {
|
2018-12-24 17:03:53 +00:00
|
|
|
if (lastResultsUpdate && lastResultsUpdate + kShortPollTimeout > now) {
|
|
|
|
return;
|
|
|
|
} else if (closed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
lastResultsUpdate = now;
|
|
|
|
Auth().api().reloadPollResults(item);
|
|
|
|
}
|
|
|
|
|
2018-12-18 05:43:11 +00:00
|
|
|
PollAnswer *PollData::answerByOption(const QByteArray &option) {
|
|
|
|
return AnswerByOption(answers, option);
|
|
|
|
}
|
|
|
|
|
|
|
|
const PollAnswer *PollData::answerByOption(const QByteArray &option) const {
|
|
|
|
return AnswerByOption(answers, option);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PollData::applyResultToAnswers(
|
|
|
|
const MTPPollAnswerVoters &result,
|
|
|
|
bool isMinResults) {
|
|
|
|
return result.match([&](const MTPDpollAnswerVoters &voters) {
|
|
|
|
const auto &option = voters.voption.v;
|
|
|
|
const auto answer = answerByOption(option);
|
|
|
|
if (!answer) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
auto changed = (answer->votes != voters.vvoters.v);
|
|
|
|
if (changed) {
|
|
|
|
answer->votes = voters.vvoters.v;
|
|
|
|
}
|
|
|
|
if (!isMinResults) {
|
|
|
|
if (answer->chosen != voters.is_chosen()) {
|
|
|
|
answer->chosen = voters.is_chosen();
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
} else if (const auto existing = answerByOption(option)) {
|
|
|
|
answer->chosen = existing->chosen;
|
|
|
|
}
|
|
|
|
return changed;
|
|
|
|
});
|
|
|
|
}
|
2018-12-19 11:20:04 +00:00
|
|
|
|
|
|
|
bool PollData::voted() const {
|
|
|
|
return ranges::find(answers, true, &PollAnswer::chosen) != end(answers);
|
|
|
|
}
|
2018-12-20 16:02:44 +00:00
|
|
|
|
|
|
|
MTPPoll PollDataToMTP(not_null<const PollData*> poll) {
|
|
|
|
const auto convert = [](const PollAnswer &answer) {
|
|
|
|
return MTP_pollAnswer(
|
|
|
|
MTP_string(answer.text),
|
|
|
|
MTP_bytes(answer.option));
|
|
|
|
};
|
|
|
|
auto answers = QVector<MTPPollAnswer>();
|
|
|
|
answers.reserve(poll->answers.size());
|
|
|
|
ranges::transform(
|
|
|
|
poll->answers,
|
|
|
|
ranges::back_inserter(answers),
|
|
|
|
convert);
|
|
|
|
return MTP_poll(
|
|
|
|
MTP_long(poll->id),
|
|
|
|
MTP_flags(MTPDpoll::Flag::f_closed),
|
|
|
|
MTP_string(poll->question),
|
|
|
|
MTP_vector<MTPPollAnswer>(answers));
|
|
|
|
}
|