You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

apps.js 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. * @copyright Copyright (c) 2018 Julius Härtl <jus@bitgrid.net>
  3. *
  4. * @author Julius Härtl <jus@bitgrid.net>
  5. *
  6. * @license GNU AGPL version 3 or any later version
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. import api from './api'
  23. import Vue from 'vue'
  24. const state = {
  25. apps: [],
  26. categories: [],
  27. updateCount: 0,
  28. loading: {},
  29. loadingList: false
  30. }
  31. const mutations = {
  32. APPS_API_FAILURE(state, error) {
  33. OC.Notification.showHtml(t('settings', 'An error occured during the request. Unable to proceed.') + '<br>' + error.error.response.data.data.message, { timeout: 7 })
  34. console.error(state, error)
  35. },
  36. initCategories(state, { categories, updateCount }) {
  37. state.categories = categories
  38. state.updateCount = updateCount
  39. },
  40. setUpdateCount(state, updateCount) {
  41. state.updateCount = updateCount
  42. },
  43. addCategory(state, category) {
  44. state.categories.push(category)
  45. },
  46. appendCategories(state, categoriesArray) {
  47. // convert obj to array
  48. state.categories = categoriesArray
  49. },
  50. setAllApps(state, apps) {
  51. state.apps = apps
  52. },
  53. setError(state, { appId, error }) {
  54. if (!Array.isArray(appId)) {
  55. appId = [appId]
  56. }
  57. appId.forEach((_id) => {
  58. let app = state.apps.find(app => app.id === _id)
  59. app.error = error
  60. })
  61. },
  62. clearError(state, { appId, error }) {
  63. let app = state.apps.find(app => app.id === appId)
  64. app.error = null
  65. },
  66. enableApp(state, { appId, groups }) {
  67. let app = state.apps.find(app => app.id === appId)
  68. app.active = true
  69. app.groups = groups
  70. },
  71. disableApp(state, appId) {
  72. let app = state.apps.find(app => app.id === appId)
  73. app.active = false
  74. app.groups = []
  75. if (app.removable) {
  76. app.canUnInstall = true
  77. }
  78. },
  79. uninstallApp(state, appId) {
  80. state.apps.find(app => app.id === appId).active = false
  81. state.apps.find(app => app.id === appId).groups = []
  82. state.apps.find(app => app.id === appId).needsDownload = true
  83. state.apps.find(app => app.id === appId).installed = false
  84. state.apps.find(app => app.id === appId).canUnInstall = false
  85. state.apps.find(app => app.id === appId).canInstall = true
  86. },
  87. updateApp(state, appId) {
  88. let app = state.apps.find(app => app.id === appId)
  89. let version = app.update
  90. app.update = null
  91. app.version = version
  92. state.updateCount--
  93. },
  94. resetApps(state) {
  95. state.apps = []
  96. },
  97. reset(state) {
  98. state.apps = []
  99. state.categories = []
  100. state.updateCount = 0
  101. },
  102. startLoading(state, id) {
  103. if (Array.isArray(id)) {
  104. id.forEach((_id) => {
  105. Vue.set(state.loading, _id, true)
  106. })
  107. } else {
  108. Vue.set(state.loading, id, true)
  109. }
  110. },
  111. stopLoading(state, id) {
  112. if (Array.isArray(id)) {
  113. id.forEach((_id) => {
  114. Vue.set(state.loading, _id, false)
  115. })
  116. } else {
  117. Vue.set(state.loading, id, false)
  118. }
  119. }
  120. }
  121. const getters = {
  122. loading(state) {
  123. return function(id) {
  124. return state.loading[id]
  125. }
  126. },
  127. getCategories(state) {
  128. return state.categories
  129. },
  130. getAllApps(state) {
  131. return state.apps
  132. },
  133. getUpdateCount(state) {
  134. return state.updateCount
  135. }
  136. }
  137. const actions = {
  138. enableApp(context, { appId, groups }) {
  139. let apps
  140. if (Array.isArray(appId)) {
  141. apps = appId
  142. } else {
  143. apps = [appId]
  144. }
  145. return api.requireAdmin().then((response) => {
  146. context.commit('startLoading', apps)
  147. context.commit('startLoading', 'install')
  148. return api.post(OC.generateUrl(`settings/apps/enable`), { appIds: apps, groups: groups })
  149. .then((response) => {
  150. context.commit('stopLoading', apps)
  151. context.commit('stopLoading', 'install')
  152. apps.forEach(_appId => {
  153. context.commit('enableApp', { appId: _appId, groups: groups })
  154. })
  155. // check for server health
  156. return api.get(OC.generateUrl('apps/files'))
  157. .then(() => {
  158. if (response.data.update_required) {
  159. OC.dialogs.info(
  160. t(
  161. 'settings',
  162. 'The app has been enabled but needs to be updated. You will be redirected to the update page in 5 seconds.'
  163. ),
  164. t('settings', 'App update'),
  165. function() {
  166. window.location.reload()
  167. },
  168. true
  169. )
  170. setTimeout(function() {
  171. location.reload()
  172. }, 5000)
  173. }
  174. })
  175. .catch(() => {
  176. if (!Array.isArray(appId)) {
  177. context.commit('setError', {
  178. appId: apps,
  179. error: t('settings', 'Error: This app can not be enabled because it makes the server unstable')
  180. })
  181. }
  182. })
  183. })
  184. .catch((error) => {
  185. context.commit('stopLoading', apps)
  186. context.commit('stopLoading', 'install')
  187. context.commit('setError', {
  188. appId: apps,
  189. error: error.response.data.data.message
  190. })
  191. context.commit('APPS_API_FAILURE', { appId, error })
  192. })
  193. }).catch((error) => context.commit('API_FAILURE', { appId, error }))
  194. },
  195. forceEnableApp(context, { appId, groups }) {
  196. let apps
  197. if (Array.isArray(appId)) {
  198. apps = appId
  199. } else {
  200. apps = [appId]
  201. }
  202. return api.requireAdmin().then(() => {
  203. context.commit('startLoading', apps)
  204. context.commit('startLoading', 'install')
  205. return api.post(OC.generateUrl(`settings/apps/force`), { appId })
  206. .then((response) => {
  207. // TODO: find a cleaner solution
  208. location.reload()
  209. })
  210. .catch((error) => {
  211. context.commit('stopLoading', apps)
  212. context.commit('stopLoading', 'install')
  213. context.commit('setError', {
  214. appId: apps,
  215. error: error.response.data.data.message
  216. })
  217. context.commit('APPS_API_FAILURE', { appId, error })
  218. })
  219. }).catch((error) => context.commit('API_FAILURE', { appId, error }))
  220. },
  221. disableApp(context, { appId }) {
  222. let apps
  223. if (Array.isArray(appId)) {
  224. apps = appId
  225. } else {
  226. apps = [appId]
  227. }
  228. return api.requireAdmin().then((response) => {
  229. context.commit('startLoading', apps)
  230. return api.post(OC.generateUrl(`settings/apps/disable`), { appIds: apps })
  231. .then((response) => {
  232. context.commit('stopLoading', apps)
  233. apps.forEach(_appId => {
  234. context.commit('disableApp', _appId)
  235. })
  236. return true
  237. })
  238. .catch((error) => {
  239. context.commit('stopLoading', apps)
  240. context.commit('APPS_API_FAILURE', { appId, error })
  241. })
  242. }).catch((error) => context.commit('API_FAILURE', { appId, error }))
  243. },
  244. uninstallApp(context, { appId }) {
  245. return api.requireAdmin().then((response) => {
  246. context.commit('startLoading', appId)
  247. return api.get(OC.generateUrl(`settings/apps/uninstall/${appId}`))
  248. .then((response) => {
  249. context.commit('stopLoading', appId)
  250. context.commit('uninstallApp', appId)
  251. return true
  252. })
  253. .catch((error) => {
  254. context.commit('stopLoading', appId)
  255. context.commit('APPS_API_FAILURE', { appId, error })
  256. })
  257. }).catch((error) => context.commit('API_FAILURE', { appId, error }))
  258. },
  259. updateApp(context, { appId }) {
  260. return api.requireAdmin().then((response) => {
  261. context.commit('startLoading', appId)
  262. context.commit('startLoading', 'install')
  263. return api.get(OC.generateUrl(`settings/apps/update/${appId}`))
  264. .then((response) => {
  265. context.commit('stopLoading', 'install')
  266. context.commit('stopLoading', appId)
  267. context.commit('updateApp', appId)
  268. return true
  269. })
  270. .catch((error) => {
  271. context.commit('stopLoading', appId)
  272. context.commit('stopLoading', 'install')
  273. context.commit('APPS_API_FAILURE', { appId, error })
  274. })
  275. }).catch((error) => context.commit('API_FAILURE', { appId, error }))
  276. },
  277. getAllApps(context) {
  278. context.commit('startLoading', 'list')
  279. return api.get(OC.generateUrl(`settings/apps/list`))
  280. .then((response) => {
  281. context.commit('setAllApps', response.data.apps)
  282. context.commit('stopLoading', 'list')
  283. return true
  284. })
  285. .catch((error) => context.commit('API_FAILURE', error))
  286. },
  287. getCategories(context) {
  288. context.commit('startLoading', 'categories')
  289. return api.get(OC.generateUrl('settings/apps/categories'))
  290. .then((response) => {
  291. if (response.data.length > 0) {
  292. context.commit('appendCategories', response.data)
  293. context.commit('stopLoading', 'categories')
  294. return true
  295. }
  296. return false
  297. })
  298. .catch((error) => context.commit('API_FAILURE', error))
  299. }
  300. }
  301. export default { state, mutations, getters, actions }