Mercurial > prosody-modules
comparison mod_http_admin_api/mod_http_admin_api.lua @ 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 | 7f1f3b79d991 |
children | 636d56bbad97 |
comparison
equal
deleted
inserted
replaced
4361:7f1f3b79d991 | 4362:116c88c28532 |
---|---|
153 if ok and nick_item then | 153 if ok and nick_item then |
154 display_name = nick_item:get_child_text("nick", "http://jabber.org/protocol/nick"); | 154 display_name = nick_item:get_child_text("nick", "http://jabber.org/protocol/nick"); |
155 end | 155 end |
156 end | 156 end |
157 | 157 |
158 return { | |
159 username = username; | |
160 display_name = display_name; | |
161 }; | |
162 end | |
163 | |
164 local function get_user_groups(username) | |
158 local groups; | 165 local groups; |
159 do | 166 do |
160 local group_set = group_memberships:get_all(username); | 167 local group_set = group_memberships:get_all(username); |
161 if group_set and next(group_set) then | 168 if group_set and next(group_set) then |
162 groups = {}; | 169 groups = {}; |
163 for group_id in pairs(group_set) do | 170 for group_id in pairs(group_set) do |
164 table.insert(groups, group_id); | 171 table.insert(groups, group_id); |
165 end | 172 end |
166 end | 173 end |
167 end | 174 end |
168 | 175 return groups; |
169 return { | |
170 username = username; | |
171 display_name = display_name; | |
172 groups = groups; | |
173 }; | |
174 end | 176 end |
175 | 177 |
176 function list_users(event) | 178 function list_users(event) |
177 local user_list = {}; | 179 local user_list = {}; |
178 for username in usermanager.users(module.host) do | 180 for username in usermanager.users(module.host) do |
182 event.response.headers["Content-Type"] = json_content_type; | 184 event.response.headers["Content-Type"] = json_content_type; |
183 return json.encode_array(user_list); | 185 return json.encode_array(user_list); |
184 end | 186 end |
185 | 187 |
186 function get_user_by_name(event, username) | 188 function get_user_by_name(event, username) |
189 local property | |
190 do | |
191 local name, sub_path = username:match("^([^/]+)/(%w+)$"); | |
192 if name then | |
193 username = name; | |
194 property = sub_path; | |
195 end | |
196 end | |
197 | |
198 if property == "groups" then | |
199 return json.encode(get_user_groups(username)); | |
200 end | |
201 | |
187 local user_info = get_user_info(username); | 202 local user_info = get_user_info(username); |
188 if not user_info then | 203 if not user_info then |
189 return 404; | 204 return 404; |
190 end | 205 end |
191 | 206 |
204 local group_list = {}; | 219 local group_list = {}; |
205 for group_id in group_store:users() do | 220 for group_id in group_store:users() do |
206 table.insert(group_list, { | 221 table.insert(group_list, { |
207 id = group_id; | 222 id = group_id; |
208 name = group_id; | 223 name = group_id; |
224 members = group_store:get(group_id); | |
209 }); | 225 }); |
210 end | 226 end |
211 | 227 |
212 event.response.headers["Content-Type"] = json_content_type; | 228 event.response.headers["Content-Type"] = json_content_type; |
213 return json.encode_array(group_list); | 229 return json.encode_array(group_list); |
214 end | 230 end |
215 | 231 |
216 function get_group_by_id(event, group_id) | 232 function get_group_by_id(event, group_id) |
217 local property; | |
218 do | |
219 local id, sub_path = group_id:match("^[^/]+/(%w+)$"); | |
220 if id then | |
221 group_id = id; | |
222 property = sub_path; | |
223 end | |
224 end | |
225 | |
226 local group = group_store:get(group_id); | 233 local group = group_store:get(group_id); |
227 if not group then | 234 if not group then |
228 return 404; | 235 return 404; |
229 end | 236 end |
230 | 237 |
231 event.response.headers["Content-Type"] = json_content_type; | 238 event.response.headers["Content-Type"] = json_content_type; |
232 | |
233 if property == "members" then | |
234 return json.encode(group); | |
235 end | |
236 | 239 |
237 return json.encode({ | 240 return json.encode({ |
238 id = group_id; | 241 id = group_id; |
239 name = group_id; | 242 name = group_id; |
243 members = group; | |
240 }); | 244 }); |
241 end | 245 end |
242 | 246 |
243 function create_group(event) | 247 function create_group(event) |
244 local request = event.request; | 248 local request = event.request; |