-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
289 lines (259 loc) · 7.39 KB
/
app.js
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
var fs = require('fs');
var express = require('express');
var logger = require('morgan');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var request = require('request');
var busboy = require('connect-busboy');
var elastic = require('elasticsearch');
var nodemailer = require('nodemailer');
var SERVER_PORT = 3000;
var ES_PORT = ':9200';
var HOST = 'localhost';
var IP = 'ustmarketplace.ddns.net';
var INDEX = 'ustmarketplace';
var TYPE = 'item';
var EMAIL_EXT = '@ust.hk';
var app = express();
var client = new elastic.Client({host: HOST + ES_PORT});
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: '[email protected]',
pass: '********'
}
});
app.use(logger('dev'));
app.set('views', __dirname + '/views/pages');
app.set('view engine', 'ejs');
app.engine('html', require('ejs').renderFile);
app.use(bodyParser.urlencoded({ extended: false}));
app.use( require('cookie-parser')() );
app.use(bodyParser.json());
app.use(cookieParser());
app.use(busboy());
app.set('port', (process.env.PORT || SERVER_PORT));
app.use(express.static(__dirname + '/public'));
/*******************************************************************************/
app.get('/', function(req, res) {
getAllItems(function(array) {
if(array == null)
res.render('error.ejs');
else {
res.render('index.ejs', {
items : array
});
}
});
});
app.get('/error', function(req, res) {
res.render('error.ejs');
});
app.get('/unauthorised', function(req, res) {
res.render('unauthorised.ejs');
});
app.get('/about', function(req, res) {
res.render('about.ejs');
});
app.get('/contact', function(req, res) {
res.render('contact.ejs');
});
app.post('/message', function(req, res) {
if(authorise(req.body.author_itsc)) {
getItemById(req.body.item_id, function(item) {
sendMessageEmail(req.body, item, function() {
res.json({data : 'ok'});
});
});
} else
res.json({data : 'unauthorised'});
});
app.post('/upload', function(req, res) {
var item_name,
item_description,
item_price,
item_image;
req.pipe(req.busboy);
req.busboy.on('field', function(fieldname, val, fieldnameTruncated, valTruncated) {
if(fieldname == 'item_name')
item_name = val;
if(fieldname == 'item_price')
item_price = val;
if(fieldname == 'item_description')
item_description = val;
if(fieldname == 'item_image')
item_image = val;
if(fieldname == 'item_owner') {
item_owner = val;
if(authorise(item_owner)) {
req.busboy.on('file', function (fieldname, file, filename) {
createItem({
name: item_name,
price: item_price,
description: item_description,
date: Math.floor(new Date().getTime() / 1000),
owner: item_owner,
active: 'true'
}, function(err, obj) {
if(err == null) {
fstream = fs.createWriteStream(__dirname + '/public/img/' + obj._id + '.jpg');
file.pipe(fstream);
fstream.on('close', function() {
getItemById(obj._id, function(obj) {
sendUploadEmail(obj, function() {
res.redirect('/');
});
});
});
} else
res.redirect('/error');
});
});
} else
res.redirect('/unauthorised');
}
});
});
app.get('/withdraw', function(req, res) {
client.update({
index: INDEX,
type: TYPE,
id: req.query._id,
body: {
doc: {
active: 'false'
}
}
}, function (error, response) {
if(error != null)
res.render('error.ejs');
else
res.render('withdraw.ejs');
})
});
/*******************************************************************************/
function createItem(obj, callback) {
client.create({
index: INDEX,
type: TYPE,
body: obj
}, function (err, res) {
callback(err, res);
});
};
function sendUploadEmail(item, callback) {
var mailOptions = {
from: 'UST marketplace <[email protected]>',
to: item._source.owner,
subject: '✔ You successfully posted an item!',
html: '<p>Hey, just letting you know that you successfully posted the following item:</p>' +
'<p>Item name: ' + item._source.name + '</p>' +
'<p>Item description:' + item._source.description + '</p>' +
'<p>Item price: ' + item._source.price + '</p>' +
'<br>' +
'<p><a href="http://' + IP + '/withdraw?_id=' + item._id + '">Withdraw item from marketplace</a></p>' +
'<br>'
};
transporter.sendMail(mailOptions, function(error, info) {
if(error)
console.log('An error occurred');
else
callback();
});
};
function sendMessageEmail(post, item, callback) {
var mailOptions = {
from: 'ustmarketplace <[email protected]>', // sender address
to: item._source.owner, // list of receivers
subject: '✔ Someone\'s interested in your post!', // Subject line
html: '<p>Hey, someone would like to know more about:</p>' +
'<p>Item name: ' + item._source.name + '</p>' +
'<p>Item description:' + item._source.description + '</p>' +
'<p>Item price: ' + item._source.price + '</p>' +
'<br>' +
'<p>' + post.author_itsc + ' writes:</p>' +
'<p>' + post.content + '</p>' +
'<br>' +
'<p>To reply, send an email to: <a href="mailto:' + post.author_itsc + '">' + post.author_itsc + '</a></p>' +
'<br>' +
'<p><a href="http://' + IP + '/withdraw?_id=' + item._id + '">Withdraw item from marketplace</a></p>' +
'<br>'
};
transporter.sendMail(mailOptions, function(error, info) {
if(error)
console.log('An error occurred');
else
callback();
});
};
function getAllItems(callback) {
var array = [];
var end = Math.floor(new Date().getTime() / 1000);
var start = end - 2592000;
var obj = {
index: INDEX,
body: {
from : 0,
size : 5000,
query: {
filtered: {
query: {
match_all: {}
},
filter: {
and: [
{
range : {
date : {
from : start,
to : end
}
},
},
{
term: {
active: 'true'
}
}
]
}
}
}
}
};
client.search(obj, function (err, res) {
if(err == null) {
for(var i = 0; i < res.hits.total; i++)
array.push(res.hits.hits[i]);
}
callback(array);
});
};
function getItemById(id, callback) {
request.get('http://localhost:9200/' + INDEX + '/' + TYPE + '/' + id, function(err, res, body) {
if(err != null)
console.log('an error occurred');
else
callback(JSON.parse(body));
});
};
function authorise(email) {
var s = 'ust.hk';
if(email.indexOf(s) > -1)
return true;
else
return false;
};
/*******************************************************************************/
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
app.use(function(err, req, res, next) {
res.status(err.status || 500);
console.log(err.message);
});
var server = app.listen(SERVER_PORT, function() {
console.log('listening on port ', SERVER_PORT);
});