Allow to specify a chat as a local message sender.

GitOrigin-RevId: f1f5251b617a6f17188c7db49450a2eb433ef14c
This commit is contained in:
levlam 2020-10-14 21:47:20 +03:00
parent b07d9634d6
commit b1ed582737
6 changed files with 57 additions and 21 deletions

View File

@ -3785,9 +3785,13 @@ 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 @sender_user_id Identifier of the user who will be shown as the sender of the message; 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 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 The sender sender of the message
//@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:MessageSender 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 messages for all chat members. Always true for supergroups, channels and secret chats
deleteMessages chat_id:int53 message_ids:vector<int53> revoke:Bool = Ok;

Binary file not shown.

View File

@ -25459,8 +25459,8 @@ void MessagesManager::do_send_screenshot_taken_notification_message(DialogId dia
}
Result<MessageId> MessagesManager::add_local_message(
DialogId dialog_id, UserId sender_user_id, MessageId reply_to_message_id, bool disable_notification,
tl_object_ptr<td_api::InputMessageContent> &&input_message_content) {
DialogId dialog_id, td_api::object_ptr<td_api::MessageSender> &&sender, MessageId reply_to_message_id,
bool disable_notification, tl_object_ptr<td_api::InputMessageContent> &&input_message_content) {
if (input_message_content == nullptr) {
return Status::Error(5, "Can't add local message without content");
}
@ -25486,8 +25486,38 @@ Result<MessageId> MessagesManager::add_local_message(
}
bool is_channel_post = is_broadcast_channel(dialog_id);
if (sender_user_id != UserId() && !td_->contacts_manager_->have_user_force(sender_user_id)) {
return Status::Error(400, "User not found");
UserId sender_user_id;
DialogId sender_dialog_id;
if (sender != nullptr) {
switch (sender->get_id()) {
case td_api::messageSenderUser::ID:
sender_user_id = UserId(static_cast<const td_api::messageSenderUser *>(sender.get())->user_id_);
if (!td_->contacts_manager_->have_user_force(sender_user_id)) {
return Status::Error(400, "Sender user not found");
}
break;
case td_api::messageSenderChat::ID:
sender_dialog_id = DialogId(static_cast<const td_api::messageSenderChat *>(sender.get())->chat_id_);
if (sender_dialog_id.get_type() != DialogType::Channel) {
return Status::Error(400, "Sender chat must be a supergroup or channel");
}
if (!have_dialog_force(sender_dialog_id)) {
return Status::Error(400, "Sender chat not found");
}
break;
default:
UNREACHABLE();
}
} else if (is_channel_post) {
sender_dialog_id = dialog_id;
} else {
return Status::Error(400, "The message must have a sender");
}
if (is_channel_post && sender_user_id.is_valid()) {
return Status::Error(400, "Channel post can't have a sender user");
}
if (is_channel_post && sender_dialog_id != dialog_id) {
return Status::Error(400, "Channel post must have the channel as a sender");
}
auto dialog_type = dialog_id.get_type();
@ -25513,9 +25543,10 @@ Result<MessageId> MessagesManager::add_local_message(
if (td_->contacts_manager_->get_channel_sign_messages(dialog_id.get_channel_id())) {
m->author_signature = td_->contacts_manager_->get_user_title(sender_user_id);
}
m->sender_dialog_id = dialog_id;
m->sender_dialog_id = sender_dialog_id;
} else {
m->sender_user_id = sender_user_id;
m->sender_dialog_id = sender_dialog_id;
}
m->date = G()->unix_time();
m->reply_to_message_id = get_reply_to_message_id(d, MessageId(), reply_to_message_id);

View File

@ -425,9 +425,10 @@ class MessagesManager : public Actor {
Status send_screenshot_taken_notification_message(DialogId dialog_id);
Result<MessageId> add_local_message(
DialogId dialog_id, UserId sender_user_id, MessageId reply_to_message_id, bool disable_notification,
tl_object_ptr<td_api::InputMessageContent> &&input_message_content) TD_WARN_UNUSED_RESULT;
Result<MessageId> add_local_message(DialogId dialog_id, td_api::object_ptr<td_api::MessageSender> &&sender,
MessageId reply_to_message_id, bool disable_notification,
tl_object_ptr<td_api::InputMessageContent> &&input_message_content)
TD_WARN_UNUSED_RESULT;
void edit_message_text(FullMessageId full_message_id, tl_object_ptr<td_api::ReplyMarkup> &&reply_markup,
tl_object_ptr<td_api::InputMessageContent> &&input_message_content, Promise<Unit> &&promise);

View File

@ -5710,8 +5710,8 @@ void Td::on_request(uint64 id, td_api::addLocalMessage &request) {
DialogId dialog_id(request.chat_id_);
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_));
dialog_id, std::move(request.sender_), 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());
}

View File

@ -509,12 +509,12 @@ class CliClient final : public Actor {
return as_message_id(str);
}
td_api::object_ptr<td_api::MessageSender> as_message_sender(Slice str) const {
str = trim(str);
if (str.empty() || str[0] != '-') {
return td_api::make_object<td_api::messageSenderUser>(as_user_id(str));
td_api::object_ptr<td_api::MessageSender> as_message_sender(Slice sender_id) const {
sender_id = trim(sender_id);
if (sender_id.empty() || sender_id[0] != '-') {
return td_api::make_object<td_api::messageSenderUser>(as_user_id(sender_id));
} else {
return td_api::make_object<td_api::messageSenderChat>(as_chat_id(str));
return td_api::make_object<td_api::messageSenderChat>(as_chat_id(sender_id));
}
}
@ -3024,18 +3024,18 @@ 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 sender_id;
string reply_to_message_id;
string message;
std::tie(chat_id, args) = split(args);
std::tie(user_id, message) = split(args);
std::tie(sender_id, message) = split(args);
if (op == "almr") {
std::tie(reply_to_message_id, message) = split(message);
}
send_request(td_api::make_object<td_api::addLocalMessage>(
as_chat_id(chat_id), as_user_id(user_id), as_message_id(reply_to_message_id), false,
as_chat_id(chat_id), as_message_sender(sender_id), as_message_id(reply_to_message_id), false,
td_api::make_object<td_api::inputMessageText>(as_formatted_text(message), false, true)));
} else if (op == "smap" || op == "smapr") {
string chat_id;