Skip to content

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

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

Daniel5e7e82cf135a
diff --git a/app/src/types/api/index.d.ts b/app/src/types/api/index.d.ts
index 12826cc571..6a8ebc4a3c 100644
--- a/app/src/types/api/index.d.ts
+++ b/app/src/types/api/index.d.ts
@@ -186,6 +186,10 @@ export type NotebookInfoData = { "boxInfo": NotebookInfo | null; };
export type NotebookPasswordRequestInput = { "password": string; };
+export type NotificationData = { "id": string; };
+
+export type NotificationRequestInput = { "msg": string; "timeout"?: number | null; };
+
export type OpenNotebookRequestInput = { "app"?: string | null; "notebook": string; };
export type OrderedListStartData = { "found": boolean; "start": number; };
@@ -582,8 +586,6 @@ export type APILegacyPOSTPath =
"/api/network/echo/*path" |
"/api/network/forwardProxy" |
"/api/network/proxy" |
- "/api/notification/pushErrMsg" |
- "/api/notification/pushMsg" |
"/api/outline/getDocHeadingNumbers" |
"/api/outline/getDocOutline" |
"/api/petal/loadPetals" |
@@ -1308,6 +1310,16 @@ export interface APIPOSTRoutes {
response: { "code": 0; "data": null; "msg": string; } | { "code": -1; "data": { "closeTimeout": number; } | null; "msg": string; };
body: "json";
};
+ "/api/notification/pushErrMsg": {
+ request: NotificationRequestInput;
+ response: { "code": 0; "data": NotificationData; "msg": string; } | { "code": -1; "data": { "closeTimeout": number; } | null; "msg": string; };
+ body: "json";
+ };
+ "/api/notification/pushMsg": {
+ request: NotificationRequestInput;
+ response: { "code": 0; "data": NotificationData; "msg": string; } | { "code": -1; "data": { "closeTimeout": number; } | null; "msg": string; };
+ body: "json";
+ };
"/api/repo/checkSnapshot": {
request: EmptyRequestInput;
response: { "code": 0; "data": CheckSnapshotData; "msg": string; } | { "code": -1; "data": { "closeTimeout": number; } | null; "msg": string; };
diff --git a/kernel/api/contract_notification_test.go b/kernel/api/contract_notification_test.go
new file mode 100644
index 0000000000..e275a355b3
--- /dev/null
+++ b/kernel/api/contract_notification_test.go
@@ -0,0 +1,45 @@
+package api
+
+import (
+ "encoding/json"
+ "net/http/httptest"
+ "strings"
+ "testing"
+
+ "github.com/gin-gonic/gin"
+)
+
+func TestAPIContractNotificationResponses(t *testing.T) {
+ engine := gin.New()
+ engine.POST("/api/notification/pushMsg", pushMsg)
+ engine.POST("/api/notification/pushErrMsg", pushErrMsg)
+ for _, path := range []string{"/api/notification/pushMsg", "/api/notification/pushErrMsg"} {
+ for _, entry := range []struct {
+ body string
+ code int
+ }{
+ {`{"msg":" hello "}`, 0},
+ {`{"msg":"hello","timeout":null}`, 0},
+ {`{"msg":"hello","timeout":1.9}`, 0},
+ {`{"msg":"hello","timeout":-1}`, 0},
+ {`{"msg":null}`, -1},
+ {`{}`, -1},
+ } {
+ recorder := httptest.NewRecorder()
+ engine.ServeHTTP(recorder, httptest.NewRequest("POST", path, strings.NewReader(entry.body)))
+ requireAPIContract(t, "POST", path, recorder)
+ var response struct {
+ Code int `json:"code"`
+ Data *struct {
+ ID string `json:"id"`
+ } `json:"data"`
+ }
+ if err := json.Unmarshal(recorder.Body.Bytes(), &response); err != nil || response.Code != entry.code {
+ t.Fatalf("%s %s: %s, %v", path, entry.body, recorder.Body.String(), err)
+ }
+ if entry.code == 0 && (response.Data == nil || response.Data.ID == "") {
+ t.Fatalf("missing message ID: %s", recorder.Body.String())
+ }
+ }
+ }
+}
diff --git a/kernel/api/notification.go b/kernel/api/notification.go
index 08c7ad62a7..2158b2829d 100644
--- a/kernel/api/notification.go
+++ b/kernel/api/notification.go
@@ -17,63 +17,36 @@
package api
import (
- "net/http"
+ "github.com/siyuan-note/siyuan/kernel/apicontract"
- "github.com/88250/gulu"
"github.com/gin-gonic/gin"
"github.com/siyuan-note/siyuan/kernel/util"
)
-func pushMsg(c *gin.Context) {
- ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
-
- arg, ok := util.JsonArg(c, ret)
- if !ok {
- return
- }
-
- var msg string
- if !util.ParseJsonArgs(arg, ret, util.BindJsonArg("msg", &msg, true, true)) {
- return
- }
+var pushMsg = contractHandler(apicontract.PushMsg, func(c *gin.Context, request apicontract.NotificationRequest) apicontract.Response[apicontract.NotificationData] {
+ msg := request.Msg
timeout := 7000
- if nil != arg["timeout"] {
- timeout = int(arg["timeout"].(float64))
+ if request.Timeout != nil {
+ timeout = int(*request.Timeout)
}
msg = util.SanitizeHTML(msg)
msgId := util.PushMsg(msg, timeout)
- ret.Data = map[string]any{
- "id": msgId,
- }
-}
-
-func pushErrMsg(c *gin.Context) {
- ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
+ return apicontract.Success(apicontract.NotificationData{ID: msgId})
+})
- arg, ok := util.JsonArg(c, ret)
- if !ok {
- return
- }
-
- var msg string
- if !util.ParseJsonArgs(arg, ret, util.BindJsonArg("msg", &msg, true, true)) {
- return
- }
+var pushErrMsg = contractHandler(apicontract.PushErrMsg, func(c *gin.Context, request apicontract.NotificationRequest) apicontract.Response[apicontract.NotificationData] {
+ msg := request.Msg
timeout := 7000
- if nil != arg["timeout"] {
- timeout = int(arg["timeout"].(float64))
+ if request.Timeout != nil {
+ timeout = int(*request.Timeout)
}
msg = util.SanitizeHTML(msg)
msgId := util.PushErrMsg(msg, timeout)
- ret.Data = map[string]any{
- "id": msgId,
- }
-}
+ return apicontract.Success(apicontract.NotificationData{ID: msgId})
+})
diff --git a/kernel/apicontract/contracts.go b/kernel/apicontract/contracts.go
index fb155c9f97..2efdc93c29 100644
--- a/kernel/apicontract/contracts.go
+++ b/kernel/apicontract/contracts.go
@@ -361,3 +361,6 @@ var Unzip = define[UnzipRequest, Null]("unzip", "/api/archive/unzip", JSONBody,
var AutoSpace = define[TrimmedIDRequest, Null]("autoSpace", "/api/format/autoSpace", JSONBody, ResponseOptions{}, "POST")
var NetAssets2LocalAssets = define[TrimmedIDRequest, Null]("netAssets2LocalAssets", "/api/format/netAssets2LocalAssets", JSONBody, ResponseOptions{}, "POST")
var NetImg2LocalAssets = define[NetImageAssetsRequest, Null]("netImg2LocalAssets", "/api/format/netImg2LocalAssets", JSONBody, ResponseOptions{}, "POST")
+
+var PushMsg = define[NotificationRequest, NotificationData]("pushMsg", "/api/notification/pushMsg", JSONBody, ResponseOptions{}, "POST")
+var PushErrMsg = define[NotificationRequest, NotificationData]("pushErrMsg", "/api/notification/pushErrMsg", JSONBody, ResponseOptions{}, "POST")
diff --git a/kernel/apicontract/legacy_routes.json b/kernel/apicontract/legacy_routes.json
index befd08ee65..c56e5fabff 100644
--- a/kernel/apicontract/legacy_routes.json
+++ b/kernel/apicontract/legacy_routes.json
@@ -286,8 +286,6 @@
{"method":"POST","path":"/api/lute/spinBlockDOM","handler":"spinBlockDOM"},
{"method":"POST","path":"/api/lute/wpsPresentation2BlockDOM","handler":"wpsPresentation2BlockDOM"},
{"method":"POST","path":"/api/network/forwardProxy","handler":"forwardProxy"},
- {"method":"POST","path":"/api/notification/pushErrMsg","handler":"pushErrMsg"},
- {"method":"POST","path":"/api/notification/pushMsg","handler":"pushMsg"},
{"method":"POST","path":"/api/outline/getDocHeadingNumbers","handler":"getDocHeadingNumbers"},
{"method":"POST","path":"/api/outline/getDocOutline","handler":"getDocOutline"},
{"method":"POST","path":"/api/petal/loadPetals","handler":"loadPetals"},
diff --git a/kernel/apicontract/notification.go b/kernel/apicontract/notification.go
new file mode 100644
index 0000000000..9abf24a4ff
--- /dev/null
+++ b/kernel/apicontract/notification.go
@@ -0,0 +1,10 @@
+package apicontract
+
+type NotificationRequest struct {
+ Msg string `json:"msg" api:"trim"`
+ Timeout *float64 `json:"timeout" api:"optional"`
+}
+
+type NotificationData struct {
+ ID string `json:"id"`
+}
diff --git a/kernel/apicontract/schema.json b/kernel/apicontract/schema.json
index 2d87409e53..f109975a9b 100644
--- a/kernel/apicontract/schema.json
+++ b/kernel/apicontract/schema.json
@@ -2779,6 +2779,40 @@
],
"additionalProperties": false
},
+ "NotificationData": {
+ "type": "object",
+ "properties": {
+ "id": {
+ "type": "string"
+ }
+ },
+ "required": [
+ "id"
+ ],
+ "additionalProperties": false
+ },
+ "NotificationRequestInput": {
+ "type": "object",
+ "properties": {
+ "msg": {
+ "type": "string"
+ },
+ "timeout": {
+ "anyOf": [
+ {
+ "type": "number"
+ },
+ {
+ "type": "null"
+ }
+ ]
+ }
+ },
+ "required": [
+ "msg"
+ ],
+ "additionalProperties": false
+ },
"OpenNotebookRequestInput": {
"type": "object",
"properties": {
@@ -12448,6 +12482,156 @@
]
}
},
+ {
+ "method": "POST",
+ "path": "/api/notification/pushErrMsg",
+ "handler": "pushErrMsg",
+ "body": "json",
+ "request": {
+ "$ref": "#/$defs/NotificationRequestInput"
+ },
+ "response": {
+ "anyOf": [
+ {
+ "type": "object",
+ "properties": {
+ "code": {
+ "type": "integer",
+ "enum": [
+ 0
+ ]
+ },
+ "data": {
+ "$ref": "#/$defs/NotificationData"
+ },
+ "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/notification/pushMsg",
+ "handler": "pushMsg",
+ "body": "json",
+ "request": {
+ "$ref": "#/$defs/NotificationRequestInput"
+ },
+ "response": {
+ "anyOf": [
+ {
+ "type": "object",
+ "properties": {
+ "code": {
+ "type": "integer",
+ "enum": [
+ 0
+ ]
+ },
+ "data": {
+ "$ref": "#/$defs/NotificationData"
+ },
+ "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/repo/checkSnapshot",