mirror of
https://github.com/ether/etherpad-lite.git
synced 2025-05-05 22:57:11 -04:00
Added pads manager.
This commit is contained in:
parent
61a95c1630
commit
4d97c3c48f
71 changed files with 1787 additions and 65 deletions
|
@ -1,10 +1,17 @@
|
|||
'use strict';
|
||||
|
||||
|
||||
import {PadQueryResult, PadSearchQuery} from "../../types/PadSearchQuery";
|
||||
import {PadType} from "../../types/PadType";
|
||||
|
||||
const eejs = require('../../eejs');
|
||||
const fsp = require('fs').promises;
|
||||
const hooks = require('../../../static/js/pluginfw/hooks');
|
||||
const plugins = require('../../../static/js/pluginfw/plugins');
|
||||
const settings = require('../../utils/Settings');
|
||||
const UpdateCheck = require('../../utils/UpdateCheck');
|
||||
const padManager = require('../../db/PadManager');
|
||||
const api = require('../../db/API');
|
||||
|
||||
exports.expressCreateServer = (hookName:string, {app}:any) => {
|
||||
app.get('/admin/settings', (req:any, res:any) => {
|
||||
|
@ -16,14 +23,15 @@ exports.expressCreateServer = (hookName:string, {app}:any) => {
|
|||
});
|
||||
};
|
||||
|
||||
|
||||
const queryPadLimit = 12;
|
||||
|
||||
|
||||
exports.socketio = (hookName:string, {io}:any) => {
|
||||
io.of('/settings').on('connection', (socket: any ) => {
|
||||
console.log('Admin connected to /admin/settings')
|
||||
// @ts-ignore
|
||||
const {session: {user: {is_admin: isAdmin} = {}} = {}} = socket.conn.request;
|
||||
console.log('isAdmin', isAdmin)
|
||||
if (!isAdmin) return;
|
||||
console.log('Admin authenticated')
|
||||
|
||||
socket.on('load', async (query:string):Promise<any> => {
|
||||
let data;
|
||||
|
@ -46,6 +54,155 @@ exports.socketio = (hookName:string, {io}:any) => {
|
|||
socket.emit('saveprogress', 'saved');
|
||||
});
|
||||
|
||||
|
||||
socket.on('help', ()=> {
|
||||
const gitCommit = settings.getGitCommit();
|
||||
const epVersion = settings.getEpVersion();
|
||||
|
||||
const hooks:Map<string, Map<string,string>> = plugins.getHooks('hooks', false);
|
||||
const clientHooks:Map<string, Map<string,string>> = plugins.getHooks('client_hooks', false);
|
||||
|
||||
function mapToObject(map: Map<string,any>) {
|
||||
let obj = Object.create(null);
|
||||
for (let [k,v] of map) {
|
||||
if(v instanceof Map) {
|
||||
obj[k] = mapToObject(v);
|
||||
} else {
|
||||
obj[k] = v;
|
||||
}
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
|
||||
socket.emit('reply:help', {
|
||||
gitCommit,
|
||||
epVersion,
|
||||
installedPlugins: plugins.getPlugins(),
|
||||
installedParts: plugins.getParts(),
|
||||
installedServerHooks: mapToObject(hooks),
|
||||
installedClientHooks: mapToObject(clientHooks),
|
||||
latestVersion: UpdateCheck.getLatestVersion(),
|
||||
})
|
||||
});
|
||||
|
||||
|
||||
socket.on('padLoad', async (query: PadSearchQuery) => {
|
||||
const {padIDs} = await padManager.listAllPads();
|
||||
|
||||
const data:{
|
||||
total: number,
|
||||
results?: PadQueryResult[]
|
||||
} = {
|
||||
total: padIDs.length,
|
||||
};
|
||||
let result: string[] = padIDs;
|
||||
let maxResult;
|
||||
|
||||
// Filter out matches
|
||||
if (query.pattern) {
|
||||
result = result.filter((padName: string) => padName.includes(query.pattern));
|
||||
}
|
||||
|
||||
data.total = result.length;
|
||||
|
||||
maxResult = result.length - 1;
|
||||
if (maxResult < 0) {
|
||||
maxResult = 0;
|
||||
}
|
||||
|
||||
if (query.offset && query.offset < 0) {
|
||||
query.offset = 0;
|
||||
} else if (query.offset > maxResult) {
|
||||
query.offset = maxResult;
|
||||
}
|
||||
|
||||
if (query.limit && query.limit < 0) {
|
||||
query.limit = 0;
|
||||
} else if (query.limit > queryPadLimit) {
|
||||
query.limit = queryPadLimit;
|
||||
}
|
||||
|
||||
if (query.sortBy === 'padName') {
|
||||
result = result.sort((a,b)=>{
|
||||
if(a < b) return query.ascending ? -1 : 1;
|
||||
if(a > b) return query.ascending ? 1 : -1;
|
||||
return 0;
|
||||
}).slice(query.offset, query.offset + query.limit);
|
||||
|
||||
data.results = await Promise.all(result.map(async (padName: string) => {
|
||||
const pad = await padManager.getPad(padName);
|
||||
const revisionNumber = pad.getHeadRevisionNumber()
|
||||
const userCount = api.padUsersCount(padName).padUsersCount;
|
||||
const lastEdited = await pad.getLastEdit();
|
||||
|
||||
return {
|
||||
padName,
|
||||
lastEdited,
|
||||
userCount,
|
||||
revisionNumber
|
||||
}}));
|
||||
} else {
|
||||
const currentWinners: PadQueryResult[] = []
|
||||
let queryOffsetCounter = 0
|
||||
for (let res of result) {
|
||||
|
||||
const pad = await padManager.getPad(res);
|
||||
const padType = {
|
||||
padName: res,
|
||||
lastEdited: await pad.getLastEdit(),
|
||||
userCount: api.padUsersCount(res).padUsersCount,
|
||||
revisionNumber: pad.getHeadRevisionNumber()
|
||||
};
|
||||
|
||||
if (currentWinners.length < query.limit) {
|
||||
if(queryOffsetCounter < query.offset){
|
||||
queryOffsetCounter++
|
||||
continue
|
||||
}
|
||||
currentWinners.push({
|
||||
padName: res,
|
||||
lastEdited: await pad.getLastEdit(),
|
||||
userCount: api.padUsersCount(res).padUsersCount,
|
||||
revisionNumber: pad.getHeadRevisionNumber()
|
||||
})
|
||||
} else {
|
||||
// Kick out worst pad and replace by current pad
|
||||
let worstPad = currentWinners.sort((a, b) => {
|
||||
if (a[query.sortBy] < b[query.sortBy]) return query.ascending ? -1 : 1;
|
||||
if (a[query.sortBy] > b[query.sortBy]) return query.ascending ? 1 : -1;
|
||||
return 0;
|
||||
})
|
||||
if(worstPad[0]&&worstPad[0][query.sortBy] < padType[query.sortBy]){
|
||||
if(queryOffsetCounter < query.offset){
|
||||
queryOffsetCounter++
|
||||
continue
|
||||
}
|
||||
currentWinners.splice(currentWinners.indexOf(worstPad[0]), 1)
|
||||
currentWinners.push({
|
||||
padName: res,
|
||||
lastEdited: await pad.getLastEdit(),
|
||||
userCount: api.padUsersCount(res).padUsersCount,
|
||||
revisionNumber: pad.getHeadRevisionNumber()
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
data.results = currentWinners;
|
||||
}
|
||||
|
||||
socket.emit('results:padLoad', data);
|
||||
})
|
||||
|
||||
|
||||
socket.on('deletePad', async (padId: string) => {
|
||||
const padExists = await padManager.doesPadExists(padId);
|
||||
if (padExists) {
|
||||
const pad = await padManager.getPad(padId);
|
||||
await pad.remove();
|
||||
socket.emit('results:deletePad', padId);
|
||||
}
|
||||
})
|
||||
|
||||
socket.on('restartServer', async () => {
|
||||
console.log('Admin request to restart server through a socket on /admin/settings');
|
||||
settings.reloadSettings();
|
||||
|
@ -55,3 +212,10 @@ exports.socketio = (hookName:string, {io}:any) => {
|
|||
});
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
const searchPad = async (query:PadSearchQuery) => {
|
||||
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue