2019-11-26 17:18:57 +01:00
|
|
|
//
|
2021-01-01 13:57:46 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021
|
2019-11-26 17:18:57 +01:00
|
|
|
//
|
|
|
|
// 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)
|
|
|
|
//
|
|
|
|
#include "td/telegram/MessageId.h"
|
|
|
|
|
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/misc.h"
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
2019-12-02 21:29:08 +01:00
|
|
|
MessageId::MessageId(ScheduledServerMessageId server_message_id, int32 send_date, bool force) {
|
2019-11-26 17:18:57 +01:00
|
|
|
if (send_date <= (1 << 30)) {
|
|
|
|
LOG(ERROR) << "Scheduled message send date " << send_date << " is in the past";
|
2019-12-01 17:03:51 +01:00
|
|
|
return;
|
2019-11-26 17:18:57 +01:00
|
|
|
}
|
2019-12-02 21:29:08 +01:00
|
|
|
if (!server_message_id.is_valid() && !force) {
|
2019-12-01 17:03:51 +01:00
|
|
|
LOG(ERROR) << "Scheduled message ID " << server_message_id.get() << " is invalid";
|
|
|
|
return;
|
2019-11-26 17:18:57 +01:00
|
|
|
}
|
2021-11-10 19:09:28 +01:00
|
|
|
id = (static_cast<int64>(send_date - (1 << 30)) << 21) | (static_cast<int64>(server_message_id.get()) << 3) |
|
|
|
|
SCHEDULED_MASK;
|
2019-11-26 17:18:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool MessageId::is_valid() const {
|
|
|
|
if (id <= 0 || id > max().get()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ((id & FULL_TYPE_MASK) == 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
int32 type = (id & TYPE_MASK);
|
|
|
|
return type == TYPE_YET_UNSENT || type == TYPE_LOCAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MessageId::is_valid_scheduled() const {
|
2021-08-25 10:29:57 +02:00
|
|
|
if (id <= 0 || id > (static_cast<int64>(1) << 51)) {
|
2019-11-26 17:18:57 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
int32 type = (id & TYPE_MASK);
|
|
|
|
return type == SCHEDULED_MASK || type == (SCHEDULED_MASK | TYPE_YET_UNSENT) || type == (SCHEDULED_MASK | TYPE_LOCAL);
|
|
|
|
}
|
|
|
|
|
2021-12-27 16:17:02 +01:00
|
|
|
bool MessageId::is_valid_sponsored() const {
|
|
|
|
if (id <= max().get() || id > (static_cast<int64>(1) << 51)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
int32 type = (id & TYPE_MASK);
|
|
|
|
return type == TYPE_LOCAL;
|
|
|
|
}
|
|
|
|
|
2019-11-26 17:18:57 +01:00
|
|
|
MessageType MessageId::get_type() const {
|
2021-08-25 10:29:57 +02:00
|
|
|
if (id <= 0 || id > (static_cast<int64>(1) << 51)) {
|
2019-11-26 17:18:57 +01:00
|
|
|
return MessageType::None;
|
|
|
|
}
|
2019-12-02 21:29:08 +01:00
|
|
|
|
|
|
|
if (is_scheduled()) {
|
|
|
|
switch (id & TYPE_MASK) {
|
|
|
|
case SCHEDULED_MASK | TYPE_YET_UNSENT:
|
|
|
|
return MessageType::YetUnsent;
|
|
|
|
case SCHEDULED_MASK | TYPE_LOCAL:
|
|
|
|
return MessageType::Local;
|
|
|
|
case SCHEDULED_MASK:
|
|
|
|
return MessageType::Server;
|
|
|
|
default:
|
|
|
|
return MessageType::None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-26 17:18:57 +01:00
|
|
|
if ((id & FULL_TYPE_MASK) == 0) {
|
|
|
|
return MessageType::Server;
|
|
|
|
}
|
|
|
|
switch (id & TYPE_MASK) {
|
|
|
|
case TYPE_YET_UNSENT:
|
|
|
|
return MessageType::YetUnsent;
|
|
|
|
case TYPE_LOCAL:
|
|
|
|
return MessageType::Local;
|
|
|
|
default:
|
|
|
|
return MessageType::None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-26 20:35:16 +01:00
|
|
|
ServerMessageId MessageId::get_server_message_id_force() const {
|
2019-11-26 21:47:13 +01:00
|
|
|
CHECK(!is_scheduled());
|
2019-11-26 17:53:10 +01:00
|
|
|
return ServerMessageId(narrow_cast<int32>(id >> SERVER_ID_SHIFT));
|
|
|
|
}
|
|
|
|
|
2019-11-26 17:18:57 +01:00
|
|
|
MessageId MessageId::get_next_message_id(MessageType type) const {
|
2019-12-02 21:29:08 +01:00
|
|
|
if (is_scheduled()) {
|
|
|
|
CHECK(is_valid_scheduled());
|
|
|
|
auto current_type = get_type();
|
|
|
|
if (static_cast<int32>(current_type) < static_cast<int32>(type)) {
|
|
|
|
return MessageId(id - static_cast<int32>(current_type) + static_cast<int32>(type));
|
|
|
|
}
|
|
|
|
int64 base_id = (id & ~TYPE_MASK) + TYPE_MASK + 1 + SCHEDULED_MASK;
|
|
|
|
switch (type) {
|
|
|
|
case MessageType::Server:
|
|
|
|
return MessageId(base_id);
|
|
|
|
case MessageType::YetUnsent:
|
|
|
|
return MessageId(base_id + TYPE_YET_UNSENT);
|
|
|
|
case MessageType::Local:
|
|
|
|
return MessageId(base_id + TYPE_LOCAL);
|
|
|
|
case MessageType::None:
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return MessageId();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-26 17:18:57 +01:00
|
|
|
switch (type) {
|
|
|
|
case MessageType::Server:
|
2019-12-02 21:29:08 +01:00
|
|
|
if (is_server()) {
|
|
|
|
return MessageId(ServerMessageId(get_server_message_id().get() + 1));
|
|
|
|
}
|
2019-11-26 17:18:57 +01:00
|
|
|
return get_next_server_message_id();
|
|
|
|
case MessageType::YetUnsent:
|
|
|
|
return MessageId(((id + TYPE_MASK + 1 - TYPE_YET_UNSENT) & ~TYPE_MASK) + TYPE_YET_UNSENT);
|
2019-12-02 21:29:08 +01:00
|
|
|
case MessageType::Local:
|
|
|
|
return MessageId(((id + TYPE_MASK + 1 - TYPE_LOCAL) & ~TYPE_MASK) + TYPE_LOCAL);
|
2019-11-26 17:18:57 +01:00
|
|
|
case MessageType::None:
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return MessageId();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
StringBuilder &operator<<(StringBuilder &string_builder, MessageId message_id) {
|
2019-11-26 20:35:16 +01:00
|
|
|
if (message_id.is_scheduled()) {
|
|
|
|
string_builder << "scheduled ";
|
|
|
|
|
|
|
|
if (!message_id.is_valid_scheduled()) {
|
|
|
|
return string_builder << "invalid message " << message_id.get();
|
|
|
|
}
|
|
|
|
if (message_id.is_scheduled_server()) {
|
2019-12-01 17:03:51 +01:00
|
|
|
return string_builder << "server message " << message_id.get_scheduled_server_message_id_force().get();
|
2019-11-26 20:35:16 +01:00
|
|
|
}
|
|
|
|
if (message_id.is_local()) {
|
2019-12-01 17:03:51 +01:00
|
|
|
return string_builder << "local message " << message_id.get_scheduled_server_message_id_force().get();
|
2019-11-26 20:35:16 +01:00
|
|
|
}
|
|
|
|
if (message_id.is_yet_unsent()) {
|
2019-12-01 17:03:51 +01:00
|
|
|
return string_builder << "yet unsent message " << message_id.get_scheduled_server_message_id_force().get();
|
2019-11-26 20:35:16 +01:00
|
|
|
}
|
|
|
|
return string_builder << "bugged message " << message_id.get();
|
|
|
|
}
|
|
|
|
|
2019-11-26 17:18:57 +01:00
|
|
|
if (!message_id.is_valid()) {
|
|
|
|
return string_builder << "invalid message " << message_id.get();
|
|
|
|
}
|
|
|
|
if (message_id.is_server()) {
|
2019-11-26 20:35:16 +01:00
|
|
|
return string_builder << "server message " << message_id.get_server_message_id_force().get();
|
2019-11-26 17:18:57 +01:00
|
|
|
}
|
|
|
|
if (message_id.is_local()) {
|
2019-11-26 20:35:16 +01:00
|
|
|
return string_builder << "local message " << message_id.get_server_message_id_force().get() << '.'
|
2019-11-26 17:18:57 +01:00
|
|
|
<< (message_id.get() & MessageId::FULL_TYPE_MASK);
|
|
|
|
}
|
|
|
|
if (message_id.is_yet_unsent()) {
|
2019-11-26 20:35:16 +01:00
|
|
|
return string_builder << "yet unsent message " << message_id.get_server_message_id_force().get() << '.'
|
2019-11-26 17:18:57 +01:00
|
|
|
<< (message_id.get() & MessageId::FULL_TYPE_MASK);
|
|
|
|
}
|
|
|
|
return string_builder << "bugged message " << message_id.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|