2021-11-14 23:18:47 +01:00
|
|
|
//
|
2022-12-31 22:28:08 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
2021-11-14 23:18:47 +01: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/MessageSender.h"
|
|
|
|
|
|
|
|
#include "td/telegram/AuthManager.h"
|
|
|
|
#include "td/telegram/ContactsManager.h"
|
|
|
|
#include "td/telegram/MessagesManager.h"
|
|
|
|
#include "td/telegram/Td.h"
|
|
|
|
|
2021-11-15 10:52:40 +01:00
|
|
|
#include "td/utils/algorithm.h"
|
2021-12-09 22:07:59 +01:00
|
|
|
#include "td/utils/logging.h"
|
2021-11-15 10:52:40 +01:00
|
|
|
#include "td/utils/misc.h"
|
|
|
|
|
2021-11-14 23:18:47 +01:00
|
|
|
namespace td {
|
|
|
|
|
|
|
|
td_api::object_ptr<td_api::MessageSender> get_message_sender_object_const(Td *td, UserId user_id, DialogId dialog_id,
|
|
|
|
const char *source) {
|
|
|
|
if (dialog_id.is_valid() && td->messages_manager_->have_dialog(dialog_id)) {
|
2023-04-13 21:41:22 +02:00
|
|
|
return td_api::make_object<td_api::messageSenderChat>(
|
|
|
|
td->messages_manager_->get_chat_id_object(dialog_id, "get_message_sender_object_const"));
|
2021-11-14 23:18:47 +01:00
|
|
|
}
|
|
|
|
if (!user_id.is_valid()) {
|
|
|
|
// can happen only if the server sends a message with wrong sender
|
|
|
|
LOG(ERROR) << "Receive message with wrong sender " << user_id << '/' << dialog_id << " from " << source;
|
|
|
|
user_id = td->contacts_manager_->add_service_notifications_user();
|
|
|
|
}
|
|
|
|
return td_api::make_object<td_api::messageSenderUser>(td->contacts_manager_->get_user_id_object(user_id, source));
|
|
|
|
}
|
|
|
|
|
|
|
|
td_api::object_ptr<td_api::MessageSender> get_message_sender_object_const(Td *td, DialogId dialog_id,
|
|
|
|
const char *source) {
|
|
|
|
if (dialog_id.get_type() == DialogType::User) {
|
|
|
|
return get_message_sender_object_const(td, dialog_id.get_user_id(), DialogId(), source);
|
|
|
|
}
|
|
|
|
return get_message_sender_object_const(td, UserId(), dialog_id, source);
|
|
|
|
}
|
|
|
|
|
|
|
|
td_api::object_ptr<td_api::MessageSender> get_message_sender_object(Td *td, UserId user_id, DialogId dialog_id,
|
|
|
|
const char *source) {
|
|
|
|
if (dialog_id.is_valid() && !td->messages_manager_->have_dialog(dialog_id)) {
|
2023-11-08 23:52:20 +01:00
|
|
|
LOG(ERROR) << "Failed to find " << dialog_id << " from " << source;
|
2021-11-14 23:18:47 +01:00
|
|
|
td->messages_manager_->force_create_dialog(dialog_id, source);
|
|
|
|
}
|
|
|
|
if (!user_id.is_valid() && td->auth_manager_->is_bot()) {
|
|
|
|
td->contacts_manager_->add_anonymous_bot_user();
|
2021-12-09 14:36:27 +01:00
|
|
|
td->contacts_manager_->add_channel_bot_user();
|
2021-11-14 23:18:47 +01:00
|
|
|
td->contacts_manager_->add_service_notifications_user();
|
|
|
|
}
|
|
|
|
return get_message_sender_object_const(td, user_id, dialog_id, source);
|
|
|
|
}
|
|
|
|
|
|
|
|
td_api::object_ptr<td_api::MessageSender> get_message_sender_object(Td *td, DialogId dialog_id, const char *source) {
|
|
|
|
if (dialog_id.get_type() == DialogType::User) {
|
|
|
|
return get_message_sender_object(td, dialog_id.get_user_id(), DialogId(), source);
|
|
|
|
}
|
|
|
|
return get_message_sender_object(td, UserId(), dialog_id, source);
|
|
|
|
}
|
|
|
|
|
2022-01-22 21:17:41 +01:00
|
|
|
td_api::object_ptr<td_api::MessageSender> get_min_message_sender_object(Td *td, DialogId dialog_id,
|
|
|
|
const char *source) {
|
|
|
|
auto dialog_type = dialog_id.get_type();
|
|
|
|
if (dialog_type == DialogType::User) {
|
|
|
|
auto user_id = dialog_id.get_user_id();
|
|
|
|
if (td->contacts_manager_->have_min_user(user_id)) {
|
|
|
|
return td_api::make_object<td_api::messageSenderUser>(td->contacts_manager_->get_user_id_object(user_id, source));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!td->messages_manager_->have_dialog(dialog_id) &&
|
|
|
|
(td->messages_manager_->have_dialog_info(dialog_id) ||
|
|
|
|
(dialog_type == DialogType::Channel && td->contacts_manager_->have_min_channel(dialog_id.get_channel_id())))) {
|
|
|
|
LOG(INFO) << "Force creation of " << dialog_id;
|
|
|
|
td->messages_manager_->force_create_dialog(dialog_id, source, true);
|
|
|
|
}
|
|
|
|
if (td->messages_manager_->have_dialog(dialog_id)) {
|
2023-04-13 21:41:22 +02:00
|
|
|
return td_api::make_object<td_api::messageSenderChat>(
|
|
|
|
td->messages_manager_->get_chat_id_object(dialog_id, "get_min_message_sender_object"));
|
2022-01-22 21:17:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
LOG(ERROR) << "Can't return unknown " << dialog_id << " from " << source;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-11-15 10:52:40 +01:00
|
|
|
vector<DialogId> get_message_sender_dialog_ids(Td *td,
|
|
|
|
const vector<telegram_api::object_ptr<telegram_api::Peer>> &peers) {
|
|
|
|
vector<DialogId> message_sender_dialog_ids;
|
|
|
|
message_sender_dialog_ids.reserve(peers.size());
|
|
|
|
for (auto &peer : peers) {
|
|
|
|
DialogId dialog_id(peer);
|
|
|
|
if (!dialog_id.is_valid()) {
|
|
|
|
LOG(ERROR) << "Receive invalid " << dialog_id << " as message sender";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (dialog_id.get_type() == DialogType::User) {
|
|
|
|
if (!td->contacts_manager_->have_user(dialog_id.get_user_id())) {
|
2022-10-05 20:29:05 +02:00
|
|
|
LOG(ERROR) << "Receive unknown " << dialog_id.get_user_id();
|
2021-11-15 10:52:40 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!td->messages_manager_->have_dialog_info(dialog_id)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
td->messages_manager_->force_create_dialog(dialog_id, "get_message_sender_dialog_ids");
|
|
|
|
if (!td->messages_manager_->have_dialog(dialog_id)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
message_sender_dialog_ids.push_back(dialog_id);
|
|
|
|
}
|
|
|
|
return message_sender_dialog_ids;
|
|
|
|
}
|
|
|
|
|
|
|
|
td_api::object_ptr<td_api::messageSenders> convert_message_senders_object(
|
|
|
|
Td *td, const vector<telegram_api::object_ptr<telegram_api::Peer>> &peers) {
|
|
|
|
auto dialog_ids = get_message_sender_dialog_ids(td, peers);
|
|
|
|
auto message_senders = transform(dialog_ids, [td](DialogId dialog_id) {
|
|
|
|
return get_message_sender_object(td, dialog_id, "convert_message_senders_object");
|
|
|
|
});
|
|
|
|
return td_api::make_object<td_api::messageSenders>(narrow_cast<int32>(dialog_ids.size()), std::move(message_senders));
|
|
|
|
}
|
|
|
|
|
2021-11-15 11:31:07 +01:00
|
|
|
Result<DialogId> get_message_sender_dialog_id(Td *td,
|
|
|
|
const td_api::object_ptr<td_api::MessageSender> &message_sender_id,
|
|
|
|
bool check_access, bool allow_empty) {
|
2021-11-15 10:07:52 +01:00
|
|
|
if (message_sender_id == nullptr) {
|
2021-11-15 11:31:07 +01:00
|
|
|
if (allow_empty) {
|
|
|
|
return DialogId();
|
|
|
|
}
|
2021-11-15 12:00:30 +01:00
|
|
|
return Status::Error(400, "Message sender must be non-empty");
|
2021-11-15 10:07:52 +01:00
|
|
|
}
|
|
|
|
switch (message_sender_id->get_id()) {
|
|
|
|
case td_api::messageSenderUser::ID: {
|
|
|
|
auto user_id = UserId(static_cast<const td_api::messageSenderUser *>(message_sender_id.get())->user_id_);
|
|
|
|
if (!user_id.is_valid()) {
|
2021-11-15 11:31:07 +01:00
|
|
|
if (allow_empty && user_id == UserId()) {
|
|
|
|
return DialogId();
|
|
|
|
}
|
2021-11-15 10:07:52 +01:00
|
|
|
return Status::Error(400, "Invalid user identifier specified");
|
|
|
|
}
|
2023-07-20 19:38:25 +02:00
|
|
|
bool know_user = td->contacts_manager_->have_user_force(user_id, "get_message_sender_dialog_id");
|
2021-12-20 18:30:33 +01:00
|
|
|
if (check_access && !know_user) {
|
2021-11-15 11:31:07 +01:00
|
|
|
return Status::Error(400, "Unknown user identifier specified");
|
|
|
|
}
|
2021-11-15 10:07:52 +01:00
|
|
|
return DialogId(user_id);
|
|
|
|
}
|
|
|
|
case td_api::messageSenderChat::ID: {
|
|
|
|
auto dialog_id = DialogId(static_cast<const td_api::messageSenderChat *>(message_sender_id.get())->chat_id_);
|
|
|
|
if (!dialog_id.is_valid()) {
|
2021-11-15 11:31:07 +01:00
|
|
|
if (allow_empty && dialog_id == DialogId()) {
|
|
|
|
return DialogId();
|
|
|
|
}
|
2021-11-15 10:07:52 +01:00
|
|
|
return Status::Error(400, "Invalid chat identifier specified");
|
|
|
|
}
|
2023-07-20 19:38:25 +02:00
|
|
|
bool know_dialog =
|
|
|
|
dialog_id.get_type() == DialogType::User
|
|
|
|
? td->contacts_manager_->have_user_force(dialog_id.get_user_id(), "get_message_sender_dialog_id 2")
|
|
|
|
: td->messages_manager_->have_dialog_force(dialog_id, "get_message_sender_dialog_id");
|
2021-12-20 18:30:33 +01:00
|
|
|
if (check_access && !know_dialog) {
|
|
|
|
return Status::Error(400, "Unknown chat identifier specified");
|
2021-11-15 11:31:07 +01:00
|
|
|
}
|
2021-11-15 10:07:52 +01:00
|
|
|
return dialog_id;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return DialogId();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-14 23:18:47 +01:00
|
|
|
} // namespace td
|