From eb07452aaa932c0907bb9ea44c30d31f74e2a4cf Mon Sep 17 00:00:00 2001 From: levlam Date: Tue, 12 May 2020 15:52:10 +0300 Subject: [PATCH] Add InputDialogId. GitOrigin-RevId: 0fbfdb383b13c5398c8f98e7315837c892db258a --- CMakeLists.txt | 2 + td/telegram/DialogId.cpp | 12 ++---- td/telegram/DialogId.h | 1 - td/telegram/InputDialogId.cpp | 80 +++++++++++++++++++++++++++++++++++ td/telegram/InputDialogId.h | 67 +++++++++++++++++++++++++++++ td/telegram/MessagesManager.h | 1 + 6 files changed, 154 insertions(+), 9 deletions(-) create mode 100644 td/telegram/InputDialogId.cpp create mode 100644 td/telegram/InputDialogId.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 954657936..c76064e28 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -425,6 +425,7 @@ set(TDLIB_SOURCE td/telegram/Global.cpp td/telegram/HashtagHints.cpp td/telegram/InlineQueriesManager.cpp + td/telegram/InputDialogId.cpp td/telegram/InputMessageText.cpp td/telegram/JsonValue.cpp td/telegram/LanguagePackManager.cpp @@ -588,6 +589,7 @@ set(TDLIB_SOURCE td/telegram/Global.h td/telegram/HashtagHints.h td/telegram/InlineQueriesManager.h + td/telegram/InputDialogId.h td/telegram/InputMessageText.h td/telegram/JsonValue.h td/telegram/LanguagePackManager.h diff --git a/td/telegram/DialogId.cpp b/td/telegram/DialogId.cpp index 6fc4646e0..39d160336 100644 --- a/td/telegram/DialogId.cpp +++ b/td/telegram/DialogId.cpp @@ -14,18 +14,14 @@ namespace td { bool DialogId::is_valid() const { switch (get_type()) { - case DialogType::User: { + case DialogType::User: return get_user_id().is_valid(); - } - case DialogType::Chat: { + case DialogType::Chat: return get_chat_id().is_valid(); - } - case DialogType::Channel: { + case DialogType::Channel: return get_channel_id().is_valid(); - } - case DialogType::SecretChat: { + case DialogType::SecretChat: return get_secret_chat_id().is_valid(); - } case DialogType::None: return false; default: diff --git a/td/telegram/DialogId.h b/td/telegram/DialogId.h index 898824377..ce2abde86 100644 --- a/td/telegram/DialogId.h +++ b/td/telegram/DialogId.h @@ -37,7 +37,6 @@ class DialogId { static int64 get_peer_id(const tl_object_ptr &peer); public: - using UnderlyingType = decltype(id); DialogId() = default; explicit DialogId(int64 dialog_id) : id(dialog_id) { diff --git a/td/telegram/InputDialogId.cpp b/td/telegram/InputDialogId.cpp new file mode 100644 index 000000000..93b7ec15d --- /dev/null +++ b/td/telegram/InputDialogId.cpp @@ -0,0 +1,80 @@ +// +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2020 +// +// 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" + +#include "td/utils/logging.h" + +namespace td { + +InputDialogId::InputDialogId(const tl_object_ptr &input_peer) { + CHECK(input_peer != nullptr); + switch (input_peer->get_id()) { + case telegram_api::inputPeerUser::ID: { + auto input_user = static_cast(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(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(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); +} + +vector InputDialogId::get_input_dialog_ids(const vector> &input_peers) { + vector result; + result.reserve(input_peers.size()); + for (auto &input_peer:input_peers) { + InputDialogId input_dialog_id(input_peer); + if (input_dialog_id.is_valid()) { + result.push_back(input_dialog_id); + } + } + return result; +} + +tl_object_ptr InputDialogId::get_input_peer() const { + switch (dialog_id.get_type()) { + case DialogType::User: + return make_tl_object(dialog_id.get_user_id().get(), access_hash); + case DialogType::Chat: + return make_tl_object(dialog_id.get_chat_id().get()); + case DialogType::Channel: + return make_tl_object(dialog_id.get_channel_id().get(), access_hash); + case DialogType::SecretChat: + case DialogType::None: + return nullptr; + default: + UNREACHABLE(); + return false; + } +} + +} // namespace td diff --git a/td/telegram/InputDialogId.h b/td/telegram/InputDialogId.h new file mode 100644 index 000000000..6069bc404 --- /dev/null +++ b/td/telegram/InputDialogId.h @@ -0,0 +1,67 @@ +// +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2020 +// +// 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/telegram_api.h" + +#include "td/utils/common.h" +#include "td/utils/StringBuilder.h" + +namespace td { + +class InputDialogId { + DialogId dialog_id; + int64 access_hash = 0; + + public: + InputDialogId() = default; + + explicit InputDialogId(DialogId dialog_id): dialog_id(dialog_id) { + CHECK(dialog_id.get_type() == DialogType::SecretChat); + } + + explicit InputDialogId(const tl_object_ptr &input_peer); + + static vector get_input_dialog_ids(const vector> &input_peers); + + bool operator==(const InputDialogId &other) const { + return dialog_id == other.dialog_id && access_hash == other.access_hash; + } + + bool operator!=(const InputDialogId &other) const { + return !(*this == other); + } + + bool is_valid() const { + return dialog_id.is_valid(); + } + + DialogId get_dialog_id() const { + return dialog_id; + } + + tl_object_ptr get_input_peer() const; + + template + void store(StorerT &storer) const { + dialog_id.store(storer); + storer.store_long(access_hash); + } + + template + void parse(ParserT &parser) { + dialog_id.parse(parser); + access_hash = parser.fetch_long(); + } +}; + +inline StringBuilder &operator<<(StringBuilder &string_builder, InputDialogId input_dialog_id) { + return string_builder << "input " << input_dialog_id.get_dialog_id(); +} + +} // namespace td diff --git a/td/telegram/MessagesManager.h b/td/telegram/MessagesManager.h index 13fc04e1a..fbd326e6c 100644 --- a/td/telegram/MessagesManager.h +++ b/td/telegram/MessagesManager.h @@ -26,6 +26,7 @@ #include "td/telegram/FolderId.h" #include "td/telegram/FullMessageId.h" #include "td/telegram/Global.h" +#include "td/telegram/InputDialogId.h" #include "td/telegram/MessageContentType.h" #include "td/telegram/MessageId.h" #include "td/telegram/MessagesDb.h"