Add InputDialogId.

GitOrigin-RevId: 0fbfdb383b13c5398c8f98e7315837c892db258a
This commit is contained in:
levlam 2020-05-12 15:52:10 +03:00
parent 89e0dd6a06
commit eb07452aaa
6 changed files with 154 additions and 9 deletions

View File

@ -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

View File

@ -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:

View File

@ -37,7 +37,6 @@ class DialogId {
static int64 get_peer_id(const tl_object_ptr<telegram_api::Peer> &peer);
public:
using UnderlyingType = decltype(id);
DialogId() = default;
explicit DialogId(int64 dialog_id) : id(dialog_id) {

View File

@ -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<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);
}
vector<InputDialogId> InputDialogId::get_input_dialog_ids(const vector<tl_object_ptr<telegram_api::InputPeer>> &input_peers) {
vector<InputDialogId> 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<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();
return false;
}
}
} // namespace td

View File

@ -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<telegram_api::InputPeer> &input_peer);
static vector<InputDialogId> get_input_dialog_ids(const vector<tl_object_ptr<telegram_api::InputPeer>> &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<telegram_api::InputPeer> get_input_peer() const;
template <class StorerT>
void store(StorerT &storer) const {
dialog_id.store(storer);
storer.store_long(access_hash);
}
template <class ParserT>
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

View File

@ -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"