Skip to content

Commit

Permalink
Add code to guess if the user is restoring and turn off EBT to work a…
Browse files Browse the repository at this point in the history
…round #847
  • Loading branch information
mplorentz committed Nov 23, 2022
1 parent a10f879 commit a23392b
Show file tree
Hide file tree
Showing 52 changed files with 132 additions and 65 deletions.
2 changes: 1 addition & 1 deletion GoSSB/Sources/api-ios.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ func ssbBotInit(config string, notifyBlobReceivedFn uintptr, notifyNewBearerToke
// TODO: make version that prints bytes "unhumanized" so that we can count them
return countconn.WrapConn(level.Debug(log), c), nil
}),
mksbot.DisableEBT(false),
mksbot.DisableEBT(cfg.DisableEBT),
mksbot.WithPublicAuthorizer(newAcceptAllAuthorizer()),
}

Expand Down
16 changes: 11 additions & 5 deletions Source/Bot/Bot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,14 @@ protocol Bot: AnyObject {
/// whose data is contained in `AppConfiguration`.
/// - Parameter queue: The queue that `completion` will be called on.
/// - Parameter config: An object containing high-level parameters like the user's keys and the network key.
/// - Parameter fromOnboarding: A flag that should be true if the user is logging in from the onboarding flow.
/// - Parameter completion: A handler that will be called with the result of the operation.
func login(queue: DispatchQueue, config: AppConfiguration, completion: @escaping ErrorCompletion)
func login(
queue: DispatchQueue,
config: AppConfiguration,
fromOnboarding: Bool,
completion: @escaping ErrorCompletion
)
func logout(completion: @escaping ErrorCompletion)

// MARK: Invites
Expand Down Expand Up @@ -317,13 +323,13 @@ extension Bot {
}
}

