diff --git a/sci-log-db/src/__tests__/unit/websocket.utils.ts b/sci-log-db/src/__tests__/unit/websocket.utils.ts index bb6a2524..b5301d08 100644 --- a/sci-log-db/src/__tests__/unit/websocket.utils.ts +++ b/sci-log-db/src/__tests__/unit/websocket.utils.ts @@ -64,10 +64,29 @@ describe('Websocket unit tests', function (this: Suite) { ], expected: true, }, + { + input: [ + {tags: [], snippetType: 'aType'}, + {filter: {tags: ['a', 'c'], snippetType: ['aType']}}, + ], + expected: false, + }, + { + input: [ + {tags: [], snippetType: 'aType'}, + {filter: {tags: ['a', 'c'], snippetType: ['aType']}}, + {updatedFields: {tags: []}, removedFields: {}}, + ], + expected: true, + }, ].forEach((t, i) => { it(`Should test matchesFilterSettings ${i}`, () => { expect( - matchesFilterSettings(t.input[0] as Basesnippet, t.input[1]), + matchesFilterSettings( + t.input[0] as Basesnippet, + t.input[1], + t.input[2] as {updatedFields: object; removedFields: object}, + ), ).to.be.eql(t.expected); }); }); diff --git a/sci-log-db/src/utils/websocket.ts b/sci-log-db/src/utils/websocket.ts index d80fd73c..64515a57 100644 --- a/sci-log-db/src/utils/websocket.ts +++ b/sci-log-db/src/utils/websocket.ts @@ -201,7 +201,13 @@ export async function startWebsocket(app: SciLogDbApplication) { if ( doc['readACL']?.some((r: string) => c.user.roles.includes(r)) ) { - if (matchesFilterSettings(doc, c.config)) + if ( + matchesFilterSettings( + doc, + c.config, + change.updateDescription, + ) + ) c.ws.send(JSON.stringify({'new-notification': change})); } }); @@ -249,14 +255,41 @@ export async function startWebsocket(app: SciLogDbApplication) { export function matchesFilterSettings( snippet: Basesnippet, config: {filter?: {tags?: string[]; snippetType?: string[]}}, + changeUpdate: + | { + updatedFields: {tags?: string[]; snippetType?: string}; + removedFields: {tags?: string[]; snippetType?: string}; + } + | undefined, ): boolean { + if (snippet.snippetType === 'edit') return true; const tagCondition = - !config.filter?.tags || - config.filter.tags.length === 0 || - config.filter.tags.some(tag => snippet.tags?.includes(tag)); + matchFilterField(config, changeUpdate, 'tags') || + (config.filter as {tags: string[]}).tags?.some(tag => + snippet.tags?.includes(tag), + ); const snippetTypeCondition = - !config.filter?.snippetType || - config.filter.snippetType.length === 0 || - config.filter.snippetType.includes(snippet.snippetType); + matchFilterField(config, changeUpdate, 'snippetType') || + (config.filter as {snippetType: string[]}).snippetType?.includes( + snippet.snippetType, + ); return tagCondition && snippetTypeCondition; } + +function matchFilterField( + config: {filter?: {tags?: string[]; snippetType?: string[]}}, + changeUpdate: + | { + updatedFields: {tags?: string[]; snippetType?: string}; + removedFields: {tags?: string[]; snippetType?: string}; + } + | undefined, + field: 'tags' | 'snippetType', +) { + return ( + !config.filter?.[field] || + config.filter[field]?.length === 0 || + changeUpdate?.removedFields?.[field] !== undefined || + changeUpdate?.updatedFields?.[field] !== undefined + ); +}