2020-05-12 14:52:10 +02:00
|
|
|
//
|
2021-01-01 13:57:46 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021
|
2020-05-12 14:52:10 +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/InputDialogId.h"
|
|
|
|
|
2020-06-07 17:14:52 +02:00
|
|
|
#include "td/telegram/ChannelId.h"
|
|
|
|
#include "td/telegram/ChatId.h"
|
2020-06-07 19:13:33 +02:00
|
|
|
#include "td/telegram/UserId.h"
|
2020-06-07 17:14:52 +02:00
|
|
|
|
2020-05-12 14:52:10 +02:00
|
|
|
#include "td/utils/logging.h"
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
InputDialogId::InputDialogId(const tl_object_ptr<telegram_api::InputPeer> &input_peer) {
|
|
|
|
CHECK(input_peer != nullptr);
|
|
|
|
switch (input_peer->get_id()) {
|
|
|
|
case telegram_api::inputPeerUser::ID: {
|
|
|
|
auto input_user = static_cast<const telegram_api::inputPeerUser *>(input_peer.get());
|
|
|
|
UserId user_id(input_user->user_id_);
|
|
|
|
if (user_id.is_valid()) {
|
|
|
|
dialog_id = DialogId(user_id);
|
|
|
|
access_hash = input_user->access_hash_;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case telegram_api::inputPeerChat::ID: {
|
|
|
|
auto input_chat = static_cast<const telegram_api::inputPeerChat *>(input_peer.get());
|
|
|
|
ChatId chat_id(input_chat->chat_id_);
|
|
|
|
if (chat_id.is_valid()) {
|
|
|
|
dialog_id = DialogId(chat_id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case telegram_api::inputPeerChannel::ID: {
|
|
|
|
auto input_channel = static_cast<const telegram_api::inputPeerChannel *>(input_peer.get());
|
|
|
|
ChannelId channel_id(input_channel->channel_id_);
|
|
|
|
if (channel_id.is_valid()) {
|
|
|
|
dialog_id = DialogId(channel_id);
|
|
|
|
access_hash = input_channel->access_hash_;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
LOG(ERROR) << "Receive " << to_string(input_peer);
|
|
|
|
}
|
|
|
|
|
2020-05-18 21:26:44 +02:00
|
|
|
vector<InputDialogId> InputDialogId::get_input_dialog_ids(
|
2020-05-19 00:23:13 +02:00
|
|
|
const vector<tl_object_ptr<telegram_api::InputPeer>> &input_peers,
|
|
|
|
std::unordered_set<DialogId, DialogIdHash> *added_dialog_ids) {
|
|
|
|
std::unordered_set<DialogId, DialogIdHash> temp_added_dialog_ids;
|
|
|
|
if (added_dialog_ids == nullptr) {
|
|
|
|
added_dialog_ids = &temp_added_dialog_ids;
|
|
|
|
}
|
2020-05-12 14:52:10 +02:00
|
|
|
vector<InputDialogId> result;
|
|
|
|
result.reserve(input_peers.size());
|
2020-05-18 21:26:44 +02:00
|
|
|
for (auto &input_peer : input_peers) {
|
2020-05-12 14:52:10 +02:00
|
|
|
InputDialogId input_dialog_id(input_peer);
|
2020-05-19 00:23:13 +02:00
|
|
|
if (input_dialog_id.is_valid() && added_dialog_ids->insert(input_dialog_id.get_dialog_id()).second) {
|
2020-05-12 14:52:10 +02:00
|
|
|
result.push_back(input_dialog_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-05-18 21:26:44 +02:00
|
|
|
vector<telegram_api::object_ptr<telegram_api::InputDialogPeer>> InputDialogId::get_input_dialog_peers(
|
|
|
|
const vector<InputDialogId> &input_dialog_ids) {
|
|
|
|
vector<telegram_api::object_ptr<telegram_api::InputDialogPeer>> result;
|
|
|
|
result.reserve(input_dialog_ids.size());
|
2021-11-10 18:39:35 +01:00
|
|
|
for (const auto &input_dialog_id : input_dialog_ids) {
|
2020-05-18 21:26:44 +02:00
|
|
|
auto input_peer = input_dialog_id.get_input_peer();
|
|
|
|
if (input_peer != nullptr) {
|
|
|
|
result.push_back(telegram_api::make_object<telegram_api::inputDialogPeer>(std::move(input_peer)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<telegram_api::object_ptr<telegram_api::InputPeer>> InputDialogId::get_input_peers(
|
|
|
|
const vector<InputDialogId> &input_dialog_ids) {
|
|
|
|
vector<telegram_api::object_ptr<telegram_api::InputPeer>> result;
|
|
|
|
result.reserve(input_dialog_ids.size());
|
2021-11-10 18:39:35 +01:00
|
|
|
for (const auto &input_dialog_id : input_dialog_ids) {
|
2020-05-18 21:26:44 +02:00
|
|
|
auto input_peer = input_dialog_id.get_input_peer();
|
2020-05-26 18:02:17 +02:00
|
|
|
CHECK(input_peer != nullptr);
|
|
|
|
result.push_back(std::move(input_peer));
|
2020-05-18 21:26:44 +02:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-05-12 14:52:10 +02:00
|
|
|
tl_object_ptr<telegram_api::InputPeer> InputDialogId::get_input_peer() const {
|
|
|
|
switch (dialog_id.get_type()) {
|
|
|
|
case DialogType::User:
|
|
|
|
return make_tl_object<telegram_api::inputPeerUser>(dialog_id.get_user_id().get(), access_hash);
|
|
|
|
case DialogType::Chat:
|
|
|
|
return make_tl_object<telegram_api::inputPeerChat>(dialog_id.get_chat_id().get());
|
|
|
|
case DialogType::Channel:
|
|
|
|
return make_tl_object<telegram_api::inputPeerChannel>(dialog_id.get_channel_id().get(), access_hash);
|
|
|
|
case DialogType::SecretChat:
|
|
|
|
case DialogType::None:
|
|
|
|
return nullptr;
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
2020-05-31 22:26:43 +02:00
|
|
|
return nullptr;
|
2020-05-12 14:52:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-28 02:29:12 +02:00
|
|
|
bool InputDialogId::are_equivalent(const vector<InputDialogId> &lhs, const vector<InputDialogId> &rhs) {
|
|
|
|
auto lhs_it = lhs.begin();
|
|
|
|
auto rhs_it = rhs.begin();
|
2020-05-29 08:48:32 +02:00
|
|
|
while (lhs_it != lhs.end() || rhs_it != rhs.end()) {
|
2020-05-28 02:29:12 +02:00
|
|
|
while (lhs_it != lhs.end() && lhs_it->get_dialog_id().get_type() == DialogType::SecretChat) {
|
|
|
|
++lhs_it;
|
|
|
|
}
|
|
|
|
while (rhs_it != rhs.end() && rhs_it->get_dialog_id().get_type() == DialogType::SecretChat) {
|
|
|
|
++rhs_it;
|
|
|
|
}
|
|
|
|
if (lhs_it == lhs.end() || rhs_it == rhs.end()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (lhs_it->get_dialog_id() != rhs_it->get_dialog_id()) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-05-28 20:24:50 +02:00
|
|
|
++lhs_it;
|
|
|
|
++rhs_it;
|
2020-05-28 02:29:12 +02:00
|
|
|
}
|
|
|
|
return lhs_it == lhs.end() && rhs_it == rhs.end();
|
|
|
|
}
|
|
|
|
|
2020-05-30 23:26:22 +02:00
|
|
|
bool InputDialogId::contains(const vector<InputDialogId> &input_dialog_ids, DialogId dialog_id) {
|
|
|
|
for (auto &input_dialog_id : input_dialog_ids) {
|
|
|
|
if (input_dialog_id.get_dialog_id() == dialog_id) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-05-12 14:52:10 +02:00
|
|
|
} // namespace td
|