Add setPollAnswer logevent.

GitOrigin-RevId: e73a3c5af859053fa9210eec2874077f47b00a12
This commit is contained in:
levlam 2019-02-21 17:40:37 +03:00
parent 4c6fc2be14
commit 2e144e6e3e
8 changed files with 91 additions and 13 deletions

View File

@ -717,6 +717,10 @@ class MessagesManager : public Actor {
void get_current_state(vector<td_api::object_ptr<td_api::Update>> &updates) const;
static void add_dialog_dependencies(Dependencies &dependencies, DialogId dialog_id);
void resolve_dependencies_force(const Dependencies &dependencies);
ActorOwn<MultiSequenceDispatcher> sequence_dispatcher_;
private:
@ -2027,10 +2031,6 @@ class MessagesManager : public Actor {
static void add_message_dependencies(Dependencies &dependencies, DialogId dialog_id, const Message *m);
static void add_dialog_dependencies(Dependencies &dependencies, DialogId dialog_id);
void resolve_dependencies_force(const Dependencies &dependencies);
void save_send_message_logevent(DialogId dialog_id, Message *m);
uint64 save_change_dialog_report_spam_state_on_server_logevent(DialogId dialog_id, bool is_spam_dialog);

View File

@ -10,12 +10,16 @@
#include "td/telegram/logevent/LogEvent.h"
#include "td/telegram/MessagesManager.h"
#include "td/telegram/net/NetActor.h"
#include "td/telegram/PollId.hpp"
#include "td/telegram/PollManager.hpp"
#include "td/telegram/SequenceDispatcher.h"
#include "td/telegram/TdDb.h"
#include "td/telegram/Td.h"
#include "td/telegram/TdParameters.h"
#include "td/telegram/UpdatesManager.h"
#include "td/db/binlog/BinlogEvent.h"
#include "td/db/binlog/BinlogHelper.h"
#include "td/db/SqliteKeyValue.h"
#include "td/db/SqliteKeyValueAsync.h"
@ -35,8 +39,7 @@ class SetPollAnswerQuery : public NetActorOnce {
explicit SetPollAnswerQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
}
void send(FullMessageId full_message_id, vector<BufferSlice> &&options, uint64 generation,
NetQueryRef *query_ref) {
void send(FullMessageId full_message_id, vector<BufferSlice> &&options, uint64 generation, NetQueryRef *query_ref) {
dialog_id_ = full_message_id.get_dialog_id();
auto input_peer = td->messages_manager_->get_input_peer(dialog_id_, AccessRights::Read);
if (input_peer == nullptr) {
@ -264,6 +267,27 @@ void PollManager::set_poll_answer(PollId poll_id, FullMessageId full_message_id,
do_set_poll_answer(poll_id, full_message_id, std::move(options), 0, std::move(promise));
}
class PollManager::SetPollAnswerLogEvent {
public:
PollId poll_id_;
FullMessageId full_message_id_;
vector<string> options_;
template <class StorerT>
void store(StorerT &storer) const {
td::store(poll_id_, storer);
td::store(full_message_id_, storer);
td::store(options_, storer);
}
template <class ParserT>
void parse(ParserT &parser) {
td::parse(poll_id_, parser);
td::parse(full_message_id_, parser);
td::parse(options_, parser);
}
};
void PollManager::do_set_poll_answer(PollId poll_id, FullMessageId full_message_id, vector<string> &&options,
uint64 logevent_id, Promise<Unit> &&promise) {
auto &pending_answer = pending_answers_[poll_id];
@ -272,8 +296,24 @@ void PollManager::do_set_poll_answer(PollId poll_id, FullMessageId full_message_
return;
}
CHECK(pending_answer.logevent_id_ == 0 || logevent_id == 0);
if (logevent_id == 0 && G()->parameters().use_message_db) {
// TODO add logevent or rewrite pending_answer.logevent_id_
SetPollAnswerLogEvent logevent;
logevent.poll_id_ = poll_id;
logevent.full_message_id_ = full_message_id;
logevent.options_ = options;
auto storer = LogEventStorerImpl<SetPollAnswerLogEvent>(logevent);
if (pending_answer.generation_ == 0) {
CHECK(pending_answer.logevent_id_ == 0);
logevent_id = binlog_add(G()->td_db()->get_binlog(), LogEvent::HandlerType::SetPollAnswer, storer);
LOG(INFO) << "Add set poll answer logevent " << logevent_id;
} else {
CHECK(pending_answer.logevent_id_ != 0);
logevent_id = pending_answer.logevent_id_;
auto new_logevent_id = binlog_rewrite(G()->td_db()->get_binlog(), pending_answer.logevent_id_,
LogEvent::HandlerType::SetPollAnswer, storer);
LOG(INFO) << "Rewrite set poll answer logevent " << logevent_id << " with " << new_logevent_id;
}
}
if (!pending_answer.promises_.empty()) {
@ -328,13 +368,10 @@ void PollManager::on_set_poll_answer(PollId poll_id, uint64 generation, Result<U
}
if (pending_answer.logevent_id_ != 0) {
// TODO delete logevent
LOG(INFO) << "Delete set poll answer logevent " << pending_answer.logevent_id_;
binlog_erase(G()->td_db()->get_binlog(), pending_answer.logevent_id_);
}
CHECK(!pending_answer.query_ref_.empty());
cancel_query(pending_answer.query_ref_);
pending_answer.query_ref_ = NetQueryRef();
auto promises = std::move(pending_answer.promises_);
for (auto &promise : promises) {
if (result.is_ok()) {
@ -471,4 +508,32 @@ PollId PollManager::on_get_poll(PollId poll_id, tl_object_ptr<telegram_api::poll
return poll_id;
}
void PollManager::on_binlog_events(vector<BinlogEvent> &&events) {
for (auto &event : events) {
switch (event.type_) {
case LogEvent::HandlerType::SetPollAnswer: {
if (!G()->parameters().use_message_db) {
binlog_erase(G()->td_db()->get_binlog(), event.id_);
break;
}
SetPollAnswerLogEvent log_event;
log_event_parse(log_event, event.data_).ensure();
auto dialog_id = log_event.full_message_id_.get_dialog_id();
Dependencies dependencies;
td_->messages_manager_->add_dialog_dependencies(dependencies, dialog_id);
td_->messages_manager_->resolve_dependencies_force(dependencies);
do_set_poll_answer(log_event.poll_id_, log_event.full_message_id_, std::move(log_event.options_), event.id_,
Auto());
break;
}
default:
LOG(FATAL) << "Unsupported logevent type " << event.type_;
}
}
}
} // namespace td

View File

@ -22,6 +22,8 @@
namespace td {
struct BinlogEvent;
class Td;
class PollManager : public Actor {
@ -52,6 +54,8 @@ class PollManager : public Actor {
td_api::object_ptr<td_api::poll> get_poll_object(PollId poll_id) const;
void on_binlog_events(vector<BinlogEvent> &&events);
template <class StorerT>
void store_poll(PollId poll_id, StorerT &storer) const;
@ -83,6 +87,8 @@ class PollManager : public Actor {
void parse(ParserT &parser);
};
class SetPollAnswerLogEvent;
void tear_down() override;
static bool is_local_poll_id(PollId poll_id);

View File

@ -4280,6 +4280,8 @@ Status Td::init(DbKey key) {
send_closure_later(secret_chats_manager_, &SecretChatsManager::replay_binlog_event, std::move(event));
}
send_closure_later(poll_manager_actor_, &PollManager::on_binlog_events, std::move(events.to_poll_manager));
send_closure_later(messages_manager_actor_, &MessagesManager::on_binlog_events,
std::move(events.to_messages_manager));

View File

@ -75,6 +75,9 @@ Status init_binlog(Binlog &binlog, string path, BinlogKeyValue<Binlog> &binlog_p
case LogEvent::HandlerType::WebPages:
events.web_page_events.push_back(event.clone());
break;
case LogEvent::HandlerType::SetPollAnswer:
events.to_poll_manager.push_back(event.clone());
break;
case LogEvent::HandlerType::SendMessage:
case LogEvent::HandlerType::DeleteMessage:
case LogEvent::HandlerType::DeleteMessagesFromServer:

View File

@ -55,6 +55,7 @@ class TdDb {
vector<BinlogEvent> channel_events;
vector<BinlogEvent> secret_chat_events;
vector<BinlogEvent> web_page_events;
vector<BinlogEvent> to_poll_manager;
vector<BinlogEvent> to_messages_manager;
};
static Result<unique_ptr<TdDb>> open(int32 scheduler_id, const TdParameters &parameters, DbKey key, Events &events);

View File

@ -69,6 +69,7 @@ class LogEvent {
Channels = 4,
SecretChatInfos = 5,
WebPages = 0x10,
SetPollAnswer = 0x20,
SendMessage = 0x100,
DeleteMessage = 0x101,
DeleteMessagesFromServer = 0x102,

View File

@ -614,7 +614,7 @@ class CheckTestC : public Task {
if (text.substr(0, tag_.size()) == tag_) {
file_id_to_check_ = messageDocument->document_->document_->id_;
LOG(ERROR) << "GOT FILE " << to_string(messageDocument->document_->document_);
this->send_query(make_tl_object<td_api::downloadFile>(file_id_to_check_, 1, 0),
this->send_query(make_tl_object<td_api::downloadFile>(file_id_to_check_, 1, 0, 0),
[](auto res) { check_td_error(res); });
}
}