2024-02-20 14:33:46 +04:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop application for the Telegram messaging service.
|
|
|
|
|
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
|
|
|
*/
|
|
|
|
#include "settings/business/settings_chatbots.h"
|
|
|
|
|
2024-02-20 16:02:02 +04:00
|
|
|
#include "core/application.h"
|
|
|
|
#include "data/business/data_business_chatbots.h"
|
|
|
|
#include "data/data_session.h"
|
|
|
|
#include "data/data_user.h"
|
2024-02-20 14:33:46 +04:00
|
|
|
#include "lang/lang_keys.h"
|
2024-02-20 16:02:02 +04:00
|
|
|
#include "main/main_session.h"
|
|
|
|
#include "settings/business/settings_business_exceptions.h"
|
2024-02-20 14:33:46 +04:00
|
|
|
#include "settings/settings_common_session.h"
|
|
|
|
#include "ui/text/text_utilities.h"
|
|
|
|
#include "ui/widgets/fields/input_field.h"
|
|
|
|
#include "ui/widgets/checkbox.h"
|
|
|
|
#include "ui/wrap/slide_wrap.h"
|
|
|
|
#include "ui/wrap/vertical_layout.h"
|
|
|
|
#include "ui/vertical_list.h"
|
2024-02-20 16:02:02 +04:00
|
|
|
#include "window/window_session_controller.h"
|
2024-02-20 14:33:46 +04:00
|
|
|
#include "styles/style_layers.h"
|
|
|
|
#include "styles/style_settings.h"
|
|
|
|
|
|
|
|
namespace Settings {
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
constexpr auto kAllExcept = 0;
|
|
|
|
constexpr auto kSelectedOnly = 1;
|
|
|
|
|
|
|
|
class Chatbots : public Section<Chatbots> {
|
|
|
|
public:
|
|
|
|
Chatbots(
|
|
|
|
QWidget *parent,
|
|
|
|
not_null<Window::SessionController*> controller);
|
2024-02-20 16:02:02 +04:00
|
|
|
~Chatbots();
|
2024-02-20 14:33:46 +04:00
|
|
|
|
|
|
|
[[nodiscard]] rpl::producer<QString> title() override;
|
|
|
|
|
|
|
|
rpl::producer<> showFinishes() const {
|
|
|
|
return _showFinished.events();
|
|
|
|
}
|
|
|
|
|
|
|
|
const Ui::RoundRect *bottomSkipRounding() const {
|
|
|
|
return &_bottomSkipRounding;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void setupContent(not_null<Window::SessionController*> controller);
|
2024-02-20 16:02:02 +04:00
|
|
|
void save();
|
2024-02-20 14:33:46 +04:00
|
|
|
|
|
|
|
void showFinished() override {
|
|
|
|
_showFinished.fire({});
|
|
|
|
}
|
|
|
|
|
2024-02-20 16:02:02 +04:00
|
|
|
const not_null<Window::SessionController*> _controller;
|
|
|
|
const not_null<Main::Session*> _session;
|
|
|
|
|
2024-02-20 14:33:46 +04:00
|
|
|
rpl::event_stream<> _showFinished;
|
|
|
|
Ui::RoundRect _bottomSkipRounding;
|
|
|
|
|
2024-02-20 16:02:02 +04:00
|
|
|
rpl::variable<bool> _onlySelected = false;
|
|
|
|
rpl::variable<bool> _repliesAllowed = true;
|
|
|
|
rpl::variable<Data::BusinessExceptions> _allowed;
|
|
|
|
rpl::variable<Data::BusinessExceptions> _disallowed;
|
|
|
|
|
2024-02-20 14:33:46 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
Chatbots::Chatbots(
|
|
|
|
QWidget *parent,
|
|
|
|
not_null<Window::SessionController*> controller)
|
|
|
|
: Section(parent)
|
2024-02-20 16:02:02 +04:00
|
|
|
, _controller(controller)
|
|
|
|
, _session(&controller->session())
|
2024-02-20 14:33:46 +04:00
|
|
|
, _bottomSkipRounding(st::boxRadius, st::boxDividerBg) {
|
|
|
|
setupContent(controller);
|
|
|
|
}
|
|
|
|
|
2024-02-20 16:02:02 +04:00
|
|
|
Chatbots::~Chatbots() {
|
|
|
|
if (!Core::Quitting()) {
|
|
|
|
save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-20 14:33:46 +04:00
|
|
|
rpl::producer<QString> Chatbots::title() {
|
|
|
|
return tr::lng_chatbots_title();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Chatbots::setupContent(
|
|
|
|
not_null<Window::SessionController*> controller) {
|
|
|
|
using namespace rpl::mappers;
|
|
|
|
|
|
|
|
const auto content = Ui::CreateChild<Ui::VerticalLayout>(this);
|
2024-02-20 16:02:02 +04:00
|
|
|
const auto current = controller->session().data().chatbots().current();
|
2024-02-20 14:33:46 +04:00
|
|
|
|
2024-02-20 16:02:02 +04:00
|
|
|
_onlySelected = current.onlySelected;
|
|
|
|
_repliesAllowed = current.repliesAllowed;
|
|
|
|
_allowed = current.allowed;
|
|
|
|
_disallowed = current.disallowed;
|
2024-02-20 14:33:46 +04:00
|
|
|
|
|
|
|
AddDividerTextWithLottie(content, {
|
|
|
|
.lottie = u"robot"_q,
|
|
|
|
.lottieSize = st::settingsCloudPasswordIconSize,
|
|
|
|
.lottieMargins = st::peerAppearanceIconPadding,
|
|
|
|
.showFinished = showFinishes(),
|
|
|
|
.about = tr::lng_chatbots_about(
|
|
|
|
lt_link,
|
|
|
|
tr::lng_chatbots_about_link(
|
|
|
|
) | Ui::Text::ToLink(u"internal:about_business_chatbots"_q),
|
|
|
|
Ui::Text::WithEntities),
|
|
|
|
.aboutMargins = st::peerAppearanceCoverLabelMargin,
|
|
|
|
});
|
|
|
|
|
|
|
|
const auto username = content->add(
|
|
|
|
object_ptr<Ui::InputField>(
|
|
|
|
content,
|
|
|
|
st::settingsChatbotsUsername,
|
2024-02-20 16:02:02 +04:00
|
|
|
tr::lng_chatbots_placeholder(),
|
|
|
|
(current.bot
|
|
|
|
? current.bot->session().createInternalLink(
|
|
|
|
current.bot->username())
|
|
|
|
: QString())),
|
2024-02-20 14:33:46 +04:00
|
|
|
st::settingsChatbotsUsernameMargins);
|
|
|
|
|
|
|
|
Ui::AddDividerText(
|
|
|
|
content,
|
|
|
|
tr::lng_chatbots_add_about(),
|
|
|
|
st::peerAppearanceDividerTextMargin);
|
|
|
|
Ui::AddSkip(content);
|
|
|
|
Ui::AddSubsectionTitle(content, tr::lng_chatbots_access_title());
|
|
|
|
|
|
|
|
const auto group = std::make_shared<Ui::RadiobuttonGroup>(
|
2024-02-20 16:02:02 +04:00
|
|
|
_onlySelected.current() ? kSelectedOnly : kAllExcept);
|
2024-02-20 14:33:46 +04:00
|
|
|
const auto everyone = content->add(
|
|
|
|
object_ptr<Ui::Radiobutton>(
|
|
|
|
content,
|
|
|
|
group,
|
|
|
|
kAllExcept,
|
|
|
|
tr::lng_chatbots_all_except(tr::now),
|
|
|
|
st::settingsChatbotsAccess),
|
|
|
|
st::settingsChatbotsAccessMargins);
|
|
|
|
const auto selected = content->add(
|
|
|
|
object_ptr<Ui::Radiobutton>(
|
|
|
|
content,
|
|
|
|
group,
|
|
|
|
kSelectedOnly,
|
|
|
|
tr::lng_chatbots_selected(tr::now),
|
|
|
|
st::settingsChatbotsAccess),
|
|
|
|
st::settingsChatbotsAccessMargins);
|
|
|
|
|
|
|
|
Ui::AddSkip(content, st::settingsChatbotsAccessSkip);
|
|
|
|
Ui::AddDivider(content);
|
|
|
|
|
|
|
|
const auto excludeWrap = content->add(
|
|
|
|
object_ptr<Ui::SlideWrap<Ui::VerticalLayout>>(
|
|
|
|
content,
|
|
|
|
object_ptr<Ui::VerticalLayout>(content))
|
|
|
|
)->setDuration(0);
|
|
|
|
const auto excludeInner = excludeWrap->entity();
|
|
|
|
|
|
|
|
Ui::AddSkip(excludeInner);
|
|
|
|
Ui::AddSubsectionTitle(excludeInner, tr::lng_chatbots_excluded_title());
|
|
|
|
const auto excludeAdd = AddButtonWithIcon(
|
|
|
|
excludeInner,
|
|
|
|
tr::lng_chatbots_exclude_button(),
|
|
|
|
st::settingsChatbotsAdd,
|
|
|
|
{ &st::settingsIconRemove, IconType::Round, &st::windowBgActive });
|
2024-02-20 16:02:02 +04:00
|
|
|
excludeAdd->setClickedCallback([=] {
|
|
|
|
EditBusinessExceptions(_controller, {
|
|
|
|
.current = _disallowed.current(),
|
|
|
|
.save = crl::guard(this, [=](Data::BusinessExceptions value) {
|
|
|
|
_disallowed = std::move(value);
|
|
|
|
}),
|
|
|
|
.allow = false,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
SetupBusinessExceptionsPreview(excludeInner, &_disallowed);
|
2024-02-20 14:33:46 +04:00
|
|
|
|
2024-02-20 16:02:02 +04:00
|
|
|
excludeWrap->toggleOn(_onlySelected.value() | rpl::map(!_1));
|
2024-02-20 14:33:46 +04:00
|
|
|
excludeWrap->finishAnimating();
|
|
|
|
|
|
|
|
const auto includeWrap = content->add(
|
|
|
|
object_ptr<Ui::SlideWrap<Ui::VerticalLayout>>(
|
|
|
|
content,
|
|
|
|
object_ptr<Ui::VerticalLayout>(content))
|
|
|
|
)->setDuration(0);
|
|
|
|
const auto includeInner = includeWrap->entity();
|
|
|
|
|
|
|
|
Ui::AddSkip(includeInner);
|
|
|
|
Ui::AddSubsectionTitle(includeInner, tr::lng_chatbots_included_title());
|
|
|
|
const auto includeAdd = AddButtonWithIcon(
|
|
|
|
includeInner,
|
|
|
|
tr::lng_chatbots_include_button(),
|
|
|
|
st::settingsChatbotsAdd,
|
|
|
|
{ &st::settingsIconAdd, IconType::Round, &st::windowBgActive });
|
2024-02-20 16:02:02 +04:00
|
|
|
includeAdd->setClickedCallback([=] {
|
|
|
|
EditBusinessExceptions(_controller, {
|
|
|
|
.current = _allowed.current(),
|
|
|
|
.save = crl::guard(this, [=](Data::BusinessExceptions value) {
|
|
|
|
_allowed = std::move(value);
|
|
|
|
}),
|
|
|
|
.allow = true,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
SetupBusinessExceptionsPreview(includeInner, &_allowed);
|
2024-02-20 14:33:46 +04:00
|
|
|
|
2024-02-20 16:02:02 +04:00
|
|
|
includeWrap->toggleOn(_onlySelected.value());
|
2024-02-20 14:33:46 +04:00
|
|
|
includeWrap->finishAnimating();
|
|
|
|
|
|
|
|
group->setChangedCallback([=](int value) {
|
2024-02-20 16:02:02 +04:00
|
|
|
_onlySelected = (value == kSelectedOnly);
|
2024-02-20 14:33:46 +04:00
|
|
|
});
|
|
|
|
|
|
|
|
Ui::AddSkip(content, st::settingsChatbotsAccessSkip);
|
|
|
|
Ui::AddDividerText(
|
|
|
|
content,
|
|
|
|
tr::lng_chatbots_exclude_about(),
|
|
|
|
st::peerAppearanceDividerTextMargin);
|
|
|
|
|
|
|
|
Ui::AddSkip(content);
|
|
|
|
Ui::AddSubsectionTitle(content, tr::lng_chatbots_permissions_title());
|
|
|
|
content->add(object_ptr<Ui::SettingsButton>(
|
|
|
|
content,
|
|
|
|
tr::lng_chatbots_reply(),
|
|
|
|
st::settingsButtonNoIcon
|
2024-02-20 16:02:02 +04:00
|
|
|
))->toggleOn(_repliesAllowed.value())->toggledChanges(
|
2024-02-20 14:33:46 +04:00
|
|
|
) | rpl::start_with_next([=](bool value) {
|
2024-02-20 16:02:02 +04:00
|
|
|
_repliesAllowed = value;
|
2024-02-20 14:33:46 +04:00
|
|
|
}, content->lifetime());
|
|
|
|
Ui::AddSkip(content);
|
|
|
|
|
|
|
|
Ui::AddDividerText(
|
|
|
|
content,
|
|
|
|
tr::lng_chatbots_reply_about(),
|
|
|
|
st::settingsChatbotsBottomTextMargin,
|
|
|
|
RectPart::Top);
|
|
|
|
|
|
|
|
Ui::ResizeFitChild(this, content);
|
|
|
|
}
|
|
|
|
|
2024-02-20 16:02:02 +04:00
|
|
|
void Chatbots::save() {
|
|
|
|
const auto settings = Data::ChatbotsSettings{
|
|
|
|
.bot = nullptr,
|
|
|
|
.allowed = _allowed.current(),
|
|
|
|
.disallowed = _disallowed.current(),
|
|
|
|
.repliesAllowed = _repliesAllowed.current(),
|
|
|
|
.onlySelected = _onlySelected.current(),
|
|
|
|
};
|
|
|
|
_session->data().chatbots().save(settings);
|
|
|
|
}
|
|
|
|
|
2024-02-20 14:33:46 +04:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
Type ChatbotsId() {
|
|
|
|
return Chatbots::Id();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Settings
|