Skip to content

Subscriptions: New entitlement shape#15059

Merged
prateekbh merged 20 commits intoampproject:masterfrom
prateekbh:new-entitlement
May 12, 2018
Merged

Subscriptions: New entitlement shape#15059
prateekbh merged 20 commits intoampproject:masterfrom
prateekbh:new-entitlement

Conversation

@prateekbh
Copy link
Copy Markdown
Member

  • Rewriting the entitlement object

@prateekbh prateekbh requested a review from dvoytenko May 3, 2018 20:13
constructor({source, raw = '', service, products = [],
subscriptionToken = '', loggedIn = false, metering = null}) {
constructor({source, raw = '', service, granted = false,
grantReason = '', data}) {
Copy link
Copy Markdown
Member Author

@prateekbh prateekbh May 3, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

data here throws

The type information available for this expression is too loose to ensure conformance.

I don't know a workaround

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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, }} */
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, removed RenderState


/** @typedef {{left: number, total: number, resetTime: number, durationUnit: string, token: string}} */
export let MeteringData;
export const GrantReasons = {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure what do you mean by singular?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Plus yeah no strong reason for SUBSCRIBED as well so i can change if you want

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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}) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we actually should remove raw from json() form. In pingback it's ok since it serves a pretty different purpose.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed

'raw': this.raw,
'source': this.source,
'grantState': this.enablesThis(),
'grantState': this.granted,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should apply the same form with granted and grantReason. Probably data too.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made this raw + json()

this.grantStatusEntitlementPromise_ = new Promise(resolve => {
if ((this.grantStatusEntitlement_
&& this.grantStatusEntitlement_.subscriptionToken)
if ((this.grantStatusEntitlement_ && this.grantStatusEntitlement_.granted)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 :)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh yeah may bad, should be checking .isSubscribed


// Subscriber wins immediatly.
if (!!entitlement.subscriptionToken) {
if (!!entitlement.isSubscribed()) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's strictly boolean so let's remove !!

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

entitlement: selectedEntitlement.json(),
loggedIn: selectedEntitlement.loggedIn,
subscribed: !!selectedEntitlement.subscriptionToken,
subscribed: !!selectedEntitlement.isSubscribed(),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why !!?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo. Removed.

Entitlement.parseFromJson(entitlements[index], token);
if (entitlementObject.enables(currentProductId)) {
entitlement = entitlementObject;
if (entitlements[index].products.indexOf(currentProductId) !== -1) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So now you're accessing a json object, right? In that case you need to reference the fields with quotes.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

* @param {?string} [input.subscriptionToken]
* @param {boolean} [input.loggedIn]
* @param {?MeteringData} [input.metering]
* @param {boolean} [input.granted]
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case we should also update amp-subscriptions-google platform in this PR, right?

Copy link
Copy Markdown
Contributor

@dvoytenko dvoytenko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost there. Couple more clarifications.


selectedPlatform.activate(renderState);

selectedPlatform.activate(selectedEntitlement.json());
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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']) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto: let's pass the actual Entitlement object. Then it'd be a cleaner:

if (!entitlement.granted) {}
..
if (!entitlement.isSubscriber()) {}
...

@ghost
Copy link
Copy Markdown

ghost commented May 7, 2018

This pull request introduces 1 alert when merging 0cb9b43 into 9beaf37 - view on lgtm.com

new alerts:

  • 1 for Property access on null or undefined

Comment posted by lgtm.com

if (!entitlement.granted) {
this.runtime_.showOffers({list: 'amp'});
} else if (!renderState.subscribed) {
} else if (entitlement.grantReason !== GrantReason.SUBSCRIBER) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just use entitlement.isSubscriber()?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

"products": ["norcal_tribune.com:basic",...],
"subscriptionToken": "subscription-token",
"loggedIn": true/false,
"granted": true
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comma

@prateekbh prateekbh merged commit 04c5b80 into ampproject:master May 12, 2018
@prateekbh prateekbh deleted the new-entitlement branch May 12, 2018 02:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants