-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnip-09.ts
274 lines (239 loc) · 6.68 KB
/
nip-09.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/**
* @file NIP-09: Event Deletion
* @module nips/nip-09
* @see https://github.com/nostr-protocol/nips/blob/master/09.md
*/
import type { NostrEvent, NostrSubscriptionEvent } from '../types/events.js';
/**
* Represents the result of a deletion operation
* @interface DeletionResult
* @property {boolean} success - Whether the deletion operation was successful
* @property {string} [error] - Error message if the operation failed
* @property {string[]} [deletedIds] - Array of successfully deleted event IDs
*/
interface DeletionResult {
success: boolean;
error?: string;
deletedIds?: string[];
}
/**
* Event deletion kind
*/
export const EVENT_DELETION_KIND = 5;
/**
* Creates a deletion event
*/
export function createDeletionEvent(
eventIds: string[],
reason?: string
): Omit<NostrEvent, 'id' | 'sig'> {
return {
kind: EVENT_DELETION_KIND,
created_at: Math.floor(Date.now() / 1000),
tags: eventIds.map(id => ['e', id]),
content: reason || '',
pubkey: '', // To be filled by the caller
};
}
/**
* Validates a deletion event
*/
export async function validateDeletionEvent(
event: NostrEvent,
_logger: any
): Promise<boolean> {
try {
// Must be kind 5
if (event.kind !== EVENT_DELETION_KIND) {
return false;
}
// Must have at least one e tag
const eventIds = event.tags
.filter((tag: string[]): tag is [string, string] =>
Array.isArray(tag) && tag[0] === 'e' && typeof tag[1] === 'string')
.map(tag => tag[1]);
if (eventIds.length === 0) {
return false;
}
// Validate each event ID format
const validHexString = /^[0-9a-f]{64}$/;
const invalidIds = eventIds.filter(id => !validHexString.test(id));
if (invalidIds.length > 0) {
return false;
}
return true;
} catch (error: unknown) {
return false;
}
}
/**
* Processes a deletion event
*/
export async function processDeletionEvent(
event: NostrEvent,
_logger: any,
deleteEvent: (id: string) => Promise<boolean>
): Promise<DeletionResult> {
try {
if (!validateDeletionEvent(event, _logger)) {
return {
success: false,
error: 'Invalid deletion event'
};
}
const eventIds = event.tags
.filter((tag): tag is [string, string] => tag[0] === 'e')
.map(tag => tag[1]);
const deletedIds: string[] = [];
const failedIds: string[] = [];
for (const eventId of eventIds) {
try {
const success = await deleteEvent(eventId);
if (success) {
deletedIds.push(eventId);
} else {
failedIds.push(eventId);
}
} catch (error: unknown) {
failedIds.push(eventId);
}
}
if (failedIds.length > 0) {
return {
success: false,
error: `Failed to delete events: ${failedIds.join(', ')}`,
deletedIds
};
}
return {
success: true,
deletedIds
};
} catch (error: unknown) {
return {
success: false,
error: 'Unknown error'
};
}
}
/**
* Event deletion manager interface
*/
export interface EventDeletionManager {
/**
* Processes a deletion event
* @param message - Deletion message
* @returns {Promise<string[]>} Deleted event IDs
*/
processDeletion(message: NostrEvent): Promise<string[]>;
/**
* Checks if an event has been deleted
* @param eventId - Event ID to check
* @returns {boolean} True if event is deleted
*/
isDeleted(eventId: string): boolean;
/**
* Gets deletion reason for an event
* @param eventId - Event ID
* @returns {string | undefined} Deletion reason if available
*/
getDeletionReason(eventId: string): string | undefined;
/**
* Gets all deleted events
* @returns {Map<string, string>} Map of event IDs to deletion reasons
*/
getDeletedEvents(): Map<string, string>;
}
/**
* Creates an event deletion manager
* @param _logger - Logger instance
* @returns {EventDeletionManager} Deletion manager
*/
export function createEventDeletionManager(
_logger: any
): EventDeletionManager {
// Map of deleted event IDs to deletion reasons
const deletedEvents = new Map<string, string>();
// Map of deleted event IDs to deletion event IDs
const deletionEvents = new Map<string, string>();
return {
async processDeletion(message: NostrEvent): Promise<string[]> {
try {
if (!validateDeletionEvent(message, _logger)) {
throw new Error('Invalid deletion event');
}
const eventIds = message.tags
.filter(tag => tag[0] === 'e')
.map(tag => tag[1]);
const { content: reason, id: deletionId } = message;
// Process each event ID
eventIds.forEach(eventId => {
// Only delete if not already deleted or if this deletion is newer
const existingDeletionId = deletionEvents.get(eventId);
if (!existingDeletionId || deletionId > existingDeletionId) {
deletedEvents.set(eventId, reason);
deletionEvents.set(eventId, deletionId);
}
});
return eventIds;
} catch (error) {
return [];
}
},
isDeleted(eventId: string): boolean {
return deletedEvents.has(eventId);
},
getDeletionReason(eventId: string): string | undefined {
return deletedEvents.get(eventId);
},
getDeletedEvents(): Map<string, string> {
return new Map(deletedEvents);
}
};
}
/**
* Event deletion subscription manager interface
*/
export interface DeletionSubscriptionManager {
/**
* Creates a subscription for deletion events
* @param eventIds - Event IDs to monitor
* @returns {NostrSubscriptionEvent} Subscription message
*/
subscribeToDeletions(eventIds: string[]): NostrSubscriptionEvent;
/**
* Creates a subscription for all deletions by a user
* @param pubkey - Public key of user
* @returns {NostrSubscriptionEvent} Subscription message
*/
subscribeToUserDeletions(pubkey: string): NostrSubscriptionEvent;
}
/**
* Creates a deletion subscription manager
* @param _logger - Logger instance
* @returns {DeletionSubscriptionManager} Subscription manager
*/
export function createDeletionSubscriptionManager(
_logger: any
): DeletionSubscriptionManager {
return {
subscribeToDeletions(eventIds: string[]): NostrSubscriptionEvent {
return {
subscriptionId: `deletion_${eventIds.join('_')}`,
filters: [{
kinds: [EVENT_DELETION_KIND],
'#e': eventIds
}]
};
},
subscribeToUserDeletions(pubkey: string): NostrSubscriptionEvent {
return {
subscriptionId: `user_deletion_${pubkey}`,
filters: [{
kinds: [EVENT_DELETION_KIND],
authors: [pubkey]
}]
};
}
};
}