forked from ktt-ol/ep_pad-lister
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
100 lines (87 loc) · 2.44 KB
/
index.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
'use strict';
var eejs = require('ep_etherpad-lite/node/eejs');
var padManager = require('ep_etherpad-lite/node/db/PadManager');
var dateFormat = require('dateformat');
var PRIVATE_PAD_PREFIX = 'private_';
exports.eejsBlock_indexWrapper = function (hook_name, args, cb) {
var render_args = {
PRIVATE_PAD_PREFIX: PRIVATE_PAD_PREFIX
};
args.content = args.content + eejs.require("ep_pad-lister/templates/linkToList.ejs", render_args);
return cb();
};
exports.registerRoute = function (hook_name, args, cb) {
args.app.get('/pad-lister/static/bootstrap.min.css', function (req, res) {
res.sendFile(__dirname + '/static/css/bootstrap.min.css');
});
args.app.get('/pad-lister', function (req, res) {
getDetailedPadList(function (pads) {
var render_args = {
PRIVATE_PAD_PREFIX: PRIVATE_PAD_PREFIX,
pads: pads
};
res.send(eejs.require('ep_pad-lister/templates/list.html', render_args));
});
});
};
function getDetailedPadList(callback) {
var padsToDo;
var padData = [];
function doneAction() {
padsToDo--;
if (padsToDo > 0) {
return;
}
// sort
padData = sortPadData(padData);
// format each timestamp
padData.forEach(function (padObj) {
padObj.lastAccess = formatDate(padObj.lastAccess);
});
callback(padData);
}
padManager.listAllPads(function (err, listResult) {
var padNames = listResult.padIDs;
// if we have no pads...
if (padNames.length === 0) {
callback([]);
return;
}
padsToDo = padNames.length;
padNames.forEach(function (padName) {
// ignore private pads
if (padName.indexOf(PRIVATE_PAD_PREFIX) === 0) {
doneAction();
return;
}
padManager.getPad(padName, function (err, pad) {
if (!pad) {
doneAction();
return;
}
// ignore pads without any changes
if (pad.head === 0) {
doneAction();
return;
}
pad.getLastEdit(function (err, time) {
padData.push({
name: padName,
lastRevision: pad.head,
lastAccess: time
});
doneAction();
});
}); // end getPad
}); // end forEach
});
}
// sort by last access
function sortPadData(padData) {
return padData.sort(function (a, b) {
return b.lastAccess - a.lastAccess;
});
}
function formatDate(timestamp) {
return dateFormat(new Date(timestamp), 'dd.mm.yyyy HH:MM:ss');
}