bazaar.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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. "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. packageType := arg["packageType"].(string)
  34. ret.Data = map[string]interface{}{
  35. "html": model.GetPackageREADME(repoURL, repoHash, packageType),
  36. }
  37. }
  38. func getBazaarPlugin(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. frontend := arg["frontend"].(string)
  46. var keyword string
  47. if keywordArg := arg["keyword"]; nil != keywordArg {
  48. keyword = keywordArg.(string)
  49. }
  50. ret.Data = map[string]interface{}{
  51. "packages": model.BazaarPlugins(frontend, keyword),
  52. }
  53. }
  54. func getInstalledPlugin(c *gin.Context) {
  55. ret := gulu.Ret.NewResult()
  56. defer c.JSON(http.StatusOK, ret)
  57. arg, ok := util.JsonArg(c, ret)
  58. if !ok {
  59. return
  60. }
  61. frontend := arg["frontend"].(string)
  62. var keyword string
  63. if keywordArg := arg["keyword"]; nil != keywordArg {
  64. keyword = keywordArg.(string)
  65. }
  66. ret.Data = map[string]interface{}{
  67. "packages": model.InstalledPlugins(frontend, keyword),
  68. }
  69. }
  70. func installBazaarPlugin(c *gin.Context) {
  71. ret := gulu.Ret.NewResult()
  72. defer c.JSON(http.StatusOK, ret)
  73. arg, ok := util.JsonArg(c, ret)
  74. if !ok {
  75. return
  76. }
  77. repoURL := arg["repoURL"].(string)
  78. repoHash := arg["repoHash"].(string)
  79. packageName := arg["packageName"].(string)
  80. err := model.InstallBazaarPlugin(repoURL, repoHash, packageName)
  81. if nil != err {
  82. ret.Code = 1
  83. ret.Msg = err.Error()
  84. return
  85. }
  86. frontend := arg["frontend"].(string)
  87. util.PushMsg(model.Conf.Language(69), 3000)
  88. ret.Data = map[string]interface{}{
  89. "packages": model.BazaarPlugins(frontend, ""),
  90. }
  91. }
  92. func uninstallBazaarPlugin(c *gin.Context) {
  93. ret := gulu.Ret.NewResult()
  94. defer c.JSON(http.StatusOK, ret)
  95. arg, ok := util.JsonArg(c, ret)
  96. if !ok {
  97. return
  98. }
  99. frontend := arg["frontend"].(string)
  100. packageName := arg["packageName"].(string)
  101. err := model.UninstallBazaarPlugin(packageName, frontend)
  102. if nil != err {
  103. ret.Code = -1
  104. ret.Msg = err.Error()
  105. return
  106. }
  107. ret.Data = map[string]interface{}{
  108. "packages": model.BazaarPlugins(frontend, ""),
  109. }
  110. }
  111. func getBazaarWidget(c *gin.Context) {
  112. ret := gulu.Ret.NewResult()
  113. defer c.JSON(http.StatusOK, ret)
  114. arg, ok := util.JsonArg(c, ret)
  115. if !ok {
  116. return
  117. }
  118. var keyword string
  119. if keywordArg := arg["keyword"]; nil != keywordArg {
  120. keyword = keywordArg.(string)
  121. }
  122. ret.Data = map[string]interface{}{
  123. "packages": model.BazaarWidgets(keyword),
  124. }
  125. }
  126. func getInstalledWidget(c *gin.Context) {
  127. ret := gulu.Ret.NewResult()
  128. defer c.JSON(http.StatusOK, ret)
  129. ret.Data = map[string]interface{}{
  130. "packages": model.InstalledWidgets(),
  131. }
  132. }
  133. func installBazaarWidget(c *gin.Context) {
  134. ret := gulu.Ret.NewResult()
  135. defer c.JSON(http.StatusOK, ret)
  136. arg, ok := util.JsonArg(c, ret)
  137. if !ok {
  138. return
  139. }
  140. repoURL := arg["repoURL"].(string)
  141. repoHash := arg["repoHash"].(string)
  142. packageName := arg["packageName"].(string)
  143. err := model.InstallBazaarWidget(repoURL, repoHash, packageName)
  144. if nil != err {
  145. ret.Code = 1
  146. ret.Msg = err.Error()
  147. return
  148. }
  149. util.PushMsg(model.Conf.Language(69), 3000)
  150. ret.Data = map[string]interface{}{
  151. "packages": model.BazaarWidgets(""),
  152. }
  153. }
  154. func uninstallBazaarWidget(c *gin.Context) {
  155. ret := gulu.Ret.NewResult()
  156. defer c.JSON(http.StatusOK, ret)
  157. arg, ok := util.JsonArg(c, ret)
  158. if !ok {
  159. return
  160. }
  161. packageName := arg["packageName"].(string)
  162. err := model.UninstallBazaarWidget(packageName)
  163. if nil != err {
  164. ret.Code = -1
  165. ret.Msg = err.Error()
  166. return
  167. }
  168. ret.Data = map[string]interface{}{
  169. "packages": model.BazaarWidgets(""),
  170. }
  171. }
  172. func getBazaarIcon(c *gin.Context) {
  173. ret := gulu.Ret.NewResult()
  174. defer c.JSON(http.StatusOK, ret)
  175. arg, ok := util.JsonArg(c, ret)
  176. if !ok {
  177. return
  178. }
  179. var keyword string
  180. if keywordArg := arg["keyword"]; nil != keywordArg {
  181. keyword = keywordArg.(string)
  182. }
  183. ret.Data = map[string]interface{}{
  184. "packages": model.BazaarIcons(keyword),
  185. }
  186. }
  187. func getInstalledIcon(c *gin.Context) {
  188. ret := gulu.Ret.NewResult()
  189. defer c.JSON(http.StatusOK, ret)
  190. ret.Data = map[string]interface{}{
  191. "packages": model.InstalledIcons(),
  192. }
  193. }
  194. func installBazaarIcon(c *gin.Context) {
  195. ret := gulu.Ret.NewResult()
  196. defer c.JSON(http.StatusOK, ret)
  197. arg, ok := util.JsonArg(c, ret)
  198. if !ok {
  199. return
  200. }
  201. repoURL := arg["repoURL"].(string)
  202. repoHash := arg["repoHash"].(string)
  203. packageName := arg["packageName"].(string)
  204. err := model.InstallBazaarIcon(repoURL, repoHash, packageName)
  205. if nil != err {
  206. ret.Code = 1
  207. ret.Msg = err.Error()
  208. return
  209. }
  210. util.PushMsg(model.Conf.Language(69), 3000)
  211. ret.Data = map[string]interface{}{
  212. "packages": model.BazaarIcons(""),
  213. "appearance": model.Conf.Appearance,
  214. }
  215. }
  216. func uninstallBazaarIcon(c *gin.Context) {
  217. ret := gulu.Ret.NewResult()
  218. defer c.JSON(http.StatusOK, ret)
  219. arg, ok := util.JsonArg(c, ret)
  220. if !ok {
  221. return
  222. }
  223. packageName := arg["packageName"].(string)
  224. err := model.UninstallBazaarIcon(packageName)
  225. if nil != err {
  226. ret.Code = -1
  227. ret.Msg = err.Error()
  228. return
  229. }
  230. ret.Data = map[string]interface{}{
  231. "packages": model.BazaarIcons(""),
  232. "appearance": model.Conf.Appearance,
  233. }
  234. }
  235. func getBazaarTemplate(c *gin.Context) {
  236. ret := gulu.Ret.NewResult()
  237. defer c.JSON(http.StatusOK, ret)
  238. arg, ok := util.JsonArg(c, ret)
  239. if !ok {
  240. return
  241. }
  242. var keyword string
  243. if keywordArg := arg["keyword"]; nil != keywordArg {
  244. keyword = keywordArg.(string)
  245. }
  246. ret.Data = map[string]interface{}{
  247. "packages": model.BazaarTemplates(keyword),
  248. }
  249. }
  250. func getInstalledTemplate(c *gin.Context) {
  251. ret := gulu.Ret.NewResult()
  252. defer c.JSON(http.StatusOK, ret)
  253. ret.Data = map[string]interface{}{
  254. "packages": model.InstalledTemplates(),
  255. }
  256. }
  257. func installBazaarTemplate(c *gin.Context) {
  258. ret := gulu.Ret.NewResult()
  259. defer c.JSON(http.StatusOK, ret)
  260. arg, ok := util.JsonArg(c, ret)
  261. if !ok {
  262. return
  263. }
  264. repoURL := arg["repoURL"].(string)
  265. repoHash := arg["repoHash"].(string)
  266. packageName := arg["packageName"].(string)
  267. err := model.InstallBazaarTemplate(repoURL, repoHash, packageName)
  268. if nil != err {
  269. ret.Code = 1
  270. ret.Msg = err.Error()
  271. return
  272. }
  273. ret.Data = map[string]interface{}{
  274. "packages": model.BazaarTemplates(""),
  275. }
  276. util.PushMsg(model.Conf.Language(69), 3000)
  277. }
  278. func uninstallBazaarTemplate(c *gin.Context) {
  279. ret := gulu.Ret.NewResult()
  280. defer c.JSON(http.StatusOK, ret)
  281. arg, ok := util.JsonArg(c, ret)
  282. if !ok {
  283. return
  284. }
  285. packageName := arg["packageName"].(string)
  286. err := model.UninstallBazaarTemplate(packageName)
  287. if nil != err {
  288. ret.Code = -1
  289. ret.Msg = err.Error()
  290. return
  291. }
  292. ret.Data = map[string]interface{}{
  293. "packages": model.BazaarTemplates(""),
  294. }
  295. }
  296. func getBazaarTheme(c *gin.Context) {
  297. ret := gulu.Ret.NewResult()
  298. defer c.JSON(http.StatusOK, ret)
  299. arg, ok := util.JsonArg(c, ret)
  300. if !ok {
  301. return
  302. }
  303. var keyword string
  304. if keywordArg := arg["keyword"]; nil != keywordArg {
  305. keyword = keywordArg.(string)
  306. }
  307. ret.Data = map[string]interface{}{
  308. "packages": model.BazaarThemes(keyword),
  309. }
  310. }
  311. func getInstalledTheme(c *gin.Context) {
  312. ret := gulu.Ret.NewResult()
  313. defer c.JSON(http.StatusOK, ret)
  314. ret.Data = map[string]interface{}{
  315. "packages": model.InstalledThemes(),
  316. }
  317. }
  318. func installBazaarTheme(c *gin.Context) {
  319. ret := gulu.Ret.NewResult()
  320. defer c.JSON(http.StatusOK, ret)
  321. arg, ok := util.JsonArg(c, ret)
  322. if !ok {
  323. return
  324. }
  325. repoURL := arg["repoURL"].(string)
  326. repoHash := arg["repoHash"].(string)
  327. packageName := arg["packageName"].(string)
  328. mode := arg["mode"].(float64)
  329. update := false
  330. if nil != arg["update"] {
  331. update = arg["update"].(bool)
  332. }
  333. err := model.InstallBazaarTheme(repoURL, repoHash, packageName, int(mode), update)
  334. if nil != err {
  335. ret.Code = 1
  336. ret.Msg = err.Error()
  337. return
  338. }
  339. // 安装集市主题后不跟随系统切换外观模式
  340. model.Conf.Appearance.ModeOS = false
  341. model.Conf.Save()
  342. util.PushMsg(model.Conf.Language(69), 3000)
  343. ret.Data = map[string]interface{}{
  344. "packages": model.BazaarThemes(""),
  345. "appearance": model.Conf.Appearance,
  346. }
  347. }
  348. func uninstallBazaarTheme(c *gin.Context) {
  349. ret := gulu.Ret.NewResult()
  350. defer c.JSON(http.StatusOK, ret)
  351. arg, ok := util.JsonArg(c, ret)
  352. if !ok {
  353. return
  354. }
  355. packageName := arg["packageName"].(string)
  356. err := model.UninstallBazaarTheme(packageName)
  357. if nil != err {
  358. ret.Code = -1
  359. ret.Msg = err.Error()
  360. return
  361. }
  362. ret.Data = map[string]interface{}{
  363. "packages": model.BazaarThemes(""),
  364. "appearance": model.Conf.Appearance,
  365. }
  366. }