Added support of bot apps to sponsored messages.

Added api support of custom button text in sponsored messages.
This commit is contained in:
23rd 2023-11-23 17:09:12 +03:00 committed by John Preston
parent d28ba4fad9
commit 0f3faf59ca
4 changed files with 47 additions and 12 deletions

View File

@ -10,7 +10,9 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "api/api_text_entities.h"
#include "apiwrap.h"
#include "base/unixtime.h"
#include "data/data_bot_app.h"
#include "data/data_channel.h"
#include "data/data_peer.h"
#include "data/data_peer_id.h"
#include "data/data_photo.h"
#include "data/data_session.h"
@ -273,10 +275,10 @@ void SponsoredMessages::append(
.isMegagroup = (channel && channel->isMegagroup()),
.isChannel = (channel != nullptr),
.isPublic = (channel && channel->isPublic()),
.isBot = (peer->isUser() && peer->asUser()->isBot()),
.isExactPost = exactPost,
.isRecommended = data.is_recommended(),
.isForceUserpicDisplay = data.is_show_peer_photo(),
.buttonText = qs(data.vbutton_text().value_or_empty()),
};
};
const auto externalLink = data.vwebpage()
@ -291,13 +293,36 @@ void SponsoredMessages::append(
return SponsoredFrom{
.title = qs(data.vsite_name()),
.externalLink = externalLink,
.externalLinkPhotoId = photoId,
.webpageOrBotPhotoId = photoId,
.isForceUserpicDisplay = message.data().is_show_peer_photo(),
};
} else if (const auto fromId = data.vfrom_id()) {
return makeFrom(
_session->data().peer(peerFromMTP(*fromId)),
const auto peerId = peerFromMTP(*fromId);
auto result = makeFrom(
_session->data().peer(peerId),
(data.vchannel_post() != nullptr));
const auto user = result.peer->asUser();
if (user && user->isBot()) {
const auto botAppData = data.vapp()
? _session->data().processBotApp(peerId, *data.vapp())
: nullptr;
result.botLinkInfo = Window::PeerByLinkInfo{
.usernameOrId = user->userName(),
.resolveType = botAppData
? Window::ResolveType::BotApp
: data.vstart_param()
? Window::ResolveType::BotStart
: Window::ResolveType::Default,
.startToken = qs(data.vstart_param().value_or_empty()),
.botAppName = botAppData
? botAppData->shortName
: QString(),
};
result.webpageOrBotPhotoId = (botAppData && botAppData->photo)
? botAppData->photo->id
: PhotoId(0);
}
return result;
}
Assert(data.vchat_invite());
return data.vchat_invite()->match([&](const MTPDchatInvite &data) {
@ -434,11 +459,14 @@ SponsoredMessages::Details SponsoredMessages::lookupDetails(
.info = std::move(info),
.externalLink = data.externalLink,
.isForceUserpicDisplay = data.from.isForceUserpicDisplay,
.buttonText = !data.externalLink.isEmpty()
.buttonText = !data.from.buttonText.isEmpty()
? data.from.buttonText
: !data.externalLink.isEmpty()
? tr::lng_view_button_external_link(tr::now)
: data.from.isBot
: data.from.botLinkInfo
? tr::lng_view_button_bot(tr::now)
: QString(),
.botLinkInfo = data.from.botLinkInfo,
};
}

View File

@ -7,9 +7,10 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#pragma once
#include "history/history_item.h"
#include "base/timer.h"
#include "history/history_item.h"
#include "ui/image/image_location.h"
#include "window/window_session_controller_link_info.h"
class History;
@ -28,12 +29,13 @@ struct SponsoredFrom {
bool isMegagroup = false;
bool isChannel = false;
bool isPublic = false;
bool isBot = false;
std::optional<Window::PeerByLinkInfo> botLinkInfo;
bool isExactPost = false;
bool isRecommended = false;
QString externalLink;
PhotoId externalLinkPhotoId;
PhotoId webpageOrBotPhotoId = PhotoId(0);
bool isForceUserpicDisplay = false;
QString buttonText;
};
struct SponsoredMessage {
@ -63,6 +65,7 @@ public:
QString externalLink;
bool isForceUserpicDisplay = false;
QString buttonText;
std::optional<Window::PeerByLinkInfo> botLinkInfo;
};
using RandomId = QByteArray;
explicit SponsoredMessages(not_null<Session*> owner);

View File

@ -699,7 +699,9 @@ HistoryItem::HistoryItem(
const auto webPageType = from.isExactPost
? WebPageType::Message
: from.isBot
: (from.botLinkInfo && !from.botLinkInfo->botAppName.isEmpty())
? WebPageType::BotApp
: from.botLinkInfo
? WebPageType::Bot
: from.isBroadcast
? WebPageType::Channel
@ -717,8 +719,8 @@ HistoryItem::HistoryItem(
: tr::lng_sponsored_message_title(tr::now),
from.title,
textWithEntities,
from.externalLinkPhotoId
? history->owner().photo(from.externalLinkPhotoId)
from.webpageOrBotPhotoId
? history->owner().photo(from.webpageOrBotPhotoId)
: nullptr,
nullptr,
WebPageCollage(),

View File

@ -44,6 +44,8 @@ ClickHandlerPtr SponsoredLink(const QString &externalLink) {
File::OpenUrl(details.externalLink);
} else if (details.hash) {
Api::CheckChatInvite(controller, *details.hash);
} else if (details.botLinkInfo) {
controller->showPeerByLink(*details.botLinkInfo);
} else if (details.peer) {
controller->showPeerHistory(
details.peer,