Added support of non-self users to UsernamesBox.

This commit is contained in:
23rd 2023-04-09 23:25:18 +03:00 committed by John Preston
parent c53b26dec8
commit cfc8820bad
4 changed files with 45 additions and 25 deletions

View File

@ -10,6 +10,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "boxes/peers/edit_peer_usernames_list.h"
#include "base/timer.h"
#include "boxes/peers/edit_peer_common.h"
#include "data/data_channel.h"
#include "data/data_session.h"
#include "data/data_user.h"
#include "lang/lang_keys.h"
@ -45,7 +46,7 @@ namespace {
class UsernameEditor final : public Ui::RpWidget {
public:
UsernameEditor(not_null<Ui::RpWidget*>, not_null<Main::Session*> session);
UsernameEditor(not_null<Ui::RpWidget*>, not_null<PeerData*> peer);
void setInnerFocus();
[[nodiscard]] rpl::producer<> submitted() const;
@ -66,8 +67,11 @@ private:
void checkInfoChange();
[[nodiscard]] QString editableUsername() const;
QString getName() const;
const not_null<PeerData*> _peer;
const not_null<Main::Session*> _session;
const style::margins &_padding;
MTP::Sender _api;
@ -87,18 +91,19 @@ private:
UsernameEditor::UsernameEditor(
not_null<Ui::RpWidget*>,
not_null<Main::Session*> session)
: _session(session)
not_null<PeerData*> peer)
: _peer(peer)
, _session(&peer->session())
, _padding(st::usernamePadding)
, _api(&_session->mtp())
, _username(
this,
st::defaultInputField,
rpl::single(u"@username"_q),
session->user()->editableUsername(),
editableUsername(),
QString())
, _checkTimer([=] { check(); }) {
_goodText = _session->user()->editableUsername().isEmpty()
_goodText = editableUsername().isEmpty()
? QString()
: tr::lng_username_available(tr::now);
@ -148,6 +153,16 @@ rpl::producer<> UsernameEditor::save() {
return _saved.events();
}
QString UsernameEditor::editableUsername() const {
if (const auto user = _peer->asUser()) {
return user->editableUsername();
} else if (const auto channel = _peer->asChannel()) {
return channel->editableUsername();
} else {
return QString();
}
}
rpl::producer<UsernameCheckInfo> UsernameEditor::checkInfoChanged() const {
return _checkInfoChanged.events();
}
@ -166,7 +181,7 @@ void UsernameEditor::check() {
_checkRequestId = 0;
_errorText = (mtpIsTrue(result)
|| _checkUsername == _session->user()->editableUsername())
|| (_checkUsername == editableUsername()))
? QString()
: tr::lng_username_occupied(tr::now);
_goodText = _errorText.isEmpty()
@ -251,14 +266,15 @@ void UsernameEditor::checkInfoPurchaseAvailable() {
}
void UsernameEditor::updateFail(const QString &error) {
const auto self = _session->user();
if ((error == u"USERNAME_NOT_MODIFIED"_q)
|| (_sentUsername == self->editableUsername())) {
self->setName(
TextUtilities::SingleLine(self->firstName),
TextUtilities::SingleLine(self->lastName),
TextUtilities::SingleLine(self->nameOrPhone),
TextUtilities::SingleLine(_sentUsername));
|| (_sentUsername == editableUsername())) {
if (const auto user = _peer->asUser()) {
user->setName(
TextUtilities::SingleLine(user->firstName),
TextUtilities::SingleLine(user->lastName),
TextUtilities::SingleLine(user->nameOrPhone),
TextUtilities::SingleLine(_sentUsername));
}
_saved.fire_done();
} else if (error == u"USERNAME_INVALID"_q) {
_username->setFocus();
@ -283,7 +299,7 @@ void UsernameEditor::checkFail(const QString &error) {
_errorText = tr::lng_username_invalid(tr::now);
checkInfoChange();
} else if ((error == u"USERNAME_OCCUPIED"_q)
&& (_checkUsername != _session->user()->editableUsername())) {
&& (_checkUsername != editableUsername())) {
_errorText = tr::lng_username_occupied(tr::now);
checkInfoChange();
} else if (error == u"USERNAME_PURCHASE_AVAILABLE"_q) {
@ -302,20 +318,25 @@ QString UsernameEditor::getName() const {
void UsernamesBox(
not_null<Ui::GenericBox*> box,
not_null<Main::Session*> session) {
box->setTitle(tr::lng_username_title());
not_null<PeerData*> peer) {
const auto isBot = peer && peer->isUser() && peer->asUser()->isBot();
box->setTitle(isBot
? tr::lng_bot_username_title()
: tr::lng_username_title());
const auto container = box->verticalLayout();
const auto editor = box->addRow(
object_ptr<UsernameEditor>(box, session),
object_ptr<UsernameEditor>(box, peer),
{});
box->setFocusCallback([=] { editor->setInnerFocus(); });
AddUsernameCheckLabel(container, editor->checkInfoChanged());
auto description = rpl::combine(
tr::lng_username_description1(Ui::Text::RichLangValue),
(isBot
? tr::lng_bot_username_description1
: tr::lng_username_description1)(Ui::Text::RichLangValue),
tr::lng_username_description2(Ui::Text::RichLangValue)
) | rpl::map([](TextWithEntities d1, TextWithEntities d2) {
return d1.append("\n\n").append(std::move(d2));
@ -331,7 +352,7 @@ void UsernamesBox(
const auto list = box->addRow(
object_ptr<UsernamesList>(
box,
session->user(),
peer,
std::make_shared<Ui::BoxShow>(box),
[=] {
box->scrollToY(0);

View File

@ -12,13 +12,11 @@ class GenericBox;
class VerticalLayout;
} // namespace Ui
namespace Main {
class Session;
} // namespace Main
class PeerData;
void UsernamesBox(
not_null<Ui::GenericBox*> box,
not_null<Main::Session*> session);
not_null<PeerData*> peer);
struct UsernameCheckInfo final {
enum class Type {

View File

@ -419,7 +419,8 @@ void SetupRows(
std::move(value),
tr::lng_context_copy_mention(tr::now),
[=] {
const auto box = controller->show(Box(UsernamesBox, session));
const auto box = controller->show(
Box(UsernamesBox, session->user()));
box->boxClosing(
) | rpl::start_with_next([=] {
session->api().usernames().requestToCache(session->user());

View File

@ -209,7 +209,7 @@ void Cover::initViewers() {
_username->overrideLinkClickHandler([=] {
const auto username = _user->userName();
if (username.isEmpty()) {
_controller->show(Box(UsernamesBox, &_user->session()));
_controller->show(Box(UsernamesBox, _user));
} else {
QGuiApplication::clipboard()->setText(
_user->session().createInternalLinkFull(username));