forked from coracle/flotilla
Get chat back to working ish
This commit is contained in:
@@ -4,13 +4,14 @@
|
||||
import {writable} from "svelte/store"
|
||||
import {createEditor, type Editor, EditorContent} from "svelte-tiptap"
|
||||
import {NProfileExtension, ImageExtension} from "nostr-editor"
|
||||
import {createEvent, CHAT_MESSAGE} from "@welshman/util"
|
||||
import {createEvent} from "@welshman/util"
|
||||
import {publishThunk, makeThunk} from "@welshman/app"
|
||||
import {findNodes} from "@lib/tiptap"
|
||||
import Icon from "@lib/components/Icon.svelte"
|
||||
import Button from "@lib/components/Button.svelte"
|
||||
import {makeMention, makeIMeta} from "@app/commands"
|
||||
import {getChatEditorOptions, addFile} from "@app/editor"
|
||||
import {MESSAGE} from "@app/state"
|
||||
|
||||
export let url
|
||||
export let topic = ""
|
||||
@@ -21,7 +22,7 @@
|
||||
|
||||
const sendMessage = () => {
|
||||
const json = $editor.getJSON()
|
||||
const topicTag = topic ? ["t", topic] : []
|
||||
const topicTags = topic ? [["t", topic]] : []
|
||||
const mentionTags = findNodes(NProfileExtension.name, json).map(m =>
|
||||
makeMention(m.attrs!.pubkey, m.attrs!.relays),
|
||||
)
|
||||
@@ -29,9 +30,9 @@
|
||||
makeIMeta(src, {x, ox: x}),
|
||||
)
|
||||
|
||||
const event = createEvent(CHAT_MESSAGE, {
|
||||
const event = createEvent(MESSAGE, {
|
||||
content: $editor.getText(),
|
||||
tags: [topicTag, ...mentionTags, ...imetaTags],
|
||||
tags: [["-"], ["relay", url], ...topicTags, ...mentionTags, ...imetaTags],
|
||||
})
|
||||
|
||||
publishThunk(makeThunk({event, relays: [url]}))
|
||||
|
||||
+2
-2
@@ -1,6 +1,6 @@
|
||||
import {nip19} from "nostr-tools"
|
||||
import type {Page} from "@sveltejs/kit"
|
||||
import {userMembership, decodeNEvent} from "@app/state"
|
||||
import {userMembership, decodeNRelay} from "@app/state"
|
||||
|
||||
export const makeSpacePath = (url: string, extra = "") => {
|
||||
let path = `/spaces/${nip19.nrelayEncode(url)}`
|
||||
@@ -21,7 +21,7 @@ export const getPrimaryNavItemIndex = ($page: Page) => {
|
||||
case "discover":
|
||||
return urls.length + 2
|
||||
case "spaces": {
|
||||
const routeUrl = decodeNEvent($page.params.nrelay)
|
||||
const routeUrl = decodeNRelay($page.params.nrelay)
|
||||
|
||||
return urls.findIndex(url => url === routeUrl) + 1
|
||||
}
|
||||
|
||||
+8
-11
@@ -69,10 +69,6 @@ export const deriveEvent = (idOrAddress: string, hints: string[] = []) => {
|
||||
)
|
||||
}
|
||||
|
||||
// Persist relay/event mappings
|
||||
|
||||
export const relaysByMessage = withGetter(writable(new Map<string, string[]>()))
|
||||
|
||||
// Topics
|
||||
|
||||
export const topicsByUrl = withGetter(writable(new Map<string, string[]>()))
|
||||
@@ -133,18 +129,19 @@ export type Message = {
|
||||
}
|
||||
|
||||
export const readMessage = (event: TrustedEvent): Maybe<Message[]> => {
|
||||
const urls = getRelayTagValues(event.tags)
|
||||
const topics = getTopicTagValues(event.tags)
|
||||
|
||||
if (topics.length !== 1) return undefined
|
||||
if (topics.length > 1 || urls.length !== 1) return undefined
|
||||
|
||||
const topic = topics[0]
|
||||
const urls = relaysByMessage.get().get(event.id) || []
|
||||
const topic = topics[0] || ""
|
||||
const url = urls[0]
|
||||
|
||||
return urls.map(url => ({url, topic, chat: getChatId(url, topic), event}))
|
||||
return urls.map(url => ({url, topic, chat: makeChatId(url, topic), event}))
|
||||
}
|
||||
|
||||
export const messages = deriveEventsMapped<Message>(repository, {
|
||||
filters: [{}],
|
||||
filters: [{kinds: [MESSAGE, REPLY]}],
|
||||
eventToItem: readMessage,
|
||||
itemToEvent: item => item.event,
|
||||
})
|
||||
@@ -158,7 +155,7 @@ export type Chat = {
|
||||
messages: Message[]
|
||||
}
|
||||
|
||||
export const getChatId = (url: string, topic: string) => `${url}'${topic}`
|
||||
export const makeChatId = (url: string, topic: string) => `${url}'${topic}`
|
||||
|
||||
export const splitChatId = (id: string) => id.split("'")
|
||||
|
||||
@@ -210,7 +207,7 @@ export const userMembership = withGetter(
|
||||
|
||||
// Other utils
|
||||
|
||||
export const decodeNEvent = (nevent: string) => nip19.decode(nevent).data as string
|
||||
export const decodeNRelay = (nevent: string) => nip19.decode(nevent).data as string
|
||||
|
||||
export const displayReaction = (content: string) => {
|
||||
if (content === "+") return "❤️"
|
||||
|
||||
Reference in New Issue
Block a user