turn Kind enum into a simple object with numbers.

This commit is contained in:
fiatjaf 2023-10-30 08:03:42 -03:00
parent 318e3f8c88
commit 842cba25f3
No known key found for this signature in database
GPG Key ID: BAD43C4BE5C1A3A1
7 changed files with 46 additions and 56 deletions

View File

@ -14,7 +14,7 @@ import { getPublicKey } from './keys.ts'
describe('Event', () => { describe('Event', () => {
describe('getBlankEvent', () => { describe('getBlankEvent', () => {
it('should return a blank event object', () => { it('should return a blank event object', () => {
expect(getBlankEvent()).toEqual({ expect(getBlankEvent(255)).toEqual({
kind: 255, kind: 255,
content: '', content: '',
tags: [], tags: [],

View File

@ -8,35 +8,33 @@ import { utf8Encoder } from './utils.ts'
/** Designates a verified event signature. */ /** Designates a verified event signature. */
export const verifiedSymbol = Symbol('verified') export const verifiedSymbol = Symbol('verified')
/** @deprecated Use numbers instead. */ export const Kind = {
/* eslint-disable no-unused-vars */ Metadata: 0,
export enum Kind { Text: 1,
Metadata = 0, RecommendRelay: 2,
Text = 1, Contacts: 3,
RecommendRelay = 2, EncryptedDirectMessage: 4,
Contacts = 3, EventDeletion: 5,
EncryptedDirectMessage = 4, Repost: 6,
EventDeletion = 5, Reaction: 7,
Repost = 6, BadgeAward: 8,
Reaction = 7, ChannelCreation: 40,
BadgeAward = 8, ChannelMetadata: 41,
ChannelCreation = 40, ChannelMessage: 42,
ChannelMetadata = 41, ChannelHideMessage: 43,
ChannelMessage = 42, ChannelMuteUser: 44,
ChannelHideMessage = 43, Blank: 255,
ChannelMuteUser = 44, Report: 1984,
Blank = 255, ZapRequest: 9734,
Report = 1984, Zap: 9735,
ZapRequest = 9734, RelayList: 10002,
Zap = 9735, ClientAuth: 22242,
RelayList = 10002, NwcRequest: 23194,
ClientAuth = 22242, HttpAuth: 27235,
NwcRequest = 23194, ProfileBadge: 30008,
HttpAuth = 27235, BadgeDefinition: 30009,
ProfileBadge = 30008, Article: 30023,
BadgeDefinition = 30009, FileMetadata: 1063,
Article = 30023,
FileMetadata = 1063,
} }
export interface Event<K extends number = number> { export interface Event<K extends number = number> {
@ -61,9 +59,7 @@ export interface VerifiedEvent<K extends number = number> extends Event<K> {
[verifiedSymbol]: true [verifiedSymbol]: true
} }
export function getBlankEvent(): EventTemplate<Kind.Blank> export function getBlankEvent(kind: number = 255): EventTemplate {
export function getBlankEvent<K extends number>(kind: K): EventTemplate<K>
export function getBlankEvent<K>(kind: K | Kind.Blank = Kind.Blank) {
return { return {
kind, kind,
content: '', content: '',

View File

@ -1,4 +1,5 @@
import { getPow, minePow } from './nip13.ts' import { getPow, minePow } from './nip13.ts'
import { Kind } from './event.ts'
test('identifies proof-of-work difficulty', async () => { test('identifies proof-of-work difficulty', async () => {
const id = '000006d8c378af1779d2feebc7603a125d99eca0ccf1085959b307f64e5dd358' const id = '000006d8c378af1779d2feebc7603a125d99eca0ccf1085959b307f64e5dd358'
@ -11,7 +12,7 @@ test('mines POW for an event', async () => {
const event = minePow( const event = minePow(
{ {
kind: 1, kind: Kind.Text,
tags: [], tags: [],
content: 'Hello, world!', content: 'Hello, world!',
created_at: 0, created_at: 0,

View File

@ -23,7 +23,7 @@ export function finishRepostEvent(
reposted: Event<number>, reposted: Event<number>,
relayUrl: string, relayUrl: string,
privateKey: string, privateKey: string,
): Event<Kind.Repost> { ): Event {
return finishEvent( return finishEvent(
{ {
kind: Kind.Repost, kind: Kind.Repost,

View File

@ -16,11 +16,7 @@ export type ReactionEventTemplate = {
created_at: number created_at: number
} }
export function finishReactionEvent( export function finishReactionEvent(t: ReactionEventTemplate, reacted: Event<number>, privateKey: string): Event {
t: ReactionEventTemplate,
reacted: Event<number>,
privateKey: string,
): Event<Kind.Reaction> {
const inheritedTags = reacted.tags.filter(tag => tag.length >= 2 && (tag[0] === 'e' || tag[0] === 'p')) const inheritedTags = reacted.tags.filter(tag => tag.length >= 2 && (tag[0] === 'e' || tag[0] === 'p'))
return finishEvent( return finishEvent(

View File

@ -44,10 +44,7 @@ export interface ChannelMuteUserEventTemplate {
tags?: string[][] tags?: string[][]
} }
export const channelCreateEvent = ( export const channelCreateEvent = (t: ChannelCreateEventTemplate, privateKey: string): Event | undefined => {
t: ChannelCreateEventTemplate,
privateKey: string,
): Event<Kind.ChannelCreation> | undefined => {
let content: string let content: string
if (typeof t.content === 'object') { if (typeof t.content === 'object') {
content = JSON.stringify(t.content) content = JSON.stringify(t.content)
@ -71,7 +68,7 @@ export const channelCreateEvent = (
export const channelMetadataEvent = ( export const channelMetadataEvent = (
t: ChannelMetadataEventTemplate, t: ChannelMetadataEventTemplate,
privateKey: string, privateKey: string,
): Event<Kind.ChannelMetadata> | undefined => { ): Event | undefined => {
let content: string let content: string
if (typeof t.content === 'object') { if (typeof t.content === 'object') {
content = JSON.stringify(t.content) content = JSON.stringify(t.content)
@ -92,7 +89,7 @@ export const channelMetadataEvent = (
) )
} }
export const channelMessageEvent = (t: ChannelMessageEventTemplate, privateKey: string): Event<Kind.ChannelMessage> => { export const channelMessageEvent = (t: ChannelMessageEventTemplate, privateKey: string): Event => {
const tags = [['e', t.channel_create_event_id, t.relay_url, 'root']] const tags = [['e', t.channel_create_event_id, t.relay_url, 'root']]
if (t.reply_to_channel_message_event_id) { if (t.reply_to_channel_message_event_id) {
@ -114,7 +111,7 @@ export const channelMessageEvent = (t: ChannelMessageEventTemplate, privateKey:
export const channelHideMessageEvent = ( export const channelHideMessageEvent = (
t: ChannelHideMessageEventTemplate, t: ChannelHideMessageEventTemplate,
privateKey: string, privateKey: string,
): Event<Kind.ChannelHideMessage> | undefined => { ): Event | undefined => {
let content: string let content: string
if (typeof t.content === 'object') { if (typeof t.content === 'object') {
content = JSON.stringify(t.content) content = JSON.stringify(t.content)
@ -138,7 +135,7 @@ export const channelHideMessageEvent = (
export const channelMuteUserEvent = ( export const channelMuteUserEvent = (
t: ChannelMuteUserEventTemplate, t: ChannelMuteUserEventTemplate,
privateKey: string, privateKey: string,
): Event<Kind.ChannelMuteUser> | undefined => { ): Event | undefined => {
let content: string let content: string
if (typeof t.content === 'object') { if (typeof t.content === 'object') {
content = JSON.stringify(t.content) content = JSON.stringify(t.content)

View File

@ -13,7 +13,7 @@ export function useFetchImplementation(fetchImplementation: any) {
_fetch = fetchImplementation _fetch = fetchImplementation
} }
export async function getZapEndpoint(metadata: Event<Kind.Metadata>): Promise<null | string> { export async function getZapEndpoint(metadata: Event): Promise<null | string> {
try { try {
let lnurl: string = '' let lnurl: string = ''
let { lud06, lud16 } = JSON.parse(metadata.content) let { lud06, lud16 } = JSON.parse(metadata.content)
@ -53,12 +53,12 @@ export function makeZapRequest({
amount: number amount: number
comment: string comment: string
relays: string[] relays: string[]
}): EventTemplate<Kind.ZapRequest> { }): EventTemplate {
if (!amount) throw new Error('amount not given') if (!amount) throw new Error('amount not given')
if (!profile) throw new Error('profile not given') if (!profile) throw new Error('profile not given')
let zr: EventTemplate<Kind.ZapRequest> = { let zr: EventTemplate = {
kind: 9734, kind: Kind.ZapRequest,
created_at: Math.round(Date.now() / 1000), created_at: Math.round(Date.now() / 1000),
content: comment, content: comment,
tags: [ tags: [
@ -111,12 +111,12 @@ export function makeZapReceipt({
preimage?: string preimage?: string
bolt11: string bolt11: string
paidAt: Date paidAt: Date
}): EventTemplate<Kind.Zap> { }): EventTemplate {
let zr: Event<Kind.ZapRequest> = JSON.parse(zapRequest) let zr: Event = JSON.parse(zapRequest)
let tagsFromZapRequest = zr.tags.filter(([t]) => t === 'e' || t === 'p' || t === 'a') let tagsFromZapRequest = zr.tags.filter(([t]) => t === 'e' || t === 'p' || t === 'a')
let zap: EventTemplate<Kind.Zap> = { let zap: EventTemplate = {
kind: 9735, kind: Kind.Zap,
created_at: Math.round(paidAt.getTime() / 1000), created_at: Math.round(paidAt.getTime() / 1000),
content: '', content: '',
tags: [...tagsFromZapRequest, ['bolt11', bolt11], ['description', zapRequest]], tags: [...tagsFromZapRequest, ['bolt11', bolt11], ['description', zapRequest]],