Skip to content

Commit

Permalink
feat(pnp): add paginated requests handler and use address/name vs. ow… (
Browse files Browse the repository at this point in the history
#193)

…ner/name
  • Loading branch information
arielmelendez authored Nov 16, 2024
2 parents 23782fc + a0ceb1d commit 10f880d
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 6 deletions.
54 changes: 54 additions & 0 deletions spec/primary_names_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -404,4 +404,58 @@ describe("Primary Names", function()
}, _G.PrimaryNames)
end)
end)

describe("getPaginatedPrimaryNames", function()
it("should return all primary names", function()
_G.PrimaryNames = {
owners = {
["owner"] = { name = "test", startTimestamp = 1234567890 },
},
names = {
["test"] = "owner",
},
requests = {},
}
local paginatedPrimaryNames = primaryNames.getPaginatedPrimaryNames(nil, 10, "startTimestamp", "asc")
assert.are.same({
items = {
{ name = "test", owner = "owner", startTimestamp = 1234567890 },
},
totalItems = 1,
limit = 10,
hasMore = false,
sortBy = "startTimestamp",
sortOrder = "asc",
}, paginatedPrimaryNames)
end)
end)

describe("getPaginatedPrimaryNameRequests", function()
it("should return all primary name requests", function()
_G.PrimaryNames.requests = {
["initiator1"] = {
name = "test",
startTimestamp = 1234567890,
endTimestamp = 1234567890 + 30 * 24 * 60 * 60 * 1000,
},
}
local paginatedPrimaryNameRequests =
primaryNames.getPaginatedPrimaryNameRequests(nil, 10, "startTimestamp", "asc")
assert.are.same({
items = {
{
name = "test",
startTimestamp = 1234567890,
endTimestamp = 1234567890 + 30 * 24 * 60 * 60 * 1000,
initiator = "initiator1",
},
},
totalItems = 1,
limit = 10,
hasMore = false,
sortBy = "startTimestamp",
sortOrder = "asc",
}, paginatedPrimaryNameRequests)
end)
end)
end)
34 changes: 34 additions & 0 deletions src/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ local ActionMap = {
-- PRIMARY NAMES
RemovePrimaryNames = "Remove-Primary-Names",
PrimaryNameRequest = "Primary-Name-Request",
PrimaryNameRequests = "Primary-Name-Requests",
ApprovePrimaryNameRequest = "Approve-Primary-Name-Request",
PrimaryNames = "Primary-Names",
PrimaryName = "Primary-Name",
Expand Down Expand Up @@ -3456,6 +3457,39 @@ addEventingHandler("getPrimaryNameData", utils.hasMatchingTag("Action", ActionMa
})
end)

addEventingHandler(
"getPaginatedPrimaryNameRequests",
utils.hasMatchingTag("Action", ActionMap.PrimaryNameRequests),
function(msg)
local page = utils.parsePaginationTags(msg)
local status, result = eventingPcall(
msg.ioEvent,
function(error)
ao.send({
Target = msg.From,
Tags = {
Action = "Invalid-" .. ActionMap.PrimaryNameRequests .. "-Notice",
Error = tostring(error),
},
})
end,
primaryNames.getPaginatedPrimaryNameRequests,
page.cursor,
page.limit,
page.sortBy or "startTimestamp",
page.sortOrder or "asc"
)
if not status or not result then
return
end
return ao.send({
Target = msg.From,
Action = ActionMap.PrimaryNameRequests .. "-Notice",
Data = json.encode(result),
})
end
)

