refactor
This commit is contained in:
264
src/lib/event-metadata.ts
Normal file
264
src/lib/event-metadata.ts
Normal file
@@ -0,0 +1,264 @@
|
||||
import { BIG_RELAY_URLS } from '@/constants'
|
||||
import { TRelayList, TRelaySet } from '@/types'
|
||||
import { Event, kinds } from 'nostr-tools'
|
||||
import { getReplaceableEventIdentifier } from './event'
|
||||
import { getAmountFromInvoice, getLightningAddressFromProfile } from './lightning'
|
||||
import { formatPubkey, pubkeyToNpub } from './pubkey'
|
||||
import { generateBech32IdFromETag, tagNameEquals } from './tag'
|
||||
import { isWebsocketUrl, normalizeHttpUrl, normalizeUrl } from './url'
|
||||
import { isTorBrowser } from './utils'
|
||||
|
||||
export function getRelayListFromEvent(event?: Event) {
|
||||
if (!event) {
|
||||
return { write: BIG_RELAY_URLS, read: BIG_RELAY_URLS, originalRelays: [] }
|
||||
}
|
||||
|
||||
const torBrowserDetected = isTorBrowser()
|
||||
const relayList = { write: [], read: [], originalRelays: [] } as TRelayList
|
||||
event.tags.filter(tagNameEquals('r')).forEach(([, url, type]) => {
|
||||
if (!url || !isWebsocketUrl(url)) return
|
||||
|
||||
const normalizedUrl = normalizeUrl(url)
|
||||
if (!normalizedUrl) return
|
||||
|
||||
const scope = type === 'read' ? 'read' : type === 'write' ? 'write' : 'both'
|
||||
relayList.originalRelays.push({ url: normalizedUrl, scope })
|
||||
|
||||
// Filter out .onion URLs if not using Tor browser
|
||||
if (normalizedUrl.endsWith('.onion/') && !torBrowserDetected) return
|
||||
|
||||
if (type === 'write') {
|
||||
relayList.write.push(normalizedUrl)
|
||||
} else if (type === 'read') {
|
||||
relayList.read.push(normalizedUrl)
|
||||
} else {
|
||||
relayList.write.push(normalizedUrl)
|
||||
relayList.read.push(normalizedUrl)
|
||||
}
|
||||
})
|
||||
|
||||
// If there are too many relays, use the default BIG_RELAY_URLS
|
||||
// Because they don't know anything about relays, their settings cannot be trusted
|
||||
return {
|
||||
write: relayList.write.length && relayList.write.length <= 8 ? relayList.write : BIG_RELAY_URLS,
|
||||
read: relayList.read.length && relayList.write.length <= 8 ? relayList.read : BIG_RELAY_URLS,
|
||||
originalRelays: relayList.originalRelays
|
||||
}
|
||||
}
|
||||
|
||||
export function getProfileFromEvent(event: Event) {
|
||||
try {
|
||||
const profileObj = JSON.parse(event.content)
|
||||
const username =
|
||||
profileObj.display_name?.trim() ||
|
||||
profileObj.name?.trim() ||
|
||||
profileObj.nip05?.split('@')[0]?.trim()
|
||||
return {
|
||||
pubkey: event.pubkey,
|
||||
npub: pubkeyToNpub(event.pubkey) ?? '',
|
||||
banner: profileObj.banner,
|
||||
avatar: profileObj.picture,
|
||||
username: username || formatPubkey(event.pubkey),
|
||||
original_username: username,
|
||||
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) {
|
||||
console.error(err)
|
||||
return {
|
||||
pubkey: event.pubkey,
|
||||
npub: pubkeyToNpub(event.pubkey) ?? '',
|
||||
username: formatPubkey(event.pubkey)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function getRelaySetFromEvent(event: Event): TRelaySet {
|
||||
const id = getReplaceableEventIdentifier(event)
|
||||
const relayUrls = event.tags
|
||||
.filter(tagNameEquals('relay'))
|
||||
.map((tag) => tag[1])
|
||||
.filter((url) => url && isWebsocketUrl(url))
|
||||
.map((url) => normalizeUrl(url))
|
||||
|
||||
let name = event.tags.find(tagNameEquals('title'))?.[1]
|
||||
if (!name) {
|
||||
name = id
|
||||
}
|
||||
|
||||
return { id, name, relayUrls }
|
||||
}
|
||||
|
||||
export function getZapInfoFromEvent(receiptEvent: Event) {
|
||||
if (receiptEvent.kind !== kinds.Zap) return null
|
||||
|
||||
let senderPubkey: string | undefined
|
||||
let recipientPubkey: string | undefined
|
||||
let originalEventId: 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((tag) => {
|
||||
const [tagName, tagValue] = tag
|
||||
switch (tagName) {
|
||||
case 'P':
|
||||
senderPubkey = tagValue
|
||||
break
|
||||
case 'p':
|
||||
recipientPubkey = tagValue
|
||||
break
|
||||
case 'e':
|
||||
originalEventId = tag[1]
|
||||
eventId = generateBech32IdFromETag(tag)
|
||||
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,
|
||||
originalEventId,
|
||||
invoice,
|
||||
amount,
|
||||
comment,
|
||||
preimage
|
||||
}
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
export function getLongFormArticleMetadataFromEvent(event: Event) {
|
||||
let title: string | undefined
|
||||
let summary: string | undefined
|
||||
let image: string | undefined
|
||||
const tags = new Set<string>()
|
||||
|
||||
event.tags.forEach(([tagName, tagValue]) => {
|
||||
if (tagName === 'title') {
|
||||
title = tagValue
|
||||
} else if (tagName === 'summary') {
|
||||
summary = tagValue
|
||||
} else if (tagName === 'image') {
|
||||
image = tagValue
|
||||
} else if (tagName === 't' && tagValue && tags.size < 6) {
|
||||
tags.add(tagValue.toLocaleLowerCase())
|
||||
}
|
||||
})
|
||||
|
||||
if (!title) {
|
||||
title = event.tags.find(tagNameEquals('d'))?.[1] ?? 'no title'
|
||||
}
|
||||
|
||||
return { title, summary, image, tags: Array.from(tags) }
|
||||
}
|
||||
|
||||
export function getLiveEventMetadataFromEvent(event: Event) {
|
||||
let title: string | undefined
|
||||
let summary: string | undefined
|
||||
let image: string | undefined
|
||||
let status: string | undefined
|
||||
const tags = new Set<string>()
|
||||
|
||||
event.tags.forEach(([tagName, tagValue]) => {
|
||||
if (tagName === 'title') {
|
||||
title = tagValue
|
||||
} else if (tagName === 'summary') {
|
||||
summary = tagValue
|
||||
} else if (tagName === 'image') {
|
||||
image = tagValue
|
||||
} else if (tagName === 'status') {
|
||||
status = tagValue
|
||||
} else if (tagName === 't' && tagValue && tags.size < 6) {
|
||||
tags.add(tagValue.toLocaleLowerCase())
|
||||
}
|
||||
})
|
||||
|
||||
if (!title) {
|
||||
title = event.tags.find(tagNameEquals('d'))?.[1] ?? 'no title'
|
||||
}
|
||||
|
||||
return { title, summary, image, status, tags: Array.from(tags) }
|
||||
}
|
||||
|
||||
export function getGroupMetadataFromEvent(event: Event) {
|
||||
let d: string | undefined
|
||||
let name: string | undefined
|
||||
let about: string | undefined
|
||||
let picture: string | undefined
|
||||
const tags = new Set<string>()
|
||||
|
||||
event.tags.forEach(([tagName, tagValue]) => {
|
||||
if (tagName === 'name') {
|
||||
name = tagValue
|
||||
} else if (tagName === 'about') {
|
||||
about = tagValue
|
||||
} else if (tagName === 'picture') {
|
||||
picture = tagValue
|
||||
} else if (tagName === 't' && tagValue) {
|
||||
tags.add(tagValue.toLocaleLowerCase())
|
||||
} else if (tagName === 'd') {
|
||||
d = tagValue
|
||||
}
|
||||
})
|
||||
|
||||
if (!name) {
|
||||
name = d ?? 'no name'
|
||||
}
|
||||
|
||||
return { d, name, about, picture, tags: Array.from(tags) }
|
||||
}
|
||||
|
||||
export function getCommunityDefinitionFromEvent(event: Event) {
|
||||
let name: string | undefined
|
||||
let description: string | undefined
|
||||
let image: string | undefined
|
||||
|
||||
event.tags.forEach(([tagName, tagValue]) => {
|
||||
if (tagName === 'name') {
|
||||
name = tagValue
|
||||
} else if (tagName === 'description') {
|
||||
description = tagValue
|
||||
} else if (tagName === 'image') {
|
||||
image = tagValue
|
||||
}
|
||||
})
|
||||
|
||||
if (!name) {
|
||||
name = event.tags.find(tagNameEquals('d'))?.[1] ?? 'no name'
|
||||
}
|
||||
|
||||
return { name, description, image }
|
||||
}
|
||||
Reference in New Issue
Block a user