Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sync conect #281

Merged
merged 17 commits into from
Mar 31, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Sources/DDGSync/DDGSync.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ public class DDGSync: DDGSyncing {
updateIsAuthenticated()
}

public func remoteConnect() throws -> RemoteConnecting {
guard try dependencies.secureStore.account() == nil else {
ayoy marked this conversation as resolved.
Show resolved Hide resolved
throw SyncError.accountAlreadyExists
}

return dependencies.createRemoteConnector()
}

public func sender() throws -> UpdatesSending {
return try dependencies.createUpdatesSender(persistence)
}
Expand Down
11 changes: 11 additions & 0 deletions Sources/DDGSync/DDGSyncing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ public protocol DDGSyncing {
*/
func login(_ recoveryKey: SyncCode.RecoveryKey, deviceName: String, deviceType: String) async throws

/// Returns a device id and temporary secret key ready for display then polls the end point for a recovery key.
func remoteConnect() throws -> RemoteConnecting

/**
Creates an atomic sender. Add items to the sender and then call send to send them all in a single PATCH. Will automatically re-try if there is a network failure.
*/
Expand Down Expand Up @@ -155,3 +158,11 @@ public struct SavedSiteFolder: Codable {
}

}

public protocol RemoteConnecting {

var code: String { get }

func connect() async throws

}
33 changes: 33 additions & 0 deletions Sources/DDGSync/internal/ProductionDependencies.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,39 @@ struct ProductionDependencies: SyncDependencies {
responseHandler = ResponseHandler(persistence: persistence, crypter: crypter)
}

func createRemoteConnector() -> RemoteConnecting {

// TODO move this somewhere else
struct RemoteConnector: RemoteConnecting {

let code: String = "device id + temp secret key as base64 encoded json"

func connect() async throws {
// TODO create device id

// TODO create temporary secret key

// TODO call end the point

while true {
// If the UI closes it should cancel the task
try Task.checkCancellation()

// TODO poll the endpoint

// TODO parse the recovery key if available

// TODO login using the recovery key

// Wait for 5 seconds before polling again
try await Task.sleep(nanoseconds: 5 * 1_000_000_000)
}
}
}

return RemoteConnector()
}

func createUpdatesSender(_ persistence: LocalDataPersisting) throws -> UpdatesSending {
return UpdatesSender(fileStorageUrl: fileStorageUrl, persistence: persistence, dependencies: self)
}
Expand Down
2 changes: 2 additions & 0 deletions Sources/DDGSync/internal/SyncDependencies.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ protocol SyncDependencies {
var responseHandler: ResponseHandling { get }
var crypter: Crypting { get }

func createRemoteConnector() -> RemoteConnecting
func createUpdatesSender(_ persistence: LocalDataPersisting) throws -> UpdatesSending
func createUpdatesFetcher(_ persistence: LocalDataPersisting) throws -> UpdatesFetching

Expand All @@ -37,6 +38,7 @@ protocol AccountManaging {
func createAccount(deviceName: String, deviceType: String) async throws -> SyncAccount

func login(_ recoveryKey: SyncCode.RecoveryKey, deviceName: String, deviceType: String) async throws -> (account: SyncAccount, devices: [RegisteredDevice])

func logout(deviceId: String, token: String) async throws

}
Expand Down