Use channels.exportMessageLink to check links.

This commit is contained in:
John Preston 2019-04-05 15:50:16 +04:00
parent 60a991bcb0
commit 5deee18247
5 changed files with 68 additions and 39 deletions

View File

@ -615,6 +615,46 @@ void ApiWrap::finalizeMessageDataRequest(
}
}
QString ApiWrap::exportDirectMessageLink(not_null<HistoryItem*> item) {
Expects(item->history()->peer->isChannel());
const auto itemId = item->fullId();
const auto channel = item->history()->peer->asChannel();
const auto fallback = [&] {
const auto base = channel->isPublic()
? channel->username
: "c/" + QString::number(channel->bareId());
const auto query = base + '/' + QString::number(item->id);
if (channel->isPublic() && !channel->isMegagroup()) {
if (const auto media = item->media()) {
if (const auto document = media->document()) {
if (document->isVideoMessage()) {
return qsl("https://telesco.pe/") + query;
}
}
}
}
return Core::App().createInternalLinkFull(query);
};
const auto i = _unlikelyMessageLinks.find(itemId);
const auto current = (i != end(_unlikelyMessageLinks))
? i->second
: fallback();
request(MTPchannels_ExportMessageLink(
channel->inputChannel,
MTP_int(item->id),
MTP_bool(false)
)).done([=](const MTPExportedMessageLink &result) {
const auto link = result.match([&](const auto &data) {
return qs(data.vlink);
});
if (current != link) {
_unlikelyMessageLinks.emplace_or_assign(itemId, link);
}
}).send();
return current;
}
void ApiWrap::requestContacts() {
if (_session->data().contactsLoaded().value() || _contactsRequestId) {
return;
@ -5711,11 +5751,11 @@ void ApiWrap::sendPollVotes(
_pollVotesRequestIds.emplace(itemId, requestId);
}
void ApiWrap::closePoll(FullMsgId itemId) {
void ApiWrap::closePoll(not_null<HistoryItem*> item) {
const auto itemId = item->fullId();
if (_pollCloseRequestIds.contains(itemId)) {
return;
}
const auto item = App::histItemById(itemId);
const auto media = item ? item->media() : nullptr;
const auto poll = media ? media->poll() : nullptr;
if (!poll) {

View File

@ -76,6 +76,7 @@ public:
ChannelData *channel,
MsgId msgId,
RequestMessageDataCallback callback);
QString exportDirectMessageLink(not_null<HistoryItem*> item);
void requestContacts();
void requestDialogEntry(not_null<Data::Feed*> feed);
@ -427,7 +428,7 @@ public:
void sendPollVotes(
FullMsgId itemId,
const std::vector<QByteArray> &options);
void closePoll(FullMsgId itemId);
void closePoll(not_null<HistoryItem*> item);
void reloadPollResults(not_null<HistoryItem*> item);
~ApiWrap();
@ -822,4 +823,6 @@ private:
mtpRequestId _attachedStickerSetsRequestId = 0;
base::flat_map<FullMsgId, QString> _unlikelyMessageLinks;
};

View File

@ -552,29 +552,6 @@ bool HistoryItem::hasDirectLink() const {
return IsServerMsgId(id) && _history->peer->isChannel();
}
QString HistoryItem::directLink() const {
if (hasDirectLink()) {
const auto channel = _history->peer->asChannel();
Assert(channel != nullptr);
const auto base = channel->isPublic()
? channel->username
: "c/" + QString::number(channel->bareId());
const auto query = base + '/' + QString::number(id);
if (channel->isPublic() && !channel->isMegagroup()) {
if (const auto media = this->media()) {
if (const auto document = media->document()) {
if (document->isVideoMessage()) {
return qsl("https://telesco.pe/") + query;
}
}
}
}
return Core::App().createInternalLinkFull(query);
}
return QString();
}
ChannelId HistoryItem::channelId() const {
return _history->channelId();
}

View File

@ -254,7 +254,6 @@ public:
bool suggestDeleteAllReport() const;
bool hasDirectLink() const;
QString directLink() const;
MsgId id;

View File

@ -38,6 +38,9 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
namespace HistoryView {
namespace {
// If we can't cloud-export link for such time we export it locally.
constexpr auto kExportLocalTimeout = crl::time(1000);
void AddToggleGroupingAction(
not_null<Ui::PopupMenu*> menu,
not_null<PeerData*> peer) {
@ -506,26 +509,33 @@ base::unique_qptr<Ui::PopupMenu> FillContextMenu(
}
void CopyPostLink(FullMsgId itemId) {
if (const auto item = App::histItemById(itemId)) {
if (item->hasDirectLink()) {
QApplication::clipboard()->setText(item->directLink());
const auto channel = item->history()->peer->asChannel();
Assert(channel != nullptr);
Ui::Toast::Show(lang(channel->isPublic()
? lng_channel_public_link_copied
: lng_context_about_private_link));
}
const auto item = App::histItemById(itemId);
if (!item || !item->hasDirectLink()) {
return;
}
QApplication::clipboard()->setText(
item->history()->session().api().exportDirectMessageLink(item));
const auto channel = item->history()->peer->asChannel();
Assert(channel != nullptr);
Ui::Toast::Show(lang(channel->isPublic()
? lng_channel_public_link_copied
: lng_context_about_private_link));
}
void StopPoll(FullMsgId itemId) {
const auto stop = [=] {
Ui::hideLayer();
if (const auto item = App::histItemById(itemId)) {
item->history()->session().api().closePoll(item);
}
};
Ui::show(Box<ConfirmBox>(
lang(lng_polls_stop_warning),
lang(lng_polls_stop_sure),
lang(lng_cancel),
[=] { Ui::hideLayer(); Auth().api().closePoll(itemId); }));
stop));
}
} // namespace HistoryView