Mercurial > prosody-modules
comparison mod_http_admin_api/mod_http_admin_api.lua @ 6330:4e63151010bf
mod_http_admin_api: Convert remaining HTTP errors to util.error
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Wed, 01 Oct 2025 12:00:09 +0100 |
| parents | 5571c75a4fed |
| children | 6a3b38051d74 |
comparison
equal
deleted
inserted
replaced
| 6329:5571c75a4fed | 6330:4e63151010bf |
|---|---|
| 132 end | 132 end |
| 133 | 133 |
| 134 function get_invite_by_id(event, invite_id) | 134 function get_invite_by_id(event, invite_id) |
| 135 local invite = invites.get_account_invite_info(invite_id); | 135 local invite = invites.get_account_invite_info(invite_id); |
| 136 if not invite then | 136 if not invite then |
| 137 return 404; | 137 return nil, "item-not-found"; |
| 138 end | 138 end |
| 139 | 139 |
| 140 event.response.headers["Content-Type"] = json_content_type; | 140 event.response.headers["Content-Type"] = json_content_type; |
| 141 return json.encode(token_info_to_invite_info(invite)); | 141 return json.encode(token_info_to_invite_info(invite)); |
| 142 end | 142 end |
| 146 | 146 |
| 147 local request = event.request; | 147 local request = event.request; |
| 148 if request.body and #request.body > 0 then | 148 if request.body and #request.body > 0 then |
| 149 if request.headers.content_type ~= json_content_type then | 149 if request.headers.content_type ~= json_content_type then |
| 150 module:log("warn", "Invalid content type"); | 150 module:log("warn", "Invalid content type"); |
| 151 return 400; | 151 return nil, "invalid-json"; |
| 152 end | 152 end |
| 153 options = json.decode(event.request.body); | 153 options = json.decode(event.request.body); |
| 154 if not options then | 154 if not options then |
| 155 module:log("warn", "Invalid JSON"); | 155 module:log("warn", "Invalid JSON"); |
| 156 return 400; | 156 return nil, "invalid-json"; |
| 157 end | 157 end |
| 158 else | 158 else |
| 159 options = {}; | 159 options = {}; |
| 160 end | 160 end |
| 161 | 161 |
| 162 local source = event.session.username .. "@" .. module.host .. "/admin_api"; | 162 local source = event.session.username .. "@" .. module.host .. "/admin_api"; |
| 163 | 163 |
| 164 local invite; | 164 local invite; |
| 165 if invite_type == "reset" then | 165 if invite_type == "reset" then |
| 166 if not options.username then | 166 if not options.username then |
| 167 return 400; | 167 return nil, "bad-request"; |
| 168 end | 168 end |
| 169 invite = invites.create_account_reset(options.username, options.ttl); | 169 invite = invites.create_account_reset(options.username, options.ttl); |
| 170 elseif invite_type == "group" then | 170 elseif invite_type == "group" then |
| 171 if not options.groups then | 171 if not options.groups then |
| 172 return 400; | 172 return nil, "bad-request"; |
| 173 end | 173 end |
| 174 invite = invites.create_group(options.groups, { | 174 invite = invites.create_group(options.groups, { |
| 175 source = source; | 175 source = source; |
| 176 roles = options.roles; | 176 roles = options.roles; |
| 177 note = options.note; | 177 note = options.note; |
| 192 event.response.headers["Content-Type"] = json_content_type; | 192 event.response.headers["Content-Type"] = json_content_type; |
| 193 return json.encode(token_info_to_invite_info(invite)); | 193 return json.encode(token_info_to_invite_info(invite)); |
| 194 end | 194 end |
| 195 | 195 |
| 196 function delete_invite(event, invite_id) --luacheck: ignore 212/event | 196 function delete_invite(event, invite_id) --luacheck: ignore 212/event |
| 197 if not invites.delete_account_invite(invite_id) then | 197 local ok, err = invites.delete_account_invite(invite_id); |
| 198 return 404; | 198 if not ok then |
| 199 return nil, err; | |
| 199 end | 200 end |
| 200 return 200; | 201 return 200; |
| 201 end | 202 end |
| 202 | 203 |
| 203 local function get_user_avatar_info(username) | 204 local function get_user_avatar_info(username) |
| 441 return json.encode(get_user_debug_info(username)); | 442 return json.encode(get_user_debug_info(username)); |
| 442 end | 443 end |
| 443 | 444 |
| 444 local user_info = get_user_info(username); | 445 local user_info = get_user_info(username); |
| 445 if not user_info then | 446 if not user_info then |
| 446 return 404; | 447 return nil, "user-not-found"; |
| 447 end | 448 end |
| 448 | 449 |
| 449 event.response.headers["Content-Type"] = json_content_type; | 450 event.response.headers["Content-Type"] = json_content_type; |
| 450 return json.encode(user_info); | 451 return json.encode(user_info); |
| 451 end | 452 end |
| 486 end | 487 end |
| 487 | 488 |
| 488 local updated_attributes = set.new(array.collect(it.keys(new_user))); | 489 local updated_attributes = set.new(array.collect(it.keys(new_user))); |
| 489 if not (updated_attributes - writable_user_attributes):empty() then | 490 if not (updated_attributes - writable_user_attributes):empty() then |
| 490 module:log("warn", "Unable to service PATCH user request, unsupported attributes: %s", (updated_attributes - writable_user_attributes)); | 491 module:log("warn", "Unable to service PATCH user request, unsupported attributes: %s", (updated_attributes - writable_user_attributes)); |
| 491 return 400; | 492 return nil, "feature-not-implemented"; |
| 492 end | 493 end |
| 493 | 494 |
| 494 if new_user.enabled ~= nil and new_user.enabled ~= current_user.enabled then | 495 if new_user.enabled ~= nil and new_user.enabled ~= current_user.enabled then |
| 495 local ok, err = user_attribute_writers.enabled(username, new_user.enabled); | 496 local ok, err = user_attribute_writers.enabled(username, new_user.enabled); |
| 496 if not ok then | 497 if not ok then |
| 501 return 200; | 502 return 200; |
| 502 end | 503 end |
| 503 | 504 |
| 504 function update_user(event, username) | 505 function update_user(event, username) |
| 505 if not username then | 506 if not username then |
| 506 return 400; | 507 return nil, "bad-request"; |
| 507 end | 508 end |
| 508 | 509 |
| 509 local request = event.request; | 510 local request = event.request; |
| 510 if request.headers.content_type ~= json_content_type | 511 if request.headers.content_type ~= json_content_type |
| 511 or (not request.body or #request.body == 0) then | 512 or (not request.body or #request.body == 0) then |
| 512 return 400; | 513 return nil, "invalid-json"; |
| 513 end | 514 end |
| 514 local new_user = json.decode(event.request.body); | 515 local new_user = json.decode(event.request.body); |
| 515 if not new_user then | 516 if not new_user then |
| 516 return 400; | 517 return nil, "invalid-json"; |
| 517 end | 518 end |
| 518 | 519 |
| 519 if new_user.username and new_user.username ~= username then | 520 if new_user.username and new_user.username ~= username then |
| 520 return 400; | 521 return nil, "bad-request"; -- Cannot change the username |
| 521 end | 522 end |
| 522 | 523 |
| 523 if new_user.display_name then | 524 if new_user.display_name then |
| 524 local pep_service = mod_pep.get_pep_service(username); | 525 local pep_service = mod_pep.get_pep_service(username); |
| 525 -- TODO: publish | 526 -- TODO: publish |
| 594 end | 595 end |
| 595 | 596 |
| 596 function get_group_by_id(event, group_id) | 597 function get_group_by_id(event, group_id) |
| 597 local group = mod_groups.get_info(group_id); | 598 local group = mod_groups.get_info(group_id); |
| 598 if not group then | 599 if not group then |
| 599 return 404; | 600 return nil, "group-not-found"; |
| 600 end | 601 end |
| 601 | 602 |
| 602 event.response.headers["Content-Type"] = json_content_type; | 603 event.response.headers["Content-Type"] = json_content_type; |
| 603 | 604 |
| 604 return json.encode({ | 605 return json.encode({ |
| 611 | 612 |
| 612 function create_group(event) | 613 function create_group(event) |
| 613 local request = event.request; | 614 local request = event.request; |
| 614 if request.headers.content_type ~= json_content_type | 615 if request.headers.content_type ~= json_content_type |
| 615 or (not request.body or #request.body == 0) then | 616 or (not request.body or #request.body == 0) then |
| 616 return 400; | 617 return nil, "invalid-json"; |
| 617 end | 618 end |
| 618 local group = json.decode(event.request.body); | 619 local group = json.decode(event.request.body); |
| 619 if not group then | 620 if not group then |
| 620 return 400; | 621 return nil, "invalid-json"; |
| 621 end | 622 end |
| 622 | 623 |
| 623 local create_muc = group.create_muc and true or false; | 624 local create_muc = group.create_muc and true or false; |
| 624 | 625 |
| 625 local group_id, err = mod_groups.create( | 626 local group_id, err = mod_groups.create( |
| 688 -- Add group chat | 689 -- Add group chat |
| 689 local group_id = subpath:match("^([^/]+)/chats$"); | 690 local group_id = subpath:match("^([^/]+)/chats$"); |
| 690 if group_id then | 691 if group_id then |
| 691 local muc_params = json.decode(event.request.body); | 692 local muc_params = json.decode(event.request.body); |
| 692 if not muc_params then | 693 if not muc_params then |
| 693 return 400; | 694 return nil, "invalid-json"; |
| 694 end | 695 end |
| 695 local muc = mod_groups.add_group_chat(group_id, muc_params.name); | 696 local muc, err = mod_groups.add_group_chat(group_id, muc_params.name); |
| 696 if not muc then | 697 if not muc then |
| 697 return 500; | 698 return nil, err; |
| 698 end | 699 end |
| 699 return json.encode(muc); | 700 return json.encode(muc); |
| 700 end | 701 end |
| 701 | 702 |
| 702 return 404; | 703 return nil, "group-not-found"; |
| 703 end | 704 end |
| 704 | 705 |
| 705 function delete_group(event, subpath) --luacheck: ignore 212/event | 706 function delete_group(event, subpath) --luacheck: ignore 212/event |
| 706 -- Check if this is a membership deletion and handle it | 707 -- Check if this is a membership deletion and handle it |
| 707 local group_id, sub_resource_type, sub_resource_id = subpath:match("^([^/]+)/([^/]+)/([^/]+)$"); | 708 local group_id, sub_resource_type, sub_resource_id = subpath:match("^([^/]+)/([^/]+)/([^/]+)$"); |
| 708 if group_id then | 709 if group_id then |
| 709 -- Operation is on a sub-resource | 710 -- Operation is on a sub-resource |
| 710 if sub_resource_type == "members" then | 711 if sub_resource_type == "members" then |
| 711 if mod_groups.remove_member(group_id, sub_resource_id) then | 712 local ok, err = mod_groups.remove_member(group_id, sub_resource_id); |
| 712 return 204; | 713 if not ok then |
| 713 else | 714 return nil, err; |
| 714 return 500; | 715 end |
| 715 end | 716 return 204; |
| 716 elseif sub_resource_type == "chats" then | 717 elseif sub_resource_type == "chats" then |
| 717 if mod_groups.remove_group_chat(group_id, sub_resource_id) then | 718 local ok, err = mod_groups.remove_group_chat(group_id, sub_resource_id); |
| 718 return 204; | 719 if not ok then |
| 719 else | 720 return nil, err; |
| 720 return 500; | 721 end |
| 721 end | 722 return 204; |
| 722 else | 723 else |
| 723 return 404; | 724 return nil, "item-not-found"; |
| 724 end | 725 end |
| 725 else | 726 else |
| 726 -- Action refers to the group | 727 -- Action refers to the group |
| 727 group_id = subpath; | 728 group_id = subpath; |
| 728 end | 729 end |
| 729 | 730 |
| 730 if not group_id then | 731 if not group_id then |
| 731 return 400; | 732 return nil, "bad-request"; |
| 732 end | 733 end |
| 733 | 734 |
| 734 if not mod_groups.exists(group_id) then | 735 if not mod_groups.exists(group_id) then |
| 735 return 404; | 736 return nil, "group-not-found"; |
| 736 end | 737 end |
| 737 | 738 |
| 738 if not mod_groups.delete(group_id) then | 739 local ok, err = mod_groups.delete(group_id); |
| 739 return 500; | 740 if not ok then |
| 741 return nil, err; | |
| 740 end | 742 end |
| 741 return 204; | 743 return 204; |
| 742 end | 744 end |
| 743 | 745 |
| 744 local function get_server_info(event) | 746 local function get_server_info(event) |
| 798 | 800 |
| 799 local function post_server_announcement(event) | 801 local function post_server_announcement(event) |
| 800 local request = event.request; | 802 local request = event.request; |
| 801 if request.headers.content_type ~= json_content_type | 803 if request.headers.content_type ~= json_content_type |
| 802 or (not request.body or #request.body == 0) then | 804 or (not request.body or #request.body == 0) then |
| 803 return 400; | 805 return nil, "invalid-json"; |
| 804 end | 806 end |
| 805 local body = json.decode(event.request.body); | 807 local body = json.decode(event.request.body); |
| 806 if not body then | 808 if not body then |
| 807 return 400; | 809 return nil, "invalid-json"; |
| 808 end | 810 end |
| 809 | 811 |
| 810 if type(body.recipients) ~= "table" and body.recipients ~= "online" and body.recipients ~= "all" then | 812 if type(body.recipients) ~= "table" and body.recipients ~= "online" and body.recipients ~= "all" then |
| 811 return 400; | 813 return nil, "bad-request"; |
| 812 end | 814 end |
| 813 | 815 |
| 814 if not body.body or #body.body == 0 then | 816 if not body.body or #body.body == 0 then |
| 815 return 400; | 817 return nil, "bad-request"; |
| 816 end | 818 end |
| 817 | 819 |
| 818 local message = st.message():tag("body"):text(body.body):up(); | 820 local message = st.message():tag("body"):text(body.body):up(); |
| 819 local host = module.host | 821 local host = module.host |
| 820 message.attr.from = host | 822 message.attr.from = host |
