From 5edb8829555e547ae39053c412c8313f8222e233 Mon Sep 17 00:00:00 2001 From: sawka Date: Tue, 28 May 2024 12:18:26 -0700 Subject: [PATCH] add an updatemetadata call to objectservice --- frontend/app/store/wos.ts | 4 ++++ frontend/types/custom.d.ts | 2 ++ frontend/wave.ts | 2 ++ pkg/service/objectservice/objectservice.go | 11 +++++++++++ pkg/wstore/wstore.go | 18 ++++++++++++++++++ 5 files changed, 37 insertions(+) diff --git a/frontend/app/store/wos.ts b/frontend/app/store/wos.ts index ad137981d..86771adfe 100644 --- a/frontend/app/store/wos.ts +++ b/frontend/app/store/wos.ts @@ -272,6 +272,10 @@ export function CloseTab(tabId: string): Promise { return wrapObjectServiceCall("CloseTab", tabId); } +export function UpdateBlockMeta(blockId: string, meta: MetadataType): Promise { + return wrapObjectServiceCall("UpdateBlockMeta", blockId, meta); +} + export { cleanWaveObjectCache, clearWaveObjectCache, diff --git a/frontend/types/custom.d.ts b/frontend/types/custom.d.ts index e94ff3e02..de115715f 100644 --- a/frontend/types/custom.d.ts +++ b/frontend/types/custom.d.ts @@ -7,6 +7,8 @@ declare global { activetabid: string; }; + type MetadataType = { [key: string]: any }; + type ORef = { otype: string; oid: string; diff --git a/frontend/wave.ts b/frontend/wave.ts index 5892ec9e1..8ea684749 100644 --- a/frontend/wave.ts +++ b/frontend/wave.ts @@ -16,6 +16,8 @@ loadFonts(); console.log("Wave Starting"); +(window as any).WOS = WOS; + document.addEventListener("DOMContentLoaded", async () => { console.log("DOMContentLoaded"); // ensures client/window are loaded into the cache before rendering diff --git a/pkg/service/objectservice/objectservice.go b/pkg/service/objectservice/objectservice.go index 0a27a0a54..ed378bbf5 100644 --- a/pkg/service/objectservice/objectservice.go +++ b/pkg/service/objectservice/objectservice.go @@ -179,3 +179,14 @@ func (svc *ObjectService) CloseTab(uiContext wstore.UIContext, tabId string) (an } return updatesRtn(ctx, nil) } + +func (svc *ObjectService) UpdateBlockMeta(uiContext wstore.UIContext, blockId string, meta map[string]any) (any, error) { + ctx, cancelFn := context.WithTimeout(context.Background(), DefaultTimeout) + defer cancelFn() + ctx = wstore.ContextWithUpdates(ctx) + err := wstore.UpdateBlockMeta(ctx, blockId, meta) + if err != nil { + return nil, fmt.Errorf("error merging block meta: %w", err) + } + return updatesRtn(ctx, nil) +} diff --git a/pkg/wstore/wstore.go b/pkg/wstore/wstore.go index 40d984936..38a56211a 100644 --- a/pkg/wstore/wstore.go +++ b/pkg/wstore/wstore.go @@ -395,6 +395,24 @@ func CloseTab(ctx context.Context, workspaceId string, tabId string) error { }) } +func UpdateBlockMeta(ctx context.Context, blockId string, meta map[string]any) error { + return WithTx(ctx, func(tx *TxWrap) error { + block, _ := DBGet[*Block](tx.Context(), blockId) + if block == nil { + return fmt.Errorf("block not found: %q", blockId) + } + for k, v := range meta { + if v == nil { + delete(block.Meta, k) + continue + } + block.Meta[k] = v + } + DBUpdate(tx.Context(), block) + return nil + }) +} + func EnsureInitialData() error { // does not need to run in a transaction since it is called on startup ctx, cancelFn := context.WithTimeout(context.Background(), 2*time.Second)