Return result of GetPollResultsQuery through Promise.

GitOrigin-RevId: fafc25acc4944066efb21f9dcc3000631fee0912
This commit is contained in:
levlam 2019-03-09 21:07:07 +03:00
parent 3d56c6dac0
commit 699bef0781
2 changed files with 23 additions and 14 deletions

View File

@ -44,10 +44,14 @@
namespace td { namespace td {
class GetPollResultsQuery : public Td::ResultHandler { class GetPollResultsQuery : public Td::ResultHandler {
Promise<tl_object_ptr<telegram_api::Updates>> promise_;
PollId poll_id_; PollId poll_id_;
DialogId dialog_id_; DialogId dialog_id_;
public: public:
explicit GetPollResultsQuery(Promise<tl_object_ptr<telegram_api::Updates>> &&promise) : promise_(std::move(promise)) {
}
void send(PollId poll_id, FullMessageId full_message_id) { void send(PollId poll_id, FullMessageId full_message_id) {
poll_id_ = poll_id; poll_id_ = poll_id;
dialog_id_ = full_message_id.get_dialog_id(); dialog_id_ = full_message_id.get_dialog_id();
@ -69,17 +73,14 @@ class GetPollResultsQuery : public Td::ResultHandler {
return on_error(id, result_ptr.move_as_error()); return on_error(id, result_ptr.move_as_error());
} }
auto result = result_ptr.move_as_ok(); promise_.set_value(result_ptr.move_as_ok());
LOG(INFO) << "Receive poll results: " << to_string(result);
td->updates_manager_->on_get_updates(std::move(result));
} }
void on_error(uint64 id, Status status) override { void on_error(uint64 id, Status status) override {
if (!td->messages_manager_->on_get_dialog_error(dialog_id_, status, "GetPollResultsQuery")) { if (!td->messages_manager_->on_get_dialog_error(dialog_id_, status, "GetPollResultsQuery")) {
LOG(ERROR) << "Receive " << status << ", while trying to get results of " << poll_id_; LOG(ERROR) << "Receive " << status << ", while trying to get results of " << poll_id_;
td->poll_manager_->on_get_poll_results_failed(poll_id_);
} }
promise_.set_error(std::move(status));
} }
}; };
@ -630,7 +631,6 @@ void PollManager::do_set_poll_answer(PollId poll_id, FullMessageId full_message_
auto query_promise = PromiseCreator::lambda([poll_id, generation, actor_id = actor_id(this)](Result<Unit> &&result) { auto query_promise = PromiseCreator::lambda([poll_id, generation, actor_id = actor_id(this)](Result<Unit> &&result) {
send_closure(actor_id, &PollManager::on_set_poll_answer, poll_id, generation, std::move(result)); send_closure(actor_id, &PollManager::on_set_poll_answer, poll_id, generation, std::move(result));
}); });
send_closure(td_->create_net_actor<SetPollAnswerActor>(std::move(query_promise)), &SetPollAnswerActor::send, send_closure(td_->create_net_actor<SetPollAnswerActor>(std::move(query_promise)), &SetPollAnswerActor::send,
full_message_id, std::move(sent_options), generation, &pending_answer.query_ref_); full_message_id, std::move(sent_options), generation, &pending_answer.query_ref_);
} }
@ -753,15 +753,24 @@ void PollManager::on_update_poll_timeout(PollId poll_id) {
auto full_message_id = *it->second.begin(); auto full_message_id = *it->second.begin();
LOG(INFO) << "Fetching results of " << poll_id << " from " << full_message_id; LOG(INFO) << "Fetching results of " << poll_id << " from " << full_message_id;
td_->create_handler<GetPollResultsQuery>()->send(poll_id, full_message_id); auto query_promise = PromiseCreator::lambda(
[poll_id, actor_id = actor_id(this)](Result<tl_object_ptr<telegram_api::Updates>> &&result) {
send_closure(actor_id, &PollManager::on_get_poll_results, poll_id, std::move(result));
});
td_->create_handler<GetPollResultsQuery>(std::move(query_promise))->send(poll_id, full_message_id);
} }
void PollManager::on_get_poll_results_failed(PollId poll_id) { void PollManager::on_get_poll_results(PollId poll_id, Result<tl_object_ptr<telegram_api::Updates>> result) {
if (!get_poll_is_closed(poll_id) && !td_->auth_manager_->is_bot()) { if (result.is_error()) {
auto timeout = get_polling_timeout(); if (!get_poll_is_closed(poll_id) && !td_->auth_manager_->is_bot()) {
LOG(INFO) << "Schedule updating of " << poll_id << " in " << timeout; auto timeout = get_polling_timeout();
update_poll_timeout_.add_timeout_in(poll_id.get(), timeout); LOG(INFO) << "Schedule updating of " << poll_id << " in " << timeout;
update_poll_timeout_.add_timeout_in(poll_id.get(), timeout);
}
return;
} }
td_->updates_manager_->on_get_updates(result.move_as_ok());
} }
void PollManager::on_online() { void PollManager::on_online() {

View File

@ -66,8 +66,6 @@ class PollManager : public Actor {
void on_binlog_events(vector<BinlogEvent> &&events); void on_binlog_events(vector<BinlogEvent> &&events);
void on_get_poll_results_failed(PollId poll_id);
static vector<int32> get_vote_percentage(const vector<int32> &voter_counts, int32 total_voter_count); static vector<int32> get_vote_percentage(const vector<int32> &voter_counts, int32 total_voter_count);
template <class StorerT> template <class StorerT>
@ -139,6 +137,8 @@ class PollManager : public Actor {
Poll *get_poll_force(PollId poll_id); Poll *get_poll_force(PollId poll_id);
void on_get_poll_results(PollId poll_id, Result<tl_object_ptr<telegram_api::Updates>> result);
void do_set_poll_answer(PollId poll_id, FullMessageId full_message_id, vector<string> &&options, uint64 logevent_id, void do_set_poll_answer(PollId poll_id, FullMessageId full_message_id, vector<string> &&options, uint64 logevent_id,
Promise<Unit> &&promise); Promise<Unit> &&promise);