Implement ToggleTopPeers as ResultHandler.
This commit is contained in:
parent
9115ba4aa5
commit
1849953e78
@ -39,6 +39,31 @@
|
|||||||
|
|
||||||
namespace td {
|
namespace td {
|
||||||
|
|
||||||
|
class ToggleTopPeersQuery final : public Td::ResultHandler {
|
||||||
|
Promise<Unit> promise_;
|
||||||
|
|
||||||
|
public:
|
||||||
|
explicit ToggleTopPeersQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
|
||||||
|
}
|
||||||
|
|
||||||
|
void send(bool is_enabled) {
|
||||||
|
send_query(G()->net_query_creator().create(telegram_api::contacts_toggleTopPeers(is_enabled)));
|
||||||
|
}
|
||||||
|
|
||||||
|
void on_result(uint64 id, BufferSlice packet) final {
|
||||||
|
auto result_ptr = fetch_result<telegram_api::contacts_toggleTopPeers>(packet);
|
||||||
|
if (result_ptr.is_error()) {
|
||||||
|
return on_error(id, result_ptr.move_as_error());
|
||||||
|
}
|
||||||
|
|
||||||
|
promise_.set_value(Unit());
|
||||||
|
}
|
||||||
|
|
||||||
|
void on_error(uint64 id, Status status) final {
|
||||||
|
promise_.set_error(std::move(status));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
static CSlice top_dialog_category_name(TopDialogCategory category) {
|
static CSlice top_dialog_category_name(TopDialogCategory category) {
|
||||||
switch (category) {
|
switch (category) {
|
||||||
case TopDialogCategory::Correspondent:
|
case TopDialogCategory::Correspondent:
|
||||||
@ -133,6 +158,10 @@ bool TopDialogManager::set_is_enabled(bool is_enabled) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void TopDialogManager::send_toggle_top_peers(bool is_enabled) {
|
void TopDialogManager::send_toggle_top_peers(bool is_enabled) {
|
||||||
|
if (G()->close_flag()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (have_toggle_top_peers_query_) {
|
if (have_toggle_top_peers_query_) {
|
||||||
have_pending_toggle_top_peers_query_ = true;
|
have_pending_toggle_top_peers_query_ = true;
|
||||||
pending_toggle_top_peers_query_ = is_enabled;
|
pending_toggle_top_peers_query_ = is_enabled;
|
||||||
@ -141,9 +170,32 @@ void TopDialogManager::send_toggle_top_peers(bool is_enabled) {
|
|||||||
|
|
||||||
LOG(DEBUG) << "Send toggle top peers query to " << is_enabled;
|
LOG(DEBUG) << "Send toggle top peers query to " << is_enabled;
|
||||||
have_toggle_top_peers_query_ = true;
|
have_toggle_top_peers_query_ = true;
|
||||||
toggle_top_peers_query_is_enabled_ = is_enabled;
|
|
||||||
auto net_query = G()->net_query_creator().create(telegram_api::contacts_toggleTopPeers(is_enabled));
|
auto promise = PromiseCreator::lambda([actor_id = actor_id(this), is_enabled](Result<Unit> result) {
|
||||||
G()->net_query_dispatcher().dispatch_with_callback(std::move(net_query), actor_shared(this, 2));
|
send_closure(actor_id, &TopDialogManager::on_toggle_top_peers, is_enabled, std::move(result));
|
||||||
|
});
|
||||||
|
td_->create_handler<ToggleTopPeersQuery>(std::move(promise))->send(is_enabled);
|
||||||
|
}
|
||||||
|
|
||||||
|
void TopDialogManager::on_toggle_top_peers(bool is_enabled, Result<Unit> &&result) {
|
||||||
|
CHECK(have_toggle_top_peers_query_);
|
||||||
|
have_toggle_top_peers_query_ = false;
|
||||||
|
|
||||||
|
if (have_pending_toggle_top_peers_query_) {
|
||||||
|
have_pending_toggle_top_peers_query_ = false;
|
||||||
|
if (pending_toggle_top_peers_query_ != is_enabled) {
|
||||||
|
send_toggle_top_peers(pending_toggle_top_peers_query_);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (result.is_ok()) {
|
||||||
|
// everything is synchronized
|
||||||
|
G()->td_db()->get_binlog_pmc()->erase("top_peers_enabled");
|
||||||
|
} else {
|
||||||
|
// let's resend the query forever
|
||||||
|
send_toggle_top_peers(is_enabled);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void TopDialogManager::on_dialog_used(TopDialogCategory category, DialogId dialog_id, int32 date) {
|
void TopDialogManager::on_dialog_used(TopDialogCategory category, DialogId dialog_id, int32 date) {
|
||||||
@ -415,30 +467,6 @@ void TopDialogManager::do_get_top_peers() {
|
|||||||
|
|
||||||
void TopDialogManager::on_result(NetQueryPtr net_query) {
|
void TopDialogManager::on_result(NetQueryPtr net_query) {
|
||||||
auto query_type = get_link_token();
|
auto query_type = get_link_token();
|
||||||
if (query_type == 2) { // toggleTopPeers
|
|
||||||
CHECK(have_toggle_top_peers_query_);
|
|
||||||
have_toggle_top_peers_query_ = false;
|
|
||||||
|
|
||||||
if (have_pending_toggle_top_peers_query_) {
|
|
||||||
have_pending_toggle_top_peers_query_ = false;
|
|
||||||
if (pending_toggle_top_peers_query_ != toggle_top_peers_query_is_enabled_) {
|
|
||||||
send_toggle_top_peers(pending_toggle_top_peers_query_);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
auto r_result = fetch_result<telegram_api::contacts_toggleTopPeers>(std::move(net_query));
|
|
||||||
if (r_result.is_ok()) {
|
|
||||||
// everything is synchronized
|
|
||||||
G()->td_db()->get_binlog_pmc()->erase("top_peers_enabled");
|
|
||||||
} else {
|
|
||||||
// let's resend the query forever
|
|
||||||
if (!G()->close_flag()) {
|
|
||||||
send_toggle_top_peers(toggle_top_peers_query_is_enabled_);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (query_type == 1) { // resetTopPeerRating
|
if (query_type == 1) { // resetTopPeerRating
|
||||||
// ignore result
|
// ignore result
|
||||||
return;
|
return;
|
||||||
|
@ -54,7 +54,6 @@ class TopDialogManager final : public NetQueryCallback {
|
|||||||
int32 rating_e_decay_ = 241920;
|
int32 rating_e_decay_ = 241920;
|
||||||
|
|
||||||
bool have_toggle_top_peers_query_ = false;
|
bool have_toggle_top_peers_query_ = false;
|
||||||
bool toggle_top_peers_query_is_enabled_ = false;
|
|
||||||
bool have_pending_toggle_top_peers_query_ = false;
|
bool have_pending_toggle_top_peers_query_ = false;
|
||||||
bool pending_toggle_top_peers_query_ = false;
|
bool pending_toggle_top_peers_query_ = false;
|
||||||
bool was_first_sync_{false};
|
bool was_first_sync_{false};
|
||||||
@ -100,8 +99,11 @@ class TopDialogManager final : public NetQueryCallback {
|
|||||||
void normalize_rating();
|
void normalize_rating();
|
||||||
|
|
||||||
bool set_is_enabled(bool is_enabled);
|
bool set_is_enabled(bool is_enabled);
|
||||||
|
|
||||||
void send_toggle_top_peers(bool is_enabled);
|
void send_toggle_top_peers(bool is_enabled);
|
||||||
|
|
||||||
|
void on_toggle_top_peers(bool is_enabled, Result<Unit> &&result);
|
||||||
|
|
||||||
void do_get_top_dialogs(GetTopDialogsQuery &&query);
|
void do_get_top_dialogs(GetTopDialogsQuery &&query);
|
||||||
|
|
||||||
void do_get_top_peers();
|
void do_get_top_peers();
|
||||||
|
Loading…
Reference in New Issue
Block a user