2012-03-15 18:25:06 +01:00
var path = require ( 'path' ) ;
var eejs = require ( 'ep_etherpad-lite/node/eejs' ) ;
var installer = require ( 'ep_etherpad-lite/static/js/pluginfw/installer' ) ;
2012-03-19 17:16:49 +01:00
var plugins = require ( 'ep_etherpad-lite/static/js/pluginfw/plugins' ) ;
2013-01-26 22:13:28 +01:00
var _ = require ( 'underscore' ) ;
var semver = require ( 'semver' ) ;
var async = require ( 'async' ) ;
2012-03-15 18:25:06 +01:00
exports . expressCreateServer = function ( hook _name , args , cb ) {
args . app . get ( '/admin/plugins' , function ( req , res ) {
var plugins = require ( "ep_etherpad-lite/static/js/pluginfw/plugins" ) ;
var render _args = {
plugins : plugins . plugins ,
search _results : { } ,
errors : [ ] ,
} ;
2012-09-22 13:51:39 +02:00
res . send ( eejs . require ( "ep_etherpad-lite/templates/admin/plugins.html" , render _args ) ) ;
2012-03-15 21:07:48 +01:00
} ) ;
2012-06-04 14:33:38 +02:00
args . app . get ( '/admin/plugins/info' , function ( req , res ) {
2012-09-22 13:51:39 +02:00
res . send ( eejs . require ( "ep_etherpad-lite/templates/admin/plugins-info.html" , { } ) ) ;
2012-06-04 14:33:38 +02:00
} ) ;
2012-03-15 21:07:48 +01:00
}
2012-03-15 18:25:06 +01:00
2012-03-15 21:07:48 +01:00
exports . socketio = function ( hook _name , args , cb ) {
var io = args . io . of ( "/pluginfw/installer" ) ;
io . on ( 'connection' , function ( socket ) {
2014-11-18 15:56:40 +01:00
console . warn ( "The middleware now handles auth but I'm not convinced SocketIO is being responsible enough here so this needs reviewing before hitting master" ) ;
// if (!socket.handshake.session || !socket.handshake.session.user || !socket.handshake.session.user.is_admin) return;
2012-04-19 14:25:12 +02:00
2013-03-25 17:20:10 +01:00
socket . on ( "getInstalled" , function ( query ) {
2013-01-26 22:13:28 +01:00
// send currently installed plugins
2013-03-25 17:20:10 +01:00
var installed = Object . keys ( plugins . plugins ) . map ( function ( plugin ) {
return plugins . plugins [ plugin ] . package
} )
socket . emit ( "results:installed" , { installed : installed } ) ;
2012-03-19 17:16:49 +01:00
} ) ;
2013-01-26 22:13:28 +01:00
2013-03-27 12:02:19 +01:00
socket . on ( "checkUpdates" , function ( ) {
2013-01-26 22:13:28 +01:00
// Check plugins for updates
2013-03-27 12:02:19 +01:00
installer . getAvailablePlugins ( /*maxCacheAge:*/ 60 * 10 , function ( er , results ) {
2013-03-25 17:20:10 +01:00
if ( er ) {
console . warn ( er ) ;
socket . emit ( "results:updatable" , { updatable : { } } ) ;
return ;
}
2013-01-26 22:13:28 +01:00
var updatable = _ ( plugins . plugins ) . keys ( ) . filter ( function ( plugin ) {
2013-03-25 17:20:10 +01:00
if ( ! results [ plugin ] ) return false ;
var latestVersion = results [ plugin ] . version
2013-01-26 22:13:28 +01:00
var currentVersion = plugins . plugins [ plugin ] . package . version
return semver . gt ( latestVersion , currentVersion )
} ) ;
2013-03-25 17:20:10 +01:00
socket . emit ( "results:updatable" , { updatable : updatable } ) ;
2013-01-26 22:13:28 +01:00
} ) ;
} )
2013-03-25 17:20:10 +01:00
socket . on ( "getAvailable" , function ( query ) {
installer . getAvailablePlugins ( /*maxCacheAge:*/ false , function ( er , results ) {
if ( er ) {
console . error ( er )
results = { }
}
socket . emit ( "results:available" , results ) ;
} ) ;
} ) ;
2012-03-19 17:16:49 +01:00
2012-03-15 21:07:48 +01:00
socket . on ( "search" , function ( query ) {
2013-03-25 17:20:10 +01:00
installer . search ( query . searchTerm , /*maxCacheAge:*/ 60 * 10 , function ( er , results ) {
if ( er ) {
console . error ( er )
results = { }
}
var res = Object . keys ( results )
. map ( function ( pluginName ) {
return results [ pluginName ]
2013-03-26 11:58:31 +01:00
} )
. filter ( function ( plugin ) {
return ! plugins . plugins [ plugin . name ]
2013-03-25 17:20:10 +01:00
} ) ;
2013-03-25 23:09:03 +01:00
res = sortPluginList ( res , query . sortBy , query . sortDir )
. slice ( query . offset , query . offset + query . limit ) ;
2013-03-25 17:20:10 +01:00
socket . emit ( "results:search" , { results : res , query : query } ) ;
2012-03-15 18:25:06 +01:00
} ) ;
2012-03-15 21:07:48 +01:00
} ) ;
2012-03-17 18:17:10 +01:00
socket . on ( "install" , function ( plugin _name ) {
2013-03-25 17:20:10 +01:00
installer . install ( plugin _name , function ( er ) {
2013-03-26 11:19:36 +01:00
if ( er ) console . warn ( er )
2013-03-26 15:11:30 +01:00
socket . emit ( "finished:install" , { plugin : plugin _name , error : er ? er . message : null } ) ;
2012-03-17 18:17:10 +01:00
} ) ;
2012-03-15 21:07:48 +01:00
} ) ;
2012-03-17 18:17:10 +01:00
socket . on ( "uninstall" , function ( plugin _name ) {
2013-03-25 17:20:10 +01:00
installer . uninstall ( plugin _name , function ( er ) {
2013-03-26 11:19:36 +01:00
if ( er ) console . warn ( er )
2013-03-26 15:11:30 +01:00
socket . emit ( "finished:uninstall" , { plugin : plugin _name , error : er ? er . 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
} ) ;
2012-03-15 21:07:48 +01:00
}
2013-03-25 23:09:03 +01:00
function sortPluginList ( plugins , property , /*ASC?*/ dir ) {
return plugins . sort ( function ( a , b ) {
if ( a [ property ] < b [ property ] )
return dir ? - 1 : 1 ;
if ( a [ property ] > b [ property ] )
return dir ? 1 : - 1 ;
// a must be equal to b
return 0 ;
} )
2014-11-18 15:08:44 +01:00
}