|
1 | 1 | // Push notifications for TreeHacks Live |
2 | 2 |
|
3 | 3 | import { Request, Response } from 'express'; |
4 | | -import { PushSubscription } from 'web-push'; |
| 4 | +import webpush, { PushSubscription } from 'web-push'; |
5 | 5 | import axios from 'axios'; |
6 | | -import LiveNotificationSubscription from '../models/LiveNotificationSubscription'; |
7 | 6 | import { EventiveResponse } from '../services/live_notifications'; |
| 7 | +import LiveNotificationSubscription from '../models/LiveNotificationSubscription'; |
| 8 | + |
| 9 | +webpush.setVapidDetails( |
| 10 | + 'mailto:hello@treehacks.com', |
| 11 | + process.env.VAPID_PUBLIC_KEY, |
| 12 | + process.env.VAPID_PRIVATE_KEY |
| 13 | +); |
8 | 14 |
|
9 | 15 | const EVENTS_API_URL = `https://api.eventive.org/event_buckets/${process.env.EVENTIVE_EVENT_BUCKET}/events?api_key=${process.env.EVENTIVE_API_KEY}`; |
10 | 16 |
|
11 | | -async function getEvent(eventId: string) { |
| 17 | +async function getEvents() { |
12 | 18 | const req = await axios.get<EventiveResponse>(EVENTS_API_URL); |
13 | | - const events = req.data.events; |
| 19 | + return req.data.events; |
| 20 | +} |
| 21 | + |
| 22 | +async function getEvent(eventId: string) { |
| 23 | + const events = await getEvents(); |
14 | 24 | return events.find((evt) => evt.id === eventId); |
15 | 25 | } |
16 | 26 |
|
@@ -84,3 +94,67 @@ export async function deleteEventPushSubscription(req: Request, res: Response) { |
84 | 94 | const events = await getSubscriptions(sub.endpoint); |
85 | 95 | return res.json({ subscriptions: events }); |
86 | 96 | } |
| 97 | + |
| 98 | +// Admin endpoints |
| 99 | + |
| 100 | +export interface LiveStats { |
| 101 | + numDevices: number; |
| 102 | + numSubscriptions: number; |
| 103 | + events: Array<{ id: string; name: string; numSubscriptions: number }>; |
| 104 | +} |
| 105 | + |
| 106 | +export async function getLiveStats(req: Request, res: Response) { |
| 107 | + const subscriptions = await LiveNotificationSubscription.find(); |
| 108 | + const numDevices = new Set( |
| 109 | + subscriptions.map((sub) => sub.subscription.endpoint) |
| 110 | + ).size; |
| 111 | + const numSubscriptions = subscriptions.length; |
| 112 | + |
| 113 | + const eventData = await getEvents(); |
| 114 | + const groupedSubscriptions = await LiveNotificationSubscription.aggregate([ |
| 115 | + { $group: { _id: '$eventId', count: { $sum: 1 } } }, |
| 116 | + ]); |
| 117 | + |
| 118 | + const eventStats = await Promise.all( |
| 119 | + groupedSubscriptions.map(async (event) => { |
| 120 | + const evt = eventData.find((e) => e.id === event._id); |
| 121 | + return evt != null |
| 122 | + ? { id: evt.id, name: evt.name, numSubscriptions: event.count } |
| 123 | + : null; |
| 124 | + }) |
| 125 | + ).then((events) => events.filter((e) => e != null)); |
| 126 | + |
| 127 | + return res.json({ numDevices, numSubscriptions, events: eventStats }); |
| 128 | +} |
| 129 | + |
| 130 | +export async function sendLiveNotification(req: Request, res: Response) { |
| 131 | + const title = req.body.title; |
| 132 | + const body = req.body.body; |
| 133 | + |
| 134 | + if (title == null || body == null) { |
| 135 | + return res.status(400).json({ error: 'Invalid notification' }); |
| 136 | + } |
| 137 | + |
| 138 | + const uniqueSubscriptions = await LiveNotificationSubscription.aggregate([ |
| 139 | + { |
| 140 | + $group: { |
| 141 | + _id: '$subscription.endpoint', |
| 142 | + subscription: { $first: '$subscription' }, |
| 143 | + }, |
| 144 | + }, |
| 145 | + ]); |
| 146 | + |
| 147 | + const payload = JSON.stringify({ title, body }); |
| 148 | + |
| 149 | + await Promise.all( |
| 150 | + uniqueSubscriptions.map(async (sub) => { |
| 151 | + try { |
| 152 | + await webpush.sendNotification(sub.subscription, payload); |
| 153 | + } catch (err) { |
| 154 | + console.error('Error sending notification', err); |
| 155 | + } |
| 156 | + }) |
| 157 | + ); |
| 158 | + |
| 159 | + return res.json({ success: true }); |
| 160 | +} |
0 commit comments