2020-11-27 10:40:05 -05:00
|
|
|
'use strict';
|
|
|
|
|
2024-02-05 21:13:02 +01:00
|
|
|
import {ArgsExpressType} from "../../types/ArgsExpressType";
|
|
|
|
import {ErrorCaused} from "../../types/ErrorCaused";
|
|
|
|
import {QueryType} from "../../types/QueryType";
|
|
|
|
|
2024-03-14 16:06:32 +01:00
|
|
|
import {getAvailablePlugins, install, search, uninstall} from "../../../static/js/pluginfw/installer";
|
|
|
|
import {PackageData} from "../../types/PackageInfo";
|
|
|
|
|
2021-02-03 23:05:58 -05:00
|
|
|
const pluginDefs = require('../../../static/js/pluginfw/plugin_defs');
|
2024-03-14 16:06:32 +01:00
|
|
|
import semver from 'semver';
|
2024-06-10 13:00:58 -04:00
|
|
|
import log4js from 'log4js';
|
|
|
|
const logger = log4js.getLogger('adminPlugins');
|
2012-03-15 18:25:06 +01:00
|
|
|
|
|
|
|
|
2024-02-05 21:13:02 +01:00
|
|
|
exports.socketio = (hookName:string, args:ArgsExpressType, cb:Function) => {
|
2020-11-23 13:24:19 -05:00
|
|
|
const io = args.io.of('/pluginfw/installer');
|
2024-02-05 21:13:02 +01:00
|
|
|
io.on('connection', (socket:any) => {
|
|
|
|
// @ts-ignore
|
2020-11-27 10:40:05 -05:00
|
|
|
const {session: {user: {is_admin: isAdmin} = {}} = {}} = socket.conn.request;
|
|
|
|
if (!isAdmin) return;
|
2012-04-19 14:25:12 +02:00
|
|
|
|
2024-02-05 21:13:02 +01:00
|
|
|
socket.on('getInstalled', (query:string) => {
|
2013-01-26 22:13:28 +01:00
|
|
|
// send currently installed plugins
|
2020-11-27 10:40:05 -05:00
|
|
|
const installed =
|
2021-02-03 23:05:58 -05:00
|
|
|
Object.keys(pluginDefs.plugins).map((plugin) => pluginDefs.plugins[plugin].package);
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2020-11-23 13:24:19 -05:00
|
|
|
socket.emit('results:installed', {installed});
|
2012-03-19 17:16:49 +01:00
|
|
|
});
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2020-11-23 13:24:19 -05:00
|
|
|
socket.on('checkUpdates', async () => {
|
2013-01-26 22:13:28 +01:00
|
|
|
// Check plugins for updates
|
2019-01-23 12:24:53 +00:00
|
|
|
try {
|
2024-03-14 16:06:32 +01:00
|
|
|
const results = await getAvailablePlugins(/* maxCacheAge:*/ 60 * 10);
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2021-02-03 23:05:58 -05:00
|
|
|
const updatable = Object.keys(pluginDefs.plugins).filter((plugin) => {
|
2019-02-08 23:20:57 +01:00
|
|
|
if (!results[plugin]) return false;
|
|
|
|
|
2020-11-23 13:24:19 -05:00
|
|
|
const latestVersion = results[plugin].version;
|
2021-02-03 23:05:58 -05:00
|
|
|
const currentVersion = pluginDefs.plugins[plugin].package.version;
|
2019-02-08 23:20:57 +01:00
|
|
|
|
|
|
|
return semver.gt(latestVersion, currentVersion);
|
2013-01-26 22:13:28 +01:00
|
|
|
});
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2020-11-23 13:24:19 -05:00
|
|
|
socket.emit('results:updatable', {updatable});
|
2021-02-08 16:21:50 -05:00
|
|
|
} catch (err) {
|
2024-02-05 21:13:02 +01:00
|
|
|
const errc = err as ErrorCaused
|
|
|
|
console.warn(errc.stack || errc.toString());
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2020-11-23 13:24:19 -05:00
|
|
|
socket.emit('results:updatable', {updatable: {}});
|
2019-01-23 12:24:53 +00:00
|
|
|
}
|
2013-03-25 17:20:10 +01:00
|
|
|
});
|
2012-03-19 17:16:49 +01:00
|
|
|
|
2024-02-05 21:13:02 +01:00
|
|
|
socket.on('getAvailable', async (query:string) => {
|
2019-01-23 12:24:53 +00:00
|
|
|
try {
|
2024-03-14 16:06:32 +01:00
|
|
|
const results = await getAvailablePlugins(/* maxCacheAge:*/ false);
|
2020-11-23 13:24:19 -05:00
|
|
|
socket.emit('results:available', results);
|
2019-01-23 12:24:53 +00:00
|
|
|
} catch (er) {
|
|
|
|
console.error(er);
|
2020-11-23 13:24:19 -05:00
|
|
|
socket.emit('results:available', {});
|
2019-01-23 12:24:53 +00:00
|
|
|
}
|
|
|
|
});
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2024-02-05 21:13:02 +01:00
|
|
|
socket.on('search', async (query: QueryType) => {
|
2019-01-23 12:24:53 +00:00
|
|
|
try {
|
2024-06-10 13:00:58 -04:00
|
|
|
if (query.searchTerm) logger.info(`Plugin search: ${query.searchTerm}'`);
|
2024-03-14 16:06:32 +01:00
|
|
|
const results = await search(query.searchTerm, /* maxCacheAge:*/ 60 * 10);
|
2020-11-23 13:24:19 -05:00
|
|
|
let res = Object.keys(results)
|
|
|
|
.map((pluginName) => results[pluginName])
|
2021-02-03 23:05:58 -05:00
|
|
|
.filter((plugin) => !pluginDefs.plugins[plugin.name]);
|
2013-03-25 23:09:03 +01:00
|
|
|
res = sortPluginList(res, query.sortBy, query.sortDir)
|
2020-11-23 13:24:19 -05:00
|
|
|
.slice(query.offset, query.offset + query.limit);
|
|
|
|
socket.emit('results:search', {results: res, query});
|
2024-06-10 13:00:58 -04:00
|
|
|
} catch (err: any) {
|
|
|
|
logger.error(`Error searching plugins: ${err}`);
|
|
|
|
socket.emit('results:searcherror', {error: err.message, query});
|
2019-01-23 12:24:53 +00:00
|
|
|
}
|
2012-03-15 21:07:48 +01:00
|
|
|
});
|
|
|
|
|
2024-02-05 21:13:02 +01:00
|
|
|
socket.on('install', (pluginName: string) => {
|
2024-03-14 16:06:32 +01:00
|
|
|
install(pluginName, (err: ErrorCaused) => {
|
2021-02-08 16:21:50 -05:00
|
|
|
if (err) console.warn(err.stack || err.toString());
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2020-11-27 10:40:05 -05:00
|
|
|
socket.emit('finished:install', {
|
|
|
|
plugin: pluginName,
|
2021-02-08 16:21:50 -05:00
|
|
|
code: err ? err.code : null,
|
|
|
|
error: err ? err.message : null,
|
2020-11-27 10:40:05 -05:00
|
|
|
});
|
2012-03-17 18:17:10 +01:00
|
|
|
});
|
2012-03-15 21:07:48 +01:00
|
|
|
});
|
|
|
|
|
2024-04-21 17:58:51 +02:00
|
|
|
|
2024-02-05 21:13:02 +01:00
|
|
|
socket.on('uninstall', (pluginName:string) => {
|
2024-03-14 16:06:32 +01:00
|
|
|
uninstall(pluginName, (err:ErrorCaused) => {
|
2021-02-08 16:21:50 -05:00
|
|
|
if (err) console.warn(err.stack || err.toString());
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2021-02-08 16:21:50 -05:00
|
|
|
socket.emit('finished:uninstall', {plugin: pluginName, error: err ? err.message : null});
|
2012-03-17 18:17:10 +01:00
|
|
|
});
|
2012-03-15 21:07:48 +01:00
|
|
|
});
|
2012-03-15 18:25:06 +01:00
|
|
|
});
|
2020-10-10 22:51:26 -04:00
|
|
|
return cb();
|
2020-11-23 13:24:19 -05:00
|
|
|
};
|
2013-03-25 23:09:03 +01:00
|
|
|
|
2023-10-17 12:49:56 +02:00
|
|
|
/**
|
|
|
|
* Sorts a list of plugins by a property
|
|
|
|
* @param {Object} plugins The plugins to sort
|
|
|
|
* @param {Object} property The property to sort by
|
|
|
|
* @param {String} dir The directory of the plugin
|
|
|
|
* @return {Object[]}
|
|
|
|
*/
|
2024-03-14 16:06:32 +01:00
|
|
|
const sortPluginList = (plugins:PackageData[], property:string, /* ASC?*/dir:string): PackageData[] => plugins.sort((a, b) => {
|
2024-02-05 21:13:02 +01:00
|
|
|
// @ts-ignore
|
2020-11-27 10:40:05 -05:00
|
|
|
if (a[property] < b[property]) {
|
|
|
|
return dir ? -1 : 1;
|
|
|
|
}
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2024-02-05 21:13:02 +01:00
|
|
|
// @ts-ignore
|
2020-11-27 10:40:05 -05:00
|
|
|
if (a[property] > b[property]) {
|
|
|
|
return dir ? 1 : -1;
|
|
|
|
}
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2020-11-27 10:40:05 -05:00
|
|
|
// a must be equal to b
|
|
|
|
return 0;
|
|
|
|
});
|