Skip to content
Merged
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
67 changes: 42 additions & 25 deletions happyview-modules/lua/endpoints/getActorLikes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,43 @@ local function hydrate_actor_views(actors, run_query)
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")
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])$"

-- Sort key for a happyview_records row: the record's own createdAt when it is a
-- valid timestamp, otherwise COALESCE(indexed_at, created_at). `alias` is the
-- table alias; nil selects unqualified columns.
local function created_at_sort(alias)
local prefix = alias and (alias .. ".") or ""
local record, indexed_at, created_at = prefix .. "record", prefix .. "indexed_at", prefix .. "created_at"
local created = record .. "::jsonb->>'createdAt'"
return "CASE WHEN jsonb_typeof(" .. record .. "::jsonb->'createdAt') = 'string' AND " .. created ..
" ~ '" .. CREATED_AT_SORT_PATTERN .. "' AND " .. created .. " !~ '-00:00$' AND pg_input_is_valid(" .. created ..
", 'timestamptz') THEN (" .. created .. ")::timestamptz ELSE COALESCE(" .. indexed_at ..
"::timestamptz, " .. created_at .. "::timestamptz) END"
end

local FEED_REACTIONS = {
likes = {
collection = "app.certified.feed.like",
Expand All @@ -139,12 +176,6 @@ local FEED_REACTIONS = {
},
}

local function feed_reaction_sort_key()
local created = "record::jsonb->>'createdAt'"
local zoned = "^[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])$"
return "CASE WHEN jsonb_typeof(record::jsonb->'createdAt') = 'string' AND " .. created .. " ~ '" .. zoned .. "' AND " .. created .. " !~ '-00:00$' AND pg_input_is_valid(" .. created .. ", 'timestamptz') THEN (" .. created .. ")::timestamptz ELSE COALESCE(indexed_at::timestamptz, created_at::timestamptz) END"
end

local function feed_reaction_query(reaction, sql, values)
if db.backend() ~= "postgres" then error(reaction.failure .. ": reaction queries require PostgreSQL", 0) end
local ok, result = pcall(db.raw, sql, values)
Expand All @@ -153,24 +184,10 @@ local function feed_reaction_query(reaction, sql, values)
end

local function feed_reaction_decode_cursor(token, direction, collection)
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")
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
local valid, cursor_collection = valid_record_uri(value.u)
if not valid or cursor_collection ~= collection or not valid_datetime(value.t) then
invalid("cursor is malformed")
end
return value
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)
Expand All @@ -196,7 +213,7 @@ local function feed_reaction_query_page(reaction, mode, identity, limit, cursor,
local ordering = direction == "asc" and "ASC" or "DESC"
local subject_uri = "record::jsonb->'subject'->>'uri'"
-- Select the earliest representative before applying the caller's independent page direction.
local sql = "WITH ranked_reactions AS (SELECT uri, did, cid, indexed_at::text AS indexed_at, record::text AS record, sorted.sort_at, ROW_NUMBER() OVER (PARTITION BY did, " .. subject_uri .. " ORDER BY sorted.sort_at ASC, uri ASC) AS relationship_rank FROM happyview_records CROSS JOIN LATERAL (SELECT " .. feed_reaction_sort_key() .. " AS sort_at) sorted WHERE " .. table.concat(filters, " AND ") .. "), " ..
local sql = "WITH ranked_reactions AS (SELECT uri, did, cid, indexed_at::text AS indexed_at, record::text AS record, sorted.sort_at, ROW_NUMBER() OVER (PARTITION BY did, " .. subject_uri .. " ORDER BY sorted.sort_at ASC, uri ASC) AS relationship_rank FROM happyview_records CROSS JOIN LATERAL (SELECT " .. created_at_sort() .. " AS sort_at) sorted WHERE " .. table.concat(filters, " AND ") .. "), " ..
"representatives AS (SELECT uri, did, cid, indexed_at, record, sort_at FROM ranked_reactions WHERE relationship_rank = 1), "

local page = "page AS (SELECT uri, did, cid, indexed_at, record, sort_at FROM representatives" .. cursor_filter .. " ORDER BY sort_at " .. ordering .. ", uri " .. ordering .. " LIMIT $" .. #values .. ") "
Expand Down
67 changes: 42 additions & 25 deletions happyview-modules/lua/endpoints/getActorReposts.lua
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,43 @@ local function hydrate_actor_views(actors, run_query)
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")
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])$"

