tdesktop/Telegram/SourceFiles/settings/business/settings_chatbots.cpp

215 lines
5.5 KiB
C++
Raw Normal View History

2024-02-20 10:33:46 +00: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-23 17:23:15 +00:00
2024-02-20 12:02:02 +00: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 10:33:46 +00:00
#include "lang/lang_keys.h"
2024-02-20 12:02:02 +00:00
#include "main/main_session.h"
#include "settings/business/settings_recipients_helper.h"
2024-02-20 10:33:46 +00:00
#include "ui/text/text_utilities.h"
#include "ui/widgets/fields/input_field.h"
#include "ui/widgets/buttons.h"
2024-02-20 10:33:46 +00:00
#include "ui/wrap/slide_wrap.h"
#include "ui/wrap/vertical_layout.h"
#include "ui/vertical_list.h"
2024-02-20 12:02:02 +00:00
#include "window/window_session_controller.h"
2024-02-20 10:33:46 +00:00
#include "styles/style_layers.h"
#include "styles/style_settings.h"
namespace Settings {
namespace {
enum class LookupState {
Empty,
Loading,
Ready,
};
struct BotState {
UserData *bot = nullptr;
LookupState state = LookupState::Empty;
};
2024-02-20 10:33:46 +00:00
class Chatbots : public BusinessSection<Chatbots> {
2024-02-20 10:33:46 +00:00
public:
Chatbots(
QWidget *parent,
not_null<Window::SessionController*> controller);
2024-02-20 12:02:02 +00:00
~Chatbots();
2024-02-20 10:33:46 +00:00
[[nodiscard]] rpl::producer<QString> title() override;
2024-03-01 18:18:01 +00:00
const Ui::RoundRect *bottomSkipRounding() const override {
2024-02-20 10:33:46 +00:00
return &_bottomSkipRounding;
}
private:
void setupContent(not_null<Window::SessionController*> controller);
2024-02-20 12:02:02 +00:00
void save();
2024-02-20 10:33:46 +00:00
Ui::RoundRect _bottomSkipRounding;
rpl::variable<Data::BusinessRecipients> _recipients;
rpl::variable<QString> _usernameValue;
2024-03-01 18:18:01 +00:00
rpl::variable<BotState> _botValue;
2024-02-20 12:02:02 +00:00
rpl::variable<bool> _repliesAllowed = true;
2024-02-20 10:33:46 +00:00
};
[[nodiscard]] rpl::producer<QString> DebouncedValue(
not_null<Ui::InputField*> field) {
return rpl::single(field->getLastText());
}
[[nodiscard]] rpl::producer<BotState> LookupBot(
not_null<Main::Session*> session,
rpl::producer<QString> usernameChanges) {
return rpl::never<BotState>();
}
[[nodiscard]] object_ptr<Ui::RpWidget> MakeBotPreview(
not_null<QWidget*> parent,
rpl::producer<BotState> state,
Fn<void()> resetBot) {
return object_ptr<Ui::RpWidget>(parent.get());
}
2024-02-20 10:33:46 +00:00
Chatbots::Chatbots(
QWidget *parent,
not_null<Window::SessionController*> controller)
: BusinessSection(parent, controller)
2024-02-20 10:33:46 +00:00
, _bottomSkipRounding(st::boxRadius, st::boxDividerBg) {
setupContent(controller);
}
2024-02-20 12:02:02 +00:00
Chatbots::~Chatbots() {
if (!Core::Quitting()) {
save();
}
}
2024-02-20 10:33:46 +00: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 12:02:02 +00:00
const auto current = controller->session().data().chatbots().current();
2024-02-20 10:33:46 +00:00
_recipients = current.recipients;
2024-02-20 12:02:02 +00:00
_repliesAllowed = current.repliesAllowed;
2024-02-20 10:33:46 +00: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 12:02:02 +00:00
tr::lng_chatbots_placeholder(),
(current.bot
? current.bot->session().createInternalLink(
current.bot->username())
: QString())),
2024-02-20 10:33:46 +00:00
st::settingsChatbotsUsernameMargins);
_usernameValue = DebouncedValue(username);
_botValue = rpl::single(BotState{
current.bot,
current.bot ? LookupState::Ready : LookupState::Empty
}) | rpl::then(
LookupBot(&controller->session(), _usernameValue.changes())
);
const auto resetBot = [=] {
username->setText(QString());
username->setFocus();
};
content->add(object_ptr<Ui::SlideWrap<Ui::RpWidget>>(
content,
MakeBotPreview(content, _botValue.value(), resetBot)));
2024-02-20 10:33:46 +00:00
Ui::AddDividerText(
content,
tr::lng_chatbots_add_about(),
st::peerAppearanceDividerTextMargin);
AddBusinessRecipientsSelector(content, {
.controller = controller,
.title = tr::lng_chatbots_access_title(),
.data = &_recipients,
2024-02-20 10:33:46 +00: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 12:02:02 +00:00
))->toggleOn(_repliesAllowed.value())->toggledChanges(
2024-02-20 10:33:46 +00:00
) | rpl::start_with_next([=](bool value) {
2024-02-20 12:02:02 +00:00
_repliesAllowed = value;
2024-02-20 10:33:46 +00: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 12:02:02 +00:00
void Chatbots::save() {
2024-03-01 13:28:51 +00:00
const auto show = controller()->uiShow();
const auto session = &controller()->session();
const auto fail = [=](QString error) {
if (error == u"BUSINESS_RECIPIENTS_EMPTY"_q) {
AssertIsDebug();
show->showToast(u"Please choose at least one recipient."_q);
//tr::lng_greeting_recipients_empty(tr::now));
}
};
controller()->session().data().chatbots().save({
.bot = _botValue.current().bot,
.recipients = _recipients.current(),
2024-02-20 12:02:02 +00:00
.repliesAllowed = _repliesAllowed.current(),
2024-03-01 13:28:51 +00:00
}, [=](QString error) { show->showToast(error); });
2024-02-20 12:02:02 +00:00
}
2024-02-20 10:33:46 +00:00
} // namespace
Type ChatbotsId() {
return Chatbots::Id();
}
} // namespace Settings