Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return websocket when config filters are removed #403

Merged
merged 1 commit into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion sci-log-db/src/__tests__/unit/websocket.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
Expand Down
47 changes: 40 additions & 7 deletions sci-log-db/src/utils/websocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}));
}
});
Expand Down Expand Up @@ -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
);
}
Loading