|
| 1 | +import * as yup from 'yup'; |
| 2 | +import { getLatestReleases } from './github.js'; |
| 3 | + |
| 4 | +export async function postNotifications(req, res) { |
| 5 | + const notifications = await getLatestReleases(); |
| 6 | + |
| 7 | + if (req.body.op === 'getNotificationCount') { |
| 8 | + return res.status(200).json(notifications.map(rel => rel.id)); |
| 9 | + } else if (req.body.op === 'getNotifications') { |
| 10 | + return res.status(200).json(notifications.map(rel => ({ |
| 11 | + id: rel.id, |
| 12 | + name: rel.name, |
| 13 | + content: makeFullPageFromDiv(rel.body_html, rel.name), |
| 14 | + date: rel.published_at, |
| 15 | + }))); |
| 16 | + } else { |
| 17 | + return res.status(400).json({ |
| 18 | + error: 'Invalid operation', |
| 19 | + }); |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +function makeFullPageFromDiv(div, name) { |
| 24 | + name = htmlEncode(name); |
| 25 | + return `<html> |
| 26 | +<head> |
| 27 | + <title>${name}</title> |
| 28 | + <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.6/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-4Q6Gf2aSP4eDXB8Miphtr37CMZZQ5oXLH2yaXMJ2w8e2ZtHTl7GptT4jmndRuHDT" crossorigin="anonymous"> |
| 29 | + <style> |
| 30 | + body { |
| 31 | + padding: 1em; |
| 32 | + } |
| 33 | + </style> |
| 34 | +</head> |
| 35 | +<body> |
| 36 | + <main> |
| 37 | + <h1>${name}</h1> |
| 38 | + <div> |
| 39 | + ${div} |
| 40 | + </div> |
| 41 | + </main> |
| 42 | +</body> |
| 43 | +</html>`; |
| 44 | +} |
| 45 | + |
| 46 | +function htmlEncode(text) { |
| 47 | + return text |
| 48 | + .replace(/&/g, '&') |
| 49 | + .replace(/</g, '<') |
| 50 | + .replace(/>/g, '>') |
| 51 | + .replace(/"/g, '"') |
| 52 | + .replace(/'/g, '''); |
| 53 | +} |
| 54 | + |
| 55 | +/* |
| 56 | +POST https://connect.mirthcorp.com/NotificationServlet HTTP/1.1 |
| 57 | +Content-Type: application/x-www-form-urlencoded; charset=UTF-8 |
| 58 | +User-Agent: Apache-HttpClient/4.5.13 (Java/21.0.7) |
| 59 | +Host: connect.mirthcorp.com |
| 60 | +Content-Length: 2116 |
| 61 | +
|
| 62 | +op=getNotificationCount&serverId=49885e13-4f2e-41a6-b66a-8e65f5492d9a&version=4.5.2&extensionVersions=url_encode_json |
| 63 | +*/ |
| 64 | +export const notificationBodySchema = yup.object({ |
| 65 | + op: yup.string().oneOf(['getNotificationCount', 'getNotifications']).required(), |
| 66 | + serverId: yup.string().required(), |
| 67 | + version: yup.string().required(), |
| 68 | + extensionVersions: yup.string().required(), |
| 69 | +}).required(); |
| 70 | + |
| 71 | +/* |
| 72 | +extensionVersions is a JSON object with the version of each extension: |
| 73 | +{ |
| 74 | + "Server Log":"4.5.2", |
| 75 | + "File Writer":"4.5.2", |
| 76 | + ... |
| 77 | +} |
| 78 | +*/ |
0 commit comments