From 5e69f957f355798a8d0893eac70629ba91248799 Mon Sep 17 00:00:00 2001 From: levlam Date: Fri, 22 Mar 2019 15:23:44 +0300 Subject: [PATCH] Add get_cha{t,nnel}_permissions. GitOrigin-RevId: 7d00023ed7497f89cc3895c9a1d4f267f7332922 --- td/telegram/ContactsManager.cpp | 34 +++++++++++++++++++++++++++++++++ td/telegram/ContactsManager.h | 4 ++++ td/telegram/DialogParticipant.h | 6 ++++++ td/telegram/MessageContent.cpp | 2 +- 4 files changed, 45 insertions(+), 1 deletion(-) diff --git a/td/telegram/ContactsManager.cpp b/td/telegram/ContactsManager.cpp index 7b6a34330..0bdd951d3 100644 --- a/td/telegram/ContactsManager.cpp +++ b/td/telegram/ContactsManager.cpp @@ -9149,6 +9149,24 @@ DialogParticipantStatus ContactsManager::get_chat_status(const Chat *c) { return c->status; } +DialogParticipantStatus ContactsManager::get_chat_permissions(ChatId chat_id) const { + auto c = get_chat(chat_id); + if (c == nullptr) { + return DialogParticipantStatus::Banned(0); + } + return get_chat_permissions(c); +} + +DialogParticipantStatus ContactsManager::get_chat_permissions(const Chat *c) { + if (!c->is_active) { + return DialogParticipantStatus::Banned(0); + } + if (c->status.is_administrator() || c->status.is_banned()) { + return c->status; + } + return c->status.apply_restrictions(c->default_permissions); +} + bool ContactsManager::is_appointed_chat_administrator(ChatId chat_id) const { auto c = get_chat(chat_id); if (c == nullptr) { @@ -9202,6 +9220,22 @@ DialogParticipantStatus ContactsManager::get_channel_status(const Channel *c) { return c->status; } +DialogParticipantStatus ContactsManager::get_channel_permissions(ChannelId channel_id) const { + auto c = get_channel(channel_id); + if (c == nullptr) { + return DialogParticipantStatus::Banned(0); + } + return get_channel_permissions(c); +} + +DialogParticipantStatus ContactsManager::get_channel_permissions(const Channel *c) { + c->status.update_restrictions(); + if (!c->is_megagroup || c->status.is_administrator() || c->status.is_banned()) { + return c->status; + } + return c->status.apply_restrictions(c->default_permissions); +} + int32 ContactsManager::get_channel_participant_count(ChannelId channel_id) const { auto c = get_channel(channel_id); if (c == nullptr) { diff --git a/td/telegram/ContactsManager.h b/td/telegram/ContactsManager.h index 87e7b79fe..6b5bedfe5 100644 --- a/td/telegram/ContactsManager.h +++ b/td/telegram/ContactsManager.h @@ -366,6 +366,7 @@ class ContactsManager : public Actor { bool get_chat_is_active(ChatId chat_id) const; DialogParticipantStatus get_chat_status(ChatId chat_id) const; + DialogParticipantStatus get_chat_permissions(ChatId chat_id) const; bool is_appointed_chat_administrator(ChatId chat_id) const; FileSourceId get_chat_photo_file_source_id(ChatId chat_id); @@ -384,6 +385,7 @@ class ContactsManager : public Actor { ChannelType get_channel_type(ChannelId channel_id) const; int32 get_channel_date(ChannelId channel_id) const; DialogParticipantStatus get_channel_status(ChannelId channel_id) const; + DialogParticipantStatus get_channel_permissions(ChannelId channel_id) const; int32 get_channel_participant_count(ChannelId channel_id) const; bool get_channel_sign_messages(ChannelId channel_id) const; FileSourceId get_channel_photo_file_source_id(ChannelId channel_id); @@ -855,9 +857,11 @@ class ContactsManager : public Actor { SecretChat *add_secret_chat(SecretChatId secret_chat_id); static DialogParticipantStatus get_chat_status(const Chat *c); + static DialogParticipantStatus get_chat_permissions(const Chat *c); static ChannelType get_channel_type(const Channel *c); static DialogParticipantStatus get_channel_status(const Channel *c); + static DialogParticipantStatus get_channel_permissions(const Channel *c); static bool get_channel_sign_messages(const Channel *c); void set_my_id(UserId my_id); diff --git a/td/telegram/DialogParticipant.h b/td/telegram/DialogParticipant.h index 61cb12342..5eed05d51 100644 --- a/td/telegram/DialogParticipant.h +++ b/td/telegram/DialogParticipant.h @@ -32,6 +32,8 @@ class RestrictedRights { uint32 flags_; + friend class DialogParticipantStatus; + public: RestrictedRights(bool can_send_messages, bool can_send_media, bool can_send_stickers, bool can_send_animations, bool can_send_games, bool can_use_inline_bots, bool can_add_web_page_previews, bool can_send_polls, @@ -187,6 +189,10 @@ class DialogParticipantStatus { RestrictedRights get_restricted_rights() const; + DialogParticipantStatus apply_restrictions(RestrictedRights default_restrictions) const { + return DialogParticipantStatus(type_, flags_ & (~ALL_RESTRICTED_RIGHTS | default_restrictions.flags_), 0); + } + tl_object_ptr get_chat_member_status_object() const; tl_object_ptr get_chat_admin_rights() const; diff --git a/td/telegram/MessageContent.cpp b/td/telegram/MessageContent.cpp index d635865a4..acf8d6a4b 100644 --- a/td/telegram/MessageContent.cpp +++ b/td/telegram/MessageContent.cpp @@ -1487,7 +1487,7 @@ static Result create_input_message_content( WebPageId web_page_id; bool can_add_web_page_previews = dialog_id.get_type() != DialogType::Channel || - td->contacts_manager_->get_channel_status(dialog_id.get_channel_id()).can_add_web_page_previews(); + td->contacts_manager_->get_channel_permissions(dialog_id.get_channel_id()).can_add_web_page_previews(); if (!is_bot && !disable_web_page_preview && can_add_web_page_previews) { web_page_id = td->web_pages_manager_->get_web_page_by_url( get_first_url(input_message_text.text.text, input_message_text.text.entities));