From 301aa9572f45fa199d61b5b294fb6dbb96224237 Mon Sep 17 00:00:00 2001 From: John Preston Date: Sat, 2 Dec 2017 15:07:27 +0400 Subject: [PATCH] Apply channel admin edition changes. --- Telegram/SourceFiles/apiwrap.cpp | 25 +++------- .../SourceFiles/data/data_channel_admins.cpp | 48 +++++++++++++++++++ .../SourceFiles/data/data_channel_admins.h | 40 ++++++++++++++++ Telegram/SourceFiles/data/data_peer.cpp | 13 ++++- Telegram/SourceFiles/history/history.cpp | 2 + Telegram/gyp/telegram_sources.txt | 2 + 6 files changed, 111 insertions(+), 19 deletions(-) create mode 100644 Telegram/SourceFiles/data/data_channel_admins.cpp create mode 100644 Telegram/SourceFiles/data/data_channel_admins.h diff --git a/Telegram/SourceFiles/apiwrap.cpp b/Telegram/SourceFiles/apiwrap.cpp index f39ea7a36..705a4edb0 100644 --- a/Telegram/SourceFiles/apiwrap.cpp +++ b/Telegram/SourceFiles/apiwrap.cpp @@ -44,6 +44,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org #include "storage/storage_user_photos.h" #include "data/data_sparse_ids.h" #include "data/data_search_controller.h" +#include "data/data_channel_admins.h" namespace { @@ -1792,24 +1793,12 @@ void ApiWrap::parseChannelParticipants( void ApiWrap::refreshChannelAdmins( not_null channel, const QVector &participants) { - auto changes = base::flat_map(); - auto &admins = channel->mgInfo->admins; - for (auto &participant : participants) { - auto userId = TLHelp::ReadChannelParticipantUserId(participant); - auto admin = (participant.type() == mtpc_channelParticipantAdmin) - || (participant.type() == mtpc_channelParticipantCreator); - if (admin && !admins.contains(userId)) { - admins.insert(userId); - changes.emplace(userId, true); - } else if (!admin && admins.contains(userId)) { - admins.remove(userId); - changes.emplace(userId, false); - } - } - if (!changes.empty()) { - if (auto history = App::historyLoaded(channel)) { - history->applyGroupAdminChanges(changes); - } + Data::ChannelAdminChanges changes(channel); + for (auto &p : participants) { + const auto userId = TLHelp::ReadChannelParticipantUserId(p); + const auto isAdmin = (p.type() == mtpc_channelParticipantAdmin) + || (p.type() == mtpc_channelParticipantCreator); + changes.feed(userId, isAdmin); } } diff --git a/Telegram/SourceFiles/data/data_channel_admins.cpp b/Telegram/SourceFiles/data/data_channel_admins.cpp new file mode 100644 index 000000000..fc3cd4230 --- /dev/null +++ b/Telegram/SourceFiles/data/data_channel_admins.cpp @@ -0,0 +1,48 @@ +/* +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 "data/data_channel_admins.h" + +namespace Data { + +ChannelAdminChanges::ChannelAdminChanges(not_null channel) +: _channel(channel) +, _admins(_channel->mgInfo->admins) { +} + +void ChannelAdminChanges::feed(UserId userId, bool isAdmin) { + if (isAdmin && !_admins.contains(userId)) { + _admins.insert(userId); + _changes.emplace(userId, true); + } else if (!isAdmin && _admins.contains(userId)) { + _admins.remove(userId); + _changes.emplace(userId, false); + } +} + +ChannelAdminChanges::~ChannelAdminChanges() { + if (!_changes.empty()) { + if (auto history = App::historyLoaded(_channel)) { + history->applyGroupAdminChanges(_changes); + } + } +} + +} // namespace Data diff --git a/Telegram/SourceFiles/data/data_channel_admins.h b/Telegram/SourceFiles/data/data_channel_admins.h new file mode 100644 index 000000000..86b6590cc --- /dev/null +++ b/Telegram/SourceFiles/data/data_channel_admins.h @@ -0,0 +1,40 @@ +/* +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 +*/ +#pragma once + +namespace Data { + +class ChannelAdminChanges { +public: + ChannelAdminChanges(not_null channel); + + void feed(UserId userId, bool isAdmin); + + ~ChannelAdminChanges(); + +private: + not_null _channel; + base::flat_set &_admins; + base::flat_map _changes; + +}; + +} // namespace Data diff --git a/Telegram/SourceFiles/data/data_peer.cpp b/Telegram/SourceFiles/data/data_peer.cpp index edbaa948b..12a61010b 100644 --- a/Telegram/SourceFiles/data/data_peer.cpp +++ b/Telegram/SourceFiles/data/data_peer.cpp @@ -23,6 +23,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org #include #include #include "data/data_peer_values.h" +#include "data/data_channel_admins.h" #include "lang/lang_keys.h" #include "observer_peer.h" #include "mainwidget.h" @@ -863,6 +864,8 @@ void ChannelData::applyEditAdmin(not_null user, const MTPChannelAdmin setRestrictedCount(restrictedCount() - 1); } } + + auto userId = peerToUser(user->id); auto it = mgInfo->lastAdmins.find(user); if (newRights.c_channelAdminRights().vflags.v != 0) { auto lastAdmin = MegagroupInfo::Admin { newRights }; @@ -873,6 +876,7 @@ void ChannelData::applyEditAdmin(not_null user, const MTPChannelAdmin } else { it->second = lastAdmin; } + Data::ChannelAdminChanges(this).feed(userId, true); } else { if (it != mgInfo->lastAdmins.cend()) { mgInfo->lastAdmins.erase(it); @@ -880,6 +884,7 @@ void ChannelData::applyEditAdmin(not_null user, const MTPChannelAdmin setAdminsCount(adminsCount() - 1); } } + Data::ChannelAdminChanges(this).feed(userId, false); } } if (oldRights.c_channelAdminRights().vflags.v && !newRights.c_channelAdminRights().vflags.v) { @@ -904,7 +909,8 @@ void ChannelData::applyEditBanned(not_null user, const MTPChannelBann auto isKicked = (newRights.c_channelBannedRights().vflags.v & MTPDchannelBannedRights::Flag::f_view_messages); auto isRestricted = !isKicked && (newRights.c_channelBannedRights().vflags.v != 0); if (mgInfo) { - if (mgInfo->lastAdmins.contains(user)) { // If rights are empty - still remove admin? TODO check + // If rights are empty - still remove admin? TODO check + if (mgInfo->lastAdmins.contains(user)) { mgInfo->lastAdmins.remove(user); if (adminsCount() > 1) { setAdminsCount(adminsCount() - 1); @@ -949,6 +955,7 @@ void ChannelData::applyEditBanned(not_null user, const MTPChannelBann Auth().data().removeMegagroupParticipant(this, user); } } + Data::ChannelAdminChanges(this).feed(peerToUser(user->id), false); } else { if (isKicked && membersCount() > 1) { setMembersCount(membersCount() - 1); @@ -1145,6 +1152,9 @@ void ChannelData::setAdminRights(const MTPChannelAdminRights &rights) { } else { mgInfo->lastAdmins.remove(App::self()); } + + auto amAdmin = hasAdminRights() || amCreator(); + Data::ChannelAdminChanges(this).feed(Auth().userId(), amAdmin); } Notify::peerUpdatedDelayed(this, UpdateFlag::ChannelRightsChanged | UpdateFlag::AdminsChanged | UpdateFlag::BannedUsersChanged); } @@ -1163,6 +1173,7 @@ void ChannelData::setRestrictedRights(const MTPChannelBannedRights &rights) { mgInfo->lastRestricted.emplace(App::self(), me); } mgInfo->lastAdmins.remove(App::self()); + Data::ChannelAdminChanges(this).feed(Auth().userId(), false); } else { mgInfo->lastRestricted.remove(App::self()); } diff --git a/Telegram/SourceFiles/history/history.cpp b/Telegram/SourceFiles/history/history.cpp index 8279a4c52..89750ac29 100644 --- a/Telegram/SourceFiles/history/history.cpp +++ b/Telegram/SourceFiles/history/history.cpp @@ -37,6 +37,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org #include "calls/calls_instance.h" #include "storage/storage_facade.h" #include "storage/storage_shared_media.h" +#include "data/data_channel_admins.h" namespace { @@ -1025,6 +1026,7 @@ HistoryItem *History::createItem(const MTPMessage &msg, bool applyServiceAction, mgInfo->botStatus = -1; } } + Data::ChannelAdminChanges(megagroup).feed(uid, false); } } break; diff --git a/Telegram/gyp/telegram_sources.txt b/Telegram/gyp/telegram_sources.txt index b9ecb3df3..d653d341a 100644 --- a/Telegram/gyp/telegram_sources.txt +++ b/Telegram/gyp/telegram_sources.txt @@ -151,6 +151,8 @@ <(src_loc)/core/version.h <(src_loc)/data/data_abstract_structure.cpp <(src_loc)/data/data_abstract_structure.h +<(src_loc)/data/data_channel_admins.cpp +<(src_loc)/data/data_channel_admins.h <(src_loc)/data/data_document.cpp <(src_loc)/data/data_document.h <(src_loc)/data/data_drafts.cpp