Combine GetCreatedPublicChannels queries.

This commit is contained in:
levlam 2021-12-03 15:40:39 +03:00
parent 5e6628d5f9
commit 03baffcd1f
4 changed files with 58 additions and 33 deletions

View File

@ -2377,7 +2377,7 @@ class GetGroupsForDiscussionQuery final : public Td::ResultHandler {
}
case telegram_api::messages_chatsSlice::ID: {
auto chats = move_tl_object_as<telegram_api::messages_chatsSlice>(chats_ptr);
LOG(ERROR) << "Receive chatsSlice in result of GetCreatedPublicChannelsQuery";
LOG(ERROR) << "Receive chatsSlice in result of GetGroupsForDiscussionQuery";
td_->contacts_manager_->on_get_dialogs_for_discussion(std::move(chats->chats_));
break;
}
@ -7621,19 +7621,51 @@ vector<DialogId> ContactsManager::get_dialog_ids(vector<tl_object_ptr<telegram_a
return dialog_ids;
}
vector<DialogId> ContactsManager::get_created_public_dialogs(PublicDialogType type, Promise<Unit> &&promise) {
auto index = static_cast<int32>(type);
if (created_public_channels_inited_[index]) {
promise.set_value(Unit());
return transform(created_public_channels_[index], [&](ChannelId channel_id) {
DialogId dialog_id(channel_id);
td_->messages_manager_->force_create_dialog(dialog_id, "get_created_public_dialogs");
return dialog_id;
});
void ContactsManager::return_created_public_dialogs(Promise<td_api::object_ptr<td_api::chats>> &&promise,
const vector<ChannelId> &channel_ids) {
if (!promise) {
return;
}
td_->create_handler<GetCreatedPublicChannelsQuery>(std::move(promise))->send(type, false);
return {};
auto total_count = narrow_cast<int32>(channel_ids.size());
promise.set_value(td_api::make_object<td_api::chats>(
total_count, transform(channel_ids, [](ChannelId channel_id) { return DialogId(channel_id).get(); })));
}
void ContactsManager::get_created_public_dialogs(PublicDialogType type,
Promise<td_api::object_ptr<td_api::chats>> &&promise) {
auto index = static_cast<int32>(type);
if (created_public_channels_inited_[index]) {
return return_created_public_dialogs(std::move(promise), created_public_channels_[index]);
}
get_created_public_channels_queries_[index].push_back(std::move(promise));
if (get_created_public_channels_queries_[index].size() == 1) {
auto query_promise = PromiseCreator::lambda([actor_id = actor_id(this), type](Result<Unit> &&result) {
send_closure(actor_id, &ContactsManager::finish_get_created_public_dialogs, type, std::move(result));
});
td_->create_handler<GetCreatedPublicChannelsQuery>(std::move(query_promise))->send(type, false);
}
}
void ContactsManager::finish_get_created_public_dialogs(PublicDialogType type, Result<Unit> &&result) {
auto index = static_cast<int32>(type);
auto promises = std::move(get_created_public_channels_queries_[index]);
reset_to_empty(get_created_public_channels_queries_[index]);
if (G()->close_flag()) {
result = G()->close_status();
}
if (result.is_error()) {
for (auto &promise : promises) {
promise.set_error(result.error().clone());
}
return;
}
CHECK(created_public_channels_inited_[index]);
for (auto &promise : promises) {
return_created_public_dialogs(std::move(promise), created_public_channels_[index]);
}
}
void ContactsManager::update_created_public_channels(Channel *c, ChannelId channel_id) {
@ -7678,6 +7710,9 @@ void ContactsManager::on_get_created_public_channels(PublicDialogType type,
if (created_public_channels_inited_[index] && created_public_channels_[index] == channel_ids) {
return;
}
for (auto channel_id : channel_ids) {
td_->messages_manager_->force_create_dialog(DialogId(channel_id), "on_get_created_public_channels");
}
created_public_channels_[index] = std::move(channel_ids);
created_public_channels_inited_[index] = true;

View File

@ -417,7 +417,7 @@ class ContactsManager final : public Actor {
ChannelId migrate_chat_to_megagroup(ChatId chat_id, Promise<Unit> &promise);
vector<DialogId> get_created_public_dialogs(PublicDialogType type, Promise<Unit> &&promise);
void get_created_public_dialogs(PublicDialogType type, Promise<td_api::object_ptr<td_api::chats>> &&promise);
void check_created_public_dialogs_limit(PublicDialogType type, Promise<Unit> &&promise);
@ -1410,6 +1410,11 @@ class ContactsManager final : public Actor {
static bool is_channel_public(const Channel *c);
static void return_created_public_dialogs(Promise<td_api::object_ptr<td_api::chats>> &&promise,
const vector<ChannelId> &channel_ids);
void finish_get_created_public_dialogs(PublicDialogType type, Result<Unit> &&result);
void update_created_public_channels(Channel *c, ChannelId channel_id);
void update_created_public_broadcasts();
@ -1643,6 +1648,7 @@ class ContactsManager final : public Actor {
bool created_public_channels_inited_[2] = {false, false};
vector<ChannelId> created_public_channels_[2];
vector<Promise<td_api::object_ptr<td_api::chats>>> get_created_public_channels_queries_[2];
bool dialogs_for_discussion_inited_ = false;
vector<DialogId> dialogs_for_discussion_;

View File

@ -24127,7 +24127,8 @@ void MessagesManager::get_dialog_send_message_as_dialog_ids(
return promise.set_value(std::move(senders));
}
td_->contacts_manager_->get_created_public_dialogs(PublicDialogType::HasUsername, Promise<Unit>());
td_->contacts_manager_->get_created_public_dialogs(PublicDialogType::HasUsername,
Promise<td_api::object_ptr<td_api::chats>>());
td_->create_handler<GetSendAsQuery>(std::move(promise))->send(dialog_id);
}

View File

@ -904,24 +904,6 @@ class GetGroupsInCommonRequest final : public RequestActor<> {
}
};
class GetCreatedPublicChatsRequest final : public RequestActor<> {
vector<DialogId> dialog_ids_;
PublicDialogType type_;
void do_run(Promise<Unit> &&promise) final {
dialog_ids_ = td_->contacts_manager_->get_created_public_dialogs(type_, std::move(promise));
}
void do_send_result() final {
send_result(MessagesManager::get_chats_object(-1, dialog_ids_));
}
public:
GetCreatedPublicChatsRequest(ActorShared<Td> td, uint64 request_id, PublicDialogType type)
: RequestActor(std::move(td), request_id), type_(type) {
}
};
class GetSuitableDiscussionChatsRequest final : public RequestActor<> {
vector<DialogId> dialog_ids_;
@ -5224,7 +5206,8 @@ void Td::on_request(uint64 id, td_api::checkChatUsername &request) {
void Td::on_request(uint64 id, const td_api::getCreatedPublicChats &request) {
CHECK_IS_USER();
CREATE_REQUEST(GetCreatedPublicChatsRequest, get_public_dialog_type(request.type_));
CREATE_REQUEST_PROMISE();
contacts_manager_->get_created_public_dialogs(get_public_dialog_type(request.type_), std::move(promise));
}
void Td::on_request(uint64 id, const td_api::checkCreatedPublicChatsLimit &request) {