-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnip-28.ts
258 lines (227 loc) · 6.41 KB
/
nip-28.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
/**
* @file NIP-28: Public Chat
* @module nips/nip-28
* @see https://github.com/nostr-protocol/nips/blob/master/28.md
*/
import type { NostrWSMessage } from '../types/messages.js';
import type { Logger } from '../types/logger.js';
/**
* Chat event kinds
*/
export const ChatEventKinds = {
CHANNEL_CREATION: 40,
CHANNEL_METADATA: 41,
CHANNEL_MESSAGE: 42,
CHANNEL_HIDE_MESSAGE: 43,
CHANNEL_MUTE_USER: 44,
USER_MUTE: 45
} as const;
/**
* Channel metadata structure
*/
export interface ChannelMetadata {
name: string;
about?: string;
picture?: string;
rules?: string[];
moderators?: string[];
pinned?: string[];
}
/**
* Creates a channel creation message
* @param metadata - Channel metadata
* @returns {NostrWSMessage} Channel creation event
*/
export function createChannelCreationEvent(
metadata: ChannelMetadata
): NostrWSMessage {
return ['EVENT', {
kind: ChatEventKinds.CHANNEL_CREATION,
content: JSON.stringify(metadata),
tags: []
}];
}
/**
* Creates a channel message
* @param channelId - Channel ID
* @param content - Message content
* @param replyTo - Optional ID of message being replied to
* @returns {NostrWSMessage} Channel message event
*/
export function createChannelMessage(
channelId: string,
content: string,
replyTo?: string
): NostrWSMessage {
const tags = [['e', channelId, '', 'root']];
if (replyTo) {
tags.push(['e', replyTo, '', 'reply']);
}
return ['EVENT', {
kind: ChatEventKinds.CHANNEL_MESSAGE,
content,
tags
}];
}
/**
* Creates a message moderation event
* @param channelId - Channel ID
* @param messageId - Message ID to moderate
* @param reason - Moderation reason
* @returns {NostrWSMessage} Hide message event
*/
export function createHideMessageEvent(
channelId: string,
messageId: string,
reason: string
): NostrWSMessage {
return ['EVENT', {
kind: ChatEventKinds.CHANNEL_HIDE_MESSAGE,
content: reason,
tags: [
['e', channelId, '', 'root'],
['e', messageId, '', 'reply']
]
}];
}
/**
* Chat message handler interface
*/
export interface ChatMessageHandler {
/**
* Handles incoming chat message
* @param message - Chat message
* @returns {Promise<void>}
*/
handleMessage(message: NostrWSMessage): Promise<void>;
/**
* Handles message moderation
* @param message - Moderation message
* @returns {Promise<void>}
*/
handleModeration(message: NostrWSMessage): Promise<void>;
}
/**
* Creates a chat message handler
* @param logger - Logger instance
* @returns {ChatMessageHandler} Message handler
*/
export function createChatMessageHandler(logger: Logger): ChatMessageHandler {
return {
async handleMessage(message: NostrWSMessage): Promise<void> {
try {
if (!Array.isArray(message) || message[0] !== 'EVENT') return;
const event = message[1] as NostrEvent;
if (event.kind !== ChatEventKinds.CHANNEL_MESSAGE) return;
// Extract channel ID and reply ID from tags
const { tags } = event;
const channelId = tags.find(tag =>
tag[0] === 'e' && tag[3] === 'root'
)?.[1];
const replyId = tags.find(tag =>
tag[0] === 'e' && tag[3] === 'reply'
)?.[1];
// Process message
logger.debug('Processing chat message', {
channelId,
replyId,
content: event.content
});
// Additional message processing logic here
} catch (error) {
logger.error('Error handling chat message:', error);
}
},
async handleModeration(message: NostrWSMessage): Promise<void> {
try {
if (!Array.isArray(message) || message[0] !== 'EVENT') return;
const event = message[1] as NostrEvent;
if (event.kind !== ChatEventKinds.CHANNEL_HIDE_MESSAGE) return;
// Extract channel and message IDs
const { tags } = event;
const channelId = tags.find(tag =>
tag[0] === 'e' && tag[3] === 'root'
)?.[1];
const messageId = tags.find(tag =>
tag[0] === 'e' && tag[3] === 'reply'
)?.[1];
// Process moderation
logger.debug('Processing message moderation', {
channelId,
messageId,
reason: event.content
});
// Additional moderation logic here
} catch (error) {
logger.error('Error handling message moderation:', error);
}
}
};
}
/**
* Channel subscription manager interface
*/
export interface ChannelSubscriptionManager {
/**
* Subscribes to a channel
* @param channelId - Channel ID
* @returns {NostrWSMessage} Subscription message
*/
subscribe(channelId: string): NostrWSMessage;
/**
* Unsubscribes from a channel
* @param channelId - Channel ID
* @returns {NostrWSMessage} Unsubscribe message
*/
unsubscribe(channelId: string): NostrWSMessage;
/**
* Gets channel metadata
* @param channelId - Channel ID
* @returns {Promise<ChannelMetadata | undefined>} Channel metadata
*/
getMetadata(channelId: string): Promise<ChannelMetadata | undefined>;
}
/**
* Creates a channel subscription manager
* @param logger - Logger instance
* @returns {ChannelSubscriptionManager} Subscription manager
*/
export function createChannelSubscriptionManager(
logger: Logger
): ChannelSubscriptionManager {
const subscriptions = new Map<string, string>(); // channelId -> subscriptionId
const metadata = new Map<string, ChannelMetadata>();
return {
subscribe(channelId: string): NostrWSMessage {
const subscriptionId = `chat:${channelId}:${Date.now()}`;
subscriptions.set(channelId, subscriptionId);
return ['REQ', {
subscription_id: subscriptionId,
filter: {
kinds: [
ChatEventKinds.CHANNEL_MESSAGE,
ChatEventKinds.CHANNEL_HIDE_MESSAGE
],
'#e': [channelId]
}
}];
},
unsubscribe(channelId: string): NostrWSMessage {
const subscriptionId = subscriptions.get(channelId);
if (!subscriptionId) {
logger.debug(`No subscription found for channel ${channelId}`);
return ['CLOSE', { subscription_id: '' }];
}
subscriptions.delete(channelId);
return ['CLOSE', { subscription_id: subscriptionId }];
},
async getMetadata(channelId: string): Promise<ChannelMetadata | undefined> {
return metadata.get(channelId);
}
};
}
interface NostrEvent {
kind: number;
content: string;
tags: string[][];
}