|
| 1 | +local http = require("http") |
| 2 | +local json = require("json") |
| 3 | +local artifact_repo = require("artifact_repo") |
| 4 | +local session_repo = require("session_repo") |
| 5 | +local security = require("security") |
| 6 | + |
| 7 | +local function handler() |
| 8 | + -- Get response object |
| 9 | + local res = http.response() |
| 10 | + local req = http.request() |
| 11 | + if not res or not req then |
| 12 | + return nil, "Failed to get HTTP context" |
| 13 | + end |
| 14 | + |
| 15 | + -- Security check - ensure user is authenticated |
| 16 | + local actor = security.actor() |
| 17 | + if not actor then |
| 18 | + res:set_status(http.STATUS.UNAUTHORIZED) |
| 19 | + res:set_content_type(http.CONTENT.JSON) |
| 20 | + res:write_json({ |
| 21 | + success = false, |
| 22 | + error = "Authentication required" |
| 23 | + }) |
| 24 | + return |
| 25 | + end |
| 26 | + |
| 27 | + -- Get user ID from the authenticated actor |
| 28 | + local user_id = actor:id() |
| 29 | + |
| 30 | + -- Get artifact ID from URL path |
| 31 | + local artifact_id = req:param("id") |
| 32 | + if not artifact_id or artifact_id == "" then |
| 33 | + res:set_status(http.STATUS.BAD_REQUEST) |
| 34 | + res:set_content_type(http.CONTENT.JSON) |
| 35 | + res:write_json({ |
| 36 | + success = false, |
| 37 | + error = "Missing artifact ID in path" |
| 38 | + }) |
| 39 | + return |
| 40 | + end |
| 41 | + |
| 42 | + -- Fetch the artifact from the repository |
| 43 | + local artifact, err = artifact_repo.get(artifact_id) |
| 44 | + if err then |
| 45 | + -- Handle not found error |
| 46 | + if err:match("not found") then |
| 47 | + res:set_status(http.STATUS.NOT_FOUND) |
| 48 | + res:set_content_type(http.CONTENT.JSON) |
| 49 | + res:write_json({ |
| 50 | + success = false, |
| 51 | + error = "Artifact not found: " .. artifact_id |
| 52 | + }) |
| 53 | + return |
| 54 | + end |
| 55 | + |
| 56 | + -- Handle other errors |
| 57 | + res:set_status(http.STATUS.INTERNAL_ERROR) |
| 58 | + res:set_content_type(http.CONTENT.JSON) |
| 59 | + res:write_json({ |
| 60 | + success = false, |
| 61 | + error = "Failed to retrieve artifact: " .. err |
| 62 | + }) |
| 63 | + return |
| 64 | + end |
| 65 | + |
| 66 | + -- Get the session to check ownership |
| 67 | + local session, session_err = session_repo.get(artifact.session_id) |
| 68 | + if session_err then |
| 69 | + res:set_status(http.STATUS.INTERNAL_ERROR) |
| 70 | + res:set_content_type(http.CONTENT.JSON) |
| 71 | + res:write_json({ |
| 72 | + success = false, |
| 73 | + error = "Failed to verify artifact ownership: " .. session_err |
| 74 | + }) |
| 75 | + return |
| 76 | + end |
| 77 | + |
| 78 | + -- Verify the session belongs to the authenticated user |
| 79 | + if session.user_id ~= user_id then |
| 80 | + res:set_status(http.STATUS.FORBIDDEN) |
| 81 | + res:set_content_type(http.CONTENT.JSON) |
| 82 | + res:write_json({ |
| 83 | + success = false, |
| 84 | + error = "Access denied: You don't have permission to access this artifact" |
| 85 | + }) |
| 86 | + return |
| 87 | + end |
| 88 | + |
| 89 | + -- Convert to a client-friendly format |
| 90 | + local response = { |
| 91 | + uuid = artifact.artifact_id, |
| 92 | + type = artifact.kind, |
| 93 | + title = artifact.title, |
| 94 | + created_at = artifact.created_at, |
| 95 | + updated_at = artifact.updated_at, |
| 96 | + content_version = 1 |
| 97 | + } |
| 98 | + |
| 99 | + -- Include metadata if available |
| 100 | + if artifact.meta then |
| 101 | + response.content_type = artifact.meta.content_type |
| 102 | + response.description = artifact.meta.description |
| 103 | + response.icon = artifact.meta.icon |
| 104 | + response.status = artifact.meta.status |
| 105 | + |
| 106 | + -- Add page reference specific data if this is a view_ref artifact |
| 107 | + if artifact.kind == "view_ref" then |
| 108 | + response.page_id = artifact.meta.page_id |
| 109 | + response.is_view_reference = true |
| 110 | + response.type = artifact.meta.display_type or "standalone" |
| 111 | + |
| 112 | + -- Also get the params if this is a view_ref |
| 113 | + if artifact.content and artifact.content ~= "" then |
| 114 | + local params, decode_err = json.decode(artifact.content) |
| 115 | + if not decode_err then |
| 116 | + response.params = params |
| 117 | + end |
| 118 | + end |
| 119 | + end |
| 120 | + end |
| 121 | + |
| 122 | + -- Return JSON metadata response |
| 123 | + res:set_content_type(http.CONTENT.JSON) |
| 124 | + res:set_status(http.STATUS.OK) |
| 125 | + res:write_json(response) |
| 126 | +end |
| 127 | + |
| 128 | +return { |
| 129 | + handler = handler |
| 130 | +} |
0 commit comments