feat: zap (#107)
This commit is contained in:
@@ -3,11 +3,13 @@ import client from '@/services/client.service'
|
||||
import { TImageInfo, TRelayList } from '@/types'
|
||||
import { LRUCache } from 'lru-cache'
|
||||
import { Event, kinds, nip19 } from 'nostr-tools'
|
||||
import { getAmountFromInvoice, getLightningAddressFromProfile } from './lightning'
|
||||
import { formatPubkey } from './pubkey'
|
||||
import { extractImageInfoFromTag, isReplyETag, isRootETag, tagNameEquals } from './tag'
|
||||
import { isWebsocketUrl, normalizeHttpUrl, normalizeUrl } from './url'
|
||||
|
||||
const EVENT_EMBEDDED_EVENT_IDS_CACHE = new LRUCache<string, string[]>({ max: 10000 })
|
||||
const EVENT_IS_REPLY_NOTE_CACHE = new LRUCache<string, boolean>({ max: 10000 })
|
||||
|
||||
export function isNsfwEvent(event: Event) {
|
||||
return event.tags.some(
|
||||
@@ -19,15 +21,23 @@ export function isNsfwEvent(event: Event) {
|
||||
export function isReplyNoteEvent(event: Event) {
|
||||
if (event.kind !== kinds.ShortTextNote) return false
|
||||
|
||||
const cache = EVENT_IS_REPLY_NOTE_CACHE.get(event.id)
|
||||
if (cache !== undefined) return cache
|
||||
|
||||
const mentionsEventIds: string[] = []
|
||||
for (const [tagName, eventId, , marker] of event.tags) {
|
||||
if (tagName !== 'e' || !eventId) continue
|
||||
|
||||
mentionsEventIds.push(eventId)
|
||||
if (['root', 'reply'].includes(marker)) return true
|
||||
if (['root', 'reply'].includes(marker)) {
|
||||
EVENT_IS_REPLY_NOTE_CACHE.set(event.id, true)
|
||||
return true
|
||||
}
|
||||
}
|
||||
const embeddedEventIds = extractEmbeddedEventIds(event)
|
||||
return mentionsEventIds.some((id) => !embeddedEventIds.includes(id))
|
||||
const result = mentionsEventIds.some((id) => !embeddedEventIds.includes(id))
|
||||
EVENT_IS_REPLY_NOTE_CACHE.set(event.id, result)
|
||||
return result
|
||||
}
|
||||
|
||||
export function isCommentEvent(event: Event) {
|
||||
@@ -159,6 +169,9 @@ export function getProfileFromProfileEvent(event: Event) {
|
||||
nip05: profileObj.nip05,
|
||||
about: profileObj.about,
|
||||
website: profileObj.website ? normalizeHttpUrl(profileObj.website) : undefined,
|
||||
lud06: profileObj.lud06,
|
||||
lud16: profileObj.lud16,
|
||||
lightningAddress: getLightningAddressFromProfile(profileObj),
|
||||
created_at: event.created_at
|
||||
}
|
||||
} catch (err) {
|
||||
@@ -363,6 +376,68 @@ export function extractEmbeddedNotesFromContent(content: string) {
|
||||
return { embeddedNotes, contentWithoutEmbeddedNotes: c }
|
||||
}
|
||||
|
||||
export function extractZapInfoFromReceipt(receiptEvent: Event) {
|
||||
if (receiptEvent.kind !== kinds.Zap) return null
|
||||
|
||||
let senderPubkey: string | undefined
|
||||
let recipientPubkey: string | undefined
|
||||
let eventId: string | undefined
|
||||
let invoice: string | undefined
|
||||
let amount: number | undefined
|
||||
let comment: string | undefined
|
||||
let description: string | undefined
|
||||
let preimage: string | undefined
|
||||
try {
|
||||
receiptEvent.tags.forEach(([tagName, tagValue]) => {
|
||||
switch (tagName) {
|
||||
case 'P':
|
||||
senderPubkey = tagValue
|
||||
break
|
||||
case 'p':
|
||||
recipientPubkey = tagValue
|
||||
break
|
||||
case 'e':
|
||||
eventId = tagValue
|
||||
break
|
||||
case 'bolt11':
|
||||
invoice = tagValue
|
||||
break
|
||||
case 'description':
|
||||
description = tagValue
|
||||
break
|
||||
case 'preimage':
|
||||
preimage = tagValue
|
||||
break
|
||||
}
|
||||
})
|
||||
if (!recipientPubkey || !invoice) return null
|
||||
amount = invoice ? getAmountFromInvoice(invoice) : 0
|
||||
if (description) {
|
||||
try {
|
||||
const zapRequest = JSON.parse(description)
|
||||
comment = zapRequest.content
|
||||
if (!senderPubkey) {
|
||||
senderPubkey = zapRequest.pubkey
|
||||
}
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
senderPubkey,
|
||||
recipientPubkey,
|
||||
eventId,
|
||||
invoice,
|
||||
amount,
|
||||
comment,
|
||||
preimage
|
||||
}
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
export function extractEmbeddedEventIds(event: Event) {
|
||||
const cache = EVENT_EMBEDDED_EVENT_IDS_CACHE.get(event.id)
|
||||
if (cache) return cache
|
||||
|
||||
Reference in New Issue
Block a user