-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathroutes.js
180 lines (149 loc) · 4.28 KB
/
routes.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
var effects = require('./effects');
var remote = require('./utils/remote');
var conf = require('./conf.json');
var VERSION = '0.6.0';
exports.index = function(req, res) {
effects.getAll(function(err, data) {
res.render('index', {
title: 'Elovalo Webide', // TODO: move to tpl
effects: data,
user: getUser(req),
dev: conf.dev,
version: VERSION
});
});
};
exports.editor = function(req, res) {
var id = req.query.id;
if(id) {
effects.read(id, function(err, d) {
res.render('editor', {
title: 'Effect editor', // TODO: move to tpl
initialCode: d,
codeId: id,
user: getUser(req),
dev: conf.dev,
version: VERSION
});
});
}
else {
res.render('editor', {
title: 'Effect editor', // TODO: move to tpl
user: getUser(req),
initialCode: undefined,
codeId: undefined,
dev: conf.dev,
version: VERSION
});
}
};
exports.editorPost = function(req, res) {
var ops = {
save: save,
playbackOnCube: playbackOnCube,
stopOnCube: stopOnCube,
pingOnCube: pingOnCube
}[req.param('op')](req, res);
};
function save(req, res) {
var code = req.param('code');
var id = req.param('id') || req.session.effectId;
var author = getUser(req).id;
var status;
// TODO: attach name
// TODO: attach description
if(code && author) {
if(id) {
console.log('updating effect', author, id);
effects.getMeta(id, function(err, d) {
if(err) return res.send(404);
if(author == d.author) {
console.log('saving effect', author);
effects.commitEffect('Save effect', id, code, function(err) {
if(err) return res.send(404);
res.send(200);
});
}
else {
console.log('forking effect', author, d.author);
createEffect(req, res, {author: author, code: code, parent: d.author}, function(err, d) {
if(err) return res.send(404);
res.send(200);
});
}
});
}
else {
console.log('creating effect', author);
createEffect(req, res, {author: author, code: code}, function(err, d) {
if(err) return res.send(404);
res.send(200);
});
}
}
else res.send(404);
}
function playbackOnCube(req, res) {
var author = getUser(req).id;
var code = req.param('code');
if(author && code) {
remote.play(author, code, function(err) {
if(err) return res.send(404);
res.send(200);
});
}
else res.send(404);
}
function stopOnCube(req, res) {
var author = getUser(req).id;
if(author) {
remote.stop(author, function(err) {
if(err) return res.send(404);
res.send(200);
});
}
else res.send(404);
}
function pingOnCube(req, res) {
var author = getUser(req).id;
var code = req.param('code');
if(author && code) {
remote.ping(author, code, function(err) {
if(err) return res.send(404);
res.send(200);
});
}
else stopOnCube(req, res, function(err) {
if(err) return res.send(404);
res.send(200);
});
}
function createEffect(req, res, o, cb) {
effects.create(o, function(err, d) {
if(err) return res.send(400);
res.send(200);
req.session.effectId = d.id;
req.session.save(cb);
});
cb();
}
exports.effects = function(req, res) {
var id = req.query.id;
if(id) {
effects.read(id, function(err, d) {
if(err) return res.send(404);
return res.send(d);
});
}
else {
effects.getAllMeta(function(err, d) {
if(err) return res.send(404);
return res.json(d);
});
}
};
// TODO: eliminate this by fixing plain auth
function getUser(req) {
return req.user? req.user: req.session.user;
}