summaryrefslogtreecommitdiff
path: root/protocols/Telegram/tdlib/td/td/telegram/DialogId.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'protocols/Telegram/tdlib/td/td/telegram/DialogId.cpp')
-rw-r--r--protocols/Telegram/tdlib/td/td/telegram/DialogId.cpp87
1 files changed, 44 insertions, 43 deletions
diff --git a/protocols/Telegram/tdlib/td/td/telegram/DialogId.cpp b/protocols/Telegram/tdlib/td/td/telegram/DialogId.cpp
index 7ea818599c..9c69d4ebb5 100644
--- a/protocols/Telegram/tdlib/td/td/telegram/DialogId.cpp
+++ b/protocols/Telegram/tdlib/td/td/telegram/DialogId.cpp
@@ -1,5 +1,5 @@
//
-// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2018
+// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022
//
// 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)
@@ -10,42 +10,31 @@
#include "td/utils/logging.h"
+#include <limits>
+
namespace td {
bool DialogId::is_valid() const {
- switch (get_type()) {
- case DialogType::User: {
- return get_user_id().is_valid();
- }
- case DialogType::Chat: {
- return get_chat_id().is_valid();
- }
- case DialogType::Channel: {
- return get_channel_id().is_valid();
- }
- case DialogType::SecretChat: {
- return get_secret_chat_id().is_valid();
- }
- case DialogType::None:
- return false;
- default:
- UNREACHABLE();
- return false;
- }
+ return get_type() != DialogType::None;
}
DialogType DialogId::get_type() const {
+ // check that valid ranges are continuous
+ static_assert(ZERO_CHANNEL_ID + 1 == -ChatId::MAX_CHAT_ID, "");
+ static_assert(
+ ZERO_SECRET_CHAT_ID + std::numeric_limits<int32>::max() + 1 == ZERO_CHANNEL_ID - ChannelId::MAX_CHANNEL_ID, "");
+
if (id < 0) {
- if (MIN_CHAT_ID <= id) {
+ if (-ChatId::MAX_CHAT_ID <= id) {
return DialogType::Chat;
}
- if (MIN_CHANNEL_ID <= id && id < MAX_CHANNEL_ID) {
+ if (ZERO_CHANNEL_ID - ChannelId::MAX_CHANNEL_ID <= id && id != ZERO_CHANNEL_ID) {
return DialogType::Channel;
}
- if (MIN_SECRET_ID <= id && id < MAX_SECRET_ID) {
+ if (ZERO_SECRET_CHAT_ID + std::numeric_limits<int32>::min() <= id && id != ZERO_SECRET_CHAT_ID) {
return DialogType::SecretChat;
}
- } else if (0 < id && id <= MAX_USER_ID) {
+ } else if (0 < id && id <= UserId::MAX_USER_ID) {
return DialogType::User;
}
return DialogType::None;
@@ -53,27 +42,27 @@ DialogType DialogId::get_type() const {
UserId DialogId::get_user_id() const {
CHECK(get_type() == DialogType::User);
- return UserId(static_cast<int32>(id));
+ return UserId(id);
}
ChatId DialogId::get_chat_id() const {
CHECK(get_type() == DialogType::Chat);
- return ChatId(static_cast<int32>(-id));
-}
-
-SecretChatId DialogId::get_secret_chat_id() const {
- CHECK(get_type() == DialogType::SecretChat);
- return SecretChatId(static_cast<int32>(id - ZERO_SECRET_ID));
+ return ChatId(-id);
}
ChannelId DialogId::get_channel_id() const {
CHECK(get_type() == DialogType::Channel);
- return ChannelId(static_cast<int32>(MAX_CHANNEL_ID - id));
+ return ChannelId(ZERO_CHANNEL_ID - id);
+}
+
+SecretChatId DialogId::get_secret_chat_id() const {
+ CHECK(get_type() == DialogType::SecretChat);
+ return SecretChatId(static_cast<int32>(id - ZERO_SECRET_CHAT_ID));
}
DialogId::DialogId(UserId user_id) {
if (user_id.is_valid()) {
- id = static_cast<int64>(user_id.get());
+ id = user_id.get();
} else {
id = 0;
}
@@ -81,7 +70,7 @@ DialogId::DialogId(UserId user_id) {
DialogId::DialogId(ChatId chat_id) {
if (chat_id.is_valid()) {
- id = -static_cast<int64>(chat_id.get());
+ id = -chat_id.get();
} else {
id = 0;
}
@@ -89,22 +78,34 @@ DialogId::DialogId(ChatId chat_id) {
DialogId::DialogId(ChannelId channel_id) {
if (channel_id.is_valid()) {
- id = MAX_CHANNEL_ID - static_cast<int64>(channel_id.get());
+ id = ZERO_CHANNEL_ID - channel_id.get();
} else {
id = 0;
}
}
-DialogId::DialogId(SecretChatId chat_id) {
- if (chat_id.is_valid()) {
- id = ZERO_SECRET_ID + static_cast<int64>(chat_id.get());
+DialogId::DialogId(SecretChatId secret_chat_id) {
+ if (secret_chat_id.is_valid()) {
+ id = ZERO_SECRET_CHAT_ID + static_cast<int64>(secret_chat_id.get());
} else {
id = 0;
}
}
-DialogId::DialogId(const tl_object_ptr<telegram_api::dialogPeer> &dialog_peer) {
- id = get_peer_id(dialog_peer->peer_);
+DialogId::DialogId(const tl_object_ptr<telegram_api::DialogPeer> &dialog_peer) {
+ CHECK(dialog_peer != nullptr);
+ switch (dialog_peer->get_id()) {
+ case telegram_api::dialogPeer::ID:
+ id = get_peer_id(static_cast<const telegram_api::dialogPeer *>(dialog_peer.get())->peer_);
+ break;
+ case telegram_api::dialogPeerFolder::ID:
+ LOG(ERROR) << "Receive unsupported " << to_string(dialog_peer);
+ id = 0;
+ break;
+ default:
+ id = 0;
+ UNREACHABLE();
+ }
}
DialogId::DialogId(const tl_object_ptr<telegram_api::Peer> &peer) : id(get_peer_id(peer)) {
@@ -122,7 +123,7 @@ int64 DialogId::get_peer_id(const tl_object_ptr<telegram_api::Peer> &peer) {
return 0;
}
- return static_cast<int64>(user_id.get());
+ return user_id.get();
}
case telegram_api::peerChat::ID: {
auto peer_chat = static_cast<const telegram_api::peerChat *>(peer.get());
@@ -132,7 +133,7 @@ int64 DialogId::get_peer_id(const tl_object_ptr<telegram_api::Peer> &peer) {
return 0;
}
- return -static_cast<int64>(chat_id.get());
+ return -chat_id.get();
}
case telegram_api::peerChannel::ID: {
auto peer_channel = static_cast<const telegram_api::peerChannel *>(peer.get());
@@ -142,7 +143,7 @@ int64 DialogId::get_peer_id(const tl_object_ptr<telegram_api::Peer> &peer) {
return 0;
}
- return MAX_CHANNEL_ID - static_cast<int64>(channel_id.get());
+ return ZERO_CHANNEL_ID - channel_id.get();
}
default:
UNREACHABLE();