2019-02-19 14:45:32 +01:00
|
|
|
//
|
2020-01-01 02:23:48 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2020
|
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)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "td/telegram/PollManager.h"
|
2020-01-11 01:46:26 +01:00
|
|
|
#include "td/telegram/Version.h"
|
2019-02-19 14:45:32 +01:00
|
|
|
|
|
|
|
#include "td/utils/common.h"
|
|
|
|
#include "td/utils/misc.h"
|
|
|
|
#include "td/utils/tl_helpers.h"
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
template <class StorerT>
|
2019-02-21 01:52:58 +01:00
|
|
|
void PollManager::PollOption::store(StorerT &storer) const {
|
2019-02-19 14:45:32 +01:00
|
|
|
using ::td::store;
|
|
|
|
BEGIN_STORE_FLAGS();
|
|
|
|
STORE_FLAG(is_chosen);
|
|
|
|
END_STORE_FLAGS();
|
|
|
|
|
|
|
|
store(text, storer);
|
|
|
|
store(data, storer);
|
|
|
|
store(voter_count, storer);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ParserT>
|
2019-02-21 01:52:58 +01:00
|
|
|
void PollManager::PollOption::parse(ParserT &parser) {
|
2019-02-19 14:45:32 +01:00
|
|
|
using ::td::parse;
|
|
|
|
BEGIN_PARSE_FLAGS();
|
|
|
|
PARSE_FLAG(is_chosen);
|
|
|
|
END_PARSE_FLAGS();
|
|
|
|
|
|
|
|
parse(text, parser);
|
|
|
|
parse(data, parser);
|
|
|
|
parse(voter_count, parser);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class StorerT>
|
|
|
|
void PollManager::Poll::store(StorerT &storer) const {
|
|
|
|
using ::td::store;
|
2020-01-11 01:46:26 +01:00
|
|
|
bool is_public = !is_anonymous;
|
2020-01-12 02:40:17 +01:00
|
|
|
bool has_recent_voters = !recent_voter_user_ids.empty();
|
2020-04-03 23:05:35 +02:00
|
|
|
bool has_open_period = open_period != 0;
|
2020-04-03 15:50:10 +02:00
|
|
|
bool has_close_date = close_date != 0;
|
2020-04-03 22:39:50 +02:00
|
|
|
bool has_explanation = !explanation.text.empty();
|
2019-02-19 14:45:32 +01:00
|
|
|
BEGIN_STORE_FLAGS();
|
|
|
|
STORE_FLAG(is_closed);
|
2020-01-11 01:46:26 +01:00
|
|
|
STORE_FLAG(is_public);
|
|
|
|
STORE_FLAG(allow_multiple_answers);
|
|
|
|
STORE_FLAG(is_quiz);
|
2020-01-12 02:40:17 +01:00
|
|
|
STORE_FLAG(has_recent_voters);
|
2020-04-03 23:05:35 +02:00
|
|
|
STORE_FLAG(has_open_period);
|
2020-04-03 15:50:10 +02:00
|
|
|
STORE_FLAG(has_close_date);
|
2020-04-03 22:39:50 +02:00
|
|
|
STORE_FLAG(has_explanation);
|
2020-04-05 04:35:52 +02:00
|
|
|
STORE_FLAG(is_updated_after_close);
|
2019-02-19 14:45:32 +01:00
|
|
|
END_STORE_FLAGS();
|
|
|
|
|
|
|
|
store(question, storer);
|
2019-02-21 01:52:58 +01:00
|
|
|
store(options, storer);
|
2019-02-19 14:45:32 +01:00
|
|
|
store(total_voter_count, storer);
|
2020-01-11 01:46:26 +01:00
|
|
|
if (is_quiz) {
|
|
|
|
store(correct_option_id, storer);
|
|
|
|
}
|
2020-01-12 02:40:17 +01:00
|
|
|
if (has_recent_voters) {
|
|
|
|
store(recent_voter_user_ids, storer);
|
|
|
|
}
|
2020-04-03 23:05:35 +02:00
|
|
|
if (has_open_period) {
|
|
|
|
store(open_period, storer);
|
|
|
|
}
|
2020-04-03 15:50:10 +02:00
|
|
|
if (has_close_date) {
|
|
|
|
store(close_date, storer);
|
|
|
|
}
|
2020-04-03 22:39:50 +02:00
|
|
|
if (has_explanation) {
|
|
|
|
store(explanation, storer);
|
|
|
|
}
|
2019-02-19 14:45:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ParserT>
|
|
|
|
void PollManager::Poll::parse(ParserT &parser) {
|
|
|
|
using ::td::parse;
|
2020-01-11 01:46:26 +01:00
|
|
|
bool is_public;
|
2020-01-12 02:40:17 +01:00
|
|
|
bool has_recent_voters;
|
2020-04-03 23:05:35 +02:00
|
|
|
bool has_open_period;
|
2020-04-03 15:50:10 +02:00
|
|
|
bool has_close_date;
|
2020-04-03 22:39:50 +02:00
|
|
|
bool has_explanation;
|
2019-02-19 14:45:32 +01:00
|
|
|
BEGIN_PARSE_FLAGS();
|
|
|
|
PARSE_FLAG(is_closed);
|
2020-01-11 01:46:26 +01:00
|
|
|
PARSE_FLAG(is_public);
|
|
|
|
PARSE_FLAG(allow_multiple_answers);
|
|
|
|
PARSE_FLAG(is_quiz);
|
2020-01-12 02:40:17 +01:00
|
|
|
PARSE_FLAG(has_recent_voters);
|
2020-04-03 23:05:35 +02:00
|
|
|
PARSE_FLAG(has_open_period);
|
2020-04-03 15:50:10 +02:00
|
|
|
PARSE_FLAG(has_close_date);
|
2020-04-03 22:39:50 +02:00
|
|
|
PARSE_FLAG(has_explanation);
|
2020-04-05 04:35:52 +02:00
|
|
|
PARSE_FLAG(is_updated_after_close);
|
2019-02-19 14:45:32 +01:00
|
|
|
END_PARSE_FLAGS();
|
2020-01-11 01:46:26 +01:00
|
|
|
is_anonymous = !is_public;
|
2019-02-19 14:45:32 +01:00
|
|
|
|
|
|
|
parse(question, parser);
|
2019-02-21 01:52:58 +01:00
|
|
|
parse(options, parser);
|
2019-02-19 14:45:32 +01:00
|
|
|
parse(total_voter_count, parser);
|
2020-01-11 01:46:26 +01:00
|
|
|
if (is_quiz) {
|
|
|
|
parse(correct_option_id, parser);
|
2020-01-13 23:51:03 +01:00
|
|
|
if (correct_option_id < -1 || correct_option_id >= static_cast<int32>(options.size())) {
|
2020-01-11 01:46:26 +01:00
|
|
|
parser.set_error("Wrong correct_option_id");
|
|
|
|
}
|
|
|
|
}
|
2020-01-12 02:40:17 +01:00
|
|
|
if (has_recent_voters) {
|
|
|
|
parse(recent_voter_user_ids, parser);
|
|
|
|
}
|
2020-04-03 23:05:35 +02:00
|
|
|
if (has_open_period) {
|
|
|
|
parse(open_period, parser);
|
|
|
|
}
|
2020-04-03 15:50:10 +02:00
|
|
|
if (has_close_date) {
|
|
|
|
parse(close_date, parser);
|
|
|
|
}
|
2020-04-03 22:39:50 +02:00
|
|
|
if (has_explanation) {
|
|
|
|
parse(explanation, parser);
|
|
|
|
}
|
2019-02-19 14:45:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class StorerT>
|
|
|
|
void PollManager::store_poll(PollId poll_id, StorerT &storer) const {
|
|
|
|
td::store(poll_id.get(), storer);
|
|
|
|
if (is_local_poll_id(poll_id)) {
|
|
|
|
auto poll = get_poll(poll_id);
|
|
|
|
CHECK(poll != nullptr);
|
2020-04-03 23:05:35 +02:00
|
|
|
bool has_open_period = poll->open_period != 0;
|
2020-04-03 15:50:10 +02:00
|
|
|
bool has_close_date = poll->close_date != 0;
|
2020-04-03 22:39:50 +02:00
|
|
|
bool has_explanation = !poll->explanation.text.empty();
|
2020-01-11 01:46:26 +01:00
|
|
|
BEGIN_STORE_FLAGS();
|
|
|
|
STORE_FLAG(poll->is_closed);
|
|
|
|
STORE_FLAG(poll->is_anonymous);
|
|
|
|
STORE_FLAG(poll->allow_multiple_answers);
|
|
|
|
STORE_FLAG(poll->is_quiz);
|
2020-04-03 23:05:35 +02:00
|
|
|
STORE_FLAG(has_open_period);
|
2020-04-03 15:50:10 +02:00
|
|
|
STORE_FLAG(has_close_date);
|
2020-04-03 22:39:50 +02:00
|
|
|
STORE_FLAG(has_explanation);
|
2020-01-11 01:46:26 +01:00
|
|
|
END_STORE_FLAGS();
|
2019-02-19 14:45:32 +01:00
|
|
|
store(poll->question, storer);
|
2019-02-21 01:52:58 +01:00
|
|
|
vector<string> options = transform(poll->options, [](const PollOption &option) { return option.text; });
|
|
|
|
store(options, storer);
|
2020-01-11 01:46:26 +01:00
|
|
|
if (poll->is_quiz) {
|
|
|
|
store(poll->correct_option_id, storer);
|
|
|
|
}
|
2020-04-03 23:05:35 +02:00
|
|
|
if (has_open_period) {
|
|
|
|
store(poll->open_period, storer);
|
|
|
|
}
|
2020-04-03 15:50:10 +02:00
|
|
|
if (has_close_date) {
|
|
|
|
store(poll->close_date, storer);
|
|
|
|
}
|
2020-04-03 22:39:50 +02:00
|
|
|
if (has_explanation) {
|
|
|
|
store(poll->explanation, storer);
|
|
|
|
}
|
2019-02-19 14:45:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ParserT>
|
|
|
|
PollId PollManager::parse_poll(ParserT &parser) {
|
|
|
|
int64 poll_id_int;
|
|
|
|
td::parse(poll_id_int, parser);
|
|
|
|
PollId poll_id(poll_id_int);
|
|
|
|
if (is_local_poll_id(poll_id)) {
|
|
|
|
string question;
|
2019-02-21 01:52:58 +01:00
|
|
|
vector<string> options;
|
2020-04-03 22:39:50 +02:00
|
|
|
FormattedText explanation;
|
2020-04-03 23:05:35 +02:00
|
|
|
int32 open_period = 0;
|
2020-04-03 15:50:10 +02:00
|
|
|
int32 close_date = 0;
|
2020-01-11 01:46:26 +01:00
|
|
|
bool is_closed = false;
|
|
|
|
bool is_anonymous = true;
|
|
|
|
bool allow_multiple_answers = false;
|
|
|
|
bool is_quiz = false;
|
2020-04-03 23:05:35 +02:00
|
|
|
bool has_open_period = false;
|
2020-04-03 15:50:10 +02:00
|
|
|
bool has_close_date = false;
|
2020-04-03 22:39:50 +02:00
|
|
|
bool has_explanation = false;
|
2020-01-11 01:46:26 +01:00
|
|
|
int32 correct_option_id = -1;
|
|
|
|
|
|
|
|
if (parser.version() >= static_cast<int32>(Version::SupportPolls2_0)) {
|
|
|
|
BEGIN_PARSE_FLAGS();
|
|
|
|
PARSE_FLAG(is_closed);
|
|
|
|
PARSE_FLAG(is_anonymous);
|
|
|
|
PARSE_FLAG(allow_multiple_answers);
|
|
|
|
PARSE_FLAG(is_quiz);
|
2020-04-03 23:05:35 +02:00
|
|
|
PARSE_FLAG(has_open_period);
|
2020-04-03 15:50:10 +02:00
|
|
|
PARSE_FLAG(has_close_date);
|
2020-04-03 22:39:50 +02:00
|
|
|
PARSE_FLAG(has_explanation);
|
2020-01-11 01:46:26 +01:00
|
|
|
END_PARSE_FLAGS();
|
|
|
|
}
|
2019-02-19 14:45:32 +01:00
|
|
|
parse(question, parser);
|
2019-02-21 01:52:58 +01:00
|
|
|
parse(options, parser);
|
2020-01-11 01:46:26 +01:00
|
|
|
if (is_quiz) {
|
|
|
|
parse(correct_option_id, parser);
|
2020-01-13 23:51:03 +01:00
|
|
|
if (correct_option_id < -1 || correct_option_id >= static_cast<int32>(options.size())) {
|
2020-01-11 01:46:26 +01:00
|
|
|
parser.set_error("Wrong correct_option_id");
|
|
|
|
}
|
|
|
|
}
|
2020-04-03 23:05:35 +02:00
|
|
|
if (has_open_period) {
|
|
|
|
parse(open_period, parser);
|
|
|
|
}
|
2020-04-03 15:50:10 +02:00
|
|
|
if (has_close_date) {
|
|
|
|
parse(close_date, parser);
|
|
|
|
}
|
2020-04-03 22:39:50 +02:00
|
|
|
if (has_explanation) {
|
|
|
|
parse(explanation, parser);
|
|
|
|
}
|
2019-08-01 02:58:49 +02:00
|
|
|
if (parser.get_error() != nullptr) {
|
|
|
|
return PollId();
|
|
|
|
}
|
2020-01-11 01:46:26 +01:00
|
|
|
return create_poll(std::move(question), std::move(options), is_anonymous, allow_multiple_answers, is_quiz,
|
2020-04-03 23:05:35 +02:00
|
|
|
correct_option_id, std::move(explanation), open_period, close_date, is_closed);
|
2019-02-19 14:45:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
auto poll = get_poll_force(poll_id);
|
|
|
|
if (poll == nullptr) {
|
|
|
|
return PollId();
|
|
|
|
}
|
|
|
|
return poll_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|