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

View File

@ -1,6 +1,8 @@
import {map, pick, last, uniqBy} from "ramda" import {map, pick, last, uniqBy} from "ramda"
import {get} from "svelte/store" 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 {getRelayForPersonHint} from "src/agent/relays"
import {getPersonWithFallback} from "src/agent/tables" import {getPersonWithFallback} from "src/agent/tables"
import pool from "src/agent/pool" import pool from "src/agent/pool"
@ -47,7 +49,16 @@ const createDirectMessage = (pubkey, content) =>
new PublishableEvent(4, {content, tags: [["p", pubkey]]}) new PublishableEvent(4, {content, tags: [["p", pubkey]]})
const createNote = (content, mentions = [], topics = []) => { 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}) return new PublishableEvent(1, {content, tags})
} }
@ -57,7 +68,18 @@ const createReaction = (note, content) =>
const createReply = (note, content, mentions = [], topics = []) => { const createReply = (note, content, mentions = [], topics = []) => {
// Mentions have to come first so interpolation works // 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}) return new PublishableEvent(1, {content, tags})
} }
@ -88,6 +110,27 @@ const processMentions = map(pubkey => {
return ["p", pubkey, relay?.url || "", name] 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 getReplyTags = n => {
const {url} = getRelayForPersonHint(n.pubkey, n) const {url} = getRelayForPersonHint(n.pubkey, n)
const rootId = findRootId(n) || findReplyId(n) || n.id const rootId = findRootId(n) || findReplyId(n) || n.id
@ -102,10 +145,8 @@ const getReplyTags = n => {
const tagsFromParent = (n, newTags = []) => { const tagsFromParent = (n, newTags = []) => {
const pubkey = get(keys.pubkey) const pubkey = get(keys.pubkey)
return uniqBy( // Mentions have to come first for interpolation to work
// Remove duplicates due to inheritance. Keep earlier ones return (
t => t.slice(0, 2).join(":"),
// Mentions have to come first for interpolation to work
newTags newTags
// Add standard reply tags // Add standard reply tags
.concat(getReplyTags(n)) .concat(getReplyTags(n))
@ -122,6 +163,8 @@ const tagsFromParent = (n, newTags = []) => {
) )
} }
const uniqTags = uniqBy(t => t.slice(0, 2).join(":"))
class PublishableEvent { class PublishableEvent {
event: Record<string, any> event: Record<string, any>
constructor(kind, {content = "", tags = []}) { constructor(kind, {content = "", tags = []}) {

View File

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

View File

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