From 542abb26b9620c594d3b4cc9f1c82d7e7ce0c8f0 Mon Sep 17 00:00:00 2001 From: John Preston Date: Fri, 15 Jan 2021 20:55:22 +0400 Subject: [PATCH] Allow sharing link to chats. --- .../boxes/peers/edit_peer_info_box.cpp | 2 +- .../boxes/peers/edit_peer_invite_links.cpp | 61 ++++++++++++++++++- Telegram/SourceFiles/data/data_changes.h | 3 +- Telegram/SourceFiles/mainwidget.cpp | 38 +++++++----- 4 files changed, 85 insertions(+), 19 deletions(-) diff --git a/Telegram/SourceFiles/boxes/peers/edit_peer_info_box.cpp b/Telegram/SourceFiles/boxes/peers/edit_peer_info_box.cpp index a3ae08f6e..7c3c9f0c1 100644 --- a/Telegram/SourceFiles/boxes/peers/edit_peer_info_box.cpp +++ b/Telegram/SourceFiles/boxes/peers/edit_peer_info_box.cpp @@ -1003,7 +1003,7 @@ void Controller::fillManageSection() { }); }) | rpl::flatten_latest( ) | ToPositiveNumberString(), - [=] { ShowEditInviteLinks(_navigation, _peer); }, + [=] { }, st::infoIconInviteLinks); } if (canViewAdmins) { diff --git a/Telegram/SourceFiles/boxes/peers/edit_peer_invite_links.cpp b/Telegram/SourceFiles/boxes/peers/edit_peer_invite_links.cpp index bb8b31883..d963db7bc 100644 --- a/Telegram/SourceFiles/boxes/peers/edit_peer_invite_links.cpp +++ b/Telegram/SourceFiles/boxes/peers/edit_peer_invite_links.cpp @@ -9,7 +9,9 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "data/data_changes.h" #include "data/data_user.h" +#include "data/data_drafts.h" #include "main/main_session.h" +#include "data/data_session.h" #include "api/api_invite_links.h" #include "ui/wrap/vertical_layout.h" #include "ui/wrap/padding_wrap.h" @@ -19,13 +21,69 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "ui/controls/invite_link_buttons.h" #include "ui/toast/toast.h" #include "history/view/history_view_group_call_tracker.h" // GenerateUs... +#include "history/history.h" #include "lang/lang_keys.h" #include "boxes/confirm_box.h" +#include "boxes/peer_list_box.h" +#include "boxes/peer_list_controllers.h" #include "apiwrap.h" +#include "api/api_common.h" #include "styles/style_info.h" #include +namespace { + +void ShareLinkBox(not_null peer, const QString &link) { + const auto weak = std::make_shared>(); + auto callback = [ + weak, + link + ](not_null peer) mutable { + const auto history = peer->owner().history(peer); + if (peer->isSelf()) { + const auto api = &peer->session().api(); + auto message = Api::MessageToSend(history); + message.action.clearDraft = false; + message.action.generateLocal = false; + message.textWithTags = { link }; + api->sendMessage(std::move(message)); + Ui::Toast::Show(tr::lng_share_done(tr::now)); + } else { + auto textWithTags = TextWithTags{ + link + '\n', + TextWithTags::Tags() + }; + auto cursor = MessageCursor{ + link.size() + 1, + link.size() + 1, + QFIXED_MAX + }; + history->setLocalDraft( + std::make_unique(textWithTags, 0, cursor, false)); + history->clearLocalEditDraft(); + history->session().changes().historyUpdated( + history, + Data::HistoryUpdate::Flag::LocalDraftSet); + } + if (const auto strong = *weak) { + strong->closeBox(); + } + }; + auto initBox = [](not_null box) { + box->addButton(tr::lng_cancel(), [box] { + box->closeBox(); + }); + }; + *weak = Ui::show(Box( + std::make_unique( + &peer->session(), + std::move(callback)), + std::move(initBox)), Ui::LayerOption::KeepOther); +} + +} // namespace + void AddPermanentLinkBlock( not_null container, not_null peer) { @@ -55,8 +113,7 @@ void AddPermanentLinkBlock( }); const auto shareLink = crl::guard(weak, [=] { if (const auto link = computePermanentLink()) { - QGuiApplication::clipboard()->setText(link->link); - Ui::Toast::Show(tr::lng_group_invite_copied(tr::now)); + ShareLinkBox(peer, link->link); } }); const auto revokeLink = crl::guard(weak, [=] { diff --git a/Telegram/SourceFiles/data/data_changes.h b/Telegram/SourceFiles/data/data_changes.h index cf00ebec3..0a162906f 100644 --- a/Telegram/SourceFiles/data/data_changes.h +++ b/Telegram/SourceFiles/data/data_changes.h @@ -116,8 +116,9 @@ struct HistoryUpdate { OutboxRead = (1 << 10), BotKeyboard = (1 << 11), CloudDraft = (1 << 12), + LocalDraftSet = (1 << 13), - LastUsedBit = (1 << 12), + LastUsedBit = (1 << 13), }; using Flags = base::flags; friend inline constexpr auto is_flag_type(Flag) { return true; } diff --git a/Telegram/SourceFiles/mainwidget.cpp b/Telegram/SourceFiles/mainwidget.cpp index e7c258519..bbf183590 100644 --- a/Telegram/SourceFiles/mainwidget.cpp +++ b/Telegram/SourceFiles/mainwidget.cpp @@ -301,14 +301,27 @@ MainWidget::MainWidget( session().changes().historyUpdates( Data::HistoryUpdate::Flag::MessageSent + | Data::HistoryUpdate::Flag::LocalDraftSet ) | rpl::start_with_next([=](const Data::HistoryUpdate &update) { const auto history = update.history; - history->forgetScrollState(); - if (const auto from = history->peer->migrateFrom()) { - if (const auto migrated = history->owner().historyLoaded(from)) { - migrated->forgetScrollState(); + if (update.flags & Data::HistoryUpdate::Flag::MessageSent) { + history->forgetScrollState(); + if (const auto from = history->peer->migrateFrom()) { + auto &owner = history->owner(); + if (const auto migrated = owner.historyLoaded(from)) { + migrated->forgetScrollState(); + } } } + if (update.flags & Data::HistoryUpdate::Flag::LocalDraftSet) { + const auto opened = (_history->peer() == history->peer.get()); + if (opened) { + _history->applyDraft(); + } else { + Ui::showPeerHistory(history, ShowAtUnreadMsgId); + } + Ui::hideLayer(); + } }, lifetime()); // MSVC BUG + REGRESSION rpl::mappers::tuple :( @@ -538,11 +551,9 @@ bool MainWidget::shareUrl( history->setLocalDraft( std::make_unique(textWithTags, 0, cursor, false)); history->clearLocalEditDraft(); - if (_history->peer() == peer) { - _history->applyDraft(); - } else { - Ui::showPeerHistory(peer, ShowAtUnreadMsgId); - } + history->session().changes().historyUpdated( + history, + Data::HistoryUpdate::Flag::LocalDraftSet); return true; } @@ -567,12 +578,9 @@ bool MainWidget::inlineSwitchChosen(PeerId peerId, const QString &botAndQuery) { MessageCursor cursor = { botAndQuery.size(), botAndQuery.size(), QFIXED_MAX }; h->setLocalDraft(std::make_unique(textWithTags, 0, cursor, false)); h->clearLocalEditDraft(); - const auto opened = _history->peer() && (_history->peer() == peer); - if (opened) { - _history->applyDraft(); - } else { - Ui::showPeerHistory(peer, ShowAtUnreadMsgId); - } + h->session().changes().historyUpdated( + h, + Data::HistoryUpdate::Flag::LocalDraftSet); return true; }