feat: support kind 20
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { COMMENT_EVENT_KIND, PICTURE_EVENT_KIND } from '@/constants'
|
||||
import client from '@/services/client.service'
|
||||
import { Event, kinds, nip19 } from 'nostr-tools'
|
||||
import { isReplyETag, isRootETag, tagNameEquals } from './tag'
|
||||
import { extractImetaUrlFromTag, isReplyETag, isRootETag, tagNameEquals } from './tag'
|
||||
|
||||
export function isNsfwEvent(event: Event) {
|
||||
return event.tags.some(
|
||||
@@ -26,6 +27,14 @@ export function isReplyNoteEvent(event: Event) {
|
||||
return hasETag && !hasMarker
|
||||
}
|
||||
|
||||
export function isCommentEvent(event: Event) {
|
||||
return event.kind === COMMENT_EVENT_KIND
|
||||
}
|
||||
|
||||
export function isPictureEvent(event: Event) {
|
||||
return event.kind === PICTURE_EVENT_KIND
|
||||
}
|
||||
|
||||
export function getParentEventId(event?: Event) {
|
||||
return event?.tags.find(isReplyETag)?.[1]
|
||||
}
|
||||
@@ -116,6 +125,54 @@ 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
|
||||
const rootEventPubkey = parentEvent.tags.find(tagNameEquals('P'))?.[1] ?? parentEvent.pubkey
|
||||
const parentEventId = parentEvent.id
|
||||
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
|
||||
)
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
}
|
||||
}
|
||||
|
||||
pubkeySet.add(parentEvent.pubkey)
|
||||
|
||||
return {
|
||||
pubkeys: Array.from(pubkeySet),
|
||||
quoteEventIds: Array.from(quoteEventIdSet),
|
||||
rootEventId,
|
||||
rootEventKind,
|
||||
rootEventPubkey,
|
||||
parentEventId,
|
||||
parentEventKind,
|
||||
parentEventPubkey
|
||||
}
|
||||
}
|
||||
|
||||
export function extractHashtags(content: string) {
|
||||
const hashtags: string[] = []
|
||||
const matches = content.match(/#[\p{L}\p{N}\p{M}]+/gu)
|
||||
@@ -127,3 +184,22 @@ export function extractHashtags(content: string) {
|
||||
})
|
||||
return hashtags
|
||||
}
|
||||
|
||||
export function extractFirstPictureFromPictureEvent(event: Event) {
|
||||
if (!isPictureEvent(event)) return null
|
||||
for (const tag of event.tags) {
|
||||
const url = extractImetaUrlFromTag(tag)
|
||||
if (url) return url
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
export function extractImagesFromContent(content: string) {
|
||||
const images = content.match(/https?:\/\/[^\s"']+\.(jpg|jpeg|png|gif|webp|heic)/gi)
|
||||
let contentWithoutImages = content
|
||||
images?.forEach((url) => {
|
||||
contentWithoutImages = contentWithoutImages.replace(url, '').trim()
|
||||
})
|
||||
contentWithoutImages = contentWithoutImages.replace(/\n{3,}/g, '\n\n').trim()
|
||||
return { images, contentWithoutImages }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user