Skip to content

Commit

Permalink
#122 Fix crash during hashtag search (#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
mczachurski authored Oct 5, 2024
1 parent 09ad6b5 commit 243f9d5
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
25 changes: 22 additions & 3 deletions Sources/VernissageServer/Services/SearchService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down Expand Up @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 243f9d5

Please sign in to comment.