Remove generics for event type

This commit is contained in:
Jon Staab
2024-08-13 09:30:55 -07:00
parent 0576c3c0e0
commit 5a63273b9d
18 changed files with 154 additions and 136 deletions
+8 -8
View File
@@ -1,20 +1,20 @@
import {fromPairs, parseJson} from "@welshman/lib"
import {getAddress, Tags} from "@welshman/util"
import type {TrustedEvent} from "@welshman/util"
import type {ExtensibleTrustedEvent} from "@welshman/util"
export type Handler<E extends TrustedEvent> = {
export type Handler = {
kind: number
name: string
about: string
image: string
identifier: string
event: E
event: ExtensibleTrustedEvent
website?: string
lud16?: string
nip05?: string
}
export const readHandlers = <E extends TrustedEvent>(event: E) => {
export const readHandlers = (event: ExtensibleTrustedEvent) => {
const {d: identifier} = fromPairs(event.tags)
const meta = parseJson(event.content)
const normalizedMeta = {
@@ -35,14 +35,14 @@ export const readHandlers = <E extends TrustedEvent>(event: E) => {
.whereKey("k")
.values()
.valueOf()
.map(kind => ({...normalizedMeta, kind: parseInt(kind), identifier, event})) as Handler<E>[]
.map(kind => ({...normalizedMeta, kind: parseInt(kind), identifier, event})) as Handler[]
}
export const getHandlerKey = <E extends TrustedEvent>(handler: Handler<E>) => `${handler.kind}:${getAddress(handler.event)}`
export const getHandlerKey = (handler: Handler) => `${handler.kind}:${getAddress(handler.event)}`
export const displayHandler = <E extends TrustedEvent>(handler?: Handler<E>, fallback = "") => handler?.name || fallback
export const displayHandler = (handler?: Handler, fallback = "") => handler?.name || fallback
export const getHandlerAddress = <E extends TrustedEvent>(event: E) => {
export const getHandlerAddress = (event: ExtensibleTrustedEvent) => {
const tags = Tags.fromEvent(event).whereKey("a")
const tag = tags.filter(t => t.last() === "web").first() || tags.first()
+9 -9
View File
@@ -1,22 +1,22 @@
import {parseJson} from "@welshman/lib"
import {Address, isShareableRelayUrl, TrustedEvent} from "@welshman/util"
import {Address, isShareableRelayUrl} from "@welshman/util"
import {Encryptable, DecryptedEvent} from "./util"
export type ListParams = {
kind: number
}
export type List<E extends TrustedEvent> = ListParams & {
export type List = ListParams & {
publicTags: string[][]
privateTags: string[][]
event?: DecryptedEvent<E>
event?: DecryptedEvent
}
export type PublishedList<E extends TrustedEvent> = Omit<List<E>, "event"> & {
event: DecryptedEvent<E>
export type PublishedList = Omit<List, "event"> & {
event: DecryptedEvent
}
export const makeList = <E extends TrustedEvent>(list: ListParams & Partial<List<E>>): List<E> =>
export const makeList = (list: ListParams & Partial<List>): List =>
({publicTags: [], privateTags: [], ...list})
const isValidTag = (tag: string[]) => {
@@ -30,7 +30,7 @@ const isValidTag = (tag: string[]) => {
return true
}
export const readList = <E extends TrustedEvent>(event: DecryptedEvent<E>): PublishedList<E> => {
export const readList = (event: DecryptedEvent): PublishedList => {
const getTags = (tags: string[][]) => (Array.isArray(tags) ? tags.filter(isValidTag) : [])
const privateTags = getTags(parseJson(event.plaintext?.content))
const publicTags = getTags(event.tags)
@@ -38,8 +38,8 @@ export const readList = <E extends TrustedEvent>(event: DecryptedEvent<E>): Publ
return {event, kind: event.kind, publicTags, privateTags}
}
export const createList = <E extends TrustedEvent>({kind, publicTags = [], privateTags = []}: List<E>) =>
export const createList = ({kind, publicTags = [], privateTags = []}: List) =>
new Encryptable({kind, tags: publicTags}, {content: JSON.stringify(privateTags)})
export const editList = <E extends TrustedEvent>({kind, publicTags = [], privateTags = []}: PublishedList<E>) =>
export const editList = ({kind, publicTags = [], privateTags = []}: PublishedList) =>
new Encryptable({kind, tags: publicTags}, {content: JSON.stringify(privateTags)})
+13 -13
View File
@@ -1,8 +1,8 @@
import {nip19} from "nostr-tools"
import {ellipsize, parseJson} from "@welshman/lib"
import {PROFILE, TrustedEvent} from "@welshman/util"
import {PROFILE, ExtensibleTrustedEvent} from "@welshman/util"
export type Profile<E extends TrustedEvent> = {
export type Profile = {
name?: string
nip05?: string
lud06?: string
@@ -12,17 +12,17 @@ export type Profile<E extends TrustedEvent> = {
picture?: string
website?: string
display_name?: string
event?: E
event?: ExtensibleTrustedEvent
}
export type PublishedProfile<E extends TrustedEvent> = Omit<Profile<E>, "event"> & {
event: E
export type PublishedProfile = Omit<Profile, "event"> & {
event: ExtensibleTrustedEvent
}
export const isPublishedProfile = <E extends TrustedEvent>(profile: Profile<E>): profile is PublishedProfile<E> =>
export const isPublishedProfile = (profile: Profile): profile is PublishedProfile =>
Boolean(profile.event)
export const makeProfile = <E extends TrustedEvent>(profile: Partial<Profile<E>> = {}): Profile<E> => ({
export const makeProfile = (profile: Partial<Profile> = {}): Profile => ({
name: "",
nip05: "",
lud06: "",
@@ -35,18 +35,18 @@ export const makeProfile = <E extends TrustedEvent>(profile: Partial<Profile<E>>
...profile,
})
export const readProfile = <E extends TrustedEvent>(event: E) => {
export const readProfile = (event: ExtensibleTrustedEvent) => {
const profile = parseJson(event.content) || {}
return {...profile, event} as PublishedProfile<E>
return {...profile, event} as PublishedProfile
}
export const createProfile = <E extends TrustedEvent>({event, ...profile}: Profile<E>) => ({
export const createProfile = ({event, ...profile}: Profile) => ({
kind: PROFILE,
content: JSON.stringify(profile),
})
export const editProfile = <E extends TrustedEvent>({event, ...profile}: PublishedProfile<E>) => ({
export const editProfile = ({event, ...profile}: PublishedProfile) => ({
kind: PROFILE,
content: JSON.stringify(profile),
tags: event.tags,
@@ -58,7 +58,7 @@ export const displayPubkey = (pubkey: string) => {
return d.slice(0, 8) + "…" + d.slice(-5)
}
export const displayProfile = <E extends TrustedEvent>(profile?: Profile<E>, fallback = "") => {
export const displayProfile = (profile?: Profile, fallback = "") => {
const {display_name, name, event} = profile || {}
if (name) return ellipsize(name, 60)
@@ -68,4 +68,4 @@ export const displayProfile = <E extends TrustedEvent>(profile?: Profile<E>, fal
return fallback
}
export const profileHasName = <E extends TrustedEvent>(profile?: Profile<E>) => Boolean(profile?.name || profile?.display_name)
export const profileHasName = (profile?: Profile) => Boolean(profile?.name || profile?.display_name)
+9 -14
View File
@@ -1,21 +1,16 @@
import type {TrustedEvent} from "@welshman/util"
import type {EventContent, ExtensibleTrustedEvent} from "@welshman/util"
export type Encrypt = (x: string) => Promise<string>
export type EventContent = {
content?: string
tags?: string[][]
export type DecryptedEvent = ExtensibleTrustedEvent & {
plaintext: Partial<EventContent>
}
export type DecryptedEvent<E extends TrustedEvent> = E & {
plaintext: EventContent
}
export const asDecryptedEvent = (event: ExtensibleTrustedEvent, plaintext: Partial<EventContent>) =>
({...event, plaintext}) as DecryptedEvent
export const asDecryptedEvent = <E extends TrustedEvent>(event: E, plaintext: EventContent) =>
({...event, plaintext}) as DecryptedEvent<E>
export class Encryptable<E extends TrustedEvent> {
constructor(readonly event: Partial<E>, readonly updates: EventContent) {}
export class Encryptable<E extends Partial<EventContent>> {
constructor(readonly event: E, readonly updates: E) {}
async reconcile(encrypt: Encrypt) {
const encryptContent = () => {
@@ -41,8 +36,8 @@ export class Encryptable<E extends TrustedEvent> {
// Updates are optional. If not provided, fall back to the event's content and tags.
return {
...this.event,
tags: tags || this.event.tags,
content: content || this.event.content,
tags: tags || this.event.tags || [],
content: content || this.event.content || "",
}
}
}