]> source.dussan.org Git - nextcloud-server.git/commitdiff
Cancel user search requests to avoid duplicate results being added
authorJulius Härtl <jus@bitgrid.net>
Wed, 16 Dec 2020 09:41:41 +0000 (10:41 +0100)
committerbackportbot[bot] <backportbot[bot]@users.noreply.github.com>
Wed, 16 Dec 2020 12:59:38 +0000 (12:59 +0000)
Signed-off-by: Julius Härtl <jus@bitgrid.net>
apps/settings/src/store/api.js
apps/settings/src/store/users.js

index ef2a51b2a156f2f8deb59c7ca9fe5905bca55ae5..9a03fe68ef932958db104a58ac11a5ea685be06c 100644 (file)
@@ -63,8 +63,8 @@ export default {
        requireAdmin() {
                return confirmPassword()
        },
-       get(url) {
-               return axios.get(sanitize(url))
+       get(url, options) {
+               return axios.get(sanitize(url), options)
        },
        post(url, data) {
                return axios.post(sanitize(url), data)
index e24996305a06b8d6ceddbbec6fcc80afb063b334..3f87f5b004078aca26260f5e6483859817935797 100644 (file)
@@ -21,6 +21,7 @@
  */
 
 import api from './api'
+import axios from '@nextcloud/axios'
 import { generateOcsUrl } from '@nextcloud/router'
 
 const orderGroups = function(groups, orderBy) {
@@ -189,6 +190,9 @@ const getters = {
        },
 }
 
+const CancelToken = axios.CancelToken
+let searchRequestCancelSource = null
+
 const actions = {
 
        /**
@@ -203,10 +207,16 @@ const actions = {
         * @returns {Promise}
         */
        getUsers(context, { offset, limit, search, group }) {
+               if (searchRequestCancelSource) {
+                       searchRequestCancelSource.cancel('Operation canceled by another search request.')
+               }
+               searchRequestCancelSource = CancelToken.source()
                search = typeof search === 'string' ? search : ''
                group = typeof group === 'string' ? group : ''
                if (group !== '') {
-                       return api.get(generateOcsUrl(`cloud/groups/${encodeURIComponent(encodeURIComponent(group))}/users/details?offset=${offset}&limit=${limit}&search=${search}`, 2))
+                       return api.get(generateOcsUrl(`cloud/groups/${encodeURIComponent(encodeURIComponent(group))}/users/details?offset=${offset}&limit=${limit}&search=${search}`, 2), {
+                               cancelToken: searchRequestCancelSource.token,
+                       })
                                .then((response) => {
                                        if (Object.keys(response.data.ocs.data.users).length > 0) {
                                                context.commit('appendUsers', response.data.ocs.data.users)
@@ -214,10 +224,16 @@ const actions = {
                                        }
                                        return false
                                })
-                               .catch((error) => context.commit('API_FAILURE', error))
+                               .catch((error) => {
+                                       if (!axios.isCancel(error)) {
+                                               context.commit('API_FAILURE', error)
+                                       }
+                               })
                }
 
-               return api.get(generateOcsUrl(`cloud/users/details?offset=${offset}&limit=${limit}&search=${search}`, 2))
+               return api.get(generateOcsUrl(`cloud/users/details?offset=${offset}&limit=${limit}&search=${search}`, 2), {
+                       cancelToken: searchRequestCancelSource.token,
+               })
                        .then((response) => {
                                if (Object.keys(response.data.ocs.data.users).length > 0) {
                                        context.commit('appendUsers', response.data.ocs.data.users)
@@ -225,7 +241,11 @@ const actions = {
                                }
                                return false
                        })
-                       .catch((error) => context.commit('API_FAILURE', error))
+                       .catch((error) => {
+                               if (!axios.isCancel(error)) {
+                                       context.commit('API_FAILURE', error)
+                               }
+                       })
        },
 
        getGroups(context, { offset, limit, search }) {