add event deletion
This commit is contained in:
parent
25c1f48a95
commit
cd0aeb46f7
@ -12,5 +12,6 @@ module.exports = {
|
||||
rules: {
|
||||
"require-await": "error",
|
||||
eqeqeq: "error",
|
||||
"object-shorthand": "warn",
|
||||
},
|
||||
}
|
||||
|
48
packages/nostr/src/event/deletion.ts
Normal file
48
packages/nostr/src/event/deletion.ts
Normal 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]
|
||||
})
|
||||
}
|
@ -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,
|
||||
|
Loading…
x
Reference in New Issue
Block a user