mirror of
https://github.com/AyuGram/AyuGramDesktop.git
synced 2026-07-25 15:04:57 +00:00
440 lines
12 KiB
C++
440 lines
12 KiB
C++
/*
|
|
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 "api/api_text_entities.h"
|
|
#include "data/data_user.h"
|
|
#include "data/data_session.h"
|
|
#include "base/call_delayed.h"
|
|
#include "main/main_session.h"
|
|
#include "api/api_text_entities.h"
|
|
#include "ui/text/text_options.h"
|
|
|
|
namespace {
|
|
|
|
constexpr auto kShortPollTimeout = 30 * crl::time(1000);
|
|
constexpr auto kReloadAfterAutoCloseDelay = crl::time(1000);
|
|
|
|
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(not_null<Data::Session*> owner, PollId id)
|
|
: id(id)
|
|
, _owner(owner) {
|
|
}
|
|
|
|
Data::Session &PollData::owner() const {
|
|
return *_owner;
|
|
}
|
|
|
|
Main::Session &PollData::session() const {
|
|
return _owner->session();
|
|
}
|
|
|
|
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);
|
|
});
|
|
return true;
|
|
}
|
|
|
|
bool PollData::applyChanges(const MTPDpoll &poll) {
|
|
Expects(poll.vid().v == id);
|
|
|
|
const auto newQuestion = Api::ParseTextWithEntities(
|
|
&session(),
|
|
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))
|
|
| (poll.is_shuffle_answers() ? Flag::ShuffleAnswers : Flag(0))
|
|
| (poll.is_revoting_disabled() ? Flag::RevotingDisabled : Flag(0))
|
|
| (poll.is_open_answers() ? Flag::OpenAnswers : Flag(0))
|
|
| (poll.is_hide_results_until_close()
|
|
? Flag::HideResultsUntilClose
|
|
: Flag(0));
|
|
const auto newCloseDate = poll.vclose_date().value_or_empty();
|
|
const auto newClosePeriod = poll.vclose_period().value_or_empty();
|
|
auto newAnswers = ranges::views::all(
|
|
poll.vanswers().v
|
|
) | ranges::views::transform([&](const MTPPollAnswer &data) {
|
|
return data.match([&](const MTPDpollAnswer &answer) {
|
|
auto result = PollAnswer();
|
|
result.option = answer.voption().v;
|
|
result.text = Api::ParseTextWithEntities(
|
|
&session(),
|
|
answer.vtext());
|
|
if (const auto media = answer.vmedia()) {
|
|
result.media = PollMediaToInputMedia(*media);
|
|
}
|
|
return result;
|
|
}, [&](const MTPDinputPollAnswer &answer) {
|
|
auto result = PollAnswer();
|
|
result.text = Api::ParseTextWithEntities(
|
|
&session(),
|
|
answer.vtext());
|
|
if (const auto media = answer.vmedia()) {
|
|
result.media = *media;
|
|
}
|
|
return result;
|
|
}, [](const auto &) {
|
|
return PollAnswer();
|
|
});
|
|
}) | ranges::views::take(
|
|
kMaxOptions
|
|
) | ranges::to_vector;
|
|
|
|
const auto changed1 = (question != newQuestion)
|
|
|| (closeDate != newCloseDate)
|
|
|| (closePeriod != newClosePeriod)
|
|
|| (_flags != newFlags);
|
|
const auto changed2 = (answers != newAnswers);
|
|
if (!changed1 && !changed2) {
|
|
return false;
|
|
}
|
|
if (changed1) {
|
|
question = newQuestion;
|
|
closeDate = newCloseDate;
|
|
closePeriod = newClosePeriod;
|
|
_flags = newFlags;
|
|
}
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
++version;
|
|
return true;
|
|
}
|
|
|
|
bool PollData::applyResults(const MTPPollResults &results) {
|
|
return results.match([&](const MTPDpollResults &results) {
|
|
_lastResultsUpdate = crl::now();
|
|
|
|
const auto newTotalVoters
|
|
= results.vtotal_voters().value_or(totalVoters);
|
|
auto changed = (newTotalVoters != totalVoters);
|
|
if (const auto list = results.vresults()) {
|
|
for (const auto &result : list->v) {
|
|
if (applyResultToAnswers(result, results.is_min())) {
|
|
changed = true;
|
|
}
|
|
}
|
|
}
|
|
if (const auto recent = results.vrecent_voters()) {
|
|
const auto recentChanged = !ranges::equal(
|
|
recentVoters,
|
|
recent->v,
|
|
ranges::equal_to(),
|
|
&PeerData::id,
|
|
peerFromMTP);
|
|
if (recentChanged) {
|
|
changed = true;
|
|
recentVoters = ranges::views::all(
|
|
recent->v
|
|
) | ranges::views::transform([&](MTPPeer peerId) {
|
|
const auto peer = _owner->peer(peerFromMTP(peerId));
|
|
return peer->isMinimalLoaded() ? peer.get() : nullptr;
|
|
}) | ranges::views::filter([](PeerData *peer) {
|
|
return peer != nullptr;
|
|
}) | ranges::views::transform([](PeerData *peer) {
|
|
return not_null(peer);
|
|
}) | ranges::to_vector;
|
|
}
|
|
}
|
|
if (results.vsolution()) {
|
|
auto newSolution = TextWithEntities{
|
|
results.vsolution().value_or_empty(),
|
|
Api::EntitiesFromMTP(
|
|
&_owner->session(),
|
|
results.vsolution_entities().value_or_empty())
|
|
};
|
|
if (solution != newSolution) {
|
|
solution = std::move(newSolution);
|
|
changed = true;
|
|
}
|
|
}
|
|
if (const auto media = results.vsolution_media()) {
|
|
const auto parsed = PollMediaToInputMedia(*media);
|
|
const auto changedMedia = !parsed
|
|
? solutionMedia.has_value()
|
|
: (!solutionMedia || (solutionMedia->type() != parsed->type()));
|
|
solutionMedia = parsed;
|
|
if (changedMedia) {
|
|
changed = true;
|
|
}
|
|
}
|
|
if (!changed) {
|
|
return false;
|
|
}
|
|
totalVoters = newTotalVoters;
|
|
++version;
|
|
return changed;
|
|
});
|
|
}
|
|
|
|
bool PollData::checkResultsReload(crl::time now) {
|
|
if (_lastResultsUpdate > 0
|
|
&& _lastResultsUpdate + kShortPollTimeout > now) {
|
|
return false;
|
|
} else if (closed() && _lastResultsUpdate >= 0) {
|
|
return false;
|
|
}
|
|
_lastResultsUpdate = now;
|
|
return true;
|
|
}
|
|
|
|
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 = false;
|
|
if (const auto count = voters.vvoters()) {
|
|
if (answer->votes != count->v) {
|
|
answer->votes = count->v;
|
|
changed = true;
|
|
}
|
|
}
|
|
if (!isMinResults) {
|
|
if (answer->chosen != voters.is_chosen()) {
|
|
answer->chosen = voters.is_chosen();
|
|
changed = true;
|
|
}
|
|
}
|
|
if (voters.is_correct() && !answer->correct) {
|
|
answer->correct = voters.is_correct();
|
|
changed = true;
|
|
}
|
|
return changed;
|
|
});
|
|
}
|
|
|
|
void PollData::setFlags(Flags flags) {
|
|
if (_flags != flags) {
|
|
_flags = flags;
|
|
++version;
|
|
}
|
|
}
|
|
|
|
PollData::Flags PollData::flags() const {
|
|
return _flags;
|
|
}
|
|
|
|
bool PollData::voted() const {
|
|
return ranges::contains(answers, true, &PollAnswer::chosen);
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
bool PollData::shuffleAnswers() const {
|
|
return (_flags & Flag::ShuffleAnswers);
|
|
}
|
|
|
|
bool PollData::revotingDisabled() const {
|
|
return (_flags & Flag::RevotingDisabled);
|
|
}
|
|
|
|
bool PollData::openAnswers() const {
|
|
return (_flags & Flag::OpenAnswers);
|
|
}
|
|
|
|
bool PollData::hideResultsUntilClose() const {
|
|
return (_flags & Flag::HideResultsUntilClose);
|
|
}
|
|
|
|
std::optional<MTPInputMedia> PollMediaToInputMedia(
|
|
const MTPMessageMedia &media) {
|
|
return media.match([&](const MTPDmessageMediaPhoto &media) {
|
|
const auto photo = media.vphoto();
|
|
if (!photo || photo->type() != mtpc_photo) {
|
|
return std::optional<MTPInputMedia>();
|
|
}
|
|
const auto &fields = photo->c_photo();
|
|
using Flag = MTPDinputMediaPhoto::Flag;
|
|
const auto flags = media.vttl_seconds()
|
|
? Flag::f_ttl_seconds
|
|
: Flag(0);
|
|
return std::optional<MTPInputMedia>(MTP_inputMediaPhoto(
|
|
MTP_flags(flags),
|
|
MTP_inputPhoto(
|
|
fields.vid(),
|
|
fields.vaccess_hash(),
|
|
fields.vfile_reference()),
|
|
MTP_int(media.vttl_seconds().value_or_empty()),
|
|
MTPInputDocument()));
|
|
}, [&](const MTPDmessageMediaDocument &media) {
|
|
const auto document = media.vdocument();
|
|
if (!document || document->type() != mtpc_document) {
|
|
return std::optional<MTPInputMedia>();
|
|
}
|
|
const auto &fields = document->c_document();
|
|
using Flag = MTPDinputMediaDocument::Flag;
|
|
const auto flags = Flag()
|
|
| (media.vttl_seconds() ? Flag::f_ttl_seconds : Flag())
|
|
| (media.vvideo_timestamp()
|
|
? Flag::f_video_timestamp
|
|
: Flag());
|
|
return std::optional<MTPInputMedia>(MTP_inputMediaDocument(
|
|
MTP_flags(flags),
|
|
MTP_inputDocument(
|
|
fields.vid(),
|
|
fields.vaccess_hash(),
|
|
fields.vfile_reference()),
|
|
MTPInputPhoto(),
|
|
MTP_int(media.vvideo_timestamp().value_or_empty()),
|
|
MTP_int(media.vttl_seconds().value_or_empty()),
|
|
MTPstring()));
|
|
}, [](const auto &) {
|
|
return std::optional<MTPInputMedia>();
|
|
});
|
|
}
|
|
|
|
MTPPoll PollDataToMTP(not_null<const PollData*> poll, bool close) {
|
|
const auto convert = [&](const PollAnswer &answer) {
|
|
const auto flags = answer.media
|
|
? MTPDinputPollAnswer::Flag::f_media
|
|
: MTPDinputPollAnswer::Flag(0);
|
|
return MTP_inputPollAnswer(
|
|
MTP_flags(flags),
|
|
MTP_textWithEntities(
|
|
MTP_string(answer.text.text),
|
|
Api::EntitiesToMTP(&poll->session(), answer.text.entities)),
|
|
answer.media ? *answer.media : MTPInputMedia());
|
|
};
|
|
auto answers = QVector<MTPPollAnswer>();
|
|
answers.reserve(poll->answers.size());
|
|
ranges::transform(
|
|
poll->answers,
|
|
ranges::back_inserter(answers),
|
|
convert);
|
|
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))
|
|
| (poll->quiz() ? Flag::f_quiz : Flag(0))
|
|
| (poll->shuffleAnswers() ? Flag::f_shuffle_answers : Flag(0))
|
|
| (poll->revotingDisabled() ? Flag::f_revoting_disabled : Flag(0))
|
|
| (poll->openAnswers() ? Flag::f_open_answers : Flag(0))
|
|
| (poll->hideResultsUntilClose()
|
|
? Flag::f_hide_results_until_close
|
|
: Flag(0))
|
|
| (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),
|
|
MTP_flags(flags),
|
|
MTP_textWithEntities(
|
|
MTP_string(poll->question.text),
|
|
Api::EntitiesToMTP(&poll->session(), poll->question.entities)),
|
|
MTP_vector<MTPPollAnswer>(answers),
|
|
MTP_int(poll->closePeriod),
|
|
MTP_int(poll->closeDate),
|
|
MTP_long(0));
|
|
}
|
|
|
|
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<MTPint>();
|
|
for (auto i = 0, count = int(poll->answers.size()); i < count; ++i) {
|
|
if (poll->answers[i].correct) {
|
|
correct.push_back(MTP_int(i));
|
|
}
|
|
}
|
|
|
|
auto solution = poll->solution;
|
|
const auto prepareFlags = Ui::ItemTextDefaultOptions().flags;
|
|
TextUtilities::PrepareForSending(solution, prepareFlags);
|
|
TextUtilities::Trim(solution);
|
|
const auto sentEntities = Api::EntitiesToMTP(
|
|
&poll->session(),
|
|
solution.entities,
|
|
Api::ConvertOption::SkipLocal);
|
|
if (!solution.text.isEmpty()) {
|
|
inputFlags |= MTPDinputMediaPoll::Flag::f_solution;
|
|
}
|
|
if (!sentEntities.v.isEmpty()) {
|
|
inputFlags |= MTPDinputMediaPoll::Flag::f_solution_entities;
|
|
}
|
|
if (poll->attachedMedia) {
|
|
inputFlags |= MTPDinputMediaPoll::Flag::f_attached_media;
|
|
}
|
|
if (poll->solutionMedia) {
|
|
inputFlags |= MTPDinputMediaPoll::Flag::f_solution_media;
|
|
}
|
|
return MTP_inputMediaPoll(
|
|
MTP_flags(inputFlags),
|
|
PollDataToMTP(poll, close),
|
|
MTP_vector<MTPint>(correct),
|
|
poll->attachedMedia ? *poll->attachedMedia : MTPInputMedia(),
|
|
MTP_string(solution.text),
|
|
sentEntities,
|
|
poll->solutionMedia ? *poll->solutionMedia : MTPInputMedia());
|
|
}
|