2018-11-09 15:14:02 +01:00
|
|
|
//
|
2018-12-31 23:02:34 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2019
|
2018-11-09 15:14:02 +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)
|
|
|
|
//
|
|
|
|
#pragma once
|
2018-12-02 22:30:05 +01:00
|
|
|
|
2018-12-18 21:59:35 +01:00
|
|
|
#include "td/telegram/CallId.h"
|
2018-11-09 23:56:00 +01:00
|
|
|
#include "td/telegram/DialogId.h"
|
2018-11-20 15:08:44 +01:00
|
|
|
#include "td/telegram/MessageId.h"
|
2018-11-26 18:05:06 +01:00
|
|
|
#include "td/telegram/Notification.h"
|
2018-11-10 00:08:47 +01:00
|
|
|
#include "td/telegram/NotificationGroupId.h"
|
2018-11-28 22:51:25 +01:00
|
|
|
#include "td/telegram/NotificationGroupKey.h"
|
2018-12-23 22:34:40 +01:00
|
|
|
#include "td/telegram/NotificationGroupType.h"
|
2018-11-10 00:08:47 +01:00
|
|
|
#include "td/telegram/NotificationId.h"
|
2018-11-09 23:56:00 +01:00
|
|
|
#include "td/telegram/NotificationType.h"
|
2018-11-15 16:58:33 +01:00
|
|
|
#include "td/telegram/td_api.h"
|
2018-11-09 23:56:00 +01:00
|
|
|
|
2018-11-11 13:58:52 +01:00
|
|
|
#include "td/actor/actor.h"
|
2018-11-09 15:14:02 +01:00
|
|
|
#include "td/actor/PromiseFuture.h"
|
2018-11-15 16:58:33 +01:00
|
|
|
#include "td/actor/Timeout.h"
|
2018-11-09 15:14:02 +01:00
|
|
|
|
2018-11-09 23:56:00 +01:00
|
|
|
#include "td/utils/common.h"
|
2018-12-11 21:18:58 +01:00
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/Status.h"
|
2018-11-15 16:58:33 +01:00
|
|
|
#include "td/utils/StringBuilder.h"
|
|
|
|
|
2018-11-22 18:17:26 +01:00
|
|
|
#include <functional>
|
2018-11-15 16:58:33 +01:00
|
|
|
#include <map>
|
2018-11-21 23:28:56 +01:00
|
|
|
#include <unordered_map>
|
2018-12-11 21:18:58 +01:00
|
|
|
#include <unordered_set>
|
2018-11-09 23:56:00 +01:00
|
|
|
|
2018-11-09 15:14:02 +01:00
|
|
|
namespace td {
|
|
|
|
|
2018-11-11 13:58:52 +01:00
|
|
|
extern int VERBOSITY_NAME(notifications);
|
|
|
|
|
2018-11-09 15:14:02 +01:00
|
|
|
class Td;
|
|
|
|
|
2018-11-11 13:58:52 +01:00
|
|
|
class NotificationManager : public Actor {
|
2018-11-09 15:14:02 +01:00
|
|
|
public:
|
2018-12-12 03:10:47 +01:00
|
|
|
static constexpr int32 MIN_NOTIFICATION_GROUP_COUNT_MAX = 0;
|
2018-11-15 16:58:33 +01:00
|
|
|
static constexpr int32 MAX_NOTIFICATION_GROUP_COUNT_MAX = 25;
|
|
|
|
static constexpr int32 MIN_NOTIFICATION_GROUP_SIZE_MAX = 1;
|
|
|
|
static constexpr int32 MAX_NOTIFICATION_GROUP_SIZE_MAX = 25;
|
|
|
|
|
2018-11-11 13:58:52 +01:00
|
|
|
NotificationManager(Td *td, ActorShared<> parent);
|
2018-11-09 15:14:02 +01:00
|
|
|
|
2018-12-02 22:55:35 +01:00
|
|
|
size_t get_max_notification_group_size() const;
|
2018-11-28 21:19:30 +01:00
|
|
|
|
2018-11-17 22:24:19 +01:00
|
|
|
NotificationId get_max_notification_id() const;
|
|
|
|
|
2018-11-10 00:08:47 +01:00
|
|
|
NotificationId get_next_notification_id();
|
2018-11-09 23:56:00 +01:00
|
|
|
|
2018-11-11 13:58:52 +01:00
|
|
|
NotificationGroupId get_next_notification_group_id();
|
|
|
|
|
2018-12-04 19:01:41 +01:00
|
|
|
void try_reuse_notification_group_id(NotificationGroupId group_id);
|
|
|
|
|
2018-12-02 20:04:47 +01:00
|
|
|
void load_group_force(NotificationGroupId group_id);
|
|
|
|
|
2018-12-23 22:34:40 +01:00
|
|
|
void add_notification(NotificationGroupId group_id, NotificationGroupType group_type, DialogId dialog_id, int32 date,
|
2019-01-08 19:24:06 +01:00
|
|
|
DialogId notification_settings_dialog_id, bool is_silent, int32 min_delay_ms,
|
|
|
|
NotificationId notification_id, unique_ptr<NotificationType> type);
|
2018-11-09 23:56:00 +01:00
|
|
|
|
2018-11-16 16:00:46 +01:00
|
|
|
void edit_notification(NotificationGroupId group_id, NotificationId notification_id,
|
|
|
|
unique_ptr<NotificationType> type);
|
2018-11-09 23:56:00 +01:00
|
|
|
|
2018-11-20 14:11:34 +01:00
|
|
|
void remove_notification(NotificationGroupId group_id, NotificationId notification_id, bool is_permanent,
|
|
|
|
Promise<Unit> &&promise);
|
2018-11-09 15:14:02 +01:00
|
|
|
|
2018-11-10 00:08:47 +01:00
|
|
|
void remove_notification_group(NotificationGroupId group_id, NotificationId max_notification_id,
|
2018-11-20 15:08:44 +01:00
|
|
|
MessageId max_message_id, int32 new_total_count, Promise<Unit> &&promise);
|
2018-11-09 15:14:02 +01:00
|
|
|
|
2018-12-25 16:04:29 +01:00
|
|
|
void set_notification_total_count(NotificationGroupId group_id, int32 new_total_count);
|
|
|
|
|
2019-01-10 18:54:46 +01:00
|
|
|
vector<MessageId> get_notification_group_message_ids(NotificationGroupId group_id);
|
|
|
|
|
2018-12-18 21:59:35 +01:00
|
|
|
void add_call_notification(DialogId dialog_id, CallId call_id);
|
|
|
|
|
|
|
|
void remove_call_notification(DialogId dialog_id, CallId call_id);
|
|
|
|
|
2018-12-12 03:10:47 +01:00
|
|
|
void on_notification_group_count_max_changed(bool send_updates);
|
2018-11-15 16:58:33 +01:00
|
|
|
|
|
|
|
void on_notification_group_size_max_changed();
|
|
|
|
|
2018-11-15 23:03:04 +01:00
|
|
|
void on_online_cloud_timeout_changed();
|
|
|
|
|
|
|
|
void on_notification_cloud_delay_changed();
|
|
|
|
|
|
|
|
void on_notification_default_delay_changed();
|
|
|
|
|
2018-12-28 23:48:32 +01:00
|
|
|
void process_push_notification(string payload, Promise<Unit> &&promise);
|
|
|
|
|
|
|
|
static Result<int64> get_push_receiver_id(string payload);
|
2018-12-24 01:12:59 +01:00
|
|
|
|
2018-11-22 02:00:28 +01:00
|
|
|
void before_get_difference();
|
|
|
|
|
|
|
|
void after_get_difference();
|
|
|
|
|
|
|
|
void before_get_chat_difference(NotificationGroupId group_id);
|
|
|
|
|
|
|
|
void after_get_chat_difference(NotificationGroupId group_id);
|
|
|
|
|
2018-12-05 17:11:28 +01:00
|
|
|
void get_current_state(vector<td_api::object_ptr<td_api::Update>> &updates) const;
|
|
|
|
|
2018-12-24 03:53:24 +01:00
|
|
|
void flush_all_notifications();
|
|
|
|
|
2018-12-24 02:08:52 +01:00
|
|
|
void destroy_all_notifications();
|
|
|
|
|
2018-11-09 15:14:02 +01:00
|
|
|
private:
|
2018-12-12 03:10:47 +01:00
|
|
|
static constexpr int32 DEFAULT_GROUP_COUNT_MAX = 0;
|
2018-11-15 16:58:33 +01:00
|
|
|
static constexpr int32 DEFAULT_GROUP_SIZE_MAX = 10;
|
|
|
|
static constexpr size_t EXTRA_GROUP_SIZE = 10;
|
|
|
|
|
2018-12-18 21:59:35 +01:00
|
|
|
static constexpr size_t MAX_CALL_NOTIFICATION_GROUPS = 10;
|
|
|
|
static constexpr size_t MAX_CALL_NOTIFICATIONS = 10;
|
|
|
|
|
2018-11-15 16:58:33 +01:00
|
|
|
static constexpr int32 DEFAULT_ONLINE_CLOUD_TIMEOUT_MS = 300000;
|
|
|
|
static constexpr int32 DEFAULT_ONLINE_CLOUD_DELAY_MS = 30000;
|
|
|
|
static constexpr int32 DEFAULT_DEFAULT_DELAY_MS = 1500;
|
|
|
|
|
|
|
|
static constexpr int32 MIN_NOTIFICATION_DELAY_MS = 1;
|
|
|
|
|
2018-11-21 23:28:56 +01:00
|
|
|
static constexpr int32 MIN_UPDATE_DELAY_MS = 50;
|
|
|
|
static constexpr int32 MAX_UPDATE_DELAY_MS = 60000;
|
|
|
|
|
2018-11-12 15:44:42 +01:00
|
|
|
struct PendingNotification {
|
2018-11-15 16:58:33 +01:00
|
|
|
int32 date = 0;
|
2018-11-12 15:44:42 +01:00
|
|
|
DialogId settings_dialog_id;
|
2018-11-15 16:58:33 +01:00
|
|
|
bool is_silent = false;
|
2018-11-12 15:44:42 +01:00
|
|
|
NotificationId notification_id;
|
|
|
|
unique_ptr<NotificationType> type;
|
|
|
|
};
|
|
|
|
|
2018-11-15 16:58:33 +01:00
|
|
|
struct NotificationGroup {
|
2018-11-12 15:44:42 +01:00
|
|
|
int32 total_count = 0;
|
2018-12-23 22:34:40 +01:00
|
|
|
NotificationGroupType type = NotificationGroupType::Calls;
|
2018-12-03 16:38:29 +01:00
|
|
|
bool is_loaded_from_database = false;
|
|
|
|
bool is_being_loaded_from_database = false;
|
2018-11-12 15:44:42 +01:00
|
|
|
|
|
|
|
vector<Notification> notifications;
|
2018-11-15 16:58:33 +01:00
|
|
|
|
|
|
|
double pending_notifications_flush_time = 0;
|
2018-11-12 15:44:42 +01:00
|
|
|
vector<PendingNotification> pending_notifications;
|
|
|
|
};
|
|
|
|
|
2018-11-15 16:58:33 +01:00
|
|
|
using NotificationGroups = std::map<NotificationGroupKey, NotificationGroup>;
|
|
|
|
|
|
|
|
static void on_flush_pending_notifications_timeout_callback(void *notification_manager_ptr, int64 group_id_int);
|
|
|
|
|
2018-11-21 23:28:56 +01:00
|
|
|
static void on_flush_pending_updates_timeout_callback(void *notification_manager_ptr, int64 group_id_int);
|
|
|
|
|
2018-11-12 15:44:42 +01:00
|
|
|
bool is_disabled() const;
|
|
|
|
|
2018-11-11 13:58:52 +01:00
|
|
|
void start_up() override;
|
|
|
|
void tear_down() override;
|
|
|
|
|
2018-11-21 23:28:56 +01:00
|
|
|
void add_update(int32 group_id, td_api::object_ptr<td_api::Update> update);
|
|
|
|
|
|
|
|
void add_update_notification_group(td_api::object_ptr<td_api::updateNotificationGroup> update);
|
2018-11-15 16:58:33 +01:00
|
|
|
|
2018-11-21 23:28:56 +01:00
|
|
|
void add_update_notification(NotificationGroupId notification_group_id, DialogId dialog_id,
|
|
|
|
const Notification ¬ification);
|
2018-11-16 16:00:46 +01:00
|
|
|
|
2018-12-02 21:03:05 +01:00
|
|
|
NotificationGroups::iterator add_group(NotificationGroupKey &&group_key, NotificationGroup &&group);
|
|
|
|
|
2018-11-15 16:58:33 +01:00
|
|
|
NotificationGroups::iterator get_group(NotificationGroupId group_id);
|
|
|
|
|
2018-11-28 22:51:25 +01:00
|
|
|
NotificationGroups::iterator get_group_force(NotificationGroupId group_id, bool send_update = true);
|
|
|
|
|
2018-12-02 21:03:05 +01:00
|
|
|
void delete_group(NotificationGroups::iterator &&group_it);
|
|
|
|
|
2018-12-03 16:38:29 +01:00
|
|
|
static NotificationId get_first_notification_id(const NotificationGroup &group);
|
|
|
|
|
2018-12-05 23:59:33 +01:00
|
|
|
static MessageId get_first_message_id(const NotificationGroup &group);
|
|
|
|
|
2018-11-30 16:11:58 +01:00
|
|
|
int32 load_message_notification_groups_from_database(int32 limit, bool send_update);
|
2018-11-26 18:05:06 +01:00
|
|
|
|
2018-12-03 16:38:29 +01:00
|
|
|
void load_message_notifications_from_database(const NotificationGroupKey &group_key, NotificationGroup &group,
|
|
|
|
size_t desired_size);
|
|
|
|
|
|
|
|
void on_get_message_notifications_from_database(NotificationGroupId group_id, size_t limit,
|
|
|
|
Result<vector<Notification>> r_notifications);
|
|
|
|
|
|
|
|
void add_notifications_to_group_begin(NotificationGroups::iterator group_it, vector<Notification> notifications);
|
2018-12-02 23:25:34 +01:00
|
|
|
|
2018-11-15 16:58:33 +01:00
|
|
|
NotificationGroupKey get_last_updated_group_key() const;
|
|
|
|
|
2018-12-24 02:08:52 +01:00
|
|
|
td_api::object_ptr<td_api::updateActiveNotifications> get_update_active_notifications() const;
|
|
|
|
|
|
|
|
td_api::object_ptr<td_api::updateNotificationGroup> get_remove_group_update(
|
|
|
|
const NotificationGroupKey &group_key, const NotificationGroup &group,
|
|
|
|
vector<int32> &&removed_notification_ids) const;
|
2018-12-05 17:11:28 +01:00
|
|
|
|
2018-11-21 16:00:14 +01:00
|
|
|
void send_remove_group_update(const NotificationGroupKey &group_key, const NotificationGroup &group,
|
|
|
|
vector<int32> &&removed_notification_ids);
|
2018-11-15 16:58:33 +01:00
|
|
|
|
|
|
|
void send_add_group_update(const NotificationGroupKey &group_key, const NotificationGroup &group);
|
|
|
|
|
2019-01-08 19:24:06 +01:00
|
|
|
int32 get_notification_delay_ms(DialogId dialog_id, const PendingNotification ¬ification,
|
|
|
|
int32 min_delay_ms) const;
|
2018-11-15 16:58:33 +01:00
|
|
|
|
2018-11-21 13:23:43 +01:00
|
|
|
void do_flush_pending_notifications(NotificationGroupKey &group_key, NotificationGroup &group,
|
|
|
|
vector<PendingNotification> &pending_notifications);
|
2018-11-15 16:58:33 +01:00
|
|
|
|
|
|
|
void flush_pending_notifications(NotificationGroupId group_id);
|
|
|
|
|
2018-11-27 02:10:52 +01:00
|
|
|
void flush_all_pending_notifications();
|
|
|
|
|
2018-11-20 14:11:34 +01:00
|
|
|
void on_notifications_removed(NotificationGroups::iterator &&group_it,
|
|
|
|
vector<td_api::object_ptr<td_api::notification>> &&added_notifications,
|
|
|
|
vector<int32> &&removed_notification_ids);
|
|
|
|
|
2018-11-22 18:17:26 +01:00
|
|
|
void remove_added_notifications_from_pending_updates(
|
|
|
|
NotificationGroupId group_id,
|
|
|
|
std::function<bool(const td_api::object_ptr<td_api::notification> ¬ification)> is_removed);
|
|
|
|
|
2018-11-23 12:42:34 +01:00
|
|
|
void flush_pending_updates(int32 group_id, const char *source);
|
2018-11-21 23:28:56 +01:00
|
|
|
|
2018-11-27 02:10:52 +01:00
|
|
|
void flush_all_pending_updates(bool include_delayed_chats, const char *source);
|
|
|
|
|
2018-12-18 21:59:35 +01:00
|
|
|
NotificationGroupId get_call_notification_group_id(DialogId dialog_id);
|
|
|
|
|
2018-12-12 03:10:47 +01:00
|
|
|
void after_get_difference_impl();
|
|
|
|
|
|
|
|
void after_get_chat_difference_impl(NotificationGroupId group_id);
|
|
|
|
|
2018-12-26 18:23:12 +01:00
|
|
|
void on_pending_notification_update_count_changed(int32 diff, int32 notification_group_id, const char *source);
|
2018-12-24 17:45:19 +01:00
|
|
|
|
2018-11-11 13:58:52 +01:00
|
|
|
NotificationId current_notification_id_;
|
|
|
|
NotificationGroupId current_notification_group_id_;
|
|
|
|
|
2018-11-15 16:58:33 +01:00
|
|
|
size_t max_notification_group_count_ = 0;
|
|
|
|
size_t max_notification_group_size_ = 0;
|
|
|
|
size_t keep_notification_group_size_ = 0;
|
|
|
|
|
|
|
|
int32 online_cloud_timeout_ms_ = DEFAULT_ONLINE_CLOUD_TIMEOUT_MS;
|
|
|
|
int32 notification_cloud_delay_ms_ = DEFAULT_ONLINE_CLOUD_DELAY_MS;
|
|
|
|
int32 notification_default_delay_ms_ = DEFAULT_DEFAULT_DELAY_MS;
|
|
|
|
|
2018-12-24 17:45:19 +01:00
|
|
|
int32 pending_notification_update_count_ = 0;
|
|
|
|
|
2018-12-02 22:30:05 +01:00
|
|
|
NotificationGroupKey last_loaded_notification_group_key_;
|
2018-11-28 22:51:25 +01:00
|
|
|
|
2018-12-24 02:08:52 +01:00
|
|
|
bool is_destroyed_ = false;
|
|
|
|
|
2018-11-21 23:28:56 +01:00
|
|
|
bool running_get_difference_ = false;
|
2018-11-22 02:00:28 +01:00
|
|
|
std::unordered_set<int32> running_get_chat_difference_;
|
2018-11-21 23:28:56 +01:00
|
|
|
|
2018-11-15 16:58:33 +01:00
|
|
|
NotificationGroups groups_;
|
2018-12-02 21:28:24 +01:00
|
|
|
std::unordered_map<NotificationGroupId, NotificationGroupKey, NotificationGroupIdHash> group_keys_;
|
2018-11-12 15:44:42 +01:00
|
|
|
|
2018-11-21 23:28:56 +01:00
|
|
|
std::unordered_map<int32, vector<td_api::object_ptr<td_api::Update>>> pending_updates_;
|
|
|
|
|
2018-11-15 16:58:33 +01:00
|
|
|
MultiTimeout flush_pending_notifications_timeout_{"FlushPendingNotificationsTimeout"};
|
2018-11-21 23:28:56 +01:00
|
|
|
MultiTimeout flush_pending_updates_timeout_{"FlushPendingUpdatesTimeout"};
|
2018-11-12 15:44:42 +01:00
|
|
|
|
2018-12-18 21:59:35 +01:00
|
|
|
vector<NotificationGroupId> call_notification_group_ids_;
|
|
|
|
std::unordered_set<NotificationGroupId, NotificationGroupIdHash> available_call_notification_group_ids_;
|
|
|
|
std::unordered_map<DialogId, NotificationGroupId, DialogIdHash> dialog_id_to_call_notification_group_id_;
|
|
|
|
|
|
|
|
struct ActiveCallNotification {
|
|
|
|
CallId call_id;
|
|
|
|
NotificationId notification_id;
|
|
|
|
};
|
|
|
|
std::unordered_map<DialogId, vector<ActiveCallNotification>, DialogIdHash> active_call_notifications_;
|
|
|
|
|
2018-11-09 15:14:02 +01:00
|
|
|
Td *td_;
|
2018-11-11 13:58:52 +01:00
|
|
|
ActorShared<> parent_;
|
2018-11-09 15:14:02 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace td
|