Skip to content

Commit

Permalink
refactor: move beforeHandleEvent aspect
Browse files Browse the repository at this point in the history
  • Loading branch information
CodyTseng committed Sep 13, 2024
1 parent d721fe9 commit 986abd7
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 47 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ This method will be called before handling an event. If the method returns false

params:

- `ctx`: The context object of the client.
- `event`: The incoming event.

Example:
Expand All @@ -112,7 +111,7 @@ class BlacklistGuardPlugin implements BeforeHandleEventPlugin {
// ...
];

beforeHandleEvent(_, event) {
beforeHandleEvent(event) {
const canHandle = !this.blacklist.includes(event.pubkey);
return {
canHandle,
Expand Down
4 changes: 1 addition & 3 deletions packages/common/src/interfaces/plugin.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export interface HandleMessagePlugin {
* // ...
* ];
*
* beforeHandleEvent(_, event) {
* beforeHandleEvent(event) {
* const canHandle = !this.blacklist.includes(event.pubkey);
* return {
* canHandle,
Expand All @@ -79,11 +79,9 @@ export interface BeforeHandleEventPlugin {
/**
* This method will be called before handling an event.
*
* @param ctx The client context
* @param event The event will be handled
*/
beforeHandleEvent(
ctx: ClientContext,
event: Event,
): Promise<BeforeHandleEventResult> | BeforeHandleEventResult;
}
Expand Down
17 changes: 0 additions & 17 deletions packages/core/__test__/nostr-relay.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,23 +151,6 @@ describe('NostrRelay', () => {
expect(client.send).toHaveBeenNthCalledWith(1, outgoingMessageStr);
expect(client.send).toHaveBeenNthCalledWith(2, outgoingMessageStr);
});

it("should ignore if plugin's beforeHandleEvent returns false", async () => {
const event = { id: 'eventId' } as Event;
const outgoingMessage: OutgoingOkMessage = [
MessageType.OK,
event.id,
false,
'block',
];
jest
.spyOn(nostrRelay['pluginManagerService'], 'beforeHandleEvent')
.mockResolvedValue({ canHandle: false, message: 'block' });

await nostrRelay.handleMessage(client, [MessageType.EVENT, event]);

expect(client.send).toHaveBeenCalledWith(JSON.stringify(outgoingMessage));
});
});

describe('req', () => {
Expand Down
12 changes: 12 additions & 0 deletions packages/core/__test__/services/event.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,18 @@ describe('eventService', () => {
expect(subscriptionService.broadcast).not.toHaveBeenCalled();
expect(spyLoggerError).toHaveBeenCalled();
});

it('should return directly if beforeHandleEvent return false', async () => {
jest.spyOn(pluginManagerService, 'beforeHandleEvent').mockResolvedValue({
canHandle: false,
message: 'block: test',
});

expect(await eventService.handleEvent({} as Event)).toEqual({
success: false,
message: 'block: test',
});
});
});

describe('destroy', () => {
Expand Down
10 changes: 2 additions & 8 deletions packages/core/__test__/services/plugin-manager.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,7 @@ describe('PluginManagerService', () => {
},
);

const result = await pluginManagerService.beforeHandleEvent(
ctx,
{} as Event,
);
const result = await pluginManagerService.beforeHandleEvent({} as Event);

expect(arr).toEqual([1, 2]);
expect(result).toEqual({ canHandle: true });
Expand All @@ -177,10 +174,7 @@ describe('PluginManagerService', () => {
},
);

const result = await pluginManagerService.beforeHandleEvent(
ctx,
{} as Event,
);
const result = await pluginManagerService.beforeHandleEvent({} as Event);

expect(arr).toEqual([1]);
expect(result).toEqual({ canHandle: false, message: 'block' });
Expand Down
13 changes: 1 addition & 12 deletions packages/core/src/nostr-relay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,18 +190,7 @@ export class NostrRelay {
ctx: ClientContext,
event: Event,
): Promise<HandleEventMessageResult> {
let handleResult: HandleEventResult;
const beforeHandleEventResult =
await this.pluginManagerService.beforeHandleEvent(ctx, event);

if (!beforeHandleEventResult.canHandle) {
handleResult = {
success: false,
message: beforeHandleEventResult.message,
};
} else {
handleResult = await this.handleEvent(event);
}
const handleResult = await this.handleEvent(event);

ctx.sendMessage(
createOutgoingOkMessage(
Expand Down
9 changes: 9 additions & 0 deletions packages/core/src/services/event.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ export class EventService {
}

async handleEvent(event: Event): Promise<HandleEventResult> {
const beforeHandleEventResult =
await this.pluginManagerService.beforeHandleEvent(event);
if (!beforeHandleEventResult.canHandle) {
return {
success: false,
message: beforeHandleEventResult.message,
};
}

if (event.kind === EventKind.AUTHENTICATION) {
return { success: true };
}
Expand Down
7 changes: 2 additions & 5 deletions packages/core/src/services/plugin-manager.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,9 @@ export class PluginManagerService {
);
}

async beforeHandleEvent(
ctx: ClientContext,
event: Event,
): Promise<BeforeHandleEventResult> {
async beforeHandleEvent(event: Event): Promise<BeforeHandleEventResult> {
for (const plugin of this.beforeHandleEventPlugins) {
const result = await plugin.beforeHandleEvent(ctx, event);
const result = await plugin.beforeHandleEvent(event);
if (!result.canHandle) {
return result;
}
Expand Down

0 comments on commit 986abd7

Please sign in to comment.