Skip to content

Commit e32e1c6

Browse files
committed
Add td_api::MessageSender class.
GitOrigin-RevId: 082a1e74188bffc590c4a4182793a9d94b60c148
1 parent d968fd0 commit e32e1c6

File tree

6 files changed

+62
-23
lines changed

6 files changed

+62
-23
lines changed

example/cpp/td_example.cpp

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,14 +172,22 @@ class TdExample {
172172
}
173173
}
174174

175-
std::string get_user_name(std::int32_t user_id) {
175+
std::string get_user_name(std::int32_t user_id) const {
176176
auto it = users_.find(user_id);
177177
if (it == users_.end()) {
178178
return "unknown user";
179179
}
180180
return it->second->first_name_ + " " + it->second->last_name_;
181181
}
182182

183+
std::string get_chat_title(std::int64_t chat_id) const {
184+
auto it = chat_title_.find(chat_id);
185+
if (it == chat_title_.end()) {
186+
return "unknown chat";
187+
}
188+
return it->second;
189+
}
190+
183191
void process_update(td_api::object_ptr<td_api::Object> update) {
184192
td_api::downcast_call(
185193
*update, overloaded(
@@ -199,13 +207,21 @@ class TdExample {
199207
},
200208
[this](td_api::updateNewMessage &update_new_message) {
201209
auto chat_id = update_new_message.message_->chat_id_;
202-
auto sender_user_name = get_user_name(update_new_message.message_->sender_user_id_);
210+
std::string sender_name;
211+
td_api::downcast_call(*update_new_message.message_->sender_,
212+
overloaded(
213+
[this, &sender_name](td_api::messageSenderUser &user) {
214+
sender_name = get_user_name(user.user_id_);
215+
},
216+
[this, &sender_name](td_api::messageSenderChat &chat) {
217+
sender_name = get_chat_title(chat.chat_id_);
218+
}));
203219
std::string text;
204220
if (update_new_message.message_->content_->get_id() == td_api::messageText::ID) {
205221
text = static_cast<td_api::messageText &>(*update_new_message.message_->content_).text_->text_;
206222
}
207-
std::cout << "Got message: [chat_id:" << chat_id << "] [from:" << sender_user_name << "] ["
208-
<< text << "]" << std::endl;
223+
std::cout << "Got message: [chat_id:" << chat_id << "] [from:" << sender_name << "] [" << text
224+
<< "]" << std::endl;
209225
},
210226
[](auto &update) {}));
211227
}

td/generate/scheme/td_api.tl

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -613,15 +613,24 @@ secretChatStateClosed = SecretChatState;
613613
secretChat id:int32 user_id:int32 state:SecretChatState is_outbound:Bool ttl:int32 key_hash:bytes layer:int32 = SecretChat;
614614

615615

616+
//@class MessageSender @description Contains information about the sender of a message
617+
618+
//@description The message was sent by a known user @user_id Identifier of the user that sent the message
619+
messageSenderUser user_id:int32 = MessageSender;
620+
621+
//@description The message was sent on behalf of a chat @chat_id Identifier of the chat that sent the message
622+
messageSenderChat chat_id:int53 = MessageSender;
623+
624+
616625
//@class MessageForwardOrigin @description Contains information about the origin of a forwarded message
617626

618-
//@description The message was originally written by a known user @sender_user_id Identifier of the user that originally sent the message
627+
//@description The message was originally sent by a known user @sender_user_id Identifier of the user that originally sent the message
619628
messageForwardOriginUser sender_user_id:int32 = MessageForwardOrigin;
620629

621-
//@description The message was originally written by an anonymous chat administrator on behalf of the chat @sender_chat_id Identifier of the chat that originally sent the message
630+
//@description The message was originally sent by an anonymous chat administrator on behalf of the chat @sender_chat_id Identifier of the chat that originally sent the message
622631
messageForwardOriginChat sender_chat_id:int53 = MessageForwardOrigin;
623632

624-
//@description The message was originally written by a user, which is hidden by their privacy settings @sender_name Name of the sender
633+
//@description The message was originally sent by a user, which is hidden by their privacy settings @sender_name Name of the sender
625634
messageForwardOriginHiddenUser sender_name:string = MessageForwardOrigin;
626635

627636
//@description The message was originally a post in a channel
@@ -666,8 +675,7 @@ messageSendingStateFailed error_code:int32 error_message:string can_retry:Bool r
666675

667676
//@description Describes a message
668677
//@id Message identifier; unique for the chat to which the message belongs
669-
//@sender_user_id Identifier of the user who sent the message; 0 if unknown. Currently, it is unknown for channel posts, for channel posts automatically forwarded to discussion group and for anonymously sent supergroup messages
670-
//@sender_chat_id Identifier of the chat on behalf of which the message was sent; 0 if none
678+
//@sender The sender of the message
671679
//@chat_id Chat identifier
672680
//@sending_state Information about the sending state of the message; may be null
673681
//@scheduling_state Information about the scheduling state of the message; may be null
@@ -695,7 +703,7 @@ messageSendingStateFailed error_code:int32 error_message:string can_retry:Bool r
695703
//@restriction_reason If non-empty, contains a human-readable description of the reason why access to this message must be restricted
696704
//@content Content of the message
697705
//@reply_markup Reply markup for the message; may be null
698-
message id:int53 sender_user_id:int32 sender_chat_id:int53 chat_id:int53 sending_state:MessageSendingState scheduling_state:MessageSchedulingState is_outgoing:Bool can_be_edited:Bool can_be_forwarded:Bool can_be_deleted_only_for_self:Bool can_be_deleted_for_all_users:Bool can_get_statistics:Bool can_get_message_thread:Bool is_channel_post:Bool contains_unread_mention:Bool date:int32 edit_date:int32 forward_info:messageForwardInfo interaction_info:messageInteractionInfo reply_in_chat_id:int53 reply_to_message_id:int53 message_thread_id:int53 ttl:int32 ttl_expires_in:double via_bot_user_id:int32 author_signature:string media_album_id:int64 restriction_reason:string content:MessageContent reply_markup:ReplyMarkup = Message;
706+
message id:int53 sender:MessageSender chat_id:int53 sending_state:MessageSendingState scheduling_state:MessageSchedulingState is_outgoing:Bool can_be_edited:Bool can_be_forwarded:Bool can_be_deleted_only_for_self:Bool can_be_deleted_for_all_users:Bool can_get_statistics:Bool can_get_message_thread:Bool is_channel_post:Bool contains_unread_mention:Bool date:int32 edit_date:int32 forward_info:messageForwardInfo interaction_info:messageInteractionInfo reply_in_chat_id:int53 reply_to_message_id:int53 message_thread_id:int53 ttl:int32 ttl_expires_in:double via_bot_user_id:int32 author_signature:string media_album_id:int64 restriction_reason:string content:MessageContent reply_markup:ReplyMarkup = Message;
699707

700708
//@description Contains a list of messages @total_count Approximate total count of messages found @messages List of messages; messages may be null
701709
messages total_count:int32 messages:vector<message> = Messages;
@@ -2558,12 +2566,11 @@ notificationTypeNewCall call_id:int32 = NotificationType;
25582566

25592567
//@description New message was received through a push notification
25602568
//@message_id The message identifier. The message will not be available in the chat history, but the ID can be used in viewMessages, or as reply_to_message_id
2561-
//@sender_user_id Sender of the message; 0 if unknown. Corresponding user may be inaccessible
2562-
//@sender_chat_id Sender chat of the message; 0 if none
2569+
//@sender The sender of the message. Corresponding user or chat may be inaccessible
25632570
//@sender_name Name of the sender; can be different from the name of the sender user
25642571
//@is_outgoing True, if the message is outgoing
25652572
//@content Push message content
2566-
notificationTypeNewPushMessage message_id:int53 sender_user_id:int32 sender_chat_id:int53 sender_name:string is_outgoing:Bool content:PushMessageContent = NotificationType;
2573+
notificationTypeNewPushMessage message_id:int53 sender:MessageSender sender_name:string is_outgoing:Bool content:PushMessageContent = NotificationType;
25672574

25682575

25692576
//@class NotificationGroupType @description Describes the type of notifications in a notification group

td/generate/scheme/td_api.tlo

132 Bytes
Binary file not shown.

td/telegram/MessagesManager.cpp

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5528,6 +5528,20 @@ void MessagesManager::on_preload_folder_dialog_list_timeout_callback(void *messa
55285528
FolderId(narrow_cast<int32>(folder_id_int)));
55295529
}
55305530

