Comparison

plugins/mod_admin_shell.lua @ 12870:56397f3b58c1

mod_admin_shell: Sort MUC users by relation and JID Suggested by MattJ, our resident UI expert :)
author Kim Alvefur <zash@zash.se>
date Sun, 29 Jan 2023 21:37:13 +0100
parent 12869:70ee82579076
child 12871:885323e2a1ce
comparison
equal deleted inserted replaced
12869:70ee82579076 12870:56397f3b58c1
1359 return nil, "No such room: "..room_jid; 1359 return nil, "No such room: "..room_jid;
1360 end 1360 end
1361 return room_obj; 1361 return room_obj;
1362 end 1362 end
1363 1363
1364 local muc_util = module:require"muc/util";
1365
1364 function def_env.muc:create(room_jid, config) 1366 function def_env.muc:create(room_jid, config)
1365 local room_name, host = check_muc(room_jid); 1367 local room_name, host = check_muc(room_jid);
1366 if not room_name then 1368 if not room_name then
1367 return room_name, host; 1369 return room_name, host;
1368 end 1370 end
1404 local row = format_table({ 1406 local row = format_table({
1405 { title = "Role"; width = 12; key = "role" }; -- longest role name 1407 { title = "Role"; width = 12; key = "role" }; -- longest role name
1406 { title = "JID"; width = "75%"; key = "bare_jid" }; 1408 { title = "JID"; width = "75%"; key = "bare_jid" };
1407 { title = "Nickname"; width = "25%"; key = "nick"; mapper = jid_resource }; 1409 { title = "Nickname"; width = "25%"; key = "nick"; mapper = jid_resource };
1408 }, self.session.width); 1410 }, self.session.width);
1409 local total, displayed = 0, 0; 1411 local occupants = array.collect(iterators.select(2, room_obj:each_occupant()));
1410 for nick_jid, occupant in room_obj:each_occupant() do 1412 local total = #occupants;
1411 if total == 0 then 1413 if filter then
1412 print(row()); 1414 occupants:filter(function(occupant)
1413 end 1415 return occupant.role == filter or jid_resource(occupant.nick):find(filter, 1, true);
1414 local nick = jid_resource(nick_jid); 1416 end);
1415 if filter == nil or occupant.role == filter or nick:find(filter, 1, true) then 1417 end
1416 print(row(occupant)); 1418 local displayed = #occupants;
1417 displayed = displayed + 1; 1419 occupants:sort(function(a, b)
1418 end 1420 if a.role ~= b.role then
1419 total = total + 1 1421 return muc_util.valid_roles[a.role] > muc_util.valid_roles[b.role];
1422 else
1423 return a.bare_jid < b.bare_jid;
1424 end
1425 end);
1426
1427 if displayed == 0 then
1428 return true, ("%d out of %d occupant%s listed"):format(displayed, total, total ~= 1 and "s" or "")
1429 end
1430
1431 print(row());
1432 for _, occupant in ipairs(occupants) do
1433 print(row(occupant));
1420 end 1434 end
1421 1435
1422 if total == displayed then 1436 if total == displayed then
1423 return true, ("%d occupant%s listed"):format(total, total ~= 1 and "s" or "") 1437 return true, ("%d occupant%s listed"):format(total, total ~= 1 and "s" or "")
1424 else 1438 else
1436 local row = format_table({ 1450 local row = format_table({
1437 { title = "Affiliation"; width = 12 }; -- longest affiliation name 1451 { title = "Affiliation"; width = 12 }; -- longest affiliation name
1438 { title = "JID"; width = "75%" }; 1452 { title = "JID"; width = "75%" };
1439 { title = "Nickname"; width = "25%"; key = "reserved_nickname" }; 1453 { title = "Nickname"; width = "25%"; key = "reserved_nickname" };
1440 }, self.session.width); 1454 }, self.session.width);
1441 local total, displayed = 0, 0; 1455 local affiliated = array();
1442 for affiliated_jid, affiliation, affiliation_data in room_obj:each_affiliation() do 1456 for affiliated_jid, affiliation, affiliation_data in room_obj:each_affiliation() do
1443 if total == 0 then 1457 affiliated:push(setmetatable({ affiliation; affiliated_jid }, { __index = affiliation_data }));
1444 print(row()); 1458 end
1445 end 1459
1446 if filter == nil or affiliation == filter or affiliated_jid:find(filter, 1, true) then 1460 local total = #affiliated;
1447 print(row(setmetatable({ affiliation; affiliated_jid }, { __index = affiliation_data }))) 1461 if filter then
1448 displayed = displayed + 1; 1462 affiliated:filter(function(affiliation)
1449 end 1463 return filter == affiliation[1] or filter == affiliation[2];
1450 total = total + 1 1464 end);
1451 end 1465 end
1466 local displayed = #affiliated;
1467 local aff_ranking = muc_util.valid_affiliations;
1468 affiliated:sort(function(a, b)
1469 if a[1] ~= b[1] then
1470 return aff_ranking[a[1]] > aff_ranking[b[1]];
1471 else
1472 return a[2] < b[2];
1473 end
1474 end);
1475
1476 if displayed == 0 then
1477 return true, ("%d out of %d affiliations%s listed"):format(displayed, total, total ~= 1 and "s" or "")
1478 end
1479
1480 print(row());
1481 for _, affiliation in ipairs(affiliated) do
1482 print(row(affiliation));
1483 end
1484
1452 1485
1453 if total == displayed then 1486 if total == displayed then
1454 return true, ("%d affiliation%s listed"):format(total, total ~= 1 and "s" or "") 1487 return true, ("%d affiliation%s listed"):format(total, total ~= 1 and "s" or "")
1455 else 1488 else
1456 return true, ("%d out of %d affiliation%s listed"):format(displayed, total, total ~= 1 and "s" or "") 1489 return true, ("%d out of %d affiliation%s listed"):format(displayed, total, total ~= 1 and "s" or "")