2017-03-13 15:40:21 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2017-03-13 15:40:21 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2017-03-13 15:40:21 +00:00
|
|
|
*/
|
2018-09-02 14:23:52 +00:00
|
|
|
#include "old_settings/settings_privacy_controllers.h"
|
2017-03-13 15:40:21 +00:00
|
|
|
|
2017-04-13 08:27:10 +00:00
|
|
|
#include "lang/lang_keys.h"
|
2017-03-13 15:40:21 +00:00
|
|
|
#include "apiwrap.h"
|
|
|
|
#include "observer_peer.h"
|
2017-03-14 14:16:03 +00:00
|
|
|
#include "mainwidget.h"
|
2017-03-17 18:44:55 +00:00
|
|
|
#include "auth_session.h"
|
|
|
|
#include "storage/localstorage.h"
|
2018-01-13 12:45:11 +00:00
|
|
|
#include "history/history.h"
|
2017-08-14 12:53:49 +00:00
|
|
|
#include "boxes/peer_list_controllers.h"
|
2017-04-06 14:38:10 +00:00
|
|
|
#include "boxes/confirm_box.h"
|
2017-03-13 15:40:21 +00:00
|
|
|
|
2018-09-02 14:23:52 +00:00
|
|
|
namespace OldSettings {
|
2017-03-13 15:40:21 +00:00
|
|
|
namespace {
|
|
|
|
|
2017-03-16 15:14:01 +00:00
|
|
|
constexpr auto kBlockedPerPage = 40;
|
|
|
|
|
2017-03-17 12:05:50 +00:00
|
|
|
class BlockUserBoxController : public ChatsListBoxController {
|
2017-03-16 15:14:01 +00:00
|
|
|
public:
|
2017-08-17 08:31:24 +00:00
|
|
|
void rowClicked(not_null<PeerListRow*> row) override;
|
2017-06-13 21:08:47 +00:00
|
|
|
|
2018-06-04 15:35:11 +00:00
|
|
|
void setBlockUserCallback(Fn<void(not_null<UserData*> user)> callback) {
|
2017-06-13 21:08:47 +00:00
|
|
|
_blockUserCallback = std::move(callback);
|
|
|
|
}
|
2017-03-17 12:05:50 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
void prepareViewHook() override;
|
2017-08-17 08:31:24 +00:00
|
|
|
std::unique_ptr<Row> createRow(not_null<History*> history) override;
|
|
|
|
void updateRowHook(not_null<Row*> row) override {
|
2017-03-17 12:05:50 +00:00
|
|
|
updateIsBlocked(row, row->peer()->asUser());
|
2017-06-13 21:08:47 +00:00
|
|
|
delegate()->peerListUpdateRow(row);
|
2017-03-17 12:05:50 +00:00
|
|
|
}
|
2017-03-16 15:14:01 +00:00
|
|
|
|
|
|
|
private:
|
2017-08-17 08:31:24 +00:00
|
|
|
void updateIsBlocked(not_null<PeerListRow*> row, UserData *user) const;
|
2017-06-13 21:08:47 +00:00
|
|
|
|
2018-06-04 15:35:11 +00:00
|
|
|
Fn<void(not_null<UserData*> user)> _blockUserCallback;
|
2017-03-16 15:14:01 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2017-03-17 12:05:50 +00:00
|
|
|
void BlockUserBoxController::prepareViewHook() {
|
2017-06-13 21:08:47 +00:00
|
|
|
delegate()->peerListSetTitle(langFactory(lng_blocked_list_add_title));
|
2017-03-16 15:14:01 +00:00
|
|
|
subscribe(Notify::PeerUpdated(), Notify::PeerUpdatedHandler(Notify::PeerUpdate::Flag::UserIsBlocked, [this](const Notify::PeerUpdate &update) {
|
|
|
|
if (auto user = update.peer->asUser()) {
|
2017-06-13 21:08:47 +00:00
|
|
|
if (auto row = delegate()->peerListFindRow(user->id)) {
|
2017-03-16 15:14:01 +00:00
|
|
|
updateIsBlocked(row, user);
|
2017-06-13 21:08:47 +00:00
|
|
|
delegate()->peerListUpdateRow(row);
|
2017-03-16 15:14:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2017-08-17 08:31:24 +00:00
|
|
|
void BlockUserBoxController::updateIsBlocked(not_null<PeerListRow*> row, UserData *user) const {
|
2017-03-16 15:14:01 +00:00
|
|
|
auto blocked = user->isBlocked();
|
2017-08-14 12:53:49 +00:00
|
|
|
row->setDisabledState(blocked ? PeerListRow::State::DisabledChecked : PeerListRow::State::Active);
|
2017-03-16 15:14:01 +00:00
|
|
|
if (blocked) {
|
|
|
|
row->setCustomStatus(lang(lng_blocked_list_already_blocked));
|
|
|
|
} else {
|
|
|
|
row->clearCustomStatus();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-17 08:31:24 +00:00
|
|
|
void BlockUserBoxController::rowClicked(not_null<PeerListRow*> row) {
|
2017-06-13 21:08:47 +00:00
|
|
|
_blockUserCallback(row->peer()->asUser());
|
2017-03-16 15:14:01 +00:00
|
|
|
}
|
|
|
|
|
2017-08-17 08:31:24 +00:00
|
|
|
std::unique_ptr<BlockUserBoxController::Row> BlockUserBoxController::createRow(not_null<History*> history) {
|
2017-08-13 16:14:00 +00:00
|
|
|
if (history->peer->isSelf()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2017-03-17 12:05:50 +00:00
|
|
|
if (auto user = history->peer->asUser()) {
|
|
|
|
auto row = std::make_unique<Row>(history);
|
|
|
|
updateIsBlocked(row.get(), user);
|
|
|
|
return row;
|
2017-03-16 15:14:01 +00:00
|
|
|
}
|
2017-08-13 16:14:00 +00:00
|
|
|
return nullptr;
|
2017-03-16 15:14:01 +00:00
|
|
|
}
|
|
|
|
|
2017-03-13 15:40:21 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
void BlockedBoxController::prepare() {
|
2017-06-13 21:08:47 +00:00
|
|
|
delegate()->peerListSetTitle(langFactory(lng_blocked_list_title));
|
|
|
|
setDescriptionText(lang(lng_contacts_loading));
|
|
|
|
delegate()->peerListRefreshRows();
|
2017-03-13 15:40:21 +00:00
|
|
|
|
|
|
|
subscribe(Notify::PeerUpdated(), Notify::PeerUpdatedHandler(Notify::PeerUpdate::Flag::UserIsBlocked, [this](const Notify::PeerUpdate &update) {
|
2017-03-14 14:16:03 +00:00
|
|
|
if (auto user = update.peer->asUser()) {
|
|
|
|
handleBlockedEvent(user);
|
2017-03-13 15:40:21 +00:00
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
2017-06-14 10:46:06 +00:00
|
|
|
loadMoreRows();
|
2017-03-13 15:40:21 +00:00
|
|
|
}
|
|
|
|
|
2017-06-14 10:46:06 +00:00
|
|
|
void BlockedBoxController::loadMoreRows() {
|
2017-03-13 15:40:21 +00:00
|
|
|
if (_loadRequestId || _allLoaded) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-22 15:38:40 +00:00
|
|
|
_loadRequestId = request(MTPcontacts_GetBlocked(MTP_int(_offset), MTP_int(kBlockedPerPage))).done([this](const MTPcontacts_Blocked &result) {
|
2017-03-13 15:40:21 +00:00
|
|
|
_loadRequestId = 0;
|
|
|
|
|
|
|
|
if (!_offset) {
|
2017-06-13 21:08:47 +00:00
|
|
|
setDescriptionText(lang(lng_blocked_list_about));
|
2017-03-13 15:40:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2017-03-17 12:05:50 +00:00
|
|
|
default: Unexpected("Bad type() in MTPcontacts_GetBlocked() result.");
|
2017-03-13 15:40:21 +00:00
|
|
|
}
|
2017-03-22 15:38:40 +00:00
|
|
|
}).fail([this](const RPCError &error) {
|
2017-03-13 15:40:21 +00:00
|
|
|
_loadRequestId = 0;
|
2017-03-22 15:38:40 +00:00
|
|
|
}).send();
|
2017-03-13 15:40:21 +00:00
|
|
|
}
|
|
|
|
|
2017-08-17 08:31:24 +00:00
|
|
|
void BlockedBoxController::rowClicked(not_null<PeerListRow*> row) {
|
2017-10-03 13:05:58 +00:00
|
|
|
InvokeQueued(App::main(), [peerId = row->peer()->id] {
|
|
|
|
Ui::showPeerHistory(peerId, ShowAtUnreadMsgId);
|
|
|
|
});
|
2017-03-13 15:40:21 +00:00
|
|
|
}
|
|
|
|
|
2017-08-17 08:31:24 +00:00
|
|
|
void BlockedBoxController::rowActionClicked(not_null<PeerListRow*> row) {
|
2017-03-17 12:05:50 +00:00
|
|
|
auto user = row->peer()->asUser();
|
|
|
|
Expects(user != nullptr);
|
2017-03-13 15:40:21 +00:00
|
|
|
|
2017-08-04 14:54:32 +00:00
|
|
|
Auth().api().unblockUser(user);
|
2017-03-13 15:40:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2017-06-13 21:08:47 +00:00
|
|
|
delegate()->peerListRefreshRows();
|
2017-03-13 15:40:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BlockedBoxController::handleBlockedEvent(UserData *user) {
|
|
|
|
if (user->isBlocked()) {
|
|
|
|
if (prependRow(user)) {
|
2017-06-13 21:08:47 +00:00
|
|
|
delegate()->peerListRefreshRows();
|
|
|
|
delegate()->peerListScrollToTop();
|
2017-03-13 15:40:21 +00:00
|
|
|
}
|
2017-06-13 21:08:47 +00:00
|
|
|
} else if (auto row = delegate()->peerListFindRow(user->id)) {
|
|
|
|
delegate()->peerListRemoveRow(row);
|
|
|
|
delegate()->peerListRefreshRows();
|
2017-03-13 15:40:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-13 21:08:47 +00:00
|
|
|
void BlockedBoxController::BlockNewUser() {
|
|
|
|
auto controller = std::make_unique<BlockUserBoxController>();
|
2017-08-17 08:31:24 +00:00
|
|
|
auto initBox = [controller = controller.get()](not_null<PeerListBox*> box) {
|
|
|
|
controller->setBlockUserCallback([box](not_null<UserData*> user) {
|
2017-08-04 14:54:32 +00:00
|
|
|
Auth().api().blockUser(user);
|
2017-06-13 21:08:47 +00:00
|
|
|
box->closeBox();
|
|
|
|
});
|
|
|
|
box->addButton(langFactory(lng_cancel), [box] { box->closeBox(); });
|
|
|
|
};
|
2017-09-15 17:34:41 +00:00
|
|
|
Ui::show(
|
|
|
|
Box<PeerListBox>(std::move(controller), std::move(initBox)),
|
|
|
|
LayerOption::KeepOther);
|
2017-03-14 14:16:03 +00:00
|
|
|
}
|
|
|
|
|
2017-03-13 15:40:21 +00:00
|
|
|
bool BlockedBoxController::appendRow(UserData *user) {
|
2017-06-13 21:08:47 +00:00
|
|
|
if (delegate()->peerListFindRow(user->id)) {
|
2017-03-13 15:40:21 +00:00
|
|
|
return false;
|
|
|
|
}
|
2017-06-13 21:08:47 +00:00
|
|
|
delegate()->peerListAppendRow(createRow(user));
|
2017-03-13 15:40:21 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BlockedBoxController::prependRow(UserData *user) {
|
2017-06-13 21:08:47 +00:00
|
|
|
if (delegate()->peerListFindRow(user->id)) {
|
2017-03-13 15:40:21 +00:00
|
|
|
return false;
|
|
|
|
}
|
2017-06-13 21:08:47 +00:00
|
|
|
delegate()->peerListPrependRow(createRow(user));
|
2017-03-13 15:40:21 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-06-13 21:08:47 +00:00
|
|
|
std::unique_ptr<PeerListRow> BlockedBoxController::createRow(UserData *user) const {
|
2017-04-27 19:04:45 +00:00
|
|
|
auto row = std::make_unique<PeerListRowWithLink>(user);
|
2017-03-13 15:40:21 +00:00
|
|
|
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());
|
2017-04-27 19:04:45 +00:00
|
|
|
return std::move(row);
|
2017-03-13 15:40:21 +00:00
|
|
|
}
|
|
|
|
|
2018-09-11 09:11:52 +00:00
|
|
|
ApiWrap::Privacy::Key LastSeenPrivacyController::key() {
|
|
|
|
return Key::LastSeen;
|
|
|
|
}
|
|
|
|
|
|
|
|
MTPInputPrivacyKey LastSeenPrivacyController::apiKey() {
|
2017-03-16 15:15:07 +00:00
|
|
|
return MTP_inputPrivacyKeyStatusTimestamp();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString LastSeenPrivacyController::title() {
|
|
|
|
return lang(lng_edit_privacy_lastseen_title);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString LastSeenPrivacyController::description() {
|
|
|
|
return lang(lng_edit_privacy_lastseen_description);
|
|
|
|
}
|
|
|
|
|
2017-03-19 08:29:05 +00:00
|
|
|
QString LastSeenPrivacyController::warning() {
|
|
|
|
return lang(lng_edit_privacy_lastseen_warning);
|
|
|
|
}
|
|
|
|
|
2017-03-17 12:05:50 +00:00
|
|
|
QString LastSeenPrivacyController::exceptionLinkText(Exception exception, int count) {
|
|
|
|
switch (exception) {
|
2017-06-02 11:46:02 +00:00
|
|
|
case Exception::Always: return (count > 0) ? lng_edit_privacy_lastseen_always(lt_count, count) : lang(lng_edit_privacy_lastseen_always_empty);
|
|
|
|
case Exception::Never: return (count > 0) ? lng_edit_privacy_lastseen_never(lt_count, count) : lang(lng_edit_privacy_lastseen_never_empty);
|
2017-03-17 12:05:50 +00:00
|
|
|
}
|
|
|
|
Unexpected("Invalid exception value.");
|
2017-03-16 15:15:07 +00:00
|
|
|
}
|
|
|
|
|
2017-03-17 12:05:50 +00:00
|
|
|
QString LastSeenPrivacyController::exceptionBoxTitle(Exception exception) {
|
|
|
|
switch (exception) {
|
|
|
|
case Exception::Always: return lang(lng_edit_privacy_lastseen_always_title);
|
|
|
|
case Exception::Never: return lang(lng_edit_privacy_lastseen_never_title);
|
|
|
|
}
|
|
|
|
Unexpected("Invalid exception value.");
|
2017-03-16 15:15:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString LastSeenPrivacyController::exceptionsDescription() {
|
|
|
|
return lang(lng_edit_privacy_lastseen_exceptions);
|
|
|
|
}
|
|
|
|
|
2018-06-04 15:35:11 +00:00
|
|
|
void LastSeenPrivacyController::confirmSave(bool someAreDisallowed, FnMut<void()> saveCallback) {
|
2018-01-04 10:22:53 +00:00
|
|
|
if (someAreDisallowed && !Auth().settings().lastSeenWarningSeen()) {
|
2017-03-17 18:44:55 +00:00
|
|
|
auto weakBox = std::make_shared<QPointer<ConfirmBox>>();
|
|
|
|
auto callback = [weakBox, saveCallback = std::move(saveCallback)]() mutable {
|
|
|
|
if (auto box = *weakBox) {
|
|
|
|
box->closeBox();
|
|
|
|
}
|
|
|
|
saveCallback();
|
2018-01-04 10:22:53 +00:00
|
|
|
Auth().settings().setLastSeenWarningSeen(true);
|
2017-03-17 18:44:55 +00:00
|
|
|
Local::writeUserSettings();
|
|
|
|
};
|
2017-03-19 08:29:05 +00:00
|
|
|
auto box = Box<ConfirmBox>(lang(lng_edit_privacy_lastseen_warning), lang(lng_continue), lang(lng_cancel), std::move(callback));
|
2017-09-15 17:34:41 +00:00
|
|
|
*weakBox = Ui::show(std::move(box), LayerOption::KeepOther);
|
2017-03-17 18:44:55 +00:00
|
|
|
} else {
|
|
|
|
saveCallback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-11 09:11:52 +00:00
|
|
|
ApiWrap::Privacy::Key GroupsInvitePrivacyController::key() {
|
|
|
|
return Key::Invites;
|
|
|
|
}
|
|
|
|
|
|
|
|
MTPInputPrivacyKey GroupsInvitePrivacyController::apiKey() {
|
2017-03-17 16:19:57 +00:00
|
|
|
return MTP_inputPrivacyKeyChatInvite();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString GroupsInvitePrivacyController::title() {
|
|
|
|
return lang(lng_edit_privacy_groups_title);
|
|
|
|
}
|
|
|
|
|
2017-03-19 08:29:05 +00:00
|
|
|
bool GroupsInvitePrivacyController::hasOption(Option option) {
|
|
|
|
return (option != Option::Nobody);
|
2017-03-17 16:19:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString GroupsInvitePrivacyController::description() {
|
|
|
|
return lang(lng_edit_privacy_groups_description);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString GroupsInvitePrivacyController::exceptionLinkText(Exception exception, int count) {
|
|
|
|
switch (exception) {
|
2017-06-02 11:46:02 +00:00
|
|
|
case Exception::Always: return (count > 0) ? lng_edit_privacy_groups_always(lt_count, count) : lang(lng_edit_privacy_groups_always_empty);
|
|
|
|
case Exception::Never: return (count > 0) ? lng_edit_privacy_groups_never(lt_count, count) : lang(lng_edit_privacy_groups_never_empty);
|
2017-03-17 16:19:57 +00:00
|
|
|
}
|
|
|
|
Unexpected("Invalid exception value.");
|
|
|
|
}
|
|
|
|
|
|
|
|
QString GroupsInvitePrivacyController::exceptionBoxTitle(Exception exception) {
|
|
|
|
switch (exception) {
|
|
|
|
case Exception::Always: return lang(lng_edit_privacy_groups_always_title);
|
|
|
|
case Exception::Never: return lang(lng_edit_privacy_groups_never_title);
|
|
|
|
}
|
|
|
|
Unexpected("Invalid exception value.");
|
|
|
|
}
|
|
|
|
|
|
|
|
QString GroupsInvitePrivacyController::exceptionsDescription() {
|
|
|
|
return lang(lng_edit_privacy_groups_exceptions);
|
|
|
|
}
|
|
|
|
|
2018-09-11 09:11:52 +00:00
|
|
|
ApiWrap::Privacy::Key CallsPrivacyController::key() {
|
|
|
|
return Key::Calls;
|
|
|
|
}
|
|
|
|
|
|
|
|
MTPInputPrivacyKey CallsPrivacyController::apiKey() {
|
2017-04-29 15:32:01 +00:00
|
|
|
return MTP_inputPrivacyKeyPhoneCall();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString CallsPrivacyController::title() {
|
|
|
|
return lang(lng_edit_privacy_calls_title);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString CallsPrivacyController::description() {
|
|
|
|
return lang(lng_edit_privacy_calls_description);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString CallsPrivacyController::exceptionLinkText(Exception exception, int count) {
|
|
|
|
switch (exception) {
|
2017-06-02 11:46:02 +00:00
|
|
|
case Exception::Always: return (count > 0) ? lng_edit_privacy_calls_always(lt_count, count) : lang(lng_edit_privacy_calls_always_empty);
|
|
|
|
case Exception::Never: return (count > 0) ? lng_edit_privacy_calls_never(lt_count, count) : lang(lng_edit_privacy_calls_never_empty);
|
2017-04-29 15:32:01 +00:00
|
|
|
}
|
|
|
|
Unexpected("Invalid exception value.");
|
|
|
|
}
|
|
|
|
|
|
|
|
QString CallsPrivacyController::exceptionBoxTitle(Exception exception) {
|
|
|
|
switch (exception) {
|
|
|
|
case Exception::Always: return lang(lng_edit_privacy_calls_always_title);
|
|
|
|
case Exception::Never: return lang(lng_edit_privacy_calls_never_title);
|
|
|
|
}
|
|
|
|
Unexpected("Invalid exception value.");
|
|
|
|
}
|
|
|
|
|
|
|
|
QString CallsPrivacyController::exceptionsDescription() {
|
|
|
|
return lang(lng_edit_privacy_calls_exceptions);
|
|
|
|
}
|
|
|
|
|
2017-03-13 15:40:21 +00:00
|
|
|
} // namespace Settings
|