1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
|
/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import axios from '@nextcloud/axios'
import { confirmPassword } from '@nextcloud/password-confirmation'
import { showError, showInfo } from '@nextcloud/dialogs'
import { loadState } from '@nextcloud/initial-state'
import { translate as t } from '@nextcloud/l10n'
import { generateUrl } from '@nextcloud/router'
import { defineStore } from 'pinia'
import api from './api'
import logger from '../logger'
import type { IAppstoreExApp, IDeployDaemon, IExAppStatus } from '../app-types'
import { reactive } from 'vue'
interface AppApiState {
apps: IAppstoreExApp[]
updateCount: number
loading: Record<string, boolean>
loadingList: boolean
statusUpdater: number | null | undefined
daemonAccessible: boolean
defaultDaemon: IDeployDaemon | null
}
export const useAppApiStore = defineStore('app-api-apps', {
state: (): AppApiState => ({
apps: [],
updateCount: loadState('settings', 'appstoreExAppUpdateCount', 0),
loading: reactive({}),
loadingList: false,
statusUpdater: null,
daemonAccessible: loadState('settings', 'defaultDaemonConfigAccessible', false),
defaultDaemon: loadState('settings', 'defaultDaemonConfig', null),
}),
getters: {
getLoading: (state) => (id: string) => state.loading[id] ?? false,
getAllApps: (state) => state.apps,
getUpdateCount: (state) => state.updateCount,
getDaemonAccessible: (state) => state.daemonAccessible,
getDefaultDaemon: (state) => state.defaultDaemon,
getAppStatus: (state) => (appId: string) =>
state.apps.find((app) => app.id === appId)?.status || null,
getStatusUpdater: (state) => state.statusUpdater,
getInitializingOrDeployingApps: (state) =>
state.apps.filter((app) =>
app?.status?.action
&& (app?.status?.action === 'deploy' || app.status.action === 'init' || app.status.action === 'healthcheck')
&& app.status.type !== '',
),
},
actions: {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
appsApiFailure(error: any) {
showError(t('settings', 'An error occurred during the request. Unable to proceed.') + '<br>' + error.error.response.data.data.message, { isHTML: true })
logger.error(error)
},
setError(appId: string | string[], error: string) {
const appIds = Array.isArray(appId) ? appId : [appId]
appIds.forEach((_id) => {
const app = this.apps.find((app) => app.id === _id)
if (app) {
app.error = error
}
})
},
enableApp(appId: string) {
this.loading[appId] = true
this.loading.install = true
return confirmPassword().then(() => {
return axios.post(generateUrl(`/apps/app_api/apps/enable/${appId}`))
.then((response) => {
this.loading[appId] = false
this.loading.install = false
const app = this.apps.find((app) => app.id === appId)
if (app) {
if (!app.installed) {
app.installed = true
app.needsDownload = false
app.daemon = this.defaultDaemon
app.status = {
type: 'install',
action: 'deploy',
init: 0,
deploy: 0,
} as IExAppStatus
}
app.active = true
app.canUnInstall = false
app.removable = true
app.error = ''
}
this.updateAppsStatus()
return axios.get(generateUrl('apps/files'))
.then(() => {
if (response.data.update_required) {
showInfo(
t('settings', 'The app has been enabled but needs to be updated.'),
{
onClick: () => window.location.reload(),
close: false,
},
)
setTimeout(() => {
location.reload()
}, 5000)
}
})
.catch(() => {
this.setError(appId, t('settings', 'Error: This app cannot be enabled because it makes the server unstable'))
})
})
.catch((error) => {
this.loading[appId] = false
this.loading.install = false
this.setError(appId, error.response.data.data.message)
this.appsApiFailure({ appId, error })
})
})
},
forceEnableApp(appId: string) {
this.loading[appId] = true
this.loading.install = true
return confirmPassword().then(() => {
return api.post(generateUrl('/apps/app_api/apps/force'), { appId })
.then(() => {
location.reload()
})
.catch((error) => {
this.loading[appId] = false
this.loading.install = false
this.setError(appId, error.response.data.data.message)
this.appsApiFailure({ appId, error })
})
})
},
disableApp(appId: string) {
this.loading[appId] = true
return confirmPassword().then(() => {
return api.get(generateUrl(`apps/app_api/apps/disable/${appId}`))
.then(() => {
this.loading[appId] = false
const app = this.apps.find((app) => app.id === appId)
if (app) {
app.active = false
if (app.removable) {
app.canUnInstall = true
}
}
return true
})
.catch((error) => {
this.loading[appId] = false
this.appsApiFailure({ appId, error })
})
})
},
uninstallApp(appId: string, removeData: boolean) {
this.loading[appId] = true
return confirmPassword().then(() => {
return api.get(generateUrl(`/apps/app_api/apps/uninstall/${appId}?removeData=${removeData}`))
.then(() => {
this.loading[appId] = false
const app = this.apps.find((app) => app.id === appId)
if (app) {
app.active = false
app.needsDownload = true
app.installed = false
app.canUnInstall = false
app.canInstall = true
app.daemon = null
app.status = {}
if (app.update !== null) {
this.updateCount--
}
app.update = undefined
}
return true
})
.catch((error) => {
this.loading[appId] = false
this.appsApiFailure({ appId, error })
})
})
},
updateApp(appId: string) {
this.loading[appId] = true
this.loading.install = true
return confirmPassword().then(() => {
return api.get(generateUrl(`/apps/app_api/apps/update/${appId}`))
.then(() => {
this.loading.install = false
this.loading[appId] = false
const app = this.apps.find((app) => app.id === appId)
if (app) {
const version = app.update
app.update = undefined
app.version = version || app.version
app.status = {
type: 'update',
action: 'deploy',
init: 0,
deploy: 0,
} as IExAppStatus
app.error = ''
}
this.updateCount--
this.updateAppsStatus()
return true
})
.catch((error) => {
this.loading[appId] = false
this.loading.install = false
this.appsApiFailure({ appId, error })
})
})
},
async fetchAllApps() {
this.loadingList = true
try {
const response = await api.get(generateUrl('/apps/app_api/apps/list'))
this.apps = response.data.apps
this.loadingList = false
return true
} catch (error) {
logger.error(error as string)
showError(t('settings', 'An error occurred during the request. Unable to proceed.'))
this.loadingList = false
}
},
async fetchAppStatus(appId: string) {
return api.get(generateUrl(`/apps/app_api/apps/status/${appId}`))
.then((response) => {
const app = this.apps.find((app) => app.id === appId)
if (app) {
app.status = response.data
}
const initializingOrDeployingApps = this.getInitializingOrDeployingApps
console.debug('initializingOrDeployingApps after setAppStatus', initializingOrDeployingApps)
if (initializingOrDeployingApps.length === 0) {
console.debug('clearing interval')
clearInterval(this.statusUpdater as number)
this.statusUpdater = null
}
if (Object.hasOwn(response.data, 'error')
&& response.data.error !== ''
&& initializingOrDeployingApps.length === 1) {
clearInterval(this.statusUpdater as number)
this.statusUpdater = null
}
})
.catch((error) => {
this.appsApiFailure({ appId, error })
this.apps = this.apps.filter((app) => app.id !== appId)
this.updateAppsStatus()
})
},
updateAppsStatus() {
clearInterval(this.statusUpdater as number)
const initializingOrDeployingApps = this.getInitializingOrDeployingApps
if (initializingOrDeployingApps.length === 0) {
return
}
this.statusUpdater = setInterval(() => {
const initializingOrDeployingApps = this.getInitializingOrDeployingApps
console.debug('initializingOrDeployingApps', initializingOrDeployingApps)
initializingOrDeployingApps.forEach(app => {
this.fetchAppStatus(app.id)
})
}, 2000) as unknown as number
},
},
})
|