import type { SysCallMapping } from "../system.ts"; import type { LuaCollectionQuery, LuaQueryCollection, } from "../../space_lua/query_collection.ts"; import { type ObjectIndex, ObjectValidationError, } from "../../data/object_index.ts"; import type { ObjectValue } from "@silverbulletmd/silverbullet/type/index"; import type { Client } from "../../client.ts"; import type { LuaTable } from "../../space_lua/runtime.ts"; export function indexSyscalls( objectIndex: ObjectIndex, client: Client, ): SysCallMapping { return { "index.tag": (_ctx, tagName: string): LuaQueryCollection => { return objectIndex.tag(tagName); }, "index.aggregates": (_ctx): LuaQueryCollection => { return objectIndex.aggregates(); }, "index.ensureFullIndex": (_ctx) => { return objectIndex.ensureFullIndex(client.space); }, "index.reindexSpace": () => { return objectIndex.reindexSpace(client.space); }, "index.indexObjects": ( _ctx, page: string, objects: ObjectValue[], ): Promise => { return objectIndex.indexObjects(page, objects); }, "index.validateObjects": async ( _ctx, page: string, objects: ObjectValue[], ): Promise<{ error: string; object: ObjectValue } | null> => { try { await objectIndex.validateObjects(page, objects); return null; } catch (e: any) { if (e instanceof ObjectValidationError) { return { error: e.message, object: e.object, }; } else { throw e; } } }, "index.getObjectByRef": ( _ctx, page: string, tag: string, ref: string, ): Promise => { return objectIndex.getObjectByRef(ref, page, tag); }, "index.queryLuaObjects": ( _ctx, tag: string, query: LuaCollectionQuery, scopedVariables?: Record, ): Promise => { return objectIndex.queryLuaObjects( client.clientSystem.spaceLuaEnv.env, tag, query, scopedVariables, ); }, "index.deleteObject": ( _ctx, page: string, tag: string, ref: string, ): Promise => { return objectIndex.deleteObject(page, tag, ref); }, "lua:index.defineTag": (_ctx, tagDef: LuaTable) => { // Using 'lua:' prefix to _not_ convert tagDef to a JS version (but keep original LuaTable) if (!tagDef.has("name")) { throw new Error("A tag name is required"); } const currentTag = client.config.get(["tags", tagDef.get("name")], null); if (!currentTag) { client.config.set(["tags", tagDef.get("name")], { name: tagDef.get("name"), }); } client.config.set( ["tags", tagDef.get("name"), "metatable"], tagDef.get("metatable"), ); }, }; }