Allow ctrl/cmd+click copy private post link.

This commit is contained in:
John Preston 2024-11-04 15:22:13 +04:00
parent f77fdc799d
commit 52953626a7
4 changed files with 33 additions and 9 deletions

View file

@ -2101,6 +2101,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
"lng_channel_public_link_copied" = "Link copied to clipboard."; "lng_channel_public_link_copied" = "Link copied to clipboard.";
"lng_context_about_private_link" = "This link will only work for members of this chat."; "lng_context_about_private_link" = "This link will only work for members of this chat.";
"lng_public_post_private_hint_ctrl" = "Use Ctrl+Click to copy a non-public link.";
"lng_public_post_private_hint_cmd" = "Use Cmd+Click to copy a non-public link.";
"lng_forwarded" = "Forwarded from {user}"; "lng_forwarded" = "Forwarded from {user}";
"lng_forwarded_story" = "Story from {user}"; "lng_forwarded_story" = "Story from {user}";

View file

@ -708,7 +708,8 @@ void ApiWrap::finalizeMessageDataRequest(
QString ApiWrap::exportDirectMessageLink( QString ApiWrap::exportDirectMessageLink(
not_null<HistoryItem*> item, not_null<HistoryItem*> item,
bool inRepliesContext) { bool inRepliesContext,
bool forceNonPublicLink) {
Expects(item->history()->peer->isChannel()); Expects(item->history()->peer->isChannel());
const auto itemId = item->fullId(); const auto itemId = item->fullId();
@ -731,7 +732,7 @@ QString ApiWrap::exportDirectMessageLink(
const auto sender = root const auto sender = root
? root->discussionPostOriginalSender() ? root->discussionPostOriginalSender()
: nullptr; : nullptr;
if (sender && sender->hasUsername()) { if (sender && sender->hasUsername() && !forceNonPublicLink) {
// Comment to a public channel. // Comment to a public channel.
const auto forwarded = root->Get<HistoryMessageForwarded>(); const auto forwarded = root->Get<HistoryMessageForwarded>();
linkItemId = forwarded->savedFromMsgId; linkItemId = forwarded->savedFromMsgId;
@ -747,7 +748,7 @@ QString ApiWrap::exportDirectMessageLink(
} }
} }
} }
const auto base = linkChannel->hasUsername() const auto base = (linkChannel->hasUsername() && !forceNonPublicLink)
? linkChannel->username() ? linkChannel->username()
: "c/" + QString::number(peerToChannel(linkChannel->id).bare); : "c/" + QString::number(peerToChannel(linkChannel->id).bare);
const auto post = QString::number(linkItemId.bare); const auto post = QString::number(linkItemId.bare);
@ -761,6 +762,7 @@ QString ApiWrap::exportDirectMessageLink(
? (QString::number(linkThreadId.bare) + '/' + post) ? (QString::number(linkThreadId.bare) + '/' + post)
: post); : post);
if (linkChannel->hasUsername() if (linkChannel->hasUsername()
&& !forceNonPublicLink
&& !linkChannel->isMegagroup() && !linkChannel->isMegagroup()
&& !linkCommentId && !linkCommentId
&& !linkThreadId) { && !linkThreadId) {
@ -774,6 +776,9 @@ QString ApiWrap::exportDirectMessageLink(
} }
return session().createInternalLinkFull(query); return session().createInternalLinkFull(query);
}; };
if (forceNonPublicLink) {
return fallback();
}
const auto i = _unlikelyMessageLinks.find(itemId); const auto i = _unlikelyMessageLinks.find(itemId);
const auto current = (i != end(_unlikelyMessageLinks)) const auto current = (i != end(_unlikelyMessageLinks))
? i->second ? i->second

View file

@ -164,7 +164,8 @@ public:
void requestMessageData(PeerData *peer, MsgId msgId, Fn<void()> done); void requestMessageData(PeerData *peer, MsgId msgId, Fn<void()> done);
QString exportDirectMessageLink( QString exportDirectMessageLink(
not_null<HistoryItem*> item, not_null<HistoryItem*> item,
bool inRepliesContext); bool inRepliesContext,
bool forceNonPublicLink = false);
QString exportDirectStoryLink(not_null<Data::Story*> item); QString exportDirectStoryLink(not_null<Data::Story*> item);
void requestContacts(); void requestContacts();

View file

@ -98,6 +98,7 @@ namespace {
constexpr auto kRescheduleLimit = 20; constexpr auto kRescheduleLimit = 20;
constexpr auto kTagNameLimit = 12; constexpr auto kTagNameLimit = 12;
constexpr auto kPublicPostLinkToastDuration = 4 * crl::time(1000);
bool HasEditMessageAction( bool HasEditMessageAction(
const ContextMenuRequest &request, const ContextMenuRequest &request,
@ -1307,12 +1308,17 @@ void CopyPostLink(
return; return;
} }
const auto inRepliesContext = (context == Context::Replies); const auto inRepliesContext = (context == Context::Replies);
const auto forceNonPublicLink = base::IsCtrlPressed();
QGuiApplication::clipboard()->setText( QGuiApplication::clipboard()->setText(
item->history()->session().api().exportDirectMessageLink( item->history()->session().api().exportDirectMessageLink(
item, item,
inRepliesContext)); inRepliesContext,
forceNonPublicLink));
const auto isPublicLink = [&] { const auto isPublicLink = [&] {
if (forceNonPublicLink) {
return false;
}
const auto channel = item->history()->peer->asChannel(); const auto channel = item->history()->peer->asChannel();
Assert(channel != nullptr); Assert(channel != nullptr);
if (const auto rootId = item->replyToTop()) { if (const auto rootId = item->replyToTop()) {
@ -1328,10 +1334,20 @@ void CopyPostLink(
} }
return channel->hasUsername(); return channel->hasUsername();
}(); }();
if (isPublicLink) {
show->showToast(isPublicLink show->showToast({
? tr::lng_channel_public_link_copied(tr::now) .text = tr::lng_channel_public_link_copied(
: tr::lng_context_about_private_link(tr::now)); tr::now, Ui::Text::Bold
).append('\n').append(Platform::IsMac()
? tr::lng_public_post_private_hint_cmd(tr::now)
: tr::lng_public_post_private_hint_ctrl(tr::now)),
.duration = kPublicPostLinkToastDuration,
});
} else {
show->showToast(isPublicLink
? tr::lng_channel_public_link_copied(tr::now)
: tr::lng_context_about_private_link(tr::now));
}
} }
void CopyStoryLink( void CopyStoryLink(