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

Update retry handler used by Pro Client #1238

Merged
merged 29 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
c25fff5
move code to create user with backoff to pro client
atavism Nov 22, 2024
e11cc27
move code to create user with backoff to pro client
atavism Nov 22, 2024
aa953c9
move create user code to pro client
atavism Nov 22, 2024
c28bde9
clean-ups
atavism Nov 22, 2024
1b45fb0
Update desktop to use same code to retry creating user
atavism Nov 22, 2024
051dbc4
Update desktop to use same code to retry creating user
atavism Nov 22, 2024
dc5bed5
revert changes
atavism Nov 22, 2024
18b9fcd
Add client session mock
atavism Nov 22, 2024
2f83a2f
clean-ups
atavism Nov 22, 2024
aacff92
clean-ups
atavism Nov 22, 2024
36b5fc4
clean-ups
atavism Nov 22, 2024
77d916e
clean-ups
atavism Nov 22, 2024
a60dcf7
merge latest
atavism Dec 1, 2024
5f07f09
update flashlight and run go mod tidy
atavism Dec 1, 2024
ad01305
update flashlight and run go mod tidy
atavism Dec 1, 2024
b742df5
Remove old tests
atavism Dec 1, 2024
b9dc679
Stop polling if user becomes pro and some other clean-ups
atavism Dec 2, 2024
16f1ad7
Fix user create api on android. (#1246)
jigar-f Dec 3, 2024
624b1db
merge latest
atavism Dec 3, 2024
cca94f6
Merge remote-tracking branch 'origin/main' into atavism/updates-pro-c…
atavism Dec 3, 2024
5e8cb2e
Merge remote-tracking branch 'origin/main' into atavism/updates-pro-c…
atavism Dec 4, 2024
a5e31d0
Add back auth methods for now
atavism Dec 4, 2024
1a97e30
Merge remote-tracking branch 'origin/main' into atavism/updates-pro-c…
atavism Dec 4, 2024
4252f78
move plans and payment methods calls to pro client
atavism Dec 4, 2024
779d9cc
move plans and payment methods calls to pro client
atavism Dec 4, 2024
f6e67b7
Fixed issue on desktop while creating user and other small fixes.
jigar-f Dec 5, 2024
68d579d
remove commented code.
jigar-f Dec 5, 2024
fb0e326
clean-ups
atavism Dec 5, 2024
9ef2d82
Merge remote-tracking branch 'origin/main' into atavism/updates-pro-c…
atavism Dec 5, 2024
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
4 changes: 2 additions & 2 deletions android/.project
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
</natures>
<filteredResources>
<filter>
<id>1620924294143</id>
<id>1732230225439</id>
<name></name>
<type>30</type>
<matcher>
<id>org.eclipse.core.resources.regexFilterMatcher</id>
<arguments>node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
<arguments>node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
</matcher>
</filter>
</filteredResources>
Expand Down
89 changes: 8 additions & 81 deletions desktop/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -562,17 +562,18 @@ func (app *App) IsPro() bool {
return isPro
}

func (app *App) fetchOrCreateUser(ctx context.Context) (*protos.User, error) {
settings := app.Settings()
lang := settings.GetLanguage()
func (app *App) fetchOrCreateUser(ctx context.Context) {
ss := app.Settings()
lang := ss.GetLanguage()
if lang == "" {
// set default language
settings.SetLanguage("en_us")
ss.SetLanguage("en_us")
}
if userID := settings.GetUserID(); userID == 0 {
return app.CreateUser(ctx)
if userID := ss.GetUserID(); userID == 0 {
ss.SetUserFirstVisit(true)
app.proClient.RetryCreateUser(ctx, ss, 5*time.Minute)
} else {
return app.UserData(ctx)
app.proClient.UpdateUserData(ctx, ss)
}
}

Expand All @@ -592,27 +593,6 @@ func (app *App) fetchDeviceLinkingCode(ctx context.Context) (string, error) {
return resp.Code, nil
}

// CreateUser is used when Lantern is run for the first time and creates a new user with the pro server
func (app *App) CreateUser(ctx context.Context) (*protos.User, error) {
log.Debug("New user, calling user create")
settings := app.Settings()
settings.SetUserFirstVisit(true)
resp, err := app.proClient.UserCreate(ctx)
if err != nil {
return nil, errors.New("Could not create new Pro user: %v", err)
}
user := resp.User
log.Debugf("DEBUG: User created: %v", user)
if resp.BaseResponse != nil && resp.BaseResponse.Error != "" {
return nil, errors.New("Could not create new Pro user: %v", err)
}
app.SetUserData(ctx, user.UserId, user)
settings.SetReferralCode(user.Referral)
settings.SetUserIDAndToken(user.UserId, user.Token)
go app.UserData(ctx)
return resp.User, nil
}

// Plans returns the plans available to a user
func (app *App) Plans(ctx context.Context) ([]protos.Plan, error) {
if v, ok := app.plansCache.Load("plans"); ok {
Expand Down Expand Up @@ -677,59 +657,6 @@ func (app *App) FetchPaymentMethods(ctx context.Context) (*proclient.PaymentMeth
return resp, nil
}

// UserData looks up user data that is associated with the given UserConfig
func (app *App) UserData(ctx context.Context) (*protos.User, error) {
log.Debug("Refreshing user data")
resp, err := app.proClient.UserData(context.Background())
if err != nil {
return nil, errors.New("error fetching user data: %v", err)
} else if resp.User == nil {
return nil, errors.New("error fetching user data")
}
userDetail := resp.User
settings := app.Settings()

setProUser := func(isPro bool) {
app.Settings().SetProUser(isPro)
}

currentDevice := app.Settings().GetDeviceID()

// Check if device id is connect to same device if not create new user
// this is for the case when user removed device from other device
deviceFound := false
if userDetail.Devices != nil {
for _, device := range userDetail.Devices {
if device.Id == currentDevice {
deviceFound = true
break
}
}
}

/// Check if user has installed app first time
firstTime := settings.GetUserFirstVisit()
log.Debugf("First time visit %v", firstTime)
if userDetail.UserLevel == "pro" && firstTime {
log.Debugf("User is pro and first time")
setProUser(true)
} else if userDetail.UserLevel == "pro" && !firstTime && deviceFound {
log.Debugf("User is pro and not first time")
setProUser(true)
} else {
log.Debugf("User is not pro")
setProUser(false)
}
settings.SetUserIDAndToken(userDetail.UserId, userDetail.Token)
settings.SetExpiration(userDetail.Expiration)
settings.SetReferralCode(resp.User.Referral)
log.Debugf("User caching successful: %+v", userDetail)
// Save data in userData cache
app.SetUserData(ctx, userDetail.UserId, userDetail)
app.SendUpdateUserDataToUI()
return resp.User, nil
}

func (app *App) devices() protos.Devices {
user, found := app.GetUserData(app.Settings().GetUserID())

Expand Down
11 changes: 1 addition & 10 deletions desktop/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,6 @@ func logout() *C.char {
}

clearLocalUserData()
// Create new user
atavism marked this conversation as resolved.
Show resolved Hide resolved
if _, err := a.CreateUser(ctx); err != nil {
return sendError(err)
}
return C.CString("true")
}

Expand Down Expand Up @@ -253,7 +249,7 @@ func deleteAccount(password *C.char) *C.char {
A: A.Bytes(),
}
log.Debugf("Delete Account request email %v A %v", lowerCaseEmail, A.Bytes())
srpB, err := a.AuthClient().LoginPrepare(context.Background(), prepareRequestBody)
srpB, err := a.AuthClient().LoginPrepare(ctx, prepareRequestBody)
if err != nil {
return sendError(err)
}
Expand Down Expand Up @@ -309,10 +305,5 @@ func deleteAccount(password *C.char) *C.char {
clearLocalUserData()
// Set user id and token to nil
a.Settings().SetUserIDAndToken(0, "")
// Create new user
// Create new user
if _, err := a.CreateUser(ctx); err != nil {
atavism marked this conversation as resolved.
Show resolved Hide resolved
return sendError(err)
}
return C.CString("true")
}
16 changes: 10 additions & 6 deletions desktop/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,19 +148,23 @@ func setProxyAll(value *C.char) {
//
//export hasPlanUpdatedOrBuy
func hasPlanUpdatedOrBuy() *C.char {
ctx := context.Background()
proClient := a.ProClient()
go proClient.PollUserData(ctx, a.Settings(), 10*time.Minute, proClient)
atavism marked this conversation as resolved.
Show resolved Hide resolved
//Get the cached user data
log.Debugf("DEBUG: Checking if user has updated plan or bought new plan")
cacheUserData, isOldFound := cachedUserData()
//Get latest user data
resp, err := a.ProClient().UserData(context.Background())
resp, err := a.ProClient().UserData(ctx)
if err != nil {
return sendError(err)
}
if isOldFound {
if cacheUserData.Expiration < resp.User.Expiration {
user := resp.User
if cacheUserData.Expiration < user.Expiration {
// New data has a later expiration
// if foud then update the cache
a.Settings().SetExpiration(resp.User.Expiration)
a.Settings().SetExpiration(user.Expiration)
return C.CString(string("true"))
}
}
Expand Down Expand Up @@ -268,8 +272,6 @@ func testProviderRequest(email *C.char, paymentProvider *C.char, plan *C.char) *
if err != nil {
return sendError(err)
}
//a.SetProUser(true)
go a.UserData(ctx)
return C.CString("true")
}

Expand Down Expand Up @@ -383,7 +385,8 @@ func deviceLinkingCode() *C.char {
//export paymentRedirect
func paymentRedirect(planID, currency, provider, email, deviceName *C.char) *C.char {
country := a.Settings().GetCountry()
resp, err := a.ProClient().PaymentRedirect(context.Background(), &protos.PaymentRedirectRequest{
ctx := context.Background()
resp, err := a.ProClient().PaymentRedirect(ctx, &protos.PaymentRedirectRequest{
Plan: C.GoString(planID),
Provider: C.GoString(provider),
Currency: strings.ToUpper(C.GoString(currency)),
Expand All @@ -394,6 +397,7 @@ func paymentRedirect(planID, currency, provider, email, deviceName *C.char) *C.c
if err != nil {
return sendError(err)
}

return sendJson(resp)
}

Expand Down
5 changes: 5 additions & 0 deletions desktop/settings/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,11 @@ func (s *Settings) GetLanguage() string {
return s.getString(SNLanguage)
}

// Locale returns the user language
func (s *Settings) Locale() string {
return s.getString(SNLanguage)
}

// SetReferralCode sets the user referral code
func (s *Settings) SetReferralCode(referralCode string) {
s.setVal(SNReferralCode, referralCode)
Expand Down
2 changes: 1 addition & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ github.com/getlantern/fdcount v0.0.0-20210503151800-5decd65b3731/go.mod h1:XZwE+
github.com/getlantern/filepersist v0.0.0-20160317154340-c5f0cd24e799/go.mod h1:8DGAx0LNUfXNnEH+fXI0s3OCBA/351kZCiz/8YSK3i8=
github.com/getlantern/filepersist v0.0.0-20210901195658-ed29a1cb0b7c h1:mcz27xtAkb1OuOLBct/uFfL1p3XxAIcFct82GbT+UZM=
github.com/getlantern/filepersist v0.0.0-20210901195658-ed29a1cb0b7c/go.mod h1:8DGAx0LNUfXNnEH+fXI0s3OCBA/351kZCiz/8YSK3i8=
github.com/getlantern/flashlight/v7 v7.6.147 h1:sF5wftnezW3kg3vn2dLpNDTHc25BhrwnHJox2zZChHo=
github.com/getlantern/flashlight/v7 v7.6.147 h1:bL20KaA+UkPzYI9uXMDJHf+gSpHJbVBV9f/8+/EwFHI=
github.com/getlantern/flashlight/v7 v7.6.147/go.mod h1:sLQXxC8MW4gqHXFjINLxbbz1GOPEf6Hwvs30gdVsAk8=
github.com/getlantern/framed v0.0.0-20190601192238-ceb6431eeede h1:yrU6Px3ZkvCsDLPryPGi6FN+2iqFPq+JeCb7EFoDBhw=
github.com/getlantern/framed v0.0.0-20190601192238-ceb6431eeede/go.mod h1:nhnoiS6DE6zfe+BaCMU4YI01UpsuiXnDqM5S8jxHuuI=
Expand Down
44 changes: 37 additions & 7 deletions internalsdk/android.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,17 @@ type AdProvider interface {
// thrown from the Java code. If a method interface doesn't include an error, exceptions on the
// Java side immediately result in a panic from which Go cannot recover.
type Session interface {
ClientSession
GetDeviceID() (string, error)
GetUserID() (int64, error)
GetToken() (string, error)
Locale() (string, error)
GetUserFirstVisit() (bool, error)
GetAppName() string
SetCountry(string) error
SetExpiration(int64) error
SetIP(string) error
SetReferralCode(string) error
SetProUser(bool) error
UpdateAdSettings(AdSettings) error
UpdateStats(serverCity string, serverCountry string, serverCountryCode string, p3 int, p4 int, hasSucceedingProxy bool) error
SetStaging(bool) error
Expand All @@ -105,7 +112,7 @@ type Session interface {
SetShowGoogleAds(bool)
SetHasConfigFetched(bool)
SetHasProxyFetched(bool)
SetUserIdAndToken(int64, string) error
SetUserIDAndToken(int64, string) error
SetOnSuccess(bool)
ChatEnable() bool
// workaround for lack of any sequence types in gomobile bind... ;_;
Expand All @@ -117,9 +124,11 @@ type Session interface {
// PanickingSession wraps the Session interface but panics instead of returning errors
type PanickingSession interface {
common.AuthConfig
GetUserFirstVisit() bool
SetCountry(string)
UpdateAdSettings(AdSettings)
UpdateStats(string, string, string, int, int, bool)
SetExpiration(int64)
SetStaging(bool)
BandwidthUpdate(int, int, int, int)
Locale() string
Expand All @@ -145,7 +154,9 @@ type PanickingSession interface {
SerializedInternalHeaders() string
SetHasConfigFetched(bool)
SetHasProxyFetched(bool)
SetUserIdAndToken(int64, string)
SetProUser(bool)
SetReferralCode(string)
SetUserIDAndToken(int64, string)
SetOnSuccess(bool)
ChatEnable() bool

Expand Down Expand Up @@ -183,10 +194,28 @@ func (s *panickingSessionImpl) GetToken() string {
return result
}

func (s *panickingSessionImpl) GetUserFirstVisit() bool {
result, err := s.wrapped.GetUserFirstVisit()
panicIfNecessary(err)
return result
}

func (s *panickingSessionImpl) SetCountry(country string) {
panicIfNecessary(s.wrapped.SetCountry(country))
}

func (s *panickingSessionImpl) SetExpiration(expiration int64) {
panicIfNecessary(s.wrapped.SetExpiration(expiration))
}

func (s *panickingSessionImpl) SetProUser(proUser bool) {
panicIfNecessary(s.wrapped.SetProUser(proUser))
}

func (s *panickingSessionImpl) SetReferralCode(referralCode string) {
panicIfNecessary(s.wrapped.SetReferralCode(referralCode))
}

func (s *panickingSessionImpl) SetIP(ipAddress string) {
panicIfNecessary(s.wrapped.SetIP(ipAddress))
}
Expand Down Expand Up @@ -300,8 +329,8 @@ func (s *panickingSessionImpl) SetShowGoogleAds(enabled bool) {
s.wrapped.SetShowGoogleAds(enabled)
}

func (s *panickingSessionImpl) SetUserIdAndToken(userID int64, token string) {
err := s.wrapped.SetUserIdAndToken(userID, token)
func (s *panickingSessionImpl) SetUserIDAndToken(userID int64, token string) {
err := s.wrapped.SetUserIDAndToken(userID, token)
panicIfNecessary(err)
}

Expand Down Expand Up @@ -538,7 +567,7 @@ func run(configDir, locale string, settings Settings, wrappedSession Session) {
config.ForceCountry(forcedCountryCode)
}

userConfig := newUserConfig(session)
userConfig := &userConfig{session: session}
globalConfigChanged := make(chan interface{})
geoRefreshed := geolookup.OnRefresh()

Expand Down Expand Up @@ -683,7 +712,8 @@ func afterStart(wrappedSession Session, session PanickingSession) {

if session.GetUserID() == 0 {
ctx := context.Background()
go retryCreateUser(ctx, wrappedSession)
proClient := createProClient(wrappedSession, "android")
go proClient.RetryCreateUser(ctx, session, 10*time.Minute)
atavism marked this conversation as resolved.
Show resolved Hide resolved
}

bandwidthUpdates(session)
Expand Down
6 changes: 5 additions & 1 deletion internalsdk/android_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func (c testSession) UpdateStats(string, string, string, int, int, bool) error {
func (c testSession) UpdateAdSettings(AdSettings) error { return nil }

func (c testSession) GetAppName() string { return "lantern" }
func (c testSession) GetUserFirstVisit() (bool, error) { return false, nil }
func (c testSession) AppVersion() (string, error) { return "6.9.0", nil }
func (c testSession) Code() (string, error) { return "1", nil }
func (c testSession) Currency() (string, error) { return "usd", nil }
Expand All @@ -73,9 +74,12 @@ func (c testSession) GetCountryCode() (string, error) { retur
func (c testSession) IsStoreVersion() (bool, error) { return false, nil }
func (c testSession) Provider() (string, error) { return "stripe", nil }
func (c testSession) SetChatEnabled(enabled bool) {}
func (c testSession) SetUserIdAndToken(userId int64, token string) error { return nil }
func (c testSession) SetUserIDAndToken(userId int64, token string) error { return nil }
func (c testSession) SetAuthEnabled(enabled bool) {}
func (c testSession) SetMatomoEnabled(bool) {}
func (c testSession) SetExpiration(expiration int64) error { return nil }
func (c testSession) SetProUser(proUser bool) error { return nil }
func (c testSession) SetReferralCode(referralCode string) error { return nil }
func (c testSession) IsPlayVersion() (bool, error) { return false, nil }
func (c testSession) SetShowGoogleAds(enabled bool) {}
func (c testSession) SetHasConfigFetched(enabled bool) {}
Expand Down
2 changes: 1 addition & 1 deletion internalsdk/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func SendIssueReport(
return err
}
return issue.SendReport(
newUserConfig(&panickingSessionImpl{session}),
&userConfig{&panickingSessionImpl{session}},
issueTypeInt,
description,
subscriptionLevel,
Expand Down
Loading
Loading