-
-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathnotifications.js
More file actions
91 lines (74 loc) · 2.2 KB
/
notifications.js
File metadata and controls
91 lines (74 loc) · 2.2 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
import { fetchActivityPub } from './request.js';
export async function waitForItemInNotifications(
input,
options = {
retryCount: 0,
delay: 0,
},
) {
let matcher;
if (typeof input === 'string') {
matcher = (notification) => {
return notification.post?.id === input;
};
} else {
matcher = input;
}
const MAX_RETRIES = 5;
const response = await fetchActivityPub(
'https://self.test/.ghost/activitypub/v1/notifications',
{
headers: {
Accept: 'application/ld+json',
},
},
);
const json = await response.json();
const found = json.notifications.find((notificiation) => {
return matcher(notificiation);
});
if (found) {
return found;
}
if (options.retryCount === MAX_RETRIES) {
throw new Error(
`Max retries reached when waiting on item in notifications`,
);
}
if (options.delay > 0) {
await new Promise((resolve) => setTimeout(resolve, options.delay));
}
return await waitForItemInNotifications(matcher, {
retryCount: options.retryCount + 1,
delay: options.delay + 500,
});
}
export async function waitForUnreadNotifications(
unreadNotificationCount,
options = {
retryCount: 0,
delay: 0,
},
) {
const MAX_RETRIES = 5;
const response = await fetchActivityPub(
'https://self.test/.ghost/activitypub/v1/notifications/unread/count',
);
const responseJson = await response.clone().json();
const found = responseJson.count === unreadNotificationCount;
if (found) {
return found;
}
if (options.retryCount === MAX_RETRIES) {
throw new Error(
`Max retries reached (${MAX_RETRIES}) when waiting for notifications count ${unreadNotificationCount}. Notification count found ${responseJson.count}`,
);
}
if (options.delay > 0) {
await new Promise((resolve) => setTimeout(resolve, options.delay));
}
return await waitForUnreadNotifications(unreadNotificationCount, {
retryCount: options.retryCount + 1,
delay: options.delay + 500,
});
}