...
 
Commits (5)
......@@ -35,7 +35,7 @@ module.exports = {
notification: "Someone has registered!",
// The subject line for acknowledgements
acknowledgement: "Thanks for registering!",
}
},
templates: {
// Note: recommended place for templates is in src/email-templates/.
// notification: 'src/email-templates/notification-template.hbs',
......@@ -44,6 +44,16 @@ module.exports = {
// Also, request under 'request'
},
},
gitlab: {
server: {
issuesUrl: 'https://example.com/api/v4/projects/9/issues',
token: 'sekret',
},
registration: {
title: 'Registration for {{oc-user}}',
description: './src/templates/issue.hbs',
},
},
webhook: {
path: '/example-path',
secret: 'secret!',
......
'use strict';
const axios = require('axios');
const submitlog = require('./logging.js').loggers.submit;
const config = require('./config.js').gitlab;
const templates = require('./templates.js');
if (!config) {
// We can't do anything
module.exports = null;
return;
}
const {server, registration} = config;
if (!server.issuesUrl)
throw new Error("there must be a gitlab.server.issuesUrl value configured");
if (!server.token)
throw new Error("there must be a gitlab.server.token value configured");
module.exports = {
newRegistrationIssue: function(context) {
const {request, config} = context;
const body = request.body;
function onsuccess(response) {
const id = response.data.iid;
submitlog.info(`added a new ticket ${id} for registration by ${body['oc-user']}`);
}
function onfailure(error) {
submitlog.info(`failed to add a new ticket for registration by ${body['oc-user']}. `, error);
}
return axios.post(
server.issuesUrl,
{
title: `Registration for ${body['oc-user']}`,
confidential: true,
description: templates.issue(context),
},
{ headers: {'PRIVATE-TOKEN': server.token} },
).then(onsuccess, onfailure);
},
};
......@@ -42,6 +42,24 @@ module.exports = {
return new Handlebars.SafeString(text);
},
/** Markdown quote text */
mdquote: function() {
var [opts, text] = getopts(arguments);
text = (text || '').replace(/\r/mg, '').replace(/^/mg, '> ');
return new Handlebars.SafeString(text);
},
/** Json output */
json: function() {
var [opts, data] = getopts(arguments);
console.log("hash", opts.hash);
data = JSON.stringify(
data, null,
opts.hash.pretty? Number(opts.hash.indent || 2) : null
);
return new Handlebars.SafeString(data);
},
// Ghost emulations
asset: function(path) {
return '/'+path;
......
......@@ -42,11 +42,14 @@ function defineTemplateLoader(file, name, defaultTemplate) {
// one required field in submitted data is an 'email' field with a
// recipient address. All fields, plus parts of the config, are
// supplied to the templates.
var notificationTemplate, acknowledgementTemplate;
var notificationTemplate, acknowledgementTemplate, issueTemplate;
if (config.email && config.email.templates) {
notificationTemplate = config.email.templates.notification;
acknowledgementTemplate = config.email.templates.acknowledgement;
}
if (config.gitlab && config.gitlab.registration && config.gitlab.registration) {
issueTemplate = config.gitlab.registration.description;
}
module.exports = {
notification: defineTemplateLoader(
......@@ -60,4 +63,10 @@ module.exports = {
'email acknowledgement template',
(context) => 'Thanks! We shall be in touch.'
),
issue: defineTemplateLoader(
issueTemplate,
'registration issue template',
(context) =>
'Form Data:\n\n```'+JSON.stringify(context.request.body, null, 2)+'```'
),
};
......@@ -134,7 +134,7 @@ module.exports = () => {
.ignore([
'**/*~', // Emacs backup files
'.*', // dotfiles
'email-templates', // email templates directory
'templates', // templates directory
])
.clean(true) // clean destination before building
.use(metalsmithDebug())
......
......@@ -9,6 +9,7 @@
"author": "wu-lee <wu_lee@social.coop>",
"license": "AGPL-3.0-only",
"dependencies": {
"axios": "^0.18.0",
"body-parser": "^1.18.3",
"emailjs": "^2.2.0",
"express": "^4.15.2",
......
'use strict';
const express = require('express');
const fs = require('fs');
const emailTemplates = require('./lib/email-templates.js');
const templates = require('./lib/templates.js');
const metalsmith = require('./metalsmith.js');
const email = require('./lib/email.js');
const gitlab = require('./lib/gitlab.js');
const git = require('./lib/git.js')({});
const loggers = require('./lib/logging.js').loggers;
const config = require('./lib/config.js');
......@@ -115,30 +116,40 @@ else{
});
}
app.post('/registration', (req, res) => {
const templateContext = {
config: {theme: config.theme},
request: req,
};
function ok() {}
function error(err) {
submitlog.info("error emailing registration notification, ",
err);
function error() {
return (err) => submitlog.info(`$message: `, err);
}
if (gitlab) {
try {
gitlab.newRegistrationIssue(templateContext)
.then(ok, error("error posting new gitlab issue"));
}
catch(e) {
submitlog.info('error posting new gitlab issue: ', e);
}
}
// Send notification of registration to admins
const sendConfig = config.email.registration || {};
const templateContext = {
config: {theme: config.theme},
request: req
};
submitlog.info(`emailing registration notification to ${sendConfig.to}`, req.body);
const notificationConfig = {
from: sendConfig.from,
to: req.body.email,
to: sendConfig.to,
subject: sendConfig.notification,
};
try {
const html = emailTemplates.notification(templateContext);
const html = templates.notification(templateContext);
send(notificationConfig,
html,
req.body)
.then(ok, error);
.then(ok, error("error emailing registration notification"));
}
catch(e) {
maillog.error(`failed to send notification of registration to ${notificationConfig.to}: `, e);
......@@ -152,11 +163,11 @@ app.post('/registration', (req, res) => {
subject: sendConfig.acknowledgement,
};
try {
const html = emailTemplates.acknowledgement(templateContext);
const html = templates.acknowledgement(templateContext);
submitlog.info(`emailing registration acknowledgement to ${acknowledgementConfig.to}: `);
send(acknowledgementConfig,
html)
.then(ok, error);
.then(ok, error("error emailing registration acknowledgement"));
}
catch(e) {
maillog.error(`failed to send registration acknowledgement to ${acknowledgementConfig.to} `, e);
......