2
0
mirror of https://github.com/ars3niy/tdlib-purple synced 2025-09-01 06:25:10 +00:00

Decide how to show downloaded file based on message type

Rather than use multiple callbacks
This commit is contained in:
Arseniy Lartsev
2020-10-12 18:15:24 +02:00
parent 147b6431e0
commit fe76205fd2
4 changed files with 55 additions and 57 deletions

View File

@@ -117,6 +117,7 @@ public:
struct TgMessageInfo { struct TgMessageInfo {
enum class Type { enum class Type {
Photo,
Sticker, Sticker,
Other Other
}; };

View File

@@ -311,6 +311,7 @@ void makeFullMessage(const td::td_api::chat &chat, td::td_api::object_ptr<td::td
if (message && message->content_) { if (message && message->content_) {
if (message->content_->get_id() == td::td_api::messagePhoto::ID) { if (message->content_->get_id() == td::td_api::messagePhoto::ID) {
messageInfo.type = TgMessageInfo::Type::Photo;
const td::td_api::messagePhoto &photo = static_cast<const td::td_api::messagePhoto &>(*message->content_); const td::td_api::messagePhoto &photo = static_cast<const td::td_api::messagePhoto &>(*message->content_);
const td::td_api::file *file = selectPhotoSize(account.purpleAccount, photo); const td::td_api::file *file = selectPhotoSize(account.purpleAccount, photo);
if (file) if (file)

View File

@@ -938,7 +938,7 @@ void PurpleTdClient::showPhotoMessage(const td::td_api::chat &chat, TgMessageInf
if (photoSize) { if (photoSize) {
// TRANSLATOR: File-type, used to describe what is being downloaded, in sentences like "Downloading photo" or "Ignoring photo download". // TRANSLATOR: File-type, used to describe what is being downloaded, in sentences like "Downloading photo" or "Ignoring photo download".
showFileInline(chat, message, *photoSize, captionCstr, _("photo"), nullptr, &PurpleTdClient::showDownloadedImage); showFileInline(chat, message, *photoSize, captionCstr, _("photo"), nullptr, &PurpleTdClient::showDownloadedFileInline);
} else { } else {
// Unlikely message not worth translating // Unlikely message not worth translating
std::string notice = makeNoticeWithSender(chat, message, "Faulty image", m_account); std::string notice = makeNoticeWithSender(chat, message, "Faulty image", m_account);
@@ -1002,37 +1002,32 @@ void PurpleTdClient::requestInlineDownload(const char *sender, const td::td_api:
_("_No"), ignoreInlineDownload); _("_No"), ignoreInlineDownload);
} }
void PurpleTdClient::showDownloadedImage(ChatId chatId, TgMessageInfo &message, void PurpleTdClient::showDownloadedImage(const td::td_api::chat &chat, TgMessageInfo &message,
const std::string &filePath, const char *caption, const std::string &filePath, const char *caption)
const std::string &fileDesc,
td::td_api::object_ptr<td::td_api::file> thumbnail)
{ {
const td::td_api::chat *chat = m_data.getChat(chatId); std::string text;
if (chat) { std::string notice;
std::string text; gchar *data = NULL;
std::string notice; size_t len = 0;
gchar *data = NULL;
size_t len = 0;
if (g_file_get_contents (filePath.c_str(), &data, &len, NULL)) { if (g_file_get_contents (filePath.c_str(), &data, &len, NULL)) {
int id = purple_imgstore_add_with_id (data, len, NULL); int id = purple_imgstore_add_with_id (data, len, NULL);
text = makeInlineImageText(id); text = makeInlineImageText(id);
} else if (filePath.find('"') == std::string::npos) } else if (filePath.find('"') == std::string::npos)
text = "<img src=\"file://" + filePath + "\">"; text = "<img src=\"file://" + filePath + "\">";
else { else {
// Unlikely error, not worth translating // Unlikely error, not worth translating
notice = makeNoticeWithSender(*chat, message, "Cannot show photo: file path contains quotes", m_account); notice = makeNoticeWithSender(chat, message, "Cannot show photo: file path contains quotes", m_account);
}
if (caption && *caption) {
if (!text.empty())
text += "\n";
text += caption;
}
showMessageText(m_data, *chat, message, text.empty() ? NULL : text.c_str(),
notice.empty() ? NULL : notice.c_str(), PURPLE_MESSAGE_IMAGES);
} }
if (caption && *caption) {
if (!text.empty())
text += "\n";
text += caption;
}
showMessageText(m_data, chat, message, text.empty() ? NULL : text.c_str(),
notice.empty() ? NULL : notice.c_str(), PURPLE_MESSAGE_IMAGES);
} }
void PurpleTdClient::showFileMessage(const td::td_api::chat &chat, TgMessageInfo &message, void PurpleTdClient::showFileMessage(const td::td_api::chat &chat, TgMessageInfo &message,
@@ -1072,7 +1067,7 @@ void PurpleTdClient::showStickerMessage(const td::td_api::chat &chat, TgMessageI
// TRANSLATOR: File-type, used to describe what is being downloaded, in sentences like "Downloading photo" or "Ignoring photo download". // TRANSLATOR: File-type, used to describe what is being downloaded, in sentences like "Downloading photo" or "Ignoring photo download".
showFileInline(chat, message, *sticker.sticker_, NULL, _("sticker"), std::move(thumbnail), showFileInline(chat, message, *sticker.sticker_, NULL, _("sticker"), std::move(thumbnail),
&PurpleTdClient::showDownloadedSticker); &PurpleTdClient::showDownloadedFileInline);
} }
} }
@@ -1081,8 +1076,8 @@ static bool isTgs(const std::string &path)
return (path.size() >= 4) && !strcmp(path.c_str() + path.size() - 4, ".tgs"); return (path.size() >= 4) && !strcmp(path.c_str() + path.size() - 4, ".tgs");
} }
void PurpleTdClient::showDownloadedSticker(ChatId chatId, TgMessageInfo &message, void PurpleTdClient::showDownloadedSticker(const td::td_api::chat &chat, TgMessageInfo &message,
const std::string &filePath, const char *caption, const std::string &filePath,
const std::string &fileDescription, const std::string &fileDescription,
td::td_api::object_ptr<td::td_api::file> thumbnail) td::td_api::object_ptr<td::td_api::file> thumbnail)
{ {
@@ -1095,34 +1090,27 @@ void PurpleTdClient::showDownloadedSticker(ChatId chatId, TgMessageInfo &message
#endif #endif
if (isTgs(filePath)) { if (isTgs(filePath)) {
if (convertAnimated) { if (convertAnimated) {
const td::td_api::chat *chat = m_data.getChat(chatId); // TRANSLATOR: In-chat status update
if (chat) { std::string notice = makeNoticeWithSender(chat, message, _("Converting sticker"), m_account);
// TRANSLATOR: In-chat status update showMessageText(m_data, chat, message, NULL, notice.c_str());
std::string notice = makeNoticeWithSender(*chat, message, _("Converting sticker"), m_account);
showMessageText(m_data, *chat, message, NULL, notice.c_str());
}
StickerConversionThread *thread; StickerConversionThread *thread;
thread = new StickerConversionThread(m_account, &PurpleTdClient::showConvertedAnimation, thread = new StickerConversionThread(m_account, &PurpleTdClient::showConvertedAnimation,
filePath, chatId, std::move(message)); filePath, getId(chat), std::move(message));
thread->startThread(); thread->startThread();
} else if (thumbnail) { } else if (thumbnail) {
// Avoid message like "Downloading sticker thumbnail... // Avoid message like "Downloading sticker thumbnail...
// Also ignore size limits, but only determined testers and crazy people would notice. // Also ignore size limits, but only determined testers and crazy people would notice.
if (thumbnail->local_ && thumbnail->local_->is_downloading_completed_) if (thumbnail->local_ && thumbnail->local_->is_downloading_completed_)
showDownloadedSticker(chatId, message, thumbnail->local_->path_, caption, showDownloadedSticker(chat, message, thumbnail->local_->path_,
fileDescription, nullptr); fileDescription, nullptr);
else else
downloadFileInline(thumbnail->id_, chatId, message, fileDescription, nullptr, downloadFileInline(thumbnail->id_, getId(chat), message, fileDescription, nullptr,
&PurpleTdClient::showDownloadedSticker, this, m_transceiver, m_data); &PurpleTdClient::showDownloadedFileInline, this, m_transceiver, m_data);
} else { } else {
const td::td_api::chat *chat = m_data.getChat(chatId); showGenericFileInline(chat, message, filePath, fileDescription, m_data);
if (chat)
showGenericFileInline(*chat, message, filePath, fileDescription, m_data);
} }
} else { } else {
const td::td_api::chat *chat = m_data.getChat(chatId); showWebpSticker(chat, message, filePath, fileDescription, m_data);
if (chat)
showWebpSticker(*chat, message, filePath, fileDescription, m_data);
} }
} }
@@ -1171,8 +1159,19 @@ void PurpleTdClient::showDownloadedFileInline(ChatId chatId, TgMessageInfo &mess
td::td_api::object_ptr<td::td_api::file> thumbnail) td::td_api::object_ptr<td::td_api::file> thumbnail)
{ {
const td::td_api::chat *chat = m_data.getChat(chatId); const td::td_api::chat *chat = m_data.getChat(chatId);
if (chat) if (!chat) return;
switch (message.type) {
case TgMessageInfo::Type::Photo:
showDownloadedImage(*chat, message, filePath, caption);
break;
case TgMessageInfo::Type::Sticker:
showDownloadedSticker(*chat, message, filePath, fileDescription, std::move(thumbnail));
break;
case TgMessageInfo::Type::Other:
showGenericFileInline(*chat, message, filePath, fileDescription, m_data); showGenericFileInline(*chat, message, filePath, fileDescription, m_data);
break;
}
} }
void PurpleTdClient::showMessage(const td::td_api::chat &chat, IncomingMessage &fullMessage) void PurpleTdClient::showMessage(const td::td_api::chat &chat, IncomingMessage &fullMessage)

View File

@@ -146,17 +146,14 @@ private:
TgMessageInfo &message, FileDownloadCb callback); TgMessageInfo &message, FileDownloadCb callback);
static void startInlineDownload(void *user_data); static void startInlineDownload(void *user_data);
void inlineDownloadResponse(uint64_t requestId, td::td_api::object_ptr<td::td_api::Object> object); void inlineDownloadResponse(uint64_t requestId, td::td_api::object_ptr<td::td_api::Object> object);
void showDownloadedImage(ChatId chatId, TgMessageInfo &message, void showDownloadedImage(const td::td_api::chat &chat, TgMessageInfo &message,
const std::string &filePath, const char *caption, const std::string &filePath, const char *caption);
const std::string &fileDesc,
td::td_api::object_ptr<td::td_api::file> thumbnail);
void showDownloadedFileInline(ChatId chatId, TgMessageInfo &message, void showDownloadedFileInline(ChatId chatId, TgMessageInfo &message,
const std::string &filePath, const char *caption, const std::string &filePath, const char *caption,
const std::string &fileDescription, const std::string &fileDescription,
td::td_api::object_ptr<td::td_api::file> thumbnail); td::td_api::object_ptr<td::td_api::file> thumbnail);
void showDownloadedSticker(ChatId chatId, TgMessageInfo &message, void showDownloadedSticker(const td::td_api::chat &chat, TgMessageInfo &message,
const std::string &filePath, const char *caption, const std::string &filePath, const std::string &fileDescription,
const std::string &fileDescription,
td::td_api::object_ptr<td::td_api::file> thumbnail); td::td_api::object_ptr<td::td_api::file> thumbnail);
void showConvertedAnimation(AccountThread *arg); void showConvertedAnimation(AccountThread *arg);
void sendMessageCreatePrivateChatResponse(uint64_t requestId, td::td_api::object_ptr<td::td_api::Object> object); void sendMessageCreatePrivateChatResponse(uint64_t requestId, td::td_api::object_ptr<td::td_api::Object> object);