Merge branch '582' into 'develop'

Refactor user search api, better api error response handling

See merge request pleroma/pleroma-fe!844
This commit is contained in:
Shpuld Shpludson 2019-07-10 16:58:49 +00:00
commit cf2dc5b683
7 changed files with 31 additions and 79 deletions

View file

@ -35,15 +35,13 @@ const userSearch = {
}, },
search (query) { search (query) {
if (!query) { if (!query) {
this.users = []
return return
} }
this.loading = true this.loading = true
this.userIds = []
this.$store.dispatch('searchUsers', query) this.$store.dispatch('searchUsers', query)
.then((res) => { .then((res) => { this.userIds = map(res, 'id') })
this.loading = false .finally(() => { this.loading = false })
this.userIds = map(res, 'id')
})
} }
} }
} }

View file

@ -17,7 +17,6 @@ import Autosuggest from '../autosuggest/autosuggest.vue'
import Importer from '../importer/importer.vue' import Importer from '../importer/importer.vue'
import Exporter from '../exporter/exporter.vue' import Exporter from '../exporter/exporter.vue'
import withSubscription from '../../hocs/with_subscription/with_subscription' import withSubscription from '../../hocs/with_subscription/with_subscription'
import userSearchApi from '../../services/new_api/user_search.js'
import Mfa from './mfa.vue' import Mfa from './mfa.vue'
const BlockList = withSubscription({ const BlockList = withSubscription({
@ -322,11 +321,8 @@ const UserSettings = {
}) })
}, },
queryUserIds (query) { queryUserIds (query) {
return userSearchApi.search({ query, store: this.$store }) return this.$store.dispatch('searchUsers', query)
.then((users) => { .then((users) => map(users, 'id'))
this.$store.dispatch('addNewUsers', users)
return map(users, 'id')
})
}, },
blockUsers (ids) { blockUsers (ids) {
return this.$store.dispatch('blockUsers', ids) return this.$store.dispatch('blockUsers', ids)

View file

@ -1,5 +1,4 @@
import backendInteractorService from '../services/backend_interactor_service/backend_interactor_service.js' 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 oauthApi from '../services/new_api/oauth.js'
import { compact, map, each, merge, last, concat, uniq } from 'lodash' import { compact, map, each, merge, last, concat, uniq } from 'lodash'
import { set } from 'vue' import { set } from 'vue'
@ -356,14 +355,7 @@ const users = {
}) })
}, },
searchUsers (store, query) { searchUsers (store, query) {
// TODO: Move userSearch api into api.service return store.rootState.api.backendInteractor.searchUsers(query)
return userSearchApi.search({
query,
store: {
state: store.rootState,
getters: store.rootGetters
}
})
.then((users) => { .then((users) => {
store.commit('addNewUsers', users) store.commit('addNewUsers', users)
return users return users

View file

@ -65,6 +65,7 @@ const MASTODON_PROFILE_UPDATE_URL = '/api/v1/accounts/update_credentials'
const MASTODON_REPORT_USER_URL = '/api/v1/reports' const MASTODON_REPORT_USER_URL = '/api/v1/reports'
const MASTODON_PIN_OWN_STATUS = id => `/api/v1/statuses/${id}/pin` const MASTODON_PIN_OWN_STATUS = id => `/api/v1/statuses/${id}/pin`
const MASTODON_UNPIN_OWN_STATUS = id => `/api/v1/statuses/${id}/unpin` const MASTODON_UNPIN_OWN_STATUS = id => `/api/v1/statuses/${id}/unpin`
const MASTODON_USER_SEARCH_URL = '/api/v1/accounts/search'
const oldfetch = window.fetch const oldfetch = window.fetch
@ -76,7 +77,7 @@ let fetch = (url, options) => {
return oldfetch(fullUrl, options) return oldfetch(fullUrl, options)
} }
const promisedRequest = ({ method, url, payload, credentials, headers = {} }) => { const promisedRequest = ({ method, url, params, payload, credentials, headers = {} }) => {
const options = { const options = {
method, method,
headers: { headers: {
@ -85,6 +86,11 @@ const promisedRequest = ({ method, url, payload, credentials, headers = {} }) =>
...headers ...headers
} }
} }
if (params) {
url += '?' + Object.entries(params)
.map(([key, value]) => encodeURIComponent(key) + '=' + encodeURIComponent(value))
.join('&')
}
if (payload) { if (payload) {
options.body = JSON.stringify(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 = { const apiService = {
verifyCredentials, verifyCredentials,
fetchTimeline, fetchTimeline,
@ -899,7 +917,8 @@ const apiService = {
fetchFavoritedByUsers, fetchFavoritedByUsers,
fetchRebloggedByUsers, fetchRebloggedByUsers,
reportUser, reportUser,
updateNotificationSettings updateNotificationSettings,
searchUsers
} }
export default apiService export default apiService

View file

@ -147,6 +147,8 @@ const backendInteractorService = credentials => {
const retweet = (id) => apiService.retweet({ id, credentials }) const retweet = (id) => apiService.retweet({ id, credentials })
const unretweet = (id) => apiService.unretweet({ id, credentials }) const unretweet = (id) => apiService.unretweet({ id, credentials })
const searchUsers = (query) => apiService.searchUsers({ query, credentials })
const backendInteractorServiceInstance = { const backendInteractorServiceInstance = {
fetchStatus, fetchStatus,
fetchConversation, fetchConversation,
@ -205,7 +207,8 @@ const backendInteractorService = credentials => {
unfavorite, unfavorite,
retweet, retweet,
unretweet, unretweet,
updateNotificationSettings updateNotificationSettings,
searchUsers
} }
return backendInteractorServiceInstance return backendInteractorServiceInstance

View file

@ -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

View file

@ -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