Skip to content

Commit

Permalink
feat: modify handling of DMs queries by non-auth user (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
CodyTseng authored Feb 17, 2024
1 parent bb960a8 commit 6d64f16
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 38 deletions.
1 change: 0 additions & 1 deletion packages/common/src/enums/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from './client.enum';
export * from './event.enum';
export * from './log.enum';
export * from './message.enum';
12 changes: 0 additions & 12 deletions packages/common/src/enums/message.enum.ts

This file was deleted.

58 changes: 40 additions & 18 deletions packages/common/src/interfaces/message.interface.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,58 @@
import { MessageType } from '../enums';
import { EventId, SubscriptionId } from './common.interface';
import { Event } from './event.interface';
import { Filter } from './filter.interface';

export type MessageTypeEvent = MessageType.EVENT | 'EVENT';
export type MessageTypeReq = MessageType.REQ | 'REQ';
export type MessageTypeClose = MessageType.CLOSE | 'CLOSE';
export type MessageTypeAuth = MessageType.AUTH | 'AUTH';
export type MessageTypeOk = MessageType.OK | 'OK';
export type MessageTypeEose = MessageType.EOSE | 'EOSE';
export type MessageTypeNotice = MessageType.NOTICE | 'NOTICE';
export const MessageType = {
REQ: 'REQ',
EVENT: 'EVENT',
CLOSE: 'CLOSE',
AUTH: 'AUTH',
EOSE: 'EOSE',
OK: 'OK',
NOTICE: 'NOTICE',
CLOSED: 'CLOSED',
} as const;
export type TMessageType = (typeof MessageType)[keyof typeof MessageType];

export type IncomingMessage =
| IncomingEventMessage
| IncomingReqMessage
| IncomingCloseMessage
| IncomingAuthMessage;

export type IncomingEventMessage = [MessageTypeEvent, Event];
export type IncomingReqMessage = [MessageTypeReq, SubscriptionId, ...Filter[]];
export type IncomingCloseMessage = [MessageTypeClose, SubscriptionId];
export type IncomingAuthMessage = [MessageTypeAuth, Event];
export type IncomingEventMessage = [typeof MessageType.EVENT, Event];
export type IncomingReqMessage = [
typeof MessageType.REQ,
SubscriptionId,
...Filter[],
];
export type IncomingCloseMessage = [typeof MessageType.CLOSE, SubscriptionId];
export type IncomingAuthMessage = [typeof MessageType.AUTH, Event];

export type OutgoingMessage =
| OutgoingOkMessage
| OutgoingEventMessage
| OutgoingEoseMessage
| OutgoingNoticeMessage
| OutgoingAuthMessage;
| OutgoingAuthMessage
| OutgoingClosedMessage;

export type OutgoingOkMessage = [MessageTypeOk, EventId, boolean, string];
export type OutgoingEventMessage = [MessageTypeEvent, SubscriptionId, Event];
export type OutgoingEoseMessage = [MessageTypeEose, SubscriptionId];
export type OutgoingNoticeMessage = [MessageTypeNotice, string];
export type OutgoingAuthMessage = [MessageTypeAuth, string];
export type OutgoingOkMessage = [
typeof MessageType.OK,
EventId,
boolean,
string,
];
export type OutgoingEventMessage = [
typeof MessageType.EVENT,
SubscriptionId,
Event,
];
export type OutgoingEoseMessage = [typeof MessageType.EOSE, SubscriptionId];
export type OutgoingNoticeMessage = [typeof MessageType.NOTICE, string];
export type OutgoingAuthMessage = [typeof MessageType.AUTH, string];
export type OutgoingClosedMessage = [
typeof MessageType.CLOSED,
SubscriptionId,
string,
];
10 changes: 8 additions & 2 deletions packages/core/__test__/nostr-relay.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,12 +228,18 @@ describe('NostrRelay', () => {
);

expect(result).toEqual({ events: [] });
expect(client.send).toHaveBeenCalledWith(
expect(client.send).toHaveBeenNthCalledWith(
1,
JSON.stringify([
MessageType.NOTICE,
MessageType.CLOSED,
subscriptionId,
"restricted: we can't serve DMs to unauthenticated users, does your client implement NIP-42?",
]),
);
expect(client.send).toHaveBeenNthCalledWith(
2,
JSON.stringify([MessageType.AUTH, ctx.id]),
);
});

it('should handle req successfully if client is authenticated and filter contains encrypted direct message kind', async () => {
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/interfaces/handle-result.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ export type HandleAuthMessageResult = {
* Result of handling an incoming message
*/
export type HandleMessageResult =
| ({ messageType: MessageType.REQ } & HandleReqMessageResult)
| ({ messageType: MessageType.EVENT } & HandleEventMessageResult)
| ({ messageType: MessageType.CLOSE } & HandleCloseMessageResult)
| ({ messageType: MessageType.AUTH } & HandleAuthMessageResult)
| ({ messageType: typeof MessageType.REQ } & HandleReqMessageResult)
| ({ messageType: typeof MessageType.EVENT } & HandleEventMessageResult)
| ({ messageType: typeof MessageType.CLOSE } & HandleCloseMessageResult)
| ({ messageType: typeof MessageType.AUTH } & HandleAuthMessageResult)
| void;
5 changes: 4 additions & 1 deletion packages/core/src/nostr-relay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { SubscriptionService } from './services/subscription.service';
import {
LazyCache,
createOutgoingAuthMessage,
createOutgoingClosedMessage,
createOutgoingEoseMessage,
createOutgoingEventMessage,
createOutgoingNoticeMessage,
Expand Down Expand Up @@ -252,10 +253,12 @@ export class NostrRelay {
!ctx.pubkey
) {
ctx.sendMessage(
createOutgoingNoticeMessage(
createOutgoingClosedMessage(
subscriptionId,
"restricted: we can't serve DMs to unauthenticated users, does your client implement NIP-42?",
),
);
ctx.sendMessage(createOutgoingAuthMessage(ctx.id));
return { events };
}

Expand Down
15 changes: 15 additions & 0 deletions packages/core/src/utils/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
Event,
MessageType,
OutgoingAuthMessage,
OutgoingClosedMessage,
OutgoingEoseMessage,
OutgoingEventMessage,
OutgoingNoticeMessage,
Expand Down Expand Up @@ -74,3 +75,17 @@ export function createOutgoingAuthMessage(
): OutgoingAuthMessage {
return [MessageType.AUTH, challenge];
}

/**
* Create an outgoing CLOSED message.
* More info: https://github.com/nostr-protocol/nips/blob/master/01.md
*
* @param subscriptionId Subscription Id
* @param message Reason for closing the subscription
*/
export function createOutgoingClosedMessage(
subscriptionId: SubscriptionId,
message = '',
): OutgoingClosedMessage {
return [MessageType.CLOSED, subscriptionId, message];
}

0 comments on commit 6d64f16

Please sign in to comment.