1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
|
/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#include "data/data_web_page.h"
#include "main/main_session.h"
#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"
#include "ui/text/text_entity.h"
namespace {
QString SiteNameFromUrl(const QString &url) {
const auto u = QUrl(url);
QString pretty = u.isValid() ? u.toDisplayString() : url;
const auto m = QRegularExpression(u"^[a-zA-Z0-9]+://"_q).match(pretty);
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);
if (components.size() >= 2) {
components = components.mid(components.size() - 2);
return components.at(0).at(0).toUpper()
+ components.at(0).mid(1)
+ '.'
+ components.at(1);
}
return QString();
}
WebPageCollage ExtractCollage(
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) {
owner->processPhoto(photo);
}
for (const auto &document : documents) {
owner->processDocument(document);
}
auto result = WebPageCollage();
result.items.reserve(count);
for (const auto &item : items) {
const auto good = item.match([&](const MTPDpageBlockPhoto &data) {
const auto photo = owner->photo(data.vphoto_id().v);
if (photo->isNull()) {
return false;
}
result.items.emplace_back(photo);
return true;
}, [&](const MTPDpageBlockVideo &data) {
const auto document = owner->document(data.vvideo_id().v);
if (!document->isVideoFile()) {
return false;
}
result.items.emplace_back(document);
return true;
}, [](const auto &) -> bool {
Unexpected("Type of block in Collage.");
});
if (!good) {
return {};
}
}
return result;
}
WebPageCollage ExtractCollage(
not_null<Data::Session*> owner,
const MTPDwebPage &data) {
const auto page = data.vcached_page();
if (!page) {
return {};
}
const auto processMedia = [&] {
if (const auto photo = data.vphoto()) {
owner->processPhoto(*photo);
}
if (const auto document = data.vdocument()) {
owner->processDocument(*document);
}
};
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:
processMedia();
return ExtractCollage(
owner,
block.c_pageBlockSlideshow().vitems().v,
page.vphotos().v,
page.vdocuments().v);
case mtpc_pageBlockCollage:
processMedia();
return ExtractCollage(
owner,
block.c_pageBlockCollage().vitems().v,
page.vphotos().v,
page.vdocuments().v);
default: break;
}
}
return WebPageCollage();
});
}
} // namespace
WebPageType ParseWebPageType(
const QString &type,
const QString &embedUrl,
bool hasIV) {
if (type == u"video"_q || !embedUrl.isEmpty()) {
return WebPageType::Video;
} else if (type == u"photo"_q) {
return WebPageType::Photo;
} else if (type == u"document"_q) {
return WebPageType::Document;
} else if (type == u"profile"_q) {
return WebPageType::Profile;
} else if (type == u"telegram_background"_q) {
return WebPageType::WallPaper;
} else if (type == u"telegram_theme"_q) {
return WebPageType::Theme;
} else if (type == u"telegram_channel"_q) {
return WebPageType::Channel;
} else if (type == u"telegram_channel_request"_q) {
return WebPageType::ChannelWithRequest;
} else if (type == u"telegram_megagroup"_q
|| type == u"telegram_chat"_q) {
return WebPageType::Group;
} else if (type == u"telegram_megagroup_request"_q
|| type == u"telegram_chat_request"_q) {
return WebPageType::GroupWithRequest;
} else if (type == u"telegram_message"_q) {
return WebPageType::Message;
} else if (type == u"telegram_bot"_q) {
return WebPageType::Bot;
} else if (type == u"telegram_voicechat"_q) {
return WebPageType::VoiceChat;
} else if (type == u"telegram_livestream"_q) {
return WebPageType::Livestream;
} else if (type == u"telegram_user"_q) {
return WebPageType::User;
} else if (hasIV) {
return WebPageType::ArticleWithIV;
} else {
return WebPageType::Article;
}
}
WebPageType ParseWebPageType(const MTPDwebPage &page) {
return ParseWebPageType(
qs(page.vtype().value_or_empty()),
page.vembed_url().value_or_empty(),
!!page.vcached_page());
}
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();
}
bool WebPageData::applyChanges(
WebPageType newType,
const QString &newUrl,
const QString &newDisplayUrl,
const QString &newSiteName,
const QString &newTitle,
const TextWithEntities &newDescription,
PhotoData *newPhoto,
DocumentData *newDocument,
WebPageCollage &&newCollage,
int newDuration,
const QString &newAuthor,
int newPendingTill) {
if (newPendingTill != 0
&& (!url.isEmpty() || pendingTill < 0)
&& (!pendingTill
|| pendingTill == newPendingTill
|| newPendingTill < -1)) {
return false;
}
const auto resultUrl = newUrl;
const auto resultDisplayUrl = newDisplayUrl;
const auto possibleSiteName = newSiteName;
const auto resultTitle = TextUtilities::SingleLine(newTitle);
const auto resultAuthor = newAuthor;
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
&& url == resultUrl
&& displayUrl == resultDisplayUrl
&& siteName == resultSiteName
&& title == resultTitle
&& description.text == newDescription.text
&& photo == newPhoto
&& document == newDocument
&& collage.items == newCollage.items
&& duration == newDuration
&& author == resultAuthor
&& pendingTill == newPendingTill) {
return false;
}
if (pendingTill > 0 && newPendingTill <= 0) {
_owner->session().api().clearWebPageRequest(this);
}
type = newType;
url = resultUrl;
displayUrl = resultDisplayUrl;
siteName = resultSiteName;
title = resultTitle;
description = newDescription;
photo = newPhoto;
document = newDocument;
collage = std::move(newCollage);
duration = newDuration;
author = resultAuthor;
pendingTill = newPendingTill;
++version;
if (type == WebPageType::WallPaper && document) {
document->checkWallPaperProperties();
}
replaceDocumentGoodThumbnail();
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();
}
|