From dd6fd86637f2bfa54a3994366b1ab5130bf2a4fa Mon Sep 17 00:00:00 2001 From: levlam Date: Tue, 17 Oct 2023 15:09:57 +0300 Subject: [PATCH] Add chat.accentColorId. --- td/generate/scheme/td_api.tl | 8 +++-- td/telegram/AccentColorId.h | 1 + td/telegram/ContactsManager.cpp | 53 ++++++++++++++++++++++++++++----- td/telegram/ContactsManager.h | 7 +++++ td/telegram/MessagesManager.cpp | 28 +++++++++++++++++ td/telegram/MessagesManager.h | 4 +++ 6 files changed, 92 insertions(+), 9 deletions(-) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index cc0484f12..57f1b8757 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -771,7 +771,7 @@ usernames active_usernames:vector disabled_usernames:vector edit //@phone_number Phone number of the user //@status Current online status of the user //@profile_photo Profile photo of the user; may be null -//@accent_color_id Identifier of the accent color for name, and backgrounds of profile photo, reply and link preview +//@accent_color_id Identifier of the accent color for name, and backgrounds of profile photo, reply header, and link preview //@emoji_status Emoji status to be shown instead of the default Telegram Premium badge; may be null. For Telegram Premium users only //@is_contact The user is a contact of the current user //@is_mutual_contact The user is a contact of the current user and the current user is a contact of the user @@ -1570,6 +1570,7 @@ videoChat group_call_id:int32 has_participants:Bool default_participant_id:Messa //@type Type of the chat //@title Chat title //@photo Chat photo; may be null +//@accent_color_id Identifier of the accent color for message sender name, and backgrounds of chat photo, reply header, and link preview //@permissions Actions that non-administrator chat members are allowed to take in the chat //@last_message Last message in the chat; may be null if none or unknown //@positions Positions of the chat in chat lists @@ -1599,7 +1600,7 @@ videoChat group_call_id:int32 has_participants:Bool default_participant_id:Messa //@reply_markup_message_id Identifier of the message from which reply markup needs to be used; 0 if there is no default custom reply markup in the chat //@draft_message A draft of a message in the chat; may be null if none //@client_data Application-specific data associated with the chat. (For example, the chat scroll position or local chat notification settings can be stored here.) Persistent if the message database is used -chat id:int53 type:ChatType title:string photo:chatPhotoInfo permissions:chatPermissions last_message:message positions:vector message_sender_id:MessageSender block_list:BlockList has_protected_content:Bool is_translatable:Bool is_marked_as_unread:Bool has_scheduled_messages:Bool can_be_deleted_only_for_self:Bool can_be_deleted_for_all_users:Bool can_be_reported:Bool default_disable_notification:Bool unread_count:int32 last_read_inbox_message_id:int53 last_read_outbox_message_id:int53 unread_mention_count:int32 unread_reaction_count:int32 notification_settings:chatNotificationSettings available_reactions:ChatAvailableReactions message_auto_delete_time:int32 background:chatBackground theme_name:string action_bar:ChatActionBar video_chat:videoChat pending_join_requests:chatJoinRequestsInfo reply_markup_message_id:int53 draft_message:draftMessage client_data:string = Chat; +chat id:int53 type:ChatType title:string photo:chatPhotoInfo accent_color_id:accentColorId permissions:chatPermissions last_message:message positions:vector message_sender_id:MessageSender block_list:BlockList has_protected_content:Bool is_translatable:Bool is_marked_as_unread:Bool has_scheduled_messages:Bool can_be_deleted_only_for_self:Bool can_be_deleted_for_all_users:Bool can_be_reported:Bool default_disable_notification:Bool unread_count:int32 last_read_inbox_message_id:int53 last_read_outbox_message_id:int53 unread_mention_count:int32 unread_reaction_count:int32 notification_settings:chatNotificationSettings available_reactions:ChatAvailableReactions message_auto_delete_time:int32 background:chatBackground theme_name:string action_bar:ChatActionBar video_chat:videoChat pending_join_requests:chatJoinRequestsInfo reply_markup_message_id:int53 draft_message:draftMessage client_data:string = Chat; //@description Represents a list of chats @total_count Approximate total number of chats found @chat_ids List of chat identifiers chats total_count:int32 chat_ids:vector = Chats; @@ -5997,6 +5998,9 @@ updateChatTitle chat_id:int53 title:string = Update; //@description A chat photo was changed @chat_id Chat identifier @photo The new chat photo; may be null updateChatPhoto chat_id:int53 photo:chatPhotoInfo = Update; +//@description A chat accent color has changed @chat_id Chat identifier @accent_color_id The new chat accent color identifier +updateChatAccentColorId chat_id:int53 accent_color_id:accentColorId = Update; + //@description Chat permissions was changed @chat_id Chat identifier @permissions The new chat permissions updateChatPermissions chat_id:int53 permissions:chatPermissions = Update; diff --git a/td/telegram/AccentColorId.h b/td/telegram/AccentColorId.h index c8c125c43..c7ce853a4 100644 --- a/td/telegram/AccentColorId.h +++ b/td/telegram/AccentColorId.h @@ -56,6 +56,7 @@ class AccentColorId { } td_api::object_ptr get_accent_color_id_object() const { + CHECK(is_valid()); return td_api::make_object(id); } diff --git a/td/telegram/ContactsManager.cpp b/td/telegram/ContactsManager.cpp index a4429c481..7e0ecd837 100644 --- a/td/telegram/ContactsManager.cpp +++ b/td/telegram/ContactsManager.cpp @@ -5654,6 +5654,31 @@ const DialogPhoto *ContactsManager::get_secret_chat_dialog_photo(SecretChatId se return get_user_dialog_photo(c->user_id); } +AccentColorId ContactsManager::get_user_accent_color_id(UserId user_id) const { + auto u = get_user(user_id); + if (u == nullptr || !u->accent_color_id.is_valid()) { + return AccentColorId(user_id); + } + + return u->accent_color_id; +} + +AccentColorId ContactsManager::get_chat_accent_color_id(ChatId chat_id) const { + return AccentColorId(chat_id); +} + +AccentColorId ContactsManager::get_channel_accent_color_id(ChannelId channel_id) const { + return AccentColorId(channel_id); +} + +AccentColorId ContactsManager::get_secret_chat_accent_color_id(SecretChatId secret_chat_id) const { + auto c = get_secret_chat(secret_chat_id); + if (c == nullptr) { + return AccentColorId(0); + } + return get_user_accent_color_id(c->user_id); +} + string ContactsManager::get_user_title(UserId user_id) const { auto u = get_user(user_id); if (u == nullptr) { @@ -10082,6 +10107,7 @@ void ContactsManager::on_get_user(tl_object_ptr &&user_ptr, on_update_user_usernames(u, user_id, Usernames{std::move(user->username_), std::move(user->usernames_)}); } on_update_user_emoji_status(u, user_id, EmojiStatus(std::move(user->emoji_status_))); + on_update_user_accent_color_id(u, user_id, AccentColorId(user->color_)); bool is_verified = (flags & USER_FLAG_IS_VERIFIED) != 0; bool is_premium = (flags & USER_FLAG_IS_PREMIUM) != 0; @@ -10103,10 +10129,6 @@ void ContactsManager::on_get_user(tl_object_ptr &&user_ptr, bool stories_available = user->stories_max_id_ > 0; bool stories_unavailable = user->stories_unavailable_; bool stories_hidden = user->stories_hidden_; - AccentColorId accent_color_id; - if (AccentColorId(user->color_) != AccentColorId(user_id)) { - accent_color_id = AccentColorId(user->color_); - } LOG_IF(ERROR, !can_join_groups && !is_bot) << "Receive not bot " << user_id << " which can't join groups from " << source; @@ -10145,8 +10167,7 @@ void ContactsManager::on_get_user(tl_object_ptr &&user_ptr, can_join_groups != u->can_join_groups || can_read_all_group_messages != u->can_read_all_group_messages || restriction_reasons != u->restriction_reasons || is_scam != u->is_scam || is_fake != u->is_fake || is_inline_bot != u->is_inline_bot || inline_query_placeholder != u->inline_query_placeholder || - need_location_bot != u->need_location_bot || can_be_added_to_attach_menu != u->can_be_added_to_attach_menu || - accent_color_id != u->accent_color_id) { + need_location_bot != u->need_location_bot || can_be_added_to_attach_menu != u->can_be_added_to_attach_menu) { if (is_bot != u->is_bot) { LOG_IF(ERROR, !is_deleted && !u->is_deleted && u->is_received) << "User.is_bot has changed for " << user_id << "/" << u->usernames << " from " << source << " from " @@ -10165,7 +10186,6 @@ void ContactsManager::on_get_user(tl_object_ptr &&user_ptr, u->inline_query_placeholder = std::move(inline_query_placeholder); u->need_location_bot = need_location_bot; u->can_be_added_to_attach_menu = can_be_added_to_attach_menu; - u->accent_color_id = accent_color_id; LOG(DEBUG) << "Info has changed for " << user_id; u->is_changed = true; @@ -11760,6 +11780,14 @@ void ContactsManager::update_user(User *u, UserId user_id, bool from_binlog, boo }); u->is_photo_changed = false; } + if (u->is_accent_color_id_changed) { + auto messages_manager = td_->messages_manager_.get(); + messages_manager->on_dialog_accent_color_id_updated(DialogId(user_id)); + for_each_secret_chat_with_user(user_id, [messages_manager](SecretChatId secret_chat_id) { + messages_manager->on_dialog_accent_color_id_updated(DialogId(secret_chat_id)); + }); + u->is_accent_color_id_changed = false; + } if (u->is_phone_number_changed) { if (!u->phone_number.empty() && !td_->auth_manager_->is_bot()) { resolved_phone_numbers_[u->phone_number] = user_id; @@ -13357,6 +13385,17 @@ void ContactsManager::register_user_photo(User *u, UserId user_id, const Photo & } } +void ContactsManager::on_update_user_accent_color_id(User *u, UserId user_id, AccentColorId accent_color_id) { + if (accent_color_id == AccentColorId(user_id)) { + accent_color_id = AccentColorId(); + } + if (u->accent_color_id != accent_color_id) { + u->accent_color_id = accent_color_id; + u->is_accent_color_id_changed = true; + u->is_changed = true; + } +} + void ContactsManager::on_update_user_emoji_status(UserId user_id, tl_object_ptr &&emoji_status) { if (!user_id.is_valid()) { diff --git a/td/telegram/ContactsManager.h b/td/telegram/ContactsManager.h index cf3a699ef..88768cfdb 100644 --- a/td/telegram/ContactsManager.h +++ b/td/telegram/ContactsManager.h @@ -117,6 +117,11 @@ class ContactsManager final : public Actor { const DialogPhoto *get_channel_dialog_photo(ChannelId channel_id) const; const DialogPhoto *get_secret_chat_dialog_photo(SecretChatId secret_chat_id); + AccentColorId get_user_accent_color_id(UserId user_id) const; + AccentColorId get_chat_accent_color_id(ChatId chat_id) const; + AccentColorId get_channel_accent_color_id(ChannelId channel_id) const; + AccentColorId get_secret_chat_accent_color_id(SecretChatId secret_chat_id) const; + string get_user_title(UserId user_id) const; string get_chat_title(ChatId chat_id) const; string get_channel_title(ChannelId channel_id) const; @@ -790,6 +795,7 @@ class ContactsManager final : public Actor { bool is_name_changed = true; bool is_username_changed = true; bool is_photo_changed = true; + bool is_accent_color_id_changed = true; bool is_phone_number_changed = true; bool is_emoji_status_changed = true; bool is_is_contact_changed = true; @@ -1411,6 +1417,7 @@ class ContactsManager final : public Actor { void on_update_user_phone_number(User *u, UserId user_id, string &&phone_number); void on_update_user_photo(User *u, UserId user_id, tl_object_ptr &&photo, const char *source); + void on_update_user_accent_color_id(User *u, UserId user_id, AccentColorId accent_color_id); void on_update_user_emoji_status(User *u, UserId user_id, EmojiStatus emoji_status); void on_update_user_story_ids_impl(User *u, UserId user_id, StoryId max_active_story_id, StoryId max_read_story_id); void on_update_user_max_read_story_id(User *u, UserId user_id, StoryId max_read_story_id); diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index 819f336b0..c1a782290 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -20713,6 +20713,7 @@ td_api::object_ptr MessagesManager::get_chat_object(const Dialog * return make_tl_object( d->dialog_id.get(), get_chat_type_object(d->dialog_id), get_dialog_title(d->dialog_id), get_chat_photo_info_object(td_->file_manager_.get(), get_dialog_photo(d->dialog_id)), + get_dialog_accent_color_id(d->dialog_id).get_accent_color_id_object(), get_dialog_default_permissions(d->dialog_id).get_chat_permissions_object(), get_message_object(d->dialog_id, get_message(d, d->last_message_id), "get_chat_object"), get_chat_positions_object(d), get_default_message_sender_object(d), block_list_id.get_block_list_object(), @@ -32267,6 +32268,16 @@ void MessagesManager::on_dialog_photo_updated(DialogId dialog_id) { } } +void MessagesManager::on_dialog_accent_color_id_updated(DialogId dialog_id) { + auto d = get_dialog(dialog_id); // called from update_user, must not create the dialog + if (d != nullptr && d->is_update_new_chat_sent) { + send_closure(G()->td(), &Td::send_update, + td_api::make_object( + get_chat_id_object(dialog_id, "updateChatAccentColorId"), + get_dialog_accent_color_id(dialog_id).get_accent_color_id_object())); + } +} + void MessagesManager::on_dialog_title_updated(DialogId dialog_id) { auto d = get_dialog(dialog_id); // called from update_user, must not create the dialog if (d != nullptr) { @@ -32705,6 +32716,23 @@ const DialogPhoto *MessagesManager::get_dialog_photo(DialogId dialog_id) const { } } +AccentColorId MessagesManager::get_dialog_accent_color_id(DialogId dialog_id) const { + switch (dialog_id.get_type()) { + case DialogType::User: + return td_->contacts_manager_->get_user_accent_color_id(dialog_id.get_user_id()); + case DialogType::Chat: + return td_->contacts_manager_->get_chat_accent_color_id(dialog_id.get_chat_id()); + case DialogType::Channel: + return td_->contacts_manager_->get_channel_accent_color_id(dialog_id.get_channel_id()); + case DialogType::SecretChat: + return td_->contacts_manager_->get_secret_chat_accent_color_id(dialog_id.get_secret_chat_id()); + case DialogType::None: + default: + UNREACHABLE(); + return AccentColorId(); + } +} + string MessagesManager::get_dialog_title(DialogId dialog_id) const { switch (dialog_id.get_type()) { case DialogType::User: diff --git a/td/telegram/MessagesManager.h b/td/telegram/MessagesManager.h index ef342233a..1044dd460 100644 --- a/td/telegram/MessagesManager.h +++ b/td/telegram/MessagesManager.h @@ -6,6 +6,7 @@ // #pragma once +#include "td/telegram/AccentColorId.h" #include "td/telegram/AccessRights.h" #include "td/telegram/AffectedHistory.h" #include "td/telegram/BackgroundInfo.h" @@ -875,6 +876,7 @@ class MessagesManager final : public Actor { void on_dialog_bots_updated(DialogId dialog_id, vector bot_user_ids, bool from_database); void on_dialog_photo_updated(DialogId dialog_id); + void on_dialog_accent_color_id_updated(DialogId dialog_id); void on_dialog_title_updated(DialogId dialog_id); void on_dialog_usernames_updated(DialogId dialog_id, const Usernames &old_usernames, const Usernames &new_usernames); void on_dialog_usernames_received(DialogId dialog_id, const Usernames &usernames, bool from_database); @@ -2942,6 +2944,8 @@ class MessagesManager final : public Actor { const DialogPhoto *get_dialog_photo(DialogId dialog_id) const; + AccentColorId get_dialog_accent_color_id(DialogId dialog_id) const; + RestrictedRights get_dialog_default_permissions(DialogId dialog_id) const; bool get_dialog_has_protected_content(DialogId dialog_id) const;