From 243f9d5e909149525f84fb4f228acb7171b4731b Mon Sep 17 00:00:00 2001 From: Marcin Czachurski Date: Sat, 5 Oct 2024 08:29:39 +0200 Subject: [PATCH] #122 Fix crash during hashtag search (#124) --- .../Services/SearchService.swift | 25 ++++++++++++++++--- .../SearchController/SearchActionTests.swift | 18 +++++++++++++ 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/Sources/VernissageServer/Services/SearchService.swift b/Sources/VernissageServer/Services/SearchService.swift index e682b30..2a693c4 100644 --- a/Sources/VernissageServer/Services/SearchService.swift +++ b/Sources/VernissageServer/Services/SearchService.swift @@ -180,16 +180,33 @@ final class SearchService: SearchServiceType { } private func searchByStatuses(query: String, on request: Request) -> SearchResultDto { + // For empty query we don't have to retrieve anything from database and return empty list. + if query.isEmpty { + return SearchResultDto(users: []) + } + + // TODO: Implement searching by statuses. return SearchResultDto(statuses: []) } private func searchByHashtags(query: String, on request: Request) -> SearchResultDto { + // For empty query we don't have to retrieve anything from database and return empty list. + if query.isEmpty { + return SearchResultDto(users: []) + } + + // TODO: Implement searching by tags. return SearchResultDto(hashtags: []) } private func searchByLocalUsers(query: String, on request: Request) async -> SearchResultDto { let usersService = request.application.services.usersService + // For empty query we don't have to retrieve anything from database and return empty list. + if query.isEmpty { + return SearchResultDto(users: []) + } + // In case of error we have to return empty list. guard let users = try? await usersService.search(query: query, on: request, page: 1, size: 20) else { request.logger.notice("Issue during filtering local users.") @@ -393,12 +410,14 @@ final class SearchService: SearchServiceType { private func isLocalSearch(query: String, on request: Request) -> Bool { let queryParts = query.split(separator: "@") - if queryParts.count == 1 { + if queryParts.count <= 1 { return true } - let applicationSettings = request.application.settings.cached! - if queryParts[1].uppercased() == applicationSettings.domain.uppercased() { + let applicationSettings = request.application.settings.cached + let domain = applicationSettings?.domain ?? "" + + if queryParts[1].uppercased() == domain.uppercased() { return true } diff --git a/Tests/VernissageServerTests/AcceptanceTests/SearchController/SearchActionTests.swift b/Tests/VernissageServerTests/AcceptanceTests/SearchController/SearchActionTests.swift index 4472288..6877156 100644 --- a/Tests/VernissageServerTests/AcceptanceTests/SearchController/SearchActionTests.swift +++ b/Tests/VernissageServerTests/AcceptanceTests/SearchController/SearchActionTests.swift @@ -76,6 +76,24 @@ extension ControllersTests { #expect((searchResultDto.users?.count ?? 0) == 0, "Empty list should be returned.") } + @Test("Empty search result should be returned when query has not been specified") + func emptySearchResultShouldBeReturnedWhenQueryHasNotBeenSpecified() async throws { + // Arrange. + _ = try await application.createUser(userName: "filipfinder") + + // Act. + let searchResultDto = try application.getResponse( + as: .user(userName: "filipfinder", password: "p@ssword"), + to: "/search?query=", + version: .v1, + decodeTo: SearchResultDto.self + ) + + // Assert. + #expect(searchResultDto.users != nil, "Users should be returned.") + #expect((searchResultDto.users?.count ?? 0) == 0, "Empty list should be returned.") + } + @Test("Search results should not be returned when query is not specified") func searchResultsShouldNotBeReturnedWhenQueryIsNotSpecified() async throws { // Arrange.