bazaar.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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. arg, ok := util.JsonArg(c, ret)
  130. if !ok {
  131. return
  132. }
  133. var keyword string
  134. if keywordArg := arg["keyword"]; nil != keywordArg {
  135. keyword = keywordArg.(string)
  136. }
  137. ret.Data = map[string]interface{}{
  138. "packages": model.InstalledWidgets(keyword),
  139. }
  140. }
  141. func installBazaarWidget(c *gin.Context) {
  142. ret := gulu.Ret.NewResult()
  143. defer c.JSON(http.StatusOK, ret)
  144. arg, ok := util.JsonArg(c, ret)
  145. if !ok {
  146. return
  147. }
  148. repoURL := arg["repoURL"].(string)
  149. repoHash := arg["repoHash"].(string)
  150. packageName := arg["packageName"].(string)
  151. err := model.InstallBazaarWidget(repoURL, repoHash, packageName)
  152. if nil != err {
  153. ret.Code = 1
  154. ret.Msg = err.Error()
  155. return
  156. }
  157. util.PushMsg(model.Conf.Language(69), 3000)
  158. ret.Data = map[string]interface{}{
  159. "packages": model.BazaarWidgets(""),
  160. }
  161. }
  162. func uninstallBazaarWidget(c *gin.Context) {
  163. ret := gulu.Ret.NewResult()
  164. defer c.JSON(http.StatusOK, ret)
  165. arg, ok := util.JsonArg(c, ret)
  166. if !ok {
  167. return
  168. }
  169. packageName := arg["packageName"].(string)
  170. err := model.UninstallBazaarWidget(packageName)
  171. if nil != err {
  172. ret.Code = -1
  173. ret.Msg = err.Error()
  174. return
  175. }
  176. ret.Data = map[string]interface{}{
  177. "packages": model.BazaarWidgets(""),
  178. }
  179. }
  180. func getBazaarIcon(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. var keyword string
  188. if keywordArg := arg["keyword"]; nil != keywordArg {
  189. keyword = keywordArg.(string)
  190. }
  191. ret.Data = map[string]interface{}{
  192. "packages": model.BazaarIcons(keyword),
  193. }
  194. }
  195. func getInstalledIcon(c *gin.Context) {
  196. ret := gulu.Ret.NewResult()
  197. defer c.JSON(http.StatusOK, ret)
  198. arg, ok := util.JsonArg(c, ret)
  199. if !ok {
  200. return
  201. }
  202. var keyword string
  203. if keywordArg := arg["keyword"]; nil != keywordArg {
  204. keyword = keywordArg.(string)
  205. }
  206. ret.Data = map[string]interface{}{
  207. "packages": model.InstalledIcons(keyword),
  208. }
  209. }
  210. func installBazaarIcon(c *gin.Context) {
  211. ret := gulu.Ret.NewResult()
  212. defer c.JSON(http.StatusOK, ret)
  213. arg, ok := util.JsonArg(c, ret)
  214. if !ok {
  215. return
  216. }
  217. repoURL := arg["repoURL"].(string)
  218. repoHash := arg["repoHash"].(string)
  219. packageName := arg["packageName"].(string)
  220. err := model.InstallBazaarIcon(repoURL, repoHash, packageName)
  221. if nil != err {
  222. ret.Code = 1
  223. ret.Msg = err.Error()
  224. return
  225. }
  226. util.PushMsg(model.Conf.Language(69), 3000)
  227. ret.Data = map[string]interface{}{
  228. "packages": model.BazaarIcons(""),
  229. "appearance": model.Conf.Appearance,
  230. }
  231. }
  232. func uninstallBazaarIcon(c *gin.Context) {
  233. ret := gulu.Ret.NewResult()
  234. defer c.JSON(http.StatusOK, ret)
  235. arg, ok := util.JsonArg(c, ret)
  236. if !ok {
  237. return
  238. }
  239. packageName := arg["packageName"].(string)
  240. err := model.UninstallBazaarIcon(packageName)
  241. if nil != err {
  242. ret.Code = -1
  243. ret.Msg = err.Error()
  244. return
  245. }
  246. ret.Data = map[string]interface{}{
  247. "packages": model.BazaarIcons(""),
  248. "appearance": model.Conf.Appearance,
  249. }
  250. }
  251. func getBazaarTemplate(c *gin.Context) {
  252. ret := gulu.Ret.NewResult()
  253. defer c.JSON(http.StatusOK, ret)
  254. arg, ok := util.JsonArg(c, ret)
  255. if !ok {
  256. return
  257. }
  258. var keyword string
  259. if keywordArg := arg["keyword"]; nil != keywordArg {
  260. keyword = keywordArg.(string)
  261. }
  262. ret.Data = map[string]interface{}{
  263. "packages": model.BazaarTemplates(keyword),
  264. }
  265. }
  266. func getInstalledTemplate(c *gin.Context) {
  267. ret := gulu.Ret.NewResult()
  268. defer c.JSON(http.StatusOK, ret)
  269. arg, ok := util.JsonArg(c, ret)
  270. if !ok {
  271. return
  272. }
  273. var keyword string
  274. if keywordArg := arg["keyword"]; nil != keywordArg {
  275. keyword = keywordArg.(string)
  276. }
  277. ret.Data = map[string]interface{}{
  278. "packages": model.InstalledTemplates(keyword),
  279. }
  280. }
  281. func installBazaarTemplate(c *gin.Context) {
  282. ret := gulu.Ret.NewResult()
  283. defer c.JSON(http.StatusOK, ret)
  284. arg, ok := util.JsonArg(c, ret)
  285. if !ok {
  286. return
  287. }
  288. repoURL := arg["repoURL"].(string)
  289. repoHash := arg["repoHash"].(string)
  290. packageName := arg["packageName"].(string)
  291. err := model.InstallBazaarTemplate(repoURL, repoHash, packageName)
  292. if nil != err {
  293. ret.Code = 1
  294. ret.Msg = err.Error()
  295. return
  296. }
  297. ret.Data = map[string]interface{}{
  298. "packages": model.BazaarTemplates(""),
  299. }
  300. util.PushMsg(model.Conf.Language(69), 3000)
  301. }
  302. func uninstallBazaarTemplate(c *gin.Context) {
  303. ret := gulu.Ret.NewResult()
  304. defer c.JSON(http.StatusOK, ret)
  305. arg, ok := util.JsonArg(c, ret)
  306. if !ok {
  307. return
  308. }
  309. packageName := arg["packageName"].(string)
  310. err := model.UninstallBazaarTemplate(packageName)
  311. if nil != err {
  312. ret.Code = -1
  313. ret.Msg = err.Error()
  314. return
  315. }
  316. ret.Data = map[string]interface{}{
  317. "packages": model.BazaarTemplates(""),
  318. }
  319. }
  320. func getBazaarTheme(c *gin.Context) {
  321. ret := gulu.Ret.NewResult()
  322. defer c.JSON(http.StatusOK, ret)
  323. arg, ok := util.JsonArg(c, ret)
  324. if !ok {
  325. return
  326. }
  327. var keyword string
  328. if keywordArg := arg["keyword"]; nil != keywordArg {
  329. keyword = keywordArg.(string)
  330. }
  331. ret.Data = map[string]interface{}{
  332. "packages": model.BazaarThemes(keyword),
  333. }
  334. }
  335. func getInstalledTheme(c *gin.Context) {
  336. ret := gulu.Ret.NewResult()
  337. defer c.JSON(http.StatusOK, ret)
  338. arg, ok := util.JsonArg(c, ret)
  339. if !ok {
  340. return
  341. }
  342. var keyword string
  343. if keywordArg := arg["keyword"]; nil != keywordArg {
  344. keyword = keywordArg.(string)
  345. }
  346. ret.Data = map[string]interface{}{
  347. "packages": model.InstalledThemes(keyword),
  348. }
  349. }
  350. func installBazaarTheme(c *gin.Context) {
  351. ret := gulu.Ret.NewResult()
  352. defer c.JSON(http.StatusOK, ret)
  353. arg, ok := util.JsonArg(c, ret)
  354. if !ok {
  355. return
  356. }
  357. repoURL := arg["repoURL"].(string)
  358. repoHash := arg["repoHash"].(string)
  359. packageName := arg["packageName"].(string)
  360. mode := arg["mode"].(float64)
  361. update := false
  362. if nil != arg["update"] {
  363. update = arg["update"].(bool)
  364. }
  365. err := model.InstallBazaarTheme(repoURL, repoHash, packageName, int(mode), update)
  366. if nil != err {
  367. ret.Code = 1
  368. ret.Msg = err.Error()
  369. return
  370. }
  371. // 安装集市主题后不跟随系统切换外观模式
  372. model.Conf.Appearance.ModeOS = false
  373. model.Conf.Save()
  374. util.PushMsg(model.Conf.Language(69), 3000)
  375. ret.Data = map[string]interface{}{
  376. "packages": model.BazaarThemes(""),
  377. "appearance": model.Conf.Appearance,
  378. }
  379. }
  380. func uninstallBazaarTheme(c *gin.Context) {
  381. ret := gulu.Ret.NewResult()
  382. defer c.JSON(http.StatusOK, ret)
  383. arg, ok := util.JsonArg(c, ret)
  384. if !ok {
  385. return
  386. }
  387. packageName := arg["packageName"].(string)
  388. err := model.UninstallBazaarTheme(packageName)
  389. if nil != err {
  390. ret.Code = -1
  391. ret.Msg = err.Error()
  392. return
  393. }
  394. ret.Data = map[string]interface{}{
  395. "packages": model.BazaarThemes(""),
  396. "appearance": model.Conf.Appearance,
  397. }
  398. }