refactor
This commit is contained in:
@@ -1,14 +1,14 @@
|
|||||||
import { Button } from '@/components/ui/button'
|
import { Button } from '@/components/ui/button'
|
||||||
import { Separator } from '@/components/ui/separator'
|
import { Separator } from '@/components/ui/separator'
|
||||||
import { isDevEnv } from '@/lib/common'
|
import { isDevEnv } from '@/lib/utils'
|
||||||
import { useNostr } from '@/providers/NostrProvider'
|
import { useNostr } from '@/providers/NostrProvider'
|
||||||
import { useTheme } from '@/providers/ThemeProvider'
|
import { useTheme } from '@/providers/ThemeProvider'
|
||||||
import { NstartModal } from 'nstart-modal'
|
import { NstartModal } from 'nstart-modal'
|
||||||
import { useState } from 'react'
|
import { useState } from 'react'
|
||||||
import { useTranslation } from 'react-i18next'
|
import { useTranslation } from 'react-i18next'
|
||||||
import AccountList from '../AccountList'
|
import AccountList from '../AccountList'
|
||||||
import NostrConnectLogin from './NostrConnectionLogin'
|
|
||||||
import GenerateNewAccount from './GenerateNewAccount'
|
import GenerateNewAccount from './GenerateNewAccount'
|
||||||
|
import NostrConnectLogin from './NostrConnectionLogin'
|
||||||
import NpubLogin from './NpubLogin'
|
import NpubLogin from './NpubLogin'
|
||||||
import PrivateKeyLogin from './PrivateKeyLogin'
|
import PrivateKeyLogin from './PrivateKeyLogin'
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { useFetchEvent } from '@/hooks'
|
import { useFetchEvent } from '@/hooks'
|
||||||
import { generateEventIdFromETag } from '@/lib/tag'
|
import { generateBech32IdFromETag } from '@/lib/tag'
|
||||||
import { useNostr } from '@/providers/NostrProvider'
|
import { useNostr } from '@/providers/NostrProvider'
|
||||||
import { useEffect, useMemo, useRef, useState } from 'react'
|
import { useEffect, useMemo, useRef, useState } from 'react'
|
||||||
import { useTranslation } from 'react-i18next'
|
import { useTranslation } from 'react-i18next'
|
||||||
@@ -15,7 +15,7 @@ export default function BookmarkList() {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
bookmarkListEvent.tags
|
bookmarkListEvent.tags
|
||||||
.map((tag) => (tag[0] === 'e' ? generateEventIdFromETag(tag) : undefined))
|
.map((tag) => (tag[0] === 'e' ? generateBech32IdFromETag(tag) : undefined))
|
||||||
.filter(Boolean) as `nevent1${string}`[]
|
.filter(Boolean) as `nevent1${string}`[]
|
||||||
).reverse()
|
).reverse()
|
||||||
}, [bookmarkListEvent])
|
}, [bookmarkListEvent])
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import { Dialog, DialogContent, DialogTrigger } from '@/components/ui/dialog'
|
|||||||
import { Drawer, DrawerContent, DrawerOverlay, DrawerTrigger } from '@/components/ui/drawer'
|
import { Drawer, DrawerContent, DrawerOverlay, DrawerTrigger } from '@/components/ui/drawer'
|
||||||
import { Separator } from '@/components/ui/separator'
|
import { Separator } from '@/components/ui/separator'
|
||||||
import { ExtendedKind } from '@/constants'
|
import { ExtendedKind } from '@/constants'
|
||||||
import { getReplaceableEventIdentifier, getSharableEventId } from '@/lib/event'
|
import { getReplaceableEventIdentifier, getNoteBech32Id } from '@/lib/event'
|
||||||
import { toChachiChat } from '@/lib/link'
|
import { toChachiChat } from '@/lib/link'
|
||||||
import { useScreenSize } from '@/providers/ScreenSizeProvider'
|
import { useScreenSize } from '@/providers/ScreenSizeProvider'
|
||||||
import clientService from '@/services/client.service'
|
import clientService from '@/services/client.service'
|
||||||
@@ -139,7 +139,7 @@ export default function ClientSelect({
|
|||||||
<ClientSelectItem
|
<ClientSelectItem
|
||||||
key={clientId}
|
key={clientId}
|
||||||
onClick={() => setOpen(false)}
|
onClick={() => setOpen(false)}
|
||||||
href={client.getUrl(originalNoteId ?? getSharableEventId(event!))}
|
href={client.getUrl(originalNoteId ?? getNoteBech32Id(event!))}
|
||||||
name={client.name}
|
name={client.name}
|
||||||
/>
|
/>
|
||||||
)
|
)
|
||||||
@@ -150,7 +150,7 @@ export default function ClientSelect({
|
|||||||
variant="ghost"
|
variant="ghost"
|
||||||
className="w-full py-6 font-semibold"
|
className="w-full py-6 font-semibold"
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
navigator.clipboard.writeText(originalNoteId ?? getSharableEventId(event!))
|
navigator.clipboard.writeText(originalNoteId ?? getNoteBech32Id(event!))
|
||||||
setOpen(false)
|
setOpen(false)
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
|
|||||||
@@ -11,8 +11,8 @@ import {
|
|||||||
EmbeddedWebsocketUrlParser,
|
EmbeddedWebsocketUrlParser,
|
||||||
parseContent
|
parseContent
|
||||||
} from '@/lib/content-parser'
|
} from '@/lib/content-parser'
|
||||||
import { extractEmojiInfosFromTags } from '@/lib/event'
|
import { getImageInfosFromEvent } from '@/lib/event'
|
||||||
import { extractImageInfoFromTag } from '@/lib/tag'
|
import { getEmojiInfosFromEmojiTags, getImageInfoFromImetaTag } from '@/lib/tag'
|
||||||
import { cn } from '@/lib/utils'
|
import { cn } from '@/lib/utils'
|
||||||
import mediaUpload from '@/services/media-upload.service'
|
import mediaUpload from '@/services/media-upload.service'
|
||||||
import { TImageInfo } from '@/types'
|
import { TImageInfo } from '@/types'
|
||||||
@@ -45,9 +45,7 @@ const Content = memo(({ event, className }: { event: Event; className?: string }
|
|||||||
EmbeddedEmojiParser
|
EmbeddedEmojiParser
|
||||||
])
|
])
|
||||||
|
|
||||||
const imageInfos = event.tags
|
const imageInfos = getImageInfosFromEvent(event)
|
||||||
.map((tag) => extractImageInfoFromTag(tag, event.pubkey))
|
|
||||||
.filter(Boolean) as TImageInfo[]
|
|
||||||
const allImages = nodes
|
const allImages = nodes
|
||||||
.map((node) => {
|
.map((node) => {
|
||||||
if (node.type === 'image') {
|
if (node.type === 'image') {
|
||||||
@@ -57,7 +55,7 @@ const Content = memo(({ event, className }: { event: Event; className?: string }
|
|||||||
}
|
}
|
||||||
const tag = mediaUpload.getImetaTagByUrl(node.data)
|
const tag = mediaUpload.getImetaTagByUrl(node.data)
|
||||||
return tag
|
return tag
|
||||||
? extractImageInfoFromTag(tag, event.pubkey)
|
? getImageInfoFromImetaTag(tag, event.pubkey)
|
||||||
: { url: node.data, pubkey: event.pubkey }
|
: { url: node.data, pubkey: event.pubkey }
|
||||||
}
|
}
|
||||||
if (node.type === 'images') {
|
if (node.type === 'images') {
|
||||||
@@ -73,7 +71,7 @@ const Content = memo(({ event, className }: { event: Event; className?: string }
|
|||||||
.flat() as TImageInfo[]
|
.flat() as TImageInfo[]
|
||||||
let imageIndex = 0
|
let imageIndex = 0
|
||||||
|
|
||||||
const emojiInfos = extractEmojiInfosFromTags(event.tags)
|
const emojiInfos = getEmojiInfosFromEmojiTags(event.tags)
|
||||||
|
|
||||||
const lastNormalUrlNode = nodes.findLast((node) => node.type === 'url')
|
const lastNormalUrlNode = nodes.findLast((node) => node.type === 'url')
|
||||||
const lastNormalUrl =
|
const lastNormalUrl =
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { getCommunityDefinition } from '@/lib/event'
|
import { getCommunityDefinitionFromEvent } from '@/lib/event-metadata'
|
||||||
import { cn } from '@/lib/utils'
|
import { cn } from '@/lib/utils'
|
||||||
import { Event } from 'nostr-tools'
|
import { Event } from 'nostr-tools'
|
||||||
import { useMemo } from 'react'
|
import { useMemo } from 'react'
|
||||||
@@ -14,7 +14,7 @@ export default function CommunityDefinitionPreview({
|
|||||||
onClick?: React.MouseEventHandler<HTMLDivElement> | undefined
|
onClick?: React.MouseEventHandler<HTMLDivElement> | undefined
|
||||||
}) {
|
}) {
|
||||||
const { t } = useTranslation()
|
const { t } = useTranslation()
|
||||||
const metadata = useMemo(() => getCommunityDefinition(event), [event])
|
const metadata = useMemo(() => getCommunityDefinitionFromEvent(event), [event])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={cn('pointer-events-none', className)} onClick={onClick}>
|
<div className={cn('pointer-events-none', className)} onClick={onClick}>
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { getGroupMetadata } from '@/lib/event'
|
import { getGroupMetadataFromEvent } from '@/lib/event-metadata'
|
||||||
import { cn } from '@/lib/utils'
|
import { cn } from '@/lib/utils'
|
||||||
import { Event } from 'nostr-tools'
|
import { Event } from 'nostr-tools'
|
||||||
import { useMemo } from 'react'
|
import { useMemo } from 'react'
|
||||||
@@ -14,7 +14,7 @@ export default function GroupMetadataPreview({
|
|||||||
onClick?: React.MouseEventHandler<HTMLDivElement> | undefined
|
onClick?: React.MouseEventHandler<HTMLDivElement> | undefined
|
||||||
}) {
|
}) {
|
||||||
const { t } = useTranslation()
|
const { t } = useTranslation()
|
||||||
const metadata = useMemo(() => getGroupMetadata(event), [event])
|
const metadata = useMemo(() => getGroupMetadataFromEvent(event), [event])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={cn('pointer-events-none', className)} onClick={onClick}>
|
<div className={cn('pointer-events-none', className)} onClick={onClick}>
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { getLiveEventMetadata } from '@/lib/event'
|
import { getLiveEventMetadataFromEvent } from '@/lib/event-metadata'
|
||||||
import { cn } from '@/lib/utils'
|
import { cn } from '@/lib/utils'
|
||||||
import { Event } from 'nostr-tools'
|
import { Event } from 'nostr-tools'
|
||||||
import { useMemo } from 'react'
|
import { useMemo } from 'react'
|
||||||
@@ -14,7 +14,7 @@ export default function LiveEventPreview({
|
|||||||
onClick?: React.MouseEventHandler<HTMLDivElement> | undefined
|
onClick?: React.MouseEventHandler<HTMLDivElement> | undefined
|
||||||
}) {
|
}) {
|
||||||
const { t } = useTranslation()
|
const { t } = useTranslation()
|
||||||
const metadata = useMemo(() => getLiveEventMetadata(event), [event])
|
const metadata = useMemo(() => getLiveEventMetadataFromEvent(event), [event])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={cn('pointer-events-none', className)} onClick={onClick}>
|
<div className={cn('pointer-events-none', className)} onClick={onClick}>
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { getLongFormArticleMetadata } from '@/lib/event'
|
import { getLongFormArticleMetadataFromEvent } from '@/lib/event-metadata'
|
||||||
import { cn } from '@/lib/utils'
|
import { cn } from '@/lib/utils'
|
||||||
import { Event } from 'nostr-tools'
|
import { Event } from 'nostr-tools'
|
||||||
import { useMemo } from 'react'
|
import { useMemo } from 'react'
|
||||||
@@ -14,7 +14,7 @@ export default function LongFormArticlePreview({
|
|||||||
onClick?: React.MouseEventHandler<HTMLDivElement> | undefined
|
onClick?: React.MouseEventHandler<HTMLDivElement> | undefined
|
||||||
}) {
|
}) {
|
||||||
const { t } = useTranslation()
|
const { t } = useTranslation()
|
||||||
const metadata = useMemo(() => getLongFormArticleMetadata(event), [event])
|
const metadata = useMemo(() => getLongFormArticleMetadataFromEvent(event), [event])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={cn('pointer-events-none', className)} onClick={onClick}>
|
<div className={cn('pointer-events-none', className)} onClick={onClick}>
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import {
|
|||||||
EmbeddedVideoParser,
|
EmbeddedVideoParser,
|
||||||
parseContent
|
parseContent
|
||||||
} from '@/lib/content-parser'
|
} from '@/lib/content-parser'
|
||||||
import { extractEmojiInfosFromTags } from '@/lib/event'
|
import { getEmojiInfosFromEmojiTags } from '@/lib/tag'
|
||||||
import { cn } from '@/lib/utils'
|
import { cn } from '@/lib/utils'
|
||||||
import { Event } from 'nostr-tools'
|
import { Event } from 'nostr-tools'
|
||||||
import { useMemo } from 'react'
|
import { useMemo } from 'react'
|
||||||
@@ -36,7 +36,7 @@ export default function NormalContentPreview({
|
|||||||
])
|
])
|
||||||
}, [event, translatedEvent])
|
}, [event, translatedEvent])
|
||||||
|
|
||||||
const emojiInfos = extractEmojiInfosFromTags(event?.tags)
|
const emojiInfos = getEmojiInfosFromEmojiTags(event?.tags)
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={cn('pointer-events-none', className)} onClick={onClick}>
|
<div className={cn('pointer-events-none', className)} onClick={onClick}>
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { Carousel, CarouselApi, CarouselContent, CarouselItem } from '@/components/ui/carousel'
|
import { Carousel, CarouselApi, CarouselContent, CarouselItem } from '@/components/ui/carousel'
|
||||||
import { isTouchDevice } from '@/lib/common'
|
import { isTouchDevice } from '@/lib/utils'
|
||||||
import { TImageInfo } from '@/types'
|
import { TImageInfo } from '@/types'
|
||||||
import { ChevronLeftIcon, ChevronRightIcon } from 'lucide-react'
|
import { ChevronLeftIcon, ChevronRightIcon } from 'lucide-react'
|
||||||
import { useEffect, useState } from 'react'
|
import { useEffect, useState } from 'react'
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { getCommunityDefinition } from '@/lib/event'
|
import { getCommunityDefinitionFromEvent } from '@/lib/event-metadata'
|
||||||
import { Event } from 'nostr-tools'
|
import { Event } from 'nostr-tools'
|
||||||
import { useMemo } from 'react'
|
import { useMemo } from 'react'
|
||||||
import ClientSelect from '../ClientSelect'
|
import ClientSelect from '../ClientSelect'
|
||||||
@@ -11,7 +11,7 @@ export default function CommunityDefinition({
|
|||||||
event: Event
|
event: Event
|
||||||
className?: string
|
className?: string
|
||||||
}) {
|
}) {
|
||||||
const metadata = useMemo(() => getCommunityDefinition(event), [event])
|
const metadata = useMemo(() => getCommunityDefinitionFromEvent(event), [event])
|
||||||
|
|
||||||
const communityNameComponent = (
|
const communityNameComponent = (
|
||||||
<div className="text-xl font-semibold line-clamp-1">{metadata.name}</div>
|
<div className="text-xl font-semibold line-clamp-1">{metadata.name}</div>
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { getGroupMetadata } from '@/lib/event'
|
import { getGroupMetadataFromEvent } from '@/lib/event-metadata'
|
||||||
import { Event } from 'nostr-tools'
|
import { Event } from 'nostr-tools'
|
||||||
import { useMemo } from 'react'
|
import { useMemo } from 'react'
|
||||||
import ClientSelect from '../ClientSelect'
|
import ClientSelect from '../ClientSelect'
|
||||||
@@ -13,7 +13,7 @@ export default function GroupMetadata({
|
|||||||
originalNoteId?: string
|
originalNoteId?: string
|
||||||
className?: string
|
className?: string
|
||||||
}) {
|
}) {
|
||||||
const metadata = useMemo(() => getGroupMetadata(event), [event])
|
const metadata = useMemo(() => getGroupMetadataFromEvent(event), [event])
|
||||||
|
|
||||||
const groupNameComponent = (
|
const groupNameComponent = (
|
||||||
<div className="text-xl font-semibold line-clamp-1">{metadata.name}</div>
|
<div className="text-xl font-semibold line-clamp-1">{metadata.name}</div>
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import { useFetchEvent, useTranslatedEvent } from '@/hooks'
|
|||||||
import { createFakeEvent } from '@/lib/event'
|
import { createFakeEvent } from '@/lib/event'
|
||||||
import { toNjump, toNote } from '@/lib/link'
|
import { toNjump, toNote } from '@/lib/link'
|
||||||
import { isValidPubkey } from '@/lib/pubkey'
|
import { isValidPubkey } from '@/lib/pubkey'
|
||||||
import { generateEventIdFromATag } from '@/lib/tag'
|
import { generateBech32IdFromATag } from '@/lib/tag'
|
||||||
import { cn } from '@/lib/utils'
|
import { cn } from '@/lib/utils'
|
||||||
import { useSecondaryPage } from '@/PageManager'
|
import { useSecondaryPage } from '@/PageManager'
|
||||||
import { Event } from 'nostr-tools'
|
import { Event } from 'nostr-tools'
|
||||||
@@ -70,7 +70,7 @@ function HighlightSource({ event }: { event: Event }) {
|
|||||||
return sourceTag[1]
|
return sourceTag[1]
|
||||||
}
|
}
|
||||||
if (sourceTag[0] === 'a') {
|
if (sourceTag[0] === 'a') {
|
||||||
return generateEventIdFromATag(sourceTag)
|
return generateBech32IdFromATag(sourceTag)
|
||||||
}
|
}
|
||||||
}, [sourceTag])
|
}, [sourceTag])
|
||||||
const pubkey = useMemo(() => {
|
const pubkey = useMemo(() => {
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { Badge } from '@/components/ui/badge'
|
import { Badge } from '@/components/ui/badge'
|
||||||
import { getLiveEventMetadata } from '@/lib/event'
|
import { getLiveEventMetadataFromEvent } from '@/lib/event-metadata'
|
||||||
import { useScreenSize } from '@/providers/ScreenSizeProvider'
|
import { useScreenSize } from '@/providers/ScreenSizeProvider'
|
||||||
import { Event } from 'nostr-tools'
|
import { Event } from 'nostr-tools'
|
||||||
import { useMemo } from 'react'
|
import { useMemo } from 'react'
|
||||||
@@ -8,7 +8,7 @@ import Image from '../Image'
|
|||||||
|
|
||||||
export default function LiveEvent({ event, className }: { event: Event; className?: string }) {
|
export default function LiveEvent({ event, className }: { event: Event; className?: string }) {
|
||||||
const { isSmallScreen } = useScreenSize()
|
const { isSmallScreen } = useScreenSize()
|
||||||
const metadata = useMemo(() => getLiveEventMetadata(event), [event])
|
const metadata = useMemo(() => getLiveEventMetadataFromEvent(event), [event])
|
||||||
|
|
||||||
const liveStatusComponent =
|
const liveStatusComponent =
|
||||||
metadata.status &&
|
metadata.status &&
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { Badge } from '@/components/ui/badge'
|
import { Badge } from '@/components/ui/badge'
|
||||||
import { getLongFormArticleMetadata } from '@/lib/event'
|
import { getLongFormArticleMetadataFromEvent } from '@/lib/event-metadata'
|
||||||
import { useScreenSize } from '@/providers/ScreenSizeProvider'
|
import { useScreenSize } from '@/providers/ScreenSizeProvider'
|
||||||
import { Event } from 'nostr-tools'
|
import { Event } from 'nostr-tools'
|
||||||
import { useMemo } from 'react'
|
import { useMemo } from 'react'
|
||||||
@@ -14,7 +14,7 @@ export default function LongFormArticle({
|
|||||||
className?: string
|
className?: string
|
||||||
}) {
|
}) {
|
||||||
const { isSmallScreen } = useScreenSize()
|
const { isSmallScreen } = useScreenSize()
|
||||||
const metadata = useMemo(() => getLongFormArticleMetadata(event), [event])
|
const metadata = useMemo(() => getLongFormArticleMetadataFromEvent(event), [event])
|
||||||
|
|
||||||
const titleComponent = <div className="text-xl font-semibold line-clamp-2">{metadata.title}</div>
|
const titleComponent = <div className="text-xl font-semibold line-clamp-2">{metadata.title}</div>
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
import { useSecondaryPage } from '@/PageManager'
|
import { useSecondaryPage } from '@/PageManager'
|
||||||
import { ExtendedKind } from '@/constants'
|
import { ExtendedKind } from '@/constants'
|
||||||
import {
|
import {
|
||||||
extractImageInfosFromEventTags,
|
getImageInfosFromEvent,
|
||||||
getParentEventId,
|
getParentBech32Id,
|
||||||
getUsingClient,
|
getUsingClient,
|
||||||
isNsfwEvent,
|
isNsfwEvent,
|
||||||
isPictureEvent
|
isPictureEvent
|
||||||
@@ -47,11 +47,11 @@ export default function Note({
|
|||||||
const { push } = useSecondaryPage()
|
const { push } = useSecondaryPage()
|
||||||
const { isSmallScreen } = useScreenSize()
|
const { isSmallScreen } = useScreenSize()
|
||||||
const parentEventId = useMemo(
|
const parentEventId = useMemo(
|
||||||
() => (hideParentNotePreview ? undefined : getParentEventId(event)),
|
() => (hideParentNotePreview ? undefined : getParentBech32Id(event)),
|
||||||
[event, hideParentNotePreview]
|
[event, hideParentNotePreview]
|
||||||
)
|
)
|
||||||
const imageInfos = useMemo(
|
const imageInfos = useMemo(
|
||||||
() => (isPictureEvent(event) ? extractImageInfosFromEventTags(event) : []),
|
() => (isPictureEvent(event) ? getImageInfosFromEvent(event) : []),
|
||||||
[event]
|
[event]
|
||||||
)
|
)
|
||||||
const usingClient = useMemo(() => getUsingClient(event), [event])
|
const usingClient = useMemo(() => getUsingClient(event), [event])
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import {
|
|||||||
DropdownMenuSeparator,
|
DropdownMenuSeparator,
|
||||||
DropdownMenuTrigger
|
DropdownMenuTrigger
|
||||||
} from '@/components/ui/dropdown-menu'
|
} from '@/components/ui/dropdown-menu'
|
||||||
import { getSharableEventId } from '@/lib/event'
|
import { getNoteBech32Id } from '@/lib/event'
|
||||||
import { toNjump } from '@/lib/link'
|
import { toNjump } from '@/lib/link'
|
||||||
import { pubkeyToNpub } from '@/lib/pubkey'
|
import { pubkeyToNpub } from '@/lib/pubkey'
|
||||||
import { useMuteList } from '@/providers/MuteListProvider'
|
import { useMuteList } from '@/providers/MuteListProvider'
|
||||||
@@ -56,7 +56,7 @@ export default function NoteOptions({ event, className }: { event: Event; classN
|
|||||||
<Button
|
<Button
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
setIsDrawerOpen(false)
|
setIsDrawerOpen(false)
|
||||||
navigator.clipboard.writeText(getSharableEventId(event))
|
navigator.clipboard.writeText(getNoteBech32Id(event))
|
||||||
}}
|
}}
|
||||||
className="w-full p-6 justify-start text-lg gap-4 [&_svg]:size-5"
|
className="w-full p-6 justify-start text-lg gap-4 [&_svg]:size-5"
|
||||||
variant="ghost"
|
variant="ghost"
|
||||||
@@ -78,7 +78,7 @@ export default function NoteOptions({ event, className }: { event: Event; classN
|
|||||||
<Button
|
<Button
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
setIsDrawerOpen(false)
|
setIsDrawerOpen(false)
|
||||||
navigator.clipboard.writeText(toNjump(getSharableEventId(event)))
|
navigator.clipboard.writeText(toNjump(getNoteBech32Id(event)))
|
||||||
}}
|
}}
|
||||||
className="w-full p-6 justify-start text-lg gap-4 [&_svg]:size-5"
|
className="w-full p-6 justify-start text-lg gap-4 [&_svg]:size-5"
|
||||||
variant="ghost"
|
variant="ghost"
|
||||||
@@ -149,9 +149,7 @@ export default function NoteOptions({ event, className }: { event: Event; classN
|
|||||||
<DropdownMenu>
|
<DropdownMenu>
|
||||||
<DropdownMenuTrigger asChild>{trigger}</DropdownMenuTrigger>
|
<DropdownMenuTrigger asChild>{trigger}</DropdownMenuTrigger>
|
||||||
<DropdownMenuContent>
|
<DropdownMenuContent>
|
||||||
<DropdownMenuItem
|
<DropdownMenuItem onClick={() => navigator.clipboard.writeText(getNoteBech32Id(event))}>
|
||||||
onClick={() => navigator.clipboard.writeText(getSharableEventId(event))}
|
|
||||||
>
|
|
||||||
<Copy />
|
<Copy />
|
||||||
{t('Copy event ID')}
|
{t('Copy event ID')}
|
||||||
</DropdownMenuItem>
|
</DropdownMenuItem>
|
||||||
@@ -162,7 +160,7 @@ export default function NoteOptions({ event, className }: { event: Event; classN
|
|||||||
{t('Copy user ID')}
|
{t('Copy user ID')}
|
||||||
</DropdownMenuItem>
|
</DropdownMenuItem>
|
||||||
<DropdownMenuItem
|
<DropdownMenuItem
|
||||||
onClick={() => navigator.clipboard.writeText(toNjump(getSharableEventId(event)))}
|
onClick={() => navigator.clipboard.writeText(toNjump(getNoteBech32Id(event)))}
|
||||||
>
|
>
|
||||||
<Link />
|
<Link />
|
||||||
{t('Copy share link')}
|
{t('Copy share link')}
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import {
|
|||||||
} from '@/components/ui/dropdown-menu'
|
} from '@/components/ui/dropdown-menu'
|
||||||
import { useNoteStatsById } from '@/hooks/useNoteStatsById'
|
import { useNoteStatsById } from '@/hooks/useNoteStatsById'
|
||||||
import { createRepostDraftEvent } from '@/lib/draft-event'
|
import { createRepostDraftEvent } from '@/lib/draft-event'
|
||||||
import { getSharableEventId } from '@/lib/event'
|
import { getNoteBech32Id } from '@/lib/event'
|
||||||
import { cn } from '@/lib/utils'
|
import { cn } from '@/lib/utils'
|
||||||
import { useNostr } from '@/providers/NostrProvider'
|
import { useNostr } from '@/providers/NostrProvider'
|
||||||
import { useScreenSize } from '@/providers/ScreenSizeProvider'
|
import { useScreenSize } from '@/providers/ScreenSizeProvider'
|
||||||
@@ -87,7 +87,7 @@ export default function RepostButton({ event }: { event: Event }) {
|
|||||||
<PostEditor
|
<PostEditor
|
||||||
open={isPostDialogOpen}
|
open={isPostDialogOpen}
|
||||||
setOpen={setIsPostDialogOpen}
|
setOpen={setIsPostDialogOpen}
|
||||||
defaultContent={'\nnostr:' + getSharableEventId(event)}
|
defaultContent={'\nnostr:' + getNoteBech32Id(event)}
|
||||||
/>
|
/>
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { useFetchEvent } from '@/hooks'
|
import { useFetchEvent } from '@/hooks'
|
||||||
import { extractZapInfoFromReceipt } from '@/lib/event'
|
import { getZapInfoFromEvent } from '@/lib/event-metadata'
|
||||||
import { formatAmount } from '@/lib/lightning'
|
import { formatAmount } from '@/lib/lightning'
|
||||||
import { toNote, toProfile } from '@/lib/link'
|
import { toNote, toProfile } from '@/lib/link'
|
||||||
import { cn } from '@/lib/utils'
|
import { cn } from '@/lib/utils'
|
||||||
@@ -24,7 +24,7 @@ export function ZapNotification({
|
|||||||
const { push } = useSecondaryPage()
|
const { push } = useSecondaryPage()
|
||||||
const { pubkey } = useNostr()
|
const { pubkey } = useNostr()
|
||||||
const { senderPubkey, eventId, amount, comment } = useMemo(
|
const { senderPubkey, eventId, amount, comment } = useMemo(
|
||||||
() => extractZapInfoFromReceipt(notification) ?? ({} as any),
|
() => getZapInfoFromEvent(notification) ?? ({} as any),
|
||||||
[notification]
|
[notification]
|
||||||
)
|
)
|
||||||
const { event, isFetching } = useFetchEvent(eventId)
|
const { event, isFetching } = useFetchEvent(eventId)
|
||||||
|
|||||||
@@ -7,7 +7,8 @@ import {
|
|||||||
EmbeddedWebsocketUrlParser,
|
EmbeddedWebsocketUrlParser,
|
||||||
parseContent
|
parseContent
|
||||||
} from '@/lib/content-parser'
|
} from '@/lib/content-parser'
|
||||||
import { extractEmojiInfosFromTags, extractImageInfosFromEventTags } from '@/lib/event'
|
import { getImageInfosFromEvent } from '@/lib/event'
|
||||||
|
import { getEmojiInfosFromEmojiTags } from '@/lib/tag'
|
||||||
import { cn } from '@/lib/utils'
|
import { cn } from '@/lib/utils'
|
||||||
import { Event } from 'nostr-tools'
|
import { Event } from 'nostr-tools'
|
||||||
import { memo, useMemo } from 'react'
|
import { memo, useMemo } from 'react'
|
||||||
@@ -22,7 +23,7 @@ import Emoji from '../Emoji'
|
|||||||
import { ImageCarousel } from '../ImageCarousel'
|
import { ImageCarousel } from '../ImageCarousel'
|
||||||
|
|
||||||
const PictureContent = memo(({ event, className }: { event: Event; className?: string }) => {
|
const PictureContent = memo(({ event, className }: { event: Event; className?: string }) => {
|
||||||
const images = useMemo(() => extractImageInfosFromEventTags(event), [event])
|
const images = useMemo(() => getImageInfosFromEvent(event), [event])
|
||||||
|
|
||||||
const nodes = parseContent(event.content, [
|
const nodes = parseContent(event.content, [
|
||||||
EmbeddedNormalUrlParser,
|
EmbeddedNormalUrlParser,
|
||||||
@@ -33,7 +34,7 @@ const PictureContent = memo(({ event, className }: { event: Event; className?: s
|
|||||||
EmbeddedEmojiParser
|
EmbeddedEmojiParser
|
||||||
])
|
])
|
||||||
|
|
||||||
const emojiInfos = extractEmojiInfosFromTags(event.tags)
|
const emojiInfos = getEmojiInfosFromEmojiTags(event.tags)
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={cn('text-wrap break-words whitespace-pre-wrap space-y-2', className)}>
|
<div className={cn('text-wrap break-words whitespace-pre-wrap space-y-2', className)}>
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { EmbeddedHashtagParser, EmbeddedMentionParser, parseContent } from '@/lib/content-parser'
|
import { EmbeddedHashtagParser, EmbeddedMentionParser, parseContent } from '@/lib/content-parser'
|
||||||
import { extractImageInfosFromEventTags } from '@/lib/event'
|
import { getImageInfosFromEvent } from '@/lib/event'
|
||||||
import { toNote } from '@/lib/link'
|
import { toNote } from '@/lib/link'
|
||||||
import { tagNameEquals } from '@/lib/tag'
|
import { tagNameEquals } from '@/lib/tag'
|
||||||
import { cn } from '@/lib/utils'
|
import { cn } from '@/lib/utils'
|
||||||
@@ -21,7 +21,7 @@ export default function PictureNoteCard({
|
|||||||
className?: string
|
className?: string
|
||||||
}) {
|
}) {
|
||||||
const { push } = useSecondaryPage()
|
const { push } = useSecondaryPage()
|
||||||
const images = useMemo(() => extractImageInfosFromEventTags(event), [event])
|
const images = useMemo(() => getImageInfosFromEvent(event), [event])
|
||||||
const title = useMemo(() => {
|
const title = useMemo(() => {
|
||||||
const nodes = parseContent(event.tags.find(tagNameEquals('title'))?.[1] ?? event.content, [
|
const nodes = parseContent(event.tags.find(tagNameEquals('title'))?.[1] ?? event.content, [
|
||||||
EmbeddedMentionParser,
|
EmbeddedMentionParser,
|
||||||
|
|||||||
@@ -1,14 +1,14 @@
|
|||||||
import { Button } from '@/components/ui/button'
|
import { Button } from '@/components/ui/button'
|
||||||
import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover'
|
import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover'
|
||||||
import { extractMentions } from '@/lib/event'
|
import { useMuteList } from '@/providers/MuteListProvider'
|
||||||
import { useNostr } from '@/providers/NostrProvider'
|
import { useNostr } from '@/providers/NostrProvider'
|
||||||
|
import client from '@/services/client.service'
|
||||||
import { Check } from 'lucide-react'
|
import { Check } from 'lucide-react'
|
||||||
import { Event } from 'nostr-tools'
|
import { Event, nip19 } from 'nostr-tools'
|
||||||
import { HTMLAttributes, useEffect, useState } from 'react'
|
import { HTMLAttributes, useEffect, useState } from 'react'
|
||||||
import { useTranslation } from 'react-i18next'
|
import { useTranslation } from 'react-i18next'
|
||||||
import { SimpleUserAvatar } from '../UserAvatar'
|
import { SimpleUserAvatar } from '../UserAvatar'
|
||||||
import { SimpleUsername } from '../Username'
|
import { SimpleUsername } from '../Username'
|
||||||
import { useMuteList } from '@/providers/MuteListProvider'
|
|
||||||
|
|
||||||
export default function Mentions({
|
export default function Mentions({
|
||||||
content,
|
content,
|
||||||
@@ -131,3 +131,50 @@ function PopoverCheckboxItem({
|
|||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function extractMentions(content: string, parentEvent?: Event) {
|
||||||
|
const parentEventPubkey = parentEvent ? parentEvent.pubkey : undefined
|
||||||
|
const pubkeys: string[] = []
|
||||||
|
const relatedPubkeys: 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
|
||||||
|
)
|
||||||
|
|
||||||
|
const addToSet = (arr: string[], pubkey: string) => {
|
||||||
|
if (pubkey === parentEventPubkey) return
|
||||||
|
if (!arr.includes(pubkey)) arr.push(pubkey)
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const m of matches || []) {
|
||||||
|
try {
|
||||||
|
const id = m.split(':')[1]
|
||||||
|
const { type, data } = nip19.decode(id)
|
||||||
|
if (type === 'nprofile') {
|
||||||
|
addToSet(pubkeys, data.pubkey)
|
||||||
|
} else if (type === 'npub') {
|
||||||
|
addToSet(pubkeys, data)
|
||||||
|
} else if (['nevent', 'note'].includes(type)) {
|
||||||
|
const event = await client.fetchEvent(id)
|
||||||
|
if (event) {
|
||||||
|
addToSet(pubkeys, event.pubkey)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (parentEvent) {
|
||||||
|
parentEvent.tags.forEach(([tagName, tagValue]) => {
|
||||||
|
if (['p', 'P'].includes(tagName) && !!tagValue) {
|
||||||
|
addToSet(relatedPubkeys, tagValue)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
pubkeys,
|
||||||
|
relatedPubkeys: relatedPubkeys.filter((p) => !pubkeys.includes(p)),
|
||||||
|
parentEventPubkey
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,14 +1,14 @@
|
|||||||
import { BIG_RELAY_URLS, ExtendedKind } from '@/constants'
|
import { BIG_RELAY_URLS, ExtendedKind } from '@/constants'
|
||||||
import {
|
import {
|
||||||
getEventCoordinate,
|
getParentETag,
|
||||||
getParentEventTag,
|
getReplaceableEventCoordinate,
|
||||||
getRootAddressableEventTag,
|
getRootATag,
|
||||||
|
getRootETag,
|
||||||
getRootEventHexId,
|
getRootEventHexId,
|
||||||
getRootEventTag,
|
isReplaceableEvent,
|
||||||
isReplaceable,
|
|
||||||
isReplyNoteEvent
|
isReplyNoteEvent
|
||||||
} from '@/lib/event'
|
} from '@/lib/event'
|
||||||
import { generateEventIdFromETag, tagNameEquals } from '@/lib/tag'
|
import { generateBech32IdFromETag, tagNameEquals } from '@/lib/tag'
|
||||||
import { useSecondaryPage } from '@/PageManager'
|
import { useSecondaryPage } from '@/PageManager'
|
||||||
import { useReply } from '@/providers/ReplyProvider'
|
import { useReply } from '@/providers/ReplyProvider'
|
||||||
import { useUserTrust } from '@/providers/UserTrustProvider'
|
import { useUserTrust } from '@/providers/UserTrustProvider'
|
||||||
@@ -36,16 +36,18 @@ export default function ReplyNoteList({ index, event }: { index?: number; event:
|
|||||||
const replies = useMemo(() => {
|
const replies = useMemo(() => {
|
||||||
const replyIdSet = new Set<string>()
|
const replyIdSet = new Set<string>()
|
||||||
const replyEvents: NEvent[] = []
|
const replyEvents: NEvent[] = []
|
||||||
const currentEventId = isReplaceable(event.kind) ? getEventCoordinate(event) : event.id
|
const currentEventKey = isReplaceableEvent(event.kind)
|
||||||
let parentEventIds = [currentEventId]
|
? getReplaceableEventCoordinate(event)
|
||||||
while (parentEventIds.length > 0) {
|
: event.id
|
||||||
const events = parentEventIds.flatMap((id) => repliesMap.get(id)?.events || [])
|
let parentEventKeys = [currentEventKey]
|
||||||
|
while (parentEventKeys.length > 0) {
|
||||||
|
const events = parentEventKeys.flatMap((id) => repliesMap.get(id)?.events || [])
|
||||||
events.forEach((evt) => {
|
events.forEach((evt) => {
|
||||||
if (replyIdSet.has(evt.id)) return
|
if (replyIdSet.has(evt.id)) return
|
||||||
replyIdSet.add(evt.id)
|
replyIdSet.add(evt.id)
|
||||||
replyEvents.push(evt)
|
replyEvents.push(evt)
|
||||||
})
|
})
|
||||||
parentEventIds = events.map((evt) => evt.id)
|
parentEventKeys = events.map((evt) => evt.id)
|
||||||
}
|
}
|
||||||
return replyEvents.sort((a, b) => a.created_at - b.created_at)
|
return replyEvents.sort((a, b) => a.created_at - b.created_at)
|
||||||
}, [event.id, repliesMap])
|
}, [event.id, repliesMap])
|
||||||
@@ -59,22 +61,22 @@ export default function ReplyNoteList({ index, event }: { index?: number; event:
|
|||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const fetchRootEvent = async () => {
|
const fetchRootEvent = async () => {
|
||||||
let root: TRootInfo = isReplaceable(event.kind)
|
let root: TRootInfo = isReplaceableEvent(event.kind)
|
||||||
? {
|
? {
|
||||||
type: 'A',
|
type: 'A',
|
||||||
id: getEventCoordinate(event),
|
id: getReplaceableEventCoordinate(event),
|
||||||
eventId: event.id,
|
eventId: event.id,
|
||||||
pubkey: event.pubkey,
|
pubkey: event.pubkey,
|
||||||
relay: client.getEventHint(event.id)
|
relay: client.getEventHint(event.id)
|
||||||
}
|
}
|
||||||
: { type: 'E', id: event.id, pubkey: event.pubkey }
|
: { type: 'E', id: event.id, pubkey: event.pubkey }
|
||||||
const rootEventTag = getRootEventTag(event)
|
const rootETag = getRootETag(event)
|
||||||
if (rootEventTag) {
|
if (rootETag) {
|
||||||
const [, rootEventHexId, , , rootEventPubkey] = rootEventTag
|
const [, rootEventHexId, , , rootEventPubkey] = rootETag
|
||||||
if (rootEventHexId && rootEventPubkey) {
|
if (rootEventHexId && rootEventPubkey) {
|
||||||
root = { type: 'E', id: rootEventHexId, pubkey: rootEventPubkey }
|
root = { type: 'E', id: rootEventHexId, pubkey: rootEventPubkey }
|
||||||
} else {
|
} else {
|
||||||
const rootEventId = generateEventIdFromETag(rootEventTag)
|
const rootEventId = generateBech32IdFromETag(rootETag)
|
||||||
if (rootEventId) {
|
if (rootEventId) {
|
||||||
const rootEvent = await client.fetchEvent(rootEventId)
|
const rootEvent = await client.fetchEvent(rootEventId)
|
||||||
if (rootEvent) {
|
if (rootEvent) {
|
||||||
@@ -83,7 +85,7 @@ export default function ReplyNoteList({ index, event }: { index?: number; event:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (event.kind === ExtendedKind.COMMENT) {
|
} else if (event.kind === ExtendedKind.COMMENT) {
|
||||||
const rootATag = getRootAddressableEventTag(event)
|
const rootATag = getRootATag(event)
|
||||||
if (rootATag) {
|
if (rootATag) {
|
||||||
const [, coordinate, relay] = rootATag
|
const [, coordinate, relay] = rootATag
|
||||||
const [, pubkey] = coordinate.split(':')
|
const [, pubkey] = coordinate.split(':')
|
||||||
@@ -295,9 +297,9 @@ export default function ReplyNoteList({ index, event }: { index?: number; event:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const parentEventTag = getParentEventTag(reply)
|
const parentETag = getParentETag(reply)
|
||||||
const parentEventOriginalId = parentEventTag?.[1]
|
const parentEventHexId = parentETag?.[1]
|
||||||
const parentEventId = parentEventTag ? generateEventIdFromETag(parentEventTag) : undefined
|
const parentEventId = parentETag ? generateBech32IdFromETag(parentETag) : undefined
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
ref={(el) => (replyRefs.current[reply.id] = el)}
|
ref={(el) => (replyRefs.current[reply.id] = el)}
|
||||||
@@ -306,8 +308,8 @@ export default function ReplyNoteList({ index, event }: { index?: number; event:
|
|||||||
>
|
>
|
||||||
<ReplyNote
|
<ReplyNote
|
||||||
event={reply}
|
event={reply}
|
||||||
parentEventId={event.id !== parentEventOriginalId ? parentEventId : undefined}
|
parentEventId={event.id !== parentEventHexId ? parentEventId : undefined}
|
||||||
onClickParent={() => parentEventOriginalId && highlightReply(parentEventOriginalId)}
|
onClickParent={() => parentEventHexId && highlightReply(parentEventHexId)}
|
||||||
highlight={highlightReplyId === reply.id}
|
highlight={highlightReplyId === reply.id}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { extractPubkeysFromEventTags } from '@/lib/tag'
|
import { getPubkeysFromPTags } from '@/lib/tag'
|
||||||
import client from '@/services/client.service'
|
import client from '@/services/client.service'
|
||||||
import { Event } from 'nostr-tools'
|
import { Event } from 'nostr-tools'
|
||||||
import { useEffect, useState } from 'react'
|
import { useEffect, useState } from 'react'
|
||||||
@@ -18,7 +18,7 @@ export function useFetchFollowings(pubkey?: string | null) {
|
|||||||
if (!event) return
|
if (!event) return
|
||||||
|
|
||||||
setFollowListEvent(event)
|
setFollowListEvent(event)
|
||||||
setFollowings(extractPubkeysFromEventTags(event.tags))
|
setFollowings(getPubkeysFromPTags(event.tags))
|
||||||
} finally {
|
} finally {
|
||||||
setIsFetching(false)
|
setIsFetching(false)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +0,0 @@
|
|||||||
export function isTouchDevice() {
|
|
||||||
return 'ontouchstart' in window || navigator.maxTouchPoints > 0
|
|
||||||
}
|
|
||||||
|
|
||||||
export function isEmail(email: string) {
|
|
||||||
return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)
|
|
||||||
}
|
|
||||||
|
|
||||||
export function isDevEnv() {
|
|
||||||
return process.env.NODE_ENV === 'development'
|
|
||||||
}
|
|
||||||
@@ -3,16 +3,14 @@ import client from '@/services/client.service'
|
|||||||
import mediaUpload from '@/services/media-upload.service'
|
import mediaUpload from '@/services/media-upload.service'
|
||||||
import { TDraftEvent, TEmoji, TMailboxRelay, TRelaySet } from '@/types'
|
import { TDraftEvent, TEmoji, TMailboxRelay, TRelaySet } from '@/types'
|
||||||
import dayjs from 'dayjs'
|
import dayjs from 'dayjs'
|
||||||
import { Event, kinds } from 'nostr-tools'
|
import { Event, kinds, nip19 } from 'nostr-tools'
|
||||||
import {
|
import {
|
||||||
extractCommentMentions,
|
getReplaceableEventCoordinate,
|
||||||
extractHashtags,
|
getRootETag,
|
||||||
extractImagesFromContent,
|
|
||||||
extractRelatedEventIds,
|
|
||||||
getEventCoordinate,
|
|
||||||
isProtectedEvent,
|
isProtectedEvent,
|
||||||
isReplaceable
|
isReplaceableEvent
|
||||||
} from './event'
|
} from './event'
|
||||||
|
import { generateBech32IdFromETag, tagNameEquals } from './tag'
|
||||||
import { normalizeHttpUrl } from './url'
|
import { normalizeHttpUrl } from './url'
|
||||||
|
|
||||||
// https://github.com/nostr-protocol/nips/blob/master/25.md
|
// https://github.com/nostr-protocol/nips/blob/master/25.md
|
||||||
@@ -25,8 +23,12 @@ export function createReactionDraftEvent(event: Event, emoji: TEmoji | string =
|
|||||||
tags.push(['k', event.kind.toString()])
|
tags.push(['k', event.kind.toString()])
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isReplaceable(event.kind)) {
|
if (isReplaceableEvent(event.kind)) {
|
||||||
tags.push(hint ? ['a', getEventCoordinate(event), hint] : ['a', getEventCoordinate(event)])
|
tags.push(
|
||||||
|
hint
|
||||||
|
? ['a', getReplaceableEventCoordinate(event), hint]
|
||||||
|
: ['a', getReplaceableEventCoordinate(event)]
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
let content: string
|
let content: string
|
||||||
@@ -81,7 +83,7 @@ export async function createShortTextNoteDraftEvent(
|
|||||||
const tags = hashtags.map((hashtag) => ['t', hashtag])
|
const tags = hashtags.map((hashtag) => ['t', hashtag])
|
||||||
|
|
||||||
// imeta tags
|
// imeta tags
|
||||||
const { images } = extractImagesFromContent(content)
|
const images = extractImagesFromContent(content)
|
||||||
if (images && images.length) {
|
if (images && images.length) {
|
||||||
tags.push(...generateImetaTags(images))
|
tags.push(...generateImetaTags(images))
|
||||||
}
|
}
|
||||||
@@ -209,7 +211,7 @@ export async function createCommentDraftEvent(
|
|||||||
.map((hashtag) => ['t', hashtag])
|
.map((hashtag) => ['t', hashtag])
|
||||||
.concat(quoteEventIds.map((eventId) => ['q', eventId, client.getEventHint(eventId)]))
|
.concat(quoteEventIds.map((eventId) => ['q', eventId, client.getEventHint(eventId)]))
|
||||||
|
|
||||||
const { images } = extractImagesFromContent(content)
|
const images = extractImagesFromContent(content)
|
||||||
if (images && images.length) {
|
if (images && images.length) {
|
||||||
tags.push(...generateImetaTags(images))
|
tags.push(...generateImetaTags(images))
|
||||||
}
|
}
|
||||||
@@ -319,7 +321,7 @@ export function createFavoriteRelaysDraftEvent(
|
|||||||
tags.push(['relay', url])
|
tags.push(['relay', url])
|
||||||
})
|
})
|
||||||
relaySetEvents.forEach((event) => {
|
relaySetEvents.forEach((event) => {
|
||||||
tags.push(['a', getEventCoordinate(event)])
|
tags.push(['a', getReplaceableEventCoordinate(event)])
|
||||||
})
|
})
|
||||||
return {
|
return {
|
||||||
kind: ExtendedKind.FAVORITE_RELAYS,
|
kind: ExtendedKind.FAVORITE_RELAYS,
|
||||||
@@ -364,3 +366,155 @@ function generateImetaTags(imageUrls: string[]) {
|
|||||||
})
|
})
|
||||||
.filter(Boolean) as string[][]
|
.filter(Boolean) as string[][]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function extractRelatedEventIds(content: string, parentEvent?: Event) {
|
||||||
|
const quoteEventIds: string[] = []
|
||||||
|
let rootETag: string[] = []
|
||||||
|
let parentETag: string[] = []
|
||||||
|
const matches = content.match(/nostr:(note1[a-z0-9]{58}|nevent1[a-z0-9]+)/g)
|
||||||
|
|
||||||
|
const addToSet = (arr: string[], item: string) => {
|
||||||
|
if (!arr.includes(item)) arr.push(item)
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const m of matches || []) {
|
||||||
|
try {
|
||||||
|
const id = m.split(':')[1]
|
||||||
|
const { type, data } = nip19.decode(id)
|
||||||
|
if (type === 'nevent') {
|
||||||
|
addToSet(quoteEventIds, data.id)
|
||||||
|
} else if (type === 'note') {
|
||||||
|
addToSet(quoteEventIds, data)
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (parentEvent) {
|
||||||
|
const _rootETag = getRootETag(parentEvent)
|
||||||
|
if (_rootETag) {
|
||||||
|
parentETag = [
|
||||||
|
'e',
|
||||||
|
parentEvent.id,
|
||||||
|
client.getEventHint(parentEvent.id),
|
||||||
|
'reply',
|
||||||
|
parentEvent.pubkey
|
||||||
|
]
|
||||||
|
|
||||||
|
const [, rootEventHexId, hint, , rootEventPubkey] = _rootETag
|
||||||
|
if (rootEventPubkey) {
|
||||||
|
rootETag = [
|
||||||
|
'e',
|
||||||
|
rootEventHexId,
|
||||||
|
hint ?? client.getEventHint(rootEventHexId),
|
||||||
|
'root',
|
||||||
|
rootEventPubkey
|
||||||
|
]
|
||||||
|
} else {
|
||||||
|
const rootEventId = generateBech32IdFromETag(_rootETag)
|
||||||
|
const rootEvent = rootEventId ? await client.fetchEvent(rootEventId) : undefined
|
||||||
|
rootETag = rootEvent
|
||||||
|
? ['e', rootEvent.id, hint ?? client.getEventHint(rootEvent.id), 'root', rootEvent.pubkey]
|
||||||
|
: ['e', rootEventHexId, hint ?? client.getEventHint(rootEventHexId), 'root']
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// reply to root event
|
||||||
|
rootETag = [
|
||||||
|
'e',
|
||||||
|
parentEvent.id,
|
||||||
|
client.getEventHint(parentEvent.id),
|
||||||
|
'root',
|
||||||
|
parentEvent.pubkey
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
quoteEventIds,
|
||||||
|
rootETag,
|
||||||
|
parentETag
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function extractCommentMentions(content: string, parentEvent: Event) {
|
||||||
|
const quoteEventIds: string[] = []
|
||||||
|
const parentEventIsReplaceable = isReplaceableEvent(parentEvent.kind)
|
||||||
|
const rootCoordinateTag =
|
||||||
|
parentEvent.kind === ExtendedKind.COMMENT
|
||||||
|
? parentEvent.tags.find(tagNameEquals('A'))
|
||||||
|
: isReplaceableEvent(parentEvent.kind)
|
||||||
|
? ['A', getReplaceableEventCoordinate(parentEvent), client.getEventHint(parentEvent.id)]
|
||||||
|
: undefined
|
||||||
|
const rootEventId =
|
||||||
|
parentEvent.kind === ExtendedKind.COMMENT
|
||||||
|
? parentEvent.tags.find(tagNameEquals('E'))?.[1]
|
||||||
|
: parentEvent.id
|
||||||
|
const rootKind =
|
||||||
|
parentEvent.kind === ExtendedKind.COMMENT
|
||||||
|
? parentEvent.tags.find(tagNameEquals('K'))?.[1]
|
||||||
|
: parentEvent.kind
|
||||||
|
const rootPubkey =
|
||||||
|
parentEvent.kind === ExtendedKind.COMMENT
|
||||||
|
? parentEvent.tags.find(tagNameEquals('P'))?.[1]
|
||||||
|
: parentEvent.pubkey
|
||||||
|
const rootUrl =
|
||||||
|
parentEvent.kind === ExtendedKind.COMMENT
|
||||||
|
? parentEvent.tags.find(tagNameEquals('I'))?.[1]
|
||||||
|
: undefined
|
||||||
|
|
||||||
|
const parentEventId = parentEvent.id
|
||||||
|
const parentCoordinate = parentEventIsReplaceable
|
||||||
|
? getReplaceableEventCoordinate(parentEvent)
|
||||||
|
: undefined
|
||||||
|
const parentKind = parentEvent.kind
|
||||||
|
const parentPubkey = parentEvent.pubkey
|
||||||
|
|
||||||
|
const addToSet = (arr: string[], item: string) => {
|
||||||
|
if (!arr.includes(item)) arr.push(item)
|
||||||
|
}
|
||||||
|
|
||||||
|
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') {
|
||||||
|
addToSet(quoteEventIds, data.id)
|
||||||
|
} else if (type === 'note') {
|
||||||
|
addToSet(quoteEventIds, data)
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
quoteEventIds,
|
||||||
|
rootEventId,
|
||||||
|
rootCoordinateTag,
|
||||||
|
rootKind,
|
||||||
|
rootPubkey,
|
||||||
|
rootUrl,
|
||||||
|
parentEventId,
|
||||||
|
parentCoordinate,
|
||||||
|
parentKind,
|
||||||
|
parentPubkey
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function extractHashtags(content: string) {
|
||||||
|
const hashtags: string[] = []
|
||||||
|
const matches = content.match(/#[\p{L}\p{N}\p{M}]+/gu)
|
||||||
|
matches?.forEach((m) => {
|
||||||
|
const hashtag = m.slice(1).toLowerCase()
|
||||||
|
if (hashtag) {
|
||||||
|
hashtags.push(hashtag)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
return hashtags
|
||||||
|
}
|
||||||
|
|
||||||
|
function extractImagesFromContent(content: string) {
|
||||||
|
return content.match(/https?:\/\/[^\s"']+\.(jpg|jpeg|png|gif|webp|heic)/gi)
|
||||||
|
}
|
||||||
|
|||||||
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 }
|
||||||
|
}
|
||||||
610
src/lib/event.ts
610
src/lib/event.ts
@@ -1,22 +1,16 @@
|
|||||||
import { BIG_RELAY_URLS, ExtendedKind } from '@/constants'
|
import { ExtendedKind } from '@/constants'
|
||||||
import client from '@/services/client.service'
|
import client from '@/services/client.service'
|
||||||
import { TEmoji, TImageInfo, TRelayList, TRelaySet } from '@/types'
|
import { TImageInfo } from '@/types'
|
||||||
import { LRUCache } from 'lru-cache'
|
import { LRUCache } from 'lru-cache'
|
||||||
import { Event, kinds, nip19 } from 'nostr-tools'
|
import { Event, kinds, nip19 } from 'nostr-tools'
|
||||||
import { getAmountFromInvoice, getLightningAddressFromProfile } from './lightning'
|
|
||||||
import { formatPubkey, pubkeyToNpub } from './pubkey'
|
|
||||||
import {
|
import {
|
||||||
extractImageInfoFromTag,
|
getImageInfoFromImetaTag,
|
||||||
generateEventIdFromATag,
|
generateBech32IdFromATag,
|
||||||
generateEventIdFromETag,
|
generateBech32IdFromETag,
|
||||||
isReplyETag,
|
|
||||||
isRootETag,
|
|
||||||
tagNameEquals
|
tagNameEquals
|
||||||
} from './tag'
|
} from './tag'
|
||||||
import { isWebsocketUrl, normalizeHttpUrl, normalizeUrl } from './url'
|
|
||||||
import { isTorBrowser } from './utils'
|
|
||||||
|
|
||||||
const EVENT_EMBEDDED_EVENT_IDS_CACHE = new LRUCache<string, string[]>({ max: 10000 })
|
const EVENT_EMBEDDED_NOTES_CACHE = new LRUCache<string, string[]>({ max: 10000 })
|
||||||
const EVENT_IS_REPLY_NOTE_CACHE = new LRUCache<string, boolean>({ max: 10000 })
|
const EVENT_IS_REPLY_NOTE_CACHE = new LRUCache<string, boolean>({ max: 10000 })
|
||||||
|
|
||||||
export function isNsfwEvent(event: Event) {
|
export function isNsfwEvent(event: Event) {
|
||||||
@@ -27,36 +21,18 @@ export function isNsfwEvent(event: Event) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function isReplyNoteEvent(event: Event) {
|
export function isReplyNoteEvent(event: Event) {
|
||||||
if (event.kind === ExtendedKind.COMMENT) {
|
if (![kinds.ShortTextNote, ExtendedKind.COMMENT].includes(event.kind)) return false
|
||||||
return !!getParentEventTag(event) || !!getParentAddressableEventTag(event)
|
|
||||||
}
|
|
||||||
if (event.kind !== kinds.ShortTextNote) return false
|
|
||||||
|
|
||||||
const cache = EVENT_IS_REPLY_NOTE_CACHE.get(event.id)
|
const cache = EVENT_IS_REPLY_NOTE_CACHE.get(event.id)
|
||||||
if (cache !== undefined) return cache
|
if (cache !== undefined) return cache
|
||||||
|
|
||||||
const mentionsEventIds: string[] = []
|
const isReply = !!getParentETag(event) || !!getParentATag(event)
|
||||||
for (const [tagName, id, , marker] of event.tags) {
|
EVENT_IS_REPLY_NOTE_CACHE.set(event.id, isReply)
|
||||||
if (tagName === 'a' && id) {
|
return isReply
|
||||||
EVENT_IS_REPLY_NOTE_CACHE.set(event.id, true)
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
if (tagName !== 'e' || !id) continue
|
|
||||||
|
|
||||||
mentionsEventIds.push(id)
|
|
||||||
if (['root', 'reply'].includes(marker)) {
|
|
||||||
EVENT_IS_REPLY_NOTE_CACHE.set(event.id, true)
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
const embeddedEventIds = extractEmbeddedEventIds(event)
|
|
||||||
const result = mentionsEventIds.some((id) => !embeddedEventIds.includes(id))
|
|
||||||
EVENT_IS_REPLY_NOTE_CACHE.set(event.id, result)
|
|
||||||
return result
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isCommentEvent(event: Event) {
|
export function isReplaceableEvent(kind: number) {
|
||||||
return event.kind === ExtendedKind.COMMENT
|
return kinds.isReplaceableKind(kind) || kinds.isAddressableKind(kind)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isPictureEvent(event: Event) {
|
export function isPictureEvent(event: Event) {
|
||||||
@@ -67,16 +43,18 @@ export function isProtectedEvent(event: Event) {
|
|||||||
return event.tags.some(([tagName]) => tagName === '-')
|
return event.tags.some(([tagName]) => tagName === '-')
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getParentEventTag(event?: Event) {
|
export function getParentETag(event?: Event) {
|
||||||
if (!event || ![kinds.ShortTextNote, ExtendedKind.COMMENT].includes(event.kind)) return undefined
|
if (!event || ![kinds.ShortTextNote, ExtendedKind.COMMENT].includes(event.kind)) return undefined
|
||||||
|
|
||||||
if (event.kind === ExtendedKind.COMMENT) {
|
if (event.kind === ExtendedKind.COMMENT) {
|
||||||
return event.tags.find(tagNameEquals('e')) ?? event.tags.find(tagNameEquals('E'))
|
return event.tags.find(tagNameEquals('e')) ?? event.tags.find(tagNameEquals('E'))
|
||||||
}
|
}
|
||||||
|
|
||||||
let tag = event.tags.find(isReplyETag)
|
let tag = event.tags.find(([tagName, , , marker]) => {
|
||||||
|
return tagName === 'e' && marker === 'reply'
|
||||||
|
})
|
||||||
if (!tag) {
|
if (!tag) {
|
||||||
const embeddedEventIds = extractEmbeddedEventIds(event)
|
const embeddedEventIds = getEmbeddedNoteBech32Ids(event)
|
||||||
tag = event.tags.findLast(
|
tag = event.tags.findLast(
|
||||||
([tagName, tagValue]) => tagName === 'e' && !!tagValue && !embeddedEventIds.includes(tagValue)
|
([tagName, tagValue]) => tagName === 'e' && !!tagValue && !embeddedEventIds.includes(tagValue)
|
||||||
)
|
)
|
||||||
@@ -84,39 +62,41 @@ export function getParentEventTag(event?: Event) {
|
|||||||
return tag
|
return tag
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getParentAddressableEventTag(event?: Event) {
|
export function getParentATag(event?: Event) {
|
||||||
if (!event || ![kinds.ShortTextNote, ExtendedKind.COMMENT].includes(event.kind)) return undefined
|
if (!event || ![kinds.ShortTextNote, ExtendedKind.COMMENT].includes(event.kind)) return undefined
|
||||||
|
|
||||||
return event.tags.find(tagNameEquals('a')) ?? event.tags.find(tagNameEquals('A'))
|
return event.tags.find(tagNameEquals('a')) ?? event.tags.find(tagNameEquals('A'))
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getParentEventHexId(event?: Event) {
|
export function getParentEventHexId(event?: Event) {
|
||||||
const tag = getParentEventTag(event)
|
const tag = getParentETag(event)
|
||||||
return tag?.[1]
|
return tag?.[1]
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getParentEventId(event?: Event) {
|
export function getParentBech32Id(event?: Event) {
|
||||||
const eTag = getParentEventTag(event)
|
const eTag = getParentETag(event)
|
||||||
if (!eTag) {
|
if (!eTag) {
|
||||||
const aTag = getParentAddressableEventTag(event)
|
const aTag = getParentATag(event)
|
||||||
if (!aTag) return undefined
|
if (!aTag) return undefined
|
||||||
|
|
||||||
return generateEventIdFromATag(aTag)
|
return generateBech32IdFromATag(aTag)
|
||||||
}
|
}
|
||||||
|
|
||||||
return generateEventIdFromETag(eTag)
|
return generateBech32IdFromETag(eTag)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getRootEventTag(event?: Event) {
|
export function getRootETag(event?: Event) {
|
||||||
if (!event || ![kinds.ShortTextNote, ExtendedKind.COMMENT].includes(event.kind)) return undefined
|
if (!event || ![kinds.ShortTextNote, ExtendedKind.COMMENT].includes(event.kind)) return undefined
|
||||||
|
|
||||||
if (event.kind === ExtendedKind.COMMENT) {
|
if (event.kind === ExtendedKind.COMMENT) {
|
||||||
return event.tags.find(tagNameEquals('E'))
|
return event.tags.find(tagNameEquals('E'))
|
||||||
}
|
}
|
||||||
|
|
||||||
let tag = event.tags.find(isRootETag)
|
let tag = event.tags.find(([tagName, , , marker]) => {
|
||||||
|
return tagName === 'e' && marker === 'root'
|
||||||
|
})
|
||||||
if (!tag) {
|
if (!tag) {
|
||||||
const embeddedEventIds = extractEmbeddedEventIds(event)
|
const embeddedEventIds = getEmbeddedNoteBech32Ids(event)
|
||||||
tag = event.tags.find(
|
tag = event.tags.find(
|
||||||
([tagName, tagValue]) => tagName === 'e' && !!tagValue && !embeddedEventIds.includes(tagValue)
|
([tagName, tagValue]) => tagName === 'e' && !!tagValue && !embeddedEventIds.includes(tagValue)
|
||||||
)
|
)
|
||||||
@@ -124,41 +104,37 @@ export function getRootEventTag(event?: Event) {
|
|||||||
return tag
|
return tag
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getRootAddressableEventTag(event?: Event) {
|
export function getRootATag(event?: Event) {
|
||||||
if (!event || event.kind !== ExtendedKind.COMMENT) return undefined
|
if (!event || event.kind !== ExtendedKind.COMMENT) return undefined
|
||||||
|
|
||||||
return event.tags.find(tagNameEquals('A'))
|
return event.tags.find(tagNameEquals('A'))
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getRootEventHexId(event?: Event) {
|
export function getRootEventHexId(event?: Event) {
|
||||||
const tag = getRootEventTag(event)
|
const tag = getRootETag(event)
|
||||||
return tag?.[1]
|
return tag?.[1]
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getRootEventId(event?: Event) {
|
export function getRootBech32Id(event?: Event) {
|
||||||
const tag = getRootEventTag(event)
|
const eTag = getRootETag(event)
|
||||||
if (!tag) {
|
if (!eTag) {
|
||||||
const aTag = getRootAddressableEventTag(event)
|
const aTag = getRootATag(event)
|
||||||
if (!aTag) return undefined
|
if (!aTag) return undefined
|
||||||
|
|
||||||
return generateEventIdFromATag(aTag)
|
return generateBech32IdFromATag(aTag)
|
||||||
}
|
}
|
||||||
|
|
||||||
return generateEventIdFromETag(tag)
|
return generateBech32IdFromETag(eTag)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isReplaceable(kind: number) {
|
export function getReplaceableEventCoordinate(event: Event) {
|
||||||
return kinds.isReplaceableKind(kind) || kinds.isAddressableKind(kind)
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getEventCoordinate(event: Event) {
|
|
||||||
const d = event.tags.find(tagNameEquals('d'))?.[1]
|
const d = event.tags.find(tagNameEquals('d'))?.[1]
|
||||||
return d ? `${event.kind}:${event.pubkey}:${d}` : `${event.kind}:${event.pubkey}`
|
return d ? `${event.kind}:${event.pubkey}:${d}` : `${event.kind}:${event.pubkey}`
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getSharableEventId(event: Event) {
|
export function getNoteBech32Id(event: Event) {
|
||||||
const hints = client.getEventHints(event.id).slice(0, 2)
|
const hints = client.getEventHints(event.id).slice(0, 2)
|
||||||
if (isReplaceable(event.kind)) {
|
if (isReplaceableEvent(event.kind)) {
|
||||||
const identifier = event.tags.find(tagNameEquals('d'))?.[1] ?? ''
|
const identifier = event.tags.find(tagNameEquals('d'))?.[1] ?? ''
|
||||||
return nip19.naddrEncode({ pubkey: event.pubkey, kind: event.kind, identifier, relays: hints })
|
return nip19.naddrEncode({ pubkey: event.pubkey, kind: event.kind, identifier, relays: hints })
|
||||||
}
|
}
|
||||||
@@ -169,289 +145,10 @@ export function getUsingClient(event: Event) {
|
|||||||
return event.tags.find(tagNameEquals('client'))?.[1]
|
return event.tags.find(tagNameEquals('client'))?.[1]
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getRelayListFromRelayListEvent(event?: Event) {
|
export function getImageInfosFromEvent(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 getProfileFromProfileEvent(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 getRelaySetFromRelaySetEvent(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 async function extractMentions(content: string, parentEvent?: Event) {
|
|
||||||
const parentEventPubkey = parentEvent ? parentEvent.pubkey : undefined
|
|
||||||
const pubkeys: string[] = []
|
|
||||||
const relatedPubkeys: 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
|
|
||||||
)
|
|
||||||
|
|
||||||
const addToSet = (arr: string[], pubkey: string) => {
|
|
||||||
if (pubkey === parentEventPubkey) return
|
|
||||||
if (!arr.includes(pubkey)) arr.push(pubkey)
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const m of matches || []) {
|
|
||||||
try {
|
|
||||||
const id = m.split(':')[1]
|
|
||||||
const { type, data } = nip19.decode(id)
|
|
||||||
if (type === 'nprofile') {
|
|
||||||
addToSet(pubkeys, data.pubkey)
|
|
||||||
} else if (type === 'npub') {
|
|
||||||
addToSet(pubkeys, data)
|
|
||||||
} else if (['nevent', 'note'].includes(type)) {
|
|
||||||
const event = await client.fetchEvent(id)
|
|
||||||
if (event) {
|
|
||||||
addToSet(pubkeys, event.pubkey)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
console.error(e)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (parentEvent) {
|
|
||||||
parentEvent.tags.forEach(([tagName, tagValue]) => {
|
|
||||||
if (['p', 'P'].includes(tagName) && !!tagValue) {
|
|
||||||
addToSet(relatedPubkeys, tagValue)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
pubkeys,
|
|
||||||
relatedPubkeys: relatedPubkeys.filter((p) => !pubkeys.includes(p)),
|
|
||||||
parentEventPubkey
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function extractRelatedEventIds(content: string, parentEvent?: Event) {
|
|
||||||
const quoteEventIds: string[] = []
|
|
||||||
let rootETag: string[] = []
|
|
||||||
let parentETag: string[] = []
|
|
||||||
const matches = content.match(/nostr:(note1[a-z0-9]{58}|nevent1[a-z0-9]+)/g)
|
|
||||||
|
|
||||||
const addToSet = (arr: string[], item: string) => {
|
|
||||||
if (!arr.includes(item)) arr.push(item)
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const m of matches || []) {
|
|
||||||
try {
|
|
||||||
const id = m.split(':')[1]
|
|
||||||
const { type, data } = nip19.decode(id)
|
|
||||||
if (type === 'nevent') {
|
|
||||||
addToSet(quoteEventIds, data.id)
|
|
||||||
} else if (type === 'note') {
|
|
||||||
addToSet(quoteEventIds, data)
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
console.error(e)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (parentEvent) {
|
|
||||||
const rootEventTag = getRootEventTag(parentEvent)
|
|
||||||
if (rootEventTag) {
|
|
||||||
parentETag = [
|
|
||||||
'e',
|
|
||||||
parentEvent.id,
|
|
||||||
client.getEventHint(parentEvent.id),
|
|
||||||
'reply',
|
|
||||||
parentEvent.pubkey
|
|
||||||
]
|
|
||||||
|
|
||||||
const [, rootEventHexId, hint, , rootEventPubkey] = rootEventTag
|
|
||||||
if (rootEventPubkey) {
|
|
||||||
rootETag = [
|
|
||||||
'e',
|
|
||||||
rootEventHexId,
|
|
||||||
hint ?? client.getEventHint(rootEventHexId),
|
|
||||||
'root',
|
|
||||||
rootEventPubkey
|
|
||||||
]
|
|
||||||
} else {
|
|
||||||
const rootEventId = generateEventIdFromETag(rootEventTag)
|
|
||||||
const rootEvent = rootEventId ? await client.fetchEvent(rootEventId) : undefined
|
|
||||||
rootETag = rootEvent
|
|
||||||
? ['e', rootEvent.id, hint ?? client.getEventHint(rootEvent.id), 'root', rootEvent.pubkey]
|
|
||||||
: ['e', rootEventHexId, hint ?? client.getEventHint(rootEventHexId), 'root']
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// reply to root event
|
|
||||||
rootETag = [
|
|
||||||
'e',
|
|
||||||
parentEvent.id,
|
|
||||||
client.getEventHint(parentEvent.id),
|
|
||||||
'root',
|
|
||||||
parentEvent.pubkey
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
quoteEventIds,
|
|
||||||
rootETag,
|
|
||||||
parentETag
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function extractCommentMentions(content: string, parentEvent: Event) {
|
|
||||||
const quoteEventIds: string[] = []
|
|
||||||
const parentEventIsReplaceable = isReplaceable(parentEvent.kind)
|
|
||||||
const rootCoordinateTag =
|
|
||||||
parentEvent.kind === ExtendedKind.COMMENT
|
|
||||||
? parentEvent.tags.find(tagNameEquals('A'))
|
|
||||||
: isReplaceable(parentEvent.kind)
|
|
||||||
? ['A', getEventCoordinate(parentEvent), client.getEventHint(parentEvent.id)]
|
|
||||||
: undefined
|
|
||||||
const rootEventId =
|
|
||||||
parentEvent.kind === ExtendedKind.COMMENT
|
|
||||||
? parentEvent.tags.find(tagNameEquals('E'))?.[1]
|
|
||||||
: parentEvent.id
|
|
||||||
const rootKind =
|
|
||||||
parentEvent.kind === ExtendedKind.COMMENT
|
|
||||||
? parentEvent.tags.find(tagNameEquals('K'))?.[1]
|
|
||||||
: parentEvent.kind
|
|
||||||
const rootPubkey =
|
|
||||||
parentEvent.kind === ExtendedKind.COMMENT
|
|
||||||
? parentEvent.tags.find(tagNameEquals('P'))?.[1]
|
|
||||||
: parentEvent.pubkey
|
|
||||||
const rootUrl =
|
|
||||||
parentEvent.kind === ExtendedKind.COMMENT
|
|
||||||
? parentEvent.tags.find(tagNameEquals('I'))?.[1]
|
|
||||||
: undefined
|
|
||||||
|
|
||||||
const parentEventId = parentEvent.id
|
|
||||||
const parentCoordinate = parentEventIsReplaceable ? getEventCoordinate(parentEvent) : undefined
|
|
||||||
const parentKind = parentEvent.kind
|
|
||||||
const parentPubkey = parentEvent.pubkey
|
|
||||||
|
|
||||||
const addToSet = (arr: string[], item: string) => {
|
|
||||||
if (!arr.includes(item)) arr.push(item)
|
|
||||||
}
|
|
||||||
|
|
||||||
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') {
|
|
||||||
addToSet(quoteEventIds, data.id)
|
|
||||||
} else if (type === 'note') {
|
|
||||||
addToSet(quoteEventIds, data)
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
console.error(e)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
quoteEventIds,
|
|
||||||
rootEventId,
|
|
||||||
rootCoordinateTag,
|
|
||||||
rootKind,
|
|
||||||
rootPubkey,
|
|
||||||
rootUrl,
|
|
||||||
parentEventId,
|
|
||||||
parentCoordinate,
|
|
||||||
parentKind,
|
|
||||||
parentPubkey
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export function extractHashtags(content: string) {
|
|
||||||
const hashtags: string[] = []
|
|
||||||
const matches = content.match(/#[\p{L}\p{N}\p{M}]+/gu)
|
|
||||||
matches?.forEach((m) => {
|
|
||||||
const hashtag = m.slice(1).toLowerCase()
|
|
||||||
if (hashtag) {
|
|
||||||
hashtags.push(hashtag)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
return hashtags
|
|
||||||
}
|
|
||||||
|
|
||||||
export function extractImageInfosFromEventTags(event: Event) {
|
|
||||||
const images: TImageInfo[] = []
|
const images: TImageInfo[] = []
|
||||||
event.tags.forEach((tag) => {
|
event.tags.forEach((tag) => {
|
||||||
const imageInfo = extractImageInfoFromTag(tag, event.pubkey)
|
const imageInfo = getImageInfoFromImetaTag(tag, event.pubkey)
|
||||||
if (imageInfo) {
|
if (imageInfo) {
|
||||||
images.push(imageInfo)
|
images.push(imageInfo)
|
||||||
}
|
}
|
||||||
@@ -459,116 +156,26 @@ export function extractImageInfosFromEventTags(event: Event) {
|
|||||||
return images
|
return images
|
||||||
}
|
}
|
||||||
|
|
||||||
export function extractImagesFromContent(content: string) {
|
export function getEmbeddedNoteBech32Ids(event: Event) {
|
||||||
const images = content.match(/https?:\/\/[^\s"']+\.(jpg|jpeg|png|gif|webp|heic)/gi)
|
const cache = EVENT_EMBEDDED_NOTES_CACHE.get(event.id)
|
||||||
let contentWithoutImages = content
|
|
||||||
images?.forEach((url) => {
|
|
||||||
contentWithoutImages = contentWithoutImages.replace(url, '').trim()
|
|
||||||
})
|
|
||||||
contentWithoutImages = contentWithoutImages.replace(/\n{3,}/g, '\n\n').trim()
|
|
||||||
return { images, contentWithoutImages }
|
|
||||||
}
|
|
||||||
|
|
||||||
export function extractEmbeddedNotesFromContent(content: string) {
|
|
||||||
let c = content
|
|
||||||
const embeddedNotes: string[] = []
|
|
||||||
const embeddedNoteRegex = /nostr:(note1[a-z0-9]{58}|nevent1[a-z0-9]+|naddr1[a-z0-9]+)/g
|
|
||||||
;(c.match(embeddedNoteRegex) || []).forEach((note) => {
|
|
||||||
c = c.replace(note, '').trim()
|
|
||||||
embeddedNotes.push(note)
|
|
||||||
})
|
|
||||||
|
|
||||||
c = c.replace(/\n{3,}/g, '\n\n').trim()
|
|
||||||
|
|
||||||
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 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 = generateEventIdFromETag(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 extractEmbeddedEventIds(event: Event) {
|
|
||||||
const cache = EVENT_EMBEDDED_EVENT_IDS_CACHE.get(event.id)
|
|
||||||
if (cache) return cache
|
if (cache) return cache
|
||||||
|
|
||||||
const embeddedEventIds: string[] = []
|
const embeddedNoteBech32Ids: string[] = []
|
||||||
const embeddedNoteRegex = /nostr:(note1[a-z0-9]{58}|nevent1[a-z0-9]+)/g
|
const embeddedNoteRegex = /nostr:(note1[a-z0-9]{58}|nevent1[a-z0-9]+)/g
|
||||||
;(event.content.match(embeddedNoteRegex) || []).forEach((note) => {
|
;(event.content.match(embeddedNoteRegex) || []).forEach((note) => {
|
||||||
try {
|
try {
|
||||||
const { type, data } = nip19.decode(note.split(':')[1])
|
const { type, data } = nip19.decode(note.split(':')[1])
|
||||||
if (type === 'nevent') {
|
if (type === 'nevent') {
|
||||||
embeddedEventIds.push(data.id)
|
embeddedNoteBech32Ids.push(data.id)
|
||||||
} else if (type === 'note') {
|
} else if (type === 'note') {
|
||||||
embeddedEventIds.push(data)
|
embeddedNoteBech32Ids.push(data)
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
// ignore
|
// ignore
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
EVENT_EMBEDDED_EVENT_IDS_CACHE.set(event.id, embeddedEventIds)
|
EVENT_EMBEDDED_NOTES_CACHE.set(event.id, embeddedNoteBech32Ids)
|
||||||
return embeddedEventIds
|
return embeddedNoteBech32Ids
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getLatestEvent(events: Event[]) {
|
export function getLatestEvent(events: Event[]) {
|
||||||
@@ -579,22 +186,6 @@ export function getReplaceableEventIdentifier(event: Event) {
|
|||||||
return event.tags.find(tagNameEquals('d'))?.[1] ?? ''
|
return event.tags.find(tagNameEquals('d'))?.[1] ?? ''
|
||||||
}
|
}
|
||||||
|
|
||||||
export function extractEmojiInfosFromTags(tags: string[][] = []) {
|
|
||||||
return tags
|
|
||||||
.map((tag) => {
|
|
||||||
if (tag.length < 3 || tag[0] !== 'emoji') return null
|
|
||||||
return { shortcode: tag[1], url: tag[2] }
|
|
||||||
})
|
|
||||||
.filter(Boolean) as TEmoji[]
|
|
||||||
}
|
|
||||||
|
|
||||||
export function extractServersFromTags(tags: string[][] = []) {
|
|
||||||
return tags
|
|
||||||
.filter(tagNameEquals('server'))
|
|
||||||
.map(([, url]) => (url ? normalizeHttpUrl(url) : ''))
|
|
||||||
.filter(Boolean)
|
|
||||||
}
|
|
||||||
|
|
||||||
export function createFakeEvent(event: Partial<Event>): Event {
|
export function createFakeEvent(event: Partial<Event>): Event {
|
||||||
return {
|
return {
|
||||||
id: '',
|
id: '',
|
||||||
@@ -607,106 +198,3 @@ export function createFakeEvent(event: Partial<Event>): Event {
|
|||||||
...event
|
...event
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getLongFormArticleMetadata(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 getLiveEventMetadata(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 getGroupMetadata(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 getCommunityDefinition(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 }
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { TProfile } from '@/types'
|
import { TProfile } from '@/types'
|
||||||
import { Invoice } from '@getalby/lightning-tools'
|
import { Invoice } from '@getalby/lightning-tools'
|
||||||
import { isEmail } from './common'
|
import { isEmail } from './utils'
|
||||||
|
|
||||||
export function getAmountFromInvoice(invoice: string): number {
|
export function getAmountFromInvoice(invoice: string): number {
|
||||||
const _invoice = new Invoice({ pr: invoice }) // TODO: need to validate
|
const _invoice = new Invoice({ pr: invoice }) // TODO: need to validate
|
||||||
|
|||||||
@@ -1,11 +1,10 @@
|
|||||||
import { Event, nip19 } from 'nostr-tools'
|
import { Event, nip19 } from 'nostr-tools'
|
||||||
import { getSharableEventId } from './event'
|
import { getNoteBech32Id } from './event'
|
||||||
import { generateEventId } from './tag'
|
|
||||||
|
|
||||||
export const toHome = () => '/'
|
export const toHome = () => '/'
|
||||||
export const toNote = (eventOrId: Pick<Event, 'id' | 'pubkey'> | string) => {
|
export const toNote = (eventOrId: Event | string) => {
|
||||||
if (typeof eventOrId === 'string') return `/notes/${eventOrId}`
|
if (typeof eventOrId === 'string') return `/notes/${eventOrId}`
|
||||||
const nevent = generateEventId(eventOrId)
|
const nevent = getNoteBech32Id(eventOrId)
|
||||||
return `/notes/${nevent}`
|
return `/notes/${nevent}`
|
||||||
}
|
}
|
||||||
export const toNoteList = ({
|
export const toNoteList = ({
|
||||||
@@ -59,12 +58,6 @@ export const toProfileEditor = () => '/profile-editor'
|
|||||||
export const toRelay = (url: string) => `/relays/${encodeURIComponent(url)}`
|
export const toRelay = (url: string) => `/relays/${encodeURIComponent(url)}`
|
||||||
export const toMuteList = () => '/mutes'
|
export const toMuteList = () => '/mutes'
|
||||||
|
|
||||||
export const toHablaLongFormArticle = (event: Event) => {
|
|
||||||
return `https://habla.news/a/${getSharableEventId(event)}`
|
|
||||||
}
|
|
||||||
export const toZapStreamLiveEvent = (event: Event) => {
|
|
||||||
return `https://zap.stream/${getSharableEventId(event)}`
|
|
||||||
}
|
|
||||||
export const toChachiChat = (relay: string, d: string) => {
|
export const toChachiChat = (relay: string, d: string) => {
|
||||||
return `https://chachi.chat/${relay.replace(/^wss?:\/\//, '').replace(/\/$/, '')}/${d}`
|
return `https://chachi.chat/${relay.replace(/^wss?:\/\//, '').replace(/\/$/, '')}/${d}`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,26 +1,22 @@
|
|||||||
import client from '@/services/client.service'
|
import { TEmoji, TImageInfo } from '@/types'
|
||||||
import { TImageInfo } from '@/types'
|
|
||||||
import { isBlurhashValid } from 'blurhash'
|
import { isBlurhashValid } from 'blurhash'
|
||||||
import { Event, nip19 } from 'nostr-tools'
|
import { nip19 } from 'nostr-tools'
|
||||||
import { isValidPubkey } from './pubkey'
|
import { isValidPubkey } from './pubkey'
|
||||||
|
import { normalizeHttpUrl } from './url'
|
||||||
|
|
||||||
|
export function isSameTag(tag1: string[], tag2: string[]) {
|
||||||
|
if (tag1.length !== tag2.length) return false
|
||||||
|
for (let i = 0; i < tag1.length; i++) {
|
||||||
|
if (tag1[i] !== tag2[i]) return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
export function tagNameEquals(tagName: string) {
|
export function tagNameEquals(tagName: string) {
|
||||||
return (tag: string[]) => tag[0] === tagName
|
return (tag: string[]) => tag[0] === tagName
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isReplyETag([tagName, , , marker]: string[]) {
|
export function generateBech32IdFromETag(tag: string[]) {
|
||||||
return tagName === 'e' && marker === 'reply'
|
|
||||||
}
|
|
||||||
|
|
||||||
export function isRootETag([tagName, , , marker]: string[]) {
|
|
||||||
return tagName === 'e' && marker === 'root'
|
|
||||||
}
|
|
||||||
|
|
||||||
export function isMentionETag([tagName, , , marker]: string[]) {
|
|
||||||
return tagName === 'e' && marker === 'mention'
|
|
||||||
}
|
|
||||||
|
|
||||||
export function generateEventIdFromETag(tag: string[]) {
|
|
||||||
try {
|
try {
|
||||||
const [, id, relay, , author] = tag
|
const [, id, relay, , author] = tag
|
||||||
return nip19.neventEncode({ id, relays: relay ? [relay] : undefined, author })
|
return nip19.neventEncode({ id, relays: relay ? [relay] : undefined, author })
|
||||||
@@ -29,7 +25,7 @@ export function generateEventIdFromETag(tag: string[]) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function generateEventIdFromATag(tag: string[]) {
|
export function generateBech32IdFromATag(tag: string[]) {
|
||||||
try {
|
try {
|
||||||
const [, coordinate, relay] = tag
|
const [, coordinate, relay] = tag
|
||||||
const [kind, pubkey, identifier] = coordinate.split(':')
|
const [kind, pubkey, identifier] = coordinate.split(':')
|
||||||
@@ -44,12 +40,7 @@ export function generateEventIdFromATag(tag: string[]) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function generateEventId(event: Pick<Event, 'id' | 'pubkey'>) {
|
export function getImageInfoFromImetaTag(tag: string[], pubkey?: string): TImageInfo | null {
|
||||||
const relay = client.getEventHint(event.id)
|
|
||||||
return nip19.neventEncode({ id: event.id, author: event.pubkey, relays: [relay] })
|
|
||||||
}
|
|
||||||
|
|
||||||
export function extractImageInfoFromTag(tag: string[], pubkey?: string): TImageInfo | null {
|
|
||||||
if (tag[0] !== 'imeta') return null
|
if (tag[0] !== 'imeta') return null
|
||||||
const urlItem = tag.find((item) => item.startsWith('url '))
|
const urlItem = tag.find((item) => item.startsWith('url '))
|
||||||
const url = urlItem?.slice(4)
|
const url = urlItem?.slice(4)
|
||||||
@@ -75,7 +66,7 @@ export function extractImageInfoFromTag(tag: string[], pubkey?: string): TImageI
|
|||||||
return image
|
return image
|
||||||
}
|
}
|
||||||
|
|
||||||
export function extractPubkeysFromEventTags(tags: string[][]) {
|
export function getPubkeysFromPTags(tags: string[][]) {
|
||||||
return Array.from(
|
return Array.from(
|
||||||
new Set(
|
new Set(
|
||||||
tags
|
tags
|
||||||
@@ -87,10 +78,18 @@ export function extractPubkeysFromEventTags(tags: string[][]) {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isSameTag(tag1: string[], tag2: string[]) {
|
export function getEmojiInfosFromEmojiTags(tags: string[][] = []) {
|
||||||
if (tag1.length !== tag2.length) return false
|
return tags
|
||||||
for (let i = 0; i < tag1.length; i++) {
|
.map((tag) => {
|
||||||
if (tag1[i] !== tag2[i]) return false
|
if (tag.length < 3 || tag[0] !== 'emoji') return null
|
||||||
}
|
return { shortcode: tag[1], url: tag[2] }
|
||||||
return true
|
})
|
||||||
|
.filter(Boolean) as TEmoji[]
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getServersFromServerTags(tags: string[][] = []) {
|
||||||
|
return tags
|
||||||
|
.filter(tagNameEquals('server'))
|
||||||
|
.map(([, url]) => (url ? normalizeHttpUrl(url) : ''))
|
||||||
|
.filter(Boolean)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -49,6 +49,14 @@ export function isInViewport(el: HTMLElement) {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function isEmail(email: string) {
|
||||||
|
return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)
|
||||||
|
}
|
||||||
|
|
||||||
|
export function isDevEnv() {
|
||||||
|
return process.env.NODE_ENV === 'development'
|
||||||
|
}
|
||||||
|
|
||||||
export function detectLanguage(text?: string): string | null {
|
export function detectLanguage(text?: string): string | null {
|
||||||
if (!text) {
|
if (!text) {
|
||||||
return null
|
return null
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ import { Skeleton } from '@/components/ui/skeleton'
|
|||||||
import { ExtendedKind } from '@/constants'
|
import { ExtendedKind } from '@/constants'
|
||||||
import { useFetchEvent } from '@/hooks'
|
import { useFetchEvent } from '@/hooks'
|
||||||
import SecondaryPageLayout from '@/layouts/SecondaryPageLayout'
|
import SecondaryPageLayout from '@/layouts/SecondaryPageLayout'
|
||||||
import { getParentEventId, getParentEventTag, getRootEventId, isPictureEvent } from '@/lib/event'
|
import { getParentBech32Id, getParentETag, getRootBech32Id, isPictureEvent } from '@/lib/event'
|
||||||
import { toNote, toNoteList } from '@/lib/link'
|
import { toNote, toNoteList } from '@/lib/link'
|
||||||
import { tagNameEquals } from '@/lib/tag'
|
import { tagNameEquals } from '@/lib/tag'
|
||||||
import { cn } from '@/lib/utils'
|
import { cn } from '@/lib/utils'
|
||||||
@@ -24,8 +24,8 @@ import NotFoundPage from '../NotFoundPage'
|
|||||||
const NotePage = forwardRef(({ id, index }: { id?: string; index?: number }, ref) => {
|
const NotePage = forwardRef(({ id, index }: { id?: string; index?: number }, ref) => {
|
||||||
const { t } = useTranslation()
|
const { t } = useTranslation()
|
||||||
const { event, isFetching } = useFetchEvent(id)
|
const { event, isFetching } = useFetchEvent(id)
|
||||||
const parentEventId = useMemo(() => getParentEventId(event), [event])
|
const parentEventId = useMemo(() => getParentBech32Id(event), [event])
|
||||||
const rootEventId = useMemo(() => getRootEventId(event), [event])
|
const rootEventId = useMemo(() => getRootBech32Id(event), [event])
|
||||||
const rootITag = useMemo(
|
const rootITag = useMemo(
|
||||||
() => (event?.kind === ExtendedKind.COMMENT ? event.tags.find(tagNameEquals('I')) : undefined),
|
() => (event?.kind === ExtendedKind.COMMENT ? event.tags.find(tagNameEquals('I')) : undefined),
|
||||||
[event]
|
[event]
|
||||||
@@ -173,7 +173,7 @@ function ParentNote({
|
|||||||
}
|
}
|
||||||
|
|
||||||
function isConsecutive(rootEvent?: Event, parentEvent?: Event) {
|
function isConsecutive(rootEvent?: Event, parentEvent?: Event) {
|
||||||
const eTag = getParentEventTag(parentEvent)
|
const eTag = getParentETag(parentEvent)
|
||||||
if (!eTag) return false
|
if (!eTag) return false
|
||||||
|
|
||||||
return rootEvent?.id === eTag[1]
|
return rootEvent?.id === eTag[1]
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import { Input } from '@/components/ui/input'
|
|||||||
import { Separator } from '@/components/ui/separator'
|
import { Separator } from '@/components/ui/separator'
|
||||||
import { RECOMMENDED_BLOSSOM_SERVERS } from '@/constants'
|
import { RECOMMENDED_BLOSSOM_SERVERS } from '@/constants'
|
||||||
import { createBlossomServerListDraftEvent } from '@/lib/draft-event'
|
import { createBlossomServerListDraftEvent } from '@/lib/draft-event'
|
||||||
import { extractServersFromTags } from '@/lib/event'
|
import { getServersFromServerTags } from '@/lib/tag'
|
||||||
import { cn } from '@/lib/utils'
|
import { cn } from '@/lib/utils'
|
||||||
import { useNostr } from '@/providers/NostrProvider'
|
import { useNostr } from '@/providers/NostrProvider'
|
||||||
import client from '@/services/client.service'
|
import client from '@/services/client.service'
|
||||||
@@ -18,7 +18,7 @@ export default function BlossomServerListSetting() {
|
|||||||
const { pubkey, publish } = useNostr()
|
const { pubkey, publish } = useNostr()
|
||||||
const [blossomServerListEvent, setBlossomServerListEvent] = useState<Event | null>(null)
|
const [blossomServerListEvent, setBlossomServerListEvent] = useState<Event | null>(null)
|
||||||
const serverUrls = useMemo(() => {
|
const serverUrls = useMemo(() => {
|
||||||
return extractServersFromTags(blossomServerListEvent ? blossomServerListEvent.tags : [])
|
return getServersFromServerTags(blossomServerListEvent ? blossomServerListEvent.tags : [])
|
||||||
}, [blossomServerListEvent])
|
}, [blossomServerListEvent])
|
||||||
const [url, setUrl] = useState('')
|
const [url, setUrl] = useState('')
|
||||||
const [removingIndex, setRemovingIndex] = useState(-1)
|
const [removingIndex, setRemovingIndex] = useState(-1)
|
||||||
|
|||||||
@@ -6,9 +6,9 @@ import { Input } from '@/components/ui/input'
|
|||||||
import { Label } from '@/components/ui/label'
|
import { Label } from '@/components/ui/label'
|
||||||
import { Textarea } from '@/components/ui/textarea'
|
import { Textarea } from '@/components/ui/textarea'
|
||||||
import SecondaryPageLayout from '@/layouts/SecondaryPageLayout'
|
import SecondaryPageLayout from '@/layouts/SecondaryPageLayout'
|
||||||
import { isEmail } from '@/lib/common'
|
|
||||||
import { createProfileDraftEvent } from '@/lib/draft-event'
|
import { createProfileDraftEvent } from '@/lib/draft-event'
|
||||||
import { generateImageByPubkey } from '@/lib/pubkey'
|
import { generateImageByPubkey } from '@/lib/pubkey'
|
||||||
|
import { isEmail } from '@/lib/utils'
|
||||||
import { useSecondaryPage } from '@/PageManager'
|
import { useSecondaryPage } from '@/PageManager'
|
||||||
import { useNostr } from '@/providers/NostrProvider'
|
import { useNostr } from '@/providers/NostrProvider'
|
||||||
import { Loader, Upload } from 'lucide-react'
|
import { Loader, Upload } from 'lucide-react'
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
import { Button } from '@/components/ui/button'
|
import { Button } from '@/components/ui/button'
|
||||||
import { Input } from '@/components/ui/input'
|
import { Input } from '@/components/ui/input'
|
||||||
import { Label } from '@/components/ui/label'
|
import { Label } from '@/components/ui/label'
|
||||||
import { isEmail } from '@/lib/common'
|
|
||||||
import { createProfileDraftEvent } from '@/lib/draft-event'
|
import { createProfileDraftEvent } from '@/lib/draft-event'
|
||||||
|
import { isEmail } from '@/lib/utils'
|
||||||
import { useNostr } from '@/providers/NostrProvider'
|
import { useNostr } from '@/providers/NostrProvider'
|
||||||
import { Loader } from 'lucide-react'
|
import { Loader } from 'lucide-react'
|
||||||
import { useEffect, useState } from 'react'
|
import { useEffect, useState } from 'react'
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { BIG_RELAY_URLS, DEFAULT_FAVORITE_RELAYS } from '@/constants'
|
import { BIG_RELAY_URLS, DEFAULT_FAVORITE_RELAYS } from '@/constants'
|
||||||
import { createFavoriteRelaysDraftEvent, createRelaySetDraftEvent } from '@/lib/draft-event'
|
import { createFavoriteRelaysDraftEvent, createRelaySetDraftEvent } from '@/lib/draft-event'
|
||||||
import { getRelaySetFromRelaySetEvent, getReplaceableEventIdentifier } from '@/lib/event'
|
import { getReplaceableEventIdentifier } from '@/lib/event'
|
||||||
|
import { getRelaySetFromEvent } from '@/lib/event-metadata'
|
||||||
import { randomString } from '@/lib/random'
|
import { randomString } from '@/lib/random'
|
||||||
import { isWebsocketUrl, normalizeUrl } from '@/lib/url'
|
import { isWebsocketUrl, normalizeUrl } from '@/lib/url'
|
||||||
import client from '@/services/client.service'
|
import client from '@/services/client.service'
|
||||||
@@ -121,7 +122,7 @@ export function FavoriteRelaysProvider({ children }: { children: React.ReactNode
|
|||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setRelaySets(
|
setRelaySets(
|
||||||
relaySetEvents.map((evt) => getRelaySetFromRelaySetEvent(evt)).filter(Boolean) as TRelaySet[]
|
relaySetEvents.map((evt) => getRelaySetFromEvent(evt)).filter(Boolean) as TRelaySet[]
|
||||||
)
|
)
|
||||||
}, [relaySetEvents])
|
}, [relaySetEvents])
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { DEFAULT_FAVORITE_RELAYS } from '@/constants'
|
import { DEFAULT_FAVORITE_RELAYS } from '@/constants'
|
||||||
import { getRelaySetFromRelaySetEvent } from '@/lib/event'
|
import { getRelaySetFromEvent } from '@/lib/event-metadata'
|
||||||
import { checkAlgoRelay } from '@/lib/relay'
|
import { checkAlgoRelay } from '@/lib/relay'
|
||||||
import { isWebsocketUrl, normalizeUrl } from '@/lib/url'
|
import { isWebsocketUrl, normalizeUrl } from '@/lib/url'
|
||||||
import client from '@/services/client.service'
|
import client from '@/services/client.service'
|
||||||
@@ -150,7 +150,7 @@ export function FeedProvider({ children }: { children: React.ReactNode }) {
|
|||||||
relaySetId
|
relaySetId
|
||||||
)
|
)
|
||||||
if (storedRelaySetEvent) {
|
if (storedRelaySetEvent) {
|
||||||
relaySet = getRelaySetFromRelaySetEvent(storedRelaySetEvent)
|
relaySet = getRelaySetFromEvent(storedRelaySetEvent)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (relaySet) {
|
if (relaySet) {
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { createFollowListDraftEvent } from '@/lib/draft-event'
|
import { createFollowListDraftEvent } from '@/lib/draft-event'
|
||||||
import { extractPubkeysFromEventTags } from '@/lib/tag'
|
import { getPubkeysFromPTags } from '@/lib/tag'
|
||||||
import client from '@/services/client.service'
|
import client from '@/services/client.service'
|
||||||
import { createContext, useContext, useMemo } from 'react'
|
import { createContext, useContext, useMemo } from 'react'
|
||||||
import { useNostr } from './NostrProvider'
|
import { useNostr } from './NostrProvider'
|
||||||
@@ -23,7 +23,7 @@ export const useFollowList = () => {
|
|||||||
export function FollowListProvider({ children }: { children: React.ReactNode }) {
|
export function FollowListProvider({ children }: { children: React.ReactNode }) {
|
||||||
const { pubkey: accountPubkey, followListEvent, publish, updateFollowListEvent } = useNostr()
|
const { pubkey: accountPubkey, followListEvent, publish, updateFollowListEvent } = useNostr()
|
||||||
const followings = useMemo(
|
const followings = useMemo(
|
||||||
() => (followListEvent ? extractPubkeysFromEventTags(followListEvent.tags) : []),
|
() => (followListEvent ? getPubkeysFromPTags(followListEvent.tags) : []),
|
||||||
[followListEvent]
|
[followListEvent]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { createMuteListDraftEvent } from '@/lib/draft-event'
|
import { createMuteListDraftEvent } from '@/lib/draft-event'
|
||||||
import { extractPubkeysFromEventTags } from '@/lib/tag'
|
import { getPubkeysFromPTags } from '@/lib/tag'
|
||||||
import client from '@/services/client.service'
|
import client from '@/services/client.service'
|
||||||
import indexedDb from '@/services/indexed-db.service'
|
import indexedDb from '@/services/indexed-db.service'
|
||||||
import dayjs from 'dayjs'
|
import dayjs from 'dayjs'
|
||||||
@@ -44,9 +44,9 @@ export function MuteListProvider({ children }: { children: React.ReactNode }) {
|
|||||||
} = useNostr()
|
} = useNostr()
|
||||||
const [tags, setTags] = useState<string[][]>([])
|
const [tags, setTags] = useState<string[][]>([])
|
||||||
const [privateTags, setPrivateTags] = useState<string[][]>([])
|
const [privateTags, setPrivateTags] = useState<string[][]>([])
|
||||||
const publicMutePubkeySet = useMemo(() => new Set(extractPubkeysFromEventTags(tags)), [tags])
|
const publicMutePubkeySet = useMemo(() => new Set(getPubkeysFromPTags(tags)), [tags])
|
||||||
const privateMutePubkeySet = useMemo(
|
const privateMutePubkeySet = useMemo(
|
||||||
() => new Set(extractPubkeysFromEventTags(privateTags)),
|
() => new Set(getPubkeysFromPTags(privateTags)),
|
||||||
[privateTags]
|
[privateTags]
|
||||||
)
|
)
|
||||||
const mutePubkeys = useMemo(() => {
|
const mutePubkeys = useMemo(() => {
|
||||||
|
|||||||
@@ -1,12 +1,8 @@
|
|||||||
import LoginDialog from '@/components/LoginDialog'
|
import LoginDialog from '@/components/LoginDialog'
|
||||||
import { ApplicationDataKey, BIG_RELAY_URLS, ExtendedKind } from '@/constants'
|
import { ApplicationDataKey, BIG_RELAY_URLS, ExtendedKind } from '@/constants'
|
||||||
import { createSeenNotificationsAtDraftEvent } from '@/lib/draft-event'
|
import { createSeenNotificationsAtDraftEvent } from '@/lib/draft-event'
|
||||||
import {
|
import { getLatestEvent, getReplaceableEventIdentifier } from '@/lib/event'
|
||||||
getLatestEvent,
|
import { getProfileFromEvent, getRelayListFromEvent } from '@/lib/event-metadata'
|
||||||
getProfileFromProfileEvent,
|
|
||||||
getRelayListFromRelayListEvent,
|
|
||||||
getReplaceableEventIdentifier
|
|
||||||
} from '@/lib/event'
|
|
||||||
import { formatPubkey, isValidPubkey, pubkeyToNpub } from '@/lib/pubkey'
|
import { formatPubkey, isValidPubkey, pubkeyToNpub } from '@/lib/pubkey'
|
||||||
import client from '@/services/client.service'
|
import client from '@/services/client.service'
|
||||||
import indexedDb from '@/services/indexed-db.service'
|
import indexedDb from '@/services/indexed-db.service'
|
||||||
@@ -172,11 +168,11 @@ export function NostrProvider({ children }: { children: React.ReactNode }) {
|
|||||||
indexedDb.getReplaceableEvent(account.pubkey, ExtendedKind.FAVORITE_RELAYS)
|
indexedDb.getReplaceableEvent(account.pubkey, ExtendedKind.FAVORITE_RELAYS)
|
||||||
])
|
])
|
||||||
if (storedRelayListEvent) {
|
if (storedRelayListEvent) {
|
||||||
setRelayList(getRelayListFromRelayListEvent(storedRelayListEvent))
|
setRelayList(getRelayListFromEvent(storedRelayListEvent))
|
||||||
}
|
}
|
||||||
if (storedProfileEvent) {
|
if (storedProfileEvent) {
|
||||||
setProfileEvent(storedProfileEvent)
|
setProfileEvent(storedProfileEvent)
|
||||||
setProfile(getProfileFromProfileEvent(storedProfileEvent))
|
setProfile(getProfileFromEvent(storedProfileEvent))
|
||||||
}
|
}
|
||||||
if (storedFollowListEvent) {
|
if (storedFollowListEvent) {
|
||||||
setFollowListEvent(storedFollowListEvent)
|
setFollowListEvent(storedFollowListEvent)
|
||||||
@@ -196,7 +192,7 @@ export function NostrProvider({ children }: { children: React.ReactNode }) {
|
|||||||
authors: [account.pubkey]
|
authors: [account.pubkey]
|
||||||
})
|
})
|
||||||
const relayListEvent = getLatestEvent(relayListEvents) ?? storedRelayListEvent
|
const relayListEvent = getLatestEvent(relayListEvents) ?? storedRelayListEvent
|
||||||
const relayList = getRelayListFromRelayListEvent(relayListEvent)
|
const relayList = getRelayListFromEvent(relayListEvent)
|
||||||
if (relayListEvent) {
|
if (relayListEvent) {
|
||||||
client.updateRelayListCache(relayListEvent)
|
client.updateRelayListCache(relayListEvent)
|
||||||
await indexedDb.putReplaceableEvent(relayListEvent)
|
await indexedDb.putReplaceableEvent(relayListEvent)
|
||||||
@@ -237,7 +233,7 @@ export function NostrProvider({ children }: { children: React.ReactNode }) {
|
|||||||
)
|
)
|
||||||
if (profileEvent) {
|
if (profileEvent) {
|
||||||
setProfileEvent(profileEvent)
|
setProfileEvent(profileEvent)
|
||||||
setProfile(getProfileFromProfileEvent(profileEvent))
|
setProfile(getProfileFromEvent(profileEvent))
|
||||||
await indexedDb.putReplaceableEvent(profileEvent)
|
await indexedDb.putReplaceableEvent(profileEvent)
|
||||||
} else if (!storedProfileEvent) {
|
} else if (!storedProfileEvent) {
|
||||||
setProfile({
|
setProfile({
|
||||||
@@ -639,13 +635,13 @@ export function NostrProvider({ children }: { children: React.ReactNode }) {
|
|||||||
|
|
||||||
const updateRelayListEvent = async (relayListEvent: Event) => {
|
const updateRelayListEvent = async (relayListEvent: Event) => {
|
||||||
const newRelayList = await indexedDb.putReplaceableEvent(relayListEvent)
|
const newRelayList = await indexedDb.putReplaceableEvent(relayListEvent)
|
||||||
setRelayList(getRelayListFromRelayListEvent(newRelayList))
|
setRelayList(getRelayListFromEvent(newRelayList))
|
||||||
}
|
}
|
||||||
|
|
||||||
const updateProfileEvent = async (profileEvent: Event) => {
|
const updateProfileEvent = async (profileEvent: Event) => {
|
||||||
const newProfileEvent = await indexedDb.putReplaceableEvent(profileEvent)
|
const newProfileEvent = await indexedDb.putReplaceableEvent(profileEvent)
|
||||||
setProfileEvent(newProfileEvent)
|
setProfileEvent(newProfileEvent)
|
||||||
setProfile(getProfileFromProfileEvent(newProfileEvent))
|
setProfile(getProfileFromEvent(newProfileEvent))
|
||||||
}
|
}
|
||||||
|
|
||||||
const updateFollowListEvent = async (followListEvent: Event) => {
|
const updateFollowListEvent = async (followListEvent: Event) => {
|
||||||
|
|||||||
@@ -1,9 +1,4 @@
|
|||||||
import {
|
import { getParentATag, getParentETag, getRootATag, getRootETag } from '@/lib/event'
|
||||||
getParentAddressableEventTag,
|
|
||||||
getParentEventTag,
|
|
||||||
getRootAddressableEventTag,
|
|
||||||
getRootEventTag
|
|
||||||
} from '@/lib/event'
|
|
||||||
import { Event } from 'nostr-tools'
|
import { Event } from 'nostr-tools'
|
||||||
import { createContext, useCallback, useContext, useState } from 'react'
|
import { createContext, useCallback, useContext, useState } from 'react'
|
||||||
|
|
||||||
@@ -35,11 +30,11 @@ export function ReplyProvider({ children }: { children: React.ReactNode }) {
|
|||||||
newReplyIdSet.add(reply.id)
|
newReplyIdSet.add(reply.id)
|
||||||
|
|
||||||
let rootId: string | undefined
|
let rootId: string | undefined
|
||||||
const rootETag = getRootEventTag(reply)
|
const rootETag = getRootETag(reply)
|
||||||
if (rootETag) {
|
if (rootETag) {
|
||||||
rootId = rootETag[1]
|
rootId = rootETag[1]
|
||||||
} else {
|
} else {
|
||||||
const rootATag = getRootAddressableEventTag(reply)
|
const rootATag = getRootATag(reply)
|
||||||
if (rootATag) {
|
if (rootATag) {
|
||||||
rootId = rootATag[1]
|
rootId = rootATag[1]
|
||||||
}
|
}
|
||||||
@@ -49,11 +44,11 @@ export function ReplyProvider({ children }: { children: React.ReactNode }) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let parentId: string | undefined
|
let parentId: string | undefined
|
||||||
const parentETag = getParentEventTag(reply)
|
const parentETag = getParentETag(reply)
|
||||||
if (parentETag) {
|
if (parentETag) {
|
||||||
parentId = parentETag[1]
|
parentId = parentETag[1]
|
||||||
} else {
|
} else {
|
||||||
const parentATag = getParentAddressableEventTag(reply)
|
const parentATag = getParentATag(reply)
|
||||||
if (parentATag) {
|
if (parentATag) {
|
||||||
parentId = parentATag[1]
|
parentId = parentATag[1]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,7 @@
|
|||||||
import { BIG_RELAY_URLS, ExtendedKind } from '@/constants'
|
import { BIG_RELAY_URLS, ExtendedKind } from '@/constants'
|
||||||
import {
|
import { getProfileFromEvent, getRelayListFromEvent } from '@/lib/event-metadata'
|
||||||
extractServersFromTags,
|
|
||||||
getProfileFromProfileEvent,
|
|
||||||
getRelayListFromRelayListEvent
|
|
||||||
} from '@/lib/event'
|
|
||||||
import { formatPubkey, pubkeyToNpub, userIdToPubkey } from '@/lib/pubkey'
|
import { formatPubkey, pubkeyToNpub, userIdToPubkey } from '@/lib/pubkey'
|
||||||
import { extractPubkeysFromEventTags } from '@/lib/tag'
|
import { getPubkeysFromPTags, getServersFromServerTags } from '@/lib/tag'
|
||||||
import { isLocalNetworkUrl, isWebsocketUrl, normalizeUrl } from '@/lib/url'
|
import { isLocalNetworkUrl, isWebsocketUrl, normalizeUrl } from '@/lib/url'
|
||||||
import { ISigner, TProfile, TRelayList } from '@/types'
|
import { ISigner, TProfile, TRelayList } from '@/types'
|
||||||
import { sha256 } from '@noble/hashes/sha2'
|
import { sha256 } from '@noble/hashes/sha2'
|
||||||
@@ -716,7 +712,7 @@ class ClientService extends EventTarget {
|
|||||||
async fetchProfile(id: string, skipCache: boolean = false): Promise<TProfile | undefined> {
|
async fetchProfile(id: string, skipCache: boolean = false): Promise<TProfile | undefined> {
|
||||||
const profileEvent = await this.fetchProfileEvent(id, skipCache)
|
const profileEvent = await this.fetchProfileEvent(id, skipCache)
|
||||||
if (profileEvent) {
|
if (profileEvent) {
|
||||||
return getProfileFromProfileEvent(profileEvent)
|
return getProfileFromEvent(profileEvent)
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -735,7 +731,7 @@ class ClientService extends EventTarget {
|
|||||||
|
|
||||||
const profileEvents = events.sort((a, b) => b.created_at - a.created_at)
|
const profileEvents = events.sort((a, b) => b.created_at - a.created_at)
|
||||||
await Promise.all(profileEvents.map((profile) => this.addUsernameToIndex(profile)))
|
await Promise.all(profileEvents.map((profile) => this.addUsernameToIndex(profile)))
|
||||||
return profileEvents.map((profileEvent) => getProfileFromProfileEvent(profileEvent))
|
return profileEvents.map((profileEvent) => getProfileFromEvent(profileEvent))
|
||||||
}
|
}
|
||||||
|
|
||||||
async fetchRelayListEvent(pubkey: string) {
|
async fetchRelayListEvent(pubkey: string) {
|
||||||
@@ -769,7 +765,7 @@ class ClientService extends EventTarget {
|
|||||||
|
|
||||||
return relayEvents.map((event) => {
|
return relayEvents.map((event) => {
|
||||||
if (event) {
|
if (event) {
|
||||||
return getRelayListFromRelayListEvent(event)
|
return getRelayListFromEvent(event)
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
write: BIG_RELAY_URLS,
|
write: BIG_RELAY_URLS,
|
||||||
@@ -821,7 +817,7 @@ class ClientService extends EventTarget {
|
|||||||
|
|
||||||
async fetchFollowings(pubkey: string) {
|
async fetchFollowings(pubkey: string) {
|
||||||
const followListEvent = await this.fetchFollowListEvent(pubkey)
|
const followListEvent = await this.fetchFollowListEvent(pubkey)
|
||||||
return followListEvent ? extractPubkeysFromEventTags(followListEvent.tags) : []
|
return followListEvent ? getPubkeysFromPTags(followListEvent.tags) : []
|
||||||
}
|
}
|
||||||
|
|
||||||
async fetchFollowingFavoriteRelays(pubkey: string) {
|
async fetchFollowingFavoriteRelays(pubkey: string) {
|
||||||
@@ -881,7 +877,7 @@ class ClientService extends EventTarget {
|
|||||||
|
|
||||||
async fetchBlossomServerList(pubkey: string) {
|
async fetchBlossomServerList(pubkey: string) {
|
||||||
const evt = await this.blossomServerListEventCache.fetch(pubkey)
|
const evt = await this.blossomServerListEventCache.fetch(pubkey)
|
||||||
return evt ? extractServersFromTags(evt.tags) : []
|
return evt ? getServersFromServerTags(evt.tags) : []
|
||||||
}
|
}
|
||||||
|
|
||||||
async fetchBlossomServerListEvent(pubkey: string) {
|
async fetchBlossomServerListEvent(pubkey: string) {
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { BIG_RELAY_URLS, CODY_PUBKEY } from '@/constants'
|
import { BIG_RELAY_URLS, CODY_PUBKEY } from '@/constants'
|
||||||
import { extractZapInfoFromReceipt } from '@/lib/event'
|
import { getZapInfoFromEvent } from '@/lib/event-metadata'
|
||||||
import { TProfile } from '@/types'
|
import { TProfile } from '@/types'
|
||||||
import {
|
import {
|
||||||
init,
|
init,
|
||||||
@@ -141,7 +141,7 @@ class LightningService {
|
|||||||
filter,
|
filter,
|
||||||
{
|
{
|
||||||
onevent: (evt) => {
|
onevent: (evt) => {
|
||||||
const info = extractZapInfoFromReceipt(evt)
|
const info = getZapInfoFromEvent(evt)
|
||||||
if (!info) return
|
if (!info) return
|
||||||
|
|
||||||
if (info.invoice === pr) {
|
if (info.invoice === pr) {
|
||||||
@@ -154,7 +154,10 @@ class LightningService {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async payInvoice(invoice: string, closeOuterModel?: () => void): Promise<{ preimage: string; invoice: string } | null> {
|
async payInvoice(
|
||||||
|
invoice: string,
|
||||||
|
closeOuterModel?: () => void
|
||||||
|
): Promise<{ preimage: string; invoice: string } | null> {
|
||||||
if (this.provider) {
|
if (this.provider) {
|
||||||
const { preimage } = await this.provider.sendPayment(invoice)
|
const { preimage } = await this.provider.sendPayment(invoice)
|
||||||
closeOuterModel?.()
|
closeOuterModel?.()
|
||||||
@@ -189,7 +192,7 @@ class LightningService {
|
|||||||
events.sort((a, b) => b.created_at - a.created_at)
|
events.sort((a, b) => b.created_at - a.created_at)
|
||||||
const map = new Map<string, { pubkey: string; amount: number; comment?: string }>()
|
const map = new Map<string, { pubkey: string; amount: number; comment?: string }>()
|
||||||
events.forEach((event) => {
|
events.forEach((event) => {
|
||||||
const info = extractZapInfoFromReceipt(event)
|
const info = getZapInfoFromEvent(event)
|
||||||
if (!info || info.eventId || !info.senderPubkey || info.senderPubkey === CODY_PUBKEY) return
|
if (!info || info.eventId || !info.senderPubkey || info.senderPubkey === CODY_PUBKEY) return
|
||||||
|
|
||||||
const { amount, comment, senderPubkey } = info
|
const { amount, comment, senderPubkey } = info
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { extractEmojiInfosFromTags, extractZapInfoFromReceipt } from '@/lib/event'
|
import { getZapInfoFromEvent } from '@/lib/event-metadata'
|
||||||
import { tagNameEquals } from '@/lib/tag'
|
import { getEmojiInfosFromEmojiTags, tagNameEquals } from '@/lib/tag'
|
||||||
import client from '@/services/client.service'
|
import client from '@/services/client.service'
|
||||||
import { TEmoji } from '@/types'
|
import { TEmoji } from '@/types'
|
||||||
import dayjs from 'dayjs'
|
import dayjs from 'dayjs'
|
||||||
@@ -171,7 +171,7 @@ class NoteStatsService {
|
|||||||
if (!emoji) return
|
if (!emoji) return
|
||||||
|
|
||||||
if (/^:[a-zA-Z0-9_-]+:$/.test(evt.content)) {
|
if (/^:[a-zA-Z0-9_-]+:$/.test(evt.content)) {
|
||||||
const emojiInfos = extractEmojiInfosFromTags(evt.tags)
|
const emojiInfos = getEmojiInfosFromEmojiTags(evt.tags)
|
||||||
const shortcode = evt.content.split(':')[1]
|
const shortcode = evt.content.split(':')[1]
|
||||||
const emojiInfo = emojiInfos.find((info) => info.shortcode === shortcode)
|
const emojiInfo = emojiInfos.find((info) => info.shortcode === shortcode)
|
||||||
if (emojiInfo) {
|
if (emojiInfo) {
|
||||||
@@ -199,7 +199,7 @@ class NoteStatsService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private addZapByEvent(evt: Event) {
|
private addZapByEvent(evt: Event) {
|
||||||
const info = extractZapInfoFromReceipt(evt)
|
const info = getZapInfoFromEvent(evt)
|
||||||
if (!info) return
|
if (!info) return
|
||||||
const { originalEventId, senderPubkey, invoice, amount, comment } = info
|
const { originalEventId, senderPubkey, invoice, amount, comment } = info
|
||||||
if (!originalEventId || !senderPubkey) return
|
if (!originalEventId || !senderPubkey) return
|
||||||
|
|||||||
Reference in New Issue
Block a user