Skip to content

Commit

Permalink
Use RequestPromise in td_api::getEmojiSuggestionsUrl.
Browse files Browse the repository at this point in the history
  • Loading branch information
levlam committed Sep 9, 2024
1 parent 525f1e1 commit a33553e
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 71 deletions.
22 changes: 2 additions & 20 deletions td/telegram/Requests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1747,25 +1747,6 @@ class GetKeywordEmojisRequest final : public RequestActor<> {
}
};

class GetEmojiSuggestionsUrlRequest final : public RequestOnceActor {
string language_code_;

int64 random_id_;

void do_run(Promise<Unit> &&promise) final {
random_id_ = td_->stickers_manager_->get_emoji_suggestions_url(language_code_, std::move(promise));
}

void do_send_result() final {
send_result(td_->stickers_manager_->get_emoji_suggestions_url_result(random_id_));
}

public:
GetEmojiSuggestionsUrlRequest(ActorShared<Td> td, uint64 request_id, string &&language_code)
: RequestOnceActor(std::move(td), request_id), language_code_(std::move(language_code)), random_id_(0) {
}
};

class GetSavedAnimationsRequest final : public RequestActor<> {
vector<FileId> animation_ids_;

Expand Down Expand Up @@ -6554,7 +6535,8 @@ void Requests::on_request(uint64 id, td_api::getAnimatedEmoji &request) {
void Requests::on_request(uint64 id, td_api::getEmojiSuggestionsUrl &request) {
CHECK_IS_USER();
CLEAN_INPUT_STRING(request.language_code_);
CREATE_REQUEST(GetEmojiSuggestionsUrlRequest, std::move(request.language_code_));
CREATE_HTTP_URL_REQUEST_PROMISE();
td_->stickers_manager_->get_emoji_suggestions_url(request.language_code_, std::move(promise));
}

void Requests::on_request(uint64 id, const td_api::getCustomEmojiStickers &request) {
Expand Down
50 changes: 6 additions & 44 deletions td/telegram/StickersManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,11 +253,10 @@ class GetEmojiKeywordsDifferenceQuery final : public Td::ResultHandler {
};

class GetEmojiUrlQuery final : public Td::ResultHandler {
Promise<telegram_api::object_ptr<telegram_api::emojiURL>> promise_;
Promise<string> promise_;

public:
explicit GetEmojiUrlQuery(Promise<telegram_api::object_ptr<telegram_api::emojiURL>> &&promise)
: promise_(std::move(promise)) {
explicit GetEmojiUrlQuery(Promise<string> &&promise) : promise_(std::move(promise)) {
}

void send(const string &language_code) {
Expand All @@ -270,7 +269,8 @@ class GetEmojiUrlQuery final : public Td::ResultHandler {
return on_error(result_ptr.move_as_error());
}

promise_.set_value(result_ptr.move_as_ok());
auto ptr = result_ptr.move_as_ok();
promise_.set_value(std::move(ptr->url_));
}

void on_error(Status status) final {
Expand Down Expand Up @@ -10206,46 +10206,8 @@ vector<string> StickersManager::get_keyword_emojis(const string &text, const vec
return result;
}

int64 StickersManager::get_emoji_suggestions_url(const string &language_code, Promise<Unit> &&promise) {
int64 random_id = 0;
do {
random_id = Random::secure_int64();
} while (random_id == 0 || emoji_suggestions_urls_.count(random_id) > 0);
emoji_suggestions_urls_[random_id]; // reserve place for result

auto query_promise =
PromiseCreator::lambda([actor_id = actor_id(this), random_id, promise = std::move(promise)](
Result<telegram_api::object_ptr<telegram_api::emojiURL>> &&result) mutable {
send_closure(actor_id, &StickersManager::on_get_emoji_suggestions_url, random_id, std::move(promise),
std::move(result));
});
td_->create_handler<GetEmojiUrlQuery>(std::move(query_promise))->send(language_code);
return random_id;
}

void StickersManager::on_get_emoji_suggestions_url(
int64 random_id, Promise<Unit> &&promise, Result<telegram_api::object_ptr<telegram_api::emojiURL>> &&r_emoji_url) {
auto it = emoji_suggestions_urls_.find(random_id);
CHECK(it != emoji_suggestions_urls_.end());
auto &result = it->second;
CHECK(result.empty());

if (r_emoji_url.is_error()) {
emoji_suggestions_urls_.erase(it);
return promise.set_error(r_emoji_url.move_as_error());
}

auto emoji_url = r_emoji_url.move_as_ok();
result = std::move(emoji_url->url_);
promise.set_value(Unit());
}

td_api::object_ptr<td_api::httpUrl> StickersManager::get_emoji_suggestions_url_result(int64 random_id) {
auto it = emoji_suggestions_urls_.find(random_id);
CHECK(it != emoji_suggestions_urls_.end());
auto result = td_api::make_object<td_api::httpUrl>(it->second);
emoji_suggestions_urls_.erase(it);
return result;
void StickersManager::get_emoji_suggestions_url(const string &language_code, Promise<string> &&promise) {
td_->create_handler<GetEmojiUrlQuery>(std::move(promise))->send(language_code);
}

string StickersManager::get_emoji_groups_database_key(EmojiGroupType group_type) {
Expand Down
8 changes: 1 addition & 7 deletions td/telegram/StickersManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -391,12 +391,10 @@ class StickersManager final : public Actor {
vector<string> get_keyword_emojis(const string &text, const vector<string> &input_language_codes, bool force,
Promise<Unit> &&promise);

int64 get_emoji_suggestions_url(const string &language_code, Promise<Unit> &&promise);
void get_emoji_suggestions_url(const string &language_code, Promise<string> &&promise);

void get_emoji_groups(EmojiGroupType group_type, Promise<td_api::object_ptr<td_api::emojiCategories>> &&promise);

td_api::object_ptr<td_api::httpUrl> get_emoji_suggestions_url_result(int64 random_id);

void reload_sticker_set(StickerSetId sticker_set_id, int64 access_hash, Promise<Unit> &&promise);

void reload_installed_sticker_sets(StickerType sticker_type, bool force);
Expand Down Expand Up @@ -987,9 +985,6 @@ class StickersManager final : public Actor {

void finish_get_emoji_keywords_difference(string language_code, int32 version);

void on_get_emoji_suggestions_url(int64 random_id, Promise<Unit> &&promise,
Result<telegram_api::object_ptr<telegram_api::emojiURL>> &&r_emoji_url);

void on_load_emoji_groups_from_database(EmojiGroupType group_type, string used_language_codes, string value);

void on_load_emoji_group_icons(EmojiGroupType group_type, EmojiGroupList group_list);
Expand Down Expand Up @@ -1134,7 +1129,6 @@ class StickersManager final : public Actor {
FlatHashSet<string> reloaded_emoji_keywords_;
FlatHashMap<string, vector<Promise<Unit>>> load_emoji_keywords_queries_;
FlatHashMap<string, vector<Promise<Unit>>> load_language_codes_queries_;
FlatHashMap<int64, string> emoji_suggestions_urls_;

struct GiftPremiumMessages {
FlatHashSet<MessageFullId, MessageFullIdHash> message_full_ids_;
Expand Down

0 comments on commit a33553e

Please sign in to comment.