-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathappData.service.js
More file actions
91 lines (85 loc) · 2.54 KB
/
appData.service.js
File metadata and controls
91 lines (85 loc) · 2.54 KB
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
/**
* SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import packageJson from '../../package.json'
import { getCapabilities, getCurrentUserData } from '../shared/ocs.service.js'
/**
* Re-fetch capabilities and userMetadata and update appData
*
* @param {import('./AppData.js').appData} appData appData
* @param {boolean} [persist] Persist after re-fetch
* @return {Promise<void>}
* @throws {Error}
*/
export async function refetchAppData(appData, persist = false) {
const [userMetadata, capabilitiesResponse] = await Promise.all([
getCurrentUserData(appData.serverUrl),
getCapabilities(appData.serverUrl),
])
const talkCapabilities = capabilitiesResponse.capabilities.spreed
appData.talkHash = null
appData.talkHashDirty = false
appData.userMetadata = userMetadata
appData.capabilities = capabilitiesResponse.capabilities
appData.version.nextcloud = capabilitiesResponse.version
appData.version.talk = talkCapabilities.version
appData.version.desktop = packageJson.version
if (persist) {
appData.persist()
}
}
/**
* Re-fetch capabilities and userMetadata with retry and update appData
*
* @param {import('./AppData.js').appData} appData appData
* @return {Promise<void>}
*/
export async function refetchAppDataWithRetry(appData) {
await new Promise((resolve) => {
/**
* Try to re-fetch appData
*
* @return {Promise<boolean>} true if re-fetch finished and should not be retried
*/
async function doRefetch() {
try {
await refetchAppData(appData, true)
console.debug('AppData re-fetched')
return true
} catch (error) {
if (error.response?.status === 401) {
appData.reset().persist()
console.debug('AppData credentials are invalid... Resetting')
return true
}
// Network error, maintenance, service unavailable etc.
// Let's try again later
console.debug(`Cannot get AppData... Error: ${error.code}. Response status: ${error.response?.status || 'unknown'}`)
return false
}
}
/**
* Recursively re-try a re-fetch attempt with a timeout
*
* @param {number} [delay] delay in milliseconds
*/
function retryRefetch(delay = 1_000) {
const MAX_DELAY = 2 ** 7 * 1000 // 128_000 = ~2 minutes
console.debug(`Retry in ${delay} ms...`)
setTimeout(async () => {
if (await doRefetch()) {
return resolve()
}
retryRefetch(delay < MAX_DELAY ? delay * 2 : delay)
}, delay)
}
doRefetch()
.then((success) => {
if (success) {
return resolve()
}
retryRefetch()
})
})
}