2019-02-19 14:45:32 +01:00
|
|
|
//
|
2024-01-01 01:07:21 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
|
2019-02-19 14:45:32 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#include "td/telegram/PollManager.h"
|
|
|
|
|
2019-02-22 21:15:43 +01:00
|
|
|
#include "td/telegram/AccessRights.h"
|
2019-02-22 16:09:55 +01:00
|
|
|
#include "td/telegram/AuthManager.h"
|
2022-02-05 21:28:43 +01:00
|
|
|
#include "td/telegram/ChainId.h"
|
2020-01-12 02:40:17 +01:00
|
|
|
#include "td/telegram/ContactsManager.h"
|
2019-02-22 21:15:43 +01:00
|
|
|
#include "td/telegram/Dependencies.h"
|
|
|
|
#include "td/telegram/DialogId.h"
|
2024-01-03 21:07:50 +01:00
|
|
|
#include "td/telegram/DialogManager.h"
|
2019-02-19 14:45:32 +01:00
|
|
|
#include "td/telegram/Global.h"
|
|
|
|
#include "td/telegram/logevent/LogEvent.h"
|
2023-03-24 12:14:37 +01:00
|
|
|
#include "td/telegram/MessageId.h"
|
2023-04-25 15:41:04 +02:00
|
|
|
#include "td/telegram/MessageSender.h"
|
2019-02-20 23:54:31 +01:00
|
|
|
#include "td/telegram/MessagesManager.h"
|
2020-04-03 22:39:50 +02:00
|
|
|
#include "td/telegram/misc.h"
|
2019-02-21 15:40:37 +01:00
|
|
|
#include "td/telegram/PollId.hpp"
|
2019-02-19 14:45:32 +01:00
|
|
|
#include "td/telegram/PollManager.hpp"
|
2019-02-22 20:53:02 +01:00
|
|
|
#include "td/telegram/StateManager.h"
|
2019-02-19 14:45:32 +01:00
|
|
|
#include "td/telegram/Td.h"
|
2019-04-26 01:21:12 +02:00
|
|
|
#include "td/telegram/TdDb.h"
|
2023-07-01 13:53:04 +02:00
|
|
|
#include "td/telegram/telegram_api.h"
|
2019-02-21 13:23:05 +01:00
|
|
|
#include "td/telegram/UpdatesManager.h"
|
2019-02-19 14:45:32 +01:00
|
|
|
|
2019-02-21 15:40:37 +01:00
|
|
|
#include "td/db/binlog/BinlogEvent.h"
|
|
|
|
#include "td/db/binlog/BinlogHelper.h"
|
2019-02-19 14:45:32 +01:00
|
|
|
#include "td/db/SqliteKeyValue.h"
|
|
|
|
#include "td/db/SqliteKeyValueAsync.h"
|
|
|
|
|
2021-01-01 13:59:53 +01:00
|
|
|
#include "td/utils/algorithm.h"
|
2019-02-22 20:53:02 +01:00
|
|
|
#include "td/utils/buffer.h"
|
|
|
|
#include "td/utils/format.h"
|
2019-02-19 14:45:32 +01:00
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/misc.h"
|
2019-02-22 20:53:02 +01:00
|
|
|
#include "td/utils/Random.h"
|
2019-02-22 21:15:43 +01:00
|
|
|
#include "td/utils/Slice.h"
|
2021-05-17 14:21:11 +02:00
|
|
|
#include "td/utils/SliceBuilder.h"
|
2019-02-19 14:45:32 +01:00
|
|
|
#include "td/utils/Status.h"
|
2019-02-22 21:15:43 +01:00
|
|
|
#include "td/utils/tl_helpers.h"
|
2019-02-19 14:45:32 +01:00
|
|
|
|
2019-02-21 13:23:05 +01:00
|
|
|
#include <algorithm>
|
2019-02-22 21:15:43 +01:00
|
|
|
#include <limits>
|
2019-02-21 13:23:05 +01:00
|
|
|
|
2019-02-19 14:45:32 +01:00
|
|
|
namespace td {
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class GetPollResultsQuery final : public Td::ResultHandler {
|
2019-03-09 19:07:07 +01:00
|
|
|
Promise<tl_object_ptr<telegram_api::Updates>> promise_;
|
2019-02-22 20:53:02 +01:00
|
|
|
PollId poll_id_;
|
|
|
|
DialogId dialog_id_;
|
2023-03-09 13:54:31 +01:00
|
|
|
MessageId message_id_;
|
2019-02-22 20:53:02 +01:00
|
|
|
|
|
|
|
public:
|
2019-03-09 19:07:07 +01:00
|
|
|
explicit GetPollResultsQuery(Promise<tl_object_ptr<telegram_api::Updates>> &&promise) : promise_(std::move(promise)) {
|
|
|
|
}
|
|
|
|
|
2023-09-21 18:11:17 +02:00
|
|
|
void send(PollId poll_id, MessageFullId message_full_id) {
|
2019-02-22 20:53:02 +01:00
|
|
|
poll_id_ = poll_id;
|
2023-09-21 18:11:17 +02:00
|
|
|
dialog_id_ = message_full_id.get_dialog_id();
|
|
|
|
message_id_ = message_full_id.get_message_id();
|
2024-01-03 21:07:50 +01:00
|
|
|
auto input_peer = td_->dialog_manager_->get_input_peer(dialog_id_, AccessRights::Read);
|
2019-02-22 20:53:02 +01:00
|
|
|
if (input_peer == nullptr) {
|
|
|
|
LOG(INFO) << "Can't reget poll, because have no read access to " << dialog_id_;
|
2020-01-11 01:46:26 +01:00
|
|
|
return promise_.set_value(nullptr);
|
2019-02-22 20:53:02 +01:00
|
|
|
}
|
|
|
|
|
2023-03-09 13:54:31 +01:00
|
|
|
auto message_id = message_id_.get_server_message_id().get();
|
2020-03-15 22:17:11 +01:00
|
|
|
send_query(
|
|
|
|
G()->net_query_creator().create(telegram_api::messages_getPollResults(std::move(input_peer), message_id)));
|
2019-02-22 20:53:02 +01:00
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_result(BufferSlice packet) final {
|
2019-02-22 20:53:02 +01:00
|
|
|
auto result_ptr = fetch_result<telegram_api::messages_getPollResults>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
2021-11-08 12:19:57 +01:00
|
|
|
return on_error(result_ptr.move_as_error());
|
2019-02-22 20:53:02 +01:00
|
|
|
}
|
|
|
|
|
2019-03-09 19:07:07 +01:00
|
|
|
promise_.set_value(result_ptr.move_as_ok());
|
2019-02-22 20:53:02 +01:00
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_error(Status status) final {
|
2023-11-12 20:55:48 +01:00
|
|
|
if (!td_->messages_manager_->on_get_message_error(dialog_id_, message_id_, status, "GetPollResultsQuery")) {
|
2019-02-22 20:53:02 +01:00
|
|
|
LOG(ERROR) << "Receive " << status << ", while trying to get results of " << poll_id_;
|
|
|
|
}
|
2019-03-09 19:07:07 +01:00
|
|
|
promise_.set_error(std::move(status));
|
2019-02-22 20:53:02 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class GetPollVotersQuery final : public Td::ResultHandler {
|
2020-01-13 19:21:58 +01:00
|
|
|
Promise<tl_object_ptr<telegram_api::messages_votesList>> promise_;
|
|
|
|
PollId poll_id_;
|
|
|
|
DialogId dialog_id_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit GetPollVotersQuery(Promise<tl_object_ptr<telegram_api::messages_votesList>> &&promise)
|
|
|
|
: promise_(std::move(promise)) {
|
|
|
|
}
|
|
|
|
|
2023-09-21 18:11:17 +02:00
|
|
|
void send(PollId poll_id, MessageFullId message_full_id, BufferSlice &&option, const string &offset, int32 limit) {
|
2020-01-13 19:21:58 +01:00
|
|
|
poll_id_ = poll_id;
|
2023-09-21 18:11:17 +02:00
|
|
|
dialog_id_ = message_full_id.get_dialog_id();
|
2024-01-03 21:07:50 +01:00
|
|
|
auto input_peer = td_->dialog_manager_->get_input_peer(dialog_id_, AccessRights::Read);
|
2020-01-13 19:21:58 +01:00
|
|
|
if (input_peer == nullptr) {
|
|
|
|
LOG(INFO) << "Can't get poll, because have no read access to " << dialog_id_;
|
|
|
|
return promise_.set_error(Status::Error(400, "Chat is not accessible"));
|
|
|
|
}
|
|
|
|
|
|
|
|
CHECK(!option.empty());
|
|
|
|
int32 flags = telegram_api::messages_getPollVotes::OPTION_MASK;
|
|
|
|
if (!offset.empty()) {
|
|
|
|
flags |= telegram_api::messages_getPollVotes::OFFSET_MASK;
|
|
|
|
}
|
|
|
|
|
2023-09-21 18:11:17 +02:00
|
|
|
auto message_id = message_full_id.get_message_id().get_server_message_id().get();
|
2020-03-15 22:17:11 +01:00
|
|
|
send_query(G()->net_query_creator().create(telegram_api::messages_getPollVotes(
|
|
|
|
flags, std::move(input_peer), message_id, std::move(option), offset, limit)));
|
2020-01-13 19:21:58 +01:00
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_result(BufferSlice packet) final {
|
2020-01-13 19:21:58 +01:00
|
|
|
auto result_ptr = fetch_result<telegram_api::messages_getPollVotes>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
2021-11-08 12:19:57 +01:00
|
|
|
return on_error(result_ptr.move_as_error());
|
2020-01-13 19:21:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
promise_.set_value(result_ptr.move_as_ok());
|
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_error(Status status) final {
|
2024-01-03 21:07:50 +01:00
|
|
|
if (!td_->dialog_manager_->on_get_dialog_error(dialog_id_, status, "GetPollVotersQuery") &&
|
2020-01-13 19:21:58 +01:00
|
|
|
status.message() != "MESSAGE_ID_INVALID") {
|
|
|
|
LOG(ERROR) << "Receive " << status << ", while trying to get voters of " << poll_id_;
|
|
|
|
}
|
|
|
|
promise_.set_error(std::move(status));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-02-03 22:46:10 +01:00
|
|
|
class SendVoteQuery final : public Td::ResultHandler {
|
2019-03-11 17:06:53 +01:00
|
|
|
Promise<tl_object_ptr<telegram_api::Updates>> promise_;
|
2019-02-21 13:23:05 +01:00
|
|
|
DialogId dialog_id_;
|
|
|
|
|
|
|
|
public:
|
2022-02-03 22:46:10 +01:00
|
|
|
explicit SendVoteQuery(Promise<tl_object_ptr<telegram_api::Updates>> &&promise) : promise_(std::move(promise)) {
|
2019-02-21 13:23:05 +01:00
|
|
|
}
|
|
|
|
|
2023-09-21 18:11:17 +02:00
|
|
|
void send(MessageFullId message_full_id, vector<BufferSlice> &&options, PollId poll_id, uint64 generation,
|
2022-02-05 21:28:43 +01:00
|
|
|
NetQueryRef *query_ref) {
|
2023-09-21 18:11:17 +02:00
|
|
|
dialog_id_ = message_full_id.get_dialog_id();
|
2024-01-03 21:07:50 +01:00
|
|
|
auto input_peer = td_->dialog_manager_->get_input_peer(dialog_id_, AccessRights::Read);
|
2019-02-21 13:23:05 +01:00
|
|
|
if (input_peer == nullptr) {
|
|
|
|
LOG(INFO) << "Can't set poll answer, because have no read access to " << dialog_id_;
|
2021-11-08 12:19:57 +01:00
|
|
|
return on_error(Status::Error(400, "Can't access the chat"));
|
2019-02-21 13:23:05 +01:00
|
|
|
}
|
|
|
|
|
2023-09-21 18:11:17 +02:00
|
|
|
auto message_id = message_full_id.get_message_id().get_server_message_id().get();
|
2019-02-21 13:23:05 +01:00
|
|
|
auto query = G()->net_query_creator().create(
|
2022-02-08 15:30:06 +01:00
|
|
|
telegram_api::messages_sendVote(std::move(input_peer), message_id, std::move(options)),
|
|
|
|
{{poll_id}, {dialog_id_}});
|
2019-02-21 13:42:54 +01:00
|
|
|
*query_ref = query.get_weak();
|
2022-02-01 16:51:20 +01:00
|
|
|
send_query(std::move(query));
|
2019-02-21 13:23:05 +01:00
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_result(BufferSlice packet) final {
|
2019-02-21 13:23:05 +01:00
|
|
|
auto result_ptr = fetch_result<telegram_api::messages_sendVote>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
2021-11-08 12:19:57 +01:00
|
|
|
return on_error(result_ptr.move_as_error());
|
2019-02-21 13:23:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
auto result = result_ptr.move_as_ok();
|
2022-02-07 22:42:53 +01:00
|
|
|
LOG(INFO) << "Receive result for SendVoteQuery: " << to_string(result);
|
2019-03-11 17:06:53 +01:00
|
|
|
promise_.set_value(std::move(result));
|
2019-02-21 13:23:05 +01:00
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_error(Status status) final {
|
2024-01-03 21:07:50 +01:00
|
|
|
td_->dialog_manager_->on_get_dialog_error(dialog_id_, status, "SendVoteQuery");
|
2019-02-22 16:09:55 +01:00
|
|
|
promise_.set_error(std::move(status));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-02-03 22:46:10 +01:00
|
|
|
class StopPollQuery final : public Td::ResultHandler {
|
2019-02-22 16:09:55 +01:00
|
|
|
Promise<Unit> promise_;
|
|
|
|
DialogId dialog_id_;
|
|
|
|
|
|
|
|
public:
|
2022-02-03 22:46:10 +01:00
|
|
|
explicit StopPollQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
|
2019-02-22 16:09:55 +01:00
|
|
|
}
|
|
|
|
|
2023-09-21 18:11:17 +02:00
|
|
|
void send(MessageFullId message_full_id, unique_ptr<ReplyMarkup> &&reply_markup, PollId poll_id) {
|
|
|
|
dialog_id_ = message_full_id.get_dialog_id();
|
2024-01-03 21:07:50 +01:00
|
|
|
auto input_peer = td_->dialog_manager_->get_input_peer(dialog_id_, AccessRights::Edit);
|
2019-02-22 16:09:55 +01:00
|
|
|
if (input_peer == nullptr) {
|
|
|
|
LOG(INFO) << "Can't close poll, because have no edit access to " << dialog_id_;
|
2021-11-08 12:19:57 +01:00
|
|
|
return on_error(Status::Error(400, "Can't access the chat"));
|
2019-02-22 16:09:55 +01:00
|
|
|
}
|
|
|
|
|
2019-04-12 01:10:33 +02:00
|
|
|
int32 flags = telegram_api::messages_editMessage::MEDIA_MASK;
|
2022-06-28 17:34:53 +02:00
|
|
|
auto input_reply_markup = get_input_reply_markup(td_->contacts_manager_.get(), reply_markup);
|
2019-04-12 01:10:33 +02:00
|
|
|
if (input_reply_markup != nullptr) {
|
|
|
|
flags |= telegram_api::messages_editMessage::REPLY_MARKUP_MASK;
|
|
|
|
}
|
|
|
|
|
2023-09-21 18:11:17 +02:00
|
|
|
auto message_id = message_full_id.get_message_id().get_server_message_id().get();
|
2019-02-22 16:09:55 +01:00
|
|
|
auto poll = telegram_api::make_object<telegram_api::poll>();
|
|
|
|
poll->flags_ |= telegram_api::poll::CLOSED_MASK;
|
2020-04-03 15:50:10 +02:00
|
|
|
auto input_media = telegram_api::make_object<telegram_api::inputMediaPoll>(0, std::move(poll),
|
|
|
|
vector<BufferSlice>(), string(), Auto());
|
2022-02-05 21:28:43 +01:00
|
|
|
send_query(G()->net_query_creator().create(
|
2023-09-28 15:13:36 +02:00
|
|
|
telegram_api::messages_editMessage(flags, false /*ignored*/, false /*ignored*/, std::move(input_peer),
|
|
|
|
message_id, string(), std::move(input_media), std::move(input_reply_markup),
|
2022-02-05 21:28:43 +01:00
|
|
|
vector<tl_object_ptr<telegram_api::MessageEntity>>(), 0),
|
2022-02-08 15:30:06 +01:00
|
|
|
{{poll_id}, {dialog_id_}}));
|
2019-02-22 16:09:55 +01:00
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_result(BufferSlice packet) final {
|
2019-02-22 16:09:55 +01:00
|
|
|
auto result_ptr = fetch_result<telegram_api::messages_editMessage>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
2021-11-08 12:19:57 +01:00
|
|
|
return on_error(result_ptr.move_as_error());
|
2019-02-22 16:09:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
auto result = result_ptr.move_as_ok();
|
2022-02-03 22:46:10 +01:00
|
|
|
LOG(INFO) << "Receive result for StopPollQuery: " << to_string(result);
|
2021-11-08 13:20:38 +01:00
|
|
|
td_->updates_manager_->on_get_updates(std::move(result), std::move(promise_));
|
2019-02-22 16:09:55 +01:00
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_error(Status status) final {
|
2021-11-08 13:20:38 +01:00
|
|
|
if (!td_->auth_manager_->is_bot() && status.message() == "MESSAGE_NOT_MODIFIED") {
|
2019-02-22 16:09:55 +01:00
|
|
|
return promise_.set_value(Unit());
|
|
|
|
}
|
2024-01-03 21:07:50 +01:00
|
|
|
td_->dialog_manager_->on_get_dialog_error(dialog_id_, status, "StopPollQuery");
|
2019-02-21 13:23:05 +01:00
|
|
|
promise_.set_error(std::move(status));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-02-19 14:45:32 +01:00
|
|
|
PollManager::PollManager(Td *td, ActorShared<> parent) : td_(td), parent_(std::move(parent)) {
|
2019-02-22 20:53:02 +01:00
|
|
|
update_poll_timeout_.set_callback(on_update_poll_timeout_callback);
|
|
|
|
update_poll_timeout_.set_callback_data(static_cast<void *>(this));
|
2020-04-05 03:31:59 +02:00
|
|
|
|
|
|
|
close_poll_timeout_.set_callback(on_close_poll_timeout_callback);
|
|
|
|
close_poll_timeout_.set_callback_data(static_cast<void *>(this));
|
2022-05-09 00:46:46 +02:00
|
|
|
|
|
|
|
unload_poll_timeout_.set_callback(on_unload_poll_timeout_callback);
|
|
|
|
unload_poll_timeout_.set_callback_data(static_cast<void *>(this));
|
2019-02-22 20:53:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void PollManager::start_up() {
|
2024-02-18 22:23:46 +01:00
|
|
|
if (td_->auth_manager_->is_bot()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class StateCallback final : public StateManager::Callback {
|
2019-02-22 20:53:02 +01:00
|
|
|
public:
|
|
|
|
explicit StateCallback(ActorId<PollManager> parent) : parent_(std::move(parent)) {
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
bool on_online(bool is_online) final {
|
2019-02-22 20:53:02 +01:00
|
|
|
if (is_online) {
|
|
|
|
send_closure(parent_, &PollManager::on_online);
|
|
|
|
}
|
|
|
|
return parent_.is_alive();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
ActorId<PollManager> parent_;
|
|
|
|
};
|
|
|
|
send_closure(G()->state_manager(), &StateManager::add_callback, make_unique<StateCallback>(actor_id(this)));
|
2019-02-19 14:45:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void PollManager::tear_down() {
|
|
|
|
parent_.reset();
|
|
|
|
}
|
|
|
|
|
2022-07-20 12:40:14 +02:00
|
|
|
PollManager::~PollManager() {
|
2022-07-22 20:21:30 +02:00
|
|
|
Scheduler::instance()->destroy_on_scheduler(G()->get_gc_scheduler_id(), polls_, server_poll_messages_,
|
2023-11-06 18:37:17 +01:00
|
|
|
other_poll_messages_, reply_poll_counts_, poll_voters_,
|
|
|
|
loaded_from_database_polls_);
|
2022-07-20 12:40:14 +02:00
|
|
|
}
|
2019-02-19 14:45:32 +01:00
|
|
|
|
2019-02-22 20:53:02 +01:00
|
|
|
void PollManager::on_update_poll_timeout_callback(void *poll_manager_ptr, int64 poll_id_int) {
|
|
|
|
if (G()->close_flag()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto poll_manager = static_cast<PollManager *>(poll_manager_ptr);
|
|
|
|
send_closure_later(poll_manager->actor_id(poll_manager), &PollManager::on_update_poll_timeout, PollId(poll_id_int));
|
|
|
|
}
|
|
|
|
|
2020-04-05 03:31:59 +02:00
|
|
|
void PollManager::on_close_poll_timeout_callback(void *poll_manager_ptr, int64 poll_id_int) {
|
|
|
|
if (G()->close_flag()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto poll_manager = static_cast<PollManager *>(poll_manager_ptr);
|
|
|
|
send_closure_later(poll_manager->actor_id(poll_manager), &PollManager::on_close_poll_timeout, PollId(poll_id_int));
|
|
|
|
}
|
|
|
|
|
2022-05-09 00:46:46 +02:00
|
|
|
void PollManager::on_unload_poll_timeout_callback(void *poll_manager_ptr, int64 poll_id_int) {
|
|
|
|
if (G()->close_flag()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto poll_manager = static_cast<PollManager *>(poll_manager_ptr);
|
|
|
|
send_closure_later(poll_manager->actor_id(poll_manager), &PollManager::on_unload_poll_timeout, PollId(poll_id_int));
|
|
|
|
}
|
|
|
|
|
2019-02-19 14:45:32 +01:00
|
|
|
bool PollManager::is_local_poll_id(PollId poll_id) {
|
|
|
|
return poll_id.get() < 0 && poll_id.get() > std::numeric_limits<int32>::min();
|
|
|
|
}
|
|
|
|
|
|
|
|
const PollManager::Poll *PollManager::get_poll(PollId poll_id) const {
|
2022-11-22 10:23:58 +01:00
|
|
|
return polls_.get_pointer(poll_id);
|
2019-02-19 14:45:32 +01:00
|
|
|
}
|
|
|
|
|
2022-05-09 00:46:46 +02:00
|
|
|
const PollManager::Poll *PollManager::get_poll(PollId poll_id) {
|
2022-11-22 10:23:58 +01:00
|
|
|
auto p = polls_.get_pointer(poll_id);
|
|
|
|
if (p != nullptr) {
|
2022-05-09 00:46:46 +02:00
|
|
|
schedule_poll_unload(poll_id);
|
|
|
|
}
|
2022-11-22 10:23:58 +01:00
|
|
|
return p;
|
2022-05-09 00:46:46 +02:00
|
|
|
}
|
|
|
|
|
2019-02-19 14:45:32 +01:00
|
|
|
PollManager::Poll *PollManager::get_poll_editable(PollId poll_id) {
|
2022-11-22 10:23:58 +01:00
|
|
|
auto p = polls_.get_pointer(poll_id);
|
|
|
|
if (p != nullptr) {
|
2022-05-09 00:46:46 +02:00
|
|
|
schedule_poll_unload(poll_id);
|
2019-02-19 14:45:32 +01:00
|
|
|
}
|
2022-11-22 10:23:58 +01:00
|
|
|
return p;
|
2019-02-19 14:45:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool PollManager::have_poll(PollId poll_id) const {
|
|
|
|
return get_poll(poll_id) != nullptr;
|
|
|
|
}
|
|
|
|
|
2019-02-20 23:54:31 +01:00
|
|
|
void PollManager::notify_on_poll_update(PollId poll_id) {
|
2022-11-22 10:15:58 +01:00
|
|
|
if (td_->auth_manager_->is_bot()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-11-22 10:58:07 +01:00
|
|
|
if (server_poll_messages_.count(poll_id) > 0) {
|
2023-09-21 18:11:17 +02:00
|
|
|
server_poll_messages_[poll_id].foreach([&](const MessageFullId &message_full_id) {
|
|
|
|
td_->messages_manager_->on_external_update_message_content(message_full_id);
|
2022-11-22 10:58:07 +01:00
|
|
|
});
|
2019-02-20 23:54:31 +01:00
|
|
|
}
|
|
|
|
|
2022-11-22 10:58:07 +01:00
|
|
|
if (other_poll_messages_.count(poll_id) > 0) {
|
2023-09-21 18:11:17 +02:00
|
|
|
other_poll_messages_[poll_id].foreach([&](const MessageFullId &message_full_id) {
|
|
|
|
td_->messages_manager_->on_external_update_message_content(message_full_id);
|
2022-11-22 10:58:07 +01:00
|
|
|
});
|
2019-02-20 23:54:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-19 14:45:32 +01:00
|
|
|
string PollManager::get_poll_database_key(PollId poll_id) {
|
|
|
|
return PSTRING() << "poll" << poll_id.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PollManager::save_poll(const Poll *poll, PollId poll_id) {
|
2019-02-20 23:54:31 +01:00
|
|
|
CHECK(!is_local_poll_id(poll_id));
|
2023-04-25 14:29:24 +02:00
|
|
|
poll->was_saved_ = true;
|
2019-02-20 23:54:31 +01:00
|
|
|
|
2023-03-13 17:47:38 +01:00
|
|
|
if (!G()->use_message_database()) {
|
2019-02-19 14:45:32 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(INFO) << "Save " << poll_id << " to database";
|
|
|
|
CHECK(poll != nullptr);
|
2019-02-20 23:54:31 +01:00
|
|
|
G()->td_db()->get_sqlite_pmc()->set(get_poll_database_key(poll_id), log_event_store(*poll).as_slice().str(), Auto());
|
2019-02-19 14:45:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void PollManager::on_load_poll_from_database(PollId poll_id, string value) {
|
2022-03-11 19:38:48 +01:00
|
|
|
CHECK(poll_id.is_valid());
|
2019-02-19 14:45:32 +01:00
|
|
|
loaded_from_database_polls_.insert(poll_id);
|
|
|
|
|
|
|
|
LOG(INFO) << "Successfully loaded " << poll_id << " of size " << value.size() << " from database";
|
|
|
|
// G()->td_db()->get_sqlite_pmc()->erase(get_poll_database_key(poll_id), Auto());
|
|
|
|
// return;
|
|
|
|
|
|
|
|
CHECK(!have_poll(poll_id));
|
|
|
|
if (!value.empty()) {
|
2020-04-05 04:35:52 +02:00
|
|
|
auto poll = make_unique<Poll>();
|
2023-03-14 21:24:19 +01:00
|
|
|
if (log_event_parse(*poll, value).is_error()) {
|
|
|
|
return;
|
2019-02-19 14:45:32 +01:00
|
|
|
}
|
2023-04-25 15:41:04 +02:00
|
|
|
for (const auto &recent_voter_min_channel : poll->recent_voter_min_channels_) {
|
|
|
|
LOG(INFO) << "Add min voted " << recent_voter_min_channel.first;
|
|
|
|
td_->contacts_manager_->add_min_channel(recent_voter_min_channel.first, recent_voter_min_channel.second);
|
|
|
|
}
|
|
|
|
Dependencies dependencies;
|
|
|
|
for (auto dialog_id : poll->recent_voter_dialog_ids_) {
|
|
|
|
dependencies.add_message_sender_dependencies(dialog_id);
|
|
|
|
}
|
|
|
|
if (!dependencies.resolve_force(td_, "on_load_poll_from_database")) {
|
|
|
|
poll->recent_voter_dialog_ids_.clear();
|
|
|
|
poll->recent_voter_min_channels_.clear();
|
2020-01-12 02:40:17 +01:00
|
|
|
}
|
2023-04-25 14:29:24 +02:00
|
|
|
if (!poll->is_closed_ && poll->close_date_ != 0) {
|
|
|
|
if (poll->close_date_ <= G()->server_time()) {
|
|
|
|
poll->is_closed_ = true;
|
2020-04-05 03:31:59 +02:00
|
|
|
} else {
|
|
|
|
CHECK(!is_local_poll_id(poll_id));
|
2023-03-14 21:20:05 +01:00
|
|
|
if (!G()->close_flag()) {
|
2023-04-25 14:29:24 +02:00
|
|
|
close_poll_timeout_.set_timeout_in(poll_id.get(), poll->close_date_ - G()->server_time() + 1e-3);
|
2023-03-14 21:20:05 +01:00
|
|
|
}
|
2020-04-05 03:31:59 +02:00
|
|
|
}
|
|
|
|
}
|
2020-04-05 04:35:52 +02:00
|
|
|
polls_[poll_id] = std::move(poll);
|
2019-02-19 14:45:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PollManager::have_poll_force(PollId poll_id) {
|
|
|
|
return get_poll_force(poll_id) != nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
PollManager::Poll *PollManager::get_poll_force(PollId poll_id) {
|
|
|
|
auto poll = get_poll_editable(poll_id);
|
|
|
|
if (poll != nullptr) {
|
|
|
|
return poll;
|
|
|
|
}
|
2023-03-13 17:47:38 +01:00
|
|
|
if (!G()->use_message_database()) {
|
2019-02-19 14:45:32 +01:00
|
|
|
return nullptr;
|
|
|
|
}
|
2022-02-09 22:59:52 +01:00
|
|
|
if (!poll_id.is_valid() || loaded_from_database_polls_.count(poll_id)) {
|
2019-02-19 14:45:32 +01:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(INFO) << "Trying to load " << poll_id << " from database";
|
|
|
|
on_load_poll_from_database(poll_id, G()->td_db()->get_sqlite_sync_pmc()->get(get_poll_database_key(poll_id)));
|
|
|
|
return get_poll_editable(poll_id);
|
|
|
|
}
|
|
|
|
|
2019-02-21 01:52:58 +01:00
|
|
|
td_api::object_ptr<td_api::pollOption> PollManager::get_poll_option_object(const PollOption &poll_option) {
|
2023-04-25 14:16:29 +02:00
|
|
|
return td_api::make_object<td_api::pollOption>(poll_option.text_, poll_option.voter_count_, 0, poll_option.is_chosen_,
|
2019-03-10 12:03:19 +01:00
|
|
|
false);
|
2019-03-05 21:21:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
vector<int32> PollManager::get_vote_percentage(const vector<int32> &voter_counts, int32 total_voter_count) {
|
|
|
|
int32 sum = 0;
|
|
|
|
for (auto voter_count : voter_counts) {
|
|
|
|
CHECK(0 <= voter_count);
|
|
|
|
CHECK(voter_count <= std::numeric_limits<int32>::max() - sum);
|
|
|
|
sum += voter_count;
|
|
|
|
}
|
2019-03-05 22:12:04 +01:00
|
|
|
if (total_voter_count > sum) {
|
|
|
|
if (sum != 0) {
|
|
|
|
LOG(ERROR) << "Have total_voter_count = " << total_voter_count << ", but votes sum = " << sum << ": "
|
|
|
|
<< voter_counts;
|
|
|
|
}
|
|
|
|
total_voter_count = sum;
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<int32> result(voter_counts.size(), 0);
|
|
|
|
if (total_voter_count == 0) {
|
|
|
|
return result;
|
|
|
|
}
|
2019-03-05 21:21:10 +01:00
|
|
|
if (total_voter_count != sum) {
|
|
|
|
// just round to the nearest
|
|
|
|
for (size_t i = 0; i < result.size(); i++) {
|
|
|
|
result[i] =
|
2019-03-17 21:43:58 +01:00
|
|
|
static_cast<int32>((static_cast<int64>(voter_counts[i]) * 200 + total_voter_count) / total_voter_count / 2);
|
2019-03-05 21:21:10 +01:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure that options with equal votes have equal percent and total sum is less than 100%
|
|
|
|
int32 percent_sum = 0;
|
|
|
|
vector<int32> gap(voter_counts.size(), 0);
|
|
|
|
for (size_t i = 0; i < result.size(); i++) {
|
2019-03-17 21:43:58 +01:00
|
|
|
auto multiplied_voter_count = static_cast<int64>(voter_counts[i]) * 100;
|
2019-03-05 21:21:10 +01:00
|
|
|
result[i] = static_cast<int32>(multiplied_voter_count / total_voter_count);
|
|
|
|
CHECK(0 <= result[i] && result[i] <= 100);
|
2019-03-17 21:43:58 +01:00
|
|
|
gap[i] = static_cast<int32>(static_cast<int64>(result[i] + 1) * total_voter_count - multiplied_voter_count);
|
2019-03-05 21:21:10 +01:00
|
|
|
CHECK(0 <= gap[i] && gap[i] <= total_voter_count);
|
|
|
|
percent_sum += result[i];
|
|
|
|
}
|
|
|
|
CHECK(0 <= percent_sum && percent_sum <= 100);
|
|
|
|
if (percent_sum == 100) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-01-06 19:44:09 +01:00
|
|
|
// now we need to choose up to (100 - percent_sum) options with a minimum total gap, such that
|
2019-03-05 21:21:10 +01:00
|
|
|
// any two options with the same voter_count are chosen or not chosen simultaneously
|
|
|
|
struct Option {
|
|
|
|
int32 pos = -1;
|
|
|
|
int32 count = 0;
|
|
|
|
};
|
2022-02-07 20:41:07 +01:00
|
|
|
FlatHashMap<int32, Option> options;
|
2019-03-05 21:21:10 +01:00
|
|
|
for (size_t i = 0; i < result.size(); i++) {
|
2022-02-09 22:59:52 +01:00
|
|
|
auto &option = options[voter_counts[i] + 1];
|
2022-01-11 19:52:29 +01:00
|
|
|
if (option.pos == -1) {
|
|
|
|
option.pos = narrow_cast<int32>(i);
|
|
|
|
}
|
2019-03-05 21:21:10 +01:00
|
|
|
option.count++;
|
|
|
|
}
|
|
|
|
vector<Option> sorted_options;
|
2021-11-10 18:39:35 +01:00
|
|
|
for (const auto &it : options) {
|
|
|
|
const auto &option = it.second;
|
|
|
|
auto pos = option.pos;
|
2019-03-05 21:21:10 +01:00
|
|
|
if (gap[pos] > total_voter_count / 2) {
|
|
|
|
// do not round to wrong direction
|
|
|
|
continue;
|
|
|
|
}
|
2019-03-10 19:49:10 +01:00
|
|
|
if (total_voter_count % 2 == 0 && gap[pos] == total_voter_count / 2 && result[pos] >= 50) {
|
2019-03-05 21:21:10 +01:00
|
|
|
// round halves to the 50%
|
|
|
|
continue;
|
|
|
|
}
|
2021-11-10 18:39:35 +01:00
|
|
|
sorted_options.push_back(option);
|
2019-03-05 21:21:10 +01:00
|
|
|
}
|
|
|
|
std::sort(sorted_options.begin(), sorted_options.end(), [&](const Option &lhs, const Option &rhs) {
|
|
|
|
if (gap[lhs.pos] != gap[rhs.pos]) {
|
|
|
|
// prefer options with smallest gap
|
|
|
|
return gap[lhs.pos] < gap[rhs.pos];
|
|
|
|
}
|
2022-01-11 19:52:29 +01:00
|
|
|
if (lhs.count != rhs.count) {
|
|
|
|
// prefer more popular options
|
|
|
|
return lhs.count > rhs.count;
|
|
|
|
}
|
|
|
|
return lhs.pos < rhs.pos; // prefer the first encountered option
|
2019-03-05 21:21:10 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
// dynamic programming or brute force can give perfect result, but for now we use simple gready approach
|
|
|
|
int32 left_percent = 100 - percent_sum;
|
|
|
|
for (auto option : sorted_options) {
|
|
|
|
if (option.count <= left_percent) {
|
|
|
|
left_percent -= option.count;
|
|
|
|
|
|
|
|
auto pos = option.pos;
|
|
|
|
for (size_t i = 0; i < result.size(); i++) {
|
|
|
|
if (voter_counts[i] == voter_counts[pos]) {
|
|
|
|
result[i]++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (left_percent == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
2019-02-19 14:45:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
td_api::object_ptr<td_api::poll> PollManager::get_poll_object(PollId poll_id) const {
|
|
|
|
auto poll = get_poll(poll_id);
|
|
|
|
CHECK(poll != nullptr);
|
2019-03-29 03:16:28 +01:00
|
|
|
return get_poll_object(poll_id, poll);
|
|
|
|
}
|
|
|
|
|
|
|
|
td_api::object_ptr<td_api::poll> PollManager::get_poll_object(PollId poll_id, const Poll *poll) const {
|
2019-02-21 13:23:05 +01:00
|
|
|
vector<td_api::object_ptr<td_api::pollOption>> poll_options;
|
|
|
|
auto it = pending_answers_.find(poll_id);
|
|
|
|
int32 voter_count_diff = 0;
|
2023-08-19 05:42:06 +02:00
|
|
|
if (it == pending_answers_.end() || (it->second.is_finished_ && poll->was_saved_)) {
|
2023-04-25 14:29:24 +02:00
|
|
|
poll_options = transform(poll->options_, get_poll_option_object);
|
2019-02-21 13:23:05 +01:00
|
|
|
} else {
|
2023-08-19 05:42:06 +02:00
|
|
|
const auto &chosen_options = it->second.options_;
|
|
|
|
LOG(INFO) << "Have pending chosen options " << chosen_options << " in " << poll_id;
|
2023-04-25 14:29:24 +02:00
|
|
|
for (auto &poll_option : poll->options_) {
|
2023-04-25 14:16:29 +02:00
|
|
|
auto is_being_chosen = td::contains(chosen_options, poll_option.data_);
|
|
|
|
if (poll_option.is_chosen_) {
|
2019-02-21 13:23:05 +01:00
|
|
|
voter_count_diff = -1;
|
|
|
|
}
|
2019-02-21 13:42:54 +01:00
|
|
|
poll_options.push_back(td_api::make_object<td_api::pollOption>(
|
2023-04-25 14:16:29 +02:00
|
|
|
poll_option.text_, poll_option.voter_count_ - static_cast<int32>(poll_option.is_chosen_), 0, false,
|
2019-03-10 12:03:19 +01:00
|
|
|
is_being_chosen));
|
2019-02-21 13:23:05 +01:00
|
|
|
}
|
|
|
|
}
|
2019-03-05 22:12:04 +01:00
|
|
|
|
2023-04-25 14:29:24 +02:00
|
|
|
auto total_voter_count = poll->total_voter_count_ + voter_count_diff;
|
2019-03-05 22:12:04 +01:00
|
|
|
bool is_voted = false;
|
|
|
|
for (auto &poll_option : poll_options) {
|
|
|
|
is_voted |= poll_option->is_chosen_;
|
|
|
|
}
|
2023-04-25 14:29:24 +02:00
|
|
|
if (!is_voted && !poll->is_closed_ && !td_->auth_manager_->is_bot()) {
|
2019-03-05 22:12:04 +01:00
|
|
|
// hide the voter counts
|
|
|
|
for (auto &poll_option : poll_options) {
|
|
|
|
poll_option->voter_count_ = 0;
|
|
|
|
}
|
2019-04-26 21:44:47 +02:00
|
|
|
} else {
|
|
|
|
// calculate vote percentage and fix total_voter_count
|
|
|
|
auto voter_counts = transform(poll_options, [](auto &poll_option) { return poll_option->voter_count_; });
|
|
|
|
auto voter_count_sum = 0;
|
|
|
|
for (auto voter_count : voter_counts) {
|
|
|
|
if (total_voter_count < voter_count) {
|
2023-04-25 14:29:24 +02:00
|
|
|
LOG(ERROR) << "Fix total voter count from " << poll->total_voter_count_ << " + " << voter_count_diff << " to "
|
2019-04-26 21:44:47 +02:00
|
|
|
<< voter_count << " in " << poll_id;
|
|
|
|
total_voter_count = voter_count;
|
|
|
|
}
|
|
|
|
voter_count_sum += voter_count;
|
|
|
|
}
|
2019-04-26 21:56:52 +02:00
|
|
|
if (voter_count_sum < total_voter_count && voter_count_sum != 0) {
|
2023-04-25 14:29:24 +02:00
|
|
|
LOG(ERROR) << "Fix total voter count from " << poll->total_voter_count_ << " + " << voter_count_diff << " to "
|
2019-04-26 21:44:47 +02:00
|
|
|
<< voter_count_sum << " in " << poll_id;
|
|
|
|
total_voter_count = voter_count_sum;
|
2019-03-05 21:21:10 +01:00
|
|
|
}
|
2019-03-05 22:12:04 +01:00
|
|
|
|
2019-04-26 21:44:47 +02:00
|
|
|
auto vote_percentage = get_vote_percentage(voter_counts, total_voter_count);
|
|
|
|
CHECK(poll_options.size() == vote_percentage.size());
|
|
|
|
for (size_t i = 0; i < poll_options.size(); i++) {
|
|
|
|
poll_options[i]->vote_percentage_ = vote_percentage[i];
|
|
|
|
}
|
2019-03-05 21:21:10 +01:00
|
|
|
}
|
2020-01-11 01:46:26 +01:00
|
|
|
td_api::object_ptr<td_api::PollType> poll_type;
|
2023-04-25 14:29:24 +02:00
|
|
|
if (poll->is_quiz_) {
|
|
|
|
auto correct_option_id = is_local_poll_id(poll_id) ? -1 : poll->correct_option_id_;
|
2020-04-03 22:39:50 +02:00
|
|
|
poll_type = td_api::make_object<td_api::pollTypeQuiz>(
|
2021-07-22 04:39:16 +02:00
|
|
|
correct_option_id,
|
2023-04-25 14:29:24 +02:00
|
|
|
get_formatted_text_object(is_local_poll_id(poll_id) ? FormattedText() : poll->explanation_, true, -1));
|
2020-01-11 01:46:26 +01:00
|
|
|
} else {
|
2023-04-25 14:29:24 +02:00
|
|
|
poll_type = td_api::make_object<td_api::pollTypeRegular>(poll->allow_multiple_answers_);
|
2020-01-11 01:46:26 +01:00
|
|
|
}
|
|
|
|
|
2023-04-25 14:29:24 +02:00
|
|
|
auto open_period = poll->open_period_;
|
|
|
|
auto close_date = poll->close_date_;
|
2020-04-03 23:05:35 +02:00
|
|
|
if (open_period != 0 && close_date == 0) {
|
|
|
|
close_date = G()->unix_time() + open_period;
|
2020-04-03 15:50:10 +02:00
|
|
|
}
|
2020-04-03 23:05:35 +02:00
|
|
|
if (open_period == 0 && close_date != 0) {
|
2020-04-03 15:50:10 +02:00
|
|
|
auto now = G()->unix_time();
|
|
|
|
if (close_date < now + 5) {
|
|
|
|
close_date = 0;
|
|
|
|
} else {
|
2020-04-03 23:05:35 +02:00
|
|
|
open_period = close_date - now;
|
2020-04-03 15:50:10 +02:00
|
|
|
}
|
|
|
|
}
|
2023-04-25 14:29:24 +02:00
|
|
|
if (poll->is_closed_) {
|
2021-09-05 22:11:25 +02:00
|
|
|
open_period = 0;
|
|
|
|
close_date = 0;
|
|
|
|
}
|
2023-04-25 15:41:04 +02:00
|
|
|
|
|
|
|
vector<td_api::object_ptr<td_api::MessageSender>> recent_voters;
|
|
|
|
for (auto dialog_id : poll->recent_voter_dialog_ids_) {
|
|
|
|
auto recent_voter = get_min_message_sender_object(td_, dialog_id, "get_poll_object");
|
|
|
|
if (recent_voter != nullptr) {
|
|
|
|
recent_voters.push_back(std::move(recent_voter));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return td_api::make_object<td_api::poll>(poll_id.get(), poll->question_, std::move(poll_options), total_voter_count,
|
|
|
|
std::move(recent_voters), poll->is_anonymous_, std::move(poll_type),
|
|
|
|
open_period, close_date, poll->is_closed_);
|
2019-02-19 14:45:32 +01:00
|
|
|
}
|
|
|
|
|
2019-02-21 01:52:58 +01:00
|
|
|
telegram_api::object_ptr<telegram_api::pollAnswer> PollManager::get_input_poll_option(const PollOption &poll_option) {
|
2023-04-25 14:16:29 +02:00
|
|
|
return telegram_api::make_object<telegram_api::pollAnswer>(poll_option.text_, BufferSlice(poll_option.data_));
|
2019-02-19 14:45:32 +01:00
|
|
|
}
|
|
|
|
|
2020-01-11 01:46:26 +01:00
|
|
|
PollId PollManager::create_poll(string &&question, vector<string> &&options, bool is_anonymous,
|
2020-04-03 22:39:50 +02:00
|
|
|
bool allow_multiple_answers, bool is_quiz, int32 correct_option_id,
|
2020-04-03 23:05:35 +02:00
|
|
|
FormattedText &&explanation, int32 open_period, int32 close_date, bool is_closed) {
|
2019-02-19 14:45:32 +01:00
|
|
|
auto poll = make_unique<Poll>();
|
2023-04-25 14:29:24 +02:00
|
|
|
poll->question_ = std::move(question);
|
2019-03-12 11:06:50 +01:00
|
|
|
int pos = '0';
|
2019-02-21 01:52:58 +01:00
|
|
|
for (auto &option_text : options) {
|
|
|
|
PollOption option;
|
2023-04-25 14:16:29 +02:00
|
|
|
option.text_ = std::move(option_text);
|
|
|
|
option.data_ = string(1, narrow_cast<char>(pos++));
|
2023-04-25 14:29:24 +02:00
|
|
|
poll->options_.push_back(std::move(option));
|
2019-02-19 14:45:32 +01:00
|
|
|
}
|
2023-04-25 14:29:24 +02:00
|
|
|
poll->is_anonymous_ = is_anonymous;
|
|
|
|
poll->allow_multiple_answers_ = allow_multiple_answers;
|
|
|
|
poll->is_quiz_ = is_quiz;
|
|
|
|
poll->correct_option_id_ = correct_option_id;
|
|
|
|
poll->explanation_ = std::move(explanation);
|
|
|
|
poll->open_period_ = open_period;
|
|
|
|
poll->close_date_ = close_date;
|
|
|
|
poll->is_closed_ = is_closed;
|
2019-02-19 14:45:32 +01:00
|
|
|
|
|
|
|
PollId poll_id(--current_local_poll_id_);
|
|
|
|
CHECK(is_local_poll_id(poll_id));
|
2022-11-22 10:23:58 +01:00
|
|
|
polls_.set(poll_id, std::move(poll));
|
2019-02-19 14:45:32 +01:00
|
|
|
return poll_id;
|
|
|
|
}
|
|
|
|
|
2023-09-21 18:11:17 +02:00
|
|
|
void PollManager::register_poll(PollId poll_id, MessageFullId message_full_id, const char *source) {
|
2019-02-19 16:42:59 +01:00
|
|
|
CHECK(have_poll(poll_id));
|
2023-09-21 18:11:17 +02:00
|
|
|
if (message_full_id.get_message_id().is_scheduled() || !message_full_id.get_message_id().is_server()) {
|
|
|
|
other_poll_messages_[poll_id].insert(message_full_id);
|
2023-03-14 21:20:05 +01:00
|
|
|
if (!G()->close_flag()) {
|
|
|
|
unload_poll_timeout_.cancel_timeout(poll_id.get());
|
|
|
|
}
|
2019-03-11 13:27:28 +01:00
|
|
|
return;
|
|
|
|
}
|
2023-09-21 18:11:17 +02:00
|
|
|
LOG(INFO) << "Register " << poll_id << " from " << message_full_id << " from " << source;
|
|
|
|
server_poll_messages_[poll_id].insert(message_full_id);
|
2020-04-05 03:31:59 +02:00
|
|
|
auto poll = get_poll(poll_id);
|
|
|
|
CHECK(poll != nullptr);
|
2020-04-05 04:35:52 +02:00
|
|
|
if (!td_->auth_manager_->is_bot() && !is_local_poll_id(poll_id) &&
|
2023-04-25 14:29:24 +02:00
|
|
|
!(poll->is_closed_ && poll->is_updated_after_close_) && !G()->close_flag()) {
|
2019-02-22 20:53:02 +01:00
|
|
|
update_poll_timeout_.add_timeout_in(poll_id.get(), 0);
|
|
|
|
}
|
2023-03-14 21:20:05 +01:00
|
|
|
if (!G()->close_flag()) {
|
|
|
|
unload_poll_timeout_.cancel_timeout(poll_id.get());
|
|
|
|
}
|
2019-02-19 16:42:59 +01:00
|
|
|
}
|
|
|
|
|
2023-09-21 18:11:17 +02:00
|
|
|
void PollManager::unregister_poll(PollId poll_id, MessageFullId message_full_id, const char *source) {
|
2019-02-19 16:42:59 +01:00
|
|
|
CHECK(have_poll(poll_id));
|
2023-09-21 18:11:17 +02:00
|
|
|
if (message_full_id.get_message_id().is_scheduled() || !message_full_id.get_message_id().is_server()) {
|
2022-05-09 00:58:17 +02:00
|
|
|
auto &message_ids = other_poll_messages_[poll_id];
|
2023-09-21 18:11:17 +02:00
|
|
|
auto is_deleted = message_ids.erase(message_full_id) > 0;
|
|
|
|
LOG_CHECK(is_deleted) << source << ' ' << poll_id << ' ' << message_full_id;
|
2022-06-20 15:53:28 +02:00
|
|
|
if (is_local_poll_id(poll_id)) {
|
|
|
|
CHECK(message_ids.empty());
|
|
|
|
forget_local_poll(poll_id);
|
|
|
|
}
|
2022-05-09 00:58:17 +02:00
|
|
|
if (message_ids.empty()) {
|
|
|
|
other_poll_messages_.erase(poll_id);
|
|
|
|
|
|
|
|
schedule_poll_unload(poll_id);
|
|
|
|
}
|
2019-03-11 13:27:28 +01:00
|
|
|
return;
|
|
|
|
}
|
2023-09-21 18:11:17 +02:00
|
|
|
LOG(INFO) << "Unregister " << poll_id << " from " << message_full_id << " from " << source;
|
2022-05-09 00:58:17 +02:00
|
|
|
auto &message_ids = server_poll_messages_[poll_id];
|
2023-09-21 18:11:17 +02:00
|
|
|
auto is_deleted = message_ids.erase(message_full_id) > 0;
|
|
|
|
LOG_CHECK(is_deleted) << source << ' ' << poll_id << ' ' << message_full_id;
|
2022-06-20 15:53:28 +02:00
|
|
|
if (is_local_poll_id(poll_id)) {
|
|
|
|
CHECK(message_ids.empty());
|
|
|
|
forget_local_poll(poll_id);
|
|
|
|
}
|
2019-02-22 20:53:02 +01:00
|
|
|
if (message_ids.empty()) {
|
2022-05-09 00:58:17 +02:00
|
|
|
server_poll_messages_.erase(poll_id);
|
2023-03-14 21:20:05 +01:00
|
|
|
if (!G()->close_flag()) {
|
2023-03-16 23:02:30 +01:00
|
|
|
update_poll_timeout_.cancel_timeout(poll_id.get(), "unregister_poll");
|
2023-03-14 21:20:05 +01:00
|
|
|
}
|
2022-05-09 00:46:46 +02:00
|
|
|
|
|
|
|
schedule_poll_unload(poll_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-06 18:37:17 +01:00
|
|
|
void PollManager::register_reply_poll(PollId poll_id) {
|
|
|
|
CHECK(have_poll(poll_id));
|
|
|
|
CHECK(!is_local_poll_id(poll_id));
|
|
|
|
LOG(INFO) << "Register replied " << poll_id;
|
|
|
|
reply_poll_counts_[poll_id]++;
|
|
|
|
if (!G()->close_flag()) {
|
|
|
|
unload_poll_timeout_.cancel_timeout(poll_id.get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PollManager::unregister_reply_poll(PollId poll_id) {
|
|
|
|
CHECK(have_poll(poll_id));
|
|
|
|
CHECK(!is_local_poll_id(poll_id));
|
|
|
|
LOG(INFO) << "Unregister replied " << poll_id;
|
|
|
|
auto &count = reply_poll_counts_[poll_id];
|
|
|
|
CHECK(count > 0);
|
|
|
|
count--;
|
|
|
|
if (count == 0) {
|
|
|
|
reply_poll_counts_.erase(poll_id);
|
|
|
|
schedule_poll_unload(poll_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-09 00:46:46 +02:00
|
|
|
bool PollManager::can_unload_poll(PollId poll_id) {
|
2023-03-24 12:14:37 +01:00
|
|
|
if (G()->close_flag()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (is_local_poll_id(poll_id) || server_poll_messages_.count(poll_id) != 0 ||
|
2023-11-06 18:37:17 +01:00
|
|
|
other_poll_messages_.count(poll_id) != 0 || reply_poll_counts_.count(poll_id) != 0 ||
|
|
|
|
pending_answers_.count(poll_id) != 0 || being_closed_polls_.count(poll_id) != 0) {
|
2022-05-09 00:46:46 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto it = poll_voters_.find(poll_id);
|
|
|
|
if (it != poll_voters_.end() && !it->second.empty()) {
|
|
|
|
for (auto &voters : it->second) {
|
2023-04-25 14:16:29 +02:00
|
|
|
if (!voters.pending_queries_.empty()) {
|
2022-05-09 00:46:46 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PollManager::schedule_poll_unload(PollId poll_id) {
|
|
|
|
if (can_unload_poll(poll_id)) {
|
|
|
|
unload_poll_timeout_.set_timeout_in(poll_id.get(), UNLOAD_POLL_DELAY);
|
2019-02-22 20:53:02 +01:00
|
|
|
}
|
2019-02-19 16:42:59 +01:00
|
|
|
}
|
|
|
|
|
2019-02-22 16:09:55 +01:00
|
|
|
bool PollManager::get_poll_is_closed(PollId poll_id) const {
|
|
|
|
auto poll = get_poll(poll_id);
|
|
|
|
CHECK(poll != nullptr);
|
2023-04-25 14:29:24 +02:00
|
|
|
return poll->is_closed_;
|
2019-02-22 16:09:55 +01:00
|
|
|
}
|
|
|
|
|
2020-01-11 01:46:26 +01:00
|
|
|
bool PollManager::get_poll_is_anonymous(PollId poll_id) const {
|
|
|
|
auto poll = get_poll(poll_id);
|
|
|
|
CHECK(poll != nullptr);
|
2023-04-25 14:29:24 +02:00
|
|
|
return poll->is_anonymous_;
|
2020-01-11 01:46:26 +01:00
|
|
|
}
|
|
|
|
|
2019-03-03 04:36:30 +01:00
|
|
|
string PollManager::get_poll_search_text(PollId poll_id) const {
|
|
|
|
auto poll = get_poll(poll_id);
|
|
|
|
CHECK(poll != nullptr);
|
|
|
|
|
2023-04-25 14:29:24 +02:00
|
|
|
string result = poll->question_;
|
|
|
|
for (auto &option : poll->options_) {
|
2019-03-03 04:36:30 +01:00
|
|
|
result += ' ';
|
2023-04-25 14:16:29 +02:00
|
|
|
result += option.text_;
|
2019-03-03 04:36:30 +01:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-09-21 18:11:17 +02:00
|
|
|
void PollManager::set_poll_answer(PollId poll_id, MessageFullId message_full_id, vector<int32> &&option_ids,
|
2019-02-21 13:23:05 +01:00
|
|
|
Promise<Unit> &&promise) {
|
2020-12-30 16:50:57 +01:00
|
|
|
td::unique(option_ids);
|
2020-01-11 01:46:26 +01:00
|
|
|
|
2019-02-21 13:23:05 +01:00
|
|
|
if (is_local_poll_id(poll_id)) {
|
2020-01-11 01:46:26 +01:00
|
|
|
return promise.set_error(Status::Error(400, "Poll can't be answered"));
|
2019-02-21 13:23:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
auto poll = get_poll(poll_id);
|
|
|
|
CHECK(poll != nullptr);
|
2023-04-25 14:29:24 +02:00
|
|
|
if (poll->is_closed_) {
|
2019-02-21 13:23:05 +01:00
|
|
|
return promise.set_error(Status::Error(400, "Can't answer closed poll"));
|
|
|
|
}
|
2023-04-25 14:29:24 +02:00
|
|
|
if (!poll->allow_multiple_answers_ && option_ids.size() > 1) {
|
2020-01-11 01:46:26 +01:00
|
|
|
return promise.set_error(Status::Error(400, "Can't choose more than 1 option in the poll"));
|
|
|
|
}
|
2023-04-25 14:29:24 +02:00
|
|
|
if (poll->is_quiz_ && option_ids.empty()) {
|
2020-01-11 01:46:26 +01:00
|
|
|
return promise.set_error(Status::Error(400, "Can't retract vote in a quiz"));
|
|
|
|
}
|
2023-04-25 14:29:24 +02:00
|
|
|
if (poll->is_quiz_ && pending_answers_.count(poll_id) != 0) {
|
2020-04-29 15:35:53 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Can't revote in a quiz"));
|
|
|
|
}
|
2020-01-11 01:46:26 +01:00
|
|
|
|
2022-11-24 13:25:15 +01:00
|
|
|
FlatHashMap<uint64, int> affected_option_ids;
|
2019-02-21 13:23:05 +01:00
|
|
|
vector<string> options;
|
|
|
|
for (auto &option_id : option_ids) {
|
|
|
|
auto index = static_cast<size_t>(option_id);
|
2023-04-25 14:29:24 +02:00
|
|
|
if (index >= poll->options_.size()) {
|
2020-01-11 01:46:26 +01:00
|
|
|
return promise.set_error(Status::Error(400, "Invalid option ID specified"));
|
2019-02-21 13:23:05 +01:00
|
|
|
}
|
2023-04-25 14:29:24 +02:00
|
|
|
options.push_back(poll->options_[index].data_);
|
2020-01-13 19:21:58 +01:00
|
|
|
|
2022-02-09 22:59:52 +01:00
|
|
|
affected_option_ids[index + 1]++;
|
2020-01-13 19:21:58 +01:00
|
|
|
}
|
2023-04-25 14:29:24 +02:00
|
|
|
for (size_t option_index = 0; option_index < poll->options_.size(); option_index++) {
|
|
|
|
if (poll->options_[option_index].is_chosen_) {
|
|
|
|
if (poll->is_quiz_) {
|
2020-04-29 15:35:53 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Can't revote in a quiz"));
|
|
|
|
}
|
2022-02-09 22:59:52 +01:00
|
|
|
affected_option_ids[option_index + 1]++;
|
2020-01-13 19:21:58 +01:00
|
|
|
}
|
|
|
|
}
|
2022-02-07 20:41:07 +01:00
|
|
|
for (const auto &it : affected_option_ids) {
|
2020-01-13 19:21:58 +01:00
|
|
|
if (it.second == 1) {
|
2022-11-24 13:25:15 +01:00
|
|
|
invalidate_poll_option_voters(poll, poll_id, static_cast<size_t>(it.first - 1));
|
2020-01-13 19:21:58 +01:00
|
|
|
}
|
2019-02-21 13:23:05 +01:00
|
|
|
}
|
|
|
|
|
2023-09-21 18:11:17 +02:00
|
|
|
do_set_poll_answer(poll_id, message_full_id, std::move(options), 0, std::move(promise));
|
2019-02-21 13:23:05 +01:00
|
|
|
}
|
|
|
|
|
2019-02-21 15:40:37 +01:00
|
|
|
class PollManager::SetPollAnswerLogEvent {
|
|
|
|
public:
|
|
|
|
PollId poll_id_;
|
2023-09-21 18:11:17 +02:00
|
|
|
MessageFullId message_full_id_;
|
2019-02-21 15:40:37 +01:00
|
|
|
vector<string> options_;
|
|
|
|
|
|
|
|
template <class StorerT>
|
|
|
|
void store(StorerT &storer) const {
|
|
|
|
td::store(poll_id_, storer);
|
2023-09-21 18:11:17 +02:00
|
|
|
td::store(message_full_id_, storer);
|
2019-02-21 15:40:37 +01:00
|
|
|
td::store(options_, storer);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ParserT>
|
|
|
|
void parse(ParserT &parser) {
|
|
|
|
td::parse(poll_id_, parser);
|
2023-09-21 18:11:17 +02:00
|
|
|
td::parse(message_full_id_, parser);
|
2019-02-21 15:40:37 +01:00
|
|
|
td::parse(options_, parser);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-09-21 18:11:17 +02:00
|
|
|
void PollManager::do_set_poll_answer(PollId poll_id, MessageFullId message_full_id, vector<string> &&options,
|
2020-09-22 01:15:09 +02:00
|
|
|
uint64 log_event_id, Promise<Unit> &&promise) {
|
2023-09-21 18:11:17 +02:00
|
|
|
LOG(INFO) << "Set answer in " << poll_id << " from " << message_full_id;
|
|
|
|
if (!poll_id.is_valid() || !message_full_id.get_dialog_id().is_valid() ||
|
|
|
|
!message_full_id.get_message_id().is_valid()) {
|
2022-02-09 22:59:52 +01:00
|
|
|
CHECK(log_event_id != 0);
|
|
|
|
LOG(ERROR) << "Invalid SetPollAnswer log event";
|
|
|
|
binlog_erase(G()->td_db()->get_binlog(), log_event_id);
|
|
|
|
return;
|
|
|
|
}
|
2023-03-14 21:20:05 +01:00
|
|
|
if (!G()->close_flag()) {
|
|
|
|
unload_poll_timeout_.cancel_timeout(poll_id.get());
|
|
|
|
}
|
2022-02-09 22:59:52 +01:00
|
|
|
|
2019-02-21 13:23:05 +01:00
|
|
|
auto &pending_answer = pending_answers_[poll_id];
|
|
|
|
if (!pending_answer.promises_.empty() && pending_answer.options_ == options) {
|
|
|
|
pending_answer.promises_.push_back(std::move(promise));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-22 01:15:09 +02:00
|
|
|
if (pending_answer.log_event_id_ != 0 && log_event_id != 0) {
|
|
|
|
LOG(ERROR) << "Duplicate SetPollAnswer log event: " << pending_answer.log_event_id_ << " and " << log_event_id;
|
|
|
|
binlog_erase(G()->td_db()->get_binlog(), log_event_id);
|
2019-07-27 22:16:22 +02:00
|
|
|
return;
|
|
|
|
}
|
2023-03-13 17:47:38 +01:00
|
|
|
if (log_event_id == 0 && G()->use_message_database()) {
|
2020-09-22 01:15:09 +02:00
|
|
|
SetPollAnswerLogEvent log_event;
|
|
|
|
log_event.poll_id_ = poll_id;
|
2023-09-21 18:11:17 +02:00
|
|
|
log_event.message_full_id_ = message_full_id;
|
2020-09-22 01:15:09 +02:00
|
|
|
log_event.options_ = options;
|
|
|
|
auto storer = get_log_event_storer(log_event);
|
2019-02-21 15:40:37 +01:00
|
|
|
if (pending_answer.generation_ == 0) {
|
2020-09-22 01:15:09 +02:00
|
|
|
CHECK(pending_answer.log_event_id_ == 0);
|
|
|
|
log_event_id = binlog_add(G()->td_db()->get_binlog(), LogEvent::HandlerType::SetPollAnswer, storer);
|
|
|
|
LOG(INFO) << "Add set poll answer log event " << log_event_id;
|
2024-01-10 19:41:13 +01:00
|
|
|
CHECK(log_event_id != 0);
|
2019-02-21 15:40:37 +01:00
|
|
|
} else {
|
2020-09-22 01:15:09 +02:00
|
|
|
CHECK(pending_answer.log_event_id_ != 0);
|
|
|
|
log_event_id = pending_answer.log_event_id_;
|
2024-01-10 19:41:13 +01:00
|
|
|
auto new_log_event_id =
|
|
|
|
binlog_rewrite(G()->td_db()->get_binlog(), log_event_id, LogEvent::HandlerType::SetPollAnswer, storer);
|
2020-09-22 01:15:09 +02:00
|
|
|
LOG(INFO) << "Rewrite set poll answer log event " << log_event_id << " with " << new_log_event_id;
|
2019-02-21 15:40:37 +01:00
|
|
|
}
|
2019-02-21 13:23:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!pending_answer.promises_.empty()) {
|
2019-02-21 13:42:54 +01:00
|
|
|
CHECK(!pending_answer.query_ref_.empty());
|
|
|
|
cancel_query(pending_answer.query_ref_);
|
|
|
|
pending_answer.query_ref_ = NetQueryRef();
|
|
|
|
|
2019-02-21 13:23:05 +01:00
|
|
|
auto promises = std::move(pending_answer.promises_);
|
|
|
|
pending_answer.promises_.clear();
|
|
|
|
for (auto &old_promise : promises) {
|
|
|
|
old_promise.set_value(Unit());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<BufferSlice> sent_options;
|
|
|
|
for (auto &option : options) {
|
|
|
|
sent_options.emplace_back(option);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto generation = ++current_generation_;
|
|
|
|
|
|
|
|
pending_answer.options_ = std::move(options);
|
|
|
|
pending_answer.promises_.push_back(std::move(promise));
|
|
|
|
pending_answer.generation_ = generation;
|
2020-09-22 01:15:09 +02:00
|
|
|
pending_answer.log_event_id_ = log_event_id;
|
2023-08-19 05:42:06 +02:00
|
|
|
pending_answer.is_finished_ = false;
|
2019-02-21 13:23:05 +01:00
|
|
|
|
|
|
|
notify_on_poll_update(poll_id);
|
|
|
|
|
2019-03-11 17:06:53 +01:00
|
|
|
auto query_promise = PromiseCreator::lambda(
|
2023-07-11 16:53:43 +02:00
|
|
|
[actor_id = actor_id(this), poll_id, generation](Result<tl_object_ptr<telegram_api::Updates>> &&result) {
|
2019-03-11 17:06:53 +01:00
|
|
|
send_closure(actor_id, &PollManager::on_set_poll_answer, poll_id, generation, std::move(result));
|
|
|
|
});
|
2022-02-03 22:46:10 +01:00
|
|
|
td_->create_handler<SendVoteQuery>(std::move(query_promise))
|
2023-09-21 18:11:17 +02:00
|
|
|
->send(message_full_id, std::move(sent_options), poll_id, generation, &pending_answer.query_ref_);
|
2019-02-21 13:23:05 +01:00
|
|
|
}
|
|
|
|
|
2019-03-11 17:06:53 +01:00
|
|
|
void PollManager::on_set_poll_answer(PollId poll_id, uint64 generation,
|
|
|
|
Result<tl_object_ptr<telegram_api::Updates>> &&result) {
|
2019-02-21 13:23:05 +01:00
|
|
|
if (G()->close_flag() && result.is_error()) {
|
2019-02-22 21:15:43 +01:00
|
|
|
// request will be re-sent after restart
|
2019-02-21 13:23:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto it = pending_answers_.find(poll_id);
|
|
|
|
if (it == pending_answers_.end()) {
|
|
|
|
// can happen if this is an answer with mismatched generation and server has ignored invoke-after
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto &pending_answer = it->second;
|
|
|
|
CHECK(!pending_answer.promises_.empty());
|
|
|
|
if (pending_answer.generation_ != generation) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-22 01:15:09 +02:00
|
|
|
if (pending_answer.log_event_id_ != 0) {
|
|
|
|
LOG(INFO) << "Delete set poll answer log event " << pending_answer.log_event_id_;
|
|
|
|
binlog_erase(G()->td_db()->get_binlog(), pending_answer.log_event_id_);
|
2023-08-19 05:42:06 +02:00
|
|
|
pending_answer.log_event_id_ = 0;
|
2019-02-21 13:23:05 +01:00
|
|
|
}
|
|
|
|
|
2023-08-19 05:42:06 +02:00
|
|
|
pending_answer.is_finished_ = true;
|
2019-03-11 17:06:53 +01:00
|
|
|
|
2020-04-04 01:44:25 +02:00
|
|
|
auto poll = get_poll(poll_id);
|
|
|
|
if (poll != nullptr) {
|
2023-04-25 14:29:24 +02:00
|
|
|
poll->was_saved_ = false;
|
2020-04-04 01:44:25 +02:00
|
|
|
}
|
2019-03-16 21:48:00 +01:00
|
|
|
if (result.is_ok()) {
|
2020-12-21 23:51:20 +01:00
|
|
|
td_->updates_manager_->on_get_updates(
|
2023-08-19 05:42:06 +02:00
|
|
|
result.move_as_ok(),
|
|
|
|
PromiseCreator::lambda([actor_id = actor_id(this), poll_id, generation](Result<Unit> &&result) mutable {
|
|
|
|
send_closure(actor_id, &PollManager::on_set_poll_answer_finished, poll_id, Unit(), generation);
|
2020-12-21 23:51:20 +01:00
|
|
|
}));
|
2019-03-16 21:48:00 +01:00
|
|
|
} else {
|
2023-08-19 05:42:06 +02:00
|
|
|
on_set_poll_answer_finished(poll_id, result.move_as_error(), generation);
|
2019-02-21 13:23:05 +01:00
|
|
|
}
|
2020-12-21 23:51:20 +01:00
|
|
|
}
|
|
|
|
|
2023-08-19 05:42:06 +02:00
|
|
|
void PollManager::on_set_poll_answer_finished(PollId poll_id, Result<Unit> &&result, uint64 generation) {
|
|
|
|
auto it = pending_answers_.find(poll_id);
|
|
|
|
if (it == pending_answers_.end()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto &pending_answer = it->second;
|
|
|
|
CHECK(!pending_answer.promises_.empty());
|
|
|
|
if (pending_answer.generation_ != generation) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
CHECK(pending_answer.is_finished_);
|
|
|
|
|
|
|
|
auto promises = std::move(pending_answer.promises_);
|
|
|
|
pending_answers_.erase(it);
|
|
|
|
|
2020-12-21 23:51:20 +01:00
|
|
|
if (!G()->close_flag()) {
|
|
|
|
auto poll = get_poll(poll_id);
|
2023-04-25 14:29:24 +02:00
|
|
|
if (poll != nullptr && !poll->was_saved_) {
|
2020-12-21 23:51:20 +01:00
|
|
|
// no updates was sent during updates processing, so send them
|
|
|
|
// poll wasn't changed, so there is no reason to actually save it
|
2023-04-25 14:29:24 +02:00
|
|
|
if (!(poll->is_closed_ && poll->is_updated_after_close_)) {
|
2020-12-21 23:51:20 +01:00
|
|
|
LOG(INFO) << "Schedule updating of " << poll_id << " soon";
|
|
|
|
update_poll_timeout_.set_timeout_in(poll_id.get(), 0.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
notify_on_poll_update(poll_id);
|
2023-04-25 14:29:24 +02:00
|
|
|
poll->was_saved_ = true;
|
2020-04-04 01:44:25 +02:00
|
|
|
}
|
2020-12-21 23:51:20 +01:00
|
|
|
}
|
2020-04-04 01:44:25 +02:00
|
|
|
|
2023-08-19 05:42:06 +02:00
|
|
|
LOG(INFO) << "Finish to set answer for " << poll_id;
|
|
|
|
|
2022-04-13 16:40:12 +02:00
|
|
|
if (result.is_ok()) {
|
|
|
|
set_promises(promises);
|
|
|
|
} else {
|
|
|
|
fail_promises(promises, result.move_as_error());
|
2020-04-04 01:44:25 +02:00
|
|
|
}
|
2019-02-21 13:23:05 +01:00
|
|
|
}
|
|
|
|
|
2020-01-13 19:21:58 +01:00
|
|
|
void PollManager::invalidate_poll_voters(const Poll *poll, PollId poll_id) {
|
2023-04-25 14:29:24 +02:00
|
|
|
if (poll->is_anonymous_) {
|
2020-01-13 19:21:58 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto it = poll_voters_.find(poll_id);
|
|
|
|
if (it == poll_voters_.end()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto &voters : it->second) {
|
2023-04-25 14:16:29 +02:00
|
|
|
voters.was_invalidated_ = true;
|
2020-01-13 19:21:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PollManager::invalidate_poll_option_voters(const Poll *poll, PollId poll_id, size_t option_index) {
|
2023-04-25 14:29:24 +02:00
|
|
|
if (poll->is_anonymous_) {
|
2020-01-13 19:21:58 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto it = poll_voters_.find(poll_id);
|
|
|
|
if (it == poll_voters_.end()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto &poll_voters = it->second;
|
2023-04-25 14:29:24 +02:00
|
|
|
CHECK(poll_voters.size() == poll->options_.size());
|
2020-01-13 19:21:58 +01:00
|
|
|
CHECK(option_index < poll_voters.size());
|
2023-04-25 14:16:29 +02:00
|
|
|
poll_voters[option_index].was_invalidated_ = true;
|
2020-01-13 19:21:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
PollManager::PollOptionVoters &PollManager::get_poll_option_voters(const Poll *poll, PollId poll_id, int32 option_id) {
|
|
|
|
auto &poll_voters = poll_voters_[poll_id];
|
|
|
|
if (poll_voters.empty()) {
|
2023-04-25 14:29:24 +02:00
|
|
|
poll_voters.resize(poll->options_.size());
|
2020-01-13 19:21:58 +01:00
|
|
|
}
|
|
|
|
auto index = narrow_cast<size_t>(option_id);
|
|
|
|
CHECK(index < poll_voters.size());
|
|
|
|
return poll_voters[index];
|
|
|
|
}
|
|
|
|
|
2023-04-25 15:41:04 +02:00
|
|
|
td_api::object_ptr<td_api::messageSenders> PollManager::get_poll_voters_object(
|
|
|
|
int32 total_count, const vector<DialogId> &voter_dialog_ids) const {
|
|
|
|
auto result = td_api::make_object<td_api::messageSenders>();
|
|
|
|
result->total_count_ = total_count;
|
|
|
|
for (auto dialog_id : voter_dialog_ids) {
|
|
|
|
auto message_sender = get_min_message_sender_object(td_, dialog_id, "get_poll_voters_object");
|
|
|
|
if (message_sender != nullptr) {
|
|
|
|
result->senders_.push_back(std::move(message_sender));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-09-21 18:11:17 +02:00
|
|
|
void PollManager::get_poll_voters(PollId poll_id, MessageFullId message_full_id, int32 option_id, int32 offset,
|
2023-04-25 15:41:04 +02:00
|
|
|
int32 limit, Promise<td_api::object_ptr<td_api::messageSenders>> &&promise) {
|
2020-01-13 19:21:58 +01:00
|
|
|
if (is_local_poll_id(poll_id)) {
|
|
|
|
return promise.set_error(Status::Error(400, "Poll results can't be received"));
|
|
|
|
}
|
|
|
|
if (offset < 0) {
|
|
|
|
return promise.set_error(Status::Error(400, "Invalid offset specified"));
|
|
|
|
}
|
2020-01-18 05:01:05 +01:00
|
|
|
if (limit <= 0) {
|
|
|
|
return promise.set_error(Status::Error(400, "Parameter limit must be positive"));
|
|
|
|
}
|
|
|
|
if (limit > MAX_GET_POLL_VOTERS) {
|
|
|
|
limit = MAX_GET_POLL_VOTERS;
|
|
|
|
}
|
2020-01-13 19:21:58 +01:00
|
|
|
|
|
|
|
auto poll = get_poll(poll_id);
|
|
|
|
CHECK(poll != nullptr);
|
2023-04-25 14:29:24 +02:00
|
|
|
if (option_id < 0 || static_cast<size_t>(option_id) >= poll->options_.size()) {
|
2020-01-13 19:21:58 +01:00
|
|
|
return promise.set_error(Status::Error(400, "Invalid option ID specified"));
|
|
|
|
}
|
2023-04-25 14:29:24 +02:00
|
|
|
if (poll->is_anonymous_) {
|
2020-01-13 19:21:58 +01:00
|
|
|
return promise.set_error(Status::Error(400, "Poll is anonymous"));
|
|
|
|
}
|
|
|
|
|
|
|
|
auto &voters = get_poll_option_voters(poll, poll_id, option_id);
|
2023-04-25 14:16:29 +02:00
|
|
|
if (voters.pending_queries_.empty() && voters.was_invalidated_ && offset == 0) {
|
2023-04-25 15:41:04 +02:00
|
|
|
voters.voter_dialog_ids_.clear();
|
2023-04-25 14:16:29 +02:00
|
|
|
voters.next_offset_.clear();
|
|
|
|
voters.was_invalidated_ = false;
|
2020-01-13 19:21:58 +01:00
|
|
|
}
|
|
|
|
|
2023-04-25 15:41:04 +02:00
|
|
|
auto cur_offset = narrow_cast<int32>(voters.voter_dialog_ids_.size());
|
2020-01-13 19:21:58 +01:00
|
|
|
|
|
|
|
if (offset > cur_offset) {
|
2020-04-18 00:55:54 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Too big offset specified; voters can be received only consequently"));
|
2020-01-13 19:21:58 +01:00
|
|
|
}
|
|
|
|
if (offset < cur_offset) {
|
2023-04-25 15:41:04 +02:00
|
|
|
vector<DialogId> result;
|
2020-01-18 05:01:05 +01:00
|
|
|
for (int32 i = offset; i != cur_offset && i - offset < limit; i++) {
|
2023-04-25 15:41:04 +02:00
|
|
|
result.push_back(voters.voter_dialog_ids_[i]);
|
2020-01-13 19:21:58 +01:00
|
|
|
}
|
2023-04-25 15:41:04 +02:00
|
|
|
return promise.set_value(
|
|
|
|
get_poll_voters_object(max(poll->options_[option_id].voter_count_, cur_offset), std::move(result)));
|
2020-01-13 19:21:58 +01:00
|
|
|
}
|
|
|
|
|
2023-04-25 14:29:24 +02:00
|
|
|
if (poll->options_[option_id].voter_count_ == 0 || (voters.next_offset_.empty() && cur_offset > 0)) {
|
2023-04-25 15:41:04 +02:00
|
|
|
return promise.set_value(get_poll_voters_object(0, vector<DialogId>()));
|
2020-01-13 19:21:58 +01:00
|
|
|
}
|
|
|
|
|
2023-04-25 14:16:29 +02:00
|
|
|
voters.pending_queries_.push_back(std::move(promise));
|
|
|
|
if (voters.pending_queries_.size() > 1) {
|
2020-01-13 19:21:58 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-05-09 00:46:46 +02:00
|
|
|
unload_poll_timeout_.cancel_timeout(poll_id.get());
|
|
|
|
|
2020-05-23 19:04:51 +02:00
|
|
|
auto query_promise =
|
2023-04-25 14:16:29 +02:00
|
|
|
PromiseCreator::lambda([actor_id = actor_id(this), poll_id, option_id, offset = voters.next_offset_,
|
2021-10-19 17:11:16 +02:00
|
|
|
limit](Result<tl_object_ptr<telegram_api::messages_votesList>> &&result) mutable {
|
2020-05-23 19:04:51 +02:00
|
|
|
send_closure(actor_id, &PollManager::on_get_poll_voters, poll_id, option_id, std::move(offset), limit,
|
|
|
|
std::move(result));
|
|
|
|
});
|
2020-01-13 19:21:58 +01:00
|
|
|
td_->create_handler<GetPollVotersQuery>(std::move(query_promise))
|
2023-09-21 18:11:17 +02:00
|
|
|
->send(poll_id, message_full_id, BufferSlice(poll->options_[option_id].data_), voters.next_offset_,
|
2023-04-25 14:16:29 +02:00
|
|
|
max(limit, 10));
|
2020-01-13 19:21:58 +01:00
|
|
|
}
|
|
|
|
|
2020-05-23 19:04:51 +02:00
|
|
|
void PollManager::on_get_poll_voters(PollId poll_id, int32 option_id, string offset, int32 limit,
|
2020-01-13 19:21:58 +01:00
|
|
|
Result<tl_object_ptr<telegram_api::messages_votesList>> &&result) {
|
2023-03-14 21:20:05 +01:00
|
|
|
G()->ignore_result_if_closing(result);
|
|
|
|
|
2020-01-13 19:21:58 +01:00
|
|
|
auto poll = get_poll(poll_id);
|
|
|
|
CHECK(poll != nullptr);
|
2023-04-25 14:29:24 +02:00
|
|
|
if (option_id < 0 || static_cast<size_t>(option_id) >= poll->options_.size()) {
|
2020-01-13 19:21:58 +01:00
|
|
|
LOG(ERROR) << "Can't process voters for option " << option_id << " in " << poll_id << ", because it has only "
|
2023-04-25 14:29:24 +02:00
|
|
|
<< poll->options_.size() << " options";
|
2020-01-13 19:21:58 +01:00
|
|
|
return;
|
|
|
|
}
|
2023-04-25 14:29:24 +02:00
|
|
|
if (poll->is_anonymous_) {
|
2020-01-13 19:21:58 +01:00
|
|
|
// just in case
|
|
|
|
result = Status::Error(400, "Poll is anonymous");
|
|
|
|
}
|
|
|
|
|
|
|
|
auto &voters = get_poll_option_voters(poll, poll_id, option_id);
|
2023-04-25 14:16:29 +02:00
|
|
|
if (voters.next_offset_ != offset) {
|
2020-05-23 19:04:51 +02:00
|
|
|
LOG(ERROR) << "Expected results for option " << option_id << " in " << poll_id << " with offset "
|
2023-04-25 14:16:29 +02:00
|
|
|
<< voters.next_offset_ << ", but received with " << offset;
|
2020-05-23 19:04:51 +02:00
|
|
|
return;
|
|
|
|
}
|
2023-04-25 14:16:29 +02:00
|
|
|
auto promises = std::move(voters.pending_queries_);
|
2020-05-23 19:04:51 +02:00
|
|
|
if (promises.empty()) {
|
|
|
|
LOG(ERROR) << "Have no waiting promises for option " << option_id << " in " << poll_id;
|
|
|
|
return;
|
|
|
|
}
|
2020-01-13 19:21:58 +01:00
|
|
|
if (result.is_error()) {
|
2022-04-13 16:40:12 +02:00
|
|
|
fail_promises(promises, result.move_as_error());
|
2020-01-13 19:21:58 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto vote_list = result.move_as_ok();
|
|
|
|
td_->contacts_manager_->on_get_users(std::move(vote_list->users_), "on_get_poll_voters");
|
2023-04-25 13:27:58 +02:00
|
|
|
td_->contacts_manager_->on_get_chats(std::move(vote_list->chats_), "on_get_poll_voters");
|
2020-01-13 19:21:58 +01:00
|
|
|
|
2023-04-25 14:16:29 +02:00
|
|
|
voters.next_offset_ = std::move(vote_list->next_offset_);
|
2023-04-25 14:29:24 +02:00
|
|
|
if (poll->options_[option_id].voter_count_ != vote_list->count_) {
|
2020-01-13 19:21:58 +01:00
|
|
|
++current_generation_;
|
|
|
|
update_poll_timeout_.set_timeout_in(poll_id.get(), 0.0);
|
|
|
|
}
|
|
|
|
|
2023-04-25 15:41:04 +02:00
|
|
|
vector<DialogId> dialog_ids;
|
2023-04-25 13:27:58 +02:00
|
|
|
for (auto &peer_vote : vote_list->votes_) {
|
|
|
|
DialogId dialog_id;
|
|
|
|
switch (peer_vote->get_id()) {
|
|
|
|
case telegram_api::messagePeerVote::ID: {
|
|
|
|
auto voter = telegram_api::move_object_as<telegram_api::messagePeerVote>(peer_vote);
|
2023-04-25 14:29:24 +02:00
|
|
|
if (voter->option_ != poll->options_[option_id].data_) {
|
2020-01-14 15:16:36 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-04-25 13:27:58 +02:00
|
|
|
dialog_id = DialogId(voter->peer_);
|
2020-01-14 15:16:36 +01:00
|
|
|
break;
|
|
|
|
}
|
2023-04-25 13:27:58 +02:00
|
|
|
case telegram_api::messagePeerVoteInputOption::ID: {
|
|
|
|
auto voter = telegram_api::move_object_as<telegram_api::messagePeerVoteInputOption>(peer_vote);
|
|
|
|
dialog_id = DialogId(voter->peer_);
|
2020-01-14 15:16:36 +01:00
|
|
|
break;
|
2023-04-25 13:13:34 +02:00
|
|
|
}
|
2023-04-25 13:27:58 +02:00
|
|
|
case telegram_api::messagePeerVoteMultiple::ID: {
|
|
|
|
auto voter = telegram_api::move_object_as<telegram_api::messagePeerVoteMultiple>(peer_vote);
|
2023-04-25 14:29:24 +02:00
|
|
|
if (!td::contains(voter->options_, poll->options_[option_id].data_)) {
|
2020-01-14 15:16:36 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-04-25 13:27:58 +02:00
|
|
|
dialog_id = DialogId(voter->peer_);
|
2020-01-14 15:16:36 +01:00
|
|
|
break;
|
|
|
|
}
|
2023-04-25 13:13:34 +02:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
2023-04-25 13:27:58 +02:00
|
|
|
if (dialog_id.is_valid()) {
|
2023-04-25 15:41:04 +02:00
|
|
|
dialog_ids.push_back(dialog_id);
|
2023-04-25 13:13:34 +02:00
|
|
|
} else {
|
2023-04-25 13:27:58 +02:00
|
|
|
LOG(ERROR) << "Receive " << dialog_id << " as voter in " << poll_id;
|
2020-01-14 15:16:36 +01:00
|
|
|
}
|
2020-01-13 19:21:58 +01:00
|
|
|
}
|
2023-04-25 15:41:04 +02:00
|
|
|
append(voters.voter_dialog_ids_, dialog_ids);
|
|
|
|
if (static_cast<int32>(dialog_ids.size()) > limit) {
|
|
|
|
dialog_ids.resize(limit);
|
2020-01-18 05:01:05 +01:00
|
|
|
}
|
2023-04-25 15:41:04 +02:00
|
|
|
auto known_voter_count = narrow_cast<int32>(voters.voter_dialog_ids_.size());
|
2023-04-25 14:16:29 +02:00
|
|
|
if (voters.next_offset_.empty() && known_voter_count != vote_list->count_) {
|
2020-03-30 21:43:04 +02:00
|
|
|
// invalidate_poll_option_voters(poll, poll_id, option_id);
|
2023-04-25 14:16:29 +02:00
|
|
|
voters.was_invalidated_ = true;
|
2020-03-30 21:43:04 +02:00
|
|
|
}
|
2020-01-13 19:21:58 +01:00
|
|
|
|
|
|
|
for (auto &promise : promises) {
|
2023-04-25 15:41:04 +02:00
|
|
|
promise.set_value(get_poll_voters_object(max(vote_list->count_, known_voter_count), vector<DialogId>(dialog_ids)));
|
2020-01-13 19:21:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-21 18:11:17 +02:00
|
|
|
void PollManager::stop_poll(PollId poll_id, MessageFullId message_full_id, unique_ptr<ReplyMarkup> &&reply_markup,
|
2019-04-12 01:10:33 +02:00
|
|
|
Promise<Unit> &&promise) {
|
2019-02-22 16:09:55 +01:00
|
|
|
if (is_local_poll_id(poll_id)) {
|
2023-09-21 18:11:17 +02:00
|
|
|
LOG(ERROR) << "Receive local " << poll_id << " from " << message_full_id << " in stop_poll";
|
2019-02-22 16:09:55 +01:00
|
|
|
stop_local_poll(poll_id);
|
|
|
|
promise.set_value(Unit());
|
|
|
|
return;
|
|
|
|
}
|
2019-02-19 15:06:48 +01:00
|
|
|
auto poll = get_poll_editable(poll_id);
|
|
|
|
CHECK(poll != nullptr);
|
2023-04-25 14:29:24 +02:00
|
|
|
if (poll->is_closed_) {
|
2019-02-22 16:09:55 +01:00
|
|
|
promise.set_value(Unit());
|
2019-02-19 15:06:48 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-10 08:57:15 +01:00
|
|
|
++current_generation_;
|
|
|
|
|
2023-04-25 14:29:24 +02:00
|
|
|
poll->is_closed_ = true;
|
2019-02-22 16:09:55 +01:00
|
|
|
save_poll(poll, poll_id);
|
2023-08-19 05:42:06 +02:00
|
|
|
notify_on_poll_update(poll_id);
|
2019-02-22 16:09:55 +01:00
|
|
|
|
2023-09-21 18:11:17 +02:00
|
|
|
do_stop_poll(poll_id, message_full_id, std::move(reply_markup), 0, std::move(promise));
|
2019-02-22 16:09:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class PollManager::StopPollLogEvent {
|
|
|
|
public:
|
|
|
|
PollId poll_id_;
|
2023-09-21 18:11:17 +02:00
|
|
|
MessageFullId message_full_id_;
|
2019-02-22 16:09:55 +01:00
|
|
|
|
|
|
|
template <class StorerT>
|
|
|
|
void store(StorerT &storer) const {
|
|
|
|
td::store(poll_id_, storer);
|
2023-09-21 18:11:17 +02:00
|
|
|
td::store(message_full_id_, storer);
|
2019-02-19 15:06:48 +01:00
|
|
|
}
|
2019-02-22 16:09:55 +01:00
|
|
|
|
|
|
|
template <class ParserT>
|
|
|
|
void parse(ParserT &parser) {
|
|
|
|
td::parse(poll_id_, parser);
|
2023-09-21 18:11:17 +02:00
|
|
|
td::parse(message_full_id_, parser);
|
2019-02-22 16:09:55 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-09-21 18:11:17 +02:00
|
|
|
void PollManager::do_stop_poll(PollId poll_id, MessageFullId message_full_id, unique_ptr<ReplyMarkup> &&reply_markup,
|
2020-09-22 01:15:09 +02:00
|
|
|
uint64 log_event_id, Promise<Unit> &&promise) {
|
2023-09-21 18:11:17 +02:00
|
|
|
LOG(INFO) << "Stop " << poll_id << " from " << message_full_id;
|
2022-03-11 19:38:48 +01:00
|
|
|
CHECK(poll_id.is_valid());
|
|
|
|
|
2023-03-13 17:47:38 +01:00
|
|
|
if (log_event_id == 0 && G()->use_message_database() && reply_markup == nullptr) {
|
2023-09-21 18:11:17 +02:00
|
|
|
StopPollLogEvent log_event{poll_id, message_full_id};
|
2020-09-22 01:15:09 +02:00
|
|
|
log_event_id =
|
|
|
|
binlog_add(G()->td_db()->get_binlog(), LogEvent::HandlerType::StopPoll, get_log_event_storer(log_event));
|
2019-02-22 16:09:55 +01:00
|
|
|
}
|
|
|
|
|
2022-05-09 00:46:46 +02:00
|
|
|
unload_poll_timeout_.cancel_timeout(poll_id.get());
|
|
|
|
|
2019-04-09 12:25:29 +02:00
|
|
|
bool is_inserted = being_closed_polls_.insert(poll_id).second;
|
|
|
|
CHECK(is_inserted);
|
2023-09-21 18:11:17 +02:00
|
|
|
auto new_promise = PromiseCreator::lambda([actor_id = actor_id(this), poll_id, message_full_id, log_event_id,
|
2022-11-22 10:15:58 +01:00
|
|
|
promise = std::move(promise)](Result<Unit> result) mutable {
|
2023-09-21 18:11:17 +02:00
|
|
|
send_closure(actor_id, &PollManager::on_stop_poll_finished, poll_id, message_full_id, log_event_id,
|
2022-11-22 10:15:58 +01:00
|
|
|
std::move(result), std::move(promise));
|
|
|
|
});
|
2019-02-22 16:09:55 +01:00
|
|
|
|
2023-09-21 18:11:17 +02:00
|
|
|
td_->create_handler<StopPollQuery>(std::move(new_promise))->send(message_full_id, std::move(reply_markup), poll_id);
|
2019-02-22 16:09:55 +01:00
|
|
|
}
|
|
|
|
|
2023-09-21 18:11:17 +02:00
|
|
|
void PollManager::on_stop_poll_finished(PollId poll_id, MessageFullId message_full_id, uint64 log_event_id,
|
2022-11-22 10:15:58 +01:00
|
|
|
Result<Unit> &&result, Promise<Unit> &&promise) {
|
2022-07-24 11:19:51 +02:00
|
|
|
being_closed_polls_.erase(poll_id);
|
|
|
|
|
|
|
|
if (log_event_id != 0 && !G()->close_flag()) {
|
|
|
|
binlog_erase(G()->td_db()->get_binlog(), log_event_id);
|
|
|
|
}
|
|
|
|
|
2022-11-22 10:15:58 +01:00
|
|
|
if (td_->auth_manager_->is_bot()) {
|
2023-09-21 18:11:17 +02:00
|
|
|
if ((server_poll_messages_.count(poll_id) > 0 && server_poll_messages_[poll_id].count(message_full_id) > 0) ||
|
|
|
|
(other_poll_messages_.count(poll_id) > 0 && other_poll_messages_[poll_id].count(message_full_id) > 0)) {
|
|
|
|
td_->messages_manager_->on_external_update_message_content(message_full_id);
|
2022-11-26 10:35:29 +01:00
|
|
|
}
|
2022-11-22 10:15:58 +01:00
|
|
|
}
|
|
|
|
|
2022-07-24 11:19:51 +02:00
|
|
|
promise.set_result(std::move(result));
|
|
|
|
}
|
|
|
|
|
2019-02-22 16:09:55 +01:00
|
|
|
void PollManager::stop_local_poll(PollId poll_id) {
|
|
|
|
CHECK(is_local_poll_id(poll_id));
|
|
|
|
auto poll = get_poll_editable(poll_id);
|
|
|
|
CHECK(poll != nullptr);
|
2023-04-25 14:29:24 +02:00
|
|
|
if (poll->is_closed_) {
|
2019-02-22 16:09:55 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-04-25 14:29:24 +02:00
|
|
|
poll->is_closed_ = true;
|
2019-02-22 16:09:55 +01:00
|
|
|
notify_on_poll_update(poll_id);
|
2019-02-19 15:06:48 +01:00
|
|
|
}
|
|
|
|
|
2019-02-22 20:53:02 +01:00
|
|
|
double PollManager::get_polling_timeout() const {
|
2019-03-10 09:00:14 +01:00
|
|
|
double result = td_->is_online() ? 60 : 30 * 60;
|
|
|
|
return result * Random::fast(70, 100) * 0.01;
|
2019-02-22 20:53:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void PollManager::on_update_poll_timeout(PollId poll_id) {
|
2019-03-17 23:51:43 +01:00
|
|
|
if (G()->close_flag()) {
|
|
|
|
return;
|
|
|
|
}
|
2021-11-13 20:37:41 +01:00
|
|
|
CHECK(!td_->auth_manager_->is_bot());
|
|
|
|
CHECK(!is_local_poll_id(poll_id));
|
|
|
|
|
2020-04-05 04:35:52 +02:00
|
|
|
auto poll = get_poll(poll_id);
|
2023-04-25 14:29:24 +02:00
|
|
|
if (poll == nullptr || (poll->is_closed_ && poll->is_updated_after_close_)) {
|
2019-02-22 20:53:02 +01:00
|
|
|
return;
|
|
|
|
}
|
2022-05-01 22:03:06 +02:00
|
|
|
if (pending_answers_.count(poll_id) > 0) {
|
2019-03-10 20:06:19 +01:00
|
|
|
LOG(INFO) << "Skip fetching results of " << poll_id << ", because it is being voted now";
|
|
|
|
return;
|
|
|
|
}
|
2019-02-22 20:53:02 +01:00
|
|
|
|
2022-11-22 10:58:07 +01:00
|
|
|
if (server_poll_messages_.count(poll_id) == 0) {
|
2019-02-22 20:53:02 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-09-21 18:11:17 +02:00
|
|
|
auto message_full_id = server_poll_messages_[poll_id].get_random();
|
|
|
|
LOG(INFO) << "Fetching results of " << poll_id << " from " << message_full_id;
|
2023-07-11 16:53:43 +02:00
|
|
|
auto query_promise = PromiseCreator::lambda([actor_id = actor_id(this), poll_id, generation = current_generation_](
|
2019-03-10 08:57:15 +01:00
|
|
|
Result<tl_object_ptr<telegram_api::Updates>> &&result) {
|
|
|
|
send_closure(actor_id, &PollManager::on_get_poll_results, poll_id, generation, std::move(result));
|
|
|
|
});
|
2023-09-21 18:11:17 +02:00
|
|
|
td_->create_handler<GetPollResultsQuery>(std::move(query_promise))->send(poll_id, message_full_id);
|
2019-02-22 20:53:02 +01:00
|
|
|
}
|
|
|
|
|
2020-04-05 03:31:59 +02:00
|
|
|
void PollManager::on_close_poll_timeout(PollId poll_id) {
|
|
|
|
if (G()->close_flag()) {
|
|
|
|
return;
|
|
|
|
}
|
2021-11-13 20:37:41 +01:00
|
|
|
CHECK(!is_local_poll_id(poll_id));
|
2020-04-05 03:31:59 +02:00
|
|
|
|
|
|
|
auto poll = get_poll_editable(poll_id);
|
2023-04-25 14:29:24 +02:00
|
|
|
if (poll == nullptr || poll->is_closed_ || poll->close_date_ == 0) {
|
2020-04-05 03:31:59 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(INFO) << "Trying to close " << poll_id << " by timer";
|
2023-04-25 14:29:24 +02:00
|
|
|
if (poll->close_date_ <= G()->server_time()) {
|
|
|
|
poll->is_closed_ = true;
|
2020-04-05 03:31:59 +02:00
|
|
|
save_poll(poll, poll_id);
|
2023-08-19 05:42:06 +02:00
|
|
|
notify_on_poll_update(poll_id);
|
2020-04-05 03:31:59 +02:00
|
|
|
|
|
|
|
// don't send updatePoll for bots, because there is no way to guarantee it
|
2020-04-05 04:35:52 +02:00
|
|
|
|
|
|
|
if (!td_->auth_manager_->is_bot()) {
|
|
|
|
update_poll_timeout_.set_timeout_in(poll_id.get(), 1.0);
|
|
|
|
}
|
2020-04-05 03:31:59 +02:00
|
|
|
} else {
|
2023-04-25 14:29:24 +02:00
|
|
|
close_poll_timeout_.set_timeout_in(poll_id.get(), poll->close_date_ - G()->server_time() + 1e-3);
|
2020-04-05 03:31:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-09 00:46:46 +02:00
|
|
|
void PollManager::on_unload_poll_timeout(PollId poll_id) {
|
|
|
|
if (G()->close_flag()) {
|
|
|
|
return;
|
|
|
|
}
|
2022-06-21 02:35:54 +02:00
|
|
|
if (is_local_poll_id(poll_id)) {
|
|
|
|
LOG(INFO) << "Forget " << poll_id;
|
|
|
|
|
|
|
|
auto is_deleted = polls_.erase(poll_id) > 0;
|
|
|
|
CHECK(is_deleted);
|
|
|
|
|
|
|
|
CHECK(poll_voters_.count(poll_id) == 0);
|
|
|
|
CHECK(loaded_from_database_polls_.count(poll_id) == 0);
|
|
|
|
return;
|
|
|
|
}
|
2022-05-09 00:46:46 +02:00
|
|
|
|
|
|
|
if (!can_unload_poll(poll_id)) {
|
|
|
|
return;
|
|
|
|
}
|
2022-05-26 13:44:30 +02:00
|
|
|
if (!have_poll(poll_id)) {
|
|
|
|
return;
|
|
|
|
}
|
2022-05-09 00:46:46 +02:00
|
|
|
|
|
|
|
LOG(INFO) << "Unload " << poll_id;
|
|
|
|
|
2023-03-16 23:02:30 +01:00
|
|
|
update_poll_timeout_.cancel_timeout(poll_id.get(), "on_unload_poll_timeout");
|
2022-05-09 00:46:46 +02:00
|
|
|
close_poll_timeout_.cancel_timeout(poll_id.get());
|
|
|
|
|
|
|
|
auto is_deleted = polls_.erase(poll_id) > 0;
|
|
|
|
CHECK(is_deleted);
|
|
|
|
|
|
|
|
poll_voters_.erase(poll_id);
|
|
|
|
loaded_from_database_polls_.erase(poll_id);
|
2022-05-26 13:44:30 +02:00
|
|
|
unload_poll_timeout_.cancel_timeout(poll_id.get());
|
2022-05-09 00:46:46 +02:00
|
|
|
}
|
|
|
|
|
2022-06-20 15:53:28 +02:00
|
|
|
void PollManager::forget_local_poll(PollId poll_id) {
|
2023-03-14 21:20:05 +01:00
|
|
|
if (G()->close_flag()) {
|
|
|
|
return;
|
|
|
|
}
|
2022-06-20 15:53:28 +02:00
|
|
|
CHECK(is_local_poll_id(poll_id));
|
2022-06-21 02:35:54 +02:00
|
|
|
unload_poll_timeout_.set_timeout_in(poll_id.get(), UNLOAD_POLL_DELAY);
|
2022-06-20 15:53:28 +02:00
|
|
|
}
|
|
|
|
|
2019-03-10 08:57:15 +01:00
|
|
|
void PollManager::on_get_poll_results(PollId poll_id, uint64 generation,
|
|
|
|
Result<tl_object_ptr<telegram_api::Updates>> result) {
|
2023-03-14 21:20:05 +01:00
|
|
|
G()->ignore_result_if_closing(result);
|
|
|
|
|
2020-04-05 04:35:52 +02:00
|
|
|
auto poll = get_poll(poll_id);
|
2022-05-09 00:46:46 +02:00
|
|
|
if (poll == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
2019-03-09 19:07:07 +01:00
|
|
|
if (result.is_error()) {
|
2023-04-25 14:29:24 +02:00
|
|
|
if (!(poll->is_closed_ && poll->is_updated_after_close_) && !G()->close_flag() && !td_->auth_manager_->is_bot()) {
|
2019-03-09 19:07:07 +01:00
|
|
|
auto timeout = get_polling_timeout();
|
|
|
|
LOG(INFO) << "Schedule updating of " << poll_id << " in " << timeout;
|
|
|
|
update_poll_timeout_.add_timeout_in(poll_id.get(), timeout);
|
|
|
|
}
|
|
|
|
return;
|
2019-02-22 20:53:02 +01:00
|
|
|
}
|
2020-01-11 01:46:26 +01:00
|
|
|
if (result.ok() == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
2019-03-10 08:57:15 +01:00
|
|
|
if (generation != current_generation_) {
|
|
|
|
LOG(INFO) << "Receive possibly outdated result of " << poll_id << ", reget it";
|
2023-04-25 14:29:24 +02:00
|
|
|
if (!(poll->is_closed_ && poll->is_updated_after_close_) && !G()->close_flag() && !td_->auth_manager_->is_bot()) {
|
2019-03-10 08:57:15 +01:00
|
|
|
update_poll_timeout_.set_timeout_in(poll_id.get(), 0.0);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2019-03-09 19:07:07 +01:00
|
|
|
|
2020-12-21 20:06:52 +01:00
|
|
|
td_->updates_manager_->on_get_updates(result.move_as_ok(), Promise<Unit>());
|
2019-02-22 20:53:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void PollManager::on_online() {
|
|
|
|
if (td_->auth_manager_->is_bot()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-09-21 18:11:17 +02:00
|
|
|
server_poll_messages_.foreach([&](const PollId &poll_id, WaitFreeHashSet<MessageFullId, MessageFullIdHash> &) {
|
2019-02-22 20:53:02 +01:00
|
|
|
if (update_poll_timeout_.has_timeout(poll_id.get())) {
|
|
|
|
auto timeout = Random::fast(3, 30);
|
|
|
|
LOG(INFO) << "Schedule updating of " << poll_id << " in " << timeout;
|
|
|
|
update_poll_timeout_.set_timeout_in(poll_id.get(), timeout);
|
|
|
|
}
|
2022-11-22 10:58:07 +01:00
|
|
|
});
|
2019-02-22 20:53:02 +01:00
|
|
|
}
|
|
|
|
|
2021-09-05 22:11:25 +02:00
|
|
|
PollId PollManager::dup_poll(PollId poll_id) {
|
|
|
|
auto poll = get_poll(poll_id);
|
|
|
|
CHECK(poll != nullptr);
|
|
|
|
|
2023-04-25 14:29:24 +02:00
|
|
|
auto question = poll->question_;
|
|
|
|
auto options = transform(poll->options_, [](auto &option) { return option.text_; });
|
|
|
|
auto explanation = poll->explanation_;
|
|
|
|
return create_poll(std::move(question), std::move(options), poll->is_anonymous_, poll->allow_multiple_answers_,
|
|
|
|
poll->is_quiz_, poll->correct_option_id_, std::move(explanation), poll->open_period_,
|
|
|
|
poll->open_period_ == 0 ? 0 : G()->unix_time(), false);
|
2021-09-05 22:11:25 +02:00
|
|
|
}
|
|
|
|
|
2020-01-15 03:42:10 +01:00
|
|
|
bool PollManager::has_input_media(PollId poll_id) const {
|
|
|
|
auto poll = get_poll(poll_id);
|
|
|
|
CHECK(poll != nullptr);
|
2023-04-25 14:29:24 +02:00
|
|
|
return !poll->is_quiz_ || poll->correct_option_id_ >= 0;
|
2020-01-15 03:42:10 +01:00
|
|
|
}
|
|
|
|
|
2019-02-19 14:45:32 +01:00
|
|
|
tl_object_ptr<telegram_api::InputMedia> PollManager::get_input_media(PollId poll_id) const {
|
|
|
|
auto poll = get_poll(poll_id);
|
|
|
|
CHECK(poll != nullptr);
|
2020-01-11 01:46:26 +01:00
|
|
|
|
|
|
|
int32 poll_flags = 0;
|
2023-04-25 14:29:24 +02:00
|
|
|
if (!poll->is_anonymous_) {
|
2020-01-11 01:46:26 +01:00
|
|
|
poll_flags |= telegram_api::poll::PUBLIC_VOTERS_MASK;
|
|
|
|
}
|
2023-04-25 14:29:24 +02:00
|
|
|
if (poll->allow_multiple_answers_) {
|
2020-01-11 01:46:26 +01:00
|
|
|
poll_flags |= telegram_api::poll::MULTIPLE_CHOICE_MASK;
|
|
|
|
}
|
2023-04-25 14:29:24 +02:00
|
|
|
if (poll->is_quiz_) {
|
2020-01-11 01:46:26 +01:00
|
|
|
poll_flags |= telegram_api::poll::QUIZ_MASK;
|
|
|
|
}
|
2023-04-25 14:29:24 +02:00
|
|
|
if (poll->open_period_ != 0) {
|
2020-04-03 23:05:35 +02:00
|
|
|
poll_flags |= telegram_api::poll::CLOSE_PERIOD_MASK;
|
|
|
|
}
|
2023-04-25 14:29:24 +02:00
|
|
|
if (poll->close_date_ != 0) {
|
2020-04-03 15:50:10 +02:00
|
|
|
poll_flags |= telegram_api::poll::CLOSE_DATE_MASK;
|
|
|
|
}
|
2023-04-25 14:29:24 +02:00
|
|
|
if (poll->is_closed_) {
|
2020-01-21 16:09:15 +01:00
|
|
|
poll_flags |= telegram_api::poll::CLOSED_MASK;
|
|
|
|
}
|
2020-01-11 01:46:26 +01:00
|
|
|
|
|
|
|
int32 flags = 0;
|
|
|
|
vector<BufferSlice> correct_answers;
|
2023-04-25 14:29:24 +02:00
|
|
|
if (poll->is_quiz_) {
|
2020-01-11 01:46:26 +01:00
|
|
|
flags |= telegram_api::inputMediaPoll::CORRECT_ANSWERS_MASK;
|
2023-04-25 14:29:24 +02:00
|
|
|
CHECK(poll->correct_option_id_ >= 0);
|
|
|
|
CHECK(static_cast<size_t>(poll->correct_option_id_) < poll->options_.size());
|
|
|
|
correct_answers.push_back(BufferSlice(poll->options_[poll->correct_option_id_].data_));
|
2020-04-03 22:39:50 +02:00
|
|
|
|
2023-04-25 14:29:24 +02:00
|
|
|
if (!poll->explanation_.text.empty()) {
|
2020-04-03 22:39:50 +02:00
|
|
|
flags |= telegram_api::inputMediaPoll::SOLUTION_MASK;
|
|
|
|
}
|
2020-01-11 01:46:26 +01:00
|
|
|
}
|
|
|
|
return telegram_api::make_object<telegram_api::inputMediaPoll>(
|
|
|
|
flags,
|
2020-04-03 15:50:10 +02:00
|
|
|
telegram_api::make_object<telegram_api::poll>(
|
2023-04-25 14:29:24 +02:00
|
|
|
0, poll_flags, false /*ignored*/, false /*ignored*/, false /*ignored*/, false /*ignored*/, poll->question_,
|
|
|
|
transform(poll->options_, get_input_poll_option), poll->open_period_, poll->close_date_),
|
|
|
|
std::move(correct_answers), poll->explanation_.text,
|
|
|
|
get_input_message_entities(td_->contacts_manager_.get(), poll->explanation_.entities, "get_input_media_poll"));
|
2019-02-19 14:45:32 +01:00
|
|
|
}
|
|
|
|
|
2019-02-21 01:52:58 +01:00
|
|
|
vector<PollManager::PollOption> PollManager::get_poll_options(
|
|
|
|
vector<tl_object_ptr<telegram_api::pollAnswer>> &&poll_options) {
|
|
|
|
return transform(std::move(poll_options), [](tl_object_ptr<telegram_api::pollAnswer> &&poll_option) {
|
|
|
|
PollOption option;
|
2023-04-25 14:16:29 +02:00
|
|
|
option.text_ = std::move(poll_option->text_);
|
|
|
|
option.data_ = poll_option->option_.as_slice().str();
|
2019-02-21 01:52:58 +01:00
|
|
|
return option;
|
2019-02-19 14:45:32 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
PollId PollManager::on_get_poll(PollId poll_id, tl_object_ptr<telegram_api::poll> &&poll_server,
|
2022-05-29 18:31:36 +02:00
|
|
|
tl_object_ptr<telegram_api::pollResults> &&poll_results, const char *source) {
|
2019-03-29 03:16:28 +01:00
|
|
|
bool is_bot = td_->auth_manager_->is_bot();
|
|
|
|
bool need_update_poll = poll_id.is_valid() && is_bot;
|
2019-02-19 14:45:32 +01:00
|
|
|
if (!poll_id.is_valid() && poll_server != nullptr) {
|
|
|
|
poll_id = PollId(poll_server->id_);
|
|
|
|
}
|
|
|
|
if (!poll_id.is_valid() || is_local_poll_id(poll_id)) {
|
2022-05-29 18:31:36 +02:00
|
|
|
LOG(ERROR) << "Receive " << poll_id << " from " << source << ": " << oneline(to_string(poll_server)) << " "
|
2020-05-17 16:14:09 +02:00
|
|
|
<< oneline(to_string(poll_results));
|
2019-02-19 14:45:32 +01:00
|
|
|
return PollId();
|
|
|
|
}
|
|
|
|
if (poll_server != nullptr && poll_server->id_ != poll_id.get()) {
|
2022-05-29 18:31:36 +02:00
|
|
|
LOG(ERROR) << "Receive poll " << poll_server->id_ << " instead of " << poll_id << " from " << source;
|
2019-02-19 14:45:32 +01:00
|
|
|
return PollId();
|
|
|
|
}
|
2020-05-23 16:21:56 +02:00
|
|
|
constexpr size_t MAX_POLL_OPTIONS = 10; // server-side limit
|
|
|
|
if (poll_server != nullptr &&
|
|
|
|
(poll_server->answers_.size() <= 1 || poll_server->answers_.size() > 10 * MAX_POLL_OPTIONS)) {
|
2022-05-29 18:31:36 +02:00
|
|
|
LOG(ERROR) << "Receive " << poll_id << " from " << source
|
|
|
|
<< " with wrong number of answers: " << to_string(poll_server);
|
2020-05-23 16:21:56 +02:00
|
|
|
return PollId();
|
|
|
|
}
|
|
|
|
if (poll_server != nullptr) {
|
2022-03-11 19:38:48 +01:00
|
|
|
FlatHashSet<Slice, SliceHash> option_data;
|
2020-05-23 16:21:56 +02:00
|
|
|
for (auto &answer : poll_server->answers_) {
|
2020-05-23 16:33:30 +02:00
|
|
|
if (answer->option_.empty()) {
|
2022-05-29 18:31:36 +02:00
|
|
|
LOG(ERROR) << "Receive " << poll_id << " from " << source
|
|
|
|
<< " with an empty option data: " << to_string(poll_server);
|
2020-05-23 16:33:30 +02:00
|
|
|
return PollId();
|
|
|
|
}
|
2020-05-23 16:21:56 +02:00
|
|
|
option_data.insert(answer->option_.as_slice());
|
|
|
|
}
|
|
|
|
if (option_data.size() != poll_server->answers_.size()) {
|
2022-05-29 18:31:36 +02:00
|
|
|
LOG(ERROR) << "Receive " << poll_id << " from " << source
|
|
|
|
<< " with duplicate options: " << to_string(poll_server);
|
2020-05-30 21:46:38 +02:00
|
|
|
return PollId();
|
2020-05-23 16:21:56 +02:00
|
|
|
}
|
2020-05-23 15:22:44 +02:00
|
|
|
}
|
2019-02-19 14:45:32 +01:00
|
|
|
|
|
|
|
auto poll = get_poll_force(poll_id);
|
|
|
|
bool is_changed = false;
|
2021-09-05 22:11:25 +02:00
|
|
|
bool need_save_to_database = false;
|
2019-02-19 14:45:32 +01:00
|
|
|
if (poll == nullptr) {
|
|
|
|
if (poll_server == nullptr) {
|
|
|
|
LOG(INFO) << "Ignore " << poll_id << ", because have no data about it";
|
|
|
|
return PollId();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto p = make_unique<Poll>();
|
|
|
|
poll = p.get();
|
2022-11-22 10:23:58 +01:00
|
|
|
polls_.set(poll_id, std::move(p));
|
2023-08-19 05:42:06 +02:00
|
|
|
} else if (poll_results != nullptr && poll_results->min_ && pending_answers_.count(poll_id) != 0) {
|
|
|
|
LOG(INFO) << "Ignore being answered min-" << poll_id;
|
|
|
|
return poll_id;
|
2019-02-19 14:45:32 +01:00
|
|
|
}
|
2023-08-19 05:42:06 +02:00
|
|
|
|
2019-02-19 14:45:32 +01:00
|
|
|
CHECK(poll != nullptr);
|
|
|
|
|
2020-04-22 18:36:42 +02:00
|
|
|
bool poll_server_is_closed = false;
|
2019-02-19 14:45:32 +01:00
|
|
|
if (poll_server != nullptr) {
|
2020-05-23 16:33:30 +02:00
|
|
|
string correct_option_data;
|
2023-04-25 14:29:24 +02:00
|
|
|
if (poll->correct_option_id_ != -1) {
|
|
|
|
CHECK(0 <= poll->correct_option_id_ && poll->correct_option_id_ < static_cast<int32>(poll->options_.size()));
|
|
|
|
correct_option_data = poll->options_[poll->correct_option_id_].data_;
|
2019-02-19 14:45:32 +01:00
|
|
|
}
|
2020-05-23 19:04:51 +02:00
|
|
|
bool are_options_changed = false;
|
2023-04-25 14:29:24 +02:00
|
|
|
if (poll->options_.size() != poll_server->answers_.size()) {
|
|
|
|
poll->options_ = get_poll_options(std::move(poll_server->answers_));
|
2020-05-23 19:04:51 +02:00
|
|
|
are_options_changed = true;
|
2019-02-19 14:45:32 +01:00
|
|
|
} else {
|
2023-04-25 14:29:24 +02:00
|
|
|
for (size_t i = 0; i < poll->options_.size(); i++) {
|
|
|
|
if (poll->options_[i].text_ != poll_server->answers_[i]->text_) {
|
|
|
|
poll->options_[i].text_ = std::move(poll_server->answers_[i]->text_);
|
2019-02-19 14:45:32 +01:00
|
|
|
is_changed = true;
|
|
|
|
}
|
2023-04-25 14:29:24 +02:00
|
|
|
if (poll->options_[i].data_ != poll_server->answers_[i]->option_.as_slice()) {
|
|
|
|
poll->options_[i].data_ = poll_server->answers_[i]->option_.as_slice().str();
|
|
|
|
poll->options_[i].voter_count_ = 0;
|
|
|
|
poll->options_[i].is_chosen_ = false;
|
2020-05-23 19:04:51 +02:00
|
|
|
are_options_changed = true;
|
2019-02-19 14:45:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-23 19:04:51 +02:00
|
|
|
if (are_options_changed) {
|
|
|
|
if (!correct_option_data.empty()) {
|
2023-04-25 14:29:24 +02:00
|
|
|
poll->correct_option_id_ = -1;
|
|
|
|
for (size_t i = 0; i < poll->options_.size(); i++) {
|
|
|
|
if (poll->options_[i].data_ == correct_option_data) {
|
|
|
|
poll->correct_option_id_ = static_cast<int32>(i);
|
2020-05-23 19:04:51 +02:00
|
|
|
break;
|
|
|
|
}
|
2020-05-23 16:33:30 +02:00
|
|
|
}
|
|
|
|
}
|
2020-05-23 19:04:51 +02:00
|
|
|
auto it = poll_voters_.find(poll_id);
|
|
|
|
if (it != poll_voters_.end()) {
|
|
|
|
for (auto &voters : it->second) {
|
2023-04-25 14:16:29 +02:00
|
|
|
fail_promises(voters.pending_queries_, Status::Error(500, "The poll was changed"));
|
2020-05-23 19:04:51 +02:00
|
|
|
}
|
|
|
|
poll_voters_.erase(it);
|
|
|
|
}
|
|
|
|
is_changed = true;
|
2020-05-23 16:33:30 +02:00
|
|
|
}
|
2023-04-25 14:29:24 +02:00
|
|
|
if (poll->question_ != poll_server->question_) {
|
|
|
|
poll->question_ = std::move(poll_server->question_);
|
2020-05-23 16:33:30 +02:00
|
|
|
is_changed = true;
|
|
|
|
}
|
2020-04-22 18:36:42 +02:00
|
|
|
poll_server_is_closed = (poll_server->flags_ & telegram_api::poll::CLOSED_MASK) != 0;
|
2023-04-25 14:29:24 +02:00
|
|
|
if (poll_server_is_closed && !poll->is_closed_) {
|
|
|
|
poll->is_closed_ = poll_server_is_closed;
|
2020-04-05 03:31:59 +02:00
|
|
|
is_changed = true;
|
|
|
|
}
|
2023-04-25 14:29:24 +02:00
|
|
|
if (poll_server_is_closed && !poll->is_updated_after_close_) {
|
|
|
|
poll->is_updated_after_close_ = true;
|
2020-04-22 15:18:19 +02:00
|
|
|
is_changed = true;
|
|
|
|
}
|
2022-12-18 20:15:57 +01:00
|
|
|
int32 open_period = poll_server->close_period_;
|
|
|
|
int32 close_date = poll_server->close_date_;
|
2020-04-03 23:05:35 +02:00
|
|
|
if (close_date == 0 || open_period == 0) {
|
2020-04-03 15:50:10 +02:00
|
|
|
close_date = 0;
|
2020-04-03 23:05:35 +02:00
|
|
|
open_period = 0;
|
2020-04-03 15:50:10 +02:00
|
|
|
}
|
2023-04-25 14:29:24 +02:00
|
|
|
if (open_period != poll->open_period_) {
|
|
|
|
poll->open_period_ = open_period;
|
|
|
|
if (!poll->is_closed_) {
|
2021-09-05 22:11:25 +02:00
|
|
|
is_changed = true;
|
|
|
|
} else {
|
|
|
|
need_save_to_database = true;
|
|
|
|
}
|
2020-04-03 15:50:10 +02:00
|
|
|
}
|
2023-04-25 14:29:24 +02:00
|
|
|
if (close_date != poll->close_date_) {
|
|
|
|
poll->close_date_ = close_date;
|
|
|
|
if (!poll->is_closed_) {
|
2021-09-05 22:11:25 +02:00
|
|
|
is_changed = true;
|
2020-04-05 03:31:59 +02:00
|
|
|
if (close_date != 0) {
|
|
|
|
if (close_date <= G()->server_time()) {
|
2023-04-25 14:29:24 +02:00
|
|
|
poll->is_closed_ = true;
|
2023-03-14 21:20:05 +01:00
|
|
|
} else if (!G()->close_flag()) {
|
2020-04-05 03:31:59 +02:00
|
|
|
close_poll_timeout_.set_timeout_in(poll_id.get(), close_date - G()->server_time() + 1e-3);
|
|
|
|
}
|
2023-03-14 21:20:05 +01:00
|
|
|
} else if (!G()->close_flag()) {
|
2020-04-05 03:31:59 +02:00
|
|
|
close_poll_timeout_.cancel_timeout(poll_id.get());
|
|
|
|
}
|
2021-09-05 22:11:25 +02:00
|
|
|
} else {
|
|
|
|
need_save_to_database = true;
|
2020-04-05 03:31:59 +02:00
|
|
|
}
|
2019-02-19 14:45:32 +01:00
|
|
|
}
|
2020-01-11 01:46:26 +01:00
|
|
|
bool is_anonymous = (poll_server->flags_ & telegram_api::poll::PUBLIC_VOTERS_MASK) == 0;
|
2023-04-25 14:29:24 +02:00
|
|
|
if (is_anonymous != poll->is_anonymous_) {
|
|
|
|
poll->is_anonymous_ = is_anonymous;
|
2020-01-11 01:46:26 +01:00
|
|
|
is_changed = true;
|
|
|
|
}
|
|
|
|
bool allow_multiple_answers = (poll_server->flags_ & telegram_api::poll::MULTIPLE_CHOICE_MASK) != 0;
|
|
|
|
bool is_quiz = (poll_server->flags_ & telegram_api::poll::QUIZ_MASK) != 0;
|
|
|
|
if (is_quiz && allow_multiple_answers) {
|
2022-05-29 18:31:36 +02:00
|
|
|
LOG(ERROR) << "Receive quiz " << poll_id << " from " << source << " allowing multiple answers";
|
2020-01-11 01:46:26 +01:00
|
|
|
allow_multiple_answers = false;
|
|
|
|
}
|
2023-04-25 14:29:24 +02:00
|
|
|
if (allow_multiple_answers != poll->allow_multiple_answers_) {
|
|
|
|
poll->allow_multiple_answers_ = allow_multiple_answers;
|
2020-01-11 01:46:26 +01:00
|
|
|
is_changed = true;
|
|
|
|
}
|
2023-04-25 14:29:24 +02:00
|
|
|
if (is_quiz != poll->is_quiz_) {
|
|
|
|
poll->is_quiz_ = is_quiz;
|
2020-01-11 01:46:26 +01:00
|
|
|
is_changed = true;
|
|
|
|
}
|
2019-02-19 14:45:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
CHECK(poll_results != nullptr);
|
2021-11-01 19:53:23 +01:00
|
|
|
bool is_min = poll_results->min_;
|
2019-03-05 22:12:04 +01:00
|
|
|
bool has_total_voters = (poll_results->flags_ & telegram_api::pollResults::TOTAL_VOTERS_MASK) != 0;
|
2023-04-25 14:29:24 +02:00
|
|
|
if (has_total_voters && poll_results->total_voters_ != poll->total_voter_count_) {
|
|
|
|
poll->total_voter_count_ = poll_results->total_voters_;
|
|
|
|
if (poll->total_voter_count_ < 0) {
|
|
|
|
LOG(ERROR) << "Receive " << poll->total_voter_count_ << " voters in " << poll_id << " from " << source;
|
|
|
|
poll->total_voter_count_ = 0;
|
2019-02-24 22:00:17 +01:00
|
|
|
}
|
2019-02-19 14:45:32 +01:00
|
|
|
is_changed = true;
|
|
|
|
}
|
2020-01-11 01:46:26 +01:00
|
|
|
int32 correct_option_id = -1;
|
2020-05-23 16:11:53 +02:00
|
|
|
for (auto &poll_result : poll_results->results_) {
|
2019-02-19 14:45:32 +01:00
|
|
|
Slice data = poll_result->option_.as_slice();
|
2023-04-25 14:29:24 +02:00
|
|
|
for (size_t option_index = 0; option_index < poll->options_.size(); option_index++) {
|
|
|
|
auto &option = poll->options_[option_index];
|
2023-04-25 14:16:29 +02:00
|
|
|
if (option.data_ != data) {
|
2019-02-19 14:45:32 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!is_min) {
|
2021-11-01 19:53:23 +01:00
|
|
|
bool is_chosen = poll_result->chosen_;
|
2023-04-25 14:16:29 +02:00
|
|
|
if (is_chosen != option.is_chosen_) {
|
|
|
|
option.is_chosen_ = is_chosen;
|
2019-02-19 14:45:32 +01:00
|
|
|
is_changed = true;
|
|
|
|
}
|
2020-01-24 02:43:19 +01:00
|
|
|
}
|
2020-04-22 18:36:42 +02:00
|
|
|
if (!is_min || poll_server_is_closed) {
|
2021-11-01 19:53:23 +01:00
|
|
|
bool is_correct = poll_result->correct_;
|
2020-01-17 18:29:56 +01:00
|
|
|
if (is_correct) {
|
|
|
|
if (correct_option_id != -1) {
|
2022-05-29 18:31:36 +02:00
|
|
|
LOG(ERROR) << "Receive more than 1 correct answers " << correct_option_id << " and " << option_index
|
|
|
|
<< " in " << poll_id << " from " << source;
|
2020-01-17 18:29:56 +01:00
|
|
|
}
|
2020-05-23 16:11:53 +02:00
|
|
|
correct_option_id = static_cast<int32>(option_index);
|
2020-01-11 01:46:26 +01:00
|
|
|
}
|
2020-01-17 18:29:56 +01:00
|
|
|
} else {
|
2023-04-25 14:29:24 +02:00
|
|
|
correct_option_id = poll->correct_option_id_;
|
2020-01-11 01:46:26 +01:00
|
|
|
}
|
2020-01-13 19:21:58 +01:00
|
|
|
|
|
|
|
if (poll_result->voters_ < 0) {
|
2022-05-29 18:31:36 +02:00
|
|
|
LOG(ERROR) << "Receive " << poll_result->voters_ << " voters for an option in " << poll_id << " from "
|
|
|
|
<< source;
|
2020-01-13 19:21:58 +01:00
|
|
|
poll_result->voters_ = 0;
|
|
|
|
}
|
2023-04-25 14:16:29 +02:00
|
|
|
if (option.is_chosen_ && poll_result->voters_ == 0) {
|
2023-08-19 05:42:06 +02:00
|
|
|
LOG(ERROR) << "Receive 0 voters for the chosen option " << option_index << " in " << poll_id << " from "
|
|
|
|
<< source;
|
2020-01-13 19:21:58 +01:00
|
|
|
poll_result->voters_ = 1;
|
|
|
|
}
|
2023-04-25 14:29:24 +02:00
|
|
|
if (poll_result->voters_ > poll->total_voter_count_) {
|
|
|
|
LOG(ERROR) << "Have only " << poll->total_voter_count_ << " poll voters, but there are " << poll_result->voters_
|
2022-05-29 18:31:36 +02:00
|
|
|
<< " voters for an option in " << poll_id << " from " << source;
|
2023-04-25 14:29:24 +02:00
|
|
|
poll->total_voter_count_ = poll_result->voters_;
|
2020-01-13 19:21:58 +01:00
|
|
|
}
|
2023-04-25 14:29:24 +02:00
|
|
|
auto max_voter_count = std::numeric_limits<int32>::max() / narrow_cast<int32>(poll->options_.size()) - 2;
|
2020-01-13 19:21:58 +01:00
|
|
|
if (poll_result->voters_ > max_voter_count) {
|
2022-05-29 18:31:36 +02:00
|
|
|
LOG(ERROR) << "Have too many " << poll_result->voters_ << " poll voters for an option in " << poll_id
|
|
|
|
<< " from " << source;
|
2020-01-13 19:21:58 +01:00
|
|
|
poll_result->voters_ = max_voter_count;
|
|
|
|
}
|
2023-04-25 14:16:29 +02:00
|
|
|
if (poll_result->voters_ != option.voter_count_) {
|
2020-01-13 19:21:58 +01:00
|
|
|
invalidate_poll_option_voters(poll, poll_id, option_index);
|
2023-04-25 14:16:29 +02:00
|
|
|
option.voter_count_ = poll_result->voters_;
|
2019-02-19 14:45:32 +01:00
|
|
|
is_changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-05 22:12:04 +01:00
|
|
|
if (!poll_results->results_.empty() && has_total_voters) {
|
|
|
|
int32 max_total_voter_count = 0;
|
2023-04-25 14:29:24 +02:00
|
|
|
for (auto &option : poll->options_) {
|
2023-04-25 14:16:29 +02:00
|
|
|
max_total_voter_count += option.voter_count_;
|
2019-03-05 22:12:04 +01:00
|
|
|
}
|
2023-04-25 14:29:24 +02:00
|
|
|
if (poll->total_voter_count_ > max_total_voter_count && max_total_voter_count != 0) {
|
2019-03-05 22:12:04 +01:00
|
|
|
LOG(ERROR) << "Have only " << max_total_voter_count << " total poll voters, but there are "
|
2023-04-25 14:29:24 +02:00
|
|
|
<< poll->total_voter_count_ << " voters in " << poll_id << " from " << source;
|
|
|
|
poll->total_voter_count_ = max_total_voter_count;
|
2019-03-05 22:12:04 +01:00
|
|
|
}
|
2019-03-05 21:21:10 +01:00
|
|
|
}
|
2020-04-03 22:39:50 +02:00
|
|
|
|
|
|
|
auto entities =
|
2022-05-29 18:31:36 +02:00
|
|
|
get_message_entities(td_->contacts_manager_.get(), std::move(poll_results->solution_entities_), source);
|
2021-07-26 21:48:33 +02:00
|
|
|
auto status = fix_formatted_text(poll_results->solution_, entities, true, true, true, true, false);
|
2020-04-03 22:39:50 +02:00
|
|
|
if (status.is_error()) {
|
|
|
|
if (!clean_input_string(poll_results->solution_)) {
|
|
|
|
poll_results->solution_.clear();
|
|
|
|
}
|
2021-08-04 03:26:41 +02:00
|
|
|
entities = find_entities(poll_results->solution_, true, true);
|
2020-04-03 22:39:50 +02:00
|
|
|
}
|
|
|
|
FormattedText explanation{std::move(poll_results->solution_), std::move(entities)};
|
|
|
|
|
2023-04-25 14:29:24 +02:00
|
|
|
if (poll->is_quiz_) {
|
|
|
|
if (poll->correct_option_id_ != correct_option_id) {
|
|
|
|
if (correct_option_id == -1 && poll->correct_option_id_ != -1) {
|
|
|
|
LOG(ERROR) << "Can't change correct option of " << poll_id << " from " << poll->correct_option_id_ << " to "
|
2022-05-29 18:31:36 +02:00
|
|
|
<< correct_option_id << " from " << source;
|
2020-04-19 10:27:40 +02:00
|
|
|
} else {
|
2023-04-25 14:29:24 +02:00
|
|
|
poll->correct_option_id_ = correct_option_id;
|
2020-04-19 10:27:40 +02:00
|
|
|
is_changed = true;
|
|
|
|
}
|
2020-01-11 01:46:26 +01:00
|
|
|
}
|
2023-04-25 14:29:24 +02:00
|
|
|
if (poll->explanation_ != explanation && (!is_min || poll_server_is_closed)) {
|
|
|
|
if (explanation.text.empty() && !poll->explanation_.text.empty()) {
|
2022-06-06 19:36:15 +02:00
|
|
|
LOG(ERROR) << "Can't change known " << poll_id << " explanation to empty from " << source;
|
2020-04-19 10:27:40 +02:00
|
|
|
} else {
|
2023-04-25 14:29:24 +02:00
|
|
|
poll->explanation_ = std::move(explanation);
|
2020-04-19 10:27:40 +02:00
|
|
|
is_changed = true;
|
|
|
|
}
|
2020-04-03 22:39:50 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (correct_option_id != -1) {
|
2022-05-29 18:31:36 +02:00
|
|
|
LOG(ERROR) << "Receive correct option " << correct_option_id << " in non-quiz " << poll_id << " from " << source;
|
2020-04-03 22:39:50 +02:00
|
|
|
}
|
2020-04-19 10:27:40 +02:00
|
|
|
if (!explanation.text.empty()) {
|
2022-05-29 18:31:36 +02:00
|
|
|
LOG(ERROR) << "Receive explanation " << explanation << " in non-quiz " << poll_id << " from " << source;
|
2020-04-19 10:27:40 +02:00
|
|
|
}
|
2020-01-11 01:46:26 +01:00
|
|
|
}
|
2020-04-03 22:39:50 +02:00
|
|
|
|
2023-04-25 15:41:04 +02:00
|
|
|
vector<DialogId> recent_voter_dialog_ids;
|
2020-01-29 23:50:28 +01:00
|
|
|
if (!is_bot) {
|
2023-04-25 13:27:58 +02:00
|
|
|
for (auto &peer_id : poll_results->recent_voters_) {
|
|
|
|
DialogId dialog_id(peer_id);
|
2023-04-25 15:41:04 +02:00
|
|
|
if (dialog_id.is_valid()) {
|
|
|
|
recent_voter_dialog_ids.push_back(dialog_id);
|
2020-01-14 04:14:47 +01:00
|
|
|
} else {
|
2023-04-25 15:41:04 +02:00
|
|
|
LOG(ERROR) << "Receive " << dialog_id << " as recent voter in " << poll_id << " from " << source;
|
2020-01-14 04:14:47 +01:00
|
|
|
}
|
2020-01-12 02:40:17 +01:00
|
|
|
}
|
|
|
|
}
|
2023-04-25 15:41:04 +02:00
|
|
|
if (poll->is_anonymous_ && !recent_voter_dialog_ids.empty()) {
|
|
|
|
LOG(ERROR) << "Receive anonymous " << poll_id << " with recent voters " << recent_voter_dialog_ids << " from "
|
2022-05-29 18:31:36 +02:00
|
|
|
<< source;
|
2023-04-25 15:41:04 +02:00
|
|
|
recent_voter_dialog_ids.clear();
|
2020-01-12 02:40:17 +01:00
|
|
|
}
|
2023-04-25 15:41:04 +02:00
|
|
|
if (recent_voter_dialog_ids != poll->recent_voter_dialog_ids_) {
|
|
|
|
poll->recent_voter_dialog_ids_ = std::move(recent_voter_dialog_ids);
|
2020-01-13 19:21:58 +01:00
|
|
|
invalidate_poll_voters(poll, poll_id);
|
2020-01-12 02:40:17 +01:00
|
|
|
is_changed = true;
|
|
|
|
}
|
2019-02-19 14:45:32 +01:00
|
|
|
|
2023-04-25 14:29:24 +02:00
|
|
|
if (!is_bot && !poll->is_closed_ && !G()->close_flag()) {
|
2019-02-22 20:53:02 +01:00
|
|
|
auto timeout = get_polling_timeout();
|
|
|
|
LOG(INFO) << "Schedule updating of " << poll_id << " in " << timeout;
|
|
|
|
update_poll_timeout_.set_timeout_in(poll_id.get(), timeout);
|
|
|
|
}
|
2021-09-05 22:11:25 +02:00
|
|
|
if (is_changed || need_save_to_database) {
|
2019-02-19 14:45:32 +01:00
|
|
|
save_poll(poll, poll_id);
|
2019-04-09 12:25:29 +02:00
|
|
|
}
|
2023-08-19 05:42:06 +02:00
|
|
|
if (is_changed) {
|
|
|
|
notify_on_poll_update(poll_id);
|
|
|
|
}
|
2023-04-25 14:29:24 +02:00
|
|
|
if (need_update_poll && (is_changed || (poll->is_closed_ && being_closed_polls_.erase(poll_id) != 0))) {
|
2019-04-09 12:25:29 +02:00
|
|
|
send_closure(G()->td(), &Td::send_update, td_api::make_object<td_api::updatePoll>(get_poll_object(poll_id, poll)));
|
2022-05-09 00:46:46 +02:00
|
|
|
|
|
|
|
schedule_poll_unload(poll_id);
|
2019-02-19 14:45:32 +01:00
|
|
|
}
|
|
|
|
return poll_id;
|
|
|
|
}
|
|
|
|
|
2023-04-25 15:41:04 +02:00
|
|
|
void PollManager::on_get_poll_vote(PollId poll_id, DialogId dialog_id, vector<BufferSlice> &&options) {
|
2020-01-13 20:56:59 +01:00
|
|
|
if (!poll_id.is_valid()) {
|
|
|
|
LOG(ERROR) << "Receive updateMessagePollVote about invalid " << poll_id;
|
|
|
|
return;
|
|
|
|
}
|
2023-04-25 15:41:04 +02:00
|
|
|
if (!dialog_id.is_valid()) {
|
|
|
|
LOG(ERROR) << "Receive updateMessagePollVote from invalid " << dialog_id;
|
2020-01-13 20:56:59 +01:00
|
|
|
return;
|
|
|
|
}
|
2023-12-14 21:27:46 +01:00
|
|
|
CHECK(td_->auth_manager_->is_bot());
|
2020-01-13 20:56:59 +01:00
|
|
|
|
|
|
|
vector<int32> option_ids;
|
|
|
|
for (auto &option : options) {
|
|
|
|
auto slice = option.as_slice();
|
|
|
|
if (slice.size() != 1 || slice[0] < '0' || slice[0] > '9') {
|
2022-06-28 16:22:04 +02:00
|
|
|
LOG(INFO) << "Receive updateMessagePollVote with unexpected option \"" << format::escaped(slice) << '"';
|
2020-01-13 20:56:59 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
option_ids.push_back(static_cast<int32>(slice[0] - '0'));
|
|
|
|
}
|
|
|
|
|
2023-04-25 15:41:04 +02:00
|
|
|
send_closure(
|
|
|
|
G()->td(), &Td::send_update,
|
|
|
|
td_api::make_object<td_api::updatePollAnswer>(
|
|
|
|
poll_id.get(), get_message_sender_object(td_, dialog_id, "on_get_poll_vote"), std::move(option_ids)));
|
2020-01-13 20:56:59 +01:00
|
|
|
}
|
|
|
|
|
2019-02-21 15:40:37 +01:00
|
|
|
void PollManager::on_binlog_events(vector<BinlogEvent> &&events) {
|
2022-01-13 15:28:10 +01:00
|
|
|
if (G()->close_flag()) {
|
|
|
|
return;
|
|
|
|
}
|
2023-05-15 17:16:55 +02:00
|
|
|
bool have_old_message_database = G()->use_message_database() && !G()->td_db()->was_dialog_db_created();
|
2019-02-21 15:40:37 +01:00
|
|
|
for (auto &event : events) {
|
|
|
|
switch (event.type_) {
|
|
|
|
case LogEvent::HandlerType::SetPollAnswer: {
|
2023-05-15 17:16:55 +02:00
|
|
|
if (!have_old_message_database) {
|
2019-04-03 11:26:20 +02:00
|
|
|
binlog_erase(G()->td_db()->get_binlog(), event.id_);
|
2019-02-21 15:40:37 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
SetPollAnswerLogEvent log_event;
|
2023-02-02 01:08:26 +01:00
|
|
|
log_event_parse(log_event, event.get_data()).ensure();
|
2019-02-21 15:40:37 +01:00
|
|
|
|
2023-09-21 18:11:17 +02:00
|
|
|
auto dialog_id = log_event.message_full_id_.get_dialog_id();
|
2019-02-21 15:40:37 +01:00
|
|
|
|
|
|
|
Dependencies dependencies;
|
2022-03-11 13:10:24 +01:00
|
|
|
dependencies.add_dialog_dependencies(dialog_id); // do not load the dialog itself
|
|
|
|
dependencies.resolve_force(td_, "SetPollAnswerLogEvent");
|
2019-02-21 15:40:37 +01:00
|
|
|
|
2023-09-21 18:11:17 +02:00
|
|
|
do_set_poll_answer(log_event.poll_id_, log_event.message_full_id_, std::move(log_event.options_), event.id_,
|
2019-02-21 15:40:37 +01:00
|
|
|
Auto());
|
|
|
|
break;
|
|
|
|
}
|
2019-02-22 16:09:55 +01:00
|
|
|
case LogEvent::HandlerType::StopPoll: {
|
2023-05-15 17:16:55 +02:00
|
|
|
if (!have_old_message_database) {
|
2019-04-03 11:26:20 +02:00
|
|
|
binlog_erase(G()->td_db()->get_binlog(), event.id_);
|
2019-02-22 16:09:55 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
StopPollLogEvent log_event;
|
2023-02-02 01:08:26 +01:00
|
|
|
log_event_parse(log_event, event.get_data()).ensure();
|
2019-02-22 16:09:55 +01:00
|
|
|
|
2023-09-21 18:11:17 +02:00
|
|
|
auto dialog_id = log_event.message_full_id_.get_dialog_id();
|
2019-02-22 16:09:55 +01:00
|
|
|
|
|
|
|
Dependencies dependencies;
|
2022-03-11 13:10:24 +01:00
|
|
|
dependencies.add_dialog_dependencies(dialog_id); // do not load the dialog itself
|
|
|
|
dependencies.resolve_force(td_, "StopPollLogEvent");
|
2019-02-22 16:09:55 +01:00
|
|
|
|
2023-09-21 18:11:17 +02:00
|
|
|
do_stop_poll(log_event.poll_id_, log_event.message_full_id_, nullptr, event.id_, Auto());
|
2019-02-22 16:09:55 +01:00
|
|
|
break;
|
|
|
|
}
|
2019-02-21 15:40:37 +01:00
|
|
|
default:
|
2020-09-22 01:15:09 +02:00
|
|
|
LOG(FATAL) << "Unsupported log event type " << event.type_;
|
2019-02-21 15:40:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-19 14:45:32 +01:00
|
|
|
} // namespace td
|