tdlight/td/telegram/PollManager.hpp
levlam 5ef99afec7 Add messagePoll support.
GitOrigin-RevId: 41b93b2708285e4051fc01b856aa14a8c8c5c692
2019-02-19 16:45:32 +03:00

98 lines
2.3 KiB
C++

//
// 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
#include "td/telegram/PollManager.h"
#include "td/utils/common.h"
#include "td/utils/misc.h"
#include "td/utils/tl_helpers.h"
namespace td {
template <class StorerT>
void PollManager::PollAnswer::store(StorerT &storer) const {
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>
void PollManager::PollAnswer::parse(ParserT &parser) {
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;
BEGIN_STORE_FLAGS();
STORE_FLAG(is_closed);
END_STORE_FLAGS();
store(question, storer);
store(answers, storer);
store(total_voter_count, storer);
}
template <class ParserT>
void PollManager::Poll::parse(ParserT &parser) {
using ::td::parse;
BEGIN_PARSE_FLAGS();
PARSE_FLAG(is_closed);
END_PARSE_FLAGS();
parse(question, parser);
parse(answers, parser);
parse(total_voter_count, parser);
}
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);
store(poll->question, storer);
vector<string> answers = transform(poll->answers, [](const PollAnswer &answer) { return answer.text; });
store(answers, storer);
}
}
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;
vector<string> answers;
parse(question, parser);
parse(answers, parser);
return create_poll(std::move(question), std::move(answers));
}
auto poll = get_poll_force(poll_id);
if (poll == nullptr) {
return PollId();
}
return poll_id;
}
} // namespace td