Fix legacy e tag parsing

This commit is contained in:
Jonathan Staab 2023-05-19 06:14:22 -07:00
parent 6e9f7e678f
commit 688f6af160
4 changed files with 34 additions and 36 deletions

View File

@ -5,6 +5,7 @@
- [x] Add basic naddr support
- [x] Add scroll to top button, and scroll to top on navigate
- [x] Add close all button to modal, open person in modal more often
- [x] Fix legacy e tag parsing
# 0.2.27

View File

@ -1,4 +1,4 @@
import {map, pick, last, uniqBy} from "ramda"
import {map, pick, uniqBy} from "ramda"
import {get} from "svelte/store"
import {doPipe} from "hurdak/lib/hurdak"
import {parseContent, Tags, roomAttrs, displayPerson, findRoot, findReply} from "src/util/nostr"
@ -76,7 +76,7 @@ const createReply = (note, content, mentions = [], topics = []) => {
[
tags => tags.concat(processMentions(mentions)),
tags => tags.concat(topics.map(t => ["t", t])),
tags => tagsFromParent(note, tags),
tags => tags.concat(getReplyTags(note)),
tags => tagsFromContent(content, tags),
uniqTags,
]
@ -144,27 +144,6 @@ const getReplyTags = n => {
return [["p", n.pubkey, pHint?.url || ""], root, reply]
}
const tagsFromParent = (n, newTags = []) => {
const pubkey = get(keys.pubkey)
// Mentions have to come first for interpolation to work
return (
newTags
// Add standard reply tags
.concat(getReplyTags(n))
// Inherit p and e tags, but remove marks and self-mentions
.concat(
n.tags.filter(t => {
if (t[1] === pubkey) return false
if (!["p", "e"].includes(t[0])) return false
if (["reply", "root"].includes(last(t))) return false
return true
})
)
)
}
const uniqTags = uniqBy(t => t.slice(0, 2).join(":"))
class PublishableEvent {

View File

@ -170,7 +170,6 @@ export const listen = async () => {
;(listen as any)._listener?.unsub()
;(listen as any)._listener = await network.listen({
delay: 3000,
relays: getUserReadRelays(),
filter: [
{kinds: [1, 4], authors: [pubkey], since},

View File

@ -27,6 +27,12 @@ export class Tags {
all() {
return this.tags
}
count() {
return this.tags.length
}
exists() {
return this.tags.length > 0
}
first() {
return first(this.tags)
}
@ -54,6 +60,9 @@ export class Tags {
filter(f) {
return new Tags(this.tags.filter(f))
}
any(f) {
return this.filter(f).exists()
}
type(type) {
const types = ensurePlural(type)
@ -67,21 +76,31 @@ export class Tags {
}
}
// Support the deprecated version where tags are not marked as replies
export const findReply = e =>
Tags.from(e).type("e").mark("reply").first() || Tags.from(e).type("e").last()
export const findReplyAndRoot = e => {
const tags = Tags.from(e).type("e")
const legacy = tags.any(t => !["reply", "root"].includes(last(t)))
export const findReplyId = e =>
Tags.wrap([findReply(e)])
.values()
.first()
// Support the deprecated version where tags are not marked as replies
if (legacy) {
const reply = tags.last()
const root = tags.count() > 1 ? tags.first() : null
export const findRoot = e => Tags.from(e).type("e").mark("root").first()
return {reply, root}
}
export const findRootId = e =>
Tags.wrap([findRoot(e)])
.values()
.first()
return {
reply: tags.mark("reply").first(),
root: tags.mark("root").first(),
}
}
export const findReply = e => prop("reply", findReplyAndRoot(e))
export const findReplyId = e => findReply(e)?.[1]
export const findRoot = e => prop("root", findReplyAndRoot(e))
export const findRootId = e => findRoot(e)?.[1]
export const displayPerson = p => {
if (p.kind0?.display_name) {