-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
197 lines (171 loc) · 5.31 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
// ==========================================================================
// UnorderedList Backend
// ==========================================================================
'use strict';
// Init
// --------------------------------------------------------------------------
// server
var express = require('express'),
app = express(),
parser = require('body-parser').json(),
server = require('http').createServer(app),
io = require('socket.io').listen(server),
favicon = require('serve-favicon'),
env = process.env.NODE_ENV || 'development',
port = process.env.PORT || 3000;
server.listen(port);
// database
var mongoose = require('mongoose'),
List = require('./models/list.js');
if (env === 'development') {
mongoose.connect('mongodb://localhost/ul');
} else if (env === 'test') {
mongoose.connect('mongodb://' + process.env.TEST_MONGOLAB_HOST + '/ul');
} else if (env === 'production') {
mongoose.connect('mongodb://' + process.env.MONGOLAB_URI);
}
// Routes
// --------------------------------------------------------------------------
// default
app.get('/', function (req, res) {
res.sendFile(__dirname + '/public/index.html');
});
// testing
if (env === 'test' || env === 'development') {
// spec runner
app.get('/test', function (req, res) {
res.sendFile(__dirname + '/_SpecRunner.html');
});
// spec fixtures
app.get('/tmpl/inc/:filename', function (req, res) {
res.sendFile(__dirname + '/tmpl/inc/' + req.params.filename);
});
// test assets
app.use('/.grunt', express.static(__dirname + '/.grunt'));
app.use('/spec', express.static(__dirname + '/spec'));
}
// load list
app.get('/:list', function (req, res) {
res.sendFile(__dirname + '/public/index.html');
});
// get item
app.get('/get/:list', function (req, res) {
List.aggregate(
{ $match: { name: req.params.list } },
{ $unwind: '$items' },
{ $match: { 'items.active': true } },
{ $sort: { 'items.added': -1 } },
{ $limit: 1 }, function (err, list) {
if (err || !list.length) {
res.status(500);
res.send(err);
} else {
res.send(list[0].items);
}
});
});
// post item
app.post('/:list', parser, function (req, res) {
List.findOne({ name: req.params.list }, function (err, list) {
if (err || !list) {
res.status(500);
res.send(err);
} else {
insert_item(list, req.body.item);
res.send('ITEM_ADDED');
}
});
});
// public assets
app.use('/public', express.static(__dirname + '/public'));
app.use(favicon(__dirname + '/public/favicon.ico'));
// Sockets
// --------------------------------------------------------------------------
io.sockets.on('connection', function (socket) {
// user connected
console.log('User Connected - ' + new Date());
// load list
socket.on('load list', function (list_name) {
List.find({ name : list_name }, function (err, doc) {
if (err) {
console.error(err);
// create new
} else if (!doc.length) {
var new_list = new List({ name: list_name });
new_list.save( function (err) {
if (err) {
console.error(err);
} else {
socket.emit('new list', list_name);
console.log('List Created: ' + list_name + ' (' + new Date() + ')');
}
});
// load history
} else {
List.find({ name: list_name }, function (err, docs) {
if (err) {
console.error(err);
// send active items
} else {
var list_items = [],
i, max = docs[0].items.length;
for(i = 0; i < max; i++) {
if(docs[0].items[i].active === true) {
list_items.push(docs[0].items[i]);
}
}
socket.emit('load list', list_name, list_items);
console.log('List Loaded: ' + list_name + ' (' + new Date() + ')');
}
});
}
});
});
// add item
socket.on('add item', function (list_name, item) {
List.findOne({ name: list_name }, function (err, list) {
if (err) {
console.error(err);
} else {
insert_item(list, item);
}
});
});
// remove item (set inactive)
socket.on('remove item', function (list_name, item) {
var conditions = { name: list_name, 'items._id': item },
update = { '$set': { 'items.$.active': false } };
List.update(conditions, update, function (err) {
if (err) {
console.error(err);
} else {
io.sockets.emit('item deleted', item);
console.log('Item Deleted: [' + list_name + '] ' + item + ' (' + new Date() + ')');
}
});
});
// user disconnected
socket.on('disconnect', function () {
console.log('User Disconnected - ' + new Date());
});
});
// Helpers
// --------------------------------------------------------------------------
// insert list item
function insert_item(list, item) {
var data = {
body: item,
active: true,
_id: new mongoose.Types.ObjectId,
added: new Date
};
list.items.push(data);
list.save( function (err) {
if (err) {
console.error('err');
} else {
io.sockets.emit('item added', data, list.name);
console.log('Item Added: [' + list.name + '] ' + item + ' (' + new Date() + ')');
}
});
}