Rename / move settings_blocked_box_controller.
Next commit fixes the build.
This commit is contained in:
parent
18151359f3
commit
a563cf553c
|
@ -18,7 +18,7 @@ to link the code of portions of this program with the OpenSSL library.
|
|||
Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE
|
||||
Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
|
||||
*/
|
||||
#include "settings/settings_blocked_box_controller.h"
|
||||
#include "settings/settings_privacy_controllers.h"
|
||||
|
||||
#include "lang.h"
|
||||
#include "apiwrap.h"
|
||||
|
@ -30,138 +30,35 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
|
|||
namespace Settings {
|
||||
namespace {
|
||||
|
||||
constexpr auto kPerPage = 40;
|
||||
constexpr auto kBlockedPerPage = 40;
|
||||
|
||||
} // namespace
|
||||
class BlockUserBoxController : public PeerListBox::Controller, private base::Subscriber {
|
||||
public:
|
||||
void prepare() override;
|
||||
void rowClicked(PeerData *peer) override;
|
||||
std::unique_ptr<PeerListBox::Row> createGlobalRow(PeerData *peer) override;
|
||||
|
||||
void BlockedBoxController::prepare() {
|
||||
view()->setTitle(lang(lng_blocked_list_title));
|
||||
view()->addButton(lang(lng_close), [this] { view()->closeBox(); });
|
||||
view()->addLeftButton(lang(lng_blocked_list_add), [this] { blockUser(); });
|
||||
view()->setAboutText(lang(lng_contacts_loading));
|
||||
view()->refreshRows();
|
||||
private:
|
||||
void rebuildRows();
|
||||
void checkForEmptyRows();
|
||||
void updateIsBlocked(PeerListBox::Row *row, UserData *user) const;
|
||||
bool appendRow(History *history);
|
||||
|
||||
subscribe(Notify::PeerUpdated(), Notify::PeerUpdatedHandler(Notify::PeerUpdate::Flag::UserIsBlocked, [this](const Notify::PeerUpdate &update) {
|
||||
if (auto user = update.peer->asUser()) {
|
||||
handleBlockedEvent(user);
|
||||
class Row : public PeerListBox::Row {
|
||||
public:
|
||||
Row(History *history) : PeerListBox::Row(history->peer), _history(history) {
|
||||
}
|
||||
}));
|
||||
|
||||
preloadRows();
|
||||
}
|
||||
|
||||
void BlockedBoxController::preloadRows() {
|
||||
if (_loadRequestId || _allLoaded) {
|
||||
return;
|
||||
}
|
||||
|
||||
_loadRequestId = MTP::send(MTPcontacts_GetBlocked(MTP_int(_offset), MTP_int(kPerPage)), rpcDone(base::lambda_guarded(this, [this](const MTPcontacts_Blocked &result) {
|
||||
_loadRequestId = 0;
|
||||
|
||||
if (!_offset) {
|
||||
view()->setAboutText(lang(lng_blocked_list_about));
|
||||
History *history() const {
|
||||
return _history;
|
||||
}
|
||||
|
||||
auto handleContactsBlocked = [](auto &list) {
|
||||
App::feedUsers(list.vusers);
|
||||
return list.vblocked.v;
|
||||
};
|
||||
switch (result.type()) {
|
||||
case mtpc_contacts_blockedSlice: {
|
||||
receivedUsers(handleContactsBlocked(result.c_contacts_blockedSlice()));
|
||||
} break;
|
||||
case mtpc_contacts_blocked: {
|
||||
_allLoaded = true;
|
||||
receivedUsers(handleContactsBlocked(result.c_contacts_blocked()));
|
||||
} break;
|
||||
default: t_assert(!"Bad type() in MTPcontacts_GetBlocked() result.");
|
||||
}
|
||||
})), rpcFail(base::lambda_guarded(this, [this](const RPCError &error) {
|
||||
if (MTP::isDefaultHandledError(error)) {
|
||||
return false;
|
||||
}
|
||||
_loadRequestId = 0;
|
||||
return true;
|
||||
})));
|
||||
}
|
||||
private:
|
||||
History *_history = nullptr;
|
||||
|
||||
void BlockedBoxController::rowClicked(PeerData *peer) {
|
||||
Ui::showPeerHistoryAsync(peer->id, ShowAtUnreadMsgId);
|
||||
}
|
||||
|
||||
void BlockedBoxController::rowActionClicked(PeerData *peer) {
|
||||
auto user = peer->asUser();
|
||||
t_assert(user != nullptr);
|
||||
|
||||
App::api()->unblockUser(user);
|
||||
}
|
||||
|
||||
void BlockedBoxController::receivedUsers(const QVector<MTPContactBlocked> &result) {
|
||||
if (result.empty()) {
|
||||
_allLoaded = true;
|
||||
}
|
||||
|
||||
for_const (auto &item, result) {
|
||||
++_offset;
|
||||
if (item.type() != mtpc_contactBlocked) {
|
||||
continue;
|
||||
}
|
||||
auto &contactBlocked = item.c_contactBlocked();
|
||||
auto userId = contactBlocked.vuser_id.v;
|
||||
if (auto user = App::userLoaded(userId)) {
|
||||
appendRow(user);
|
||||
user->setBlockStatus(UserData::BlockStatus::Blocked);
|
||||
}
|
||||
}
|
||||
view()->refreshRows();
|
||||
}
|
||||
|
||||
void BlockedBoxController::handleBlockedEvent(UserData *user) {
|
||||
if (user->isBlocked()) {
|
||||
if (prependRow(user)) {
|
||||
view()->refreshRows();
|
||||
view()->onScrollToY(0);
|
||||
}
|
||||
} else if (auto row = view()->findRow(user)) {
|
||||
view()->removeRow(row);
|
||||
view()->refreshRows();
|
||||
}
|
||||
}
|
||||
|
||||
void BlockedBoxController::blockUser() {
|
||||
Ui::show(Box<PeerListBox>(std::make_unique<BlockUserBoxController>()), KeepOtherLayers);
|
||||
}
|
||||
|
||||
bool BlockedBoxController::appendRow(UserData *user) {
|
||||
if (view()->findRow(user)) {
|
||||
return false;
|
||||
}
|
||||
view()->appendRow(createRow(user));
|
||||
return true;
|
||||
}
|
||||
|
||||
bool BlockedBoxController::prependRow(UserData *user) {
|
||||
if (view()->findRow(user)) {
|
||||
return false;
|
||||
}
|
||||
view()->prependRow(createRow(user));
|
||||
return true;
|
||||
}
|
||||
|
||||
std::unique_ptr<PeerListBox::Row> BlockedBoxController::createRow(UserData *user) const {
|
||||
auto row = std::make_unique<PeerListBox::Row>(user);
|
||||
row->setActionLink(lang(lng_blocked_list_unblock));
|
||||
auto status = [user]() -> QString {
|
||||
if (user->botInfo) {
|
||||
return lang(lng_status_bot);
|
||||
} else if (user->phone().isEmpty()) {
|
||||
return lang(lng_blocked_list_unknown_phone);
|
||||
}
|
||||
return App::formatPhone(user->phone());
|
||||
};
|
||||
row->setCustomStatus(status());
|
||||
return row;
|
||||
}
|
||||
std::unique_ptr<Row> createRow(History *history) const;
|
||||
|
||||
};
|
||||
|
||||
void BlockUserBoxController::prepare() {
|
||||
view()->setTitle(lang(lng_blocked_list_add_title));
|
||||
|
@ -271,4 +168,135 @@ std::unique_ptr<BlockUserBoxController::Row> BlockUserBoxController::createRow(H
|
|||
return row;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void BlockedBoxController::prepare() {
|
||||
view()->setTitle(lang(lng_blocked_list_title));
|
||||
view()->addButton(lang(lng_close), [this] { view()->closeBox(); });
|
||||
view()->addLeftButton(lang(lng_blocked_list_add), [this] { blockUser(); });
|
||||
view()->setAboutText(lang(lng_contacts_loading));
|
||||
view()->refreshRows();
|
||||
|
||||
subscribe(Notify::PeerUpdated(), Notify::PeerUpdatedHandler(Notify::PeerUpdate::Flag::UserIsBlocked, [this](const Notify::PeerUpdate &update) {
|
||||
if (auto user = update.peer->asUser()) {
|
||||
handleBlockedEvent(user);
|
||||
}
|
||||
}));
|
||||
|
||||
preloadRows();
|
||||
}
|
||||
|
||||
void BlockedBoxController::preloadRows() {
|
||||
if (_loadRequestId || _allLoaded) {
|
||||
return;
|
||||
}
|
||||
|
||||
_loadRequestId = MTP::send(MTPcontacts_GetBlocked(MTP_int(_offset), MTP_int(kBlockedPerPage)), rpcDone(base::lambda_guarded(this, [this](const MTPcontacts_Blocked &result) {
|
||||
_loadRequestId = 0;
|
||||
|
||||
if (!_offset) {
|
||||
view()->setAboutText(lang(lng_blocked_list_about));
|
||||
}
|
||||
|
||||
auto handleContactsBlocked = [](auto &list) {
|
||||
App::feedUsers(list.vusers);
|
||||
return list.vblocked.v;
|
||||
};
|
||||
switch (result.type()) {
|
||||
case mtpc_contacts_blockedSlice: {
|
||||
receivedUsers(handleContactsBlocked(result.c_contacts_blockedSlice()));
|
||||
} break;
|
||||
case mtpc_contacts_blocked: {
|
||||
_allLoaded = true;
|
||||
receivedUsers(handleContactsBlocked(result.c_contacts_blocked()));
|
||||
} break;
|
||||
default: t_assert(!"Bad type() in MTPcontacts_GetBlocked() result.");
|
||||
}
|
||||
})), rpcFail(base::lambda_guarded(this, [this](const RPCError &error) {
|
||||
if (MTP::isDefaultHandledError(error)) {
|
||||
return false;
|
||||
}
|
||||
_loadRequestId = 0;
|
||||
return true;
|
||||
})));
|
||||
}
|
||||
|
||||
void BlockedBoxController::rowClicked(PeerData *peer) {
|
||||
Ui::showPeerHistoryAsync(peer->id, ShowAtUnreadMsgId);
|
||||
}
|
||||
|
||||
void BlockedBoxController::rowActionClicked(PeerData *peer) {
|
||||
auto user = peer->asUser();
|
||||
t_assert(user != nullptr);
|
||||
|
||||
App::api()->unblockUser(user);
|
||||
}
|
||||
|
||||
void BlockedBoxController::receivedUsers(const QVector<MTPContactBlocked> &result) {
|
||||
if (result.empty()) {
|
||||
_allLoaded = true;
|
||||
}
|
||||
|
||||
for_const (auto &item, result) {
|
||||
++_offset;
|
||||
if (item.type() != mtpc_contactBlocked) {
|
||||
continue;
|
||||
}
|
||||
auto &contactBlocked = item.c_contactBlocked();
|
||||
auto userId = contactBlocked.vuser_id.v;
|
||||
if (auto user = App::userLoaded(userId)) {
|
||||
appendRow(user);
|
||||
user->setBlockStatus(UserData::BlockStatus::Blocked);
|
||||
}
|
||||
}
|
||||
view()->refreshRows();
|
||||
}
|
||||
|
||||
void BlockedBoxController::handleBlockedEvent(UserData *user) {
|
||||
if (user->isBlocked()) {
|
||||
if (prependRow(user)) {
|
||||
view()->refreshRows();
|
||||
view()->onScrollToY(0);
|
||||
}
|
||||
} else if (auto row = view()->findRow(user)) {
|
||||
view()->removeRow(row);
|
||||
view()->refreshRows();
|
||||
}
|
||||
}
|
||||
|
||||
void BlockedBoxController::blockUser() {
|
||||
Ui::show(Box<PeerListBox>(std::make_unique<BlockUserBoxController>()), KeepOtherLayers);
|
||||
}
|
||||
|
||||
bool BlockedBoxController::appendRow(UserData *user) {
|
||||
if (view()->findRow(user)) {
|
||||
return false;
|
||||
}
|
||||
view()->appendRow(createRow(user));
|
||||
return true;
|
||||
}
|
||||
|
||||
bool BlockedBoxController::prependRow(UserData *user) {
|
||||
if (view()->findRow(user)) {
|
||||
return false;
|
||||
}
|
||||
view()->prependRow(createRow(user));
|
||||
return true;
|
||||
}
|
||||
|
||||
std::unique_ptr<PeerListBox::Row> BlockedBoxController::createRow(UserData *user) const {
|
||||
auto row = std::make_unique<PeerListBox::Row>(user);
|
||||
row->setActionLink(lang(lng_blocked_list_unblock));
|
||||
auto status = [user]() -> QString {
|
||||
if (user->botInfo) {
|
||||
return lang(lng_status_bot);
|
||||
} else if (user->phone().isEmpty()) {
|
||||
return lang(lng_blocked_list_unknown_phone);
|
||||
}
|
||||
return App::formatPhone(user->phone());
|
||||
};
|
||||
row->setCustomStatus(status());
|
||||
return row;
|
||||
}
|
||||
|
||||
} // namespace Settings
|
|
@ -21,6 +21,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
|
|||
#pragma once
|
||||
|
||||
#include "boxes/peer_list_box.h"
|
||||
#include "boxes/edit_privacy_box.h"
|
||||
|
||||
namespace Settings {
|
||||
|
||||
|
@ -46,32 +47,4 @@ private:
|
|||
|
||||
};
|
||||
|
||||
class BlockUserBoxController : public QObject, public PeerListBox::Controller, private base::Subscriber {
|
||||
public:
|
||||
void prepare() override;
|
||||
void rowClicked(PeerData *peer) override;
|
||||
std::unique_ptr<PeerListBox::Row> createGlobalRow(PeerData *peer) override;
|
||||
|
||||
private:
|
||||
void rebuildRows();
|
||||
void checkForEmptyRows();
|
||||
void updateIsBlocked(PeerListBox::Row *row, UserData *user) const;
|
||||
bool appendRow(History *history);
|
||||
|
||||
class Row : public PeerListBox::Row {
|
||||
public:
|
||||
Row(History *history) : PeerListBox::Row(history->peer), _history(history) {
|
||||
}
|
||||
History *history() const {
|
||||
return _history;
|
||||
}
|
||||
|
||||
private:
|
||||
History *_history = nullptr;
|
||||
|
||||
};
|
||||
std::unique_ptr<Row> createRow(History *history) const;
|
||||
|
||||
};
|
||||
|
||||
} // namespace Settings
|
Loading…
Reference in New Issue