Set up thunks

This commit is contained in:
Jon Staab
2024-08-16 16:39:35 -07:00
parent 297e69af21
commit fafbf79a75
2 changed files with 65 additions and 29 deletions
+11 -23
View File
@@ -8,6 +8,7 @@ import {
editList,
makeList,
createList,
createEvent,
} from "@welshman/util"
import {pk, signer, repository, INDEXER_RELAYS} from "@app/base"
import {
@@ -17,8 +18,10 @@ import {
loadProfile,
loadFollows,
loadMutes,
getRelaySelectionsByPubkey,
loadRelaySelections,
publish,
makeThunk,
publishThunk,
ensurePlaintext,
} from "@app/state"
@@ -49,30 +52,15 @@ export const updateList = async (kind: number, modifyTags: ModifyTags) => {
const $pk = pk.get()!
const $signer = signer.get()!
const [prev] = repository.query([{kinds: [kind], authors: [$pk]}])
const userRelays = getRelaySelectionsByPubkey().get($pk)
const relays = [...INDEXER_RELAYS, ...getWriteRelayUrls(userRelays)]
// Preserve content instead of use encrypted tags because kind 3 content is used for
// relay selections in many places. Content isn't supported for mutes or relays so this is ok
const relays = [...INDEXER_RELAYS, ...getWriteRelayUrls(await loadRelaySelections($pk))]
const encrypt = (content: string) => $signer.nip44.encrypt($pk, content)
// Preserve content if we have it
const event = prev
? {...prev, tags: modifyTags(prev.tags)}
: createEvent(kind, {tags: modifyTags([])})
let encryptable
if (prev) {
const content = await ensurePlaintext(prev)
const list = readList(asDecryptedEvent(prev, {content}))
const publicTags = modifyTags(list.publicTags)
encryptable = editList({...list, publicTags})
} else {
const list = makeList({kind})
const publicTags = modifyTags(list.publicTags)
encryptable = createList({...list, publicTags})
}
const template = await encryptable.reconcile(encrypt)
const event = await $signer.sign({...template, created_at: now()})
await publish({event, relays})
publishThunk(makeThunk({event, relays}))
}
export const addGroupMemberships = (newTags: string[][]) =>