5531+
td_api::object_ptr<td_api::MessageSender> MessagesManager::get_message_sender_object(UserId user_id,
5532+
DialogId dialog_id) const {
5533+
if (dialog_id.is_valid()) {
5534+
CHECK(have_dialog(dialog_id));
5535+
return td_api::make_object<td_api::messageSenderChat>(dialog_id.get());
5536+
}
5537+
if (!user_id.is_valid()) {
5538+
// can happen only if the server sends a message with wrong sender
5539+
user_id = td_->contacts_manager_->add_service_notifications_user();
5540+
}
5541+
return td_api::make_object<td_api::messageSenderUser>(
5542+
td_->contacts_manager_->get_user_id_object(user_id, "get_message_sender_object"));
5543+
}
5544+
55315545
BufferSlice MessagesManager::get_dialog_database_value(const Dialog *d) {
55325546
// can't use log_event_store, because it tries to parse stored Dialog
55335547
LogEventStorerCalcLength storer_calc_length;
@@ -21709,13 +21723,13 @@ tl_object_ptr<td_api::message> MessagesManager::get_message_object(DialogId dial
2170921723
auto date = is_scheduled ? 0 : m->date;
2171021724
auto edit_date = m->hide_edit_date ? 0 : m->edit_date;
2171121725
return make_tl_object<td_api::message>(
21712-
m->message_id.get(), td_->contacts_manager_->get_user_id_object(m->sender_user_id, "sender_user_id"),
21713-
m->sender_dialog_id.get(), dialog_id.get(), std::move(sending_state), std::move(scheduling_state), is_outgoing,
21714-
can_be_edited, can_be_forwarded, can_delete_for_self, can_delete_for_all_users, can_get_statistics,
21715-
can_get_message_thread, m->is_channel_post, contains_unread_mention, date, edit_date,
21716-
get_message_forward_info_object(m->forward_info), get_message_interaction_info_object(dialog_id, m),
21717-
reply_in_dialog_id.get(), reply_to_message_id, top_thread_message_id, ttl, ttl_expires_in, via_bot_user_id,
21718-
m->author_signature, media_album_id, get_restriction_reason_description(m->restriction_reasons),
21726+
m->message_id.get(), get_message_sender_object(m->sender_user_id, m->sender_dialog_id), dialog_id.get(),
21727+
std::move(sending_state), std::move(scheduling_state), is_outgoing, can_be_edited, can_be_forwarded,
21728+
can_delete_for_self, can_delete_for_all_users, can_get_statistics, can_get_message_thread, m->is_channel_post,
21729+
contains_unread_mention, date, edit_date, get_message_forward_info_object(m->forward_info),
21730+
get_message_interaction_info_object(dialog_id, m), reply_in_dialog_id.get(), reply_to_message_id,
21731+
top_thread_message_id, ttl, ttl_expires_in, via_bot_user_id, m->author_signature, media_album_id,
21732+
get_restriction_reason_description(m->restriction_reasons),
2171921733
get_message_content_object(m->content.get(), td_, live_location_date, m->is_content_secret),
2172021734
get_reply_markup_object(m->reply_markup));
2172121735
}

td/telegram/MessagesManager.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@ class MessagesManager : public Actor {
190190
MessagesManager &operator=(MessagesManager &&) = delete;
191191
~MessagesManager() override;
192192

193+
td_api::object_ptr<td_api::MessageSender> get_message_sender_object(UserId user_id, DialogId dialog_id) const;
194+
193195
static vector<MessageId> get_message_ids(const vector<int64> &input_message_ids);
194196

195197
static vector<int32> get_server_message_ids(const vector<MessageId> &message_ids);

td/telegram/NotificationType.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -318,10 +318,10 @@ class NotificationTypePushMessage : public NotificationType {
318318
}
319319

320320
td_api::object_ptr<td_api::NotificationType> get_notification_type_object(DialogId dialog_id) const override {
321-
auto sender_user_id = G()->td().get_actor_unsafe()->contacts_manager_->get_user_id_object(
322-
sender_user_id_, "get_notification_type_object");
321+
auto sender =
322+
G()->td().get_actor_unsafe()->messages_manager_->get_message_sender_object(sender_user_id_, sender_dialog_id_);
323323
return td_api::make_object<td_api::notificationTypeNewPushMessage>(
324-
message_id_.get(), sender_user_id, sender_dialog_id_.get(), sender_name_, is_outgoing_,
324+
message_id_.get(), std::move(sender), sender_name_, is_outgoing_,
325325
get_push_message_content_object(key_, arg_, photo_, document_));
326326
}
327327

0 commit comments

Comments
 (0)