tdesktop/Telegram/SourceFiles/data/data_poll.cpp

319 lines
8.3 KiB
C++
Raw Normal View History

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"
#include "apiwrap.h"
2020-01-09 17:24:54 +00:00
#include "data/data_user.h"
#include "data/data_session.h"
#include "base/call_delayed.h"
2019-07-24 11:45:24 +00:00
#include "main/main_session.h"
2020-04-06 14:55:31 +00:00
#include "api/api_text_entities.h"
2020-10-10 09:15:37 +00:00
#include "ui/text/text_options.h"
2018-12-18 05:43:11 +00:00
namespace {
constexpr auto kShortPollTimeout = 30 * crl::time(1000);
constexpr auto kReloadAfterAutoCloseDelay = crl::time(1000);
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
2020-01-09 17:24:54 +00:00
PollData::PollData(not_null<Data::Session*> owner, PollId id)
: id(id)
, _owner(owner) {
2018-12-18 05:43:11 +00:00
}
2020-06-08 08:03:45 +00:00
Data::Session &PollData::owner() const {
return *_owner;
}
Main::Session &PollData::session() const {
return _owner->session();
}
2020-04-07 12:10:34 +00:00
bool PollData::closeByTimer() {
if (closed()) {
return false;
}
_flags |= Flag::Closed;
++version;
base::call_delayed(kReloadAfterAutoCloseDelay, &_owner->session(), [=] {
_lastResultsUpdate = -1; // Force reload results.
++version;
_owner->notifyPollUpdateDelayed(this);
});
2020-04-07 12:10:34 +00:00
return true;
}
2018-12-18 05:43:11 +00:00
bool PollData::applyChanges(const MTPDpoll &poll) {
2019-07-05 13:38:38 +00:00
Expects(poll.vid().v == id);
2018-12-18 05:43:11 +00:00
2019-07-05 13:38:38 +00:00
const auto newQuestion = qs(poll.vquestion());
const auto newFlags = (poll.is_closed() ? Flag::Closed : Flag(0))
| (poll.is_public_voters() ? Flag::PublicVotes : Flag(0))
| (poll.is_multiple_choice() ? Flag::MultiChoice : Flag(0))
| (poll.is_quiz() ? Flag::Quiz : Flag(0));
2020-04-07 12:10:34 +00:00
const auto newCloseDate = poll.vclose_date().value_or_empty();
const auto newClosePeriod = poll.vclose_period().value_or_empty();
2018-12-18 05:43:11 +00:00
auto newAnswers = ranges::view::all(
2019-07-05 13:38:38 +00:00
poll.vanswers().v
2018-12-18 05:43:11 +00:00
) | ranges::view::transform([](const MTPPollAnswer &data) {
return data.match([](const MTPDpollAnswer &answer) {
auto result = PollAnswer();
2019-07-05 13:38:38 +00:00
result.option = answer.voption().v;
result.text = qs(answer.vtext());
2018-12-18 05:43:11 +00:00
return result;
});
}) | ranges::view::take(
kMaxOptions
) | ranges::to_vector;
2018-12-18 05:43:11 +00:00
const auto changed1 = (question != newQuestion)
2020-04-07 12:10:34 +00:00
|| (closeDate != newCloseDate)
|| (closePeriod != newClosePeriod)
|| (_flags != newFlags);
2018-12-18 05:43:11 +00:00
const auto changed2 = (answers != newAnswers);
if (!changed1 && !changed2) {
return false;
}
if (changed1) {
question = newQuestion;
2020-04-07 12:10:34 +00:00
closeDate = newCloseDate;
closePeriod = newClosePeriod;
_flags = newFlags;
2018-12-18 05:43:11 +00:00
}
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;
current->correct = old.correct;
2018-12-18 05:43:11 +00:00
}
}
}
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) {
_lastResultsUpdate = crl::now();
2019-07-05 13:38:38 +00:00
const auto newTotalVoters =
results.vtotal_voters().value_or(totalVoters);
2018-12-18 05:43:11 +00:00
auto changed = (newTotalVoters != totalVoters);
2019-07-05 13:38:38 +00:00
if (const auto list = results.vresults()) {
for (const auto &result : list->v) {
2018-12-18 05:43:11 +00:00
if (applyResultToAnswers(result, results.is_min())) {
changed = true;
}
}
}
2020-01-09 17:24:54 +00:00
if (const auto recent = results.vrecent_voters()) {
const auto recentChanged = !ranges::equal(
recentVoters,
recent->v,
ranges::equal_to(),
&UserData::id,
&MTPint::v);
if (recentChanged) {
changed = true;
recentVoters = ranges::view::all(
recent->v
) | ranges::view::transform([&](MTPint userId) {
2020-01-14 11:55:18 +00:00
const auto user = _owner->user(userId.v);
return user->isMinimalLoaded() ? user.get() : nullptr;
2020-01-09 17:24:54 +00:00
}) | ranges::view::filter([](UserData *user) {
return user != nullptr;
}) | ranges::view::transform([](UserData *user) {
return not_null<UserData*>(user);
}) | ranges::to_vector;
}
}
2020-04-17 10:58:23 +00:00
if (results.vsolution()) {
auto newSolution = TextWithEntities{
results.vsolution().value_or_empty(),
Api::EntitiesFromMTP(
2020-06-08 08:03:45 +00:00
&_owner->session(),
2020-04-17 10:58:23 +00:00
results.vsolution_entities().value_or_empty())
};
if (solution != newSolution) {
solution = std::move(newSolution);
changed = true;
}
2020-04-06 14:55:31 +00:00
}
if (!changed) {
return false;
}
2018-12-18 05:43:11 +00:00
totalVoters = newTotalVoters;
++version;
2018-12-18 05:43:11 +00:00
return changed;
});
}
void PollData::checkResultsReload(
not_null<HistoryItem*> item,
crl::time now) {
if (_lastResultsUpdate > 0
&& _lastResultsUpdate + kShortPollTimeout > now) {
return;
} else if (closed() && _lastResultsUpdate >= 0) {
return;
}
_lastResultsUpdate = now;
2020-01-09 17:24:54 +00:00
_owner->session().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) {
2019-07-05 13:38:38 +00:00
const auto &option = voters.voption().v;
2018-12-18 05:43:11 +00:00
const auto answer = answerByOption(option);
if (!answer) {
return false;
}
2019-07-05 13:38:38 +00:00
auto changed = (answer->votes != voters.vvoters().v);
2018-12-18 05:43:11 +00:00
if (changed) {
2019-07-05 13:38:38 +00:00
answer->votes = voters.vvoters().v;
2018-12-18 05:43:11 +00:00
}
if (!isMinResults) {
if (answer->chosen != voters.is_chosen()) {
answer->chosen = voters.is_chosen();
changed = true;
}
}
2020-04-17 10:58:23 +00:00
if (voters.is_correct() && !answer->correct) {
answer->correct = voters.is_correct();
changed = true;
2018-12-18 05:43:11 +00:00
}
return changed;
});
}
2018-12-19 11:20:04 +00:00
2020-01-13 10:49:18 +00:00
void PollData::setFlags(Flags flags) {
if (_flags != flags) {
_flags = flags;
++version;
}
}
PollData::Flags PollData::flags() const {
return _flags;
}
2018-12-19 11:20:04 +00:00
bool PollData::voted() const {
return ranges::contains(answers, true, &PollAnswer::chosen);
2018-12-19 11:20:04 +00:00
}
bool PollData::closed() const {
return (_flags & Flag::Closed);
}
bool PollData::publicVotes() const {
return (_flags & Flag::PublicVotes);
}
bool PollData::multiChoice() const {
return (_flags & Flag::MultiChoice);
}
bool PollData::quiz() const {
return (_flags & Flag::Quiz);
}
2020-01-13 10:49:18 +00:00
MTPPoll PollDataToMTP(not_null<const PollData*> poll, bool close) {
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);
2020-01-13 10:49:18 +00:00
using Flag = MTPDpoll::Flag;
const auto flags = ((poll->closed() || close) ? Flag::f_closed : Flag(0))
| (poll->multiChoice() ? Flag::f_multiple_choice : Flag(0))
| (poll->publicVotes() ? Flag::f_public_voters : Flag(0))
2020-04-06 14:55:31 +00:00
| (poll->quiz() ? Flag::f_quiz : Flag(0))
2020-04-07 12:10:34 +00:00
| (poll->closePeriod > 0 ? Flag::f_close_period : Flag(0))
| (poll->closeDate > 0 ? Flag::f_close_date : Flag(0));
return MTP_poll(
MTP_long(poll->id),
2020-01-13 10:49:18 +00:00
MTP_flags(flags),
MTP_string(poll->question),
MTP_vector<MTPPollAnswer>(answers),
2020-04-06 14:55:31 +00:00
MTP_int(poll->closePeriod),
MTP_int(poll->closeDate));
}
MTPInputMedia PollDataToInputMedia(
not_null<const PollData*> poll,
bool close) {
auto inputFlags = MTPDinputMediaPoll::Flag(0)
| (poll->quiz()
? MTPDinputMediaPoll::Flag::f_correct_answers
: MTPDinputMediaPoll::Flag(0));
auto correct = QVector<MTPbytes>();
for (const auto &answer : poll->answers) {
if (answer.correct) {
correct.push_back(MTP_bytes(answer.option));
}
}
auto solution = poll->solution;
const auto prepareFlags = Ui::ItemTextDefaultOptions().flags;
TextUtilities::PrepareForSending(solution, prepareFlags);
TextUtilities::Trim(solution);
const auto sentEntities = Api::EntitiesToMTP(
2020-06-08 08:03:45 +00:00
&poll->session(),
2020-04-06 14:55:31 +00:00
solution.entities,
Api::ConvertOption::SkipLocal);
if (!solution.text.isEmpty()) {
inputFlags |= MTPDinputMediaPoll::Flag::f_solution;
}
if (!sentEntities.v.isEmpty()) {
inputFlags |= MTPDinputMediaPoll::Flag::f_solution_entities;
}
return MTP_inputMediaPoll(
MTP_flags(inputFlags),
PollDataToMTP(poll, close),
MTP_vector<MTPbytes>(correct),
MTP_string(solution.text),
sentEntities);
}