notebook.go 7.9 KB

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