From 29c0004bfd75779b9a331eea58ec209d4b9494ab Mon Sep 17 00:00:00 2001 From: levlam Date: Sun, 30 Apr 2023 16:00:20 +0300 Subject: [PATCH] Automatically send screenshot notification messages. --- td/generate/scheme/td_api.tl | 3 --- td/telegram/MessagesManager.cpp | 41 ++++++++++++++++----------------- td/telegram/MessagesManager.h | 4 ++-- td/telegram/Td.cpp | 5 ---- td/telegram/Td.h | 2 -- td/telegram/cli.cpp | 4 ---- 6 files changed, 22 insertions(+), 37 deletions(-) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 44838e136..9908a2ed5 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -6309,9 +6309,6 @@ forwardMessages chat_id:int53 message_thread_id:int53 from_chat_id:int53 message //@message_ids Identifiers of the messages to resend. Message identifiers must be in a strictly increasing order resendMessages chat_id:int53 message_ids:vector = Messages; -//@description Sends a notification about a screenshot taken in a chat. Supported only in private and secret chats @chat_id Chat identifier -sendChatScreenshotTakenNotification chat_id:int53 = Ok; - //@description Adds a local message to a chat. The message is persistent across application restarts only if the message database is used. Returns the added message //@chat_id Target chat //@sender_id Identifier of the sender of the message diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index 8d5bc26e6..306585e6b 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -19878,6 +19878,10 @@ Status MessagesManager::view_messages(DialogId dialog_id, vector mess bool need_invalidate_authentication_code = dialog_id == DialogId(ContactsManager::get_service_notifications_user_id()) && source == MessageSource::Screenshot; + auto dialog_type = dialog_id.get_type(); + bool need_screenshot_notification = source == MessageSource::Screenshot && + (dialog_type == DialogType::User || dialog_type == DialogType::SecretChat) && + can_send_message(dialog_id).is_ok(); // keep only valid message identifiers size_t pos = 0; @@ -19923,7 +19927,7 @@ Status MessagesManager::view_messages(DialogId dialog_id, vector mess // get information about thread of the messages MessageId top_thread_message_id; if (source == MessageSource::MessageThreadHistory) { - if (dialog_id.get_type() != DialogType::Channel || is_broadcast_channel(dialog_id)) { + if (dialog_type != DialogType::Channel || is_broadcast_channel(dialog_id)) { return Status::Error(400, "There are no message threads in the chat"); } @@ -20004,6 +20008,7 @@ Status MessagesManager::view_messages(DialogId dialog_id, vector mess vector new_viewed_message_ids; vector viewed_reaction_message_ids; vector authentication_codes; + vector screenshotted_secret_message_ids; for (auto message_id : message_ids) { auto *m = get_message_force(d, message_id, "view_messages 4"); if (m != nullptr) { @@ -20067,6 +20072,11 @@ Status MessagesManager::view_messages(DialogId dialog_id, vector mess if (need_invalidate_authentication_code) { extract_authentication_codes(dialog_id, m, authentication_codes); } + if (need_screenshot_notification && !m->is_outgoing) { + if ((dialog_type == DialogType::User && m->is_content_secret) || dialog_type == DialogType::SecretChat) { + screenshotted_secret_message_ids.push_back(m->message_id); + } + } } else if (!message_id.is_yet_unsent() && message_id > max_message_id) { if ((d->notification_info != nullptr && message_id <= d->notification_info->max_notification_message_id_) || message_id <= d->last_new_message_id || message_id <= max_thread_message_id) { @@ -20115,6 +20125,9 @@ Status MessagesManager::view_messages(DialogId dialog_id, vector mess if (!authentication_codes.empty()) { invalidate_authentication_codes(td_, std::move(authentication_codes)); } + if (!screenshotted_secret_message_ids.empty()) { + send_screenshot_taken_notification_message(d); + } if (!need_read) { return Status::OK(); @@ -28282,39 +28295,25 @@ Result> MessagesManager::resend_messages(DialogId dialog_id, v return result; } -Status MessagesManager::send_screenshot_taken_notification_message(DialogId dialog_id) { - auto dialog_type = dialog_id.get_type(); - if (dialog_type != DialogType::User && dialog_type != DialogType::SecretChat) { - return Status::Error(400, "Notification about taken screenshot can be sent only in private and secret chats"); - } - - LOG(INFO) << "Begin to send notification about taken screenshot in " << dialog_id; - - Dialog *d = get_dialog_force(dialog_id, "send_screenshot_taken_notification_message"); - if (d == nullptr) { - return Status::Error(400, "Chat not found"); - } - - TRY_STATUS(can_send_message(dialog_id)); - +void MessagesManager::send_screenshot_taken_notification_message(Dialog *d) { + LOG(INFO) << "Begin to send notification about taken screenshot in " << d->dialog_id; + auto dialog_type = d->dialog_id.get_type(); if (dialog_type == DialogType::User) { bool need_update_dialog_pos = false; const Message *m = get_message_to_send(d, MessageId(), MessageId(), MessageSendOptions(), create_screenshot_taken_message_content(), &need_update_dialog_pos); - do_send_screenshot_taken_notification_message(dialog_id, m, 0); + do_send_screenshot_taken_notification_message(d->dialog_id, m, 0); send_update_new_message(d, m); if (need_update_dialog_pos) { send_update_chat_last_message(d, "send_screenshot_taken_notification_message"); } } else { + CHECK(dialog_type == DialogType::SecretChat); send_closure(td_->secret_chats_manager_, &SecretChatsManager::notify_screenshot_taken, - dialog_id.get_secret_chat_id(), - Promise()); // TODO Promise + d->dialog_id.get_secret_chat_id(), Promise()); } - - return Status::OK(); } class MessagesManager::SendScreenshotTakenNotificationMessageLogEvent { diff --git a/td/telegram/MessagesManager.h b/td/telegram/MessagesManager.h index 356dea6ff..b6bb12242 100644 --- a/td/telegram/MessagesManager.h +++ b/td/telegram/MessagesManager.h @@ -475,8 +475,6 @@ class MessagesManager final : public Actor { void set_dialog_message_ttl(DialogId dialog_id, int32 ttl, Promise &&promise); - Status send_screenshot_taken_notification_message(DialogId dialog_id); - void share_dialog_with_bot(FullMessageId full_message_id, int32 button_id, DialogId shared_dialog_id, bool expect_user, bool only_check, Promise &&promise); @@ -1962,6 +1960,8 @@ class MessagesManager final : public Actor { void do_send_inline_query_result_message(DialogId dialog_id, MessageId message_id, int64 query_id, const string &result_id); + void send_screenshot_taken_notification_message(Dialog *d); + static uint64 save_send_screenshot_taken_notification_message_log_event(DialogId dialog_id, const Message *m); void do_send_screenshot_taken_notification_message(DialogId dialog_id, const Message *m, uint64 log_event_id); diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 624ef01f5..0eb4ea9d1 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -5673,11 +5673,6 @@ void Td::on_request(uint64 id, td_api::sendChatAction &request) { DialogAction(std::move(request.action_)), std::move(promise)); } -void Td::on_request(uint64 id, td_api::sendChatScreenshotTakenNotification &request) { - CHECK_IS_USER(); - answer_ok_query(id, messages_manager_->send_screenshot_taken_notification_message(DialogId(request.chat_id_))); -} - void Td::on_request(uint64 id, td_api::forwardMessages &request) { auto input_message_ids = MessageId::get_message_ids(request.message_ids_); auto message_copy_options = diff --git a/td/telegram/Td.h b/td/telegram/Td.h index e830d9499..2956b8ade 100644 --- a/td/telegram/Td.h +++ b/td/telegram/Td.h @@ -812,8 +812,6 @@ class Td final : public Actor { void on_request(uint64 id, td_api::sendChatAction &request); - void on_request(uint64 id, td_api::sendChatScreenshotTakenNotification &request); - void on_request(uint64 id, td_api::forwardMessages &request); void on_request(uint64 id, const td_api::resendMessages &request); diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index 5cbea4e8f..88893ba2f 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -3413,10 +3413,6 @@ class CliClient final : public Actor { UserId user_id; get_args(args, user_id); send_request(td_api::make_object(user_id)); - } else if (op == "scstn") { - ChatId chat_id; - get_args(args, chat_id); - send_request(td_api::make_object(chat_id)); } else if (op == "closeSC" || op == "cancelSC") { send_request(td_api::make_object(as_secret_chat_id(args))); } else {