mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
fix: Update the lastEmit value after sending a notification
This commit is contained in:
parent
3a0c8159df
commit
b2f4d7fb2d
@ -57,7 +57,16 @@ export class ListeningActivityHandler extends StaticHandler {
|
|||||||
|
|
||||||
// No need to wait on this to resolve before going to the next channel.
|
// No need to wait on this to resolve before going to the next channel.
|
||||||
// Prevent failed notification from blocking other notifications.
|
// Prevent failed notification from blocking other notifications.
|
||||||
this.handler.handleSafe({ channel, activity, topic }).catch((error): void => {
|
this.handler.handleSafe({ channel, activity, topic })
|
||||||
|
.then((): Promise<void> => {
|
||||||
|
// Update the `lastEmit` value if the channel has a rate limit
|
||||||
|
if (channel.rate) {
|
||||||
|
channel.lastEmit = Date.now();
|
||||||
|
return this.storage.update(channel);
|
||||||
|
}
|
||||||
|
return Promise.resolve();
|
||||||
|
})
|
||||||
|
.catch((error): void => {
|
||||||
this.logger.error(`Error trying to handle notification for ${id}: ${createErrorMessage(error)}`);
|
this.logger.error(`Error trying to handle notification for ${id}: ${createErrorMessage(error)}`);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -38,6 +38,7 @@ describe('A ListeningActivityHandler', (): void => {
|
|||||||
storage = {
|
storage = {
|
||||||
getAll: jest.fn().mockResolvedValue([ channel.id ]),
|
getAll: jest.fn().mockResolvedValue([ channel.id ]),
|
||||||
get: jest.fn().mockResolvedValue(channel),
|
get: jest.fn().mockResolvedValue(channel),
|
||||||
|
update: jest.fn(),
|
||||||
} as any;
|
} as any;
|
||||||
|
|
||||||
emitter = new EventEmitter() as any;
|
emitter = new EventEmitter() as any;
|
||||||
@ -58,6 +59,25 @@ describe('A ListeningActivityHandler', (): void => {
|
|||||||
expect(notificationHandler.handleSafe).toHaveBeenCalledTimes(1);
|
expect(notificationHandler.handleSafe).toHaveBeenCalledTimes(1);
|
||||||
expect(notificationHandler.handleSafe).toHaveBeenLastCalledWith({ channel, activity, topic });
|
expect(notificationHandler.handleSafe).toHaveBeenLastCalledWith({ channel, activity, topic });
|
||||||
expect(logger.error).toHaveBeenCalledTimes(0);
|
expect(logger.error).toHaveBeenCalledTimes(0);
|
||||||
|
expect(storage.update).toHaveBeenCalledTimes(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('updates the lastEmit value of the channel if it has a rate limit.', async(): Promise<void> => {
|
||||||
|
jest.useFakeTimers();
|
||||||
|
channel.rate = 10 * 1000;
|
||||||
|
emitter.emit('changed', topic, activity);
|
||||||
|
|
||||||
|
await flushPromises();
|
||||||
|
|
||||||
|
expect(notificationHandler.handleSafe).toHaveBeenCalledTimes(1);
|
||||||
|
expect(notificationHandler.handleSafe).toHaveBeenLastCalledWith({ channel, activity, topic });
|
||||||
|
expect(logger.error).toHaveBeenCalledTimes(0);
|
||||||
|
expect(storage.update).toHaveBeenCalledTimes(1);
|
||||||
|
expect(storage.update).toHaveBeenLastCalledWith({
|
||||||
|
...channel,
|
||||||
|
lastEmit: Date.now(),
|
||||||
|
});
|
||||||
|
jest.useRealTimers();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('does not emit an event on channels if their rate does not yet allow it.', async(): Promise<void> => {
|
it('does not emit an event on channels if their rate does not yet allow it.', async(): Promise<void> => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user