2019-02-19 14:45:32 +01:00
|
|
|
//
|
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2019
|
|
|
|
//
|
|
|
|
// 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)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
2019-02-19 16:42:59 +01:00
|
|
|
#include "td/telegram/MessageId.h"
|
2019-02-21 13:42:54 +01:00
|
|
|
#include "td/telegram/net/NetQuery.h"
|
2019-02-19 14:45:32 +01:00
|
|
|
#include "td/telegram/PollId.h"
|
|
|
|
#include "td/telegram/td_api.h"
|
|
|
|
#include "td/telegram/telegram_api.h"
|
|
|
|
|
|
|
|
#include "td/actor/actor.h"
|
|
|
|
#include "td/actor/PromiseFuture.h"
|
2019-02-22 20:53:02 +01:00
|
|
|
#include "td/actor/Timeout.h"
|
2019-02-19 14:45:32 +01:00
|
|
|
|
|
|
|
#include "td/utils/common.h"
|
2019-02-22 21:15:43 +01:00
|
|
|
#include "td/utils/Status.h"
|
2019-02-19 14:45:32 +01:00
|
|
|
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <unordered_set>
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
2019-02-21 15:40:37 +01:00
|
|
|
struct BinlogEvent;
|
|
|
|
|
2019-02-19 14:45:32 +01:00
|
|
|
class Td;
|
|
|
|
|
|
|
|
class PollManager : public Actor {
|
|
|
|
public:
|
|
|
|
PollManager(Td *td, ActorShared<> parent);
|
|
|
|
|
|
|
|
PollManager(const PollManager &) = delete;
|
|
|
|
PollManager &operator=(const PollManager &) = delete;
|
|
|
|
PollManager(PollManager &&) = delete;
|
|
|
|
PollManager &operator=(PollManager &&) = delete;
|
|
|
|
~PollManager() override;
|
|
|
|
|
2019-02-22 20:53:02 +01:00
|
|
|
static bool is_local_poll_id(PollId poll_id);
|
|
|
|
|
2019-02-21 01:52:58 +01:00
|
|
|
PollId create_poll(string &&question, vector<string> &&options);
|
2019-02-19 14:45:32 +01:00
|
|
|
|
2019-02-19 16:42:59 +01:00
|
|
|
void register_poll(PollId poll_id, FullMessageId full_message_id);
|
|
|
|
|
|
|
|
void unregister_poll(PollId poll_id, FullMessageId full_message_id);
|
|
|
|
|
2019-02-22 16:09:55 +01:00
|
|
|
bool get_poll_is_closed(PollId poll_id) const;
|
|
|
|
|
2019-03-03 04:36:30 +01:00
|
|
|
string get_poll_search_text(PollId poll_id) const;
|
|
|
|
|
2019-02-21 13:23:05 +01:00
|
|
|
void set_poll_answer(PollId poll_id, FullMessageId full_message_id, vector<int32> &&option_ids,
|
|
|
|
Promise<Unit> &&promise);
|
|
|
|
|
2019-02-22 16:09:55 +01:00
|
|
|
void stop_poll(PollId poll_id, FullMessageId full_message_id, Promise<Unit> &&promise);
|
|
|
|
|
|
|
|
void stop_local_poll(PollId poll_id);
|
2019-02-19 15:06:48 +01:00
|
|
|
|
2019-02-19 14:45:32 +01:00
|
|
|
tl_object_ptr<telegram_api::InputMedia> get_input_media(PollId poll_id) const;
|
|
|
|
|
|
|
|
PollId on_get_poll(PollId poll_id, tl_object_ptr<telegram_api::poll> &&poll_server,
|
|
|
|
tl_object_ptr<telegram_api::pollResults> &&poll_results);
|
|
|
|
|
|
|
|
td_api::object_ptr<td_api::poll> get_poll_object(PollId poll_id) const;
|
|
|
|
|
2019-02-21 15:40:37 +01:00
|
|
|
void on_binlog_events(vector<BinlogEvent> &&events);
|
|
|
|
|
2019-03-05 21:21:10 +01:00
|
|
|
static vector<int32> get_vote_percentage(const vector<int32> &voter_counts, int32 total_voter_count);
|
|
|
|
|
2019-02-19 14:45:32 +01:00
|
|
|
template <class StorerT>
|
|
|
|
void store_poll(PollId poll_id, StorerT &storer) const;
|
|
|
|
|
|
|
|
template <class ParserT>
|
|
|
|
PollId parse_poll(ParserT &parser);
|
|
|
|
|
|
|
|
private:
|
2019-02-21 01:52:58 +01:00
|
|
|
struct PollOption {
|
2019-02-19 14:45:32 +01:00
|
|
|
string text;
|
|
|
|
string data;
|
|
|
|
int32 voter_count = 0;
|
|
|
|
bool is_chosen = false;
|
|
|
|
|
|
|
|
template <class StorerT>
|
|
|
|
void store(StorerT &storer) const;
|
|
|
|
template <class ParserT>
|
|
|
|
void parse(ParserT &parser);
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Poll {
|
|
|
|
string question;
|
2019-02-21 01:52:58 +01:00
|
|
|
vector<PollOption> options;
|
2019-02-19 14:45:32 +01:00
|
|
|
int32 total_voter_count = 0;
|
|
|
|
bool is_closed = false;
|
|
|
|
|
|
|
|
template <class StorerT>
|
|
|
|
void store(StorerT &storer) const;
|
|
|
|
template <class ParserT>
|
|
|
|
void parse(ParserT &parser);
|
|
|
|
};
|
|
|
|
|
2019-02-21 15:40:37 +01:00
|
|
|
class SetPollAnswerLogEvent;
|
2019-02-22 16:09:55 +01:00
|
|
|
class StopPollLogEvent;
|
2019-02-21 15:40:37 +01:00
|
|
|
|
2019-02-22 20:53:02 +01:00
|
|
|
void start_up() override;
|
2019-02-19 14:45:32 +01:00
|
|
|
void tear_down() override;
|
|
|
|
|
2019-02-22 20:53:02 +01:00
|
|
|
static void on_update_poll_timeout_callback(void *poll_manager_ptr, int64 poll_id_int);
|
2019-02-19 14:45:32 +01:00
|
|
|
|
2019-02-23 18:59:29 +01:00
|
|
|
static td_api::object_ptr<td_api::pollOption> get_poll_option_object(const PollOption &poll_option);
|
2019-02-19 14:45:32 +01:00
|
|
|
|
2019-02-21 01:52:58 +01:00
|
|
|
static telegram_api::object_ptr<telegram_api::pollAnswer> get_input_poll_option(const PollOption &poll_option);
|
2019-02-19 14:45:32 +01:00
|
|
|
|
2019-02-21 01:52:58 +01:00
|
|
|
static vector<PollOption> get_poll_options(vector<tl_object_ptr<telegram_api::pollAnswer>> &&poll_options);
|
2019-02-19 14:45:32 +01:00
|
|
|
|
|
|
|
bool have_poll(PollId poll_id) const;
|
|
|
|
|
|
|
|
bool have_poll_force(PollId poll_id);
|
|
|
|
|
|
|
|
const Poll *get_poll(PollId poll_id) const;
|
|
|
|
|
|
|
|
Poll *get_poll_editable(PollId poll_id);
|
|
|
|
|
2019-02-20 23:54:31 +01:00
|
|
|
void notify_on_poll_update(PollId poll_id);
|
|
|
|
|
2019-02-19 14:45:32 +01:00
|
|
|
static string get_poll_database_key(PollId poll_id);
|
|
|
|
|
|
|
|
void save_poll(const Poll *poll, PollId poll_id);
|
|
|
|
|
|
|
|
void on_load_poll_from_database(PollId poll_id, string value);
|
|
|
|
|
2019-02-22 20:53:02 +01:00
|
|
|
double get_polling_timeout() const;
|
|
|
|
|
|
|
|
void on_update_poll_timeout(PollId poll_id);
|
|
|
|
|
|
|
|
void on_online();
|
|
|
|
|
2019-02-19 14:45:32 +01:00
|
|
|
Poll *get_poll_force(PollId poll_id);
|
|
|
|
|
2019-03-29 03:16:28 +01:00
|
|
|
td_api::object_ptr<td_api::poll> get_poll_object(PollId poll_id, const Poll *poll) const;
|
|
|
|
|
2019-03-10 08:57:15 +01:00
|
|
|
void on_get_poll_results(PollId poll_id, uint64 generation, Result<tl_object_ptr<telegram_api::Updates>> result);
|
2019-03-09 19:07:07 +01:00
|
|
|
|
2019-02-21 13:23:05 +01:00
|
|
|
void do_set_poll_answer(PollId poll_id, FullMessageId full_message_id, vector<string> &&options, uint64 logevent_id,
|
|
|
|
Promise<Unit> &&promise);
|
|
|
|
|
2019-03-11 17:06:53 +01:00
|
|
|
void 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
|
|
|
|
2019-02-22 16:09:55 +01:00
|
|
|
void do_stop_poll(PollId poll_id, FullMessageId full_message_id, uint64 logevent_id, Promise<Unit> &&promise);
|
|
|
|
|
2019-02-22 20:53:02 +01:00
|
|
|
MultiTimeout update_poll_timeout_{"UpdatePollTimeout"};
|
|
|
|
|
2019-02-19 14:45:32 +01:00
|
|
|
Td *td_;
|
|
|
|
ActorShared<> parent_;
|
|
|
|
std::unordered_map<PollId, unique_ptr<Poll>, PollIdHash> polls_;
|
|
|
|
|
2019-02-19 16:42:59 +01:00
|
|
|
std::unordered_map<PollId, std::unordered_set<FullMessageId, FullMessageIdHash>, PollIdHash> poll_messages_;
|
|
|
|
|
2019-02-21 13:23:05 +01:00
|
|
|
struct PendingPollAnswer {
|
|
|
|
vector<string> options_;
|
|
|
|
vector<Promise<Unit>> promises_;
|
|
|
|
uint64 generation_ = 0;
|
|
|
|
uint64 logevent_id_ = 0;
|
2019-02-21 13:42:54 +01:00
|
|
|
NetQueryRef query_ref_;
|
2019-02-21 13:23:05 +01:00
|
|
|
};
|
|
|
|
std::unordered_map<PollId, PendingPollAnswer, PollIdHash> pending_answers_;
|
|
|
|
|
2019-02-19 14:45:32 +01:00
|
|
|
int64 current_local_poll_id_ = 0;
|
|
|
|
|
2019-02-21 13:23:05 +01:00
|
|
|
uint64 current_generation_ = 0;
|
|
|
|
|
2019-02-19 14:45:32 +01:00
|
|
|
std::unordered_set<PollId, PollIdHash> loaded_from_database_polls_;
|
2019-04-09 12:25:29 +02:00
|
|
|
|
|
|
|
std::unordered_set<PollId, PollIdHash> being_closed_polls_;
|
2019-02-19 14:45:32 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace td
|