2016-04-04 21:09:46 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2016-04-04 21:09:46 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2016-04-04 21:09:46 +00:00
|
|
|
*/
|
|
|
|
#include "inline_bots/inline_bot_result.h"
|
|
|
|
|
2019-09-16 11:14:06 +00:00
|
|
|
#include "api/api_text_entities.h"
|
2021-09-15 10:21:45 +00:00
|
|
|
#include "base/random.h"
|
2017-09-26 11:49:16 +00:00
|
|
|
#include "data/data_photo.h"
|
|
|
|
#include "data/data_document.h"
|
2018-01-17 16:21:01 +00:00
|
|
|
#include "data/data_session.h"
|
2021-06-17 23:28:09 +00:00
|
|
|
#include "data/data_file_click_handler.h"
|
2019-12-05 08:32:33 +00:00
|
|
|
#include "data/data_file_origin.h"
|
2020-05-25 14:16:04 +00:00
|
|
|
#include "data/data_photo_media.h"
|
2020-04-10 13:18:51 +00:00
|
|
|
#include "data/data_document_media.h"
|
2023-01-10 18:56:20 +00:00
|
|
|
#include "history/history.h"
|
2021-10-02 11:28:21 +00:00
|
|
|
#include "history/history_item_reply_markup.h"
|
2016-04-04 21:09:46 +00:00
|
|
|
#include "inline_bots/inline_bot_layout_item.h"
|
|
|
|
#include "inline_bots/inline_bot_send_data.h"
|
2017-03-04 10:23:56 +00:00
|
|
|
#include "storage/file_download.h"
|
2017-02-28 14:05:30 +00:00
|
|
|
#include "core/file_utilities.h"
|
2018-06-13 22:06:21 +00:00
|
|
|
#include "core/mime_type.h"
|
2018-10-23 09:44:42 +00:00
|
|
|
#include "ui/image/image.h"
|
2020-04-16 12:37:26 +00:00
|
|
|
#include "ui/image/image_location_factory.h"
|
2016-04-04 21:09:46 +00:00
|
|
|
#include "mainwidget.h"
|
2019-07-24 11:45:24 +00:00
|
|
|
#include "main/main_session.h"
|
2023-11-15 00:01:43 +00:00
|
|
|
#include "styles/style_chat_helpers.h"
|
2016-04-04 21:09:46 +00:00
|
|
|
|
|
|
|
namespace InlineBots {
|
2018-03-04 20:04:13 +00:00
|
|
|
namespace {
|
|
|
|
|
2020-05-22 15:23:19 +00:00
|
|
|
const auto kVideoThumbMime = "video/mp4"_q;
|
|
|
|
|
2018-03-04 20:04:13 +00:00
|
|
|
QString GetContentUrl(const MTPWebDocument &document) {
|
|
|
|
switch (document.type()) {
|
|
|
|
case mtpc_webDocument:
|
2019-07-05 13:38:38 +00:00
|
|
|
return qs(document.c_webDocument().vurl());
|
2018-03-04 20:04:13 +00:00
|
|
|
case mtpc_webDocumentNoProxy:
|
2019-07-05 13:38:38 +00:00
|
|
|
return qs(document.c_webDocumentNoProxy().vurl());
|
2018-03-04 20:04:13 +00:00
|
|
|
}
|
|
|
|
Unexpected("Type in GetContentUrl.");
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
2016-04-04 21:09:46 +00:00
|
|
|
|
2020-05-28 13:51:18 +00:00
|
|
|
Result::Result(not_null<Main::Session*> session, const Creator &creator)
|
|
|
|
: _session(session)
|
|
|
|
, _queryId(creator.queryId)
|
|
|
|
, _type(creator.type) {
|
2016-04-04 21:09:46 +00:00
|
|
|
}
|
|
|
|
|
2020-05-28 13:51:18 +00:00
|
|
|
std::unique_ptr<Result> Result::Create(
|
|
|
|
not_null<Main::Session*> session,
|
2020-04-16 12:37:26 +00:00
|
|
|
uint64 queryId,
|
2021-03-30 11:43:47 +00:00
|
|
|
const MTPBotInlineResult &data) {
|
2020-05-26 10:13:32 +00:00
|
|
|
using Type = Result::Type;
|
2016-04-05 20:24:27 +00:00
|
|
|
|
2020-05-26 10:13:32 +00:00
|
|
|
const auto type = [&] {
|
|
|
|
static const auto kStringToTypeMap = base::flat_map<QString, Type>{
|
|
|
|
{ u"photo"_q, Type::Photo },
|
|
|
|
{ u"video"_q, Type::Video },
|
|
|
|
{ u"audio"_q, Type::Audio },
|
|
|
|
{ u"voice"_q, Type::Audio },
|
|
|
|
{ u"sticker"_q, Type::Sticker },
|
|
|
|
{ u"file"_q, Type::File },
|
|
|
|
{ u"gif"_q, Type::Gif },
|
|
|
|
{ u"article"_q, Type::Article },
|
|
|
|
{ u"contact"_q, Type::Contact },
|
|
|
|
{ u"venue"_q, Type::Venue },
|
|
|
|
{ u"geo"_q, Type::Geo },
|
|
|
|
{ u"game"_q, Type::Game },
|
|
|
|
};
|
2021-03-30 11:43:47 +00:00
|
|
|
const auto type = data.match([](const auto &data) {
|
2020-05-26 10:13:32 +00:00
|
|
|
return qs(data.vtype());
|
|
|
|
});
|
|
|
|
const auto i = kStringToTypeMap.find(type);
|
|
|
|
return (i != kStringToTypeMap.end()) ? i->second : Type::Unknown;
|
|
|
|
}();
|
2016-04-04 21:09:46 +00:00
|
|
|
if (type == Type::Unknown) {
|
2017-08-13 16:14:00 +00:00
|
|
|
return nullptr;
|
2016-04-04 21:09:46 +00:00
|
|
|
}
|
|
|
|
|
2020-05-28 13:51:18 +00:00
|
|
|
auto result = std::make_unique<Result>(
|
|
|
|
session,
|
|
|
|
Creator{ queryId, type });
|
2021-03-30 11:43:47 +00:00
|
|
|
const auto message = data.match([&](const MTPDbotInlineResult &data) {
|
|
|
|
result->_id = qs(data.vid());
|
|
|
|
result->_title = qs(data.vtitle().value_or_empty());
|
|
|
|
result->_description = qs(data.vdescription().value_or_empty());
|
|
|
|
result->_url = qs(data.vurl().value_or_empty());
|
2020-05-22 15:23:19 +00:00
|
|
|
const auto thumbMime = [&] {
|
2021-03-30 11:43:47 +00:00
|
|
|
if (const auto thumb = data.vthumb()) {
|
2020-05-22 15:23:19 +00:00
|
|
|
return thumb->match([&](const auto &data) {
|
|
|
|
return data.vmime_type().v;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return QByteArray();
|
|
|
|
}();
|
2020-05-26 10:13:32 +00:00
|
|
|
const auto contentMime = [&] {
|
2021-03-30 11:43:47 +00:00
|
|
|
if (const auto content = data.vcontent()) {
|
2020-05-26 10:13:32 +00:00
|
|
|
return content->match([&](const auto &data) {
|
|
|
|
return data.vmime_type().v;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return QByteArray();
|
|
|
|
}();
|
2020-05-22 15:23:19 +00:00
|
|
|
const auto imageThumb = !thumbMime.isEmpty()
|
|
|
|
&& (thumbMime != kVideoThumbMime);
|
|
|
|
const auto videoThumb = !thumbMime.isEmpty() && !imageThumb;
|
2021-03-30 11:43:47 +00:00
|
|
|
if (const auto content = data.vcontent()) {
|
2019-07-05 13:38:38 +00:00
|
|
|
result->_content_url = GetContentUrl(*content);
|
2018-03-04 20:04:13 +00:00
|
|
|
if (result->_type == Type::Photo) {
|
2020-06-08 09:06:50 +00:00
|
|
|
result->_photo = session->data().photoFromWeb(
|
2019-07-05 13:38:38 +00:00
|
|
|
*content,
|
2020-05-25 14:16:04 +00:00
|
|
|
(imageThumb
|
2021-03-30 11:43:47 +00:00
|
|
|
? Images::FromWebDocument(*data.vthumb())
|
2020-05-26 10:13:32 +00:00
|
|
|
: ImageLocation()));
|
|
|
|
} else if (contentMime != "text/html"_q) {
|
2020-06-08 09:06:50 +00:00
|
|
|
result->_document = session->data().documentFromWeb(
|
2019-07-05 13:38:38 +00:00
|
|
|
result->adjustAttributes(*content),
|
2020-05-22 15:23:19 +00:00
|
|
|
(imageThumb
|
2021-03-30 11:43:47 +00:00
|
|
|
? Images::FromWebDocument(*data.vthumb())
|
2020-05-22 15:23:19 +00:00
|
|
|
: ImageLocation()),
|
|
|
|
(videoThumb
|
2021-03-30 11:43:47 +00:00
|
|
|
? Images::FromWebDocument(*data.vthumb())
|
2020-04-16 12:37:26 +00:00
|
|
|
: ImageLocation()));
|
2018-03-04 20:04:13 +00:00
|
|
|
}
|
2016-04-04 21:09:46 +00:00
|
|
|
}
|
2020-05-26 10:13:32 +00:00
|
|
|
if (!result->_photo && !result->_document && imageThumb) {
|
2020-05-28 13:51:18 +00:00
|
|
|
result->_thumbnail.update(result->_session, ImageWithLocation{
|
2021-03-30 11:43:47 +00:00
|
|
|
.location = Images::FromWebDocument(*data.vthumb())
|
2021-09-02 07:31:57 +00:00
|
|
|
});
|
2020-05-26 10:13:32 +00:00
|
|
|
}
|
2021-03-30 11:43:47 +00:00
|
|
|
return &data.vsend_message();
|
|
|
|
}, [&](const MTPDbotInlineMediaResult &data) {
|
|
|
|
result->_id = qs(data.vid());
|
|
|
|
result->_title = qs(data.vtitle().value_or_empty());
|
|
|
|
result->_description = qs(data.vdescription().value_or_empty());
|
|
|
|
if (const auto photo = data.vphoto()) {
|
2020-06-08 09:06:50 +00:00
|
|
|
result->_photo = session->data().processPhoto(*photo);
|
2016-04-05 20:24:27 +00:00
|
|
|
}
|
2021-03-30 11:43:47 +00:00
|
|
|
if (const auto document = data.vdocument()) {
|
2020-06-08 09:06:50 +00:00
|
|
|
result->_document = session->data().processDocument(*document);
|
2016-04-05 20:24:27 +00:00
|
|
|
}
|
2021-03-30 11:43:47 +00:00
|
|
|
return &data.vsend_message();
|
|
|
|
});
|
2021-03-30 08:36:09 +00:00
|
|
|
if ((result->_photo && result->_photo->isNull())
|
2021-03-30 11:43:47 +00:00
|
|
|
|| (result->_document && result->_document->isNull())) {
|
2017-08-13 16:14:00 +00:00
|
|
|
return nullptr;
|
2016-04-04 21:09:46 +00:00
|
|
|
}
|
|
|
|
|
2016-04-10 18:18:26 +00:00
|
|
|
// Ensure required media fields for layouts.
|
|
|
|
if (result->_type == Type::Photo) {
|
2018-03-04 20:04:13 +00:00
|
|
|
if (!result->_photo) {
|
2017-08-13 16:14:00 +00:00
|
|
|
return nullptr;
|
2016-04-10 18:18:26 +00:00
|
|
|
}
|
2018-03-04 20:04:13 +00:00
|
|
|
} else if (result->_type == Type::Audio
|
|
|
|
|| result->_type == Type::File
|
|
|
|
|| result->_type == Type::Sticker
|
|
|
|
|| result->_type == Type::Gif) {
|
|
|
|
if (!result->_document) {
|
2017-08-13 16:14:00 +00:00
|
|
|
return nullptr;
|
2016-04-10 18:18:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-30 08:36:09 +00:00
|
|
|
message->match([&](const MTPDbotInlineMessageMediaAuto &data) {
|
|
|
|
const auto message = qs(data.vmessage());
|
2019-09-16 11:14:06 +00:00
|
|
|
const auto entities = Api::EntitiesFromMTP(
|
2020-06-08 08:03:45 +00:00
|
|
|
session,
|
2021-03-30 08:36:09 +00:00
|
|
|
data.ventities().value_or_empty());
|
2016-04-05 20:24:27 +00:00
|
|
|
if (result->_type == Type::Photo) {
|
2021-03-30 08:36:09 +00:00
|
|
|
if (result->_photo) {
|
|
|
|
result->sendData = std::make_unique<internal::SendPhoto>(
|
|
|
|
session,
|
|
|
|
result->_photo,
|
|
|
|
message,
|
|
|
|
entities);
|
|
|
|
} else {
|
|
|
|
LOG(("Inline Error: No 'photo' in media-auto, type=photo."));
|
2018-03-04 20:04:13 +00:00
|
|
|
}
|
2016-09-28 16:23:25 +00:00
|
|
|
} else if (result->_type == Type::Game) {
|
2020-06-08 09:06:50 +00:00
|
|
|
result->createGame(session);
|
2019-07-05 13:38:38 +00:00
|
|
|
result->sendData = std::make_unique<internal::SendGame>(
|
2020-06-08 08:03:45 +00:00
|
|
|
session,
|
2019-07-05 13:38:38 +00:00
|
|
|
result->_game);
|
2016-04-04 21:09:46 +00:00
|
|
|
} else {
|
2021-03-30 08:36:09 +00:00
|
|
|
if (result->_document) {
|
|
|
|
result->sendData = std::make_unique<internal::SendFile>(
|
|
|
|
session,
|
|
|
|
result->_document,
|
|
|
|
message,
|
|
|
|
entities);
|
|
|
|
} else {
|
|
|
|
LOG(("Inline Error: No 'document' in media-auto, type=%1."
|
|
|
|
).arg(int(result->_type)));
|
2018-03-04 20:04:13 +00:00
|
|
|
}
|
2016-04-06 08:00:37 +00:00
|
|
|
}
|
2021-03-30 08:36:09 +00:00
|
|
|
}, [&](const MTPDbotInlineMessageText &data) {
|
2019-07-05 13:38:38 +00:00
|
|
|
result->sendData = std::make_unique<internal::SendText>(
|
2020-06-08 08:03:45 +00:00
|
|
|
session,
|
2021-03-30 08:36:09 +00:00
|
|
|
qs(data.vmessage()),
|
|
|
|
Api::EntitiesFromMTP(session, data.ventities().value_or_empty()),
|
|
|
|
data.is_no_webpage());
|
|
|
|
}, [&](const MTPDbotInlineMessageMediaGeo &data) {
|
|
|
|
data.vgeo().match([&](const MTPDgeoPoint &geo) {
|
|
|
|
if (const auto period = data.vperiod()) {
|
|
|
|
result->sendData = std::make_unique<internal::SendGeo>(
|
|
|
|
session,
|
|
|
|
geo,
|
|
|
|
period->v,
|
|
|
|
(data.vheading()
|
|
|
|
? std::make_optional(data.vheading()->v)
|
|
|
|
: std::nullopt),
|
|
|
|
(data.vproximity_notification_radius()
|
|
|
|
? std::make_optional(
|
|
|
|
data.vproximity_notification_radius()->v)
|
|
|
|
: std::nullopt));
|
|
|
|
} else {
|
|
|
|
result->sendData = std::make_unique<internal::SendGeo>(
|
|
|
|
session,
|
|
|
|
geo);
|
|
|
|
}
|
|
|
|
}, [&](const MTPDgeoPointEmpty &) {
|
|
|
|
LOG(("Inline Error: Empty 'geo' in media-geo."));
|
|
|
|
});
|
|
|
|
}, [&](const MTPDbotInlineMessageMediaVenue &data) {
|
|
|
|
data.vgeo().match([&](const MTPDgeoPoint &geo) {
|
2020-06-08 08:03:45 +00:00
|
|
|
result->sendData = std::make_unique<internal::SendVenue>(
|
|
|
|
session,
|
2021-03-30 08:36:09 +00:00
|
|
|
geo,
|
|
|
|
qs(data.vvenue_id()),
|
|
|
|
qs(data.vprovider()),
|
|
|
|
qs(data.vtitle()),
|
|
|
|
qs(data.vaddress()));
|
|
|
|
}, [&](const MTPDgeoPointEmpty &) {
|
|
|
|
LOG(("Inline Error: Empty 'geo' in media-venue."));
|
|
|
|
});
|
|
|
|
}, [&](const MTPDbotInlineMessageMediaContact &data) {
|
2020-06-08 08:03:45 +00:00
|
|
|
result->sendData = std::make_unique<internal::SendContact>(
|
|
|
|
session,
|
2021-03-30 08:36:09 +00:00
|
|
|
qs(data.vfirst_name()),
|
|
|
|
qs(data.vlast_name()),
|
|
|
|
qs(data.vphone_number()));
|
|
|
|
}, [&](const MTPDbotInlineMessageMediaInvoice &data) {
|
2021-03-30 11:43:47 +00:00
|
|
|
using Flag = MTPDmessageMediaInvoice::Flag;
|
|
|
|
const auto media = MTP_messageMediaInvoice(
|
|
|
|
MTP_flags((data.is_shipping_address_requested()
|
|
|
|
? Flag::f_shipping_address_requested
|
|
|
|
: Flag(0))
|
|
|
|
| (data.is_test() ? Flag::f_test : Flag(0))
|
|
|
|
| (data.vphoto() ? Flag::f_photo : Flag(0))),
|
|
|
|
data.vtitle(),
|
|
|
|
data.vdescription(),
|
|
|
|
data.vphoto() ? (*data.vphoto()) : MTPWebDocument(),
|
|
|
|
MTPint(), // receipt_msg_id
|
|
|
|
data.vcurrency(),
|
|
|
|
data.vtotal_amount(),
|
2022-09-08 09:17:13 +00:00
|
|
|
MTP_string(QString()), // start_param
|
|
|
|
MTPMessageExtendedMedia());
|
2021-03-30 11:43:47 +00:00
|
|
|
result->sendData = std::make_unique<internal::SendInvoice>(
|
|
|
|
session,
|
|
|
|
media);
|
2023-10-18 07:29:42 +00:00
|
|
|
}, [&](const MTPDbotInlineMessageMediaWebPage &data) {
|
|
|
|
result->sendData = std::make_unique<internal::SendText>(
|
|
|
|
session,
|
|
|
|
qs(data.vmessage()),
|
|
|
|
Api::EntitiesFromMTP(session, data.ventities().value_or_empty()),
|
|
|
|
false);
|
2021-03-30 08:36:09 +00:00
|
|
|
});
|
2016-04-04 21:09:46 +00:00
|
|
|
|
2021-03-30 08:36:09 +00:00
|
|
|
if (!result->sendData || !result->sendData->isValid()) {
|
2017-08-13 16:14:00 +00:00
|
|
|
return nullptr;
|
2016-04-04 21:09:46 +00:00
|
|
|
}
|
2016-04-05 20:24:27 +00:00
|
|
|
|
2021-03-30 08:36:09 +00:00
|
|
|
message->match([&](const auto &data) {
|
|
|
|
if (const auto markup = data.vreply_markup()) {
|
2021-10-02 11:28:21 +00:00
|
|
|
result->_replyMarkup
|
|
|
|
= std::make_unique<HistoryMessageMarkupData>(markup);
|
2021-03-30 08:36:09 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-06-21 12:27:46 +00:00
|
|
|
if (const auto point = result->getLocationPoint()) {
|
2018-10-15 11:44:48 +00:00
|
|
|
const auto scale = 1 + (cScale() * cIntRetinaFactor()) / 200;
|
|
|
|
const auto zoom = 15 + (scale - 1);
|
2018-10-16 09:05:32 +00:00
|
|
|
const auto w = st::inlineThumbSize / scale;
|
|
|
|
const auto h = st::inlineThumbSize / scale;
|
2018-10-15 11:44:48 +00:00
|
|
|
|
|
|
|
auto location = GeoPointLocation();
|
2019-06-21 12:27:46 +00:00
|
|
|
location.lat = point->lat();
|
|
|
|
location.lon = point->lon();
|
|
|
|
location.access = point->accessHash();
|
2018-10-15 11:44:48 +00:00
|
|
|
location.width = w;
|
|
|
|
location.height = h;
|
|
|
|
location.zoom = zoom;
|
|
|
|
location.scale = scale;
|
2020-05-28 13:51:18 +00:00
|
|
|
result->_locationThumbnail.update(result->_session, ImageWithLocation{
|
2020-05-27 14:31:39 +00:00
|
|
|
.location = ImageLocation({ location }, w, h)
|
|
|
|
});
|
2016-04-05 20:24:27 +00:00
|
|
|
}
|
|
|
|
|
2016-04-04 21:09:46 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-04-05 20:24:27 +00:00
|
|
|
bool Result::onChoose(Layout::ItemBase *layout) {
|
|
|
|
if (_photo && _type == Type::Photo) {
|
2020-05-25 14:16:04 +00:00
|
|
|
const auto media = _photo->activeMediaView();
|
|
|
|
if (!media || media->image(Data::PhotoSize::Thumbnail)) {
|
2016-04-05 20:24:27 +00:00
|
|
|
return true;
|
2020-05-25 14:16:04 +00:00
|
|
|
} else if (!_photo->loading(Data::PhotoSize::Thumbnail)) {
|
|
|
|
_photo->load(
|
|
|
|
Data::PhotoSize::Thumbnail,
|
|
|
|
Data::FileOrigin());
|
2016-04-05 20:24:27 +00:00
|
|
|
}
|
2016-05-19 15:02:07 +00:00
|
|
|
return false;
|
2016-04-05 20:24:27 +00:00
|
|
|
}
|
|
|
|
if (_document && (
|
|
|
|
_type == Type::Video ||
|
|
|
|
_type == Type::Audio ||
|
|
|
|
_type == Type::Sticker ||
|
|
|
|
_type == Type::File ||
|
|
|
|
_type == Type::Gif)) {
|
|
|
|
if (_type == Type::Gif) {
|
2020-04-10 13:18:51 +00:00
|
|
|
const auto media = _document->activeMediaView();
|
2020-05-22 11:57:13 +00:00
|
|
|
const auto preview = Data::VideoPreviewState(media.get());
|
|
|
|
if (!media || preview.loaded()) {
|
2016-04-05 20:24:27 +00:00
|
|
|
return true;
|
2020-05-22 11:57:13 +00:00
|
|
|
} else if (!preview.usingThumbnail()) {
|
|
|
|
if (preview.loading()) {
|
|
|
|
_document->cancel();
|
|
|
|
} else {
|
|
|
|
DocumentSaveClickHandler::Save(
|
|
|
|
Data::FileOriginSavedGifs(),
|
|
|
|
_document);
|
|
|
|
}
|
2016-04-05 20:24:27 +00:00
|
|
|
}
|
2016-05-19 15:02:07 +00:00
|
|
|
return false;
|
2016-04-05 20:24:27 +00:00
|
|
|
}
|
2016-05-19 15:02:07 +00:00
|
|
|
return true;
|
2016-04-05 20:24:27 +00:00
|
|
|
}
|
2016-04-10 11:13:37 +00:00
|
|
|
return true;
|
2016-04-04 21:09:46 +00:00
|
|
|
}
|
|
|
|
|
2021-06-14 06:21:56 +00:00
|
|
|
Media::View::OpenRequest Result::openRequest() {
|
2022-10-11 15:08:19 +00:00
|
|
|
using namespace Media::View;
|
2016-04-10 11:13:37 +00:00
|
|
|
if (_document) {
|
2022-10-11 15:08:19 +00:00
|
|
|
return OpenRequest(nullptr, _document, nullptr, MsgId());
|
2016-04-10 11:13:37 +00:00
|
|
|
} else if (_photo) {
|
2022-10-11 15:08:19 +00:00
|
|
|
return OpenRequest(nullptr, _photo, nullptr, MsgId());
|
2016-04-10 11:13:37 +00:00
|
|
|
}
|
2021-06-14 06:21:56 +00:00
|
|
|
return {};
|
2016-04-10 11:13:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Result::cancelFile() {
|
|
|
|
if (_document) {
|
2021-06-18 06:20:49 +00:00
|
|
|
DocumentCancelClickHandler(_document, nullptr).onClick({});
|
2016-04-10 11:13:37 +00:00
|
|
|
} else if (_photo) {
|
2021-06-18 06:20:49 +00:00
|
|
|
PhotoCancelClickHandler(_photo, nullptr).onClick({});
|
2016-04-10 11:13:37 +00:00
|
|
|
}
|
2016-04-04 21:09:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Result::hasThumbDisplay() const {
|
2020-05-27 14:31:39 +00:00
|
|
|
if (!_thumbnail.empty()
|
2020-05-26 10:13:32 +00:00
|
|
|
|| _photo
|
|
|
|
|| (_document && _document->hasThumbnail())) {
|
2016-04-04 21:09:46 +00:00
|
|
|
return true;
|
2020-05-26 10:13:32 +00:00
|
|
|
} else if (_type == Type::Contact) {
|
2016-04-04 21:09:46 +00:00
|
|
|
return true;
|
2020-05-26 10:13:32 +00:00
|
|
|
} else if (sendData->hasLocationCoords()) {
|
2016-04-04 21:09:46 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2019-08-09 19:19:23 +00:00
|
|
|
void Result::addToHistory(
|
2023-01-10 18:56:20 +00:00
|
|
|
not_null<History*> history,
|
2021-07-28 11:55:02 +00:00
|
|
|
MessageFlags flags,
|
2019-08-09 19:19:23 +00:00
|
|
|
MsgId msgId,
|
2020-09-01 06:44:18 +00:00
|
|
|
PeerId fromId,
|
2021-07-28 11:55:02 +00:00
|
|
|
TimeId date,
|
2019-08-09 19:19:23 +00:00
|
|
|
UserId viaBotId,
|
2023-05-25 09:32:13 +00:00
|
|
|
FullReplyTo replyTo,
|
2019-08-09 19:19:23 +00:00
|
|
|
const QString &postAuthor) const {
|
2021-07-28 11:55:02 +00:00
|
|
|
flags |= MessageFlag::FromInlineBot;
|
2016-04-06 08:00:37 +00:00
|
|
|
|
2021-10-02 11:28:21 +00:00
|
|
|
auto markup = _replyMarkup ? *_replyMarkup : HistoryMessageMarkupData();
|
|
|
|
if (!markup.isNull()) {
|
2021-07-28 11:55:02 +00:00
|
|
|
flags |= MessageFlag::HasReplyMarkup;
|
2016-04-06 08:00:37 +00:00
|
|
|
}
|
2019-08-09 19:19:23 +00:00
|
|
|
sendData->addToHistory(
|
|
|
|
this,
|
|
|
|
history,
|
|
|
|
flags,
|
|
|
|
msgId,
|
|
|
|
fromId,
|
2021-07-28 11:55:02 +00:00
|
|
|
date,
|
2019-08-09 19:19:23 +00:00
|
|
|
viaBotId,
|
2023-05-25 09:32:13 +00:00
|
|
|
replyTo,
|
2019-08-09 19:19:23 +00:00
|
|
|
postAuthor,
|
2021-10-02 11:28:21 +00:00
|
|
|
std::move(markup));
|
2016-04-04 21:09:46 +00:00
|
|
|
}
|
|
|
|
|
2023-01-10 18:56:20 +00:00
|
|
|
QString Result::getErrorOnSend(not_null<History*> history) const {
|
|
|
|
const auto specific = sendData->getErrorOnSend(this, history);
|
|
|
|
return !specific.isEmpty()
|
|
|
|
? specific
|
|
|
|
: Data::RestrictionError(
|
|
|
|
history->peer,
|
|
|
|
ChatRestriction::SendInline).value_or(QString());
|
2017-06-11 18:33:20 +00:00
|
|
|
}
|
|
|
|
|
2019-06-21 12:27:46 +00:00
|
|
|
std::optional<Data::LocationPoint> Result::getLocationPoint() const {
|
|
|
|
return sendData->getLocationPoint();
|
2016-04-04 21:09:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString Result::getLayoutTitle() const {
|
|
|
|
return sendData->getLayoutTitle(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Result::getLayoutDescription() const {
|
|
|
|
return sendData->getLayoutDescription(this);
|
|
|
|
}
|
|
|
|
|
2016-09-28 21:33:05 +00:00
|
|
|
// just to make unique_ptr see the destructors.
|
|
|
|
Result::~Result() {
|
|
|
|
}
|
|
|
|
|
2020-06-08 09:06:50 +00:00
|
|
|
void Result::createGame(not_null<Main::Session*> session) {
|
|
|
|
if (_game) {
|
|
|
|
return;
|
|
|
|
}
|
2016-09-28 16:23:25 +00:00
|
|
|
|
2021-09-15 10:21:45 +00:00
|
|
|
const auto gameId = base::RandomValue<GameId>();
|
2020-06-08 09:06:50 +00:00
|
|
|
_game = session->data().game(
|
2018-01-17 16:21:01 +00:00
|
|
|
gameId,
|
|
|
|
0,
|
|
|
|
QString(),
|
|
|
|
_title,
|
|
|
|
_description,
|
|
|
|
_photo,
|
|
|
|
_document);
|
2016-04-04 21:09:46 +00:00
|
|
|
}
|
|
|
|
|
2018-03-04 20:04:13 +00:00
|
|
|
QSize Result::thumbBox() const {
|
|
|
|
return (_type == Type::Photo) ? QSize(100, 100) : QSize(90, 90);
|
|
|
|
}
|
|
|
|
|
|
|
|
MTPWebDocument Result::adjustAttributes(const MTPWebDocument &document) {
|
|
|
|
switch (document.type()) {
|
|
|
|
case mtpc_webDocument: {
|
|
|
|
const auto &data = document.c_webDocument();
|
|
|
|
return MTP_webDocument(
|
2019-07-05 13:38:38 +00:00
|
|
|
data.vurl(),
|
|
|
|
data.vaccess_hash(),
|
|
|
|
data.vsize(),
|
|
|
|
data.vmime_type(),
|
|
|
|
adjustAttributes(data.vattributes(), data.vmime_type()));
|
2018-03-04 20:04:13 +00:00
|
|
|
} break;
|
|
|
|
|
|
|
|
case mtpc_webDocumentNoProxy: {
|
|
|
|
const auto &data = document.c_webDocumentNoProxy();
|
|
|
|
return MTP_webDocumentNoProxy(
|
2019-07-05 13:38:38 +00:00
|
|
|
data.vurl(),
|
|
|
|
data.vsize(),
|
|
|
|
data.vmime_type(),
|
|
|
|
adjustAttributes(data.vattributes(), data.vmime_type()));
|
2018-03-04 20:04:13 +00:00
|
|
|
} break;
|
|
|
|
}
|
|
|
|
Unexpected("Type in InlineBots::Result::adjustAttributes.");
|
|
|
|
}
|
|
|
|
|
|
|
|
MTPVector<MTPDocumentAttribute> Result::adjustAttributes(
|
|
|
|
const MTPVector<MTPDocumentAttribute> &existing,
|
|
|
|
const MTPstring &mimeType) {
|
|
|
|
auto result = existing.v;
|
|
|
|
const auto find = [&](mtpTypeId attributeType) {
|
|
|
|
return ranges::find(
|
|
|
|
result,
|
|
|
|
attributeType,
|
|
|
|
[](const MTPDocumentAttribute &value) { return value.type(); });
|
|
|
|
};
|
|
|
|
const auto exists = [&](mtpTypeId attributeType) {
|
|
|
|
return find(attributeType) != result.cend();
|
|
|
|
};
|
|
|
|
const auto mime = qs(mimeType);
|
|
|
|
if (_type == Type::Gif) {
|
|
|
|
if (!exists(mtpc_documentAttributeFilename)) {
|
2022-11-26 21:20:17 +00:00
|
|
|
auto filename = (mime == u"video/mp4"_q
|
2018-03-04 20:04:13 +00:00
|
|
|
? "animation.gif.mp4"
|
|
|
|
: "animation.gif");
|
|
|
|
result.push_back(MTP_documentAttributeFilename(
|
|
|
|
MTP_string(filename)));
|
|
|
|
}
|
|
|
|
if (!exists(mtpc_documentAttributeAnimated)) {
|
|
|
|
result.push_back(MTP_documentAttributeAnimated());
|
|
|
|
}
|
|
|
|
} else if (_type == Type::Audio) {
|
|
|
|
const auto audio = find(mtpc_documentAttributeAudio);
|
|
|
|
if (audio != result.cend()) {
|
|
|
|
using Flag = MTPDdocumentAttributeAudio::Flag;
|
2022-11-26 21:20:17 +00:00
|
|
|
if (mime == u"audio/ogg"_q) {
|
2018-03-04 20:04:13 +00:00
|
|
|
// We always treat audio/ogg as a voice message.
|
|
|
|
// It was that way before we started to get attributes here.
|
|
|
|
const auto &fields = audio->c_documentAttributeAudio();
|
2019-07-05 13:38:38 +00:00
|
|
|
if (!(fields.vflags().v & Flag::f_voice)) {
|
2018-03-04 20:04:13 +00:00
|
|
|
*audio = MTP_documentAttributeAudio(
|
2019-07-05 13:38:38 +00:00
|
|
|
MTP_flags(fields.vflags().v | Flag::f_voice),
|
|
|
|
fields.vduration(),
|
|
|
|
MTP_bytes(fields.vtitle().value_or_empty()),
|
|
|
|
MTP_bytes(fields.vperformer().value_or_empty()),
|
|
|
|
MTP_bytes(fields.vwaveform().value_or_empty()));
|
2018-03-04 20:04:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto &fields = audio->c_documentAttributeAudio();
|
|
|
|
if (!exists(mtpc_documentAttributeFilename)
|
2019-07-05 13:38:38 +00:00
|
|
|
&& !(fields.vflags().v & Flag::f_voice)) {
|
2018-06-13 22:06:21 +00:00
|
|
|
const auto p = Core::MimeTypeForName(mime).globPatterns();
|
2018-03-04 20:04:13 +00:00
|
|
|
auto pattern = p.isEmpty() ? QString() : p.front();
|
|
|
|
const auto extension = pattern.isEmpty()
|
2022-11-29 21:46:36 +00:00
|
|
|
? u".unknown"_q
|
2018-03-04 20:04:13 +00:00
|
|
|
: pattern.replace('*', QString());
|
|
|
|
const auto filename = filedialogDefaultName(
|
2022-11-29 21:46:36 +00:00
|
|
|
u"inline"_q,
|
2018-03-04 20:04:13 +00:00
|
|
|
extension,
|
|
|
|
QString(),
|
|
|
|
true);
|
|
|
|
result.push_back(
|
|
|
|
MTP_documentAttributeFilename(MTP_string(filename)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return MTP_vector<MTPDocumentAttribute>(std::move(result));
|
|
|
|
}
|
|
|
|
|
2016-04-04 21:09:46 +00:00
|
|
|
} // namespace InlineBots
|