Skip to content

Commit

Permalink
Use kind 1111 from NIP-22 comment (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
AsaiToshiya authored Oct 5, 2024
1 parent 7147612 commit d9006ba
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 114 deletions.
67 changes: 21 additions & 46 deletions src/Editor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export function Editor({
url,
pool,
relays,
parentId = undefined,
parent = undefined,
settingsContent,
placeholder
}) {
Expand Down Expand Up @@ -111,56 +111,31 @@ export function Editor({
async function publishEvent() {
setEditable(false)

let rootReference = baseTag?.reference
if (!rootReference) {
// create base event right here
let sk = generateSecretKey()
let tags = [['r', url]]
if (ownerTag) {
tags.push(ownerTag)
}
let root = {
pubkey: getPublicKey(sk),
created_at: Math.round(Date.now() / 1000),
kind: 1,
tags: tags,
content: `Comments on ${url}` + (ownerTag ? ` by #[1]` : '') + ` ↴`
}
root = finalizeEvent(root, sk)
rootReference = ['e', root.id, '', 'root']
setBaseTag({filter: {'#e': [root.id]}, reference: rootReference})

await Promise.any(pool.current.publish(relays, root))
pool.current.trackRelays = true
await pool.current.get(relays, {ids: [root.id]})
pool.current.trackRelays = false
setBaseTag(prev => {
rootReference[2] = Array.from(pool.current.seenOn.get(root.id))[0].url

return {
filter: {'#e': [root.id]},
reference: rootReference
}
})
}

console.log('base: ', rootReference)

let inReplyTo = []
if (parentId) {
inReplyTo.push([
'e',
parentId,
Array.from(pool.current.seenOn.get(parentId))[0].url,
'reply'
])
let rootReference = baseTag.rootReference

console.log('base: ', rootReference[0])

let inReplyTo
if (parent) {
inReplyTo = [
[
'e',
parent.id,
Array.from(pool.current.seenOn.get(parent.id))[0].url,
parent.pubkey
],
['k', parent.kind.toString()],
['p', parent.pubkey]
]
} else {
inReplyTo = baseTag.parentReference
}

let event = {
pubkey: publicKey,
created_at: Math.round(Date.now() / 1000),
kind: 1,
tags: [rootReference].concat(inReplyTo),
kind: 1111,
tags: rootReference.concat(inReplyTo),
content: comment
}

Expand Down
168 changes: 102 additions & 66 deletions src/NoComment.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,46 +19,6 @@ export function NoComment({
placeholder,
readonly
}) {
let customBaseTag = useMemo(() => {
if (customBase) {
try {
let {type, data} = nip19.decode(customBase)
switch (type) {
case 'note':
return {
ref: data,
filter: {'#e': [data]},
reference: ['e', data, '', 'root']
}
case 'nevent':
return {
ref: data.id,
filter: {'#e': [data.id]},
reference: ['e', data.id, data.relays[0] || '', 'root']
}
case 'naddr':
const {kind, pubkey, identifier} = data
return {
ref: `${kind}:${pubkey}:${identifier}`,
filter: {'#a': [`${kind}:${pubkey}:${identifier}`]},
reference: [
'a',
`${kind}:${pubkey}:${identifier}`,
data.relays[0] || '',
'root'
]
}
}
} catch (err) {
return {
ref: customBase,
filter: {'#e': [customBase]},
reference: ['e', customBase, '', 'root']
}
}
}
}, [customBase])

let ownerTag = null
if (owner) {
try {
Expand All @@ -81,7 +41,7 @@ export function NoComment({
}
}

const [baseTagImmediate, setBaseTag] = useState(customBaseTag)
const [baseTagImmediate, setBaseTag] = useState(null)
const [publicKey, setPublicKey] = useState(null)
const [eventsImmediate, setEvents] = useState([])
const [metadata, setMetadata] = useState({})
Expand All @@ -97,7 +57,76 @@ export function NoComment({
const [chosenRelays, setChosenRelays] = useState(relays)

useEffect(() => {
if (baseTag) return
if (customBase) {
let id = null
let address = null
let filter = null
let relay = ''
try {
let {type, data} = nip19.decode(customBase)
if (type === 'naddr') {
address = `${data.kind}:${data.pubkey}:${data.identifier}`
filter = {kinds: [data.kind], authors: [data.pubkey], "#d": [data.identifier]}
} else {
id = data.id ?? data
filter = {ids: [id]}
}
relay = data.relays?.[0] ?? ''
} catch (err) {
id = customBase
filter = {ids: [id]}
}

pool.current.trackRelays = true
pool.current.get(relays, filter)
.then(event => {
relay = relay || Array.from(pool.current.seenOn.get(event.id))[0].url

if (address) {
setBaseTag({
ref: address,
filters: [
{'#a': [address], kinds: [1]},
{'#A': [address], kinds: [1111]}
],
rootReference: [
['A', address, relay],
['K', event.kind.toString()]
],
parentReference: [
['a', address, relay],
['e', event.id, relay],
['k', event.kind.toString()],
['p', event.pubkey]
]
})
} else {
setBaseTag({
ref: id,
filters: [
{'#e': [id], kinds: [1]},
{'#E': [id], kinds: [1111]}
],
rootReference: [
['E', id, relay, event.pubkey],
['K', event.kind.toString()]
],
parentReference: [
['e', id, relay, event.pubkey],
['k', event.kind.toString()],
['p', event.pubkey]
]
})
}
})
.finally(() => {
pool.current.trackRelays = false
})
}
}, [customBase])

useEffect(() => {
if (customBase) return

// search for the base event based on the #r tag (url)
pool.current.trackRelays = true
Expand All @@ -107,16 +136,31 @@ export function NoComment({
kinds: [1]
})
.then(events => {
if (events.length === 0) return
let filters = [{'#I': [url], kinds: [1111]}]
if (events.length !== 0) {
filters.push({
'#e': events.slice(0, 3).map(event => event.id),
kinds: [1]
})
}

let urlObj = new URL(url)
let domain = `${urlObj.protocol}//${urlObj.host}`
let parentReference = [
['i', url],
['k', domain]
]
if (ownerTag) {
parentReference.push(ownerTag)
}

setBaseTag({
filter: {'#e': events.slice(0, 3).map(event => event.id)},
reference: [
'e',
events[0].id,
Array.from(pool.current.seenOn.get(events[0].id))[0].url,
'root'
]
filters,
rootReference: [
['I', url],
['K', domain]
],
parentReference
})
})
.finally(() => {
Expand All @@ -129,21 +173,13 @@ export function NoComment({

// query for comments
pool.current.trackRelays = true
let sub = pool.current.subscribeMany(chosenRelays,
[
{
...baseTag.filter,
kinds: [1]
}
],
{
onevent(event) {
setEvents(events => insertEventIntoDescendingList(events, event))
fetchMetadata(event.pubkey, i)
i++
}
let sub = pool.current.subscribeMany(chosenRelays, baseTag.filters, {
onevent(event) {
setEvents(events => insertEventIntoDescendingList(events, event))
fetchMetadata(event.pubkey, i)
i++
}
)
})

let i = 0

Expand Down Expand Up @@ -176,7 +212,7 @@ export function NoComment({
</Container>
)

function editor(parentId) {
function editor(parent) {
let selfName = getName(metadata, publicKey)
return (
<Editor
Expand All @@ -189,7 +225,7 @@ export function NoComment({
url={url}
setBaseTag={setBaseTag}
pool={pool}
parentId={parentId}
parent={parent}
relays={chosenRelays}
placeholder={placeholder}
settingsContent={
Expand Down
4 changes: 2 additions & 2 deletions src/Thread.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export default function Thread({
{!readonly && <ReplyButton onClick={() => setExpanded(!expanded)} />}
</div>
<CommentContent>{thread.content}</CommentContent>
{expanded && <ReplyWrap>{replyForm(thread.id)}</ReplyWrap>}
{expanded && <ReplyWrap>{replyForm(thread)}</ReplyWrap>}
<div
style={{
paddingLeft: `${36 - 6 * Math.pow(1.2, level)}px`
Expand Down Expand Up @@ -111,7 +111,7 @@ export function computeThreads(baseTag, events) {
}

// use the last "e" tag if none are marked as "reply"
if (curr === null) curr = tag[1]
if (curr === null || tag[0] === 'a') curr = tag[1]
}
}
return curr
Expand Down

0 comments on commit d9006ba

Please sign in to comment.