2024-03-07 13:44:15 +01:00
|
|
|
//
|
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
|
2024-03-07 18:18:04 +01:00
|
|
|
#include "td/telegram/BusinessConnectionId.h"
|
2024-03-07 18:59:06 +01:00
|
|
|
#include "td/telegram/DialogId.h"
|
2024-03-12 14:26:43 +01:00
|
|
|
#include "td/telegram/files/FileId.h"
|
2024-03-11 19:27:16 +01:00
|
|
|
#include "td/telegram/MessageInputReplyTo.h"
|
2024-03-07 21:05:58 +01:00
|
|
|
#include "td/telegram/net/DcId.h"
|
2024-03-07 16:02:30 +01:00
|
|
|
#include "td/telegram/td_api.h"
|
2024-03-07 15:18:11 +01:00
|
|
|
#include "td/telegram/telegram_api.h"
|
|
|
|
|
2024-03-07 13:44:15 +01:00
|
|
|
#include "td/actor/actor.h"
|
|
|
|
|
|
|
|
#include "td/utils/common.h"
|
2024-03-12 14:26:43 +01:00
|
|
|
#include "td/utils/FlatHashMap.h"
|
2024-03-07 16:02:30 +01:00
|
|
|
#include "td/utils/Promise.h"
|
|
|
|
#include "td/utils/Status.h"
|
2024-03-07 15:18:11 +01:00
|
|
|
#include "td/utils/WaitFreeHashMap.h"
|
2024-03-07 13:44:15 +01:00
|
|
|
|
2024-03-12 14:26:43 +01:00
|
|
|
#include <memory>
|
|
|
|
|
2024-03-07 13:44:15 +01:00
|
|
|
namespace td {
|
|
|
|
|
2024-03-11 19:27:16 +01:00
|
|
|
struct InputMessageContent;
|
|
|
|
struct ReplyMarkup;
|
2024-03-07 13:44:15 +01:00
|
|
|
class Td;
|
|
|
|
|
|
|
|
class BusinessConnectionManager final : public Actor {
|
|
|
|
public:
|
|
|
|
BusinessConnectionManager(Td *td, ActorShared<> parent);
|
2024-03-07 15:18:11 +01:00
|
|
|
BusinessConnectionManager(const BusinessConnectionManager &) = delete;
|
|
|
|
BusinessConnectionManager &operator=(const BusinessConnectionManager &) = delete;
|
|
|
|
BusinessConnectionManager(BusinessConnectionManager &&) = delete;
|
|
|
|
BusinessConnectionManager &operator=(BusinessConnectionManager &&) = delete;
|
|
|
|
~BusinessConnectionManager() final;
|
|
|
|
|
2024-03-07 18:59:06 +01:00
|
|
|
Status check_business_connection(const BusinessConnectionId &connection_id, DialogId dialog_id) const;
|
2024-03-07 18:03:12 +01:00
|
|
|
|
2024-03-07 21:05:58 +01:00
|
|
|
DcId get_business_connection_dc_id(const BusinessConnectionId &connection_id) const;
|
|
|
|
|
2024-03-07 15:18:11 +01:00
|
|
|
void on_update_bot_business_connect(telegram_api::object_ptr<telegram_api::botBusinessConnection> &&connection);
|
2024-03-07 13:44:15 +01:00
|
|
|
|
2024-03-09 00:14:02 +01:00
|
|
|
void on_update_bot_new_business_message(const BusinessConnectionId &connection_id,
|
|
|
|
telegram_api::object_ptr<telegram_api::Message> &&message);
|
|
|
|
|
2024-03-09 00:17:00 +01:00
|
|
|
void on_update_bot_edit_business_message(const BusinessConnectionId &connection_id,
|
|
|
|
telegram_api::object_ptr<telegram_api::Message> &&message);
|
|
|
|
|
2024-03-11 14:41:19 +01:00
|
|
|
void on_update_bot_delete_business_messages(const BusinessConnectionId &connection_id, DialogId dialog_id,
|
|
|
|
vector<int32> &&messages);
|
|
|
|
|
2024-03-07 18:18:04 +01:00
|
|
|
void get_business_connection(const BusinessConnectionId &connection_id,
|
2024-03-07 16:02:30 +01:00
|
|
|
Promise<td_api::object_ptr<td_api::businessConnection>> &&promise);
|
|
|
|
|
2024-03-11 19:27:16 +01:00
|
|
|
void send_message(BusinessConnectionId business_connection_id, DialogId dialog_id,
|
|
|
|
td_api::object_ptr<td_api::InputMessageReplyTo> &&reply_to, bool disable_notification,
|
|
|
|
bool protect_content, td_api::object_ptr<td_api::ReplyMarkup> &&reply_markup,
|
|
|
|
td_api::object_ptr<td_api::InputMessageContent> &&input_message_content,
|
|
|
|
Promise<td_api::object_ptr<td_api::message>> &&promise);
|
|
|
|
|
2024-03-13 15:44:00 +01:00
|
|
|
void send_message_album(BusinessConnectionId business_connection_id, DialogId dialog_id,
|
|
|
|
td_api::object_ptr<td_api::InputMessageReplyTo> &&reply_to, bool disable_notification,
|
|
|
|
bool protect_content,
|
|
|
|
vector<td_api::object_ptr<td_api::InputMessageContent>> &&input_message_contents,
|
|
|
|
Promise<td_api::object_ptr<td_api::messages>> &&promise);
|
|
|
|
|
2024-03-07 13:44:15 +01:00
|
|
|
private:
|
2024-03-13 15:44:00 +01:00
|
|
|
static constexpr size_t MAX_GROUPED_MESSAGES = 10; // server side limit
|
|
|
|
|
2024-03-07 15:18:11 +01:00
|
|
|
struct BusinessConnection;
|
2024-03-11 19:27:16 +01:00
|
|
|
struct PendingMessage;
|
|
|
|
class SendBusinessMessageQuery;
|
2024-03-11 23:29:35 +01:00
|
|
|
class SendBusinessMediaQuery;
|
2024-03-13 15:44:00 +01:00
|
|
|
class SendBusinessMultiMediaQuery;
|
2024-03-12 14:26:43 +01:00
|
|
|
class UploadBusinessMediaQuery;
|
|
|
|
class UploadMediaCallback;
|
|
|
|
class UploadThumbnailCallback;
|
|
|
|
|
|
|
|
struct UploadMediaResult {
|
|
|
|
unique_ptr<PendingMessage> message_;
|
|
|
|
telegram_api::object_ptr<telegram_api::InputMedia> input_media_;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct BeingUploadedMedia {
|
|
|
|
unique_ptr<PendingMessage> message_;
|
|
|
|
telegram_api::object_ptr<telegram_api::InputFile> input_file_;
|
|
|
|
Promise<UploadMediaResult> promise_;
|
|
|
|
};
|
2024-03-07 15:18:11 +01:00
|
|
|
|
2024-03-13 15:44:00 +01:00
|
|
|
struct MediaGroupSendRequest {
|
|
|
|
size_t finished_count_ = 0;
|
|
|
|
vector<Result<UploadMediaResult>> upload_results_;
|
|
|
|
Promise<td_api::object_ptr<td_api::messages>> promise_;
|
|
|
|
};
|
|
|
|
|
2024-03-07 16:02:30 +01:00
|
|
|
void tear_down() final;
|
|
|
|
|
2024-03-07 18:18:04 +01:00
|
|
|
void on_get_business_connection(const BusinessConnectionId &connection_id,
|
2024-03-07 16:02:30 +01:00
|
|
|
Result<telegram_api::object_ptr<telegram_api::Updates>> r_updates);
|
|
|
|
|
2024-03-11 19:27:16 +01:00
|
|
|
MessageInputReplyTo create_business_message_input_reply_to(
|
|
|
|
td_api::object_ptr<td_api::InputMessageReplyTo> &&reply_to);
|
|
|
|
|
|
|
|
Result<InputMessageContent> process_input_message_content(
|
|
|
|
DialogId dialog_id, td_api::object_ptr<td_api::InputMessageContent> &&input_message_content);
|
|
|
|
|
|
|
|
unique_ptr<PendingMessage> create_business_message_to_send(BusinessConnectionId business_connection_id,
|
|
|
|
DialogId dialog_id, MessageInputReplyTo &&input_reply_to,
|
|
|
|
bool disable_notification, bool protect_content,
|
|
|
|
unique_ptr<ReplyMarkup> &&reply_markup,
|
|
|
|
InputMessageContent &&input_content) const;
|
|
|
|
|
|
|
|
void do_send_message(unique_ptr<PendingMessage> &&message, Promise<td_api::object_ptr<td_api::message>> &&promise);
|
|
|
|
|
2024-03-12 14:26:43 +01:00
|
|
|
static FileId get_message_file_id(const unique_ptr<PendingMessage> &message);
|
|
|
|
|
|
|
|
FileId get_message_thumbnail_file_id(const unique_ptr<PendingMessage> &message, FileId file_id) const;
|
|
|
|
|
|
|
|
void upload_media(unique_ptr<PendingMessage> &&message, Promise<UploadMediaResult> &&promise,
|
|
|
|
vector<int> bad_parts = {});
|
|
|
|
|
|
|
|
void complete_send_media(unique_ptr<PendingMessage> &&message,
|
|
|
|
telegram_api::object_ptr<telegram_api::InputMedia> &&input_media,
|
|
|
|
Promise<td_api::object_ptr<td_api::message>> &&promise);
|
|
|
|
|
|
|
|
void on_upload_media(FileId file_id, telegram_api::object_ptr<telegram_api::InputFile> input_file);
|
|
|
|
|
|
|
|
void on_upload_media_error(FileId file_id, Status status);
|
|
|
|
|
|
|
|
void on_upload_thumbnail(FileId thumbnail_file_id,
|
|
|
|
telegram_api::object_ptr<telegram_api::InputFile> thumbnail_input_file);
|
|
|
|
|
|
|
|
void do_upload_media(BeingUploadedMedia &&being_uploaded_media,
|
|
|
|
telegram_api::object_ptr<telegram_api::InputFile> input_thumbnail);
|
|
|
|
|
|
|
|
void complete_upload_media(unique_ptr<PendingMessage> &&message,
|
|
|
|
telegram_api::object_ptr<telegram_api::MessageMedia> &&media,
|
|
|
|
Promise<UploadMediaResult> &&promise);
|
|
|
|
|
2024-03-13 15:44:00 +01:00
|
|
|
int64 generate_new_media_album_id();
|
|
|
|
|
|
|
|
void on_upload_message_album_media(int64 request_id, size_t media_pos, Result<UploadMediaResult> &&result);
|
|
|
|
|
2024-03-07 18:18:04 +01:00
|
|
|
WaitFreeHashMap<BusinessConnectionId, unique_ptr<BusinessConnection>, BusinessConnectionIdHash> business_connections_;
|
2024-03-07 15:18:11 +01:00
|
|
|
|
2024-03-07 18:18:04 +01:00
|
|
|
FlatHashMap<BusinessConnectionId, vector<Promise<td_api::object_ptr<td_api::businessConnection>>>,
|
|
|
|
BusinessConnectionIdHash>
|
|
|
|
get_business_connection_queries_;
|
2024-03-07 13:44:15 +01:00
|
|
|
|
2024-03-13 15:44:00 +01:00
|
|
|
int64 current_media_group_send_request_id_ = 0;
|
|
|
|
FlatHashMap<int64, MediaGroupSendRequest> media_group_send_requests_;
|
|
|
|
|
2024-03-12 14:26:43 +01:00
|
|
|
std::shared_ptr<UploadMediaCallback> upload_media_callback_;
|
|
|
|
std::shared_ptr<UploadThumbnailCallback> upload_thumbnail_callback_;
|
|
|
|
|
|
|
|
FlatHashMap<FileId, BeingUploadedMedia, FileIdHash> being_uploaded_files_;
|
|
|
|
FlatHashMap<FileId, BeingUploadedMedia, FileIdHash> being_uploaded_thumbnails_;
|
|
|
|
|
2024-03-07 13:44:15 +01:00
|
|
|
Td *td_;
|
|
|
|
ActorShared<> parent_;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace td
|