2020-06-02 12:58:53 +02:00
|
|
|
//
|
2024-01-01 01:07:21 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
|
2020-06-02 12:58:53 +02:00
|
|
|
//
|
|
|
|
// 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/DialogFilter.h"
|
|
|
|
|
2023-03-21 14:26:06 +01:00
|
|
|
#include "td/telegram/ContactsManager.h"
|
2020-06-07 17:14:52 +02:00
|
|
|
#include "td/telegram/DialogId.h"
|
2024-01-04 13:26:42 +01:00
|
|
|
#include "td/telegram/DialogManager.h"
|
2022-05-23 19:04:36 +02:00
|
|
|
#include "td/telegram/Global.h"
|
2023-03-21 14:55:07 +01:00
|
|
|
#include "td/telegram/MessagesManager.h"
|
|
|
|
#include "td/telegram/misc.h"
|
2023-03-21 14:26:06 +01:00
|
|
|
#include "td/telegram/Td.h"
|
2020-06-02 12:58:53 +02:00
|
|
|
|
2021-01-01 13:59:53 +01:00
|
|
|
#include "td/utils/algorithm.h"
|
2021-09-21 05:32:34 +02:00
|
|
|
#include "td/utils/emoji.h"
|
2022-03-11 19:38:48 +01:00
|
|
|
#include "td/utils/FlatHashSet.h"
|
2020-06-07 17:14:52 +02:00
|
|
|
#include "td/utils/logging.h"
|
2022-05-23 19:04:36 +02:00
|
|
|
#include "td/utils/misc.h"
|
2020-06-02 12:58:53 +02:00
|
|
|
|
2023-03-24 12:14:37 +01:00
|
|
|
#include <algorithm>
|
2023-03-21 23:24:14 +01:00
|
|
|
#include <utility>
|
|
|
|
|
2020-06-02 12:58:53 +02:00
|
|
|
namespace td {
|
|
|
|
|
2022-05-23 18:04:09 +02:00
|
|
|
int32 DialogFilter::get_max_filter_dialogs() {
|
2023-04-03 16:28:51 +02:00
|
|
|
return narrow_cast<int32>(G()->get_option_integer("chat_folder_chosen_chat_count_max", 100));
|
2022-05-23 18:04:09 +02:00
|
|
|
}
|
|
|
|
|
2022-04-19 16:43:24 +02:00
|
|
|
unique_ptr<DialogFilter> DialogFilter::get_dialog_filter(
|
|
|
|
telegram_api::object_ptr<telegram_api::DialogFilter> filter_ptr, bool with_id) {
|
2023-03-24 13:32:47 +01:00
|
|
|
FlatHashSet<DialogId, DialogIdHash> added_dialog_ids;
|
|
|
|
switch (filter_ptr->get_id()) {
|
|
|
|
case telegram_api::dialogFilter::ID: {
|
|
|
|
auto filter = telegram_api::move_object_as<telegram_api::dialogFilter>(filter_ptr);
|
|
|
|
DialogFilterId dialog_filter_id(filter->id_);
|
|
|
|
if (with_id) {
|
|
|
|
if (!dialog_filter_id.is_valid()) {
|
|
|
|
LOG(ERROR) << "Receive invalid " << to_string(filter);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
dialog_filter_id = DialogFilterId();
|
|
|
|
}
|
|
|
|
auto dialog_filter = make_unique<DialogFilter>();
|
|
|
|
dialog_filter->dialog_filter_id_ = dialog_filter_id;
|
|
|
|
dialog_filter->title_ = std::move(filter->title_);
|
|
|
|
dialog_filter->emoji_ = std::move(filter->emoticon_);
|
|
|
|
dialog_filter->pinned_dialog_ids_ = InputDialogId::get_input_dialog_ids(filter->pinned_peers_, &added_dialog_ids);
|
|
|
|
dialog_filter->included_dialog_ids_ =
|
|
|
|
InputDialogId::get_input_dialog_ids(filter->include_peers_, &added_dialog_ids);
|
|
|
|
dialog_filter->excluded_dialog_ids_ =
|
|
|
|
InputDialogId::get_input_dialog_ids(filter->exclude_peers_, &added_dialog_ids);
|
|
|
|
auto flags = filter->flags_;
|
|
|
|
dialog_filter->exclude_muted_ = (flags & telegram_api::dialogFilter::EXCLUDE_MUTED_MASK) != 0;
|
|
|
|
dialog_filter->exclude_read_ = (flags & telegram_api::dialogFilter::EXCLUDE_READ_MASK) != 0;
|
|
|
|
dialog_filter->exclude_archived_ = (flags & telegram_api::dialogFilter::EXCLUDE_ARCHIVED_MASK) != 0;
|
|
|
|
dialog_filter->include_contacts_ = (flags & telegram_api::dialogFilter::CONTACTS_MASK) != 0;
|
|
|
|
dialog_filter->include_non_contacts_ = (flags & telegram_api::dialogFilter::NON_CONTACTS_MASK) != 0;
|
|
|
|
dialog_filter->include_bots_ = (flags & telegram_api::dialogFilter::BOTS_MASK) != 0;
|
|
|
|
dialog_filter->include_groups_ = (flags & telegram_api::dialogFilter::GROUPS_MASK) != 0;
|
|
|
|
dialog_filter->include_channels_ = (flags & telegram_api::dialogFilter::BROADCASTS_MASK) != 0;
|
|
|
|
return dialog_filter;
|
2023-03-22 21:19:32 +01:00
|
|
|
}
|
2023-03-24 13:32:47 +01:00
|
|
|
case telegram_api::dialogFilterChatlist::ID: {
|
|
|
|
auto filter = telegram_api::move_object_as<telegram_api::dialogFilterChatlist>(filter_ptr);
|
|
|
|
DialogFilterId dialog_filter_id(filter->id_);
|
|
|
|
if (with_id) {
|
|
|
|
if (!dialog_filter_id.is_valid()) {
|
|
|
|
LOG(ERROR) << "Receive invalid " << to_string(filter);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
dialog_filter_id = DialogFilterId();
|
|
|
|
}
|
|
|
|
auto dialog_filter = make_unique<DialogFilter>();
|
|
|
|
dialog_filter->dialog_filter_id_ = dialog_filter_id;
|
|
|
|
dialog_filter->title_ = std::move(filter->title_);
|
|
|
|
dialog_filter->emoji_ = std::move(filter->emoticon_);
|
|
|
|
dialog_filter->pinned_dialog_ids_ = InputDialogId::get_input_dialog_ids(filter->pinned_peers_, &added_dialog_ids);
|
|
|
|
dialog_filter->included_dialog_ids_ =
|
|
|
|
InputDialogId::get_input_dialog_ids(filter->include_peers_, &added_dialog_ids);
|
|
|
|
dialog_filter->is_shareable_ = true;
|
2023-04-03 14:06:37 +02:00
|
|
|
dialog_filter->has_my_invites_ = filter->has_my_invites_;
|
2023-03-24 13:32:47 +01:00
|
|
|
return dialog_filter;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
LOG(ERROR) << "Ignore " << to_string(filter_ptr);
|
|
|
|
return nullptr;
|
2020-06-02 12:58:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-21 14:55:07 +01:00
|
|
|
Result<unique_ptr<DialogFilter>> DialogFilter::create_dialog_filter(Td *td, DialogFilterId dialog_filter_id,
|
2023-04-03 16:28:51 +02:00
|
|
|
td_api::object_ptr<td_api::chatFolder> filter) {
|
2023-03-27 18:08:25 +02:00
|
|
|
if (filter == nullptr) {
|
2023-04-03 16:28:51 +02:00
|
|
|
return Status::Error(400, "Chat folder must be non-empty");
|
2023-03-27 18:08:25 +02:00
|
|
|
}
|
2023-03-31 12:34:47 +02:00
|
|
|
string icon_name;
|
|
|
|
if (filter->icon_ != nullptr) {
|
|
|
|
icon_name = std::move(filter->icon_->name_);
|
|
|
|
}
|
|
|
|
if (!clean_input_string(filter->title_) || !clean_input_string(icon_name)) {
|
2023-03-27 18:08:25 +02:00
|
|
|
return Status::Error(400, "Strings must be encoded in UTF-8");
|
|
|
|
}
|
2023-03-21 14:55:07 +01:00
|
|
|
|
|
|
|
auto dialog_filter = make_unique<DialogFilter>();
|
2023-03-23 15:26:24 +01:00
|
|
|
dialog_filter->dialog_filter_id_ = dialog_filter_id;
|
2023-03-21 14:55:07 +01:00
|
|
|
|
|
|
|
FlatHashSet<int64> added_dialog_ids;
|
|
|
|
auto add_chats = [td, &added_dialog_ids](vector<InputDialogId> &input_dialog_ids, const vector<int64> &chat_ids) {
|
|
|
|
for (const auto &chat_id : chat_ids) {
|
2023-04-19 14:30:31 +02:00
|
|
|
if (chat_id == 0 || !added_dialog_ids.insert(chat_id).second) {
|
2023-03-21 14:55:07 +01:00
|
|
|
// do not allow duplicate chat_ids
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-01-04 14:20:15 +01:00
|
|
|
input_dialog_ids.push_back(td->dialog_manager_->get_input_dialog_id(DialogId(chat_id)));
|
2023-03-21 14:55:07 +01:00
|
|
|
}
|
|
|
|
};
|
2023-03-23 15:26:24 +01:00
|
|
|
add_chats(dialog_filter->pinned_dialog_ids_, filter->pinned_chat_ids_);
|
|
|
|
add_chats(dialog_filter->included_dialog_ids_, filter->included_chat_ids_);
|
|
|
|
add_chats(dialog_filter->excluded_dialog_ids_, filter->excluded_chat_ids_);
|
2023-03-21 14:55:07 +01:00
|
|
|
|
|
|
|
constexpr size_t MAX_TITLE_LENGTH = 12; // server-side limit for dialog filter title
|
2023-03-23 15:26:24 +01:00
|
|
|
dialog_filter->title_ = clean_name(std::move(filter->title_), MAX_TITLE_LENGTH);
|
|
|
|
if (dialog_filter->title_.empty()) {
|
2023-03-21 14:55:07 +01:00
|
|
|
return Status::Error(400, "Title must be non-empty");
|
|
|
|
}
|
2023-03-31 12:34:47 +02:00
|
|
|
dialog_filter->emoji_ = get_emoji_by_icon_name(icon_name);
|
|
|
|
if (dialog_filter->emoji_.empty() && !icon_name.empty()) {
|
2023-03-21 14:55:07 +01:00
|
|
|
return Status::Error(400, "Invalid icon name specified");
|
|
|
|
}
|
2023-03-23 15:26:24 +01:00
|
|
|
dialog_filter->exclude_muted_ = filter->exclude_muted_;
|
|
|
|
dialog_filter->exclude_read_ = filter->exclude_read_;
|
|
|
|
dialog_filter->exclude_archived_ = filter->exclude_archived_;
|
|
|
|
dialog_filter->include_contacts_ = filter->include_contacts_;
|
|
|
|
dialog_filter->include_non_contacts_ = filter->include_non_contacts_;
|
|
|
|
dialog_filter->include_bots_ = filter->include_bots_;
|
|
|
|
dialog_filter->include_groups_ = filter->include_groups_;
|
|
|
|
dialog_filter->include_channels_ = filter->include_channels_;
|
2023-03-30 17:23:26 +02:00
|
|
|
dialog_filter->is_shareable_ = filter->is_shareable_;
|
2023-04-03 14:06:37 +02:00
|
|
|
dialog_filter->has_my_invites_ = false;
|
2023-03-21 14:55:07 +01:00
|
|
|
|
|
|
|
TRY_STATUS(dialog_filter->check_limits());
|
|
|
|
dialog_filter->sort_input_dialog_ids(td, "create_dialog_filter");
|
|
|
|
|
2023-03-27 17:46:01 +02:00
|
|
|
Status status;
|
|
|
|
dialog_filter->for_each_dialog(
|
|
|
|
[messages_manager = td->messages_manager_.get(), &status](const InputDialogId &input_dialog_id) {
|
|
|
|
if (status.is_error()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
status = messages_manager->can_add_dialog_to_filter(input_dialog_id.get_dialog_id());
|
|
|
|
});
|
|
|
|
if (status.is_error()) {
|
|
|
|
return std::move(status);
|
|
|
|
}
|
|
|
|
|
2023-03-21 14:55:07 +01:00
|
|
|
return std::move(dialog_filter);
|
|
|
|
}
|
|
|
|
|
2023-03-21 21:21:19 +01:00
|
|
|
void DialogFilter::set_dialog_is_pinned(InputDialogId input_dialog_id, bool is_pinned) {
|
|
|
|
auto dialog_id = input_dialog_id.get_dialog_id();
|
|
|
|
if (is_pinned) {
|
2023-03-23 15:26:24 +01:00
|
|
|
pinned_dialog_ids_.insert(pinned_dialog_ids_.begin(), input_dialog_id);
|
|
|
|
InputDialogId::remove(included_dialog_ids_, dialog_id);
|
|
|
|
InputDialogId::remove(excluded_dialog_ids_, dialog_id);
|
2023-03-21 21:21:19 +01:00
|
|
|
} else {
|
2023-03-23 15:26:24 +01:00
|
|
|
bool is_removed = InputDialogId::remove(pinned_dialog_ids_, dialog_id);
|
2023-03-21 21:21:19 +01:00
|
|
|
CHECK(is_removed);
|
2023-03-23 15:26:24 +01:00
|
|
|
included_dialog_ids_.push_back(input_dialog_id);
|
2023-03-21 21:21:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-22 12:14:21 +01:00
|
|
|
void DialogFilter::set_pinned_dialog_ids(vector<InputDialogId> &&input_dialog_ids) {
|
|
|
|
FlatHashSet<DialogId, DialogIdHash> new_pinned_dialog_ids;
|
|
|
|
for (auto input_dialog_id : input_dialog_ids) {
|
2023-04-19 14:30:31 +02:00
|
|
|
auto dialog_id = input_dialog_id.get_dialog_id();
|
|
|
|
CHECK(dialog_id.is_valid());
|
|
|
|
new_pinned_dialog_ids.insert(dialog_id);
|
2023-03-22 12:14:21 +01:00
|
|
|
}
|
|
|
|
|
2023-03-23 15:26:24 +01:00
|
|
|
auto old_pinned_dialog_ids = std::move(pinned_dialog_ids_);
|
|
|
|
pinned_dialog_ids_ = std::move(input_dialog_ids);
|
2023-03-22 12:14:21 +01:00
|
|
|
auto is_new_pinned = [&new_pinned_dialog_ids](InputDialogId input_dialog_id) {
|
|
|
|
return new_pinned_dialog_ids.count(input_dialog_id.get_dialog_id()) > 0;
|
|
|
|
};
|
|
|
|
td::remove_if(old_pinned_dialog_ids, is_new_pinned);
|
2023-03-23 15:26:24 +01:00
|
|
|
td::remove_if(included_dialog_ids_, is_new_pinned);
|
|
|
|
td::remove_if(excluded_dialog_ids_, is_new_pinned);
|
|
|
|
append(included_dialog_ids_, old_pinned_dialog_ids);
|
2023-03-22 12:14:21 +01:00
|
|
|
}
|
|
|
|
|
2023-03-21 21:21:19 +01:00
|
|
|
void DialogFilter::include_dialog(InputDialogId input_dialog_id) {
|
2023-03-23 15:26:24 +01:00
|
|
|
included_dialog_ids_.push_back(input_dialog_id);
|
|
|
|
InputDialogId::remove(excluded_dialog_ids_, input_dialog_id.get_dialog_id());
|
2023-03-21 21:21:19 +01:00
|
|
|
}
|
|
|
|
|
2020-06-02 12:58:53 +02:00
|
|
|
void DialogFilter::remove_secret_chat_dialog_ids() {
|
|
|
|
auto remove_secret_chats = [](vector<InputDialogId> &input_dialog_ids) {
|
|
|
|
td::remove_if(input_dialog_ids, [](InputDialogId input_dialog_id) {
|
|
|
|
return input_dialog_id.get_dialog_id().get_type() == DialogType::SecretChat;
|
|
|
|
});
|
|
|
|
};
|
2023-03-23 15:26:24 +01:00
|
|
|
remove_secret_chats(pinned_dialog_ids_);
|
|
|
|
remove_secret_chats(included_dialog_ids_);
|
|
|
|
remove_secret_chats(excluded_dialog_ids_);
|
2020-06-02 12:58:53 +02:00
|
|
|
}
|
|
|
|
|
2023-03-21 21:20:33 +01:00
|
|
|
void DialogFilter::remove_dialog_id(DialogId dialog_id) {
|
2023-03-23 15:26:24 +01:00
|
|
|
InputDialogId::remove(pinned_dialog_ids_, dialog_id);
|
|
|
|
InputDialogId::remove(included_dialog_ids_, dialog_id);
|
|
|
|
InputDialogId::remove(excluded_dialog_ids_, dialog_id);
|
2023-03-21 21:20:33 +01:00
|
|
|
}
|
|
|
|
|
2020-06-02 12:58:53 +02:00
|
|
|
bool DialogFilter::is_empty(bool for_server) const {
|
2023-03-23 15:26:24 +01:00
|
|
|
if (include_contacts_ || include_non_contacts_ || include_bots_ || include_groups_ || include_channels_) {
|
2020-06-02 12:58:53 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (for_server) {
|
|
|
|
vector<InputDialogId> empty_input_dialog_ids;
|
2023-03-23 15:26:24 +01:00
|
|
|
return InputDialogId::are_equivalent(pinned_dialog_ids_, empty_input_dialog_ids) &&
|
|
|
|
InputDialogId::are_equivalent(included_dialog_ids_, empty_input_dialog_ids);
|
2020-06-02 12:58:53 +02:00
|
|
|
} else {
|
2023-03-23 15:26:24 +01:00
|
|
|
return pinned_dialog_ids_.empty() && included_dialog_ids_.empty();
|
2020-06-02 12:58:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-22 21:05:13 +01:00
|
|
|
bool DialogFilter::is_dialog_pinned(DialogId dialog_id) const {
|
2023-03-23 15:26:24 +01:00
|
|
|
return InputDialogId::contains(pinned_dialog_ids_, dialog_id);
|
2023-03-22 21:05:13 +01:00
|
|
|
}
|
|
|
|
|
2023-03-22 20:30:00 +01:00
|
|
|
bool DialogFilter::is_dialog_included(DialogId dialog_id) const {
|
2023-03-23 15:26:24 +01:00
|
|
|
return InputDialogId::contains(included_dialog_ids_, dialog_id) || is_dialog_pinned(dialog_id);
|
2023-03-22 20:30:00 +01:00
|
|
|
}
|
|
|
|
|
2023-03-22 12:26:05 +01:00
|
|
|
bool DialogFilter::can_include_dialog(DialogId dialog_id) const {
|
2023-03-22 20:30:00 +01:00
|
|
|
if (is_dialog_included(dialog_id)) {
|
2023-03-22 12:26:05 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-03-23 15:26:24 +01:00
|
|
|
if (included_dialog_ids_.size() + pinned_dialog_ids_.size() < narrow_cast<size_t>(get_max_filter_dialogs())) {
|
2023-03-22 12:26:05 +01:00
|
|
|
// fast path
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto new_dialog_filter = make_unique<DialogFilter>(*this);
|
|
|
|
new_dialog_filter->include_dialog(InputDialogId(dialog_id));
|
|
|
|
return new_dialog_filter->check_limits().is_ok();
|
|
|
|
}
|
|
|
|
|
2020-06-02 12:58:53 +02:00
|
|
|
Status DialogFilter::check_limits() const {
|
|
|
|
auto get_server_dialog_count = [](const vector<InputDialogId> &input_dialog_ids) {
|
|
|
|
int32 result = 0;
|
|
|
|
for (auto &input_dialog_id : input_dialog_ids) {
|
|
|
|
if (input_dialog_id.get_dialog_id().get_type() != DialogType::SecretChat) {
|
|
|
|
result++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
2023-03-23 15:26:24 +01:00
|
|
|
auto excluded_server_dialog_count = get_server_dialog_count(excluded_dialog_ids_);
|
|
|
|
auto included_server_dialog_count = get_server_dialog_count(included_dialog_ids_);
|
|
|
|
auto pinned_server_dialog_count = get_server_dialog_count(pinned_dialog_ids_);
|
2020-06-02 12:58:53 +02:00
|
|
|
|
2023-03-23 15:26:24 +01:00
|
|
|
auto excluded_secret_dialog_count = static_cast<int32>(excluded_dialog_ids_.size()) - excluded_server_dialog_count;
|
|
|
|
auto included_secret_dialog_count = static_cast<int32>(included_dialog_ids_.size()) - included_server_dialog_count;
|
|
|
|
auto pinned_secret_dialog_count = static_cast<int32>(pinned_dialog_ids_.size()) - pinned_server_dialog_count;
|
2020-06-02 12:58:53 +02:00
|
|
|
|
2022-05-23 18:04:09 +02:00
|
|
|
auto limit = get_max_filter_dialogs();
|
|
|
|
if (excluded_server_dialog_count > limit || excluded_secret_dialog_count > limit) {
|
2021-04-09 01:35:50 +02:00
|
|
|
return Status::Error(400, "The maximum number of excluded chats exceeded");
|
2020-06-02 12:58:53 +02:00
|
|
|
}
|
2022-05-23 18:04:09 +02:00
|
|
|
if (included_server_dialog_count > limit || included_secret_dialog_count > limit) {
|
2021-04-09 01:35:50 +02:00
|
|
|
return Status::Error(400, "The maximum number of included chats exceeded");
|
2020-06-02 12:58:53 +02:00
|
|
|
}
|
2022-05-23 18:04:09 +02:00
|
|
|
if (included_server_dialog_count + pinned_server_dialog_count > limit ||
|
|
|
|
included_secret_dialog_count + pinned_secret_dialog_count > limit) {
|
2021-04-09 01:35:50 +02:00
|
|
|
return Status::Error(400, "The maximum number of pinned chats exceeded");
|
2020-06-02 12:58:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (is_empty(false)) {
|
|
|
|
return Status::Error(400, "Folder must contain at least 1 chat");
|
|
|
|
}
|
2023-03-24 13:32:47 +01:00
|
|
|
if (is_shareable_) {
|
|
|
|
if (!excluded_dialog_ids_.empty()) {
|
2023-03-30 17:23:26 +02:00
|
|
|
return Status::Error(400, "Shareable folders can't have excluded chats");
|
2023-03-24 13:32:47 +01:00
|
|
|
}
|
|
|
|
if (include_contacts_ || include_non_contacts_ || include_bots_ || include_groups_ || include_channels_ ||
|
|
|
|
exclude_archived_ || exclude_read_ || exclude_muted_) {
|
2023-03-30 17:23:26 +02:00
|
|
|
return Status::Error(400, "Shareable folders can't have chat filters");
|
2023-03-24 13:32:47 +01:00
|
|
|
}
|
2023-04-03 14:06:37 +02:00
|
|
|
} else if (has_my_invites_) {
|
|
|
|
LOG(ERROR) << "Have shareable folder with invite links";
|
2023-03-24 13:32:47 +01:00
|
|
|
}
|
2020-06-02 12:58:53 +02:00
|
|
|
|
2023-03-23 15:26:24 +01:00
|
|
|
if (include_contacts_ && include_non_contacts_ && include_bots_ && include_groups_ && include_channels_ &&
|
|
|
|
exclude_archived_ && !exclude_read_ && !exclude_muted_) {
|
2020-06-02 12:58:53 +02:00
|
|
|
return Status::Error(400, "Folder must be different from the main chat list");
|
|
|
|
}
|
|
|
|
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2023-04-03 14:06:37 +02:00
|
|
|
void DialogFilter::update_from(const DialogFilter &old_filter) {
|
|
|
|
has_my_invites_ = old_filter.has_my_invites_;
|
|
|
|
}
|
|
|
|
|
2020-06-02 12:58:53 +02:00
|
|
|
string DialogFilter::get_emoji_by_icon_name(const string &icon_name) {
|
|
|
|
init_icon_names();
|
|
|
|
auto it = icon_name_to_emoji_.find(icon_name);
|
|
|
|
if (it != icon_name_to_emoji_.end()) {
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
return string();
|
|
|
|
}
|
|
|
|
|
2023-03-31 14:48:57 +02:00
|
|
|
string DialogFilter::get_icon_name_by_emoji(const string &emoji) {
|
2020-06-02 12:58:53 +02:00
|
|
|
init_icon_names();
|
2023-03-31 14:48:57 +02:00
|
|
|
auto it = emoji_to_icon_name_.find(emoji);
|
2020-06-02 12:58:53 +02:00
|
|
|
if (it != emoji_to_icon_name_.end()) {
|
|
|
|
return it->second;
|
|
|
|
}
|
2020-06-05 02:43:13 +02:00
|
|
|
return string();
|
|
|
|
}
|
|
|
|
|
2023-03-31 14:48:57 +02:00
|
|
|
string DialogFilter::get_icon_name() const {
|
|
|
|
return get_icon_name_by_emoji(emoji_);
|
|
|
|
}
|
|
|
|
|
2021-12-24 18:42:49 +01:00
|
|
|
string DialogFilter::get_chosen_or_default_icon_name() const {
|
|
|
|
auto icon_name = get_icon_name();
|
|
|
|
if (!icon_name.empty()) {
|
|
|
|
return icon_name;
|
|
|
|
}
|
|
|
|
|
2023-03-23 15:26:24 +01:00
|
|
|
if (!pinned_dialog_ids_.empty() || !included_dialog_ids_.empty() || !excluded_dialog_ids_.empty()) {
|
2021-12-24 18:42:49 +01:00
|
|
|
return "Custom";
|
|
|
|
}
|
|
|
|
|
2023-03-23 15:26:24 +01:00
|
|
|
if (include_contacts_ || include_non_contacts_) {
|
|
|
|
if (!include_bots_ && !include_groups_ && !include_channels_) {
|
2021-12-24 18:42:49 +01:00
|
|
|
return "Private";
|
|
|
|
}
|
|
|
|
} else {
|
2023-03-23 15:26:24 +01:00
|
|
|
if (!include_bots_ && !include_channels_) {
|
|
|
|
if (!include_groups_) {
|
2021-12-24 18:42:49 +01:00
|
|
|
// just in case
|
|
|
|
return "Custom";
|
|
|
|
}
|
|
|
|
return "Groups";
|
|
|
|
}
|
2023-03-23 15:26:24 +01:00
|
|
|
if (!include_bots_ && !include_groups_) {
|
2021-12-24 18:42:49 +01:00
|
|
|
return "Channels";
|
|
|
|
}
|
2023-03-23 15:26:24 +01:00
|
|
|
if (!include_groups_ && !include_channels_) {
|
2021-12-24 18:42:49 +01:00
|
|
|
return "Bots";
|
|
|
|
}
|
|
|
|
}
|
2023-03-23 15:26:24 +01:00
|
|
|
if (exclude_read_ && !exclude_muted_) {
|
2021-12-24 18:42:49 +01:00
|
|
|
return "Unread";
|
|
|
|
}
|
2023-03-23 15:26:24 +01:00
|
|
|
if (exclude_muted_ && !exclude_read_) {
|
2021-12-24 18:42:49 +01:00
|
|
|
return "Unmuted";
|
|
|
|
}
|
|
|
|
return "Custom";
|
|
|
|
}
|
|
|
|
|
2023-04-03 16:28:51 +02:00
|
|
|
string DialogFilter::get_default_icon_name(const td_api::chatFolder *filter) {
|
2023-03-31 12:34:47 +02:00
|
|
|
if (filter->icon_ != nullptr && !filter->icon_->name_.empty() &&
|
|
|
|
!get_emoji_by_icon_name(filter->icon_->name_).empty()) {
|
|
|
|
return filter->icon_->name_;
|
2020-06-05 02:43:13 +02:00
|
|
|
}
|
2020-06-02 12:58:53 +02:00
|
|
|
|
2020-06-05 02:43:13 +02:00
|
|
|
if (!filter->pinned_chat_ids_.empty() || !filter->included_chat_ids_.empty() || !filter->excluded_chat_ids_.empty()) {
|
2020-06-02 12:58:53 +02:00
|
|
|
return "Custom";
|
|
|
|
}
|
|
|
|
|
2020-06-05 02:43:13 +02:00
|
|
|
if (filter->include_contacts_ || filter->include_non_contacts_) {
|
|
|
|
if (!filter->include_bots_ && !filter->include_groups_ && !filter->include_channels_) {
|
2020-06-02 12:58:53 +02:00
|
|
|
return "Private";
|
|
|
|
}
|
|
|
|
} else {
|
2020-06-05 02:43:13 +02:00
|
|
|
if (!filter->include_bots_ && !filter->include_channels_) {
|
|
|
|
if (!filter->include_groups_) {
|
2020-06-02 12:58:53 +02:00
|
|
|
// just in case
|
|
|
|
return "Custom";
|
|
|
|
}
|
|
|
|
return "Groups";
|
|
|
|
}
|
2020-06-05 02:43:13 +02:00
|
|
|
if (!filter->include_bots_ && !filter->include_groups_) {
|
2020-06-02 12:58:53 +02:00
|
|
|
return "Channels";
|
|
|
|
}
|
2020-06-05 02:43:13 +02:00
|
|
|
if (!filter->include_groups_ && !filter->include_channels_) {
|
2020-06-02 12:58:53 +02:00
|
|
|
return "Bots";
|
|
|
|
}
|
|
|
|
}
|
2020-06-05 02:43:13 +02:00
|
|
|
if (filter->exclude_read_ && !filter->exclude_muted_) {
|
2020-06-02 12:58:53 +02:00
|
|
|
return "Unread";
|
|
|
|
}
|
2020-06-05 02:43:13 +02:00
|
|
|
if (filter->exclude_muted_ && !filter->exclude_read_) {
|
2020-06-02 12:58:53 +02:00
|
|
|
return "Unmuted";
|
|
|
|
}
|
|
|
|
return "Custom";
|
|
|
|
}
|
|
|
|
|
2022-04-19 16:43:24 +02:00
|
|
|
telegram_api::object_ptr<telegram_api::DialogFilter> DialogFilter::get_input_dialog_filter() const {
|
2023-03-24 13:32:47 +01:00
|
|
|
if (is_shareable_) {
|
|
|
|
int32 flags = 0;
|
|
|
|
if (!emoji_.empty()) {
|
|
|
|
flags |= telegram_api::dialogFilterChatlist::EMOTICON_MASK;
|
|
|
|
}
|
2023-04-03 14:06:37 +02:00
|
|
|
if (has_my_invites_) {
|
|
|
|
flags |= telegram_api::dialogFilterChatlist::HAS_MY_INVITES_MASK;
|
|
|
|
}
|
2023-03-24 13:32:47 +01:00
|
|
|
return telegram_api::make_object<telegram_api::dialogFilterChatlist>(
|
2024-02-19 12:07:10 +01:00
|
|
|
flags, false /*ignored*/, dialog_filter_id_.get(), title_, emoji_, 0,
|
2023-03-24 13:32:47 +01:00
|
|
|
InputDialogId::get_input_peers(pinned_dialog_ids_), InputDialogId::get_input_peers(included_dialog_ids_));
|
|
|
|
}
|
2020-06-02 12:58:53 +02:00
|
|
|
int32 flags = 0;
|
2023-03-23 15:26:24 +01:00
|
|
|
if (!emoji_.empty()) {
|
2020-06-02 12:58:53 +02:00
|
|
|
flags |= telegram_api::dialogFilter::EMOTICON_MASK;
|
|
|
|
}
|
2023-03-23 15:26:24 +01:00
|
|
|
if (exclude_muted_) {
|
2020-06-02 12:58:53 +02:00
|
|
|
flags |= telegram_api::dialogFilter::EXCLUDE_MUTED_MASK;
|
|
|
|
}
|
2023-03-23 15:26:24 +01:00
|
|
|
if (exclude_read_) {
|
2020-06-02 12:58:53 +02:00
|
|
|
flags |= telegram_api::dialogFilter::EXCLUDE_READ_MASK;
|
|
|
|
}
|
2023-03-23 15:26:24 +01:00
|
|
|
if (exclude_archived_) {
|
2020-06-02 12:58:53 +02:00
|
|
|
flags |= telegram_api::dialogFilter::EXCLUDE_ARCHIVED_MASK;
|
|
|
|
}
|
2023-03-23 15:26:24 +01:00
|
|
|
if (include_contacts_) {
|
2020-06-02 12:58:53 +02:00
|
|
|
flags |= telegram_api::dialogFilter::CONTACTS_MASK;
|
|
|
|
}
|
2023-03-23 15:26:24 +01:00
|
|
|
if (include_non_contacts_) {
|
2020-06-02 12:58:53 +02:00
|
|
|
flags |= telegram_api::dialogFilter::NON_CONTACTS_MASK;
|
|
|
|
}
|
2023-03-23 15:26:24 +01:00
|
|
|
if (include_bots_) {
|
2020-06-02 12:58:53 +02:00
|
|
|
flags |= telegram_api::dialogFilter::BOTS_MASK;
|
|
|
|
}
|
2023-03-23 15:26:24 +01:00
|
|
|
if (include_groups_) {
|
2020-06-02 12:58:53 +02:00
|
|
|
flags |= telegram_api::dialogFilter::GROUPS_MASK;
|
|
|
|
}
|
2023-03-23 15:26:24 +01:00
|
|
|
if (include_channels_) {
|
2020-06-02 12:58:53 +02:00
|
|
|
flags |= telegram_api::dialogFilter::BROADCASTS_MASK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return telegram_api::make_object<telegram_api::dialogFilter>(
|
|
|
|
flags, false /*ignored*/, false /*ignored*/, false /*ignored*/, false /*ignored*/, false /*ignored*/,
|
2024-02-19 12:07:10 +01:00
|
|
|
false /*ignored*/, false /*ignored*/, false /*ignored*/, dialog_filter_id_.get(), title_, emoji_, 0,
|
2023-03-23 15:26:24 +01:00
|
|
|
InputDialogId::get_input_peers(pinned_dialog_ids_), InputDialogId::get_input_peers(included_dialog_ids_),
|
|
|
|
InputDialogId::get_input_peers(excluded_dialog_ids_));
|
2020-06-02 12:58:53 +02:00
|
|
|
}
|
|
|
|
|
2023-04-03 16:28:51 +02:00
|
|
|
td_api::object_ptr<td_api::chatFolder> DialogFilter::get_chat_folder_object(
|
2023-03-21 23:00:54 +01:00
|
|
|
const vector<DialogId> &unknown_dialog_ids) const {
|
|
|
|
auto get_chat_ids = [unknown_dialog_ids](const vector<InputDialogId> &input_dialog_ids) {
|
|
|
|
vector<int64> chat_ids;
|
|
|
|
chat_ids.reserve(input_dialog_ids.size());
|
|
|
|
for (auto &input_dialog_id : input_dialog_ids) {
|
|
|
|
auto dialog_id = input_dialog_id.get_dialog_id();
|
|
|
|
if (!td::contains(unknown_dialog_ids, dialog_id)) {
|
|
|
|
chat_ids.push_back(dialog_id.get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return chat_ids;
|
|
|
|
};
|
|
|
|
|
2023-04-03 16:28:51 +02:00
|
|
|
td_api::object_ptr<td_api::chatFolderIcon> icon;
|
2023-03-31 12:34:47 +02:00
|
|
|
auto icon_name = get_icon_name();
|
|
|
|
if (!icon_name.empty()) {
|
2023-04-03 16:28:51 +02:00
|
|
|
icon = td_api::make_object<td_api::chatFolderIcon>(icon_name);
|
2023-03-31 12:34:47 +02:00
|
|
|
}
|
2023-04-03 16:28:51 +02:00
|
|
|
return td_api::make_object<td_api::chatFolder>(
|
2023-03-31 12:34:47 +02:00
|
|
|
title_, std::move(icon), is_shareable_, get_chat_ids(pinned_dialog_ids_), get_chat_ids(included_dialog_ids_),
|
2023-03-23 15:26:24 +01:00
|
|
|
get_chat_ids(excluded_dialog_ids_), exclude_muted_, exclude_read_, exclude_archived_, include_contacts_,
|
|
|
|
include_non_contacts_, include_bots_, include_groups_, include_channels_);
|
2023-03-21 23:00:54 +01:00
|
|
|
}
|
|
|
|
|
2023-04-03 16:28:51 +02:00
|
|
|
td_api::object_ptr<td_api::chatFolderInfo> DialogFilter::get_chat_folder_info_object() const {
|
|
|
|
return td_api::make_object<td_api::chatFolderInfo>(
|
|
|
|
dialog_filter_id_.get(), title_, td_api::make_object<td_api::chatFolderIcon>(get_chosen_or_default_icon_name()),
|
2023-04-29 21:04:25 +02:00
|
|
|
is_shareable_, has_my_invites_);
|
2020-06-07 20:51:21 +02:00
|
|
|
}
|
|
|
|
|
2023-03-21 14:12:47 +01:00
|
|
|
void DialogFilter::for_each_dialog(std::function<void(const InputDialogId &)> callback) const {
|
2023-03-23 15:26:24 +01:00
|
|
|
for (auto input_dialog_ids : {&pinned_dialog_ids_, &excluded_dialog_ids_, &included_dialog_ids_}) {
|
2023-03-21 14:12:47 +01:00
|
|
|
for (const auto &input_dialog_id : *input_dialog_ids) {
|
|
|
|
callback(input_dialog_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-02 12:58:53 +02:00
|
|
|
// merges changes from old_server_filter to new_server_filter in old_filter
|
|
|
|
unique_ptr<DialogFilter> DialogFilter::merge_dialog_filter_changes(const DialogFilter *old_filter,
|
|
|
|
const DialogFilter *old_server_filter,
|
|
|
|
const DialogFilter *new_server_filter) {
|
|
|
|
CHECK(old_filter != nullptr);
|
|
|
|
CHECK(old_server_filter != nullptr);
|
|
|
|
CHECK(new_server_filter != nullptr);
|
2023-03-23 15:26:24 +01:00
|
|
|
CHECK(old_filter->dialog_filter_id_ == old_server_filter->dialog_filter_id_);
|
|
|
|
CHECK(old_filter->dialog_filter_id_ == new_server_filter->dialog_filter_id_);
|
|
|
|
auto dialog_filter_id = old_filter->dialog_filter_id_;
|
2020-06-02 12:58:53 +02:00
|
|
|
auto new_filter = make_unique<DialogFilter>(*old_filter);
|
2023-03-23 15:26:24 +01:00
|
|
|
new_filter->dialog_filter_id_ = dialog_filter_id;
|
2020-06-02 12:58:53 +02:00
|
|
|
|
|
|
|
auto merge_ordered_changes = [dialog_filter_id](auto &new_dialog_ids, auto old_server_dialog_ids,
|
|
|
|
auto new_server_dialog_ids) {
|
|
|
|
if (old_server_dialog_ids == new_server_dialog_ids) {
|
|
|
|
LOG(INFO) << "Pinned chats was not changed remotely in " << dialog_filter_id << ", keep local changes";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (InputDialogId::are_equivalent(new_dialog_ids, old_server_dialog_ids)) {
|
|
|
|
LOG(INFO) << "Pinned chats was not changed locally in " << dialog_filter_id << ", keep remote changes";
|
|
|
|
|
|
|
|
size_t kept_server_dialogs = 0;
|
2022-03-11 19:38:48 +01:00
|
|
|
FlatHashSet<DialogId, DialogIdHash> removed_dialog_ids;
|
2020-06-02 12:58:53 +02:00
|
|
|
auto old_it = old_server_dialog_ids.rbegin();
|
|
|
|
for (auto &input_dialog_id : reversed(new_server_dialog_ids)) {
|
|
|
|
auto dialog_id = input_dialog_id.get_dialog_id();
|
|
|
|
while (old_it < old_server_dialog_ids.rend()) {
|
|
|
|
if (old_it->get_dialog_id() == dialog_id) {
|
|
|
|
kept_server_dialogs++;
|
|
|
|
++old_it;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove the dialog, it could be added back later
|
2022-03-11 19:38:48 +01:00
|
|
|
CHECK(old_it->get_dialog_id().is_valid());
|
2020-06-02 12:58:53 +02:00
|
|
|
removed_dialog_ids.insert(old_it->get_dialog_id());
|
|
|
|
++old_it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (old_it < old_server_dialog_ids.rend()) {
|
|
|
|
// remove the dialog, it could be added back later
|
2022-03-11 19:38:48 +01:00
|
|
|
CHECK(old_it->get_dialog_id().is_valid());
|
2020-06-02 12:58:53 +02:00
|
|
|
removed_dialog_ids.insert(old_it->get_dialog_id());
|
|
|
|
++old_it;
|
|
|
|
}
|
|
|
|
td::remove_if(new_dialog_ids, [&removed_dialog_ids](auto input_dialog_id) {
|
|
|
|
return removed_dialog_ids.count(input_dialog_id.get_dialog_id()) > 0;
|
|
|
|
});
|
|
|
|
new_dialog_ids.insert(new_dialog_ids.begin(), new_server_dialog_ids.begin(),
|
|
|
|
new_server_dialog_ids.end() - kept_server_dialogs);
|
|
|
|
} else {
|
|
|
|
LOG(WARNING) << "Ignore remote changes of pinned chats in " << dialog_filter_id;
|
|
|
|
// there are both local and remote changes; ignore remote changes for now
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
auto merge_changes = [](auto &new_dialog_ids, const auto &old_server_dialog_ids, const auto &new_server_dialog_ids) {
|
|
|
|
if (old_server_dialog_ids == new_server_dialog_ids) {
|
|
|
|
// fast path
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// merge additions and deletions from other clients to the local changes
|
2022-03-11 19:38:48 +01:00
|
|
|
FlatHashSet<DialogId, DialogIdHash> deleted_dialog_ids;
|
2021-11-10 18:39:35 +01:00
|
|
|
for (const auto &old_dialog_id : old_server_dialog_ids) {
|
2022-03-11 19:38:48 +01:00
|
|
|
CHECK(old_dialog_id.get_dialog_id().is_valid());
|
2020-06-02 12:58:53 +02:00
|
|
|
deleted_dialog_ids.insert(old_dialog_id.get_dialog_id());
|
|
|
|
}
|
2022-03-11 19:38:48 +01:00
|
|
|
FlatHashSet<DialogId, DialogIdHash> added_dialog_ids;
|
2021-11-10 18:39:35 +01:00
|
|
|
for (const auto &new_dialog_id : new_server_dialog_ids) {
|
2020-06-02 12:58:53 +02:00
|
|
|
auto dialog_id = new_dialog_id.get_dialog_id();
|
|
|
|
if (deleted_dialog_ids.erase(dialog_id) == 0) {
|
|
|
|
added_dialog_ids.insert(dialog_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
vector<InputDialogId> result;
|
2021-11-10 18:39:35 +01:00
|
|
|
for (const auto &input_dialog_id : new_dialog_ids) {
|
2020-06-02 12:58:53 +02:00
|
|
|
// do not add dialog twice
|
|
|
|
added_dialog_ids.erase(input_dialog_id.get_dialog_id());
|
|
|
|
}
|
2021-11-10 18:39:35 +01:00
|
|
|
for (const auto &new_dialog_id : new_server_dialog_ids) {
|
2020-06-02 12:58:53 +02:00
|
|
|
if (added_dialog_ids.count(new_dialog_id.get_dialog_id()) == 1) {
|
|
|
|
result.push_back(new_dialog_id);
|
|
|
|
}
|
|
|
|
}
|
2021-11-10 18:39:35 +01:00
|
|
|
for (const auto &input_dialog_id : new_dialog_ids) {
|
2020-06-02 12:58:53 +02:00
|
|
|
if (deleted_dialog_ids.count(input_dialog_id.get_dialog_id()) == 0) {
|
|
|
|
result.push_back(input_dialog_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
new_dialog_ids = std::move(result);
|
|
|
|
};
|
|
|
|
|
2023-03-23 15:26:24 +01:00
|
|
|
merge_ordered_changes(new_filter->pinned_dialog_ids_, old_server_filter->pinned_dialog_ids_,
|
|
|
|
new_server_filter->pinned_dialog_ids_);
|
|
|
|
merge_changes(new_filter->included_dialog_ids_, old_server_filter->included_dialog_ids_,
|
|
|
|
new_server_filter->included_dialog_ids_);
|
|
|
|
merge_changes(new_filter->excluded_dialog_ids_, old_server_filter->excluded_dialog_ids_,
|
|
|
|
new_server_filter->excluded_dialog_ids_);
|
2020-06-02 12:58:53 +02:00
|
|
|
|
|
|
|
{
|
2022-03-11 19:38:48 +01:00
|
|
|
FlatHashSet<DialogId, DialogIdHash> added_dialog_ids;
|
2020-06-02 12:58:53 +02:00
|
|
|
auto remove_duplicates = [&added_dialog_ids](auto &input_dialog_ids) {
|
|
|
|
td::remove_if(input_dialog_ids, [&added_dialog_ids](auto input_dialog_id) {
|
2022-03-11 19:38:48 +01:00
|
|
|
auto dialog_id = input_dialog_id.get_dialog_id();
|
|
|
|
CHECK(dialog_id.is_valid());
|
|
|
|
return !added_dialog_ids.insert(dialog_id).second;
|
2020-06-02 12:58:53 +02:00
|
|
|
});
|
|
|
|
};
|
2023-03-23 15:26:24 +01:00
|
|
|
remove_duplicates(new_filter->pinned_dialog_ids_);
|
|
|
|
remove_duplicates(new_filter->included_dialog_ids_);
|
|
|
|
remove_duplicates(new_filter->excluded_dialog_ids_);
|
2020-06-02 12:58:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
auto update_value = [](auto &new_value, const auto &old_server_value, const auto &new_server_value) {
|
|
|
|
// if the value was changed from other client and wasn't changed from the current client, update it
|
|
|
|
if (new_server_value != old_server_value && old_server_value == new_value) {
|
|
|
|
new_value = new_server_value;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-03-23 15:26:24 +01:00
|
|
|
update_value(new_filter->exclude_muted_, old_server_filter->exclude_muted_, new_server_filter->exclude_muted_);
|
|
|
|
update_value(new_filter->exclude_read_, old_server_filter->exclude_read_, new_server_filter->exclude_read_);
|
|
|
|
update_value(new_filter->exclude_archived_, old_server_filter->exclude_archived_,
|
|
|
|
new_server_filter->exclude_archived_);
|
|
|
|
update_value(new_filter->include_contacts_, old_server_filter->include_contacts_,
|
|
|
|
new_server_filter->include_contacts_);
|
|
|
|
update_value(new_filter->include_non_contacts_, old_server_filter->include_non_contacts_,
|
|
|
|
new_server_filter->include_non_contacts_);
|
|
|
|
update_value(new_filter->include_bots_, old_server_filter->include_bots_, new_server_filter->include_bots_);
|
|
|
|
update_value(new_filter->include_groups_, old_server_filter->include_groups_, new_server_filter->include_groups_);
|
|
|
|
update_value(new_filter->include_channels_, old_server_filter->include_channels_,
|
|
|
|
new_server_filter->include_channels_);
|
2023-03-24 13:32:47 +01:00
|
|
|
update_value(new_filter->is_shareable_, old_server_filter->is_shareable_, new_server_filter->is_shareable_);
|
2023-04-03 14:06:37 +02:00
|
|
|
update_value(new_filter->has_my_invites_, old_server_filter->has_my_invites_, new_server_filter->has_my_invites_);
|
2023-03-24 13:32:47 +01:00
|
|
|
|
|
|
|
if (new_filter->is_shareable_) {
|
|
|
|
new_filter->exclude_muted_ = false;
|
|
|
|
new_filter->exclude_read_ = false;
|
|
|
|
new_filter->exclude_archived_ = false;
|
|
|
|
new_filter->include_contacts_ = false;
|
|
|
|
new_filter->include_non_contacts_ = false;
|
|
|
|
new_filter->include_bots_ = false;
|
|
|
|
new_filter->include_groups_ = false;
|
|
|
|
new_filter->include_channels_ = false;
|
|
|
|
new_filter->excluded_dialog_ids_.clear();
|
2023-04-03 14:06:37 +02:00
|
|
|
} else {
|
|
|
|
new_filter->has_my_invites_ = false;
|
2023-03-24 13:32:47 +01:00
|
|
|
}
|
2020-06-02 12:58:53 +02:00
|
|
|
|
|
|
|
if (new_filter->check_limits().is_error()) {
|
2023-03-23 15:26:24 +01:00
|
|
|
LOG(WARNING) << "Failed to merge local and remote changes in " << new_filter->dialog_filter_id_
|
2020-06-02 12:58:53 +02:00
|
|
|
<< ", keep only local changes";
|
|
|
|
*new_filter = *old_filter;
|
|
|
|
}
|
|
|
|
|
2023-03-23 15:26:24 +01:00
|
|
|
update_value(new_filter->title_, old_server_filter->title_, new_server_filter->title_);
|
|
|
|
update_value(new_filter->emoji_, old_server_filter->emoji_, new_server_filter->emoji_);
|
2023-03-27 20:40:07 +02:00
|
|
|
|
|
|
|
LOG(INFO) << "Old local filter: " << *old_filter;
|
|
|
|
LOG(INFO) << "Old server filter: " << *old_server_filter;
|
|
|
|
LOG(INFO) << "New server filter: " << *new_server_filter;
|
|
|
|
LOG(INFO) << "New local filter: " << *new_filter;
|
|
|
|
|
2020-06-02 12:58:53 +02:00
|
|
|
return new_filter;
|
|
|
|
}
|
|
|
|
|
2023-03-21 14:26:06 +01:00
|
|
|
void DialogFilter::sort_input_dialog_ids(const Td *td, const char *source) {
|
2023-03-23 15:26:24 +01:00
|
|
|
if (!include_contacts_ && !include_non_contacts_ && !include_bots_ && !include_groups_ && !include_channels_) {
|
|
|
|
excluded_dialog_ids_.clear();
|
2023-03-21 14:26:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
auto sort_input_dialog_ids = [contacts_manager =
|
|
|
|
td->contacts_manager_.get()](vector<InputDialogId> &input_dialog_ids) {
|
|
|
|
std::sort(input_dialog_ids.begin(), input_dialog_ids.end(),
|
|
|
|
[contacts_manager](InputDialogId lhs, InputDialogId rhs) {
|
|
|
|
auto get_order = [contacts_manager](InputDialogId input_dialog_id) {
|
|
|
|
auto dialog_id = input_dialog_id.get_dialog_id();
|
|
|
|
if (dialog_id.get_type() != DialogType::SecretChat) {
|
|
|
|
return dialog_id.get() * 10;
|
|
|
|
}
|
|
|
|
auto user_id = contacts_manager->get_secret_chat_user_id(dialog_id.get_secret_chat_id());
|
|
|
|
return DialogId(user_id).get() * 10 + 1;
|
|
|
|
};
|
|
|
|
return get_order(lhs) < get_order(rhs);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-03-23 15:26:24 +01:00
|
|
|
sort_input_dialog_ids(excluded_dialog_ids_);
|
|
|
|
sort_input_dialog_ids(included_dialog_ids_);
|
2023-03-21 14:26:06 +01:00
|
|
|
|
|
|
|
FlatHashSet<DialogId, DialogIdHash> all_dialog_ids;
|
|
|
|
for_each_dialog([&](const InputDialogId &input_dialog_id) {
|
|
|
|
auto dialog_id = input_dialog_id.get_dialog_id();
|
|
|
|
CHECK(dialog_id.is_valid());
|
|
|
|
LOG_CHECK(all_dialog_ids.insert(dialog_id).second) << source << ' ' << dialog_id << ' ' << *this;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-04-09 20:54:40 +02:00
|
|
|
vector<DialogId> DialogFilter::get_dialogs_for_invite_link(Td *td) {
|
|
|
|
if (!excluded_dialog_ids_.empty() || exclude_muted_ || exclude_read_ || exclude_archived_ || include_contacts_ ||
|
|
|
|
include_non_contacts_ || include_bots_ || include_groups_ || include_channels_) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
vector<DialogId> result;
|
|
|
|
for_each_dialog([&](const InputDialogId &input_dialog_id) {
|
|
|
|
auto dialog_id = input_dialog_id.get_dialog_id();
|
2024-01-04 13:26:42 +01:00
|
|
|
if (!td->dialog_manager_->have_dialog_force(dialog_id, "get_dialogs_for_invite_link")) {
|
2023-04-09 20:54:40 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
bool is_good = false;
|
|
|
|
switch (dialog_id.get_type()) {
|
|
|
|
case DialogType::Chat: {
|
|
|
|
auto chat_id = dialog_id.get_chat_id();
|
|
|
|
// the user can manage invite links in the chat
|
|
|
|
is_good = td->contacts_manager_->get_chat_status(chat_id).can_manage_invite_links();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case DialogType::Channel: {
|
|
|
|
auto channel_id = dialog_id.get_channel_id();
|
|
|
|
// the user can manage invite links in the chat
|
|
|
|
// or the chat is a public chat, which can be joined without administrator approval
|
|
|
|
is_good = td->contacts_manager_->get_channel_status(channel_id).can_manage_invite_links() ||
|
|
|
|
(td->contacts_manager_->is_channel_public(channel_id) &&
|
|
|
|
!td->contacts_manager_->get_channel_join_request(channel_id));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (is_good) {
|
|
|
|
result.push_back(dialog_id);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-03-21 15:02:09 +01:00
|
|
|
vector<FolderId> DialogFilter::get_folder_ids() const {
|
2023-03-27 16:44:13 +02:00
|
|
|
if (exclude_archived_ && pinned_dialog_ids_.empty() && included_dialog_ids_.empty()) {
|
2023-03-21 15:02:09 +01:00
|
|
|
return {FolderId::main()};
|
|
|
|
}
|
|
|
|
return {FolderId::main(), FolderId::archive()};
|
|
|
|
}
|
|
|
|
|
2023-03-27 17:34:25 +02:00
|
|
|
bool DialogFilter::need_dialog(const Td *td, const DialogFilterDialogInfo &dialog_info) const {
|
|
|
|
auto dialog_id = dialog_info.dialog_id_;
|
2023-03-22 20:30:00 +01:00
|
|
|
if (is_dialog_included(dialog_id)) {
|
2023-03-21 23:17:07 +01:00
|
|
|
return true;
|
|
|
|
}
|
2023-03-23 15:26:24 +01:00
|
|
|
if (InputDialogId::contains(excluded_dialog_ids_, dialog_id)) {
|
2023-03-21 23:17:07 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (dialog_id.get_type() == DialogType::SecretChat) {
|
|
|
|
auto user_id = td->contacts_manager_->get_secret_chat_user_id(dialog_id.get_secret_chat_id());
|
|
|
|
if (user_id.is_valid()) {
|
|
|
|
auto user_dialog_id = DialogId(user_id);
|
2023-03-22 20:30:00 +01:00
|
|
|
if (is_dialog_included(user_dialog_id)) {
|
2023-03-21 23:17:07 +01:00
|
|
|
return true;
|
|
|
|
}
|
2023-03-23 15:26:24 +01:00
|
|
|
if (InputDialogId::contains(excluded_dialog_ids_, user_dialog_id)) {
|
2023-03-21 23:17:07 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-03-27 17:34:25 +02:00
|
|
|
if (!dialog_info.has_unread_mentions_) {
|
|
|
|
if (exclude_muted_ && dialog_info.is_muted_) {
|
2023-03-21 23:17:07 +01:00
|
|
|
return false;
|
|
|
|
}
|
2023-03-27 17:34:25 +02:00
|
|
|
if (exclude_read_ && !dialog_info.has_unread_messages_) {
|
2023-03-21 23:17:07 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2023-03-27 17:34:25 +02:00
|
|
|
if (exclude_archived_ && dialog_info.folder_id_ == FolderId::archive()) {
|
2023-03-21 23:17:07 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
switch (dialog_id.get_type()) {
|
|
|
|
case DialogType::User: {
|
|
|
|
auto user_id = dialog_id.get_user_id();
|
|
|
|
if (td->contacts_manager_->is_user_bot(user_id)) {
|
2023-03-23 15:26:24 +01:00
|
|
|
return include_bots_;
|
2023-03-21 23:17:07 +01:00
|
|
|
}
|
|
|
|
if (user_id == td->contacts_manager_->get_my_id() || td->contacts_manager_->is_user_contact(user_id)) {
|
2023-03-23 15:26:24 +01:00
|
|
|
return include_contacts_;
|
2023-03-21 23:17:07 +01:00
|
|
|
}
|
2023-03-23 15:26:24 +01:00
|
|
|
return include_non_contacts_;
|
2023-03-21 23:17:07 +01:00
|
|
|
}
|
|
|
|
case DialogType::Chat:
|
2023-03-23 15:26:24 +01:00
|
|
|
return include_groups_;
|
2023-03-21 23:17:07 +01:00
|
|
|
case DialogType::Channel:
|
2023-03-23 15:26:24 +01:00
|
|
|
return td->contacts_manager_->is_broadcast_channel(dialog_id.get_channel_id()) ? include_channels_
|
|
|
|
: include_groups_;
|
2023-03-21 23:17:07 +01:00
|
|
|
case DialogType::SecretChat: {
|
|
|
|
auto user_id = td->contacts_manager_->get_secret_chat_user_id(dialog_id.get_secret_chat_id());
|
|
|
|
if (td->contacts_manager_->is_user_bot(user_id)) {
|
2023-03-23 15:26:24 +01:00
|
|
|
return include_bots_;
|
2023-03-21 23:17:07 +01:00
|
|
|
}
|
|
|
|
if (td->contacts_manager_->is_user_contact(user_id)) {
|
2023-03-23 15:26:24 +01:00
|
|
|
return include_contacts_;
|
2023-03-21 23:17:07 +01:00
|
|
|
}
|
2023-03-23 15:26:24 +01:00
|
|
|
return include_non_contacts_;
|
2023-03-21 23:17:07 +01:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-21 20:35:00 +01:00
|
|
|
vector<DialogFilterId> DialogFilter::get_dialog_filter_ids(const vector<unique_ptr<DialogFilter>> &dialog_filters,
|
|
|
|
int32 main_dialog_list_position) {
|
2023-03-23 15:26:24 +01:00
|
|
|
auto result = transform(dialog_filters, [](const auto &dialog_filter) { return dialog_filter->dialog_filter_id_; });
|
2023-03-21 20:35:00 +01:00
|
|
|
if (static_cast<size_t>(main_dialog_list_position) <= result.size()) {
|
|
|
|
result.insert(result.begin() + main_dialog_list_position, DialogFilterId());
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-03-21 23:24:14 +01:00
|
|
|
bool DialogFilter::set_dialog_filters_order(vector<unique_ptr<DialogFilter>> &dialog_filters,
|
|
|
|
vector<DialogFilterId> dialog_filter_ids) {
|
|
|
|
auto old_dialog_filter_ids = get_dialog_filter_ids(dialog_filters, -1);
|
|
|
|
if (old_dialog_filter_ids == dialog_filter_ids) {
|
|
|
|
return false;
|
|
|
|
}
|
2023-04-03 16:28:51 +02:00
|
|
|
LOG(INFO) << "Reorder chat folders from " << old_dialog_filter_ids << " to " << dialog_filter_ids;
|
2023-03-21 23:24:14 +01:00
|
|
|
|
|
|
|
if (dialog_filter_ids.size() != old_dialog_filter_ids.size()) {
|
|
|
|
for (auto dialog_filter_id : old_dialog_filter_ids) {
|
|
|
|
if (!td::contains(dialog_filter_ids, dialog_filter_id)) {
|
|
|
|
dialog_filter_ids.push_back(dialog_filter_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CHECK(dialog_filter_ids.size() == old_dialog_filter_ids.size());
|
|
|
|
}
|
|
|
|
if (old_dialog_filter_ids == dialog_filter_ids) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
CHECK(dialog_filter_ids.size() == dialog_filters.size());
|
|
|
|
for (size_t i = 0; i < dialog_filters.size(); i++) {
|
|
|
|
for (size_t j = i; j < dialog_filters.size(); j++) {
|
2023-03-23 15:26:24 +01:00
|
|
|
if (dialog_filters[j]->dialog_filter_id_ == dialog_filter_ids[i]) {
|
2023-03-21 23:24:14 +01:00
|
|
|
if (i != j) {
|
|
|
|
std::swap(dialog_filters[i], dialog_filters[j]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2023-03-23 15:26:24 +01:00
|
|
|
CHECK(dialog_filters[i]->dialog_filter_id_ == dialog_filter_ids[i]);
|
2023-03-21 23:24:14 +01:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-06-02 12:58:53 +02:00
|
|
|
bool DialogFilter::are_similar(const DialogFilter &lhs, const DialogFilter &rhs) {
|
2023-03-23 15:26:24 +01:00
|
|
|
if (lhs.title_ == rhs.title_) {
|
2020-06-02 12:58:53 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!are_flags_equal(lhs, rhs)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<InputDialogId> empty_input_dialog_ids;
|
2023-03-23 15:26:24 +01:00
|
|
|
if (InputDialogId::are_equivalent(lhs.excluded_dialog_ids_, empty_input_dialog_ids) !=
|
|
|
|
InputDialogId::are_equivalent(rhs.excluded_dialog_ids_, empty_input_dialog_ids)) {
|
2020-06-02 12:58:53 +02:00
|
|
|
return false;
|
|
|
|
}
|
2023-03-23 15:26:24 +01:00
|
|
|
if ((InputDialogId::are_equivalent(lhs.pinned_dialog_ids_, empty_input_dialog_ids) &&
|
|
|
|
InputDialogId::are_equivalent(lhs.included_dialog_ids_, empty_input_dialog_ids)) !=
|
|
|
|
(InputDialogId::are_equivalent(rhs.pinned_dialog_ids_, empty_input_dialog_ids) &&
|
|
|
|
InputDialogId::are_equivalent(rhs.included_dialog_ids_, empty_input_dialog_ids))) {
|
2020-06-02 12:58:53 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DialogFilter::are_equivalent(const DialogFilter &lhs, const DialogFilter &rhs) {
|
2023-03-24 13:32:47 +01:00
|
|
|
return lhs.title_ == rhs.title_ && lhs.emoji_ == rhs.emoji_ && lhs.is_shareable_ == rhs.is_shareable_ &&
|
2023-04-03 14:06:37 +02:00
|
|
|
lhs.has_my_invites_ == rhs.has_my_invites_ &&
|
2023-03-23 15:26:24 +01:00
|
|
|
InputDialogId::are_equivalent(lhs.pinned_dialog_ids_, rhs.pinned_dialog_ids_) &&
|
|
|
|
InputDialogId::are_equivalent(lhs.included_dialog_ids_, rhs.included_dialog_ids_) &&
|
|
|
|
InputDialogId::are_equivalent(lhs.excluded_dialog_ids_, rhs.excluded_dialog_ids_) && are_flags_equal(lhs, rhs);
|
2020-06-02 12:58:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
StringBuilder &operator<<(StringBuilder &string_builder, const DialogFilter &filter) {
|
2023-03-23 15:26:24 +01:00
|
|
|
return string_builder << filter.dialog_filter_id_ << " (pinned " << filter.pinned_dialog_ids_ << ", included "
|
|
|
|
<< filter.included_dialog_ids_ << ", excluded " << filter.excluded_dialog_ids_ << ", "
|
|
|
|
<< filter.exclude_muted_ << ' ' << filter.exclude_read_ << ' ' << filter.exclude_archived_
|
|
|
|
<< '/' << filter.include_contacts_ << ' ' << filter.include_non_contacts_ << ' '
|
|
|
|
<< filter.include_bots_ << ' ' << filter.include_groups_ << ' ' << filter.include_channels_
|
|
|
|
<< ')';
|
2020-06-02 12:58:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void DialogFilter::init_icon_names() {
|
|
|
|
static bool is_inited = [&] {
|
|
|
|
vector<string> emojis{"\xF0\x9F\x92\xAC", "\xE2\x9C\x85", "\xF0\x9F\x94\x94",
|
|
|
|
"\xF0\x9F\xA4\x96", "\xF0\x9F\x93\xA2", "\xF0\x9F\x91\xA5",
|
|
|
|
"\xF0\x9F\x91\xA4", "\xF0\x9F\x93\x81", "\xF0\x9F\x93\x8B",
|
|
|
|
"\xF0\x9F\x90\xB1", "\xF0\x9F\x91\x91", "\xE2\xAD\x90\xEF\xB8\x8F",
|
|
|
|
"\xF0\x9F\x8C\xB9", "\xF0\x9F\x8E\xAE", "\xF0\x9F\x8F\xA0",
|
|
|
|
"\xE2\x9D\xA4\xEF\xB8\x8F", "\xF0\x9F\x8E\xAD", "\xF0\x9F\x8D\xB8",
|
|
|
|
"\xE2\x9A\xBD\xEF\xB8\x8F", "\xF0\x9F\x8E\x93", "\xF0\x9F\x93\x88",
|
2022-06-24 13:27:49 +02:00
|
|
|
"\xE2\x9C\x88\xEF\xB8\x8F", "\xF0\x9F\x92\xBC", "\xF0\x9F\x9B\xAB",
|
2022-06-24 16:07:48 +02:00
|
|
|
"\xF0\x9F\x93\x95", "\xF0\x9F\x92\xA1", "\xF0\x9F\x91\x8D",
|
2022-06-24 13:27:49 +02:00
|
|
|
"\xF0\x9F\x92\xB0", "\xF0\x9F\x8E\xB5", "\xF0\x9F\x8E\xA8"};
|
2020-06-02 12:58:53 +02:00
|
|
|
vector<string> icon_names{"All", "Unread", "Unmuted", "Bots", "Channels", "Groups", "Private", "Custom",
|
|
|
|
"Setup", "Cat", "Crown", "Favorite", "Flower", "Game", "Home", "Love",
|
2022-06-24 13:27:49 +02:00
|
|
|
"Mask", "Party", "Sport", "Study", "Trade", "Travel", "Work", "Airplane",
|
|
|
|
"Book", "Light", "Like", "Money", "Note", "Palette"};
|
|
|
|
|
2020-06-02 12:58:53 +02:00
|
|
|
CHECK(emojis.size() == icon_names.size());
|
|
|
|
for (size_t i = 0; i < emojis.size(); i++) {
|
2021-09-20 12:45:49 +02:00
|
|
|
remove_emoji_modifiers_in_place(emojis[i]);
|
2022-06-24 13:27:49 +02:00
|
|
|
bool is_inserted = emoji_to_icon_name_.emplace(emojis[i], icon_names[i]).second &&
|
|
|
|
icon_name_to_emoji_.emplace(icon_names[i], emojis[i]).second;
|
|
|
|
CHECK(is_inserted);
|
2020-06-02 12:58:53 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}();
|
|
|
|
CHECK(is_inited);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DialogFilter::are_flags_equal(const DialogFilter &lhs, const DialogFilter &rhs) {
|
2023-03-23 15:26:24 +01:00
|
|
|
return lhs.exclude_muted_ == rhs.exclude_muted_ && lhs.exclude_read_ == rhs.exclude_read_ &&
|
|
|
|
lhs.exclude_archived_ == rhs.exclude_archived_ && lhs.include_contacts_ == rhs.include_contacts_ &&
|
|
|
|
lhs.include_non_contacts_ == rhs.include_non_contacts_ && lhs.include_bots_ == rhs.include_bots_ &&
|
|
|
|
lhs.include_groups_ == rhs.include_groups_ && lhs.include_channels_ == rhs.include_channels_;
|
2020-06-02 12:58:53 +02:00
|
|
|
}
|
|
|
|
|
2022-02-07 20:41:07 +01:00
|
|
|
FlatHashMap<string, string> DialogFilter::emoji_to_icon_name_;
|
|
|
|
FlatHashMap<string, string> DialogFilter::icon_name_to_emoji_;
|
2020-06-02 12:58:53 +02:00
|
|
|
|
|
|
|
} // namespace td
|