update dependencies, fixes to support new authentication features
[websub-hub] / src / template / template-helper.js
index 3fd371035d0ab4989bcfbf771839f5d6431a5f11..5850fc1256604c955fdf973931b0e93ed97d2734 100644 (file)
@@ -1,13 +1,14 @@
 'use strict';
 
 const { TemplateHelper } = require('@squeep/html-template-helper');
+const { Message } = require('../enum');
 
 /**
  * Render a topic as a row of details.
- * @param {Object} topic
- * @param {Object[]} subscribers
- * @param {Boolean} detailsLink
- * @returns {String}
+ * @param {object} topic topic
+ * @param {object[]} subscribers subscribers
+ * @param {boolean} detailsLink link to details
+ * @returns {string} html
  */
 function renderTopicRow(topic, subscribers, detailsLink = true) {
   if (!topic) {
@@ -18,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.dateFormat(topic.created, 'End of Time', 'Beginning of Time', '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.dateFormat(topic.lastPublish, 'End of Time', 'Never', 'Never')}</td>
-  <td>${TemplateHelper.dateFormat(topic.contentFetchNextAttempt, 'Next Publish', 'Pending', '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.dateFormat(topic.contentUpdated, 'End of Time', 'Never', 'Never')}</td>
+  <td>${TemplateHelper.dateFormat(topic.contentUpdated, Message.EndOfTime, Message.Never, Message.Never)}</td>
   <td>${topic.contentType}</td>
   <td>${topic.id}</td>
 </tr>`;
@@ -37,7 +38,7 @@ function renderTopicRow(topic, subscribers, detailsLink = true) {
 
 /**
  * Render the header row for topic details.
- * @returns {String}
+ * @returns {string} html
  */
 function renderTopicRowHeader() {
   return `<tr>
@@ -62,8 +63,8 @@ function renderTopicRowHeader() {
 
 /**
  * Render a subscription as a row of details.
- * @param {Object} subscription
- * @returns {String}
+ * @param {object} subscription subscription
+ * @returns {string} html
  */
 function renderSubscriptionRow(subscription) {
   if (!subscription) {
@@ -73,16 +74,16 @@ function renderSubscriptionRow(subscription) {
   }
   return `<tr>
   <td scope="row">${subscription.callback}</td>
-  <td>${TemplateHelper.dateFormat(subscription.created, 'End of Time', 'Beginning of Time', 'Unknown')}</td>
-  <td>${TemplateHelper.dateFormat(subscription.verified, 'End of Time', 'Never', 'Never')}</td>
-  <td>${TemplateHelper.dateFormat(subscription.expires, 'Never', 'Beginning of Time', '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.dateFormat(subscription.contentDelivered, 'End of Time', 'Never', 'Never')}</td>
+  <td>${TemplateHelper.dateFormat(subscription.contentDelivered, Message.EndOfTime, Message.Never, Message.Never)}</td>
   <td>${subscription.deliveryAttemptsSinceSuccess}</td>
-  <td>${TemplateHelper.dateFormat(subscription.deliveryNextAttempt, 'End of Time', 'Next Publish', 'Next Publish')}</td>
+  <td>${TemplateHelper.dateFormat(subscription.deliveryNextAttempt, Message.EndOfTime, Message.NextPublish, Message.NextPublish)}</td>
   <td>${subscription.id}</td>
 </tr>`;
 }
@@ -90,7 +91,7 @@ function renderSubscriptionRow(subscription) {
 
 /**
  * Render a row of headers for subscription details.
- * @returns {String}
+ * @returns {string} html
  */
 function renderSubscriptionRowHeader() {
   return `<tr>
@@ -113,7 +114,8 @@ function renderSubscriptionRowHeader() {
 
 /**
  * Escape some xml things in strings.
- * @param {String} string
+ * @param {string} string string to escape
+ * @returns {string} escaped
  */
 function xmlEscape(string) {
   if (typeof string === 'number') {
@@ -132,10 +134,33 @@ function xmlEscape(string) {
   }[c]));
 }
 
+
+/**
+ * Add common site links to navigation header.
+ * @param {number} pagePathLevel depth from root
+ * @param {object} ctx context
+ * @param {object} options options
+ */
+function navLinks(pagePathLevel, ctx, options) {
+  if (!options.navLinks) {
+    options.navLinks = [];
+  }
+  const rootPath = '../'.repeat(pagePathLevel);
+
+  if (options.pageIdentifier !== 'admin') {
+    options.navLinks.push({
+      text: 'Admin',
+      href: `${rootPath}admin/`,
+    });
+  }
+}
+
+
 module.exports = Object.assign(Object.create(TemplateHelper), {
+  navLinks,
   xmlEscape,
   renderTopicRowHeader,
   renderTopicRow,
   renderSubscriptionRowHeader,
   renderSubscriptionRow,
-});
\ No newline at end of file
+});