Add strongly typed ScheduledServerMessageId.

GitOrigin-RevId: 222322bbf861c773b777c3b9beac3bf0dba561db
This commit is contained in:
levlam 2019-12-01 19:03:51 +03:00
parent fb43213362
commit f7cf15b9f2
6 changed files with 71 additions and 29 deletions

View File

@ -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

View File

@ -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<int64>(send_date - (1 << 30)) << 21) | (server_message_id << 3) | SCHEDULED_MASK);
id = (static_cast<int64>(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();
}

View File

@ -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<int32>((id >> 3) & ((1 << 18) - 1));
return ScheduledServerMessageId(static_cast<int32>((id >> 3) & ((1 << 18) - 1)));
}
public:
@ -54,7 +55,7 @@ class MessageId {
: id(static_cast<int64>(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();
}

View File

@ -4878,7 +4878,8 @@ vector<int32> MessagesManager::get_server_message_ids(const vector<MessageId> &m
}
vector<int32> MessagesManager::get_scheduled_server_message_ids(const vector<MessageId> &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<telegram_api::InputMessage> MessagesManager::get_input_message(MessageId message_id) {
@ -13217,7 +13218,7 @@ void MessagesManager::get_messages_from_server(vector<FullMessageId> &&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;
}

View File

@ -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 <type_traits>
namespace td {
class ScheduledServerMessageId {
int32 id = 0;
public:
ScheduledServerMessageId() = default;
explicit ScheduledServerMessageId(int32 message_id) : id(message_id) {
}
template <class T, typename = std::enable_if_t<std::is_convertible<T, int32>::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 <class StorerT>
void store(StorerT &storer) const {
storer.store_int(id);
}
template <class ParserT>
void parse(ParserT &parser) {
id = parser.fetch_int();
}
};
} // namespace td

View File

@ -38,16 +38,6 @@ class ServerMessageId {
bool operator!=(const ServerMessageId &other) const {
return id != other.id;
}
template <class StorerT>
void store(StorerT &storer) const {
storer.store_int(id);
}
template <class ParserT>
void parse(ParserT &parser) {
id = parser.fetch_int();
}
};
} // namespace td