Files
welshman/packages/app/src/mutes.ts
T
2024-09-02 15:57:40 -07:00

38 lines
1.1 KiB
TypeScript

import {MUTES, asDecryptedEvent, readList} from '@welshman/util'
import {type TrustedEvent, type PublishedList} from '@welshman/util'
import {type SubscribeRequest} from "@welshman/net"
import {deriveEventsMapped, withGetter} from '@welshman/store'
import {repository, load} from './core'
import {collection} from './collection'
import {ensurePlaintext} from './plaintext'
import {getHintsForPubkey} from './relaySelections'
export const mutes = withGetter(
deriveEventsMapped<PublishedList>(repository, {
filters: [{kinds: [MUTES]}],
itemToEvent: item => item.event,
eventToItem: async (event: TrustedEvent) =>
readList(
asDecryptedEvent(event, {
content: await ensurePlaintext(event),
}),
),
})
)
export const {
indexStore: mutesByPubkey,
deriveItem: deriveMutes,
loadItem: loadMutes,
} = collection({
name: "mutes",
store: mutes,
getKey: mute => mute.event.pubkey,
load: async (pubkey: string, request: Partial<SubscribeRequest> = {}) =>
load({
...request,
filters: [{kinds: [MUTES], authors: [pubkey]}],
relays: await getHintsForPubkey(pubkey, request.relays || []),
}),
})