-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnotificationManager.js
More file actions
195 lines (172 loc) · 6.72 KB
/
notificationManager.js
File metadata and controls
195 lines (172 loc) · 6.72 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import { GitHubApi } from "./githubApi.js";
import { gettext as _, ngettext } from "resource:///org/gnome/shell/extensions/extension.js";
/**
* Converts a GitHub REST API subject URL to its corresponding web URL.
* Supports both github.com and GitHub Enterprise Server.
* e.g. https://api.github.com/repos/owner/repo/pulls/1 → https://github.com/owner/repo/pull/1
* e.g. https://ghe.example.com/api/v3/repos/owner/repo/pulls/1 → https://ghe.example.com/owner/repo/pull/1
*
* @param {string|null|undefined} apiUrl
* @param {string} enterpriseUrl - base URL of the GHE instance, or empty for github.com
* @returns {string|null}
*/
function _subjectApiToWebUrl(apiUrl, enterpriseUrl = "") {
if (!apiUrl) return null;
let webUrl;
if (enterpriseUrl) {
const base = enterpriseUrl.replace(/\/$/, "");
webUrl = apiUrl.replace(`${base}/api/v3/repos/`, `${base}/`);
} else {
webUrl = apiUrl.replace("https://api.github.com/repos/", "https://github.com/");
}
return webUrl.replace(/\/commits\/([a-f0-9]+)$/, "/commit/$1");
}
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|null): void} opts.sendNotification
* @param {object} opts.ui - GitHubTrayUI instance
*/
constructor({ httpSession, settings, sendNotification, ui }) {
this._httpSession = httpSession;
this._settings = settings;
this._sendNotification = sendNotification;
this._ui = ui;
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);
notifications = this._filter(notifications);
// Enrich notifications with state info (open/closed/merged) via GraphQL
try {
const stateMap = await api.fetchNotificationStates(token, notifications);
for (const n of notifications) {
n._stateInfo = stateMap.get(n.id) ?? null;
}
} catch (graphqlError) {
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) ?? url;
}
this._sendNotification(
_("GitHub Notifications"),
ngettext(
"%d new notification",
"%d new notifications",
newCount,
).format(newCount),
url,
);
}
}
}
} catch (error) {
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);
// 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) {
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;
}
}