Allow creating discussion groups for channels.

This commit is contained in:
John Preston 2019-05-24 13:14:02 +02:00
parent b9d89cb4a0
commit ee2c7fad78
5 changed files with 56 additions and 28 deletions

View File

@ -352,12 +352,6 @@ protected:
};
enum CreatingGroupType {
CreatingGroupNone,
CreatingGroupGroup,
CreatingGroupChannel,
};
class BoxPointer {
public:
BoxPointer() = default;

View File

@ -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<void(not_null<ChannelData*>)> 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<const QVector<MTPChat>*> {
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<SetupChannelBox>(_createdChannel));
if (_channelDone) {
const auto callback = _channelDone;
const auto argument = _createdChannel;
closeBox();
callback(argument);
} else {
Ui::show(Box<SetupChannelBox>(_createdChannel));
}
}).send();
};
if (!success) {

View File

@ -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<void(not_null<ChannelData*>)> 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<void(not_null<ChannelData*>)> _channelDone;
object_ptr<Ui::UserpicButton> _photo = { nullptr };
object_ptr<Ui::InputField> _title = { nullptr };

View File

@ -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<Ui::RpWidget> SetupAbout(
object_ptr<Ui::RpWidget> SetupCreateGroup(
not_null<QWidget*> parent,
not_null<ChannelData*> channel) {
not_null<ChannelData*> channel,
Fn<void(ChannelData*)> callback) {
Expects(channel->isBroadcast());
auto result = object_ptr<Info::Profile::Button>(
parent,
Lang::Viewer(
@ -143,7 +147,13 @@ object_ptr<Ui::RpWidget> SetupCreateGroup(
) | Info::Profile::ToUpperValue(),
st::infoCreateLinkedChatButton);
result->addClickHandler([=] {
const auto guarded = crl::guard(parent, callback);
Ui::show(
Box<GroupInfoBox>(
GroupInfoBox::Type::Megagroup,
channel->name + " Chat",
guarded),
LayerOption::KeepOther);
});
return result;
}
@ -197,7 +207,7 @@ object_ptr<Ui::RpWidget> 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) {

View File

@ -592,7 +592,7 @@ void MainWindow::onShowNewGroup() {
if (AuthSession::Exists()) {
Ui::show(
Box<GroupInfoBox>(CreatingGroupGroup, false),
Box<GroupInfoBox>(GroupInfoBox::Type::Group),
LayerOption::KeepOther);
}
}
@ -602,7 +602,7 @@ void MainWindow::onShowNewChannel() {
if (_main) {
Ui::show(
Box<GroupInfoBox>(CreatingGroupChannel, false),
Box<GroupInfoBox>(GroupInfoBox::Type::Channel),
LayerOption::KeepOther);
}
}