func login(config: AppConfiguration, completion: @escaping ErrorCompletion) {
self.login(queue: .main, config: config, completion: completion)
func login(config: AppConfiguration, fromOnboarding: Bool, completion: @escaping ErrorCompletion) {
self.login(queue: .main, config: config, fromOnboarding: fromOnboarding, completion: completion)
}

func login(config: AppConfiguration) async throws {
func login(config: AppConfiguration, fromOnboarding: Bool) async throws {
let error: Error? = await withCheckedContinuation { continuation in
self.login(config: config) { error in
self.login(config: config, fromOnboarding: fromOnboarding) { error in
continuation.resume(with: .success(error))
}
}
Expand Down
2 changes: 1 addition & 1 deletion Source/Bot/Operations/LoginOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class LoginOperation: AsynchronousOperation {
self.success = true
self.finish()
} else {
Bots.current.login(config: configuration) { [weak self] (error) in
Bots.current.login(config: configuration, fromOnboarding: false) { [weak self] (error) in
if let strongSelf = self, !strongSelf.isCancelled {
self?.success = ((error as? BotError) == .alreadyLoggedIn) || error == nil
self?.error = error
Expand Down
5 changes: 2 additions & 3 deletions Source/Controller/Beta1MigrationController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,6 @@ class Beta1MigrationController: ObservableObject, Beta1MigrationViewModel {
hostingController.modalTransitionStyle = .crossDissolve
}
await appController.present(hostingController, animated: true)

bot.isRestoring = true

if !userDefaults.bool(forKey: beta1MigrationStartKey) {
try await bot.dropDatabase(for: appConfiguration)
Expand All @@ -116,7 +114,8 @@ class Beta1MigrationController: ObservableObject, Beta1MigrationViewModel {
controller.progress = userDefaults.float(forKey: beta1MigrationProgress)
}

try await bot.login(config: appConfiguration)
try await bot.login(config: appConfiguration, fromOnboarding: false)
bot.isRestoring = true
await controller.bindProgress(to: bot)

return true
Expand Down
2 changes: 1 addition & 1 deletion Source/Controller/Fix814AccountsHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ enum Fix814AccountsHelper {
newConfiguration.bot = bot
newConfiguration.ssbNetwork = Environment.Networks.mainNet

try await bot.login(config: newConfiguration)
try await bot.login(config: newConfiguration, fromOnboarding: false)

// copy published messages
for message in publishedMessages {
Expand Down
2 changes: 1 addition & 1 deletion Source/Controller/LaunchViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class LaunchViewController: UIViewController {
guard configuration.network != nil else { return }
guard let bot = configuration.bot else { return }

try await bot.login(config: configuration)
try await bot.login(config: configuration, fromOnboarding: false)
} catch {
self.handleLoginFailure(with: error, configuration: configuration)
}
Expand Down
7 changes: 6 additions & 1 deletion Source/FakeBot/FakeBot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,12 @@ class FakeBot: Bot {
completion(nil, FakeBotError.runtimeError("TODO:createSecret"))
}

func login(queue: DispatchQueue, config: AppConfiguration, completion: @escaping ErrorCompletion) {
func login(
queue: DispatchQueue,
config: AppConfiguration,
fromOnboarding: Bool,
completion: @escaping ErrorCompletion
) {
self._network = config.network?.string
self._identity = config.secret.identity
queue.async {
Expand Down
24 changes: 22 additions & 2 deletions Source/GoBot/GoBot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,18 @@ class GoBot: Bot {

Analytics.shared.trackDidDropDatabase()
}

private func guessIfRestoring() throws -> Bool {
guard database.isOpen(),
let config = config else {
throw BotError.notLoggedIn
}

let numberOfPublishedMessagesInViewDB = try database.numberOfMessages(for: config.identity)
let numberOfPublishedMessagesInAppConfig = config.numberOfPublishedMessages
return numberOfPublishedMessagesInViewDB == 0 ||
numberOfPublishedMessagesInViewDB < numberOfPublishedMessagesInAppConfig
}

// MARK: Login/Logout

Expand All @@ -192,7 +204,12 @@ class GoBot: Bot {
completion(secret, nil)
}

func login(queue: DispatchQueue, config: AppConfiguration, completion: @escaping ErrorCompletion) {
func login(
queue: DispatchQueue,
config: AppConfiguration,
fromOnboarding isLoggingInFromOnboarding: Bool = false,
completion: @escaping ErrorCompletion
) {
guard let network = config.network else {
queue.async { completion(BotError.invalidAppConfiguration) }
return
Expand Down Expand Up @@ -224,6 +241,8 @@ class GoBot: Bot {
path: repoPrefix,
user: secret.identity
)

isRestoring = isLoggingInFromOnboarding ? false : try guessIfRestoring()
} catch {
queue.async { completion(error) }
return
Expand All @@ -237,7 +256,8 @@ class GoBot: Bot {
network: network,
hmacKey: hmacKey,
secret: secret,
pathPrefix: repoPrefix
pathPrefix: repoPrefix,
disableEBT: self.isRestoring // This is a terrible hack to work around #847
)

defer {
Expand Down
15 changes: 13 additions & 2 deletions Source/GoBot/GoBotInternal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ private struct GoBotConfig: Encodable {
let SchemaVersion: UInt

let ServicePubs: [Identity]? // identities of services which supply planetary specific services

/// Disables EBT replication if true. Part of a workaround for #847.
let DisableEBT: Bool

#if DEBUG
let Testing = true
Expand Down Expand Up @@ -132,7 +135,13 @@ class GoBotInternal {

// MARK: login / logout

func login(network: NetworkKey, hmacKey: HMACKey?, secret: Secret, pathPrefix: String) -> Error? {
func login(
network: NetworkKey,
hmacKey: HMACKey?,
secret: Secret,
pathPrefix: String,
disableEBT: Bool
) -> Error? {
if self.isRunning {
guard self.logout() == true else {
return GoBotError.duringProcessing("failure during logging out previous session", GoBotError.alreadyStarted)
Expand All @@ -155,7 +164,9 @@ class GoBotInternal {
ListenAddr: listenAddr,
Hops: 1,
SchemaVersion: ViewDatabase.schemaVersion,
ServicePubs: servicePubs)
ServicePubs: servicePubs,
DisableEBT: disableEBT
)

let enc = JSONEncoder()
var cfgStr: String
Expand Down
2 changes: 1 addition & 1 deletion Source/Localization/af-ZA.lproj/Generated.strings
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@
"Error.invalidRoomURL" = "Could not parse invitation.";
"Error.invalidRoomInvitationOrAddress" = "Planetary does not recognize this as a valid room invitation or address.";
"Error.notLoggedIn" = "The operation could not be completed because no user is logged in.";
"Error.alreadyJoinedRoom" = "You are already a member of this room.";
"Error.alreadyJoinedRoom" = "You are already a member of this room";

"Channel.one" = "channel";
"Channel.many" = "channels";
Expand Down
2 changes: 1 addition & 1 deletion Source/Localization/ar-SA.lproj/Generated.strings
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@
"Error.invalidRoomURL" = "Could not parse invitation.";
"Error.invalidRoomInvitationOrAddress" = "Planetary does not recognize this as a valid room invitation or address.";
"Error.notLoggedIn" = "The operation could not be completed because no user is logged in.";
"Error.alreadyJoinedRoom" = "You are already a member of this room.";
"Error.alreadyJoinedRoom" = "You are already a member of this room";

"Channel.one" = "channel";
"Channel.many" = "channels";
Expand Down
2 changes: 1 addition & 1 deletion Source/Localization/ca-ES.lproj/Generated.strings
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@
"Error.invalidRoomURL" = "Could not parse invitation.";
"Error.invalidRoomInvitationOrAddress" = "Planetary does not recognize this as a valid room invitation or address.";
"Error.notLoggedIn" = "The operation could not be completed because no user is logged in.";
"Error.alreadyJoinedRoom" = "You are already a member of this room.";
"Error.alreadyJoinedRoom" = "You are already a member of this room";

"Channel.one" = "channel";
"Channel.many" = "channels";
Expand Down
2 changes: 1 addition & 1 deletion Source/Localization/cs-CZ.lproj/Generated.strings
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@
"Error.invalidRoomURL" = "Could not parse invitation.";
"Error.invalidRoomInvitationOrAddress" = "Planetary does not recognize this as a valid room invitation or address.";
"Error.notLoggedIn" = "The operation could not be completed because no user is logged in.";
"Error.alreadyJoinedRoom" = "You are already a member of this room.";
"Error.alreadyJoinedRoom" = "You are already a member of this room";

"Channel.one" = "channel";
"Channel.many" = "channels";
Expand Down
2 changes: 1 addition & 1 deletion Source/Localization/da-DK.lproj/Generated.strings
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@
"Error.invalidRoomURL" = "Could not parse invitation.";
"Error.invalidRoomInvitationOrAddress" = "Planetary does not recognize this as a valid room invitation or address.";
"Error.notLoggedIn" = "The operation could not be completed because no user is logged in.";
"Error.alreadyJoinedRoom" = "You are already a member of this room.";
"Error.alreadyJoinedRoom" = "You are already a member of this room";

"Channel.one" = "channel";
"Channel.many" = "channels";
Expand Down
2 changes: 1 addition & 1 deletion Source/Localization/de-DE.lproj/Generated.strings
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@
"Error.invalidRoomURL" = "Could not parse invitation.";
"Error.invalidRoomInvitationOrAddress" = "Planetary does not recognize this as a valid room invitation or address.";
"Error.notLoggedIn" = "The operation could not be completed because no user is logged in.";
"Error.alreadyJoinedRoom" = "You are already a member of this room.";
"Error.alreadyJoinedRoom" = "You are already a member of this room";

"Channel.one" = "Kanal";
"Channel.many" = "Kanäle";
Expand Down
2 changes: 1 addition & 1 deletion Source/Localization/el-GR.lproj/Generated.strings
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@
"Error.invalidRoomURL" = "Could not parse invitation.";
"Error.invalidRoomInvitationOrAddress" = "Planetary does not recognize this as a valid room invitation or address.";
"Error.notLoggedIn" = "The operation could not be completed because no user is logged in.";
"Error.alreadyJoinedRoom" = "You are already a member of this room.";
"Error.alreadyJoinedRoom" = "You are already a member of this room";

"Channel.one" = "channel";
"Channel.many" = "channels";
Expand Down
2 changes: 1 addition & 1 deletion Source/Localization/en-US.lproj/Generated.strings
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@
"Error.invalidRoomURL" = "Could not parse invitation.";
"Error.invalidRoomInvitationOrAddress" = "Planetary does not recognize this as a valid room invitation or address.";
"Error.notLoggedIn" = "The operation could not be completed because no user is logged in.";
"Error.alreadyJoinedRoom" = "You are already a member of this room.";
"Error.alreadyJoinedRoom" = "You are already a member of this room";

"Channel.one" = "channel";
"Channel.many" = "channels";
Expand Down
2 changes: 1 addition & 1 deletion Source/Localization/en.lproj/Generated.strings
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@
"Error.invalidRoomURL" = "Could not parse invitation.";
"Error.invalidRoomInvitationOrAddress" = "Planetary does not recognize this as a valid room invitation or address.";
"Error.notLoggedIn" = "The operation could not be completed because no user is logged in.";
"Error.alreadyJoinedRoom" = "You are already a member of this room.";
"Error.alreadyJoinedRoom" = "You are already a member of this room";

"Channel.one" = "channel";
"Channel.many" = "channels";
Expand Down
2 changes: 1 addition & 1 deletion Source/Localization/es-AR.lproj/Generated.strings
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@
"Error.invalidRoomURL" = "Could not parse invitation.";
"Error.invalidRoomInvitationOrAddress" = "Planetary does not recognize this as a valid room invitation or address.";
"Error.notLoggedIn" = "The operation could not be completed because no user is logged in.";
"Error.alreadyJoinedRoom" = "You are already a member of this room.";
"Error.alreadyJoinedRoom" = "You are already a member of this room";

"Channel.one" = "hashtag";
"Channel.many" = "hashtags";
Expand Down
2 changes: 1 addition & 1 deletion Source/Localization/es-ES.lproj/Generated.strings
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@
"Error.invalidRoomURL" = "No se pudo analizar tu invitación.";
"Error.invalidRoomInvitationOrAddress" = "Planetary does not recognize this as a valid room invitation or address.";
"Error.notLoggedIn" = "La operación no pudo ser completada porque no hay ningún usuario conectado.";
"Error.alreadyJoinedRoom" = "You are already a member of this room.";
"Error.alreadyJoinedRoom" = "You are already a member of this room";

"Channel.one" = "etiqueta";
"Channel.many" = "etiquetas";
Expand Down
2 changes: 1 addition & 1 deletion Source/Localization/es-UY.lproj/Generated.strings
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@
"Error.invalidRoomURL" = "No se pudo interpretar la invitación.";
"Error.invalidRoomInvitationOrAddress" = "Planetary no reconoce esto como una invitación o dirección válida de la sala.";
"Error.notLoggedIn" = "La operación no pudo ser completada porque no hay ningún usuario conectado.";
"Error.alreadyJoinedRoom" = "You are already a member of this room.";
"Error.alreadyJoinedRoom" = "You are already a member of this room";

"Channel.one" = "hashtag";
"Channel.many" = "hashtags";
Expand Down
2 changes: 1 addition & 1 deletion Source/Localization/es.lproj/Generated.strings
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@
"Error.invalidRoomURL" = "Could not parse invitation.";
"Error.invalidRoomInvitationOrAddress" = "Planetary does not recognize this as a valid room invitation or address.";
"Error.notLoggedIn" = "The operation could not be completed because no user is logged in.";
"Error.alreadyJoinedRoom" = "You are already a member of this room.";
"Error.alreadyJoinedRoom" = "You are already a member of this room";

"Channel.one" = "etiqueta";
"Channel.many" = "etiquetas";
Expand Down
2 changes: 1 addition & 1 deletion Source/Localization/fi-FI.lproj/Generated.strings
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@
"Error.invalidRoomURL" = "Could not parse invitation.";
"Error.invalidRoomInvitationOrAddress" = "Planetary does not recognize this as a valid room invitation or address.";
"Error.notLoggedIn" = "The operation could not be completed because no user is logged in.";
"Error.alreadyJoinedRoom" = "You are already a member of this room.";
"Error.alreadyJoinedRoom" = "You are already a member of this room";

"Channel.one" = "channel";
"Channel.many" = "channels";
Expand Down
2 changes: 1 addition & 1 deletion Source/Localization/fr-FR.lproj/Generated.strings
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@
"Error.invalidRoomURL" = "Could not parse invitation.";
"Error.invalidRoomInvitationOrAddress" = "Planetary does not recognize this as a valid room invitation or address.";
"Error.notLoggedIn" = "The operation could not be completed because no user is logged in.";
"Error.alreadyJoinedRoom" = "You are already a member of this room.";
"Error.alreadyJoinedRoom" = "You are already a member of this room";

"Channel.one" = "channel";
"Channel.many" = "channels";
Expand Down
2 changes: 1 addition & 1 deletion Source/Localization/he-IL.lproj/Generated.strings
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@
"Error.invalidRoomURL" = "Could not parse invitation.";
"Error.invalidRoomInvitationOrAddress" = "Planetary does not recognize this as a valid room invitation or address.";
"Error.notLoggedIn" = "The operation could not be completed because no user is logged in.";
"Error.alreadyJoinedRoom" = "You are already a member of this room.";
"Error.alreadyJoinedRoom" = "You are already a member of this room";

"Channel.one" = "channel";
"Channel.many" = "channels";
Expand Down
2 changes: 1 addition & 1 deletion Source/Localization/hu-HU.lproj/Generated.strings
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@
"Error.invalidRoomURL" = "Could not parse invitation.";
"Error.invalidRoomInvitationOrAddress" = "Planetary does not recognize this as a valid room invitation or address.";
"Error.notLoggedIn" = "The operation could not be completed because no user is logged in.";
"Error.alreadyJoinedRoom" = "You are already a member of this room.";
"Error.alreadyJoinedRoom" = "You are already a member of this room";

"Channel.one" = "channel";
"Channel.many" = "channels";
Expand Down
2 changes: 1 addition & 1 deletion Source/Localization/it-IT.lproj/Generated.strings
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@
"Error.invalidRoomURL" = "Could not parse invitation.";
"Error.invalidRoomInvitationOrAddress" = "Planetary does not recognize this as a valid room invitation or address.";
"Error.notLoggedIn" = "The operation could not be completed because no user is logged in.";
"Error.alreadyJoinedRoom" = "You are already a member of this room.";
"Error.alreadyJoinedRoom" = "You are already a member of this room";

"Channel.one" = "channel";
"Channel.many" = "channels";
Expand Down
2 changes: 1 addition & 1 deletion Source/Localization/ja-JP.lproj/Generated.strings
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@
"Error.invalidRoomURL" = "Could not parse invitation.";
"Error.invalidRoomInvitationOrAddress" = "Planetary does not recognize this as a valid room invitation or address.";
"Error.notLoggedIn" = "The operation could not be completed because no user is logged in.";
"Error.alreadyJoinedRoom" = "You are already a member of this room.";
"Error.alreadyJoinedRoom" = "You are already a member of this room";

"Channel.one" = "channel";
"Channel.many" = "channels";
Expand Down
2 changes: 1 addition & 1 deletion Source/Localization/ko-KR.lproj/Generated.strings
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@
"Error.invalidRoomURL" = "Could not parse invitation.";
"Error.invalidRoomInvitationOrAddress" = "Planetary does not recognize this as a valid room invitation or address.";
"Error.notLoggedIn" = "The operation could not be completed because no user is logged in.";
"Error.alreadyJoinedRoom" = "You are already a member of this room.";
"Error.alreadyJoinedRoom" = "You are already a member of this room";

"Channel.one" = "channel";
"Channel.many" = "channels";
Expand Down
2 changes: 1 addition & 1 deletion Source/Localization/mi-NZ.lproj/Generated.strings
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@
"Error.invalidRoomURL" = "Could not parse invitation.";
"Error.invalidRoomInvitationOrAddress" = "Planetary does not recognize this as a valid room invitation or address.";
"Error.notLoggedIn" = "The operation could not be completed because no user is logged in.";
"Error.alreadyJoinedRoom" = "You are already a member of this room.";
"Error.alreadyJoinedRoom" = "You are already a member of this room";

"Channel.one" = "channel";
"Channel.many" = "channels";
Expand Down
2 changes: 1 addition & 1 deletion Source/Localization/nl-NL.lproj/Generated.strings
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@
"Error.invalidRoomURL" = "Could not parse invitation.";
"Error.invalidRoomInvitationOrAddress" = "Planetary does not recognize this as a valid room invitation or address.";
"Error.notLoggedIn" = "The operation could not be completed because no user is logged in.";
"Error.alreadyJoinedRoom" = "You are already a member of this room.";
"Error.alreadyJoinedRoom" = "You are already a member of this room";

"Channel.one" = "channel";
"Channel.many" = "channels";
Expand Down
2 changes: 1 addition & 1 deletion Source/Localization/no-NO.lproj/Generated.strings
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@
"Error.invalidRoomURL" = "Could not parse invitation.";
"Error.invalidRoomInvitationOrAddress" = "Planetary does not recognize this as a valid room invitation or address.";
"Error.notLoggedIn" = "The operation could not be completed because no user is logged in.";
"Error.alreadyJoinedRoom" = "You are already a member of this room.";
"Error.alreadyJoinedRoom" = "You are already a member of this room";

"Channel.one" = "channel";
"Channel.many" = "channels";
Expand Down
Loading

0 comments on commit a23392b

Please sign in to comment.