notebook.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. package api
  17. import (
  18. "net/http"
  19. "strings"
  20. "time"
  21. "github.com/88250/gulu"
  22. "github.com/gin-gonic/gin"
  23. "github.com/siyuan-note/siyuan/kernel/model"
  24. "github.com/siyuan-note/siyuan/kernel/treenode"
  25. "github.com/siyuan-note/siyuan/kernel/util"
  26. )
  27. func setNotebookIcon(c *gin.Context) {
  28. ret := gulu.Ret.NewResult()
  29. defer c.JSON(http.StatusOK, ret)
  30. arg, ok := util.JsonArg(c, ret)
  31. if !ok {
  32. return
  33. }
  34. boxID := arg["notebook"].(string)
  35. icon := arg["icon"].(string)
  36. model.SetBoxIcon(boxID, icon)
  37. }
  38. func changeSortNotebook(c *gin.Context) {
  39. ret := gulu.Ret.NewResult()
  40. defer c.JSON(http.StatusOK, ret)
  41. arg, ok := util.JsonArg(c, ret)
  42. if !ok {
  43. return
  44. }
  45. idsArg := arg["notebooks"].([]interface{})
  46. var ids []string
  47. for _, p := range idsArg {
  48. ids = append(ids, p.(string))
  49. }
  50. model.ChangeBoxSort(ids)
  51. }
  52. func renameNotebook(c *gin.Context) {
  53. ret := gulu.Ret.NewResult()
  54. defer c.JSON(http.StatusOK, ret)
  55. arg, ok := util.JsonArg(c, ret)
  56. if !ok {
  57. return
  58. }
  59. notebook := arg["notebook"].(string)
  60. if util.InvalidIDPattern(notebook, ret) {
  61. return
  62. }
  63. name := arg["name"].(string)
  64. err := model.RenameBox(notebook, name)
  65. if nil != err {
  66. ret.Code = -1
  67. ret.Msg = err.Error()
  68. ret.Data = map[string]interface{}{"closeTimeout": 5000}
  69. return
  70. }
  71. evt := util.NewCmdResult("renamenotebook", 0, util.PushModeBroadcast)
  72. evt.Data = map[string]interface{}{
  73. "box": notebook,
  74. "name": name,
  75. }
  76. util.PushEvent(evt)
  77. }
  78. func removeNotebook(c *gin.Context) {
  79. ret := gulu.Ret.NewResult()
  80. defer c.JSON(http.StatusOK, ret)
  81. arg, ok := util.JsonArg(c, ret)
  82. if !ok {
  83. return
  84. }
  85. notebook := arg["notebook"].(string)
  86. if util.InvalidIDPattern(notebook, ret) {
  87. return
  88. }
  89. if util.ReadOnly && !model.IsUserGuide(notebook) {
  90. ret.Code = -1
  91. ret.Msg = model.Conf.Language(34)
  92. ret.Data = map[string]interface{}{"closeTimeout": 5000}
  93. return
  94. }
  95. err := model.RemoveBox(notebook)
  96. if nil != err {
  97. ret.Code = -1
  98. ret.Msg = err.Error()
  99. return
  100. }
  101. evt := util.NewCmdResult("unmount", 0, util.PushModeBroadcast)
  102. evt.Data = map[string]interface{}{
  103. "box": notebook,
  104. }
  105. evt.Callback = arg["callback"]
  106. util.PushEvent(evt)
  107. }
  108. func createNotebook(c *gin.Context) {
  109. ret := gulu.Ret.NewResult()
  110. defer c.JSON(http.StatusOK, ret)
  111. arg, ok := util.JsonArg(c, ret)
  112. if !ok {
  113. return
  114. }
  115. name := arg["name"].(string)
  116. id, err := model.CreateBox(name)
  117. if nil != err {
  118. ret.Code = -1
  119. ret.Msg = err.Error()
  120. return
  121. }
  122. existed, err := model.Mount(id)
  123. if nil != err {
  124. ret.Code = -1
  125. ret.Msg = err.Error()
  126. return
  127. }
  128. box := model.Conf.Box(id)
  129. if nil == box {
  130. ret.Code = -1
  131. ret.Msg = "opened notebook [" + id + "] not found"
  132. return
  133. }
  134. ret.Data = map[string]interface{}{
  135. "notebook": box,
  136. }
  137. evt := util.NewCmdResult("createnotebook", 0, util.PushModeBroadcast)
  138. evt.Data = map[string]interface{}{
  139. "box": box,
  140. "existed": existed,
  141. }
  142. util.PushEvent(evt)
  143. }
  144. func openNotebook(c *gin.Context) {
  145. ret := gulu.Ret.NewResult()
  146. defer c.JSON(http.StatusOK, ret)
  147. arg, ok := util.JsonArg(c, ret)
  148. if !ok {
  149. return
  150. }
  151. notebook := arg["notebook"].(string)
  152. if util.InvalidIDPattern(notebook, ret) {
  153. return
  154. }
  155. isUserGuide := model.IsUserGuide(notebook)
  156. if util.ReadOnly && !isUserGuide {
  157. ret.Code = -1
  158. ret.Msg = model.Conf.Language(34)
  159. ret.Data = map[string]interface{}{"closeTimeout": 5000}
  160. return
  161. }
  162. if isUserGuide && util.ContainerIOS == util.Container {
  163. // iOS 端不再支持打开用户指南,请参考桌面端用户指南
  164. // 用户指南中包含了付费相关内容,无法通过商店上架审核
  165. // Opening the user guide is no longer supported on iOS https://github.com/siyuan-note/siyuan/issues/11492
  166. ret.Code = -1
  167. ret.Msg = model.Conf.Language(215)
  168. ret.Data = map[string]interface{}{"closeTimeout": 7000}
  169. return
  170. }
  171. msgId := util.PushMsg(model.Conf.Language(45), 1000*60*15)
  172. defer util.PushClearMsg(msgId)
  173. existed, err := model.Mount(notebook)
  174. if nil != err {
  175. ret.Code = -1
  176. ret.Msg = err.Error()
  177. return
  178. }
  179. box := model.Conf.Box(notebook)
  180. if nil == box {
  181. ret.Code = -1
  182. ret.Msg = "opened notebook [" + notebook + "] not found"
  183. return
  184. }
  185. evt := util.NewCmdResult("mount", 0, util.PushModeBroadcast)
  186. evt.Data = map[string]interface{}{
  187. "box": box,
  188. "existed": existed,
  189. }
  190. evt.Callback = arg["callback"]
  191. util.PushEvent(evt)
  192. if isUserGuide {
  193. appArg := arg["app"]
  194. app := ""
  195. if nil != appArg {
  196. app = appArg.(string)
  197. }
  198. go func() {
  199. var startID string
  200. i := 0
  201. for ; i < 70; i++ {
  202. time.Sleep(100 * time.Millisecond)
  203. guideStartID := map[string]string{
  204. "20210808180117-czj9bvb": "20200812220555-lj3enxa",
  205. "20211226090932-5lcq56f": "20211226115423-d5z1joq",
  206. "20210808180117-6v0mkxr": "20200923234011-ieuun1p",
  207. "20240530133126-axarxgx": "20240530101000-4qitucx",
  208. }
  209. startID = guideStartID[notebook]
  210. if nil != treenode.GetBlockTree(startID) {
  211. util.BroadcastByTypeAndApp("main", app, "openFileById", 0, "", map[string]interface{}{
  212. "id": startID,
  213. })
  214. break
  215. }
  216. }
  217. }()
  218. }
  219. }
  220. func closeNotebook(c *gin.Context) {
  221. ret := gulu.Ret.NewResult()
  222. defer c.JSON(http.StatusOK, ret)
  223. arg, ok := util.JsonArg(c, ret)
  224. if !ok {
  225. return
  226. }
  227. notebook := arg["notebook"].(string)
  228. if util.InvalidIDPattern(notebook, ret) {
  229. return
  230. }
  231. model.Unmount(notebook)
  232. }
  233. func getNotebookConf(c *gin.Context) {
  234. ret := gulu.Ret.NewResult()
  235. defer c.JSON(http.StatusOK, ret)
  236. arg, ok := util.JsonArg(c, ret)
  237. if !ok {
  238. return
  239. }
  240. notebook := arg["notebook"].(string)
  241. if util.InvalidIDPattern(notebook, ret) {
  242. return
  243. }
  244. box := model.Conf.GetBox(notebook)
  245. if nil == box {
  246. ret.Code = -1
  247. ret.Msg = "notebook [" + notebook + "] not found"
  248. return
  249. }
  250. ret.Data = map[string]interface{}{
  251. "box": box.ID,
  252. "name": box.Name,
  253. "conf": box.GetConf(),
  254. }
  255. }
  256. func setNotebookConf(c *gin.Context) {
  257. ret := gulu.Ret.NewResult()
  258. defer c.JSON(http.StatusOK, ret)
  259. arg, ok := util.JsonArg(c, ret)
  260. if !ok {
  261. return
  262. }
  263. notebook := arg["notebook"].(string)
  264. if util.InvalidIDPattern(notebook, ret) {
  265. return
  266. }
  267. box := model.Conf.GetBox(notebook)
  268. if nil == box {
  269. ret.Code = -1
  270. ret.Msg = "notebook [" + notebook + "] not found"
  271. return
  272. }
  273. param, err := gulu.JSON.MarshalJSON(arg["conf"])
  274. if nil != err {
  275. ret.Code = -1
  276. ret.Msg = err.Error()
  277. return
  278. }
  279. boxConf := box.GetConf()
  280. if err = gulu.JSON.UnmarshalJSON(param, boxConf); nil != err {
  281. ret.Code = -1
  282. ret.Msg = err.Error()
  283. return
  284. }
  285. boxConf.RefCreateSavePath = strings.TrimSpace(boxConf.RefCreateSavePath)
  286. if "" != boxConf.RefCreateSavePath {
  287. if !strings.HasSuffix(boxConf.RefCreateSavePath, "/") {
  288. boxConf.RefCreateSavePath += "/"
  289. }
  290. }
  291. boxConf.DailyNoteSavePath = strings.TrimSpace(boxConf.DailyNoteSavePath)
  292. if "" != boxConf.DailyNoteSavePath {
  293. if !strings.HasPrefix(boxConf.DailyNoteSavePath, "/") {
  294. boxConf.DailyNoteSavePath = "/" + boxConf.DailyNoteSavePath
  295. }
  296. }
  297. if "/" == boxConf.DailyNoteSavePath {
  298. ret.Code = -1
  299. ret.Msg = model.Conf.Language(49)
  300. return
  301. }
  302. boxConf.DailyNoteTemplatePath = strings.TrimSpace(boxConf.DailyNoteTemplatePath)
  303. if "" != boxConf.DailyNoteTemplatePath {
  304. if !strings.HasSuffix(boxConf.DailyNoteTemplatePath, ".md") {
  305. boxConf.DailyNoteTemplatePath += ".md"
  306. }
  307. if !strings.HasPrefix(boxConf.DailyNoteTemplatePath, "/") {
  308. boxConf.DailyNoteTemplatePath = "/" + boxConf.DailyNoteTemplatePath
  309. }
  310. }
  311. boxConf.DocCreateSavePath = strings.TrimSpace(boxConf.DocCreateSavePath)
  312. box.SaveConf(boxConf)
  313. ret.Data = boxConf
  314. }
  315. func lsNotebooks(c *gin.Context) {
  316. ret := gulu.Ret.NewResult()
  317. defer c.JSON(http.StatusOK, ret)
  318. flashcard := false
  319. // 兼容旧版接口,不能直接使用 util.JsonArg()
  320. arg := map[string]interface{}{}
  321. if err := c.ShouldBindJSON(&arg); nil == err {
  322. if arg["flashcard"] != nil {
  323. flashcard = arg["flashcard"].(bool)
  324. }
  325. }
  326. var notebooks []*model.Box
  327. if flashcard {
  328. notebooks = model.GetFlashcardNotebooks()
  329. } else {
  330. var err error
  331. notebooks, err = model.ListNotebooks()
  332. if nil != err {
  333. return
  334. }
  335. }
  336. ret.Data = map[string]interface{}{
  337. "notebooks": notebooks,
  338. }
  339. }