tdesktop/Telegram/SourceFiles/data/data_web_page.cpp

344 lines
9.1 KiB
C++
Raw Normal View History

2017-12-31 09:39:21 +00:00
/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
2017-12-31 09:39:21 +00:00
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
2017-12-31 09:39:21 +00:00
*/
#include "data/data_web_page.h"
2019-07-24 11:45:24 +00:00
#include "main/main_session.h"
2017-12-31 09:39:21 +00:00
#include "apiwrap.h"
#include "mainwidget.h"
#include "data/data_session.h"
#include "data/data_photo.h"
#include "data/data_channel.h"
#include "data/data_document.h"
#include "ui/image/image.h"
2017-12-31 09:39:21 +00:00
#include "ui/text/text_entity.h"
namespace {
QString SiteNameFromUrl(const QString &url) {
2022-11-29 21:46:36 +00:00
const auto u = QUrl(url);
2017-12-31 09:39:21 +00:00
QString pretty = u.isValid() ? u.toDisplayString() : url;
2022-11-29 21:46:36 +00:00
const auto m = QRegularExpression(u"^[a-zA-Z0-9]+://"_q).match(pretty);
2017-12-31 09:39:21 +00:00
if (m.hasMatch()) pretty = pretty.mid(m.capturedLength());
int32 slash = pretty.indexOf('/');
if (slash > 0) pretty = pretty.mid(0, slash);
QStringList components = pretty.split('.', Qt::SkipEmptyParts);
2017-12-31 09:39:21 +00:00
if (components.size() >= 2) {
components = components.mid(components.size() - 2);
2022-11-29 21:46:36 +00:00
return components.at(0).at(0).toUpper()
+ components.at(0).mid(1)
+ '.'
+ components.at(1);
2017-12-31 09:39:21 +00:00
}
return QString();
}
WebPageCollage ExtractCollage(
2020-06-08 09:06:50 +00:00
not_null<Data::Session*> owner,
const QVector<MTPPageBlock> &items,
const QVector<MTPPhoto> &photos,
const QVector<MTPDocument> &documents) {
const auto count = items.size();
if (count < 2) {
return {};
}
const auto bad = ranges::find_if(items, [](mtpTypeId type) {
return (type != mtpc_pageBlockPhoto && type != mtpc_pageBlockVideo);
}, [](const MTPPageBlock &item) {
return item.type();
});
if (bad != items.end()) {
return {};
}
for (const auto &photo : photos) {
2020-06-08 09:06:50 +00:00
owner->processPhoto(photo);
}
for (const auto &document : documents) {
2020-06-08 09:06:50 +00:00
owner->processDocument(document);
}
auto result = WebPageCollage();
result.items.reserve(count);
for (const auto &item : items) {
const auto good = item.match([&](const MTPDpageBlockPhoto &data) {
2020-06-08 09:06:50 +00:00
const auto photo = owner->photo(data.vphoto_id().v);
if (photo->isNull()) {
return false;
}
2019-07-05 13:38:38 +00:00
result.items.emplace_back(photo);
return true;
}, [&](const MTPDpageBlockVideo &data) {
2020-06-08 09:06:50 +00:00
const auto document = owner->document(data.vvideo_id().v);
if (!document->isVideoFile()) {
return false;
}
2019-07-05 13:38:38 +00:00
result.items.emplace_back(document);
return true;
}, [](const auto &) -> bool {
Unexpected("Type of block in Collage.");
});
if (!good) {
return {};
}
}
return result;
}
2020-06-08 09:06:50 +00:00
WebPageCollage ExtractCollage(
not_null<Data::Session*> owner,
const MTPDwebPage &data) {
2019-07-05 13:38:38 +00:00
const auto page = data.vcached_page();
if (!page) {
return {};
}
2019-01-18 12:27:37 +00:00
const auto processMedia = [&] {
2019-07-05 13:38:38 +00:00
if (const auto photo = data.vphoto()) {
2020-06-08 09:06:50 +00:00
owner->processPhoto(*photo);
}
2019-07-05 13:38:38 +00:00
if (const auto document = data.vdocument()) {
2020-06-08 09:06:50 +00:00
owner->processDocument(*document);
}
};
2019-07-05 13:38:38 +00:00
return page->match([&](const auto &page) {
for (const auto &block : page.vblocks().v) {
switch (block.type()) {
case mtpc_pageBlockPhoto:
case mtpc_pageBlockVideo:
case mtpc_pageBlockCover:
case mtpc_pageBlockEmbed:
case mtpc_pageBlockEmbedPost:
case mtpc_pageBlockAudio:
return WebPageCollage();
case mtpc_pageBlockSlideshow:
2019-01-18 12:27:37 +00:00
processMedia();
return ExtractCollage(
2020-06-08 09:06:50 +00:00
owner,
2019-07-05 13:38:38 +00:00
block.c_pageBlockSlideshow().vitems().v,
page.vphotos().v,
page.vdocuments().v);
case mtpc_pageBlockCollage:
2019-01-18 12:27:37 +00:00
processMedia();
return ExtractCollage(
2020-06-08 09:06:50 +00:00
owner,
2019-07-05 13:38:38 +00:00
block.c_pageBlockCollage().vitems().v,
page.vphotos().v,
page.vdocuments().v);
default: break;
}
}
return WebPageCollage();
});
}
2017-12-31 09:39:21 +00:00
} // namespace
2021-10-11 17:47:36 +00:00
WebPageType ParseWebPageType(
const QString &type,
const QString &embedUrl,
bool hasIV) {
2022-11-26 21:20:17 +00:00
if (type == u"video"_q || !embedUrl.isEmpty()) {
return WebPageType::Video;
2022-11-26 21:20:17 +00:00
} else if (type == u"photo"_q) {
return WebPageType::Photo;
} else if (type == u"document"_q) {
return WebPageType::Document;
2022-11-26 21:20:17 +00:00
} else if (type == u"profile"_q) {
return WebPageType::Profile;
2022-11-26 21:20:17 +00:00
} else if (type == u"telegram_background"_q) {
return WebPageType::WallPaper;
2022-11-26 21:20:17 +00:00
} else if (type == u"telegram_theme"_q) {
return WebPageType::Theme;
2023-06-23 12:04:53 +00:00
} else if (type == u"telegram_story"_q) {
return WebPageType::Story;
2022-11-26 21:20:17 +00:00
} else if (type == u"telegram_channel"_q) {
2021-10-10 16:11:08 +00:00
return WebPageType::Channel;
2022-11-26 21:20:17 +00:00
} else if (type == u"telegram_channel_request"_q) {
2021-10-12 12:50:18 +00:00
return WebPageType::ChannelWithRequest;
2022-11-26 21:20:17 +00:00
} else if (type == u"telegram_megagroup"_q
|| type == u"telegram_chat"_q) {
2021-10-12 12:50:18 +00:00
return WebPageType::Group;
2022-11-26 21:20:17 +00:00
} else if (type == u"telegram_megagroup_request"_q
|| type == u"telegram_chat_request"_q) {
2021-10-12 12:50:18 +00:00
return WebPageType::GroupWithRequest;
} else if (type == u"telegram_message"_q) {
2021-10-10 16:11:08 +00:00
return WebPageType::Message;
} else if (type == u"telegram_bot"_q) {
2021-10-10 16:11:08 +00:00
return WebPageType::Bot;
} else if (type == u"telegram_voicechat"_q) {
2021-10-10 16:11:08 +00:00
return WebPageType::VoiceChat;
} else if (type == u"telegram_livestream"_q) {
2021-10-10 16:11:08 +00:00
return WebPageType::Livestream;
} else if (type == u"telegram_user"_q) {
2021-10-10 16:11:08 +00:00
return WebPageType::User;
} else if (type == u"telegram_botapp"_q) {
return WebPageType::BotApp;
2021-10-11 17:47:36 +00:00
} else if (hasIV) {
return WebPageType::ArticleWithIV;
} else {
return WebPageType::Article;
}
}
2021-10-11 17:47:36 +00:00
WebPageType ParseWebPageType(const MTPDwebPage &page) {
return ParseWebPageType(
qs(page.vtype().value_or_empty()),
page.vembed_url().value_or_empty(),
!!page.vcached_page());
}
2020-06-08 09:06:50 +00:00
WebPageCollage::WebPageCollage(
not_null<Data::Session*> owner,
const MTPDwebPage &data)
: WebPageCollage(ExtractCollage(owner, data)) {
}
WebPageData::WebPageData(not_null<Data::Session*> owner, const WebPageId &id)
: id(id)
, _owner(owner) {
}
Data::Session &WebPageData::owner() const {
return *_owner;
}
Main::Session &WebPageData::session() const {
return _owner->session();
}
2017-12-31 09:39:21 +00:00
bool WebPageData::applyChanges(
WebPageType newType,
2017-12-31 09:39:21 +00:00
const QString &newUrl,
const QString &newDisplayUrl,
const QString &newSiteName,
const QString &newTitle,
const TextWithEntities &newDescription,
2023-06-23 12:04:53 +00:00
FullStoryId newStoryId,
2017-12-31 09:39:21 +00:00
PhotoData *newPhoto,
DocumentData *newDocument,
WebPageCollage &&newCollage,
2017-12-31 09:39:21 +00:00
int newDuration,
const QString &newAuthor,
int newPendingTill) {
if (newPendingTill != 0
&& (!url.isEmpty() || pendingTill < 0)
2017-12-31 09:39:21 +00:00
&& (!pendingTill
|| pendingTill == newPendingTill
|| newPendingTill < -1)) {
return false;
}
2022-01-06 23:04:26 +00:00
const auto resultUrl = newUrl;
const auto resultDisplayUrl = newDisplayUrl;
const auto possibleSiteName = newSiteName;
const auto resultTitle = TextUtilities::SingleLine(newTitle);
const auto resultAuthor = newAuthor;
2017-12-31 09:39:21 +00:00
const auto viewTitleText = resultTitle.isEmpty()
? TextUtilities::SingleLine(resultAuthor)
: resultTitle;
const auto resultSiteName = [&] {
if (!possibleSiteName.isEmpty()) {
return possibleSiteName;
} else if (!newDescription.text.isEmpty()
&& viewTitleText.isEmpty()
&& !resultUrl.isEmpty()) {
return SiteNameFromUrl(resultUrl);
}
return QString();
}();
if (type == newType
2017-12-31 09:39:21 +00:00
&& url == resultUrl
&& displayUrl == resultDisplayUrl
&& siteName == resultSiteName
&& title == resultTitle
&& description.text == newDescription.text
2023-06-23 12:04:53 +00:00
&& storyId == newStoryId
2017-12-31 09:39:21 +00:00
&& photo == newPhoto
&& document == newDocument
&& collage.items == newCollage.items
2017-12-31 09:39:21 +00:00
&& duration == newDuration
&& author == resultAuthor
&& pendingTill == newPendingTill) {
return false;
}
if (pendingTill > 0 && newPendingTill <= 0) {
2020-06-08 09:06:50 +00:00
_owner->session().api().clearWebPageRequest(this);
2017-12-31 09:39:21 +00:00
}
type = newType;
2017-12-31 09:39:21 +00:00
url = resultUrl;
displayUrl = resultDisplayUrl;
siteName = resultSiteName;
title = resultTitle;
description = newDescription;
2023-06-23 12:04:53 +00:00
storyId = newStoryId;
2017-12-31 09:39:21 +00:00
photo = newPhoto;
document = newDocument;
collage = std::move(newCollage);
2017-12-31 09:39:21 +00:00
duration = newDuration;
author = resultAuthor;
pendingTill = newPendingTill;
++version;
if (type == WebPageType::WallPaper && document) {
2019-01-16 12:25:29 +00:00
document->checkWallPaperProperties();
}
replaceDocumentGoodThumbnail();
2017-12-31 09:39:21 +00:00
return true;
}
void WebPageData::replaceDocumentGoodThumbnail() {
if (document && photo) {
document->setGoodThumbnailPhoto(photo);
}
}
void WebPageData::ApplyChanges(
not_null<Main::Session*> session,
ChannelData *channel,
const MTPmessages_Messages &result) {
result.match([&](
const MTPDmessages_channelMessages &data) {
if (channel) {
channel->ptsReceived(data.vpts().v);
channel->processTopics(data.vtopics());
} else {
LOG(("API Error: received messages.channelMessages "
"when no channel was passed! (WebPageData::ApplyChanges)"));
}
}, [&](const auto &) {
});
const auto list = result.match([](
const MTPDmessages_messagesNotModified &) {
LOG(("API Error: received messages.messagesNotModified! "
"(WebPageData::ApplyChanges)"));
return static_cast<const QVector<MTPMessage>*>(nullptr);
}, [&](const auto &data) {
session->data().processUsers(data.vusers());
session->data().processChats(data.vchats());
return &data.vmessages().v;
});
if (!list) {
return;
}
for (const auto &message : *list) {
message.match([&](const MTPDmessage &data) {
if (const auto media = data.vmedia()) {
media->match([&](const MTPDmessageMediaWebPage &data) {
session->data().processWebpage(data.vwebpage());
}, [&](const auto &) {
});
}
}, [&](const auto &) {
});
}
session->data().sendWebPageGamePollNotifications();
2021-10-10 16:11:08 +00:00
}