From 1a0e524b49ac2609ed758a160eba0be07b05411d Mon Sep 17 00:00:00 2001 From: John Preston Date: Tue, 26 Sep 2017 16:53:51 +0300 Subject: [PATCH] Optimize third column (create it only once). Before historyPeer and historyPeerCanWrite were independent, so we created a new Info section for both of them changing. Now we use Data::CanWriteValue(peer) and rpl::flatten_latest(). --- Telegram/SourceFiles/app.cpp | 10 +- .../boxes/edit_participant_box.cpp | 4 +- .../boxes/peer_list_controllers.cpp | 2 +- .../chat_helpers/tabbed_selector.cpp | 4 +- Telegram/SourceFiles/data/data_flags.h | 30 ++- Telegram/SourceFiles/data/data_peer.cpp | 33 ++- Telegram/SourceFiles/data/data_peer.h | 91 +++++--- Telegram/SourceFiles/data/data_peer_values.h | 212 ++++++++++++++++++ .../SourceFiles/history/history_message.cpp | 15 +- .../SourceFiles/history/history_widget.cpp | 18 +- .../inline_bots/inline_bot_send_data.cpp | 12 +- .../inline_bots/inline_results_widget.cpp | 2 +- Telegram/SourceFiles/mainwidget.cpp | 18 +- Telegram/SourceFiles/mtproto/type_utils.h | 16 +- .../SourceFiles/window/window_controller.h | 1 - Telegram/gyp/telegram_sources.txt | 1 + 16 files changed, 363 insertions(+), 106 deletions(-) create mode 100644 Telegram/SourceFiles/data/data_peer_values.h diff --git a/Telegram/SourceFiles/app.cpp b/Telegram/SourceFiles/app.cpp index b5395fb58c..0bbe315385 100644 --- a/Telegram/SourceFiles/app.cpp +++ b/Telegram/SourceFiles/app.cpp @@ -394,7 +394,8 @@ namespace { data->inputUser = MTP_inputUser(d.vid, MTP_long(0)); data->setName(lang(lng_deleted), QString(), QString(), QString()); data->setPhoto(MTP_userProfilePhotoEmpty()); - data->setFlags(MTPDuser_ClientFlag::f_inaccessible | 0); + //data->setFlags(MTPDuser_ClientFlag::f_inaccessible | 0); + data->setFlags(MTPDuser::Flag::f_deleted); data->setBotInfoVersion(-1); status = &emptyStatus; data->contact = -1; @@ -412,7 +413,8 @@ namespace { wasContact = data->isContact(); if (minimal) { auto mask = 0 - | MTPDuser_ClientFlag::f_inaccessible; + //| MTPDuser_ClientFlag::f_inaccessible + | MTPDuser::Flag::f_deleted; data->setFlags((data->flags() & ~mask) | (d.vflags.v & mask)); } else { data->setFlags(d.vflags.v); @@ -681,7 +683,7 @@ namespace { } if (d.has_banned_rights()) { cdata->setRestrictedRights(d.vbanned_rights); - } else if (cdata->hasRestrictedRights()) { + } else if (cdata->hasRestrictions()) { cdata->setRestrictedRights(MTP_channelBannedRights(MTP_flags(0), MTP_int(0))); } cdata->inputChannel = MTP_inputChannel(d.vid, d.vaccess_hash); @@ -729,7 +731,7 @@ namespace { if (cdata->hasAdminRights()) { cdata->setAdminRights(MTP_channelAdminRights(MTP_flags(0))); } - if (cdata->hasRestrictedRights()) { + if (cdata->hasRestrictions()) { cdata->setRestrictedRights(MTP_channelBannedRights(MTP_flags(0), MTP_int(0))); } diff --git a/Telegram/SourceFiles/boxes/edit_participant_box.cpp b/Telegram/SourceFiles/boxes/edit_participant_box.cpp index b49eadee91..52a23cb69e 100644 --- a/Telegram/SourceFiles/boxes/edit_participant_box.cpp +++ b/Telegram/SourceFiles/boxes/edit_participant_box.cpp @@ -222,7 +222,7 @@ void EditAdminBox::prepare() { InvokeQueued(this, [this, control] { applyDependencies(control); }); }); if (!channel()->amCreator()) { - if (!(channel()->adminRights().vflags.v & flags)) { + if (!(channel()->adminRights() & flags)) { control->setDisabled(true); // Grey out options that we don't have ourselves. } } @@ -272,7 +272,7 @@ void EditAdminBox::prepare() { } if (!channel()->amCreator()) { // Leave only rights that we have so we could save them. - newFlags &= channel()->adminRights().vflags.v; + newFlags &= channel()->adminRights(); } _saveCallback(_oldRights, MTP_channelAdminRights(MTP_flags(newFlags))); }); diff --git a/Telegram/SourceFiles/boxes/peer_list_controllers.cpp b/Telegram/SourceFiles/boxes/peer_list_controllers.cpp index 9279e61eba..83113261ac 100644 --- a/Telegram/SourceFiles/boxes/peer_list_controllers.cpp +++ b/Telegram/SourceFiles/boxes/peer_list_controllers.cpp @@ -812,7 +812,7 @@ bool AddBotToGroupBoxController::needToCreateRow(not_null peer) const return false; } if (auto group = peer->asMegagroup()) { - if (group->restrictedRights().is_send_games()) { + if (group->restricted(ChannelRestriction::f_send_games)) { return false; } } diff --git a/Telegram/SourceFiles/chat_helpers/tabbed_selector.cpp b/Telegram/SourceFiles/chat_helpers/tabbed_selector.cpp index 690ef2e2ce..36a35ee72d 100644 --- a/Telegram/SourceFiles/chat_helpers/tabbed_selector.cpp +++ b/Telegram/SourceFiles/chat_helpers/tabbed_selector.cpp @@ -535,8 +535,8 @@ void TabbedSelector::setCurrentPeer(PeerData *peer) { void TabbedSelector::checkRestrictedPeer() { if (auto megagroup = _currentPeer ? _currentPeer->asMegagroup() : nullptr) { - auto restricted = (_currentTabType == SelectorTab::Stickers) ? megagroup->restrictedRights().is_send_stickers() : - (_currentTabType == SelectorTab::Gifs) ? megagroup->restrictedRights().is_send_gifs() : false; + auto restricted = (_currentTabType == SelectorTab::Stickers) ? megagroup->restricted(ChannelRestriction::f_send_stickers) : + (_currentTabType == SelectorTab::Gifs) ? megagroup->restricted(ChannelRestriction::f_send_gifs) : false; if (restricted) { if (!_restrictedLabel) { auto text = (_currentTabType == SelectorTab::Stickers) ? lang(lng_restricted_send_stickers) : diff --git a/Telegram/SourceFiles/data/data_flags.h b/Telegram/SourceFiles/data/data_flags.h index 85452821c7..5c78a217bc 100644 --- a/Telegram/SourceFiles/data/data_flags.h +++ b/Telegram/SourceFiles/data/data_flags.h @@ -29,38 +29,44 @@ template < typename FlagsType::Type kEssential = -1> class Flags { public: + using Type = FlagsType; + using Enum = typename Type::Enum; + struct Change { - Change(FlagsType diff, FlagsType value) + using Type = FlagsType; + using Enum = typename Type::Enum; + + Change(Type diff, Type value) : diff(diff) , value(value) { } - FlagsType diff = 0; - FlagsType value = 0; + Type diff = 0; + Type value = 0; }; Flags() = default; - Flags(FlagsType value) : _value(value) { + Flags(Type value) : _value(value) { } - void set(FlagsType which) { + void set(Type which) { if (auto diff = which ^ _value) { _value = which; updated(diff); } } - void add(FlagsType which) { + void add(Type which) { if (auto diff = which & ~_value) { _value |= which; updated(diff); } } - void remove(FlagsType which) { + void remove(Type which) { if (auto diff = which & _value) { _value &= ~which; updated(diff); } } - FlagsType current() const { + Type current() const { return _value; } rpl::producer changes() const { @@ -68,18 +74,18 @@ public: } rpl::producer value() const { return _changes.events_starting_with({ - FlagsType::from_raw(kEssential), + Type::from_raw(kEssential), _value }); } private: - void updated(FlagsType diff) { - if ((diff &= FlagsType::from_raw(kEssential))) { + void updated(Type diff) { + if ((diff &= Type::from_raw(kEssential))) { _changes.fire({ diff, _value }); } } - FlagsType _value = 0; + Type _value = 0; rpl::event_stream _changes; }; diff --git a/Telegram/SourceFiles/data/data_peer.cpp b/Telegram/SourceFiles/data/data_peer.cpp index 0961431571..7d8f555b61 100644 --- a/Telegram/SourceFiles/data/data_peer.cpp +++ b/Telegram/SourceFiles/data/data_peer.cpp @@ -22,6 +22,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org #include #include +#include "data/data_peer_values.h" #include "lang/lang_keys.h" #include "observer_peer.h" #include "mainwidget.h" @@ -609,7 +610,8 @@ void UserData::madeAction(TimeId when) { void UserData::setAccessHash(uint64 accessHash) { if (accessHash == kInaccessibleAccessHashOld) { _accessHash = 0; - _flags.add(MTPDuser_ClientFlag::f_inaccessible | 0); +// _flags.add(MTPDuser_ClientFlag::f_inaccessible | 0); + _flags.add(MTPDuser::Flag::f_deleted); } else { _accessHash = accessHash; } @@ -689,13 +691,7 @@ void ChatData::setInviteLink(const QString &newInviteLink) { ChannelData::ChannelData(const PeerId &id) : PeerData(id) , inputChannel(MTP_inputChannel(MTP_int(bareId()), MTP_long(0))) { - flagsValue() - | rpl::filter([](const Flags::Change &change) { - return change.diff & MTPDchannel::Flag::f_megagroup; - }) - | rpl::map([](const Flags::Change &change) -> bool { - return change.value & MTPDchannel::Flag::f_megagroup; - }) + Data::PeerFlagValue(this, MTPDchannel::Flag::f_megagroup) | rpl::start([this](bool megagroup) { if (megagroup) { if (!mgInfo) { @@ -947,7 +943,7 @@ bool ChannelData::canEditAdmin(not_null user) const { } else if (canNotEditLastAdmin(user)) { return false; } - return adminRights().is_add_admins(); + return adminRights() & AdminRight::f_add_admins; } bool ChannelData::canRestrictUser(not_null user) const { @@ -958,18 +954,18 @@ bool ChannelData::canRestrictUser(not_null user) const { } else if (canNotEditLastAdmin(user)) { return false; } - return adminRights().is_ban_users(); + return adminRights() & AdminRight::f_ban_users; } void ChannelData::setAdminRights(const MTPChannelAdminRights &rights) { - if (rights.c_channelAdminRights().vflags.v == _adminRights.c_channelAdminRights().vflags.v) { + if (rights.c_channelAdminRights().vflags.v == adminRights()) { return; } - _adminRights = rights; + _adminRights.set(rights.c_channelAdminRights().vflags.v); if (isMegagroup()) { if (hasAdminRights()) { if (!amCreator()) { - auto me = MegagroupInfo::Admin { _adminRights }; + auto me = MegagroupInfo::Admin { rights }; me.canEdit = false; mgInfo->lastAdmins.insert(App::self(), me); } @@ -982,15 +978,16 @@ void ChannelData::setAdminRights(const MTPChannelAdminRights &rights) { } void ChannelData::setRestrictedRights(const MTPChannelBannedRights &rights) { - if (rights.c_channelBannedRights().vflags.v == _restrictedRights.c_channelBannedRights().vflags.v - && rights.c_channelBannedRights().vuntil_date.v == _restrictedRights.c_channelBannedRights().vuntil_date.v) { + if (rights.c_channelBannedRights().vflags.v == restrictions() + && rights.c_channelBannedRights().vuntil_date.v == _restrictedUntill) { return; } - _restrictedRights = rights; + _restrictedUntill = rights.c_channelBannedRights().vuntil_date.v; + _restrictions.set(rights.c_channelBannedRights().vflags.v); if (isMegagroup()) { - if (hasRestrictedRights()) { + if (hasRestrictions()) { if (!amCreator()) { - auto me = MegagroupInfo::Restricted { _restrictedRights }; + auto me = MegagroupInfo::Restricted { rights }; mgInfo->lastRestricted.insert(App::self(), me); } mgInfo->lastAdmins.remove(App::self()); diff --git a/Telegram/SourceFiles/data/data_peer.h b/Telegram/SourceFiles/data/data_peer.h index 2338fab8ac..5a78801015 100644 --- a/Telegram/SourceFiles/data/data_peer.h +++ b/Telegram/SourceFiles/data/data_peer.h @@ -425,9 +425,13 @@ public: return flags() & MTPDuser::Flag::f_bot_inline_geo; } bool isInaccessible() const { - return flags() & MTPDuser_ClientFlag::f_inaccessible; + constexpr auto inaccessible = 0 + | MTPDuser::Flag::f_deleted; +// | MTPDuser_ClientFlag::f_inaccessible; + return flags() & inaccessible; } bool canWrite() const { + // Duplicated in Data::CanWriteValue(). return !isInaccessible(); } bool isContact() const { @@ -584,6 +588,7 @@ public: || (adminsEnabled() ? amAdmin() : amIn())); } bool canWrite() const { + // Duplicated in Data::CanWriteValue(). return !isDeactivated() && amIn(); } bool haveLeft() const { @@ -752,6 +757,11 @@ struct MegagroupInfo { }; +using ChannelAdminRight = MTPDchannelAdminRights::Flag; +using ChannelRestriction = MTPDchannelBannedRights::Flag; +using ChannelAdminRights = MTPDchannelAdminRights::Flags; +using ChannelRestrictions = MTPDchannelBannedRights::Flags; + class ChannelData : public PeerData { public: static constexpr auto kEssentialFlags = 0 @@ -906,63 +916,83 @@ public: bool amCreator() const { return flags() & MTPDchannel::Flag::f_creator; } - const MTPChannelAdminRights &adminRightsBoxed() const { - return _adminRights; + + using AdminRight = ChannelAdminRight; + using Restriction = ChannelRestriction; + using AdminRights = ChannelAdminRights; + using Restrictions = ChannelRestrictions; + using AdminRightFlags = Data::Flags; + using RestrictionFlags = Data::Flags; + AdminRights adminRights() const { + return _adminRights.current(); } - const MTPDchannelAdminRights &adminRights() const { - return _adminRights.c_channelAdminRights(); + rpl::producer adminRightsValue() const { + return _adminRights.value(); } void setAdminRights(const MTPChannelAdminRights &rights); bool hasAdminRights() const { - return (adminRights().vflags.v != 0); + return (adminRights() != 0); } - const MTPChannelBannedRights &restrictedRightsBoxed() const { - return _restrictedRights; + Restrictions restrictions() const { + return _restrictions.current(); } - const MTPDchannelBannedRights &restrictedRights() const { - return _restrictedRights.c_channelBannedRights(); + rpl::producer restrictionsValue() const { + return _restrictions.value(); + } + bool restricted(Restriction right) const { + return restrictions() & right; + } + TimeId restrictedUntil() const { + return _restrictedUntill; } void setRestrictedRights(const MTPChannelBannedRights &rights); - bool hasRestrictedRights() const { - return (restrictedRights().vflags.v != 0); + bool hasRestrictions() const { + return (restrictions() != 0); } - bool hasRestrictedRights(int32 now) const { - return hasRestrictedRights() - && (restrictedRights().vuntil_date.v > now); + bool hasRestrictions(TimeId now) const { + return hasRestrictions() + && (restrictedUntil() > now); } bool canBanMembers() const { - return adminRights().is_ban_users() || amCreator(); + return (adminRights() & AdminRight::f_ban_users) + || amCreator(); } bool canEditMessages() const { - return adminRights().is_edit_messages() || amCreator(); + return (adminRights() & AdminRight::f_edit_messages) + || amCreator(); } bool canDeleteMessages() const { - return adminRights().is_delete_messages() || amCreator(); + return (adminRights() & AdminRight::f_delete_messages) + || amCreator(); } bool anyoneCanAddMembers() const { return (flags() & MTPDchannel::Flag::f_democracy); } bool canAddMembers() const { - return adminRights().is_invite_users() + return (adminRights() & AdminRight::f_invite_users) || amCreator() || (anyoneCanAddMembers() && amIn() - && !hasRestrictedRights()); + && !hasRestrictions()); } bool canAddAdmins() const { - return adminRights().is_add_admins() || amCreator(); + return (adminRights() & AdminRight::f_add_admins) + || amCreator(); } bool canPinMessages() const { - return adminRights().is_pin_messages() || amCreator(); + return (adminRights() & AdminRight::f_pin_messages) + || amCreator(); } bool canPublish() const { - return adminRights().is_post_messages() || amCreator(); + return (adminRights() & AdminRight::f_post_messages) + || amCreator(); } bool canWrite() const { + // Duplicated in Data::CanWriteValue(). return amIn() && (canPublish() || (!isBroadcast() - && !restrictedRights().is_send_messages())); + && !restricted(Restriction::f_send_messages))); } bool canViewMembers() const { return fullFlags() @@ -975,7 +1005,8 @@ public: return (hasAdminRights() || amCreator()); } bool canEditInformation() const { - return adminRights().is_change_info() || amCreator(); + return (adminRights() & AdminRight::f_change_info) + || amCreator(); } bool canEditUsername() const { return amCreator() @@ -999,7 +1030,8 @@ public: return _inviteLink; } bool canHaveInviteLink() const { - return adminRights().is_invite_link() || amCreator(); + return (adminRights() & AdminRight::f_invite_link) + || amCreator(); } int32 inviter = 0; // > 0 - user who invited me to channel, < 0 - not in channel @@ -1069,10 +1101,9 @@ private: int _restrictedCount = 0; int _kickedCount = 0; - MTPChannelAdminRights _adminRights - = MTP_channelAdminRights(MTP_flags(0)); - MTPChannelBannedRights _restrictedRights - = MTP_channelBannedRights(MTP_flags(0), MTP_int(0)); + AdminRightFlags _adminRights; + RestrictionFlags _restrictions; + TimeId _restrictedUntill; QString _restrictionReason; QString _about; diff --git a/Telegram/SourceFiles/data/data_peer_values.h b/Telegram/SourceFiles/data/data_peer_values.h new file mode 100644 index 0000000000..4036c57fb2 --- /dev/null +++ b/Telegram/SourceFiles/data/data_peer_values.h @@ -0,0 +1,212 @@ +/* +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 + +#include +#include +#include +#include "data/data_peer.h" + +namespace Data { + +template +inline auto FlagsValueWithMask( + rpl::producer &&value, + typename ChangeType::Type mask) { + return std::move(value) + | rpl::filter([mask](const ChangeType &change) { + return change.diff & mask; + }) + | rpl::map([mask](const ChangeType &change) { + return change.value & mask; + }); +} + +template +inline auto SingleFlagValue( + rpl::producer &&value, + typename ChangeType::Enum flag) { + return FlagsValueWithMask(std::move(value), flag) + | rpl::map([flag](typename ChangeType::Type value) { + return !!value; + }); +} + +template < + typename PeerType, + typename ChangeType = typename PeerType::Flags::Change> +inline auto PeerFlagsValue(PeerType *peer) { + Expects(peer != nullptr); + return peer->flagsValue(); +} + +template < + typename PeerType, + typename ChangeType = typename PeerType::Flags::Change> +inline auto PeerFlagsValue( + PeerType *peer, + typename PeerType::Flags::Type mask) { + return FlagsValueWithMask(PeerFlagsValue(peer), mask); +} + +template < + typename PeerType, + typename ChangeType = typename PeerType::Flags::Change> +inline auto PeerFlagValue( + PeerType *peer, + typename PeerType::Flags::Enum flag) { + return SingleFlagValue(PeerFlagsValue(peer), flag); +} +// +//inline auto PeerFlagValue( +// UserData *user, +// MTPDuser_ClientFlag flag) { +// return PeerFlagValue(user, static_cast(flag)); +//} + +inline auto PeerFlagValue( + ChatData *chat, + MTPDchat_ClientFlag flag) { + return PeerFlagValue(chat, static_cast(flag)); +} + +inline auto PeerFlagValue( + ChannelData *channel, + MTPDchannel_ClientFlag flag) { + return PeerFlagValue(channel, static_cast(flag)); +} + +template < + typename PeerType, + typename = typename PeerType::FullFlags::Change> +inline auto PeerFullFlagsValue(PeerType *peer) { + Expects(peer != nullptr); + return peer->flagsValue(); +} + +template < + typename PeerType, + typename = typename PeerType::FullFlags::Change> +inline auto PeerFullFlagsValue( + PeerType *peer, + typename PeerType::FullFlags::Type mask) { + return FlagsValueWithMask(PeerFullFlagsValue(peer), mask); +} + +template < + typename PeerType, + typename = typename PeerType::FullFlags::Change> +inline auto PeerFullFlagValue( + PeerType *peer, + typename PeerType::FullFlags::Enum flag) { + return SingleFlagValue(PeerFullFlagsValue(peer), flag); +} + +inline auto AdminRightsValue(not_null channel) { + return channel->adminRightsValue(); +} + +inline auto AdminRightsValue( + not_null channel, + MTPDchannelAdminRights::Flags mask) { + return FlagsValueWithMask(AdminRightsValue(channel), mask); +} + +inline auto AdminRightValue( + not_null channel, + MTPDchannelAdminRights::Flag flag) { + return SingleFlagValue(AdminRightsValue(channel), flag); +} + +inline auto RestrictionsValue(not_null channel) { + return channel->restrictionsValue(); +} + +inline auto RestrictionsValue( + not_null channel, + MTPDchannelBannedRights::Flags mask) { + return FlagsValueWithMask(RestrictionsValue(channel), mask); +} + +inline auto RestrictionValue( + not_null channel, + MTPDchannelBannedRights::Flag flag) { + return SingleFlagValue(RestrictionsValue(channel), flag); +} + +inline auto CanWriteValue(UserData *user) { + using namespace rpl::mappers; + return PeerFlagValue(user, MTPDuser::Flag::f_deleted) + | rpl::map(!$1); +} + +inline auto CanWriteValue(ChatData *chat) { + using namespace rpl::mappers; + auto mask = 0 + | MTPDchat::Flag::f_deactivated + | MTPDchat_ClientFlag::f_forbidden + | MTPDchat::Flag::f_left + | MTPDchat::Flag::f_kicked; + return PeerFlagsValue(chat, mask) + | rpl::map(!$1); +} + +inline rpl::producer CanWriteValue(ChannelData *channel) { + auto flagsMask = 0 + | MTPDchannel::Flag::f_left + | MTPDchannel_ClientFlag::f_forbidden + | MTPDchannel::Flag::f_creator + | MTPDchannel::Flag::f_broadcast; + return rpl::combine( + PeerFlagsValue(channel, flagsMask), + AdminRightValue( + channel, + MTPDchannelAdminRights::Flag::f_post_messages), + RestrictionValue( + channel, + MTPDchannelBannedRights::Flag::f_send_messages), + []( + MTPDchannel::Flags flags, + bool postMessagesRight, + bool sendMessagesRestriction) { + auto notAmInFlags = 0 + | MTPDchannel::Flag::f_left + | MTPDchannel_ClientFlag::f_forbidden; + return !(flags & notAmInFlags) + && (postMessagesRight + || (flags & MTPDchannel::Flag::f_creator) + || (!(flags & MTPDchannel::Flag::f_broadcast) + && !sendMessagesRestriction)); + }); +} + +inline rpl::producer CanWriteValue(not_null peer) { + if (auto user = peer->asUser()) { + return CanWriteValue(user); + } else if (auto chat = peer->asChat()) { + return CanWriteValue(chat); + } else if (auto channel = peer->asChannel()) { + return CanWriteValue(channel); + } + Unexpected("Bad peer value in CanWriteValue()"); +} + +} // namespace Data diff --git a/Telegram/SourceFiles/history/history_message.cpp b/Telegram/SourceFiles/history/history_message.cpp index e602465096..602a868977 100644 --- a/Telegram/SourceFiles/history/history_message.cpp +++ b/Telegram/SourceFiles/history/history_message.cpp @@ -91,7 +91,8 @@ MTPDmessage::Flags NewForwardedFlags(not_null peer, UserId from, not_ if (auto media = fwd->getMedia()) { if (media->type() == MediaTypeWebPage) { // Drop web page if we're not allowed to send it. - if (channel->restrictedRights().is_embed_links()) { + if (channel->restricted( + ChannelRestriction::f_embed_links)) { result &= MTPDmessage::Flag::f_media; } } @@ -311,15 +312,15 @@ QString GetErrorTextForForward(not_null peer, const SelectedItemSet & } if (auto megagroup = peer->asMegagroup()) { - if (megagroup->restrictedRights().is_send_media() && HasMediaItems(items)) { + if (megagroup->restricted(ChannelRestriction::f_send_media) && HasMediaItems(items)) { return lang(lng_restricted_send_media); - } else if (megagroup->restrictedRights().is_send_stickers() && HasStickerItems(items)) { + } else if (megagroup->restricted(ChannelRestriction::f_send_stickers) && HasStickerItems(items)) { return lang(lng_restricted_send_stickers); - } else if (megagroup->restrictedRights().is_send_gifs() && HasGifItems(items)) { + } else if (megagroup->restricted(ChannelRestriction::f_send_gifs) && HasGifItems(items)) { return lang(lng_restricted_send_gifs); - } else if (megagroup->restrictedRights().is_send_games() && HasGameItems(items)) { + } else if (megagroup->restricted(ChannelRestriction::f_send_games) && HasGameItems(items)) { return lang(lng_restricted_send_inline); - } else if (megagroup->restrictedRights().is_send_inline() && HasInlineItems(items)) { + } else if (megagroup->restricted(ChannelRestriction::f_send_inline) && HasInlineItems(items)) { return lang(lng_restricted_send_inline); } } @@ -694,7 +695,7 @@ HistoryMessage::HistoryMessage(not_null history, MsgId id, MTPDmessage auto cloneMedia = [this, history, mediaType] { if (mediaType == MediaTypeWebPage) { if (auto channel = history->peer->asChannel()) { - if (channel->restrictedRights().is_embed_links()) { + if (channel->restricted(ChannelRestriction::f_embed_links)) { return false; } } diff --git a/Telegram/SourceFiles/history/history_widget.cpp b/Telegram/SourceFiles/history/history_widget.cpp index a4bd717d64..a22f0433ae 100644 --- a/Telegram/SourceFiles/history/history_widget.cpp +++ b/Telegram/SourceFiles/history/history_widget.cpp @@ -1820,7 +1820,6 @@ void HistoryWidget::showHistory(const PeerId &peerId, MsgId showAtMsgId, bool re App::main()->dlgUpdated(wasHistory ? wasHistory->peer : nullptr, wasMsgId); emit historyShown(_history, _showAtMsgId); - controller()->historyCanWrite = _canSendMessages; controller()->historyPeer = _peer; update(); } @@ -1979,7 +1978,8 @@ bool HistoryWidget::canWriteMessage() const { bool HistoryWidget::isRestrictedWrite() const { if (auto megagroup = _peer ? _peer->asMegagroup() : nullptr) { - return megagroup->restrictedRights().is_send_messages(); + return megagroup->restricted( + ChannelRestriction::f_send_messages); } return false; } @@ -3038,7 +3038,7 @@ void HistoryWidget::step_recording(float64 ms, bool timer) { void HistoryWidget::chooseAttach() { if (!_peer || !_peer->canWrite()) return; if (auto megagroup = _peer->asMegagroup()) { - if (megagroup->restrictedRights().is_send_media()) { + if (megagroup->restricted(ChannelRestriction::f_send_media)) { Ui::show(Box(lang(lng_restricted_send_media))); return; } @@ -3142,7 +3142,7 @@ void HistoryWidget::recordStartCallback() { return; } if (auto megagroup = _peer ? _peer->asMegagroup() : nullptr) { - if (megagroup->restrictedRights().is_send_media()) { + if (megagroup->restricted(ChannelRestriction::f_send_media)) { Ui::show(Box(lang(lng_restricted_send_media))); return; } @@ -4128,7 +4128,7 @@ bool HistoryWidget::confirmSendingFiles(const QStringList &files, CompressConfir bool HistoryWidget::confirmSendingFiles(const SendingFilesLists &lists, CompressConfirm compressed, const QString *addedComment) { if (auto megagroup = _peer ? _peer->asMegagroup() : nullptr) { - if (megagroup->restrictedRights().is_send_media()) { + if (megagroup->restricted(ChannelRestriction::f_send_media)) { Ui::show(Box(lang(lng_restricted_send_media))); return false; } @@ -5141,7 +5141,7 @@ void HistoryWidget::onFieldTabbed() { bool HistoryWidget::onStickerSend(DocumentData *sticker) { if (auto megagroup = _peer ? _peer->asMegagroup() : nullptr) { - if (megagroup->restrictedRights().is_send_stickers()) { + if (megagroup->restricted(ChannelRestriction::f_send_stickers)) { Ui::show( Box(lang(lng_restricted_send_stickers)), LayerOption::KeepOther); @@ -5153,7 +5153,7 @@ bool HistoryWidget::onStickerSend(DocumentData *sticker) { void HistoryWidget::onPhotoSend(PhotoData *photo) { if (auto megagroup = _peer ? _peer->asMegagroup() : nullptr) { - if (megagroup->restrictedRights().is_send_media()) { + if (megagroup->restricted(ChannelRestriction::f_send_media)) { Ui::show( Box(lang(lng_restricted_send_media)), LayerOption::KeepOther); @@ -5769,7 +5769,7 @@ void HistoryWidget::onPreviewParse() { void HistoryWidget::onPreviewCheck() { auto previewRestricted = [this] { if (auto megagroup = _peer ? _peer->asMegagroup() : nullptr) { - if (megagroup->restrictedRights().is_embed_links()) { + if (megagroup->restricted(ChannelRestriction::f_embed_links)) { return true; } } @@ -5917,7 +5917,6 @@ void HistoryWidget::fullPeerUpdated(PeerData *peer) { bool newCanSendMessages = canSendMessages(_peer); if (newCanSendMessages != _canSendMessages) { _canSendMessages = newCanSendMessages; - controller()->historyCanWrite = _canSendMessages; if (!_canSendMessages) { cancelReply(); } @@ -5956,7 +5955,6 @@ void HistoryWidget::handlePeerUpdate() { bool newCanSendMessages = canSendMessages(_peer); if (newCanSendMessages != _canSendMessages) { _canSendMessages = newCanSendMessages; - controller()->historyCanWrite = _canSendMessages; if (!_canSendMessages) { cancelReply(); } diff --git a/Telegram/SourceFiles/inline_bots/inline_bot_send_data.cpp b/Telegram/SourceFiles/inline_bots/inline_bot_send_data.cpp index 2693e134d0..ddc059aa71 100644 --- a/Telegram/SourceFiles/inline_bots/inline_bot_send_data.cpp +++ b/Telegram/SourceFiles/inline_bots/inline_bot_send_data.cpp @@ -48,7 +48,7 @@ UserId viaBotId, MsgId replyToId, const QString &postAuthor, const MTPReplyMarku QString SendDataCommon::getErrorOnSend(const Result *owner, History *history) const { if (auto megagroup = history->peer->asMegagroup()) { - if (megagroup->restrictedRights().is_send_messages()) { + if (megagroup->restricted(ChannelRestriction::f_send_messages)) { return lang(lng_restricted_send_message); } } @@ -96,7 +96,7 @@ UserId viaBotId, MsgId replyToId, const QString &postAuthor, const MTPReplyMarku QString SendPhoto::getErrorOnSend(const Result *owner, History *history) const { if (auto megagroup = history->peer->asMegagroup()) { - if (megagroup->restrictedRights().is_send_media()) { + if (megagroup->restricted(ChannelRestriction::f_send_media)) { return lang(lng_restricted_send_media); } } @@ -111,11 +111,11 @@ UserId viaBotId, MsgId replyToId, const QString &postAuthor, const MTPReplyMarku QString SendFile::getErrorOnSend(const Result *owner, History *history) const { if (auto megagroup = history->peer->asMegagroup()) { - if (megagroup->restrictedRights().is_send_media()) { + if (megagroup->restricted(ChannelRestriction::f_send_media)) { return lang(lng_restricted_send_media); - } else if (megagroup->restrictedRights().is_send_stickers() && (_document->sticker() != nullptr)) { + } else if (megagroup->restricted(ChannelRestriction::f_send_stickers) && (_document->sticker() != nullptr)) { return lang(lng_restricted_send_stickers); - } else if (megagroup->restrictedRights().is_send_gifs() && _document->isAnimation() && !_document->isRoundVideo()) { + } else if (megagroup->restricted(ChannelRestriction::f_send_gifs) && _document->isAnimation() && !_document->isRoundVideo()) { return lang(lng_restricted_send_gifs); } } @@ -130,7 +130,7 @@ void SendGame::addToHistory(const Result *owner, History *history, QString SendGame::getErrorOnSend(const Result *owner, History *history) const { if (auto megagroup = history->peer->asMegagroup()) { - if (megagroup->restrictedRights().is_send_games()) { + if (megagroup->restricted(ChannelRestriction::f_send_games)) { return lang(lng_restricted_send_inline); } } diff --git a/Telegram/SourceFiles/inline_bots/inline_results_widget.cpp b/Telegram/SourceFiles/inline_bots/inline_results_widget.cpp index 8ba96c2ae6..7f93b05768 100644 --- a/Telegram/SourceFiles/inline_bots/inline_results_widget.cpp +++ b/Telegram/SourceFiles/inline_bots/inline_results_widget.cpp @@ -94,7 +94,7 @@ void Inner::visibleTopBottomUpdated( void Inner::checkRestrictedPeer() { if (auto megagroup = _inlineQueryPeer ? _inlineQueryPeer->asMegagroup() : nullptr) { - if (megagroup->restrictedRights().is_send_inline()) { + if (megagroup->restricted(ChannelRestriction::f_send_inline)) { if (!_restrictedLabel) { _restrictedLabel.create(this, lang(lng_restricted_send_inline), Ui::FlatLabel::InitType::Simple, st::stickersRestrictedLabel); _restrictedLabel->show(); diff --git a/Telegram/SourceFiles/mainwidget.cpp b/Telegram/SourceFiles/mainwidget.cpp index a832c31bb9..8ce1fd36b2 100644 --- a/Telegram/SourceFiles/mainwidget.cpp +++ b/Telegram/SourceFiles/mainwidget.cpp @@ -21,10 +21,12 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org #include "mainwidget.h" #include +#include #include "data/data_photo.h" #include "data/data_document.h" #include "data/data_web_page.h" #include "data/data_game.h" +#include "data/data_peer_values.h" #include "styles/style_dialogs.h" #include "styles/style_history.h" #include "ui/special_buttons.h" @@ -174,9 +176,17 @@ MainWidget::MainWidget( subscribe(_controller->floatPlayerAreaUpdated(), [this] { checkFloatPlayerVisibility(); }); - rpl::combine( - _controller->historyPeer.value(), - _controller->historyCanWrite.value()) + + using namespace rpl::mappers; + _controller->historyPeer.value() + | rpl::map([](PeerData *peer) { + auto canWrite = peer + ? Data::CanWriteValue(peer) + : rpl::single(false); + return std::move(canWrite) + | rpl::map(tuple(peer, $1)); + }) + | rpl::flatten_latest() | rpl::start([this](PeerData *peer, bool canWrite) { updateThirdColumnToCurrentPeer(peer, canWrite); }, lifetime()); @@ -759,7 +769,7 @@ bool MainWidget::onSendPaths(const PeerId &peerId) { Ui::show(Box(lang(lng_forward_send_files_cant))); return false; } else if (auto megagroup = peer->asMegagroup()) { - if (megagroup->restrictedRights().is_send_media()) { + if (megagroup->restricted(ChannelRestriction::f_send_media)) { Ui::show(Box(lang(lng_restricted_send_media))); return false; } diff --git a/Telegram/SourceFiles/mtproto/type_utils.h b/Telegram/SourceFiles/mtproto/type_utils.h index def7742c8b..9a36052267 100644 --- a/Telegram/SourceFiles/mtproto/type_utils.h +++ b/Telegram/SourceFiles/mtproto/type_utils.h @@ -122,14 +122,14 @@ enum class MTPDstickerSet_ClientFlag : uint32 { }; DEFINE_MTP_CLIENT_FLAGS(MTPDstickerSet) -enum class MTPDuser_ClientFlag : uint32 { - // forbidden constructor received - f_inaccessible = (1U << 31), - - // update this when adding new client side flags - MIN_FIELD = (1U << 31), -}; -DEFINE_MTP_CLIENT_FLAGS(MTPDuser) +//enum class MTPDuser_ClientFlag : uint32 { +// // forbidden constructor received +// f_inaccessible = (1U << 31), +// +// // update this when adding new client side flags +// MIN_FIELD = (1U << 31), +//}; +//DEFINE_MTP_CLIENT_FLAGS(MTPDuser) enum class MTPDchat_ClientFlag : uint32 { // forbidden constructor received diff --git a/Telegram/SourceFiles/window/window_controller.h b/Telegram/SourceFiles/window/window_controller.h index 4d42248109..4422786828 100644 --- a/Telegram/SourceFiles/window/window_controller.h +++ b/Telegram/SourceFiles/window/window_controller.h @@ -60,7 +60,6 @@ public: // for all histories we show in a window. Once each history is shown // in its own HistoryWidget with its own TopBarWidget this can be removed. rpl::variable historyPeer; - rpl::variable historyCanWrite; void enableGifPauseReason(GifPauseReason reason); void disableGifPauseReason(GifPauseReason reason); diff --git a/Telegram/gyp/telegram_sources.txt b/Telegram/gyp/telegram_sources.txt index e140d4bbe5..136c4f37be 100644 --- a/Telegram/gyp/telegram_sources.txt +++ b/Telegram/gyp/telegram_sources.txt @@ -149,6 +149,7 @@ <(src_loc)/data/data_game.h <(src_loc)/data/data_peer.cpp <(src_loc)/data/data_peer.h +<(src_loc)/data/data_peer_values.h <(src_loc)/data/data_photo.cpp <(src_loc)/data/data_photo.h <(src_loc)/data/data_types.cpp