Move DialogActionBar to a separate header.

This commit is contained in:
levlam 2021-11-23 00:24:18 +03:00
parent f4d62f9ca4
commit 6b1833a259
5 changed files with 140 additions and 107 deletions

View File

@ -301,6 +301,7 @@ set(TDLIB_SOURCE
td/telegram/DeviceTokenManager.cpp
td/telegram/DhCache.cpp
td/telegram/DialogAction.cpp
td/telegram/DialogActionBar.cpp
td/telegram/DialogAdministrator.cpp
td/telegram/DialogDb.cpp
td/telegram/DialogEventLog.cpp
@ -485,6 +486,7 @@ set(TDLIB_SOURCE
td/telegram/DhCache.h
td/telegram/DhConfig.h
td/telegram/DialogAction.h
td/telegram/DialogActionBar.h
td/telegram/DialogAdministrator.h
td/telegram/DialogDate.h
td/telegram/DialogDb.h

View File

@ -0,0 +1,83 @@
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include "td/telegram/DialogActionBar.h"
namespace td {
unique_ptr<DialogActionBar> DialogActionBar::create(bool can_report_spam, bool can_add_contact, bool can_block_user,
bool can_share_phone_number, bool can_report_location,
bool can_unarchive, int32 distance, bool can_invite_members) {
if (!can_report_spam && !can_add_contact && !can_block_user && !can_share_phone_number && !can_report_location &&
!can_unarchive && distance < 0 && !can_invite_members) {
return nullptr;
}
auto action_bar = make_unique<DialogActionBar>();
action_bar->can_report_spam = can_report_spam;
action_bar->can_add_contact = can_add_contact;
action_bar->can_block_user = can_block_user;
action_bar->can_share_phone_number = can_share_phone_number;
action_bar->can_report_location = can_report_location;
action_bar->can_unarchive = can_unarchive;
action_bar->distance = distance >= 0 ? distance : -1;
action_bar->can_invite_members = can_invite_members;
return action_bar;
}
td_api::object_ptr<td_api::ChatActionBar> DialogActionBar::get_chat_action_bar_object(DialogType dialog_type,
bool hide_unarchive) const {
if (can_report_location) {
CHECK(dialog_type == DialogType::Channel);
CHECK(!can_share_phone_number && !can_block_user && !can_add_contact && !can_report_spam && !can_invite_members);
return td_api::make_object<td_api::chatActionBarReportUnrelatedLocation>();
}
if (can_invite_members) {
CHECK(!can_share_phone_number && !can_block_user && !can_add_contact && !can_report_spam);
return td_api::make_object<td_api::chatActionBarInviteMembers>();
}
if (can_share_phone_number) {
CHECK(dialog_type == DialogType::User);
CHECK(!can_block_user && !can_add_contact && !can_report_spam);
return td_api::make_object<td_api::chatActionBarSharePhoneNumber>();
}
if (hide_unarchive) {
if (can_add_contact) {
return td_api::make_object<td_api::chatActionBarAddContact>();
} else {
return nullptr;
}
}
if (can_block_user) {
CHECK(dialog_type == DialogType::User);
CHECK(can_report_spam && can_add_contact);
return td_api::make_object<td_api::chatActionBarReportAddBlock>(can_unarchive, distance);
}
if (can_add_contact) {
CHECK(dialog_type == DialogType::User);
CHECK(!can_report_spam);
return td_api::make_object<td_api::chatActionBarAddContact>();
}
if (can_report_spam) {
return td_api::make_object<td_api::chatActionBarReportSpam>(can_unarchive);
}
return nullptr;
}
bool operator==(const unique_ptr<DialogActionBar> &lhs, const unique_ptr<DialogActionBar> &rhs) {
if (lhs == nullptr) {
return rhs == nullptr;
}
if (rhs == nullptr) {
return false;
}
return lhs->can_report_spam == rhs->can_report_spam && lhs->can_add_contact == rhs->can_add_contact &&
lhs->can_block_user == rhs->can_block_user && lhs->can_share_phone_number == rhs->can_share_phone_number &&
lhs->can_report_location == rhs->can_report_location && lhs->can_unarchive == rhs->can_unarchive &&
lhs->distance == rhs->distance && lhs->can_invite_members == rhs->can_invite_members;
}
} // namespace td

View File

@ -0,0 +1,37 @@
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#pragma once
#include "td/telegram/DialogId.h"
#include "td/telegram/td_api.h"
#include "td/utils/common.h"
namespace td {
struct DialogActionBar {
int32 distance = -1; // distance to the peer
bool can_report_spam = false;
bool can_add_contact = false;
bool can_block_user = false;
bool can_share_phone_number = false;
bool can_report_location = false;
bool can_unarchive = false;
bool can_invite_members = false;
static unique_ptr<DialogActionBar> create(bool can_report_spam, bool can_add_contact, bool can_block_user,
bool can_share_phone_number, bool can_report_location, bool can_unarchive,
int32 distance, bool can_invite_members);
td_api::object_ptr<td_api::ChatActionBar> get_chat_action_bar_object(DialogType dialog_type,
bool hide_unarchive) const;
};
bool operator==(const unique_ptr<DialogActionBar> &lhs, const unique_ptr<DialogActionBar> &rhs);
} // namespace td

View File

@ -11,6 +11,7 @@
#include "td/telegram/ConfigShared.h"
#include "td/telegram/ContactsManager.h"
#include "td/telegram/Dependencies.h"
#include "td/telegram/DialogActionBar.h"
#include "td/telegram/DialogDb.h"
#include "td/telegram/DialogFilter.h"
#include "td/telegram/DialogFilter.hpp"
@ -5622,10 +5623,10 @@ void MessagesManager::Dialog::parse(ParserT &parser) {
action_bar_can_report_spam = false;
}
if (know_action_bar) {
action_bar =
create_action_bar(action_bar_can_report_spam, action_bar_can_add_contact, action_bar_can_block_user,
action_bar_can_share_phone_number, action_bar_can_report_location, action_bar_can_unarchive,
hide_distance ? -1 : action_bar_distance, action_bar_can_invite_members);
action_bar = DialogActionBar::create(action_bar_can_report_spam, action_bar_can_add_contact,
action_bar_can_block_user, action_bar_can_share_phone_number,
action_bar_can_report_location, action_bar_can_unarchive,
hide_distance ? -1 : action_bar_distance, action_bar_can_invite_members);
}
}
@ -8206,40 +8207,6 @@ void MessagesManager::report_dialog_photo(DialogId dialog_id, FileId file_id, Re
->send(dialog_id, file_id, file_view.remote_location().as_input_photo(), std::move(reason));
}
unique_ptr<MessagesManager::DialogActionBar> MessagesManager::create_action_bar(
bool can_report_spam, bool can_add_contact, bool can_block_user, bool can_share_phone_number,
bool can_report_location, bool can_unarchive, int32 distance, bool can_invite_members) {
if (!can_report_spam && !can_add_contact && !can_block_user && !can_share_phone_number && !can_report_location &&
!can_unarchive && distance < 0 && !can_invite_members) {
return nullptr;
}
auto action_bar = td::make_unique<DialogActionBar>();
action_bar->can_report_spam = can_report_spam;
action_bar->can_add_contact = can_add_contact;
action_bar->can_block_user = can_block_user;
action_bar->can_share_phone_number = can_share_phone_number;
action_bar->can_report_location = can_report_location;
action_bar->can_unarchive = can_unarchive;
action_bar->distance = distance >= 0 ? distance : -1;
action_bar->can_invite_members = can_invite_members;
return action_bar;
}
bool MessagesManager::cmp_dialog_action_bar(const unique_ptr<DialogActionBar> &lhs,
const unique_ptr<DialogActionBar> &rhs) {
if (lhs == nullptr) {
return rhs == nullptr;
}
if (rhs == nullptr) {
return false;
}
return lhs->can_report_spam == rhs->can_report_spam && lhs->can_add_contact == rhs->can_add_contact &&
lhs->can_block_user == rhs->can_block_user && lhs->can_share_phone_number == rhs->can_share_phone_number &&
lhs->can_report_location == rhs->can_report_location && lhs->can_unarchive == rhs->can_unarchive &&
lhs->distance == rhs->distance && lhs->can_invite_members == rhs->can_invite_members;
}
void MessagesManager::on_get_peer_settings(DialogId dialog_id,
tl_object_ptr<telegram_api::peerSettings> &&peer_settings,
bool ignore_privacy_exception) {
@ -8260,11 +8227,11 @@ void MessagesManager::on_get_peer_settings(DialogId dialog_id,
distance = -1;
}
auto action_bar =
create_action_bar(peer_settings->report_spam_, peer_settings->add_contact_, peer_settings->block_contact_,
peer_settings->share_contact_, peer_settings->report_geo_, peer_settings->autoarchived_,
distance, peer_settings->invite_members_);
DialogActionBar::create(peer_settings->report_spam_, peer_settings->add_contact_, peer_settings->block_contact_,
peer_settings->share_contact_, peer_settings->report_geo_, peer_settings->autoarchived_,
distance, peer_settings->invite_members_);
if (cmp_dialog_action_bar(d->action_bar, action_bar)) {
if (d->action_bar == action_bar) {
if (!d->know_action_bar || d->need_repair_action_bar) {
d->know_action_bar = true;
d->need_repair_action_bar = false;
@ -20137,63 +20104,25 @@ td_api::object_ptr<td_api::ChatType> MessagesManager::get_chat_type_object(Dialo
}
}
td_api::object_ptr<td_api::ChatActionBar> MessagesManager::get_chat_action_bar_object(const Dialog *d,
bool hide_unarchive) const {
td_api::object_ptr<td_api::ChatActionBar> MessagesManager::get_chat_action_bar_object(const Dialog *d) const {
CHECK(d != nullptr);
if (d->dialog_id.get_type() == DialogType::SecretChat) {
auto dialog_type = d->dialog_id.get_type();
if (dialog_type == DialogType::SecretChat) {
auto user_id = td_->contacts_manager_->get_secret_chat_user_id(d->dialog_id.get_secret_chat_id());
if (!user_id.is_valid()) {
return nullptr;
}
const Dialog *user_d = get_dialog(DialogId(user_id));
if (user_d == nullptr) {
if (user_d == nullptr || user_d->action_bar == nullptr) {
return nullptr;
}
return get_chat_action_bar_object(user_d, d->folder_id != FolderId::archive());
return user_d->action_bar->get_chat_action_bar_object(DialogType::User, d->folder_id != FolderId::archive());
}
auto action_bar = d->action_bar.get();
if (action_bar == nullptr) {
if (d->action_bar == nullptr) {
return nullptr;
}
if (action_bar->can_report_location) {
CHECK(d->dialog_id.get_type() == DialogType::Channel);
CHECK(!action_bar->can_share_phone_number && !action_bar->can_block_user && !action_bar->can_add_contact &&
!action_bar->can_report_spam && !action_bar->can_invite_members);
return td_api::make_object<td_api::chatActionBarReportUnrelatedLocation>();
}
if (action_bar->can_invite_members) {
CHECK(!action_bar->can_share_phone_number && !action_bar->can_block_user && !action_bar->can_add_contact &&
!action_bar->can_report_spam);
return td_api::make_object<td_api::chatActionBarInviteMembers>();
}
if (action_bar->can_share_phone_number) {
CHECK(d->dialog_id.get_type() == DialogType::User);
CHECK(!action_bar->can_block_user && !action_bar->can_add_contact && !action_bar->can_report_spam);
return td_api::make_object<td_api::chatActionBarSharePhoneNumber>();
}
if (hide_unarchive) {
if (action_bar->can_add_contact) {
return td_api::make_object<td_api::chatActionBarAddContact>();
} else {
return nullptr;
}
}
if (action_bar->can_block_user) {
CHECK(d->dialog_id.get_type() == DialogType::User);
CHECK(action_bar->can_report_spam && action_bar->can_add_contact);
return td_api::make_object<td_api::chatActionBarReportAddBlock>(action_bar->can_unarchive, action_bar->distance);
}
if (action_bar->can_add_contact) {
CHECK(d->dialog_id.get_type() == DialogType::User);
CHECK(!action_bar->can_report_spam);
return td_api::make_object<td_api::chatActionBarAddContact>();
}
if (action_bar->can_report_spam) {
return td_api::make_object<td_api::chatActionBarReportSpam>(action_bar->can_unarchive);
}
return nullptr;
return d->action_bar->get_chat_action_bar_object(dialog_type, false);
}
string MessagesManager::get_dialog_theme_name(const Dialog *d) const {

View File

@ -86,6 +86,7 @@ namespace td {
struct BinlogEvent;
struct Dependencies;
struct DialogActionBar;
class DialogFilter;
class DraftMessage;
struct InputMessageContent;
@ -1154,18 +1155,6 @@ class MessagesManager final : public Actor {
void parse(ParserT &parser);
};
struct DialogActionBar {
int32 distance = -1; // distance to the peer
bool can_report_spam = false;
bool can_add_contact = false;
bool can_block_user = false;
bool can_share_phone_number = false;
bool can_report_location = false;
bool can_unarchive = false;
bool can_invite_members = false;
};
struct Dialog {
DialogId dialog_id;
MessageId last_new_message_id; // identifier of the last known server message received from update, there should be
@ -2561,18 +2550,11 @@ class MessagesManager final : public Actor {
void add_dialog_last_database_message(Dialog *d, unique_ptr<Message> &&last_database_message);
static unique_ptr<DialogActionBar> create_action_bar(bool can_report_spam, bool can_add_contact, bool can_block_user,
bool can_share_phone_number, bool can_report_location,
bool can_unarchive, int32 distance, bool can_invite_members);
static bool cmp_dialog_action_bar(const unique_ptr<DialogActionBar> &lhs, const unique_ptr<DialogActionBar> &rhs);
void fix_dialog_action_bar(Dialog *d);
td_api::object_ptr<td_api::ChatType> get_chat_type_object(DialogId dialog_id) const;
td_api::object_ptr<td_api::ChatActionBar> get_chat_action_bar_object(const Dialog *d,
bool hide_unarchive = false) const;
td_api::object_ptr<td_api::ChatActionBar> get_chat_action_bar_object(const Dialog *d) const;
string get_dialog_theme_name(const Dialog *d) const;