Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

♻️ Migrate bookmark API contracts https://github.com/siyuan-note/siyuan/issues/19378

Daniel32040ad40f15
diff --git a/app/src/types/api/index.d.ts b/app/src/types/api/index.d.ts
index 6a8ebc4a3c..c35b9559ef 100644
--- a/app/src/types/api/index.d.ts
+++ b/app/src/types/api/index.d.ts
@@ -66,6 +66,8 @@ export type BlocksQueryRequestInput = { "id"?: string | null; "ids": Array<strin
export type BlocksWordCountRequestInput = { "id"?: string | null; "ids": Array<string>; "notebook"?: string | null; "reqId"?: JSONValue | null; };
+export type Bookmark = { "blocks": Array<SearchBlock | null> | null; "count": number; "depth": number; "name": string; "type": string; };
+
export type BootProgressData = { "details": string; "progress": number; };
export type ChangeMasterPasswordRequestInput = { "newPassword": string; "oldPassword": string; };
@@ -454,7 +456,6 @@ export type APILegacyPOSTPath =
"/api/bazaar/uninstallBazaarTheme" |
"/api/bazaar/uninstallBazaarWidget" |
"/api/bazaar/updateBazaarPackage" |
- "/api/bookmark/getBookmark" |
"/api/broadcast/getChannelInfo" |
"/api/broadcast/getChannels" |
"/api/broadcast/postMessage" |
@@ -1140,6 +1141,11 @@ export interface APIPOSTRoutes {
response: { "code": 0; "data": Array<BlockTransaction | null> | null; "msg": string; } | { "code": -1; "data": { "closeTimeout": number; } | null; "msg": string; };
body: "json";
};
+ "/api/bookmark/getBookmark": {
+ request: EmptyRequestInput;
+ response: { "code": 0; "data": Array<Bookmark | null> | null; "msg": string; } | { "code": -1; "data": { "closeTimeout": number; } | null; "msg": string; };
+ body: "none";
+ };
"/api/bookmark/removeBookmark": {
request: RemoveBookmarkRequestInput;
response: { "code": 0; "data": null; "msg": string; } | { "code": -1; "data": { "closeTimeout": number; } | null; "msg": string; };
diff --git a/kernel/api/bookmark.go b/kernel/api/bookmark.go
index ea9dc43a78..8300e6c2ff 100644
--- a/kernel/api/bookmark.go
+++ b/kernel/api/bookmark.go
@@ -17,17 +17,12 @@
package api
import (
- "net/http"
-
- "github.com/88250/gulu"
"github.com/gin-gonic/gin"
"github.com/siyuan-note/siyuan/kernel/apicontract"
"github.com/siyuan-note/siyuan/kernel/model"
)
-func getBookmark(c *gin.Context) {
- ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
+var getBookmark = contractHandler(apicontract.GetBookmark, func(c *gin.Context, request apicontract.EmptyRequest) apicontract.Response[[]*apicontract.Bookmark] {
bookmarks := model.BuildBookmark()
if model.IsReadOnlyRoleContext(c) {
@@ -42,8 +37,8 @@ func getBookmark(c *gin.Context) {
}
bookmarks = tempBookmarks
}
- ret.Data = bookmarks
-}
+ return apicontract.Success(bookmarkContracts(bookmarks))
+})
var removeBookmark = contractHandler(apicontract.RemoveBookmark, func(c *gin.Context, request apicontract.RemoveBookmarkRequest) apicontract.Response[apicontract.Null] {
if err := model.RemoveBookmark(request.Bookmark); err != nil {
diff --git a/kernel/api/contract_bookmark.go b/kernel/api/contract_bookmark.go
new file mode 100644
index 0000000000..6110d3dfa7
--- /dev/null
+++ b/kernel/api/contract_bookmark.go
@@ -0,0 +1,19 @@
+package api
+
+import (
+ "github.com/siyuan-note/siyuan/kernel/apicontract"
+ "github.com/siyuan-note/siyuan/kernel/model"
+)
+
+func bookmarkContracts(values *model.Bookmarks) []*apicontract.Bookmark {
+ if values == nil || *values == nil {
+ return nil
+ }
+ result := make([]*apicontract.Bookmark, len(*values))
+ for i, value := range *values {
+ if value != nil {
+ result[i] = &apicontract.Bookmark{Name: string(value.Name), Blocks: searchBlockContracts(value.Blocks), Type: value.Type, Depth: value.Depth, Count: value.Count}
+ }
+ }
+ return result
+}
diff --git a/kernel/api/contract_bookmark_test.go b/kernel/api/contract_bookmark_test.go
new file mode 100644
index 0000000000..979618388f
--- /dev/null
+++ b/kernel/api/contract_bookmark_test.go
@@ -0,0 +1,21 @@
+package api
+
+import (
+ "encoding/json"
+ "testing"
+
+ "github.com/siyuan-note/siyuan/kernel/model"
+)
+
+func TestAPIContractBookmarkModelConversion(t *testing.T) {
+ for _, values := range []*model.Bookmarks{nil, {}, {nil, {Name: "bookmark", Type: "bookmark", Depth: 1, Count: 2, Blocks: []*model.Block{{ID: "id", Children: []*model.Block{{ID: "child"}}}}}}} {
+ before, err := json.Marshal(values)
+ if err != nil {
+ t.Fatal(err)
+ }
+ after, err := json.Marshal(bookmarkContracts(values))
+ if err != nil || string(before) != string(after) {
+ t.Fatalf("bookmark conversion mismatch: %s != %s, %v", before, after, err)
+ }
+ }
+}
diff --git a/kernel/apicontract/bookmark.go b/kernel/apicontract/bookmark.go
new file mode 100644
index 0000000000..bb604342ac
--- /dev/null
+++ b/kernel/apicontract/bookmark.go
@@ -0,0 +1,9 @@
+package apicontract
+
+type Bookmark struct {
+ Name string `json:"name"`
+ Blocks []*SearchBlock `json:"blocks"`
+ Type string `json:"type"`
+ Depth int `json:"depth"`
+ Count int `json:"count"`
+}
diff --git a/kernel/apicontract/contracts.go b/kernel/apicontract/contracts.go
index 2efdc93c29..7bce996560 100644
--- a/kernel/apicontract/contracts.go
+++ b/kernel/apicontract/contracts.go
@@ -364,3 +364,5 @@ var NetImg2LocalAssets = define[NetImageAssetsRequest, Null]("netImg2LocalAssets
var PushMsg = define[NotificationRequest, NotificationData]("pushMsg", "/api/notification/pushMsg", JSONBody, ResponseOptions{}, "POST")
var PushErrMsg = define[NotificationRequest, NotificationData]("pushErrMsg", "/api/notification/pushErrMsg", JSONBody, ResponseOptions{}, "POST")
+
+var GetBookmark = define[EmptyRequest, []*Bookmark]("getBookmark", "/api/bookmark/getBookmark", NoBody, ResponseOptions{}, "POST")
diff --git a/kernel/apicontract/legacy_routes.json b/kernel/apicontract/legacy_routes.json
index c56e5fabff..f60b8c8530 100644
--- a/kernel/apicontract/legacy_routes.json
+++ b/kernel/apicontract/legacy_routes.json
@@ -157,7 +157,6 @@
{"method":"POST","path":"/api/bazaar/uninstallBazaarTheme","handler":"uninstallBazaarTheme"},
{"method":"POST","path":"/api/bazaar/uninstallBazaarWidget","handler":"uninstallBazaarWidget"},
{"method":"POST","path":"/api/bazaar/updateBazaarPackage","handler":"updateBazaarPackage"},
- {"method":"POST","path":"/api/bookmark/getBookmark","handler":"getBookmark"},
{"method":"POST","path":"/api/broadcast/getChannelInfo","handler":"getChannelInfo"},
{"method":"POST","path":"/api/broadcast/getChannels","handler":"getChannels"},
{"method":"POST","path":"/api/broadcast/postMessage","handler":"postMessage"},
diff --git a/kernel/apicontract/schema.json b/kernel/apicontract/schema.json
index f109975a9b..c4bf9a2ae8 100644
--- a/kernel/apicontract/schema.json
+++ b/kernel/apicontract/schema.json
@@ -1086,6 +1086,51 @@
],
"additionalProperties": false
},
+ "Bookmark": {
+ "type": "object",
+ "properties": {
+ "blocks": {
+ "anyOf": [
+ {
+ "type": "array",
+ "items": {
+ "anyOf": [
+ {
+ "$ref": "#/$defs/SearchBlock"
+ },
+ {
+ "type": "null"
+ }
+ ]
+ }
+ },
+ {
+ "type": "null"
+ }
+ ]
+ },
+ "count": {
+ "type": "integer"
+ },
+ "depth": {
+ "type": "integer"
+ },
+ "name": {
+ "type": "string"
+ },
+ "type": {
+ "type": "string"
+ }
+ },
+ "required": [
+ "blocks",
+ "count",
+ "depth",
+ "name",
+ "type"
+ ],
+ "additionalProperties": false
+ },
"BootProgressData": {
"type": "object",
"properties": {
@@ -9898,6 +9943,98 @@
]
}
},
+ {
+ "method": "POST",
+ "path": "/api/bookmark/getBookmark",
+ "handler": "getBookmark",
+ "body": "none",
+ "request": {
+ "$ref": "#/$defs/EmptyRequestInput"
+ },
+ "response": {
+ "anyOf": [
+ {
+ "type": "object",
+ "properties": {
+ "code": {
+ "type": "integer",
+ "enum": [
+ 0
+ ]
+ },
+ "data": {
+ "anyOf": [
+ {
+ "type": "array",
+ "items": {
+ "anyOf": [
+ {
+ "$ref": "#/$defs/Bookmark"
+ },
+ {
+ "type": "null"
+ }
+ ]
+ }
+ },
+ {
+ "type": "null"
+ }
+ ]
+ },
+ "msg": {
+ "type": "string"
+ }
+ },
+ "required": [
+ "code",
+ "msg",
+ "data"
+ ],
+ "additionalProperties": false
+ },
+ {
+ "type": "object",
+ "properties": {
+ "code": {
+ "type": "integer",
+ "enum": [
+ -1
+ ]
+ },
+ "data": {
+ "anyOf": [
+ {
+ "type": "object",
+ "properties": {
+ "closeTimeout": {
+ "type": "number"
+ }
+ },
+ "required": [
+ "closeTimeout"
+ ],
+ "additionalProperties": false
+ },
+ {
+ "type": "null"
+ }
+ ]
+ },
+ "msg": {
+ "type": "string"
+ }
+ },
+ "required": [
+ "code",
+ "msg",
+ "data"
+ ],
+ "additionalProperties": true
+ }
+ ]
+ }
+ },
{
"method": "POST",
"path": "/api/bookmark/removeBookmark",