Do not allow to find by name in findUser()

This commit is contained in:
Tusooa Zhu 2022-08-10 12:17:18 -04:00
parent 09f9640be1
commit ab4a75bdd9
No known key found for this signature in database
GPG Key ID: 7B467EDE43A08224
4 changed files with 54 additions and 17 deletions

View File

@ -110,7 +110,7 @@ const UserProfile = {
const maybeName = userNameOrId.name const maybeName = userNameOrId.name
// Check if user data is already loaded in store // Check if user data is already loaded in store
const user = this.$store.getters.findUser(maybeId || maybeName) const user = maybeId ? this.$store.getters.findUser(maybeId) : this.$store.getters.findUserByName(maybeName)
if (user) { if (user) {
loadById(user.id) loadById(user.id)
} else { } else {

View File

@ -16,9 +16,6 @@ export const mergeOrAdd = (arr, obj, item) => {
// This is a new item, prepare it // This is a new item, prepare it
arr.push(item) arr.push(item)
obj[item.id] = item obj[item.id] = item
if (item.screen_name && !item.screen_name.includes('@')) {
obj[item.screen_name.toLowerCase()] = item
}
return { item, new: true } return { item, new: true }
} }
} }
@ -162,7 +159,11 @@ export const mutations = {
if (user.relationship) { if (user.relationship) {
state.relationships[user.relationship.id] = user.relationship state.relationships[user.relationship.id] = user.relationship
} }
mergeOrAdd(state.users, state.usersObject, user) const res = mergeOrAdd(state.users, state.usersObject, user)
const item = res.item
if (res.new && item.screen_name && !item.screen_name.includes('@')) {
state.usersByNameObject[item.screen_name.toLowerCase()] = item
}
}) })
}, },
updateUserRelationship (state, relationships) { updateUserRelationship (state, relationships) {
@ -239,12 +240,10 @@ export const mutations = {
export const getters = { export const getters = {
findUser: state => query => { findUser: state => query => {
const result = state.usersObject[query] return state.usersObject[query]
// In case it's a screen_name, we can try searching case-insensitive },
if (!result && typeof query === 'string') { findUserByName: state => query => {
return state.usersObject[query.toLowerCase()] return state.usersByNameObject[query.toLowerCase()]
}
return result
}, },
findUserByUrl: state => query => { findUserByUrl: state => query => {
return state.users return state.users
@ -263,6 +262,7 @@ export const defaultState = {
currentUser: false, currentUser: false,
users: [], users: [],
usersObject: {}, usersObject: {},
usersByNameObject: {},
signUpPending: false, signUpPending: false,
signUpErrors: [], signUpErrors: [],
relationships: {} relationships: {}

View File

@ -15,6 +15,7 @@ const actions = {
const testGetters = { const testGetters = {
findUser: state => getters.findUser(state.users), findUser: state => getters.findUser(state.users),
findUserByName: state => getters.findUserByName(state.users),
relationship: state => getters.relationship(state.users), relationship: state => getters.relationship(state.users),
mergedConfig: state => ({ mergedConfig: state => ({
colors: '', colors: '',
@ -95,6 +96,7 @@ const externalProfileStore = createStore({
credentials: '' credentials: ''
}, },
usersObject: { 100: extUser }, usersObject: { 100: extUser },
usersByNameObject: {},
users: [extUser], users: [extUser],
relationships: {} relationships: {}
} }
@ -163,7 +165,8 @@ const localProfileStore = createStore({
currentUser: { currentUser: {
credentials: '' credentials: ''
}, },
usersObject: { 100: localUser, testuser: localUser }, usersObject: { 100: localUser },
usersByNameObject: { testuser: localUser },
users: [localUser], users: [localUser],
relationships: {} relationships: {}
} }

View File

@ -57,24 +57,27 @@ describe('The users module', () => {
}) })
describe('findUser', () => { describe('findUser', () => {
it('returns user with matching screen_name', () => { it('does not return user with matching screen_name', () => {
const user = { screen_name: 'Guy', id: '1' } const user = { screen_name: 'Guy', id: '1' }
const state = { const state = {
usersObject: { usersObject: {
1: user, 1: user
},
usersByNameObject: {
guy: user guy: user
} }
} }
const name = 'Guy' const name = 'Guy'
const expected = { screen_name: 'Guy', id: '1' } expect(getters.findUser(state)(name)).to.eql(undefined)
expect(getters.findUser(state)(name)).to.eql(expected)
}) })
it('returns user with matching id', () => { it('returns user with matching id', () => {
const user = { screen_name: 'Guy', id: '1' } const user = { screen_name: 'Guy', id: '1' }
const state = { const state = {
usersObject: { usersObject: {
1: user, 1: user
},
usersByNameObject: {
guy: user guy: user
} }
} }
@ -83,4 +86,35 @@ describe('The users module', () => {
expect(getters.findUser(state)(id)).to.eql(expected) expect(getters.findUser(state)(id)).to.eql(expected)
}) })
}) })
describe('findUserByName', () => {
it('returns user with matching screen_name', () => {
const user = { screen_name: 'Guy', id: '1' }
const state = {
usersObject: {
1: user
},
usersByNameObject: {
guy: user
}
}
const name = 'Guy'
const expected = { screen_name: 'Guy', id: '1' }
expect(getters.findUserByName(state)(name)).to.eql(expected)
})
it('does not return user with matching id', () => {
const user = { screen_name: 'Guy', id: '1' }
const state = {
usersObject: {
1: user
},
usersByNameObject: {
guy: user
}
}
const id = '1'
expect(getters.findUserByName(state)(id)).to.eql(undefined)
})
})
}) })