feat: pow
This commit is contained in:
@@ -95,7 +95,14 @@ export default function AudioPlayer({ src, className }: AudioPlayerProps) {
|
|||||||
|
|
||||||
{/* Progress Section */}
|
{/* Progress Section */}
|
||||||
<div className="flex-1 relative">
|
<div className="flex-1 relative">
|
||||||
<Slider value={[currentTime]} max={duration || 100} step={1} onValueChange={handleSeek} />
|
<Slider
|
||||||
|
value={[currentTime]}
|
||||||
|
max={duration || 100}
|
||||||
|
step={1}
|
||||||
|
onValueChange={handleSeek}
|
||||||
|
hideThumb
|
||||||
|
enableHoverAnimation
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="text-sm font-mono text-muted-foreground">
|
<div className="text-sm font-mono text-muted-foreground">
|
||||||
|
|||||||
@@ -4,7 +4,8 @@ import { ScrollArea } from '@/components/ui/scroll-area'
|
|||||||
import {
|
import {
|
||||||
createCommentDraftEvent,
|
createCommentDraftEvent,
|
||||||
createPollDraftEvent,
|
createPollDraftEvent,
|
||||||
createShortTextNoteDraftEvent
|
createShortTextNoteDraftEvent,
|
||||||
|
deleteDraftEventCache
|
||||||
} from '@/lib/draft-event'
|
} from '@/lib/draft-event'
|
||||||
import { isTouchDevice } from '@/lib/utils'
|
import { isTouchDevice } from '@/lib/utils'
|
||||||
import { useNostr } from '@/providers/NostrProvider'
|
import { useNostr } from '@/providers/NostrProvider'
|
||||||
@@ -56,6 +57,7 @@ export default function PostContent({
|
|||||||
endsAt: undefined,
|
endsAt: undefined,
|
||||||
relays: []
|
relays: []
|
||||||
})
|
})
|
||||||
|
const [minPow, setMinPow] = useState(0)
|
||||||
const isFirstRender = useRef(true)
|
const isFirstRender = useRef(true)
|
||||||
const canPost =
|
const canPost =
|
||||||
!!pubkey &&
|
!!pubkey &&
|
||||||
@@ -133,10 +135,12 @@ export default function PostContent({
|
|||||||
|
|
||||||
const newEvent = await publish(draftEvent, {
|
const newEvent = await publish(draftEvent, {
|
||||||
specifiedRelayUrls,
|
specifiedRelayUrls,
|
||||||
additionalRelayUrls: isPoll ? pollCreateData.relays : []
|
additionalRelayUrls: isPoll ? pollCreateData.relays : [],
|
||||||
|
minPow
|
||||||
})
|
})
|
||||||
addReplies([newEvent])
|
|
||||||
postEditorCache.clearPostCache({ defaultContent, parentEvent })
|
postEditorCache.clearPostCache({ defaultContent, parentEvent })
|
||||||
|
deleteDraftEventCache(draftEvent)
|
||||||
|
addReplies([newEvent])
|
||||||
close()
|
close()
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error instanceof AggregateError) {
|
if (error instanceof AggregateError) {
|
||||||
@@ -311,11 +315,14 @@ export default function PostContent({
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<PostOptions
|
<PostOptions
|
||||||
|
posting={posting}
|
||||||
show={showMoreOptions}
|
show={showMoreOptions}
|
||||||
addClientTag={addClientTag}
|
addClientTag={addClientTag}
|
||||||
setAddClientTag={setAddClientTag}
|
setAddClientTag={setAddClientTag}
|
||||||
isNsfw={isNsfw}
|
isNsfw={isNsfw}
|
||||||
setIsNsfw={setIsNsfw}
|
setIsNsfw={setIsNsfw}
|
||||||
|
minPow={minPow}
|
||||||
|
setMinPow={setMinPow}
|
||||||
/>
|
/>
|
||||||
<div className="flex gap-2 items-center justify-around sm:hidden">
|
<div className="flex gap-2 items-center justify-around sm:hidden">
|
||||||
<Button
|
<Button
|
||||||
|
|||||||
@@ -1,21 +1,28 @@
|
|||||||
import { Label } from '@/components/ui/label'
|
import { Label } from '@/components/ui/label'
|
||||||
|
import { Slider } from '@/components/ui/slider'
|
||||||
import { Switch } from '@/components/ui/switch'
|
import { Switch } from '@/components/ui/switch'
|
||||||
import { StorageKey } from '@/constants'
|
import { StorageKey } from '@/constants'
|
||||||
import { Dispatch, SetStateAction, useEffect } from 'react'
|
import { Dispatch, SetStateAction, useEffect } from 'react'
|
||||||
import { useTranslation } from 'react-i18next'
|
import { useTranslation } from 'react-i18next'
|
||||||
|
|
||||||
export default function PostOptions({
|
export default function PostOptions({
|
||||||
|
posting,
|
||||||
show,
|
show,
|
||||||
addClientTag,
|
addClientTag,
|
||||||
setAddClientTag,
|
setAddClientTag,
|
||||||
isNsfw,
|
isNsfw,
|
||||||
setIsNsfw
|
setIsNsfw,
|
||||||
|
minPow,
|
||||||
|
setMinPow
|
||||||
}: {
|
}: {
|
||||||
|
posting: boolean
|
||||||
show: boolean
|
show: boolean
|
||||||
addClientTag: boolean
|
addClientTag: boolean
|
||||||
setAddClientTag: Dispatch<SetStateAction<boolean>>
|
setAddClientTag: Dispatch<SetStateAction<boolean>>
|
||||||
isNsfw: boolean
|
isNsfw: boolean
|
||||||
setIsNsfw: Dispatch<SetStateAction<boolean>>
|
setIsNsfw: Dispatch<SetStateAction<boolean>>
|
||||||
|
minPow: number
|
||||||
|
setMinPow: Dispatch<SetStateAction<number>>
|
||||||
}) {
|
}) {
|
||||||
const { t } = useTranslation()
|
const { t } = useTranslation()
|
||||||
|
|
||||||
@@ -43,6 +50,7 @@ export default function PostOptions({
|
|||||||
id="add-client-tag"
|
id="add-client-tag"
|
||||||
checked={addClientTag}
|
checked={addClientTag}
|
||||||
onCheckedChange={onAddClientTagChange}
|
onCheckedChange={onAddClientTagChange}
|
||||||
|
disabled={posting}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<div className="text-muted-foreground text-xs">
|
<div className="text-muted-foreground text-xs">
|
||||||
@@ -52,7 +60,24 @@ export default function PostOptions({
|
|||||||
|
|
||||||
<div className="flex items-center space-x-2">
|
<div className="flex items-center space-x-2">
|
||||||
<Label htmlFor="add-nsfw-tag">{t('NSFW')}</Label>
|
<Label htmlFor="add-nsfw-tag">{t('NSFW')}</Label>
|
||||||
<Switch id="add-nsfw-tag" checked={isNsfw} onCheckedChange={onNsfwChange} />
|
<Switch
|
||||||
|
id="add-nsfw-tag"
|
||||||
|
checked={isNsfw}
|
||||||
|
onCheckedChange={onNsfwChange}
|
||||||
|
disabled={posting}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="grid gap-4 pb-4">
|
||||||
|
<Label>{t('Proof of Work (difficulty {{minPow}})', { minPow })}</Label>
|
||||||
|
<Slider
|
||||||
|
defaultValue={[0]}
|
||||||
|
value={[minPow]}
|
||||||
|
onValueChange={([pow]) => setMinPow(pow)}
|
||||||
|
max={28}
|
||||||
|
step={1}
|
||||||
|
disabled={posting}
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -5,8 +5,11 @@ import { cn } from '@/lib/utils'
|
|||||||
|
|
||||||
const Slider = React.forwardRef<
|
const Slider = React.forwardRef<
|
||||||
React.ElementRef<typeof SliderPrimitive.Root>,
|
React.ElementRef<typeof SliderPrimitive.Root>,
|
||||||
React.ComponentPropsWithoutRef<typeof SliderPrimitive.Root> & { hideThumb?: boolean }
|
React.ComponentPropsWithoutRef<typeof SliderPrimitive.Root> & {
|
||||||
>(({ className, ...props }, ref) => {
|
hideThumb?: boolean
|
||||||
|
enableHoverAnimation?: boolean
|
||||||
|
}
|
||||||
|
>(({ className, hideThumb, enableHoverAnimation, ...props }, ref) => {
|
||||||
const [isHovered, setIsHovered] = React.useState(false)
|
const [isHovered, setIsHovered] = React.useState(false)
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -22,17 +25,18 @@ const Slider = React.forwardRef<
|
|||||||
<SliderPrimitive.Track
|
<SliderPrimitive.Track
|
||||||
className={cn(
|
className={cn(
|
||||||
'relative w-full grow overflow-hidden rounded-full bg-primary/20 cursor-pointer transition-all',
|
'relative w-full grow overflow-hidden rounded-full bg-primary/20 cursor-pointer transition-all',
|
||||||
isHovered ? 'h-3' : 'h-1.5'
|
isHovered && enableHoverAnimation ? 'h-3' : 'h-1.5'
|
||||||
)}
|
)}
|
||||||
>
|
>
|
||||||
<SliderPrimitive.Range className="absolute h-full bg-primary rounded-full" />
|
<SliderPrimitive.Range className="absolute h-full bg-primary disabled:bg-primary/30 rounded-full" />
|
||||||
</SliderPrimitive.Track>
|
</SliderPrimitive.Track>
|
||||||
{/* <SliderPrimitive.Thumb
|
{!hideThumb && (
|
||||||
className={cn(
|
<SliderPrimitive.Thumb
|
||||||
'block h-4 w-4 rounded-full border border-primary/50 bg-background shadow transition-all duration-300 cursor-pointer focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:pointer-events-none disabled:opacity-50',
|
className={cn(
|
||||||
isHovered ? 'opacity-100' : 'opacity-0'
|
'block h-4 w-4 rounded-full border border-primary/50 bg-background shadow transition-all duration-300 cursor-pointer focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:pointer-events-none disabled:opacity-50'
|
||||||
)}
|
)}
|
||||||
/> */}
|
/>
|
||||||
|
)}
|
||||||
</SliderPrimitive.Root>
|
</SliderPrimitive.Root>
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -399,6 +399,7 @@ export default {
|
|||||||
Detailed: 'تفصيلي',
|
Detailed: 'تفصيلي',
|
||||||
Compact: 'مضغوط',
|
Compact: 'مضغوط',
|
||||||
'Submit Relay': 'إرسال ريلاي',
|
'Submit Relay': 'إرسال ريلاي',
|
||||||
Homepage: 'الصفحة الرئيسية'
|
Homepage: 'الصفحة الرئيسية',
|
||||||
|
'Proof of Work (difficulty {{minPow}})': 'إثبات العمل (الصعوبة {{minPow}})'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -409,6 +409,7 @@ export default {
|
|||||||
Detailed: 'Detailliert',
|
Detailed: 'Detailliert',
|
||||||
Compact: 'Kompakt',
|
Compact: 'Kompakt',
|
||||||
'Submit Relay': 'Relay einreichen',
|
'Submit Relay': 'Relay einreichen',
|
||||||
Homepage: 'Homepage'
|
Homepage: 'Homepage',
|
||||||
|
'Proof of Work (difficulty {{minPow}})': 'Arbeitsnachweis (Schwierigkeit {{minPow}})'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -398,6 +398,7 @@ export default {
|
|||||||
Detailed: 'Detailed',
|
Detailed: 'Detailed',
|
||||||
Compact: 'Compact',
|
Compact: 'Compact',
|
||||||
'Submit Relay': 'Submit Relay',
|
'Submit Relay': 'Submit Relay',
|
||||||
Homepage: 'Homepage'
|
Homepage: 'Homepage',
|
||||||
|
'Proof of Work (difficulty {{minPow}})': 'Proof of Work (difficulty {{minPow}})'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -404,6 +404,7 @@ export default {
|
|||||||
Detailed: 'Detallado',
|
Detailed: 'Detallado',
|
||||||
Compact: 'Compacto',
|
Compact: 'Compacto',
|
||||||
'Submit Relay': 'Enviar relé',
|
'Submit Relay': 'Enviar relé',
|
||||||
Homepage: 'Página principal'
|
Homepage: 'Página principal',
|
||||||
|
'Proof of Work (difficulty {{minPow}})': 'Prueba de Trabajo (dificultad {{minPow}})'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -400,6 +400,7 @@ export default {
|
|||||||
Detailed: 'تفصیلی',
|
Detailed: 'تفصیلی',
|
||||||
Compact: 'فشرده',
|
Compact: 'فشرده',
|
||||||
'Submit Relay': 'ارسال رله',
|
'Submit Relay': 'ارسال رله',
|
||||||
Homepage: 'صفحه اصلی'
|
Homepage: 'صفحه اصلی',
|
||||||
|
'Proof of Work (difficulty {{minPow}})': 'اثبات کار (دشواری {{minPow}})'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -409,6 +409,7 @@ export default {
|
|||||||
Detailed: 'Détaillé',
|
Detailed: 'Détaillé',
|
||||||
Compact: 'Compact',
|
Compact: 'Compact',
|
||||||
'Submit Relay': 'Soumettre un relais',
|
'Submit Relay': 'Soumettre un relais',
|
||||||
Homepage: 'Page d’accueil'
|
Homepage: 'Page d’accueil',
|
||||||
|
'Proof of Work (difficulty {{minPow}})': 'Preuve de travail (difficulté {{minPow}})'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -403,6 +403,7 @@ export default {
|
|||||||
Detailed: 'विस्तृत',
|
Detailed: 'विस्तृत',
|
||||||
Compact: 'संक्षिप्त',
|
Compact: 'संक्षिप्त',
|
||||||
'Submit Relay': 'रिले सबमिट करें',
|
'Submit Relay': 'रिले सबमिट करें',
|
||||||
Homepage: 'होमपेज'
|
Homepage: 'होमपेज',
|
||||||
|
'Proof of Work (difficulty {{minPow}})': 'कार्य प्रमाण (कठिनाई {{minPow}})'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -404,6 +404,7 @@ export default {
|
|||||||
Detailed: 'Dettagliato',
|
Detailed: 'Dettagliato',
|
||||||
Compact: 'Compatto',
|
Compact: 'Compatto',
|
||||||
'Submit Relay': 'Invia Relay',
|
'Submit Relay': 'Invia Relay',
|
||||||
Homepage: 'Homepage'
|
Homepage: 'Homepage',
|
||||||
|
'Proof of Work (difficulty {{minPow}})': 'Proof of Work (difficoltà {{minPow}})'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -401,6 +401,7 @@ export default {
|
|||||||
Detailed: '詳細',
|
Detailed: '詳細',
|
||||||
Compact: 'コンパクト',
|
Compact: 'コンパクト',
|
||||||
'Submit Relay': 'リレーを提出',
|
'Submit Relay': 'リレーを提出',
|
||||||
Homepage: 'ホームページ'
|
Homepage: 'ホームページ',
|
||||||
|
'Proof of Work (difficulty {{minPow}})': 'プルーフオブワーク (難易度 {{minPow}})'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -401,6 +401,7 @@ export default {
|
|||||||
Detailed: '상세',
|
Detailed: '상세',
|
||||||
Compact: '간단',
|
Compact: '간단',
|
||||||
'Submit Relay': '릴레이 제출',
|
'Submit Relay': '릴레이 제출',
|
||||||
Homepage: '홈페이지'
|
Homepage: '홈페이지',
|
||||||
|
'Proof of Work (difficulty {{minPow}})': '작업 증명 (난이도 {{minPow}})'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -405,6 +405,7 @@ export default {
|
|||||||
Detailed: 'Szczegółowy',
|
Detailed: 'Szczegółowy',
|
||||||
Compact: 'Zwięzły',
|
Compact: 'Zwięzły',
|
||||||
'Submit Relay': 'Prześlij przekaźnik',
|
'Submit Relay': 'Prześlij przekaźnik',
|
||||||
Homepage: 'Strona główna'
|
Homepage: 'Strona główna',
|
||||||
|
'Proof of Work (difficulty {{minPow}})': 'Dowód pracy (trudność {{minPow}})'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -401,6 +401,7 @@ export default {
|
|||||||
Detailed: 'Detalhado',
|
Detailed: 'Detalhado',
|
||||||
Compact: 'Compacto',
|
Compact: 'Compacto',
|
||||||
'Submit Relay': 'Enviar Relay',
|
'Submit Relay': 'Enviar Relay',
|
||||||
Homepage: 'Página inicial'
|
Homepage: 'Página inicial',
|
||||||
|
'Proof of Work (difficulty {{minPow}})': 'Prova de Trabalho (dificuldade {{minPow}})'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -404,6 +404,7 @@ export default {
|
|||||||
Detailed: 'Detalhado',
|
Detailed: 'Detalhado',
|
||||||
Compact: 'Compacto',
|
Compact: 'Compacto',
|
||||||
'Submit Relay': 'Enviar Relay',
|
'Submit Relay': 'Enviar Relay',
|
||||||
Homepage: 'Página inicial'
|
Homepage: 'Página inicial',
|
||||||
|
'Proof of Work (difficulty {{minPow}})': 'Prova de Trabalho (dificuldade {{minPow}})'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -406,6 +406,7 @@ export default {
|
|||||||
Detailed: 'Подробный',
|
Detailed: 'Подробный',
|
||||||
Compact: 'Компактный',
|
Compact: 'Компактный',
|
||||||
'Submit Relay': 'Отправить релей',
|
'Submit Relay': 'Отправить релей',
|
||||||
Homepage: 'Домашняя страница'
|
Homepage: 'Домашняя страница',
|
||||||
|
'Proof of Work (difficulty {{minPow}})': 'Доказательство работы (сложность {{minPow}})'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -396,6 +396,7 @@ export default {
|
|||||||
Detailed: 'รายละเอียด',
|
Detailed: 'รายละเอียด',
|
||||||
Compact: 'กะทัดรัด',
|
Compact: 'กะทัดรัด',
|
||||||
'Submit Relay': 'ส่งรีเลย์',
|
'Submit Relay': 'ส่งรีเลย์',
|
||||||
Homepage: 'หน้าแรก'
|
Homepage: 'หน้าแรก',
|
||||||
|
'Proof of Work (difficulty {{minPow}})': 'หลักฐานการทำงาน (ความยาก {{minPow}})'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -394,6 +394,7 @@ export default {
|
|||||||
Detailed: '详细',
|
Detailed: '详细',
|
||||||
Compact: '紧凑',
|
Compact: '紧凑',
|
||||||
'Submit Relay': '提交服务器',
|
'Submit Relay': '提交服务器',
|
||||||
Homepage: '主页'
|
Homepage: '主页',
|
||||||
|
'Proof of Work (difficulty {{minPow}})': '工作量证明 (难度 {{minPow}})'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import {
|
|||||||
TPollCreateData,
|
TPollCreateData,
|
||||||
TRelaySet
|
TRelaySet
|
||||||
} from '@/types'
|
} from '@/types'
|
||||||
|
import { sha256 } from '@noble/hashes/sha2'
|
||||||
import dayjs from 'dayjs'
|
import dayjs from 'dayjs'
|
||||||
import { Event, kinds, nip19 } from 'nostr-tools'
|
import { Event, kinds, nip19 } from 'nostr-tools'
|
||||||
import {
|
import {
|
||||||
@@ -22,6 +23,39 @@ import {
|
|||||||
import { randomString } from './random'
|
import { randomString } from './random'
|
||||||
import { generateBech32IdFromETag, tagNameEquals } from './tag'
|
import { generateBech32IdFromETag, tagNameEquals } from './tag'
|
||||||
|
|
||||||
|
const draftEventCache: Map<string, string> = new Map()
|
||||||
|
|
||||||
|
export function deleteDraftEventCache(draftEvent: TDraftEvent) {
|
||||||
|
const key = generateDraftEventCacheKey(draftEvent)
|
||||||
|
draftEventCache.delete(key)
|
||||||
|
}
|
||||||
|
|
||||||
|
function setDraftEventCache(baseDraft: Omit<TDraftEvent, 'created_at'>): TDraftEvent {
|
||||||
|
const cacheKey = generateDraftEventCacheKey(baseDraft)
|
||||||
|
const cache = draftEventCache.get(cacheKey)
|
||||||
|
if (cache) {
|
||||||
|
return JSON.parse(cache)
|
||||||
|
}
|
||||||
|
const draftEvent = { ...baseDraft, created_at: dayjs().unix() }
|
||||||
|
draftEventCache.set(cacheKey, JSON.stringify(draftEvent))
|
||||||
|
|
||||||
|
return draftEvent
|
||||||
|
}
|
||||||
|
|
||||||
|
function generateDraftEventCacheKey(draft: Omit<TDraftEvent, 'created_at'>) {
|
||||||
|
const str = JSON.stringify({
|
||||||
|
content: draft.content,
|
||||||
|
kind: draft.kind,
|
||||||
|
tags: draft.tags
|
||||||
|
})
|
||||||
|
|
||||||
|
const encoder = new TextEncoder()
|
||||||
|
const data = encoder.encode(str)
|
||||||
|
const hashBuffer = sha256(data)
|
||||||
|
const hashArray = Array.from(new Uint8Array(hashBuffer))
|
||||||
|
return hashArray.map((b) => b.toString(16).padStart(2, '0')).join('')
|
||||||
|
}
|
||||||
|
|
||||||
// https://github.com/nostr-protocol/nips/blob/master/25.md
|
// https://github.com/nostr-protocol/nips/blob/master/25.md
|
||||||
export function createReactionDraftEvent(event: Event, emoji: TEmoji | string = '+'): TDraftEvent {
|
export function createReactionDraftEvent(event: Event, emoji: TEmoji | string = '+'): TDraftEvent {
|
||||||
const tags: string[][] = []
|
const tags: string[][] = []
|
||||||
@@ -68,7 +102,6 @@ export function createRepostDraftEvent(event: Event): TDraftEvent {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const shortTextNoteDraftEventCache: Map<string, TDraftEvent> = new Map()
|
|
||||||
export async function createShortTextNoteDraftEvent(
|
export async function createShortTextNoteDraftEvent(
|
||||||
content: string,
|
content: string,
|
||||||
mentions: string[],
|
mentions: string[],
|
||||||
@@ -125,15 +158,7 @@ export async function createShortTextNoteDraftEvent(
|
|||||||
content: transformedEmojisContent,
|
content: transformedEmojisContent,
|
||||||
tags
|
tags
|
||||||
}
|
}
|
||||||
const cacheKey = JSON.stringify(baseDraft)
|
return setDraftEventCache(baseDraft)
|
||||||
const cache = shortTextNoteDraftEventCache.get(cacheKey)
|
|
||||||
if (cache) {
|
|
||||||
return cache
|
|
||||||
}
|
|
||||||
const draftEvent = { ...baseDraft, created_at: dayjs().unix() }
|
|
||||||
shortTextNoteDraftEventCache.set(cacheKey, draftEvent)
|
|
||||||
|
|
||||||
return draftEvent
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://github.com/nostr-protocol/nips/blob/master/51.md
|
// https://github.com/nostr-protocol/nips/blob/master/51.md
|
||||||
@@ -150,7 +175,6 @@ export function createRelaySetDraftEvent(relaySet: Omit<TRelaySet, 'aTag'>): TDr
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const commentDraftEventCache: Map<string, TDraftEvent> = new Map()
|
|
||||||
export async function createCommentDraftEvent(
|
export async function createCommentDraftEvent(
|
||||||
content: string,
|
content: string,
|
||||||
parentEvent: Event,
|
parentEvent: Event,
|
||||||
@@ -228,15 +252,7 @@ export async function createCommentDraftEvent(
|
|||||||
content: transformedEmojisContent,
|
content: transformedEmojisContent,
|
||||||
tags
|
tags
|
||||||
}
|
}
|
||||||
const cacheKey = JSON.stringify(baseDraft)
|
return setDraftEventCache(baseDraft)
|
||||||
const cache = commentDraftEventCache.get(cacheKey)
|
|
||||||
if (cache) {
|
|
||||||
return cache
|
|
||||||
}
|
|
||||||
const draftEvent = { ...baseDraft, created_at: dayjs().unix() }
|
|
||||||
commentDraftEventCache.set(cacheKey, draftEvent)
|
|
||||||
|
|
||||||
return draftEvent
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function createRelayListDraftEvent(mailboxRelays: TMailboxRelay[]): TDraftEvent {
|
export function createRelayListDraftEvent(mailboxRelays: TMailboxRelay[]): TDraftEvent {
|
||||||
@@ -325,7 +341,6 @@ export function createBlossomServerListDraftEvent(servers: string[]): TDraftEven
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const pollDraftEventCache: Map<string, TDraftEvent> = new Map()
|
|
||||||
export async function createPollDraftEvent(
|
export async function createPollDraftEvent(
|
||||||
author: string,
|
author: string,
|
||||||
question: string,
|
question: string,
|
||||||
@@ -389,15 +404,7 @@ export async function createPollDraftEvent(
|
|||||||
kind: ExtendedKind.POLL,
|
kind: ExtendedKind.POLL,
|
||||||
tags
|
tags
|
||||||
}
|
}
|
||||||
const cacheKey = JSON.stringify(baseDraft)
|
return setDraftEventCache(baseDraft)
|
||||||
const cache = pollDraftEventCache.get(cacheKey)
|
|
||||||
if (cache) {
|
|
||||||
return cache
|
|
||||||
}
|
|
||||||
const draftEvent = { ...baseDraft, created_at: dayjs().unix() }
|
|
||||||
pollDraftEventCache.set(cacheKey, draftEvent)
|
|
||||||
|
|
||||||
return draftEvent
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function createPollResponseDraftEvent(
|
export function createPollResponseDraftEvent(
|
||||||
|
|||||||
@@ -2,11 +2,12 @@ import { EMBEDDED_MENTION_REGEX, ExtendedKind } from '@/constants'
|
|||||||
import client from '@/services/client.service'
|
import client from '@/services/client.service'
|
||||||
import { TImetaInfo } from '@/types'
|
import { TImetaInfo } from '@/types'
|
||||||
import { LRUCache } from 'lru-cache'
|
import { LRUCache } from 'lru-cache'
|
||||||
import { Event, kinds, nip19 } from 'nostr-tools'
|
import { Event, kinds, nip19, UnsignedEvent } from 'nostr-tools'
|
||||||
|
import { fastEventHash, getPow } from 'nostr-tools/nip13'
|
||||||
import {
|
import {
|
||||||
getImetaInfoFromImetaTag,
|
|
||||||
generateBech32IdFromATag,
|
generateBech32IdFromATag,
|
||||||
generateBech32IdFromETag,
|
generateBech32IdFromETag,
|
||||||
|
getImetaInfoFromImetaTag,
|
||||||
tagNameEquals
|
tagNameEquals
|
||||||
} from './tag'
|
} from './tag'
|
||||||
|
|
||||||
@@ -256,6 +257,47 @@ export function createFakeEvent(event: Partial<Event>): Event {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function minePow(
|
||||||
|
unsigned: UnsignedEvent,
|
||||||
|
difficulty: number
|
||||||
|
): Promise<Omit<Event, 'sig'>> {
|
||||||
|
let count = 0
|
||||||
|
|
||||||
|
const event = unsigned as Omit<Event, 'sig'>
|
||||||
|
const tag = ['nonce', count.toString(), difficulty.toString()]
|
||||||
|
|
||||||
|
event.tags.push(tag)
|
||||||
|
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
const mine = () => {
|
||||||
|
let iterations = 0
|
||||||
|
|
||||||
|
while (iterations < 1000) {
|
||||||
|
const now = Math.floor(new Date().getTime() / 1000)
|
||||||
|
|
||||||
|
if (now !== event.created_at) {
|
||||||
|
count = 0
|
||||||
|
event.created_at = now
|
||||||
|
}
|
||||||
|
|
||||||
|
tag[1] = (++count).toString()
|
||||||
|
event.id = fastEventHash(event)
|
||||||
|
|
||||||
|
if (getPow(event.id) >= difficulty) {
|
||||||
|
resolve(event)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
iterations++
|
||||||
|
}
|
||||||
|
|
||||||
|
setTimeout(mine, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
mine()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// Legacy compare function for sorting compatibility
|
// Legacy compare function for sorting compatibility
|
||||||
// If return 0, it means the two events are equal.
|
// If return 0, it means the two events are equal.
|
||||||
// If return a negative number, it means `b` should be retained, and `a` should be discarded.
|
// If return a negative number, it means `b` should be retained, and `a` should be discarded.
|
||||||
|
|||||||
@@ -7,7 +7,12 @@ import {
|
|||||||
createRelayListDraftEvent,
|
createRelayListDraftEvent,
|
||||||
createSeenNotificationsAtDraftEvent
|
createSeenNotificationsAtDraftEvent
|
||||||
} from '@/lib/draft-event'
|
} from '@/lib/draft-event'
|
||||||
import { getLatestEvent, getReplaceableEventIdentifier, isProtectedEvent } from '@/lib/event'
|
import {
|
||||||
|
getLatestEvent,
|
||||||
|
getReplaceableEventIdentifier,
|
||||||
|
isProtectedEvent,
|
||||||
|
minePow
|
||||||
|
} from '@/lib/event'
|
||||||
import { getProfileFromEvent, getRelayListFromEvent } from '@/lib/event-metadata'
|
import { getProfileFromEvent, getRelayListFromEvent } from '@/lib/event-metadata'
|
||||||
import { formatPubkey, pubkeyToNpub } from '@/lib/pubkey'
|
import { formatPubkey, pubkeyToNpub } from '@/lib/pubkey'
|
||||||
import client from '@/services/client.service'
|
import client from '@/services/client.service'
|
||||||
@@ -594,12 +599,22 @@ export function NostrProvider({ children }: { children: React.ReactNode }) {
|
|||||||
return event as VerifiedEvent
|
return event as VerifiedEvent
|
||||||
}
|
}
|
||||||
|
|
||||||
const publish = async (draftEvent: TDraftEvent, options: TPublishOptions = {}) => {
|
const publish = async (
|
||||||
|
draftEvent: TDraftEvent,
|
||||||
|
{ minPow = 0, ...options }: TPublishOptions = {}
|
||||||
|
) => {
|
||||||
if (!account || !signer || account.signerType === 'npub') {
|
if (!account || !signer || account.signerType === 'npub') {
|
||||||
throw new Error('You need to login first')
|
throw new Error('You need to login first')
|
||||||
}
|
}
|
||||||
|
|
||||||
const event = await signEvent(draftEvent)
|
const draft = JSON.parse(JSON.stringify(draftEvent)) as TDraftEvent
|
||||||
|
let event: VerifiedEvent
|
||||||
|
if (minPow > 0) {
|
||||||
|
const unsignedEvent = await minePow({ ...draft, pubkey: account.pubkey }, minPow)
|
||||||
|
event = await signEvent(unsignedEvent)
|
||||||
|
} else {
|
||||||
|
event = await signEvent(draft)
|
||||||
|
}
|
||||||
|
|
||||||
if (event.kind !== kinds.Application && event.pubkey !== account.pubkey) {
|
if (event.kind !== kinds.Application && event.pubkey !== account.pubkey) {
|
||||||
const eventAuthor = await client.fetchProfile(event.pubkey)
|
const eventAuthor = await client.fetchProfile(event.pubkey)
|
||||||
|
|||||||
1
src/types/index.d.ts
vendored
1
src/types/index.d.ts
vendored
@@ -121,6 +121,7 @@ export type TImetaInfo = {
|
|||||||
export type TPublishOptions = {
|
export type TPublishOptions = {
|
||||||
specifiedRelayUrls?: string[]
|
specifiedRelayUrls?: string[]
|
||||||
additionalRelayUrls?: string[]
|
additionalRelayUrls?: string[]
|
||||||
|
minPow?: number
|
||||||
}
|
}
|
||||||
|
|
||||||
export type TNoteListMode = 'posts' | 'postsAndReplies' | 'you'
|
export type TNoteListMode = 'posts' | 'postsAndReplies' | 'you'
|
||||||
|
|||||||
Reference in New Issue
Block a user