Add td_api::botMediaPreview.

This commit is contained in:
levlam 2024-07-23 12:15:11 +03:00
parent 15eb2754bd
commit 5a01890272
3 changed files with 48 additions and 34 deletions

View File

@ -4393,13 +4393,18 @@ publicForwardStory story:story = PublicForward;
publicForwards total_count:int32 forwards:vector<PublicForward> next_offset:string = PublicForwards;
//@description Describes media previews of a bot
//@date Point in time (Unix timestamp) when the preview was added or changed last time
//@content Content of the preview
botMediaPreview date:int32 content:StoryContent = BotMediaPreview;
//@description Contains a list of media previews of a bot @previews List of media previews
botMediaPreviews previews:vector<StoryContent> = BotMediaPreviews;
botMediaPreviews previews:vector<botMediaPreview> = BotMediaPreviews;
//@description Contains a list of media previews of a bot for the given language and the list of langauges for which the bot has dedicated previews
//@previews List of media previews
//@language_codes List of language codes for which the bot has dedicated previews
botMediaPreviewInfo previews:vector<StoryContent> language_codes:vector<string> = BotMediaPreviewInfo;
botMediaPreviewInfo previews:vector<botMediaPreview> language_codes:vector<string> = BotMediaPreviewInfo;
//@description Contains a list of features available on a specific chat boost level
@ -10653,14 +10658,14 @@ getBotMediaPreviewInfo bot_user_id:int53 language_code:string = BotMediaPreviewI
//@language_code A two-letter ISO 639-1 language code for which preview is added. If empty, then the preview will be shown to all users for whose languages there are no dedicated previews.
//-If non-empty, then there must be an official language pack of the same name, which is returned by getLocalizationTargetInfo
//@content Content of the added preview
addBotMediaPreview bot_user_id:int53 language_code:string content:InputStoryContent = StoryContent;
addBotMediaPreview bot_user_id:int53 language_code:string content:InputStoryContent = BotMediaPreview;
//@description Replaces media preview in the list of media previews of a bot. Returns the new preview after edit is completed server-side
//@bot_user_id Identifier of the target bot. The bot must be owned and must have the main Web App
//@language_code Language code of the media preview to edit
//@file_id File identifier of the media to replace
//@content Content of the new preview
editBotMediaPreview bot_user_id:int53 language_code:string file_id:int32 content:InputStoryContent = StoryContent;
editBotMediaPreview bot_user_id:int53 language_code:string file_id:int32 content:InputStoryContent = BotMediaPreview;
//@description Changes order of media previews in the list of media previews of a bot
//@bot_user_id Identifier of the target bot. The bot must be owned and must have the main Web App

View File

