From 81b06be53a905b8c8a7714a73ef2fcb9211c51ea Mon Sep 17 00:00:00 2001 From: Harald Schilly Date: Thu, 31 Oct 2024 14:00:14 +0100 Subject: [PATCH 1/2] bookmarks: setup user_query support --- src/packages/util/consts/bookmarks.ts | 2 ++ src/packages/util/db-schema/bookmarks.ts | 41 ++++++++++++++++++++++-- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/src/packages/util/consts/bookmarks.ts b/src/packages/util/consts/bookmarks.ts index 0d6570e071..519dad406e 100644 --- a/src/packages/util/consts/bookmarks.ts +++ b/src/packages/util/consts/bookmarks.ts @@ -4,3 +4,5 @@ export const MAX_STARS = 256; export const MAX_LENGTH_STAR = 2048; // the type of bookmark for starring files export const STARRED_FILES = "starred-files"; +// all allowed types (right now just one) +export const BOOKMARK_TYPES = [STARRED_FILES] as const; diff --git a/src/packages/util/db-schema/bookmarks.ts b/src/packages/util/db-schema/bookmarks.ts index 5f1764e8e9..148ab4ecea 100644 --- a/src/packages/util/db-schema/bookmarks.ts +++ b/src/packages/util/db-schema/bookmarks.ts @@ -3,8 +3,10 @@ * License: MS-RSL – see LICENSE.md for details */ -import { Table } from "./types"; +import { BOOKMARK_TYPES } from "@cocalc/util/consts/bookmarks"; + import { ID } from "./crm"; +import { Table } from "./types"; // This table stores various types of bookmarks. This started with backing up starred tabs for a user in a project. Table({ @@ -41,6 +43,41 @@ Table({ desc: "Table for various types of bookmarks.", primary_key: "id", pg_indexes: ["type", "project_id", "account_id"], - user_query: {}, + user_query: { + get: { + pg_where: [ + { + "project_id = $::UUID": "project_id", + "account_id = $::UUID": "account_id", + }, + ], + fields: { + id: null, + type: null, + project_id: null, + account_id: null, + path: null, + stars: null, + last_edited: null, + }, + }, + set: { + fields: { + type: true, + project_id: "project_write", + account_id: "account_id", + path: true, + stars: true, + }, + async check_hook(_db, obj, _account_id, _project_id, cb) { + const type = obj.type; + if (!BOOKMARK_TYPES.includes(type)) { + cb("type '${type} is invalid"); + return; + } + cb(); + }, + }, + }, }, }); From 8a311d1bdafda6e1e3bffc41a5217a503d24b4b8 Mon Sep 17 00:00:00 2001 From: Harald Schilly Date: Thu, 31 Oct 2024 15:51:00 +0100 Subject: [PATCH 2/2] =?UTF-8?q?bookmarks:=20user=5Fquery=20to=20set=20star?= =?UTF-8?q?red=20files=20=E2=80=93=20does=20not=20work,=20though?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../frontend/project/page/flyouts/store.ts | 27 ++++++++++++++++++- src/packages/util/db-schema/bookmarks.ts | 1 + 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/packages/frontend/project/page/flyouts/store.ts b/src/packages/frontend/project/page/flyouts/store.ts index 4965237e08..e210bfeae5 100644 --- a/src/packages/frontend/project/page/flyouts/store.ts +++ b/src/packages/frontend/project/page/flyouts/store.ts @@ -7,7 +7,9 @@ import { merge, sortBy, throttle, uniq, xor } from "lodash"; import { useState } from "react"; import useAsyncEffect from "use-async-effect"; +import { useTypedRedux } from "@cocalc/frontend/app-framework"; import api from "@cocalc/frontend/client/api"; +import { webapp_client } from "@cocalc/frontend/webapp-client"; import { STARRED_FILES } from "@cocalc/util/consts/bookmarks"; import { GetStarredBookmarks, @@ -25,6 +27,8 @@ import { // The only really important situation to think of are when there is nothing in local storage but in the database, // or when there is export function useStarredFilesManager(project_id: string) { + const account_id = useTypedRedux("account", "account_id"); + const [starred, setStarred] = useState( getFlyoutActiveStarred(project_id), ); @@ -55,7 +59,17 @@ export function useStarredFilesManager(project_id: string) { project_id, stars, }; - await api("bookmarks/set", payload); + //await api("bookmarks/set", payload); + + const data = await webapp_client.async_query({ + query: { + bookmarks: { + ...payload, + account_id, + }, + }, + }); + console.log("save starred files", data); } catch (err) { console.error("api error", err); } @@ -72,6 +86,17 @@ export function useStarredFilesManager(project_id: string) { }; const data: GetStarredBookmarks = await api("bookmarks/get", payload); + const data2 = await webapp_client.async_query({ + query: { + bookmarks: { + ...payload, + type: STARRED_FILES, + stars: null, + }, + }, + }); + console.log({ data, data2 }); + const { type, status } = data; if (type !== STARRED_FILES) { diff --git a/src/packages/util/db-schema/bookmarks.ts b/src/packages/util/db-schema/bookmarks.ts index 148ab4ecea..a4ae8fcaa0 100644 --- a/src/packages/util/db-schema/bookmarks.ts +++ b/src/packages/util/db-schema/bookmarks.ts @@ -49,6 +49,7 @@ Table({ { "project_id = $::UUID": "project_id", "account_id = $::UUID": "account_id", + "type = $::TEXT": "type", }, ], fields: {