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

This commit is contained in:
Liang Ding 2022-10-21 11:35:02 +08:00
parent 9fb4d9da2a
commit 8ac1104d6f
No known key found for this signature in database
GPG key ID: 136F30F901A2231D
5 changed files with 41 additions and 11 deletions

View file

@ -405,7 +405,7 @@ func createDailyNote(c *gin.Context) {
} }
notebook := arg["notebook"].(string) notebook := arg["notebook"].(string)
p, err := model.CreateDailyNote(notebook) p, existed, err := model.CreateDailyNote(notebook)
if nil != err { if nil != err {
if model.ErrBoxNotFound == err { if model.ErrBoxNotFound == err {
ret.Code = 1 ret.Code = 1
@ -425,7 +425,18 @@ func createDailyNote(c *gin.Context) {
return 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) name := path.Base(p)
files, _, _ := model.ListDocTree(box.ID, path.Dir(p), model.Conf.FileTree.Sort) files, _, _ := model.ListDocTree(box.ID, path.Dir(p), model.Conf.FileTree.Sort)
evt.Data = map[string]interface{}{ evt.Data = map[string]interface{}{

View file

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

View file

@ -33,9 +33,9 @@ import (
"github.com/siyuan-note/siyuan/kernel/util" "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") 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) hPath += "-" + gulu.Rand.String(7)
} }
pathBuilder := bytes.Buffer{} pathBuilder := bytes.Buffer{}

View file

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

View file

@ -226,6 +226,8 @@ func PushEvent(event *Result) {
broadcastOthers(msg, event.SessionId) broadcastOthers(msg, event.SessionId)
case PushModeBroadcastExcludeSelfApp: case PushModeBroadcastExcludeSelfApp:
broadcastOtherApps(msg, event.AppId) broadcastOtherApps(msg, event.AppId)
case PushModeBroadcastApp:
broadcastApp(msg, event.AppId)
case PushModeNone: 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) { func broadcastOthers(msg []byte, excludeSID string) {
sessions.Range(func(key, value interface{}) bool { sessions.Range(func(key, value interface{}) bool {
appSessions := value.(*sync.Map) appSessions := value.(*sync.Map)