Remove failed to load chats from chat filters.

This commit is contained in:
levlam 2022-08-30 01:18:20 +03:00
parent d110b0f5bc
commit 408a6a9d74
4 changed files with 63 additions and 2 deletions

View File

@ -10,6 +10,7 @@
#include "td/telegram/ChatId.h"
#include "td/telegram/UserId.h"
#include "td/utils/algorithm.h"
#include "td/utils/logging.h"
namespace td {
@ -143,4 +144,10 @@ bool InputDialogId::contains(const vector<InputDialogId> &input_dialog_ids, Dial
return false;
}
bool InputDialogId::remove(vector<InputDialogId> &input_dialog_ids, DialogId dialog_id) {
return td::remove_if(input_dialog_ids, [dialog_id](InputDialogId input_dialog_id) {
return input_dialog_id.get_dialog_id() == dialog_id;
});
}
} // namespace td

View File

@ -40,6 +40,8 @@ class InputDialogId {
static bool contains(const vector<InputDialogId> &input_dialog_ids, DialogId dialog_id);
static bool remove(vector<InputDialogId> &input_dialog_ids, DialogId dialog_id);
bool operator==(const InputDialogId &other) const {
return dialog_id == other.dialog_id && access_hash == other.access_hash;
}

View File

@ -16580,18 +16580,67 @@ void MessagesManager::load_dialog_filter_dialogs(DialogFilterId dialog_filter_id
const size_t MAX_SLICE_SIZE = 100; // server side limit
MultiPromiseActorSafe mpas{"GetFilterDialogsOnServerMultiPromiseActor"};
mpas.add_promise(std::move(promise));
mpas.set_ignore_errors(true);
auto lock = mpas.get_promise();
for (size_t i = 0; i < input_dialog_ids.size(); i += MAX_SLICE_SIZE) {
auto end_i = i + MAX_SLICE_SIZE;
auto end = end_i < input_dialog_ids.size() ? input_dialog_ids.begin() + end_i : input_dialog_ids.end();
td_->create_handler<GetDialogsQuery>(mpas.get_promise())->send({input_dialog_ids.begin() + i, end});
vector<InputDialogId> slice_input_dialog_ids = {input_dialog_ids.begin() + i, end};
auto slice_dialog_ids = transform(slice_input_dialog_ids,
[](InputDialogId input_dialog_id) { return input_dialog_id.get_dialog_id(); });
auto query_promise =
PromiseCreator::lambda([actor_id = actor_id(this), dialog_filter_id, dialog_ids = std::move(slice_dialog_ids),
promise = mpas.get_promise()](Result<Unit> &&result) mutable {
if (result.is_error()) {
return promise.set_error(result.move_as_error());
}
send_closure(actor_id, &MessagesManager::on_load_dialog_filter_dialogs, dialog_filter_id,
std::move(dialog_ids), std::move(promise));
});
td_->create_handler<GetDialogsQuery>(std::move(query_promise))->send(std::move(slice_input_dialog_ids));
}
lock.set_value(Unit());
}
void MessagesManager::on_load_dialog_filter_dialogs(DialogFilterId dialog_filter_id, vector<DialogId> &&dialog_ids,
Promise<Unit> &&promise) {
TRY_STATUS_PROMISE(promise, G()->close_status());
td::remove_if(dialog_ids,
[this](DialogId dialog_id) { return have_dialog_force(dialog_id, "on_load_dialog_filter_dialogs"); });
if (dialog_ids.empty()) {
LOG(INFO) << "All chats from " << dialog_filter_id << " were loaded";
return promise.set_value(Unit());
}
LOG(INFO) << "Failed to load chats " << dialog_ids << " from " << dialog_filter_id;
auto old_dialog_filter = get_dialog_filter(dialog_filter_id);
if (old_dialog_filter == nullptr) {
return promise.set_value(Unit());
}
CHECK(is_update_chat_filters_sent_);
auto new_dialog_filter = td::make_unique<DialogFilter>(*old_dialog_filter);
for (auto dialog_id : dialog_ids) {
InputDialogId::remove(new_dialog_filter->pinned_dialog_ids, dialog_id);
InputDialogId::remove(new_dialog_filter->included_dialog_ids, dialog_id);
InputDialogId::remove(new_dialog_filter->excluded_dialog_ids, dialog_id);
}
if (*new_dialog_filter != *old_dialog_filter) {
LOG(INFO) << "Update " << dialog_filter_id << " from " << *old_dialog_filter << " to " << *new_dialog_filter;
edit_dialog_filter(std::move(new_dialog_filter), "on_load_dialog_filter_dialogs");
save_dialog_filters();
send_update_chat_filters();
synchronize_dialog_filters();
}
promise.set_value(Unit());
}
void MessagesManager::load_dialog_filter(DialogFilterId dialog_filter_id, bool force, Promise<Unit> &&promise) {
CHECK(!td_->auth_manager_->is_bot());
if (!dialog_filter_id.is_valid()) {

View File

@ -2784,6 +2784,9 @@ class MessagesManager final : public Actor {
void load_dialog_filter_dialogs(DialogFilterId dialog_filter_id, vector<InputDialogId> &&input_dialog_ids,
Promise<Unit> &&promise);
void on_load_dialog_filter_dialogs(DialogFilterId dialog_filter_id, vector<DialogId> &&dialog_ids,
Promise<Unit> &&promise);
void load_dialog_filter(const DialogFilter *filter, bool force, Promise<Unit> &&promise);
void on_get_recommended_dialog_filters(Result<vector<tl_object_ptr<telegram_api::dialogFilterSuggested>>> result,