From f8efdc7b9dfb47dda5808bc9cc3ee4e6585f8020 Mon Sep 17 00:00:00 2001 From: levlam Date: Tue, 27 Mar 2018 17:55:33 +0300 Subject: [PATCH] Better td_api::addLocalMessage. GitOrigin-RevId: be6a9d27d3d683fdd7c72bca6bb8ba080bdbfc0d --- td/generate/scheme/td_api.tl | 6 +++--- td/generate/scheme/td_api.tlo | Bin 113020 -> 113108 bytes td/telegram/ContactsManager.cpp | 13 ++++++------- td/telegram/MessagesManager.cpp | 31 +++++++++++++++++++++++-------- td/telegram/MessagesManager.h | 6 +++--- td/telegram/Td.cpp | 5 +++-- td/telegram/cli.cpp | 13 +++++++++++-- 7 files changed, 49 insertions(+), 25 deletions(-) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index ce5e1e34..972b4929 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -2228,9 +2228,9 @@ sendChatSetTtlMessage chat_id:int53 ttl:int32 = Message; //@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 -//@reply_to_message_id Identifier of the message to reply to or 0 @input_message_content The content of the message to be added -addLocalMessage chat_id:int53 reply_to_message_id:int53 input_message_content:InputMessageContent = 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 @sender_user_id Identifier of the user who will be shown as message sender; may be 0 for channel posts +//@reply_to_message_id Identifier of the message to reply to or 0 @disable_notification Pass true to disable notification for the message @input_message_content The content of the message to be added +addLocalMessage chat_id:int53 sender_user_id:int32 reply_to_message_id:int53 disable_notification:Bool input_message_content:InputMessageContent = Message; //@description Deletes messages @chat_id Chat identifier @message_ids Identifiers of the messages to be deleted @revoke Pass true to try to delete outgoing messages for all chat members (may fail if messages are too old). Always true for supergroups, channels and secret chats deleteMessages chat_id:int53 message_ids:vector revoke:Bool = Ok; diff --git a/td/generate/scheme/td_api.tlo b/td/generate/scheme/td_api.tlo index 2d67cab669d5b14ae834acdc18bc12a753cd7948..dd7f6d5703077e4fb18717d4bc8f6874a26882c4 100644 GIT binary patch delta 42 ycmezKiS5c~wuUW?XKYw5_ZYvPe!+%OV)~NFj2hGD$N`BNGZ|yH+t@NX)BpgkqY=LV delta 47 zcmV+~0MP%`^9KC#27t5y%sd2h`lSq)&^!Scmk^c#2$w7f0U(#~nE@=9yqf_;x7a-a F{Co0O6Vm_y diff --git a/td/telegram/ContactsManager.cpp b/td/telegram/ContactsManager.cpp index 7962cbb7..c3a956f0 100644 --- a/td/telegram/ContactsManager.cpp +++ b/td/telegram/ContactsManager.cpp @@ -5267,12 +5267,11 @@ ContactsManager::User *ContactsManager::get_user_force(UserId user_id) { auto u = get_user_force_impl(user_id); if (user_id == UserId(777000) && (u == nullptr || !u->is_received)) { int32 flags = telegram_api::user::ACCESS_HASH_MASK | telegram_api::user::FIRST_NAME_MASK | - telegram_api::user::LAST_NAME_MASK | telegram_api::user::PHONE_MASK | telegram_api::user::PHOTO_MASK | - telegram_api::user::VERIFIED_MASK; + telegram_api::user::PHONE_MASK | telegram_api::user::PHOTO_MASK | telegram_api::user::VERIFIED_MASK; auto profile_photo = telegram_api::make_object( - 3337190045231018, - telegram_api::make_object(1, 702229962, 26779, 5859320227133863146), - telegram_api::make_object(1, 702229962, 26781, -3695031185685824216)); + 3337190045231018, + telegram_api::make_object(1, 702229962, 26779, 5859320227133863146), + telegram_api::make_object(1, 702229962, 26781, -3695031185685824216)); if (G()->is_test_dc()) { profile_photo = nullptr; flags -= telegram_api::user::PHOTO_MASK; @@ -5281,8 +5280,8 @@ ContactsManager::User *ContactsManager::get_user_force(UserId user_id) { auto user = telegram_api::make_object( flags, false /*ignored*/, false /*ignored*/, false /*ignored*/, false /*ignored*/, false /*ignored*/, false /*ignored*/, false /*ignored*/, false /*ignored*/, false /*ignored*/, false /*ignored*/, - false /*ignored*/, 777000, 1, "Telegram", "Updates", string(), "42777", std::move(profile_photo), - nullptr, 0, string(), string(), string()); + false /*ignored*/, 777000, 1, "Telegram", string(), string(), "42777", std::move(profile_photo), nullptr, 0, + string(), string(), string()); on_get_user(std::move(user)); u = get_user(user_id); CHECK(u != nullptr && u->is_received); diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index 8926d561..70d817e6 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -18047,7 +18047,7 @@ void MessagesManager::do_send_screenshot_taken_notification_message(DialogId dia } Result MessagesManager::add_local_message( - DialogId dialog_id, MessageId reply_to_message_id, + DialogId dialog_id, UserId sender_user_id, MessageId reply_to_message_id, bool disable_notification, tl_object_ptr &&input_message_content) { if (input_message_content == nullptr) { return Status::Error(5, "Can't add local message without content"); @@ -18066,30 +18066,46 @@ Result MessagesManager::add_local_message( return Status::Error(5, "Chat not found"); } - TRY_STATUS(can_send_message(dialog_id)); + if (!have_input_peer(dialog_id, AccessRights::Read)) { + return Status::Error(400, "Can't access the chat"); + } TRY_RESULT(message_content, process_input_message_content(dialog_id, std::move(input_message_content))); - MessageId message_id = get_next_local_message_id(d); + bool is_channel_post = is_broadcast_channel(dialog_id); + if (!td_->contacts_manager_->have_user_force(sender_user_id) && !(is_channel_post && sender_user_id == UserId())) { + return Status::Error(400, "User not found"); + } auto dialog_type = dialog_id.get_type(); auto my_id = td_->contacts_manager_->get_my_id("add_local_message"); + if (sender_user_id != my_id) { + if (dialog_type == DialogType::User && DialogId(sender_user_id) != dialog_id) { + return Status::Error(400, "Wrong sender user"); + } + if (dialog_type == DialogType::SecretChat && + sender_user_id != td_->contacts_manager_->get_secret_chat_user_id(dialog_id.get_secret_chat_id())) { + return Status::Error(400, "Wrong sender user"); + } + } + + MessageId message_id = get_next_local_message_id(d); auto m = make_unique(); m->random_y = get_random_y(message_id); m->message_id = message_id; - bool is_channel_post = is_broadcast_channel(dialog_id); if (is_channel_post) { // sender of the post can be hidden if (td_->contacts_manager_->get_channel_sign_messages(dialog_id.get_channel_id())) { - m->author_signature = td_->contacts_manager_->get_user_title(my_id); + m->author_signature = td_->contacts_manager_->get_user_title(sender_user_id); } } else { - m->sender_user_id = my_id; + m->sender_user_id = sender_user_id; } m->date = G()->unix_time(); m->reply_to_message_id = get_reply_to_message_id(d, reply_to_message_id); m->is_channel_post = is_channel_post; - m->is_outgoing = dialog_id != DialogId(my_id); + m->is_outgoing = dialog_id != DialogId(my_id) && sender_user_id == my_id; + m->disable_notification = disable_notification; m->from_background = false; m->views = 0; m->content = std::move(message_content.content); @@ -18100,7 +18116,6 @@ Result MessagesManager::add_local_message( if (is_service_message_content(m->content->get_id())) { m->ttl = 0; } - m->is_content_secret = is_secret_message_content(m->ttl, m->content->get_id()); } else if (message_content.ttl > 0) { m->ttl = message_content.ttl; } diff --git a/td/telegram/MessagesManager.h b/td/telegram/MessagesManager.h index 64eb2cd0..9a4720dc 100644 --- a/td/telegram/MessagesManager.h +++ b/td/telegram/MessagesManager.h @@ -1032,9 +1032,9 @@ class MessagesManager : public Actor { Status send_screenshot_taken_notification_message(DialogId dialog_id); - Result add_local_message(DialogId dialog_id, MessageId reply_to_message_id, - tl_object_ptr &&input_message_content) - TD_WARN_UNUSED_RESULT; + Result add_local_message( + DialogId dialog_id, UserId sender_user_id, MessageId reply_to_message_id, bool disable_notification, + tl_object_ptr &&input_message_content) TD_WARN_UNUSED_RESULT; void edit_message_text(FullMessageId full_message_id, tl_object_ptr &&reply_markup, tl_object_ptr &&input_message_content, Promise &&promise); diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 9d52f871..190f4fe3 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -5643,8 +5643,9 @@ void Td::on_request(uint64 id, td_api::addLocalMessage &request) { CHECK_IS_USER(); DialogId dialog_id(request.chat_id_); - auto r_new_message_id = messages_manager_->add_local_message(dialog_id, MessageId(request.reply_to_message_id_), - std::move(request.input_message_content_)); + auto r_new_message_id = messages_manager_->add_local_message( + dialog_id, UserId(request.sender_user_id_), MessageId(request.reply_to_message_id_), + request.disable_notification_, std::move(request.input_message_content_)); if (r_new_message_id.is_error()) { return send_closure(actor_id(this), &Td::send_error, id, r_new_message_id.move_as_error()); } diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index 8785c78e..93b48394 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -2071,10 +2071,12 @@ class CliClient final : public Actor { op == "sms", false, as_message_id(reply_to_message_id)); } else if (op == "alm" || op == "almr") { string chat_id; + string user_id; string reply_to_message_id; string message; - std::tie(chat_id, message) = split(args); + std::tie(chat_id, args) = split(args); + std::tie(user_id, message) = split(args); if (op == "smr") { std::tie(reply_to_message_id, message) = split(message); } @@ -2086,7 +2088,7 @@ class CliClient final : public Actor { } send_request(make_tl_object( - as_chat_id(chat_id), as_message_id(reply_to_message_id), + as_chat_id(chat_id), as_user_id(user_id), as_message_id(reply_to_message_id), false, make_tl_object(move_tl_object_as(parsed_text), false, true))); } else if (op == "smap" || op == "smapr") { @@ -2679,6 +2681,13 @@ class CliClient final : public Actor { std::tie(supergroup_id, is_all_history_available) = split(args); send_request(make_tl_object(to_integer(supergroup_id), as_bool(is_all_history_available))); + } else if (op == "tsgsm") { + string supergroup_id; + string sign_messages; + + std::tie(supergroup_id, sign_messages) = split(args); + send_request(make_tl_object(to_integer(supergroup_id), + as_bool(sign_messages))); } else if (op == "csgd" || op == "cchd") { string supergroup_id; string description;