From b593d2a3674ec72b0468c62966bbc09469098e9b Mon Sep 17 00:00:00 2001 From: levlam Date: Sun, 20 Sep 2020 00:28:26 +0300 Subject: [PATCH] Add total_count to getChats, searchChats and getGroupsInCommon method responses. GitOrigin-RevId: 2177bf47b38239a2d6e437f17d9f694ee5c31521 --- td/generate/scheme/td_api.tl | 4 +- td/generate/scheme/td_api.tlo | Bin 181916 -> 181952 bytes td/telegram/MessagesManager.cpp | 75 ++++++++++++++++++++------------ td/telegram/MessagesManager.h | 15 ++++--- td/telegram/Td.cpp | 22 +++++----- 5 files changed, 70 insertions(+), 46 deletions(-) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 37ceed813..cd5464b59 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -840,8 +840,8 @@ chatPosition list:ChatList order:int64 is_pinned:Bool source:ChatSource = ChatPo //@client_data Contains application-specific data associated with the chat. (For example, the chat scroll position or local chat notification settings can be stored here.) Persistent if the message database is used chat id:int53 type:ChatType title:string photo:chatPhotoInfo permissions:chatPermissions last_message:message positions:vector is_marked_as_unread:Bool has_scheduled_messages:Bool can_be_deleted_only_for_self:Bool can_be_deleted_for_all_users:Bool can_be_reported:Bool default_disable_notification:Bool unread_count:int32 last_read_inbox_message_id:int53 last_read_outbox_message_id:int53 unread_mention_count:int32 notification_settings:chatNotificationSettings action_bar:ChatActionBar pinned_message_id:int53 reply_markup_message_id:int53 draft_message:draftMessage client_data:string = Chat; -//@description Represents a list of chats @chat_ids List of chat identifiers -chats chat_ids:vector = Chats; +//@description Represents a list of chats @total_count Approximate total count of chats found @chat_ids List of chat identifiers +chats total_count:int32 chat_ids:vector = Chats; //@description Describes a chat located nearby @chat_id Chat identifier @distance Distance to the chat location in meters diff --git a/td/generate/scheme/td_api.tlo b/td/generate/scheme/td_api.tlo index 78790e540b78b9cf31463872d7d0ce8bfc1e72cb..daedeb6d8bf6770e5f4a75aadb4db21d6831e5ee 100644 GIT binary patch delta 306 zcmbQ!%YC4idxH%Ri;T+M?9C254_XC4EY{?V#FAnL1`vDt0c}RZ>6Skjt-*?#YbI^4 znZ$TyGnhBsW;2t{^aeL39WZzK1~(=qPLQsf$Ao-XrfX7BF-A2S26?2rI{*DMtck76T9zIkV|-*tQ)6iEaPj&%`2) zTbd`EseuV%&V*d15QsG|a+zXeA%b&ym;#`zZl)GAvF#N-Oc7A`ELh2C0MX4ck!gx4 E0M)i>D*ylh delta 306 zcmX@m%RQ%;dxH%R3-9B!*_$1B9<&O8Sggqzi6zAh3?TOOfFFz=VEN_+leRCI#CT;h zm^VFPGn3BrC2mYQVD9oOZcI#^APqN<3Hh*0pLmZ^dpd_FQwdmhdxIwv4)U`SjnWmTm E05gAaO8@`> diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index 64aaceb84..ec54a338b 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -14464,15 +14464,14 @@ void MessagesManager::on_load_recommended_dialog_filters( promise.set_value(td_api::make_object(std::move(chat_filters))); } -vector MessagesManager::get_dialogs(DialogListId dialog_list_id, DialogDate offset, int32 limit, bool force, - Promise &&promise) { +std::pair> MessagesManager::get_dialogs(DialogListId dialog_list_id, DialogDate offset, + int32 limit, bool force, Promise &&promise) { CHECK(!td_->auth_manager_->is_bot()); - vector result; auto *list_ptr = get_dialog_list(dialog_list_id); if (list_ptr == nullptr) { promise.set_error(Status::Error(3, "Chat list not found")); - return result; + return {}; } auto &list = *list_ptr; @@ -14483,13 +14482,14 @@ vector MessagesManager::get_dialogs(DialogListId dialog_list_id, Dialo if (limit <= 0) { promise.set_error(Status::Error(3, "Parameter limit in getChats must be positive")); - return result; + return {}; } if (limit > MAX_GET_DIALOGS + 2) { limit = MAX_GET_DIALOGS + 2; } + vector result; if (dialog_list_id == DialogListId(FolderId::main()) && sponsored_dialog_id_.is_valid()) { auto d = get_dialog(sponsored_dialog_id_); CHECK(d != nullptr); @@ -14506,18 +14506,19 @@ vector MessagesManager::get_dialogs(DialogListId dialog_list_id, Dialo if (!list.are_pinned_dialogs_inited_) { if (limit == 0 || force) { promise.set_value(Unit()); + return {get_dialog_total_count(list), std::move(result)}; } else { if (dialog_list_id.is_folder()) { auto &folder = *get_dialog_folder(dialog_list_id.get_folder_id()); if (folder.last_loaded_database_dialog_date_ == folder.last_database_server_dialog_date_ && folder.folder_last_dialog_date_ != MAX_DIALOG_DATE) { load_dialog_list(list, limit, std::move(promise)); - return result; + return {}; } } reload_pinned_dialogs(dialog_list_id, std::move(promise)); + return {}; } - return result; } if (dialog_list_id.is_filter()) { auto *filter = get_dialog_filter(dialog_list_id.get_filter_id()); @@ -14539,10 +14540,11 @@ vector MessagesManager::get_dialogs(DialogListId dialog_list_id, Dialo if (!input_dialog_ids.empty()) { if (limit == 0 || force) { promise.set_value(Unit()); + return {get_dialog_total_count(list), std::move(result)}; } else { td_->create_handler(std::move(promise))->send(std::move(input_dialog_ids)); + return {}; } - return result; } } @@ -14614,10 +14616,11 @@ vector MessagesManager::get_dialogs(DialogListId dialog_list_id, Dialo } promise.set_value(Unit()); + return {get_dialog_total_count(list), std::move(result)}; } else { load_dialog_list(list, limit, std::move(promise)); + return {}; } - return result; } void MessagesManager::load_dialog_list(DialogList &list, int32 limit, Promise &&promise) { @@ -15161,7 +15164,7 @@ vector MessagesManager::search_public_dialogs(const string &query, Pro } send_search_public_dialogs_query(query, std::move(promise)); - return vector(); + return {}; } void MessagesManager::send_search_public_dialogs_query(const string &query, Promise &&promise) { @@ -15175,8 +15178,8 @@ void MessagesManager::send_search_public_dialogs_query(const string &query, Prom td_->create_handler()->send(query); } -std::pair> MessagesManager::search_dialogs(const string &query, int32 limit, - Promise &&promise) { +std::pair> MessagesManager::search_dialogs(const string &query, int32 limit, + Promise &&promise) { LOG(INFO) << "Search chats with query \"" << query << "\" and limit " << limit; CHECK(!td_->auth_manager_->is_bot()); @@ -15191,7 +15194,7 @@ std::pair> MessagesManager::search_dialogs(const string promise.set_value(Unit()); size_t result_size = min(static_cast(limit), recently_found_dialog_ids_.size()); - return {recently_found_dialog_ids_.size(), + return {narrow_cast(recently_found_dialog_ids_.size()), vector(recently_found_dialog_ids_.begin(), recently_found_dialog_ids_.begin() + result_size)}; } @@ -15203,7 +15206,7 @@ std::pair> MessagesManager::search_dialogs(const string } promise.set_value(Unit()); - return {result.first, std::move(dialog_ids)}; + return {narrow_cast(result.first), std::move(dialog_ids)}; } vector MessagesManager::sort_dialogs_by_order(const vector &dialog_ids, int32 limit) const { @@ -15254,7 +15257,7 @@ vector MessagesManager::search_dialogs_on_server(const string &query, } send_search_public_dialogs_query(query, std::move(promise)); - return vector(); + return {}; } void MessagesManager::drop_common_dialogs_cache(UserId user_id) { @@ -15264,20 +15267,21 @@ void MessagesManager::drop_common_dialogs_cache(UserId user_id) { } } -vector MessagesManager::get_common_dialogs(UserId user_id, DialogId offset_dialog_id, int32 limit, bool force, - Promise &&promise) { +std::pair> MessagesManager::get_common_dialogs(UserId user_id, DialogId offset_dialog_id, + int32 limit, bool force, + Promise &&promise) { if (!td_->contacts_manager_->have_input_user(user_id)) { promise.set_error(Status::Error(6, "Have no access to the user")); - return vector(); + return {}; } if (user_id == td_->contacts_manager_->get_my_id()) { promise.set_error(Status::Error(6, "Can't get common chats with self")); - return vector(); + return {}; } if (limit <= 0) { promise.set_error(Status::Error(3, "Parameter limit must be positive")); - return vector(); + return {}; } if (limit > MAX_GET_DIALOGS) { limit = MAX_GET_DIALOGS; @@ -15299,7 +15303,7 @@ vector MessagesManager::get_common_dialogs(UserId user_id, DialogId of case DialogType::User: case DialogType::SecretChat: promise.set_error(Status::Error(6, "Wrong offset_chat_id")); - return vector(); + return {}; default: UNREACHABLE(); break; @@ -15307,6 +15311,7 @@ vector MessagesManager::get_common_dialogs(UserId user_id, DialogId of auto it = found_common_dialogs_.find(user_id); if (it != found_common_dialogs_.end() && !it->second.dialog_ids.empty()) { + int32 total_count = it->second.total_count; vector &common_dialog_ids = it->second.dialog_ids; bool use_cache = (!it->second.is_outdated && it->second.received_date >= Time::now() - 3600) || force || offset_chat_id != 0 || common_dialog_ids.size() >= static_cast(MAX_GET_DIALOGS); @@ -15317,7 +15322,7 @@ vector MessagesManager::get_common_dialogs(UserId user_id, DialogId of offset_it = std::find(common_dialog_ids.begin(), common_dialog_ids.end(), offset_dialog_id); if (offset_it == common_dialog_ids.end()) { promise.set_error(Status::Error(6, "Wrong offset_chat_id")); - return vector(); + return {}; } ++offset_it; } @@ -15329,19 +15334,19 @@ vector MessagesManager::get_common_dialogs(UserId user_id, DialogId of auto dialog_id = *offset_it++; if (dialog_id == DialogId()) { // end of the list promise.set_value(Unit()); - return result; + return {total_count, std::move(result)}; } result.push_back(dialog_id); } if (result.size() == static_cast(limit) || force) { promise.set_value(Unit()); - return result; + return {total_count, std::move(result)}; } } } td_->create_handler(std::move(promise))->send(user_id, offset_chat_id, MAX_GET_DIALOGS); - return vector(); + return {}; } void MessagesManager::on_get_common_dialogs(UserId user_id, int32 offset_chat_id, @@ -15428,8 +15433,16 @@ void MessagesManager::on_get_common_dialogs(UserId user_id, int32 offset_chat_id } } if (result.size() >= static_cast(total_count) || is_last) { + if (result.size() != static_cast(total_count)) { + LOG(ERROR) << "Fix total count of common groups with " << user_id << " from " << total_count << " to " + << result.size(); + total_count = narrow_cast(result.size()); + td_->contacts_manager_->on_update_user_common_chat_count(user_id, total_count); + } + result.push_back(DialogId()); } + common_dialogs.total_count = total_count; } bool MessagesManager::have_message_force(FullMessageId full_message_id, const char *source) { @@ -18365,8 +18378,16 @@ tl_object_ptr MessagesManager::get_chat_object(DialogId dialog_id) return get_chat_object(get_dialog(dialog_id)); } -tl_object_ptr MessagesManager::get_chats_object(const vector &dialogs) { - return td_api::make_object(transform(dialogs, [](DialogId dialog_id) { return dialog_id.get(); })); +tl_object_ptr MessagesManager::get_chats_object(int32 total_count, const vector &dialog_ids) { + if (total_count == -1) { + total_count = narrow_cast(dialog_ids.size()); + } + return td_api::make_object(total_count, + transform(dialog_ids, [](DialogId dialog_id) { return dialog_id.get(); })); +} + +tl_object_ptr MessagesManager::get_chats_object(const std::pair> &dialog_ids) { + return get_chats_object(dialog_ids.first, dialog_ids.second); } td_api::object_ptr MessagesManager::get_chat_filter_object(DialogFilterId dialog_filter_id) const { diff --git a/td/telegram/MessagesManager.h b/td/telegram/MessagesManager.h index 85a3b9830..73e01ae00 100644 --- a/td/telegram/MessagesManager.h +++ b/td/telegram/MessagesManager.h @@ -537,19 +537,19 @@ class MessagesManager : public Actor { void get_recommended_dialog_filters(Promise> &&promise); - vector get_dialogs(DialogListId dialog_list_id, DialogDate offset, int32 limit, bool force, - Promise &&promise); + std::pair> get_dialogs(DialogListId dialog_list_id, DialogDate offset, int32 limit, + bool force, Promise &&promise); vector search_public_dialogs(const string &query, Promise &&promise); - std::pair> search_dialogs(const string &query, int32 limit, Promise &&promise); + std::pair> search_dialogs(const string &query, int32 limit, Promise &&promise); vector search_dialogs_on_server(const string &query, int32 limit, Promise &&promise); void drop_common_dialogs_cache(UserId user_id); - vector get_common_dialogs(UserId user_id, DialogId offset_dialog_id, int32 limit, bool force, - Promise &&promise); + std::pair> get_common_dialogs(UserId user_id, DialogId offset_dialog_id, int32 limit, + bool force, Promise &&promise); bool can_get_message_statistics(FullMessageId full_message_id); @@ -667,7 +667,9 @@ class MessagesManager : public Actor { tl_object_ptr get_chat_object(DialogId dialog_id) const; - static tl_object_ptr get_chats_object(const vector &dialogs); + static tl_object_ptr get_chats_object(int32 total_count, const vector &dialog_ids); + + static tl_object_ptr get_chats_object(const std::pair> &dialog_ids); tl_object_ptr get_chat_filter_object(DialogFilterId dialog_filter_id) const; @@ -2983,6 +2985,7 @@ class MessagesManager : public Actor { struct CommonDialogs { vector dialog_ids; double received_date = 0; + int32 total_count = 0; bool is_outdated = false; }; std::unordered_map found_common_dialogs_; diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index c66055824..f2a2e3529 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -837,7 +837,7 @@ class GetChatsRequest : public RequestActor<> { DialogDate offset_; int32 limit_; - vector dialog_ids_; + std::pair> dialog_ids_; void do_run(Promise &&promise) override { dialog_ids_ = @@ -890,7 +890,7 @@ class SearchPublicChatsRequest : public RequestActor<> { } void do_send_result() override { - send_result(MessagesManager::get_chats_object(dialog_ids_)); + send_result(MessagesManager::get_chats_object(-1, dialog_ids_)); } public: @@ -903,10 +903,10 @@ class SearchChatsRequest : public RequestActor<> { string query_; int32 limit_; - vector dialog_ids_; + std::pair> dialog_ids_; void do_run(Promise &&promise) override { - dialog_ids_ = td->messages_manager_->search_dialogs(query_, limit_, std::move(promise)).second; + dialog_ids_ = td->messages_manager_->search_dialogs(query_, limit_, std::move(promise)); } void do_send_result() override { @@ -930,7 +930,7 @@ class SearchChatsOnServerRequest : public RequestActor<> { } void do_send_result() override { - send_result(MessagesManager::get_chats_object(dialog_ids_)); + send_result(MessagesManager::get_chats_object(-1, dialog_ids_)); } public: @@ -944,7 +944,7 @@ class GetGroupsInCommonRequest : public RequestActor<> { DialogId offset_dialog_id_; int32 limit_; - vector dialog_ids_; + std::pair> dialog_ids_; void do_run(Promise &&promise) override { dialog_ids_ = td->messages_manager_->get_common_dialogs(user_id_, offset_dialog_id_, limit_, get_tries() < 2, @@ -970,7 +970,7 @@ class GetCreatedPublicChatsRequest : public RequestActor<> { } void do_send_result() override { - send_result(MessagesManager::get_chats_object(dialog_ids_)); + send_result(MessagesManager::get_chats_object(-1, dialog_ids_)); } public: @@ -987,7 +987,7 @@ class GetSuitableDiscussionChatsRequest : public RequestActor<> { } void do_send_result() override { - send_result(MessagesManager::get_chats_object(dialog_ids_)); + send_result(MessagesManager::get_chats_object(-1, dialog_ids_)); } public: @@ -1003,7 +1003,7 @@ class GetInactiveSupergroupChatsRequest : public RequestActor<> { } void do_send_result() override { - send_result(MessagesManager::get_chats_object(dialog_ids_)); + send_result(MessagesManager::get_chats_object(-1, dialog_ids_)); } public: @@ -2307,7 +2307,7 @@ class GetChatNotificationSettingsExceptionsRequest : public RequestActor<> { } void do_send_result() override { - send_result(MessagesManager::get_chats_object(dialog_ids_)); + send_result(MessagesManager::get_chats_object(-1, dialog_ids_)); } public: @@ -5352,7 +5352,7 @@ void Td::on_request(uint64 id, td_api::getTopChats &request) { if (result.is_error()) { promise.set_error(result.move_as_error()); } else { - promise.set_value(MessagesManager::get_chats_object(result.ok())); + promise.set_value(MessagesManager::get_chats_object(-1, result.ok())); } }); send_closure(top_dialog_manager_, &TopDialogManager::get_top_dialogs, get_top_dialog_category(*request.category_),