From 4374655730c3762ea56e9c732a7c0b7ab872ae1f Mon Sep 17 00:00:00 2001 From: Adrien Hopkins Date: Fri, 5 Dec 2025 09:51:00 -0500 Subject: Move some DM code to $lib I will be using some of this elsewhere, so it really should be there. --- src/lib/nip40.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 src/lib/nip40.ts (limited to 'src/lib/nip40.ts') diff --git a/src/lib/nip40.ts b/src/lib/nip40.ts new file mode 100644 index 00000000..030fe0df --- /dev/null +++ b/src/lib/nip40.ts @@ -0,0 +1,16 @@ +// Returns true if the message has an expiration in the past, false otherwise. +export const expired = (event: object): boolean => { + const expiryTime = expiration(event); + return expiryTime !== null && Date.now() / 1000 >= expiryTime; +} + +// Returns the provided event's expiration time, as a Unix ms integer. +// If it has none, returns null. +export const expiration = (event: object): number | null => { + for (const tag of event.tags) { + if (tag[0] == "expiration") { + return parseInt(tag[1]); + } + } + return null; +} -- cgit v1.2.3