Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 46 additions & 14 deletions happyview-modules/lua/endpoints/getAcknowledgement.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ local function scalar(params, key)
return tostring(value)
end

-- luacheck: push ignore 211
-- Like scalar(), but a boolean comes back as "true" or "false": HappyView can hand
-- boolean-typed query parameters to Lua as booleans rather than strings.
local function scalar_or_boolean(values, key)
if type(values[key]) == "boolean" then return tostring(values[key]) end
return scalar(values, key)
end
-- luacheck: pop

local function valid_record_key(value)
return #value >= 1 and #value <= 512 and value ~= "." and value ~= ".."
and not value:find("[^%w_~%.:%-]")
Expand All @@ -39,10 +48,43 @@ local function valid_record_uri(value)
return true, collection, authority
end

-- luacheck: push ignore 211
-- For user-supplied filter values: bounds the size before parsing.
local function valid_bounded_record_uri(value)
if type(value) ~= "string" or #value > 8192 then return false end
return valid_record_uri(value)
end
-- luacheck: pop

local NULL = json.decode("null")

local function record_view(row)
return {
uri = row.uri,
cid = row.cid,
indexedAt = row.indexed_at == nil and NULL or row.indexed_at,
did = row.did,
record = json.decode(row.record),
}
end

-- luacheck: push ignore 211
-- Like record_view, but a row without indexed_at has no indexedAt key instead of JSON null.
-- Pass it to hydrate_actor_views for sidecars that preserve the omission.
local function record_view_omitting_indexed_at(row)
return {
uri = row.uri,
cid = row.cid,
indexedAt = row.indexed_at,
did = row.did,
record = json.decode(row.record),
}
end
-- luacheck: pop

local ACKNOWLEDGEMENT = "org.hypercerts.context.acknowledgement"
local PROFILE = "app.certified.actor.profile"
local ORGANIZATION = "app.certified.actor.organization"
local NULL = json.decode("null")

local function acknowledgement_query(sql, values)
if db.backend() ~= "postgres" then
Expand All @@ -55,16 +97,6 @@ local function acknowledgement_query(sql, values)
return result
end

local function acknowledgement_record_view(row)
return {
uri = row.uri,
cid = row.cid,
indexedAt = row.indexed_at or NULL,
did = row.did,
record = json.decode(row.record),
}
end

local function hydrate_acknowledgement_authors(views)
local dids, seen = {}, {}
for _, view in ipairs(views) do
Expand Down Expand Up @@ -93,8 +125,8 @@ local function hydrate_acknowledgement_authors(views)
for _, view in ipairs(views) do
view.author = {
did = view.did,
profile = profiles[view.did] and acknowledgement_record_view(profiles[view.did]) or NULL,
organization = organizations[view.did] and acknowledgement_record_view(organizations[view.did]) or NULL,
profile = profiles[view.did] and record_view(profiles[view.did]) or NULL,
organization = organizations[view.did] and record_view(organizations[view.did]) or NULL,
}
end
end
Expand All @@ -113,7 +145,7 @@ function handle()
{ ACKNOWLEDGEMENT, uri })
if #rows == 0 then error("RecordNotFound: acknowledgement record is not indexed", 0) end

local acknowledgement = acknowledgement_record_view(rows[1])
local acknowledgement = record_view(rows[1])
hydrate_acknowledgement_authors({ acknowledgement })
return { acknowledgement = acknowledgement }
end
17 changes: 17 additions & 0 deletions happyview-modules/lua/endpoints/getActivity.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ local function valid_record_uri(value)
return true, collection, authority
end

-- luacheck: push ignore 211
-- For user-supplied filter values: bounds the size before parsing.
local function valid_bounded_record_uri(value)
if type(value) ~= "string" or #value > 8192 then return false end
return valid_record_uri(value)
end
-- luacheck: pop

local ACTIVITY_PROJECTION_CONTRIBUTOR_INFORMATION = "org.hypercerts.claim.contributorInformation"
local ACTIVITY_PROJECTION_PROFILE = "app.certified.actor.profile"
local ACTIVITY_PROJECTION_ORGANIZATION = "app.certified.actor.organization"
Expand Down Expand Up @@ -217,6 +225,15 @@ local function scalar(params, key)
return tostring(value)
end

-- luacheck: push ignore 211
-- Like scalar(), but a boolean comes back as "true" or "false": HappyView can hand
-- boolean-typed query parameters to Lua as booleans rather than strings.
local function scalar_or_boolean(values, key)
if type(values[key]) == "boolean" then return tostring(values[key]) end
return scalar(values, key)
end
-- luacheck: pop

local ACTIVITY = "org.hypercerts.claim.activity"

local function query(sql, values)
Expand Down
39 changes: 36 additions & 3 deletions happyview-modules/lua/endpoints/getActor.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ local function scalar(params, key)
return tostring(value)
end

-- luacheck: push ignore 211
-- Like scalar(), but a boolean comes back as "true" or "false": HappyView can hand
-- boolean-typed query parameters to Lua as booleans rather than strings.
local function scalar_or_boolean(values, key)
if type(values[key]) == "boolean" then return tostring(values[key]) end
return scalar(values, key)
end
-- luacheck: pop

