// // Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023 // // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // #include "td/telegram/NotificationType.h" #include "td/telegram/AnimationsManager.h" #include "td/telegram/AudiosManager.h" #include "td/telegram/DocumentsManager.h" #include "td/telegram/Global.h" #include "td/telegram/MessageSender.h" #include "td/telegram/MessagesManager.h" #include "td/telegram/PhotoFormat.h" #include "td/telegram/StickersManager.h" #include "td/telegram/Td.h" #include "td/telegram/VideoNotesManager.h" #include "td/telegram/VideosManager.h" #include "td/telegram/VoiceNotesManager.h" #include "td/utils/misc.h" #include "td/utils/Slice.h" #include namespace td { class NotificationTypeMessage final : public NotificationType { bool can_be_delayed() const final { return message_id_.is_valid() && message_id_.is_server(); } bool is_temporary() const final { return false; } MessageId get_message_id() const final { return message_id_; } vector get_file_ids(const Td *td) const final { return {}; } td_api::object_ptr get_notification_type_object(Td *td, DialogId dialog_id) const final { auto message_object = td->messages_manager_->get_message_object({dialog_id, message_id_}, "get_notification_type_object"); if (message_object == nullptr) { return nullptr; } return td_api::make_object(std::move(message_object), show_preview_); } StringBuilder &to_string_builder(StringBuilder &string_builder) const final { return string_builder << "NewMessageNotification[" << message_id_ << ']'; } MessageId message_id_; bool show_preview_; public: NotificationTypeMessage(MessageId message_id, bool show_preview) : message_id_(message_id), show_preview_(show_preview) { } }; class NotificationTypeSecretChat final : public NotificationType { bool can_be_delayed() const final { return false; } bool is_temporary() const final { return false; } MessageId get_message_id() const final { return MessageId(); } vector get_file_ids(const Td *td) const final { return {}; } td_api::object_ptr get_notification_type_object(Td *, DialogId) const final { return td_api::make_object(); } StringBuilder &to_string_builder(StringBuilder &string_builder) const final { return string_builder << "NewSecretChatNotification[]"; } public: NotificationTypeSecretChat() { } }; class NotificationTypeCall final : public NotificationType { bool can_be_delayed() const final { return false; } bool is_temporary() const final { return false; } MessageId get_message_id() const final { return MessageId::max(); } vector get_file_ids(const Td *td) const final { return {}; } td_api::object_ptr get_notification_type_object(Td *, DialogId) const final { return td_api::make_object(call_id_.get()); } StringBuilder &to_string_builder(StringBuilder &string_builder) const final { return string_builder << "NewCallNotification[" << call_id_ << ']'; } CallId call_id_; public: explicit NotificationTypeCall(CallId call_id) : call_id_(call_id) { } }; class NotificationTypePushMessage final : public NotificationType { bool can_be_delayed() const final { return false; } bool is_temporary() const final { return true; } MessageId get_message_id() const final { return message_id_; } vector get_file_ids(const Td *td) const final { if (!document_.empty()) { return document_.get_file_ids(td); } return photo_get_file_ids(photo_); } static td_api::object_ptr get_push_message_content_object(Td *td, Slice key, const string &arg, const Photo &photo, const Document &document) { bool is_pinned = false; if (begins_with(key, "PINNED_")) { is_pinned = true; key = key.substr(7); } if (key == "MESSAGE") { return td_api::make_object(is_pinned); } if (key == "MESSAGES") { return td_api::make_object(to_integer(arg), true, true, false, false); } CHECK(key.size() > 8); switch (key[8]) { case 'A': if (key == "MESSAGE_ANIMATION") { return td_api::make_object( td->animations_manager_->get_animation_object(document.file_id), arg, is_pinned); } if (key == "MESSAGE_AUDIO") { return td_api::make_object( td->audios_manager_->get_audio_object(document.file_id), is_pinned); } if (key == "MESSAGE_AUDIOS") { return td_api::make_object(to_integer(arg), false, false, true, false); } break; case 'B': if (key == "MESSAGE_BASIC_GROUP_CHAT_CREATE") { return td_api::make_object(); } break; case 'C': if (key == "MESSAGE_CHAT_ADD_MEMBERS") { return td_api::make_object(arg, false, false); } if (key == "MESSAGE_CHAT_ADD_MEMBERS_RETURNED") { return td_api::make_object(arg, false, true); } if (key == "MESSAGE_CHAT_ADD_MEMBERS_YOU") { return td_api::make_object(arg, true, false); } if (key == "MESSAGE_CHAT_CHANGE_PHOTO") { return td_api::make_object(); } if (key == "MESSAGE_CHAT_CHANGE_THEME") { return td_api::make_object(arg); } if (key == "MESSAGE_CHAT_CHANGE_TITLE") { return td_api::make_object(arg); } if (key == "MESSAGE_CHAT_DELETE_MEMBER") { return td_api::make_object(arg, false, false); } if (key == "MESSAGE_CHAT_DELETE_MEMBER_LEFT") { return td_api::make_object(arg, false, true); } if (key == "MESSAGE_CHAT_DELETE_MEMBER_YOU") { return td_api::make_object(arg, true, false); } if (key == "MESSAGE_CHAT_JOIN_BY_LINK") { return td_api::make_object(); } if (key == "MESSAGE_CHAT_JOIN_BY_REQUEST") { return td_api::make_object(); } if (key == "MESSAGE_CONTACT") { return td_api::make_object(arg, is_pinned); } if (key == "MESSAGE_CONTACT_REGISTERED") { return td_api::make_object(); } break; case 'D': if (key == "MESSAGE_DOCUMENT") { return td_api::make_object( td->documents_manager_->get_document_object(document.file_id, PhotoFormat::Jpeg), is_pinned); } if (key == "MESSAGE_DOCUMENTS") { return td_api::make_object(to_integer(arg), false, false, false, true); } break; case 'F': if (key == "MESSAGE_FORWARDS") { return td_api::make_object(to_integer(arg)); } break; case 'G': if (key == "MESSAGE_GAME") { return td_api::make_object(arg, is_pinned); } if (key == "MESSAGE_GAME_SCORE") { int32 score = 0; string title; if (!is_pinned) { string score_str; std::tie(score_str, title) = split(arg); score = to_integer(score_str); } return td_api::make_object(title, score, is_pinned); } break; case 'I': if (key == "MESSAGE_INVOICE") { return td_api::make_object(arg, is_pinned); } break; case 'L': if (key == "MESSAGE_LIVE_LOCATION") { return td_api::make_object(false, is_pinned); } if (key == "MESSAGE_LOCATION") { return td_api::make_object(true, is_pinned); } break; case 'P': if (key == "MESSAGE_PHOTO") { return td_api::make_object(get_photo_object(td->file_manager_.get(), photo), arg, false, is_pinned); } if (key == "MESSAGE_PHOTOS") { return td_api::make_object(to_integer(arg), true, false, false, false); } if (key == "MESSAGE_POLL") { return td_api::make_object(arg, true, is_pinned); } break; case 'Q': if (key == "MESSAGE_QUIZ") { return td_api::make_object(arg, false, is_pinned); } break; case 'R': if (key == "MESSAGE_RECURRING_PAYMENT") { return td_api::make_object(arg); } break; case 'S': if (key == "MESSAGE_SAME_WALLPAPER") { return td_api::make_object(true); } if (key == "MESSAGE_SCREENSHOT_TAKEN") { return td_api::make_object(); } if (key == "MESSAGE_SECRET_PHOTO") { return td_api::make_object(nullptr, arg, true, false); } if (key == "MESSAGE_SECRET_VIDEO") { return td_api::make_object(nullptr, arg, true, false); } if (key == "MESSAGE_STICKER") { return td_api::make_object( td->stickers_manager_->get_sticker_object(document.file_id), trim(arg), is_pinned); } if (key == "MESSAGE_STORY") { return td_api::make_object(is_pinned); } if (key == "MESSAGE_SUGGEST_PHOTO") { return td_api::make_object(); } break; case 'T': if (key == "MESSAGE_TEXT") { return td_api::make_object(arg, is_pinned); } break; case 'V': if (key == "MESSAGE_VIDEO") { return td_api::make_object( td->videos_manager_->get_video_object(document.file_id), arg, false, is_pinned); } if (key == "MESSAGE_VIDEO_NOTE") { return td_api::make_object( td->video_notes_manager_->get_video_note_object(document.file_id), is_pinned); } if (key == "MESSAGE_VIDEOS") { return td_api::make_object(to_integer(arg), false, true, false, false); } if (key == "MESSAGE_VOICE_NOTE") { return td_api::make_object( td->voice_notes_manager_->get_voice_note_object(document.file_id), is_pinned); } break; case 'W': if (key == "MESSAGE_WALLPAPER") { return td_api::make_object(false); } break; default: break; } UNREACHABLE(); } td_api::object_ptr get_notification_type_object(Td *td, DialogId) const final { auto sender = get_message_sender_object(td, sender_user_id_, sender_dialog_id_, "get_notification_type_object"); return td_api::make_object( message_id_.get(), std::move(sender), sender_name_, is_outgoing_, get_push_message_content_object(td, key_, arg_, photo_, document_)); } StringBuilder &to_string_builder(StringBuilder &string_builder) const final { return string_builder << "NewPushMessageNotification[" << sender_user_id_ << "/" << sender_dialog_id_ << "/\"" << sender_name_ << "\", " << message_id_ << ", " << key_ << ", " << arg_ << ", " << photo_ << ", " << document_ << ']'; } UserId sender_user_id_; DialogId sender_dialog_id_; MessageId message_id_; string sender_name_; string key_; string arg_; Photo photo_; Document document_; bool is_outgoing_; public: NotificationTypePushMessage(UserId sender_user_id, DialogId sender_dialog_id, string sender_name, bool is_outgoing, MessageId message_id, string key, string arg, Photo photo, Document document) : sender_user_id_(sender_user_id) , sender_dialog_id_(sender_dialog_id) , message_id_(message_id) , sender_name_(std::move(sender_name)) , key_(std::move(key)) , arg_(std::move(arg)) , photo_(std::move(photo)) , document_(std::move(document)) , is_outgoing_(is_outgoing) { } }; unique_ptr create_new_message_notification(MessageId message_id, bool show_preview) { return make_unique(message_id, show_preview); } unique_ptr create_new_secret_chat_notification() { return make_unique(); } unique_ptr create_new_call_notification(CallId call_id) { return make_unique(call_id); } unique_ptr create_new_push_message_notification(UserId sender_user_id, DialogId sender_dialog_id, string sender_name, bool is_outgoing, MessageId message_id, string key, string arg, Photo photo, Document document) { return td::make_unique(sender_user_id, sender_dialog_id, std::move(sender_name), is_outgoing, message_id, std::move(key), std::move(arg), std::move(photo), std::move(document)); } } // namespace td