@ -94,6 +94,19 @@ class SetBotBroadcastDefaultAdminRightsQuery final : public Td::ResultHandler {
}
};
static td_api::object_ptr<td_api::botMediaPreview> convert_bot_media_preview(
Td *td, telegram_api::object_ptr<telegram_api::botPreviewMedia> media, UserId bot_user_id,
vector<FileId> &file_ids) {
auto content = get_story_content(td, std::move(media->media_), DialogId(bot_user_id));
if (content == nullptr) {
LOG(ERROR) << "Receive invalid media preview for " << bot_user_id;
return nullptr;
}
append(file_ids, get_story_content_file_ids(td, content.get()));
return td_api::make_object<td_api::botMediaPreview>(max(media->date_, 0),
get_story_content_object(td, content.get()));
}
class GetPreviewMediasQuery final : public Td::ResultHandler {
Promise<td_api::object_ptr<td_api::botMediaPreviews>> promise_;
UserId bot_user_id_;
@ -117,15 +130,12 @@ 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<td_api::object_ptr<td_api::botMediaPreview>> previews;
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 media preview 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()));
for (auto &media : ptr) {
auto preview = convert_bot_media_preview(td_, std::move(media), bot_user_id_, file_ids);
if (preview != nullptr) {
previews.push_back(std::move(preview));
}
}
if (!file_ids.empty()) {
@ -134,8 +144,8 @@ class GetPreviewMediasQuery final : public Td::ResultHandler {
td_->file_manager_->add_file_source(file_id, file_source_id);
}
}
td_->user_manager_->on_update_bot_has_preview_medias(bot_user_id_, !contents.empty());
promise_.set_value(td_api::make_object<td_api::botMediaPreviews>(std::move(contents)));
td_->user_manager_->on_update_bot_has_preview_medias(bot_user_id_, !previews.empty());
promise_.set_value(td_api::make_object<td_api::botMediaPreviews>(std::move(previews)));
}
void on_error(Status status) final {
@ -169,15 +179,12 @@ class GetPreviewInfoQuery final : public Td::ResultHandler {
auto ptr = result_ptr.move_as_ok();
LOG(INFO) << "Receive result for GetPreviewInfoQuery: " << to_string(ptr);
vector<td_api::object_ptr<td_api::StoryContent>> contents;
vector<td_api::object_ptr<td_api::botMediaPreview>> previews;
vector<FileId> file_ids;
for (auto &media_ptr : ptr->media_) {
auto content = get_story_content(td_, std::move(media_ptr->media_), DialogId(bot_user_id_));
if (content == nullptr) {
LOG(ERROR) << "Receive invalid media preview 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()));
for (auto &media : ptr->media_) {
auto preview = convert_bot_media_preview(td_, std::move(media), bot_user_id_, file_ids);
if (preview != nullptr) {
previews.push_back(std::move(preview));
}
}
if (!file_ids.empty()) {
@ -188,7 +195,7 @@ class GetPreviewInfoQuery final : public Td::ResultHandler {
}
}
promise_.set_value(
td_api::make_object<td_api::botMediaPreviewInfo>(std::move(contents), std::move(ptr->lang_codes_)));
td_api::make_object<td_api::botMediaPreviewInfo>(std::move(previews), std::move(ptr->lang_codes_)));
}
void on_error(Status status) final {
@ -245,12 +252,12 @@ class BotInfoManager::AddPreviewMediaQuery final : public Td::ResultHandler {
auto ptr = result_ptr.move_as_ok();
LOG(INFO) << "Receive result for AddPreviewMediaQuery: " << to_string(ptr);
auto bot_user_id = pending_preview_->bot_user_id_;
auto content = get_story_content(td_, std::move(ptr->media_), DialogId(bot_user_id));
if (content == nullptr) {
LOG(ERROR) << "Receive invalid media preview";
vector<FileId> file_ids;
auto preview = convert_bot_media_preview(td_, std::move(ptr), bot_user_id, file_ids);
if (preview == nullptr) {
LOG(ERROR) << "Receive invalid sent media preview";
return pending_preview_->promise_.set_error(Status::Error(500, "Receive invalid preview"));
}
auto file_ids = get_story_content_file_ids(td_, content.get());
if (!file_ids.empty()) {
auto file_source_id = td_->bot_info_manager_->get_bot_media_preview_info_file_source_id(
bot_user_id, pending_preview_->language_code_);
@ -258,8 +265,10 @@ class BotInfoManager::AddPreviewMediaQuery final : public Td::ResultHandler {
td_->file_manager_->add_file_source(file_id, file_source_id);
}
}
td_->user_manager_->on_update_bot_has_preview_medias(pending_preview_->bot_user_id_, true);
pending_preview_->promise_.set_value(get_story_content_object(td_, content.get()));
if (pending_preview_->language_code_.empty()) {
td_->user_manager_->on_update_bot_has_preview_medias(bot_user_id, true);
}
pending_preview_->promise_.set_value(std::move(preview));
}
void on_error(Status status) final {
@ -746,7 +755,7 @@ void BotInfoManager::reload_bot_media_preview_info(UserId bot_user_id, const str
void BotInfoManager::add_bot_media_preview(UserId bot_user_id, const string &language_code,
td_api::object_ptr<td_api::InputStoryContent> &&input_content,
Promise<td_api::object_ptr<td_api::StoryContent>> &&promise) {
Promise<td_api::object_ptr<td_api::botMediaPreview>> &&promise) {
TRY_RESULT_PROMISE(promise, input_user, get_media_preview_bot_input_user(bot_user_id, true));
TRY_STATUS_PROMISE(promise, validate_bot_language_code(language_code));
TRY_RESULT_PROMISE(promise, content, get_input_story_content(td_, std::move(input_content), DialogId(bot_user_id)));
@ -762,7 +771,7 @@ void BotInfoManager::add_bot_media_preview(UserId bot_user_id, const string &lan
void BotInfoManager::edit_bot_media_preview(UserId bot_user_id, const string &language_code, FileId file_id,
td_api::object_ptr<td_api::InputStoryContent> &&input_content,
Promise<td_api::object_ptr<td_api::StoryContent>> &&promise) {
Promise<td_api::object_ptr<td_api::botMediaPreview>> &&promise) {
TRY_RESULT_PROMISE(promise, input_user, get_media_preview_bot_input_user(bot_user_id, true));
TRY_STATUS_PROMISE(promise, validate_bot_language_code(language_code));
TRY_RESULT_PROMISE(promise, content, get_input_story_content(td_, std::move(input_content), DialogId(bot_user_id)));

View File

@ -56,11 +56,11 @@ class BotInfoManager final : public Actor {
void add_bot_media_preview(UserId bot_user_id, const string &language_code,
td_api::object_ptr<td_api::InputStoryContent> &&input_content,
Promise<td_api::object_ptr<td_api::StoryContent>> &&promise);
Promise<td_api::object_ptr<td_api::botMediaPreview>> &&promise);
void edit_bot_media_preview(UserId bot_user_id, const string &language_code, FileId file_id,
td_api::object_ptr<td_api::InputStoryContent> &&input_content,
Promise<td_api::object_ptr<td_api::StoryContent>> &&promise);
Promise<td_api::object_ptr<td_api::botMediaPreview>> &&promise);
void reorder_bot_media_previews(UserId bot_user_id, const string &language_code, const vector<int32> &file_ids,
Promise<Unit> &&promise);
@ -96,7 +96,7 @@ class BotInfoManager final : public Actor {
unique_ptr<StoryContent> content_;
uint32 upload_order_ = 0;
bool was_reuploaded_ = false;
Promise<td_api::object_ptr<td_api::StoryContent>> promise_;
Promise<td_api::object_ptr<td_api::botMediaPreview>> promise_;
};
struct PendingSetBotInfoQuery {