Skip to content

Commit

Permalink
Add ffiUserData and notes on protobuf JSON decoding (#1022)
Browse files Browse the repository at this point in the history
* updates to fetch user data when in app browser is closed

* update comment

* Update comment
  • Loading branch information
atavism authored Mar 19, 2024
1 parent 56a7f56 commit 7fd6b37
Show file tree
Hide file tree
Showing 6 changed files with 255 additions and 4 deletions.
2 changes: 1 addition & 1 deletion desktop/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func userData() *C.char {
if err != nil {
return sendError(err)
}
b, _ := json.Marshal(resp)
b, _ := json.Marshal(resp.User)
return C.CString(string(b))
}

Expand Down
9 changes: 9 additions & 0 deletions lib/ffi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ Pointer<Utf8> ffiHasSucceedingProxy() =>

Pointer<Utf8> ffiProUser() => _bindings.proUser().cast<Utf8>();

Future<User> ffiUserData() async {
final res = await _bindings.userData().cast<Utf8>().toDartString();
// it's necessary to use mergeFromProto3Json here instead of fromJson; otherwise, a FormatException with
// message Invalid radix-10 number is thrown.In addition, all possible JSON fields have to be defined on
// the User protobuf message or JSON decoding fails because of an "unknown field name" error:
// Protobuf JSON decoding failed at: root["telephone"]. Unknown field name 'telephone'
return User.create()..mergeFromProto3Json(jsonDecode(res));
}

Pointer<Utf8> ffiDevelopmentMode() => _bindings.developmentMode().cast<Utf8>();

Pointer<Utf8> ffiAcceptedTermsVersion() =>
Expand Down
4 changes: 2 additions & 2 deletions lib/plans/checkout.dart
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ class _CheckoutState extends State<Checkout>
await context.pushRoute(AppWebview(url: redirectUrl));
} else {
await browser.openUrl(redirectUrl, () async {
final res = await ffiProUser();
if (!widget.isPro && res == "true") {
final user = await ffiUserData();
if (!widget.isPro && user.userStatus == "active") {
// show success dialog if user becomes Pro during browser session
showSuccessDialog(context, widget.isPro);
}
Expand Down
198 changes: 198 additions & 0 deletions lib/vpn/protos_shared/vpn.pb.dart
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,204 @@ class PaymentMethod extends $pb.GeneratedMessage {
$core.List<PaymentProviders> get providers => $_getList(1);
}

class User extends $pb.GeneratedMessage {
factory User({
$fixnum.Int64? userId,
$core.String? email,
$core.String? telephone,
$core.String? userStatus,
$core.String? locale,
$fixnum.Int64? expiration,
$core.Iterable<Device>? devices,
$core.String? code,
$fixnum.Int64? expireAt,
$core.String? referral,
$core.String? token,
$core.bool? yinbiEnabled,
}) {
final $result = create();
if (userId != null) {
$result.userId = userId;
}
if (email != null) {
$result.email = email;
}
if (telephone != null) {
$result.telephone = telephone;
}
if (userStatus != null) {
$result.userStatus = userStatus;
}
if (locale != null) {
$result.locale = locale;
}
if (expiration != null) {
$result.expiration = expiration;
}
if (devices != null) {
$result.devices.addAll(devices);
}
if (code != null) {
$result.code = code;
}
if (expireAt != null) {
$result.expireAt = expireAt;
}
if (referral != null) {
$result.referral = referral;
}
if (token != null) {
$result.token = token;
}
if (yinbiEnabled != null) {
$result.yinbiEnabled = yinbiEnabled;
}
return $result;
}
User._() : super();
factory User.fromBuffer($core.List<$core.int> i, [$pb.ExtensionRegistry r = $pb.ExtensionRegistry.EMPTY]) => create()..mergeFromBuffer(i, r);
factory User.fromJson($core.String i, [$pb.ExtensionRegistry r = $pb.ExtensionRegistry.EMPTY]) => create()..mergeFromJson(i, r);

static final $pb.BuilderInfo _i = $pb.BuilderInfo(_omitMessageNames ? '' : 'User', createEmptyInstance: create)
..aInt64(1, _omitFieldNames ? '' : 'userId', protoName: 'userId')
..aOS(2, _omitFieldNames ? '' : 'email')
..aOS(3, _omitFieldNames ? '' : 'telephone')
..aOS(4, _omitFieldNames ? '' : 'userStatus', protoName: 'userStatus')
..aOS(5, _omitFieldNames ? '' : 'locale')
..aInt64(6, _omitFieldNames ? '' : 'expiration')
..pc<Device>(7, _omitFieldNames ? '' : 'devices', $pb.PbFieldType.PM, subBuilder: Device.create)
..aOS(8, _omitFieldNames ? '' : 'code')
..aInt64(9, _omitFieldNames ? '' : 'expireAt', protoName: 'expireAt')
..aOS(10, _omitFieldNames ? '' : 'referral')
..aOS(11, _omitFieldNames ? '' : 'token')
..aOB(12, _omitFieldNames ? '' : 'yinbiEnabled', protoName: 'yinbiEnabled')
..hasRequiredFields = false
;

@$core.Deprecated(
'Using this can add significant overhead to your binary. '
'Use [GeneratedMessageGenericExtensions.deepCopy] instead. '
'Will be removed in next major version')
User clone() => User()..mergeFromMessage(this);
@$core.Deprecated(
'Using this can add significant overhead to your binary. '
'Use [GeneratedMessageGenericExtensions.rebuild] instead. '
'Will be removed in next major version')
User copyWith(void Function(User) updates) => super.copyWith((message) => updates(message as User)) as User;

$pb.BuilderInfo get info_ => _i;

@$core.pragma('dart2js:noInline')
static User create() => User._();
User createEmptyInstance() => create();
static $pb.PbList<User> createRepeated() => $pb.PbList<User>();
@$core.pragma('dart2js:noInline')
static User getDefault() => _defaultInstance ??= $pb.GeneratedMessage.$_defaultFor<User>(create);
static User? _defaultInstance;

@$pb.TagNumber(1)
$fixnum.Int64 get userId => $_getI64(0);
@$pb.TagNumber(1)
set userId($fixnum.Int64 v) { $_setInt64(0, v); }
@$pb.TagNumber(1)
$core.bool hasUserId() => $_has(0);
@$pb.TagNumber(1)
void clearUserId() => clearField(1);

@$pb.TagNumber(2)
$core.String get email => $_getSZ(1);
@$pb.TagNumber(2)
set email($core.String v) { $_setString(1, v); }
@$pb.TagNumber(2)
$core.bool hasEmail() => $_has(1);
@$pb.TagNumber(2)
void clearEmail() => clearField(2);

@$pb.TagNumber(3)
$core.String get telephone => $_getSZ(2);
@$pb.TagNumber(3)
set telephone($core.String v) { $_setString(2, v); }
@$pb.TagNumber(3)
$core.bool hasTelephone() => $_has(2);
@$pb.TagNumber(3)
void clearTelephone() => clearField(3);

@$pb.TagNumber(4)
$core.String get userStatus => $_getSZ(3);
@$pb.TagNumber(4)
set userStatus($core.String v) { $_setString(3, v); }
@$pb.TagNumber(4)
$core.bool hasUserStatus() => $_has(3);
@$pb.TagNumber(4)
void clearUserStatus() => clearField(4);

@$pb.TagNumber(5)
$core.String get locale => $_getSZ(4);
@$pb.TagNumber(5)
set locale($core.String v) { $_setString(4, v); }
@$pb.TagNumber(5)
$core.bool hasLocale() => $_has(4);
@$pb.TagNumber(5)
void clearLocale() => clearField(5);

@$pb.TagNumber(6)
$fixnum.Int64 get expiration => $_getI64(5);
@$pb.TagNumber(6)
set expiration($fixnum.Int64 v) { $_setInt64(5, v); }
@$pb.TagNumber(6)
$core.bool hasExpiration() => $_has(5);
@$pb.TagNumber(6)
void clearExpiration() => clearField(6);

@$pb.TagNumber(7)
$core.List<Device> get devices => $_getList(6);

@$pb.TagNumber(8)
$core.String get code => $_getSZ(7);
@$pb.TagNumber(8)
set code($core.String v) { $_setString(7, v); }
@$pb.TagNumber(8)
$core.bool hasCode() => $_has(7);
@$pb.TagNumber(8)
void clearCode() => clearField(8);

@$pb.TagNumber(9)
$fixnum.Int64 get expireAt => $_getI64(8);
@$pb.TagNumber(9)
set expireAt($fixnum.Int64 v) { $_setInt64(8, v); }
@$pb.TagNumber(9)
$core.bool hasExpireAt() => $_has(8);
@$pb.TagNumber(9)
void clearExpireAt() => clearField(9);

@$pb.TagNumber(10)
$core.String get referral => $_getSZ(9);
@$pb.TagNumber(10)
set referral($core.String v) { $_setString(9, v); }
@$pb.TagNumber(10)
$core.bool hasReferral() => $_has(9);
@$pb.TagNumber(10)
void clearReferral() => clearField(10);

@$pb.TagNumber(11)
$core.String get token => $_getSZ(10);
@$pb.TagNumber(11)
set token($core.String v) { $_setString(10, v); }
@$pb.TagNumber(11)
$core.bool hasToken() => $_has(10);
@$pb.TagNumber(11)
void clearToken() => clearField(11);

@$pb.TagNumber(12)
$core.bool get yinbiEnabled => $_getBF(11);
@$pb.TagNumber(12)
set yinbiEnabled($core.bool v) { $_setBool(11, v); }
@$pb.TagNumber(12)
$core.bool hasYinbiEnabled() => $_has(11);
@$pb.TagNumber(12)
void clearYinbiEnabled() => clearField(12);
}


const _omitFieldNames = $core.bool.fromEnvironment('protobuf.omit_field_names');
const _omitMessageNames = $core.bool.fromEnvironment('protobuf.omit_message_names');
29 changes: 29 additions & 0 deletions lib/vpn/protos_shared/vpn.pbjson.dart
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,32 @@ final $typed_data.Uint8List paymentMethodDescriptor = $convert.base64Decode(
'Cg1QYXltZW50TWV0aG9kEhYKBm1ldGhvZBgBIAEoCVIGbWV0aG9kEi8KCXByb3ZpZGVycxgCIA'
'MoCzIRLlBheW1lbnRQcm92aWRlcnNSCXByb3ZpZGVycw==');

@$core.Deprecated('Use userDescriptor instead')
const User$json = {
'1': 'User',
'2': [
{'1': 'userId', '3': 1, '4': 1, '5': 3, '10': 'userId'},
{'1': 'email', '3': 2, '4': 1, '5': 9, '10': 'email'},
{'1': 'telephone', '3': 3, '4': 1, '5': 9, '10': 'telephone'},
{'1': 'userStatus', '3': 4, '4': 1, '5': 9, '10': 'userStatus'},
{'1': 'locale', '3': 5, '4': 1, '5': 9, '10': 'locale'},
{'1': 'expiration', '3': 6, '4': 1, '5': 3, '10': 'expiration'},
{'1': 'devices', '3': 7, '4': 3, '5': 11, '6': '.Device', '10': 'devices'},
{'1': 'code', '3': 8, '4': 1, '5': 9, '10': 'code'},
{'1': 'expireAt', '3': 9, '4': 1, '5': 3, '10': 'expireAt'},
{'1': 'referral', '3': 10, '4': 1, '5': 9, '10': 'referral'},
{'1': 'token', '3': 11, '4': 1, '5': 9, '10': 'token'},
{'1': 'yinbiEnabled', '3': 12, '4': 1, '5': 8, '10': 'yinbiEnabled'},
],
};

/// Descriptor for `User`. Decode as a `google.protobuf.DescriptorProto`.
final $typed_data.Uint8List userDescriptor = $convert.base64Decode(
'CgRVc2VyEhYKBnVzZXJJZBgBIAEoA1IGdXNlcklkEhQKBWVtYWlsGAIgASgJUgVlbWFpbBIcCg'
'l0ZWxlcGhvbmUYAyABKAlSCXRlbGVwaG9uZRIeCgp1c2VyU3RhdHVzGAQgASgJUgp1c2VyU3Rh'
'dHVzEhYKBmxvY2FsZRgFIAEoCVIGbG9jYWxlEh4KCmV4cGlyYXRpb24YBiABKANSCmV4cGlyYX'
'Rpb24SIQoHZGV2aWNlcxgHIAMoCzIHLkRldmljZVIHZGV2aWNlcxISCgRjb2RlGAggASgJUgRj'
'b2RlEhoKCGV4cGlyZUF0GAkgASgDUghleHBpcmVBdBIaCghyZWZlcnJhbBgKIAEoCVIIcmVmZX'
'JyYWwSFAoFdG9rZW4YCyABKAlSBXRva2VuEiIKDHlpbmJpRW5hYmxlZBgMIAEoCFIMeWluYmlF'
'bmFibGVk');

17 changes: 16 additions & 1 deletion protos_shared/vpn.proto
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,19 @@ message PaymentProviders {
message PaymentMethod {
string method = 1;
repeated PaymentProviders providers = 2;
}
}

message User {
int64 userId = 1;
string email = 2;
string telephone = 3;
string userStatus = 4;
string locale = 5;
int64 expiration = 6;
repeated Device devices = 7;
string code = 8;
int64 expireAt = 9;
string referral = 10;
string token = 11;
bool yinbiEnabled = 12;
}

0 comments on commit 7fd6b37

Please sign in to comment.