From 7c3edf3cf5b507abce05883650848fab754967d8 Mon Sep 17 00:00:00 2001 From: dtfiedler Date: Fri, 15 Nov 2024 21:51:54 -0600 Subject: [PATCH 1/2] feat(pnp): add paginated requests handler and use address/name vs. owner/name --- spec/primary_names_spec.lua | 29 +++++++++++++++++++++++++++++ src/main.lua | 34 ++++++++++++++++++++++++++++++++++ src/primary_names.lua | 24 ++++++++++++++++++++++-- tests/handlers.test.mjs | 2 +- tests/primary.test.mjs | 25 +++++++++++++++++++++++++ 5 files changed, 111 insertions(+), 3 deletions(-) diff --git a/spec/primary_names_spec.lua b/spec/primary_names_spec.lua index 96b378b..726ca3a 100644 --- a/spec/primary_names_spec.lua +++ b/spec/primary_names_spec.lua @@ -404,4 +404,33 @@ describe("Primary Names", function() }, _G.PrimaryNames) 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) diff --git a/src/main.lua b/src/main.lua index 5351c08..8361ccd 100644 --- a/src/main.lua +++ b/src/main.lua @@ -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", @@ -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( diff --git a/src/primary_names.lua b/src/primary_names.lua index bc1c4b4..3c4e545 100644 --- a/src/primary_names.lua +++ b/src/primary_names.lua @@ -300,16 +300,36 @@ end function primaryNames.getPaginatedPrimaryNames(cursor, limit, sortBy, sortOrder) local primaryNamesArray = {} local cursorField = "name" - for owner, primaryName in ipairs(primaryNames.getUnsafePrimaryNameOwners()) do + for address, primaryName in ipairs(primaryNames.getUnsafePrimaryNameOwners()) do table.insert(primaryNamesArray, { name = primaryName.name, - owner = owner, + address = address, startTimestamp = primaryName.startTimestamp, }) end 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 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 prunedNameClaims - the names of the requests that were pruned diff --git a/tests/handlers.test.mjs b/tests/handlers.test.mjs index f052ca2..5665921 100644 --- a/tests/handlers.test.mjs +++ b/tests/handlers.test.mjs @@ -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); diff --git a/tests/primary.test.mjs b/tests/primary.test.mjs index 19898ca..6bbff75 100644 --- a/tests/primary.test.mjs +++ b/tests/primary.test.mjs @@ -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', + }); + }); + }); }); From a0ceb1dea099f0618e74208b3f0aed8a1c8e8900 Mon Sep 17 00:00:00 2001 From: dtfiedler Date: Fri, 15 Nov 2024 22:04:56 -0600 Subject: [PATCH 2/2] chore(test): add test for getting paginated primary names --- spec/primary_names_spec.lua | 25 +++++++++++++++++++++++++ src/primary_names.lua | 12 ++++++------ 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/spec/primary_names_spec.lua b/spec/primary_names_spec.lua index 726ca3a..7ecf16c 100644 --- a/spec/primary_names_spec.lua +++ b/spec/primary_names_spec.lua @@ -405,6 +405,31 @@ describe("Primary Names", function() 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 = { diff --git a/src/primary_names.lua b/src/primary_names.lua index 3c4e545..debad16 100644 --- a/src/primary_names.lua +++ b/src/primary_names.lua @@ -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" @@ -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 primaryNameClaims - the primary name requests +--- @return table primaryNameClaims - the primary name requests function primaryNames.getUnsafePrimaryNameRequests() return PrimaryNames.requests or {} end @@ -118,7 +118,7 @@ function primaryNames.getUnsafePrimaryNames() end --- Unsafe access to the primary name owners ---- @return table primaryNames - the primary names +--- @return table primaryNames - the primary names function primaryNames.getUnsafePrimaryNameOwners() return PrimaryNames.owners or {} end @@ -300,10 +300,10 @@ end function primaryNames.getPaginatedPrimaryNames(cursor, limit, sortBy, sortOrder) local primaryNamesArray = {} local cursorField = "name" - for address, primaryName in ipairs(primaryNames.getUnsafePrimaryNameOwners()) do + for owner, primaryName in pairs(primaryNames.getUnsafePrimaryNameOwners()) do table.insert(primaryNamesArray, { name = primaryName.name, - address = address, + owner = owner, startTimestamp = primaryName.startTimestamp, }) end