Skip to content

Commit

Permalink
fix: move version comparison to utils
Browse files Browse the repository at this point in the history
- post-review refactoring

Signed-off-by: Maksim Sukharev <antreesy.web@gmail.com>
  • Loading branch information
Antreesy committed Oct 11, 2024
1 parent a6ded79 commit 21c4620
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 16 deletions.
10 changes: 5 additions & 5 deletions src/components/CallView/CallView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,12 @@ import BrowserStorage from '../../services/BrowserStorage.js'
import { fetchPeers } from '../../services/callsService.js'
import { getTalkConfig } from '../../services/CapabilitiesManager.ts'
import { EventBus } from '../../services/EventBus.js'
import { satisfyVersion } from '../../utils/satisfyVersion.ts'
import { localMediaModel, localCallParticipantModel, callParticipantCollection } from '../../utils/webrtc/index.js'
import RemoteVideoBlocker from '../../utils/webrtc/RemoteVideoBlocker.js'
const serverVersion = loadState('core', 'config', {}).versionstring ?? '29.0.0'
const serverSupportsBackgroundBlurred = '29.0.4'.localeCompare(serverVersion) < 1
const serverVersion = loadState('core', 'config', {}).version ?? '29.0.0.0'
const serverSupportsBackgroundBlurred = satisfyVersion(serverVersion, '29.0.4.0')
export default {
name: 'CallView',
Expand Down Expand Up @@ -201,9 +202,8 @@ export default {
const screenshotMode = ref(false)
provide('CallView:screenshotModeEnabled', screenshotMode)
const isBackgroundBlurred = ref(serverSupportsBackgroundBlurred
? null
: BrowserStorage.getItem('background-blurred') !== 'false')
// Fallback ref for versions before v29.0.4
const isBackgroundBlurred = ref(BrowserStorage.getItem('background-blurred') !== 'false')
return {
localMediaModel,
Expand Down
22 changes: 11 additions & 11 deletions src/components/SettingsDialog/SettingsDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,10 @@ import { getTalkConfig } from '../../services/CapabilitiesManager.ts'
import { useCustomSettings } from '../../services/SettingsAPI.ts'
import { useSettingsStore } from '../../stores/settings.js'
import { useSoundsStore } from '../../stores/sounds.js'
import { satisfyVersion } from '../../utils/satisfyVersion.ts'
const serverVersion = loadState('core', 'config', {}).versionstring ?? '29.0.0'
const serverSupportsBackgroundBlurred = '29.0.4'.localeCompare(serverVersion) < 1
const serverVersion = loadState('core', 'config', {}).version ?? '29.0.0.0'
const serverSupportsBackgroundBlurred = satisfyVersion(serverVersion, '29.0.4.0')
const isBackgroundBlurredState = serverSupportsBackgroundBlurred
? loadState('spreed', 'force_enable_blur_filter', '') // 'yes', 'no', ''
Expand Down Expand Up @@ -313,11 +314,9 @@ export default {
{ configValue: 'no' })
}
BrowserStorage.removeItem('background-blurred')
} else {
} else if (blurred === null) {
// Fallback to BrowserStorage
if (blurred === null) {
BrowserStorage.setItem('background-blurred', 'true')
}
BrowserStorage.setItem('background-blurred', 'true')
}
},
Expand Down Expand Up @@ -374,12 +373,13 @@ export default {
this.privacyLoading = false
},
/**
* Fallback method for versions before v29.0.4
* @param {boolean} value whether background should be blurred
*/
toggleBackgroundBlurred(value) {
if (serverSupportsBackgroundBlurred) {
return
}
this.isBackgroundBlurred = value ? 'true' : 'false'
BrowserStorage.setItem('background-blurred', value)
this.isBackgroundBlurred = value.toString()
BrowserStorage.setItem('background-blurred', this.isBackgroundBlurred)
emit('set-background-blurred', value)
},
Expand Down
26 changes: 26 additions & 0 deletions src/utils/__tests__/satisfyVersion.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import { satisfyVersion } from '../satisfyVersion.ts'

describe('satisfyVersion', () => {
const testCases = [
// Older
['29.1.4.3', '28.2.5.4', true],
['29.1.4.3', '29.0.15.3', true],
['29.1.4.3', '29.1.1.3', true],
['29.1.4.3', '29.1.4.0', true],
// Exact
['29.1.4.3', '29.1.4.3', true],
// Newer
['29.1.4.3', '29.1.4.4', false],
['29.1.4.3', '29.1.5.0', false],
['29.1.4.3', '29.2.0.0', false],
['29.1.4.3', '30.0.0.0', false],
]

it.each(testCases)('check if %s should satisfy requirement %s returns %s', (a, b, c) => {
expect(satisfyVersion(a, b)).toBe(c)
})
})
26 changes: 26 additions & 0 deletions src/utils/satisfyVersion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

/**
* Checks if given version satisfy requirements (newer than)
* Versions are expected to have format like '29.1.4.3'
* @param version given version
* @param requirement version to compare against
*/
function satisfyVersion(version: string, requirement: string): boolean {
const versionMap = version.split('.').map(Number)
const requirementMap = requirement.split('.').map(Number)

for (let i = 0; i < requirementMap.length; i++) {
if (versionMap[i] !== requirementMap[i]) {
return versionMap[i] > requirementMap[i]
}
}
return true
}

export {
satisfyVersion,
}

0 comments on commit 21c4620

Please sign in to comment.