|
|
@@ -2,10 +2,11 @@
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
-var MongoClient = require('mongodb').MongoClient,
|
|
|
- ObjectId = require('mongodb').ObjectID,
|
|
|
+var fs = require('fs'),
|
|
|
async = require('async'),
|
|
|
- superagent = require('superagent'),
|
|
|
+ things = require('./things.js'),
|
|
|
+ tags = require('./tags.js'),
|
|
|
+ settings = require('./settings.js'),
|
|
|
HttpError = require('connect-lastmile').HttpError,
|
|
|
HttpSuccess = require('connect-lastmile').HttpSuccess;
|
|
|
|
|
|
@@ -18,69 +19,16 @@ exports = module.exports = {
|
|
|
del: del,
|
|
|
getTags: getTags,
|
|
|
settingsSave: settingsSave,
|
|
|
- settingsGet: settingsGet
|
|
|
+ settingsGet: settingsGet,
|
|
|
+ exportThings: exportThings
|
|
|
};
|
|
|
|
|
|
-var databaseUrl = process.env.MONGODB_URL || 'mongodb://127.0.0.1:27017/guacamoly';
|
|
|
-var g_db, g_things, g_tags, g_settings;
|
|
|
-
|
|
|
-function sanitize(data) {
|
|
|
- data = data.replace(/##/g, data);
|
|
|
- return data;
|
|
|
-}
|
|
|
-
|
|
|
-function facelift(data, tags, callback) {
|
|
|
- var geturl = new RegExp('(^|[ \t\r\n])((ftp|http|https|gopher|mailto|news|nntp|telnet|wais|file|prospero|aim|webcal):(([A-Za-z0-9$_.+!*(),;/?:@&~=-])|%[A-Fa-f0-9]{2}){2,}(#([a-zA-Z0-9][a-zA-Z0-9$_.+!*(),;/?:@&~=%-]*))?([A-Za-z0-9$_+!*();/?:~-]))', 'g');
|
|
|
- var urls = data.match(geturl);
|
|
|
-
|
|
|
- async.each(urls, function (url, callback) {
|
|
|
- superagent.get(url).end(function (error, result) {
|
|
|
- if (error) return callback(null);
|
|
|
-
|
|
|
- if (result.type === 'image/png') {
|
|
|
- data = data.replace(url, '');
|
|
|
- }
|
|
|
-
|
|
|
- callback(null);
|
|
|
- });
|
|
|
- }, function () {
|
|
|
- tags.forEach(function (tag) {
|
|
|
- data = data.replace(new RegExp('#' + tag, 'gmi'), '[#' + tag + '](#' + tag + ')');
|
|
|
- });
|
|
|
-
|
|
|
- callback(data);
|
|
|
- });
|
|
|
-}
|
|
|
-
|
|
|
-function extractTags(data) {
|
|
|
- var lines = data.split('\n');
|
|
|
- var tags = [];
|
|
|
-
|
|
|
- lines.forEach(function (line) {
|
|
|
- var tmp = line.match(/\B#(\w+)/g);
|
|
|
- if (tmp === null) return;
|
|
|
-
|
|
|
- tags = tmp.map(function (tag) {
|
|
|
- return tag.slice(1).toLowerCase();
|
|
|
- }).concat(tags);
|
|
|
- });
|
|
|
-
|
|
|
- return tags;
|
|
|
-}
|
|
|
-
|
|
|
function init(callback) {
|
|
|
- MongoClient.connect(databaseUrl, function (error, db) {
|
|
|
- if (error) return callback(error);
|
|
|
-
|
|
|
- g_db = db;
|
|
|
- g_things = db.collection('things');
|
|
|
- g_tags = db.collection('tags');
|
|
|
- g_settings = db.collection('settings');
|
|
|
-
|
|
|
- g_things.createIndex({ content: 'text' });
|
|
|
-
|
|
|
- callback(null);
|
|
|
- });
|
|
|
+ async.series([
|
|
|
+ things.init,
|
|
|
+ tags.init,
|
|
|
+ settings.init
|
|
|
+ ], callback);
|
|
|
}
|
|
|
|
|
|
function getAll(req, res, next) {
|
|
|
@@ -92,112 +40,71 @@ function getAll(req, res, next) {
|
|
|
};
|
|
|
}
|
|
|
|
|
|
- g_things.find(query).sort({ modifiedAt: -1 }).toArray(function (error, result) {
|
|
|
- if (error || !result) return next(new HttpError(500, error));
|
|
|
-
|
|
|
- async.each(result, function (thing, callback) {
|
|
|
- var tags = extractTags(thing.content);
|
|
|
- facelift(thing.content, tags, function (data) {
|
|
|
- thing.richContent = data;
|
|
|
-
|
|
|
- callback(null);
|
|
|
- });
|
|
|
- }, function () {
|
|
|
- next(new HttpSuccess(200, { things: result }));
|
|
|
- });
|
|
|
+ things.getAll(query, function (error, result) {
|
|
|
+ if (error) return next(new HttpError(500, error));
|
|
|
+ next(new HttpSuccess(200, { things: result }));
|
|
|
});
|
|
|
}
|
|
|
|
|
|
function get(req, res, next) {
|
|
|
- g_things.find({ _id: req.params.id }).toArray(function (error, result) {
|
|
|
- if (error || !result) return next(new HttpError(500, error));
|
|
|
-
|
|
|
+ things.get(req.params.id, function (error, result) {
|
|
|
+ if (error) return next(new HttpError(500, error));
|
|
|
next(new HttpSuccess(200, { thing: result }));
|
|
|
});
|
|
|
}
|
|
|
|
|
|
function add(req, res, next) {
|
|
|
- console.log('add', req.body);
|
|
|
-
|
|
|
- var tags = extractTags(req.body.content);
|
|
|
- var data = sanitize(req.body.content);
|
|
|
-
|
|
|
- var doc = {
|
|
|
- content: data,
|
|
|
- createdAt: new Date(),
|
|
|
- modifiedAt: new Date(),
|
|
|
- tags: tags
|
|
|
- };
|
|
|
-
|
|
|
- async.eachSeries(tags, function (tag, callback) {
|
|
|
- g_tags.update({ name: tag }, {
|
|
|
- $inc: { usage: 1 },
|
|
|
- $set: {
|
|
|
- name: tag
|
|
|
- }
|
|
|
- }, { upsert:true }, callback);
|
|
|
- }, function (error) {
|
|
|
- if (error) return next(new HttpError(500, error));
|
|
|
+ if (typeof req.body.content !== 'string' || !req.body.content) return next(new HttpError(400, 'content must be a string'));
|
|
|
|
|
|
- g_things.insert(doc, function (error, result) {
|
|
|
- if (error || !result) return next(new HttpError(500, error));
|
|
|
-
|
|
|
- next(new HttpSuccess(201, { id: result._id }));
|
|
|
- });
|
|
|
+ things.add(req.body.content, function (error, id) {
|
|
|
+ if (error) return next(new HttpError(500, error));
|
|
|
+ next(new HttpSuccess(201, { id: id }));
|
|
|
});
|
|
|
}
|
|
|
|
|
|
function put(req, res, next) {
|
|
|
- console.log('put', req.body);
|
|
|
-
|
|
|
- var tags = extractTags(req.body.content);
|
|
|
- var data = sanitize(req.body.content);
|
|
|
-
|
|
|
- async.eachSeries(tags, function (tag, callback) {
|
|
|
- g_tags.update({ name: tag }, {
|
|
|
- $inc: { usage: 1 },
|
|
|
- $set: {
|
|
|
- name: tag
|
|
|
- }
|
|
|
- }, { upsert:true }, callback);
|
|
|
- }, function (error) {
|
|
|
+ things.put(req.params.id, req.body.content, function (error) {
|
|
|
if (error) return next(new HttpError(500, error));
|
|
|
-
|
|
|
- g_things.update({_id: new ObjectId(req.params.id) }, { $set: { content: data, tags: tags, modifiedAt: new Date() } }, function (error, result) {
|
|
|
- if (error || !result) return next(new HttpError(500, error));
|
|
|
-
|
|
|
- next(new HttpSuccess(201, { id: result._id }));
|
|
|
- });
|
|
|
+ next(new HttpSuccess(201, { id: req.params.id }));
|
|
|
});
|
|
|
}
|
|
|
|
|
|
function del(req, res, next) {
|
|
|
- g_things.deleteOne({ _id: new ObjectId(req.params.id) }, function (error, result) {
|
|
|
- if (error || !result) return next(new HttpError(500, error));
|
|
|
-
|
|
|
+ things.del(req.params.id, function (error) {
|
|
|
+ if (error) return next(new HttpError(500, error));
|
|
|
next(new HttpSuccess(200, {}));
|
|
|
});
|
|
|
}
|
|
|
|
|
|
function getTags(req, res, next) {
|
|
|
- g_tags.find({}).sort({ createdAt: -1 }).toArray(function (error, result) {
|
|
|
- if (error || !result) return next(new HttpError(500, error));
|
|
|
-
|
|
|
+ tags.get(function (error, result) {
|
|
|
+ if (error) return next(new HttpError(500, error));
|
|
|
next(new HttpSuccess(200, { tags: result }));
|
|
|
});
|
|
|
}
|
|
|
|
|
|
function settingsSave(req, res, next) {
|
|
|
- g_settings.update({ type: 'frontend' }, { type: 'frontend', value: req.body.settings }, { upsert: true }, function (error) {
|
|
|
+ settings.put(req.body.settings, function (error) {
|
|
|
if (error) return next(new HttpError(500, error));
|
|
|
next(new HttpSuccess(202, {}));
|
|
|
});
|
|
|
}
|
|
|
|
|
|
function settingsGet(req, res, next) {
|
|
|
- g_settings.find({ type: 'frontend' }).toArray(function (error, result) {
|
|
|
- console.log(error, result);
|
|
|
+ settings.get(function (error, result) {
|
|
|
if (error) return next(new HttpError(500, error));
|
|
|
- next(new HttpSuccess(200, { settings: result[0].value || {} }));
|
|
|
+ next(new HttpSuccess(200, { settings: result }));
|
|
|
+ });
|
|
|
+}
|
|
|
+
|
|
|
+function exportThings(req, res, next) {
|
|
|
+ things.exp(function (error, fileName) {
|
|
|
+ if (error) return next(new HttpError(500, error));
|
|
|
+
|
|
|
+ res.download(fileName, 'things.json', function (error) {
|
|
|
+ if (error) console.error(error);
|
|
|
+
|
|
|
+ fs.unlink(fileName);
|
|
|
+ });
|
|
|
});
|
|
|
}
|