add event deletion

This commit is contained in:
ennmichael 2023-04-01 14:17:58 +02:00
parent 25c1f48a95
commit cd0aeb46f7
No known key found for this signature in database
GPG Key ID: 6E6E183431A26AF7
3 changed files with 61 additions and 1 deletions

View File

@ -12,5 +12,6 @@ module.exports = {
rules: {
"require-await": "error",
eqeqeq: "error",
"object-shorthand": "warn",
},
}

View File

@ -0,0 +1,48 @@
import { EventId, EventKind, RawEvent, signEvent } from "."
import { NostrError } from "../common"
import { HexOrBechPrivateKey } from "../crypto"
/**
* A deletion event. Used for marking published events as deleted.
*
* Related NIPs: NIP-09.
*/
export interface Deletion extends RawEvent {
kind: EventKind.Deletion
/**
* The IDs of events to delete.
*/
getEvents(): EventId[]
}
/**
* Create a deletion event.
*/
export function createDeletion(
{ events, content }: { events: EventId[]; content?: string },
priv?: HexOrBechPrivateKey
): Promise<Deletion> {
return signEvent(
{
kind: EventKind.Deletion,
tags: events.map((id) => ["e", id]),
content: content ?? "",
getEvents,
},
priv
)
}
export function getEvents(this: Deletion): EventId[] {
return this.tags
.filter((tag) => tag[0] === "e")
.map((tag) => {
if (tag[1] !== undefined) {
throw new NostrError(
`invalid deletion event tag: ${JSON.stringify(tag)}`
)
}
return tag[1]
})
}

View File

@ -21,6 +21,7 @@ import {
getRecipient,
} from "./direct-message"
import { ContactList, getContacts } from "./contact-list"
import { Deletion, getEvents } from "./deletion"
// TODO Add remaining event types
@ -69,8 +70,9 @@ export interface Unknown extends RawEvent {
EventKind,
| EventKind.SetMetadata
| EventKind.TextNote
| EventKind.DirectMessage
| EventKind.ContactList
| EventKind.DirectMessage
| EventKind.Deletion
>
}
@ -79,6 +81,7 @@ export type Event =
| TextNote
| ContactList
| DirectMessage
| Deletion
| Unknown
/**
@ -189,6 +192,14 @@ export async function parseEvent(event: RawEvent): Promise<Event> {
}
}
if (event.kind === EventKind.Deletion) {
return {
...event,
kind: EventKind.Deletion,
getEvents,
}
}
return {
...event,
kind: event.kind,