-- Sort key for a happyview_records row: the record's own createdAt when it is a
-- valid timestamp, otherwise COALESCE(indexed_at, created_at). `alias` is the
-- table alias; nil selects unqualified columns.
local function created_at_sort(alias)
local prefix = alias and (alias .. ".") or ""
local record, indexed_at, created_at = prefix .. "record", prefix .. "indexed_at", prefix .. "created_at"
local created = record .. "::jsonb->>'createdAt'"
return "CASE WHEN jsonb_typeof(" .. record .. "::jsonb->'createdAt') = 'string' AND " .. created ..
" ~ '" .. CREATED_AT_SORT_PATTERN .. "' AND " .. created .. " !~ '-00:00$' AND pg_input_is_valid(" .. created ..
", 'timestamptz') THEN (" .. created .. ")::timestamptz ELSE COALESCE(" .. indexed_at ..
"::timestamptz, " .. created_at .. "::timestamptz) END"
end

local FEED_REACTIONS = {
likes = {
collection = "app.certified.feed.like",
Expand All @@ -139,12 +176,6 @@ local FEED_REACTIONS = {
},
}

local function feed_reaction_sort_key()
local created = "record::jsonb->>'createdAt'"
local zoned = "^[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])$"
return "CASE WHEN jsonb_typeof(record::jsonb->'createdAt') = 'string' AND " .. created .. " ~ '" .. zoned .. "' AND " .. created .. " !~ '-00:00$' AND pg_input_is_valid(" .. created .. ", 'timestamptz') THEN (" .. created .. ")::timestamptz ELSE COALESCE(indexed_at::timestamptz, created_at::timestamptz) END"
end

local function feed_reaction_query(reaction, sql, values)
if db.backend() ~= "postgres" then error(reaction.failure .. ": reaction queries require PostgreSQL", 0) end
local ok, result = pcall(db.raw, sql, values)
Expand All @@ -153,24 +184,10 @@ local function feed_reaction_query(reaction, sql, values)
end

local function feed_reaction_decode_cursor(token, direction, collection)
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")
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
local valid, cursor_collection = valid_record_uri(value.u)
if not valid or cursor_collection ~= collection or not valid_datetime(value.t) then
invalid("cursor is malformed")
end
return value
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)
Expand All @@ -196,7 +213,7 @@ local function feed_reaction_query_page(reaction, mode, identity, limit, cursor,
local ordering = direction == "asc" and "ASC" or "DESC"
local subject_uri = "record::jsonb->'subject'->>'uri'"
-- Select the earliest representative before applying the caller's independent page direction.
local sql = "WITH ranked_reactions AS (SELECT uri, did, cid, indexed_at::text AS indexed_at, record::text AS record, sorted.sort_at, ROW_NUMBER() OVER (PARTITION BY did, " .. subject_uri .. " ORDER BY sorted.sort_at ASC, uri ASC) AS relationship_rank FROM happyview_records CROSS JOIN LATERAL (SELECT " .. feed_reaction_sort_key() .. " AS sort_at) sorted WHERE " .. table.concat(filters, " AND ") .. "), " ..
local sql = "WITH ranked_reactions AS (SELECT uri, did, cid, indexed_at::text AS indexed_at, record::text AS record, sorted.sort_at, ROW_NUMBER() OVER (PARTITION BY did, " .. subject_uri .. " ORDER BY sorted.sort_at ASC, uri ASC) AS relationship_rank FROM happyview_records CROSS JOIN LATERAL (SELECT " .. created_at_sort() .. " AS sort_at) sorted WHERE " .. table.concat(filters, " AND ") .. "), " ..
"representatives AS (SELECT uri, did, cid, indexed_at, record, sort_at FROM ranked_reactions WHERE relationship_rank = 1), "

local page = "page AS (SELECT uri, did, cid, indexed_at, record, sort_at FROM representatives" .. cursor_filter .. " ORDER BY sort_at " .. ordering .. ", uri " .. ordering .. " LIMIT $" .. #values .. ") "
Expand Down
47 changes: 24 additions & 23 deletions happyview-modules/lua/endpoints/getCollection.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,27 @@ local function valid_did(value)
return true
end

local function invalid(message)
error("InvalidRequest: " .. message, 0)
end

local function keys_only(values, allowed, unknown_message_prefix)
for key in pairs(values) do
if not allowed[key] then
invalid(unknown_message_prefix and (unknown_message_prefix .. key) or "unknown query parameter")
end
end
end

local function scalar(params, key)
local value = params[key]
if value == nil then return nil end
if type(value) ~= "string" and type(value) ~= "number" then
invalid(key .. " must occur once")
end
return tostring(value)
end

local function valid_record_key(value)
return #value >= 1 and #value <= 512 and value ~= "." and value ~= ".."
and not value:find("[^%w_~%.:%-]")
Expand Down Expand Up @@ -208,26 +229,6 @@ end

local COLLECTION = "org.hypercerts.collection"

local function collection_invalid(message)
error("InvalidRequest: " .. message, 0)
end

local function collection_keys_only(values, allowed)
for key in pairs(values) do
if not allowed[key] then collection_invalid("unknown query parameter: " .. key) end
end
end

local function collection_scalar(values, key)
local value = values[key]
if value == nil then return nil end
if key == "hasOrganizationRecord" and type(value) == "boolean" then return tostring(value) end
if type(value) ~= "string" and type(value) ~= "number" then
collection_invalid(key .. " must occur once")
end
return tostring(value)
end

local function collection_valid_record_uri(value)
if type(value) ~= "string" or #value > 8192 then return false end
return valid_record_uri(value)
Expand All @@ -253,11 +254,11 @@ local function collection_hydrate(views, omit_invalid)
end

function handle()
collection_keys_only(params, { uri = true })
local uri = collection_scalar(params, "uri")
keys_only(params, { uri = true }, "unknown query parameter: ")
local uri = scalar(params, "uri")
local valid, collection = collection_valid_record_uri(uri)
if not uri or not valid or collection ~= COLLECTION then
collection_invalid("uri must be a full org.hypercerts.collection AT-URI with a DID authority")
invalid("uri must be a full org.hypercerts.collection AT-URI with a DID authority")
end

local rows = collection_query(
Expand Down
24 changes: 16 additions & 8 deletions happyview-modules/lua/endpoints/getEntityFollow.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,6 @@ local function entity_follow_query(sql, values)
return result
end

-- Keep malformed publisher timestamps out of casts; preserve the actor-follow fallback order.
local function entity_follow_sort_key()
local created = "record::jsonb->>'createdAt'"
local zoned = "^[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])$"
return "CASE WHEN jsonb_typeof(record::jsonb->'createdAt') = 'string' AND " .. created .. " ~ '" .. zoned .. "' AND " .. created .. " !~ '-00:00$' AND pg_input_is_valid(" .. created .. ", 'timestamptz') THEN (" .. created .. ")::timestamptz ELSE COALESCE(indexed_at::timestamptz, created_at::timestamptz) END"
end

local function entity_follow_indexed_record_view(row)
return {
uri = row.uri,
Expand All @@ -76,9 +69,24 @@ local function entity_follow_record_view(row)
return view
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])$"

-- Sort key for a happyview_records row: the record's own createdAt when it is a
-- valid timestamp, otherwise COALESCE(indexed_at, created_at). `alias` is the
-- table alias; nil selects unqualified columns.
local function created_at_sort(alias)
local prefix = alias and (alias .. ".") or ""
local record, indexed_at, created_at = prefix .. "record", prefix .. "indexed_at", prefix .. "created_at"
local created = record .. "::jsonb->>'createdAt'"
return "CASE WHEN jsonb_typeof(" .. record .. "::jsonb->'createdAt') = 'string' AND " .. created ..
" ~ '" .. CREATED_AT_SORT_PATTERN .. "' AND " .. created .. " !~ '-00:00$' AND pg_input_is_valid(" .. created ..
", 'timestamptz') THEN (" .. created .. ")::timestamptz ELSE COALESCE(" .. indexed_at ..
"::timestamptz, " .. created_at .. "::timestamptz) END"
end

local function entity_follow_lookup(actor, entity_uri)
local rows = entity_follow_query(
"SELECT uri, did, cid, indexed_at::text AS indexed_at, record::text AS record FROM happyview_records CROSS JOIN LATERAL (SELECT " .. entity_follow_sort_key() .. " AS sort_at) sorted WHERE collection = $1 AND did = $2 AND record::jsonb->'subject'->>'uri' = $3 ORDER BY sorted.sort_at ASC, uri ASC LIMIT 1",
"SELECT uri, did, cid, indexed_at::text AS indexed_at, record::text AS record FROM happyview_records CROSS JOIN LATERAL (SELECT " .. created_at_sort() .. " AS sort_at) sorted WHERE collection = $1 AND did = $2 AND record::jsonb->'subject'->>'uri' = $3 ORDER BY sorted.sort_at ASC, uri ASC LIMIT 1",
{ ENTITY_FOLLOW, actor, entity_uri })
if #rows == 0 then return ENTITY_FOLLOW_NULL end
return entity_follow_record_view(rows[1])
Expand Down
24 changes: 16 additions & 8 deletions happyview-modules/lua/endpoints/getEntityFollows.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,21 @@ local function valid_record_uri(value)
return true, collection, authority
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])$"

