diff --git a/Cargo.lock b/Cargo.lock index 355fef35d..ef6b70b3d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4829,6 +4829,7 @@ dependencies = [ "tauri-plugin-dialog", "tauri-plugin-export", "tauri-plugin-extensions", + "tauri-plugin-folder", "tauri-plugin-fs", "tauri-plugin-hooks", "tauri-plugin-http", @@ -18244,6 +18245,24 @@ dependencies = [ "tracing", ] +[[package]] +name = "tauri-plugin-folder" +version = "0.1.0" +dependencies = [ + "serde", + "specta", + "specta-typescript", + "tauri", + "tauri-plugin", + "tauri-plugin-path2", + "tauri-specta", + "tempfile", + "thiserror 2.0.17", + "tokio", + "tracing", + "uuid", +] + [[package]] name = "tauri-plugin-fs" version = "2.4.4" @@ -18386,6 +18405,7 @@ dependencies = [ "strum 0.26.3", "tauri", "tauri-plugin", + "tauri-plugin-folder", "tauri-plugin-hooks", "tauri-plugin-local-stt", "tauri-plugin-tray", @@ -18531,6 +18551,7 @@ dependencies = [ "sysinfo 0.37.2", "tauri", "tauri-plugin", + "tauri-plugin-folder", "tauri-plugin-opener", "tauri-plugin-path2", "tauri-specta", @@ -18593,6 +18614,7 @@ dependencies = [ "specta-typescript", "tauri", "tauri-plugin", + "tauri-plugin-folder", "tauri-plugin-path2", "tauri-specta", "thiserror 2.0.17", diff --git a/Cargo.toml b/Cargo.toml index 32642295d..3238f7e9a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -139,6 +139,7 @@ tauri-plugin-deeplink2 = { path = "plugins/deeplink2" } tauri-plugin-detect = { path = "plugins/detect" } tauri-plugin-export = { path = "plugins/export" } tauri-plugin-extensions = { path = "plugins/extensions" } +tauri-plugin-folder = { path = "plugins/folder" } tauri-plugin-hooks = { path = "plugins/hooks" } tauri-plugin-icon = { path = "plugins/icon" } tauri-plugin-importer = { path = "plugins/importer" } diff --git a/apps/desktop/package.json b/apps/desktop/package.json index 1305a2693..287203425 100644 --- a/apps/desktop/package.json +++ b/apps/desktop/package.json @@ -37,6 +37,7 @@ "@hypr/plugin-deeplink2": "workspace:*", "@hypr/plugin-detect": "workspace:*", "@hypr/plugin-export": "workspace:*", + "@hypr/plugin-folder": "workspace:*", "@hypr/plugin-extensions": "workspace:*", "@hypr/plugin-hooks": "workspace:*", "@hypr/plugin-icon": "workspace:*", diff --git a/apps/desktop/src-tauri/Cargo.toml b/apps/desktop/src-tauri/Cargo.toml index 577179803..c4b8f1d3c 100644 --- a/apps/desktop/src-tauri/Cargo.toml +++ b/apps/desktop/src-tauri/Cargo.toml @@ -36,6 +36,7 @@ tauri-plugin-detect = { workspace = true } tauri-plugin-dialog = { workspace = true } tauri-plugin-export = { workspace = true } tauri-plugin-extensions = { workspace = true } +tauri-plugin-folder = { workspace = true } tauri-plugin-fs = { workspace = true } tauri-plugin-hooks = { workspace = true } tauri-plugin-http = { workspace = true } diff --git a/apps/desktop/src-tauri/capabilities/default.json b/apps/desktop/src-tauri/capabilities/default.json index 536f2a09a..b5f74603f 100644 --- a/apps/desktop/src-tauri/capabilities/default.json +++ b/apps/desktop/src-tauri/capabilities/default.json @@ -194,6 +194,7 @@ "os:default", "detect:default", "export:default", + "folder:default", "permissions:default", "settings:default", "sfx:default", diff --git a/apps/desktop/src-tauri/src/lib.rs b/apps/desktop/src-tauri/src/lib.rs index ae507432a..875570c74 100644 --- a/apps/desktop/src-tauri/src/lib.rs +++ b/apps/desktop/src-tauri/src/lib.rs @@ -95,6 +95,7 @@ pub async fn main() { .plugin(tauri_plugin_deep_link::init()) .plugin(tauri_plugin_deeplink2::init()) .plugin(tauri_plugin_export::init()) + .plugin(tauri_plugin_folder::init()) .plugin(tauri_plugin_os::init()) .plugin(tauri_plugin_fs::init()) .plugin(tauri_plugin_path2::init()) diff --git a/apps/desktop/src/components/main/body/sessions/outer-header/shared/folder.tsx b/apps/desktop/src/components/main/body/sessions/outer-header/shared/folder.tsx index 0909ead71..6bac48ac8 100644 --- a/apps/desktop/src/components/main/body/sessions/outer-header/shared/folder.tsx +++ b/apps/desktop/src/components/main/body/sessions/outer-header/shared/folder.tsx @@ -1,6 +1,7 @@ import { FolderIcon } from "lucide-react"; -import { type ReactNode, useState } from "react"; +import { type ReactNode, useCallback, useState } from "react"; +import { commands as folderCommands } from "@hypr/plugin-folder"; import { Command, CommandEmpty, @@ -31,13 +32,7 @@ export function SearchableFolderDropdown({ main.STORE_ID, ); - const handleSelectFolder = main.UI.useSetPartialRowCallback( - "sessions", - sessionId, - (folderId: string) => ({ folder_id: folderId }), - [], - main.STORE_ID, - ); + const handleSelectFolder = useMoveSessionToFolder(sessionId); return ( @@ -71,13 +66,7 @@ export function SearchableFolderSubmenuContent({ main.STORE_ID, ); - const handleSelectFolder = main.UI.useSetPartialRowCallback( - "sessions", - sessionId, - (folderId: string) => ({ folder_id: folderId }), - [], - main.STORE_ID, - ); + const handleSelectFolder = useMoveSessionToFolder(sessionId); return ( @@ -102,11 +91,11 @@ function SearchableFolderContent({ setOpen, }: { folders: Record; - onSelectFolder: (folderId: string) => void; + onSelectFolder: (folderId: string) => Promise; setOpen?: (open: boolean) => void; }) { - const handleSelect = (folderId: string) => { - onSelectFolder(folderId); + const handleSelect = async (folderId: string) => { + await onSelectFolder(folderId); setOpen?.(false); }; @@ -131,3 +120,21 @@ function SearchableFolderContent({ ); } + +function useMoveSessionToFolder(sessionId: string) { + const setFolderId = main.UI.useSetPartialRowCallback( + "sessions", + sessionId, + (folderId: string) => ({ folder_id: folderId }), + [], + main.STORE_ID, + ); + + return useCallback( + async (targetFolderId: string) => { + await folderCommands.moveSession(sessionId, targetFolderId); + setFolderId(targetFolderId); + }, + [sessionId, setFolderId], + ); +} diff --git a/apps/desktop/src/store/tinybase/persister/folder.ts b/apps/desktop/src/store/tinybase/persister/folder.ts new file mode 100644 index 000000000..4faf43b58 --- /dev/null +++ b/apps/desktop/src/store/tinybase/persister/folder.ts @@ -0,0 +1,176 @@ +import { exists, readDir } from "@tauri-apps/plugin-fs"; +import { createCustomPersister } from "tinybase/persisters/with-schemas"; +import type { + Content, + MergeableStore, + OptionalSchemas, +} from "tinybase/with-schemas"; + +import { + commands as notifyCommands, + events as notifyEvents, +} from "@hypr/plugin-notify"; +import { commands as path2Commands } from "@hypr/plugin-path2"; +import type { FolderStorage } from "@hypr/store"; + +import { DEFAULT_USER_ID } from "../../../utils"; +import { StoreOrMergeableStore } from "../store/shared"; +import { getParentFolderPath, type PersisterMode } from "./utils"; + +type FoldersJson = Record; + +interface ScanResult { + folders: FoldersJson; + sessionFolderMap: Map; +} + +async function getSessionsDir(): Promise { + const base = await path2Commands.base(); + return `${base}/sessions`; +} + +async function scanDirectoryRecursively( + sessionsDir: string, + currentPath: string = "", +): Promise { + const folders: FoldersJson = {}; + const sessionFolderMap = new Map(); + + const fullPath = currentPath ? `${sessionsDir}/${currentPath}` : sessionsDir; + + try { + const entries = await readDir(fullPath); + + for (const entry of entries) { + if (!entry.isDirectory) { + continue; + } + + const entryPath = currentPath + ? `${currentPath}/${entry.name}` + : entry.name; + + const hasMemoMd = await exists(`${sessionsDir}/${entryPath}/_memo.md`); + + if (hasMemoMd) { + const folderPath = currentPath === "_default" ? "" : currentPath; + sessionFolderMap.set(entry.name, folderPath); + } else { + if (entry.name !== "_default") { + folders[entryPath] = { + user_id: DEFAULT_USER_ID, + created_at: new Date().toISOString(), + name: entry.name, + parent_folder_id: getParentFolderPath(entryPath), + }; + } + + const subResult = await scanDirectoryRecursively( + sessionsDir, + entryPath, + ); + + for (const [id, folder] of Object.entries(subResult.folders)) { + folders[id] = folder; + } + for (const [sessionId, folderPath] of subResult.sessionFolderMap) { + sessionFolderMap.set(sessionId, folderPath); + } + } + } + } catch (error) { + const errorStr = String(error); + if ( + !errorStr.includes("No such file or directory") && + !errorStr.includes("ENOENT") && + !errorStr.includes("not found") + ) { + console.error("[FolderPersister] scan error:", error); + } + } + + return { folders, sessionFolderMap }; +} + +export function jsonToContent( + data: FoldersJson, +): Content { + return [{ folders: data }, {}] as unknown as Content; +} + +export function createFolderPersister( + store: MergeableStore, + config: { mode: PersisterMode } = { mode: "load-only" }, +) { + const loadFn = + config.mode === "save-only" + ? async (): Promise | undefined> => undefined + : async (): Promise | undefined> => { + try { + const sessionsDir = await getSessionsDir(); + const dirExists = await exists(sessionsDir); + + if (!dirExists) { + return jsonToContent({}); + } + + const { folders, sessionFolderMap } = + await scanDirectoryRecursively(sessionsDir); + + for (const [sessionId, folderPath] of sessionFolderMap) { + // @ts-ignore - we're setting cells on the sessions table + if (store.hasRow("sessions", sessionId)) { + // @ts-ignore + store.setCell("sessions", sessionId, "folder_id", folderPath); + } + } + + return jsonToContent(folders); + } catch (error) { + console.error("[FolderPersister] load error:", error); + return undefined; + } + }; + + const saveFn = async () => {}; + + return createCustomPersister( + store, + loadFn, + saveFn, + (listener) => setInterval(listener, 5000), + (handle) => clearInterval(handle), + (error) => console.error("[FolderPersister]:", error), + StoreOrMergeableStore, + ); +} + +interface Loadable { + load(): Promise; +} + +export async function startFolderWatcher( + persister: Loadable, +): Promise<() => void> { + const result = await notifyCommands.start(); + if (result.status === "error") { + console.error("[FolderWatcher] Failed to start:", result.error); + return () => {}; + } + + const unlisten = await notifyEvents.fileChanged.listen(async (event) => { + const path = event.payload.path; + if (path.startsWith("sessions/")) { + try { + await persister.load(); + } catch (error) { + console.error("[FolderWatcher] Failed to reload:", error); + } + } + }); + + return () => { + unlisten(); + void notifyCommands.stop(); + }; +} diff --git a/apps/desktop/src/store/tinybase/persister/local.test.ts b/apps/desktop/src/store/tinybase/persister/local.test.ts index 80e4701ca..428d2c4f9 100644 --- a/apps/desktop/src/store/tinybase/persister/local.test.ts +++ b/apps/desktop/src/store/tinybase/persister/local.test.ts @@ -89,7 +89,7 @@ describe("createNotePersister", () => { expect(commands.exportTiptapJsonToMdBatch).toHaveBeenCalledWith([ [ { type: "doc", content: [{ type: "paragraph" }] }, - "/mock/data/dir/hyprnote/sessions/session-1/My Template.md", + "/mock/data/dir/hyprnote/sessions/_default/session-1/My Template.md", ], ]); }); @@ -115,7 +115,7 @@ describe("createNotePersister", () => { expect(commands.exportTiptapJsonToMdBatch).toHaveBeenCalledWith([ [ { type: "doc", content: [{ type: "paragraph" }] }, - "/mock/data/dir/hyprnote/sessions/session-1/_summary.md", + "/mock/data/dir/hyprnote/sessions/_default/session-1/_summary.md", ], ]); }); @@ -199,7 +199,7 @@ describe("createNotePersister", () => { expect(commands.exportTiptapJsonToMdBatch).toHaveBeenCalledWith([ [ expect.any(Object), - "/mock/data/dir/hyprnote/sessions/session-1/My_________Template.md", + "/mock/data/dir/hyprnote/sessions/_default/session-1/My_________Template.md", ], ]); }); @@ -226,7 +226,7 @@ describe("createNotePersister", () => { expect(commands.exportTiptapJsonToMdBatch).toHaveBeenCalledWith([ [ expect.any(Object), - "/mock/data/dir/hyprnote/sessions/session-1/unknown-template-id.md", + "/mock/data/dir/hyprnote/sessions/_default/session-1/unknown-template-id.md", ], ]); }); @@ -292,7 +292,7 @@ describe("createNotePersister", () => { expect(commands.exportTiptapJsonToMdBatch).toHaveBeenCalledWith([ [ { type: "doc", content: [{ type: "paragraph" }] }, - "/mock/data/dir/hyprnote/sessions/session-1/_memo.md", + "/mock/data/dir/hyprnote/sessions/_default/session-1/_memo.md", ], ]); }); @@ -338,7 +338,7 @@ describe("createNotePersister", () => { await persister.save(); expect(mkdir).toHaveBeenCalledWith( - "/mock/data/dir/hyprnote/sessions/session-1", + "/mock/data/dir/hyprnote/sessions/_default/session-1", { recursive: true }, ); }); diff --git a/apps/desktop/src/store/tinybase/persister/note.test.ts b/apps/desktop/src/store/tinybase/persister/note.test.ts index 80e4701ca..428d2c4f9 100644 --- a/apps/desktop/src/store/tinybase/persister/note.test.ts +++ b/apps/desktop/src/store/tinybase/persister/note.test.ts @@ -89,7 +89,7 @@ describe("createNotePersister", () => { expect(commands.exportTiptapJsonToMdBatch).toHaveBeenCalledWith([ [ { type: "doc", content: [{ type: "paragraph" }] }, - "/mock/data/dir/hyprnote/sessions/session-1/My Template.md", + "/mock/data/dir/hyprnote/sessions/_default/session-1/My Template.md", ], ]); }); @@ -115,7 +115,7 @@ describe("createNotePersister", () => { expect(commands.exportTiptapJsonToMdBatch).toHaveBeenCalledWith([ [ { type: "doc", content: [{ type: "paragraph" }] }, - "/mock/data/dir/hyprnote/sessions/session-1/_summary.md", + "/mock/data/dir/hyprnote/sessions/_default/session-1/_summary.md", ], ]); }); @@ -199,7 +199,7 @@ describe("createNotePersister", () => { expect(commands.exportTiptapJsonToMdBatch).toHaveBeenCalledWith([ [ expect.any(Object), - "/mock/data/dir/hyprnote/sessions/session-1/My_________Template.md", + "/mock/data/dir/hyprnote/sessions/_default/session-1/My_________Template.md", ], ]); }); @@ -226,7 +226,7 @@ describe("createNotePersister", () => { expect(commands.exportTiptapJsonToMdBatch).toHaveBeenCalledWith([ [ expect.any(Object), - "/mock/data/dir/hyprnote/sessions/session-1/unknown-template-id.md", + "/mock/data/dir/hyprnote/sessions/_default/session-1/unknown-template-id.md", ], ]); }); @@ -292,7 +292,7 @@ describe("createNotePersister", () => { expect(commands.exportTiptapJsonToMdBatch).toHaveBeenCalledWith([ [ { type: "doc", content: [{ type: "paragraph" }] }, - "/mock/data/dir/hyprnote/sessions/session-1/_memo.md", + "/mock/data/dir/hyprnote/sessions/_default/session-1/_memo.md", ], ]); }); @@ -338,7 +338,7 @@ describe("createNotePersister", () => { await persister.save(); expect(mkdir).toHaveBeenCalledWith( - "/mock/data/dir/hyprnote/sessions/session-1", + "/mock/data/dir/hyprnote/sessions/_default/session-1", { recursive: true }, ); }); diff --git a/apps/desktop/src/store/tinybase/persister/note.ts b/apps/desktop/src/store/tinybase/persister/note.ts index 9bedd6ff3..57b10e606 100644 --- a/apps/desktop/src/store/tinybase/persister/note.ts +++ b/apps/desktop/src/store/tinybase/persister/note.ts @@ -127,7 +127,13 @@ function collectEnhancedNoteBatchItems( handleSyncToSession(enhancedNote.session_id, enhancedNote.content); } - const sessionDir = getSessionDir(dataDir, enhancedNote.session_id); + const session = tables?.sessions?.[enhancedNote.session_id]; + const folderPath = session?.folder_id ?? ""; + const sessionDir = getSessionDir( + dataDir, + enhancedNote.session_id, + folderPath, + ); dirs.add(sessionDir); items.push([parsed, `${sessionDir}/${filename}`]); } @@ -152,7 +158,8 @@ function collectSessionBatchItems( continue; } - const sessionDir = getSessionDir(dataDir, session.id); + const folderPath = session.folder_id ?? ""; + const sessionDir = getSessionDir(dataDir, session.id, folderPath); dirs.add(sessionDir); items.push([parsed, `${sessionDir}/_memo.md`]); } diff --git a/apps/desktop/src/store/tinybase/persister/transcript.test.ts b/apps/desktop/src/store/tinybase/persister/transcript.test.ts index aa6eaf488..5a0e6c7f8 100644 --- a/apps/desktop/src/store/tinybase/persister/transcript.test.ts +++ b/apps/desktop/src/store/tinybase/persister/transcript.test.ts @@ -100,7 +100,7 @@ describe("createTranscriptPersister", () => { expect(writeTextFile).toHaveBeenCalledTimes(1); expect(writeTextFile).toHaveBeenCalledWith( - "/mock/data/dir/hyprnote/sessions/session-1/_transcript.json", + "/mock/data/dir/hyprnote/sessions/_default/session-1/_transcript.json", expect.any(String), ); @@ -179,10 +179,10 @@ describe("createTranscriptPersister", () => { const paths = vi.mocked(writeTextFile).mock.calls.map((call) => call[0]); expect(paths).toContain( - "/mock/data/dir/hyprnote/sessions/session-1/_transcript.json", + "/mock/data/dir/hyprnote/sessions/_default/session-1/_transcript.json", ); expect(paths).toContain( - "/mock/data/dir/hyprnote/sessions/session-2/_transcript.json", + "/mock/data/dir/hyprnote/sessions/_default/session-2/_transcript.json", ); }); @@ -202,7 +202,7 @@ describe("createTranscriptPersister", () => { await persister.save(); expect(mkdir).toHaveBeenCalledWith( - "/mock/data/dir/hyprnote/sessions/session-1", + "/mock/data/dir/hyprnote/sessions/_default/session-1", { recursive: true }, ); }); diff --git a/apps/desktop/src/store/tinybase/persister/transcript.ts b/apps/desktop/src/store/tinybase/persister/transcript.ts index bd0f0a8ca..06b93e7e9 100644 --- a/apps/desktop/src/store/tinybase/persister/transcript.ts +++ b/apps/desktop/src/store/tinybase/persister/transcript.ts @@ -95,7 +95,9 @@ export function createTranscriptPersister( const writeOperations: Array<{ path: string; content: string }> = []; for (const [sessionId, transcripts] of transcriptsBySession) { - const sessionDir = getSessionDir(dataDir, sessionId); + const session = tables?.sessions?.[sessionId]; + const folderPath = session?.folder_id ?? ""; + const sessionDir = getSessionDir(dataDir, sessionId, folderPath); dirs.add(sessionDir); const json: TranscriptJson = { transcripts }; diff --git a/apps/desktop/src/store/tinybase/persister/utils.ts b/apps/desktop/src/store/tinybase/persister/utils.ts index ae4f2355b..3c689ae77 100644 --- a/apps/desktop/src/store/tinybase/persister/utils.ts +++ b/apps/desktop/src/store/tinybase/persister/utils.ts @@ -18,8 +18,13 @@ export async function getDataDir(): Promise { return path2Commands.base(); } -export function getSessionDir(dataDir: string, sessionId: string): string { - return `${dataDir}/sessions/${sessionId}`; +export function getSessionDir( + dataDir: string, + sessionId: string, + folderPath: string = "", +): string { + const folder = folderPath || "_default"; + return `${dataDir}/sessions/${folder}/${sessionId}`; } export function getChatDir(dataDir: string, chatGroupId: string): string { @@ -42,6 +47,15 @@ export function sanitizeFilename(name: string): string { return name.replace(/[<>:"/\\|?*]/g, "_").trim(); } +export function getParentFolderPath(folderPath: string): string { + if (!folderPath) { + return ""; + } + const parts = folderPath.split("/"); + parts.pop(); + return parts.join("/"); +} + export function safeParseJson( value: unknown, ): Record | undefined { diff --git a/apps/desktop/src/store/tinybase/store/persisters.ts b/apps/desktop/src/store/tinybase/store/persisters.ts index a88c29704..ec51820fe 100644 --- a/apps/desktop/src/store/tinybase/store/persisters.ts +++ b/apps/desktop/src/store/tinybase/store/persisters.ts @@ -9,6 +9,7 @@ import { type Schemas } from "@hypr/store"; import { DEFAULT_USER_ID } from "../../../utils"; import { createChatPersister } from "../persister/chat"; import { createEventPersister } from "../persister/events"; +import { createFolderPersister, startFolderWatcher } from "../persister/folder"; import { createHumanPersister } from "../persister/human"; import { createLocalPersister } from "../persister/local"; import { createNotePersister } from "../persister/note"; @@ -92,6 +93,28 @@ export function useMainPersisters( [persist], ); + const folderPersister = useCreatePersister( + store, + async (store) => { + if (!persist) { + return undefined; + } + + const persister = createFolderPersister(store as Store, { + mode: "load-only", + }); + await persister.load(); + await persister.startAutoLoad(); + + if (getCurrentWebviewWindowLabel() === "main") { + void startFolderWatcher(persister); + } + + return persister; + }, + [persist], + ); + const markdownPersister = useCreatePersister( store, async (store) => { @@ -214,6 +237,7 @@ export function useMainPersisters( const persisters = useMemo( () => localPersister && + folderPersister && markdownPersister && transcriptPersister && organizationPersister && @@ -222,6 +246,7 @@ export function useMainPersisters( chatPersister ? [ localPersister, + folderPersister, markdownPersister, transcriptPersister, organizationPersister, @@ -232,6 +257,7 @@ export function useMainPersisters( : null, [ localPersister, + folderPersister, markdownPersister, transcriptPersister, organizationPersister, @@ -245,6 +271,7 @@ export function useMainPersisters( return { localPersister, + folderPersister, markdownPersister, transcriptPersister, organizationPersister, diff --git a/plugins/folder/.gitignore b/plugins/folder/.gitignore new file mode 100644 index 000000000..50d8e32e8 --- /dev/null +++ b/plugins/folder/.gitignore @@ -0,0 +1,17 @@ +/.vs +.DS_Store +.Thumbs.db +*.sublime* +.idea/ +debug.log +package-lock.json +.vscode/settings.json +yarn.lock + +/.tauri +/target +Cargo.lock +node_modules/ + +dist-js +dist diff --git a/plugins/folder/Cargo.toml b/plugins/folder/Cargo.toml new file mode 100644 index 000000000..ba8f36721 --- /dev/null +++ b/plugins/folder/Cargo.toml @@ -0,0 +1,28 @@ +[package] +name = "tauri-plugin-folder" +version = "0.1.0" +authors = ["You"] +edition = "2024" +exclude = ["/js", "/node_modules"] +links = "tauri-plugin-folder" +description = "" + +[build-dependencies] +tauri-plugin = { workspace = true, features = ["build"] } + +[dev-dependencies] +specta-typescript = { workspace = true } +tempfile = "3" +tokio = { workspace = true, features = ["macros"] } + +[dependencies] +tauri = { workspace = true, features = ["test"] } +tauri-plugin-path2 = { path = "../path2" } +tauri-specta = { workspace = true, features = ["derive", "typescript"] } + +serde = { workspace = true } +specta = { workspace = true } + +thiserror = { workspace = true } +tracing = { workspace = true } +uuid = { workspace = true } diff --git a/plugins/folder/build.rs b/plugins/folder/build.rs new file mode 100644 index 000000000..cc22cedd8 --- /dev/null +++ b/plugins/folder/build.rs @@ -0,0 +1,11 @@ +const COMMANDS: &[&str] = &[ + "ping", + "move_session", + "create_folder", + "rename_folder", + "delete_folder", +]; + +fn main() { + tauri_plugin::Builder::new(COMMANDS).build(); +} diff --git a/plugins/folder/js/bindings.gen.ts b/plugins/folder/js/bindings.gen.ts new file mode 100644 index 000000000..acec2cbe4 --- /dev/null +++ b/plugins/folder/js/bindings.gen.ts @@ -0,0 +1,121 @@ +// @ts-nocheck + +// This file was generated by [tauri-specta](https://github.com/oscartbeaumont/tauri-specta). Do not edit this file manually. + +/** user-defined commands **/ + + +export const commands = { +async ping() : Promise> { + try { + return { status: "ok", data: await TAURI_INVOKE("plugin:folder|ping") }; +} catch (e) { + if(e instanceof Error) throw e; + else return { status: "error", error: e as any }; +} +}, +async moveSession(sessionId: string, targetFolderPath: string) : Promise> { + try { + return { status: "ok", data: await TAURI_INVOKE("plugin:folder|move_session", { sessionId, targetFolderPath }) }; +} catch (e) { + if(e instanceof Error) throw e; + else return { status: "error", error: e as any }; +} +}, +async createFolder(folderPath: string) : Promise> { + try { + return { status: "ok", data: await TAURI_INVOKE("plugin:folder|create_folder", { folderPath }) }; +} catch (e) { + if(e instanceof Error) throw e; + else return { status: "error", error: e as any }; +} +}, +async renameFolder(oldPath: string, newPath: string) : Promise> { + try { + return { status: "ok", data: await TAURI_INVOKE("plugin:folder|rename_folder", { oldPath, newPath }) }; +} catch (e) { + if(e instanceof Error) throw e; + else return { status: "error", error: e as any }; +} +}, +async deleteFolder(folderPath: string) : Promise> { + try { + return { status: "ok", data: await TAURI_INVOKE("plugin:folder|delete_folder", { folderPath }) }; +} catch (e) { + if(e instanceof Error) throw e; + else return { status: "error", error: e as any }; +} +} +} + +/** user-defined events **/ + + + +/** user-defined constants **/ + + + +/** user-defined types **/ + + + +/** tauri-specta globals **/ + +import { + invoke as TAURI_INVOKE, + Channel as TAURI_CHANNEL, +} from "@tauri-apps/api/core"; +import * as TAURI_API_EVENT from "@tauri-apps/api/event"; +import { type WebviewWindow as __WebviewWindow__ } from "@tauri-apps/api/webviewWindow"; + +type __EventObj__ = { + listen: ( + cb: TAURI_API_EVENT.EventCallback, + ) => ReturnType>; + once: ( + cb: TAURI_API_EVENT.EventCallback, + ) => ReturnType>; + emit: null extends T + ? (payload?: T) => ReturnType + : (payload: T) => ReturnType; +}; + +export type Result = + | { status: "ok"; data: T } + | { status: "error"; error: E }; + +function __makeEvents__>( + mappings: Record, +) { + return new Proxy( + {} as unknown as { + [K in keyof T]: __EventObj__ & { + (handle: __WebviewWindow__): __EventObj__; + }; + }, + { + get: (_, event) => { + const name = mappings[event as keyof T]; + + return new Proxy((() => {}) as any, { + apply: (_, __, [window]: [__WebviewWindow__]) => ({ + listen: (arg: any) => window.listen(name, arg), + once: (arg: any) => window.once(name, arg), + emit: (arg: any) => window.emit(name, arg), + }), + get: (_, command: keyof __EventObj__) => { + switch (command) { + case "listen": + return (arg: any) => TAURI_API_EVENT.listen(name, arg); + case "once": + return (arg: any) => TAURI_API_EVENT.once(name, arg); + case "emit": + return (arg: any) => TAURI_API_EVENT.emit(name, arg); + } + }, + }); + }, + }, + ); +} diff --git a/plugins/folder/js/index.ts b/plugins/folder/js/index.ts new file mode 100644 index 000000000..a96e122f0 --- /dev/null +++ b/plugins/folder/js/index.ts @@ -0,0 +1 @@ +export * from "./bindings.gen"; diff --git a/plugins/folder/package.json b/plugins/folder/package.json new file mode 100644 index 000000000..669b5ee0b --- /dev/null +++ b/plugins/folder/package.json @@ -0,0 +1,11 @@ +{ + "name": "@hypr/plugin-folder", + "private": true, + "main": "./js/index.ts", + "scripts": { + "codegen": "cargo test -p tauri-plugin-folder" + }, + "dependencies": { + "@tauri-apps/api": "^2.9.1" + } +} diff --git a/plugins/folder/permissions/autogenerated/commands/create_folder.toml b/plugins/folder/permissions/autogenerated/commands/create_folder.toml new file mode 100644 index 000000000..2a8539994 --- /dev/null +++ b/plugins/folder/permissions/autogenerated/commands/create_folder.toml @@ -0,0 +1,13 @@ +# Automatically generated - DO NOT EDIT! + +"$schema" = "../../schemas/schema.json" + +[[permission]] +identifier = "allow-create-folder" +description = "Enables the create_folder command without any pre-configured scope." +commands.allow = ["create_folder"] + +[[permission]] +identifier = "deny-create-folder" +description = "Denies the create_folder command without any pre-configured scope." +commands.deny = ["create_folder"] diff --git a/plugins/folder/permissions/autogenerated/commands/delete_folder.toml b/plugins/folder/permissions/autogenerated/commands/delete_folder.toml new file mode 100644 index 000000000..9e3a0504e --- /dev/null +++ b/plugins/folder/permissions/autogenerated/commands/delete_folder.toml @@ -0,0 +1,13 @@ +# Automatically generated - DO NOT EDIT! + +"$schema" = "../../schemas/schema.json" + +[[permission]] +identifier = "allow-delete-folder" +description = "Enables the delete_folder command without any pre-configured scope." +commands.allow = ["delete_folder"] + +[[permission]] +identifier = "deny-delete-folder" +description = "Denies the delete_folder command without any pre-configured scope." +commands.deny = ["delete_folder"] diff --git a/plugins/folder/permissions/autogenerated/commands/move_session.toml b/plugins/folder/permissions/autogenerated/commands/move_session.toml new file mode 100644 index 000000000..690f47df0 --- /dev/null +++ b/plugins/folder/permissions/autogenerated/commands/move_session.toml @@ -0,0 +1,13 @@ +# Automatically generated - DO NOT EDIT! + +"$schema" = "../../schemas/schema.json" + +[[permission]] +identifier = "allow-move-session" +description = "Enables the move_session command without any pre-configured scope." +commands.allow = ["move_session"] + +[[permission]] +identifier = "deny-move-session" +description = "Denies the move_session command without any pre-configured scope." +commands.deny = ["move_session"] diff --git a/plugins/folder/permissions/autogenerated/commands/ping.toml b/plugins/folder/permissions/autogenerated/commands/ping.toml new file mode 100644 index 000000000..1d1358807 --- /dev/null +++ b/plugins/folder/permissions/autogenerated/commands/ping.toml @@ -0,0 +1,13 @@ +# Automatically generated - DO NOT EDIT! + +"$schema" = "../../schemas/schema.json" + +[[permission]] +identifier = "allow-ping" +description = "Enables the ping command without any pre-configured scope." +commands.allow = ["ping"] + +[[permission]] +identifier = "deny-ping" +description = "Denies the ping command without any pre-configured scope." +commands.deny = ["ping"] diff --git a/plugins/folder/permissions/autogenerated/commands/rename_folder.toml b/plugins/folder/permissions/autogenerated/commands/rename_folder.toml new file mode 100644 index 000000000..9853ca0c1 --- /dev/null +++ b/plugins/folder/permissions/autogenerated/commands/rename_folder.toml @@ -0,0 +1,13 @@ +# Automatically generated - DO NOT EDIT! + +"$schema" = "../../schemas/schema.json" + +[[permission]] +identifier = "allow-rename-folder" +description = "Enables the rename_folder command without any pre-configured scope." +commands.allow = ["rename_folder"] + +[[permission]] +identifier = "deny-rename-folder" +description = "Denies the rename_folder command without any pre-configured scope." +commands.deny = ["rename_folder"] diff --git a/plugins/folder/permissions/autogenerated/reference.md b/plugins/folder/permissions/autogenerated/reference.md new file mode 100644 index 000000000..938272ce8 --- /dev/null +++ b/plugins/folder/permissions/autogenerated/reference.md @@ -0,0 +1,151 @@ +## Default Permission + +Default permissions for the plugin + +#### This default permission set includes the following: + +- `allow-ping` +- `allow-move-session` +- `allow-create-folder` +- `allow-rename-folder` +- `allow-delete-folder` + +## Permission Table + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
IdentifierDescription
+ +`folder:allow-create-folder` + + + +Enables the create_folder command without any pre-configured scope. + +
+ +`folder:deny-create-folder` + + + +Denies the create_folder command without any pre-configured scope. + +
+ +`folder:allow-delete-folder` + + + +Enables the delete_folder command without any pre-configured scope. + +
+ +`folder:deny-delete-folder` + + + +Denies the delete_folder command without any pre-configured scope. + +
+ +`folder:allow-move-session` + + + +Enables the move_session command without any pre-configured scope. + +
+ +`folder:deny-move-session` + + + +Denies the move_session command without any pre-configured scope. + +
+ +`folder:allow-ping` + + + +Enables the ping command without any pre-configured scope. + +
+ +`folder:deny-ping` + + + +Denies the ping command without any pre-configured scope. + +
+ +`folder:allow-rename-folder` + + + +Enables the rename_folder command without any pre-configured scope. + +
+ +`folder:deny-rename-folder` + + + +Denies the rename_folder command without any pre-configured scope. + +
diff --git a/plugins/folder/permissions/default.toml b/plugins/folder/permissions/default.toml new file mode 100644 index 000000000..4aee22fc9 --- /dev/null +++ b/plugins/folder/permissions/default.toml @@ -0,0 +1,9 @@ +[default] +description = "Default permissions for the plugin" +permissions = [ + "allow-ping", + "allow-move-session", + "allow-create-folder", + "allow-rename-folder", + "allow-delete-folder", +] diff --git a/plugins/folder/permissions/schemas/schema.json b/plugins/folder/permissions/schemas/schema.json new file mode 100644 index 000000000..93b9b0e36 --- /dev/null +++ b/plugins/folder/permissions/schemas/schema.json @@ -0,0 +1,366 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "PermissionFile", + "description": "Permission file that can define a default permission, a set of permissions or a list of inlined permissions.", + "type": "object", + "properties": { + "default": { + "description": "The default permission set for the plugin", + "anyOf": [ + { + "$ref": "#/definitions/DefaultPermission" + }, + { + "type": "null" + } + ] + }, + "set": { + "description": "A list of permissions sets defined", + "type": "array", + "items": { + "$ref": "#/definitions/PermissionSet" + } + }, + "permission": { + "description": "A list of inlined permissions", + "default": [], + "type": "array", + "items": { + "$ref": "#/definitions/Permission" + } + } + }, + "definitions": { + "DefaultPermission": { + "description": "The default permission set of the plugin.\n\nWorks similarly to a permission with the \"default\" identifier.", + "type": "object", + "required": [ + "permissions" + ], + "properties": { + "version": { + "description": "The version of the permission.", + "type": [ + "integer", + "null" + ], + "format": "uint64", + "minimum": 1.0 + }, + "description": { + "description": "Human-readable description of what the permission does. Tauri convention is to use `

` headings in markdown content for Tauri documentation generation purposes.", + "type": [ + "string", + "null" + ] + }, + "permissions": { + "description": "All permissions this set contains.", + "type": "array", + "items": { + "type": "string" + } + } + } + }, + "PermissionSet": { + "description": "A set of direct permissions grouped together under a new name.", + "type": "object", + "required": [ + "description", + "identifier", + "permissions" + ], + "properties": { + "identifier": { + "description": "A unique identifier for the permission.", + "type": "string" + }, + "description": { + "description": "Human-readable description of what the permission does.", + "type": "string" + }, + "permissions": { + "description": "All permissions this set contains.", + "type": "array", + "items": { + "$ref": "#/definitions/PermissionKind" + } + } + } + }, + "Permission": { + "description": "Descriptions of explicit privileges of commands.\n\nIt can enable commands to be accessible in the frontend of the application.\n\nIf the scope is defined it can be used to fine grain control the access of individual or multiple commands.", + "type": "object", + "required": [ + "identifier" + ], + "properties": { + "version": { + "description": "The version of the permission.", + "type": [ + "integer", + "null" + ], + "format": "uint64", + "minimum": 1.0 + }, + "identifier": { + "description": "A unique identifier for the permission.", + "type": "string" + }, + "description": { + "description": "Human-readable description of what the permission does. Tauri internal convention is to use `

` headings in markdown content for Tauri documentation generation purposes.", + "type": [ + "string", + "null" + ] + }, + "commands": { + "description": "Allowed or denied commands when using this permission.", + "default": { + "allow": [], + "deny": [] + }, + "allOf": [ + { + "$ref": "#/definitions/Commands" + } + ] + }, + "scope": { + "description": "Allowed or denied scoped when using this permission.", + "allOf": [ + { + "$ref": "#/definitions/Scopes" + } + ] + }, + "platforms": { + "description": "Target platforms this permission applies. By default all platforms are affected by this permission.", + "type": [ + "array", + "null" + ], + "items": { + "$ref": "#/definitions/Target" + } + } + } + }, + "Commands": { + "description": "Allowed and denied commands inside a permission.\n\nIf two commands clash inside of `allow` and `deny`, it should be denied by default.", + "type": "object", + "properties": { + "allow": { + "description": "Allowed command.", + "default": [], + "type": "array", + "items": { + "type": "string" + } + }, + "deny": { + "description": "Denied command, which takes priority.", + "default": [], + "type": "array", + "items": { + "type": "string" + } + } + } + }, + "Scopes": { + "description": "An argument for fine grained behavior control of Tauri commands.\n\nIt can be of any serde serializable type and is used to allow or prevent certain actions inside a Tauri command. The configured scope is passed to the command and will be enforced by the command implementation.\n\n## Example\n\n```json { \"allow\": [{ \"path\": \"$HOME/**\" }], \"deny\": [{ \"path\": \"$HOME/secret.txt\" }] } ```", + "type": "object", + "properties": { + "allow": { + "description": "Data that defines what is allowed by the scope.", + "type": [ + "array", + "null" + ], + "items": { + "$ref": "#/definitions/Value" + } + }, + "deny": { + "description": "Data that defines what is denied by the scope. This should be prioritized by validation logic.", + "type": [ + "array", + "null" + ], + "items": { + "$ref": "#/definitions/Value" + } + } + } + }, + "Value": { + "description": "All supported ACL values.", + "anyOf": [ + { + "description": "Represents a null JSON value.", + "type": "null" + }, + { + "description": "Represents a [`bool`].", + "type": "boolean" + }, + { + "description": "Represents a valid ACL [`Number`].", + "allOf": [ + { + "$ref": "#/definitions/Number" + } + ] + }, + { + "description": "Represents a [`String`].", + "type": "string" + }, + { + "description": "Represents a list of other [`Value`]s.", + "type": "array", + "items": { + "$ref": "#/definitions/Value" + } + }, + { + "description": "Represents a map of [`String`] keys to [`Value`]s.", + "type": "object", + "additionalProperties": { + "$ref": "#/definitions/Value" + } + } + ] + }, + "Number": { + "description": "A valid ACL number.", + "anyOf": [ + { + "description": "Represents an [`i64`].", + "type": "integer", + "format": "int64" + }, + { + "description": "Represents a [`f64`].", + "type": "number", + "format": "double" + } + ] + }, + "Target": { + "description": "Platform target.", + "oneOf": [ + { + "description": "MacOS.", + "type": "string", + "enum": [ + "macOS" + ] + }, + { + "description": "Windows.", + "type": "string", + "enum": [ + "windows" + ] + }, + { + "description": "Linux.", + "type": "string", + "enum": [ + "linux" + ] + }, + { + "description": "Android.", + "type": "string", + "enum": [ + "android" + ] + }, + { + "description": "iOS.", + "type": "string", + "enum": [ + "iOS" + ] + } + ] + }, + "PermissionKind": { + "type": "string", + "oneOf": [ + { + "description": "Enables the create_folder command without any pre-configured scope.", + "type": "string", + "const": "allow-create-folder", + "markdownDescription": "Enables the create_folder command without any pre-configured scope." + }, + { + "description": "Denies the create_folder command without any pre-configured scope.", + "type": "string", + "const": "deny-create-folder", + "markdownDescription": "Denies the create_folder command without any pre-configured scope." + }, + { + "description": "Enables the delete_folder command without any pre-configured scope.", + "type": "string", + "const": "allow-delete-folder", + "markdownDescription": "Enables the delete_folder command without any pre-configured scope." + }, + { + "description": "Denies the delete_folder command without any pre-configured scope.", + "type": "string", + "const": "deny-delete-folder", + "markdownDescription": "Denies the delete_folder command without any pre-configured scope." + }, + { + "description": "Enables the move_session command without any pre-configured scope.", + "type": "string", + "const": "allow-move-session", + "markdownDescription": "Enables the move_session command without any pre-configured scope." + }, + { + "description": "Denies the move_session command without any pre-configured scope.", + "type": "string", + "const": "deny-move-session", + "markdownDescription": "Denies the move_session command without any pre-configured scope." + }, + { + "description": "Enables the ping command without any pre-configured scope.", + "type": "string", + "const": "allow-ping", + "markdownDescription": "Enables the ping command without any pre-configured scope." + }, + { + "description": "Denies the ping command without any pre-configured scope.", + "type": "string", + "const": "deny-ping", + "markdownDescription": "Denies the ping command without any pre-configured scope." + }, + { + "description": "Enables the rename_folder command without any pre-configured scope.", + "type": "string", + "const": "allow-rename-folder", + "markdownDescription": "Enables the rename_folder command without any pre-configured scope." + }, + { + "description": "Denies the rename_folder command without any pre-configured scope.", + "type": "string", + "const": "deny-rename-folder", + "markdownDescription": "Denies the rename_folder command without any pre-configured scope." + }, + { + "description": "Default permissions for the plugin\n#### This default permission set includes:\n\n- `allow-ping`\n- `allow-move-session`\n- `allow-create-folder`\n- `allow-rename-folder`\n- `allow-delete-folder`", + "type": "string", + "const": "default", + "markdownDescription": "Default permissions for the plugin\n#### This default permission set includes:\n\n- `allow-ping`\n- `allow-move-session`\n- `allow-create-folder`\n- `allow-rename-folder`\n- `allow-delete-folder`" + } + ] + } + } +} \ No newline at end of file diff --git a/plugins/folder/src/commands.rs b/plugins/folder/src/commands.rs new file mode 100644 index 000000000..1e08076d6 --- /dev/null +++ b/plugins/folder/src/commands.rs @@ -0,0 +1,53 @@ +use crate::FolderPluginExt; + +#[tauri::command] +#[specta::specta] +pub(crate) async fn ping(app: tauri::AppHandle) -> Result { + app.folder().ping().map_err(|e| e.to_string()) +} + +#[tauri::command] +#[specta::specta] +pub(crate) async fn move_session( + app: tauri::AppHandle, + session_id: String, + target_folder_path: String, +) -> Result<(), String> { + app.folder() + .move_session(&session_id, &target_folder_path) + .map_err(|e| e.to_string()) +} + +#[tauri::command] +#[specta::specta] +pub(crate) async fn create_folder( + app: tauri::AppHandle, + folder_path: String, +) -> Result<(), String> { + app.folder() + .create_folder(&folder_path) + .map_err(|e| e.to_string()) +} + +#[tauri::command] +#[specta::specta] +pub(crate) async fn rename_folder( + app: tauri::AppHandle, + old_path: String, + new_path: String, +) -> Result<(), String> { + app.folder() + .rename_folder(&old_path, &new_path) + .map_err(|e| e.to_string()) +} + +#[tauri::command] +#[specta::specta] +pub(crate) async fn delete_folder( + app: tauri::AppHandle, + folder_path: String, +) -> Result<(), String> { + app.folder() + .delete_folder(&folder_path) + .map_err(|e| e.to_string()) +} diff --git a/plugins/folder/src/error.rs b/plugins/folder/src/error.rs new file mode 100644 index 000000000..6b7a67d9d --- /dev/null +++ b/plugins/folder/src/error.rs @@ -0,0 +1,22 @@ +use serde::{Serialize, ser::Serializer}; + +pub type Result = std::result::Result; + +#[derive(Debug, thiserror::Error)] +pub enum Error { + #[error("Unknown error")] + Unknown, + #[error("IO error: {0}")] + Io(#[from] std::io::Error), + #[error("Path error: {0}")] + Path(String), +} + +impl Serialize for Error { + fn serialize(&self, serializer: S) -> std::result::Result + where + S: Serializer, + { + serializer.serialize_str(self.to_string().as_ref()) + } +} diff --git a/plugins/folder/src/ext.rs b/plugins/folder/src/ext.rs new file mode 100644 index 000000000..4655a2bc9 --- /dev/null +++ b/plugins/folder/src/ext.rs @@ -0,0 +1,289 @@ +use std::path::{Path, PathBuf}; + +use tauri_plugin_path2::Path2PluginExt; +use uuid::Uuid; + +pub fn is_uuid(name: &str) -> bool { + Uuid::try_parse(name).is_ok() +} + +pub fn find_session_dir(sessions_base: &Path, session_id: &str) -> PathBuf { + if let Some(found) = find_session_dir_recursive(sessions_base, session_id) { + return found; + } + sessions_base.join("_default").join(session_id) +} + +fn find_session_dir_recursive(dir: &Path, session_id: &str) -> Option { + let entries = std::fs::read_dir(dir).ok()?; + + for entry in entries.flatten() { + let path = entry.path(); + if !path.is_dir() { + continue; + } + + let name = path.file_name()?.to_str()?; + + if name == session_id { + return Some(path); + } + + if !is_uuid(name) { + if let Some(found) = find_session_dir_recursive(&path, session_id) { + return Some(found); + } + } + } + + None +} + +pub fn migrate_session_to_default(sessions_base: &Path, session_id: &str) { + let source = sessions_base.join(session_id); + if !source.is_dir() { + return; + } + + let default_dir = sessions_base.join("_default"); + if !default_dir.exists() { + if let Err(e) = std::fs::create_dir_all(&default_dir) { + tracing::warn!("Failed to create _default directory: {}", e); + return; + } + } + + let target = default_dir.join(session_id); + if target.exists() { + return; + } + + if let Err(e) = std::fs::rename(&source, &target) { + tracing::warn!("Failed to migrate {}: {}", session_id, e); + } else { + tracing::info!("Migrated session {} to _default", session_id); + } +} + +pub fn migrate_all_uuid_folders(sessions_base: &Path) { + let entries = match std::fs::read_dir(sessions_base) { + Ok(entries) => entries, + Err(_) => return, + }; + + let default_dir = sessions_base.join("_default"); + + for entry in entries.flatten() { + let path = entry.path(); + if !path.is_dir() { + continue; + } + + let name = match path.file_name().and_then(|n| n.to_str()) { + Some(name) => name, + None => continue, + }; + + if !is_uuid(name) { + continue; + } + + if !default_dir.exists() { + if let Err(e) = std::fs::create_dir_all(&default_dir) { + tracing::warn!("Failed to create _default directory: {}", e); + return; + } + } + + let new_path = default_dir.join(name); + if new_path.exists() { + tracing::warn!("Skipping migration of {}: already exists in _default", name); + continue; + } + + if let Err(e) = std::fs::rename(&path, &new_path) { + tracing::warn!("Failed to migrate {}: {}", name, e); + } else { + tracing::info!("Migrated session {} to _default", name); + } + } +} + +pub struct Folder<'a, R: tauri::Runtime, M: tauri::Manager> { + manager: &'a M, + _runtime: std::marker::PhantomData R>, +} + +impl<'a, R: tauri::Runtime, M: tauri::Manager> Folder<'a, R, M> { + pub fn ping(&self) -> Result { + Ok("pong".to_string()) + } + + fn sessions_dir(&self) -> Result { + let base = self + .manager + .app_handle() + .path2() + .base() + .map_err(|e| crate::Error::Path(e.to_string()))?; + Ok(base.join("sessions")) + } + + pub fn move_session( + &self, + session_id: &str, + target_folder_path: &str, + ) -> Result<(), crate::Error> { + let sessions_dir = self.sessions_dir()?; + let source = find_session_dir(&sessions_dir, session_id); + + if !source.exists() { + return Ok(()); + } + + let target_folder = if target_folder_path.is_empty() { + sessions_dir.join("_default") + } else { + sessions_dir.join(target_folder_path) + }; + let target = target_folder.join(session_id); + + if source == target { + return Ok(()); + } + + std::fs::create_dir_all(&target_folder)?; + std::fs::rename(&source, &target)?; + + tracing::info!( + "Moved session {} from {:?} to {:?}", + session_id, + source, + target + ); + + Ok(()) + } + + pub fn create_folder(&self, folder_path: &str) -> Result<(), crate::Error> { + let sessions_dir = self.sessions_dir()?; + let folder = sessions_dir.join(folder_path); + + if folder.exists() { + return Ok(()); + } + + std::fs::create_dir_all(&folder)?; + tracing::info!("Created folder: {:?}", folder); + Ok(()) + } + + pub fn rename_folder(&self, old_path: &str, new_path: &str) -> Result<(), crate::Error> { + let sessions_dir = self.sessions_dir()?; + let source = sessions_dir.join(old_path); + let target = sessions_dir.join(new_path); + + if !source.exists() { + return Err(crate::Error::Path(format!( + "Folder does not exist: {:?}", + source + ))); + } + + if target.exists() { + return Err(crate::Error::Path(format!( + "Target folder already exists: {:?}", + target + ))); + } + + if let Some(parent) = target.parent() { + std::fs::create_dir_all(parent)?; + } + + std::fs::rename(&source, &target)?; + tracing::info!("Renamed folder from {:?} to {:?}", source, target); + Ok(()) + } + + pub fn delete_folder(&self, folder_path: &str) -> Result<(), crate::Error> { + let sessions_dir = self.sessions_dir()?; + let folder = sessions_dir.join(folder_path); + + if !folder.exists() { + return Ok(()); + } + + std::fs::remove_dir_all(&folder)?; + tracing::info!("Deleted folder: {:?}", folder); + Ok(()) + } +} + +pub trait FolderPluginExt { + fn folder(&self) -> Folder<'_, R, Self> + where + Self: tauri::Manager + Sized; +} + +impl> FolderPluginExt for T { + fn folder(&self) -> Folder<'_, R, Self> + where + Self: Sized, + { + Folder { + manager: self, + _runtime: std::marker::PhantomData, + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use std::fs; + + #[test] + fn test_is_uuid() { + assert!(is_uuid("550e8400-e29b-41d4-a716-446655440000")); + assert!(is_uuid("550E8400-E29B-41D4-A716-446655440000")); + assert!(!is_uuid("_default")); + assert!(!is_uuid("work")); + assert!(!is_uuid("not-a-uuid")); + } + + #[test] + fn test_find_session_dir_in_default() { + let temp = tempfile::tempdir().unwrap(); + let sessions = temp.path().join("sessions"); + let session_id = "550e8400-e29b-41d4-a716-446655440000"; + let expected = sessions.join("_default").join(session_id); + fs::create_dir_all(&expected).unwrap(); + + let result = find_session_dir(&sessions, session_id); + assert_eq!(result, expected); + } + + #[test] + fn test_find_session_dir_in_subfolder() { + let temp = tempfile::tempdir().unwrap(); + let sessions = temp.path().join("sessions"); + let session_id = "550e8400-e29b-41d4-a716-446655440000"; + let expected = sessions.join("work").join("project").join(session_id); + fs::create_dir_all(&expected).unwrap(); + + let result = find_session_dir(&sessions, session_id); + assert_eq!(result, expected); + } + + #[test] + fn test_find_session_dir_fallback() { + let temp = tempfile::tempdir().unwrap(); + let sessions = temp.path().join("sessions"); + fs::create_dir_all(&sessions).unwrap(); + + let session_id = "550e8400-e29b-41d4-a716-446655440000"; + let result = find_session_dir(&sessions, session_id); + assert_eq!(result, sessions.join("_default").join(session_id)); + } +} diff --git a/plugins/folder/src/lib.rs b/plugins/folder/src/lib.rs new file mode 100644 index 000000000..4373466fd --- /dev/null +++ b/plugins/folder/src/lib.rs @@ -0,0 +1,51 @@ +mod commands; +mod error; +mod ext; + +pub use error::{Error, Result}; +pub use ext::*; + +const PLUGIN_NAME: &str = "folder"; + +fn make_specta_builder() -> tauri_specta::Builder { + tauri_specta::Builder::::new() + .plugin_name(PLUGIN_NAME) + .commands(tauri_specta::collect_commands![ + commands::ping::, + commands::move_session::, + commands::create_folder::, + commands::rename_folder::, + commands::delete_folder::, + ]) + .error_handling(tauri_specta::ErrorHandlingMode::Result) +} + +pub fn init() -> tauri::plugin::TauriPlugin { + let specta_builder = make_specta_builder(); + + tauri::plugin::Builder::new(PLUGIN_NAME) + .invoke_handler(specta_builder.invoke_handler()) + .build() +} + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn export_types() { + const OUTPUT_FILE: &str = "./js/bindings.gen.ts"; + + make_specta_builder::() + .export( + specta_typescript::Typescript::default() + .formatter(specta_typescript::formatter::prettier) + .bigint(specta_typescript::BigIntExportBehavior::Number), + OUTPUT_FILE, + ) + .unwrap(); + + let content = std::fs::read_to_string(OUTPUT_FILE).unwrap(); + std::fs::write(OUTPUT_FILE, format!("// @ts-nocheck\n{content}")).unwrap(); + } +} diff --git a/plugins/folder/tsconfig.json b/plugins/folder/tsconfig.json new file mode 100644 index 000000000..13b985325 --- /dev/null +++ b/plugins/folder/tsconfig.json @@ -0,0 +1,5 @@ +{ + "extends": "../tsconfig.base.json", + "include": ["./js/*.ts"], + "exclude": ["node_modules"] +} diff --git a/plugins/listener/Cargo.toml b/plugins/listener/Cargo.toml index c95b12413..8851f4f98 100644 --- a/plugins/listener/Cargo.toml +++ b/plugins/listener/Cargo.toml @@ -30,6 +30,7 @@ hypr-language = { workspace = true } hypr-llm = { workspace = true } hypr-vad-ext = { workspace = true } hypr-vad2 = { workspace = true } +tauri-plugin-folder = { workspace = true } owhisper-client = { workspace = true } owhisper-interface = { workspace = true } diff --git a/plugins/listener/src/actors/recorder.rs b/plugins/listener/src/actors/recorder.rs index 31f2f4c94..8fe195386 100644 --- a/plugins/listener/src/actors/recorder.rs +++ b/plugins/listener/src/actors/recorder.rs @@ -8,6 +8,7 @@ use hypr_audio_utils::{ VorbisEncodeSettings, decode_vorbis_to_wav_file, encode_wav_to_vorbis_file, mix_audio_f32, }; use ractor::{Actor, ActorName, ActorProcessingErr, ActorRef}; +use tauri_plugin_folder::find_session_dir; const FLUSH_INTERVAL: std::time::Duration = std::time::Duration::from_millis(1000); @@ -49,7 +50,7 @@ impl Actor for RecorderActor { _myself: ActorRef, args: Self::Arguments, ) -> Result { - let dir = args.app_dir.join(&args.session_id); + let dir = find_session_dir(&args.app_dir, &args.session_id); std::fs::create_dir_all(&dir)?; let filename_base = "audio".to_string(); diff --git a/plugins/misc/Cargo.toml b/plugins/misc/Cargo.toml index f9ff3686f..181b7cd06 100644 --- a/plugins/misc/Cargo.toml +++ b/plugins/misc/Cargo.toml @@ -23,6 +23,7 @@ tauri-plugin-path2 = { workspace = true } hypr-audio-utils = { workspace = true } hypr-buffer = { workspace = true } hypr-host = { workspace = true } +tauri-plugin-folder = { workspace = true } tauri = { workspace = true, features = ["test"] } tauri-specta = { workspace = true, features = ["derive", "typescript"] } diff --git a/plugins/misc/src/commands.rs b/plugins/misc/src/commands.rs index 9dd8c9f74..bf59b3c4a 100644 --- a/plugins/misc/src/commands.rs +++ b/plugins/misc/src/commands.rs @@ -1,5 +1,6 @@ use std::path::PathBuf; +use tauri_plugin_folder::find_session_dir; use tauri_plugin_opener::OpenerExt; use tauri_plugin_path2::Path2PluginExt; @@ -37,7 +38,7 @@ pub async fn audio_exist( session_id: String, ) -> Result { let base = app.path2().base().map_err(|e| e.to_string())?; - let session_dir = base.join("sessions").join(session_id); + let session_dir = find_session_dir(&base.join("sessions"), &session_id); ["audio.wav", "audio.ogg"] .iter() @@ -56,7 +57,7 @@ pub async fn audio_delete( session_id: String, ) -> Result<(), String> { let base = app.path2().base().map_err(|e| e.to_string())?; - let session_dir = base.join("sessions").join(session_id); + let session_dir = find_session_dir(&base.join("sessions"), &session_id); ["audio.wav", "audio.ogg"] .iter() @@ -92,7 +93,7 @@ fn audio_import_internal( .path2() .base() .map_err(|e| AudioImportError::PathResolver(e.to_string()))?; - let session_dir = base.join("sessions").join(session_id); + let session_dir = find_session_dir(&base.join("sessions"), session_id); std::fs::create_dir_all(&session_dir)?; @@ -122,7 +123,7 @@ pub async fn audio_path( session_id: String, ) -> Result { let base = app.path2().base().map_err(|e| e.to_string())?; - let session_dir = base.join("sessions").join(session_id); + let session_dir = find_session_dir(&base.join("sessions"), &session_id); let path = ["audio.ogg", "audio.wav"] .iter() @@ -140,7 +141,7 @@ pub async fn audio_open( session_id: String, ) -> Result<(), String> { let base = app.path2().base().map_err(|e| e.to_string())?; - let session_dir = base.join("sessions").join(session_id); + let session_dir = find_session_dir(&base.join("sessions"), &session_id); app.opener() .open_path(session_dir.to_string_lossy(), None::<&str>) @@ -156,7 +157,7 @@ pub async fn delete_session_folder( session_id: String, ) -> Result<(), String> { let base = app.path2().base().map_err(|e| e.to_string())?; - let session_dir = base.join("sessions").join(session_id); + let session_dir = find_session_dir(&base.join("sessions"), &session_id); if session_dir.exists() { std::fs::remove_dir_all(session_dir).map_err(|e| e.to_string())?; diff --git a/plugins/notify/Cargo.toml b/plugins/notify/Cargo.toml index 5edf49048..01917e5ea 100644 --- a/plugins/notify/Cargo.toml +++ b/plugins/notify/Cargo.toml @@ -15,13 +15,15 @@ specta-typescript = { workspace = true } tokio = { workspace = true, features = ["macros"] } [dependencies] -notify = "8" -notify-debouncer-full = "0.5" - tauri = { workspace = true, features = ["test"] } -tauri-plugin-path2 = { workspace = true } tauri-specta = { workspace = true, features = ["derive", "typescript"] } +tauri-plugin-folder = { workspace = true } +tauri-plugin-path2 = { workspace = true } + +notify = "8" +notify-debouncer-full = "0.5" + serde = { workspace = true } specta = { workspace = true } diff --git a/plugins/notify/src/ext.rs b/plugins/notify/src/ext.rs index 361df44b6..cf8497272 100644 --- a/plugins/notify/src/ext.rs +++ b/plugins/notify/src/ext.rs @@ -5,7 +5,7 @@ use notify_debouncer_full::{DebouncedEvent, new_debouncer}; use tauri_plugin_path2::Path2PluginExt; use tauri_specta::Event; -use crate::{FileChanged, WatcherState}; +use crate::{FileChanged, WatcherState, migration}; const DEBOUNCE_DELAY_MS: u64 = 500; @@ -39,6 +39,9 @@ impl<'a, R: tauri::Runtime, M: tauri::Manager> Notify<'a, R, M> { .unwrap_or(path) .to_string_lossy() .to_string(); + + migration::maybe_migrate_path(&base_for_closure, &relative_path); + let _ = FileChanged { path: relative_path, } diff --git a/plugins/notify/src/lib.rs b/plugins/notify/src/lib.rs index c1b7a7f51..372bbd599 100644 --- a/plugins/notify/src/lib.rs +++ b/plugins/notify/src/lib.rs @@ -2,6 +2,7 @@ mod commands; mod error; mod events; mod ext; +mod migration; use std::sync::Mutex; @@ -13,6 +14,7 @@ pub use commands::*; pub use error::*; pub use events::*; pub use ext::*; +pub use migration::*; const PLUGIN_NAME: &str = "notify"; @@ -38,6 +40,9 @@ pub fn init() -> tauri::plugin::TauriPlugin { .invoke_handler(specta_builder.invoke_handler()) .setup(move |app, _api| { specta_builder.mount_events(app); + + migration::migrate_uuid_folders(app); + app.manage(WatcherState { debouncer: Mutex::new(None), }); diff --git a/plugins/notify/src/migration.rs b/plugins/notify/src/migration.rs new file mode 100644 index 000000000..9dde82dcf --- /dev/null +++ b/plugins/notify/src/migration.rs @@ -0,0 +1,38 @@ +use std::path::Path; + +use tauri_plugin_path2::Path2PluginExt; + +pub use tauri_plugin_folder::{is_uuid, migrate_all_uuid_folders, migrate_session_to_default}; + +pub fn migrate_uuid_folders(app: &tauri::AppHandle) { + let base = match app.path2().base() { + Ok(base) => base, + Err(e) => { + tracing::warn!("Failed to get base path for migration: {}", e); + return; + } + }; + + let sessions_dir = base.join("sessions"); + if !sessions_dir.exists() { + return; + } + + migrate_all_uuid_folders(&sessions_dir); +} + +pub fn maybe_migrate_path(base: &Path, relative_path: &str) { + let parts: Vec<&str> = relative_path.split('/').collect(); + + if parts.len() != 2 || parts[0] != "sessions" { + return; + } + + let folder_name = parts[1]; + if !is_uuid(folder_name) { + return; + } + + let sessions_dir = base.join("sessions"); + migrate_session_to_default(&sessions_dir, folder_name); +} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index cf502fa8b..20986296d 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -263,6 +263,9 @@ importers: '@hypr/plugin-extensions': specifier: workspace:* version: link:../../plugins/extensions + '@hypr/plugin-folder': + specifier: workspace:* + version: link:../../plugins/folder '@hypr/plugin-hooks': specifier: workspace:* version: link:../../plugins/hooks @@ -838,7 +841,7 @@ importers: version: 1.11.19 drizzle-orm: specifier: ^0.44.7 - version: 0.44.7(@cloudflare/workers-types@4.20251221.0)(@opentelemetry/api@1.9.0)(@types/pg@8.16.0)(bun-types@1.3.5)(pg@8.16.3)(postgres@3.4.7) + version: 0.44.7(@cloudflare/workers-types@4.20251221.0)(@opentelemetry/api@1.8.0)(@types/pg@8.16.0)(bun-types@1.3.5)(pg@8.16.3)(postgres@3.4.7) exa-js: specifier: ^1.10.2 version: 1.10.2(ws@8.18.3) @@ -1496,6 +1499,12 @@ importers: specifier: ^2.9.1 version: 2.9.1 + plugins/folder: + dependencies: + '@tauri-apps/api': + specifier: ^2.9.1 + version: 2.9.1 + plugins/hooks: dependencies: '@tauri-apps/api': @@ -26266,10 +26275,10 @@ snapshots: dotenv@17.2.3: {} - drizzle-orm@0.44.7(@cloudflare/workers-types@4.20251221.0)(@opentelemetry/api@1.9.0)(@types/pg@8.16.0)(bun-types@1.3.5)(pg@8.16.3)(postgres@3.4.7): + drizzle-orm@0.44.7(@cloudflare/workers-types@4.20251221.0)(@opentelemetry/api@1.8.0)(@types/pg@8.16.0)(bun-types@1.3.5)(pg@8.16.3)(postgres@3.4.7): optionalDependencies: '@cloudflare/workers-types': 4.20251221.0 - '@opentelemetry/api': 1.9.0 + '@opentelemetry/api': 1.8.0 '@types/pg': 8.16.0 bun-types: 1.3.5 pg: 8.16.3