Use unix days instead of time hashes

This commit is contained in:
Jon Staab
2025-02-05 15:15:50 -08:00
parent 65aabf5feb
commit a0c6e46184
3 changed files with 10 additions and 45 deletions

View File

@@ -1,10 +1,10 @@
<script lang="ts">
import {writable} from "svelte/store"
import {randomId, HOUR} from "@welshman/lib"
import {randomId} from "@welshman/lib"
import {createEvent, EVENT_TIME} from "@welshman/util"
import {publishThunk} from "@welshman/app"
import {preventDefault} from "@lib/html"
import {timeHashesBetween} from "@lib/util"
import {daysBetween} from "@lib/util"
import Icon from "@lib/components/Icon.svelte"
import Field from "@lib/components/Field.svelte"
import Button from "@lib/components/Button.svelte"
@@ -47,7 +47,7 @@
["location", location],
["start", start.toString()],
["end", end.toString()],
...timeHashesBetween(start, end).map(T => ["T", T]),
...daysBetween(start, end).map(D => ["D", D]),
...editor.storage.nostr.getEditorTags(),
PROTECTED,
],

View File

@@ -1,6 +1,6 @@
import {hexToBytes, bytesToHex} from "@noble/hashes/utils"
import * as nip19 from "nostr-tools/nip19"
import {HOUR, MONTH, DAY} from "@welshman/lib"
import {range, DAY} from "@welshman/lib"
export const displayList = <T>(xs: T[], conj = "and", n = 6, locale = "en-US") => {
const stringItems = xs.map(String)
@@ -26,41 +26,6 @@ export const nsecDecode = (nsec: string) => {
return bytesToHex(data)
}
export const timeHash = (seconds: number, precision = 32) => {
const alphabet = "0123456789bcdefghjkmnpqrstuvwxyz"
const uint32 = Math.min(seconds >>> 0, 0xffffffff)
const binary = uint32.toString(2).padStart(32, "0")
const chunks = Math.min(Math.floor(precision / 5), 6)
export const day = (seconds: number) => Math.floor(seconds / DAY).toString()
let hash = ""
for (let i = 0; i < chunks * 5; i += 5) {
const chunk = binary.slice(i, i + 5)
const index = parseInt(chunk, 2)
hash += alphabet[index]
}
return hash
}
export const timeHashesBetween = (start: number, end: number, precisions = [10, 15, 20]) => {
const hashes = new Set<string>()
for (let seconds = start; seconds <= end; seconds += HOUR) {
for (const precision of precisions) {
hashes.add(timeHash(seconds, precision))
}
}
return Array.from(hashes).sort()
}
export const timeHashesForFilter = (start: number, end: number) => {
const diff = end - start
if (diff < 3 * DAY) return timeHashesBetween(start, end, [20])
if (diff < 3 * MONTH) return timeHashesBetween(start, end, [15])
return timeHashesBetween(start, end, [10])
}
export const daysBetween = (start: number, end: number) => [...range(start, end, DAY)].map(day)

View File

@@ -1,11 +1,11 @@
<script lang="ts">
import {onMount, onDestroy} from "svelte"
import {page} from "$app/stores"
import {sortBy, now, int, MONTH, last} from "@welshman/lib"
import {sortBy, now, MONTH, last} from "@welshman/lib"
import type {TrustedEvent} from "@welshman/util"
import {EVENT_DATE, EVENT_TIME, getTagValue} from "@welshman/util"
import {subscribe, formatTimestampAsDate} from "@welshman/app"
import {timeHashesForFilter} from "@lib/util"
import {daysBetween} from "@lib/util"
import Icon from "@lib/components/Icon.svelte"
import Button from "@lib/components/Button.svelte"
import Spinner from "@lib/components/Spinner.svelte"
@@ -57,9 +57,9 @@
onMount(() => {
const sub = subscribe({filters: [{kinds, since: now()}]})
const hashes = timeHashesForFilter(now() - int(3, MONTH), now() + int(3, MONTH))
const hashes = daysBetween(now() - MONTH, now() + MONTH)
pullConservatively({filters: [{kinds, "#T": hashes}], relays: [url]})
pullConservatively({filters: [{kinds, "#D": hashes}], relays: [url]})
return () => sub.close()
})