Automatically send screenshot notification messages.

This commit is contained in:
levlam 2023-04-30 16:00:20 +03:00
parent 44d2b0af1e
commit 29c0004bfd
6 changed files with 22 additions and 37 deletions

View File

@ -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 //@message_ids Identifiers of the messages to resend. Message identifiers must be in a strictly increasing order
resendMessages chat_id:int53 message_ids:vector<int53> = Messages; resendMessages chat_id:int53 message_ids:vector<int53> = 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 //@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 //@chat_id Target chat
//@sender_id Identifier of the sender of the message //@sender_id Identifier of the sender of the message

View File

@ -19878,6 +19878,10 @@ Status MessagesManager::view_messages(DialogId dialog_id, vector<MessageId> mess
bool need_invalidate_authentication_code = bool need_invalidate_authentication_code =
dialog_id == DialogId(ContactsManager::get_service_notifications_user_id()) && dialog_id == DialogId(ContactsManager::get_service_notifications_user_id()) &&
source == MessageSource::Screenshot; 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 // keep only valid message identifiers
size_t pos = 0; size_t pos = 0;
@ -19923,7 +19927,7 @@ Status MessagesManager::view_messages(DialogId dialog_id, vector<MessageId> mess
// get information about thread of the messages // get information about thread of the messages
MessageId top_thread_message_id; MessageId top_thread_message_id;
if (source == MessageSource::MessageThreadHistory) { 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"); return Status::Error(400, "There are no message threads in the chat");
} }
@ -20004,6 +20008,7 @@ Status MessagesManager::view_messages(DialogId dialog_id, vector<MessageId> mess
vector<MessageId> new_viewed_message_ids; vector<MessageId> new_viewed_message_ids;
vector<MessageId> viewed_reaction_message_ids; vector<MessageId> viewed_reaction_message_ids;
vector<string> authentication_codes; vector<string> authentication_codes;
vector<MessageId> screenshotted_secret_message_ids;
for (auto message_id : message_ids) { for (auto message_id : message_ids) {
auto *m = get_message_force(d, message_id, "view_messages 4"); auto *m = get_message_force(d, message_id, "view_messages 4");
if (m != nullptr) { if (m != nullptr) {
@ -20067,6 +20072,11 @@ Status MessagesManager::view_messages(DialogId dialog_id, vector<MessageId> mess
if (need_invalidate_authentication_code) { if (need_invalidate_authentication_code) {
extract_authentication_codes(dialog_id, m, authentication_codes); 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) { } 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_) || 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) { 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<MessageId> mess
if (!authentication_codes.empty()) { if (!authentication_codes.empty()) {
invalidate_authentication_codes(td_, std::move(authentication_codes)); invalidate_authentication_codes(td_, std::move(authentication_codes));
} }
if (!screenshotted_secret_message_ids.empty()) {
send_screenshot_taken_notification_message(d);
}
if (!need_read) { if (!need_read) {
return Status::OK(); return Status::OK();
@ -28282,39 +28295,25 @@ Result<vector<MessageId>> MessagesManager::resend_messages(DialogId dialog_id, v
return result; return result;
} }
Status MessagesManager::send_screenshot_taken_notification_message(DialogId dialog_id) { void MessagesManager::send_screenshot_taken_notification_message(Dialog *d) {
auto dialog_type = dialog_id.get_type(); LOG(INFO) << "Begin to send notification about taken screenshot in " << d->dialog_id;
if (dialog_type != DialogType::User && dialog_type != DialogType::SecretChat) { auto dialog_type = d->dialog_id.get_type();
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));
if (dialog_type == DialogType::User) { if (dialog_type == DialogType::User) {
bool need_update_dialog_pos = false; bool need_update_dialog_pos = false;
const Message *m = get_message_to_send(d, MessageId(), MessageId(), MessageSendOptions(), const Message *m = get_message_to_send(d, MessageId(), MessageId(), MessageSendOptions(),
create_screenshot_taken_message_content(), &need_update_dialog_pos); 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); send_update_new_message(d, m);
if (need_update_dialog_pos) { if (need_update_dialog_pos) {
send_update_chat_last_message(d, "send_screenshot_taken_notification_message"); send_update_chat_last_message(d, "send_screenshot_taken_notification_message");
} }
} else { } else {
CHECK(dialog_type == DialogType::SecretChat);
send_closure(td_->secret_chats_manager_, &SecretChatsManager::notify_screenshot_taken, send_closure(td_->secret_chats_manager_, &SecretChatsManager::notify_screenshot_taken,
dialog_id.get_secret_chat_id(), d->dialog_id.get_secret_chat_id(), Promise<Unit>());
Promise<Unit>()); // TODO Promise
} }
return Status::OK();
} }
class MessagesManager::SendScreenshotTakenNotificationMessageLogEvent { class MessagesManager::SendScreenshotTakenNotificationMessageLogEvent {

View File

@ -475,8 +475,6 @@ class MessagesManager final : public Actor {
void set_dialog_message_ttl(DialogId dialog_id, int32 ttl, Promise<Unit> &&promise); void set_dialog_message_ttl(DialogId dialog_id, int32 ttl, Promise<Unit> &&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, void share_dialog_with_bot(FullMessageId full_message_id, int32 button_id, DialogId shared_dialog_id,
bool expect_user, bool only_check, Promise<Unit> &&promise); bool expect_user, bool only_check, Promise<Unit> &&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, void do_send_inline_query_result_message(DialogId dialog_id, MessageId message_id, int64 query_id,
const string &result_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); 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); void do_send_screenshot_taken_notification_message(DialogId dialog_id, const Message *m, uint64 log_event_id);

View File

@ -5673,11 +5673,6 @@ void Td::on_request(uint64 id, td_api::sendChatAction &request) {
DialogAction(std::move(request.action_)), std::move(promise)); 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) { void Td::on_request(uint64 id, td_api::forwardMessages &request) {
auto input_message_ids = MessageId::get_message_ids(request.message_ids_); auto input_message_ids = MessageId::get_message_ids(request.message_ids_);
auto message_copy_options = auto message_copy_options =

View File

@ -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::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, td_api::forwardMessages &request);
void on_request(uint64 id, const td_api::resendMessages &request); void on_request(uint64 id, const td_api::resendMessages &request);

View File

@ -3413,10 +3413,6 @@ class CliClient final : public Actor {
UserId user_id; UserId user_id;
get_args(args, user_id); get_args(args, user_id);
send_request(td_api::make_object<td_api::createNewSecretChat>(user_id)); send_request(td_api::make_object<td_api::createNewSecretChat>(user_id));
} else if (op == "scstn") {
ChatId chat_id;
get_args(args, chat_id);
send_request(td_api::make_object<td_api::sendChatScreenshotTakenNotification>(chat_id));
} else if (op == "closeSC" || op == "cancelSC") { } else if (op == "closeSC" || op == "cancelSC") {
send_request(td_api::make_object<td_api::closeSecretChat>(as_secret_chat_id(args))); send_request(td_api::make_object<td_api::closeSecretChat>(as_secret_chat_id(args)));
} else { } else {