Add some common commands

This commit is contained in:
Jon Staab
2024-10-08 15:24:08 -07:00
parent a4085af9e3
commit cabeba4533
6 changed files with 70 additions and 31 deletions
+35
View File
@@ -0,0 +1,35 @@
import {get} from 'svelte/store'
import {ctx} from '@welshman/lib'
import {addToListPublicly, removeFromList, makeList, FOLLOWS, MUTES} from '@welshman/util'
import {userFollows, userMutes} from './user'
import {nip44EncryptToSelf} from './session'
import {publishThunk} from './thunk'
export const unfollow = async (value: string) => {
const list = get(userFollows) || makeList({kind: FOLLOWS})
const event = await removeFromList(list, value).reconcile(nip44EncryptToSelf)
return publishThunk({event, relays: ctx.app.router.WriteRelays().getUrls()})
}
export const follow = async (tag: string[]) => {
const list = get(userFollows) || makeList({kind: FOLLOWS})
const event = await addToListPublicly(list, tag).reconcile(nip44EncryptToSelf)
return publishThunk({event, relays: ctx.app.router.WriteRelays().getUrls()})
}
export const unmute = async (value: string) => {
const list = get(userMutes) || makeList({kind: MUTES})
const event = await removeFromList(list, value).reconcile(nip44EncryptToSelf)
return publishThunk({event, relays: ctx.app.router.WriteRelays().getUrls()})
}
export const mute = async (tag: string[]) => {
const list = get(userMutes) || makeList({kind: MUTES})
const event = await addToListPublicly(list, tag).reconcile(nip44EncryptToSelf)
return publishThunk({event, relays: ctx.app.router.WriteRelays().getUrls()})
}
+1
View File
@@ -1,6 +1,7 @@
export * from './context'
export * from './core'
export * from './collection'
export * from './commands'
export * from './freshness'
export * from './follows'
export * from './handles'
+11
View File
@@ -82,3 +82,14 @@ export const onAuth = async (url: string, challenge: string) => {
return event
}
export const nip44EncryptToSelf = (payload: string) => {
const $pubkey = pubkey.get()
const $signer = signer.get()
if (!$signer) {
throw new Error("Unable to encrypt to self without valid signer")
}
return $signer.nip44.encrypt($pubkey!, payload)
}
+10 -9
View File
@@ -20,12 +20,9 @@ export type PublishStatusDataByUrlById = Record<string, PublishStatusDataByUrl>
export const publishStatusData = writable<PublishStatusDataByUrlById>({})
export type Thunk = {
export type ThunkWithResolve = {
event: TrustedEvent
relays: string[]
}
export type ThunkWithResolve = Thunk & {
resolve: (data: PublishStatusDataByUrl) => void
}
@@ -103,13 +100,17 @@ export const prepEvent = (event: ThunkEvent) => {
return event as TrustedEvent
}
export const makeThunk = ({event, relays}: {event: ThunkEvent, relays: string[]}) =>
({event, relays})
export type ThunkParams = {
event: ThunkEvent
relays: string[]
}
export const publishThunk = ({event, relays}: Thunk) =>
export const makeThunk = (params: ThunkParams) => params
export const publishThunk = (params: ThunkParams) =>
new Promise<PublishStatusDataByUrl>(resolve => {
event = prepEvent(event)
const event = prepEvent(params.event)
thunkWorker.push({event, relays, resolve})
thunkWorker.push({...params, event, resolve})
repository.publish(event)
})