local function valid_record_key(value)
return #value >= 1 and #value <= 512 and value ~= "." and value ~= ".."
and not value:find("[^%w_~%.:%-]")
Expand All @@ -39,6 +48,14 @@ local function valid_record_uri(value)
return true, collection, authority
end

-- luacheck: push ignore 211
-- For user-supplied filter values: bounds the size before parsing.
local function valid_bounded_record_uri(value)
if type(value) ~= "string" or #value > 8192 then return false end
return valid_record_uri(value)
end
-- luacheck: pop

local NULL = json.decode("null")

local function record_view(row)
Expand All @@ -51,10 +68,26 @@ local function record_view(row)
}
end

-- luacheck: push ignore 211
-- Like record_view, but a row without indexed_at has no indexedAt key instead of JSON null.
-- Pass it to hydrate_actor_views for sidecars that preserve the omission.
local function record_view_omitting_indexed_at(row)
return {
uri = row.uri,
cid = row.cid,
indexedAt = row.indexed_at,
did = row.did,
record = json.decode(row.record),
}
end
-- luacheck: pop

local PROFILE = "app.certified.actor.profile"
local ORGANIZATION = "app.certified.actor.organization"

local function hydrate_actor_views(actors, run_query)
-- view(row) builds each profile/organization sidecar; it defaults to record_view.
local function hydrate_actor_views(actors, run_query, view)
view = view or record_view
if #actors == 0 then return end
local dids, seen = {}, {}
for _, actor in ipairs(actors) do
Expand All @@ -76,8 +109,8 @@ local function hydrate_actor_views(actors, run_query)
load(PROFILE, profiles)
load(ORGANIZATION, organizations)
for _, actor in ipairs(actors) do
actor.profile = profiles[actor.did] and record_view(profiles[actor.did]) or NULL
actor.organization = organizations[actor.did] and record_view(organizations[actor.did]) or NULL
actor.profile = profiles[actor.did] and view(profiles[actor.did]) or NULL
actor.organization = organizations[actor.did] and view(organizations[actor.did]) or NULL
end
end

Expand Down
123 changes: 86 additions & 37 deletions happyview-modules/lua/endpoints/getActorLikes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ local function scalar(params, key)
return tostring(value)
end

-- luacheck: push ignore 211
-- Like scalar(), but a boolean comes back as "true" or "false": HappyView can hand
-- boolean-typed query parameters to Lua as booleans rather than strings.
local function scalar_or_boolean(values, key)
if type(values[key]) == "boolean" then return tostring(values[key]) end
return scalar(values, key)
end
-- luacheck: pop

local function valid_record_key(value)
return #value >= 1 and #value <= 512 and value ~= "." and value ~= ".."
and not value:find("[^%w_~%.:%-]")
Expand All @@ -39,6 +48,14 @@ local function valid_record_uri(value)
return true, collection, authority
end

-- luacheck: push ignore 211
-- For user-supplied filter values: bounds the size before parsing.
local function valid_bounded_record_uri(value)
if type(value) ~= "string" or #value > 8192 then return false end
return valid_record_uri(value)
end
-- luacheck: pop

local function valid_datetime(value)
if type(value) ~= "string" then return false end
local year, month, day, hour, minute, second, suffix = value:match(
Expand Down Expand Up @@ -71,17 +88,62 @@ local function parse_list_limit(params)
return limit
end

local function parse_sort_direction(params)
local direction = scalar(params, "sortDirection") or "desc"
if direction ~= "asc" and direction ~= "desc" then invalid("sortDirection must be 'asc' or 'desc'") end
return direction
-- The hex prologue every list cursor shape shares: rejects empty, over-long (8192),
-- odd-length and non-lowercase-hex tokens with "cursor is malformed" and returns the
-- decoded JSON text. Shape and JSON checks stay with each cursor's own decoder.
local function decode_hex_cursor_token(token)
if #token == 0 or #token > 8192 or #token % 2 ~= 0 or token:find("[^0-9a-f]") then
invalid("cursor is malformed")
end
return (token:gsub("..", function(pair) return string.char(tonumber(pair, 16)) end))
end

-- Hex-encodes the JSON of a cursor table.
local function cursor_encode(value)
local encoded = json.encode(value)
return (encoded:gsub(".", function(char) return string.format("%02x", string.byte(char)) end))
end

-- luacheck: push ignore 211
-- Decodes a hex-encoded {v, d, t, u} list cursor. Rejects malformed tokens, other
-- sort directions and unknown keys; validate_value(value) -> boolean runs the
-- endpoint-specific check on t and u. Not for endpoints that skip the 8192-character
-- cap or accept extra keys.
local function decode_cursor(token, direction, validate_value)
if token == nil then return nil end
local decoded = decode_hex_cursor_token(token)
local ok, value = pcall(json.decode, decoded)
if not ok or type(value) ~= "table" or value.v ~= 1 or value.d ~= direction
or type(value.t) ~= "string" or type(value.u) ~= "string" then
invalid("cursor is malformed or belongs to another sortDirection")
end
for key in pairs(value) do
if key ~= "v" and key ~= "d" and key ~= "t" and key ~= "u" then invalid("cursor is malformed") end
end
if not validate_value(value) then invalid("cursor is malformed") end
return value
end

-- decode_cursor for the common case: t is a datetime and u a record URI.
-- record_uri is the collection NSID u must belong to, or a predicate(uri) -> boolean
-- for endpoints with stricter URI rules. valid_t(t) defaults to valid_datetime.
local function decode_record_cursor(token, direction, record_uri, valid_t)
valid_t = valid_t or valid_datetime
return decode_cursor(token, direction, function(value)
if not valid_t(value.t) then return false end
if type(record_uri) == "function" then return record_uri(value.u) end
local valid, collection = valid_record_uri(value.u)
return valid and collection == record_uri
end)
end
-- luacheck: pop

local function parse_sort_direction(params)
local direction = scalar(params, "sortDirection") or "desc"
if direction ~= "asc" and direction ~= "desc" then invalid("sortDirection must be 'asc' or 'desc'") end
return direction
end

local NULL = json.decode("null")

local function record_view(row)
Expand All @@ -94,10 +156,26 @@ local function record_view(row)
}
end

