-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
258 lines (222 loc) · 8.65 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
var express = require('express');
var MySQLStore = require('connect-mysql-session')(express);
var config = require('./config');
var Request = require('./lib/request');
var Auth = require('./lib/auth');
var ApiAdapter = require('./lib/api_adapter');
var app = express();
app.configure(function() {
// Headers for Cross Origin Resource Sharing (CORS)
app.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'POST, GET, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept');
next();
});
app.use(express.cookieParser(config.sessionSecret));
// Hack to set session cookie if session ID is set as a URL param.
// This is because not all browsers support sending cookies via CORS
app.use(function(req, res, next) {
if (req.query.sid && req.query.sid != 'null') {
var sid = req.query.sid.replace(/ /g, '+');
req.sessionID = sid;
req.signedCookies = req.signedCookies || {};
req.signedCookies['sencha_docs'] = sid;
}
next();
});
// Use MongoDB for session storage
app.use(express.session({
secret: config.sessionSecret,
key: 'sencha_docs',
store: new MySQLStore(
config.db.database,
config.db.user,
config.db.password,
{logging: false}
)
}));
app.use(function(req, res, next) {
// IE doesn't get content-type, so default to form encoded.
if (!req.headers['content-type']) {
req.headers['content-type'] = 'application/x-www-form-urlencoded';
}
next();
});
app.use(express.bodyParser());
app.use(express.methodOverride());
app.enable('jsonp callback');
app.use(express.logger('dev'));
});
// Authentication
app.get('/auth/session', function(req, res) {
new Request(req).getUser(function(err, user) {
if (user) {
var json = ApiAdapter.userToJson(user);
json.sessionID = req.sessionID;
res.json(json);
}
else {
res.json({sessionID: req.sessionID});
}
});
});
app.post('/auth/login', Auth.attemptLogin, function(req, res) {
new Request(req).getUser(function(err, user) {
var json = ApiAdapter.userToJson(user);
json.sessionID = req.sessionID;
json.success = true;
res.json(json);
});
});
app.post('/auth/logout', Auth.doLogout, function(req, res) {
res.json({ success: true });
});
// Redirects to the registration page.
app.get('/auth/register', function(req, res) {
res.redirect(config.auth.register);
});
// Requests for Comments
// Returns n most recent comments.
app.get('/auth/:sdk/:version/comments_recent', function(req, res) {
var query = {
offset: parseInt(req.query.offset, 10),
limit: parseInt(req.query.limit, 10),
orderBy: req.query.sortByScore ? "vote" : "created_at",
hideCurrentUser: req.query.hideCurrentUser,
hideRead: req.query.hideRead,
username: req.query.username,
targetId: req.query.targetId,
tagname: req.query.tagname
};
new Request(req).getRecentComments(query, function(err, comments) {
res.json(comments);
});
});
// Returns top users (with most upvotes or with most comments).
app.get('/auth/:sdk/:version/users', function(req, res) {
new Request(req).getTopUsers(req.query.sortBy, function(err, users) {
res.json({ success: true, data: users });
});
});
// Returns the most commented targets.
app.get('/auth/:sdk/:version/targets', function(req, res) {
new Request(req).getTopTargets(function(err, targets) {
res.json({ success: true, data: targets });
});
});
// Returns the most used tags.
app.get('/auth/:sdk/:version/tags', function(req, res) {
new Request(req).getTopTags(function(err, tags) {
res.send({ success: true, data: tags });
});
});
// Returns number of comments for each class/member,
// and when user is logged in, all his subscriptions.
app.get('/auth/:sdk/:version/comments_meta', function(req, res) {
var r = new Request(req);
r.getCommentCountsPerTarget(function(err, counts) {
r.getSubscriptions(function(err, subs) {
res.json({ comments: counts, subscriptions: subs });
});
});
});
// Returns a list of comments for a particular target (eg class, guide, video)
app.get('/auth/:sdk/:version/comments', Auth.hasStartKey, function(req, res) {
new Request(req).getComments(req.query.startkey, function(err, comments) {
res.json(comments);
});
});
// Returns a list of comments for a particular target (eg class, guide, video)
app.get('/auth/:sdk/:version/replies', function(req, res) {
new Request(req).getReplies(req.query.parentId, function(err, comments) {
res.json(comments);
});
});
// Adds new comment
app.post('/auth/:sdk/:version/comments', Auth.isLoggedIn, function(req, res) {
new Request(req).addComment(req.body.target, req.body.parentId, req.body.comment, req.body.url, function(err, comment) {
res.json({ id: comment._id, comment: comment, success: true });
});
});
// Returns plain markdown content of individual comment (used when editing a comment)
app.get('/auth/:sdk/:version/comments/:commentId', function(req, res) {
new Request(req).getComment(req.params.commentId, function(err, comment) {
res.json({ success: true, content: comment.content });
});
});
// Updates an existing comment (for voting or updating contents)
app.post('/auth/:sdk/:version/comments/:commentId', Auth.isLoggedIn, function(req, res) {
if (req.body.vote) {
Auth.canVote(req, res, function() {
new Request(req).vote(req.params.commentId, req.body.vote, function(err, direction, total) {
res.json({
success: true,
direction: direction,
total: total
});
});
});
}
else {
Auth.canModify(req, res, function() {
new Request(req).updateComment(req.params.commentId, req.body.content, function(err, comment) {
res.json({ success: true, content: comment.contentHtml });
});
});
}
});
// Deletes a comment
app.post('/auth/:sdk/:version/comments/:commentId/delete', Auth.isLoggedIn, Auth.canModify, function(req, res) {
new Request(req).setDeleted(req.params.commentId, true, function(err) {
res.send({ success: true });
});
});
// Restores a deleted comment
app.post('/auth/:sdk/:version/comments/:commentId/undo_delete', Auth.isLoggedIn, Auth.canModify, function(req, res) {
var r = new Request(req);
r.setDeleted(req.params.commentId, false, function(err) {
r.getComment(req.params.commentId, function(err, comment) {
res.send({ success: true, comment: comment });
});
});
});
// Tags a comment
app.post('/auth/:sdk/:version/comments/:commentId/add_tag', Auth.isLoggedIn, Auth.isModerator, function(req, res) {
new Request(req).addTag(req.params.commentId, req.body.tagname, function(err) {
res.send({ success: true });
});
});
// Removes tag from a comment
app.post('/auth/:sdk/:version/comments/:commentId/remove_tag', Auth.isLoggedIn, Auth.isModerator, function(req, res) {
new Request(req).removeTag(req.params.commentId, req.body.tagname, function(err) {
res.send({ success: true });
});
});
// Marks a comment 'read'
app.post('/auth/:sdk/:version/comments/:commentId/read', Auth.isLoggedIn, function(req, res) {
new Request(req).markRead(req.params.commentId, function(err) {
res.send({ success: true });
});
});
// Changes a comment to be a child of another,
// or to be at the top level (when parentId is undefined).
app.post('/auth/:sdk/:version/comments/:commentId/set_parent', Auth.isLoggedIn, Auth.isModerator, function(req, res) {
new Request(req).setParent(req.params.commentId, req.body.parentId, function(err) {
res.json({ success: true });
});
});
// Returns all subscriptions for logged in user
app.get('/auth/:sdk/:version/subscriptions', function(req, res) {
new Request(req).getSubscriptions(function(err, subs) {
res.json({ subscriptions: subs });
});
});
// Subscibe / unsubscribe to a comment thread
app.post('/auth/:sdk/:version/subscribe', Auth.isLoggedIn, function(req, res) {
new Request(req).changeSubscription(req.body.target, req.body.subscribed === 'true', function(err) {
res.send({ success: true });
});
});
app.listen(config.port);
console.log("Server started at port "+config.port+"...");