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"
|
|
|
|
|
2020-01-09 17:24:54 +00:00
|
|
|
#include "data/data_user.h"
|
|
|
|
#include "data/data_session.h"
|
2020-04-14 12:14:55 +00:00
|
|
|
#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-24 17:03:53 +00:00
|
|
|
|
2018-12-18 05:43:11 +00:00
|
|
|
namespace {
|
|
|
|
|
2019-02-19 06:57:53 +00:00
|
|
|
constexpr auto kShortPollTimeout = 30 * crl::time(1000);
|
2020-04-14 12:14:55 +00:00
|
|
|
constexpr auto kReloadAfterAutoCloseDelay = 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
|
|
|
|
|
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;
|
2020-04-14 12:14:55 +00:00
|
|
|
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());
|
2020-01-09 14:13:35 +00:00
|
|
|
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();
|
2021-03-13 12:12:08 +00:00
|
|
|
auto newAnswers = ranges::views::all(
|
2019-07-05 13:38:38 +00:00
|
|
|
poll.vanswers().v
|
2021-03-13 12:12:08 +00:00
|
|
|
) | ranges::views::transform([](const MTPPollAnswer &data) {
|
2018-12-18 05:43:11 +00:00
|
|
|
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;
|
|
|
|
});
|
2021-03-13 12:12:08 +00:00
|
|
|
}) | ranges::views::take(
|
2018-12-25 14:17:02 +00:00
|
|
|
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)
|
2020-01-09 14:13:35 +00:00
|
|
|
|| (_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;
|
2020-01-09 14:13:35 +00:00
|
|
|
_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;
|
2020-01-09 14:13:35 +00:00
|
|
|
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) {
|
2020-04-13 08:27:53 +00:00
|
|
|
_lastResultsUpdate = crl::now();
|
2018-12-25 14:17:02 +00:00
|
|
|
|
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()) {
|
2021-04-01 21:04:10 +00:00
|
|
|
const auto bareProj = [](not_null<UserData*> user) {
|
|
|
|
return peerToUser(user->id).bare;
|
|
|
|
};
|
2020-01-09 17:24:54 +00:00
|
|
|
const auto recentChanged = !ranges::equal(
|
|
|
|
recentVoters,
|
|
|
|
recent->v,
|
|
|
|
ranges::equal_to(),
|
2021-04-01 21:04:10 +00:00
|
|
|
bareProj,
|
2021-08-25 08:15:05 +00:00
|
|
|
&MTPlong::v);
|
2020-01-09 17:24:54 +00:00
|
|
|
if (recentChanged) {
|
|
|
|
changed = true;
|
2021-03-13 12:12:08 +00:00
|
|
|
recentVoters = ranges::views::all(
|
2020-01-09 17:24:54 +00:00
|
|
|
recent->v
|
2021-08-25 08:15:05 +00:00
|
|
|
) | ranges::views::transform([&](MTPlong userId) {
|
2020-01-14 11:55:18 +00:00
|
|
|
const auto user = _owner->user(userId.v);
|
2020-09-01 06:44:18 +00:00
|
|
|
return user->isMinimalLoaded() ? user.get() : nullptr;
|
2021-03-13 12:12:08 +00:00
|
|
|
}) | ranges::views::filter([](UserData *user) {
|
2020-01-09 17:24:54 +00:00
|
|
|
return user != nullptr;
|
2021-03-13 12:12:08 +00:00
|
|
|
}) | ranges::views::transform([](UserData *user) {
|
2020-01-09 17:24:54 +00:00
|
|
|
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
|
|
|
}
|
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;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-10-20 19:56:10 +00:00
|
|
|
bool PollData::checkResultsReload(crl::time now) {
|
2020-04-13 08:27:53 +00:00
|
|
|
if (_lastResultsUpdate > 0
|
|
|
|
&& _lastResultsUpdate + kShortPollTimeout > now) {
|
2021-10-20 19:56:10 +00:00
|
|
|
return false;
|
2020-04-13 08:27:53 +00:00
|
|
|
} else if (closed() && _lastResultsUpdate >= 0) {
|
2021-10-20 19:56:10 +00:00
|
|
|
return false;
|
2018-12-24 17:03:53 +00:00
|
|
|
}
|
2020-04-13 08:27:53 +00:00
|
|
|
_lastResultsUpdate = now;
|
2021-10-20 19:56:10 +00:00
|
|
|
return true;
|
2018-12-24 17:03:53 +00:00
|
|
|
}
|
|
|
|
|
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-01-24 07:31:18 +00:00
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-09 14:13:35 +00:00
|
|
|
PollData::Flags PollData::flags() const {
|
|
|
|
return _flags;
|
|
|
|
}
|
|
|
|
|
2018-12-19 11:20:04 +00:00
|
|
|
bool PollData::voted() const {
|
2020-05-18 19:33:14 +00:00
|
|
|
return ranges::contains(answers, true, &PollAnswer::chosen);
|
2018-12-19 11:20:04 +00:00
|
|
|
}
|
2018-12-20 16:02:44 +00:00
|
|
|
|
2020-01-09 14:13:35 +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) {
|
2018-12-20 16:02:44 +00:00
|
|
|
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));
|
2018-12-20 16:02:44 +00:00
|
|
|
return MTP_poll(
|
|
|
|
MTP_long(poll->id),
|
2020-01-13 10:49:18 +00:00
|
|
|
MTP_flags(flags),
|
2018-12-20 16:02:44 +00:00
|
|
|
MTP_string(poll->question),
|
2020-04-06 09:07:49 +00:00
|
|
|
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);
|
2018-12-20 16:02:44 +00:00
|
|
|
}
|