From 88b960a4e051122ef555ca5c4199ef025c670013 Mon Sep 17 00:00:00 2001 From: minottic Date: Mon, 26 Aug 2024 16:56:29 +0200 Subject: [PATCH] Fix websocket match filter not filtering --- .../src/__tests__/unit/utils.misc.unit.ts | 51 +++++++++++++++++++ sci-log-db/src/utils/websocket.ts | 30 +++++------ 2 files changed, 63 insertions(+), 18 deletions(-) diff --git a/sci-log-db/src/__tests__/unit/utils.misc.unit.ts b/sci-log-db/src/__tests__/unit/utils.misc.unit.ts index b3473736..27e2e5bb 100644 --- a/sci-log-db/src/__tests__/unit/utils.misc.unit.ts +++ b/sci-log-db/src/__tests__/unit/utils.misc.unit.ts @@ -14,6 +14,7 @@ import { sanitizeTextContent, sanitizeTextContentInPlace, } from '../../utils/misc'; +import {matchesFilterSettings} from '../../utils/websocket'; describe('Utils unit tests', function (this: Suite) { it('Should filterEmptySubsnippets', () => { @@ -316,4 +317,54 @@ describe('Utils unit tests', function (this: Suite) { expect(t.input).to.be.eql(t.expected); }); }); + + [ + { + input: [{tags: ['a']}, {filter: {}}], + expected: true, + }, + { + input: [{tags: ['a', 'p']}, {filter: {tags: ['b', 'c']}}], + expected: false, + }, + { + input: [{snippetType: 'anotherType'}, {filter: {snippetType: ['aType']}}], + expected: false, + }, + { + input: [{snippetType: 'aType'}, {filter: {snippetType: ['aType']}}], + expected: true, + }, + { + input: [{tags: ['a', 'p']}, {filter: {tags: ['a', 'c']}}], + expected: true, + }, + { + input: [ + {tags: ['a', 'p'], snippetType: 'anotherType'}, + {filter: {tags: ['a', 'c'], snippetType: ['aType']}}, + ], + expected: false, + }, + { + input: [ + {tags: ['a', 'p'], snippetType: 'aType'}, + {filter: {tags: ['a', 'c'], snippetType: ['aType']}}, + ], + expected: true, + }, + { + input: [ + {tags: ['b', 'p'], snippetType: 'aType'}, + {filter: {tags: ['a', 'c'], snippetType: ['aType']}}, + ], + expected: false, + }, + ].forEach((t, i) => { + it(`Should test matchesFilterSettings ${i}`, () => { + expect( + matchesFilterSettings(t.input[0] as Basesnippet, t.input[1]), + ).to.be.eql(t.expected); + }); + }); }); diff --git a/sci-log-db/src/utils/websocket.ts b/sci-log-db/src/utils/websocket.ts index 786b01c5..242b6e0a 100644 --- a/sci-log-db/src/utils/websocket.ts +++ b/sci-log-db/src/utils/websocket.ts @@ -2,6 +2,7 @@ import {TokenServiceBindings} from '@loopback/authentication-jwt'; import {AnyObject} from '@loopback/repository'; import {SciLogDbApplication} from '../application'; import {MongoDataSource} from '../datasources'; +import {Basesnippet} from '../models'; export interface WebsocketClient { // eslint-disable-next-line @typescript-eslint/no-explicit-any @@ -245,22 +246,15 @@ export async function startWebsocket(app: SciLogDbApplication) { }); } -// eslint-disable-next-line @typescript-eslint/no-explicit-any -function matchesFilterSettings(snippet: any, config: any): boolean { - const acceptSnippet = true; - if (typeof config.filter.tags != 'undefined') { - // eslint-disable-next-line @typescript-eslint/no-explicit-any - config.filter.tags.forEach((tag: any) => { - if (!snippet.tags.includes(tag)) { - return false; - } - }); - } - - if (typeof config.filter.snippetType != 'undefined') { - if (!config.filter.snippetType.includes(snippet.snippetType)) { - return false; - } - } - return acceptSnippet; +export function matchesFilterSettings( + snippet: Basesnippet, + config: {filter?: {tags?: string[]; snippetType?: string[]}}, +): boolean { + const tagCondition = + !config.filter?.tags || + config.filter?.tags?.some(tag => snippet.tags?.includes(tag)); + const snippetTypeCondition = + !config.filter?.snippetType || + config.filter?.snippetType?.includes(snippet.snippetType); + return tagCondition && snippetTypeCondition; }