feat: blurhash
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import { COMMENT_EVENT_KIND, PICTURE_EVENT_KIND } from '@/constants'
|
||||
import client from '@/services/client.service'
|
||||
import { Event, kinds, nip19 } from 'nostr-tools'
|
||||
import { extractImetaUrlFromTag, isReplyETag, isRootETag, tagNameEquals } from './tag'
|
||||
import { extractImageInfoFromTag, isReplyETag, isRootETag, tagNameEquals } from './tag'
|
||||
|
||||
export function isNsfwEvent(event: Event) {
|
||||
return event.tags.some(
|
||||
@@ -200,7 +200,7 @@ export function extractHashtags(content: string) {
|
||||
export function extractFirstPictureFromPictureEvent(event: Event) {
|
||||
if (!isPictureEvent(event)) return null
|
||||
for (const tag of event.tags) {
|
||||
const url = extractImetaUrlFromTag(tag)
|
||||
const url = extractImageInfoFromTag(tag)
|
||||
if (url) return url
|
||||
}
|
||||
return null
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
import { TImageInfo } from '@/types'
|
||||
import { isBlurhashValid } from 'blurhash'
|
||||
|
||||
export function tagNameEquals(tagName: string) {
|
||||
return (tag: string[]) => tag[0] === tagName
|
||||
}
|
||||
@@ -14,9 +17,28 @@ export function isMentionETag([tagName, , , marker]: string[]) {
|
||||
return tagName === 'e' && marker === 'mention'
|
||||
}
|
||||
|
||||
export function extractImetaUrlFromTag(tag: string[]) {
|
||||
export function extractImageInfoFromTag(tag: string[]): TImageInfo | null {
|
||||
if (tag[0] !== 'imeta') return null
|
||||
const urlItem = tag.find((item) => item.startsWith('url '))
|
||||
const url = urlItem?.slice(4)
|
||||
return url || null
|
||||
if (!url) return null
|
||||
|
||||
const image: TImageInfo = { url }
|
||||
const blurHashItem = tag.find((item) => item.startsWith('blurhash '))
|
||||
const blurHash = blurHashItem?.slice(9)
|
||||
if (blurHash) {
|
||||
const validRes = isBlurhashValid(blurHash)
|
||||
if (validRes.result) {
|
||||
image.blurHash = blurHash
|
||||
}
|
||||
}
|
||||
const dimItem = tag.find((item) => item.startsWith('dim '))
|
||||
const dim = dimItem?.slice(4)
|
||||
if (dim) {
|
||||
const [width, height] = dim.split('x').map(Number)
|
||||
if (width && height) {
|
||||
image.dim = { width, height }
|
||||
}
|
||||
}
|
||||
return image
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user