-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathindex.js
More file actions
98 lines (83 loc) · 2.37 KB
/
index.js
File metadata and controls
98 lines (83 loc) · 2.37 KB
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
'use strict';
const eejs = require('ep_etherpad-lite/node/eejs');
const padManager = require('ep_etherpad-lite/node/db/PadManager');
const dateFormat = require('dateformat');
const db = require('ep_etherpad-lite/node/db/DB').db;
const PRIVATE_PAD_PREFIX = 'private_';
exports.eejsBlock_indexWrapper = function (hook_name, args, cb) {
const 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) {
args.app.get('/pad-lister/static/bootstrap.min.css', (req, res) => {
res.sendFile('static/css/bootstrap.min.css', {
root: __dirname
});
});
args.app.get('/pad-lister', (req, res) => {
getDetailedPadList().then(pads => {
const render_args = {
PRIVATE_PAD_PREFIX: PRIVATE_PAD_PREFIX,
pads: pads
};
res.send(eejs.require('ep_pad-lister/templates/list.html', render_args));
});
});
};
async function getDetailedPadList() {
let padData = [];
const listResult = await padManager.listAllPads();
const padIDs = listResult.padIDs;
// if we have no pads...
if (padIDs.length === 0) {
return [];
}
// padsToDo = padNames.length;
for (let i = 0; i < padIDs.length; i++) {
const padID = padIDs[i];
// ignore private pads
if (padID.indexOf(PRIVATE_PAD_PREFIX) === 0) {
continue;
}
const pad = await padManager.getPad(padID);
if (!pad) {
continue;
}
// ignore pads without any changes
if (pad.head === 0) {
continue;
}
// support for ep_set_title_on_pad
let title;
db.get("title:"+padID, function(err, value){
if(!err && value) {
title = value;
}
});
const time = await pad.getLastEdit();
padData.push({
name: title ? title+' ('+padID+')' : padID,
padid: padID,
lastRevision: pad.head,
lastAccess: time
});
}
padData = sortPadData(padData);
// format each timestamp
padData.forEach(function (padObj) {
padObj.lastAccess = formatDate(padObj.lastAccess);
});
return padData;
}
// 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');
}