-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
384 lines (355 loc) · 15.6 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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*
(c) 2015 Finnian Anderson. All rights reserved.
*/
var config = require("./config.js");
var io, socket_end_point;
var extensions = require("./modules/extensions/index.js");
var fs = require("fs");
var express = require("express");
var app = express();
var http = require("http").Server(app);
var sha1 = require("sha1");
var request = require("request");
var cheerio = require('cheerio');
var bodyParser = require('body-parser');
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({
extended: true
})); // support encoded bodies
if (config.production) {
io = require("socket.io")(http, {
path: '/socket.io'
});
config.socket = {
path: '/codevisor/socket.io'
};
} else {
io = require("socket.io")(http);
config.socket = {};
}
var mongoose = require("mongoose");
var ObjectId = require('mongoose').Types.ObjectId;
// Schema
var pageSchema = mongoose.Schema({
owner: String, // owner of repository
repo: String, // repository
head: String, // branch
theme: String, // page theme
tree: Object, // cache of tree
commits: Object, // cache of commits
contributors: Object, // cache of contributors
sockets: Array // currently connected sockets and their preferences
});
var Page = mongoose.model("page", pageSchema);
mongoose.connect('mongodb://localhost:27017/codevisor');
var db = mongoose.connection;
db.on('error', function(callback) {
console.log("[CodeVisor] Error Connecting to MongoDB");
});
db.once('open', function(callback) {
console.log("[CodeVisor] Connected to MongoDB");
});
app.use("/", express.static(__dirname + "/static"));
app.get("/get_config", function(req, res) {
res.json(config);
});
app.post("/hooks", function(req, res) {
var data = req.body;
Page.findOne({
owner: data.organization.login.toLowerCase(),
repo: data.repository.name.toLowerCase()
}, function(err, page) {
if (err) console.log(err);
if (page) {
for (var i = 0; i < page.sockets.length; i++) {
console.log("Notifying socket " + page.sockets[i].id + " about a commit for " + data.repository.full_name);
io.to(page.sockets[i].id).emit('commits', data); // if this fails, we need to remove this socket from page.sockets in the DB
}
if (page.sockets.length < 1) {
console.log("No connected sockets for " + data.repository.full_name);
}
} else {
console.log("No page for " + data.repository.full_name);
}
});
res.json({
message: "Thanks GitHub, we've notified all the users!"
});
});
app.get("/p/*", function(req, res) {
res.redirect("/");
// // what follows is probably really really bad code and uses practises frowned upon by the Node Gods
// console.log("Repo page requested for " + req.params[0]);
// var splitParam = req.params[0].split("/")
// var owner = splitParam[0];
// var repo = splitParam[1];
// Page.findOne({
// "owner": owner,
// "repo": repo
// }, function(err, doc) {
// if (err) console.log(err);
// if (doc) {
// fs.readFile(__dirname + '/static/index.html', 'utf8', function(err, html) {
// // prevent the url rewriting that we use in the front end
// $ = cheerio.load(html);
// $("body").find("script").each(function() {
// if ($(this).data("name") == "main") {
// $(this).text("var doNotChangeURL=true;" + $(this).text());
// }
// });
// res.send($.html());
// });
// } else {
// res.status(404).send('Sorry, that page could not be found!');
// }
// });
});
io.on("connection", function(socket) {
socket.on("disconnect", function() {
Page.findOne({
"sockets.id": socket.id
}, function(err, page) {
if (err) console.log(err);
if (page) {
for (var i = 0; i < page.sockets.length; i++) {
if (page.sockets[i].id == socket.id) {
Page.update({
_id: ObjectId(page._id)
}, {
$pull: {
sockets: {
id: socket.id
}
}
}); // in the future, this needs to work
break;
}
}
}
});
});
socket.on("extension colour", function(packet, fn) {
var extension = packet.extension.split(".");
extension = extension[extension.length - 1];
colour = extensions.ext("." + extension);
fn(colour);
});
socket.on("get heads", function(packet) {
packet.url.replace("http://", "").replace("https://", "");
var urlRegExp = /^((ht|f)tps?:\/\/|)[a-z0-9-\.]+\.[a-z]{2,4}\/?([^\s<>\#%"\,\{\}\\|\\\^\[\]`]+)?$/;
if (!packet.url || urlRegExp.test(packet.url.match)) {
socket.emit("e", {
message: "Invalid URL"
});
} else {
if (packet.url.indexOf("github.com") !== -1) {
var splitted = packet.url.split("/");
var owner = splitted[1];
var repo = splitted[2];
request({
url: "https://api.github.com/repos/" + owner + "/" + repo + "/git/refs/heads",
headers: {
"User-Agent": "CodeVisor"
}
}, function(error, response, body) {
if (!error && (response.statusCode == 200 || response.statusCode == 403)) {
socket.emit("head list", {
status: true,
heads: JSON.parse(body),
owner: owner,
repo: repo
});
} else {
if (response.statusCode == 404) {
socket.emit("head list", {
status: false,
message: "Repository not found"
});
} else {
socket.emit("head list", {
status: false,
message: "Unknown error"
});
}
}
});
} else {
socket.emit("head list", {
status: false,
message: "Unsupported host"
});
}
}
});
socket.on("repo page", function(packet) {
var page;
Page.findOne({
"owner": packet.owner,
"repo": packet.repo
}, function(err, doc) {
if (err) console.log(err);
if (doc) {
doc.sockets.push({
push: true,
id: socket.id
});
doc.save();
page = doc;
socket.emit("repo page", {
status: true,
url: "p/" + page.owner + "/" + page.repo,
contributors: page.contributors,
commits: page.commits,
tree: page.tree,
theme: page.theme,
head: page.head
});
} else {
var treeCommitCollectComplete = false;
var contributorCommitCollectComplete = false;
var page = new Page({
owner: packet.owner,
repo: packet.repo,
head: packet.head,
theme: config.defaultTheme,
tree: {},
commits: {},
contributors: {},
sockets: [{
push: true,
id: socket.id
}]
});
function complete() {
if (treeCommitCollectComplete && contributorCommitCollectComplete) {
page.save(function(err, self) {
if (err) console.log("[CodeVisor] " + err);
});
socket.emit("repo page", {
status: true,
url: "p/" + packet.owner + "/" + packet.repo,
contributors: page.contributors,
commits: page.commits,
tree: page.tree,
theme: page.theme
});
}
}
request({
url: "https://api.github.com/repos/" + page.owner + "/" + page.repo + "/commits",
headers: {
'User-Agent': 'CodeVisor'
}
}, function(error, response, body) {
if (!error && (response.statusCode == 200 || response.statusCode == 403)) {
var commits = JSON.parse(body);
var contributors = [];
var commits2 = [];
for (var i = 0; i < commits.length; i++) {
var duplicate = false;
if (commits[i].author !== null) { // this happens if someone didn't set up their SSH key to use their GitHub email
for (var i2 = 0; i2 < contributors.length; i2++) { // check we don't have a duplicate contributor
if (contributors[i2].id == commits[i].author.id) {
duplicate = true;
break;
}
}
if (!duplicate) { // we didn't find a duplicate contributor
contributors.push({
id: commits[i].author.id,
name: commits[i].commit.author.name,
icon: commits[i].author.avatar_url,
url: commits[i].author.html_url
});
}
delete commits[i].commit.committer;
commits[i].commit.author.icon = commits[i].author.avatar_url;
commits2.push(commits[i].commit);
}
}
page.commits = commits2;
page.contributors = contributors;
contributorCommitCollectComplete = true;
complete();
} else {
console.log(error);
console.log(response.statusCode);
}
});
request({
url: "https://api.github.com/repos/" + page.owner + "/" + page.repo + "/git/trees/" + page.head + "?recursive=1",
headers: {
'User-Agent': 'CodeVisor'
}
}, function(error, response, body) {
if (!error && (response.statusCode == 200 || response.statusCode == 403)) {
body = JSON.parse(body);
var directory = {
name: "/",
children: [],
path: "/"
};
function hasChild(name, root) {
var indexOfChild = -1;
for (var i = 0; i < root.children.length; i++) {
if (root.children[i].name === name) {
indexOfChild = i;
break;
}
}
return (indexOfChild);
}
var root = body.tree;
for (var i = 0; i < root.length; i++) { // loop through every file
var bookmark = directory;
var path = root[i].path.split("/");
var name = path[path.length - 1];
for (var part = 0; part < path.length; part++) { // loop through every part of the path
var newChild = { // this is who we're gonna give birth to
name: name,
path: path.join("/")
};
var extension = name.split(".");
extension = extension[extension.length - 1];
color = extensions.ext("." + extension);
newChild.color = color;
if (root[i].type == "tree") { // the file is a directory
newChild.children = []; // our baby currently hasn't got any children
var indexOfChild = hasChild(path[part], bookmark); // does this child already exist?
if (indexOfChild != -1) { // it does
bookmark = bookmark.children[indexOfChild]; // set the bookmark to the correct child
} else { // we need to give birth to it
var newChildIndex = bookmark.children.push({
name: path[part],
children: [],
path: path.join("/")
}); // give birth to it!
bookmark = bookmark.children[newChildIndex - 1]; // set the bookmark to be the new child
}
} else { // it's a file
for (var part = 0; part < path.length - 1; part++) { // loop through every part of the path (excluding file itself)
bookmark = bookmark.children[hasChild(path[part], bookmark)];
}
newChild.size = root[i].size; // add the size attribute
bookmark.children.push(newChild); // give birth to it!
// note: we don't now set the bookmark because we want the next child to be a younger sibling
}
}
}
page.tree = directory;
treeCommitCollectComplete = true;
complete();
} else {
console.log(error);
console.log(response.statusCode);
}
});
}
});
});
});
var server = http.listen(3003, function() {
var host = server.address().address;
var port = server.address().port;
console.log('[CodeVisor] Listening at http://%s:%s', host, port);
});