Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
index.js 1.74 KiB
"use strict";

// Dependencies
var express    = require( 'express' );
var bodyParser = require( 'body-parser' );
var WPAPI      = require( 'wpapi' );
var striptags  = require( 'striptags' );

var app = express();
app.use( bodyParser.json() );
app.use( bodyParser.urlencoded( { extended: true } ) );

// Config
var config = require( './config.json' );

var wp = new WPAPI( {
	endpoint: config.wp_endpoint,
	username: config.wp_username,
	password: config.wp_password
} );

// Landing page for status checks
app.get( '/', function( req, res ) {
	res.sendStatus( 200 );
} );

// Parse POST body to create WP post
app.post( '/log', function( req, res ) {
	// Don't bother if there's nothing to record
	if ( 'undefined' === typeof req.body || 'undefined' === typeof req.body.text || ! req.body.text.length ) {
		return res.sendStatus( 400 );
	}

	// Massage the title
	var title = req.body.text;
	title = striptags( title, [] );

	if ( title.length > 30 ) {
		title = 'Did: ' + title.slice( 0, 29 );
		title += '…';
	} else {
		title = 'Did: ' + title;
	}

	// Decorate the content
	var content = req.body.text;
	content = striptags( content, [ 'a', 'code', 'strong', 'em' ] );
	content = '<blockquote>' + content + "</blockquote>\n\n#slash-done";

	// Create the post
	wp.posts().create( {
		title:   title,
		content: content,
		status:  'publish'
	} ).then( function( resp ) {
		var response = {
			"response_type": "ephemeral",
			"text": "**Entry recorded (**ID #" + resp.id + ", <" + resp.link + ">**)**\n" + resp.title.rendered
		};

		res.send( response);
	} );
} );

//tells Node which port to listen on
app.listen( config.node_port, config.node_listener, function() {
	console.log( 'slash-done running on', config.node_listener, 'and port', config.node_port );
} );