Improve handling of business messages.

This commit is contained in:
levlam 2024-06-19 14:43:05 +03:00
parent 8f19c751dc
commit 19c54ae90a
4 changed files with 80 additions and 27 deletions

View File

@ -6614,9 +6614,62 @@ unique_ptr<MessageContent> dup_message_content(Td *td, DialogId dialog_id, const
unique_ptr<MessageContent> get_action_message_content(Td *td, tl_object_ptr<telegram_api::MessageAction> &&action_ptr,
DialogId owner_dialog_id, int32 message_date,
const RepliedMessageInfo &replied_message_info) {
const RepliedMessageInfo &replied_message_info,
bool is_business_message) {
CHECK(action_ptr != nullptr);
if (is_business_message) {
switch (action_ptr->get_id()) {
case telegram_api::messageActionEmpty::ID:
case telegram_api::messageActionChatCreate::ID:
case telegram_api::messageActionChatEditTitle::ID:
case telegram_api::messageActionChatEditPhoto::ID:
case telegram_api::messageActionChatDeletePhoto::ID:
case telegram_api::messageActionChatAddUser::ID:
case telegram_api::messageActionChatJoinedByLink::ID:
case telegram_api::messageActionChatDeleteUser::ID:
case telegram_api::messageActionChatMigrateTo::ID:
case telegram_api::messageActionChannelCreate::ID:
case telegram_api::messageActionChannelMigrateFrom::ID:
case telegram_api::messageActionPaymentSent::ID:
case telegram_api::messageActionPaymentSentMe::ID:
case telegram_api::messageActionBotAllowed::ID:
case telegram_api::messageActionSecureValuesSent::ID:
case telegram_api::messageActionSecureValuesSentMe::ID:
case telegram_api::messageActionGroupCall::ID:
case telegram_api::messageActionInviteToGroupCall::ID:
case telegram_api::messageActionGroupCallScheduled::ID:
case telegram_api::messageActionChatJoinedByRequest::ID:
case telegram_api::messageActionWebViewDataSent::ID:
case telegram_api::messageActionWebViewDataSentMe::ID:
case telegram_api::messageActionTopicCreate::ID:
case telegram_api::messageActionTopicEdit::ID:
case telegram_api::messageActionRequestedPeer::ID:
case telegram_api::messageActionGiveawayLaunch::ID:
case telegram_api::messageActionGiveawayResults::ID:
case telegram_api::messageActionBoostApply::ID:
LOG(ERROR) << "Receive business " << to_string(action_ptr);
break;
case telegram_api::messageActionHistoryClear::ID:
case telegram_api::messageActionPinMessage::ID:
case telegram_api::messageActionGameScore::ID:
case telegram_api::messageActionPhoneCall::ID:
case telegram_api::messageActionScreenshotTaken::ID:
case telegram_api::messageActionCustomAction::ID:
case telegram_api::messageActionContactSignUp::ID:
case telegram_api::messageActionGeoProximityReached::ID:
case telegram_api::messageActionSetMessagesTTL::ID:
case telegram_api::messageActionSetChatTheme::ID:
case telegram_api::messageActionGiftPremium::ID:
case telegram_api::messageActionSuggestProfilePhoto::ID:
case telegram_api::messageActionSetChatWallPaper::ID:
case telegram_api::messageActionGiftCode::ID:
case telegram_api::messageActionRequestedPeerSentMe::ID:
// ok
break;
default:
UNREACHABLE();
}
}
switch (action_ptr->get_id()) {
case telegram_api::messageActionEmpty::ID:
LOG(ERROR) << "Receive empty message action in " << owner_dialog_id;
@ -6649,12 +6702,10 @@ unique_ptr<MessageContent> get_action_message_content(Td *td, tl_object_ptr<tele
}
return make_unique<MessageChatChangePhoto>(std::move(photo));
}
case telegram_api::messageActionChatDeletePhoto::ID: {
case telegram_api::messageActionChatDeletePhoto::ID:
return make_unique<MessageChatDeletePhoto>();
}
case telegram_api::messageActionHistoryClear::ID: {
case telegram_api::messageActionHistoryClear::ID:
return make_unique<MessageChatDeleteHistory>();
}
case telegram_api::messageActionChatAddUser::ID: {
auto action = move_tl_object_as<telegram_api::messageActionChatAddUser>(action_ptr);
@ -6777,9 +6828,8 @@ unique_ptr<MessageContent> get_action_message_content(Td *td, tl_object_ptr<tele
result->provider_payment_charge_id = std::move(action->charge_->provider_charge_id_);
return std::move(result);
}
case telegram_api::messageActionScreenshotTaken::ID: {
case telegram_api::messageActionScreenshotTaken::ID:
return make_unique<MessageScreenshotTaken>();
}
case telegram_api::messageActionCustomAction::ID: {
auto action = move_tl_object_as<telegram_api::messageActionCustomAction>(action_ptr);
return td::make_unique<MessageCustomServiceAction>(std::move(action->message_));
@ -6813,10 +6863,11 @@ unique_ptr<MessageContent> get_action_message_content(Td *td, tl_object_ptr<tele
get_encrypted_secure_values(td->file_manager_.get(), std::move(action->values_)),
get_encrypted_secure_credentials(std::move(action->credentials_)));
}
case telegram_api::messageActionContactSignUp::ID: {
LOG_IF(ERROR, td->auth_manager_->is_bot()) << "Receive ContactRegistered in " << owner_dialog_id;
case telegram_api::messageActionContactSignUp::ID:
if (!is_business_message && td->auth_manager_->is_bot()) {
LOG(ERROR) << "Receive ContactRegistered in " << owner_dialog_id;
}
return td::make_unique<MessageContactRegistered>();
}
case telegram_api::messageActionGeoProximityReached::ID: {
auto action = move_tl_object_as<telegram_api::messageActionGeoProximityReached>(action_ptr);
DialogId traveler_id(action->from_id_);

View File

@ -249,7 +249,8 @@ unique_ptr<MessageContent> dup_message_content(Td *td, DialogId dialog_id, const
unique_ptr<MessageContent> get_action_message_content(Td *td, tl_object_ptr<telegram_api::MessageAction> &&action_ptr,
DialogId owner_dialog_id, int32 message_date,
const RepliedMessageInfo &replied_message_info);
const RepliedMessageInfo &replied_message_info,
bool is_business_message);
tl_object_ptr<td_api::MessageContent> get_message_content_object(const MessageContent *content, Td *td,
DialogId dialog_id, int32 message_date,

View File

@ -13179,10 +13179,12 @@ void MessagesManager::finish_add_secret_message(unique_ptr<PendingSecretMessage>
}
MessagesManager::MessageInfo MessagesManager::parse_telegram_api_message(
Td *td, tl_object_ptr<telegram_api::Message> message_ptr, bool is_scheduled, const char *source) {
Td *td, tl_object_ptr<telegram_api::Message> message_ptr, bool is_scheduled, bool is_business_message,
const char *source) {
LOG(DEBUG) << "Receive from " << source << " " << to_string(message_ptr);
LOG_CHECK(message_ptr != nullptr) << source;
auto is_bot = td->auth_manager_->is_bot();
MessageInfo message_info;
message_info.message_id = MessageId::get_message_id(message_ptr, is_scheduled);
switch (message_ptr->get_id()) {
@ -13246,7 +13248,7 @@ MessagesManager::MessageInfo MessagesManager::parse_telegram_api_message(
message_info.effect_id = MessageEffectId(message->effect_);
bool is_content_read = true;
if (!td->auth_manager_->is_bot()) {
if (!is_bot) {
if (is_scheduled) {
is_content_read = false;
} else if (td->messages_manager_->is_message_auto_read(message_info.dialog_id, message_info.is_outgoing)) {
@ -13260,8 +13262,7 @@ MessagesManager::MessageInfo MessagesManager::parse_telegram_api_message(
message_info.content = get_message_content(
td,
get_message_text(td->user_manager_.get(), std::move(message->message_), std::move(message->entities_), true,
td->auth_manager_->is_bot(),
message_info.forward_header ? message_info.forward_header->date_ : message_info.date,
is_bot, message_info.forward_header ? message_info.forward_header->date_ : message_info.date,
message_info.media_album_id != 0, new_source.c_str()),
std::move(message->media_), message_info.dialog_id, message_info.date, is_content_read,
message_info.via_bot_user_id, &message_info.ttl, &message_info.disable_web_page_preview, new_source.c_str());
@ -13269,7 +13270,7 @@ MessagesManager::MessageInfo MessagesManager::parse_telegram_api_message(
message_info.restriction_reasons = get_restriction_reasons(std::move(message->restriction_reason_));
message_info.author_signature = std::move(message->post_author_);
message_info.sender_boost_count = message->from_boosts_applied_;
if (message->saved_peer_id_ != nullptr) {
if (!is_bot && message->saved_peer_id_ != nullptr) {
message_info.saved_messages_topic_id = SavedMessagesTopicId(DialogId(message->saved_peer_id_));
}
break;
@ -13297,10 +13298,10 @@ MessagesManager::MessageInfo MessagesManager::parse_telegram_api_message(
message_info.message_id, message_info.date, can_have_thread);
message_info.content =
get_action_message_content(td, std::move(message->action_), message_info.dialog_id, message_info.date,
message_info.reply_header.replied_message_info_);
message_info.reply_header.replied_message_info_, is_business_message);
message_info.reply_header.replied_message_info_ = RepliedMessageInfo();
message_info.reply_header.story_full_id_ = StoryFullId();
if (message_info.dialog_id == td->dialog_manager_->get_my_dialog_id()) {
if (!is_bot && message_info.dialog_id == td->dialog_manager_->get_my_dialog_id()) {
message_info.saved_messages_topic_id = SavedMessagesTopicId(message_info.dialog_id);
}
break;
@ -13415,7 +13416,7 @@ std::pair<DialogId, unique_ptr<MessagesManager::Message>> MessagesManager::creat
if (reply_to_story_full_id != StoryFullId()) {
auto story_dialog_id = reply_to_story_full_id.get_dialog_id();
if (story_dialog_id != my_dialog_id && story_dialog_id != dialog_id &&
story_dialog_id != DialogId(sender_user_id)) {
story_dialog_id != DialogId(sender_user_id) && !is_business_message) {
LOG(ERROR) << "Receive reply to " << reply_to_story_full_id << " in " << dialog_id;
reply_to_story_full_id = {};
}
@ -13673,8 +13674,8 @@ void MessagesManager::delete_update_message_id(DialogId dialog_id, MessageId mes
MessageFullId MessagesManager::on_get_message(tl_object_ptr<telegram_api::Message> message_ptr, bool from_update,
bool is_channel_message, bool is_scheduled, const char *source) {
return on_get_message(parse_telegram_api_message(td_, std::move(message_ptr), is_scheduled, source), from_update,
is_channel_message, source);
return on_get_message(parse_telegram_api_message(td_, std::move(message_ptr), is_scheduled, false, source),
from_update, is_channel_message, source);
}
MessageFullId MessagesManager::on_get_message(MessageInfo &&message_info, const bool from_update,
@ -22654,7 +22655,7 @@ td_api::object_ptr<td_api::MessageContent> MessagesManager::get_message_message_
td_api::object_ptr<td_api::message> MessagesManager::get_dialog_event_log_message_object(
DialogId dialog_id, tl_object_ptr<telegram_api::Message> &&message, DialogId &sender_dialog_id) {
auto dialog_message = create_message(
td_, parse_telegram_api_message(td_, std::move(message), false, "get_dialog_event_log_message_object"),
td_, parse_telegram_api_message(td_, std::move(message), false, false, "get_dialog_event_log_message_object"),
dialog_id.get_type() == DialogType::Channel, false, "get_dialog_event_log_message_object");
const Message *m = dialog_message.second.get();
if (m == nullptr || dialog_message.first != dialog_id) {
@ -22705,8 +22706,8 @@ td_api::object_ptr<td_api::message> MessagesManager::get_business_message_messag
return nullptr;
}
auto dialog_message = create_message(
td_, parse_telegram_api_message(td_, std::move(message), false, "get_business_message_message_object"), false,
true, "get_business_message_message_object");
td_, parse_telegram_api_message(td_, std::move(message), false, true, "get_business_message_message_object"),
false, true, "get_business_message_message_object");
const Message *m = dialog_message.second.get();
if (m == nullptr) {
return nullptr;
@ -22724,7 +22725,7 @@ td_api::object_ptr<td_api::message> MessagesManager::get_business_message_messag
auto forward_info =
m->forward_info == nullptr ? nullptr : m->forward_info->get_message_forward_info_object(td_, false);
auto import_info = m->forward_info == nullptr ? nullptr : m->forward_info->get_message_import_info_object();
auto can_be_saved = can_save_message(dialog_id, m);
auto can_be_saved = !m->noforwards && !m->is_content_secret;
auto via_bot_user_id =
td_->user_manager_->get_user_id_object(m->via_bot_user_id, "get_business_message_message_object via_bot_user_id");
auto via_business_bot_user_id = td_->user_manager_->get_user_id_object(

View File

@ -1666,7 +1666,7 @@ class MessagesManager final : public Actor {
Promise<Unit> promise);
static MessageInfo parse_telegram_api_message(Td *td, tl_object_ptr<telegram_api::Message> message_ptr,
bool is_scheduled, const char *source);
bool is_scheduled, bool is_business_message, const char *source);
static std::pair<DialogId, unique_ptr<Message>> create_message(Td *td, MessageInfo &&message_info,
bool is_channel_message, bool is_business_message,