Repository navigation
Expand file tree
/
Copy pathnotificationManager.js
More file actions
178 lines (158 loc) · 6.27 KB
/
Copy pathnotificationManager.js
File metadata and controls
178 lines (158 loc) · 6.27 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import { GitHubApi, isCancelled } from "./githubApi.js";
import { subjectApiToWebUrl } from "./utils.js";
import { gettext as _, ngettext } from "resource:///org/gnome/shell/extensions/extension.js";
export class NotificationManager {
/**
* @param {object} opts
* @param {import('gi://Soup').Session} opts.httpSession
* @param {import('gi://Gio').Settings} opts.settings
* @param {function(string, string, string, string|null): void} opts.sendNotification - Sends category, summary, body, and optional URL
* @param {object} opts.ui - GitHubTrayUI instance
*/
constructor({ httpSession, settings, sendNotification, ui, cancellable = null }) {
this._httpSession = httpSession;
this._settings = settings;
this._sendNotification = sendNotification;
this._ui = ui;
this._cancellable = cancellable;
this._lastNotifications = [];
this._unreadCount = 0;
}
get lastNotifications() {
return this._lastNotifications;
}
get unreadCount() {
return this._unreadCount;
}
// Loads notifications from GitHub, optionally merging with existing ones
async load(merge = false) {
const token = this._settings?.get_string("github-token");
if (!token) return;
try {
const api = new GitHubApi(this._httpSession, this._settings.get_string("github-enterprise-url"));
let notifications = await api.fetchNotifications(token, 100, this._cancellable);
notifications = this._filter(notifications);
// Enrich notifications with state info (open/closed/merged) via GraphQL
try {
const stateMap = await api.fetchNotificationStates(token, notifications, this._cancellable);
for (const n of notifications) {
n._stateInfo = stateMap.get(n.id) ?? null;
}
} catch (graphqlError) {
if (isCancelled(graphqlError)) return;
console.error(graphqlError, "GitHubTray:fetchNotificationStates");
// Non-fatal: continue without state enrichment
}
if (merge && this._lastNotifications.length > 0) {
// Append only new notifications not already in the buffer
const existingIds = new Set(this._lastNotifications.map((n) => n.id));
const newOnes = notifications.filter((n) => !existingIds.has(n.id));
// Preserve existing state info, merge new ones with their state
this._lastNotifications = [...this._lastNotifications, ...newOnes];
} else {
const oldIds = new Set(this._lastNotifications.map((n) => n.id));
const oldUnreadCount = this._unreadCount;
this._lastNotifications = notifications;
const unreadCount = notifications.filter((n) => n.unread).length;
this._unreadCount = unreadCount;
this._ui.updateBadge(unreadCount);
if (
unreadCount > oldUnreadCount &&
this._settings.get_boolean("desktop-notifications")
) {
const newCount = unreadCount - oldUnreadCount;
if (newCount > 0) {
// Determine the URL to open: specific page for a single new
// notification, or the general notifications page for multiple
const newUnread = notifications.filter(
(n) => n.unread && !oldIds.has(n.id),
);
const enterpriseUrl = this._settings.get_string("github-enterprise-url");
const baseWebUrl = enterpriseUrl ? enterpriseUrl.replace(/\/$/, "") : "https://github.com";
let url = `${baseWebUrl}/notifications`;
if (newUnread.length === 1) {
url = subjectApiToWebUrl(newUnread[0].subject?.url, enterpriseUrl, newUnread[0].subject?.type) ?? url;
}
this._sendNotification(
"github-notifications",
_("GitHub Notifications"),
ngettext(
"%d new notification",
"%d new notifications",
newCount,
).format(newCount),
url,
);
}
}
}
} catch (error) {
if (isCancelled(error)) return;
console.error(error, "GitHubTray:loadNotifications");
}
}
// Marks a single notification as read and refreshes the local buffer
async markRead(notification, callback) {
const token = this._settings?.get_string("github-token");
if (!token) {
callback();
return;
}
try {
const api = new GitHubApi(this._httpSession, this._settings.get_string("github-enterprise-url"));
await api.markNotificationRead(token, notification.id, this._cancellable);
// Remove from local buffer
this._lastNotifications = this._lastNotifications.filter(
(n) => n.id !== notification.id,
);
this._unreadCount = Math.max(0, this._unreadCount - 1);
this._ui.updateBadge(this._unreadCount);
// Refresh UI immediately with updated buffer
this._ui.refreshNotifications(this._lastNotifications);
// If buffer is running low, fetch more and merge new ones
if (this._lastNotifications.length < 5) {
await this.load(true);
this._ui.refreshNotifications(this._lastNotifications);
}
} catch (error) {
if (isCancelled(error)) return;
console.error(error, "GitHubTray:markNotificationRead");
callback();
}
}
// Filters notifications based on user settings
_filter(notifications) {
return notifications.filter((n) => {
const reason = n.reason;
const type = n.subject.type;
if (reason === "review_requested") {
return this._settings.get_boolean("notify-review-requests");
}
if (reason === "mention" || reason === "team_mention") {
return this._settings.get_boolean("notify-mentions");
}
if (reason === "assign") {
return this._settings.get_boolean("notify-assignments");
}
if (
type === "PullRequest" ||
type === "PullRequestReview" ||
type === "PullRequestReviewComment"
) {
return this._settings.get_boolean("notify-pr-comments");
}
if (type === "Issue" || type === "IssueComment") {
return this._settings.get_boolean("notify-issue-comments");
}
return true;
});
}
destroy() {
this._lastNotifications = [];
this._unreadCount = 0;
this._httpSession = null;
this._settings = null;
this._ui = null;
this._cancellable = null;
}
}