2020-05-21 18:39:34 +02:00
|
|
|
//
|
2022-12-31 22:28:08 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
2020-05-21 18:39:34 +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)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
2020-05-22 00:19:28 +02:00
|
|
|
#include "td/telegram/DialogFilterId.h"
|
2020-05-21 18:39:34 +02:00
|
|
|
#include "td/telegram/FolderId.h"
|
|
|
|
#include "td/telegram/td_api.h"
|
|
|
|
|
|
|
|
#include "td/utils/common.h"
|
2022-11-23 17:37:32 +01:00
|
|
|
#include "td/utils/HashTableUtils.h"
|
2020-05-21 18:39:34 +02:00
|
|
|
#include "td/utils/StringBuilder.h"
|
|
|
|
|
2020-06-07 17:14:52 +02:00
|
|
|
#include <limits>
|
2020-05-21 18:39:34 +02:00
|
|
|
#include <type_traits>
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
class DialogListId {
|
|
|
|
int64 id = 0;
|
|
|
|
|
2020-05-22 00:19:28 +02:00
|
|
|
static constexpr int64 FILTER_ID_SHIFT = static_cast<int64>(1) << 32;
|
|
|
|
|
2020-05-21 18:39:34 +02:00
|
|
|
public:
|
|
|
|
DialogListId() = default;
|
|
|
|
|
2023-08-24 11:21:04 +02:00
|
|
|
explicit DialogListId(int64 dialog_list_id) : id(dialog_list_id) {
|
2023-08-18 16:51:31 +02:00
|
|
|
if (is_folder() && get_folder_id() != FolderId::archive()) {
|
|
|
|
id = FolderId::main().get();
|
2023-08-21 15:23:11 +02:00
|
|
|
} else if (is_filter()) {
|
|
|
|
CHECK(get_filter_id().is_valid());
|
2023-08-18 16:51:31 +02:00
|
|
|
}
|
2020-05-21 18:39:34 +02:00
|
|
|
}
|
|
|
|
template <class T, typename = std::enable_if_t<std::is_convertible<T, int32>::value>>
|
|
|
|
DialogListId(T dialog_list_id) = delete;
|
|
|
|
|
2020-05-22 00:19:28 +02:00
|
|
|
explicit DialogListId(DialogFilterId dialog_filter_id) : id(dialog_filter_id.get() + FILTER_ID_SHIFT) {
|
|
|
|
}
|
2020-05-21 18:39:34 +02:00
|
|
|
explicit DialogListId(FolderId folder_id) : id(folder_id.get()) {
|
|
|
|
}
|
|
|
|
|
|
|
|
explicit DialogListId(const td_api::object_ptr<td_api::ChatList> &chat_list) {
|
|
|
|
if (chat_list == nullptr) {
|
|
|
|
CHECK(id == FolderId::main().get());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
switch (chat_list->get_id()) {
|
|
|
|
case td_api::chatListArchive::ID:
|
|
|
|
id = FolderId::archive().get();
|
|
|
|
break;
|
|
|
|
case td_api::chatListMain::ID:
|
|
|
|
CHECK(id == FolderId::main().get());
|
|
|
|
break;
|
2023-04-03 16:28:51 +02:00
|
|
|
case td_api::chatListFolder::ID: {
|
|
|
|
DialogFilterId filter_id(static_cast<const td_api::chatListFolder *>(chat_list.get())->chat_folder_id_);
|
2020-05-22 00:19:28 +02:00
|
|
|
if (filter_id.is_valid()) {
|
|
|
|
*this = DialogListId(filter_id);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2020-05-21 18:39:34 +02:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-31 01:59:50 +02:00
|
|
|
td_api::object_ptr<td_api::ChatList> get_chat_list_object() const {
|
|
|
|
if (is_folder()) {
|
|
|
|
auto folder_id = get_folder_id();
|
|
|
|
if (folder_id == FolderId::archive()) {
|
|
|
|
return td_api::make_object<td_api::chatListArchive>();
|
|
|
|
}
|
|
|
|
return td_api::make_object<td_api::chatListMain>();
|
|
|
|
}
|
|
|
|
if (is_filter()) {
|
2023-04-03 16:28:51 +02:00
|
|
|
return td_api::make_object<td_api::chatListFolder>(get_filter_id().get());
|
2020-05-31 01:59:50 +02:00
|
|
|
}
|
|
|
|
UNREACHABLE();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-05-21 18:39:34 +02:00
|
|
|
int64 get() const {
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(const DialogListId &other) const {
|
|
|
|
return id == other.id;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const DialogListId &other) const {
|
|
|
|
return id != other.id;
|
|
|
|
}
|
|
|
|
|
2023-08-24 11:21:04 +02:00
|
|
|
bool is_folder() const {
|
2020-05-21 18:39:34 +02:00
|
|
|
return std::numeric_limits<int32>::min() <= id && id <= std::numeric_limits<int32>::max();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_filter() const {
|
2020-05-22 00:19:28 +02:00
|
|
|
return std::numeric_limits<int32>::min() + FILTER_ID_SHIFT <= id &&
|
|
|
|
id <= std::numeric_limits<int32>::max() + FILTER_ID_SHIFT;
|
2020-05-21 18:39:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
FolderId get_folder_id() const {
|
|
|
|
CHECK(is_folder());
|
|
|
|
return FolderId(static_cast<int32>(id));
|
|
|
|
}
|
|
|
|
|
2020-05-22 00:19:28 +02:00
|
|
|
DialogFilterId get_filter_id() const {
|
|
|
|
CHECK(is_filter());
|
|
|
|
return DialogFilterId(static_cast<int32>(id - FILTER_ID_SHIFT));
|
|
|
|
}
|
2020-05-21 18:39:34 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct DialogListIdHash {
|
2022-11-23 17:37:32 +01:00
|
|
|
uint32 operator()(DialogListId dialog_list_id) const {
|
|
|
|
return Hash<int64>()(dialog_list_id.get());
|
2020-05-21 18:39:34 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
inline StringBuilder &operator<<(StringBuilder &string_builder, DialogListId dialog_list_id) {
|
2020-05-22 00:19:28 +02:00
|
|
|
if (dialog_list_id.is_folder()) {
|
2022-08-27 15:59:34 +02:00
|
|
|
auto folder_id = dialog_list_id.get_folder_id();
|
|
|
|
if (folder_id == FolderId::archive()) {
|
|
|
|
return string_builder << "Archive chat list";
|
|
|
|
}
|
|
|
|
if (folder_id == FolderId::main()) {
|
|
|
|
return string_builder << "Main chat list";
|
|
|
|
}
|
|
|
|
return string_builder << "chat list " << folder_id;
|
2020-05-22 00:19:28 +02:00
|
|
|
}
|
|
|
|
if (dialog_list_id.is_filter()) {
|
|
|
|
return string_builder << "chat list " << dialog_list_id.get_filter_id();
|
|
|
|
}
|
2023-08-18 16:51:31 +02:00
|
|
|
return string_builder << "unknown chat list " << dialog_list_id.get();
|
2020-05-21 18:39:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|