summaryrefslogtreecommitdiff
path: root/src/node/hooks/express/adminplugins.js
blob: ccc100dcc1adc9fc8bd8453446ed37d34921c8fb (plain)
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
var path = require('path');
var eejs = require('ep_etherpad-lite/node/eejs');
var installer = require('ep_etherpad-lite/static/js/pluginfw/installer');
var plugins = require('ep_etherpad-lite/static/js/pluginfw/plugins');
var _ = require('underscore');
var semver = require('semver');
var async = require('async');

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: [],
    };
    res.send( eejs.require("ep_etherpad-lite/templates/admin/plugins.html", render_args) );
  });
  args.app.get('/admin/plugins/info', function(req, res) {
    res.send( eejs.require("ep_etherpad-lite/templates/admin/plugins-info.html", {}) );
  });
}

exports.socketio = function (hook_name, args, cb) {
  var io = args.io.of("/pluginfw/installer");
  io.on('connection', function (socket) {

    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;

    socket.on("getInstalled", function (query) {
      // send currently installed plugins
      var installed = Object.keys(plugins.plugins).map(function(plugin) {
        return plugins.plugins[plugin].package
      })
      socket.emit("results:installed", {installed: installed});
    });
    
    socket.on("checkUpdates", function() {
      // Check plugins for updates
      installer.getAvailablePlugins(/*maxCacheAge:*/60*10, function(er, results) {
        if(er) {
          console.warn(er);
          socket.emit("results:updatable", {updatable: {}});
          return;
        }
        var updatable = _(plugins.plugins).keys().filter(function(plugin) {
          if(!results[plugin]) return false;
          var latestVersion = results[plugin].version
          var currentVersion = plugins.plugins[plugin].package.version
          return semver.gt(latestVersion, currentVersion)
        });
        socket.emit("results:updatable", {updatable: updatable});
      });
    })
    
    socket.on("getAvailable", function (query) {
        installer.getAvailablePlugins(/*maxCacheAge:*/false, function (er, results) {
          if(er) {
            console.error(er)
            results = {}
          }
          socket.emit("results:available", results);
      });
    });

    socket.on("search", function (query) {
      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]
          })
          .filter(function(plugin) {
            return !plugins.plugins[plugin.name]
          });
        res = sortPluginList(res, query.sortBy, query.sortDir)
          .slice(query.offset, query.offset+query.limit);
        socket.emit("results:search", {results: res, query: query});
      });
    });

    socket.on("install", function (plugin_name) {
      installer.install(plugin_name, function (er) {
        if(er) console.warn(er)
        socket.emit("finished:install", {plugin: plugin_name, error: er? er.message : null});
      });
    });

    socket.on("uninstall", function (plugin_name) {
      installer.uninstall(plugin_name, function (er) {
        if(er) console.warn(er)
        socket.emit("finished:uninstall", {plugin: plugin_name, error: er? er.message : null});
      });
    });
  });
}

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;
  })
}