incyclist-services 1.9.25 → 1.9.26
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/lib/cjs/api/rest/RestApiClient.js +5 -2
- package/lib/cjs/api/rest/api.js +25 -4
- package/lib/cjs/apps/base/api/base.js +2 -2
- package/lib/cjs/apps/base/api/strava/api.js +81 -3
- package/lib/cjs/apps/base/api/strava/consts.js +35 -0
- package/lib/cjs/apps/base/api/types.js +2 -0
- package/lib/cjs/apps/base/app/index.js +3 -0
- package/lib/cjs/apps/strava/StravaAppConnection.js +2 -2
- package/lib/cjs/ride/route/RLVDisplayService.js +2 -2
- package/lib/cjs/routes/base/parsers/gpx.js +5 -0
- package/lib/cjs/routes/list/service.js +1 -1
- package/lib/cjs/routes/sync/base/provider.js +217 -0
- package/lib/cjs/routes/sync/index.js +1 -0
- package/lib/cjs/routes/sync/komoot/provider.js +143 -335
- package/lib/cjs/routes/sync/strava/provider.js +128 -0
- package/lib/esm/api/rest/RestApiClient.js +5 -2
- package/lib/esm/api/rest/api.js +25 -4
- package/lib/esm/apps/base/api/base.js +2 -2
- package/lib/esm/apps/base/api/strava/api.js +81 -3
- package/lib/esm/apps/base/app/index.js +3 -0
- package/lib/esm/apps/strava/StravaAppConnection.js +2 -2
- package/lib/esm/ride/route/RLVDisplayService.js +2 -2
- package/lib/esm/routes/base/parsers/gpx.js +5 -0
- package/lib/esm/routes/list/service.js +1 -1
- package/lib/esm/routes/sync/index.js +1 -0
- package/lib/esm/routes/sync/komoot/provider.js +143 -336
- package/lib/types/api/rest/RestApiClient.d.ts +1 -1
- package/lib/types/api/rest/api.d.ts +5 -0
- package/lib/types/apps/base/api/base.d.ts +1 -1
- package/lib/types/apps/base/api/intervals/api.d.ts +2 -1
- package/lib/types/apps/base/api/komoot/api.d.ts +2 -1
- package/lib/types/apps/base/api/strava/api.d.ts +15 -8
- package/lib/types/apps/base/api/strava/types.d.ts +101 -0
- package/lib/types/apps/base/app/index.d.ts +1 -0
- package/lib/types/apps/base/types.d.ts +1 -0
- package/lib/types/apps/types.d.ts +1 -0
- package/lib/types/routes/base/parsers/gpx.d.ts +1 -0
- package/lib/types/routes/sync/index.d.ts +1 -0
- package/lib/types/routes/sync/komoot/provider.d.ts +11 -37
- package/lib/types/routes/sync/types.d.ts +5 -0
- package/package.json +1 -1
|
@@ -4,8 +4,11 @@ exports.RestApiClient = void 0;
|
|
|
4
4
|
const api_1 = require("./api");
|
|
5
5
|
class RestApiClient {
|
|
6
6
|
static _api;
|
|
7
|
-
static getClient() {
|
|
8
|
-
|
|
7
|
+
static getClient(enableLogging) {
|
|
8
|
+
const api = this.api();
|
|
9
|
+
if (enableLogging)
|
|
10
|
+
api.enableLogging();
|
|
11
|
+
return api.client();
|
|
9
12
|
}
|
|
10
13
|
static api() {
|
|
11
14
|
if (!this._api)
|
package/lib/cjs/api/rest/api.js
CHANGED
|
@@ -11,6 +11,12 @@ class ApiClient {
|
|
|
11
11
|
axios;
|
|
12
12
|
logger;
|
|
13
13
|
requests;
|
|
14
|
+
requestInterceptorId;
|
|
15
|
+
responseInterceptorId;
|
|
16
|
+
constructor() {
|
|
17
|
+
this.logger = new gd_eventlog_1.EventLogger('API');
|
|
18
|
+
this.requests = [];
|
|
19
|
+
}
|
|
14
20
|
init(props) {
|
|
15
21
|
const { requestLog = true, version, appVersion, channel = 'desktop', platform } = props;
|
|
16
22
|
const headers = {
|
|
@@ -29,10 +35,25 @@ class ApiClient {
|
|
|
29
35
|
}
|
|
30
36
|
this.axios = axios_1.default.create({ headers });
|
|
31
37
|
if (requestLog) {
|
|
32
|
-
this.
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
38
|
+
this.enableLogging();
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
enableLogging() {
|
|
42
|
+
const client = this.client();
|
|
43
|
+
if (this.requestInterceptorId !== undefined)
|
|
44
|
+
return;
|
|
45
|
+
this.requestInterceptorId = client.interceptors.request.use((req) => this.onRequest(req));
|
|
46
|
+
this.responseInterceptorId = client.interceptors.response.use((res) => this.onResponse(res));
|
|
47
|
+
}
|
|
48
|
+
disableLogging() {
|
|
49
|
+
const client = this.client();
|
|
50
|
+
if (this.requestInterceptorId !== undefined) {
|
|
51
|
+
client.interceptors.request.eject(this.requestInterceptorId);
|
|
52
|
+
this.requestInterceptorId = undefined;
|
|
53
|
+
}
|
|
54
|
+
if (this.responseInterceptorId !== undefined) {
|
|
55
|
+
client.interceptors.response.eject(this.responseInterceptorId);
|
|
56
|
+
this.responseInterceptorId = undefined;
|
|
36
57
|
}
|
|
37
58
|
}
|
|
38
59
|
client() {
|
|
@@ -45,9 +45,9 @@ class AppApiBase {
|
|
|
45
45
|
const form = await fp.createForm({ url: this.getBaseUrl() + url, ...requestOpts }, uploadInfo);
|
|
46
46
|
return form;
|
|
47
47
|
}
|
|
48
|
-
getApi() {
|
|
48
|
+
getApi(enableLogging = false) {
|
|
49
49
|
if (!this.api) {
|
|
50
|
-
this.api = api_1.RestApiClient.getClient();
|
|
50
|
+
this.api = api_1.RestApiClient.getClient(enableLogging);
|
|
51
51
|
return this.api;
|
|
52
52
|
}
|
|
53
53
|
return this.api;
|
|
@@ -7,6 +7,7 @@ const base_1 = require("../base");
|
|
|
7
7
|
const types_1 = require("./types");
|
|
8
8
|
const gd_eventlog_1 = require("gd-eventlog");
|
|
9
9
|
const sleep_1 = require("../../../../utils/sleep");
|
|
10
|
+
const consts_1 = require("./consts");
|
|
10
11
|
const API_BASE_URL = 'https://www.strava.com/api/v3';
|
|
11
12
|
const OAUTH_URL = 'https://www.strava.com/oauth';
|
|
12
13
|
const AUTH_SERVER_URL = 'https://auth.incyclist.com';
|
|
@@ -21,6 +22,7 @@ class StravaApi extends base_1.AppApiBase {
|
|
|
21
22
|
}
|
|
22
23
|
init(config) {
|
|
23
24
|
this.config = config;
|
|
25
|
+
this.observer = this.observer ?? new observer_1.Observer();
|
|
24
26
|
return this.observer;
|
|
25
27
|
}
|
|
26
28
|
update(config) {
|
|
@@ -46,6 +48,16 @@ class StravaApi extends base_1.AppApiBase {
|
|
|
46
48
|
const response = await this.createUpload(request);
|
|
47
49
|
return await this.waitForUploadResponse(response);
|
|
48
50
|
}
|
|
51
|
+
async getLoggedInAthlete() {
|
|
52
|
+
await this.verifyAuthentication();
|
|
53
|
+
try {
|
|
54
|
+
const response = await this.get('/athlete');
|
|
55
|
+
return response.data;
|
|
56
|
+
}
|
|
57
|
+
catch (err) {
|
|
58
|
+
return { props: {}, error: 'getLoggedInAthlete failed: ' + err.message };
|
|
59
|
+
}
|
|
60
|
+
}
|
|
49
61
|
async getLoggedInAthleteActivities(props = {}) {
|
|
50
62
|
await this.verifyAuthentication();
|
|
51
63
|
try {
|
|
@@ -57,6 +69,31 @@ class StravaApi extends base_1.AppApiBase {
|
|
|
57
69
|
return { props, error: 'getLoggedInAthleteActivities failed: ' + err.message };
|
|
58
70
|
}
|
|
59
71
|
}
|
|
72
|
+
async getLoggedInAthleteRoutes(props = {}, filters) {
|
|
73
|
+
await this.verifyAuthentication();
|
|
74
|
+
try {
|
|
75
|
+
const hasParams = Object.keys(props).length > 0;
|
|
76
|
+
const params = Object.keys(props).map(k => `${k}=${props[k]}`).join('&');
|
|
77
|
+
const url = `/athlete/routes${hasParams ? '?' + params : ''}`;
|
|
78
|
+
const response = await this.get(url);
|
|
79
|
+
const all = response.data;
|
|
80
|
+
this.logger.logEvent({ message: 'getLoggedInAthleteRoutes success', routes: all.length });
|
|
81
|
+
return this.applyFilter(all, filters);
|
|
82
|
+
}
|
|
83
|
+
catch (err) {
|
|
84
|
+
return { props, error: 'getLoggedInAthleteRoutes failed: ' + err.message };
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
async getRouteAsGPX(id) {
|
|
88
|
+
await this.verifyAuthentication();
|
|
89
|
+
try {
|
|
90
|
+
const response = await this.get(`/routes/${id}/export_gpx`);
|
|
91
|
+
return response.data;
|
|
92
|
+
}
|
|
93
|
+
catch (err) {
|
|
94
|
+
return { props: { id }, error: 'getRouteAsGPX failed: ' + err.message };
|
|
95
|
+
}
|
|
96
|
+
}
|
|
60
97
|
async getActivityById(id, include_all_efforts = false) {
|
|
61
98
|
await this.verifyAuthentication();
|
|
62
99
|
try {
|
|
@@ -78,9 +115,19 @@ class StravaApi extends base_1.AppApiBase {
|
|
|
78
115
|
return { id, error: 'getActivityStream failed: ' + err.message };
|
|
79
116
|
}
|
|
80
117
|
}
|
|
81
|
-
async
|
|
82
|
-
|
|
83
|
-
|
|
118
|
+
async getRoutesByAthleteId(id, props = {}, filters) {
|
|
119
|
+
await this.verifyAuthentication();
|
|
120
|
+
try {
|
|
121
|
+
const hasParams = Object.keys(props).length > 0;
|
|
122
|
+
const params = Object.keys(props).map(k => `${k}=${props[k]}`).join('&');
|
|
123
|
+
const url = `/athletes/${id}/routes${hasParams ? '?' + params : ''}`;
|
|
124
|
+
const response = await this.get(url);
|
|
125
|
+
const all = response.data;
|
|
126
|
+
return this.applyFilter(all, filters);
|
|
127
|
+
}
|
|
128
|
+
catch (err) {
|
|
129
|
+
return { props, error: 'getRoutesByAthleteId failed: ' + err.message };
|
|
130
|
+
}
|
|
84
131
|
}
|
|
85
132
|
async verifyToken() {
|
|
86
133
|
if (!this.isTokenStillValid()) {
|
|
@@ -186,6 +233,34 @@ class StravaApi extends base_1.AppApiBase {
|
|
|
186
233
|
this.logger.logEvent({ message: 'Strava refresh Token result', status: 'error', error: errorStr });
|
|
187
234
|
}
|
|
188
235
|
}
|
|
236
|
+
applyFilter(routes, filters) {
|
|
237
|
+
if (!filters)
|
|
238
|
+
return routes;
|
|
239
|
+
const sportTypeFilter = this.getSportTypeFilter(filters?.sport);
|
|
240
|
+
return routes.filter(route => {
|
|
241
|
+
let valid = true;
|
|
242
|
+
if (sportTypeFilter.length > 0)
|
|
243
|
+
valid = valid && (sportTypeFilter.includes(route.type));
|
|
244
|
+
if (filters?.after)
|
|
245
|
+
valid = valid && (new Date(route.created_at).valueOf() > filters.after.valueOf());
|
|
246
|
+
if (filters?.lastUpdateAfter)
|
|
247
|
+
valid = valid && (new Date(route.updated_at).valueOf() > filters.lastUpdateAfter.valueOf());
|
|
248
|
+
return valid;
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
getSportTypeFilter(filterStr) {
|
|
252
|
+
if (!filterStr)
|
|
253
|
+
return [];
|
|
254
|
+
const sportTypeFilter = filterStr?.split(',') ?? [];
|
|
255
|
+
const transformed = [];
|
|
256
|
+
for (const sport of sportTypeFilter) {
|
|
257
|
+
const stravaType = consts_1.StringToStravaRouteType[sport];
|
|
258
|
+
if (stravaType) {
|
|
259
|
+
transformed.push(stravaType);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
return transformed;
|
|
263
|
+
}
|
|
189
264
|
async get(url, config) {
|
|
190
265
|
await this.verifyToken();
|
|
191
266
|
const props = config ?? {};
|
|
@@ -224,5 +299,8 @@ class StravaApi extends base_1.AppApiBase {
|
|
|
224
299
|
return API_BASE_URL;
|
|
225
300
|
}
|
|
226
301
|
}
|
|
302
|
+
getApi() {
|
|
303
|
+
return super.getApi(true);
|
|
304
|
+
}
|
|
227
305
|
}
|
|
228
306
|
exports.StravaApi = StravaApi;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.StringToStravaRouteType = exports.StravaRouteTypeToString = exports.StringToStravaRouteSubType = exports.StravaRouteSubTypeToString = void 0;
|
|
4
|
+
exports.StravaRouteSubTypeToString = {
|
|
5
|
+
1: 'racebike',
|
|
6
|
+
2: 'mtb',
|
|
7
|
+
3: 'cross',
|
|
8
|
+
4: 'trail',
|
|
9
|
+
5: 'mixed'
|
|
10
|
+
};
|
|
11
|
+
exports.StringToStravaRouteSubType = {
|
|
12
|
+
racebike: 1,
|
|
13
|
+
mtb: 2,
|
|
14
|
+
cross: 3,
|
|
15
|
+
trail: 4,
|
|
16
|
+
mixed: 5
|
|
17
|
+
};
|
|
18
|
+
exports.StravaRouteTypeToString = {
|
|
19
|
+
1: 'ride',
|
|
20
|
+
2: 'run',
|
|
21
|
+
3: 'walk',
|
|
22
|
+
4: 'hike',
|
|
23
|
+
5: 'trailrun',
|
|
24
|
+
6: 'gravel',
|
|
25
|
+
7: 'mtb'
|
|
26
|
+
};
|
|
27
|
+
exports.StringToStravaRouteType = {
|
|
28
|
+
ride: 1,
|
|
29
|
+
run: 2,
|
|
30
|
+
walk: 3,
|
|
31
|
+
hike: 4,
|
|
32
|
+
trailrun: 5,
|
|
33
|
+
gravel: 6,
|
|
34
|
+
mtb: 7
|
|
35
|
+
};
|
|
@@ -120,10 +120,10 @@ let StravaAppConnection = (() => {
|
|
|
120
120
|
return this.api;
|
|
121
121
|
}
|
|
122
122
|
getSupportedOperations() {
|
|
123
|
-
return ['ActivityUpload'];
|
|
123
|
+
return ['ActivityUpload', 'RouteDownload'];
|
|
124
124
|
}
|
|
125
125
|
getDefaultOperations() {
|
|
126
|
-
return ['ActivityUpload'];
|
|
126
|
+
return ['ActivityUpload', 'RouteDownload'];
|
|
127
127
|
}
|
|
128
128
|
initAuth() {
|
|
129
129
|
let isInitialized;
|
|
@@ -515,8 +515,8 @@ let RLVDisplayService = (() => {
|
|
|
515
515
|
return error.message;
|
|
516
516
|
}
|
|
517
517
|
getVideoUrl(video) {
|
|
518
|
-
return video?.route?.
|
|
519
|
-
?? video?.route?.
|
|
518
|
+
return video?.route?.description?.videoUrl
|
|
519
|
+
?? video?.route?.details?.video?.url
|
|
520
520
|
?? video?.route?.details?.video?.file;
|
|
521
521
|
}
|
|
522
522
|
getVideoTime(routeDistance, video = this.currentVideo) {
|
|
@@ -36,6 +36,11 @@ class GPXParser extends xml_2.XMLParser {
|
|
|
36
36
|
supportsExtension(extension) {
|
|
37
37
|
return extension.toLowerCase() === 'gpx';
|
|
38
38
|
}
|
|
39
|
+
async parseFromString(gpx) {
|
|
40
|
+
const xmljson = await (0, xml_1.parseXml)(gpx);
|
|
41
|
+
xmljson.expectScheme('gpx');
|
|
42
|
+
return await this.parse({}, xmljson);
|
|
43
|
+
}
|
|
39
44
|
async loadDescription(context) {
|
|
40
45
|
const data = context.data;
|
|
41
46
|
const metadata = data['metadata'] ?? {};
|
|
@@ -780,7 +780,7 @@ let RouteListService = (() => {
|
|
|
780
780
|
if (source === 'user') {
|
|
781
781
|
this.logEvent({ message: 'route added', route: route.description.title, source: route.description.source });
|
|
782
782
|
}
|
|
783
|
-
if (!route.description.country) {
|
|
783
|
+
if (!route.description.country && route.points?.length > 0) {
|
|
784
784
|
this.logEvent({ message: 'route update required (country)', route: route.title });
|
|
785
785
|
route.updateCountryFromPoints()
|
|
786
786
|
.then((updated) => {
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
|
|
3
|
+
var useValue = arguments.length > 2;
|
|
4
|
+
for (var i = 0; i < initializers.length; i++) {
|
|
5
|
+
value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
|
|
6
|
+
}
|
|
7
|
+
return useValue ? value : void 0;
|
|
8
|
+
};
|
|
9
|
+
var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
|
|
10
|
+
function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
|
|
11
|
+
var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
|
|
12
|
+
var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
|
|
13
|
+
var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
|
|
14
|
+
var _, done = false;
|
|
15
|
+
for (var i = decorators.length - 1; i >= 0; i--) {
|
|
16
|
+
var context = {};
|
|
17
|
+
for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
|
|
18
|
+
for (var p in contextIn.access) context.access[p] = contextIn.access[p];
|
|
19
|
+
context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
|
|
20
|
+
var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
|
|
21
|
+
if (kind === "accessor") {
|
|
22
|
+
if (result === void 0) continue;
|
|
23
|
+
if (result === null || typeof result !== "object") throw new TypeError("Object expected");
|
|
24
|
+
if (_ = accept(result.get)) descriptor.get = _;
|
|
25
|
+
if (_ = accept(result.set)) descriptor.set = _;
|
|
26
|
+
if (_ = accept(result.init)) initializers.unshift(_);
|
|
27
|
+
}
|
|
28
|
+
else if (_ = accept(result)) {
|
|
29
|
+
if (kind === "field") initializers.unshift(_);
|
|
30
|
+
else descriptor[key] = _;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
if (target) Object.defineProperty(target, contextIn.name, descriptor);
|
|
34
|
+
done = true;
|
|
35
|
+
};
|
|
36
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
37
|
+
exports.BaseSyncProvider = void 0;
|
|
38
|
+
const apps_1 = require("../../../apps");
|
|
39
|
+
const decorators_1 = require("../../../base/decorators");
|
|
40
|
+
const service_1 = require("../../../base/service");
|
|
41
|
+
const types_1 = require("../../../base/types");
|
|
42
|
+
const settings_1 = require("../../../settings");
|
|
43
|
+
const utils_1 = require("../../../utils");
|
|
44
|
+
const route_1 = require("../../base/model/route");
|
|
45
|
+
const factory_1 = require("../factory");
|
|
46
|
+
let BaseSyncProvider = (() => {
|
|
47
|
+
let _classSuper = service_1.IncyclistService;
|
|
48
|
+
let _instanceExtraInitializers = [];
|
|
49
|
+
let _getUserSettings_decorators;
|
|
50
|
+
let _getRouteListService_decorators;
|
|
51
|
+
let _getRouteSyncFactory_decorators;
|
|
52
|
+
let _getAppsService_decorators;
|
|
53
|
+
return class BaseSyncProvider extends _classSuper {
|
|
54
|
+
static {
|
|
55
|
+
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
|
|
56
|
+
_getUserSettings_decorators = [decorators_1.Injectable];
|
|
57
|
+
_getRouteListService_decorators = [decorators_1.Injectable];
|
|
58
|
+
_getRouteSyncFactory_decorators = [decorators_1.Injectable];
|
|
59
|
+
_getAppsService_decorators = [decorators_1.Injectable];
|
|
60
|
+
__esDecorate(this, null, _getUserSettings_decorators, { kind: "method", name: "getUserSettings", static: false, private: false, access: { has: obj => "getUserSettings" in obj, get: obj => obj.getUserSettings }, metadata: _metadata }, null, _instanceExtraInitializers);
|
|
61
|
+
__esDecorate(this, null, _getRouteListService_decorators, { kind: "method", name: "getRouteListService", static: false, private: false, access: { has: obj => "getRouteListService" in obj, get: obj => obj.getRouteListService }, metadata: _metadata }, null, _instanceExtraInitializers);
|
|
62
|
+
__esDecorate(this, null, _getRouteSyncFactory_decorators, { kind: "method", name: "getRouteSyncFactory", static: false, private: false, access: { has: obj => "getRouteSyncFactory" in obj, get: obj => obj.getRouteSyncFactory }, metadata: _metadata }, null, _instanceExtraInitializers);
|
|
63
|
+
__esDecorate(this, null, _getAppsService_decorators, { kind: "method", name: "getAppsService", static: false, private: false, access: { has: obj => "getAppsService" in obj, get: obj => obj.getAppsService }, metadata: _metadata }, null, _instanceExtraInitializers);
|
|
64
|
+
if (_metadata) Object.defineProperty(this, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
|
65
|
+
}
|
|
66
|
+
lastSyncTS = __runInitializers(this, _instanceExtraInitializers);
|
|
67
|
+
observer;
|
|
68
|
+
stopRequested;
|
|
69
|
+
sync() {
|
|
70
|
+
if (this.observer)
|
|
71
|
+
return this.observer;
|
|
72
|
+
this.observer = new types_1.Observer();
|
|
73
|
+
this.observer.once('done', () => {
|
|
74
|
+
(0, utils_1.waitNextTick)().then(() => {
|
|
75
|
+
this.stopRequested = false;
|
|
76
|
+
delete this.observer;
|
|
77
|
+
}).catch(err => { this.logError(err, 'sync'); });
|
|
78
|
+
});
|
|
79
|
+
this.observer.once('stop', () => { this.stopSync(); });
|
|
80
|
+
this.loadTours().catch(err => { this.logError(err, 'sync'); });
|
|
81
|
+
return this.observer;
|
|
82
|
+
}
|
|
83
|
+
isConnected() {
|
|
84
|
+
if (this.getApi().isAuthenticated()) {
|
|
85
|
+
return true;
|
|
86
|
+
}
|
|
87
|
+
const connected = this.getConnection().isConnected();
|
|
88
|
+
return connected;
|
|
89
|
+
}
|
|
90
|
+
getOriginalId(route) {
|
|
91
|
+
const app = this.getConnection().getAppKey();
|
|
92
|
+
const idStr = route.description?.id;
|
|
93
|
+
if (!idStr.startsWith(`${app}:`))
|
|
94
|
+
return;
|
|
95
|
+
return idStr.split(':')[1];
|
|
96
|
+
}
|
|
97
|
+
async loadTours() {
|
|
98
|
+
const app = this.getConnection().getAppKey();
|
|
99
|
+
if (!this.getAppsService().isEnabled(app, 'RouteDownload')) {
|
|
100
|
+
await (0, utils_1.waitNextTick)();
|
|
101
|
+
this.observer.emit('done');
|
|
102
|
+
return [];
|
|
103
|
+
}
|
|
104
|
+
this.logEvent({ message: `start ${app} route sync` });
|
|
105
|
+
const { routes, done, error } = await this.loadRoutesFromApi();
|
|
106
|
+
if (error) {
|
|
107
|
+
this.logEvent({ message: `${app} route sync failed` });
|
|
108
|
+
}
|
|
109
|
+
else if (done) {
|
|
110
|
+
const { added, updated, deleted } = this.checkTours(routes);
|
|
111
|
+
this.updateLastSyncTime();
|
|
112
|
+
this.observer.emit('done');
|
|
113
|
+
this.logEvent({ message: `${app} route sync finished`, added: added?.length, updated: updated?.length, deleted: deleted.length });
|
|
114
|
+
return added;
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
this.observer.emit('done');
|
|
118
|
+
this.logEvent({ message: `${app} route sync stopped` });
|
|
119
|
+
return [];
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
checkTours(candidates) {
|
|
123
|
+
const app = this.getConnection().getAppKey();
|
|
124
|
+
const routes = this.getRouteListService()?.getAllAppRoutes(app) ?? [];
|
|
125
|
+
const matched = routes.map(r => ({ id: r.id, matched: false }));
|
|
126
|
+
const added = [];
|
|
127
|
+
const updated = [];
|
|
128
|
+
candidates.forEach(tour => {
|
|
129
|
+
const summary = this.getRouteSyncSummary(tour);
|
|
130
|
+
const id = `${app}:${summary.id}`;
|
|
131
|
+
const route = routes.find(route => route.id === id);
|
|
132
|
+
if (!route) {
|
|
133
|
+
added.push(tour);
|
|
134
|
+
}
|
|
135
|
+
else if (route.isDeleted) {
|
|
136
|
+
const matchRecord = matched.find(r => r.id === id);
|
|
137
|
+
if (matchRecord)
|
|
138
|
+
matchRecord.matched = true;
|
|
139
|
+
}
|
|
140
|
+
else {
|
|
141
|
+
const matchRecord = matched.find(r => r.id === id);
|
|
142
|
+
if (matchRecord)
|
|
143
|
+
matchRecord.matched = true;
|
|
144
|
+
const routeTS = route.tsLastChange ?? 0;
|
|
145
|
+
const tourTS = (new Date(summary.lastUpdate)).valueOf();
|
|
146
|
+
if (tourTS > routeTS) {
|
|
147
|
+
const description = this.buildRouteInfo(tour);
|
|
148
|
+
updated.push(new route_1.Route(description));
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
const unmatched = matched.filter(r => !r.matched);
|
|
153
|
+
const deleted = unmatched.map(r => r.id);
|
|
154
|
+
this.emitUpdates(added, updated, deleted);
|
|
155
|
+
return { added, updated, deleted };
|
|
156
|
+
}
|
|
157
|
+
stopSync() {
|
|
158
|
+
this.stopRequested = true;
|
|
159
|
+
}
|
|
160
|
+
emitUpdates(added, updated, deleted) {
|
|
161
|
+
if (added.length > 0) {
|
|
162
|
+
this.emitAdded(added);
|
|
163
|
+
}
|
|
164
|
+
if (updated.length > 0) {
|
|
165
|
+
this.emitUpdated(updated);
|
|
166
|
+
}
|
|
167
|
+
if (deleted.length > 0) {
|
|
168
|
+
this.emitDeleted(deleted);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
emitAdded(tours) {
|
|
172
|
+
this.observer.emit('added', tours.map(tour => this.buildRouteInfo(tour)));
|
|
173
|
+
}
|
|
174
|
+
emitUpdated(routes) {
|
|
175
|
+
this.observer.emit('updated', routes);
|
|
176
|
+
}
|
|
177
|
+
emitDeleted(routes) {
|
|
178
|
+
this.observer.emit('deleted', routes);
|
|
179
|
+
}
|
|
180
|
+
getLastSyncTime() {
|
|
181
|
+
const lastSyncTS = this.lastSyncTS ?? 0;
|
|
182
|
+
const key = this.getSyncKey();
|
|
183
|
+
this.lastSyncTS = Math.max(lastSyncTS, this.getUserSettings().get(key, lastSyncTS));
|
|
184
|
+
return this.lastSyncTS > 0 ? new Date(this.lastSyncTS) : undefined;
|
|
185
|
+
}
|
|
186
|
+
getCreatedFilter(days = 365) {
|
|
187
|
+
return new Date(Date.now() - days * 24 * 60 * 60 * 1000);
|
|
188
|
+
}
|
|
189
|
+
updateLastSyncTime() {
|
|
190
|
+
this.lastSyncTS = Date.now();
|
|
191
|
+
this.saveLastSyncTime();
|
|
192
|
+
}
|
|
193
|
+
saveLastSyncTime() {
|
|
194
|
+
if (!this.lastSyncTS)
|
|
195
|
+
return;
|
|
196
|
+
const key = this.getSyncKey();
|
|
197
|
+
this.getUserSettings().set(key, this.lastSyncTS);
|
|
198
|
+
}
|
|
199
|
+
getSyncKey() {
|
|
200
|
+
const app = this.getConnection().getAppKey();
|
|
201
|
+
return `apps.${app}.lastSync`;
|
|
202
|
+
}
|
|
203
|
+
getUserSettings() {
|
|
204
|
+
return (0, settings_1.useUserSettings)();
|
|
205
|
+
}
|
|
206
|
+
getRouteListService() {
|
|
207
|
+
return this.getRouteSyncFactory().getRouteList();
|
|
208
|
+
}
|
|
209
|
+
getRouteSyncFactory() {
|
|
210
|
+
return new factory_1.RouteSyncFactory();
|
|
211
|
+
}
|
|
212
|
+
getAppsService() {
|
|
213
|
+
return (0, apps_1.useAppsService)();
|
|
214
|
+
}
|
|
215
|
+
};
|
|
216
|
+
})();
|
|
217
|
+
exports.BaseSyncProvider = BaseSyncProvider;
|
|
@@ -16,3 +16,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./factory"), exports);
|
|
18
18
|
__exportStar(require("./komoot/provider"), exports);
|
|
19
|
+
__exportStar(require("./strava/provider"), exports);
|