Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Commit

Permalink
Fix #2565: Retry ref code lookup on subsequent launches. (#2574)
Browse files Browse the repository at this point in the history
  • Loading branch information
jhreis authored May 27, 2020
1 parent ac25e50 commit 0c04ba1
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
40 changes: 39 additions & 1 deletion BraveShared/Analytics/UserReferralProgram.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ public class UserReferralProgram {
static let prod = "https://laptop-updates.brave.com"
}

// In case of network problems when looking for referrral code
// we retry the call few times while the app is still alive.
private struct ReferralLookupRetry {
var timer: Timer?
var currentCount = 0
let retryLimit = 10
let retryTimeInterval = AppConstants.buildChannel.isPublic ? 3.minutes : 1.minutes
}

private var referralLookupRetry = ReferralLookupRetry()

let service: UrpService

public init?() {
Expand All @@ -49,7 +60,31 @@ public class UserReferralProgram {
public func referralLookup(refCode: String?, completion: @escaping (_ refCode: String?, _ offerUrl: String?) -> Void) {
UrpLog.log("first run referral lookup")

let referralBlock: (ReferralData?, UrpError?) -> Void = { referral, _ in
let referralBlock: (ReferralData?, UrpError?) -> Void = { [weak self] referral, error in
guard let self = self else { return }

if error == BraveShared.UrpError.endpointError {
UrpLog.log("URP look up had endpoint error, will retry on next launch.")
self.referralLookupRetry.timer?.invalidate()
self.referralLookupRetry.timer = nil

// Hit max retry attempts.
if self.referralLookupRetry.currentCount > self.referralLookupRetry.retryLimit { return }

self.referralLookupRetry.currentCount += 1
self.referralLookupRetry.timer =
Timer.scheduledTimer(withTimeInterval: self.referralLookupRetry.retryTimeInterval,
repeats: true) { [weak self] _ in
self?.referralLookup(refCode: refCode) { refCode, offerUrl in
completion(refCode, offerUrl)
}
}
return
}

// Connection "succeeded"

Preferences.URP.referralLookupOutstanding.value = false
guard let ref = referral else {
log.info("No referral code found")
UrpLog.log("No referral code found")
Expand All @@ -75,6 +110,9 @@ public class UserReferralProgram {
Preferences.URP.downloadId.value = ref.downloadId
Preferences.URP.referralCode.value = ref.referralCode

self.referralLookupRetry.timer?.invalidate()
self.referralLookupRetry.timer = nil

UrpLog.log("Found referral: downloadId: \(ref.downloadId), code: \(ref.referralCode)")
// In case of network errors or getting `isFinalized = false`, we retry the api call.
self.initRetryPingConnection(numberOfTimes: 30)
Expand Down
2 changes: 2 additions & 0 deletions BraveShared/Preferences.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ extension Preferences {
static let downloadId = Option<String?>(key: "urp.referral.download-id", default: nil)
public static let referralCode = Option<String?>(key: "urp.referral.code", default: nil)
static let referralCodeDeleteDate = Option<TimeInterval?>(key: "urp.referral.delete-date", default: nil)
/// Whether the ref code lookup has still yet to occur
public static let referralLookupOutstanding = Option<Bool?>(key: "urp.referral.lookkup-completed", default: nil)
}

public final class NTP {
Expand Down
17 changes: 15 additions & 2 deletions Client/Application/Delegates/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,22 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UIViewControllerRestorati
}

if let urp = UserReferralProgram.shared {
if isFirstLaunch {
if Preferences.URP.referralLookupOutstanding.value == nil {
// This preference has never been set, and this means it is a new or upgraded user.
// That distinction must be made to know if a network request for ref-code look up should be made.

// Setting this to an explicit value so it will never get overwritten on subsequent launches.
// Upgrade users should not have ref code ping happening.
Preferences.URP.referralLookupOutstanding.value = isFirstLaunch
}

if Preferences.URP.referralLookupOutstanding.value == true {
let refCode = UserReferralProgram.sanitize(input: UIPasteboard.general.string)
if refCode != nil { UIPasteboard.general.clearPasteboard() }
if refCode != nil {
UrpLog.log("Clipboard ref code found: " + (UIPasteboard.general.string ?? "!Clipboard Empty!"))
UrpLog.log("Clearing clipboard.")
UIPasteboard.general.clearPasteboard()
}

urp.referralLookup(refCode: refCode) { referralCode, offerUrl in
if let code = referralCode {
Expand Down

0 comments on commit 0c04ba1

Please sign in to comment.