Skip to content

Commit

Permalink
Only local user filter for users
Browse files Browse the repository at this point in the history
  • Loading branch information
mczachurski committed Nov 3, 2024
1 parent 28ec19d commit 02b523f
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 2 deletions.
2 changes: 0 additions & 2 deletions Sources/VernissageServer/Controllers/HealthController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,6 @@ struct HealthController {
return true
} catch {
await request.logger.store("WebPush service health check error.", error, on: request.application)
await request.logger.store("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse eget magna eu felis viverra luctus. Donec vehicula lectus urna, ac facilisis leo sodales nec. Fusce id blandit turpis. Ut vestibulum odio ut neque semper ornare. Ut at diam nisl. Curabitur mollis lacus elit, eu placerat neque aliquam in. Quisque non eros velit. Duis posuere dui eget interdum vestibulum. Sed laoreet semper rutrum.", error, on: request.application)

return false
}
}
Expand Down
6 changes: 6 additions & 0 deletions Sources/VernissageServer/Controllers/UsersController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ struct UsersController {
let page: Int = request.query["page"] ?? 0
let size: Int = request.query["size"] ?? 10
let query: String? = request.query["query"] ?? nil
let onlyLocal: Bool = request.query["onlyLocal"] ?? false

let usersFromDatabaseQueryBuilder = User.query(on: request.db)
.with(\.$flexiFields)
Expand All @@ -241,6 +242,11 @@ struct UsersController {
.filter(\.$name ~~ query)
}
}

if onlyLocal {
usersFromDatabaseQueryBuilder
.filter(\.$isLocal == true)
}

let usersFromDatabase = try await usersFromDatabaseQueryBuilder
.sort(\.$createdAt, .descending)
Expand Down
6 changes: 6 additions & 0 deletions Sources/VernissageServer/DataTransferObjects/UserDto.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ struct UserDto: Codable {
var activityPubProfile: String
var fields: [FlexiFieldDto]?
var bioHtml: String?
var lastLoginDate: Date?
var createdAt: Date?
var updatedAt: Date?
var roles: [String]?
Expand Down Expand Up @@ -55,6 +56,7 @@ struct UserDto: Codable {
case fields
case bioHtml
case activityPubProfile
case lastLoginDate
case createdAt
case updatedAt
case roles
Expand Down Expand Up @@ -82,6 +84,7 @@ struct UserDto: Codable {
activityPubProfile: String = "",
fields: [FlexiFieldDto]? = nil,
roles: [String]? = nil,
lastLoginDate: Date? = nil,
createdAt: Date? = nil,
updatedAt: Date? = nil,
baseAddress: String,
Expand All @@ -103,6 +106,7 @@ struct UserDto: Codable {
self.fields = fields
self.activityPubProfile = activityPubProfile
self.bioHtml = self.isLocal ? self.bio?.html(baseAddress: baseAddress, wrapInParagraph: true) : self.bio
self.lastLoginDate = lastLoginDate
self.createdAt = createdAt
self.updatedAt = updatedAt
self.roles = roles
Expand Down Expand Up @@ -137,6 +141,7 @@ struct UserDto: Codable {
locale = try values.decodeIfPresent(String.self, forKey: .locale)
fields = try values.decodeIfPresent([FlexiFieldDto].self, forKey: .fields) ?? []
activityPubProfile = try values.decodeIfPresent(String.self, forKey: .activityPubProfile) ?? ""
lastLoginDate = try values.decodeIfPresent(Date.self, forKey: .lastLoginDate)
createdAt = try values.decodeIfPresent(Date.self, forKey: .createdAt)
updatedAt = try values.decodeIfPresent(Date.self, forKey: .updatedAt)
roles = try values.decodeIfPresent([String].self, forKey: .roles)
Expand Down Expand Up @@ -167,6 +172,7 @@ struct UserDto: Codable {
try container.encodeIfPresent(fields, forKey: .fields)
try container.encodeIfPresent(bioHtml, forKey: .bioHtml)
try container.encodeIfPresent(activityPubProfile, forKey: .activityPubProfile)
try container.encodeIfPresent(lastLoginDate, forKey: .lastLoginDate)
try container.encodeIfPresent(createdAt, forKey: .createdAt)
try container.encodeIfPresent(updatedAt, forKey: .updatedAt)
try container.encodeIfPresent(roles, forKey: .roles)
Expand Down
1 change: 1 addition & 0 deletions Sources/VernissageServer/Services/UsersService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,7 @@ final class UsersService: UsersServiceType {
userDto.isApproved = user.isApproved
userDto.twoFactorEnabled = user.twoFactorEnabled
userDto.manuallyApprovesFollowers = user.manuallyApprovesFollowers
userDto.lastLoginDate = user.lastLoginDate
}

return userDto
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,30 @@ extension ControllersTests {
#expect(users.data.first?.userName == "karolfux", "Correct user should be filtered")
}

@Test("Filtered list of users should be returned when local filter is applied")
func filteredListOfUsersShouldBeReturnedWhenLocalFilterIsApplied() async throws {

// Arrange.
_ = try await application.createUser(userName: "g0rg1_1fux", isLocal: true)
_ = try await application.createUser(userName: "g0rg1_2fux", isLocal: false)

let user = try await application.createUser(userName: "marianfux")
try await application.attach(user: user, role: Role.moderator)

// Act.
let users = try application.getResponse(
as: .user(userName: "marianfux", password: "p@ssword"),
to: "/users?query=g0rg1&onlyLocal=true",
method: .GET,
decodeTo: PaginableResultDto<UserDto>.self
)

// Assert.
#expect(users != nil, "Users should be returned.")
#expect(users.data.count == 1, "Filtered user should be returned.")
#expect(users.data.first?.userName == "g0rg1_1fux", "Correct user should be filtered")
}

@Test("Forbidden shouldbe returned for regular user")
func forbiddenShouldbeReturnedForRegularUser() async throws {

Expand Down

0 comments on commit 02b523f

Please sign in to comment.