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)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
2024-08-01 20:33:52 +02:00
|
|
|
#include "td/telegram/MessageEntity.hpp"
|
2023-04-25 15:41:04 +02:00
|
|
|
#include "td/telegram/MinChannel.hpp"
|
2019-02-19 14:45:32 +01:00
|
|
|
#include "td/telegram/PollManager.h"
|
2023-07-01 15:22:01 +02:00
|
|
|
#include "td/telegram/UserId.h"
|
2020-01-11 01:46:26 +01:00
|
|
|
#include "td/telegram/Version.h"
|
2019-02-19 14:45:32 +01:00
|
|
|
|
2021-01-01 13:59:53 +01:00
|
|
|
#include "td/utils/algorithm.h"
|
2019-02-19 14:45:32 +01:00
|
|
|
#include "td/utils/common.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;
|
2024-04-23 02:10:58 +02:00
|
|
|
bool has_entities = !text_.entities.empty();
|
2019-02-19 14:45:32 +01:00
|
|
|
BEGIN_STORE_FLAGS();
|
2023-04-25 14:16:29 +02:00
|
|
|
STORE_FLAG(is_chosen_);
|
2024-04-23 02:10:58 +02:00
|
|
|
STORE_FLAG(has_entities);
|
2019-02-19 14:45:32 +01:00
|
|
|
END_STORE_FLAGS();
|
|
|
|
|
2024-04-23 02:10:58 +02:00
|
|
|
store(text_.text, storer);
|
2023-04-25 14:16:29 +02:00
|
|
|
store(data_, storer);
|
|
|
|
store(voter_count_, storer);
|
2024-04-23 02:10:58 +02:00
|
|
|
if (has_entities) {
|
|
|
|
store(text_.entities, storer);
|
|
|
|
}
|
2019-02-19 14:45:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2024-04-23 02:10:58 +02:00
|
|
|
bool has_entities;
|
2019-02-19 14:45:32 +01:00
|
|
|
BEGIN_PARSE_FLAGS();
|
2023-04-25 14:16:29 +02:00
|
|
|
PARSE_FLAG(is_chosen_);
|
2024-04-23 02:10:58 +02:00
|
|
|
PARSE_FLAG(has_entities);
|
2019-02-19 14:45:32 +01:00
|
|
|
END_PARSE_FLAGS();
|
|
|
|
|
2024-04-23 02:10:58 +02:00
|
|
|
parse(text_.text, parser);
|
2023-04-25 14:16:29 +02:00
|
|
|
parse(data_, parser);
|
|
|
|
parse(voter_count_, parser);
|
2024-04-23 02:10:58 +02:00
|
|
|
if (has_entities) {
|
|
|
|
parse(text_.entities, parser);
|
|
|
|
}
|
2019-02-19 14:45:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class StorerT>
|
|
|
|
void PollManager::Poll::store(StorerT &storer) const {
|
|
|
|
using ::td::store;
|
2023-04-25 14:29:24 +02:00
|
|
|
bool is_public = !is_anonymous_;
|
|
|
|
bool has_open_period = open_period_ != 0;
|
|
|
|
bool has_close_date = close_date_ != 0;
|
|
|
|
bool has_explanation = !explanation_.text.empty();
|
2023-04-25 15:41:04 +02:00
|
|
|
bool has_recent_voter_dialog_ids = !recent_voter_dialog_ids_.empty();
|
|
|
|
bool has_recent_voter_min_channels = !recent_voter_min_channels_.empty();
|
2024-04-23 02:10:58 +02:00
|
|
|
bool has_question_entities = !question_.entities.empty();
|
2019-02-19 14:45:32 +01:00
|
|
|
BEGIN_STORE_FLAGS();
|
2023-04-25 14:29:24 +02:00
|
|
|
STORE_FLAG(is_closed_);
|
2020-01-11 01:46:26 +01:00
|
|
|
STORE_FLAG(is_public);
|
2023-04-25 14:29:24 +02:00
|
|
|
STORE_FLAG(allow_multiple_answers_);
|
|
|
|
STORE_FLAG(is_quiz_);
|
2023-04-25 15:41:04 +02:00
|
|
|
STORE_FLAG(false);
|
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);
|
2023-04-25 14:29:24 +02:00
|
|
|
STORE_FLAG(is_updated_after_close_);
|
2023-04-25 15:41:04 +02:00
|
|
|
STORE_FLAG(has_recent_voter_dialog_ids);
|
|
|
|
STORE_FLAG(has_recent_voter_min_channels);
|
2024-04-23 02:10:58 +02:00
|
|
|
STORE_FLAG(has_question_entities);
|
2019-02-19 14:45:32 +01:00
|
|
|
END_STORE_FLAGS();
|
|
|
|
|
2024-04-23 02:10:58 +02:00
|
|
|
store(question_.text, storer);
|
2023-04-25 14:29:24 +02:00
|
|
|
store(options_, storer);
|
|
|
|
store(total_voter_count_, storer);
|
|
|
|
if (is_quiz_) {
|
|
|
|
store(correct_option_id_, storer);
|
2020-01-11 01:46:26 +01:00
|
|
|
}
|
2020-04-03 23:05:35 +02:00
|
|
|
if (has_open_period) {
|
2023-04-25 14:29:24 +02:00
|
|
|
store(open_period_, storer);
|
2020-04-03 23:05:35 +02:00
|
|
|
}
|
2020-04-03 15:50:10 +02:00
|
|
|
if (has_close_date) {
|
2023-04-25 14:29:24 +02:00
|
|
|
store(close_date_, storer);
|
2020-04-03 15:50:10 +02:00
|
|
|
}
|
2020-04-03 22:39:50 +02:00
|
|
|
if (has_explanation) {
|
2023-04-25 14:29:24 +02:00
|
|
|
store(explanation_, storer);
|
2020-04-03 22:39:50 +02:00
|
|
|
}
|
2023-04-25 15:41:04 +02:00
|
|
|
if (has_recent_voter_dialog_ids) {
|
|
|
|
store(recent_voter_dialog_ids_, storer);
|
|
|
|
}
|
|
|
|
if (has_recent_voter_min_channels) {
|
|
|
|
store(recent_voter_min_channels_, storer);
|
|
|
|
}
|
2024-04-23 02:10:58 +02:00
|
|
|
if (has_question_entities) {
|
|
|
|
store(question_.entities, 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;
|
2023-04-25 15:41:04 +02:00
|
|
|
bool has_recent_voter_user_ids;
|
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;
|
2023-04-25 15:41:04 +02:00
|
|
|
bool has_recent_voter_dialog_ids;
|
|
|
|
bool has_recent_voter_min_channels;
|
2024-04-23 02:10:58 +02:00
|
|
|
bool has_question_entities;
|
2019-02-19 14:45:32 +01:00
|
|
|
BEGIN_PARSE_FLAGS();
|
2023-04-25 14:29:24 +02:00
|
|
|
PARSE_FLAG(is_closed_);
|
2020-01-11 01:46:26 +01:00
|
|
|
PARSE_FLAG(is_public);
|
2023-04-25 14:29:24 +02:00
|
|
|
PARSE_FLAG(allow_multiple_answers_);
|
|
|
|
PARSE_FLAG(is_quiz_);
|
2023-04-25 15:41:04 +02:00
|
|
|
PARSE_FLAG(has_recent_voter_user_ids);
|
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);
|
2023-04-25 14:29:24 +02:00
|
|
|
PARSE_FLAG(is_updated_after_close_);
|
2023-04-25 15:41:04 +02:00
|
|
|
PARSE_FLAG(has_recent_voter_dialog_ids);
|
|
|
|
PARSE_FLAG(has_recent_voter_min_channels);
|
2024-04-23 02:10:58 +02:00
|
|
|
PARSE_FLAG(has_question_entities);
|
2019-02-19 14:45:32 +01:00
|
|
|
END_PARSE_FLAGS();
|
2023-04-25 14:29:24 +02:00
|
|
|
is_anonymous_ = !is_public;
|
2019-02-19 14:45:32 +01:00
|
|
|
|
2024-04-23 02:10:58 +02:00
|
|
|
parse(question_.text, parser);
|
2023-04-25 14:29:24 +02:00
|
|
|
parse(options_, parser);
|
|
|
|
parse(total_voter_count_, parser);
|
|
|
|
if (is_quiz_) {
|
|
|
|
parse(correct_option_id_, parser);
|
|
|
|
if (correct_option_id_ < -1 || correct_option_id_ >= static_cast<int32>(options_.size())) {
|
2024-05-01 16:49:24 +02:00
|
|
|
parser.set_error("Wrong quiz correct_option_id");
|
2020-01-11 01:46:26 +01:00
|
|
|
}
|
|
|
|
}
|
2023-04-25 15:41:04 +02:00
|
|
|
if (has_recent_voter_user_ids) {
|
|
|
|
vector<UserId> recent_voter_user_ids;
|
|
|
|
parse(recent_voter_user_ids, parser);
|
|
|
|
recent_voter_dialog_ids_ = transform(recent_voter_user_ids, [](UserId user_id) { return DialogId(user_id); });
|
2020-01-12 02:40:17 +01:00
|
|
|
}
|
2020-04-03 23:05:35 +02:00
|
|
|
if (has_open_period) {
|
2023-04-25 14:29:24 +02:00
|
|
|
parse(open_period_, parser);
|
2020-04-03 23:05:35 +02:00
|
|
|
}
|
2020-04-03 15:50:10 +02:00
|
|
|
if (has_close_date) {
|
2023-04-25 14:29:24 +02:00
|
|
|
parse(close_date_, parser);
|
2020-04-03 15:50:10 +02:00
|
|
|
}
|
2020-04-03 22:39:50 +02:00
|
|
|
if (has_explanation) {
|
2023-04-25 14:29:24 +02:00
|
|
|
parse(explanation_, parser);
|
2020-04-03 22:39:50 +02:00
|
|
|
}
|
2023-04-25 15:41:04 +02:00
|
|
|
if (has_recent_voter_dialog_ids) {
|
|
|
|
parse(recent_voter_dialog_ids_, parser);
|
|
|
|
}
|
|
|
|
if (has_recent_voter_min_channels) {
|
|
|
|
parse(recent_voter_min_channels_, parser);
|
|
|
|
}
|
2024-04-23 02:10:58 +02:00
|
|
|
if (has_question_entities) {
|
|
|
|
parse(question_.entities, 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);
|
2023-04-25 14:29:24 +02:00
|
|
|
bool has_open_period = poll->open_period_ != 0;
|
|
|
|
bool has_close_date = poll->close_date_ != 0;
|
|
|
|
bool has_explanation = !poll->explanation_.text.empty();
|
2024-04-23 02:10:58 +02:00
|
|
|
bool has_question_entities = !poll->question_.entities.empty();
|
|
|
|
bool has_option_entities =
|
2024-04-30 19:48:24 +02:00
|
|
|
any_of(poll->options_, [](const auto &option) { return !option.text_.entities.empty(); });
|
2020-01-11 01:46:26 +01:00
|
|
|
BEGIN_STORE_FLAGS();
|
2023-04-25 14:29:24 +02:00
|
|
|
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);
|
2024-04-23 02:10:58 +02:00
|
|
|
STORE_FLAG(has_question_entities);
|
|
|
|
STORE_FLAG(has_option_entities);
|
2020-01-11 01:46:26 +01:00
|
|
|
END_STORE_FLAGS();
|
2024-04-23 02:10:58 +02:00
|
|
|
store(poll->question_.text, storer);
|
|
|
|
vector<string> options = transform(poll->options_, [](const PollOption &option) { return option.text_.text; });
|
2019-02-21 01:52:58 +01:00
|
|
|
store(options, storer);
|
2023-04-25 14:29:24 +02:00
|
|
|
if (poll->is_quiz_) {
|
|
|
|
store(poll->correct_option_id_, storer);
|
2020-01-11 01:46:26 +01:00
|
|
|
}
|
2020-04-03 23:05:35 +02:00
|
|
|
if (has_open_period) {
|
2023-04-25 14:29:24 +02:00
|
|
|
store(poll->open_period_, storer);
|
2020-04-03 23:05:35 +02:00
|
|
|
}
|
2020-04-03 15:50:10 +02:00
|
|
|
if (has_close_date) {
|
2023-04-25 14:29:24 +02:00
|
|
|
store(poll->close_date_, storer);
|
2020-04-03 15:50:10 +02:00
|
|
|
}
|
2020-04-03 22:39:50 +02:00
|
|
|
if (has_explanation) {
|
2023-04-25 14:29:24 +02:00
|
|
|
store(poll->explanation_, storer);
|
2020-04-03 22:39:50 +02:00
|
|
|
}
|
2024-04-23 02:10:58 +02:00
|
|
|
if (has_question_entities) {
|
|
|
|
store(poll->question_.entities, storer);
|
|
|
|
}
|
|
|
|
if (has_option_entities) {
|
|
|
|
auto option_entities = transform(poll->options_, [](const PollOption &option) { return option.text_.entities; });
|
|
|
|
store(option_entities, 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)) {
|
2024-04-23 02:10:58 +02:00
|
|
|
FormattedText question;
|
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;
|
2024-04-23 02:10:58 +02:00
|
|
|
bool has_question_entities = false;
|
|
|
|
bool has_option_entities = 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);
|
2024-04-23 02:10:58 +02:00
|
|
|
PARSE_FLAG(has_question_entities);
|
|
|
|
PARSE_FLAG(has_option_entities);
|
2020-01-11 01:46:26 +01:00
|
|
|
END_PARSE_FLAGS();
|
|
|
|
}
|
2024-04-23 02:10:58 +02:00
|
|
|
parse(question.text, parser);
|
|
|
|
vector<string> option_texts;
|
|
|
|
parse(option_texts, parser);
|
2020-01-11 01:46:26 +01:00
|
|
|
if (is_quiz) {
|
|
|
|
parse(correct_option_id, parser);
|
2024-05-01 17:05:11 +02:00
|
|
|
if (correct_option_id < -1 || correct_option_id >= static_cast<int32>(option_texts.size())) {
|
2024-05-01 16:49:24 +02:00
|
|
|
parser.set_error("Wrong local quiz correct_option_id");
|
2020-01-11 01:46:26 +01:00
|
|
|
}
|
|
|
|
}
|
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);
|
|
|
|
}
|
2024-04-23 02:10:58 +02:00
|
|
|
if (has_question_entities) {
|
|
|
|
parse(question.entities, parser);
|
|
|
|
}
|
|
|
|
vector<vector<MessageEntity>> option_entities;
|
|
|
|
if (has_option_entities) {
|
|
|
|
parse(option_entities, parser);
|
|
|
|
CHECK(option_entities.size() == option_texts.size());
|
|
|
|
} else {
|
|
|
|
option_entities.resize(option_texts.size());
|
|
|
|
}
|
2024-05-01 17:05:11 +02:00
|
|
|
vector<FormattedText> options;
|
2024-04-23 02:10:58 +02:00
|
|
|
for (size_t i = 0; i < option_texts.size(); i++) {
|
|
|
|
options.push_back({std::move(option_texts[i]), std::move(option_entities[i])});
|
|
|
|
}
|
2024-05-01 17:05:11 +02:00
|
|
|
|
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
|