Skip to content

Commit

Permalink
Add strongly typed ScheduledServerMessageId.
Browse files Browse the repository at this point in the history
GitOrigin-RevId: 222322bbf861c773b777c3b9beac3bf0dba561db
  • Loading branch information
levlam committed Dec 1, 2019
1 parent fb43213 commit f7cf15b
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 29 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 9 additions & 13 deletions td/telegram/MessageId.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
}
Expand Down
9 changes: 5 additions & 4 deletions td/telegram/MessageId.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//
#pragma once

#include "td/telegram/ScheduledServerMessageId.h"
#include "td/telegram/ServerMessageId.h"

#include "td/utils/common.h"
Expand Down Expand Up @@ -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:
Expand All @@ -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) {
}
Expand Down Expand Up @@ -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();
}
Expand Down
5 changes: 3 additions & 2 deletions td/telegram/MessagesManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
Expand Down
53 changes: 53 additions & 0 deletions td/telegram/ScheduledServerMessageId.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//
// Copyright Aliaksei Levin ([email protected]), Arseny Smirnov ([email protected]) 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
10 changes: 0 additions & 10 deletions td/telegram/ServerMessageId.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit f7cf15b

Please sign in to comment.