bazaar.go 11 KB

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