2023-08-21 17:52:56 +02:00
|
|
|
//
|
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
|
|
|
//
|
|
|
|
// 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/NotificationGroupInfo.h"
|
|
|
|
|
2023-08-21 18:46:09 +02:00
|
|
|
#include "td/telegram/NotificationManager.h"
|
|
|
|
|
2023-08-21 18:28:05 +02:00
|
|
|
#include "td/utils/logging.h"
|
|
|
|
|
2023-08-21 17:52:56 +02:00
|
|
|
namespace td {
|
|
|
|
|
2023-08-21 18:46:09 +02:00
|
|
|
bool NotificationGroupInfo::set_last_notification(int32 last_notification_date, NotificationId last_notification_id,
|
|
|
|
const char *source) {
|
2023-08-21 21:13:19 +02:00
|
|
|
if (is_removed_notification_id(last_notification_id)) {
|
|
|
|
last_notification_id = NotificationId();
|
|
|
|
last_notification_date = 0;
|
|
|
|
}
|
2023-08-21 18:46:09 +02:00
|
|
|
if (last_notification_date_ != last_notification_date || last_notification_id_ != last_notification_id) {
|
|
|
|
VLOG(notifications) << "Set " << group_id_ << " last notification to " << last_notification_id << " sent at "
|
|
|
|
<< last_notification_date << " from " << source;
|
2023-08-21 21:16:42 +02:00
|
|
|
if (last_notification_date_ != last_notification_date) {
|
|
|
|
last_notification_date_ = last_notification_date;
|
|
|
|
is_key_changed_ = true;
|
|
|
|
}
|
2023-08-21 18:46:09 +02:00
|
|
|
last_notification_id_ = last_notification_id;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-08-21 19:12:30 +02:00
|
|
|
bool NotificationGroupInfo::set_max_removed_notification_id(NotificationId max_removed_notification_id,
|
2023-08-25 23:48:57 +02:00
|
|
|
NotificationObjectId max_removed_object_id,
|
|
|
|
const char *source) {
|
2023-08-21 19:12:30 +02:00
|
|
|
if (max_removed_notification_id.get() <= max_removed_notification_id_.get()) {
|
|
|
|
return false;
|
|
|
|
}
|
2023-08-22 15:32:40 +02:00
|
|
|
if (max_removed_object_id > max_removed_object_id_) {
|
|
|
|
VLOG(notifications) << "Set max_removed_object_id in " << group_id_ << " to " << max_removed_object_id << " from "
|
2023-08-21 19:12:30 +02:00
|
|
|
<< source;
|
2023-08-22 15:32:40 +02:00
|
|
|
max_removed_object_id_ = max_removed_object_id;
|
2023-08-21 19:12:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
VLOG(notifications) << "Set max_removed_notification_id in " << group_id_ << " to " << max_removed_notification_id
|
|
|
|
<< " from " << source;
|
|
|
|
max_removed_notification_id_ = max_removed_notification_id;
|
|
|
|
|
2023-08-21 21:40:17 +02:00
|
|
|
if (last_notification_id_.is_valid() && is_removed_notification_id(last_notification_id_)) {
|
|
|
|
last_notification_id_ = NotificationId();
|
|
|
|
last_notification_date_ = 0;
|
|
|
|
is_key_changed_ = true;
|
|
|
|
}
|
|
|
|
|
2023-08-21 19:12:30 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NotificationGroupInfo::drop_max_removed_notification_id() {
|
|
|
|
if (!max_removed_notification_id_.is_valid()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
VLOG(notifications) << "Drop max_removed_notification_id in " << group_id_;
|
2023-08-25 23:48:57 +02:00
|
|
|
max_removed_object_id_ = {};
|
2023-08-21 19:12:30 +02:00
|
|
|
max_removed_notification_id_ = NotificationId();
|
|
|
|
}
|
|
|
|
|
2023-08-25 23:48:57 +02:00
|
|
|
bool NotificationGroupInfo::is_removed_notification(NotificationId notification_id,
|
|
|
|
NotificationObjectId object_id) const {
|
2023-08-22 15:32:40 +02:00
|
|
|
return is_removed_notification_id(notification_id) || is_removed_object_id(object_id);
|
2023-08-21 20:43:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool NotificationGroupInfo::is_removed_notification_id(NotificationId notification_id) const {
|
|
|
|
return notification_id.get() <= max_removed_notification_id_.get();
|
|
|
|
}
|
|
|
|
|
2023-08-25 23:48:57 +02:00
|
|
|
bool NotificationGroupInfo::is_removed_object_id(NotificationObjectId object_id) const {
|
2023-08-22 15:32:40 +02:00
|
|
|
return object_id <= max_removed_object_id_;
|
2023-08-21 20:37:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool NotificationGroupInfo::is_used_notification_id(NotificationId notification_id) const {
|
|
|
|
return notification_id.get() <= max_removed_notification_id_.get() ||
|
|
|
|
notification_id.get() <= last_notification_id_.get();
|
|
|
|
}
|
|
|
|
|
2023-08-21 18:28:05 +02:00
|
|
|
void NotificationGroupInfo::try_reuse() {
|
2023-08-21 22:03:55 +02:00
|
|
|
CHECK(is_valid());
|
2023-08-21 18:36:43 +02:00
|
|
|
CHECK(last_notification_date_ == 0);
|
2023-08-21 18:28:05 +02:00
|
|
|
if (!try_reuse_) {
|
|
|
|
try_reuse_ = true;
|
2023-08-21 21:16:42 +02:00
|
|
|
is_key_changed_ = true;
|
2023-08-21 18:28:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NotificationGroupInfo::add_group_key_if_changed(vector<NotificationGroupKey> &group_keys, DialogId dialog_id) {
|
2023-08-21 21:16:42 +02:00
|
|
|
if (!is_key_changed_) {
|
2023-08-21 18:28:05 +02:00
|
|
|
return;
|
|
|
|
}
|
2023-08-21 21:16:42 +02:00
|
|
|
is_key_changed_ = false;
|
2023-08-21 18:28:05 +02:00
|
|
|
|
2023-08-21 18:36:43 +02:00
|
|
|
group_keys.emplace_back(group_id_, try_reuse_ ? DialogId() : dialog_id, last_notification_date_);
|
2023-08-21 18:28:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
NotificationGroupId NotificationGroupInfo::get_reused_group_id() {
|
|
|
|
if (!try_reuse_) {
|
|
|
|
return {};
|
|
|
|
}
|
2023-08-21 21:16:42 +02:00
|
|
|
if (is_key_changed_) {
|
2023-08-21 18:36:43 +02:00
|
|
|
LOG(ERROR) << "Failed to reuse changed " << group_id_;
|
2023-08-21 18:28:05 +02:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
try_reuse_ = false;
|
2023-08-21 22:03:55 +02:00
|
|
|
if (!is_valid()) {
|
2023-08-21 18:36:43 +02:00
|
|
|
LOG(ERROR) << "Failed to reuse invalid " << group_id_;
|
2023-08-21 18:28:05 +02:00
|
|
|
return {};
|
|
|
|
}
|
2023-08-21 18:36:43 +02:00
|
|
|
CHECK(last_notification_id_ == NotificationId());
|
|
|
|
CHECK(last_notification_date_ == 0);
|
|
|
|
auto result = group_id_;
|
|
|
|
group_id_ = NotificationGroupId();
|
|
|
|
max_removed_notification_id_ = NotificationId();
|
2023-08-25 23:48:57 +02:00
|
|
|
max_removed_object_id_ = {};
|
2023-08-21 18:28:05 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-08-21 17:52:56 +02:00
|
|
|
StringBuilder &operator<<(StringBuilder &string_builder, const NotificationGroupInfo &group_info) {
|
2023-08-21 18:36:43 +02:00
|
|
|
return string_builder << group_info.group_id_ << " with last " << group_info.last_notification_id_ << " sent at "
|
|
|
|
<< group_info.last_notification_date_ << ", max removed "
|
2023-08-22 15:32:40 +02:00
|
|
|
<< group_info.max_removed_notification_id_ << '/' << group_info.max_removed_object_id_;
|
2023-08-21 17:52:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|