addEventingHandler("getPaginatedPrimaryNames", utils.hasMatchingTag("Action", ActionMap.PrimaryNames), function(msg)
local page = utils.parsePaginationTags(msg)
local status, result = pcall(
Expand Down
30 changes: 25 additions & 5 deletions src/primary_names.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ end

--- Creates a transient request for a primary name. This is done by a user and must be approved by the name owner of the base name.
--- @param name string -- the name being requested, this could be an undername provided by the ant
--- @param initiator string -- the address that is creating the primary name request, e.g. the ANT process id
--- @param initiator WalletAddress -- the address that is creating the primary name request, e.g. the ANT process id
--- @param timestamp number -- the timestamp of the request
--- @param msgId string -- the message id of the request
--- @param fundFrom "balance"|"stakes"|"any"|nil -- the address to fund the request from. Default is "balance"
Expand Down Expand Up @@ -101,14 +101,14 @@ function primaryNames.createPrimaryNameRequest(name, initiator, timestamp, msgId
end

--- Get a primary name request, safely deep copying the request
--- @param address string
--- @param address WalletAddress
--- @return PrimaryNameRequest|nil primaryNameClaim - the request found, or nil if it does not exist
function primaryNames.getPrimaryNameRequest(address)
return utils.deepCopy(primaryNames.getUnsafePrimaryNameRequests()[address])
end

--- Unsafe access to the primary name requests
--- @return table<string, PrimaryNameRequest> primaryNameClaims - the primary name requests
--- @return table<WalletAddress, PrimaryNameRequest> primaryNameClaims - the primary name requests
function primaryNames.getUnsafePrimaryNameRequests()
return PrimaryNames.requests or {}
end
Expand All @@ -118,7 +118,7 @@ function primaryNames.getUnsafePrimaryNames()
end

--- Unsafe access to the primary name owners
--- @return table<string, PrimaryName> primaryNames - the primary names
--- @return table<WalletAddress, PrimaryName> primaryNames - the primary names
function primaryNames.getUnsafePrimaryNameOwners()
return PrimaryNames.owners or {}
end
Expand Down Expand Up @@ -300,7 +300,7 @@ end
function primaryNames.getPaginatedPrimaryNames(cursor, limit, sortBy, sortOrder)
local primaryNamesArray = {}
local cursorField = "name"
for owner, primaryName in ipairs(primaryNames.getUnsafePrimaryNameOwners()) do
for owner, primaryName in pairs(primaryNames.getUnsafePrimaryNameOwners()) do
table.insert(primaryNamesArray, {
name = primaryName.name,
owner = owner,
Expand All @@ -310,6 +310,26 @@ function primaryNames.getPaginatedPrimaryNames(cursor, limit, sortBy, sortOrder)
return utils.paginateTableWithCursor(primaryNamesArray, cursor, cursorField, limit, sortBy, sortOrder)
end

--- Get paginated primary name requests
--- @param cursor string|nil
--- @param limit number
--- @param sortBy string
--- @param sortOrder string
--- @return PaginatedTable<PrimaryNameRequest> paginatedPrimaryNameRequests - the paginated primary name requests
function primaryNames.getPaginatedPrimaryNameRequests(cursor, limit, sortBy, sortOrder)
local primaryNameRequestsArray = {}
local cursorField = "initiator"
for initiator, request in pairs(primaryNames.getUnsafePrimaryNameRequests()) do
table.insert(primaryNameRequestsArray, {
name = request.name,
startTimestamp = request.startTimestamp,
endTimestamp = request.endTimestamp,
initiator = initiator,
})
end
return utils.paginateTableWithCursor(primaryNameRequestsArray, cursor, cursorField, limit, sortBy, sortOrder)
end

--- Prune expired primary name requests
--- @param timestamp number
--- @return table<string, PrimaryNameRequest> prunedNameClaims - the names of the requests that were pruned
Expand Down
2 changes: 1 addition & 1 deletion tests/handlers.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe('handlers', async () => {
const evalIndex = handlersList.indexOf('_eval');
const defaultIndex = handlersList.indexOf('_default');
const pruneIndex = handlersList.indexOf('prune');
const expectedHandlerCount = 70; // TODO: update this if more handlers are added
const expectedHandlerCount = 71; // TODO: update this if more handlers are added
assert.ok(evalIndex === 0);
assert.ok(defaultIndex === 1);
assert.ok(pruneIndex === 2);
Expand Down
25 changes: 25 additions & 0 deletions tests/primary.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -412,4 +412,29 @@ describe('primary names', function () {
});
});
});

describe('getPaginatedPrimaryNameRequests', function () {
it('should return all primary name requests', async function () {
const getPaginatedPrimaryNameRequestsResult = await handle({
Tags: [
{ name: 'Action', value: 'Primary-Name-Requests' },
{ name: 'Limit', value: 10 },
{ name: 'Sort-By', value: 'startTimestamp' },
{ name: 'Sort-Order', value: 'asc' },
],
});
assertNoResultError(getPaginatedPrimaryNameRequestsResult);
const primaryNameRequests = JSON.parse(
getPaginatedPrimaryNameRequestsResult.Messages[0].Data,
);
assert.deepStrictEqual(primaryNameRequests, {
items: [],
totalItems: 0,
limit: 10,
hasMore: false,
sortBy: 'startTimestamp',
sortOrder: 'asc',
});
});
});
});

0 comments on commit 10f880d

Please sign in to comment.