-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
137 lines (116 loc) · 4.3 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
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
const eejs = require("ep_etherpad-lite/node/eejs");
const padManager = require("ep_etherpad-lite/node/db/PadManager");
const api = require("ep_etherpad-lite/node/db/API");
const queryLimit = 12;
RegExp.quote = function (x) {
return x.toString().replace(/[-\\^$*+?.()|[\]{}]/g, "\\$&")
};
const isNumeric = function (arg) {
return typeof (arg) == "number" || (typeof (arg) == "string" && parseInt(arg));
};
let pads = {
pads: [],
search: async function (query) {
let the_pads = await padManager.listAllPads();
return await pads._do_search(the_pads.padIDs, query);
},
_do_search: async function (pads, query) {
let data = {
progress: 1
, message: "Search done."
, query: query
, total: pads.length
}
, maxResult = 0
, result = []
;
if (query["pattern"] != null && query["pattern"] !== "") {
let pattern = "*" + query.pattern + "*";
pattern = RegExp.quote(pattern);
pattern = pattern.replace(/(\\\*)+/g, ".*");
pattern = "^" + pattern + "$";
let regex = new RegExp(pattern, "i");
pads.forEach(function (padID) {
if (regex.test(padID)) {
result.push(padID);
}
});
} else {
result = pads;
}
data.total = result.length;
maxResult = result.length - 1;
if (maxResult < 0) {
maxResult = 0;
}
if (!isNumeric(query.offset) || query.offset < 0) {
query.offset = 0;
} else if (query.offset > maxResult) {
query.offset = maxResult;
}
if (!isNumeric(query.limit) || query.limit < 0) {
query.limit = queryLimit;
}
let rs = result.slice(query.offset, query.offset + query.limit);
pads.pads = rs;
let entrySet;
data.results = [];
rs.forEach(function (value) {
entrySet = {padName: value, lastEdited: "", userCount: 0};
data.results.push(entrySet);
});
if (data.results.length > 0) {
data.results.forEach(function (value) {
let resultObject = api.getLastEdited(value.padName);
value.lastEdited = resultObject.lastEdited;
resultObject = api.padUsersCount(value.padName);
value.userCount = resultObject.padUsersCount;
});
} else {
data.message = "No results";
}
return data;
}
};
exports.registerRoute = async function (hook_name, args) {
args.app.get("/admin/pads", function (req, res) {
let render_args = {
errors: []
};
res.send(eejs.require("ep_adminpads/templates/admin/pads.html", render_args));
});
};
let io = null;
exports.socketio = function (hook_name, args) {
io = args.io.of("/pluginfw/admin/pads");
io.on("connection", function (socket) {
socket.on("load", async function (query) {
let result = await pads.search({pattern: "", offset: 0, limit: queryLimit});
socket.emit("search-result", result);
});
socket.on("search", async function (query) {
let result = await pads.search(query);
socket.emit("search-result", result);
});
socket.on("delete", async function (padId) {
let padExists = await padManager.doesPadExists(padId);
if (padExists) {
//pad exists, remove
let pad = await padManager.getPad(padId);
await pad.remove();
socket.emit("progress", {progress: 1});
}
});
});
};
exports.updatePads = async function (hook_name, args) {
io.emit("progress", {progress: 1});
};
exports.eejsBlock_adminMenu = async function (hook_name, args) {
let hasAdminUrlPrefix = (args.content.indexOf("<a href=\"admin/") !== -1)
, hasOneDirDown = (args.content.indexOf("<a href=\"../") !== -1)
, hasTwoDirDown = (args.content.indexOf("<a href=\"../../") !== -1)
, urlPrefix = hasAdminUrlPrefix ? "admin/" : hasTwoDirDown ? "../../" : hasOneDirDown ? "../" : ""
;
args.content = args.content + "<li><a href=\"" + urlPrefix + "pads\">Manage pads</a></li>";
};