diff --git a/CMakeLists.txt b/CMakeLists.txt index 5ea5b8a91829..aea3d155c616 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -621,6 +621,7 @@ set(TDLIB_SOURCE td/telegram/ReplyMarkup.h td/telegram/RequestActor.h td/telegram/RestrictionReason.h + td/telegram/ScheduledServerMessageId.h td/telegram/SecretChatActor.h td/telegram/SecretChatId.h td/telegram/SecretChatDb.h diff --git a/td/telegram/MessageId.cpp b/td/telegram/MessageId.cpp index 432634d80a5a..6a4f61534b2d 100644 --- a/td/telegram/MessageId.cpp +++ b/td/telegram/MessageId.cpp @@ -11,20 +11,16 @@ namespace td { -MessageId MessageId::get_scheduled_message_id(int32 server_message_id, int32 send_date) { +MessageId::MessageId(ScheduledServerMessageId server_message_id, int32 send_date) { if (send_date <= (1 << 30)) { LOG(ERROR) << "Scheduled message send date " << send_date << " is in the past"; - return MessageId(); + return; } - if (server_message_id <= 0) { - LOG(ERROR) << "Scheduled message ID " << server_message_id << " is non-positive"; - return MessageId(); + if (!server_message_id.is_valid()) { + LOG(ERROR) << "Scheduled message ID " << server_message_id.get() << " is invalid"; + return; } - if (server_message_id >= (1 << 18)) { - LOG(ERROR) << "Scheduled message ID " << server_message_id << " is too big"; - return MessageId(); - } - return MessageId((static_cast(send_date - (1 << 30)) << 21) | (server_message_id << 3) | SCHEDULED_MASK); + id = (static_cast(send_date - (1 << 30)) << 21) | (server_message_id.get() << 3) | SCHEDULED_MASK; } bool MessageId::is_valid() const { @@ -92,13 +88,13 @@ StringBuilder &operator<<(StringBuilder &string_builder, MessageId message_id) { return string_builder << "invalid message " << message_id.get(); } if (message_id.is_scheduled_server()) { - return string_builder << "server message " << message_id.get_scheduled_server_message_id_force(); + return string_builder << "server message " << message_id.get_scheduled_server_message_id_force().get(); } if (message_id.is_local()) { - return string_builder << "local message " << message_id.get_scheduled_server_message_id_force(); + return string_builder << "local message " << message_id.get_scheduled_server_message_id_force().get(); } if (message_id.is_yet_unsent()) { - return string_builder << "yet unsent message " << message_id.get_scheduled_server_message_id_force(); + return string_builder << "yet unsent message " << message_id.get_scheduled_server_message_id_force().get(); } return string_builder << "bugged message " << message_id.get(); } diff --git a/td/telegram/MessageId.h b/td/telegram/MessageId.h index 5fa318949217..4834e0eb64cf 100644 --- a/td/telegram/MessageId.h +++ b/td/telegram/MessageId.h @@ -6,6 +6,7 @@ // #pragma once +#include "td/telegram/ScheduledServerMessageId.h" #include "td/telegram/ServerMessageId.h" #include "td/utils/common.h" @@ -42,9 +43,9 @@ class MessageId { ServerMessageId get_server_message_id_force() const; - int32 get_scheduled_server_message_id_force() const { + ScheduledServerMessageId get_scheduled_server_message_id_force() const { CHECK(is_scheduled()); - return static_cast((id >> 3) & ((1 << 18) - 1)); + return ScheduledServerMessageId(static_cast((id >> 3) & ((1 << 18) - 1))); } public: @@ -54,7 +55,7 @@ class MessageId { : id(static_cast(server_message_id.get()) << SERVER_ID_SHIFT) { } - static MessageId get_scheduled_message_id(int32 server_message_id, int32 send_date); + MessageId(ScheduledServerMessageId server_message_id, int32 send_date); explicit constexpr MessageId(int64 message_id) : id(message_id) { } @@ -125,7 +126,7 @@ class MessageId { MessageId get_next_message_id(MessageType type) const; - int32 get_scheduled_server_message_id() const { + ScheduledServerMessageId get_scheduled_server_message_id() const { CHECK(is_scheduled_server()); return get_scheduled_server_message_id_force(); } diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index a5c460180f50..1cf8ab5143e8 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -4878,7 +4878,8 @@ vector MessagesManager::get_server_message_ids(const vector &m } vector MessagesManager::get_scheduled_server_message_ids(const vector &message_ids) { - return transform(message_ids, [](MessageId message_id) { return message_id.get_scheduled_server_message_id(); }); + return transform(message_ids, + [](MessageId message_id) { return message_id.get_scheduled_server_message_id().get(); }); } tl_object_ptr MessagesManager::get_input_message(MessageId message_id) { @@ -13217,7 +13218,7 @@ void MessagesManager::get_messages_from_server(vector &&message_i auto message_id = full_message_id.get_message_id(); if (!message_id.is_valid() || !message_id.is_server()) { if (message_id.is_valid_scheduled()) { - scheduled_message_ids.push_back(message_id.get_scheduled_server_message_id()); + scheduled_message_ids.push_back(message_id.get_scheduled_server_message_id().get()); } continue; } diff --git a/td/telegram/ScheduledServerMessageId.h b/td/telegram/ScheduledServerMessageId.h new file mode 100644 index 000000000000..c9364ed4b7aa --- /dev/null +++ b/td/telegram/ScheduledServerMessageId.h @@ -0,0 +1,53 @@ +// +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2019 +// +// 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) +// +#pragma once + +#include "td/utils/common.h" + +#include + +namespace td { + +class ScheduledServerMessageId { + int32 id = 0; + + public: + ScheduledServerMessageId() = default; + + explicit ScheduledServerMessageId(int32 message_id) : id(message_id) { + } + template ::value>> + ScheduledServerMessageId(T message_id) = delete; + + bool is_valid() const { + return id > 0 && id < (1 << 18); + } + + int32 get() const { + return id; + } + + bool operator==(const ScheduledServerMessageId &other) const { + return id == other.id; + } + + bool operator!=(const ScheduledServerMessageId &other) const { + return id != other.id; + } + + template + void store(StorerT &storer) const { + storer.store_int(id); + } + + template + void parse(ParserT &parser) { + id = parser.fetch_int(); + } +}; + +} // namespace td diff --git a/td/telegram/ServerMessageId.h b/td/telegram/ServerMessageId.h index 39055794d7c8..4827bbe970f0 100644 --- a/td/telegram/ServerMessageId.h +++ b/td/telegram/ServerMessageId.h @@ -38,16 +38,6 @@ class ServerMessageId { bool operator!=(const ServerMessageId &other) const { return id != other.id; } - - template - void store(StorerT &storer) const { - storer.store_int(id); - } - - template - void parse(ParserT &parser) { - id = parser.fetch_int(); - } }; } // namespace td