Get rid of domain module, allow app to override default event type

This commit is contained in:
Jon Staab
2024-08-13 15:45:20 -07:00
parent 5a63273b9d
commit 149c29472c
33 changed files with 201 additions and 285 deletions
+43
View File
@@ -0,0 +1,43 @@
import type {EventContent, CustomEvent} from './Events'
export type Encrypt = (x: string) => Promise<string>
export type DecryptedEvent = CustomEvent & {
plaintext: Partial<EventContent>
}
export const asDecryptedEvent = (event: CustomEvent, plaintext: Partial<EventContent>) =>
({...event, plaintext}) as DecryptedEvent
export class Encryptable<E extends Partial<EventContent>> {
constructor(readonly event: E, readonly updates: E) {}
async reconcile(encrypt: Encrypt) {
const encryptContent = () => {
if (!this.updates.content) return null
return encrypt(this.updates.content)
}
const encryptTags = () => {
if (!this.updates.tags) return null
return Promise.all(
this.updates.tags.map(async tag => {
tag[1] = await encrypt(tag[1])
return tag
})
)
}
const [content, tags] = await Promise.all([encryptContent(), encryptTags()])
// 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 || "",
}
}
}