-
Notifications
You must be signed in to change notification settings - Fork 285
Expand file tree
/
Copy pathnotifications.ts
More file actions
137 lines (119 loc) · 3.54 KB
/
notifications.ts
File metadata and controls
137 lines (119 loc) · 3.54 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
import { logError, logWarn } from '../../../shared/logger';
import type {
AccountNotifications,
GitifyState,
SettingsState,
} from '../../types';
import type { GitifySubject, Notification } from '../../typesGitHub';
import { listNotificationsForAuthenticatedUser } from '../api/client';
import { determineFailureType } from '../api/errors';
import { updateTrayIcon } from '../comms';
import { getGitifySubjectDetails } from '../subject';
import {
filterBaseNotifications,
filterDetailedNotifications,
} from './filters/filter';
export function setTrayIconColor(notifications: AccountNotifications[]) {
const allNotificationsCount = getNotificationCount(notifications);
updateTrayIcon(allNotificationsCount);
}
export function getNotificationCount(notifications: AccountNotifications[]) {
return notifications.reduce(
(sum, account) => sum + account.notifications.length,
0,
);
}
function getNotifications(state: GitifyState) {
return state.auth.accounts.map((account) => {
return {
account,
notifications: listNotificationsForAuthenticatedUser(
account,
state.settings,
),
};
});
}
export async function getAllNotifications(
state: GitifyState,
): Promise<AccountNotifications[]> {
const responses = await Promise.all([...getNotifications(state)]);
const notifications: AccountNotifications[] = await Promise.all(
responses
.filter((response) => !!response)
.map(async (accountNotifications) => {
try {
let notifications = (
await accountNotifications.notifications
).data.map((notification: Notification) => ({
...notification,
account: accountNotifications.account,
}));
notifications = filterBaseNotifications(
notifications,
state.settings,
);
notifications = await enrichNotifications(
notifications,
state.settings,
);
notifications = filterDetailedNotifications(
notifications,
state.settings,
);
return {
account: accountNotifications.account,
notifications: notifications,
error: null,
};
} catch (err) {
logError(
'getAllNotifications',
'error occurred while fetching account notifications',
err,
);
return {
account: accountNotifications.account,
notifications: [],
error: determineFailureType(err),
};
}
}),
);
return notifications;
}
export async function enrichNotifications(
notifications: Notification[],
settings: SettingsState,
): Promise<Notification[]> {
if (!settings.detailedNotifications) {
return notifications;
}
const enrichedNotifications = await Promise.all(
notifications.map(async (notification: Notification) => {
let additionalSubjectDetails: GitifySubject = {};
try {
additionalSubjectDetails = await getGitifySubjectDetails(notification);
} catch (err) {
logError(
'enrichNotifications',
'failed to enrich notification details for',
err,
notification,
);
logWarn(
'enrichNotifications',
'Continuing with base notification details',
);
}
return {
...notification,
subject: {
...notification.subject,
...additionalSubjectDetails,
},
};
}),
);
return enrichedNotifications;
}