MCPcopy Create free account
hub / github.com/CommE2E/comm / webPush

Function webPush

keyserver/src/push/utils.js:242–288  ·  view source on GitHub ↗
(
  targetedNotifications: $ReadOnlyArray<TargetedWebNotification>,
)

Source from the content-addressed store, hash-verified

240 +error?: WebPushError,
241};
242async function webPush(
243 targetedNotifications: $ReadOnlyArray<TargetedWebNotification>,
244): Promise<WebPushResult> {
245 await ensureWebPushInitialized();
246
247 const pushResults: $ReadOnlyArray<WebPushAttempt> = await Promise.all(
248 targetedNotifications.map(
249 async ({ notification, deliveryID: deviceTokenString }) => {
250 const deviceToken: PushSubscriptionJSON = JSON.parse(deviceTokenString);
251 const notificationString = JSON.stringify(notification);
252 try {
253 await webpush.sendNotification(deviceToken, notificationString);
254 } catch (error) {
255 return ({ error }: WebPushAttempt);
256 }
257 return {};
258 },
259 ),
260 );
261
262 const errors = [];
263 const invalidTokens = [];
264 const deviceTokens = targetedNotifications.map(
265 ({ deliveryID }) => deliveryID,
266 );
267 for (let i = 0; i < pushResults.length; i++) {
268 const pushResult = pushResults[i];
269 const { error } = pushResult;
270 if (error) {
271 errors.push(error);
272 if (webInvalidTokenErrorCodes.includes(error.statusCode)) {
273 invalidTokens.push(deviceTokens[i]);
274 }
275 }
276 }
277
278 const result: WritableWebPushResult = {};
279 if (errors.length > 0) {
280 result.errors = errors;
281 } else {
282 result.success = true;
283 }
284 if (invalidTokens.length > 0) {
285 result.invalidTokens = invalidTokens;
286 }
287 return result;
288}
289
290export type WNSPushError = any | string | Response;
291type WritableWNSPushResult = {

Callers 1

sendWebNotificationsFunction · 0.90

Calls 3

ensureWebPushInitializedFunction · 0.90
parseMethod · 0.80
pushMethod · 0.80

Tested by

no test coverage detected