feat: support customizing mentioned users
This commit is contained in:
parent
c1d469b1f4
commit
3c23a7f9f8
5 changed files with 200 additions and 76 deletions
|
|
@ -171,11 +171,9 @@ export function getProfileFromProfileEvent(event: Event) {
|
|||
}
|
||||
|
||||
export async function extractMentions(content: string, parentEvent?: Event) {
|
||||
let parentEventPubkey: string | undefined
|
||||
const pubkeySet = new Set<string>()
|
||||
const relatedEventIdSet = new Set<string>()
|
||||
const quoteEventIdSet = new Set<string>()
|
||||
let rootEventId: string | undefined
|
||||
let parentEventId: string | undefined
|
||||
const relatedPubkeySet = new Set<string>()
|
||||
const matches = content.match(
|
||||
/nostr:(npub1[a-z0-9]{58}|nprofile1[a-z0-9]+|note1[a-z0-9]{58}|nevent1[a-z0-9]+)/g
|
||||
)
|
||||
|
|
@ -192,7 +190,6 @@ export async function extractMentions(content: string, parentEvent?: Event) {
|
|||
const event = await client.fetchEvent(id)
|
||||
if (event) {
|
||||
pubkeySet.add(event.pubkey)
|
||||
quoteEventIdSet.add(event.id)
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
|
|
@ -200,13 +197,52 @@ export async function extractMentions(content: string, parentEvent?: Event) {
|
|||
}
|
||||
}
|
||||
|
||||
if (parentEvent) {
|
||||
parentEventPubkey = parentEvent.pubkey
|
||||
parentEvent.tags.forEach(([tagName, tagValue]) => {
|
||||
if (['p', 'P'].includes(tagName) && !!tagValue) {
|
||||
relatedPubkeySet.add(tagValue)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if (parentEventPubkey) {
|
||||
pubkeySet.delete(parentEventPubkey)
|
||||
relatedPubkeySet.delete(parentEventPubkey)
|
||||
}
|
||||
|
||||
return {
|
||||
pubkeys: Array.from(pubkeySet),
|
||||
relatedPubkeys: Array.from(relatedPubkeySet).filter((p) => !pubkeySet.has(p)),
|
||||
parentEventPubkey
|
||||
}
|
||||
}
|
||||
|
||||
export async function extractRelatedEventIds(content: string, parentEvent?: Event) {
|
||||
const relatedEventIdSet = new Set<string>()
|
||||
const quoteEventIdSet = new Set<string>()
|
||||
let rootEventId: string | undefined
|
||||
let parentEventId: string | undefined
|
||||
const matches = content.match(/nostr:(note1[a-z0-9]{58}|nevent1[a-z0-9]+)/g)
|
||||
|
||||
for (const m of matches || []) {
|
||||
try {
|
||||
const id = m.split(':')[1]
|
||||
const { type, data } = nip19.decode(id)
|
||||
if (type === 'nevent') {
|
||||
quoteEventIdSet.add(data.id)
|
||||
} else if (type === 'note') {
|
||||
quoteEventIdSet.add(data)
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
}
|
||||
}
|
||||
|
||||
if (parentEvent) {
|
||||
relatedEventIdSet.add(parentEvent.id)
|
||||
pubkeySet.add(parentEvent.pubkey)
|
||||
parentEvent.tags.forEach((tag) => {
|
||||
if (tagNameEquals('p')(tag)) {
|
||||
pubkeySet.add(tag[1])
|
||||
} else if (isRootETag(tag)) {
|
||||
if (isRootETag(tag)) {
|
||||
rootEventId = tag[1]
|
||||
} else if (tagNameEquals('e')(tag)) {
|
||||
relatedEventIdSet.add(tag[1])
|
||||
|
|
@ -223,7 +259,6 @@ export async function extractMentions(content: string, parentEvent?: Event) {
|
|||
if (parentEventId) relatedEventIdSet.delete(parentEventId)
|
||||
|
||||
return {
|
||||
pubkeys: Array.from(pubkeySet),
|
||||
otherRelatedEventIds: Array.from(relatedEventIdSet),
|
||||
quoteEventIds: Array.from(quoteEventIdSet),
|
||||
rootEventId,
|
||||
|
|
@ -232,7 +267,6 @@ export async function extractMentions(content: string, parentEvent?: Event) {
|
|||
}
|
||||
|
||||
export async function extractCommentMentions(content: string, parentEvent: Event) {
|
||||
const pubkeySet = new Set<string>()
|
||||
const quoteEventIdSet = new Set<string>()
|
||||
const rootEventId = parentEvent.tags.find(tagNameEquals('E'))?.[1] ?? parentEvent.id
|
||||
const rootEventKind = parentEvent.tags.find(tagNameEquals('K'))?.[1] ?? parentEvent.kind
|
||||
|
|
@ -241,34 +275,23 @@ export async function extractCommentMentions(content: string, parentEvent: Event
|
|||
const parentEventKind = parentEvent.kind
|
||||
const parentEventPubkey = parentEvent.pubkey
|
||||
|
||||
const matches = content.match(
|
||||
/nostr:(npub1[a-z0-9]{58}|nprofile1[a-z0-9]+|note1[a-z0-9]{58}|nevent1[a-z0-9]+)/g
|
||||
)
|
||||
const matches = content.match(/nostr:(note1[a-z0-9]{58}|nevent1[a-z0-9]+)/g)
|
||||
|
||||
for (const m of matches || []) {
|
||||
try {
|
||||
const id = m.split(':')[1]
|
||||
const { type, data } = nip19.decode(id)
|
||||
if (type === 'nprofile') {
|
||||
pubkeySet.add(data.pubkey)
|
||||
} else if (type === 'npub') {
|
||||
pubkeySet.add(data)
|
||||
} else if (['nevent', 'note', 'naddr'].includes(type)) {
|
||||
const event = await client.fetchEvent(id)
|
||||
if (event) {
|
||||
pubkeySet.add(event.pubkey)
|
||||
quoteEventIdSet.add(event.id)
|
||||
}
|
||||
if (type === 'nevent') {
|
||||
quoteEventIdSet.add(data.id)
|
||||
} else if (type === 'note') {
|
||||
quoteEventIdSet.add(data)
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
}
|
||||
}
|
||||
|
||||
pubkeySet.add(parentEvent.pubkey)
|
||||
|
||||
return {
|
||||
pubkeys: Array.from(pubkeySet),
|
||||
quoteEventIds: Array.from(quoteEventIdSet),
|
||||
rootEventId,
|
||||
rootEventKind,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue