Skip to content

Commit

Permalink
Remove invoices and games from inline query results in unallowed chats.
Browse files Browse the repository at this point in the history
  • Loading branch information
levlam committed Apr 13, 2021
1 parent 20096b3 commit 7e9818f
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 36 deletions.
95 changes: 61 additions & 34 deletions td/telegram/InlineQueriesManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "td/telegram/InputMessageText.h"
#include "td/telegram/Location.h"
#include "td/telegram/MessageContent.h"
#include "td/telegram/MessageContentType.h"
#include "td/telegram/MessageEntity.h"
#include "td/telegram/MessagesManager.h"
#include "td/telegram/misc.h"
Expand Down Expand Up @@ -59,6 +60,7 @@ namespace td {

class GetInlineBotResultsQuery : public Td::ResultHandler {
Promise<Unit> promise_;
DialogId dialog_id_;
UserId bot_user_id_;
uint64 query_hash_;

Expand All @@ -68,11 +70,12 @@ class GetInlineBotResultsQuery : public Td::ResultHandler {
explicit GetInlineBotResultsQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
}

NetQueryRef send(UserId bot_user_id, tl_object_ptr<telegram_api::InputUser> bot_input_user,
NetQueryRef send(UserId bot_user_id, DialogId dialog_id, tl_object_ptr<telegram_api::InputUser> bot_input_user,
tl_object_ptr<telegram_api::InputPeer> input_peer, Location user_location, const string &query,
const string &offset, uint64 query_hash) {
CHECK(input_peer != nullptr);
bot_user_id_ = bot_user_id;
dialog_id_ = dialog_id;
query_hash_ = query_hash;
int32 flags = 0;
if (!user_location.empty()) {
Expand All @@ -94,7 +97,8 @@ class GetInlineBotResultsQuery : public Td::ResultHandler {
return on_error(id, result_ptr.move_as_error());
}

td->inline_queries_manager_->on_get_inline_query_results(bot_user_id_, query_hash_, result_ptr.move_as_ok());
td->inline_queries_manager_->on_get_inline_query_results(dialog_id_, bot_user_id_, query_hash_,
result_ptr.move_as_ok());
promise_.set_value(Unit());
}

Expand All @@ -106,7 +110,7 @@ class GetInlineBotResultsQuery : public Td::ResultHandler {
}
LOG(INFO) << "Inline query returned error " << status;

td->inline_queries_manager_->on_get_inline_query_results(bot_user_id_, query_hash_, nullptr);
td->inline_queries_manager_->on_get_inline_query_results(dialog_id_, bot_user_id_, query_hash_, nullptr);
promise_.set_error(std::move(status));
}
};
Expand Down Expand Up @@ -298,11 +302,15 @@ Result<tl_object_ptr<telegram_api::InputBotInlineMessage>> InlineQueriesManager:

bool InlineQueriesManager::register_inline_message_content(
int64 query_id, const string &result_id, FileId file_id,
tl_object_ptr<telegram_api::BotInlineMessage> &&inline_message, int32 allowed_media_content_id, Photo *photo,
Game *game) {
tl_object_ptr<telegram_api::BotInlineMessage> &&inline_message, int32 allowed_media_content_id, bool allow_invoice,
Photo *photo, Game *game) {
InlineMessageContent content =
create_inline_message_content(td_, file_id, std::move(inline_message), allowed_media_content_id, photo, game);
if (content.message_content != nullptr) {
if (!allow_invoice && content.message_content->get_type() == MessageContentType::Invoice) {
return false;
}

inline_message_contents_[query_id].emplace(result_id, std::move(content));
return true;
}
Expand Down Expand Up @@ -817,12 +825,13 @@ uint64 InlineQueriesManager::send_inline_query(UserId bot_user_id, DialogId dial

if (pending_inline_query_ != nullptr) {
LOG(INFO) << "Drop inline query " << pending_inline_query_->query_hash;
on_get_inline_query_results(pending_inline_query_->bot_user_id, pending_inline_query_->query_hash, nullptr);
on_get_inline_query_results(pending_inline_query_->dialog_id, pending_inline_query_->bot_user_id,
pending_inline_query_->query_hash, nullptr);
pending_inline_query_->promise.set_error(Status::Error(406, "Request cancelled"));
}

pending_inline_query_ = make_unique<PendingInlineQuery>(PendingInlineQuery{
query_hash, bot_user_id, std::move(input_peer), user_location, query, offset, std::move(promise)});
query_hash, bot_user_id, dialog_id, std::move(input_peer), user_location, query, offset, std::move(promise)});

loop();

Expand All @@ -846,7 +855,7 @@ void InlineQueriesManager::loop() {
}
sent_query_ =
td_->create_handler<GetInlineBotResultsQuery>(std::move(pending_inline_query_->promise))
->send(pending_inline_query_->bot_user_id, std::move(bot_input_user),
->send(pending_inline_query_->bot_user_id, pending_inline_query_->dialog_id, std::move(bot_input_user),
std::move(pending_inline_query_->input_peer), pending_inline_query_->user_location,
pending_inline_query_->query, pending_inline_query_->offset, pending_inline_query_->query_hash);

Expand Down Expand Up @@ -1225,7 +1234,7 @@ string InlineQueriesManager::get_web_document_content_type(
return {};
}

void InlineQueriesManager::on_get_inline_query_results(UserId bot_user_id, uint64 query_hash,
void InlineQueriesManager::on_get_inline_query_results(DialogId dialog_id, UserId bot_user_id, uint64 query_hash,
tl_object_ptr<telegram_api::messages_botResults> &&results) {
LOG(INFO) << "Receive results for inline query " << query_hash;
if (results == nullptr) {
Expand All @@ -1236,6 +1245,8 @@ void InlineQueriesManager::on_get_inline_query_results(UserId bot_user_id, uint6

td_->contacts_manager_->on_get_users(std::move(results->users_), "on_get_inline_query_results");

auto dialog_type = dialog_id.get_type();
bool allow_invoice = dialog_type != DialogType::SecretChat;
vector<tl_object_ptr<td_api::InlineQueryResult>> output_results;
for (auto &result_ptr : results->results_) {
tl_object_ptr<td_api::InlineQueryResult> output_result;
Expand All @@ -1251,6 +1262,14 @@ void InlineQueriesManager::on_get_inline_query_results(UserId bot_user_id, uint6
LOG(ERROR) << "Receive game without photo in the result of inline query: " << to_string(result);
break;
}
if (dialog_type == DialogType::Channel &&
td_->contacts_manager_->get_channel_type(dialog_id.get_channel_id()) == ChannelType::Broadcast) {
continue;
}
if (dialog_type == DialogType::SecretChat) {
continue;
}

auto game = make_tl_object<td_api::inlineQueryResultGame>();
Game inline_game(td_, std::move(result->title_), std::move(result->description_), std::move(result->photo_),
std::move(result->document_), DialogId());
Expand All @@ -1259,8 +1278,8 @@ void InlineQueriesManager::on_get_inline_query_results(UserId bot_user_id, uint6
game->game_ = inline_game.get_game_object(td_);

if (!register_inline_message_content(results->query_id_, game->id_, FileId(),
std::move(result->send_message_), td_api::inputMessageGame::ID, nullptr,
&inline_game)) {
std::move(result->send_message_), td_api::inputMessageGame::ID,
allow_invoice, nullptr, &inline_game)) {
continue;
}
output_result = std::move(game);
Expand All @@ -1286,8 +1305,8 @@ void InlineQueriesManager::on_get_inline_query_results(UserId bot_user_id, uint6
animation->title_ = std::move(result->title_);

if (!register_inline_message_content(results->query_id_, animation->id_, parsed_document.file_id,
std::move(result->send_message_),
td_api::inputMessageAnimation::ID)) {
std::move(result->send_message_), td_api::inputMessageAnimation::ID,
allow_invoice)) {
continue;
}
output_result = std::move(animation);
Expand All @@ -1301,7 +1320,8 @@ void InlineQueriesManager::on_get_inline_query_results(UserId bot_user_id, uint6
audio->audio_ = td_->audios_manager_->get_audio_object(parsed_document.file_id);

if (!register_inline_message_content(results->query_id_, audio->id_, parsed_document.file_id,
std::move(result->send_message_), td_api::inputMessageAudio::ID)) {
std::move(result->send_message_), td_api::inputMessageAudio::ID,
allow_invoice)) {
continue;
}
output_result = std::move(audio);
Expand All @@ -1318,8 +1338,8 @@ void InlineQueriesManager::on_get_inline_query_results(UserId bot_user_id, uint6
document->description_ = std::move(result->description_);

if (!register_inline_message_content(results->query_id_, document->id_, parsed_document.file_id,
std::move(result->send_message_),
td_api::inputMessageDocument::ID)) {
std::move(result->send_message_), td_api::inputMessageDocument::ID,
allow_invoice)) {
continue;
}
output_result = std::move(document);
Expand All @@ -1333,7 +1353,8 @@ void InlineQueriesManager::on_get_inline_query_results(UserId bot_user_id, uint6
sticker->sticker_ = td_->stickers_manager_->get_sticker_object(parsed_document.file_id);

if (!register_inline_message_content(results->query_id_, sticker->id_, parsed_document.file_id,
std::move(result->send_message_), td_api::inputMessageSticker::ID)) {
std::move(result->send_message_), td_api::inputMessageSticker::ID,
allow_invoice)) {
continue;
}
output_result = std::move(sticker);
Expand All @@ -1349,7 +1370,8 @@ void InlineQueriesManager::on_get_inline_query_results(UserId bot_user_id, uint6
video->description_ = std::move(result->description_);

if (!register_inline_message_content(results->query_id_, video->id_, parsed_document.file_id,
std::move(result->send_message_), td_api::inputMessageVideo::ID)) {
std::move(result->send_message_), td_api::inputMessageVideo::ID,
allow_invoice)) {
continue;
}
output_result = std::move(video);
Expand All @@ -1367,8 +1389,8 @@ void InlineQueriesManager::on_get_inline_query_results(UserId bot_user_id, uint6
voice_note->title_ = std::move(result->title_);

if (!register_inline_message_content(results->query_id_, voice_note->id_, parsed_document.file_id,
std::move(result->send_message_),
td_api::inputMessageVoiceNote::ID)) {
std::move(result->send_message_), td_api::inputMessageVoiceNote::ID,
allow_invoice)) {
continue;
}
output_result = std::move(voice_note);
Expand All @@ -1395,7 +1417,8 @@ void InlineQueriesManager::on_get_inline_query_results(UserId bot_user_id, uint6
photo->description_ = std::move(result->description_);

if (!register_inline_message_content(results->query_id_, photo->id_, FileId(),
std::move(result->send_message_), td_api::inputMessagePhoto::ID, &p)) {
std::move(result->send_message_), td_api::inputMessagePhoto::ID,
allow_invoice, &p)) {
continue;
}
output_result = std::move(photo);
Expand Down Expand Up @@ -1423,7 +1446,7 @@ void InlineQueriesManager::on_get_inline_query_results(UserId bot_user_id, uint6
article->thumbnail_ = register_thumbnail(std::move(result->thumb_));

if (!register_inline_message_content(results->query_id_, article->id_, FileId(),
std::move(result->send_message_), -1)) {
std::move(result->send_message_), -1, allow_invoice)) {
continue;
}
output_result = std::move(article);
Expand All @@ -1443,7 +1466,7 @@ void InlineQueriesManager::on_get_inline_query_results(UserId bot_user_id, uint6
contact->thumbnail_ = register_thumbnail(std::move(result->thumb_));

if (!register_inline_message_content(results->query_id_, contact->id_, FileId(),
std::move(result->send_message_), -1)) {
std::move(result->send_message_), -1, allow_invoice)) {
continue;
}
output_result = std::move(contact);
Expand All @@ -1464,7 +1487,7 @@ void InlineQueriesManager::on_get_inline_query_results(UserId bot_user_id, uint6
location->thumbnail_ = register_thumbnail(std::move(result->thumb_));

if (!register_inline_message_content(results->query_id_, location->id_, FileId(),
std::move(result->send_message_), -1)) {
std::move(result->send_message_), -1, allow_invoice)) {
continue;
}
output_result = std::move(location);
Expand All @@ -1491,7 +1514,7 @@ void InlineQueriesManager::on_get_inline_query_results(UserId bot_user_id, uint6
venue->thumbnail_ = register_thumbnail(std::move(result->thumb_));

if (!register_inline_message_content(results->query_id_, venue->id_, FileId(),
std::move(result->send_message_), -1)) {
std::move(result->send_message_), -1, allow_invoice)) {
continue;
}
output_result = std::move(venue);
Expand Down Expand Up @@ -1521,7 +1544,7 @@ void InlineQueriesManager::on_get_inline_query_results(UserId bot_user_id, uint6

if (!register_inline_message_content(results->query_id_, photo->id_, FileId(),
std::move(result->send_message_), td_api::inputMessagePhoto::ID,
&new_photo)) {
allow_invoice, &new_photo)) {
continue;
}
output_result = std::move(photo);
Expand Down Expand Up @@ -1573,7 +1596,8 @@ void InlineQueriesManager::on_get_inline_query_results(UserId bot_user_id, uint6
audio->id_ = std::move(result->id_);
audio->audio_ = td_->audios_manager_->get_audio_object(file_id);
if (!register_inline_message_content(results->query_id_, audio->id_, file_id,
std::move(result->send_message_), td_api::inputMessageAudio::ID)) {
std::move(result->send_message_), td_api::inputMessageAudio::ID,
allow_invoice)) {
continue;
}
output_result = std::move(audio);
Expand All @@ -1584,7 +1608,8 @@ void InlineQueriesManager::on_get_inline_query_results(UserId bot_user_id, uint6
document->title_ = std::move(result->title_);
document->description_ = std::move(result->description_);
if (!register_inline_message_content(results->query_id_, document->id_, file_id,
std::move(result->send_message_), td_api::inputMessageDocument::ID)) {
std::move(result->send_message_), td_api::inputMessageDocument::ID,
allow_invoice)) {
continue;
}
output_result = std::move(document);
Expand All @@ -1595,7 +1620,8 @@ void InlineQueriesManager::on_get_inline_query_results(UserId bot_user_id, uint6
td_->animations_manager_->get_animation_object(file_id, "inlineQueryResultAnimationCached");
animation->title_ = std::move(result->title_);
if (!register_inline_message_content(results->query_id_, animation->id_, file_id,
std::move(result->send_message_), td_api::inputMessageAnimation::ID)) {
std::move(result->send_message_), td_api::inputMessageAnimation::ID,
allow_invoice)) {
continue;
}
output_result = std::move(animation);
Expand All @@ -1604,7 +1630,8 @@ void InlineQueriesManager::on_get_inline_query_results(UserId bot_user_id, uint6
sticker->id_ = std::move(result->id_);
sticker->sticker_ = td_->stickers_manager_->get_sticker_object(file_id);
if (!register_inline_message_content(results->query_id_, sticker->id_, file_id,
std::move(result->send_message_), td_api::inputMessageSticker::ID)) {
std::move(result->send_message_), td_api::inputMessageSticker::ID,
allow_invoice)) {
continue;
}
output_result = std::move(sticker);
Expand All @@ -1615,7 +1642,8 @@ void InlineQueriesManager::on_get_inline_query_results(UserId bot_user_id, uint6
video->title_ = std::move(result->title_);
video->description_ = std::move(result->description_);
if (!register_inline_message_content(results->query_id_, video->id_, file_id,
std::move(result->send_message_), td_api::inputMessageVideo::ID)) {
std::move(result->send_message_), td_api::inputMessageVideo::ID,
allow_invoice)) {
continue;
}
output_result = std::move(video);
Expand All @@ -1625,7 +1653,8 @@ void InlineQueriesManager::on_get_inline_query_results(UserId bot_user_id, uint6
voice_note->voice_note_ = td_->voice_notes_manager_->get_voice_note_object(file_id);
voice_note->title_ = std::move(result->title_);
if (!register_inline_message_content(results->query_id_, voice_note->id_, file_id,
std::move(result->send_message_), td_api::inputMessageVoiceNote::ID)) {
std::move(result->send_message_), td_api::inputMessageVoiceNote::ID,
allow_invoice)) {
continue;
}
output_result = std::move(voice_note);
Expand Down Expand Up @@ -1759,8 +1788,6 @@ bool InlineQueriesManager::load_recently_used_bots(Promise<Unit> &promise) {
}

tl_object_ptr<td_api::inlineQueryResults> InlineQueriesManager::get_inline_query_results_object(uint64 query_hash) {
// TODO filter out games if request is sent in a broadcast channel or in a secret chat
// TODO filter out invoice messages if request is sent in a secret chat
return decrease_pending_request_count(query_hash);
}

Expand Down
Loading

0 comments on commit 7e9818f

Please sign in to comment.