fix line breaks in compose, add embedded nostr entities to tags

This commit is contained in:
Jonathan Staab 2023-03-30 16:02:21 -05:00
parent ce07869656
commit 316c9f8d76
4 changed files with 59 additions and 12 deletions

View File

@ -4,10 +4,9 @@
- [ ] Improve note rendering
- [ ] Fix reactions and replies showing up
- [x] Show all images in preview as slideshow or something
- [ ] Extract nostr: links and bech32 entities, hover
- [ ] Add nostr: links and bech32 entities in compose to tags
- [ ] Linkify topics
- [ ] Fix extra newlines when composing
- [x] Extract nostr: links and bech32 entities, hover
- [x] Add nostr: links and bech32 entities in compose to tags
- [x] Fix extra newlines when composing
- [ ] Multiplexer
- [ ] Announce multiplextr, paravel, coracle update w/url
- [ ] Write NIP to support proxies. Update COUNT NIP to mention how proxies are a good use case for COUNT
@ -37,6 +36,7 @@
# More
- [ ] Linkify topics
- [ ] Add suggestion list for topics on compose so people know there are suggestions
- [ ] Badges link to https://badges.page/p/97c70a44366a6535c145b333f973ea86dfdc2d7a99da618c40c64705ad98e322
- [ ] Link/embed good chat/DM micro-apps

View File

@ -1,6 +1,8 @@
import {map, pick, last, uniqBy} from "ramda"
import {get} from "svelte/store"
import {roomAttrs, displayPerson, findReplyId, findRootId} from "src/util/nostr"
import {doPipe} from "hurdak/lib/hurdak"
import {parseContent} from "src/util/html"
import {Tags, roomAttrs, displayPerson, findReplyId, findRootId} from "src/util/nostr"
import {getRelayForPersonHint} from "src/agent/relays"
import {getPersonWithFallback} from "src/agent/tables"
import pool from "src/agent/pool"
@ -47,7 +49,16 @@ const createDirectMessage = (pubkey, content) =>
new PublishableEvent(4, {content, tags: [["p", pubkey]]})
const createNote = (content, mentions = [], topics = []) => {
const tags = processMentions(mentions).concat(topics.map(t => ["t", t]))
// Mentions have to come first so interpolation works
const tags = doPipe(
[],
[
tags => tags.concat(processMentions(mentions)),
tags => tags.concat(topics.map(t => ["t", t])),
tags => tagsFromContent(content, tags),
uniqTags,
]
)
return new PublishableEvent(1, {content, tags})
}
@ -57,7 +68,18 @@ const createReaction = (note, content) =>
const createReply = (note, content, mentions = [], topics = []) => {
// Mentions have to come first so interpolation works
const tags = tagsFromParent(note, processMentions(mentions).concat(topics.map(t => ["t", t])))
const tags = doPipe(
[],
[
tags => tags.concat(processMentions(mentions)),
tags => tags.concat(topics.map(t => ["t", t])),
tags => tagsFromParent(note, tags),
tags => tagsFromContent(content, tags),
uniqTags,
]
)
console.log(tags)
return new PublishableEvent(1, {content, tags})
}
@ -88,6 +110,27 @@ const processMentions = map(pubkey => {
return ["p", pubkey, relay?.url || "", name]
})
const tagsFromContent = (content, tags) => {
const seen = new Set(Tags.wrap(tags).values().all())
for (const {type, value} of parseContent(content)) {
if (type.match(/nostr:(note|nevent)/) && !seen.has(value.id)) {
tags = tags.concat([["e", value.id]])
seen.add(value.id)
}
if (type.match(/nostr:(nprofile|npub)/) && !seen.has(value.pubkey)) {
const name = displayPerson(getPersonWithFallback(value.pubkey))
const relay = getRelayForPersonHint(value.pubkey)
tags = tags.concat([["p", value.pubkey, relay?.url || "", name]])
seen.add(value.pubkey)
}
}
return tags
}
const getReplyTags = n => {
const {url} = getRelayForPersonHint(n.pubkey, n)
const rootId = findRootId(n) || findReplyId(n) || n.id
@ -102,10 +145,8 @@ const getReplyTags = n => {
const tagsFromParent = (n, newTags = []) => {
const pubkey = get(keys.pubkey)
return uniqBy(
// Remove duplicates due to inheritance. Keep earlier ones
t => t.slice(0, 2).join(":"),
// Mentions have to come first for interpolation to work
// Mentions have to come first for interpolation to work
return (
newTags
// Add standard reply tags
.concat(getReplyTags(n))
@ -122,6 +163,8 @@ const tagsFromParent = (n, newTags = []) => {
)
}
const uniqTags = uniqBy(t => t.slice(0, 2).join(":"))
class PublishableEvent {
event: Record<string, any>
constructor(kind, {content = "", tags = []}) {

View File

@ -58,6 +58,10 @@
for (const child of node.childNodes) {
const lineBreaks = child.querySelectorAll?.("br") || []
if (child.tagName === "BR") {
continue
}
// Line breaks may be bare brs or divs wrapping brs
if (lineBreaks.length > 0) {
content += "\n".repeat(lineBreaks.length)

View File

@ -8,7 +8,7 @@
</script>
{#if $canPublish}
<div class="fixed bottom-0 right-0 m-8">
<div class="fixed bottom-0 right-0 z-10 m-8">
<button
class="color-white flex h-16 w-16 items-center justify-center rounded-full
border border-accent-light bg-accent text-white shadow-2xl