forked from coracle/flotilla
Move from topic to room using tilde
This commit is contained in:
+6
-5
@@ -14,7 +14,7 @@ import {
|
||||
loadMutes,
|
||||
followsByPubkey,
|
||||
} from "@welshman/app"
|
||||
import {MEMBERSHIPS, INDEXER_RELAYS} from "@app/state"
|
||||
import {ROOM, MEMBERSHIPS, INDEXER_RELAYS} from "@app/state"
|
||||
|
||||
// Utils
|
||||
|
||||
@@ -95,9 +95,9 @@ export const updateList = async (kind: number, modifyTags: ModifyTags) => {
|
||||
export const addSpaceMembership = (url: string) =>
|
||||
updateList(MEMBERSHIPS, (tags: string[][]) => uniqBy(t => t.join(""), [...tags, ["r", url]]))
|
||||
|
||||
export const addRoomMembership = (url: string, topic: string) =>
|
||||
export const addRoomMembership = (url: string, room: string) =>
|
||||
updateList(MEMBERSHIPS, (tags: string[][]) =>
|
||||
uniqBy(t => t.join(""), [...tags, ["t", topic, url]]),
|
||||
uniqBy(t => t.join(""), [...tags, [ROOM, room, url]]),
|
||||
)
|
||||
|
||||
export const removeSpaceMembership = (url: string) =>
|
||||
@@ -105,5 +105,6 @@ export const removeSpaceMembership = (url: string) =>
|
||||
tags.filter(t => !equals(["r", url], t) && t[2] !== url),
|
||||
)
|
||||
|
||||
export const removeRoomMembership = (url: string, topic: string) =>
|
||||
updateList(MEMBERSHIPS, (tags: string[][]) => tags.filter(t => !equals(["t", topic, url], t)))
|
||||
export const removeRoomMembership = (url: string, room: string) =>
|
||||
updateList(MEMBERSHIPS, (tags: string[][]) => tags.filter(t => !equals([ROOM, room, url], t)))
|
||||
|
||||
|
||||
@@ -11,10 +11,10 @@
|
||||
import Button from "@lib/components/Button.svelte"
|
||||
import {makeMention, makeIMeta} from "@app/commands"
|
||||
import {getChatEditorOptions, addFile} from "@app/editor"
|
||||
import {MESSAGE} from "@app/state"
|
||||
import {ROOM, MESSAGE, GENERAL} from "@app/state"
|
||||
|
||||
export let url
|
||||
export let topic = ""
|
||||
export let room = GENERAL
|
||||
|
||||
const uploading = writable(false)
|
||||
|
||||
@@ -22,7 +22,6 @@
|
||||
|
||||
const sendMessage = () => {
|
||||
const json = $editor.getJSON()
|
||||
const topicTags = topic ? [["t", topic]] : []
|
||||
const mentionTags = findNodes(NProfileExtension.name, json).map(m =>
|
||||
makeMention(m.attrs!.pubkey, m.attrs!.relays),
|
||||
)
|
||||
@@ -32,7 +31,7 @@
|
||||
|
||||
const event = createEvent(MESSAGE, {
|
||||
content: $editor.getText(),
|
||||
tags: [["-"], ...topicTags, ...mentionTags, ...imetaTags],
|
||||
tags: [[ROOM, room], ...mentionTags, ...imetaTags],
|
||||
})
|
||||
|
||||
publishThunk(makeThunk({event, relays: [url]}))
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
} from "@welshman/app"
|
||||
import type {PublishStatusData} from "@welshman/app"
|
||||
import {
|
||||
GROUP_REPLY,
|
||||
REACTION,
|
||||
ZAP_RESPONSE,
|
||||
displayRelayUrl,
|
||||
@@ -27,7 +26,7 @@
|
||||
import Icon from "@lib/components/Icon.svelte"
|
||||
import Button from "@lib/components/Button.svelte"
|
||||
import Avatar from "@lib/components/Avatar.svelte"
|
||||
import {deriveEvent, displayReaction} from "@app/state"
|
||||
import {REPLY, deriveEvent, displayReaction} from "@app/state"
|
||||
import {getChatViewOptions} from "@app/editor"
|
||||
|
||||
export let event: TrustedEvent
|
||||
@@ -85,7 +84,7 @@
|
||||
</script>
|
||||
|
||||
<div in:fly class="group relative flex flex-col gap-1 p-2 transition-colors hover:bg-base-300">
|
||||
{#if event.kind === GROUP_REPLY}
|
||||
{#if event.kind === REPLY}
|
||||
<div class="flex items-center gap-1 pl-12 text-xs">
|
||||
<Icon icon="arrow-right" />
|
||||
<Avatar src={$parentProfile?.picture} size={4} />
|
||||
|
||||
@@ -52,7 +52,6 @@
|
||||
const event = createEvent(kind, {
|
||||
content: $editor.getText(),
|
||||
tags: [
|
||||
["-"],
|
||||
["d", randomId()],
|
||||
["title", title],
|
||||
["location", location],
|
||||
|
||||
@@ -51,7 +51,7 @@
|
||||
class="!h-10 !w-10 border border-solid border-base-300"
|
||||
size={7} />
|
||||
</PrimaryNavItem>
|
||||
{#each $userMembership?.topicsByUrl.keys() || [] as url (url)}
|
||||
{#each $userMembership?.roomsByUrl.keys() || [] as url (url)}
|
||||
<PrimaryNavItem title={displayRelayUrl(url)} href={makeSpacePath(url)}>
|
||||
<SpaceAvatar {url} />
|
||||
</PrimaryNavItem>
|
||||
|
||||
@@ -13,9 +13,9 @@
|
||||
const back = () => history.back()
|
||||
|
||||
const tryCreate = async () => {
|
||||
await addRoomMembership(url, topic)
|
||||
await addRoomMembership(url, room)
|
||||
|
||||
goto(makeSpacePath(url, topic))
|
||||
goto(makeSpacePath(url, room))
|
||||
}
|
||||
|
||||
const create = async () => {
|
||||
@@ -28,7 +28,7 @@
|
||||
}
|
||||
}
|
||||
|
||||
let topic = ""
|
||||
let room = ""
|
||||
let loading = false
|
||||
</script>
|
||||
|
||||
@@ -41,7 +41,7 @@
|
||||
<p slot="label">Room Name</p>
|
||||
<label class="input input-bordered flex w-full items-center gap-2" slot="input">
|
||||
<Icon icon="hashtag" />
|
||||
<input bind:value={topic} class="grow" type="text" />
|
||||
<input bind:value={room} class="grow" type="text" />
|
||||
</label>
|
||||
</Field>
|
||||
<div class="flex flex-row items-center justify-between gap-4">
|
||||
@@ -49,7 +49,7 @@
|
||||
<Icon icon="alt-arrow-left" />
|
||||
Go back
|
||||
</Button>
|
||||
<Button type="submit" class="btn btn-primary" disabled={!topic || loading}>
|
||||
<Button type="submit" class="btn btn-primary" disabled={!room || loading}>
|
||||
<Spinner {loading}>Create Room</Spinner>
|
||||
<Icon icon="alt-arrow-right" />
|
||||
</Button>
|
||||
|
||||
+1
-1
@@ -15,7 +15,7 @@ export const makeSpacePath = (url: string, extra = "") => {
|
||||
export const getPrimaryNavItem = ($page: Page) => $page.route?.id?.split("/")[1]
|
||||
|
||||
export const getPrimaryNavItemIndex = ($page: Page) => {
|
||||
const urls = Array.from(userMembership.get()?.topicsByUrl.keys() || [])
|
||||
const urls = Array.from(userMembership.get()?.roomsByUrl.keys() || [])
|
||||
|
||||
switch (getPrimaryNavItem($page)) {
|
||||
case "discover":
|
||||
|
||||
+28
-25
@@ -1,7 +1,7 @@
|
||||
import {nip19} from "nostr-tools"
|
||||
import {get, derived} from "svelte/store"
|
||||
import type {Maybe} from "@welshman/lib"
|
||||
import {setContext, max, pushToMapKey, nthEq} from "@welshman/lib"
|
||||
import {setContext, nth, max, pushToMapKey, nthEq} from "@welshman/lib"
|
||||
import {
|
||||
getIdFilters,
|
||||
RELAYS,
|
||||
@@ -10,7 +10,6 @@ import {
|
||||
EVENT_DATE,
|
||||
EVENT_TIME,
|
||||
getRelayTagValues,
|
||||
getTopicTagValues,
|
||||
isShareableRelayUrl,
|
||||
} from "@welshman/util"
|
||||
import type {TrustedEvent} from "@welshman/util"
|
||||
@@ -31,9 +30,13 @@ import {
|
||||
import type {SubscribeRequestWithHandlers} from "@welshman/net"
|
||||
import {deriveEvents, deriveEventsMapped, withGetter} from "@welshman/store"
|
||||
|
||||
export const ROOM = "~"
|
||||
|
||||
export const GENERAL = "general"
|
||||
|
||||
export const MESSAGE = 209
|
||||
|
||||
export const REPLY = 210
|
||||
export const REPLY = 1111
|
||||
|
||||
export const MEMBERSHIPS = 10209
|
||||
|
||||
@@ -75,7 +78,7 @@ export const deriveEvent = (idOrAddress: string, hints: string[] = []) => {
|
||||
// Membership
|
||||
|
||||
export type Membership = {
|
||||
topicsByUrl: Map<string, string[]>
|
||||
roomsByUrl: Map<string, string[]>
|
||||
event?: TrustedEvent
|
||||
}
|
||||
|
||||
@@ -84,17 +87,17 @@ export type PublishedMembership = Omit<Membership, "event"> & {
|
||||
}
|
||||
|
||||
export const readMembership = (event: TrustedEvent): PublishedMembership => {
|
||||
const topicsByUrl = new Map<string, string[]>()
|
||||
const roomsByUrl = new Map<string, string[]>()
|
||||
|
||||
for (const tag of event.tags.filter(nthEq(0, "r"))) {
|
||||
topicsByUrl.set(tag[1], [])
|
||||
roomsByUrl.set(tag[1], [])
|
||||
}
|
||||
|
||||
for (const tag of event.tags.filter(nthEq(0, "t"))) {
|
||||
pushToMapKey(topicsByUrl, tag[2], tag[1])
|
||||
pushToMapKey(roomsByUrl, tag[2], tag[1])
|
||||
}
|
||||
|
||||
return {event, topicsByUrl}
|
||||
return {event, roomsByUrl}
|
||||
}
|
||||
|
||||
export const memberships = deriveEventsMapped<PublishedMembership>(repository, {
|
||||
@@ -121,16 +124,16 @@ export const {
|
||||
// Messages
|
||||
|
||||
export type Message = {
|
||||
topic: string
|
||||
room: string
|
||||
event: TrustedEvent
|
||||
}
|
||||
|
||||
export const readMessage = (event: TrustedEvent): Maybe<Message> => {
|
||||
const topics = getTopicTagValues(event.tags)
|
||||
const rooms = event.tags.filter(nthEq(0, ROOM)).map(nth(1))
|
||||
|
||||
if (topics.length > 1) return undefined
|
||||
if (rooms.length > 1) return undefined
|
||||
|
||||
return {topic: topics[0] || "", event}
|
||||
return {room: rooms[0] || "", event}
|
||||
}
|
||||
|
||||
export const messages = deriveEventsMapped<Message>(repository, {
|
||||
@@ -144,11 +147,11 @@ export const messages = deriveEventsMapped<Message>(repository, {
|
||||
export type Chat = {
|
||||
id: string
|
||||
url: string
|
||||
topic: string
|
||||
room: string
|
||||
messages: Message[]
|
||||
}
|
||||
|
||||
export const makeChatId = (url: string, topic: string) => `${url}'${topic}`
|
||||
export const makeChatId = (url: string, room: string) => `${url}'${room}`
|
||||
|
||||
export const splitChatId = (id: string) => id.split("'")
|
||||
|
||||
@@ -157,16 +160,16 @@ export const chats = derived([trackerStore, messages], ([$tracker, $messages]) =
|
||||
|
||||
for (const message of $messages) {
|
||||
for (const url of $tracker.getRelays(message.event.id)) {
|
||||
const chatId = makeChatId(url, message.topic)
|
||||
const chatId = makeChatId(url, message.room)
|
||||
|
||||
pushToMapKey(messagesByChatId, chatId, message)
|
||||
}
|
||||
}
|
||||
|
||||
return Array.from(messagesByChatId.entries()).map(([id, messages]) => {
|
||||
const [url, topic] = splitChatId(id)
|
||||
const [url, room] = splitChatId(id)
|
||||
|
||||
return {id, url, topic, messages}
|
||||
return {id, url, room, messages}
|
||||
})
|
||||
})
|
||||
|
||||
@@ -179,12 +182,12 @@ export const {
|
||||
store: chats,
|
||||
getKey: chat => chat.id,
|
||||
load: (id: string, request: Partial<SubscribeRequestWithHandlers> = {}) => {
|
||||
const [url, topic] = splitChatId(id)
|
||||
const [url, room] = splitChatId(id)
|
||||
const chat = get(chatsById).get(id)
|
||||
const timestamps = chat?.messages.map(m => m.event.created_at) || []
|
||||
const since = Math.max(0, max(timestamps) - 3600)
|
||||
|
||||
return load({...request, relays: [url], filters: [{"#t": [topic], since}]})
|
||||
return load({...request, relays: [url], filters: [{'#~': [room], since}]})
|
||||
},
|
||||
})
|
||||
|
||||
@@ -204,18 +207,18 @@ export const eventsByUrl = derived([trackerStore, events], ([$tracker, $events])
|
||||
return eventsByUrl
|
||||
})
|
||||
|
||||
// Topics
|
||||
// Rooms
|
||||
|
||||
export const topicsByUrl = derived(chats, $chats => {
|
||||
const $topicsByUrl = new Map<string, string[]>()
|
||||
export const roomsByUrl = derived(chats, $chats => {
|
||||
const $roomsByUrl = new Map<string, string[]>()
|
||||
|
||||
for (const chat of $chats) {
|
||||
if (chat.topic) {
|
||||
pushToMapKey($topicsByUrl, chat.url, chat.topic)
|
||||
if (chat.room) {
|
||||
pushToMapKey($roomsByUrl, chat.url, chat.room)
|
||||
}
|
||||
}
|
||||
|
||||
return $topicsByUrl
|
||||
return $roomsByUrl
|
||||
})
|
||||
|
||||
// User stuff
|
||||
|
||||
Reference in New Issue
Block a user