A privacy-first, self-hosted, fully open source personal knowledge management software, written in typescript and golang. (PERSONAL FORK)
at lambda-fork/main 82 lines 2.2 kB view raw
1// SiYuan - Refactor your thinking 2// Copyright (c) 2020-present, b3log.org 3// 4// This program is free software: you can redistribute it and/or modify 5// it under the terms of the GNU Affero General Public License as published by 6// the Free Software Foundation, either version 3 of the License, or 7// (at your option) any later version. 8// 9// This program is distributed in the hope that it will be useful, 10// but WITHOUT ANY WARRANTY; without even the implied warranty of 11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12// GNU Affero General Public License for more details. 13// 14// You should have received a copy of the GNU Affero General Public License 15// along with this program. If not, see <https://www.gnu.org/licenses/>. 16 17package cmd 18 19import ( 20 "github.com/olahol/melody" 21 "github.com/siyuan-note/logging" 22 "github.com/siyuan-note/siyuan/kernel/util" 23) 24 25type Cmd interface { 26 Name() string 27 IsRead() bool // 非读即写 28 Id() float64 29 Exec() 30} 31 32type BaseCmd struct { 33 id float64 34 param map[string]interface{} 35 session *melody.Session 36 PushPayload *util.Result 37} 38 39func (cmd *BaseCmd) Id() float64 { 40 return cmd.id 41} 42 43func (cmd *BaseCmd) Push() { 44 cmd.PushPayload.Callback = cmd.param["callback"] 45 appId, _ := cmd.session.Get("app") 46 cmd.PushPayload.AppId = appId.(string) 47 sid, _ := cmd.session.Get("id") 48 cmd.PushPayload.SessionId = sid.(string) 49 util.PushEvent(cmd.PushPayload) 50} 51 52func NewCommand(cmdStr string, cmdId float64, param map[string]interface{}, session *melody.Session) (ret Cmd) { 53 baseCmd := &BaseCmd{id: cmdId, param: param, session: session} 54 switch cmdStr { 55 case "closews": 56 ret = &closews{baseCmd} 57 case "ping": 58 ret = &ping{baseCmd} 59 } 60 61 if nil == ret { 62 return 63 } 64 65 pushMode := util.PushModeSingleSelf 66 if pushModeParam := param["pushMode"]; nil != pushModeParam { 67 pushMode = util.PushMode(pushModeParam.(float64)) 68 } 69 baseCmd.PushPayload = util.NewCmdResult(ret.Name(), cmdId, pushMode) 70 appId, _ := baseCmd.session.Get("app") 71 baseCmd.PushPayload.AppId = appId.(string) 72 sid, _ := baseCmd.session.Get("id") 73 baseCmd.PushPayload.SessionId = sid.(string) 74 return 75} 76 77func Exec(cmd Cmd) { 78 go func() { 79 defer logging.Recover() 80 cmd.Exec() 81 }() 82}