From 31f73278e3584d14896b85b0a984d858a0ff798d Mon Sep 17 00:00:00 2001 From: arthurfranca Date: Tue, 27 Jun 2023 12:11:06 -0300 Subject: [PATCH] Add 34seen algo --- 34.md | 7 ++++--- 34seen.md | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 34seen.md diff --git a/34.md b/34.md index 16ad34be..73ffbd44 100644 --- a/34.md +++ b/34.md @@ -68,6 +68,7 @@ Bugfixes and small updates to embrace new event kinds may be submitted at any ti ## Available Algorithms -| Extension | Name | Description | Modification Date | -| ----------------- | ----------| -------------------------------------------------- | ----------------- | -| [34asc](34asc.md) | Ascending | Events with older `created_at` are retrieved first | 12 / jun / 2023 | +| Extension | Name | Description | Modification Date | +| ------------------- | ----------| -------------------------------------------------- | ----------------- | +| [34asc](34asc.md) | Ascending | Events with older `created_at` are retrieved first | 27 / jun / 2023 | +| [34seen](34seen.md) | Seen At | Events are desc sorted by first seen at timestamp | 27 / jun / 2023 | diff --git a/34seen.md b/34seen.md new file mode 100644 index 00000000..780eff7d --- /dev/null +++ b/34seen.md @@ -0,0 +1,46 @@ +NIP-34seen +========== + +Seen At +------- + +`draft` `optional` `author:arthurfranca` `author:mikedilger` + +Events are desc sorted by first seen at timestamp. + +## Motivation + +Some `clients` may want to check if `relay` has recently received old events +created before last time they checked. + +## Implementation + +`Relay` computes `nip34seen` field once upon receiving the event. + +The event field is set with the timestamp of the moment the `relay` first became aware of it. + +### Javascript + +```js +// Use this same value even if your programming language allows a higher one +// It is ok because it is lower than Number.MAX_SAFE_INTEGER +const maxDateNowSeconds = 8640000000000 // 8.64e15 ms / 1000 +const maxSecondsLength = maxDateNowSeconds.toString().length + +function getNip34seen (id = '') { + // The id length must always be the same to not affect sorting + // All relays must use the same length + if (id.length !== 64) { throw new Error('Wrong id length') } + let seconds = Math.trunc(Date.now() / 1000) // Make sure it is int instead of float + // don't allow negative timestamp as new Date(-1) is on year 1969 + if (seconds < 0) { throw new Error('Wrong server clock') } + seconds = Math.min(seconds, maxDateNowSeconds) // Keep max limit + // '10' is wrongly lower then '2' while '10' is higher than padded '02' + const paddedTsSeconds = seconds.toString().padStart(maxSecondsLength, '0') + + // Event id is used as a fixed length unique identifier + return `${paddedTsSeconds}${id}` +} + +event.nip34seen = getNip34seen(event.id) +```