From 532b76eb64119848d424d6d0d644a72d817fba59 Mon Sep 17 00:00:00 2001 From: Tae Hoon Date: Wed, 10 Jul 2019 16:58:49 +0000 Subject: [PATCH] Refactor user search api, better api error response handling --- src/components/user_search/user_search.js | 8 ++--- src/components/user_settings/user_settings.js | 8 ++--- src/modules/users.js | 10 +----- src/services/api/api.service.js | 23 ++++++++++-- .../backend_interactor_service.js | 5 ++- src/services/new_api/user_search.js | 20 ----------- src/services/new_api/utils.js | 36 ------------------- 7 files changed, 31 insertions(+), 79 deletions(-) delete mode 100644 src/services/new_api/user_search.js delete mode 100644 src/services/new_api/utils.js diff --git a/src/components/user_search/user_search.js b/src/components/user_search/user_search.js index 62dafdf1..5c29d8f2 100644 --- a/src/components/user_search/user_search.js +++ b/src/components/user_search/user_search.js @@ -35,15 +35,13 @@ const userSearch = { }, search (query) { if (!query) { - this.users = [] return } this.loading = true + this.userIds = [] this.$store.dispatch('searchUsers', query) - .then((res) => { - this.loading = false - this.userIds = map(res, 'id') - }) + .then((res) => { this.userIds = map(res, 'id') }) + .finally(() => { this.loading = false }) } } } diff --git a/src/components/user_settings/user_settings.js b/src/components/user_settings/user_settings.js index 19f4604f..0c3b2aef 100644 --- a/src/components/user_settings/user_settings.js +++ b/src/components/user_settings/user_settings.js @@ -17,7 +17,6 @@ import Autosuggest from '../autosuggest/autosuggest.vue' import Importer from '../importer/importer.vue' import Exporter from '../exporter/exporter.vue' import withSubscription from '../../hocs/with_subscription/with_subscription' -import userSearchApi from '../../services/new_api/user_search.js' import Mfa from './mfa.vue' const BlockList = withSubscription({ @@ -322,11 +321,8 @@ const UserSettings = { }) }, queryUserIds (query) { - return userSearchApi.search({ query, store: this.$store }) - .then((users) => { - this.$store.dispatch('addNewUsers', users) - return map(users, 'id') - }) + return this.$store.dispatch('searchUsers', query) + .then((users) => map(users, 'id')) }, blockUsers (ids) { return this.$store.dispatch('blockUsers', ids) diff --git a/src/modules/users.js b/src/modules/users.js index 6c7a96f6..453a6899 100644 --- a/src/modules/users.js +++ b/src/modules/users.js @@ -1,5 +1,4 @@ import backendInteractorService from '../services/backend_interactor_service/backend_interactor_service.js' -import userSearchApi from '../services/new_api/user_search.js' import oauthApi from '../services/new_api/oauth.js' import { compact, map, each, merge, last, concat, uniq } from 'lodash' import { set } from 'vue' @@ -356,14 +355,7 @@ const users = { }) }, searchUsers (store, query) { - // TODO: Move userSearch api into api.service - return userSearchApi.search({ - query, - store: { - state: store.rootState, - getters: store.rootGetters - } - }) + return store.rootState.api.backendInteractor.searchUsers(query) .then((users) => { store.commit('addNewUsers', users) return users diff --git a/src/services/api/api.service.js b/src/services/api/api.service.js index 22db671e..304fc869 100644 --- a/src/services/api/api.service.js +++ b/src/services/api/api.service.js @@ -65,6 +65,7 @@ const MASTODON_PROFILE_UPDATE_URL = '/api/v1/accounts/update_credentials' const MASTODON_REPORT_USER_URL = '/api/v1/reports' const MASTODON_PIN_OWN_STATUS = id => `/api/v1/statuses/${id}/pin` const MASTODON_UNPIN_OWN_STATUS = id => `/api/v1/statuses/${id}/unpin` +const MASTODON_USER_SEARCH_URL = '/api/v1/accounts/search' const oldfetch = window.fetch @@ -76,7 +77,7 @@ let fetch = (url, options) => { return oldfetch(fullUrl, options) } -const promisedRequest = ({ method, url, payload, credentials, headers = {} }) => { +const promisedRequest = ({ method, url, params, payload, credentials, headers = {} }) => { const options = { method, headers: { @@ -85,6 +86,11 @@ const promisedRequest = ({ method, url, payload, credentials, headers = {} }) => ...headers } } + if (params) { + url += '?' + Object.entries(params) + .map(([key, value]) => encodeURIComponent(key) + '=' + encodeURIComponent(value)) + .join('&') + } if (payload) { options.body = JSON.stringify(payload) } @@ -837,6 +843,18 @@ const reportUser = ({ credentials, userId, statusIds, comment, forward }) => { }) } +const searchUsers = ({ credentials, query }) => { + return promisedRequest({ + url: MASTODON_USER_SEARCH_URL, + params: { + q: query, + resolve: true + }, + credentials + }) + .then((data) => data.map(parseUser)) +} + const apiService = { verifyCredentials, fetchTimeline, @@ -899,7 +917,8 @@ const apiService = { fetchFavoritedByUsers, fetchRebloggedByUsers, reportUser, - updateNotificationSettings + updateNotificationSettings, + searchUsers } export default apiService diff --git a/src/services/backend_interactor_service/backend_interactor_service.js b/src/services/backend_interactor_service/backend_interactor_service.js index 3550aafd..5162d38f 100644 --- a/src/services/backend_interactor_service/backend_interactor_service.js +++ b/src/services/backend_interactor_service/backend_interactor_service.js @@ -147,6 +147,8 @@ const backendInteractorService = credentials => { const retweet = (id) => apiService.retweet({ id, credentials }) const unretweet = (id) => apiService.unretweet({ id, credentials }) + const searchUsers = (query) => apiService.searchUsers({ query, credentials }) + const backendInteractorServiceInstance = { fetchStatus, fetchConversation, @@ -205,7 +207,8 @@ const backendInteractorService = credentials => { unfavorite, retweet, unretweet, - updateNotificationSettings + updateNotificationSettings, + searchUsers } return backendInteractorServiceInstance diff --git a/src/services/new_api/user_search.js b/src/services/new_api/user_search.js deleted file mode 100644 index 5936fef9..00000000 --- a/src/services/new_api/user_search.js +++ /dev/null @@ -1,20 +0,0 @@ -import utils from './utils.js' -import { parseUser } from '../entity_normalizer/entity_normalizer.service.js' - -const search = ({ query, store }) => { - return utils.request({ - store, - url: '/api/v1/accounts/search', - params: { - q: query, - resolve: true - } - }) - .then((data) => data.json()) - .then((data) => data.map(parseUser)) -} -const UserSearch = { - search -} - -export default UserSearch diff --git a/src/services/new_api/utils.js b/src/services/new_api/utils.js deleted file mode 100644 index 57111026..00000000 --- a/src/services/new_api/utils.js +++ /dev/null @@ -1,36 +0,0 @@ -const queryParams = (params) => { - return Object.keys(params) - .map(k => encodeURIComponent(k) + '=' + encodeURIComponent(params[k])) - .join('&') -} - -const headers = (store) => { - const accessToken = store.getters.getToken() - if (accessToken) { - return { 'Authorization': `Bearer ${accessToken}` } - } else { - return {} - } -} - -const request = ({ method = 'GET', url, params, store }) => { - const instance = store.state.instance.server - let fullUrl = `${instance}${url}` - - if (method === 'GET' && params) { - fullUrl = fullUrl + `?${queryParams(params)}` - } - - return window.fetch(fullUrl, { - method, - headers: headers(store), - credentials: 'same-origin' - }) -} - -const utils = { - queryParams, - request -} - -export default utils