mirror of
https://github.com/coracle-social/coracle.git
synced 2024-09-19 11:43:35 +00:00
Clean up alerts page
This commit is contained in:
parent
7c9ac4f6e2
commit
2748fad2cc
@ -1,6 +1,6 @@
|
|||||||
<script>
|
<script>
|
||||||
import {propEq, uniqBy, prop, sortBy} from 'ramda'
|
import {propEq, uniqBy, prop, sortBy} from 'ramda'
|
||||||
import {onMount, onDestroy} from 'svelte'
|
import {onMount} from 'svelte'
|
||||||
import {fly} from 'svelte/transition'
|
import {fly} from 'svelte/transition'
|
||||||
import {alerts} from 'src/state/app'
|
import {alerts} from 'src/state/app'
|
||||||
import {findReply} from 'src/util/nostr'
|
import {findReply} from 'src/util/nostr'
|
||||||
@ -10,51 +10,43 @@
|
|||||||
import Note from 'src/partials/Note.svelte'
|
import Note from 'src/partials/Note.svelte'
|
||||||
import Like from 'src/partials/Like.svelte'
|
import Like from 'src/partials/Like.svelte'
|
||||||
|
|
||||||
let sub
|
let notes = []
|
||||||
let scroller
|
|
||||||
let notes
|
|
||||||
let limit = 0
|
|
||||||
|
|
||||||
const cursor = new Cursor(timedelta(1, 'hours'))
|
const cursor = new Cursor(timedelta(1, 'days'))
|
||||||
|
|
||||||
onMount(async () => {
|
onMount(() => {
|
||||||
sub = await relay.pool.listenForEvents(
|
const scroller = createScroller(async () => {
|
||||||
'routes/Alerts',
|
notes = notes.concat(await loadNotes())
|
||||||
[{kinds: [1, 5, 7], '#p': [$user.pubkey], since: cursor.since}],
|
})
|
||||||
onEvent
|
|
||||||
|
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({
|
const events = await relay.filterEvents({
|
||||||
limit,
|
since,
|
||||||
|
until,
|
||||||
kinds: [1, 7],
|
kinds: [1, 7],
|
||||||
'#p': [$user.pubkey],
|
'#p': [$user.pubkey],
|
||||||
customFilter: e => {
|
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(
|
const reactions = await Promise.all(
|
||||||
events
|
events
|
||||||
.filter(e => e.kind === 7)
|
.filter(e => e.kind === 7)
|
||||||
@ -83,21 +78,10 @@
|
|||||||
}))
|
}))
|
||||||
)
|
)
|
||||||
|
|
||||||
if (events.length <= limit) {
|
// Combine likes of a single note. Remove grandchild likes
|
||||||
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
|
|
||||||
const likesById = {}
|
const likesById = {}
|
||||||
const alerts = notes.filter(e => e.pubkey !== $user.pubkey)
|
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]) {
|
if (!likesById[reaction.parent.id]) {
|
||||||
likesById[reaction.parent.id] = {...reaction.parent, people: []}
|
likesById[reaction.parent.id] = {...reaction.parent, people: []}
|
||||||
}
|
}
|
||||||
@ -115,7 +99,7 @@
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
<ul class="py-4 flex flex-col gap-2 max-w-xl m-auto">
|
<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}
|
{#if e.people}
|
||||||
<li in:fly={{y: 20}}><Like note={e} /></li>
|
<li in:fly={{y: 20}}><Like note={e} /></li>
|
||||||
{:else}
|
{:else}
|
||||||
@ -124,12 +108,4 @@
|
|||||||
{/each}
|
{/each}
|
||||||
</ul>
|
</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 />
|
<Spinner />
|
||||||
{/if}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user