feat: Better styling on web pages, bad-words check now has less edge cases.
This commit is contained in:
@@ -5,13 +5,18 @@ import Vapor
|
||||
struct ApiController: RouteCollection {
|
||||
|
||||
func boot(routes: any RoutesBuilder) throws {
|
||||
let users = routes.grouped("api", "users")
|
||||
let api = routes.grouped("api")
|
||||
let users = api.grouped("users")
|
||||
let proCon = api.grouped("procons")
|
||||
let utils = api.grouped("utils")
|
||||
|
||||
users.get(use: usersIndex(req:))
|
||||
users.post(use: createUser(req:))
|
||||
|
||||
let proCon = routes.grouped("api", "procons")
|
||||
proCon.get(use: prosAndConsIndex(req:))
|
||||
proCon.post(use: createProCon(req:))
|
||||
|
||||
utils.post("check-words", use: checkWords(req:))
|
||||
}
|
||||
|
||||
@Sendable
|
||||
@@ -39,6 +44,17 @@ struct ApiController: RouteCollection {
|
||||
return proCon
|
||||
}
|
||||
|
||||
@Sendable
|
||||
func checkWords(req: Request) async throws -> HTTPStatus {
|
||||
let input = try req.content.decode(CheckWords.self)
|
||||
try checkForBadWords(in: input.string)
|
||||
return .ok
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
struct CheckWords: Content {
|
||||
let string: String
|
||||
}
|
||||
|
||||
struct ProConDTO: Content {
|
||||
|
||||
@@ -1,20 +1,20 @@
|
||||
import Foundation
|
||||
import Vapor
|
||||
|
||||
func checkForBadWords(in string: String) throws {
|
||||
if badWords.contains(string) {
|
||||
throw BadWordError()
|
||||
} else if string.contains(" ") {
|
||||
let parts = string.split(separator: " ")
|
||||
for part in parts {
|
||||
if badWords.contains(String(part)) {
|
||||
throw BadWordError()
|
||||
}
|
||||
let split = string.split(separator: "\n")
|
||||
for string in split {
|
||||
for word in badWords where string.contains(word) {
|
||||
throw Abort(.badRequest, reason: "Stop using such naughty language.")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct BadWordError: Error {}
|
||||
|
||||
let badWords: [String] = [
|
||||
"420",
|
||||
"69",
|
||||
"puppy",
|
||||
"kitty",
|
||||
"2g1c",
|
||||
"a-hole",
|
||||
"a-holes",
|
||||
|
||||
@@ -8,64 +8,59 @@ func routes(_ app: Application) throws {
|
||||
return output
|
||||
}
|
||||
|
||||
app.get("loggedIn") { req in
|
||||
guard let userIdString = req.session.data["userId"],
|
||||
let displayName = req.session.data["displayName"],
|
||||
let userId = UUID(uuidString: userIdString)
|
||||
else {
|
||||
return try await req.view.render("/")
|
||||
app.get("loggedIn") { req -> View in
|
||||
guard let user = try await req.currentUser(withProsAndCons: true) else {
|
||||
throw Abort(.badRequest)
|
||||
}
|
||||
guard let user = try await User.query(on: req.db)
|
||||
.filter(\.$id == userId)
|
||||
.with(\.$prosAndCons)
|
||||
.first()
|
||||
else {
|
||||
throw Abort(.unauthorized)
|
||||
}
|
||||
// let prosAndCons = try await user.$prosAndCons.get(on: req.db)
|
||||
return try await req.view.render(
|
||||
"loggedIn",
|
||||
LoggedInContext(name: displayName, prosAndCons: user.prosAndCons)
|
||||
LoggedInContext(name: user.displayName, prosAndCons: user.prosAndCons)
|
||||
)
|
||||
}
|
||||
|
||||
app.get("submitProOrCon") { req in
|
||||
let params = try req.query.decode(SubmitProOrCon.self)
|
||||
guard let userIdString = req.session.data["userId"],
|
||||
let userId = UUID(uuidString: userIdString)
|
||||
else {
|
||||
guard let userId = req.userId else {
|
||||
throw Abort(.unauthorized)
|
||||
}
|
||||
try checkForBadWords(in: params.description)
|
||||
let proOrCon = ProCon(type: params.type, description: params.description, userId: userId)
|
||||
_ = try await req.db.transaction {
|
||||
proOrCon.save(on: $0)
|
||||
}
|
||||
.get()
|
||||
|
||||
try await proOrCon.save(on: req.db)
|
||||
return req.redirect(to: "loggedIn")
|
||||
}
|
||||
|
||||
app.get("login") { req in
|
||||
let params = try req.query.decode(LoginParams.self)
|
||||
req.logger.info("params: \(params)")
|
||||
try checkForBadWords(in: params.displayName)
|
||||
let user = User(displayName: params.displayName)
|
||||
try await user.save(on: req.db)
|
||||
req.session.data["userId"] = user.id?.uuidString
|
||||
return req.redirect(to: "loggedIn")
|
||||
}
|
||||
}
|
||||
|
||||
do {
|
||||
try checkForBadWords(in: params.displayName)
|
||||
} catch {
|
||||
throw Abort(.unauthorized, reason: "Stop using such naughty language.")
|
||||
private extension Request {
|
||||
|
||||
var userId: UUID? {
|
||||
guard let userIdString = session.data["userId"],
|
||||
let userId = UUID(uuidString: userIdString)
|
||||
else {
|
||||
return nil
|
||||
}
|
||||
return userId
|
||||
}
|
||||
|
||||
func currentUser(withProsAndCons: Bool) async throws -> User? {
|
||||
guard let userId = userId else {
|
||||
return nil
|
||||
}
|
||||
|
||||
let user = User(displayName: params.displayName)
|
||||
_ = try await req.db.transaction {
|
||||
user.save(on: $0)
|
||||
}.get()
|
||||
|
||||
let userId = user.id?.uuidString ?? "nil"
|
||||
req.session.data["userId"] = userId
|
||||
req.session.data["displayName"] = user.displayName
|
||||
|
||||
// return try await req.view.render("loggedIn", ["name": user.displayName])
|
||||
return req.redirect(to: "loggedIn")
|
||||
var query = User.query(on: db).filter(\.$id == userId)
|
||||
if withProsAndCons {
|
||||
query = query.with(\.$prosAndCons)
|
||||
}
|
||||
return try await query.first()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user