@sumaris-net/ngx-components 0.28.5 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bundles/sumaris-net.ngx-components.umd.js +301 -312
- package/bundles/sumaris-net.ngx-components.umd.js.map +1 -1
- package/bundles/sumaris-net.ngx-components.umd.min.js +2 -2
- package/bundles/sumaris-net.ngx-components.umd.min.js.map +1 -1
- package/doc/changelog.md +5 -0
- package/esm2015/src/app/core/graphql/graphql.service.js +5 -8
- package/esm2015/src/app/core/services/account.service.js +177 -188
- package/esm2015/src/app/core/services/base-entity-service.class.js +9 -7
- package/esm2015/src/app/core/services/base-graphql-service.class.js +7 -2
- package/esm2015/src/app/core/services/config.service.js +9 -54
- package/esm2015/src/app/core/services/model/account.model.js +1 -1
- package/esm2015/src/app/core/services/model/settings.model.js +29 -2
- package/esm2015/src/app/core/services/network.service.js +5 -7
- package/esm2015/src/app/core/services/storage/entities-storage.service.js +3 -6
- package/esm2015/src/app/shared/functions.js +15 -9
- package/esm2015/src/app/shared/services/startable-service.class.js +13 -2
- package/fesm2015/sumaris-net.ngx-components.js +255 -270
- package/fesm2015/sumaris-net.ngx-components.js.map +1 -1
- package/package.json +1 -1
- package/src/app/core/graphql/graphql.service.d.ts +0 -1
- package/src/app/core/services/account.service.d.ts +8 -21
- package/src/app/core/services/base-entity-service.class.d.ts +1 -0
- package/src/app/core/services/base-graphql-service.class.d.ts +3 -2
- package/src/app/core/services/config.service.d.ts +2 -9
- package/src/app/core/services/model/settings.model.d.ts +2 -0
- package/src/app/core/services/network.service.d.ts +2 -2
- package/src/app/core/services/storage/entities-storage.service.d.ts +0 -1
- package/src/app/shared/services/startable-service.class.d.ts +3 -1
- package/sumaris-net.ngx-components.metadata.json +1 -1
|
@@ -250,26 +250,32 @@ function suggestFromArray(items, value, opts) {
|
|
|
250
250
|
return { data: [value] };
|
|
251
251
|
value = (typeof value === 'string' && value !== '*') && value.toUpperCase() || undefined;
|
|
252
252
|
const keys = opts && (opts.searchAttribute && [opts.searchAttribute] || opts.searchAttributes) || ['label'];
|
|
253
|
+
if (!items) {
|
|
254
|
+
return { data: [], total: 0 };
|
|
255
|
+
}
|
|
253
256
|
// Filter items
|
|
254
|
-
|
|
257
|
+
let target = items;
|
|
258
|
+
if (isNotNilOrBlank(value)) {
|
|
255
259
|
// If wildcard, search using regexp
|
|
256
260
|
if (value.indexOf('*') !== -1) {
|
|
257
261
|
value = value.replace('*', '.*');
|
|
258
|
-
|
|
262
|
+
target = target.filter(v => keys.findIndex(key => matchUpperCase(getPropertyByPathAsString(v, key), value)) !== -1);
|
|
259
263
|
}
|
|
260
264
|
// Else (no wildcard), search using startsWith
|
|
261
265
|
else {
|
|
262
|
-
|
|
266
|
+
target = target.filter(v => keys.findIndex(key => startsWithUpperCase(getPropertyByPathAsString(v, key), value)) !== -1);
|
|
263
267
|
}
|
|
264
268
|
}
|
|
265
269
|
// Exclude ids
|
|
266
|
-
if (
|
|
267
|
-
|
|
268
|
-
}
|
|
269
|
-
// Sort
|
|
270
|
-
|
|
270
|
+
if (isNotEmptyArray(opts === null || opts === void 0 ? void 0 : opts.excludedIds)) {
|
|
271
|
+
target = target.filter(v => !opts.excludedIds.includes(+v['id']));
|
|
272
|
+
}
|
|
273
|
+
// Sort (copy if need, before sorting)
|
|
274
|
+
if (target === items)
|
|
275
|
+
target = target.slice();
|
|
276
|
+
target.sort(propertiesPathComparator(keys));
|
|
277
|
+
const total = items.length || 0;
|
|
271
278
|
// Truncate if need
|
|
272
|
-
const total = (items === null || items === void 0 ? void 0 : items.length) || 0;
|
|
273
279
|
if (opts) {
|
|
274
280
|
if (opts.offset > 0) {
|
|
275
281
|
items = items.slice(opts.offset);
|
|
@@ -7757,6 +7763,9 @@ class StartableService {
|
|
|
7757
7763
|
this._startByReadyFunction = true; // should start when calling ready() ?
|
|
7758
7764
|
this._data = null;
|
|
7759
7765
|
this._started = false;
|
|
7766
|
+
this._startPromise = null;
|
|
7767
|
+
this._startPrerequisite = null;
|
|
7768
|
+
this._subscription = null;
|
|
7760
7769
|
this._startPrerequisite = prerequisiteService
|
|
7761
7770
|
? () => prerequisiteService.ready()
|
|
7762
7771
|
: () => Promise.resolve();
|
|
@@ -7786,6 +7795,10 @@ class StartableService {
|
|
|
7786
7795
|
stop() {
|
|
7787
7796
|
return __awaiter(this, void 0, void 0, function* () {
|
|
7788
7797
|
try {
|
|
7798
|
+
if (this._subscription) {
|
|
7799
|
+
this._subscription.unsubscribe();
|
|
7800
|
+
this._subscription = null;
|
|
7801
|
+
}
|
|
7789
7802
|
yield this.ngOnStop();
|
|
7790
7803
|
this._started = false;
|
|
7791
7804
|
this._startPromise = undefined;
|
|
@@ -7820,6 +7833,10 @@ class StartableService {
|
|
|
7820
7833
|
return waitFor(() => this._started)
|
|
7821
7834
|
.then(() => this._data);
|
|
7822
7835
|
}
|
|
7836
|
+
registerSubscription(sub) {
|
|
7837
|
+
this._subscription = this._subscription || new Subscription();
|
|
7838
|
+
return this._subscription.add(sub);
|
|
7839
|
+
}
|
|
7823
7840
|
ngOnStop() {
|
|
7824
7841
|
return __awaiter(this, void 0, void 0, function* () {
|
|
7825
7842
|
// Can be override by subclasses
|
|
@@ -10023,7 +10040,6 @@ class NetworkService extends StartableService {
|
|
|
10023
10040
|
this.onPeerChanges = this.onStart.pipe(map(peer => peer && peer.url), filter(isNotNilOrBlank), distinctUntilChanged());
|
|
10024
10041
|
this.onNetworkStatusChanges = new BehaviorSubject(null);
|
|
10025
10042
|
this.onResetNetworkCache = new EventEmitter(true);
|
|
10026
|
-
this._subscription = new Subscription();
|
|
10027
10043
|
this._listeners = {};
|
|
10028
10044
|
this._mobile = this.platform.is('mobile');
|
|
10029
10045
|
if (this._mobile) {
|
|
@@ -10184,7 +10200,8 @@ class NetworkService extends StartableService {
|
|
|
10184
10200
|
/**
|
|
10185
10201
|
* Check if the peer is alive
|
|
10186
10202
|
*
|
|
10187
|
-
* @param
|
|
10203
|
+
* @param peer
|
|
10204
|
+
* @param opts
|
|
10188
10205
|
*/
|
|
10189
10206
|
checkPeerAlive(peer, opts) {
|
|
10190
10207
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -10324,8 +10341,8 @@ class NetworkService extends StartableService {
|
|
|
10324
10341
|
this.startRefreshTimer();
|
|
10325
10342
|
// Listen for device network changes
|
|
10326
10343
|
if (this.network) {
|
|
10327
|
-
this.
|
|
10328
|
-
this.
|
|
10344
|
+
this.registerSubscription(this.network.onDisconnect().subscribe(() => this.onDeviceConnectionChanged('none')));
|
|
10345
|
+
this.registerSubscription(this.network.onConnect().subscribe(() => this.onDeviceConnectionChanged(this.network.type)));
|
|
10329
10346
|
}
|
|
10330
10347
|
});
|
|
10331
10348
|
}
|
|
@@ -10336,8 +10353,6 @@ class NetworkService extends StartableService {
|
|
|
10336
10353
|
if (this._timerRefreshCondition() === false) {
|
|
10337
10354
|
this.stopRefreshTimer();
|
|
10338
10355
|
}
|
|
10339
|
-
this._subscription.unsubscribe();
|
|
10340
|
-
this._subscription = new Subscription();
|
|
10341
10356
|
});
|
|
10342
10357
|
}
|
|
10343
10358
|
/**
|
|
@@ -11032,7 +11047,6 @@ class EntitiesStorage extends StartableService {
|
|
|
11032
11047
|
this.progressBarService = progressBarService;
|
|
11033
11048
|
this.storage = storage;
|
|
11034
11049
|
this.environment = environment;
|
|
11035
|
-
this._subscription = new Subscription();
|
|
11036
11050
|
this._$save = new EventEmitter(true);
|
|
11037
11051
|
this._dirty = false;
|
|
11038
11052
|
this._saving = false;
|
|
@@ -11325,7 +11339,7 @@ class EntitiesStorage extends StartableService {
|
|
|
11325
11339
|
// Restore sequences
|
|
11326
11340
|
yield this.restoreLocally();
|
|
11327
11341
|
// Start a save timer
|
|
11328
|
-
this.
|
|
11342
|
+
this.registerSubscription(merge(this._$save, timer(this._saveTimerPeriod, this._saveTimerPeriod))
|
|
11329
11343
|
.pipe(
|
|
11330
11344
|
// Avoid to many call (e.g. when $save AND timer are triggered
|
|
11331
11345
|
throttleTime(this._saveTimerPeriod))
|
|
@@ -11336,8 +11350,6 @@ class EntitiesStorage extends StartableService {
|
|
|
11336
11350
|
}
|
|
11337
11351
|
ngOnStop() {
|
|
11338
11352
|
return __awaiter(this, void 0, void 0, function* () {
|
|
11339
|
-
this._subscription.unsubscribe();
|
|
11340
|
-
this._subscription = new Subscription();
|
|
11341
11353
|
if (this.dirty) {
|
|
11342
11354
|
yield this.storeLocally();
|
|
11343
11355
|
}
|
|
@@ -11519,7 +11531,34 @@ let UserSettings = UserSettings_1 = class UserSettings extends Entity {
|
|
|
11519
11531
|
}
|
|
11520
11532
|
this.nonce = source.nonce;
|
|
11521
11533
|
}
|
|
11534
|
+
merge(source, includesProperties) {
|
|
11535
|
+
if (!source)
|
|
11536
|
+
return; // Skip
|
|
11537
|
+
// Reset content
|
|
11538
|
+
this.content = {};
|
|
11539
|
+
let hasChanges = false;
|
|
11540
|
+
// For each property of source object
|
|
11541
|
+
Object.keys(source)
|
|
11542
|
+
.forEach(key => {
|
|
11543
|
+
// If property exists in UserSettings class: store it directly
|
|
11544
|
+
if (UserSettings_1.SETTINGS_PROPERTIES.includes(key)) {
|
|
11545
|
+
if (!equals(this[key], source[key])) {
|
|
11546
|
+
this[key] = source[key];
|
|
11547
|
+
hasChanges = true;
|
|
11548
|
+
}
|
|
11549
|
+
}
|
|
11550
|
+
// Else, store property into the 'content' map
|
|
11551
|
+
else if (includesProperties && includesProperties.includes(key)) {
|
|
11552
|
+
if (!equals(this.content[key], source[key])) {
|
|
11553
|
+
this.content[key] = Object.assign({}, source[key]);
|
|
11554
|
+
hasChanges = true;
|
|
11555
|
+
}
|
|
11556
|
+
}
|
|
11557
|
+
});
|
|
11558
|
+
return hasChanges;
|
|
11559
|
+
}
|
|
11522
11560
|
};
|
|
11561
|
+
UserSettings.SETTINGS_PROPERTIES = ['locale', 'latLongFormat'];
|
|
11523
11562
|
UserSettings = UserSettings_1 = __decorate([
|
|
11524
11563
|
EntityClass({ typename: 'UserSettingsVO' })
|
|
11525
11564
|
], UserSettings);
|
|
@@ -11932,7 +11971,6 @@ class GraphqlService extends StartableService {
|
|
|
11932
11971
|
this.cryptoService = cryptoService;
|
|
11933
11972
|
this.environment = environment;
|
|
11934
11973
|
this.typePolicies = typePolicies;
|
|
11935
|
-
this._subscription = new Subscription();
|
|
11936
11974
|
this.connectionParams = {};
|
|
11937
11975
|
this.onNetworkError = new Subject();
|
|
11938
11976
|
this.customErrors = {};
|
|
@@ -12432,7 +12470,7 @@ class GraphqlService extends StartableService {
|
|
|
12432
12470
|
});
|
|
12433
12471
|
// Creating a mutation queue
|
|
12434
12472
|
const queueLink = new QueueLink();
|
|
12435
|
-
this.
|
|
12473
|
+
this.registerSubscription(this._networkStatusChanged$
|
|
12436
12474
|
.subscribe(type => {
|
|
12437
12475
|
// Network is offline: start buffering into queue
|
|
12438
12476
|
if (type === 'none') {
|
|
@@ -12495,9 +12533,9 @@ class GraphqlService extends StartableService {
|
|
|
12495
12533
|
}
|
|
12496
12534
|
}
|
|
12497
12535
|
// Listen for network restart
|
|
12498
|
-
this.
|
|
12536
|
+
this.registerSubscription(this.network.on('start', () => this.restart()));
|
|
12499
12537
|
// Listen for clear cache request, from network
|
|
12500
|
-
this.
|
|
12538
|
+
this.registerSubscription(this.network.on('resetCache', () => this.clearCache()));
|
|
12501
12539
|
console.info('[graphql] Starting graphql [OK]');
|
|
12502
12540
|
return client;
|
|
12503
12541
|
});
|
|
@@ -12505,8 +12543,6 @@ class GraphqlService extends StartableService {
|
|
|
12505
12543
|
ngOnStop() {
|
|
12506
12544
|
return __awaiter(this, void 0, void 0, function* () {
|
|
12507
12545
|
console.info('[graphql] Stopping graphql service...');
|
|
12508
|
-
this._subscription.unsubscribe();
|
|
12509
|
-
this._subscription = new Subscription();
|
|
12510
12546
|
yield this.resetClient();
|
|
12511
12547
|
});
|
|
12512
12548
|
}
|
|
@@ -12625,8 +12661,9 @@ const sha256 = require('hash.js/lib/hash/sha/256');
|
|
|
12625
12661
|
class BaseGraphqlServiceOptions {
|
|
12626
12662
|
}
|
|
12627
12663
|
// eslint-disable-next-line @angular-eslint/directive-class-suffix
|
|
12628
|
-
class BaseGraphqlService {
|
|
12664
|
+
class BaseGraphqlService extends StartableService {
|
|
12629
12665
|
constructor(graphql, options) {
|
|
12666
|
+
super(graphql);
|
|
12630
12667
|
this.graphql = graphql;
|
|
12631
12668
|
this._mutableWatchQueries = [];
|
|
12632
12669
|
// Max updated queries, for this entity.
|
|
@@ -12818,6 +12855,9 @@ class BaseGraphqlService {
|
|
|
12818
12855
|
return { query, variables };
|
|
12819
12856
|
});
|
|
12820
12857
|
}
|
|
12858
|
+
ngOnStart() {
|
|
12859
|
+
return Promise.resolve(null);
|
|
12860
|
+
}
|
|
12821
12861
|
}
|
|
12822
12862
|
BaseGraphqlService.decorators = [
|
|
12823
12863
|
{ type: Directive }
|
|
@@ -12951,18 +12991,16 @@ class AccountService extends BaseGraphqlService {
|
|
|
12951
12991
|
this.onChange = new Subject();
|
|
12952
12992
|
this.onAuthTokenChange = new Subject();
|
|
12953
12993
|
this.onAuthBasicChange = new Subject();
|
|
12954
|
-
this.
|
|
12994
|
+
this._cache = {
|
|
12955
12995
|
loaded: false,
|
|
12956
12996
|
keypair: null,
|
|
12957
12997
|
authToken: null,
|
|
12958
12998
|
authBasic: null,
|
|
12959
12999
|
pubkey: null,
|
|
12960
13000
|
mainProfile: null,
|
|
12961
|
-
account: null,
|
|
12962
13001
|
person: null,
|
|
12963
13002
|
department: null
|
|
12964
13003
|
};
|
|
12965
|
-
this._started = false;
|
|
12966
13004
|
this._$additionalFields = new BehaviorSubject([]);
|
|
12967
13005
|
this._tokenType$ = new BehaviorSubject(undefined);
|
|
12968
13006
|
this._debug = !environment.production;
|
|
@@ -12972,41 +13010,31 @@ class AccountService extends BaseGraphqlService {
|
|
|
12972
13010
|
// Send auth token to the graphql layer, when changed
|
|
12973
13011
|
this.onAuthTokenChange.subscribe((token) => this.graphql.setAuthToken(token));
|
|
12974
13012
|
this.onAuthBasicChange.subscribe((basic) => this.graphql.setAuthBasic(basic));
|
|
12975
|
-
// Listen graphql start (or restart)
|
|
12976
|
-
this.graphql.onStart.subscribe(() => __awaiter(this, void 0, void 0, function* () {
|
|
12977
|
-
if (!this._started) {
|
|
12978
|
-
this.ready();
|
|
12979
|
-
}
|
|
12980
|
-
if (this._started && this.isLogin()) {
|
|
12981
|
-
console.debug('[account] Restarting, to retry to authenticate...');
|
|
12982
|
-
this.restart();
|
|
12983
|
-
}
|
|
12984
|
-
}));
|
|
12985
13013
|
// Force network to wait account service, after getting connection to the peer
|
|
12986
13014
|
this.network.on('beforeTryOnlineFinish', (online) => __awaiter(this, void 0, void 0, function* () {
|
|
12987
13015
|
// If online, wait a full restart, because it can force offline mode
|
|
12988
|
-
if (online && (!this.
|
|
12989
|
-
console.debug('[account] Force networkService.tryOnline() to wait, that account service is restarted...');
|
|
12990
|
-
|
|
12991
|
-
|
|
12992
|
-
|
|
13016
|
+
if (online && (!this.started || this.isLogin())) {
|
|
13017
|
+
console.debug('[account] Force networkService.tryOnline() to wait, that graphql and account service is restarted...');
|
|
13018
|
+
yield sleep(500); // wait graphql service to be restarted
|
|
13019
|
+
if (!this.started)
|
|
13020
|
+
yield this.ready();
|
|
12993
13021
|
}
|
|
12994
13022
|
}));
|
|
12995
13023
|
}
|
|
12996
13024
|
get account() {
|
|
12997
|
-
return this.
|
|
13025
|
+
return this._cache.loaded ? this._data : undefined;
|
|
12998
13026
|
}
|
|
12999
13027
|
get person() {
|
|
13000
|
-
if (this.
|
|
13001
|
-
this.
|
|
13028
|
+
if (this._cache.loaded && !this._cache.person) {
|
|
13029
|
+
this._cache.person = this._cache.loaded ? this._data.asPerson() : undefined;
|
|
13002
13030
|
}
|
|
13003
|
-
return this.
|
|
13031
|
+
return this._cache.person;
|
|
13004
13032
|
}
|
|
13005
13033
|
get department() {
|
|
13006
|
-
if (this.
|
|
13007
|
-
this.
|
|
13034
|
+
if (this._cache.loaded && !this._cache.department) {
|
|
13035
|
+
this._cache.department = this._cache.loaded ? this._data.asPerson().department : undefined;
|
|
13008
13036
|
}
|
|
13009
|
-
return this.
|
|
13037
|
+
return this._cache.department;
|
|
13010
13038
|
}
|
|
13011
13039
|
get tokenType() {
|
|
13012
13040
|
return this._tokenType$.value;
|
|
@@ -13016,91 +13044,73 @@ class AccountService extends BaseGraphqlService {
|
|
|
13016
13044
|
console.info('[account] Using authentication token type: ' + value);
|
|
13017
13045
|
this._tokenType$.next(value);
|
|
13018
13046
|
// Reset values
|
|
13019
|
-
this.
|
|
13047
|
+
this._cache.authToken = undefined;
|
|
13020
13048
|
this.onAuthTokenChange.next(undefined);
|
|
13021
|
-
this.
|
|
13049
|
+
this._cache.authBasic = undefined;
|
|
13022
13050
|
this.onAuthBasicChange.next(undefined);
|
|
13023
13051
|
}
|
|
13024
13052
|
}
|
|
13025
|
-
|
|
13026
|
-
if (this._startPromise)
|
|
13027
|
-
return this._startPromise;
|
|
13028
|
-
if (this._started)
|
|
13029
|
-
return Promise.resolve(this.account);
|
|
13030
|
-
// Restoring local settings
|
|
13031
|
-
this._startPromise = Promise.all([
|
|
13032
|
-
this.settings.ready(),
|
|
13033
|
-
// Wait token type to be set
|
|
13034
|
-
firstNotNilPromise(this._tokenType$)
|
|
13035
|
-
])
|
|
13036
|
-
.then(() => this.restoreLocally())
|
|
13037
|
-
.then(() => {
|
|
13038
|
-
this._started = true;
|
|
13039
|
-
this._startPromise = undefined;
|
|
13040
|
-
return this.account;
|
|
13041
|
-
});
|
|
13042
|
-
return this._startPromise;
|
|
13043
|
-
}
|
|
13044
|
-
get started() {
|
|
13045
|
-
return this._started;
|
|
13046
|
-
}
|
|
13047
|
-
stop() {
|
|
13053
|
+
ngOnStart() {
|
|
13048
13054
|
return __awaiter(this, void 0, void 0, function* () {
|
|
13049
|
-
|
|
13050
|
-
this.
|
|
13051
|
-
|
|
13052
|
-
|
|
13053
|
-
|
|
13054
|
-
|
|
13055
|
-
|
|
13056
|
-
if (
|
|
13057
|
-
|
|
13058
|
-
this.
|
|
13059
|
-
if (hadAuthToken)
|
|
13060
|
-
this.onAuthTokenChange.next(undefined);
|
|
13061
|
-
if (hadAuthBasic)
|
|
13062
|
-
this.onAuthBasicChange.next(undefined);
|
|
13055
|
+
yield Promise.all([
|
|
13056
|
+
this.settings.ready(),
|
|
13057
|
+
// Wait token type to be set
|
|
13058
|
+
firstNotNilPromise(this._tokenType$)
|
|
13059
|
+
]);
|
|
13060
|
+
// Listen graphql start (or restart)
|
|
13061
|
+
this.registerSubscription(this.graphql.onStart.subscribe(() => {
|
|
13062
|
+
if (this.started && this.isLogin()) {
|
|
13063
|
+
console.debug('[account] Restarting, to retry to authenticate...');
|
|
13064
|
+
this.restart();
|
|
13063
13065
|
}
|
|
13064
|
-
}
|
|
13066
|
+
}));
|
|
13067
|
+
yield this.restoreLocally();
|
|
13068
|
+
yield this.listenSettings();
|
|
13069
|
+
return this._data;
|
|
13065
13070
|
});
|
|
13066
13071
|
}
|
|
13067
|
-
|
|
13072
|
+
ngOnStop() {
|
|
13068
13073
|
return __awaiter(this, void 0, void 0, function* () {
|
|
13069
|
-
|
|
13070
|
-
|
|
13074
|
+
const hadAuthToken = this._cache.authToken && true;
|
|
13075
|
+
const hadAuthBasic = this._cache.authBasic && true;
|
|
13076
|
+
const hadAuth = hadAuthToken || hadAuthBasic;
|
|
13077
|
+
this.resetData();
|
|
13078
|
+
if (hadAuth) {
|
|
13079
|
+
this.onLogout.next();
|
|
13080
|
+
this.onChange.next(undefined);
|
|
13081
|
+
if (hadAuthToken)
|
|
13082
|
+
this.onAuthTokenChange.next(undefined);
|
|
13083
|
+
if (hadAuthBasic)
|
|
13084
|
+
this.onAuthBasicChange.next(undefined);
|
|
13085
|
+
}
|
|
13071
13086
|
});
|
|
13072
13087
|
}
|
|
13073
|
-
ready() {
|
|
13074
|
-
if (this._started)
|
|
13075
|
-
return Promise.resolve(this.account);
|
|
13076
|
-
return this.start();
|
|
13077
|
-
}
|
|
13078
13088
|
isLogin() {
|
|
13079
|
-
return !!(this.
|
|
13089
|
+
return !!(this._cache.pubkey && this._cache.loaded);
|
|
13080
13090
|
}
|
|
13081
13091
|
isAuth() {
|
|
13082
|
-
return !!(this.
|
|
13092
|
+
return !!(this._cache.pubkey && this._cache.keypair && this._cache.keypair.secretKey);
|
|
13083
13093
|
}
|
|
13084
13094
|
hasMinProfile(userProfile) {
|
|
13085
13095
|
// should be login, and status ENABLE or TEMPORARY
|
|
13086
|
-
if (!this._data
|
|
13087
|
-
(this._data.
|
|
13096
|
+
if (!this._data || !this._data.pubkey ||
|
|
13097
|
+
(this._data.statusId !== StatusIds.ENABLE && this._data.statusId !== StatusIds.TEMPORARY)) {
|
|
13088
13098
|
return false;
|
|
13089
13099
|
}
|
|
13090
|
-
return PersonUtils.hasUpperOrEqualsProfile(this._data.
|
|
13100
|
+
return PersonUtils.hasUpperOrEqualsProfile(this._data.profiles, userProfile);
|
|
13091
13101
|
}
|
|
13092
13102
|
hasExactProfile(label) {
|
|
13093
13103
|
// should be login, and status ENABLE or TEMPORARY
|
|
13094
|
-
if (!this._data
|
|
13095
|
-
(this._data.
|
|
13104
|
+
if (!this._data || !this._data.pubkey ||
|
|
13105
|
+
(this._data.statusId !== StatusIds.ENABLE && this._data.statusId !== StatusIds.TEMPORARY))
|
|
13096
13106
|
return false;
|
|
13097
|
-
return this._data.
|
|
13107
|
+
return this._data.profiles.some(profile => profile === label);
|
|
13098
13108
|
}
|
|
13099
13109
|
hasProfileAndIsEnable(userProfile) {
|
|
13100
13110
|
// should be login, and status ENABLE
|
|
13101
|
-
if (!this._data
|
|
13111
|
+
if (!this._data || !this._data.pubkey || this._data.statusId !== StatusIds.ENABLE)
|
|
13102
13112
|
return false;
|
|
13103
|
-
return PersonUtils.hasUpperOrEqualsProfile(this._data.
|
|
13113
|
+
return PersonUtils.hasUpperOrEqualsProfile(this._data.profiles, userProfile);
|
|
13104
13114
|
}
|
|
13105
13115
|
isAdmin() {
|
|
13106
13116
|
return this.hasProfileAndIsEnable('ADMIN');
|
|
@@ -13120,11 +13130,11 @@ class AccountService extends BaseGraphqlService {
|
|
|
13120
13130
|
}
|
|
13121
13131
|
isOnlyGuest() {
|
|
13122
13132
|
// Should be login, and status ENABLE or TEMPORARY
|
|
13123
|
-
if (!this._data
|
|
13124
|
-
(this._data.
|
|
13133
|
+
if (!this._data || !this._data.pubkey ||
|
|
13134
|
+
(this._data.statusId !== StatusIds.ENABLE && this._data.statusId !== StatusIds.TEMPORARY))
|
|
13125
13135
|
return false;
|
|
13126
13136
|
// Profile less then user
|
|
13127
|
-
return !PersonUtils.hasUpperOrEqualsProfile(this._data.
|
|
13137
|
+
return !PersonUtils.hasUpperOrEqualsProfile(this._data.profiles, 'USER');
|
|
13128
13138
|
}
|
|
13129
13139
|
canUserWriteDataForDepartment(recorderDepartment) {
|
|
13130
13140
|
if (ReferentialUtils.isEmpty(recorderDepartment)) {
|
|
@@ -13133,17 +13143,17 @@ class AccountService extends BaseGraphqlService {
|
|
|
13133
13143
|
return this.isAdmin();
|
|
13134
13144
|
}
|
|
13135
13145
|
// Should be login, and status ENABLE
|
|
13136
|
-
if (!this._data
|
|
13146
|
+
if (!this._data || !this._data.pubkey || this._data.statusId !== StatusIds.ENABLE)
|
|
13137
13147
|
return false;
|
|
13138
|
-
if (!this._data.
|
|
13148
|
+
if (!this._data.department || !this._data.department.id) {
|
|
13139
13149
|
console.warn('User account has no department ! Unable to check write right against recorderDepartment');
|
|
13140
13150
|
return false;
|
|
13141
13151
|
}
|
|
13142
13152
|
// Same recorder department: OK, user can write
|
|
13143
|
-
if (this._data.
|
|
13153
|
+
if (this._data.department.id === recorderDepartment.id)
|
|
13144
13154
|
return true;
|
|
13145
13155
|
// Else, check if supervisor (or more)
|
|
13146
|
-
return PersonUtils.hasUpperOrEqualsProfile(this._data.
|
|
13156
|
+
return PersonUtils.hasUpperOrEqualsProfile(this._data.profiles, 'SUPERVISOR');
|
|
13147
13157
|
}
|
|
13148
13158
|
register(data) {
|
|
13149
13159
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -13154,7 +13164,7 @@ class AccountService extends BaseGraphqlService {
|
|
|
13154
13164
|
throw new Error('Missing required username or password');
|
|
13155
13165
|
if (this._debug)
|
|
13156
13166
|
console.debug('[account] Register new user account...', data.account);
|
|
13157
|
-
this.
|
|
13167
|
+
this._cache.loaded = false;
|
|
13158
13168
|
const now = Date.now();
|
|
13159
13169
|
try {
|
|
13160
13170
|
const keypair = yield this.cryptoService.scryptKeypair(data.username, data.password);
|
|
@@ -13164,22 +13174,22 @@ class AccountService extends BaseGraphqlService {
|
|
|
13164
13174
|
data.account.settings.locale = this.settings.locale;
|
|
13165
13175
|
data.account.settings.latLongFormat = this.settings.latLongFormat;
|
|
13166
13176
|
data.account.department.id = data.account.department.id || this.environment.defaultDepartmentId;
|
|
13167
|
-
this.
|
|
13177
|
+
this._cache.keypair = keypair;
|
|
13168
13178
|
const account = yield this.saveRemotely(data.account, keypair);
|
|
13169
13179
|
// Default values
|
|
13170
13180
|
account.avatar = account.avatar || (this.environment.baseUrl + DEFAULT_AVATAR_IMAGE);
|
|
13171
|
-
this.
|
|
13172
|
-
this._data
|
|
13173
|
-
this.
|
|
13181
|
+
this._cache.mainProfile = PersonUtils.getMainProfile(account.profiles);
|
|
13182
|
+
this._data = account;
|
|
13183
|
+
this._cache.pubkey = account.pubkey;
|
|
13174
13184
|
// Try to auth on pod
|
|
13175
13185
|
yield this.authenticate(data);
|
|
13176
|
-
this.
|
|
13186
|
+
this._cache.loaded = true;
|
|
13177
13187
|
yield this.saveLocally();
|
|
13178
13188
|
console.debug(`[account] Account successfully registered in ${Date.now() - now}ms`);
|
|
13179
13189
|
// Emit events
|
|
13180
|
-
this.onLogin.next(this._data
|
|
13181
|
-
this.onChange.next(this._data
|
|
13182
|
-
return this._data
|
|
13190
|
+
this.onLogin.next(this._data);
|
|
13191
|
+
this.onChange.next(this._data);
|
|
13192
|
+
return this._data;
|
|
13183
13193
|
}
|
|
13184
13194
|
catch (error) {
|
|
13185
13195
|
console.error(error && error.message || error);
|
|
@@ -13195,20 +13205,20 @@ class AccountService extends BaseGraphqlService {
|
|
|
13195
13205
|
// Basic auth
|
|
13196
13206
|
if (tokenType === 'basic' || tokenType === 'basic-and-token') {
|
|
13197
13207
|
// Generate the authBasic, if used
|
|
13198
|
-
if (!this.
|
|
13208
|
+
if (!this._cache.authBasic) {
|
|
13199
13209
|
// Skip if token already provided
|
|
13200
|
-
if (!(this.
|
|
13210
|
+
if (!(this._cache.authToken && tokenType === 'basic-and-token')) {
|
|
13201
13211
|
if (!data || !data.username || !data.password)
|
|
13202
13212
|
throw new Error('Missing username and password');
|
|
13203
|
-
this.
|
|
13213
|
+
this._cache.authBasic = this.cryptoService.encodeBase64(`${data.username}:${data.password}`);
|
|
13204
13214
|
}
|
|
13205
13215
|
}
|
|
13206
|
-
this.onAuthBasicChange.next(this.
|
|
13216
|
+
this.onAuthBasicChange.next(this._cache.authBasic);
|
|
13207
13217
|
}
|
|
13208
13218
|
// Generate the authToken, if used
|
|
13209
13219
|
if (tokenType === 'token' || tokenType === 'basic-and-token') {
|
|
13210
13220
|
try {
|
|
13211
|
-
this.
|
|
13221
|
+
this._cache.authToken = yield this.authenticateAndGetToken(this._cache.authToken);
|
|
13212
13222
|
}
|
|
13213
13223
|
catch (error) {
|
|
13214
13224
|
// Never authenticate, or not ready for offline mode => exit
|
|
@@ -13219,7 +13229,7 @@ class AccountService extends BaseGraphqlService {
|
|
|
13219
13229
|
}
|
|
13220
13230
|
// Forget authBasic, to switch to authToken
|
|
13221
13231
|
if (tokenType === 'basic-and-token') {
|
|
13222
|
-
this.
|
|
13232
|
+
this._cache.authBasic = undefined;
|
|
13223
13233
|
this.onAuthBasicChange.next(undefined);
|
|
13224
13234
|
}
|
|
13225
13235
|
});
|
|
@@ -13239,18 +13249,18 @@ class AccountService extends BaseGraphqlService {
|
|
|
13239
13249
|
throw { code: ErrorCodes$2.UNKNOWN_ERROR, message: 'ERROR.SCRYPT_ERROR' };
|
|
13240
13250
|
}
|
|
13241
13251
|
// Store pubkey+keypair
|
|
13242
|
-
this.
|
|
13243
|
-
this.
|
|
13252
|
+
this._cache.pubkey = Base58.encode(keypair.publicKey);
|
|
13253
|
+
this._cache.keypair = keypair;
|
|
13244
13254
|
// Try to load previous token
|
|
13245
13255
|
let previousToken = yield this.storage.get(TOKEN_STORAGE_KEY);
|
|
13246
|
-
previousToken = previousToken && previousToken.startsWith(this.
|
|
13256
|
+
previousToken = previousToken && previousToken.startsWith(this._cache.pubkey) && previousToken || null;
|
|
13247
13257
|
// Offline mode
|
|
13248
13258
|
const offline = this.settings.hasOfflineFeature() && (this.network.offline || data.offline === true);
|
|
13249
13259
|
if (offline) {
|
|
13250
|
-
this.
|
|
13260
|
+
this._cache.authToken = previousToken;
|
|
13251
13261
|
// Make sure network if set as offline
|
|
13252
13262
|
this.network.setForceOffline(true, { showToast: false });
|
|
13253
|
-
console.info(`[account] Login [OK] {pubkey: ${this.
|
|
13263
|
+
console.info(`[account] Login [OK] {pubkey: ${this._cache.pubkey.substr(0, 8)}}, {offline: true}`);
|
|
13254
13264
|
}
|
|
13255
13265
|
// Online mode: try to auth on pod
|
|
13256
13266
|
else {
|
|
@@ -13300,14 +13310,14 @@ class AccountService extends BaseGraphqlService {
|
|
|
13300
13310
|
throw error;
|
|
13301
13311
|
}
|
|
13302
13312
|
// Emit event to observers
|
|
13303
|
-
this.onLogin.next(this._data
|
|
13304
|
-
this.onChange.next(this._data
|
|
13305
|
-
return this._data
|
|
13313
|
+
this.onLogin.next(this._data);
|
|
13314
|
+
this.onChange.next(this._data);
|
|
13315
|
+
return this._data;
|
|
13306
13316
|
});
|
|
13307
13317
|
}
|
|
13308
13318
|
refresh() {
|
|
13309
13319
|
return __awaiter(this, void 0, void 0, function* () {
|
|
13310
|
-
if (!this.
|
|
13320
|
+
if (!this._cache.pubkey)
|
|
13311
13321
|
throw new Error('User not logged');
|
|
13312
13322
|
if (this.network.offline)
|
|
13313
13323
|
throw new Error('Cannot check account in offline mode');
|
|
@@ -13315,9 +13325,9 @@ class AccountService extends BaseGraphqlService {
|
|
|
13315
13325
|
yield this.saveLocally();
|
|
13316
13326
|
console.debug('[account] Successfully reload account');
|
|
13317
13327
|
// Emit login event to subscribers
|
|
13318
|
-
this.onLogin.next(this._data
|
|
13319
|
-
this.onChange.next(this._data
|
|
13320
|
-
return this._data
|
|
13328
|
+
this.onLogin.next(this._data);
|
|
13329
|
+
this.onChange.next(this._data);
|
|
13330
|
+
return this._data;
|
|
13321
13331
|
});
|
|
13322
13332
|
}
|
|
13323
13333
|
/**
|
|
@@ -13327,29 +13337,29 @@ class AccountService extends BaseGraphqlService {
|
|
|
13327
13337
|
*/
|
|
13328
13338
|
save(account) {
|
|
13329
13339
|
return __awaiter(this, void 0, void 0, function* () {
|
|
13330
|
-
if (!this.
|
|
13340
|
+
if (!this._cache.pubkey)
|
|
13331
13341
|
return Promise.reject('User not logged');
|
|
13332
|
-
if (this.
|
|
13342
|
+
if (this._cache.pubkey !== account.pubkey)
|
|
13333
13343
|
return Promise.reject('Not user account');
|
|
13334
|
-
account = yield this.saveRemotely(account, this.
|
|
13344
|
+
account = yield this.saveRemotely(account, this._cache.keypair);
|
|
13335
13345
|
// Set defaults
|
|
13336
13346
|
account.avatar = account.avatar || (this.environment.baseUrl + DEFAULT_AVATAR_IMAGE);
|
|
13337
|
-
this.
|
|
13338
|
-
this._data
|
|
13339
|
-
this.
|
|
13347
|
+
this._cache.mainProfile = PersonUtils.getMainProfile(account.profiles);
|
|
13348
|
+
this._data = account;
|
|
13349
|
+
this._cache.loaded = true;
|
|
13340
13350
|
// Save locally (in storage)
|
|
13341
13351
|
yield this.saveLocally();
|
|
13342
13352
|
// Send event
|
|
13343
|
-
this.onLogin.next(this._data
|
|
13344
|
-
this.onChange.next(this._data
|
|
13345
|
-
return this._data
|
|
13353
|
+
this.onLogin.next(this._data);
|
|
13354
|
+
this.onChange.next(this._data);
|
|
13355
|
+
return this._data;
|
|
13346
13356
|
});
|
|
13347
13357
|
}
|
|
13348
13358
|
logout() {
|
|
13349
13359
|
return __awaiter(this, void 0, void 0, function* () {
|
|
13350
|
-
const hadAuthToken = this.
|
|
13351
|
-
const hadAuthBasic = this.
|
|
13352
|
-
const pubkey = this.
|
|
13360
|
+
const hadAuthToken = this._cache.authToken && true;
|
|
13361
|
+
const hadAuthBasic = this._cache.authBasic && true;
|
|
13362
|
+
const pubkey = this._cache && this._cache.pubkey;
|
|
13353
13363
|
this.resetData();
|
|
13354
13364
|
if (!this.settings.hasOfflineFeature()) {
|
|
13355
13365
|
// Remove all data from the local storage
|
|
@@ -13402,9 +13412,9 @@ class AccountService extends BaseGraphqlService {
|
|
|
13402
13412
|
if (offline) {
|
|
13403
13413
|
json = yield this.storage.get(ACCOUNT_STORAGE_KEY);
|
|
13404
13414
|
json = json && (typeof json === 'string') && JSON.parse(json) || json;
|
|
13405
|
-
json = json && this.
|
|
13406
|
-
if (!json && this.
|
|
13407
|
-
json = yield this.storage.get(ACCOUNT_STORAGE_KEY + '#' + this.
|
|
13415
|
+
json = json && this._cache.pubkey && (json.pubkey === this._cache.pubkey) && json || null;
|
|
13416
|
+
if (!json && this._cache.pubkey) {
|
|
13417
|
+
json = yield this.storage.get(ACCOUNT_STORAGE_KEY + '#' + this._cache.pubkey);
|
|
13408
13418
|
json = json && (typeof json === 'string') && JSON.parse(json) || json;
|
|
13409
13419
|
}
|
|
13410
13420
|
}
|
|
@@ -13499,7 +13509,7 @@ class AccountService extends BaseGraphqlService {
|
|
|
13499
13509
|
});
|
|
13500
13510
|
}
|
|
13501
13511
|
listenChanges() {
|
|
13502
|
-
if (!this.
|
|
13512
|
+
if (!this._cache.pubkey)
|
|
13503
13513
|
return Subscription.EMPTY;
|
|
13504
13514
|
const self = this;
|
|
13505
13515
|
console.debug('[account] [WS] Listening account changes');
|
|
@@ -13517,7 +13527,7 @@ class AccountService extends BaseGraphqlService {
|
|
|
13517
13527
|
var _a;
|
|
13518
13528
|
if (!data)
|
|
13519
13529
|
return;
|
|
13520
|
-
const existingUpdateDate = toDateISOString((_a = self._data
|
|
13530
|
+
const existingUpdateDate = toDateISOString((_a = self._data) === null || _a === void 0 ? void 0 : _a.updateDate);
|
|
13521
13531
|
if (existingUpdateDate !== data.updateDate) {
|
|
13522
13532
|
console.debug(`[account] [WS] Detected update on {${data.updateDate}}`);
|
|
13523
13533
|
yield self.refresh();
|
|
@@ -13544,20 +13554,6 @@ class AccountService extends BaseGraphqlService {
|
|
|
13544
13554
|
subscription.add(() => console.debug('[account] [WS] Stop listening account changes'));
|
|
13545
13555
|
return subscription;
|
|
13546
13556
|
}
|
|
13547
|
-
/**
|
|
13548
|
-
* @deprecated
|
|
13549
|
-
*/
|
|
13550
|
-
getPageSettings(pageId, propertyName) {
|
|
13551
|
-
return this.settings.getPageSettings(pageId, propertyName);
|
|
13552
|
-
}
|
|
13553
|
-
/**
|
|
13554
|
-
* @deprecated
|
|
13555
|
-
*/
|
|
13556
|
-
savePageSetting(pageId, value, propertyName) {
|
|
13557
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
13558
|
-
yield this.settings.savePageSetting(pageId, value, propertyName);
|
|
13559
|
-
});
|
|
13560
|
-
}
|
|
13561
13557
|
get additionalFields() {
|
|
13562
13558
|
return this._$additionalFields.getValue();
|
|
13563
13559
|
}
|
|
@@ -13578,21 +13574,21 @@ class AccountService extends BaseGraphqlService {
|
|
|
13578
13574
|
}
|
|
13579
13575
|
/* -- protected method -- */
|
|
13580
13576
|
resetData() {
|
|
13581
|
-
this.
|
|
13582
|
-
this.
|
|
13583
|
-
this.
|
|
13584
|
-
this.
|
|
13585
|
-
this.
|
|
13586
|
-
this.
|
|
13587
|
-
this.
|
|
13588
|
-
this.
|
|
13589
|
-
this._data
|
|
13577
|
+
this._cache.loaded = false;
|
|
13578
|
+
this._cache.keypair = null;
|
|
13579
|
+
this._cache.authToken = null;
|
|
13580
|
+
this._cache.authBasic = null;
|
|
13581
|
+
this._cache.pubkey = null;
|
|
13582
|
+
this._cache.mainProfile = null;
|
|
13583
|
+
this._cache.person = null;
|
|
13584
|
+
this._cache.department = null;
|
|
13585
|
+
this._data = new Account();
|
|
13590
13586
|
}
|
|
13591
13587
|
loadData(opts) {
|
|
13592
13588
|
return __awaiter(this, void 0, void 0, function* () {
|
|
13593
|
-
if (!this.
|
|
13589
|
+
if (!this._cache.pubkey)
|
|
13594
13590
|
throw new Error('User not logged');
|
|
13595
|
-
this.
|
|
13591
|
+
this._cache.loaded = false;
|
|
13596
13592
|
try {
|
|
13597
13593
|
const account = (yield this.load(opts)) || new Account();
|
|
13598
13594
|
// Set defaults
|
|
@@ -13600,17 +13596,25 @@ class AccountService extends BaseGraphqlService {
|
|
|
13600
13596
|
account.settings = account.settings || new UserSettings();
|
|
13601
13597
|
account.settings.locale = account.settings.locale || this.settings.locale;
|
|
13602
13598
|
account.settings.latLongFormat = account.settings.latLongFormat || this.settings.latLongFormat || 'DDMM';
|
|
13599
|
+
account.settings.content = account.settings.content || {};
|
|
13603
13600
|
// Read main profile
|
|
13604
|
-
this.
|
|
13601
|
+
this._cache.mainProfile = PersonUtils.getMainProfile(account.profiles);
|
|
13605
13602
|
// Update, instead of replace it
|
|
13606
|
-
if (this._data
|
|
13607
|
-
this._data.
|
|
13603
|
+
if (this._data) {
|
|
13604
|
+
this._data.fromObject(account);
|
|
13608
13605
|
}
|
|
13609
13606
|
else {
|
|
13610
|
-
this._data
|
|
13607
|
+
this._data = account;
|
|
13608
|
+
}
|
|
13609
|
+
this._cache.loaded = true;
|
|
13610
|
+
// Apply settings properties, found in remote settings
|
|
13611
|
+
const accountSettingsContentKeys = Object.keys(account.settings.content);
|
|
13612
|
+
if (isNotEmptyArray(accountSettingsContentKeys)) {
|
|
13613
|
+
const settings = Object.assign(Object.assign(Object.assign({}, this.settings.settings), account.settings.content), account.settings);
|
|
13614
|
+
delete settings.content; // Remove content attribute
|
|
13615
|
+
yield this.settings.apply(settings);
|
|
13611
13616
|
}
|
|
13612
|
-
this._data
|
|
13613
|
-
return this._data.account;
|
|
13617
|
+
return this._data;
|
|
13614
13618
|
}
|
|
13615
13619
|
catch (error) {
|
|
13616
13620
|
this.resetData();
|
|
@@ -13624,6 +13628,14 @@ class AccountService extends BaseGraphqlService {
|
|
|
13624
13628
|
}
|
|
13625
13629
|
});
|
|
13626
13630
|
}
|
|
13631
|
+
listenSettings() {
|
|
13632
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
13633
|
+
// When settings changed: save it remotely
|
|
13634
|
+
this.registerSubscription(this.settings.onChange
|
|
13635
|
+
.pipe(debounceTime(1000), filter(() => this.isLogin() && this.network.online))
|
|
13636
|
+
.subscribe(settings => this.saveLocalSettingsRemotely(settings)));
|
|
13637
|
+
});
|
|
13638
|
+
}
|
|
13627
13639
|
restoreLocally() {
|
|
13628
13640
|
return __awaiter(this, void 0, void 0, function* () {
|
|
13629
13641
|
// Restore from storage
|
|
@@ -13644,9 +13656,9 @@ class AccountService extends BaseGraphqlService {
|
|
|
13644
13656
|
return;
|
|
13645
13657
|
if (this._debug)
|
|
13646
13658
|
console.debug(`[account] Account restoration...`);
|
|
13647
|
-
this.
|
|
13648
|
-
this.
|
|
13649
|
-
this.
|
|
13659
|
+
this._cache.authToken = token;
|
|
13660
|
+
this._cache.pubkey = pubkey;
|
|
13661
|
+
this._cache.keypair = seckey && {
|
|
13650
13662
|
publicKey: Base58.decode(pubkey),
|
|
13651
13663
|
secretKey: Base58.decode(seckey)
|
|
13652
13664
|
} || null;
|
|
@@ -13654,7 +13666,7 @@ class AccountService extends BaseGraphqlService {
|
|
|
13654
13666
|
if (this.network.online) {
|
|
13655
13667
|
try {
|
|
13656
13668
|
yield this.authenticate();
|
|
13657
|
-
if (!this.
|
|
13669
|
+
if (!this._cache.authToken && !this._cache.authBasic)
|
|
13658
13670
|
throw new Error('Authentication failed');
|
|
13659
13671
|
}
|
|
13660
13672
|
catch (error) {
|
|
@@ -13685,14 +13697,14 @@ class AccountService extends BaseGraphqlService {
|
|
|
13685
13697
|
// Transform to entity
|
|
13686
13698
|
const account = Account.fromObject(jsonAccount);
|
|
13687
13699
|
// Update data
|
|
13688
|
-
this._data
|
|
13689
|
-
this.
|
|
13690
|
-
this.
|
|
13700
|
+
this._data = account;
|
|
13701
|
+
this._cache.mainProfile = PersonUtils.getMainProfile(account.profiles);
|
|
13702
|
+
this._cache.loaded = true;
|
|
13691
13703
|
// Emit event
|
|
13692
|
-
this.onLogin.next(this._data
|
|
13693
|
-
this.onChange.next(this._data
|
|
13704
|
+
this.onLogin.next(this._data);
|
|
13705
|
+
this.onChange.next(this._data);
|
|
13694
13706
|
if (this._debug)
|
|
13695
|
-
console.debug(`[account] Account restoration [OK] {pubkey: ${pubkey.substr(0, 8)}}, {profile: ${this.
|
|
13707
|
+
console.debug(`[account] Account restoration [OK] {pubkey: ${pubkey.substr(0, 8)}}, {profile: ${this._cache.mainProfile}}`);
|
|
13696
13708
|
return account;
|
|
13697
13709
|
});
|
|
13698
13710
|
}
|
|
@@ -13701,13 +13713,13 @@ class AccountService extends BaseGraphqlService {
|
|
|
13701
13713
|
*/
|
|
13702
13714
|
saveLocally() {
|
|
13703
13715
|
return __awaiter(this, void 0, void 0, function* () {
|
|
13704
|
-
if (!this.
|
|
13716
|
+
if (!this._cache.pubkey)
|
|
13705
13717
|
throw new Error('User not logged');
|
|
13706
13718
|
if (this._debug)
|
|
13707
|
-
console.debug(`[account] Saving account {${this.
|
|
13719
|
+
console.debug(`[account] Saving account {${this._cache.pubkey.substring(0, 6)}} in local storage...`);
|
|
13708
13720
|
// Convert account to json
|
|
13709
|
-
const json = this._data.
|
|
13710
|
-
const seckey = this.
|
|
13721
|
+
const json = this._data.asObject({ keepTypename: true });
|
|
13722
|
+
const seckey = this._cache.keypair && this._cache.keypair.secretKey && Base58.encode(this._cache.keypair.secretKey) || null;
|
|
13711
13723
|
// Convert avatar URL to dataUrl (e.g. 'data:image/png:<base64 content>')
|
|
13712
13724
|
const hasAvatarUrl = json.avatar && !json.avatar.endsWith(DEFAULT_AVATAR_IMAGE) &&
|
|
13713
13725
|
(json.avatar.startsWith('http://') || (json.avatar.startsWith('https://')));
|
|
@@ -13728,9 +13740,9 @@ class AccountService extends BaseGraphqlService {
|
|
|
13728
13740
|
}
|
|
13729
13741
|
try {
|
|
13730
13742
|
yield Promise.all([
|
|
13731
|
-
this.storage.set(PUBKEY_STORAGE_KEY, this.
|
|
13732
|
-
this.storage.set(TOKEN_STORAGE_KEY, this.
|
|
13733
|
-
this.storage.set(`${ACCOUNT_STORAGE_KEY}#${this.
|
|
13743
|
+
this.storage.set(PUBKEY_STORAGE_KEY, this._cache.pubkey),
|
|
13744
|
+
this.storage.set(TOKEN_STORAGE_KEY, this._cache.authToken),
|
|
13745
|
+
this.storage.set(`${ACCOUNT_STORAGE_KEY}#${this._cache.pubkey}`, json),
|
|
13734
13746
|
// Secret key (optional)
|
|
13735
13747
|
seckey && this.storage.set(SECKEY_STORAGE_KEY, seckey) || this.storage.remove(SECKEY_STORAGE_KEY),
|
|
13736
13748
|
// Remove old storage key
|
|
@@ -13744,6 +13756,22 @@ class AccountService extends BaseGraphqlService {
|
|
|
13744
13756
|
}
|
|
13745
13757
|
});
|
|
13746
13758
|
}
|
|
13759
|
+
saveLocalSettingsRemotely(source) {
|
|
13760
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
13761
|
+
if (!source || !this.isLogin())
|
|
13762
|
+
return; // Skip
|
|
13763
|
+
const account = this.account;
|
|
13764
|
+
// Merge local settings into account's settings
|
|
13765
|
+
const settings = UserSettings.fromObject(account.settings) || new UserSettings();
|
|
13766
|
+
const hasChanges = settings.merge(source, ['locale', 'latLongFormat', 'properties']);
|
|
13767
|
+
if (hasChanges) {
|
|
13768
|
+
if (this._debug)
|
|
13769
|
+
console.debug('[account] Save local settings remotely', settings);
|
|
13770
|
+
account.settings = settings;
|
|
13771
|
+
yield this.saveRemotely(account);
|
|
13772
|
+
}
|
|
13773
|
+
});
|
|
13774
|
+
}
|
|
13747
13775
|
/**
|
|
13748
13776
|
* Create or update an user account
|
|
13749
13777
|
*
|
|
@@ -13788,7 +13816,7 @@ class AccountService extends BaseGraphqlService {
|
|
|
13788
13816
|
}
|
|
13789
13817
|
authenticateAndGetToken(token, counter) {
|
|
13790
13818
|
return __awaiter(this, void 0, void 0, function* () {
|
|
13791
|
-
if (!this.
|
|
13819
|
+
if (!this._cache.pubkey)
|
|
13792
13820
|
throw new Error('User not logged');
|
|
13793
13821
|
if (!counter)
|
|
13794
13822
|
console.info('[account] Authentication on pod...');
|
|
@@ -13814,7 +13842,7 @@ class AccountService extends BaseGraphqlService {
|
|
|
13814
13842
|
if (data && data.authenticate) {
|
|
13815
13843
|
// Store the token
|
|
13816
13844
|
this.onAuthTokenChange.next(token);
|
|
13817
|
-
console.info(`[account] Authentication on pod [OK] {pubkey: '${this.
|
|
13845
|
+
console.info(`[account] Authentication on pod [OK] {pubkey: '${this._cache.pubkey.substr(0, 8)}'}`);
|
|
13818
13846
|
return token; // return the token
|
|
13819
13847
|
}
|
|
13820
13848
|
// Continue (will retry with another challenge)
|
|
@@ -13840,8 +13868,8 @@ class AccountService extends BaseGraphqlService {
|
|
|
13840
13868
|
}
|
|
13841
13869
|
// TODO: check server pubkey as a valid certificate
|
|
13842
13870
|
// Do the challenge
|
|
13843
|
-
const signature = yield this.cryptoService.sign(data.authChallenge.challenge, this.
|
|
13844
|
-
const newToken = `${this.
|
|
13871
|
+
const signature = yield this.cryptoService.sign(data.authChallenge.challenge, this._cache.keypair);
|
|
13872
|
+
const newToken = `${this._cache.pubkey}:${data.authChallenge.challenge}|${signature}`;
|
|
13845
13873
|
// iterate with the new token
|
|
13846
13874
|
return yield this.authenticateAndGetToken(newToken, (counter || 1) + 1 /* increment */);
|
|
13847
13875
|
});
|
|
@@ -14181,73 +14209,28 @@ class ConfigService extends BaseGraphqlService {
|
|
|
14181
14209
|
this.toastController = toastController;
|
|
14182
14210
|
this.translate = translate;
|
|
14183
14211
|
this.environment = environment;
|
|
14184
|
-
this._started = false;
|
|
14185
|
-
this._subscription = new Subscription();
|
|
14186
14212
|
this.$data = new BehaviorSubject(null);
|
|
14187
14213
|
this._debug = !environment.production;
|
|
14188
14214
|
if (this._debug)
|
|
14189
14215
|
console.debug('[config] Creating service');
|
|
14190
14216
|
this._optionDefs = Object.values(Object.assign(Object.assign({}, CORE_CONFIG_OPTIONS), defaultOptionsMap));
|
|
14191
|
-
// Restart if graphql service restart
|
|
14192
|
-
this.graphql.onStart.subscribe(() => this.restart());
|
|
14193
|
-
// Start
|
|
14194
|
-
if (this.graphql.started) {
|
|
14195
|
-
this.start();
|
|
14196
|
-
}
|
|
14197
|
-
}
|
|
14198
|
-
get started() {
|
|
14199
|
-
return this._started;
|
|
14200
14217
|
}
|
|
14201
14218
|
get config() {
|
|
14202
14219
|
// If first call: start loading
|
|
14203
|
-
if (!this.
|
|
14220
|
+
if (!this.started)
|
|
14204
14221
|
this.start();
|
|
14205
14222
|
return this.$data.pipe(filter(isNotNil));
|
|
14206
14223
|
}
|
|
14207
|
-
|
|
14208
|
-
|
|
14209
|
-
|
|
14210
|
-
|
|
14211
|
-
return;
|
|
14212
|
-
console.info('[config] Starting configuration...');
|
|
14213
|
-
this._startPromise = this.graphql.ready()
|
|
14214
|
-
.then(() => this.loadOrRestoreLocally())
|
|
14215
|
-
.then((data) => {
|
|
14216
|
-
this._started = true;
|
|
14217
|
-
this._startPromise = undefined;
|
|
14224
|
+
ngOnStart() {
|
|
14225
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
14226
|
+
console.info('[config] Starting configuration...');
|
|
14227
|
+
const data = yield this.loadOrRestoreLocally();
|
|
14218
14228
|
this.$data.next(data);
|
|
14229
|
+
// Restart if graphql service restart
|
|
14230
|
+
this.registerSubscription(this.graphql.onStart.subscribe(() => this.restart()));
|
|
14219
14231
|
return data;
|
|
14220
|
-
})
|
|
14221
|
-
.catch((err) => {
|
|
14222
|
-
console.error(err && err.message || err, err);
|
|
14223
|
-
this._started = false;
|
|
14224
|
-
this._startPromise = undefined;
|
|
14225
|
-
return null;
|
|
14226
|
-
});
|
|
14227
|
-
return this._startPromise;
|
|
14228
|
-
}
|
|
14229
|
-
stop() {
|
|
14230
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
14231
|
-
this._subscription.unsubscribe();
|
|
14232
|
-
this._subscription = new Subscription();
|
|
14233
|
-
this._started = false;
|
|
14234
|
-
this._startPromise = undefined;
|
|
14235
|
-
});
|
|
14236
|
-
}
|
|
14237
|
-
restart() {
|
|
14238
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
14239
|
-
if (this.started)
|
|
14240
|
-
yield this.stop();
|
|
14241
|
-
return this.start();
|
|
14242
14232
|
});
|
|
14243
14233
|
}
|
|
14244
|
-
ready() {
|
|
14245
|
-
if (this._started)
|
|
14246
|
-
return Promise.resolve(this.$data.value);
|
|
14247
|
-
if (this._startPromise)
|
|
14248
|
-
return this._startPromise;
|
|
14249
|
-
return this.start();
|
|
14250
|
-
}
|
|
14251
14234
|
loadDefault(opts) {
|
|
14252
14235
|
return __awaiter(this, void 0, void 0, function* () {
|
|
14253
14236
|
const now = Date.now();
|
|
@@ -19755,12 +19738,6 @@ class BaseEntityService extends BaseGraphqlService {
|
|
|
19755
19738
|
this.defaultSortBy = options.defaultSortBy || 'id';
|
|
19756
19739
|
this.defaultSortDirection = options.defaultSortDirection || 'asc';
|
|
19757
19740
|
this.watchQueriesUpdatePolicy = options.watchQueriesUpdatePolicy || 'update-cache';
|
|
19758
|
-
platform.ready().then(() => {
|
|
19759
|
-
// No limit for updatable watch queries, if desktop
|
|
19760
|
-
if (!platform.mobile) {
|
|
19761
|
-
this._mutableWatchQueriesMaxCount = -1;
|
|
19762
|
-
}
|
|
19763
|
-
});
|
|
19764
19741
|
const obj = new dataType();
|
|
19765
19742
|
this._typename = obj.__typename || 'UnknownVO';
|
|
19766
19743
|
this._logTypeName = removeEnd(this._typename, 'VO');
|
|
@@ -20138,6 +20115,14 @@ class BaseEntityService extends BaseGraphqlService {
|
|
|
20138
20115
|
return EntityFilterUtils.fromObject(source, this.filterType);
|
|
20139
20116
|
}
|
|
20140
20117
|
/* -- protected functions -- */
|
|
20118
|
+
ngOnStart() {
|
|
20119
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
20120
|
+
// No limit for updatable watch queries, if desktop
|
|
20121
|
+
if (!this.platform.mobile) {
|
|
20122
|
+
this._mutableWatchQueriesMaxCount = -1;
|
|
20123
|
+
}
|
|
20124
|
+
});
|
|
20125
|
+
}
|
|
20141
20126
|
fillDefaultProperties(source) {
|
|
20142
20127
|
// Can be override by subclasses
|
|
20143
20128
|
}
|