1
0
mirror of https://github.com/bitwarden/browser synced 2026-02-10 05:30:01 +00:00

Merge branch 'poc/search-query-language' of https://github.com/bitwarden/clients into poc/search-query-language

This commit is contained in:
Justin Baur
2025-03-20 15:33:51 -04:00
2 changed files with 38 additions and 14 deletions

View File

@@ -1,6 +1,6 @@
import { firstValueFrom, of } from "rxjs";
import { CipherType } from "../enums";
import { CipherType, FieldType } from "../enums";
import { AttachmentView } from "../models/view/attachment.view";
import { CipherView } from "../models/view/cipher.view";
import { FieldView } from "../models/view/field.view";
@@ -25,6 +25,8 @@ const createCipher = (data: TestCipher) => {
cipher.fields = data.fields?.map((f) => {
const field = new FieldView();
field.name = f;
field.type = FieldType.Text;
field.linkedId = null;
return field;
});
cipher.collectionIds = data.collectionIds;
@@ -54,7 +56,7 @@ describe("VaultFilterMetadataService", () => {
input: [createCipher({ type: CipherType.Card })],
output: {
vaults: new Map([[null, 1]]),
fieldNames: new Map([]),
customFields: new Map([]),
itemTypes: new Map([[CipherType.Card, 1]]),
folders: new Map([]),
collections: new Map([]),
@@ -98,9 +100,9 @@ describe("VaultFilterMetadataService", () => {
["org-one", 2],
["org-two", 2],
]),
fieldNames: new Map([
["one", 7],
["five", 1],
customFields: new Map([
[{ name: "one", type: FieldType.Text, linkedType: null }, 7],
[{ name: "five", type: FieldType.Text, linkedType: null }, 1],
]),
itemTypes: new Map([
[CipherType.Login, 3],
@@ -120,7 +122,7 @@ describe("VaultFilterMetadataService", () => {
const actualMetadata = await firstValueFrom(of(input).pipe(sut.collectMetadata()));
expect(actualMetadata.vaults).toEqual(output.vaults);
expect(actualMetadata.fieldNames).toEqual(output.fieldNames);
expect(actualMetadata.customFields).toEqual(output.customFields);
expect(actualMetadata.itemTypes).toEqual(output.itemTypes);
expect(actualMetadata.folders).toEqual(output.folders);
expect(actualMetadata.collections).toEqual(output.collections);

View File

@@ -1,26 +1,44 @@
import { map } from "rxjs";
import { CipherType } from "../enums";
import { CipherType, FieldType, LinkedIdType } from "../enums";
import { CipherView } from "../models/view/cipher.view";
export type VaultFilterMetadata = {
vaults: Map<string | null, number>;
fieldNames: Map<string, number>;
customFields: Map<CustomFieldMetadata, number>;
itemTypes: Map<CipherType, number>;
folders: Map<string, number>;
collections: Map<string, number>;
attachmentCount: number;
};
type CustomFieldMetadata = {
name: string;
type: FieldType;
linkedType: LinkedIdType | null;
};
function isCustomFieldMetadata(x: MetadataType): x is CustomFieldMetadata {
return typeof x === "object" && x != null && "name" in x && "type" in x;
}
type ExtractKey<T> = T extends Map<infer K, any> ? K : never;
type MetadataType = ExtractKey<VaultFilterMetadata[keyof VaultFilterMetadata]>;
function metaDataKeyEqual<T extends MetadataType>(a: T, b: T) {
if (isCustomFieldMetadata(a) && isCustomFieldMetadata(b)) {
return a.name === b.name && a.type === b.type && a.linkedType === b.linkedType;
} else {
return a === b;
}
}
export class VaultFilterMetadataService {
collectMetadata() {
const setOrIncrement = <T>(map: Map<T, number>, key: T) => {
const entry = map.get(key);
const setOrIncrement = <T extends MetadataType>(map: Map<T, number>, key: T) => {
const entry = Array.from(map.entries()).find(([k]) => metaDataKeyEqual(key, k));
if (entry == undefined) {
if (entry == null) {
map.set(key, 1);
} else {
map.set(key, entry + 1);
map.set(entry[0], entry[1] + 1);
}
};
@@ -36,7 +54,11 @@ export class VaultFilterMetadataService {
// Track all field names
if (cipher.fields != null) {
for (const field of cipher.fields) {
setOrIncrement(metadata.fieldNames, field.name);
setOrIncrement(metadata.customFields, {
name: field.name,
type: field.type,
linkedType: field.linkedId,
});
}
}
@@ -61,7 +83,7 @@ export class VaultFilterMetadataService {
},
{
vaults: new Map<string | null, number>(),
fieldNames: new Map<string, number>(),
customFields: new Map<CustomFieldMetadata, number>(),
itemTypes: new Map<CipherType, number>(),
folders: new Map<string, number>(),
collections: new Map<string, number>(),