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
+5 -17
View File
@@ -1,21 +1,9 @@
import type {EventContent, TrustedEvent} from './Events'
import type {EventContent, TrustedEvent, EventTemplate} from './Events'
export type Encrypt = (x: string) => Promise<string>
export type EncryptableParams = {
kind: number,
tags?: string[][]
content?: string
}
export type EncryptableUpdates = Partial<EventContent>
export type EncryptableResult = {
kind: number,
tags: string[][]
content: string
}
export type DecryptedEvent = TrustedEvent & {
plaintext: EncryptableUpdates
}
@@ -26,7 +14,7 @@ export const asDecryptedEvent = (event: TrustedEvent, plaintext: EncryptableUpda
/**
* Represents an encryptable event with optional updates.
*/
export class Encryptable {
export class Encryptable<T extends EventTemplate> {
/**
* Creates an instance of Encryptable.
* @param event - An EventTemplate with optional tags and content.
@@ -39,14 +27,14 @@ export class Encryptable {
* const eventTemplate = await encryptable.reconcile(myEncryptFunction)
* ```
*/
constructor(readonly event: EncryptableParams, readonly updates: EncryptableUpdates) {}
constructor(readonly event: Partial<T>, readonly updates: EncryptableUpdates) {}
/**
* Encrypts plaintext updates and merges them into the event template.
* @param encrypt - The encryption function to be used.
* @returns A promise that resolves to the reconciled and encrypted event.
*/
async reconcile(encrypt: Encrypt): Promise<EncryptableResult> {
async reconcile(encrypt: Encrypt) {
const encryptContent = () => {
if (!this.updates.content) return null
@@ -72,6 +60,6 @@ export class Encryptable {
...this.event,
tags: tags || this.event.tags || [],
content: content || this.event.content || "",
}
} as T
}
}
+8 -5
View File
@@ -1,4 +1,4 @@
import {parseJson, append, nthNe, nthEq} from "@welshman/lib"
import {parseJson, append, nthEq} from "@welshman/lib"
import {Address} from "./Address"
import {uniqTags} from "./Tags"
import {isShareableRelayUrl} from "./Relay"
@@ -44,22 +44,25 @@ export const readList = (event: DecryptedEvent): PublishedList => {
export const getListTags = (list: List | undefined) =>
[...list?.publicTags || [], ...list?.privateTags || []]
export const removeFromList = (list: List, value: string) => {
export const removeFromListByPredicate = (list: List, pred: (t: string[]) => boolean) => {
const plaintext: EncryptableUpdates = {}
const template = {
kind: list.kind,
content: list.event?.content || "",
tags: list.publicTags.filter(nthNe(1, value)),
tags: list.publicTags.filter(t => !pred(t)),
}
// Avoid redundant encrypt calls if possible
if (list.privateTags.some(nthEq(1, value))) {
plaintext.content = JSON.stringify(list.privateTags.filter(nthNe(1, value)))
if (list.privateTags.some(t => pred(t))) {
plaintext.content = JSON.stringify(list.privateTags.filter(t => !pred(t)))
}
return new Encryptable(template, plaintext)
}
export const removeFromList = (list: List, value: string) =>
removeFromListByPredicate(list, nthEq(1, value))
export const addToListPublicly = (list: List, tag: string[]) => {
const template = {
kind: list.kind,