bazaar.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. // SiYuan - Build Your Eternal Digital Garden
  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. "github.com/88250/gulu"
  20. "github.com/gin-gonic/gin"
  21. "github.com/siyuan-note/siyuan/kernel/model"
  22. "github.com/siyuan-note/siyuan/kernel/util"
  23. )
  24. func getBazaarPackageREAME(c *gin.Context) {
  25. ret := gulu.Ret.NewResult()
  26. defer c.JSON(http.StatusOK, ret)
  27. arg, ok := util.JsonArg(c, ret)
  28. if !ok {
  29. return
  30. }
  31. repoURL := arg["repoURL"].(string)
  32. repoHash := arg["repoHash"].(string)
  33. ret.Data = map[string]interface{}{
  34. "html": model.GetPackageREADME(repoURL, repoHash),
  35. }
  36. }
  37. func getBazaarWidget(c *gin.Context) {
  38. ret := gulu.Ret.NewResult()
  39. defer c.JSON(http.StatusOK, ret)
  40. ret.Data = map[string]interface{}{
  41. "packages": model.BazaarWidgets(),
  42. }
  43. }
  44. func getInstalledWidget(c *gin.Context) {
  45. ret := gulu.Ret.NewResult()
  46. defer c.JSON(http.StatusOK, ret)
  47. ret.Data = map[string]interface{}{
  48. "packages": model.InstalledWidgets(),
  49. }
  50. }
  51. func installBazaarWidget(c *gin.Context) {
  52. ret := gulu.Ret.NewResult()
  53. defer c.JSON(http.StatusOK, ret)
  54. arg, ok := util.JsonArg(c, ret)
  55. if !ok {
  56. return
  57. }
  58. repoURL := arg["repoURL"].(string)
  59. repoHash := arg["repoHash"].(string)
  60. packageName := arg["packageName"].(string)
  61. err := model.InstallBazaarWidget(repoURL, repoHash, packageName)
  62. if nil != err {
  63. ret.Code = 1
  64. ret.Msg = err.Error()
  65. return
  66. }
  67. util.PushMsg(model.Conf.Language(69), 3000)
  68. ret.Data = map[string]interface{}{
  69. "packages": model.BazaarWidgets(),
  70. }
  71. }
  72. func uninstallBazaarWidget(c *gin.Context) {
  73. ret := gulu.Ret.NewResult()
  74. defer c.JSON(http.StatusOK, ret)
  75. arg, ok := util.JsonArg(c, ret)
  76. if !ok {
  77. return
  78. }
  79. packageName := arg["packageName"].(string)
  80. err := model.UninstallBazaarWidget(packageName)
  81. if nil != err {
  82. ret.Code = -1
  83. ret.Msg = err.Error()
  84. return
  85. }
  86. ret.Data = map[string]interface{}{
  87. "packages": model.BazaarWidgets(),
  88. }
  89. }
  90. func getBazaarIcon(c *gin.Context) {
  91. ret := gulu.Ret.NewResult()
  92. defer c.JSON(http.StatusOK, ret)
  93. ret.Data = map[string]interface{}{
  94. "packages": model.BazaarIcons(),
  95. }
  96. }
  97. func getInstalledIcon(c *gin.Context) {
  98. ret := gulu.Ret.NewResult()
  99. defer c.JSON(http.StatusOK, ret)
  100. ret.Data = map[string]interface{}{
  101. "packages": model.InstalledIcons(),
  102. }
  103. }
  104. func installBazaarIcon(c *gin.Context) {
  105. ret := gulu.Ret.NewResult()
  106. defer c.JSON(http.StatusOK, ret)
  107. arg, ok := util.JsonArg(c, ret)
  108. if !ok {
  109. return
  110. }
  111. repoURL := arg["repoURL"].(string)
  112. repoHash := arg["repoHash"].(string)
  113. packageName := arg["packageName"].(string)
  114. err := model.InstallBazaarIcon(repoURL, repoHash, packageName)
  115. if nil != err {
  116. ret.Code = 1
  117. ret.Msg = err.Error()
  118. return
  119. }
  120. util.PushMsg(model.Conf.Language(69), 3000)
  121. ret.Data = map[string]interface{}{
  122. "packages": model.BazaarIcons(),
  123. "appearance": model.Conf.Appearance,
  124. }
  125. }
  126. func uninstallBazaarIcon(c *gin.Context) {
  127. ret := gulu.Ret.NewResult()
  128. defer c.JSON(http.StatusOK, ret)
  129. arg, ok := util.JsonArg(c, ret)
  130. if !ok {
  131. return
  132. }
  133. packageName := arg["packageName"].(string)
  134. err := model.UninstallBazaarIcon(packageName)
  135. if nil != err {
  136. ret.Code = -1
  137. ret.Msg = err.Error()
  138. return
  139. }
  140. ret.Data = map[string]interface{}{
  141. "packages": model.BazaarIcons(),
  142. "appearance": model.Conf.Appearance,
  143. }
  144. }
  145. func getBazaarTemplate(c *gin.Context) {
  146. ret := gulu.Ret.NewResult()
  147. defer c.JSON(http.StatusOK, ret)
  148. ret.Data = map[string]interface{}{
  149. "packages": model.BazaarTemplates(),
  150. }
  151. }
  152. func getInstalledTemplate(c *gin.Context) {
  153. ret := gulu.Ret.NewResult()
  154. defer c.JSON(http.StatusOK, ret)
  155. ret.Data = map[string]interface{}{
  156. "packages": model.InstalledTemplates(),
  157. }
  158. }
  159. func installBazaarTemplate(c *gin.Context) {
  160. ret := gulu.Ret.NewResult()
  161. defer c.JSON(http.StatusOK, ret)
  162. arg, ok := util.JsonArg(c, ret)
  163. if !ok {
  164. return
  165. }
  166. repoURL := arg["repoURL"].(string)
  167. repoHash := arg["repoHash"].(string)
  168. packageName := arg["packageName"].(string)
  169. err := model.InstallBazaarTemplate(repoURL, repoHash, packageName)
  170. if nil != err {
  171. ret.Code = 1
  172. ret.Msg = err.Error()
  173. return
  174. }
  175. ret.Data = map[string]interface{}{
  176. "packages": model.BazaarTemplates(),
  177. }
  178. util.PushMsg(model.Conf.Language(69), 3000)
  179. }
  180. func uninstallBazaarTemplate(c *gin.Context) {
  181. ret := gulu.Ret.NewResult()
  182. defer c.JSON(http.StatusOK, ret)
  183. arg, ok := util.JsonArg(c, ret)
  184. if !ok {
  185. return
  186. }
  187. packageName := arg["packageName"].(string)
  188. err := model.UninstallBazaarTemplate(packageName)
  189. if nil != err {
  190. ret.Code = -1
  191. ret.Msg = err.Error()
  192. return
  193. }
  194. ret.Data = map[string]interface{}{
  195. "packages": model.BazaarTemplates(),
  196. }
  197. }
  198. func getBazaarTheme(c *gin.Context) {
  199. ret := gulu.Ret.NewResult()
  200. defer c.JSON(http.StatusOK, ret)
  201. ret.Data = map[string]interface{}{
  202. "packages": model.BazaarThemes(),
  203. }
  204. }
  205. func getInstalledTheme(c *gin.Context) {
  206. ret := gulu.Ret.NewResult()
  207. defer c.JSON(http.StatusOK, ret)
  208. ret.Data = map[string]interface{}{
  209. "packages": model.InstalledThemes(),
  210. }
  211. }
  212. func installBazaarTheme(c *gin.Context) {
  213. ret := gulu.Ret.NewResult()
  214. defer c.JSON(http.StatusOK, ret)
  215. arg, ok := util.JsonArg(c, ret)
  216. if !ok {
  217. return
  218. }
  219. repoURL := arg["repoURL"].(string)
  220. repoHash := arg["repoHash"].(string)
  221. packageName := arg["packageName"].(string)
  222. mode := arg["mode"].(float64)
  223. update := false
  224. if nil != arg["update"] {
  225. update = arg["update"].(bool)
  226. }
  227. err := model.InstallBazaarTheme(repoURL, repoHash, packageName, int(mode), update)
  228. if nil != err {
  229. ret.Code = 1
  230. ret.Msg = err.Error()
  231. return
  232. }
  233. // 安装集市主题后不跟随系统切换外观模式
  234. model.Conf.Appearance.ModeOS = false
  235. model.Conf.Save()
  236. util.PushMsg(model.Conf.Language(69), 3000)
  237. ret.Data = map[string]interface{}{
  238. "packages": model.BazaarThemes(),
  239. "appearance": model.Conf.Appearance,
  240. }
  241. }
  242. func uninstallBazaarTheme(c *gin.Context) {
  243. ret := gulu.Ret.NewResult()
  244. defer c.JSON(http.StatusOK, ret)
  245. arg, ok := util.JsonArg(c, ret)
  246. if !ok {
  247. return
  248. }
  249. packageName := arg["packageName"].(string)
  250. err := model.UninstallBazaarTheme(packageName)
  251. if nil != err {
  252. ret.Code = -1
  253. ret.Msg = err.Error()
  254. return
  255. }
  256. ret.Data = map[string]interface{}{
  257. "packages": model.BazaarThemes(),
  258. "appearance": model.Conf.Appearance,
  259. }
  260. }