-- Sort key for a happyview_records row: the record's own createdAt when it is a
-- valid timestamp, otherwise COALESCE(indexed_at, created_at). `alias` is the
-- table alias; nil selects unqualified columns.
local function created_at_sort(alias)
local prefix = alias and (alias .. ".") or ""
local record, indexed_at, created_at = prefix .. "record", prefix .. "indexed_at", prefix .. "created_at"
local created = record .. "::jsonb->>'createdAt'"
return "CASE WHEN jsonb_typeof(" .. record .. "::jsonb->'createdAt') = 'string' AND " .. created ..
" ~ '" .. CREATED_AT_SORT_PATTERN .. "' AND " .. created .. " !~ '-00:00$' AND pg_input_is_valid(" .. created ..
", 'timestamptz') THEN (" .. created .. ")::timestamptz ELSE COALESCE(" .. indexed_at ..
"::timestamptz, " .. created_at .. "::timestamptz) END"
end

local ENTITY_FOLLOW = "app.certified.graph.entityFollow"
local ENTITY_FOLLOW_NULL = json.decode("null")

Expand All @@ -53,13 +68,6 @@ local function entity_follow_query(sql, values)
return result
end

-- Keep malformed publisher timestamps out of casts; preserve the actor-follow fallback order.
local function entity_follow_sort_key()
local created = "record::jsonb->>'createdAt'"
local zoned = "^[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])$"
return "CASE WHEN jsonb_typeof(record::jsonb->'createdAt') = 'string' AND " .. created .. " ~ '" .. zoned .. "' AND " .. created .. " !~ '-00:00$' AND pg_input_is_valid(" .. created .. ", 'timestamptz') THEN (" .. created .. ")::timestamptz ELSE COALESCE(indexed_at::timestamptz, created_at::timestamptz) END"
end

local function entity_follow_indexed_record_view(row)
return {
uri = row.uri,
Expand Down Expand Up @@ -219,7 +227,7 @@ local function get_entity_follows()
.. "indexed_at::text AS indexed_at, record::text AS record FROM requested "
.. "JOIN happyview_records ON collection = $1 AND did = $2 "
.. "AND record::jsonb->'subject'->>'uri' = requested.target "
.. "CROSS JOIN LATERAL (SELECT " .. entity_follow_sort_key() .. " AS sort_at) sorted "
.. "CROSS JOIN LATERAL (SELECT " .. created_at_sort() .. " AS sort_at) sorted "
.. "ORDER BY requested.target, sorted.sort_at ASC, uri ASC",
values)

Expand Down
Loading
Loading