From e455868409784c631c75146f35ab3bdf33d7bfc6 Mon Sep 17 00:00:00 2001 From: kPherox Date: Thu, 30 May 2024 17:45:47 +0000 Subject: [PATCH 1/3] refactor: fix warning about `Sendable` For `Model`: refs vapor/vapor#3180 --- Sources/Models/Did.swift | 2 +- Sources/Models/Handle.swift | 2 +- Sources/Models/Middleware/DidMiddleware.swift | 12 ++++++++++-- Sources/Models/Middleware/HandleMiddleware.swift | 12 ++++++++++-- Sources/Models/Operation.swift | 4 ++-- Sources/Models/PersonalDataServer.swift | 2 +- Sources/Models/PollingHistory.swift | 2 +- Sources/Models/PollingJobStatus.swift | 2 +- Sources/registerMiddleware.swift | 4 ++-- 9 files changed, 29 insertions(+), 13 deletions(-) diff --git a/Sources/Models/Did.swift b/Sources/Models/Did.swift index 7e0473b..ae4b60a 100644 --- a/Sources/Models/Did.swift +++ b/Sources/Models/Did.swift @@ -7,7 +7,7 @@ enum BanReason: String, Codable { case missingHistory = "missing_history" } -final class Did: Model, Content { +final class Did: Model, Content, @unchecked Sendable { static let schema = "dids" static func findWithOperations(_ did: Did.IDValue?, on db: Database) async throws -> Did? { diff --git a/Sources/Models/Handle.swift b/Sources/Models/Handle.swift index 513395e..89455c1 100644 --- a/Sources/Models/Handle.swift +++ b/Sources/Models/Handle.swift @@ -5,7 +5,7 @@ enum HandleNameError: Error { case invalidCharacter } -final class Handle: Model, Content { +final class Handle: Model, Content, @unchecked Sendable { static let schema = "handles" static func findBy(handleName: String, withOp: Bool = false, on db: Database) async throws diff --git a/Sources/Models/Middleware/DidMiddleware.swift b/Sources/Models/Middleware/DidMiddleware.swift index 0d95819..816447c 100644 --- a/Sources/Models/Middleware/DidMiddleware.swift +++ b/Sources/Models/Middleware/DidMiddleware.swift @@ -1,11 +1,19 @@ import Fluent import Redis +import Vapor struct DidMiddleware: AsyncModelMiddleware { typealias Model = Did - let redis: RedisClient - let logger: Logger + let app: Application + + var redis: RedisClient { + self.app.redis + } + + var logger: Logger { + self.app.logger + } func create(model: Model, on db: Database, next: AnyAsyncModelResponder) async throws { try await next.create(model, on: db) diff --git a/Sources/Models/Middleware/HandleMiddleware.swift b/Sources/Models/Middleware/HandleMiddleware.swift index d24c78a..6180c18 100644 --- a/Sources/Models/Middleware/HandleMiddleware.swift +++ b/Sources/Models/Middleware/HandleMiddleware.swift @@ -1,11 +1,19 @@ import Fluent import Redis +import Vapor struct HandleMiddleware: AsyncModelMiddleware { typealias Model = Handle - let redis: RedisClient - let logger: Logger + let app: Application + + var redis: RedisClient { + self.app.redis + } + + var logger: Logger { + self.app.logger + } func create(model: Model, on db: Database, next: AnyAsyncModelResponder) async throws { try await next.create(model, on: db) diff --git a/Sources/Models/Operation.swift b/Sources/Models/Operation.swift index 9f8db7c..54b4aca 100644 --- a/Sources/Models/Operation.swift +++ b/Sources/Models/Operation.swift @@ -2,10 +2,10 @@ import Fluent import FluentPostgresDriver import Vapor -final class Operation: Model, Content { +final class Operation: Model, Content, @unchecked Sendable { static let schema = "operations" - final class IDValue: Fields, Hashable { + final class IDValue: Fields, Hashable, @unchecked Sendable { @Field(key: "cid") var cid: String diff --git a/Sources/Models/PersonalDataServer.swift b/Sources/Models/PersonalDataServer.swift index c111547..fad4fa0 100644 --- a/Sources/Models/PersonalDataServer.swift +++ b/Sources/Models/PersonalDataServer.swift @@ -1,7 +1,7 @@ import Fluent import Vapor -final class PersonalDataServer: Model, Content { +final class PersonalDataServer: Model, Content, @unchecked Sendable { static let schema = "personal_data_servers" @ID(key: .id) diff --git a/Sources/Models/PollingHistory.swift b/Sources/Models/PollingHistory.swift index 21a532e..64056aa 100644 --- a/Sources/Models/PollingHistory.swift +++ b/Sources/Models/PollingHistory.swift @@ -1,7 +1,7 @@ import Fluent import Vapor -final class PollingHistory: Model, Content { +final class PollingHistory: Model, Content, @unchecked Sendable { static func getLatestWithoutErrors(on database: Database) async throws -> PollingHistory? { let errors = try await PollingJobStatus.query(on: database).filter(\.$status == .error) .all(\.$history.$id) diff --git a/Sources/Models/PollingJobStatus.swift b/Sources/Models/PollingJobStatus.swift index 399e2e3..864d3d1 100644 --- a/Sources/Models/PollingJobStatus.swift +++ b/Sources/Models/PollingJobStatus.swift @@ -1,7 +1,7 @@ import Fluent import Vapor -final class PollingJobStatus: Model, Content { +final class PollingJobStatus: Model, Content, @unchecked Sendable { static let schema = "polling_job_statuses" @ID(custom: .id, generatedBy: .user) diff --git a/Sources/registerMiddleware.swift b/Sources/registerMiddleware.swift index acdc712..0586f1c 100644 --- a/Sources/registerMiddleware.swift +++ b/Sources/registerMiddleware.swift @@ -9,6 +9,6 @@ func registerMiddleware(_ app: Application) { FileMiddleware(publicDirectory: app.directory.publicDirectory, directoryAction: .redirect)) // database middleware - app.databases.middleware.use(DidMiddleware(redis: app.redis, logger: app.logger), on: .psql) - app.databases.middleware.use(HandleMiddleware(redis: app.redis, logger: app.logger), on: .psql) + app.databases.middleware.use(DidMiddleware(app: app), on: .psql) + app.databases.middleware.use(HandleMiddleware(app: app), on: .psql) } From 80dc809682992882a0086bdbe0b876b309ba8233 Mon Sep 17 00:00:00 2001 From: kPherox Date: Thu, 30 May 2024 17:50:49 +0000 Subject: [PATCH 2/3] refactor: use `try await` in `if` expression block instead for Swift 6 --- Sources/Models/Handle.swift | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Sources/Models/Handle.swift b/Sources/Models/Handle.swift index 89455c1..511fe1e 100644 --- a/Sources/Models/Handle.swift +++ b/Sources/Models/Handle.swift @@ -12,10 +12,10 @@ final class Handle: Model, Content, @unchecked Sendable { -> Handle? { let query = Handle.query(on: db).filter(\.$handle == handleName) - return try await if withOp { - query.with(\.$operations).first() + return if withOp { + try await query.with(\.$operations).first() } else { - query.first() + try await query.first() } } From a19a1461537914cfe671d290385f19b770699a61 Mon Sep 17 00:00:00 2001 From: kPherox Date: Thu, 30 May 2024 17:57:32 +0000 Subject: [PATCH 3/3] fix: use `Application.make` instead of `Application.init` fix warning about "initializer 'init' is unavailable from asynchronous contexts" --- Sources/entrypoint.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/entrypoint.swift b/Sources/entrypoint.swift index ac80c8e..4b469f5 100644 --- a/Sources/entrypoint.swift +++ b/Sources/entrypoint.swift @@ -26,7 +26,7 @@ enum Entrypoint { var env = try Environment.detect() try LoggingSystem.bootstrap(from: &env) - let app = Application(env) + let app = try await Application.make(env) defer { app.shutdown() } try await configure(app)