Support file reference repair for bot media previews.

This commit is contained in:
levlam 2024-07-18 18:32:57 +03:00
parent daa7c39688
commit cdd7af6ade
5 changed files with 53 additions and 2 deletions

View File

@ -7,6 +7,8 @@
#include "td/telegram/BotInfoManager.h"
#include "td/telegram/AuthManager.h"
#include "td/telegram/FileReferenceManager.h"
#include "td/telegram/files/FileManager.h"
#include "td/telegram/Global.h"
#include "td/telegram/misc.h"
#include "td/telegram/net/NetQueryCreator.h"
@ -115,14 +117,22 @@ class GetPreviewMediasQuery final : public Td::ResultHandler {
auto ptr = result_ptr.move_as_ok();
LOG(INFO) << "Receive result for GetPreviewMediasQuery: " << to_string(ptr);
vector<td_api::object_ptr<td_api::StoryContent>> contents;
vector<FileId> file_ids;
for (auto &media_ptr : ptr) {
auto content = get_story_content(td_, std::move(media_ptr->media_), DialogId(bot_user_id_));
if (content == nullptr) {
LOG(ERROR) << "Receive invalid preview media for " << bot_user_id_;
} else {
append(file_ids, get_story_content_file_ids(td_, content.get()));
contents.push_back(get_story_content_object(td_, content.get()));
}
}
if (!file_ids.empty()) {
auto file_source_id = td_->bot_info_manager_->get_bot_media_preview_file_source_id(bot_user_id_);
for (auto file_id : file_ids) {
td_->file_manager_->add_file_source(file_id, file_source_id);
}
}
promise_.set_value(td_api::make_object<td_api::botMediaPreviews>(std::move(contents)));
}
@ -433,6 +443,19 @@ void BotInfoManager::allow_bot_to_send_messages(UserId bot_user_id, Promise<Unit
td_->create_handler<AllowBotSendMessageQuery>(std::move(promise))->send(bot_user_id);
}
FileSourceId BotInfoManager::get_bot_media_preview_file_source_id(UserId bot_user_id) {
if (!bot_user_id.is_valid()) {
return FileSourceId();
}
auto &source_id = bot_media_preview_file_source_ids_[bot_user_id];
if (!source_id.is_valid()) {
source_id = td_->file_reference_manager_->create_bot_media_preview_file_source(bot_user_id);
}
VLOG(file_references) << "Return " << source_id << " for media previews of " << bot_user_id;
return source_id;
}
Result<telegram_api::object_ptr<telegram_api::InputUser>> BotInfoManager::get_media_preview_bot_input_user(
UserId user_id, bool can_be_edited) {
TRY_RESULT(bot_data, td_->user_manager_->get_bot_data(user_id));

View File

@ -7,11 +7,13 @@
#pragma once
#include "td/telegram/DialogParticipant.h"
#include "td/telegram/files/FileSourceId.h"
#include "td/telegram/UserId.h"
#include "td/actor/actor.h"
#include "td/utils/common.h"
#include "td/utils/FlatHashMap.h"
#include "td/utils/Promise.h"
namespace td {
@ -30,6 +32,8 @@ class BotInfoManager final : public Actor {
void allow_bot_to_send_messages(UserId bot_user_id, Promise<Unit> &&promise);
FileSourceId get_bot_media_preview_file_source_id(UserId bot_user_id);
void get_bot_media_previews(UserId bot_user_id, Promise<td_api::object_ptr<td_api::botMediaPreviews>> &&promise);
void reload_bot_media_previews(UserId bot_user_id, Promise<Unit> &&promise);
@ -97,6 +101,8 @@ class BotInfoManager final : public Actor {
vector<PendingGetBotInfoQuery> pending_get_bot_info_queries_;
FlatHashMap<UserId, FileSourceId, UserIdHash> bot_media_preview_file_source_ids_;
Td *td_;
ActorShared<> parent_;
};

View File

@ -9,6 +9,7 @@
#include "td/telegram/AnimationsManager.h"
#include "td/telegram/AttachMenuManager.h"
#include "td/telegram/BackgroundManager.h"
#include "td/telegram/BotInfoManager.h"
#include "td/telegram/ChatManager.h"
#include "td/telegram/ConfigManager.h"
#include "td/telegram/DialogManager.h"
@ -85,6 +86,7 @@ fileSourceWebApp user_id:int53 short_name:string = FileSource;
fileSourceStory chat_id:int53 story_id:int32 = FileSource; // stories.getStoriesByID
fileSourceQuickReplyMessage shortcut_id:int32 message_id:int53 = FileSource; // messages.getQuickReplyMessages
fileSourceStarTransaction chat_id:int53 transaction_id:string is_refund:Bool = FileSource; // payments.getStarsTransactionsByID
fileSourceBotMediaPreview bot_user_id:int53 = FileSource; // bots.getPreviewMedias
*/
FileSourceId FileReferenceManager::get_current_file_source_id() const {
@ -185,6 +187,11 @@ FileSourceId FileReferenceManager::create_star_transaction_file_source(DialogId
return add_file_source_id(source, PSLICE() << "star transaction " << transaction_id << " in " << dialog_id);
}
FileSourceId FileReferenceManager::create_bot_media_preview_file_source(UserId bot_user_id) {
FileSourceBotMediaPreview source{bot_user_id};
return add_file_source_id(source, PSLICE() << "bot media preview " << bot_user_id);
}
FileReferenceManager::Node &FileReferenceManager::add_node(NodeId node_id) {
CHECK(node_id.is_valid());
auto &node = nodes_[node_id];
@ -408,6 +415,10 @@ void FileReferenceManager::send_query(Destination dest, FileSourceId file_source
[&](const FileSourceStarTransaction &source) {
send_closure_later(G()->star_manager(), &StarManager::reload_star_transaction, source.dialog_id,
source.transaction_id, source.is_refund, std::move(promise));
},
[&](const FileSourceBotMediaPreview &source) {
send_closure_later(G()->bot_info_manager(), &BotInfoManager::reload_bot_media_previews, source.bot_user_id,
std::move(promise));
}));
}

View File

@ -70,6 +70,7 @@ class FileReferenceManager final : public Actor {
FileSourceId create_story_file_source(StoryFullId story_full_id);
FileSourceId create_quick_reply_message_file_source(QuickReplyMessageFullId message_full_id);
FileSourceId create_star_transaction_file_source(DialogId dialog_id, const string &transaction_id, bool is_refund);
FileSourceId create_bot_media_preview_file_source(UserId bot_user_id);
using NodeId = FileId;
void repair_file_reference(NodeId node_id, Promise<> promise);
@ -186,6 +187,9 @@ class FileReferenceManager final : public Actor {
string transaction_id;
bool is_refund;
};
struct FileSourceBotMediaPreview {
UserId bot_user_id;
};
// append only
using FileSource =
@ -193,7 +197,7 @@ class FileReferenceManager final : public Actor {
FileSourceWebPage, FileSourceSavedAnimations, FileSourceRecentStickers, FileSourceFavoriteStickers,
FileSourceBackground, FileSourceChatFull, FileSourceChannelFull, FileSourceAppConfig,
FileSourceSavedRingtones, FileSourceUserFull, FileSourceAttachMenuBot, FileSourceWebApp, FileSourceStory,
FileSourceQuickReplyMessage, FileSourceStarTransaction>;
FileSourceQuickReplyMessage, FileSourceStarTransaction, FileSourceBotMediaPreview>;
WaitFreeVector<FileSource> file_sources_;
int64 query_generation_{0};

View File

@ -9,6 +9,7 @@
#include "td/telegram/AnimationsManager.h"
#include "td/telegram/AttachMenuManager.h"
#include "td/telegram/BackgroundManager.h"
#include "td/telegram/BotInfoManager.h"
#include "td/telegram/ChannelId.h"
#include "td/telegram/ChatId.h"
#include "td/telegram/ChatManager.h"
@ -71,7 +72,8 @@ void FileReferenceManager::store_file_source(FileSourceId file_source_id, Storer
td::store(source.dialog_id, storer);
td::store(source.transaction_id, storer);
td::store(source.is_refund, storer);
}));
},
[&](const FileSourceBotMediaPreview &source) { td::store(source.bot_user_id, storer); }));
}
template <class ParserT>
@ -173,6 +175,11 @@ FileSourceId FileReferenceManager::parse_file_source(Td *td, ParserT &parser) {
td::parse(is_refund, parser);
return td->star_manager_->get_star_transaction_file_source_id(dialog_id, transaction_id, is_refund);
}
case 20: {
UserId bot_user_id;
td::parse(bot_user_id, parser);
return td->bot_info_manager_->get_bot_media_preview_file_source_id(bot_user_id);
}
default:
parser.set_error("Invalid type in FileSource");
return FileSourceId();