Changeset

4362:116c88c28532

mod_http_admin_api: restructure group-related info in API - Return the members of the group right in the get_group_by_id call. This is an O(1) of extra work. - Remove the groups attribute from get_user_by_name as that is O(n) of work and rarely immediately needed. The replacement for the group membership information in the user is for now to use the group API and iterate; future work may fix that.
author Jonas Schäfer <jonas@wielicki.name>
date Wed, 20 Jan 2021 15:30:29 +0100
parents 4361:7f1f3b79d991
children 4363:636d56bbad97
files mod_http_admin_api/mod_http_admin_api.lua
diffstat 1 files changed, 23 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/mod_http_admin_api/mod_http_admin_api.lua	Wed Jan 20 12:51:33 2021 +0000
+++ b/mod_http_admin_api/mod_http_admin_api.lua	Wed Jan 20 15:30:29 2021 +0100
@@ -155,6 +155,13 @@
 		end
 	end
 
+	return {
+		username = username;
+		display_name = display_name;
+	};
+end
+
+local function get_user_groups(username)
 	local groups;
 	do
 		local group_set = group_memberships:get_all(username);
@@ -165,12 +172,7 @@
 			end
 		end
 	end
-
-	return {
-		username = username;
-		display_name = display_name;
-		groups = groups;
-	};
+	return groups;
 end
 
 function list_users(event)
@@ -184,6 +186,19 @@
 end
 
 function get_user_by_name(event, username)
+	local property
+	do
+		local name, sub_path = username:match("^([^/]+)/(%w+)$");
+		if name then
+			username = name;
+			property = sub_path;
+		end
+	end
+
+	if property == "groups" then
+		return json.encode(get_user_groups(username));
+	end
+
 	local user_info = get_user_info(username);
 	if not user_info then
 		return 404;
@@ -206,6 +221,7 @@
 		table.insert(group_list, {
 			id = group_id;
 			name = group_id;
+			members = group_store:get(group_id);
 		});
 	end
 
@@ -214,15 +230,6 @@
 end
 
 function get_group_by_id(event, group_id)
-	local property;
-	do
-		local id, sub_path = group_id:match("^[^/]+/(%w+)$");
-		if id then
-			group_id = id;
-			property = sub_path;
-		end
-	end
-
 	local group = group_store:get(group_id);
 	if not group then
 		return 404;
@@ -230,13 +237,10 @@
 
 	event.response.headers["Content-Type"] = json_content_type;
 
-	if property == "members" then
-		return json.encode(group);
-	end
-
 	return json.encode({
 		id = group_id;
 		name = group_id;
+		members = group;
 	});
 end