2024-08-14 00:58:26 +00:00
|
|
|
<script lang="ts">
|
2024-11-19 21:24:18 +00:00
|
|
|
import {nip19} from "nostr-tools"
|
2024-11-15 22:03:54 +00:00
|
|
|
import {onMount, onDestroy} from "svelte"
|
2024-11-19 21:24:18 +00:00
|
|
|
import type {Readable} from "svelte/store"
|
2024-12-09 20:20:16 +00:00
|
|
|
import {derived} from "svelte/store"
|
2024-11-19 21:24:18 +00:00
|
|
|
import type {Editor} from "svelte-tiptap"
|
2024-08-16 17:50:38 +00:00
|
|
|
import {page} from "$app/stores"
|
2024-12-09 19:59:42 +00:00
|
|
|
import {sleep, now, ctx} from "@welshman/lib"
|
2024-10-01 18:47:21 +00:00
|
|
|
import type {TrustedEvent, EventContent} from "@welshman/util"
|
2024-12-09 20:20:16 +00:00
|
|
|
import {throttled} from "@welshman/store"
|
2024-11-15 22:03:54 +00:00
|
|
|
import {createEvent, DELETE} from "@welshman/util"
|
2024-10-30 20:52:35 +00:00
|
|
|
import {formatTimestampAsDate, publishThunk} from "@welshman/app"
|
2024-10-16 23:16:50 +00:00
|
|
|
import {slide} from "@lib/transition"
|
2024-11-20 00:15:23 +00:00
|
|
|
import {createScroller, type Scroller} from "@lib/html"
|
2024-08-16 22:17:30 +00:00
|
|
|
import Icon from "@lib/components/Icon.svelte"
|
2024-09-11 19:19:45 +00:00
|
|
|
import Button from "@lib/components/Button.svelte"
|
2024-08-16 17:50:38 +00:00
|
|
|
import Spinner from "@lib/components/Spinner.svelte"
|
2024-10-09 23:11:23 +00:00
|
|
|
import PageBar from "@lib/components/PageBar.svelte"
|
2024-09-20 18:00:41 +00:00
|
|
|
import Divider from "@lib/components/Divider.svelte"
|
2024-11-15 22:03:54 +00:00
|
|
|
import MenuSpaceButton from "@app/components/MenuSpaceButton.svelte"
|
2024-10-03 21:43:17 +00:00
|
|
|
import ChannelMessage from "@app/components/ChannelMessage.svelte"
|
|
|
|
|
import ChannelCompose from "@app/components/ChannelCompose.svelte"
|
2024-10-08 18:39:16 +00:00
|
|
|
import {
|
2024-11-15 22:03:54 +00:00
|
|
|
pullConservatively,
|
2024-10-31 17:37:20 +00:00
|
|
|
userSettingValues,
|
2024-10-08 18:39:16 +00:00
|
|
|
userMembership,
|
2024-10-21 17:30:12 +00:00
|
|
|
decodeRelay,
|
2024-12-05 21:37:15 +00:00
|
|
|
deriveChannelMessages,
|
2024-10-08 18:39:16 +00:00
|
|
|
GENERAL,
|
|
|
|
|
tagRoom,
|
|
|
|
|
MESSAGE,
|
2024-10-08 22:30:18 +00:00
|
|
|
getMembershipRoomsByUrl,
|
2024-12-05 21:37:15 +00:00
|
|
|
displayRoom,
|
2024-10-08 18:39:16 +00:00
|
|
|
} from "@app/state"
|
2024-11-14 18:53:13 +00:00
|
|
|
import {setChecked} from "@app/notifications"
|
2024-11-15 22:03:54 +00:00
|
|
|
import {addRoomMembership, removeRoomMembership, subscribePersistent} from "@app/commands"
|
2024-12-09 19:59:42 +00:00
|
|
|
import {PROTECTED} from "@app/state"
|
2024-10-24 21:02:43 +00:00
|
|
|
import {popKey} from "@app/implicit"
|
2024-08-15 19:08:07 +00:00
|
|
|
|
2024-10-21 17:30:12 +00:00
|
|
|
const {room = GENERAL} = $page.params
|
2024-10-24 22:48:45 +00:00
|
|
|
const content = popKey<string>("content") || ""
|
2024-10-21 17:30:12 +00:00
|
|
|
const url = decodeRelay($page.params.relay)
|
2024-12-09 22:03:59 +00:00
|
|
|
const events = throttled(300, deriveChannelMessages(url, room))
|
2024-08-15 21:30:55 +00:00
|
|
|
|
2024-08-19 16:06:39 +00:00
|
|
|
const assertEvent = (e: any) => e as TrustedEvent
|
2024-08-15 21:30:55 +00:00
|
|
|
|
2024-11-19 21:24:18 +00:00
|
|
|
const replyTo = (event: TrustedEvent) => {
|
|
|
|
|
const relays = ctx.app.router.Event(event).getUrls()
|
|
|
|
|
const nevent = nip19.neventEncode({...event, relays})
|
|
|
|
|
|
|
|
|
|
$editor.commands.insertNEvent({nevent})
|
|
|
|
|
$editor.commands.insertContent("\n")
|
|
|
|
|
$editor.commands.focus()
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-12 22:32:36 +00:00
|
|
|
const onSubmit = ({content, tags}: EventContent) =>
|
|
|
|
|
publishThunk({
|
|
|
|
|
relays: [url],
|
2024-12-09 19:59:42 +00:00
|
|
|
event: createEvent(MESSAGE, {content, tags: [...tags, tagRoom(room, url), PROTECTED]}),
|
2024-11-12 22:32:36 +00:00
|
|
|
delay: $userSettingValues.send_delay,
|
|
|
|
|
})
|
2024-10-01 18:47:21 +00:00
|
|
|
|
2024-11-20 00:15:23 +00:00
|
|
|
let limit = 15
|
2024-08-15 21:30:55 +00:00
|
|
|
let loading = true
|
2024-11-20 00:15:23 +00:00
|
|
|
let unsub: () => void
|
|
|
|
|
let element: HTMLElement
|
|
|
|
|
let scroller: Scroller
|
2024-11-19 21:24:18 +00:00
|
|
|
let editor: Readable<Editor>
|
2024-08-15 21:30:55 +00:00
|
|
|
|
2024-12-09 22:03:59 +00:00
|
|
|
const elements = derived(events, $events => {
|
|
|
|
|
const $elements = []
|
2024-08-15 21:30:55 +00:00
|
|
|
|
2024-12-09 22:03:59 +00:00
|
|
|
let previousDate
|
|
|
|
|
let previousPubkey
|
2024-08-15 21:30:55 +00:00
|
|
|
|
2024-12-09 22:03:59 +00:00
|
|
|
for (const event of $events.toReversed()) {
|
|
|
|
|
const {id, pubkey, created_at} = event
|
|
|
|
|
const date = formatTimestampAsDate(created_at)
|
2024-08-15 21:30:55 +00:00
|
|
|
|
2024-12-09 22:03:59 +00:00
|
|
|
if (date !== previousDate) {
|
|
|
|
|
$elements.push({type: "date", value: date, id: date, showPubkey: false})
|
|
|
|
|
}
|
2024-08-15 21:30:55 +00:00
|
|
|
|
2024-12-09 22:03:59 +00:00
|
|
|
$elements.push({
|
|
|
|
|
id,
|
|
|
|
|
type: "note",
|
|
|
|
|
value: event,
|
|
|
|
|
showPubkey: date !== previousDate || previousPubkey !== pubkey,
|
|
|
|
|
})
|
2024-08-15 21:30:55 +00:00
|
|
|
|
2024-12-09 22:03:59 +00:00
|
|
|
previousDate = date
|
|
|
|
|
previousPubkey = pubkey
|
|
|
|
|
}
|
2024-08-15 21:30:55 +00:00
|
|
|
|
2024-12-09 22:03:59 +00:00
|
|
|
return $elements.reverse().slice(0, limit)
|
|
|
|
|
})
|
2024-08-15 21:30:55 +00:00
|
|
|
|
2024-11-20 00:15:23 +00:00
|
|
|
onMount(async () => {
|
|
|
|
|
// Sveltekiiit
|
|
|
|
|
await sleep(100)
|
|
|
|
|
|
2024-11-15 22:03:54 +00:00
|
|
|
pullConservatively({
|
|
|
|
|
relays: [url],
|
2024-12-05 21:37:15 +00:00
|
|
|
filters: [{kinds: [MESSAGE, DELETE], "#h": [room]}],
|
2024-11-15 22:03:54 +00:00
|
|
|
})
|
|
|
|
|
|
2024-11-20 00:15:23 +00:00
|
|
|
scroller = createScroller({
|
|
|
|
|
element,
|
|
|
|
|
delay: 300,
|
|
|
|
|
threshold: 3000,
|
|
|
|
|
onScroll: () => {
|
|
|
|
|
limit += 15
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
unsub = subscribePersistent({
|
2024-11-15 22:03:54 +00:00
|
|
|
relays: [url],
|
2024-12-05 21:37:15 +00:00
|
|
|
filters: [{kinds: [MESSAGE], "#h": [room], since: now()}],
|
2024-11-15 22:03:54 +00:00
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
2024-11-14 18:53:13 +00:00
|
|
|
onDestroy(() => {
|
2024-11-14 16:33:11 +00:00
|
|
|
setChecked($page.url.pathname)
|
2024-11-20 00:15:23 +00:00
|
|
|
scroller?.stop()
|
|
|
|
|
unsub?.()
|
2024-11-14 16:33:11 +00:00
|
|
|
})
|
|
|
|
|
|
2024-08-15 21:30:55 +00:00
|
|
|
setTimeout(() => {
|
|
|
|
|
loading = false
|
2024-10-24 18:52:02 +00:00
|
|
|
}, 5000)
|
2024-08-14 00:58:26 +00:00
|
|
|
</script>
|
|
|
|
|
|
2024-10-09 21:19:27 +00:00
|
|
|
<div class="relative flex h-full flex-col">
|
2024-10-09 23:11:23 +00:00
|
|
|
<PageBar>
|
|
|
|
|
<div slot="icon" class="center">
|
|
|
|
|
<Icon icon="hashtag" />
|
|
|
|
|
</div>
|
2024-12-05 21:37:15 +00:00
|
|
|
<strong slot="title">{displayRoom(room)}</strong>
|
2024-10-23 16:45:06 +00:00
|
|
|
<div slot="action" class="row-2">
|
2024-09-23 20:10:15 +00:00
|
|
|
{#if room !== GENERAL}
|
2024-10-08 22:30:18 +00:00
|
|
|
{#if getMembershipRoomsByUrl(url, $userMembership).includes(room)}
|
2024-09-23 17:28:33 +00:00
|
|
|
<Button class="btn btn-neutral btn-sm" on:click={() => removeRoomMembership(url, room)}>
|
2024-09-11 19:19:45 +00:00
|
|
|
<Icon icon="arrows-a-logout-2" />
|
|
|
|
|
Leave Room
|
|
|
|
|
</Button>
|
|
|
|
|
{:else}
|
2024-09-23 17:28:33 +00:00
|
|
|
<Button class="btn btn-neutral btn-sm" on:click={() => addRoomMembership(url, room)}>
|
2024-09-11 19:19:45 +00:00
|
|
|
<Icon icon="login-2" />
|
|
|
|
|
Join Room
|
|
|
|
|
</Button>
|
|
|
|
|
{/if}
|
|
|
|
|
{/if}
|
2024-11-15 22:03:54 +00:00
|
|
|
<MenuSpaceButton {url} />
|
2024-08-16 22:17:30 +00:00
|
|
|
</div>
|
2024-10-09 23:11:23 +00:00
|
|
|
</PageBar>
|
2024-11-20 00:15:23 +00:00
|
|
|
<div
|
|
|
|
|
class="scroll-container -mt-2 flex flex-grow flex-col-reverse overflow-auto py-2"
|
|
|
|
|
bind:this={element}>
|
2024-12-09 20:20:16 +00:00
|
|
|
{#each $elements as { type, id, value, showPubkey } (id)}
|
2024-08-15 21:30:55 +00:00
|
|
|
{#if type === "date"}
|
2024-09-20 18:00:41 +00:00
|
|
|
<Divider>{value}</Divider>
|
2024-08-15 21:30:55 +00:00
|
|
|
{:else}
|
2024-11-19 21:24:18 +00:00
|
|
|
<div in:slide class:-mt-4={!showPubkey}>
|
|
|
|
|
<ChannelMessage {url} {room} {replyTo} event={assertEvent(value)} {showPubkey} />
|
2024-10-01 18:47:21 +00:00
|
|
|
</div>
|
2024-08-15 21:30:55 +00:00
|
|
|
{/if}
|
|
|
|
|
{/each}
|
2024-08-16 17:50:38 +00:00
|
|
|
<p class="flex h-10 items-center justify-center py-20">
|
2024-08-15 21:30:55 +00:00
|
|
|
<Spinner {loading}>
|
|
|
|
|
{#if loading}
|
|
|
|
|
Looking for messages...
|
|
|
|
|
{:else}
|
|
|
|
|
End of message history
|
|
|
|
|
{/if}
|
|
|
|
|
</Spinner>
|
|
|
|
|
</p>
|
2024-08-15 19:08:07 +00:00
|
|
|
</div>
|
2024-11-19 21:24:18 +00:00
|
|
|
<ChannelCompose bind:editor {content} {onSubmit} />
|
2024-08-15 19:08:07 +00:00
|
|
|
</div>
|