X-Git-Url: https://git.squeep.com/?a=blobdiff_plain;f=src%2Ftemplate%2Ftemplate-helper.js;h=2da05dfb928007fa1f080bb35c7ab4770262d9f2;hb=refs%2Ftags%2Fv1.3.12;hp=bb4dad1e3f574f2332683c9e4bc990c9bc98b3a2;hpb=3c547e314b79a31fb3f15412a47707a22dc3eefd;p=websub-hub
diff --git a/src/template/template-helper.js b/src/template/template-helper.js
index bb4dad1..2da05df 100644
--- a/src/template/template-helper.js
+++ b/src/template/template-helper.js
@@ -1,14 +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) {
@@ -19,17 +19,17 @@ function renderTopicRow(topic, subscribers, detailsLink = true) {
return `
${detailsLink ? '' : ''}${topic.url}${detailsLink ? '' : ''} |
${subscribers.length} |
- ${TemplateHelper.dateOrNot(topic.created, 'Unknown')} |
+ ${TemplateHelper.dateFormat(topic.created, Message.EndOfTime, Message.BeginningOfTime, Message.Unknown)} |
${TemplateHelper.secondsToPeriod(topic.leaseSecondsPreferred)} |
${TemplateHelper.secondsToPeriod(topic.leaseSecondsMin)} |
${TemplateHelper.secondsToPeriod(topic.leaseSecondsMax)} |
${topic.publisherValidationUrl ? topic.publisherValidationUrl : 'None'} |
${topic.isActive} |
${topic.isDeleted} |
- ${TemplateHelper.dateOrNot(topic.lastPublish, 'Never')} |
- ${TemplateHelper.dateOrNot(topic.contentFetchNextAttempt, 'Next Publish')} |
+ ${TemplateHelper.dateFormat(topic.lastPublish, Message.EndOfTime, Message.Never, Message.Never)} |
+ ${TemplateHelper.dateFormat(topic.contentFetchNextAttempt, Message.NextPublish, Message.Pending, Message.NextPublish)} |
${topic.contentFetchAttemptsSinceSuccess} |
- ${TemplateHelper.dateOrNot(topic.contentUpdated, 'Never')} |
+ ${TemplateHelper.dateFormat(topic.contentUpdated, Message.EndOfTime, Message.Never, Message.Never)} |
${topic.contentType} |
${topic.id} |
`;
@@ -38,7 +38,7 @@ function renderTopicRow(topic, subscribers, detailsLink = true) {
/**
* Render the header row for topic details.
- * @returns {String}
+ * @returns {string} html
*/
function renderTopicRowHeader() {
return `
@@ -56,15 +56,15 @@ function renderTopicRowHeader() {
Content Fetch Failures |
Content Updated |
Content Type |
- ID |
+ Internal Id |
`;
}
/**
* 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,17 +73,17 @@ function renderSubscriptionRow(subscription) {
`;
}
return `
- ${subscription.callback} |
- ${TemplateHelper.dateOrNot(subscription.created, 'Unknown')} |
- ${TemplateHelper.dateOrNot(subscription.verified, 'Never')} |
- ${TemplateHelper.dateOrNot(subscription.expires, 'Never')} |
+ ${subscription.callback} |
+ ${TemplateHelper.dateFormat(subscription.created, Message.EndOfTime, Message.BeginningOfTime, Message.Unknown)} |
+ ${TemplateHelper.dateFormat(subscription.verified, Message.EndOfTime, Message.Never, Message.Never)} |
+ ${TemplateHelper.dateFormat(subscription.expires, Message.Never, Message.BeginningOfTime, Message.Never)} |
${!!subscription.secret} |
${subscription.signatureAlgorithm} |
${subscription.httpRemoteAddr} |
${subscription.httpFrom} |
- ${TemplateHelper.dateOrNot(subscription.contentDelivered, 'Never')} |
+ ${TemplateHelper.dateFormat(subscription.contentDelivered, Message.EndOfTime, Message.Never, Message.Never)} |
${subscription.deliveryAttemptsSinceSuccess} |
- ${TemplateHelper.dateOrNot(subscription.deliveryNextAttempt, 'Next Publish')} |
+ ${TemplateHelper.dateFormat(subscription.deliveryNextAttempt, Message.EndOfTime, Message.NextPublish, Message.NextPublish)} |
${subscription.id} |
`;
}
@@ -91,7 +91,7 @@ function renderSubscriptionRow(subscription) {
/**
* Render a row of headers for subscription details.
- * @returns {String}
+ * @returns {string} html
*/
function renderSubscriptionRowHeader() {
return `
@@ -106,15 +106,61 @@ function renderSubscriptionRowHeader() {
Content Delivered |
Content Delivery Failures |
Next Delivery |
- ID |
+ Internal Id |
`;
}
+/**
+ * Escape some xml things in strings.
+ * @param {string} string string to escape
+ * @returns {string} escaped
+ */
+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) => ({
+ '<': '<',
+ '>': '>',
+ '&': '&',
+ '\'': ''',
+ '"': '"',
+ }[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
+});