From c65468f270bb7e73f615038a521cf079aa15ceec Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Wed, 24 Nov 2021 07:25:05 +0300 Subject: [PATCH] Added new class Api::ChatParticipant. --- .../SourceFiles/api/api_chat_participants.cpp | 114 ++++++++++++++++++ .../SourceFiles/api/api_chat_participants.h | 57 ++++++++- 2 files changed, 169 insertions(+), 2 deletions(-) diff --git a/Telegram/SourceFiles/api/api_chat_participants.cpp b/Telegram/SourceFiles/api/api_chat_participants.cpp index 15eda68ee6..d6d4404c29 100644 --- a/Telegram/SourceFiles/api/api_chat_participants.cpp +++ b/Telegram/SourceFiles/api/api_chat_participants.cpp @@ -270,6 +270,120 @@ void ApplyBotsList( } // namespace +ChatParticipant::ChatParticipant( + const MTPChannelParticipant &p, + not_null peer) { + _peer = p.match([](const MTPDchannelParticipantBanned &data) { + return peerFromMTP(data.vpeer()); + }, [](const MTPDchannelParticipantLeft &data) { + return peerFromMTP(data.vpeer()); + }, [](const auto &data) { + return peerFromUser(data.vuser_id()); + }); + + p.match([&](const MTPDchannelParticipantCreator &data) { + _canBeEdited = (peer->session().userPeerId() == _peer); + _type = Type::Creator; + _rights = ChatAdminRightsInfo(data.vadmin_rights()); + _rank = qs(data.vrank().value_or_empty()); + }, [&](const MTPDchannelParticipantAdmin &data) { + _canBeEdited = data.is_can_edit(); + _type = Type::Admin; + _rank = qs(data.vrank().value_or_empty()); + _rights = ChatAdminRightsInfo(data.vadmin_rights()); + _by = peerToUser(peerFromUser(data.vpromoted_by())); + }, [&](const MTPDchannelParticipantSelf &data) { + _type = Type::Member; + _by = peerToUser(peerFromUser(data.vinviter_id())); + }, [&](const MTPDchannelParticipant &data) { + _type = Type::Member; + }, [&](const MTPDchannelParticipantBanned &data) { + _restrictions = ChatRestrictionsInfo(data.vbanned_rights()); + _by = peerToUser(peerFromUser(data.vkicked_by())); + + _type = (_restrictions.flags & ChatRestriction::ViewMessages) + ? Type::Banned + : Type::Restricted; + }, [&](const MTPDchannelParticipantLeft &data) { + _type = Type::Left; + }); +} + +ChatParticipant::ChatParticipant( + Type type, + PeerId peerId, + UserId by, + ChatRestrictionsInfo restrictions, + ChatAdminRightsInfo rights, + bool canBeEdited, + QString rank) +: _type(type) +, _peer(peerId) +, _by(by) +, _canBeEdited(canBeEdited) +, _rank(rank) +, _restrictions(std::move(restrictions)) +, _rights(std::move(rights)) { +} + +void ChatParticipant::tryApplyCreatorTo( + not_null channel) const { + if (isCreator() && isUser()) { + if (const auto info = channel->mgInfo.get()) { + info->creator = channel->owner().userLoaded(userId()); + info->creatorRank = rank(); + } + } +} + +bool ChatParticipant::isUser() const { + return peerIsUser(_peer); +} + +bool ChatParticipant::isCreator() const { + return _type == Type::Creator; +} + +bool ChatParticipant::isCreatorOrAdmin() const { + return _type == Type::Creator || _type == Type::Admin; +} + +bool ChatParticipant::isKicked() const { + return _type == Type::Banned; +} + +bool ChatParticipant::canBeEdited() const { + return _canBeEdited; +} + +UserId ChatParticipant::by() const { + return _by; +} + +PeerId ChatParticipant::id() const { + return _peer; +} + +UserId ChatParticipant::userId() const { + return peerToUser(_peer); +} + +ChatRestrictionsInfo ChatParticipant::restrictions() const { + return _restrictions; +} + +ChatAdminRightsInfo ChatParticipant::rights() const { + return _rights; +} + +ChatParticipant::Type ChatParticipant::type() const { + return _type; +} + +QString ChatParticipant::rank() const { + return _rank; +} + ChatParticipants::ChatParticipants(not_null api) : _api(&api->instance()) { } diff --git a/Telegram/SourceFiles/api/api_chat_participants.h b/Telegram/SourceFiles/api/api_chat_participants.h index be07fad94b..ce4354aa15 100644 --- a/Telegram/SourceFiles/api/api_chat_participants.h +++ b/Telegram/SourceFiles/api/api_chat_participants.h @@ -7,16 +7,69 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #pragma once +#include "data/data_chat_participant_status.h" #include "mtproto/sender.h" #include "base/timer.h" class ApiWrap; class ChannelData; -struct ChatRestrictionsInfo; - namespace Api { +class ChatParticipant final { +public: + enum class Type { + Creator, + Admin, + Member, + Restricted, + Left, + Banned, + }; + + explicit ChatParticipant( + const MTPChannelParticipant &p, + not_null peer); + ChatParticipant( + Type type, + PeerId peerId, + UserId by, + ChatRestrictionsInfo restrictions, + ChatAdminRightsInfo rights, + bool canBeEdited = false, + QString rank = QString()); + + bool isUser() const; + bool isCreator() const; + bool isCreatorOrAdmin() const; + bool isKicked() const; + bool canBeEdited() const; + + UserId by() const; + PeerId id() const; + UserId userId() const; + + ChatRestrictionsInfo restrictions() const; + ChatAdminRightsInfo rights() const; + + Type type() const; + QString rank() const; + + void tryApplyCreatorTo(not_null channel) const; +private: + Type _type = Type::Member; + + PeerId _peer; + UserId _by; // Banned/Restricted/Promoted. + + bool _canBeEdited = false; + + QString _rank; + + ChatRestrictionsInfo _restrictions; + ChatAdminRightsInfo _rights; +}; + class ChatParticipants final { public: using TLMembers = MTPchannels_ChannelParticipants;