Skip to content

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

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

Daniel78723959a853
diff --git a/app/src/layout/dock/Inbox.ts b/app/src/layout/dock/Inbox.ts
index 8393e5b1d6..c279a7030f 100644
--- a/app/src/layout/dock/Inbox.ts
+++ b/app/src/layout/dock/Inbox.ts
@@ -239,6 +239,9 @@ ${data.shorthandContent}
fetchPost("/api/inbox/getShorthand", {
id: itemElement.dataset.id
}, (response) => {
+ if (response.code !== 0 || !response.data) {
+ return;
+ }
this.data[response.data.oId] = response.data;
itemElement.outerHTML = this.genItemHTML(response.data);
});
@@ -249,6 +252,9 @@ ${data.shorthandContent}
fetchPost("/api/inbox/getShorthand", {
id: detailsElement.getAttribute("data-id")
}, (response) => {
+ if (response.code !== 0 || !response.data) {
+ return;
+ }
this.data[response.data.oId] = response.data;
detailsElement.innerHTML = this.genDetail(response.data);
detailsElement.scrollTop = 0;
@@ -337,6 +343,9 @@ ${data.shorthandContent}
const response = await fetchSyncPost("/api/inbox/getShorthand", {
id: idItem
});
+ if (response.code !== 0 || !response.data) {
+ return;
+ }
this.data[response.data.oId] = response.data;
let md = response.data.shorthandMd;
if ("" === md && "" === response.data.shorthandContent && "" != response.data.shorthandURL) {
@@ -376,6 +385,9 @@ ${data.shorthandContent}
loadingElement.classList.remove("fn__none");
fetchPost("/api/inbox/getShorthands", {page: this.currentPage}, (response) => {
loadingElement.classList.add("fn__none");
+ if (response.code !== 0 || !response.data) {
+ return;
+ }
let html = "";
if (response.data.data.shorthands.length === 0) {
html = `<ul class="b3-list b3-list--background"><li class="b3-list--empty">${window.siyuan.languages.inboxTip}</li></ul>`;
diff --git a/app/src/types/api/index.d.ts b/app/src/types/api/index.d.ts
index 1dc0155309..fe8e5ec3f2 100644
--- a/app/src/types/api/index.d.ts
+++ b/app/src/types/api/index.d.ts
@@ -334,6 +334,8 @@ export type RemoveBookmarkRequestInput = { "bookmark": string; };
export type RemoveCriterionRequestInput = { "name": string; };
+export type RemoveShorthandsRequestInput = { "ids": Array<string>; };
+
export type RemoveTagRequestInput = { "label": string; };
export type RenameBookmarkRequestInput = { "newBookmark": string; "oldBookmark": string; };
@@ -382,6 +384,16 @@ export type SetSnapshotMemoRequestInput = { "id": string; "memo": string; };
export type SetSnippetRequestInput = { "snippets": Array<SnippetInput>; };
+export type Shorthand = { "hCreated": string; "oId": string; "shorthandContent": string; "shorthandDesc": string; "shorthandFrom": number; "shorthandMd": string; "shorthandTitle": string; "shorthandURL": string; };
+
+export type ShorthandPage = { "pagination": ShorthandPagination; "shorthands": Array<Shorthand | null>; };
+
+export type ShorthandPagination = { "paginationPageCount": number; "paginationPageNums": Array<number> | null; "paginationRecordCount": number; };
+
+export type ShorthandsData = { "code": number; "data": ShorthandPage; "msg": string; };
+
+export type ShorthandsRequestInput = { "page": number; };
+
export type Snippet = { "content": string; "disabledInPublish": boolean; "enabled": boolean; "id": string; "name": string; "type": string; };
export type SnippetInput = { "content": string; "disabledInPublish"?: boolean | null; "enabled": boolean; "id": string; "name": string; "type": string; };
@@ -730,9 +742,6 @@ export type APILegacyPOSTPath =
"/api/import/importZipMd" |
"/api/import/startObsidianVaultAnalysis" |
"/api/import/startObsidianVaultImport" |
- "/api/inbox/getShorthand" |
- "/api/inbox/getShorthands" |
- "/api/inbox/removeShorthands" |
"/api/network/echo" |
"/api/network/echo/*path" |
"/api/network/forwardProxy" |
@@ -1349,6 +1358,21 @@ export interface APIPOSTRoutes {
response: { "code": 0; "data": SearchHistoryData; "msg": string; } | { "code": -1; "data": { "closeTimeout": number; } | null; "msg": string; };
body: "json";
};
+ "/api/inbox/getShorthand": {
+ request: TrimmedIDRequestInput;
+ response: { "code": 0; "data": Shorthand | null; "msg": string; } | { "code": -1 | 1; "data": { "closeTimeout": number; } | null; "msg": string; };
+ body: "json";
+ };
+ "/api/inbox/getShorthands": {
+ request: ShorthandsRequestInput;
+ response: { "code": 0; "data": ShorthandsData | null; "msg": string; } | { "code": -1 | 1; "data": { "closeTimeout": number; } | null; "msg": string; };
+ body: "json";
+ };
+ "/api/inbox/removeShorthands": {
+ request: RemoveShorthandsRequestInput;
+ response: { "code": 0; "data": null; "msg": string; } | { "code": -1 | 1; "data": { "closeTimeout": number; } | null; "msg": string; };
+ body: "json";
+ };
"/api/lute/copyStdMarkdown": {
request: CopyStdMarkdownRequestInput;
response: { "code": 0; "data": string; "msg": string; } | { "code": -1; "data": { "closeTimeout": number; } | null; "msg": string; };
diff --git a/kernel/api/contract_inbox.go b/kernel/api/contract_inbox.go
new file mode 100644
index 0000000000..a3ddeec2d0
--- /dev/null
+++ b/kernel/api/contract_inbox.go
@@ -0,0 +1,13 @@
+package api
+
+import "encoding/json"
+
+// decodeCloudInbox 将云端 JSON 数据绑定到明确的收集箱载荷,保留云端响应的嵌套层级。
+func decodeCloudInbox[T any](value map[string]any) (result *T, err error) {
+ data, err := json.Marshal(value)
+ if err != nil {
+ return nil, err
+ }
+ err = json.Unmarshal(data, &result)
+ return
+}
diff --git a/kernel/api/contract_inbox_test.go b/kernel/api/contract_inbox_test.go
new file mode 100644
index 0000000000..b1cd1c8635
--- /dev/null
+++ b/kernel/api/contract_inbox_test.go
@@ -0,0 +1,53 @@
+package api
+
+import (
+ "encoding/json"
+ "github.com/gin-gonic/gin"
+ "github.com/siyuan-note/siyuan/kernel/apicontract"
+ "net/http/httptest"
+ "reflect"
+ "strings"
+ "testing"
+)
+
+func TestAPIContractInboxCloudPayload(t *testing.T) {
+ fixture := `{"code":0,"msg":"","data":{"pagination":{"paginationPageCount":2,"paginationRecordCount":21,"paginationPageNums":[1,2]},"shorthands":[{"oId":"123","shorthandTitle":"title","shorthandURL":"url","shorthandDesc":"desc","shorthandContent":"<p>text</p>","shorthandFrom":1,"shorthandMd":"text","hCreated":"date"}]}}`
+ var original map[string]any
+ if err := json.Unmarshal([]byte(fixture), &original); err != nil {
+ t.Fatal(err)
+ }
+ converted, err := decodeCloudInbox[apicontract.ShorthandsData](original)
+ if err != nil {
+ t.Fatal(err)
+ }
+ encoded, err := json.Marshal(converted)
+ if err != nil {
+ t.Fatal(err)
+ }
+ var actual map[string]any
+ if err := json.Unmarshal(encoded, &actual); err != nil {
+ t.Fatal(err)
+ }
+ if !reflect.DeepEqual(original, actual) {
+ t.Fatalf("cloud response changed: %s", encoded)
+ }
+}
+
+func TestAPIContractInboxInvalidRequests(t *testing.T) {
+ engine := gin.New()
+ engine.POST("/api/inbox/getShorthand", getShorthand)
+ engine.POST("/api/inbox/getShorthands", getShorthands)
+ engine.POST("/api/inbox/removeShorthands", removeShorthands)
+ for _, path := range []string{"/api/inbox/getShorthand", "/api/inbox/getShorthands", "/api/inbox/removeShorthands"} {
+ recorder := httptest.NewRecorder()
+ engine.ServeHTTP(recorder, httptest.NewRequest("POST", path, strings.NewReader(`{}`)))
+ requireAPIContract(t, "POST", path, recorder)
+ if !strings.Contains(recorder.Body.String(), `"code":-1`) {
+ t.Fatalf("missing field accepted: %s", recorder.Body.String())
+ }
+ }
+ request, err := apicontract.GetShorthands.Decode(strings.NewReader(`{"page":1.9}`))
+ if err != nil || int(request.Page) != 1 {
+ t.Fatalf("fractional page changed: %#v, %v", request, err)
+ }
+}
diff --git a/kernel/api/inbox.go b/kernel/api/inbox.go
index 995da61fff..ec22939ffe 100644
--- a/kernel/api/inbox.go
+++ b/kernel/api/inbox.go
@@ -17,77 +17,40 @@
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"
- "github.com/siyuan-note/siyuan/kernel/util"
)
-func removeShorthands(c *gin.Context) {
- ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
-
- arg, ok := util.JsonArg(c, ret)
- if !ok {
- return
- }
-
- idsArg := arg["ids"].([]any)
+var removeShorthands = contractHandler(apicontract.RemoveShorthands, func(c *gin.Context, request apicontract.RemoveShorthandsRequest) apicontract.Response[apicontract.Null] {
var ids []string
- for _, id := range idsArg {
- ids = append(ids, id.(string))
+ ids = append(ids, request.IDs...)
+ if err := model.RemoveCloudShorthands(ids); err != nil {
+ return apicontract.Failure[apicontract.Null](1, err.Error())
}
+ return apicontract.Success(apicontract.Null{})
+})
- err := model.RemoveCloudShorthands(ids)
+var getShorthand = contractHandler(apicontract.GetShorthand, func(c *gin.Context, request apicontract.TrimmedIDRequest) apicontract.Response[*apicontract.Shorthand] {
+ data, err := model.GetCloudShorthand(request.ID)
if err != nil {
- ret.Code = 1
- ret.Msg = err.Error()
- return
- }
-}
-
-func getShorthand(c *gin.Context) {
- ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
-
- arg, ok := util.JsonArg(c, ret)
- if !ok {
- return
+ return apicontract.Failure[*apicontract.Shorthand](1, err.Error())
}
-
- var id string
- if !util.ParseJsonArgs(arg, ret, util.BindJsonArg("id", &id, true, true)) {
- return
- }
- data, err := model.GetCloudShorthand(id)
+ result, err := decodeCloudInbox[apicontract.Shorthand](data)
if err != nil {
- ret.Code = 1
- ret.Msg = err.Error()
- return
- }
- ret.Data = data
-}
-
-func getShorthands(c *gin.Context) {
- ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
-
- arg, ok := util.JsonArg(c, ret)
- if !ok {
- return
+ return apicontract.Failure[*apicontract.Shorthand](1, err.Error())
}
+ return apicontract.Success(result)
+})
- var page float64
- if !util.ParseJsonArgs(arg, ret, util.BindJsonArg("page", &page, true, false)) {
- return
+var getShorthands = contractHandler(apicontract.GetShorthands, func(c *gin.Context, request apicontract.ShorthandsRequest) apicontract.Response[*apicontract.ShorthandsData] {
+ data, err := model.GetCloudShorthands(int(request.Page))
+ if err != nil {
+ return apicontract.Failure[*apicontract.ShorthandsData](1, err.Error())
}
- data, err := model.GetCloudShorthands(int(page))
+ result, err := decodeCloudInbox[apicontract.ShorthandsData](data)
if err != nil {
- ret.Code = 1
- ret.Msg = err.Error()
- return
+ return apicontract.Failure[*apicontract.ShorthandsData](1, err.Error())
}
- ret.Data = data
-}
+ return apicontract.Success(result)
+})
diff --git a/kernel/apicontract/contracts.go b/kernel/apicontract/contracts.go
index 759616d81e..7ac0e4eeaf 100644
--- a/kernel/apicontract/contracts.go
+++ b/kernel/apicontract/contracts.go
@@ -438,3 +438,7 @@ var BroadcastPublish = define[MultipartFields, BroadcastPublishData]("broadcastP
var SetCloudReminder = define[CloudReminderRequest, Null]("setCloudReminder", "/api/cloud/setCloudReminder", JSONBody, ResponseOptions{}, "POST")
var GetCloudSpace = define[EmptyRequest, CloudSpaceData]("getCloudSpace", "/api/cloud/getCloudSpace", NoBody, ResponseOptions{AdditionalCodes: []int{1}}, "POST")
+
+var RemoveShorthands = define[RemoveShorthandsRequest, Null]("removeShorthands", "/api/inbox/removeShorthands", JSONBody, ResponseOptions{AdditionalCodes: []int{1}}, "POST")
+var GetShorthand = define[TrimmedIDRequest, *Shorthand]("getShorthand", "/api/inbox/getShorthand", JSONBody, ResponseOptions{AdditionalCodes: []int{1}}, "POST")
+var GetShorthands = define[ShorthandsRequest, *ShorthandsData]("getShorthands", "/api/inbox/getShorthands", JSONBody, ResponseOptions{AdditionalCodes: []int{1}}, "POST")
diff --git a/kernel/apicontract/inbox.go b/kernel/apicontract/inbox.go
new file mode 100644
index 0000000000..f520e278f1
--- /dev/null
+++ b/kernel/apicontract/inbox.go
@@ -0,0 +1,37 @@
+package apicontract
+
+type RemoveShorthandsRequest struct {
+ IDs []string `json:"ids"`
+}
+
+type ShorthandsRequest struct {
+ Page float64 `json:"page"`
+}
+
+type Shorthand struct {
+ ID string `json:"oId"`
+ Content string `json:"shorthandContent"`
+ Markdown string `json:"shorthandMd"`
+ Description string `json:"shorthandDesc"`
+ From int `json:"shorthandFrom"`
+ Title string `json:"shorthandTitle"`
+ URL string `json:"shorthandURL"`
+ Created string `json:"hCreated"`
+}
+
+type ShorthandPagination struct {
+ PageCount int `json:"paginationPageCount"`
+ RecordCount int `json:"paginationRecordCount"`
+ PageNums []int `json:"paginationPageNums"`
+}
+
+type ShorthandPage struct {
+ Pagination ShorthandPagination `json:"pagination"`
+ Shorthands []*Shorthand `json:"shorthands" api:"nonnullable"`
+}
+
+type ShorthandsData struct {
+ Code int `json:"code"`
+ Msg string `json:"msg"`
+ Data ShorthandPage `json:"data"`
+}
diff --git a/kernel/apicontract/legacy_routes.json b/kernel/apicontract/legacy_routes.json
index 404649a853..359eafbda1 100644
--- a/kernel/apicontract/legacy_routes.json
+++ b/kernel/apicontract/legacy_routes.json
@@ -267,9 +267,6 @@
{"method":"POST","path":"/api/import/importZipMd","handler":"importZipMd"},
{"method":"POST","path":"/api/import/startObsidianVaultAnalysis","handler":"startObsidianVaultAnalysis"},
{"method":"POST","path":"/api/import/startObsidianVaultImport","handler":"startObsidianVaultImport"},
- {"method":"POST","path":"/api/inbox/getShorthand","handler":"getShorthand"},
- {"method":"POST","path":"/api/inbox/getShorthands","handler":"getShorthands"},
- {"method":"POST","path":"/api/inbox/removeShorthands","handler":"removeShorthands"},
{"method":"POST","path":"/api/network/forwardProxy","handler":"forwardProxy"},
{"method":"POST","path":"/api/plugin/getLoadedPlugin","handler":"getLoadedPlugin"},
{"method":"POST","path":"/api/plugin/listLoadedPlugins","handler":"listLoadedPlugins"},
diff --git a/kernel/apicontract/schema.json b/kernel/apicontract/schema.json
index fc37cc5b57..6d4a8e4ab5 100644
--- a/kernel/apicontract/schema.json
+++ b/kernel/apicontract/schema.json
@@ -5743,6 +5743,21 @@
],
"additionalProperties": false
},
+ "RemoveShorthandsRequestInput": {
+ "type": "object",
+ "properties": {
+ "ids": {
+ "type": "array",
+ "items": {
+ "type": "string"
+ }
+ }
+ },
+ "required": [
+ "ids"
+ ],
+ "additionalProperties": false
+ },
"RemoveTagRequestInput": {
"type": "object",
"properties": {
@@ -6589,6 +6604,134 @@
],
"additionalProperties": false
},
+ "Shorthand": {
+ "type": "object",
+ "properties": {
+ "hCreated": {
+ "type": "string"
+ },
+ "oId": {
+ "type": "string"
+ },
+ "shorthandContent": {
+ "type": "string"
+ },
+ "shorthandDesc": {
+ "type": "string"
+ },
+ "shorthandFrom": {
+ "type": "integer"
+ },
+ "shorthandMd": {
+ "type": "string"
+ },
+ "shorthandTitle": {
+ "type": "string"
+ },
+ "shorthandURL": {
+ "type": "string"
+ }
+ },
+ "required": [
+ "hCreated",
+ "oId",
+ "shorthandContent",
+ "shorthandDesc",
+ "shorthandFrom",
+ "shorthandMd",
+ "shorthandTitle",
+ "shorthandURL"
+ ],
+ "additionalProperties": false
+ },
+ "ShorthandPage": {
+ "type": "object",
+ "properties": {
+ "pagination": {
+ "$ref": "#/$defs/ShorthandPagination"
+ },
+ "shorthands": {
+ "type": "array",
+ "items": {
+ "anyOf": [
+ {
+ "$ref": "#/$defs/Shorthand"
+ },
+ {
+ "type": "null"
+ }
+ ]
+ }
+ }
+ },
+ "required": [
+ "pagination",
+ "shorthands"
+ ],
+ "additionalProperties": false
+ },
+ "ShorthandPagination": {
+ "type": "object",
+ "properties": {
+ "paginationPageCount": {
+ "type": "integer"
+ },
+ "paginationPageNums": {
+ "anyOf": [
+ {
+ "type": "array",
+ "items": {
+ "type": "integer"
+ }
+ },
+ {
+ "type": "null"
+ }
+ ]
+ },
+ "paginationRecordCount": {
+ "type": "integer"
+ }
+ },
+ "required": [
+ "paginationPageCount",
+ "paginationPageNums",
+ "paginationRecordCount"
+ ],
+ "additionalProperties": false
+ },
+ "ShorthandsData": {
+ "type": "object",
+ "properties": {
+ "code": {
+ "type": "integer"
+ },
+ "data": {
+ "$ref": "#/$defs/ShorthandPage"
+ },
+ "msg": {
+ "type": "string"
+ }
+ },
+ "required": [
+ "code",
+ "data",
+ "msg"
+ ],
+ "additionalProperties": false
+ },
+ "ShorthandsRequestInput": {
+ "type": "object",
+ "properties": {
+ "page": {
+ "type": "number"
+ }
+ },
+ "required": [
+ "page"
+ ],
+ "additionalProperties": false
+ },
"Snippet": {
"type": "object",
"properties": {
@@ -14805,6 +14948,248 @@
]
}
},
+ {
+ "method": "POST",
+ "path": "/api/inbox/getShorthand",
+ "handler": "getShorthand",
+ "body": "json",
+ "request": {
+ "$ref": "#/$defs/TrimmedIDRequestInput"
+ },
+ "response": {
+ "anyOf": [
+ {
+ "type": "object",
+ "properties": {
+ "code": {
+ "type": "integer",
+ "enum": [
+ 0
+ ]
+ },
+ "data": {
+ "anyOf": [
+ {
+ "$ref": "#/$defs/Shorthand"
+ },
+ {
+ "type": "null"
+ }
+ ]
+ },
+ "msg": {
+ "type": "string"
+ }
+ },
+ "required": [
+ "code",
+ "msg",
+ "data"
+ ],
+ "additionalProperties": false
+ },
+ {
+ "type": "object",
+ "properties": {
+ "code": {
+ "type": "integer",
+ "enum": [
+ -1,
+ 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/inbox/getShorthands",
+ "handler": "getShorthands",
+ "body": "json",
+ "request": {
+ "$ref": "#/$defs/ShorthandsRequestInput"
+ },
+ "response": {
+ "anyOf": [
+ {
+ "type": "object",
+ "properties": {
+ "code": {
+ "type": "integer",
+ "enum": [
+ 0
+ ]
+ },
+ "data": {
+ "anyOf": [
+ {
+ "$ref": "#/$defs/ShorthandsData"
+ },
+ {
+ "type": "null"
+ }
+ ]
+ },
+ "msg": {
+ "type": "string"
+ }
+ },
+ "required": [
+ "code",
+ "msg",
+ "data"
+ ],
+ "additionalProperties": false
+ },
+ {
+ "type": "object",
+ "properties": {
+ "code": {
+ "type": "integer",
+ "enum": [
+ -1,
+ 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/inbox/removeShorthands",
+ "handler": "removeShorthands",
+ "body": "json",
+ "request": {
+ "$ref": "#/$defs/RemoveShorthandsRequestInput"
+ },
+ "response": {
+ "anyOf": [
+ {
+ "type": "object",
+ "properties": {
+ "code": {
+ "type": "integer",
+ "enum": [
+ 0
+ ]
+ },
+ "data": {
+ "type": "null"
+ },
+ "msg": {
+ "type": "string"
+ }
+ },
+ "required": [
+ "code",
+ "msg",
+ "data"
+ ],
+ "additionalProperties": false
+ },
+ {
+ "type": "object",
+ "properties": {
+ "code": {
+ "type": "integer",
+ "enum": [
+ -1,
+ 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/lute/copyStdMarkdown",