bazaar.go 10 KB

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