Add td_api::getSimilarChatCount.

This commit is contained in:
levlam 2023-11-29 15:31:33 +03:00
parent 85b06f9614
commit da0d999c52
6 changed files with 100 additions and 17 deletions

View File

@ -6876,6 +6876,11 @@ searchChatsNearby location:location = ChatsNearby;
//@description Returns a list of chats similar to the given chat @chat_id Identifier of the target chat; must be an identifier of a channel chat
getSimilarChats chat_id:int53 = Chats;
//@description Returns approximate number of chats similar to the given chat
//@chat_id Identifier of the target chat; must be an identifier of a channel chat
//@return_local Pass true to get the number of chats without sending network requests, or -1 if the number of chats is unknown locally
getSimilarChatCount chat_id:int53 return_local:Bool = Count;
//@description Returns a list of frequently used chats @category Category of chats to be returned @limit The maximum number of chats to be returned; up to 30
getTopChats category:TopChatCategory limit:int32 = Chats;

View File

@ -9739,29 +9739,54 @@ bool ContactsManager::are_suitable_recommended_dialogs(const RecommendedDialogs
return true;
}
void ContactsManager::get_channel_recommendations(DialogId dialog_id,
Promise<td_api::object_ptr<td_api::chats>> &&promise) {
void ContactsManager::get_channel_recommendations(DialogId dialog_id, bool return_local,
Promise<td_api::object_ptr<td_api::chats>> &&chats_promise,
Promise<td_api::object_ptr<td_api::count>> &&count_promise) {
if (!td_->messages_manager_->have_dialog_force(dialog_id, "get_channel_recommendations")) {
return promise.set_error(Status::Error(400, "Chat not found"));
if (chats_promise) {
chats_promise.set_error(Status::Error(400, "Chat not found"));
}
if (count_promise) {
count_promise.set_error(Status::Error(400, "Chat not found"));
}
return;
}
if (dialog_id.get_type() != DialogType::Channel) {
return promise.set_value(td_api::make_object<td_api::chats>());
if (chats_promise) {
chats_promise.set_value(td_api::make_object<td_api::chats>());
}
if (count_promise) {
count_promise.set_value(td_api::make_object<td_api::count>(0));
}
return;
}
auto channel_id = dialog_id.get_channel_id();
if (!is_broadcast_channel(channel_id) || get_input_channel(channel_id) == nullptr) {
return promise.set_value(td_api::make_object<td_api::chats>());
if (chats_promise) {
chats_promise.set_value(td_api::make_object<td_api::chats>());
}
if (count_promise) {
count_promise.set_value(td_api::make_object<td_api::count>(0));
}
return;
}
bool use_database = true;
auto it = channel_recommended_dialogs_.find(channel_id);
if (it != channel_recommended_dialogs_.end()) {
if (are_suitable_recommended_dialogs(it->second)) {
auto next_reload_time = it->second.next_reload_time_;
promise.set_value(td_->messages_manager_->get_chats_object(it->second.total_count_, it->second.dialog_ids_,
"get_channel_recommendations"));
if (chats_promise) {
chats_promise.set_value(td_->messages_manager_->get_chats_object(
it->second.total_count_, it->second.dialog_ids_, "get_channel_recommendations"));
}
if (count_promise) {
count_promise.set_value(td_api::make_object<td_api::count>(it->second.total_count_));
}
if (next_reload_time > Time::now()) {
return;
}
promise = {};
chats_promise = {};
count_promise = {};
} else {
LOG(INFO) << "Drop cache for similar chats of " << dialog_id;
channel_recommended_dialogs_.erase(it);
@ -9769,17 +9794,22 @@ void ContactsManager::get_channel_recommendations(DialogId dialog_id,
}
use_database = false;
}
load_channel_recommendations(channel_id, use_database, std::move(promise));
load_channel_recommendations(channel_id, use_database, return_local, std::move(chats_promise),
std::move(count_promise));
}
string ContactsManager::get_channel_recommendations_database_key(ChannelId channel_id) {
return PSTRING() << "channel_recommendations" << channel_id.get();
}
void ContactsManager::load_channel_recommendations(ChannelId channel_id, bool use_database,
Promise<td_api::object_ptr<td_api::chats>> &&promise) {
void ContactsManager::load_channel_recommendations(ChannelId channel_id, bool use_database, bool return_local,
Promise<td_api::object_ptr<td_api::chats>> &&chats_promise,
Promise<td_api::object_ptr<td_api::count>> &&count_promise) {
if (count_promise) {
get_channel_recommendation_count_queries_[return_local][channel_id].push_back(std::move(count_promise));
}
auto &queries = get_channel_recommendations_queries_[channel_id];
queries.push_back(std::move(promise));
queries.push_back(std::move(chats_promise));
if (queries.size() == 1) {
if (G()->use_message_database() && use_database) {
G()->td_db()->get_sqlite_pmc()->get(
@ -9795,6 +9825,15 @@ void ContactsManager::load_channel_recommendations(ChannelId channel_id, bool us
}
void ContactsManager::fail_load_channel_recommendations_queries(ChannelId channel_id, Status &&error) {
for (int return_local = 0; return_local < 2; return_local++) {
auto it = get_channel_recommendation_count_queries_[return_local].find(channel_id);
if (it != get_channel_recommendation_count_queries_[return_local].end()) {
auto promises = std::move(it->second);
CHECK(!promises.empty());
get_channel_recommendation_count_queries_[return_local].erase(it);
fail_promises(promises, error.clone());
}
}
auto it = get_channel_recommendations_queries_.find(channel_id);
CHECK(it != get_channel_recommendations_queries_.end());
auto promises = std::move(it->second);
@ -9805,6 +9844,17 @@ void ContactsManager::fail_load_channel_recommendations_queries(ChannelId channe
void ContactsManager::finish_load_channel_recommendations_queries(ChannelId channel_id, int32 total_count,
vector<DialogId> dialog_ids) {
for (int return_local = 0; return_local < 2; return_local++) {
auto it = get_channel_recommendation_count_queries_[return_local].find(channel_id);
if (it != get_channel_recommendation_count_queries_[return_local].end()) {
auto promises = std::move(it->second);
CHECK(!promises.empty());
get_channel_recommendation_count_queries_[return_local].erase(it);
for (auto &promise : promises) {
promise.set_value(td_api::make_object<td_api::count>(total_count));
}
}
}
auto it = get_channel_recommendations_queries_.find(channel_id);
CHECK(it != get_channel_recommendations_queries_.end());
auto promises = std::move(it->second);
@ -9839,11 +9889,20 @@ void ContactsManager::on_load_channel_recommendations_from_database(ChannelId ch
recommended_dialogs.dialog_ids_);
if (next_reload_time <= Time::now()) {
load_channel_recommendations(channel_id, false, Auto());
load_channel_recommendations(channel_id, false, false, Auto(), Auto());
}
}
void ContactsManager::reload_channel_recommendations(ChannelId channel_id) {
auto it = get_channel_recommendation_count_queries_[1].find(channel_id);
if (it != get_channel_recommendation_count_queries_[1].end()) {
auto promises = std::move(it->second);
CHECK(!promises.empty());
get_channel_recommendation_count_queries_[1].erase(it);
for (auto &promise : promises) {
promise.set_value(td_api::make_object<td_api::count>(-1));
}
}
auto query_promise =
PromiseCreator::lambda([actor_id = actor_id(this), channel_id](
Result<std::pair<int32, vector<tl_object_ptr<telegram_api::Chat>>>> &&result) {

View File

@ -566,7 +566,9 @@ class ContactsManager final : public Actor {
ChannelId migrate_chat_to_megagroup(ChatId chat_id, Promise<Unit> &promise);
void get_channel_recommendations(DialogId dialog_id, Promise<td_api::object_ptr<td_api::chats>> &&promise);
void get_channel_recommendations(DialogId dialog_id, bool return_local,
Promise<td_api::object_ptr<td_api::chats>> &&chats_promise,
Promise<td_api::object_ptr<td_api::count>> &&count_promise);
void get_created_public_dialogs(PublicDialogType type, Promise<td_api::object_ptr<td_api::chats>> &&promise,
bool from_binlog);
@ -1745,8 +1747,9 @@ class ContactsManager final : public Actor {
static string get_channel_recommendations_database_key(ChannelId channel_id);
void load_channel_recommendations(ChannelId channel_id, bool use_database,
Promise<td_api::object_ptr<td_api::chats>> &&promise);
void load_channel_recommendations(ChannelId channel_id, bool use_database, bool return_local,
Promise<td_api::object_ptr<td_api::chats>> &&chats_promise,
Promise<td_api::object_ptr<td_api::count>> &&count_promise);
void fail_load_channel_recommendations_queries(ChannelId channel_id, Status &&error);
@ -2043,6 +2046,8 @@ class ContactsManager final : public Actor {
FlatHashMap<ChannelId, RecommendedDialogs, ChannelIdHash> channel_recommended_dialogs_;
FlatHashMap<ChannelId, vector<Promise<td_api::object_ptr<td_api::chats>>>, ChannelIdHash>
get_channel_recommendations_queries_;
FlatHashMap<ChannelId, vector<Promise<td_api::object_ptr<td_api::count>>>, ChannelIdHash>
get_channel_recommendation_count_queries_[2];
bool created_public_channels_inited_[2] = {false, false};
vector<ChannelId> created_public_channels_[2];

View File

@ -5101,7 +5101,14 @@ void Td::on_request(uint64 id, const td_api::clearAutosaveSettingsExceptions &re
void Td::on_request(uint64 id, const td_api::getSimilarChats &request) {
CHECK_IS_USER();
CREATE_REQUEST_PROMISE();
contacts_manager_->get_channel_recommendations(DialogId(request.chat_id_), std::move(promise));
contacts_manager_->get_channel_recommendations(DialogId(request.chat_id_), false, std::move(promise), Auto());
}
void Td::on_request(uint64 id, const td_api::getSimilarChatCount &request) {
CHECK_IS_USER();
CREATE_REQUEST_PROMISE();
contacts_manager_->get_channel_recommendations(DialogId(request.chat_id_), request.return_local_, Auto(),
std::move(promise));
}
void Td::on_request(uint64 id, const td_api::getTopChats &request) {

View File

@ -639,6 +639,8 @@ class Td final : public Actor {
void on_request(uint64 id, const td_api::getSimilarChats &request);
void on_request(uint64 id, const td_api::getSimilarChatCount &request);
void on_request(uint64 id, const td_api::getTopChats &request);
void on_request(uint64 id, const td_api::removeTopChat &request);

View File

@ -5159,6 +5159,11 @@ class CliClient final : public Actor {
ChatId chat_id;
get_args(args, chat_id);
send_request(td_api::make_object<td_api::getSimilarChats>(chat_id));
} else if (op == "gscc") {
ChatId chat_id;
bool return_local;
get_args(args, chat_id, return_local);
send_request(td_api::make_object<td_api::getSimilarChatCount>(chat_id, return_local));
} else if (op == "gcpc") {
send_request(td_api::make_object<td_api::getCreatedPublicChats>());
} else if (op == "gcpcl") {