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('getBlankEvent', () => {
it('should return a blank event object', () => {
expect(getBlankEvent()).toEqual({
expect(getBlankEvent(255)).toEqual({
kind: 255,
content: '',
tags: [],

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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