From d8e3fd2ee6e89bf03bc59486e835a19bde48f644 Mon Sep 17 00:00:00 2001 From: levlam Date: Sat, 30 Oct 2021 21:50:25 +0300 Subject: [PATCH 1/5] Improve limit documentation. --- td/generate/scheme/td_api.tl | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 8fe2bf42e..84e66125a 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -4749,7 +4749,7 @@ getChatMember chat_id:int53 member_id:MessageSender = ChatMember; //@description Searches for a specified query in the first name, last name and username of the members of a specified chat. Requires administrator rights in channels //@chat_id Chat identifier //@query Query to search for -//@limit The maximum number of users to be returned +//@limit The maximum number of users to be returned; up to 200 //@filter The type of users to search for; pass null to search among all chat members searchChatMembers chat_id:int53 query:string limit:int32 filter:ChatMembersFilter = ChatMembers; @@ -4880,11 +4880,11 @@ getChatInviteLinkCounts chat_id:int53 = ChatInviteLinkCounts; //@is_revoked Pass true if revoked links needs to be returned instead of active or expired //@offset_date Creation date of an invite link starting after which to return invite links; use 0 to get results from the beginning //@offset_invite_link Invite link starting after which to return invite links; use empty string to get results from the beginning -//@limit The maximum number of invite links to return +//@limit The maximum number of invite links to return; up to 100 getChatInviteLinks chat_id:int53 creator_user_id:int53 is_revoked:Bool offset_date:int32 offset_invite_link:string limit:int32 = ChatInviteLinks; //@description Returns chat members joined a chat by an invite link. Requires administrator privileges and can_invite_users right in the chat for own links and owner privileges for other links @chat_id Chat identifier @invite_link Invite link for which to return chat members -//@offset_member A chat member from which to return next chat members; pass null to get results from the beginning @limit The maximum number of chat members to return +//@offset_member A chat member from which to return next chat members; pass null to get results from the beginning @limit The maximum number of chat members to return; up to 100 getChatInviteLinkMembers chat_id:int53 invite_link:string offset_member:chatInviteLinkMember limit:int32 = ChatInviteLinkMembers; //@description Revokes invite link for a chat. Available for basic groups, supergroups, and channels. Requires administrator privileges and can_invite_users right in the chat for own links and owner privileges for other links. @@ -5022,7 +5022,7 @@ toggleGroupCallParticipantIsHandRaised group_call_id:int32 participant_id:Messag //@description Loads more participants of a group call. The loaded participants will be received through updates. Use the field groupCall.loaded_all_participants to check whether all participants has already been loaded //@group_call_id Group call identifier. The group call must be previously received through getGroupCall and must be joined or being joined -//@limit The maximum number of participants to load +//@limit The maximum number of participants to load; up to 100 loadGroupCallParticipants group_call_id:int32 limit:int32 = Ok; //@description Leaves a group call @group_call_id Group call identifier @@ -5098,12 +5098,12 @@ searchStickers emoji:string limit:int32 = Stickers; //@description Returns a list of installed sticker sets @is_masks Pass true to return mask sticker sets; pass false to return ordinary sticker sets getInstalledStickerSets is_masks:Bool = StickerSets; -//@description Returns a list of archived sticker sets @is_masks Pass true to return mask stickers sets; pass false to return ordinary sticker sets @offset_sticker_set_id Identifier of the sticker set from which to return the result @limit The maximum number of sticker sets to return +//@description Returns a list of archived sticker sets @is_masks Pass true to return mask stickers sets; pass false to return ordinary sticker sets @offset_sticker_set_id Identifier of the sticker set from which to return the result @limit The maximum number of sticker sets to return; up to 100 getArchivedStickerSets is_masks:Bool offset_sticker_set_id:int64 limit:int32 = StickerSets; //@description Returns a list of trending sticker sets. For optimal performance, the number of returned sticker sets is chosen by TDLib //@offset The offset from which to return the sticker sets; must be non-negative -//@limit The maximum number of sticker sets to be returned; must be non-negative. For optimal performance, the number of returned sticker sets is chosen by TDLib and can be smaller than the specified limit, even if the end of the list has not been reached +//@limit The maximum number of sticker sets to be returned; up to 100. For optimal performance, the number of returned sticker sets is chosen by TDLib and can be smaller than the specified limit, even if the end of the list has not been reached getTrendingStickerSets offset:int32 limit:int32 = StickerSets; //@description Returns a list of sticker sets attached to a file. Currently only photos and videos can have attached sticker sets @file_id File identifier From b4324ee88cece6dfeb80ca83bc6a4d962ccf9d39 Mon Sep 17 00:00:00 2001 From: levlam Date: Sat, 30 Oct 2021 21:57:43 +0300 Subject: [PATCH 2/5] Fix handle_errors_ check. --- td/telegram/MessagesManager.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index 557969424..59055a4ec 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -2265,7 +2265,7 @@ class SearchMessagesQuery final : public Td::ResultHandler { } void on_error(uint64 id, Status status) final { - if (!handle_errors_) { + if (handle_errors_) { td->messages_manager_->on_get_dialog_error(dialog_id_, status, "SearchMessagesQuery"); } td->messages_manager_->on_failed_dialog_messages_search(dialog_id_, random_id_); From 7cd7e5afd38cb32439ecad62bb81c5bb8eb26a8a Mon Sep 17 00:00:00 2001 From: levlam Date: Sun, 31 Oct 2021 23:05:31 +0300 Subject: [PATCH 3/5] Improve slow_mode_delay documentation. --- td/generate/scheme/td_api.tl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 84e66125a..87bc6b797 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -2530,7 +2530,7 @@ chatEventInvitesToggled can_invite_users:Bool = ChatEventAction; //@description The linked chat of a supergroup was changed @old_linked_chat_id Previous supergroup linked chat identifier @new_linked_chat_id New supergroup linked chat identifier chatEventLinkedChatChanged old_linked_chat_id:int53 new_linked_chat_id:int53 = ChatEventAction; -//@description The slow_mode_delay setting of a supergroup was changed @old_slow_mode_delay Previous value of slow_mode_delay @new_slow_mode_delay New value of slow_mode_delay +//@description The slow_mode_delay setting of a supergroup was changed @old_slow_mode_delay Previous value of slow_mode_delay, in seconds @new_slow_mode_delay New value of slow_mode_delay, in seconds chatEventSlowModeDelayChanged old_slow_mode_delay:int32 new_slow_mode_delay:int32 = ChatEventAction; //@description The message TTL setting was changed @old_message_ttl_setting Previous value of message_ttl_setting @new_message_ttl_setting New value of message_ttl_setting @@ -4694,7 +4694,7 @@ setChatDiscussionGroup chat_id:int53 discussion_chat_id:int53 = Ok; //@description Changes the location of a chat. Available only for some location-based supergroups, use supergroupFullInfo.can_set_location to check whether the method is allowed to use @chat_id Chat identifier @location New location for the chat; must be valid and not null setChatLocation chat_id:int53 location:chatLocation = Ok; -//@description Changes the slow mode delay of a chat. Available only for supergroups; requires can_restrict_members rights @chat_id Chat identifier @slow_mode_delay New slow mode delay for the chat; must be one of 0, 10, 30, 60, 300, 900, 3600 +//@description Changes the slow mode delay of a chat. Available only for supergroups; requires can_restrict_members rights @chat_id Chat identifier @slow_mode_delay New slow mode delay for the chat, in seconds; must be one of 0, 10, 30, 60, 300, 900, 3600 setChatSlowModeDelay chat_id:int53 slow_mode_delay:int32 = Ok; //@description Pins a message in a chat; requires can_pin_messages rights or can_edit_messages rights in the channel From c66cf3dec6c23016ef44fed9a888053f2f5fc9ce Mon Sep 17 00:00:00 2001 From: levlam Date: Mon, 1 Nov 2021 01:24:51 +0300 Subject: [PATCH 4/5] Use range-based for on arrays/strings if possible. --- benchmark/bench_crypto.cpp | 49 +++++++++------------------------ benchmark/check_tls.cpp | 4 +-- td/telegram/MessageEntity.cpp | 4 +-- td/telegram/StickersManager.cpp | 1 + tdtl/td/tl/tl_simple.h | 6 ++-- 5 files changed, 21 insertions(+), 43 deletions(-) diff --git a/benchmark/bench_crypto.cpp b/benchmark/bench_crypto.cpp index b6d36c237..f915fbcf9 100644 --- a/benchmark/bench_crypto.cpp +++ b/benchmark/bench_crypto.cpp @@ -17,10 +17,12 @@ #include #include +#include #include #include #include #include +#include #include #include #include @@ -38,10 +40,7 @@ class SHA1Bench final : public td::Benchmark { } void start_up() final { - for (int i = 0; i < DATA_SIZE; i++) { - data[i] = 123; - data[i] = 0; - } + std::fill(std::begin(data), std::end(data), static_cast(123)); } void run(int n) final { @@ -64,9 +63,7 @@ class AesEcbBench final : public td::Benchmark { } void start_up() final { - for (int i = 0; i < DATA_SIZE; i++) { - data[i] = 123; - } + std::fill(std::begin(data), std::end(data), static_cast(123)); td::Random::secure_bytes(key.raw, sizeof(key)); td::Random::secure_bytes(iv.raw, sizeof(iv)); } @@ -95,9 +92,7 @@ class AesIgeEncryptBench final : public td::Benchmark { } void start_up() final { - for (int i = 0; i < DATA_SIZE; i++) { - data[i] = 123; - } + std::fill(std::begin(data), std::end(data), static_cast(123)); td::Random::secure_bytes(key.raw, sizeof(key)); td::Random::secure_bytes(iv.raw, sizeof(iv)); } @@ -123,9 +118,7 @@ class AesIgeDecryptBench final : public td::Benchmark { } void start_up() final { - for (int i = 0; i < DATA_SIZE; i++) { - data[i] = 123; - } + std::fill(std::begin(data), std::end(data), static_cast(123)); td::Random::secure_bytes(key.raw, sizeof(key)); td::Random::secure_bytes(iv.raw, sizeof(iv)); } @@ -151,9 +144,7 @@ class AesCtrBench final : public td::Benchmark { } void start_up() final { - for (int i = 0; i < DATA_SIZE; i++) { - data[i] = 123; - } + std::fill(std::begin(data), std::end(data), static_cast(123)); td::Random::secure_bytes(key.raw, sizeof(key)); td::Random::secure_bytes(iv.raw, sizeof(iv)); } @@ -180,9 +171,7 @@ class AesCtrOpenSSLBench final : public td::Benchmark { } void start_up() final { - for (int i = 0; i < DATA_SIZE; i++) { - data[i] = 123; - } + std::fill(std::begin(data), std::end(data), static_cast(123)); td::Random::secure_bytes(key.raw, sizeof(key)); td::Random::secure_bytes(iv.raw, sizeof(iv)); } @@ -216,9 +205,7 @@ class AesCbcDecryptBench final : public td::Benchmark { } void start_up() final { - for (int i = 0; i < DATA_SIZE; i++) { - data[i] = 123; - } + std::fill(std::begin(data), std::end(data), static_cast(123)); td::Random::secure_bytes(as_slice(key)); td::Random::secure_bytes(as_slice(iv)); } @@ -242,9 +229,7 @@ class AesCbcEncryptBench final : public td::Benchmark { } void start_up() final { - for (int i = 0; i < DATA_SIZE; i++) { - data[i] = 123; - } + std::fill(std::begin(data), std::end(data), static_cast(123)); td::Random::secure_bytes(as_slice(key)); td::Random::secure_bytes(as_slice(iv)); } @@ -269,9 +254,7 @@ class AesIgeShortBench final : public td::Benchmark { } void start_up() final { - for (int i = 0; i < SHORT_DATA_SIZE; i++) { - data[i] = 123; - } + std::fill(std::begin(data), std::end(data), static_cast(123)); td::Random::secure_bytes(as_slice(key)); td::Random::secure_bytes(as_slice(iv)); } @@ -372,10 +355,7 @@ class Crc32Bench final : public td::Benchmark { } void start_up() final { - for (int i = 0; i < DATA_SIZE; i++) { - data[i] = 123; - data[i] = 0; - } + std::fill(std::begin(data), std::end(data), static_cast(123)); } void run(int n) final { @@ -396,10 +376,7 @@ class Crc64Bench final : public td::Benchmark { } void start_up() final { - for (int i = 0; i < DATA_SIZE; i++) { - data[i] = 123; - data[i] = 0; - } + std::fill(std::begin(data), std::end(data), static_cast(123)); } void run(int n) final { diff --git a/benchmark/check_tls.cpp b/benchmark/check_tls.cpp index afc80b800..b2fa2ecd0 100644 --- a/benchmark/check_tls.cpp +++ b/benchmark/check_tls.cpp @@ -96,8 +96,8 @@ td::Result test_tls(const td::string &url) { const size_t MAX_GREASE = 7; char greases[MAX_GREASE]; td::Random::secure_bytes(td::MutableSlice{greases, MAX_GREASE}); - for (size_t i = 0; i < MAX_GREASE; i++) { - greases[i] = static_cast((greases[i] & 0xF0) + 0x0A); + for (auto &grease : greases) { + grease = static_cast((grease & 0xF0) + 0x0A); } for (size_t i = 1; i < MAX_GREASE; i += 2) { if (greases[i] == greases[i - 1]) { diff --git a/td/telegram/MessageEntity.cpp b/td/telegram/MessageEntity.cpp index 0a4203f0c..c091b7ec7 100644 --- a/td/telegram/MessageEntity.cpp +++ b/td/telegram/MessageEntity.cpp @@ -2223,8 +2223,8 @@ static FormattedText parse_text_url_entities_v3(Slice text, const vector(recent_sticker_ids_[is_attached].size()) > recent_stickers_limit) { recent_sticker_ids_[is_attached].resize(recent_stickers_limit); + need_update_recent_stickers_[is_attached] = true; send_update_recent_stickers(); } } diff --git a/tdtl/td/tl/tl_simple.h b/tdtl/td/tl/tl_simple.h index 67ad2a1aa..7647981bf 100644 --- a/tdtl/td/tl/tl_simple.h +++ b/tdtl/td/tl/tl_simple.h @@ -23,9 +23,9 @@ namespace tl { namespace simple { inline std::string gen_cpp_name(std::string name) { - for (std::size_t i = 0; i < name.size(); i++) { - if ((name[i] < '0' || '9' < name[i]) && (name[i] < 'a' || 'z' < name[i]) && (name[i] < 'A' || 'Z' < name[i])) { - name[i] = '_'; + for (auto &c : name) { + if ((c < '0' || '9' < c) && (c < 'a' || 'z' < c) && (c < 'A' || 'Z' < c)) { + c = '_'; } } assert(!name.empty()); From 5642947198b34ea8e0c3051611d77eac0c307c6e Mon Sep 17 00:00:00 2001 From: levlam Date: Mon, 1 Nov 2021 01:35:39 +0300 Subject: [PATCH 5/5] Simplify send_update_recent_stickers usage. --- td/telegram/StickersManager.cpp | 57 ++++++++++++++------------------- td/telegram/StickersManager.h | 3 +- 2 files changed, 25 insertions(+), 35 deletions(-) diff --git a/td/telegram/StickersManager.cpp b/td/telegram/StickersManager.cpp index 56a4758b5..61271adb0 100644 --- a/td/telegram/StickersManager.cpp +++ b/td/telegram/StickersManager.cpp @@ -5910,8 +5910,7 @@ void StickersManager::on_load_recent_stickers_finished(bool is_attached, vector< } recent_sticker_ids_[is_attached] = std::move(recent_sticker_ids); are_recent_stickers_loaded_[is_attached] = true; - need_update_recent_stickers_[is_attached] = true; - send_update_recent_stickers(from_database); + send_update_recent_stickers(is_attached, from_database); auto promises = std::move(load_recent_stickers_queries_[is_attached]); load_recent_stickers_queries_[is_attached].clear(); for (auto &promise : promises) { @@ -6083,8 +6082,6 @@ void StickersManager::add_recent_sticker_impl(bool is_attached, FileId sticker_i return promise.set_error(Status::Error(400, "Can't save encrypted stickers")); } - need_update_recent_stickers_[is_attached] = true; - auto it = std::find_if(sticker_ids.begin(), sticker_ids.end(), is_equal); if (it == sticker_ids.end()) { if (static_cast(sticker_ids.size()) == recent_stickers_limit_) { @@ -6099,7 +6096,7 @@ void StickersManager::add_recent_sticker_impl(bool is_attached, FileId sticker_i sticker_ids[0] = sticker_id; } - send_update_recent_stickers(); + send_update_recent_stickers(is_attached); if (add_on_server) { send_save_recent_sticker_query(is_attached, sticker_id, false, std::move(promise)); } @@ -6130,8 +6127,7 @@ void StickersManager::remove_recent_sticker(bool is_attached, const tl_object_pt send_save_recent_sticker_query(is_attached, file_id, true, std::move(promise)); - need_update_recent_stickers_[is_attached] = true; - send_update_recent_stickers(); + send_update_recent_stickers(is_attached); } void StickersManager::clear_recent_stickers(bool is_attached, Promise &&promise) { @@ -6150,8 +6146,7 @@ void StickersManager::clear_recent_stickers(bool is_attached, Promise &&pr sticker_ids.clear(); - need_update_recent_stickers_[is_attached] = true; - send_update_recent_stickers(); + send_update_recent_stickers(is_attached); } td_api::object_ptr StickersManager::get_update_recent_stickers_object( @@ -6160,30 +6155,27 @@ td_api::object_ptr StickersManager::get_update_rec is_attached != 0, td_->file_manager_->get_file_ids_object(recent_sticker_ids_[is_attached])); } -void StickersManager::send_update_recent_stickers(bool from_database) { - for (int is_attached = 0; is_attached < 2; is_attached++) { - if (need_update_recent_stickers_[is_attached]) { - need_update_recent_stickers_[is_attached] = false; - if (are_recent_stickers_loaded_[is_attached]) { - vector new_recent_sticker_file_ids; - for (auto &sticker_id : recent_sticker_ids_[is_attached]) { - append(new_recent_sticker_file_ids, get_sticker_file_ids(sticker_id)); - } - std::sort(new_recent_sticker_file_ids.begin(), new_recent_sticker_file_ids.end()); - if (new_recent_sticker_file_ids != recent_sticker_file_ids_[is_attached]) { - td_->file_manager_->change_files_source(get_recent_stickers_file_source_id(is_attached), - recent_sticker_file_ids_[is_attached], new_recent_sticker_file_ids); - recent_sticker_file_ids_[is_attached] = std::move(new_recent_sticker_file_ids); - } +void StickersManager::send_update_recent_stickers(bool is_attached, bool from_database) { + if (!are_recent_stickers_loaded_[is_attached]) { + return; + } - recent_stickers_hash_[is_attached] = get_recent_stickers_hash(recent_sticker_ids_[is_attached]); - send_closure(G()->td(), &Td::send_update, get_update_recent_stickers_object(is_attached)); + vector new_recent_sticker_file_ids; + for (auto &sticker_id : recent_sticker_ids_[is_attached]) { + append(new_recent_sticker_file_ids, get_sticker_file_ids(sticker_id)); + } + std::sort(new_recent_sticker_file_ids.begin(), new_recent_sticker_file_ids.end()); + if (new_recent_sticker_file_ids != recent_sticker_file_ids_[is_attached]) { + td_->file_manager_->change_files_source(get_recent_stickers_file_source_id(is_attached), + recent_sticker_file_ids_[is_attached], new_recent_sticker_file_ids); + recent_sticker_file_ids_[is_attached] = std::move(new_recent_sticker_file_ids); + } - if (!from_database) { - save_recent_stickers_to_database(is_attached != 0); - } - } - } + recent_stickers_hash_[is_attached] = get_recent_stickers_hash(recent_sticker_ids_[is_attached]); + send_closure(G()->td(), &Td::send_update, get_update_recent_stickers_object(is_attached)); + + if (!from_database) { + save_recent_stickers_to_database(is_attached != 0); } } @@ -6204,8 +6196,7 @@ void StickersManager::on_update_recent_stickers_limit(int32 recent_stickers_limi for (int is_attached = 0; is_attached < 2; is_attached++) { if (static_cast(recent_sticker_ids_[is_attached].size()) > recent_stickers_limit) { recent_sticker_ids_[is_attached].resize(recent_stickers_limit); - need_update_recent_stickers_[is_attached] = true; - send_update_recent_stickers(); + send_update_recent_stickers(is_attached); } } } else { diff --git a/td/telegram/StickersManager.h b/td/telegram/StickersManager.h index 0b81fc996..a3ab9a092 100644 --- a/td/telegram/StickersManager.h +++ b/td/telegram/StickersManager.h @@ -537,7 +537,7 @@ class StickersManager final : public Actor { td_api::object_ptr get_update_recent_stickers_object(int is_attached) const; - void send_update_recent_stickers(bool from_database = false); + void send_update_recent_stickers(bool is_attached, bool from_database = false); void save_recent_stickers_to_database(bool is_attached); @@ -704,7 +704,6 @@ class StickersManager final : public Actor { bool need_update_installed_sticker_sets_[2] = {false, false}; bool need_update_featured_sticker_sets_ = false; - bool need_update_recent_stickers_[2] = {false, false}; bool are_installed_sticker_sets_loaded_[2] = {false, false}; bool are_featured_sticker_sets_loaded_ = false;