Explorar o código

:art: `Alt+5` 打开已有日记时不在内核伺服客户端之间同步 Fix https://github.com/siyuan-note/siyuan/issues/5617

Liang Ding %!s(int64=2) %!d(string=hai) anos
pai
achega
8ac1104d6f
Modificáronse 5 ficheiros con 41 adicións e 11 borrados
  1. 13 2
      kernel/api/filetree.go
  2. 4 3
      kernel/model/file.go
  3. 2 2
      kernel/model/path.go
  4. 5 4
      kernel/util/result.go
  5. 17 0
      kernel/util/websocket.go

+ 13 - 2
kernel/api/filetree.go

@@ -405,7 +405,7 @@ func createDailyNote(c *gin.Context) {
 	}
 
 	notebook := arg["notebook"].(string)
-	p, err := model.CreateDailyNote(notebook)
+	p, existed, err := model.CreateDailyNote(notebook)
 	if nil != err {
 		if model.ErrBoxNotFound == err {
 			ret.Code = 1
@@ -425,7 +425,18 @@ func createDailyNote(c *gin.Context) {
 		return
 	}
 
-	evt := util.NewCmdResult("createdailynote", 0, util.PushModeBroadcast, util.PushModeNone)
+	appArg := arg["app"]
+	app := ""
+	if nil != appArg {
+		app = appArg.(string)
+	}
+	pushMode := util.PushModeBroadcast
+	if existed && "" != app {
+		pushMode = util.PushModeBroadcastApp
+	}
+	evt := util.NewCmdResult("createdailynote", 0, pushMode, util.PushModeNone)
+	evt.AppId = app
+
 	name := path.Base(p)
 	files, _, _ := model.ListDocTree(box.ID, path.Dir(p), model.Conf.FileTree.Sort)
 	evt.Data = map[string]interface{}{

+ 4 - 3
kernel/model/file.go

@@ -1012,7 +1012,7 @@ func CreateWithMarkdown(boxID, hPath, md string) (id string, err error) {
 	WaitForWritingFiles()
 	luteEngine := NewLute()
 	dom := luteEngine.Md2BlockDOM(md)
-	id, err = createDocsByHPath(box.ID, hPath, dom)
+	id, _, err = createDocsByHPath(box.ID, hPath, dom)
 	return
 }
 
@@ -1311,7 +1311,7 @@ func RenameDoc(boxID, p, title string) (err error) {
 	return
 }
 
-func CreateDailyNote(boxID string) (p string, err error) {
+func CreateDailyNote(boxID string) (p string, existed bool, err error) {
 	box := Conf.Box(boxID)
 	if nil == box {
 		err = ErrBoxNotFound
@@ -1333,11 +1333,12 @@ func CreateDailyNote(boxID string) (p string, err error) {
 
 	existRoot := treenode.GetBlockTreeRootByHPath(box.ID, hPath)
 	if nil != existRoot {
+		existed = true
 		p = existRoot.Path
 		return
 	}
 
-	id, err := createDocsByHPath(box.ID, hPath, "")
+	id, existed, err := createDocsByHPath(box.ID, hPath, "")
 	if nil != err {
 		return
 	}

+ 2 - 2
kernel/model/path.go

@@ -33,9 +33,9 @@ import (
 	"github.com/siyuan-note/siyuan/kernel/util"
 )
 
-func createDocsByHPath(boxID, hPath, content string) (id string, err error) {
+func createDocsByHPath(boxID, hPath, content string) (id string, existed bool, err error) {
 	hPath = strings.TrimSuffix(hPath, ".sy")
-	if docExist := nil != treenode.GetBlockTreeRootByHPath(boxID, hPath); docExist {
+	if existed = nil != treenode.GetBlockTreeRootByHPath(boxID, hPath); existed {
 		hPath += "-" + gulu.Rand.String(7)
 	}
 	pathBuilder := bytes.Buffer{}

+ 5 - 4
kernel/util/result.go

@@ -24,10 +24,11 @@ import (
 type PushMode int
 
 const (
-	PushModeBroadcast               PushMode = 0  // 广播
-	PushModeSingleSelf              PushMode = 1  // 自我单播
-	PushModeBroadcastExcludeSelf    PushMode = 2  // 非自我广播
-	PushModeBroadcastExcludeSelfApp PushMode = 4  // 非自我应用广播
+	PushModeBroadcast               PushMode = 0  // 所有应用所有会话广播
+	PushModeSingleSelf              PushMode = 1  // 自我应用会话单播
+	PushModeBroadcastExcludeSelf    PushMode = 2  // 非自我会话广播
+	PushModeBroadcastExcludeSelfApp PushMode = 4  // 非自我应用所有会话广播
+	PushModeBroadcastApp            PushMode = 5  // 单个应用内所有会话广播
 	PushModeNone                    PushMode = 10 // 不进行 reload
 )
 

+ 17 - 0
kernel/util/websocket.go

@@ -226,6 +226,8 @@ func PushEvent(event *Result) {
 		broadcastOthers(msg, event.SessionId)
 	case PushModeBroadcastExcludeSelfApp:
 		broadcastOtherApps(msg, event.AppId)
+	case PushModeBroadcastApp:
+		broadcastApp(msg, event.AppId)
 	case PushModeNone:
 	}
 }
@@ -275,6 +277,21 @@ func broadcastOtherApps(msg []byte, excludeApp string) {
 	})
 }
 
+func broadcastApp(msg []byte, app string) {
+	sessions.Range(func(key, value interface{}) bool {
+		appSessions := value.(*sync.Map)
+		appSessions.Range(func(key, value interface{}) bool {
+			session := value.(*melody.Session)
+			if sessionApp, _ := session.Get("app"); sessionApp != app {
+				return true
+			}
+			session.Write(msg)
+			return true
+		})
+		return true
+	})
+}
+
 func broadcastOthers(msg []byte, excludeSID string) {
 	sessions.Range(func(key, value interface{}) bool {
 		appSessions := value.(*sync.Map)