-- luacheck: push ignore 211
-- Like record_view, but a row without indexed_at has no indexedAt key instead of JSON null.
-- Pass it to hydrate_actor_views for sidecars that preserve the omission.
local function record_view_omitting_indexed_at(row)
return {
uri = row.uri,
cid = row.cid,
indexedAt = row.indexed_at,
did = row.did,
record = json.decode(row.record),
}
end
-- luacheck: pop

local PROFILE = "app.certified.actor.profile"
local ORGANIZATION = "app.certified.actor.organization"

local function hydrate_actor_views(actors, run_query)
-- view(row) builds each profile/organization sidecar; it defaults to record_view.
local function hydrate_actor_views(actors, run_query, view)
view = view or record_view
if #actors == 0 then return end
local dids, seen = {}, {}
for _, actor in ipairs(actors) do
Expand All @@ -119,31 +197,9 @@ local function hydrate_actor_views(actors, run_query)
load(PROFILE, profiles)
load(ORGANIZATION, organizations)
for _, actor in ipairs(actors) do
actor.profile = profiles[actor.did] and record_view(profiles[actor.did]) or NULL
actor.organization = organizations[actor.did] and record_view(organizations[actor.did]) or NULL
end
end

-- Decodes a hex-encoded {v, d, t, u} list cursor. Rejects malformed tokens, other
-- sort directions and unknown keys; validate_value(value) -> boolean runs the
-- endpoint-specific check on t and u. Not for endpoints that skip the 8192-character
-- cap or accept extra keys.
local function decode_cursor(token, direction, validate_value)
if token == nil then return nil end
if #token == 0 or #token > 8192 or #token % 2 ~= 0 or token:find("[^0-9a-f]") then
invalid("cursor is malformed")
end
local decoded = token:gsub("..", function(pair) return string.char(tonumber(pair, 16)) end)
local ok, value = pcall(json.decode, decoded)
if not ok or type(value) ~= "table" or value.v ~= 1 or value.d ~= direction
or type(value.t) ~= "string" or type(value.u) ~= "string" then
invalid("cursor is malformed or belongs to another sortDirection")
actor.profile = profiles[actor.did] and view(profiles[actor.did]) or NULL
actor.organization = organizations[actor.did] and view(organizations[actor.did]) or NULL
end
for key in pairs(value) do
if key ~= "v" and key ~= "d" and key ~= "t" and key ~= "u" then invalid("cursor is malformed") end
end
if not validate_value(value) then invalid("cursor is malformed") end
return value
end

local CREATED_AT_SORT_PATTERN = "^[0-9]{4}-[0-9]{2}-[0-9]{2}T([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]([.][0-9]+)?(Z|[+-]([01][0-9]|2[0-3]):[0-5][0-9])$"
Expand Down Expand Up @@ -183,13 +239,6 @@ local function feed_reaction_query(reaction, sql, values)
return result
end

local function feed_reaction_decode_cursor(token, direction, collection)
return decode_cursor(token, direction, function(value)
local valid, cursor_collection = valid_record_uri(value.u)
return valid and cursor_collection == collection and valid_datetime(value.t)
end)
end

local function feed_reaction_query_page(reaction, mode, identity, limit, cursor, direction)
local filters, values = { "collection = $1" }, { reaction.collection, identity }
if mode == "subject" then
Expand Down Expand Up @@ -263,7 +312,7 @@ local function feed_reaction_list(reaction_name, mode)

local limit = parse_list_limit(params)
local direction = parse_sort_direction(params)
local cursor = feed_reaction_decode_cursor(scalar(params, "cursor"), direction, reaction.collection)
local cursor = decode_record_cursor(scalar(params, "cursor"), direction, reaction.collection)
local sql, values = feed_reaction_query_page(reaction, mode, identity, limit, cursor, direction)
local rows = feed_reaction_query(reaction, sql, values)

Expand Down
Loading
Loading