From ee2c7fad784b702313bdc66150685a816bd92f96 Mon Sep 17 00:00:00 2001 From: John Preston Date: Fri, 24 May 2019 13:14:02 +0200 Subject: [PATCH] Allow creating discussion groups for channels. --- Telegram/SourceFiles/boxes/abstract_box.h | 6 --- .../SourceFiles/boxes/add_contact_box.cpp | 42 ++++++++++++------- Telegram/SourceFiles/boxes/add_contact_box.h | 16 +++++-- .../boxes/peers/edit_linked_chat_box.cpp | 16 +++++-- Telegram/SourceFiles/mainwindow.cpp | 4 +- 5 files changed, 56 insertions(+), 28 deletions(-) diff --git a/Telegram/SourceFiles/boxes/abstract_box.h b/Telegram/SourceFiles/boxes/abstract_box.h index dec7833404..d979fdd1bf 100644 --- a/Telegram/SourceFiles/boxes/abstract_box.h +++ b/Telegram/SourceFiles/boxes/abstract_box.h @@ -352,12 +352,6 @@ protected: }; -enum CreatingGroupType { - CreatingGroupNone, - CreatingGroupGroup, - CreatingGroupChannel, -}; - class BoxPointer { public: BoxPointer() = default; diff --git a/Telegram/SourceFiles/boxes/add_contact_box.cpp b/Telegram/SourceFiles/boxes/add_contact_box.cpp index a7a150d535..db8e888b5a 100644 --- a/Telegram/SourceFiles/boxes/add_contact_box.cpp +++ b/Telegram/SourceFiles/boxes/add_contact_box.cpp @@ -398,9 +398,14 @@ void AddContactBox::updateButtons() { } } -GroupInfoBox::GroupInfoBox(QWidget*, CreatingGroupType creating, bool fromTypeChoose) -: _creating(creating) -, _fromTypeChoose(fromTypeChoose) { +GroupInfoBox::GroupInfoBox( + QWidget*, + Type type, + const QString &title, + Fn)> channelDone) +: _type(type) +, _initialTitle(title) +, _channelDone(std::move(channelDone)) { } void GroupInfoBox::prepare() { @@ -408,7 +413,7 @@ void GroupInfoBox::prepare() { _photo.create( this, - lang((_creating == CreatingGroupChannel) + lang((_type == Type::Channel) ? lng_create_channel_crop : lng_create_group_crop), Ui::UserpicButton::Role::ChangePhoto, @@ -416,9 +421,10 @@ void GroupInfoBox::prepare() { _title.create( this, st::defaultInputField, - langFactory(_creating == CreatingGroupChannel + langFactory((_type == Type::Channel) ? lng_dlg_new_channel_name - : lng_dlg_new_group_name)); + : lng_dlg_new_group_name), + _initialTitle); _title->setMaxLength(kMaxGroupChannelTitle); _title->setInstantReplaces(Ui::InstantReplaces::Default()); _title ->setInstantReplacesEnabled(Global::ReplaceEmojiValue()); @@ -426,7 +432,7 @@ void GroupInfoBox::prepare() { getDelegate()->outerContainer(), _title); - if (_creating == CreatingGroupChannel) { + if (_type != Type::Group) { _description.create( this, st::newGroupDescription, @@ -449,8 +455,8 @@ void GroupInfoBox::prepare() { connect(_title, &Ui::InputField::submitted, [=] { submitName(); }); - addButton(langFactory(_creating == CreatingGroupChannel ? lng_create_group_create : lng_create_group_next), [this] { submit(); }); - addButton(langFactory(_fromTypeChoose ? lng_create_group_back : lng_cancel), [this] { closeBox(); }); + addButton(langFactory((_type != Type::Group) ? lng_create_group_create : lng_create_group_next), [this] { submit(); }); + addButton(langFactory(lng_cancel), [this] { closeBox(); }); updateMaxHeight(); } @@ -590,7 +596,7 @@ void GroupInfoBox::submit() { _title->showError(); return; } - if (_creating != CreatingGroupGroup) { + if (_type != Type::Group) { createChannel(title, description); } else { auto initBox = [title, weak = make_weak(this)]( @@ -619,8 +625,9 @@ void GroupInfoBox::submit() { } void GroupInfoBox::createChannel(const QString &title, const QString &description) { - bool mega = false; - auto flags = mega ? MTPchannels_CreateChannel::Flag::f_megagroup : MTPchannels_CreateChannel::Flag::f_broadcast; + const auto flags = (_type == Type::Megagroup) + ? MTPchannels_CreateChannel::Flag::f_megagroup + : MTPchannels_CreateChannel::Flag::f_broadcast; _creationRequestId = request(MTPchannels_CreateChannel( MTP_flags(flags), MTP_string(title), @@ -628,7 +635,7 @@ void GroupInfoBox::createChannel(const QString &title, const QString &descriptio )).done([=](const MTPUpdates &result) { Auth().api().applyUpdates(result); - auto success = base::make_optional(&result) + const auto success = base::make_optional(&result) | [](auto updates) -> std::optional*> { switch (updates->type()) { case mtpc_updates: @@ -663,7 +670,14 @@ void GroupInfoBox::createChannel(const QString &title, const QString &descriptio auto link = qs(result.c_chatInviteExported().vlink); _createdChannel->setInviteLink(link); } - Ui::show(Box(_createdChannel)); + if (_channelDone) { + const auto callback = _channelDone; + const auto argument = _createdChannel; + closeBox(); + callback(argument); + } else { + Ui::show(Box(_createdChannel)); + } }).send(); }; if (!success) { diff --git a/Telegram/SourceFiles/boxes/add_contact_box.h b/Telegram/SourceFiles/boxes/add_contact_box.h index 6735644631..2a96be2a66 100644 --- a/Telegram/SourceFiles/boxes/add_contact_box.h +++ b/Telegram/SourceFiles/boxes/add_contact_box.h @@ -84,7 +84,16 @@ private: class GroupInfoBox : public BoxContent, private MTP::Sender { public: - GroupInfoBox(QWidget*, CreatingGroupType creating, bool fromTypeChoose); + enum class Type { + Group, + Channel, + Megagroup, + }; + GroupInfoBox( + QWidget*, + Type type, + const QString &title = QString(), + Fn)> channelDone = nullptr); protected: void prepare() override; @@ -101,8 +110,9 @@ private: void descriptionResized(); void updateMaxHeight(); - CreatingGroupType _creating; - bool _fromTypeChoose = false; + Type _type = Type::Group; + QString _initialTitle; + Fn)> _channelDone; object_ptr _photo = { nullptr }; object_ptr _title = { nullptr }; diff --git a/Telegram/SourceFiles/boxes/peers/edit_linked_chat_box.cpp b/Telegram/SourceFiles/boxes/peers/edit_linked_chat_box.cpp index 0f6e2bcc36..bd42a4b3a9 100644 --- a/Telegram/SourceFiles/boxes/peers/edit_linked_chat_box.cpp +++ b/Telegram/SourceFiles/boxes/peers/edit_linked_chat_box.cpp @@ -14,6 +14,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "info/profile/info_profile_button.h" #include "info/profile/info_profile_values.h" #include "boxes/peer_list_box.h" +#include "boxes/add_contact_box.h" #include "styles/style_boxes.h" #include "styles/style_info.h" @@ -135,7 +136,10 @@ object_ptr SetupAbout( object_ptr SetupCreateGroup( not_null parent, - not_null channel) { + not_null channel, + Fn callback) { + Expects(channel->isBroadcast()); + auto result = object_ptr( parent, Lang::Viewer( @@ -143,7 +147,13 @@ object_ptr SetupCreateGroup( ) | Info::Profile::ToUpperValue(), st::infoCreateLinkedChatButton); result->addClickHandler([=] { - + const auto guarded = crl::guard(parent, callback); + Ui::show( + Box( + GroupInfoBox::Type::Megagroup, + channel->name + " Chat", + guarded), + LayerOption::KeepOther); }); return result; } @@ -197,7 +207,7 @@ object_ptr EditLinkedChatBox::setupContent( SetupAbout(result, channel, chat), st::linkedChatAboutPadding); if (!chat) { - result->add(SetupCreateGroup(result, channel)); + result->add(SetupCreateGroup(result, channel, callback)); } result->add(SetupList(result, channel, chat, chats, callback)); if (chat) { diff --git a/Telegram/SourceFiles/mainwindow.cpp b/Telegram/SourceFiles/mainwindow.cpp index 12d33ade85..6098d3e7c1 100644 --- a/Telegram/SourceFiles/mainwindow.cpp +++ b/Telegram/SourceFiles/mainwindow.cpp @@ -592,7 +592,7 @@ void MainWindow::onShowNewGroup() { if (AuthSession::Exists()) { Ui::show( - Box(CreatingGroupGroup, false), + Box(GroupInfoBox::Type::Group), LayerOption::KeepOther); } } @@ -602,7 +602,7 @@ void MainWindow::onShowNewChannel() { if (_main) { Ui::show( - Box(CreatingGroupChannel, false), + Box(GroupInfoBox::Type::Channel), LayerOption::KeepOther); } }