Skip to content

Commit

Permalink
fix: less strict searching for '
Browse files Browse the repository at this point in the history
  • Loading branch information
TurtIeSocks committed Feb 23, 2024
1 parent 6ec1589 commit ca08a69
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 52 deletions.
80 changes: 39 additions & 41 deletions server/src/graphql/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -390,48 +390,46 @@ const resolvers = {
},
search: async (_, args, { Event, perms, Db }) => {
const { category, webhookName, search } = args
if (perms?.[category] && /^[0-9\s\p{L}]+$/u.test(search)) {
if (!search || !search.trim()) {
return []
}
switch (args.category) {
case 'pokemon':
return Db.search('Pokemon', perms, args)
case 'pokestops':
return Db.search('Pokestop', perms, args)
case 'raids':
return Db.search('Gym', perms, args, 'searchRaids')
case 'gyms': {
const results = await Db.search('Gym', perms, args)
const webhook = webhookName ? Event.webhookObj[webhookName] : null
if (webhook && results.length) {
const withFormatted = await Promise.all(
results.map(async (result) => ({
...result,
formatted: await geocoder(
webhook.nominatimUrl,
{ lat: result.lat, lon: result.lon },
true,
),
})),
)
return withFormatted
}
return results
if (!search || !search.trim()) {
return []
}
switch (category) {
case 'pokemon':
return perms.pokemon ? Db.search('Pokemon', perms, args) : []
case 'pokestops':
return perms.pokestops ? Db.search('Pokestop', perms, args) : []
case 'raids':
return perms.raids ? Db.search('Gym', perms, args, 'searchRaids') : []
case 'gyms': {
if (!perms.gyms) return []
const results = await Db.search('Gym', perms, args)
const webhook = webhookName ? Event.webhookObj[webhookName] : null
if (webhook && results.length) {
const withFormatted = await Promise.all(
results.map(async (result) => ({
...result,
formatted: await geocoder(
webhook.nominatimUrl,
{ lat: result.lat, lon: result.lon },
true,
),
})),
)
return withFormatted
}
case 'portals':
return Db.search('Portal', perms, args)
case 'nests':
return Db.search('Nest', perms, args)
default:
return []
return results
}
case 'portals':
return perms.portals ? Db.search('Portal', perms, args) : []
case 'nests':
return perms.nests ? Db.search('Nest', perms, args) : []
default:
return []
}
return []
},
searchInvasion: (_, args, { perms, Db }) => {
const { category, search } = args
if (perms?.[category] && /^[0-9\s\p{L}]+$/u.test(search)) {
const { search } = args
if (perms?.invasions) {
if (!search || !search.trim()) {
return []
}
Expand All @@ -440,8 +438,8 @@ const resolvers = {
return []
},
searchLure: (_, args, { perms, Db }) => {
const { category, search } = args
if (perms?.[category] && /^[0-9\s\p{L}]+$/u.test(search)) {
const { search } = args
if (perms.lures) {
if (!search || !search.trim()) {
return []
}
Expand All @@ -450,8 +448,8 @@ const resolvers = {
return []
},
searchQuest: (_, args, { perms, Db }) => {
const { category, search } = args
if (perms?.[category] && /^[0-9\s\p{L}]+$/u.test(search)) {
const { search } = args
if (perms.quests) {
if (!search || !search.trim()) {
return []
}
Expand Down
4 changes: 2 additions & 2 deletions server/src/models/Gym.js
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ class Gym extends Model {

static async search(perms, args, { isMad }, distance, bbox) {
const { areaRestrictions } = perms
const { onlyAreas = [], search } = args
const { onlyAreas = [], search = '' } = args
const query = this.query()
.select([
'name',
Expand All @@ -473,7 +473,7 @@ class Gym extends Model {
])
.whereBetween(isMad ? 'latitude' : 'lat', [bbox.minLat, bbox.maxLat])
.andWhereBetween(isMad ? 'longitude' : 'lon', [bbox.minLon, bbox.maxLon])
.whereRaw(`LOWER(name) LIKE '%${search}%'`)
.whereILike('name', `%${search}%`)
.limit(searchResultsLimit)
.orderBy('distance')
if (isMad) {
Expand Down
4 changes: 2 additions & 2 deletions server/src/models/Pokestop.js
Original file line number Diff line number Diff line change
Expand Up @@ -1612,7 +1612,7 @@ class Pokestop extends Model {
}

static async search(perms, args, { isMad }, distance, bbox) {
const { onlyAreas = [], search } = args
const { onlyAreas = [], search = '' } = args
const query = this.query()
.select([
'name',
Expand All @@ -1624,7 +1624,7 @@ class Pokestop extends Model {
])
.whereBetween(isMad ? 'latitude' : 'lat', [bbox.minLat, bbox.maxLat])
.andWhereBetween(isMad ? 'longitude' : 'lon', [bbox.minLon, bbox.maxLon])
.whereRaw(`LOWER(name) LIKE '%${search}%'`)
.whereILike('name', `%${search}%`)
.limit(searchResultsLimit)
.orderBy('distance')
if (!getAreaSql(query, perms.areaRestrictions, onlyAreas, isMad)) {
Expand Down
4 changes: 2 additions & 2 deletions server/src/models/Portal.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ class Portal extends Model {
*/
static async search(perms, args, { isMad }, distance, bbox) {
const { areaRestrictions } = perms
const { onlyAreas = [], search } = args
const { onlyAreas = [], search = '' } = args
const query = this.query()
.select(['name', 'id', 'lat', 'lon', 'url', distance])
.whereRaw(`LOWER(name) LIKE '%${search}%'`)
.whereILike('name', `%${search}%`)
.whereBetween(isMad ? 'latitude' : 'lat', [bbox.minLat, bbox.maxLat])
.andWhereBetween(isMad ? 'longitude' : 'lon', [bbox.minLon, bbox.maxLon])
.andWhere(
Expand Down
2 changes: 1 addition & 1 deletion server/src/services/DbCheck.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ const { knex } = require('knex')
const { raw } = require('objection')
const extend = require('extend')
const config = require('@rm/config')

const { log, HELPERS } = require('@rm/logger')

const { getBboxFromCenter } = require('./functions/getBbox')
const { getCache } = require('./cache')

Expand Down
5 changes: 1 addition & 4 deletions src/components/layout/dialogs/search/useSendSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,7 @@ export function useSendSearch(search, open) {
/** @type {import('@mui/material').AutocompleteProps['onInputChange']} */
const handleInputChange = useCallback(
(e, newValue) => {
if (
e?.type === 'change' &&
(/^[0-9\s\p{L}]+$/u.test(newValue) || newValue === '')
) {
if (e?.type === 'change') {
useStorage.setState({ search: newValue.toLowerCase() })
debounceChange(newValue.toLowerCase())
}
Expand Down

0 comments on commit ca08a69

Please sign in to comment.