-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathNotificationManager.cpp
135 lines (117 loc) · 3.55 KB
/
NotificationManager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include "NotificationManager.hpp"
#include "td/telegram/TdDb.h"
#include "td/telegram/td_api.h"
#include <libnotify/notify.h>
#include <memory>
#include "GlobalParameters.hpp"
#include "ChatManager.hpp"
#include "TdObjectsOutput.h"
namespace tdcurses {
NotificationManager::Notification::Notification(td::int64 chat_id, const td::td_api::notification &message)
: chat_id_(chat_id) {
update_info(message);
auto notification = notify_notification_new(header_.c_str(), text_.c_str(), nullptr);
if (!notification) {
LOG(ERROR) << "failed to create notification";
return;
}
GError *error = nullptr;
if (!notify_notification_show(notification, &error)) {
LOG(ERROR) << "failed to show notification";
if (error) {
g_error_free(error);
}
g_free(notification);
return;
}
notification_ = notification;
}
NotificationManager::Notification::~Notification() {
delete_notification();
}
void NotificationManager::Notification::update_info(const td::td_api::notification &n) {
auto chat = chat_manager().get_chat(chat_id_);
header_ = (chat ? chat->title() : "UPDATE");
if (n.type_->get_id() != td::td_api::notificationTypeNewMessage::ID) {
return;
}
auto ¬if = static_cast<const td::td_api::notificationTypeNewMessage &>(*n.type_);
if (!notif.show_preview_) {
text_ = "NEW MESSAGE";
return;
}
Outputter out;
out << *notif.message_;
text_ = out.as_str();
}
void NotificationManager::Notification::process_update(td::td_api::notification &update) {
update_info(update);
if (!notification_) {
return;
}
auto ptr = static_cast<::NotifyNotification *>(notification_);
notify_notification_update(ptr, header_.c_str(), text_.c_str(), nullptr);
}
void NotificationManager::Notification::delete_notification() {
if (!notification_) {
return;
}
auto ptr = static_cast<::NotifyNotification *>(notification_);
GError *error = nullptr;
notify_notification_close(ptr, &error);
g_free(ptr);
if (error) {
g_error_free(error);
}
notification_ = nullptr;
}
void NotificationManager::process_update(td::td_api::updateNotification &update) {
if (!global_parameters().notifications_enabled()) {
return;
}
NotificationId id;
id.group_id = update.notification_group_id_;
id.notification_id = update.notification_->id_;
auto it = notifications_.find(id);
if (it == notifications_.end()) {
return;
}
it->second->process_update(*update.notification_);
}
void NotificationManager::process_update(td::td_api::updateNotificationGroup &update) {
if (!global_parameters().notifications_enabled()) {
return;
}
if (update.type_->get_id() != td::td_api::notificationGroupTypeMessages::ID) {
return;
}
for (auto &u : update.added_notifications_) {
NotificationId id;
id.group_id = update.notification_group_id_;
id.notification_id = u->id_;
auto it = notifications_.find(id);
if (it != notifications_.end()) {
return;
}
auto n = std::make_unique<Notification>(update.chat_id_, *u);
notifications_.emplace(id, std::move(n));
}
for (auto &u : update.removed_notification_ids_) {
NotificationId id;
id.group_id = update.notification_group_id_;
id.notification_id = u;
auto it = notifications_.find(id);
if (it != notifications_.end()) {
notifications_.erase(it);
return;
}
}
}
NotificationManager::NotificationManager() {
notify_init("telegram-curses");
}
NotificationManager ¬ification_manager() {
static NotificationManager notification_manager;
return notification_manager;
}
} // namespace tdcurses