Subscriptions: New entitlement shape#15059
Conversation
prateekbh
commented
May 3, 2018
- Rewriting the entitlement object
| constructor({source, raw = '', service, products = [], | ||
| subscriptionToken = '', loggedIn = false, metering = null}) { | ||
| constructor({source, raw = '', service, granted = false, | ||
| grantReason = '', data}) { |
There was a problem hiding this comment.
data here throws
The type information available for this expression is too loose to ensure conformance.
I don't know a workaround
There was a problem hiding this comment.
For data or for grantReason? B/c I could see why with grantReason...
Do you also want to default data to null?
If this doesn't work - you may have to define a @typedef.
| const SERVICE_TIMEOUT = 3000; | ||
|
|
||
| /** @typedef {{loggedIn: boolean, subscribed: boolean, granted: boolean, entitlement: !JsonObject, metered: boolean}} */ | ||
| /** @typedef {{granted: boolean, subscribed: boolean, entitlement: !JsonObject, }} */ |
There was a problem hiding this comment.
Can we remove this object completely? In general isn't it now more or less 100% equal to Entitlement object? I'd really like to get rid of this object, tbh.
If not, can we then add grantReason here?
There was a problem hiding this comment.
Yep, removed RenderState
|
|
||
| /** @typedef {{left: number, total: number, resetTime: number, durationUnit: string, token: string}} */ | ||
| export let MeteringData; | ||
| export const GrantReasons = { |
There was a problem hiding this comment.
Let's add @enum {string} and remove quotes for names. I.e.:
/** @enum {string} */
export const GrantReason = {
SUBSCRIBER: 'SUBSCRIBER',
METERING: 'METERING',
}
Also, I like "SUBSCRIBER" a bit better than "SUBSCRIBED". Feel free to argue your point though - i don't feel very strongly about it.
One thing for sure though. GrantReason must be singular, not plural.
There was a problem hiding this comment.
I am not sure what do you mean by singular?
There was a problem hiding this comment.
Plus yeah no strong reason for SUBSCRIBED as well so i can change if you want
There was a problem hiding this comment.
Singular I mean "GrantReason" vs "GrantReasons". And, after some thinking, yeah, let's use "subscriber"
| constructor({source, raw = '', service, products = [], | ||
| subscriptionToken = '', loggedIn = false, metering = null}) { | ||
| constructor({source, raw = '', service, granted = false, | ||
| grantReason = '', data}) { |
There was a problem hiding this comment.
For data or for grantReason? B/c I could see why with grantReason...
Do you also want to default data to null?
If this doesn't work - you may have to define a @typedef.
| @@ -79,10 +72,9 @@ export class Entitlement { | |||
| 'raw': this.raw, | |||
There was a problem hiding this comment.
I think we actually should remove raw from json() form. In pingback it's ok since it serves a pretty different purpose.
| 'raw': this.raw, | ||
| 'source': this.source, | ||
| 'grantState': this.enablesThis(), | ||
| 'grantState': this.granted, |
There was a problem hiding this comment.
I think we should apply the same form with granted and grantReason. Probably data too.
| this.grantStatusEntitlementPromise_ = new Promise(resolve => { | ||
| if ((this.grantStatusEntitlement_ | ||
| && this.grantStatusEntitlement_.subscriptionToken) | ||
| if ((this.grantStatusEntitlement_ && this.grantStatusEntitlement_.granted) |
There was a problem hiding this comment.
Hmm. I think the old code actually meant what it said with .subscriptionToken. I.e. we need to be checking .isSubscribed() here. I believe the idea was to only do early resolution of promise for a subscriber and wait until later for other types of grant. This also means a good comment is in order :)
There was a problem hiding this comment.
oh yeah may bad, should be checking .isSubscribed
|
|
||
| // Subscriber wins immediatly. | ||
| if (!!entitlement.subscriptionToken) { | ||
| if (!!entitlement.isSubscribed()) { |
There was a problem hiding this comment.
It's strictly boolean so let's remove !!
| entitlement: selectedEntitlement.json(), | ||
| loggedIn: selectedEntitlement.loggedIn, | ||
| subscribed: !!selectedEntitlement.subscriptionToken, | ||
| subscribed: !!selectedEntitlement.isSubscribed(), |
| Entitlement.parseFromJson(entitlements[index], token); | ||
| if (entitlementObject.enables(currentProductId)) { | ||
| entitlement = entitlementObject; | ||
| if (entitlements[index].products.indexOf(currentProductId) !== -1) { |
There was a problem hiding this comment.
So now you're accessing a json object, right? In that case you need to reference the fields with quotes.
| * @param {?string} [input.subscriptionToken] | ||
| * @param {boolean} [input.loggedIn] | ||
| * @param {?MeteringData} [input.metering] | ||
| * @param {boolean} [input.granted] |
There was a problem hiding this comment.
In this case we should also update amp-subscriptions-google platform in this PR, right?
dvoytenko
left a comment
There was a problem hiding this comment.
Almost there. Couple more clarifications.
|
|
||
| selectedPlatform.activate(renderState); | ||
|
|
||
| selectedPlatform.activate(selectedEntitlement.json()); |
There was a problem hiding this comment.
Let's just pass the entitlement itself. The platform can always call json() on it itself if it wants to :)
| // Offers or abbreviated offers may need to be shown depending on | ||
| // whether the access has been granted and whether user is a subscriber. | ||
| if (!renderState.granted) { | ||
| if (!renderState['granted']) { |
There was a problem hiding this comment.
Ditto: let's pass the actual Entitlement object. Then it'd be a cleaner:
if (!entitlement.granted) {}
..
if (!entitlement.isSubscriber()) {}
...
|
This pull request introduces 1 alert when merging 0cb9b43 into 9beaf37 - view on lgtm.com new alerts:
Comment posted by lgtm.com |
| if (!entitlement.granted) { | ||
| this.runtime_.showOffers({list: 'amp'}); | ||
| } else if (!renderState.subscribed) { | ||
| } else if (entitlement.grantReason !== GrantReason.SUBSCRIBER) { |
There was a problem hiding this comment.
Why not just use entitlement.isSubscriber()?
| "products": ["norcal_tribune.com:basic",...], | ||
| "subscriptionToken": "subscription-token", | ||
| "loggedIn": true/false, | ||
| "granted": true |