|
|
|
@ -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( |
|
|
|
|