From c1e1623da5c285d7a9f4226d4c212d7bd8634866 Mon Sep 17 00:00:00 2001 From: Angelina Filippova Date: Fri, 29 Mar 2019 14:25:53 +0000 Subject: [PATCH] Add more users filters --- src/api/__mocks__/users.js | 30 ++++-- src/api/users.js | 8 +- src/lang/en.js | 9 ++ src/store/modules/users.js | 32 +++++-- src/views/users/components/UsersFilter.vue | 72 +++++++++++++++ src/views/users/index.vue | 101 +++++++++++++-------- test/views/users/index.test.js | 65 +------------ test/views/users/usersFilters.test.js | 91 +++++++++++++++++++ 8 files changed, 291 insertions(+), 117 deletions(-) create mode 100644 src/views/users/components/UsersFilter.vue create mode 100644 test/views/users/usersFilters.test.js diff --git a/src/api/__mocks__/users.js b/src/api/__mocks__/users.js index 73a93174..b763c92d 100644 --- a/src/api/__mocks__/users.js +++ b/src/api/__mocks__/users.js @@ -1,11 +1,27 @@ const users = [ - { deactivated: false, id: '2', nickname: 'allis', local: true, roles: { admin: true, moderator: false }, tags: [] }, - { deactivated: false, id: '10', nickname: 'bob', local: false, roles: { admin: false, moderator: true }, tags: ['sandbox'] }, - { deactivated: true, id: 'abc', nickname: 'john', local: true, roles: { admin: false, moderator: false }, tags: ['strip_media'] } + { active: true, deactivated: false, id: '2', nickname: 'allis', local: true, external: false, roles: { admin: true, moderator: false }, tags: [] }, + { active: true, deactivated: false, id: '10', nickname: 'bob', local: false, external: true, roles: { admin: false, moderator: true }, tags: ['sandbox'] }, + { active: false, deactivated: true, id: 'abc', nickname: 'john', local: true, external: false, roles: { admin: false, moderator: false }, tags: ['strip_media'] } ] -export async function fetchUsers(showLocalUsersOnly, authHost, token, page = 1) { - const filteredUsers = showLocalUsersOnly ? users.filter(user => user.local) : users +const filterUsers = (str) => { + const filters = str.split(',').filter(item => item.length > 0) + if (filters.length === 0) { + return users + } + const applyFilters = (acc, filters, users) => { + if (filters.length === 0) { + return acc + } + const filteredUsers = users.filter(user => user[filters[0]]) + const newAcc = [...filteredUsers] + return applyFilters(newAcc, filters.slice(1), filteredUsers) + } + return applyFilters([], filters, users) +} + +export async function fetchUsers(filters, authHost, token, page = 1) { + const filteredUsers = filterUsers(filters) return Promise.resolve({ data: { users: filteredUsers, count: filteredUsers.length, @@ -18,8 +34,8 @@ export async function toggleUserActivation(nickname, authHost, token) { return Promise.resolve({ data: { ...response, deactivated: !response.deactivated }}) } -export async function searchUsers(query, showLocalUsersOnly, authHost, token, page = 1) { - const filteredUsers = showLocalUsersOnly ? users.filter(user => user.local) : users +export async function searchUsers(query, filters, authHost, token, page = 1) { + const filteredUsers = filterUsers(filters) const response = filteredUsers.filter(user => user.nickname === query) return Promise.resolve({ data: { users: response, diff --git a/src/api/users.js b/src/api/users.js index 2ec6a2ab..4b26c162 100644 --- a/src/api/users.js +++ b/src/api/users.js @@ -2,10 +2,10 @@ import request from '@/utils/request' import { getToken } from '@/utils/auth' import { baseName } from './utils' -export async function fetchUsers(showLocalUsersOnly, authHost, token, page = 1) { +export async function fetchUsers(filters, authHost, token, page = 1) { return await request({ baseURL: baseName(authHost), - url: `/api/pleroma/admin/users?page=${page}&local_only=${showLocalUsersOnly}`, + url: `/api/pleroma/admin/users?page=${page}&filters=${filters}`, method: 'get', headers: authHeaders(token) }) @@ -20,10 +20,10 @@ export async function toggleUserActivation(nickname, authHost, token) { }) } -export async function searchUsers(query, showLocalUsersOnly, authHost, token, page = 1) { +export async function searchUsers(query, filters, authHost, token, page = 1) { return await request({ baseURL: baseName(authHost), - url: `/api/pleroma/admin/users?query=${query}&page=${page}&local_only=${showLocalUsersOnly}`, + url: `/api/pleroma/admin/users?query=${query}&page=${page}&filters=${filters}`, method: 'get', headers: authHeaders(token) }) diff --git a/src/lang/en.js b/src/lang/en.js index 1c4097e7..4ec4b96c 100644 --- a/src/lang/en.js +++ b/src/lang/en.js @@ -189,5 +189,14 @@ export default { sandbox: 'Force posts to be followers-only', disableRemoteSubscription: 'Disallow following user from remote instances', disableAnySubscription: 'Disallow following user at all' + }, + usersFilter: { + inputPlaceholder: 'Select filter', + byUserType: 'By user type', + local: 'Local', + external: 'External', + byStatus: 'By status', + active: 'Active', + deactivated: 'Deactivated' } } diff --git a/src/store/modules/users.js b/src/store/modules/users.js index 09854c59..56eb07a0 100644 --- a/src/store/modules/users.js +++ b/src/store/modules/users.js @@ -7,7 +7,12 @@ const users = { searchQuery: '', totalUsersCount: 0, currentPage: 1, - showLocalUsersOnly: false + filters: { + local: false, + external: false, + active: false, + deactivated: false + } }, mutations: { SET_USERS: (state, users) => { @@ -37,13 +42,14 @@ const users = { SET_SEARCH_QUERY: (state, query) => { state.searchQuery = query }, - SET_LOCAL_USERS_FILTER: (state, value) => { - state.showLocalUsersOnly = value + SET_USERS_FILTERS: (state, filters) => { + state.filters = filters } }, actions: { async FetchUsers({ commit, state, getters }, { page }) { - const response = await fetchUsers(state.showLocalUsersOnly, getters.authHost, getters.token, page) + const filters = Object.keys(state.filters).filter(filter => state.filters[filter]).join() + const response = await fetchUsers(filters, getters.authHost, getters.token, page) commit('SET_LOADING', true) @@ -62,13 +68,25 @@ const users = { commit('SET_LOADING', true) commit('SET_SEARCH_QUERY', query) - const response = await searchUsers(query, state.showLocalUsersOnly, getters.authHost, getters.token, page) + const filters = Object.keys(state.filters).filter(filter => state.filters[filter]).join() + const response = await searchUsers(query, filters, getters.authHost, getters.token, page) loadUsers(commit, page, response.data) } }, - async ToggleLocalUsersFilter({ commit, dispatch, state }, value) { - commit('SET_LOCAL_USERS_FILTER', value) + async ToggleUsersFilter({ commit, dispatch, state }, filters) { + const defaultFilters = { + local: false, + external: false, + active: false, + deactivated: false + } + const currentFilters = { ...defaultFilters, ...filters } + commit('SET_USERS_FILTERS', currentFilters) + dispatch('SearchUsers', { query: state.searchQuery, page: 1 }) + }, + async ClearFilters({ commit, dispatch, state }) { + commit('CLEAR_USERS_FILTERS') dispatch('SearchUsers', { query: state.searchQuery, page: 1 }) }, async ToggleRight({ commit, getters }, { user, right }) { diff --git a/src/views/users/components/UsersFilter.vue b/src/views/users/components/UsersFilter.vue new file mode 100644 index 00000000..edb3d75d --- /dev/null +++ b/src/views/users/components/UsersFilter.vue @@ -0,0 +1,72 @@ + + + + + diff --git a/src/views/users/index.vue b/src/views/users/index.vue index f27ac791..87e76910 100644 --- a/src/views/users/index.vue +++ b/src/views/users/index.vue @@ -2,7 +2,7 @@

{{ $t('users.users') }}

- {{ $t('users.localUsersOnly') }} +
@@ -37,41 +37,65 @@ - + {{ scope.row.roles.admin ? $t('users.revokeAdmin') : $t('users.grantAdmin') }} - + {{ scope.row.roles.moderator ? $t('users.revokeModerator') : $t('users.grantModerator') }} - + {{ scope.row.deactivated ? $t('users.activateAccount') : $t('users.deactivateAccount') }} - + {{ $t('users.deleteAccount') }} - + {{ $t('users.forceNsfw') }} - + - + {{ $t('users.stripMedia') }} - + - + {{ $t('users.forceUnlisted') }} - + - + {{ $t('users.sandbox') }} - + - + {{ $t('users.disableRemoteSubscription') }} - + - + {{ $t('users.disableAnySubscription') }} - + @@ -93,9 +117,13 @@