-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapi.js
More file actions
141 lines (122 loc) · 3.09 KB
/
api.js
File metadata and controls
141 lines (122 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
//
// # API
//
// The exposed route handlers.
//
/* jslint node: true */
'use strict';
var API = module.exports = {};
//var fs = require('fs');
var path = require('path');
var jstream = require('JSONStream');
var Comments = require('./lib/commentstream');
var sanitizeHtml = require('sanitize-html');
var uglify = require('uglifyjs');
//
// ## JavaScript
//
// Serves JavaScript resources.
//
// @TODO: Add versioning
//
API.js = function js(request, reply) {
var assetPath = path.join(__dirname, 'assets', 'js');
var files = [
'client.js'
].map(function (item) {
return path.join(assetPath, item);
});
var minified = uglify.minify(files);
reply(minified.code)
.type('text/javascript');
return;
/*
var data = [];
function loadFile(file, cb) {
if (!file) {
cb(null, data);
return;
}
fs.readFile(file, 'utf8', function (err, txt) {
if (err) {
cb(err);
return;
}
data.push(txt);
loadFile(files.shift(), cb);
});
}
loadFile(files.shift(), function (err, data) {
reply(data.join('\n'))
.type('text/javascript');
});
//*/
};
//
// ## Post Comment
//
// Saves a comment to the database.
//
API.post = function postComment(request, reply) {
var data = request.payload;
data.timestamp = Date.now();
var namespace = data.resource;
delete(data.namespace);
var keys = ['username', 'email', 'comment', 'resource', 'url'];
keys.forEach(function (key) {
if (typeof data[key] !== 'string') {
return;
}
data[key] = sanitizeHtml(data[key]);
});
storeComment(data);
function storeComment(data) {
var key = [namespace, data.timestamp].join('~');
var db = request.server.settings.app.db();
db.put(key, data, {}, function (err) {
var response = reply({
success: !!!err,
message: err ? err.message : 'comment saved'
});
response.code(!!!err ? 200 : 500);
if (response.statusCode === 200) {
if (request.server.plugins.mail) {
request.server.plugins.mail.send(
'New comment!',
[
'A new comment has been posted on your blog!',
'The comment was posted by: ' + data.username,
'The users email is: ' + data.email,
'Contents of the comment were: ' + data.comment,
'The message can be viewd at: ' + data.url
].join('\n')
);
}
}
});
}
};
//
// ## Get Comments
//
// Get comments for the specified resource.
//
// @TODO: Might need pagination.
//
API.get = function getComments(request, reply) {
var key = request.params.resource;
var db = request.server.settings.app.db();
var stream = db.createReadStream({
start: key,
end: key + '\xFF'
});
var responseStream = jstream.stringify();
// @FIXME: This is a work around for a bug (?) in Hapi.
// https://github.com/hapijs/hapi/issues/2368
// Or in JSONStream...
responseStream._readableState = {};
var commentStream = new Comments();
stream.pipe(commentStream).pipe(responseStream);
reply(responseStream)
.type('application/json');
};