Generic repost
This commit is contained in:
parent
97bded8f5b
commit
4188f2c596
|
@ -1,7 +1,7 @@
|
||||||
import { describe, test, expect } from 'bun:test'
|
import { describe, test, expect } from 'bun:test'
|
||||||
import { hexToBytes } from '@noble/hashes/utils'
|
import { hexToBytes } from '@noble/hashes/utils'
|
||||||
import { finalizeEvent, getPublicKey } from './pure.ts'
|
import { EventTemplate, finalizeEvent, getPublicKey } from './pure.ts'
|
||||||
import { Repost, ShortTextNote } from './kinds.ts'
|
import { GenericRepost, Repost, ShortTextNote, BadgeDefinition as BadgeDefinitionKind } from './kinds.ts'
|
||||||
import { finishRepostEvent, getRepostedEventPointer, getRepostedEvent } from './nip18.ts'
|
import { finishRepostEvent, getRepostedEventPointer, getRepostedEvent } from './nip18.ts'
|
||||||
import { buildEvent } from './test-helpers.ts'
|
import { buildEvent } from './test-helpers.ts'
|
||||||
|
|
||||||
|
@ -86,6 +86,51 @@ describe('finishRepostEvent + getRepostedEventPointer + getRepostedEvent', () =>
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('GenericRepost', () => {
|
||||||
|
const privateKey = hexToBytes('d217c1ff2f8a65c3e3a1740db3b9f58b8c848bb45e26d00ed4714e4a0f4ceecf')
|
||||||
|
const publicKey = getPublicKey(privateKey)
|
||||||
|
|
||||||
|
const eventTemplate: EventTemplate = {
|
||||||
|
content: '',
|
||||||
|
created_at: 1617932114,
|
||||||
|
kind: BadgeDefinitionKind,
|
||||||
|
tags: [
|
||||||
|
['d', 'badge-id'],
|
||||||
|
['name', 'Badge Name'],
|
||||||
|
['description', 'Badge Description'],
|
||||||
|
['image', 'https://example.com/badge.png', '1024x1024'],
|
||||||
|
['thumb', 'https://example.com/thumb.png', '100x100'],
|
||||||
|
['thumb', 'https://example.com/thumb2.png', '200x200'],
|
||||||
|
],
|
||||||
|
}
|
||||||
|
|
||||||
|
const repostedEvent = finalizeEvent(eventTemplate, privateKey)
|
||||||
|
test('should create a generic reposted event', () => {
|
||||||
|
const template = { created_at: 1617932115 }
|
||||||
|
const event = finishRepostEvent(template, repostedEvent, relayUrl, privateKey)
|
||||||
|
|
||||||
|
expect(event.kind).toEqual(GenericRepost)
|
||||||
|
expect(event.tags).toEqual([
|
||||||
|
['e', repostedEvent.id, relayUrl],
|
||||||
|
['p', repostedEvent.pubkey],
|
||||||
|
['k', '30009'],
|
||||||
|
])
|
||||||
|
expect(event.content).toEqual(JSON.stringify(repostedEvent))
|
||||||
|
expect(event.created_at).toEqual(template.created_at)
|
||||||
|
expect(event.pubkey).toEqual(publicKey)
|
||||||
|
|
||||||
|
const repostedEventPointer = getRepostedEventPointer(event)
|
||||||
|
|
||||||
|
expect(repostedEventPointer!.id).toEqual(repostedEvent.id)
|
||||||
|
expect(repostedEventPointer!.author).toEqual(repostedEvent.pubkey)
|
||||||
|
expect(repostedEventPointer!.relays).toEqual([relayUrl])
|
||||||
|
|
||||||
|
const repostedEventFromContent = getRepostedEvent(event)
|
||||||
|
|
||||||
|
expect(repostedEventFromContent).toEqual(repostedEvent)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
describe('getRepostedEventPointer', () => {
|
describe('getRepostedEventPointer', () => {
|
||||||
test('should parse an event with only an `e` tag', () => {
|
test('should parse an event with only an `e` tag', () => {
|
||||||
const event = buildEvent({
|
const event = buildEvent({
|
||||||
|
|
19
nip18.ts
19
nip18.ts
|
@ -1,6 +1,6 @@
|
||||||
import { Event, finalizeEvent, verifyEvent } from './pure.ts'
|
import { GenericRepost, Repost, ShortTextNote } from './kinds.ts'
|
||||||
import { Repost } from './kinds.ts'
|
|
||||||
import { EventPointer } from './nip19.ts'
|
import { EventPointer } from './nip19.ts'
|
||||||
|
import { Event, finalizeEvent, verifyEvent } from './pure.ts'
|
||||||
|
|
||||||
export type RepostEventTemplate = {
|
export type RepostEventTemplate = {
|
||||||
/**
|
/**
|
||||||
|
@ -25,10 +25,19 @@ export function finishRepostEvent(
|
||||||
relayUrl: string,
|
relayUrl: string,
|
||||||
privateKey: Uint8Array,
|
privateKey: Uint8Array,
|
||||||
): Event {
|
): Event {
|
||||||
|
let kind: Repost | GenericRepost
|
||||||
|
const tags = [...(t.tags ?? []), ['e', reposted.id, relayUrl], ['p', reposted.pubkey]]
|
||||||
|
if (reposted.kind === ShortTextNote) {
|
||||||
|
kind = Repost
|
||||||
|
} else {
|
||||||
|
kind = GenericRepost
|
||||||
|
tags.push(['k', String(reposted.kind)])
|
||||||
|
}
|
||||||
|
|
||||||
return finalizeEvent(
|
return finalizeEvent(
|
||||||
{
|
{
|
||||||
kind: Repost,
|
kind,
|
||||||
tags: [...(t.tags ?? []), ['e', reposted.id, relayUrl], ['p', reposted.pubkey]],
|
tags,
|
||||||
content: t.content === '' || reposted.tags?.find(tag => tag[0] === '-') ? '' : JSON.stringify(reposted),
|
content: t.content === '' || reposted.tags?.find(tag => tag[0] === '-') ? '' : JSON.stringify(reposted),
|
||||||
created_at: t.created_at,
|
created_at: t.created_at,
|
||||||
},
|
},
|
||||||
|
@ -37,7 +46,7 @@ export function finishRepostEvent(
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getRepostedEventPointer(event: Event): undefined | EventPointer {
|
export function getRepostedEventPointer(event: Event): undefined | EventPointer {
|
||||||
if (event.kind !== Repost) {
|
if (![Repost, GenericRepost].includes(event.kind)) {
|
||||||
return undefined
|
return undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue