clean up lint issues
[websub-hub] / src / template / template-helper.js
index bb4dad1e3f574f2332683c9e4bc990c9bc98b3a2..23b0f12b37f84c6700fe9edd9f9b5851c014a225 100644 (file)
@@ -1,7 +1,7 @@
 'use strict';
 
 const { TemplateHelper } = require('@squeep/html-template-helper');
-
+const { Message } = require('../enum');
 
 /**
  * Render a topic as a row of details.
@@ -19,17 +19,17 @@ function renderTopicRow(topic, subscribers, detailsLink = true) {
   return `<tr>
   <th scope="row">${detailsLink ? '<a href="topic/' + topic.id + '">' : ''}${topic.url}${detailsLink ? '</a>' : ''}</th>
   <td>${subscribers.length}</td>
-  <td>${TemplateHelper.dateOrNot(topic.created, 'Unknown')}</td>
+  <td>${TemplateHelper.dateFormat(topic.created, Message.EndOfTime, Message.BeginningOfTime, Message.Unknown)}</td>
   <td>${TemplateHelper.secondsToPeriod(topic.leaseSecondsPreferred)}</td>
   <td>${TemplateHelper.secondsToPeriod(topic.leaseSecondsMin)}</td>
   <td>${TemplateHelper.secondsToPeriod(topic.leaseSecondsMax)}</td>
   <td>${topic.publisherValidationUrl ? topic.publisherValidationUrl : 'None'}</td>
   <td>${topic.isActive}</td>
   <td>${topic.isDeleted}</td>
-  <td>${TemplateHelper.dateOrNot(topic.lastPublish, 'Never')}</td>
-  <td>${TemplateHelper.dateOrNot(topic.contentFetchNextAttempt, 'Next Publish')}</td>
+  <td>${TemplateHelper.dateFormat(topic.lastPublish, Message.EndOfTime, Message.Never, Message.Never)}</td>
+  <td>${TemplateHelper.dateFormat(topic.contentFetchNextAttempt, Message.NextPublish, Message.Pending, Message.NextPublish)}</td>
   <td>${topic.contentFetchAttemptsSinceSuccess}</td>
-  <td>${TemplateHelper.dateOrNot(topic.contentUpdated, 'Never')}</td>
+  <td>${TemplateHelper.dateFormat(topic.contentUpdated, Message.EndOfTime, Message.Never, Message.Never)}</td>
   <td>${topic.contentType}</td>
   <td>${topic.id}</td>
 </tr>`;
@@ -56,7 +56,7 @@ function renderTopicRowHeader() {
   <th scope="col">Content Fetch Failures</th>
   <th scope="col">Content Updated</th>
   <th scope="col">Content Type</th>
-  <th scope="col">ID</th>
+  <th scope="col">Internal Id</th>
 </tr>`;
 }
 
@@ -74,16 +74,16 @@ function renderSubscriptionRow(subscription) {
   }
   return `<tr>
   <td scope="row">${subscription.callback}</td>
-  <td>${TemplateHelper.dateOrNot(subscription.created, 'Unknown')}</td>
-  <td>${TemplateHelper.dateOrNot(subscription.verified, 'Never')}</td>
-  <td>${TemplateHelper.dateOrNot(subscription.expires, 'Never')}</td>
+  <td>${TemplateHelper.dateFormat(subscription.created, Message.EndOfTime, Message.BeginningOfTime, Message.Unknown)}</td>
+  <td>${TemplateHelper.dateFormat(subscription.verified, Message.EndOfTime, Message.Never, Message.Never)}</td>
+  <td>${TemplateHelper.dateFormat(subscription.expires, Message.Never, Message.BeginningOfTime, Message.Never)}</td>
   <td>${!!subscription.secret}</td>
   <td>${subscription.signatureAlgorithm}</td>
   <td>${subscription.httpRemoteAddr}</td>
   <td>${subscription.httpFrom}</td>
-  <td>${TemplateHelper.dateOrNot(subscription.contentDelivered, 'Never')}</td>
+  <td>${TemplateHelper.dateFormat(subscription.contentDelivered, Message.EndOfTime, Message.Never, Message.Never)}</td>
   <td>${subscription.deliveryAttemptsSinceSuccess}</td>
-  <td>${TemplateHelper.dateOrNot(subscription.deliveryNextAttempt, 'Next Publish')}</td>
+  <td>${TemplateHelper.dateFormat(subscription.deliveryNextAttempt, Message.EndOfTime, Message.NextPublish, Message.NextPublish)}</td>
   <td>${subscription.id}</td>
 </tr>`;
 }
@@ -106,13 +106,35 @@ function renderSubscriptionRowHeader() {
   <th scope="col">Content Delivered</th>
   <th scope="col">Content Delivery Failures</th>
   <th scope="col">Next Delivery</th>
-  <th scope="col">ID</th>
+  <th scope="col">Internal Id</th>
 </tr>
 `;
 }
 
 
+/**
+ * Escape some xml things in strings.
+ * @param {String} string
+ */
+function xmlEscape(string) {
+  if (typeof string === 'number') {
+    return string;
+  }
+  if (typeof string !== 'string') {
+    return undefined;
+  }
+  // eslint-disable-next-line security/detect-object-injection
+  return string.replace(/[<>&'"]/, (c) => ({
+    '<': '&lt;',
+    '>': '&gt;',
+    '&': '&amp;',
+    '\'': '&apos;',
+    '"': '&quot;',
+  }[c]));
+}
+
 module.exports = Object.assign(Object.create(TemplateHelper), {
+  xmlEscape,
   renderTopicRowHeader,
   renderTopicRow,
   renderSubscriptionRowHeader,