refator(socket.io): deprecate socketGroups.getChatGroups in favour of api.admin.listGroups

This commit is contained in:
Julian Lam
2023-11-08 15:10:44 -05:00
parent 181a93996c
commit dc4cc74fbd
9 changed files with 73 additions and 11 deletions

View File

@@ -218,14 +218,16 @@ paths:
$ref: 'write/admin/analytics.yaml'
/admin/analytics/{set}:
$ref: 'write/admin/analytics/set.yaml'
/admin/chats/{roomId}:
$ref: 'write/admin/chats/roomId.yaml'
/admin/tokens:
$ref: 'write/admin/tokens.yaml'
/admin/tokens/{token}:
$ref: 'write/admin/tokens/token.yaml'
/admin/tokens/{token}/roll:
$ref: 'write/admin/tokens/token/roll.yaml'
/admin/chats/{roomId}:
$ref: 'write/admin/chats/roomId.yaml'
/admin/groups:
$ref: 'write/admin/groups.yaml'
/files/:
$ref: 'write/files.yaml'
/files/folder:

View File

@@ -0,0 +1,22 @@
get:
tags:
- admin
summary: list all groups
description: This operation returns a full list of user groups, including hidden groups.
responses:
'200':
description: user groups successfully listed
content:
application/json:
schema:
type: object
properties:
status:
$ref: ../../components/schemas/Status.yaml#/Status
response:
type: object
properties:
groups:
type: array
items:
$ref: ../../components/schemas/GroupObject.yaml#/GroupDataObject

View File

@@ -12,11 +12,15 @@ define('forum/chats/create', [
async function handleCreate() {
let groups = [];
if (app.user.isAdmin) {
groups = await socket.emit('groups.getChatGroups', {});
({ groups } = await api.get('/admin/groups'));
groups.sort((a, b) => b.system - a.system).map((g) => {
const { name, displayName } = g;
return { name, displayName };
});
}
const html = await app.parseAndTranslate('modals/create-room', {
user: app.user,
groups: groups,
groups,
});
const modal = bootbox.dialog({

View File

@@ -12,7 +12,12 @@ define('forum/chats/manage', [
buttonEl.on('click', async function () {
let groups = [];
if (app.user.isAdmin) {
groups = await socket.emit('groups.getChatGroups', {});
({ groups } = await api.get('/admin/groups'));
groups.sort((a, b) => b.system - a.system).map((g) => {
const { name, displayName } = g;
return { name, displayName };
});
if (Array.isArray(ajaxify.data.groups)) {
groups.forEach((g) => {
g.selected = ajaxify.data.groups.includes(g.name);

View File

@@ -3,6 +3,7 @@
const meta = require('../meta');
const analytics = require('../analytics');
const privileges = require('../privileges');
const groups = require('../groups');
const adminApi = module.exports;
@@ -34,3 +35,11 @@ adminApi.getAnalyticsData = async (caller, { set, until, amount, units }) => {
const getStats = units === 'days' ? analytics.getDailyStatsForSet : analytics.getHourlyStatsForSet;
return await getStats(`analytics:${set}`, parseInt(until, 10) || Date.now(), amount);
};
adminApi.listGroups = async () => {
// N.B. Returns all groups, even hidden. Beware of leakage.
// Access control handled at controller level
const payload = await groups.getNonPrivilegeGroups('groups:createtime', 0, -1, { ephemeral: false });
return { groups: payload };
};

View File

@@ -78,3 +78,7 @@ Admin.chats.deleteRoom = async (req, res) => {
});
helpers.formatApiResponse(200, res);
};
Admin.listGroups = async (req, res) => {
helpers.formatApiResponse(200, res, await api.admin.listGroups());
};

View File

@@ -84,9 +84,19 @@ Groups.getGroupsBySort = async function (sort, start, stop) {
return await Groups.getGroupsFromSet(set, start, stop);
};
Groups.getNonPrivilegeGroups = async function (set, start, stop) {
Groups.getNonPrivilegeGroups = async function (set, start, stop, flags) {
if (!flags) {
flags = {
ephemeral: true,
};
}
let groupNames = await db.getSortedSetRevRange(set, start, stop);
groupNames = groupNames.concat(Groups.ephemeralGroups).filter(groupName => !Groups.isPrivilegeGroup(groupName));
groupNames = groupNames.filter(groupName => !Groups.isPrivilegeGroup(groupName));
if (flags.ephemeral) {
groupNames = groupNames.concat(Groups.ephemeralGroups);
}
const groupsData = await Groups.getGroupsData(groupNames);
return groupsData.filter(Boolean);
};

View File

@@ -23,5 +23,7 @@ module.exports = function () {
setupApiRoute(router, 'delete', '/chats/:roomId', [...middlewares, middleware.assert.room], controllers.write.admin.chats.deleteRoom);
setupApiRoute(router, 'get', '/groups', [...middlewares], controllers.write.admin.listGroups);
return router;
};

View File

@@ -65,14 +65,18 @@ SocketGroups.loadMoreMembers = async (socket, data) => {
};
SocketGroups.getChatGroups = async (socket) => {
sockets.warnDeprecated(socket, 'GET /api/v3/admin/groups');
const isAdmin = await user.isAdministrator(socket.uid);
if (!isAdmin) {
throw new Error('[[error:no-privileges]]');
}
const allGroups = await groups.getNonPrivilegeGroups('groups:createtime', 0, -1);
const groupsList = allGroups.filter(g => !groups.ephemeralGroups.includes(g.name));
groupsList.sort((a, b) => b.system - a.system);
return groupsList.map(g => ({ name: g.name, displayName: g.displayName }));
const { groups } = await api.admin.listGroups(socket);
// Float system groups to top and return only name/displayName
groups.sort((a, b) => b.system - a.system);
return groups.map(g => ({ name: g.name, displayName: g.displayName }));
};
SocketGroups.cover = {};