2016-03-12 13:50:43 -05:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
core.register("onSessionBegin", index);
|
|
|
|
core.register("onSessionEnd", index);
|
|
|
|
|
|
|
|
function index() {
|
2016-04-09 12:42:15 -04:00
|
|
|
return Promise.all([core.getPackages(), core.getUsers()]).then(function(values) {
|
2016-03-12 13:50:43 -05:00
|
|
|
let packages = values[0];
|
|
|
|
let users = values[1];
|
|
|
|
let usersByApp = {};
|
2016-04-09 12:42:15 -04:00
|
|
|
let servicesByApp = {};
|
2016-03-12 13:50:43 -05:00
|
|
|
for (let i in users) {
|
|
|
|
let user = users[i];
|
2016-04-09 12:42:15 -04:00
|
|
|
let key = "/~" + user.packageOwner + "/" + user.packageName;
|
|
|
|
if (user.key.substring(0, "service_".length) == "service_") {
|
|
|
|
if (!servicesByApp[key]) {
|
|
|
|
servicesByApp[key] = [];
|
|
|
|
}
|
|
|
|
servicesByApp[key].push(user);
|
|
|
|
} else {
|
|
|
|
if (!usersByApp[key]) {
|
|
|
|
usersByApp[key] = [];
|
|
|
|
}
|
|
|
|
usersByApp[key].push(user.name);
|
2016-03-12 13:50:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-03 11:01:21 -04:00
|
|
|
terminal.cork();
|
2016-03-12 13:50:43 -05:00
|
|
|
terminal.clear();
|
|
|
|
terminal.print("Available applications [active users]:");
|
|
|
|
packages.sort(function(x, y) {
|
|
|
|
return Math.sign(x.owner.localeCompare(y.owner)) * 10 + Math.sign(x.name.localeCompare(y.name)) * 1;
|
|
|
|
}).forEach(function(app) {
|
|
|
|
let users = usersByApp["/~" + app.owner + "/" + app.name];
|
2016-04-09 12:42:15 -04:00
|
|
|
let services = servicesByApp["/~" + app.owner + "/" + app.name];
|
2016-03-12 13:50:43 -05:00
|
|
|
let message = [];
|
2016-04-09 12:42:15 -04:00
|
|
|
if (users || services) {
|
2016-03-12 13:50:43 -05:00
|
|
|
message.push(" [");
|
2016-04-09 12:42:15 -04:00
|
|
|
if (users) {
|
|
|
|
let counts = {};
|
|
|
|
for (let i = 0; i < users.length; i++) {
|
|
|
|
counts[users[i]] = (counts[users[i]] || 0) + 1;
|
|
|
|
}
|
|
|
|
let names = Object.keys(counts).sort();
|
|
|
|
for (let i = 0; i < names.length; i++) {
|
|
|
|
var name = names[i];
|
|
|
|
if (message.length > 1) {
|
|
|
|
message.push(", ");
|
|
|
|
}
|
|
|
|
message.push({class: "orange", value: name});
|
|
|
|
if (counts[name] > 1) {
|
|
|
|
message.push({class: "base01", value: "(x" + counts[name] + ")"});
|
|
|
|
}
|
|
|
|
}
|
2016-03-12 13:50:43 -05:00
|
|
|
}
|
2016-04-09 12:42:15 -04:00
|
|
|
if (services) {
|
|
|
|
if (users) {
|
2016-03-12 13:50:43 -05:00
|
|
|
message.push(", ");
|
|
|
|
}
|
2016-04-09 12:42:15 -04:00
|
|
|
message.push("⚒".repeat(services.length));
|
2016-03-12 13:50:43 -05:00
|
|
|
}
|
|
|
|
message.push("]");
|
|
|
|
}
|
|
|
|
terminal.print(
|
|
|
|
"* ",
|
|
|
|
{href: "/~" + app.owner + "/" + app.name},
|
|
|
|
message);
|
|
|
|
});
|
2016-04-03 11:01:21 -04:00
|
|
|
terminal.uncork();
|
2016-03-12 13:50:43 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-04-09 12:42:15 -04:00
|
|
|
index().catch(function(error) {
|
|
|
|
terminal.print("ERROR:", error);
|
|
|
|
});
|