update dependencies and devDependencies
[websub-hub] / src / template / template-helper.js
1 'use strict';
2
3 const { TemplateHelper } = require('@squeep/html-template-helper');
4
5 /**
6 * Render a topic as a row of details.
7 * @param {Object} topic
8 * @param {Object[]} subscribers
9 * @param {Boolean} detailsLink
10 * @returns {String}
11 */
12 function renderTopicRow(topic, subscribers, detailsLink = true) {
13 if (!topic) {
14 return `<tr>
15 <th colspan="15">(topic not found)</th>
16 </tr>`;
17 }
18 return `<tr>
19 <th scope="row">${detailsLink ? '<a href="topic/' + topic.id + '">' : ''}${topic.url}${detailsLink ? '</a>' : ''}</th>
20 <td>${subscribers.length}</td>
21 <td>${TemplateHelper.dateFormat(topic.created, 'End of Time', 'Beginning of Time', 'Unknown')}</td>
22 <td>${TemplateHelper.secondsToPeriod(topic.leaseSecondsPreferred)}</td>
23 <td>${TemplateHelper.secondsToPeriod(topic.leaseSecondsMin)}</td>
24 <td>${TemplateHelper.secondsToPeriod(topic.leaseSecondsMax)}</td>
25 <td>${topic.publisherValidationUrl ? topic.publisherValidationUrl : 'None'}</td>
26 <td>${topic.isActive}</td>
27 <td>${topic.isDeleted}</td>
28 <td>${TemplateHelper.dateFormat(topic.lastPublish, 'End of Time', 'Never', 'Never')}</td>
29 <td>${TemplateHelper.dateFormat(topic.contentFetchNextAttempt, 'Next Publish', 'Pending', 'Next Publish')}</td>
30 <td>${topic.contentFetchAttemptsSinceSuccess}</td>
31 <td>${TemplateHelper.dateFormat(topic.contentUpdated, 'End of Time', 'Never', 'Never')}</td>
32 <td>${topic.contentType}</td>
33 <td>${topic.id}</td>
34 </tr>`;
35 }
36
37
38 /**
39 * Render the header row for topic details.
40 * @returns {String}
41 */
42 function renderTopicRowHeader() {
43 return `<tr>
44 <th scope="col">Topic URL</th>
45 <th scope="col">Subscribers</th>
46 <th scope="col">Created</th>
47 <th scope="col">Lease Time Preferred</th>
48 <th scope="col">Lease Time Minimum</th>
49 <th scope="col">Lease Time Maximum</th>
50 <th scope="col">Publisher Validation URL</th>
51 <th scope="col">Active</th>
52 <th scope="col">Deleted</th>
53 <th scope="col">Last Publish Notification</th>
54 <th scope="col">Next Content Fetch</th>
55 <th scope="col">Content Fetch Failures</th>
56 <th scope="col">Content Updated</th>
57 <th scope="col">Content Type</th>
58 <th scope="col">Internal Id</th>
59 </tr>`;
60 }
61
62
63 /**
64 * Render a subscription as a row of details.
65 * @param {Object} subscription
66 * @returns {String}
67 */
68 function renderSubscriptionRow(subscription) {
69 if (!subscription) {
70 return `<tr>
71 <th colspan="12">(topic not found)</th>
72 </tr>`;
73 }
74 return `<tr>
75 <td scope="row">${subscription.callback}</td>
76 <td>${TemplateHelper.dateFormat(subscription.created, 'End of Time', 'Beginning of Time', 'Unknown')}</td>
77 <td>${TemplateHelper.dateFormat(subscription.verified, 'End of Time', 'Never', 'Never')}</td>
78 <td>${TemplateHelper.dateFormat(subscription.expires, 'Never', 'Beginning of Time', 'Never')}</td>
79 <td>${!!subscription.secret}</td>
80 <td>${subscription.signatureAlgorithm}</td>
81 <td>${subscription.httpRemoteAddr}</td>
82 <td>${subscription.httpFrom}</td>
83 <td>${TemplateHelper.dateFormat(subscription.contentDelivered, 'End of Time', 'Never', 'Never')}</td>
84 <td>${subscription.deliveryAttemptsSinceSuccess}</td>
85 <td>${TemplateHelper.dateFormat(subscription.deliveryNextAttempt, 'End of Time', 'Next Publish', 'Next Publish')}</td>
86 <td>${subscription.id}</td>
87 </tr>`;
88 }
89
90
91 /**
92 * Render a row of headers for subscription details.
93 * @returns {String}
94 */
95 function renderSubscriptionRowHeader() {
96 return `<tr>
97 <th scope="col">Callback URL</th>
98 <th scope="col">Created</th>
99 <th scope="col">Verified</th>
100 <th scope="col">Expires</th>
101 <th scope="col">Using Secret</th>
102 <th scope="col">Signature Type</th>
103 <th scope="col">Remote Address</th>
104 <th scope="col">From</th>
105 <th scope="col">Content Delivered</th>
106 <th scope="col">Content Delivery Failures</th>
107 <th scope="col">Next Delivery</th>
108 <th scope="col">Internal Id</th>
109 </tr>
110 `;
111 }
112
113
114 /**
115 * Escape some xml things in strings.
116 * @param {String} string
117 */
118 function xmlEscape(string) {
119 if (typeof string === 'number') {
120 return string;
121 }
122 if (typeof string !== 'string') {
123 return undefined;
124 }
125 // eslint-disable-next-line security/detect-object-injection
126 return string.replace(/[<>&'"]/, (c) => ({
127 '<': '&lt;',
128 '>': '&gt;',
129 '&': '&amp;',
130 '\'': '&apos;',
131 '"': '&quot;',
132 }[c]));
133 }
134
135 module.exports = Object.assign(Object.create(TemplateHelper), {
136 xmlEscape,
137 renderTopicRowHeader,
138 renderTopicRow,
139 renderSubscriptionRowHeader,
140 renderSubscriptionRow,
141 });