diff --git a/server/src/graphql/resolvers.js b/server/src/graphql/resolvers.js index c9d0f2211..1d58dea88 100644 --- a/server/src/graphql/resolvers.js +++ b/server/src/graphql/resolvers.js @@ -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 [] } @@ -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 [] } @@ -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 [] } diff --git a/server/src/models/Gym.js b/server/src/models/Gym.js index 69f394f25..f05089286 100644 --- a/server/src/models/Gym.js +++ b/server/src/models/Gym.js @@ -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', @@ -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) { diff --git a/server/src/models/Pokestop.js b/server/src/models/Pokestop.js index 1ec6a40d8..43d9fc99c 100644 --- a/server/src/models/Pokestop.js +++ b/server/src/models/Pokestop.js @@ -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', @@ -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)) { diff --git a/server/src/models/Portal.js b/server/src/models/Portal.js index b3e9813de..043aa6f6c 100644 --- a/server/src/models/Portal.js +++ b/server/src/models/Portal.js @@ -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( diff --git a/server/src/services/DbCheck.js b/server/src/services/DbCheck.js index 0222f7385..07056118f 100644 --- a/server/src/services/DbCheck.js +++ b/server/src/services/DbCheck.js @@ -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') diff --git a/src/components/layout/dialogs/search/useSendSearch.js b/src/components/layout/dialogs/search/useSendSearch.js index ca982248d..4f04323b1 100644 --- a/src/components/layout/dialogs/search/useSendSearch.js +++ b/src/components/layout/dialogs/search/useSendSearch.js @@ -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()) }