2017-09-26 17:57:01 +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 "info/profile/info_profile_members_controllers.h"
|
|
|
|
|
2017-10-22 12:07:57 +00:00
|
|
|
#include <rpl/variable.h>
|
2017-10-22 17:06:57 +00:00
|
|
|
#include "base/weak_unique_ptr.h"
|
2017-09-26 17:57:01 +00:00
|
|
|
#include "profile/profile_channel_controllers.h"
|
2017-10-22 17:06:57 +00:00
|
|
|
#include "ui/widgets/popup_menu.h"
|
2017-09-26 17:57:01 +00:00
|
|
|
#include "lang/lang_keys.h"
|
|
|
|
#include "apiwrap.h"
|
|
|
|
#include "auth_session.h"
|
2017-10-22 17:06:57 +00:00
|
|
|
#include "mainwidget.h"
|
2017-09-26 17:57:01 +00:00
|
|
|
#include "observer_peer.h"
|
2017-10-22 17:06:57 +00:00
|
|
|
#include "boxes/confirm_box.h"
|
2017-10-03 13:05:58 +00:00
|
|
|
#include "window/window_controller.h"
|
2017-11-07 17:53:00 +00:00
|
|
|
#include "styles/style_info.h"
|
2017-09-26 17:57:01 +00:00
|
|
|
|
|
|
|
namespace Info {
|
|
|
|
namespace Profile {
|
|
|
|
namespace {
|
|
|
|
|
2017-10-22 12:07:57 +00:00
|
|
|
constexpr auto kSortByOnlineDelay = TimeMs(1000);
|
|
|
|
|
2017-09-26 17:57:01 +00:00
|
|
|
class ChatMembersController
|
|
|
|
: public PeerListController
|
2017-10-22 17:06:57 +00:00
|
|
|
, private base::Subscriber
|
|
|
|
, public base::enable_weak_from_this {
|
2017-09-26 17:57:01 +00:00
|
|
|
public:
|
2017-10-03 13:05:58 +00:00
|
|
|
ChatMembersController(
|
|
|
|
not_null<Window::Controller*> window,
|
|
|
|
not_null<ChatData*> chat);
|
2017-09-26 17:57:01 +00:00
|
|
|
|
|
|
|
void prepare() override;
|
|
|
|
void rowClicked(not_null<PeerListRow*> row) override;
|
2017-11-07 17:53:00 +00:00
|
|
|
void rowActionClicked(not_null<PeerListRow*> row) override;
|
2017-10-22 17:06:57 +00:00
|
|
|
Ui::PopupMenu *rowContextMenu(
|
|
|
|
not_null<PeerListRow*> row) override;
|
2017-09-26 17:57:01 +00:00
|
|
|
|
2017-10-22 12:07:57 +00:00
|
|
|
rpl::producer<int> onlineCountValue() const override {
|
|
|
|
return _onlineCount.value();
|
|
|
|
}
|
|
|
|
|
2017-10-24 17:11:35 +00:00
|
|
|
std::unique_ptr<PeerListRow> createRestoredRow(
|
|
|
|
not_null<PeerData*> peer) override;
|
|
|
|
|
|
|
|
std::unique_ptr<PeerListState> saveState() override;
|
|
|
|
void restoreState(std::unique_ptr<PeerListState> state) override;
|
|
|
|
|
2017-09-26 17:57:01 +00:00
|
|
|
private:
|
2017-11-07 17:53:00 +00:00
|
|
|
using Rights = MemberListRow::Rights;
|
|
|
|
using Type = MemberListRow::Type;
|
2017-10-25 08:38:55 +00:00
|
|
|
struct SavedState : SavedStateBase {
|
|
|
|
rpl::lifetime lifetime;
|
|
|
|
};
|
2017-09-26 17:57:01 +00:00
|
|
|
void rebuildRows();
|
2017-11-07 17:53:00 +00:00
|
|
|
void rebuildRowTypes();
|
2017-10-22 12:07:57 +00:00
|
|
|
void refreshOnlineCount();
|
2017-11-07 17:53:00 +00:00
|
|
|
std::unique_ptr<PeerListRow> createRow(
|
|
|
|
not_null<UserData*> user);
|
2017-10-22 12:07:57 +00:00
|
|
|
void sortByOnline();
|
|
|
|
void sortByOnlineDelayed();
|
2017-10-22 17:06:57 +00:00
|
|
|
void removeMember(not_null<UserData*> user);
|
2017-11-07 17:53:00 +00:00
|
|
|
Type computeType(not_null<UserData*> user);
|
2017-09-26 17:57:01 +00:00
|
|
|
|
2017-10-03 13:05:58 +00:00
|
|
|
not_null<Window::Controller*> _window;
|
2017-09-26 17:57:01 +00:00
|
|
|
not_null<ChatData*> _chat;
|
|
|
|
|
2017-10-22 12:07:57 +00:00
|
|
|
base::Timer _sortByOnlineTimer;
|
|
|
|
rpl::variable<int> _onlineCount = 0;
|
|
|
|
|
2017-09-26 17:57:01 +00:00
|
|
|
};
|
|
|
|
|
2017-10-03 13:05:58 +00:00
|
|
|
ChatMembersController::ChatMembersController(
|
|
|
|
not_null<Window::Controller*> window,
|
|
|
|
not_null<ChatData*> chat)
|
2017-09-26 17:57:01 +00:00
|
|
|
: PeerListController()
|
2017-10-03 13:05:58 +00:00
|
|
|
, _window(window)
|
2017-09-26 17:57:01 +00:00
|
|
|
, _chat(chat) {
|
2017-10-22 12:07:57 +00:00
|
|
|
_sortByOnlineTimer.setCallback([this] { sortByOnline(); });
|
2017-09-26 17:57:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ChatMembersController::prepare() {
|
|
|
|
setSearchNoResultsText(lang(lng_blocked_list_not_found));
|
|
|
|
delegate()->peerListSetSearchMode(PeerListSearchMode::Enabled);
|
|
|
|
delegate()->peerListSetTitle(langFactory(lng_channel_admins));
|
|
|
|
|
|
|
|
rebuildRows();
|
|
|
|
if (!delegate()->peerListFullRowsCount()) {
|
|
|
|
Auth().api().requestFullPeer(_chat);
|
|
|
|
}
|
2017-09-26 18:44:58 +00:00
|
|
|
using UpdateFlag = Notify::PeerUpdate::Flag;
|
2017-09-26 17:57:01 +00:00
|
|
|
subscribe(Notify::PeerUpdated(), Notify::PeerUpdatedHandler(
|
2017-11-07 17:53:00 +00:00
|
|
|
UpdateFlag::MembersChanged
|
|
|
|
| UpdateFlag::UserOnlineChanged
|
|
|
|
| UpdateFlag::AdminsChanged,
|
2017-09-26 17:57:01 +00:00
|
|
|
[this](const Notify::PeerUpdate &update) {
|
2017-09-26 18:44:58 +00:00
|
|
|
if (update.flags & UpdateFlag::MembersChanged) {
|
|
|
|
if (update.peer == _chat) {
|
|
|
|
rebuildRows();
|
|
|
|
}
|
2017-11-07 17:53:00 +00:00
|
|
|
} else if (update.flags & UpdateFlag::AdminsChanged) {
|
|
|
|
if (update.peer == _chat) {
|
|
|
|
rebuildRowTypes();
|
|
|
|
}
|
2017-09-26 18:44:58 +00:00
|
|
|
} else if (update.flags & UpdateFlag::UserOnlineChanged) {
|
2017-10-22 12:07:57 +00:00
|
|
|
if (auto row = delegate()->peerListFindRow(
|
|
|
|
update.peer->id)) {
|
|
|
|
row->refreshStatus();
|
|
|
|
sortByOnlineDelayed();
|
|
|
|
}
|
2017-09-26 17:57:01 +00:00
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2017-10-22 12:07:57 +00:00
|
|
|
void ChatMembersController::sortByOnlineDelayed() {
|
|
|
|
if (!_sortByOnlineTimer.isActive()) {
|
|
|
|
_sortByOnlineTimer.callOnce(kSortByOnlineDelay);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChatMembersController::sortByOnline() {
|
|
|
|
auto now = unixtime();
|
|
|
|
delegate()->peerListSortRows([now](
|
|
|
|
const PeerListRow &a,
|
|
|
|
const PeerListRow &b) {
|
|
|
|
return App::onlineForSort(a.peer()->asUser(), now) >
|
|
|
|
App::onlineForSort(b.peer()->asUser(), now);
|
|
|
|
});
|
|
|
|
refreshOnlineCount();
|
|
|
|
}
|
|
|
|
|
2017-10-24 17:11:35 +00:00
|
|
|
std::unique_ptr<PeerListState> ChatMembersController::saveState() {
|
|
|
|
auto result = PeerListController::saveState();
|
2017-10-25 08:38:55 +00:00
|
|
|
auto my = std::make_unique<SavedState>();
|
2017-10-24 17:11:35 +00:00
|
|
|
using Flag = Notify::PeerUpdate::Flag;
|
|
|
|
Notify::PeerUpdateViewer(_chat, Flag::MembersChanged)
|
|
|
|
| rpl::start_with_next([state = result.get()](auto update) {
|
2017-10-25 08:38:55 +00:00
|
|
|
state->controllerState = nullptr;
|
|
|
|
}, my->lifetime);
|
|
|
|
result->controllerState = std::move(my);
|
2017-10-24 17:11:35 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChatMembersController::restoreState(
|
|
|
|
std::unique_ptr<PeerListState> state) {
|
|
|
|
PeerListController::restoreState(std::move(state));
|
|
|
|
sortByOnline();
|
|
|
|
}
|
|
|
|
|
2017-09-26 17:57:01 +00:00
|
|
|
void ChatMembersController::rebuildRows() {
|
|
|
|
if (_chat->participants.empty()) {
|
2017-11-07 13:13:41 +00:00
|
|
|
// We get such updates often
|
|
|
|
// (when participants list was invalidated).
|
|
|
|
//while (delegate()->peerListFullRowsCount() > 0) {
|
|
|
|
// delegate()->peerListRemoveRow(
|
|
|
|
// delegate()->peerListRowAt(0));
|
|
|
|
//}
|
2017-09-26 17:57:01 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto &participants = _chat->participants;
|
2017-10-22 17:06:57 +00:00
|
|
|
for (auto i = 0, count = delegate()->peerListFullRowsCount();
|
|
|
|
i != count;) {
|
|
|
|
auto row = delegate()->peerListRowAt(i);
|
|
|
|
auto user = row->peer()->asUser();
|
|
|
|
if (participants.contains(user)) {
|
|
|
|
++i;
|
|
|
|
} else {
|
|
|
|
delegate()->peerListRemoveRow(row);
|
|
|
|
--count;
|
|
|
|
}
|
2017-09-26 17:57:01 +00:00
|
|
|
}
|
2017-10-22 17:06:57 +00:00
|
|
|
for (auto i = participants.cbegin(), e = participants.cend();
|
|
|
|
i != e;
|
|
|
|
++i) {
|
|
|
|
if (auto row = createRow(i.key())) {
|
2017-09-26 17:57:01 +00:00
|
|
|
delegate()->peerListAppendRow(std::move(row));
|
|
|
|
}
|
2017-10-22 17:06:57 +00:00
|
|
|
}
|
|
|
|
sortByOnline();
|
2017-09-26 17:57:01 +00:00
|
|
|
|
|
|
|
delegate()->peerListRefreshRows();
|
|
|
|
}
|
|
|
|
|
2017-11-07 17:53:00 +00:00
|
|
|
void ChatMembersController::rebuildRowTypes() {
|
|
|
|
auto count = delegate()->peerListFullRowsCount();
|
|
|
|
for (auto i = 0; i != count; ++i) {
|
|
|
|
auto row = static_cast<MemberListRow*>(
|
|
|
|
delegate()->peerListRowAt(i).get());
|
|
|
|
row->setType(computeType(row->user()));
|
|
|
|
}
|
|
|
|
delegate()->peerListRefreshRows();
|
|
|
|
}
|
|
|
|
|
2017-10-22 12:07:57 +00:00
|
|
|
void ChatMembersController::refreshOnlineCount() {
|
|
|
|
auto now = unixtime();
|
|
|
|
auto left = 0, right = delegate()->peerListFullRowsCount();
|
|
|
|
while (right > left) {
|
|
|
|
auto middle = (left + right) / 2;
|
|
|
|
auto row = delegate()->peerListRowAt(middle);
|
|
|
|
if (App::onlineColorUse(row->peer()->asUser(), now)) {
|
|
|
|
left = middle + 1;
|
|
|
|
} else {
|
|
|
|
right = middle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_onlineCount = left;
|
|
|
|
}
|
|
|
|
|
2017-10-24 17:11:35 +00:00
|
|
|
std::unique_ptr<PeerListRow> ChatMembersController::createRestoredRow(
|
|
|
|
not_null<PeerData*> peer) {
|
|
|
|
if (auto user = peer->asUser()) {
|
|
|
|
return createRow(user);
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<PeerListRow> ChatMembersController::createRow(
|
|
|
|
not_null<UserData*> user) {
|
2017-11-07 17:53:00 +00:00
|
|
|
return std::make_unique<MemberListRow>(user, computeType(user));
|
|
|
|
}
|
|
|
|
|
|
|
|
auto ChatMembersController::computeType(
|
|
|
|
not_null<UserData*> user) -> Type {
|
|
|
|
auto isCreator = (peerFromUser(_chat->creator) == user->id);
|
|
|
|
auto isAdmin = _chat->adminsEnabled()
|
|
|
|
&& _chat->admins.contains(user);
|
|
|
|
auto canRemove = [&] {
|
|
|
|
if (user->isSelf()) {
|
|
|
|
return false;
|
|
|
|
} else if (_chat->amCreator()) {
|
|
|
|
return true;
|
|
|
|
} else if (isAdmin || isCreator) {
|
|
|
|
return false;
|
|
|
|
} else if (_chat->amAdmin()) {
|
|
|
|
return true;
|
|
|
|
} else if (_chat->invitedByMe.contains(user)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}();
|
|
|
|
|
|
|
|
auto result = Type();
|
|
|
|
result.rights = isCreator
|
|
|
|
? Rights::Creator
|
|
|
|
: isAdmin
|
|
|
|
? Rights::Admin
|
|
|
|
: Rights::Normal;
|
|
|
|
result.canRemove = canRemove;
|
|
|
|
return result;
|
2017-09-26 17:57:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ChatMembersController::rowClicked(not_null<PeerListRow*> row) {
|
2017-10-03 13:05:58 +00:00
|
|
|
_window->showPeerInfo(row->peer());
|
2017-09-26 17:57:01 +00:00
|
|
|
}
|
|
|
|
|
2017-11-07 17:53:00 +00:00
|
|
|
void ChatMembersController::rowActionClicked(
|
2017-10-22 17:06:57 +00:00
|
|
|
not_null<PeerListRow*> row) {
|
2017-11-07 17:53:00 +00:00
|
|
|
removeMember(row->peer()->asUser());
|
|
|
|
}
|
2017-10-22 17:06:57 +00:00
|
|
|
|
2017-11-07 17:53:00 +00:00
|
|
|
Ui::PopupMenu *ChatMembersController::rowContextMenu(
|
|
|
|
not_null<PeerListRow*> row) {
|
|
|
|
auto my = static_cast<MemberListRow*>(row.get());
|
|
|
|
auto user = my->user();
|
|
|
|
auto canRemoveMember = my->canRemove();
|
2017-11-07 13:13:41 +00:00
|
|
|
|
2017-10-22 17:06:57 +00:00
|
|
|
auto result = new Ui::PopupMenu(nullptr);
|
|
|
|
result->addAction(
|
|
|
|
lang(lng_context_view_profile),
|
|
|
|
[weak = base::make_weak_unique(this), user] {
|
|
|
|
if (weak) {
|
|
|
|
weak->_window->showPeerInfo(user);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (canRemoveMember) {
|
|
|
|
result->addAction(
|
|
|
|
lang(lng_context_remove_from_group),
|
|
|
|
[weak = base::make_weak_unique(this), user] {
|
|
|
|
if (weak) {
|
|
|
|
weak->removeMember(user);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChatMembersController::removeMember(not_null<UserData*> user) {
|
|
|
|
auto text = lng_profile_sure_kick(lt_user, user->firstName);
|
|
|
|
Ui::show(Box<ConfirmBox>(text, lang(lng_box_remove), [user, chat = _chat] {
|
|
|
|
Ui::hideLayer();
|
|
|
|
if (App::main()) App::main()->kickParticipant(chat, user);
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2017-09-26 17:57:01 +00:00
|
|
|
} // namespace
|
|
|
|
|
2017-11-07 17:53:00 +00:00
|
|
|
MemberListRow::MemberListRow(
|
|
|
|
not_null<UserData*> user,
|
|
|
|
Type type)
|
|
|
|
: PeerListRow(user)
|
|
|
|
, _type(type) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void MemberListRow::setType(Type type) {
|
|
|
|
_type = type;
|
|
|
|
}
|
|
|
|
|
|
|
|
QSize MemberListRow::actionSize() const {
|
|
|
|
return canRemove()
|
|
|
|
? QRect(
|
|
|
|
QPoint(),
|
|
|
|
st::infoMembersRemoveIcon.size()).marginsAdded(
|
|
|
|
st::infoMembersRemoveIconMargins).size()
|
|
|
|
: QSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MemberListRow::paintAction(
|
|
|
|
Painter &p,
|
|
|
|
TimeMs ms,
|
|
|
|
int x,
|
|
|
|
int y,
|
|
|
|
int outerWidth,
|
|
|
|
bool selected,
|
|
|
|
bool actionSelected) {
|
|
|
|
if (_type.canRemove && selected) {
|
|
|
|
x += st::infoMembersRemoveIconMargins.left();
|
|
|
|
y += st::infoMembersRemoveIconMargins.top();
|
|
|
|
(actionSelected
|
|
|
|
? st::infoMembersRemoveIconOver
|
|
|
|
: st::infoMembersRemoveIcon).paint(p, x, y, outerWidth);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int MemberListRow::nameIconWidth() const {
|
|
|
|
return (_type.rights == Rights::Admin)
|
|
|
|
? st::infoMembersAdminIcon.width()
|
|
|
|
: (_type.rights == Rights::Creator)
|
|
|
|
? st::infoMembersCreatorIcon.width()
|
|
|
|
: 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MemberListRow::paintNameIcon(
|
|
|
|
Painter &p,
|
|
|
|
int x,
|
|
|
|
int y,
|
2017-11-08 16:45:30 +00:00
|
|
|
int outerWidth,
|
|
|
|
bool selected) {
|
2017-11-07 17:53:00 +00:00
|
|
|
auto icon = [&] {
|
|
|
|
return (_type.rights == Rights::Admin)
|
2017-11-08 16:45:30 +00:00
|
|
|
? (selected
|
|
|
|
? &st::infoMembersAdminIconOver
|
|
|
|
: &st::infoMembersAdminIcon)
|
|
|
|
: (selected
|
|
|
|
? &st::infoMembersCreatorIconOver
|
|
|
|
: &st::infoMembersCreatorIcon);
|
2017-11-07 17:53:00 +00:00
|
|
|
}();
|
|
|
|
icon->paint(p, x, y, outerWidth);
|
|
|
|
}
|
|
|
|
|
2017-09-26 17:57:01 +00:00
|
|
|
std::unique_ptr<PeerListController> CreateMembersController(
|
2017-10-03 13:05:58 +00:00
|
|
|
not_null<Window::Controller*> window,
|
2017-09-26 17:57:01 +00:00
|
|
|
not_null<PeerData*> peer) {
|
|
|
|
if (auto chat = peer->asChat()) {
|
2017-10-03 13:05:58 +00:00
|
|
|
return std::make_unique<ChatMembersController>(
|
|
|
|
window,
|
|
|
|
chat);
|
2017-09-26 17:57:01 +00:00
|
|
|
} else if (auto channel = peer->asChannel()) {
|
|
|
|
using ChannelMembersController
|
|
|
|
= ::Profile::ParticipantsBoxController;
|
|
|
|
return std::make_unique<ChannelMembersController>(
|
2017-10-03 13:05:58 +00:00
|
|
|
window,
|
2017-09-26 17:57:01 +00:00
|
|
|
channel,
|
|
|
|
ChannelMembersController::Role::Profile);
|
|
|
|
}
|
|
|
|
Unexpected("Peer type in CreateMembersController()");
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Profile
|
|
|
|
} // namespace Info
|