diff --git a/Telegram/SourceFiles/apiwrap.cpp b/Telegram/SourceFiles/apiwrap.cpp index 774bbd51e..823f9e72f 100644 --- a/Telegram/SourceFiles/apiwrap.cpp +++ b/Telegram/SourceFiles/apiwrap.cpp @@ -5437,6 +5437,24 @@ void ApiWrap::closePoll(FullMsgId itemId) { _pollCloseRequestIds.emplace(itemId, requestId); } +void ApiWrap::reloadPollResults(not_null item) { + const auto itemId = item->fullId(); + if (!IsServerMsgId(item->id) + || _pollReloadRequestIds.contains(itemId)) { + return; + } + const auto requestId = request(MTPmessages_GetPollResults( + item->history()->peer->input, + MTP_int(item->id) + )).done([=](const MTPUpdates &result) { + _pollReloadRequestIds.erase(itemId); + applyUpdates(result); + }).fail([=](const RPCError &error) { + _pollReloadRequestIds.erase(itemId); + }).send(); + _pollReloadRequestIds.emplace(itemId, requestId); +} + void ApiWrap::readServerHistory(not_null history) { if (history->unreadCount()) { readServerHistoryForce(history); diff --git a/Telegram/SourceFiles/apiwrap.h b/Telegram/SourceFiles/apiwrap.h index ded168558..2cd351861 100644 --- a/Telegram/SourceFiles/apiwrap.h +++ b/Telegram/SourceFiles/apiwrap.h @@ -386,6 +386,7 @@ public: FullMsgId itemId, const std::vector &options); void closePoll(FullMsgId itemId); + void reloadPollResults(not_null item); ~ApiWrap(); @@ -751,5 +752,6 @@ private: base::flat_map _pollVotesRequestIds; base::flat_map _pollCloseRequestIds; + base::flat_map _pollReloadRequestIds; }; diff --git a/Telegram/SourceFiles/data/data_poll.cpp b/Telegram/SourceFiles/data/data_poll.cpp index 10d2519c3..29180e4b2 100644 --- a/Telegram/SourceFiles/data/data_poll.cpp +++ b/Telegram/SourceFiles/data/data_poll.cpp @@ -7,8 +7,13 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #include "data/data_poll.h" +#include "apiwrap.h" +#include "auth_session.h" + namespace { +constexpr auto kShortPollTimeout = 30 * TimeMs(1000); + const PollAnswer *AnswerByOption( const std::vector &list, const QByteArray &option) { @@ -85,10 +90,21 @@ bool PollData::applyResults(const MTPPollResults &results) { } } totalVoters = newTotalVoters; + lastResultsUpdate = getms(); return changed; }); } +void PollData::checkResultsReload(not_null item, TimeMs now) { + if (lastResultsUpdate && lastResultsUpdate + kShortPollTimeout > now) { + return; + } else if (closed) { + return; + } + lastResultsUpdate = now; + Auth().api().reloadPollResults(item); +} + PollAnswer *PollData::answerByOption(const QByteArray &option) { return AnswerByOption(answers, option); } diff --git a/Telegram/SourceFiles/data/data_poll.h b/Telegram/SourceFiles/data/data_poll.h index 8c0604324..6ad481b3e 100644 --- a/Telegram/SourceFiles/data/data_poll.h +++ b/Telegram/SourceFiles/data/data_poll.h @@ -28,6 +28,7 @@ struct PollData { bool applyChanges(const MTPDpoll &poll); bool applyResults(const MTPPollResults &results); + void checkResultsReload(not_null item, TimeMs now); PollAnswer *answerByOption(const QByteArray &option); const PollAnswer *answerByOption(const QByteArray &option) const; @@ -40,6 +41,7 @@ struct PollData { int totalVoters = 0; bool closed = false; QByteArray sendingVote; + TimeMs lastResultsUpdate = 0; int version = 0; diff --git a/Telegram/SourceFiles/history/media/history_media_poll.cpp b/Telegram/SourceFiles/history/media/history_media_poll.cpp index 65ccaac7a..4fadba285 100644 --- a/Telegram/SourceFiles/history/media/history_media_poll.cpp +++ b/Telegram/SourceFiles/history/media/history_media_poll.cpp @@ -401,6 +401,7 @@ void HistoryPoll::draw(Painter &p, const QRect &r, TextSelection selection, Time auto paintx = 0, painty = 0, paintw = width(), painth = height(); updateVotesCheckAnimations(); + _poll->checkResultsReload(_parent->data(), ms); const auto outbg = _parent->hasOutLayout(); const auto selected = (selection == FullSelection); diff --git a/Telegram/SourceFiles/history/media/history_media_poll.h b/Telegram/SourceFiles/history/media/history_media_poll.h index e3b242853..d54df3aa7 100644 --- a/Telegram/SourceFiles/history/media/history_media_poll.h +++ b/Telegram/SourceFiles/history/media/history_media_poll.h @@ -111,6 +111,7 @@ private: void resetAnswersAnimation() const; void step_radial(TimeMs ms, bool timer); + void checkPollResultsReload(TimeMs ms) const; void toggleRipple(Answer &answer, bool pressed); not_null _poll;