2017-03-16 15:15:07 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop version of Telegram messaging app, see https://telegram.org
|
|
|
|
|
|
|
|
Telegram Desktop is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
It is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
In addition, as a special exception, the copyright holders give permission
|
|
|
|
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 "boxes/edit_privacy_box.h"
|
|
|
|
|
|
|
|
#include "styles/style_boxes.h"
|
|
|
|
#include "ui/widgets/checkbox.h"
|
|
|
|
#include "ui/widgets/labels.h"
|
|
|
|
#include "ui/widgets/buttons.h"
|
2017-09-13 16:57:44 +00:00
|
|
|
#include "ui/wrap/slide_wrap.h"
|
2017-08-14 12:53:49 +00:00
|
|
|
#include "boxes/peer_list_controllers.h"
|
2017-03-17 17:19:46 +00:00
|
|
|
#include "apiwrap.h"
|
2017-08-04 14:54:32 +00:00
|
|
|
#include "auth_session.h"
|
2017-04-13 08:27:10 +00:00
|
|
|
#include "lang/lang_keys.h"
|
2017-03-16 15:15:07 +00:00
|
|
|
|
2017-03-17 12:05:50 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class PrivacyExceptionsBoxController : public ChatsListBoxController {
|
|
|
|
public:
|
2017-08-17 08:31:24 +00:00
|
|
|
PrivacyExceptionsBoxController(base::lambda<QString()> titleFactory, const std::vector<not_null<UserData*>> &selected);
|
|
|
|
void rowClicked(not_null<PeerListRow*> row) override;
|
2017-06-13 21:08:47 +00:00
|
|
|
|
2017-08-17 08:31:24 +00:00
|
|
|
std::vector<not_null<UserData*>> getResult() const;
|
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;
|
2017-03-17 12:05:50 +00:00
|
|
|
|
|
|
|
private:
|
2017-05-30 15:21:05 +00:00
|
|
|
base::lambda<QString()> _titleFactory;
|
2017-08-17 08:31:24 +00:00
|
|
|
std::vector<not_null<UserData*>> _selected;
|
2017-03-17 12:05:50 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2017-08-17 08:31:24 +00:00
|
|
|
PrivacyExceptionsBoxController::PrivacyExceptionsBoxController(base::lambda<QString()> titleFactory, const std::vector<not_null<UserData*>> &selected)
|
2017-05-30 15:21:05 +00:00
|
|
|
: _titleFactory(std::move(titleFactory))
|
2017-06-13 21:08:47 +00:00
|
|
|
, _selected(selected) {
|
2017-03-17 12:05:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PrivacyExceptionsBoxController::prepareViewHook() {
|
2017-06-13 21:08:47 +00:00
|
|
|
delegate()->peerListSetTitle(_titleFactory);
|
|
|
|
delegate()->peerListAddSelectedRows(_selected);
|
|
|
|
}
|
|
|
|
|
2017-08-17 08:31:24 +00:00
|
|
|
std::vector<not_null<UserData*>> PrivacyExceptionsBoxController::getResult() const {
|
2017-06-13 21:08:47 +00:00
|
|
|
auto peers = delegate()->peerListCollectSelectedRows();
|
2017-08-17 08:31:24 +00:00
|
|
|
auto users = std::vector<not_null<UserData*>>();
|
2017-06-13 21:08:47 +00:00
|
|
|
if (!peers.empty()) {
|
|
|
|
users.reserve(peers.size());
|
|
|
|
for_const (auto peer, peers) {
|
|
|
|
auto user = peer->asUser();
|
2017-08-17 09:06:26 +00:00
|
|
|
Assert(user != nullptr);
|
2017-06-13 21:08:47 +00:00
|
|
|
users.push_back(user);
|
2017-03-17 12:05:50 +00:00
|
|
|
}
|
2017-06-13 21:08:47 +00:00
|
|
|
}
|
|
|
|
return users;
|
2017-03-17 12:05:50 +00:00
|
|
|
}
|
|
|
|
|
2017-08-17 08:31:24 +00:00
|
|
|
void PrivacyExceptionsBoxController::rowClicked(not_null<PeerListRow*> row) {
|
2017-06-13 21:08:47 +00:00
|
|
|
delegate()->peerListSetRowChecked(row, !row->checked());
|
2017-03-17 12:05:50 +00:00
|
|
|
}
|
|
|
|
|
2017-08-17 08:31:24 +00:00
|
|
|
std::unique_ptr<PrivacyExceptionsBoxController::Row> PrivacyExceptionsBoxController::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()) {
|
2017-08-13 16:14:00 +00:00
|
|
|
return std::make_unique<Row>(history);
|
2017-03-17 12:05:50 +00:00
|
|
|
}
|
2017-08-13 16:14:00 +00:00
|
|
|
return nullptr;
|
2017-03-17 12:05:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2017-03-16 15:15:07 +00:00
|
|
|
EditPrivacyBox::EditPrivacyBox(QWidget*, std::unique_ptr<Controller> controller) : BoxContent()
|
|
|
|
, _controller(std::move(controller))
|
|
|
|
, _loading(this, lang(lng_contacts_loading), Ui::FlatLabel::InitType::Simple, st::membersAbout) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditPrivacyBox::prepare() {
|
|
|
|
_controller->setView(this);
|
|
|
|
|
2017-05-30 15:21:05 +00:00
|
|
|
setTitle([this] { return _controller->title(); });
|
|
|
|
addButton(langFactory(lng_cancel), [this] { closeBox(); });
|
2017-03-16 15:15:07 +00:00
|
|
|
|
2017-03-22 15:38:40 +00:00
|
|
|
loadData();
|
2017-03-16 15:15:07 +00:00
|
|
|
|
|
|
|
setDimensions(st::boxWideWidth, countDefaultHeight(st::boxWideWidth));
|
|
|
|
}
|
|
|
|
|
|
|
|
int EditPrivacyBox::resizeGetHeight(int newWidth) {
|
|
|
|
auto top = 0;
|
|
|
|
auto layoutRow = [this, newWidth, &top](auto &widget, style::margins padding) {
|
|
|
|
if (!widget) return;
|
|
|
|
widget->resizeToNaturalWidth(newWidth - padding.left() - padding.right());
|
|
|
|
widget->moveToLeft(padding.left(), top + padding.top());
|
|
|
|
top = widget->bottomNoMargins() + padding.bottom();
|
|
|
|
};
|
|
|
|
|
2017-03-19 08:29:05 +00:00
|
|
|
layoutRow(_description, st::editPrivacyPadding);
|
2017-03-16 15:15:07 +00:00
|
|
|
layoutRow(_everyone, st::editPrivacyOptionMargin);
|
|
|
|
layoutRow(_contacts, st::editPrivacyOptionMargin);
|
|
|
|
layoutRow(_nobody, st::editPrivacyOptionMargin);
|
2017-03-19 08:29:05 +00:00
|
|
|
layoutRow(_warning, st::editPrivacyWarningPadding);
|
2017-09-20 19:44:22 +00:00
|
|
|
layoutRow(_exceptionsTitle, st::editPrivacyTitlePadding);
|
2017-03-16 15:15:07 +00:00
|
|
|
auto linksTop = top;
|
|
|
|
layoutRow(_alwaysLink, st::editPrivacyPadding);
|
|
|
|
layoutRow(_neverLink, st::editPrivacyPadding);
|
|
|
|
auto linksHeight = top - linksTop;
|
|
|
|
layoutRow(_exceptionsDescription, st::editPrivacyPadding);
|
|
|
|
|
|
|
|
// Add full width of both links in any case
|
|
|
|
auto linkMargins = exceptionLinkMargins();
|
|
|
|
top -= linksHeight;
|
|
|
|
top += linkMargins.top() + st::boxLinkButton.font->height + linkMargins.bottom();
|
|
|
|
top += linkMargins.top() + st::boxLinkButton.font->height + linkMargins.bottom();
|
|
|
|
|
|
|
|
return top;
|
|
|
|
}
|
|
|
|
|
2017-03-19 08:29:05 +00:00
|
|
|
void EditPrivacyBox::resizeEvent(QResizeEvent *e) {
|
|
|
|
if (_loading) {
|
|
|
|
_loading->moveToLeft((width() - _loading->width()) / 2, height() / 3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-16 15:15:07 +00:00
|
|
|
int EditPrivacyBox::countDefaultHeight(int newWidth) {
|
|
|
|
auto height = 0;
|
2017-03-19 08:29:05 +00:00
|
|
|
auto optionHeight = [this](Option option) {
|
|
|
|
if (!_controller->hasOption(option)) {
|
2017-03-16 15:15:07 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2017-07-12 13:53:42 +00:00
|
|
|
return st::editPrivacyOptionMargin.top() + st::defaultCheck.diameter + st::editPrivacyOptionMargin.bottom();
|
2017-03-16 15:15:07 +00:00
|
|
|
};
|
2017-03-19 08:29:05 +00:00
|
|
|
auto labelHeight = [this, newWidth](const QString &text, const style::FlatLabel &st, style::margins padding) {
|
|
|
|
if (text.isEmpty()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-03-16 15:15:07 +00:00
|
|
|
auto fake = object_ptr<Ui::FlatLabel>(nullptr, text, Ui::FlatLabel::InitType::Simple, st);
|
2017-03-19 08:29:05 +00:00
|
|
|
fake->resizeToNaturalWidth(newWidth - padding.left() - padding.right());
|
|
|
|
return padding.top() + fake->heightNoMargins() + padding.bottom();
|
2017-03-16 15:15:07 +00:00
|
|
|
};
|
2017-03-19 08:29:05 +00:00
|
|
|
auto linkHeight = [this]() {
|
|
|
|
auto linkMargins = exceptionLinkMargins();
|
|
|
|
return linkMargins.top() + st::boxLinkButton.font->height + linkMargins.bottom();
|
|
|
|
};
|
|
|
|
height += labelHeight(_controller->description(), st::editPrivacyLabel, st::editPrivacyPadding);
|
|
|
|
height += optionHeight(Option::Everyone);
|
|
|
|
height += optionHeight(Option::Contacts);
|
|
|
|
height += optionHeight(Option::Nobody);
|
|
|
|
height += labelHeight(_controller->warning(), st::editPrivacyLabel, st::editPrivacyWarningPadding);
|
2017-09-20 19:44:22 +00:00
|
|
|
height += labelHeight(lang(lng_edit_privacy_exceptions), st::editPrivacyTitle, st::editPrivacyTitlePadding);
|
2017-03-19 08:29:05 +00:00
|
|
|
height += linkHeight();
|
|
|
|
height += linkHeight();
|
|
|
|
height += labelHeight(_controller->exceptionsDescription(), st::editPrivacyLabel, st::editPrivacyPadding);
|
2017-03-16 15:15:07 +00:00
|
|
|
return height;
|
|
|
|
}
|
|
|
|
|
2017-03-17 12:05:50 +00:00
|
|
|
void EditPrivacyBox::editExceptionUsers(Exception exception) {
|
2017-05-30 15:21:05 +00:00
|
|
|
auto controller = std::make_unique<PrivacyExceptionsBoxController>(base::lambda_guarded(this, [this, exception] {
|
|
|
|
return _controller->exceptionBoxTitle(exception);
|
2017-06-13 21:08:47 +00:00
|
|
|
}), exceptionUsers(exception));
|
2017-08-17 08:31:24 +00:00
|
|
|
auto initBox = [this, exception, controller = controller.get()](not_null<PeerListBox*> box) {
|
2017-06-13 21:08:47 +00:00
|
|
|
box->addButton(langFactory(lng_settings_save), base::lambda_guarded(this, [this, box, exception, controller] {
|
|
|
|
exceptionUsers(exception) = controller->getResult();
|
|
|
|
exceptionLink(exception)->entity()->setText(exceptionLinkText(exception));
|
|
|
|
auto removeFrom = ([exception] {
|
|
|
|
switch (exception) {
|
|
|
|
case Exception::Always: return Exception::Never;
|
|
|
|
case Exception::Never: return Exception::Always;
|
|
|
|
}
|
|
|
|
Unexpected("Invalid exception value.");
|
|
|
|
})();
|
|
|
|
auto &removeFromUsers = exceptionUsers(removeFrom);
|
|
|
|
auto removedSome = false;
|
|
|
|
for (auto user : exceptionUsers(exception)) {
|
|
|
|
auto removedStart = std::remove(removeFromUsers.begin(), removeFromUsers.end(), user);
|
|
|
|
if (removedStart != removeFromUsers.end()) {
|
|
|
|
removeFromUsers.erase(removedStart, removeFromUsers.end());
|
|
|
|
removedSome = true;
|
|
|
|
}
|
2017-03-17 12:05:50 +00:00
|
|
|
}
|
2017-06-13 21:08:47 +00:00
|
|
|
if (removedSome) {
|
|
|
|
exceptionLink(removeFrom)->entity()->setText(exceptionLinkText(removeFrom));
|
2017-03-17 12:05:50 +00:00
|
|
|
}
|
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-16 15:15:07 +00:00
|
|
|
}
|
|
|
|
|
2017-03-17 12:05:50 +00:00
|
|
|
QString EditPrivacyBox::exceptionLinkText(Exception exception) {
|
|
|
|
return _controller->exceptionLinkText(exception, exceptionUsers(exception).size());
|
2017-03-16 15:15:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QVector<MTPInputPrivacyRule> EditPrivacyBox::collectResult() {
|
|
|
|
auto collectInputUsers = [](auto &users) {
|
|
|
|
auto result = QVector<MTPInputUser>();
|
|
|
|
result.reserve(users.size());
|
|
|
|
for (auto user : users) {
|
|
|
|
result.push_back(user->inputUser);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
2017-03-19 08:29:05 +00:00
|
|
|
constexpr auto kMaxRules = 3; // allow users, disallow users, option
|
2017-03-16 15:15:07 +00:00
|
|
|
auto result = QVector<MTPInputPrivacyRule>();
|
2017-03-19 08:29:05 +00:00
|
|
|
result.reserve(kMaxRules);
|
2017-03-17 12:05:50 +00:00
|
|
|
if (showExceptionLink(Exception::Always) && !_alwaysUsers.empty()) {
|
2017-03-16 15:15:07 +00:00
|
|
|
result.push_back(MTP_inputPrivacyValueAllowUsers(MTP_vector<MTPInputUser>(collectInputUsers(_alwaysUsers))));
|
|
|
|
}
|
2017-03-17 12:05:50 +00:00
|
|
|
if (showExceptionLink(Exception::Never) && !_neverUsers.empty()) {
|
2017-03-16 15:15:07 +00:00
|
|
|
result.push_back(MTP_inputPrivacyValueDisallowUsers(MTP_vector<MTPInputUser>(collectInputUsers(_neverUsers))));
|
|
|
|
}
|
|
|
|
switch (_option) {
|
|
|
|
case Option::Everyone: result.push_back(MTP_inputPrivacyValueAllowAll()); break;
|
|
|
|
case Option::Contacts: result.push_back(MTP_inputPrivacyValueAllowContacts()); break;
|
|
|
|
case Option::Nobody: result.push_back(MTP_inputPrivacyValueDisallowAll()); break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
style::margins EditPrivacyBox::exceptionLinkMargins() const {
|
|
|
|
return st::editPrivacyLinkMargin;
|
|
|
|
}
|
|
|
|
|
2017-08-17 08:31:24 +00:00
|
|
|
std::vector<not_null<UserData*>> &EditPrivacyBox::exceptionUsers(Exception exception) {
|
2017-03-17 12:05:50 +00:00
|
|
|
switch (exception) {
|
|
|
|
case Exception::Always: return _alwaysUsers;
|
|
|
|
case Exception::Never: return _neverUsers;
|
|
|
|
}
|
|
|
|
Unexpected("Invalid exception value.");
|
|
|
|
}
|
|
|
|
|
2017-09-13 16:57:44 +00:00
|
|
|
object_ptr<Ui::SlideWrap<Ui::LinkButton>> &EditPrivacyBox::exceptionLink(Exception exception) {
|
2017-03-17 12:05:50 +00:00
|
|
|
switch (exception) {
|
|
|
|
case Exception::Always: return _alwaysLink;
|
|
|
|
case Exception::Never: return _neverLink;
|
|
|
|
}
|
|
|
|
Unexpected("Invalid exception value.");
|
2017-03-16 15:15:07 +00:00
|
|
|
}
|
|
|
|
|
2017-03-17 12:05:50 +00:00
|
|
|
bool EditPrivacyBox::showExceptionLink(Exception exception) const {
|
|
|
|
switch (exception) {
|
|
|
|
case Exception::Always: return (_option == Option::Contacts) || (_option == Option::Nobody);
|
|
|
|
case Exception::Never: return (_option == Option::Everyone) || (_option == Option::Contacts);
|
|
|
|
}
|
|
|
|
Unexpected("Invalid exception value.");
|
2017-03-16 15:15:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void EditPrivacyBox::createWidgets() {
|
|
|
|
_loading.destroy();
|
2017-03-18 21:06:10 +00:00
|
|
|
_optionGroup = std::make_shared<Ui::RadioenumGroup<Option>>(_option);
|
2017-03-16 15:15:07 +00:00
|
|
|
|
2017-03-19 08:29:05 +00:00
|
|
|
auto createOption = [this](object_ptr<Ui::Radioenum<Option>> &widget, Option option, const QString &label) {
|
|
|
|
if (_controller->hasOption(option) || (_option == option)) {
|
|
|
|
widget.create(this, _optionGroup, option, label, st::defaultBoxCheckbox);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
auto createLabel = [this](object_ptr<Ui::FlatLabel> &widget, const QString &text, const style::FlatLabel &st) {
|
|
|
|
if (text.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
widget.create(this, text, Ui::FlatLabel::InitType::Simple, st);
|
|
|
|
};
|
2017-03-17 12:05:50 +00:00
|
|
|
auto createExceptionLink = [this](Exception exception) {
|
2017-09-13 16:57:44 +00:00
|
|
|
exceptionLink(exception).create(this, object_ptr<Ui::LinkButton>(this, exceptionLinkText(exception)), exceptionLinkMargins());
|
|
|
|
exceptionLink(exception)->heightValue()
|
2017-09-27 18:29:13 +00:00
|
|
|
| rpl::start_with_next([this] {
|
2017-09-18 17:04:45 +00:00
|
|
|
resizeToWidth(width());
|
|
|
|
}, lifetime());
|
2017-03-17 12:05:50 +00:00
|
|
|
exceptionLink(exception)->entity()->setClickedCallback([this, exception] { editExceptionUsers(exception); });
|
2017-03-16 15:15:07 +00:00
|
|
|
};
|
2017-03-19 08:29:05 +00:00
|
|
|
|
|
|
|
createLabel(_description, _controller->description(), st::editPrivacyLabel);
|
|
|
|
createOption(_everyone, Option::Everyone, lang(lng_edit_privacy_everyone));
|
|
|
|
createOption(_contacts, Option::Contacts, lang(lng_edit_privacy_contacts));
|
|
|
|
createOption(_nobody, Option::Nobody, lang(lng_edit_privacy_nobody));
|
|
|
|
createLabel(_warning, _controller->warning(), st::editPrivacyLabel);
|
|
|
|
createLabel(_exceptionsTitle, lang(lng_edit_privacy_exceptions), st::editPrivacyTitle);
|
2017-03-17 12:05:50 +00:00
|
|
|
createExceptionLink(Exception::Always);
|
|
|
|
createExceptionLink(Exception::Never);
|
2017-03-19 08:29:05 +00:00
|
|
|
createLabel(_exceptionsDescription, _controller->exceptionsDescription(), st::editPrivacyLabel);
|
2017-03-16 15:15:07 +00:00
|
|
|
|
2017-03-17 12:05:50 +00:00
|
|
|
clearButtons();
|
2017-05-30 15:21:05 +00:00
|
|
|
addButton(langFactory(lng_settings_save), [this] {
|
2017-03-17 18:44:55 +00:00
|
|
|
auto someAreDisallowed = (_option != Option::Everyone) || !_neverUsers.empty();
|
|
|
|
_controller->confirmSave(someAreDisallowed, base::lambda_guarded(this, [this] {
|
2017-08-04 14:54:32 +00:00
|
|
|
Auth().api().savePrivacy(_controller->key(), collectResult());
|
2017-03-17 18:44:55 +00:00
|
|
|
closeBox();
|
|
|
|
}));
|
2017-03-17 17:19:46 +00:00
|
|
|
});
|
2017-05-30 15:21:05 +00:00
|
|
|
addButton(langFactory(lng_cancel), [this] { closeBox(); });
|
2017-03-19 08:29:05 +00:00
|
|
|
|
|
|
|
_optionGroup->setChangedCallback([this](Option value) {
|
|
|
|
_option = value;
|
2017-10-01 08:15:40 +00:00
|
|
|
_alwaysLink->toggle(
|
|
|
|
showExceptionLink(Exception::Always),
|
|
|
|
anim::type::normal);
|
|
|
|
_neverLink->toggle(
|
|
|
|
showExceptionLink(Exception::Never),
|
|
|
|
anim::type::normal);
|
2017-03-19 08:29:05 +00:00
|
|
|
});
|
2017-03-16 15:15:07 +00:00
|
|
|
|
|
|
|
showChildren();
|
2017-10-01 08:15:40 +00:00
|
|
|
_alwaysLink->toggle(
|
|
|
|
showExceptionLink(Exception::Always),
|
|
|
|
anim::type::instant);
|
|
|
|
_neverLink->toggle(
|
|
|
|
showExceptionLink(Exception::Never),
|
|
|
|
anim::type::instant);
|
2017-03-16 15:15:07 +00:00
|
|
|
|
|
|
|
setDimensions(st::boxWideWidth, resizeGetHeight(st::boxWideWidth));
|
|
|
|
}
|
|
|
|
|
2017-03-22 15:38:40 +00:00
|
|
|
void EditPrivacyBox::loadData() {
|
|
|
|
request(MTPaccount_GetPrivacy(_controller->key())).done([this](const MTPaccount_PrivacyRules &result) {
|
|
|
|
Expects(result.type() == mtpc_account_privacyRules);
|
|
|
|
auto &rules = result.c_account_privacyRules();
|
|
|
|
App::feedUsers(rules.vusers);
|
|
|
|
|
|
|
|
// This is simplified version of privacy rules interpretation.
|
|
|
|
// But it should be fine for all the apps that use the same subset of features.
|
|
|
|
auto optionSet = false;
|
|
|
|
auto setOption = [this, &optionSet](Option option) {
|
|
|
|
if (optionSet) return;
|
|
|
|
optionSet = true;
|
|
|
|
_option = option;
|
|
|
|
};
|
|
|
|
auto feedRule = [this, &setOption](const MTPPrivacyRule &rule) {
|
|
|
|
switch (rule.type()) {
|
|
|
|
case mtpc_privacyValueAllowAll: setOption(Option::Everyone); break;
|
|
|
|
case mtpc_privacyValueAllowContacts: setOption(Option::Contacts); break;
|
|
|
|
case mtpc_privacyValueAllowUsers: {
|
|
|
|
auto &users = rule.c_privacyValueAllowUsers().vusers.v;
|
|
|
|
_alwaysUsers.reserve(_alwaysUsers.size() + users.size());
|
|
|
|
for (auto &userId : users) {
|
|
|
|
auto user = App::user(UserId(userId.v));
|
2017-06-13 21:08:47 +00:00
|
|
|
if (!base::contains(_neverUsers, user) && !base::contains(_alwaysUsers, user)) {
|
2017-03-22 15:38:40 +00:00
|
|
|
_alwaysUsers.push_back(user);
|
|
|
|
}
|
2017-03-16 15:15:07 +00:00
|
|
|
}
|
2017-03-22 15:38:40 +00:00
|
|
|
} break;
|
|
|
|
case mtpc_privacyValueDisallowContacts: // not supported, fall through
|
|
|
|
case mtpc_privacyValueDisallowAll: setOption(Option::Nobody); break;
|
|
|
|
case mtpc_privacyValueDisallowUsers: {
|
|
|
|
auto &users = rule.c_privacyValueDisallowUsers().vusers.v;
|
|
|
|
_neverUsers.reserve(_neverUsers.size() + users.size());
|
|
|
|
for (auto &userId : users) {
|
|
|
|
auto user = App::user(UserId(userId.v));
|
2017-06-13 21:08:47 +00:00
|
|
|
if (!base::contains(_alwaysUsers, user) && !base::contains(_neverUsers, user)) {
|
2017-03-22 15:38:40 +00:00
|
|
|
_neverUsers.push_back(user);
|
|
|
|
}
|
2017-03-16 15:15:07 +00:00
|
|
|
}
|
2017-03-22 15:38:40 +00:00
|
|
|
} break;
|
2017-03-16 15:15:07 +00:00
|
|
|
}
|
2017-03-22 15:38:40 +00:00
|
|
|
};
|
|
|
|
for (auto &rule : rules.vrules.v) {
|
|
|
|
feedRule(rule);
|
2017-03-16 15:15:07 +00:00
|
|
|
}
|
2017-03-22 15:38:40 +00:00
|
|
|
feedRule(MTP_privacyValueDisallowAll()); // disallow by default.
|
2017-03-16 15:15:07 +00:00
|
|
|
|
2017-03-22 15:38:40 +00:00
|
|
|
createWidgets();
|
|
|
|
}).send();
|
2017-03-16 15:15:07 +00:00
|
|
|
}
|
|
|
|
|