Skip to content

Commit

Permalink
Support recent entry lookup in browser
Browse files Browse the repository at this point in the history
  • Loading branch information
perry-mitchell committed Feb 16, 2024
1 parent 6662007 commit b053a49
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 5 deletions.
3 changes: 2 additions & 1 deletion source/main/services/browser/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import express, { Request, Response, NextFunction } from "express";
import createRouter from "express-promise-router";
import { VERSION } from "../../library/build";
import { handleAuthPing, processAuthRequest, processAuthResponse } from "./controllers/auth";
import { searchEntries } from "./controllers/entries";
import { searchEntries, searchSpecificEntries } from "./controllers/entries";
import { getAllOTPs } from "./controllers/otp";
import { getVaults, getVaultsTree, promptVaultLock, promptVaultUnlock } from "./controllers/vaults";
import { handleError } from "./error";
Expand All @@ -29,6 +29,7 @@ function createRoutes(app: express.Application): void {
router.post("/auth/response", processAuthResponse);
router.post("/auth/test", requireClient, requireKeyAuth, handleAuthPing);
router.get("/entries", requireClient, searchEntries);
router.post("/entries/specific", requireClient, requireKeyAuth, searchSpecificEntries);
router.get("/otps", requireClient, getAllOTPs);
router.get("/vaults", requireClient, getVaults);
router.get("/vaults-tree", requireClient, getVaultsTree);
Expand Down
25 changes: 22 additions & 3 deletions source/main/services/browser/controllers/entries.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Request, Response } from "express";
import { SearchResult } from "buttercup";
import { EntriesSearchPayloadSchema, EntriesSearchType } from "../models";
import { PropertyKeyValueObject, SearchResult, getEntryURLs } from "buttercup";
import { EntriesSearchBodySchema, EntriesSearchQuerySchema, EntriesSearchType } from "../models";
import { searchAllVaultsByTerm, searchAllVaultsByURL } from "../../search";
import { respondJSON } from "../response";
import { getEntries } from "../../buttercup";

export async function searchEntries(req: Request, res: Response) {
const config = EntriesSearchPayloadSchema.parse(req.query);
const config = EntriesSearchQuerySchema.parse(req.query);
let results: Array<SearchResult> = [];
if (config.type === EntriesSearchType.Term) {
results = await searchAllVaultsByTerm(config.term);
Expand All @@ -16,3 +17,21 @@ export async function searchEntries(req: Request, res: Response) {
results
});
}

export async function searchSpecificEntries(req: Request, res: Response) {
const { entries } = EntriesSearchBodySchema.parse(req.body);
const resultItems = await getEntries(entries as Array<{ entryID: string; sourceID: string }>);
const results: Array<SearchResult> = resultItems.map((item) => ({
entryType: item.entry.getType(),
groupID: item.entry.getGroup().id,
id: item.entry.id,
properties: item.entry.getProperty() as PropertyKeyValueObject,
tags: item.entry.getTags(),
sourceID: item.sourceID,
urls: getEntryURLs(item.entry.getProperty() as PropertyKeyValueObject),
vaultID: item.entry.vault.id
}));
await respondJSON(res, {
results
});
}
11 changes: 10 additions & 1 deletion source/main/services/browser/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,16 @@ export enum EntriesSearchType {
URL = "url"
}

export const EntriesSearchPayloadSchema = z.discriminatedUnion("type", [
export const EntriesSearchBodySchema = z.object({
entries: z.array(
z.object({
entryID: z.string().min(1),
sourceID: z.string().min(1)
})
)
});

export const EntriesSearchQuerySchema = z.discriminatedUnion("type", [
z.object({
term: z.string(),
type: z.literal(EntriesSearchType.Term)
Expand Down
17 changes: 17 additions & 0 deletions source/main/services/buttercup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,23 @@ export async function getAttachmentDetails(
return source.attachmentManager.getAttachmentDetails(entry, attachmentID);
}

export async function getEntries(
entries: Array<{ entryID: EntryID; sourceID: VaultSourceID }>
): Promise<Array<{ entry: Entry; sourceID: VaultSourceID }>> {
const vaultManager = getVaultManager();
return entries.reduce((output, item) => {
const source = vaultManager.getSourceForID(item.sourceID);
if (!source || source.status !== VaultSourceStatus.Unlocked) return output;
return [
...output,
{
entry: source.vault.findEntryByID(item.entryID),
sourceID: item.sourceID
}
];
}, []);
}

export function getSourceDescription(sourceID: VaultSourceID): VaultSourceDescription {
const vaultManager = getVaultManager();
const source = vaultManager.getSourceForID(sourceID);
Expand Down

0 comments on commit b053a49

Please sign in to comment.