-
-
Notifications
You must be signed in to change notification settings - Fork 193
Expand file tree
/
Copy pathentitlements.js
More file actions
212 lines (188 loc) · 8.03 KB
/
entitlements.js
File metadata and controls
212 lines (188 loc) · 8.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
* GNU AGPL-3.0 License
*
* Copyright (c) 2021 - present core.ai . All rights reserved.
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://opensource.org/licenses/AGPL-3.0.
*
*/
/**
* Entitlements Service
*
* This module provides a dedicated API for managing user entitlements,
* including plan details, trial status, and feature entitlements.
*/
define(function (require, exports, module) {
const KernalModeTrust = window.KernalModeTrust;
if(!KernalModeTrust){
// integrated extensions will have access to kernal mode, but not external extensions
throw new Error("Login service should have access to KernalModeTrust. Cannot boot without trust ring");
}
const EventDispatcher = require("utils/EventDispatcher"),
Strings = require("strings");
const MS_IN_DAY = 24 * 60 * 60 * 1000;
const FREE_PLAN_VALIDITY_DAYS = 10000;
let LoginService;
// Create secure exports and set up event dispatcher
const Entitlements = {};
EventDispatcher.makeEventDispatcher(Entitlements);
// Event constants
const EVENT_ENTITLEMENTS_CHANGED = "entitlements_changed";
/**
* Check if user is logged in. Best to check after `EVENT_ENTITLEMENTS_CHANGED`.
* @returns {*}
*/
function isLoggedIn() {
return LoginService.isLoggedIn();
}
/**
* Attempts to sign in to the user's account if the user is not already logged in.
* You should listen to `EVENT_ENTITLEMENTS_CHANGED` to know when the login status changes. This function
* returns immediately and does not wait for the login process to complete.
*
* @return {void} Does not return a value.
*/
function loginToAccount() {
if(isLoggedIn()){
return;
}
KernalModeTrust.loginService.signInToAccount()
.catch(function(err){
console.error("Error signing in to account", err);
});
}
let effectiveEntitlements = null;
async function _getEffectiveEntitlements() {
if(effectiveEntitlements){
return effectiveEntitlements;
}
const entitlements = await LoginService.getEffectiveEntitlements();
effectiveEntitlements = entitlements;
return entitlements;
}
/**
* Get the plan details from entitlements with fallback to free plan defaults. If the user is
* in pro trial(isInProTrial API), then paidSubscriber will always be true as we need to treat user as paid.
* you should use isInProTrial API to check if user is in pro trial if some trial-related logic needs to be done.
* @returns {Promise<Object>} Plan details object
*/
async function getPlanDetails() {
const entitlements = await _getEffectiveEntitlements();
if (entitlements && entitlements.plan) {
return entitlements.plan;
}
// Fallback to free plan defaults
const currentDate = Date.now();
return {
paidSubscriber: false,
name: Strings.USER_FREE_PLAN_NAME,
validTill: currentDate + (FREE_PLAN_VALIDITY_DAYS * MS_IN_DAY)
};
}
/**
* Check if user is in a pro trial. IF the user is in pro trail, then `plan.paidSubscriber` will always be true.
* @returns {Promise<boolean>} True if user is in pro trial, false otherwise
*/
async function isInProTrial() {
const entitlements = await _getEffectiveEntitlements();
return !!(entitlements && entitlements.isInProTrial);
}
/**
* Get remaining trial days
* @returns {Promise<number>} Number of remaining trial days
*/
async function getTrialRemainingDays() {
const entitlements = await _getEffectiveEntitlements();
return entitlements && entitlements.trialDaysRemaining ? entitlements.trialDaysRemaining : 0;
}
/**
* Get current raw entitlements. Should not be used directly, use individual feature entitlement instead
* like getLiveEditEntitlement. Raw entitlements are server set and will not contain trial info.
* @returns {Promise<Object|null>} Raw entitlements object or null
*/
async function getRawEntitlements() {
return await LoginService.getEntitlements();
}
/**
* Get live edit is enabled for user, based on his logged in pro-user/trial status.
*
* @returns {Promise<Object>} Live edit entitlement object with the following shape:
* @returns {Promise<Object>} entitlement
* @returns {Promise<boolean>} entitlement.activated - If true, enable live edit feature.
* If false, use promotions.showProUpsellDialog
* with UPSELL_TYPE_LIVE_EDIT to show an upgrade dialog if needed.
* @returns {Promise<string>} entitlement.subscribeURL - URL to subscribe/purchase if not activated
* @returns {Promise<string>} entitlement.upgradeToPlan - Plan name that includes live edit entitlement
* @returns {Promise<number>} [entitlement.validTill] - Timestamp when entitlement expires (if from server)
*
* @example
* const liveEditEntitlement = await Entitlements.getLiveEditEntitlement();
* if (liveEditEntitlement.activated) {
* // Enable live edit feature
* enableLiveEditFeature();
* } else {
* // Show upgrade dialog when user tries to use live edit
* promotions.showProUpsellDialog(promotions.UPSELL_TYPE_LIVE_EDIT);
* }
*/
async function getLiveEditEntitlement() {
const entitlements = await _getEffectiveEntitlements();
if (entitlements && entitlements.entitlements && entitlements.entitlements.liveEdit) {
return entitlements.entitlements.liveEdit;
}
// Fallback defaults when live edit entitlement is not available from API
return {
activated: false,
subscribeURL: brackets.config.purchase_url,
upgradeToPlan: brackets.config.main_pro_plan
};
}
// Set up KernalModeTrust.Entitlements
KernalModeTrust.Entitlements = Entitlements;
let inited = false;
function init() {
if(inited){
return;
}
inited = true;
LoginService = KernalModeTrust.loginService;
// Set up event forwarding from LoginService
LoginService.on(LoginService.EVENT_ENTITLEMENTS_CHANGED, function() {
effectiveEntitlements = null;
Entitlements.trigger(EVENT_ENTITLEMENTS_CHANGED);
});
}
// Test-only exports for integration testing
if (Phoenix.isTestWindow) {
window._test_entitlements_exports = {
EntitlementsService: Entitlements,
isLoggedIn,
getPlanDetails,
isInProTrial,
getTrialRemainingDays,
getRawEntitlements,
getLiveEditEntitlement,
loginToAccount
};
}
exports.init = init;
// no public exports to prevent extension tampering
// Add functions to secure exports. These can be accessed via `KernalModeTrust.Entitlements.*`
Entitlements.isLoggedIn = isLoggedIn;
Entitlements.loginToAccount = loginToAccount;
Entitlements.getPlanDetails = getPlanDetails;
Entitlements.isInProTrial = isInProTrial;
Entitlements.getTrialRemainingDays = getTrialRemainingDays;
Entitlements.getRawEntitlements = getRawEntitlements;
Entitlements.getLiveEditEntitlement = getLiveEditEntitlement;
Entitlements.EVENT_ENTITLEMENTS_CHANGED = EVENT_ENTITLEMENTS_CHANGED;
});