Skip to content

Commit

Permalink
Fix websocket match filter not filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
minottic committed Aug 27, 2024
1 parent 2592c1c commit 88b960a
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 18 deletions.
51 changes: 51 additions & 0 deletions sci-log-db/src/__tests__/unit/utils.misc.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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);
});
});
});
30 changes: 12 additions & 18 deletions sci-log-db/src/utils/websocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

0 comments on commit 88b960a

Please sign in to comment.