Clean up alerts page

This commit is contained in:
Jonathan Staab 2022-12-26 14:00:11 -08:00
parent 7c9ac4f6e2
commit 2748fad2cc

View File

@ -1,6 +1,6 @@
<script>
import {propEq, uniqBy, prop, sortBy} from 'ramda'
import {onMount, onDestroy} from 'svelte'
import {onMount} from 'svelte'
import {fly} from 'svelte/transition'
import {alerts} from 'src/state/app'
import {findReply} from 'src/util/nostr'
@ -10,51 +10,43 @@
import Note from 'src/partials/Note.svelte'
import Like from 'src/partials/Like.svelte'
let sub
let scroller
let notes
let limit = 0
let notes = []
const cursor = new Cursor(timedelta(1, 'hours'))
const cursor = new Cursor(timedelta(1, 'days'))
onMount(async () => {
sub = await relay.pool.listenForEvents(
'routes/Alerts',
[{kinds: [1, 5, 7], '#p': [$user.pubkey], since: cursor.since}],
onEvent
onMount(() => {
const scroller = createScroller(async () => {
notes = notes.concat(await loadNotes())
})
return () => scroller.stop()
})
const loadNotes = async () => {
const [since, until] = cursor.step()
await relay.pool.loadEvents(
[{kinds: [1, 7], '#p': [$user.pubkey], since, until}],
e => {
if (e.kind === 1) {
relay.loadNoteContext(e)
}
if (e.kind === 7) {
const replyId = findReply(e)
if (replyId) {
relay.getOrLoadNote(replyId)
}
}
alerts.set({since: now()})
}
)
scroller = createScroller(async () => {
limit += 20
notes = relay.lq(() => loadNotes(limit))
})
})
onDestroy(() => {
sub?.unsub()
scroller?.stop()
})
const onEvent = e => {
if (e.kind === 1) {
relay.loadNoteContext(e)
}
if (e.kind === 7) {
const replyId = findReply(e)
if (replyId) {
relay.getOrLoadNote(replyId)
}
}
alerts.set({since: now()})
}
const loadNotes = async limit => {
const events = await relay.filterEvents({
limit,
since,
until,
kinds: [1, 7],
'#p': [$user.pubkey],
customFilter: e => {
@ -72,7 +64,10 @@
}
})
const notes = await relay.annotateChunk(events.filter(propEq('kind', 1)))
const notes = await relay.annotateChunk(
events.filter(propEq('kind', 1))
)
const reactions = await Promise.all(
events
.filter(e => e.kind === 7)
@ -83,21 +78,10 @@
}))
)
if (events.length <= limit) {
const [since, until] = cursor.step()
relay.pool.loadEvents(
[{kinds: [1, 7], '#p': [$user.pubkey], since, until}],
onEvent
)
} else {
setTimeout(scroller.check, 300)
}
// Combine likes of a single note
// Combine likes of a single note. Remove grandchild likes
const likesById = {}
const alerts = notes.filter(e => e.pubkey !== $user.pubkey)
for (const reaction of reactions.filter(prop('parent'))) {
for (const reaction of reactions.filter(e => e.parent?.pubkey === $user.pubkey)) {
if (!likesById[reaction.parent.id]) {
likesById[reaction.parent.id] = {...reaction.parent, people: []}
}
@ -115,7 +99,7 @@
</script>
<ul class="py-4 flex flex-col gap-2 max-w-xl m-auto">
{#each ($notes || []) as e (e.id)}
{#each notes as e (e.id)}
{#if e.people}
<li in:fly={{y: 20}}><Like note={e} /></li>
{:else}
@ -124,12 +108,4 @@
{/each}
</ul>
{#if $notes?.length === 0}
<div in:fly={{y: 20}} class="flex w-full justify-center items-center py-16">
<div class="text-center max-w-md">
No recent activity found.
</div>
</div>
{:else}
<Spinner />
{/if}