-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathworker.js
More file actions
323 lines (290 loc) · 9.29 KB
/
worker.js
File metadata and controls
323 lines (290 loc) · 9.29 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
import { defaultSession, locByLanguage } from "./actions";
import countriesList from "./components/LeadForm/PhoneInput/countriesList";
const dictionaryOf = require("./utils/dictionaries/pages.json");
const GOOGLE_KEY = "AIzaSyB6NEbEyhDU_U1z_XoyRwEu0Rc1XXeZK6c";
const getFirstBrowserLanguage = (navigator) => {
var nav = JSON.parse(navigator),
browserLanguagePropertyKeys = [
"language",
"browserLanguage",
"systemLanguage",
"userLanguage",
],
i,
language,
len,
shortLanguage = null;
// support for HTML 5.1 "navigator.languages"
if (Array.isArray(nav.languages)) {
for (i = 0; i < nav.languages.length; i++) {
language = nav.languages[i];
len = language.length;
if (!shortLanguage && len) {
shortLanguage = language;
}
if (language && len > 2) {
return language;
}
}
}
// support for other well known properties in browsers
for (i = 0; i < browserLanguagePropertyKeys.length; i++) {
language = nav[browserLanguagePropertyKeys[i]];
//skip this loop iteration if property is null/undefined. IE11 fix.
if (language == null) {
continue;
}
len = language.length;
if (!shortLanguage && len) {
shortLanguage = language;
}
if (language && len > 2) {
return language;
}
}
return shortLanguage;
};
function distance(lat1, lon1, lat2, lon2, unit) {
if (lat1 == lat2 && lon1 == lon2) {
return 0;
} else {
var radlat1 = (Math.PI * lat1) / 180;
var radlat2 = (Math.PI * lat2) / 180;
var theta = lon1 - lon2;
var radtheta = (Math.PI * theta) / 180;
var dist =
Math.sin(radlat1) * Math.sin(radlat2) +
Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
if (dist > 1) {
dist = 1;
}
dist = Math.acos(dist);
dist = (dist * 180) / Math.PI;
dist = dist * 60 * 1.1515;
if (unit == "K") {
dist = dist * 1.609344;
}
if (unit == "N") {
dist = dist * 0.8684;
}
return dist;
}
}
const getClosestLoc = (locations, lat, lon) => {
let lowerDistance = 10000000;
let tempLocation = 0;
let location = null;
for (var i = 0; i < locations.length; i++) {
// ignore unlisted locations on the ymls
if (locations[i].meta_info.visibility === "unlisted") continue;
tempLocation = distance(
locations[i].latitude,
locations[i].longitude,
lat,
lon
);
if (tempLocation <= lowerDistance) {
lowerDistance = tempLocation;
location = locations[i];
}
}
return location;
};
const getRegion = (shortName, regions) => {
const country = countriesList.filter((c) =>
c.includes(shortName.toLowerCase())
);
const latamValues = ["central-america", "south-america", "carribean"];
if (country[0][1].some((i) => latamValues.includes(i))) return "latam";
else if (country[0][1].includes("america")) return "usa-canada";
const region = regions.filter((reg) => country[0][1].includes(reg));
return region.length === 1 ? region[0] : null;
};
const initSession = async (
locationsArray,
blockListArray,
storedSession,
path,
seed = {}
) => {
console.log("Initializing session");
var v4 = null;
var latitude = null;
var longitude = null;
var langDestination = null;
let geoCode = null;
var pathsDictionary = {
...dictionaryOf.yml[0],
...dictionaryOf.md[0],
};
// session.pathDictionary[`${window.location?.pathname}`]
// langDestination = pathsDictionary
// const params = new URLSearchParams(window.location.pathname);
let { location, language, navigator, ...utm } = seed;
const browserLang = getFirstBrowserLanguage(navigator);
if (!language) {
if (storedSession) language = storedSession.language;
else language = browserLang.substring(0, 2);
}
if (language != "es") language = "us";
//cleanup the locations array and add all the data I need for locations
let languageToFilter = language || "us";
const locations = locByLanguage(locationsArray, languageToFilter);
// remove undefineds from the seed utm's to avoid overriding the originals with undefined
Object.keys(utm).forEach((key) => utm[key] === undefined && delete utm[key]);
if (location) {
location = locations.find((l) => l.breathecode_location_slug === location);
if (!location) location = null;
console.log("Hardcoded location", location);
} else if (storedSession && storedSession.location != null) {
location = locations.find(
(l) =>
l.breathecode_location_slug ===
storedSession.location.breathecode_location_slug
);
latitude = location.latitude;
longitude = location.longitude;
}
if (location === null && !path.includes("/landings")) {
console.log("Calculating nearest location because it was null...");
try {
const response = await fetch(
`https://www.googleapis.com/geolocation/v1/geolocate?key=${GOOGLE_KEY}`,
{
headers: {
"Content-Type": "application/json",
},
method: "POST",
}
);
let data = (await response.json()) || null;
if (data && data.location) {
const lang = {
us: "en",
es: "es",
};
const responseGC = await fetch(
`https://maps.googleapis.com/maps/api/geocode/json?latlng=${data.location.lat},${data.location.lng}&key=${GOOGLE_KEY}&language=${lang[languageToFilter]}`,
{
method: "POST",
}
);
let filteredLocations = [];
let dataGC = (await responseGC.json()) || null;
if (dataGC.results) {
geoCode = {};
// get country and city from geocoding
dataGC.results[0].address_components.map((comp) => {
if (comp.types.includes("locality")) geoCode.city = comp.long_name;
if (comp.types.includes("country")) {
geoCode.country = comp.long_name;
geoCode.countryShort = comp.short_name;
}
});
// Check if we have locations in user's country
const locsInCountry = locations.filter(
(location) =>
location.country_shortname === geoCode.countryShort.toLowerCase()
);
if (locsInCountry.length !== 0) {
const campus = locsInCountry.find(
(location) => location.city === geoCode.city
);
if (campus) location = campus;
else filteredLocations = locsInCountry;
} else {
// If there are no locations in the user's country, we get the locations from its region
const regions = [
...new Set(locations.map((item) => item.meta_info.region)),
];
const region = getRegion(geoCode.countryShort, regions);
if (region)
filteredLocations = locations.filter(
(location) => location.meta_info.region === region
);
}
}
if (!location) {
latitude = data.location.lat;
longitude = data.location.lng;
// Filtered locations are used to filter from the locations in the user's country or region
location = getClosestLoc(
filteredLocations.length !== 0 ? filteredLocations : locations,
data.location.lat,
data.location.lng
);
}
} else throw Error("Error when connecting to Google Geolocation API");
} catch (e) {
console.log("Error retrieving IP information: ", e);
}
}
// get the language
if (location) {
location.reliable = true;
} else {
location = locations.find(
(l) => l.breathecode_location_slug == "downtown-miami"
);
console.log(
"Location could not be loaded, using miami as default location",
location
);
if (location) {
location.reliable = false;
}
}
const defaultLang = path.split("/").filter((l) => l !== "")[0] || "us";
if (!language) language = location?.defaultLanguage || defaultLang;
//construct the academy alias dictionary
let academyAlias = await fetch(
`https://breathecode.herokuapp.com/v1/marketing/alias?academy=2,4,5,6,7,8,9,10,11`,
{
method: "GET",
}
);
academyAlias = await academyAlias.json();
const academyAliasDictionary = {};
academyAlias.map((alias) => {
const key = alias.slug;
academyAliasDictionary[key] = alias.academy.slug;
});
const blockList = blockListArray.find((element) => element !== null);
const _session = {
...defaultSession,
...storedSession,
v4,
location,
country: geoCode && geoCode.country,
city: geoCode && geoCode.city,
browserLang,
language,
latitude,
longitude,
academyAliasDictionary,
pathsDictionary,
blockList: blockList,
// marketing utm info
utm: { ...storedSession.utm, ...utm },
locations: locations
.filter((l) => {
// filter inlisted locations
if (l.meta_info.visibility === "unlisted") return false;
return true;
})
.sort((a, b) => (a.meta_info.position > b.meta_info.position ? 1 : -1)),
};
return _session;
};
self.onmessage = async (message) => {
const { locationsArray, blockListArray, storedSession, seed, path } =
message.data;
const _session = await initSession(
locationsArray,
blockListArray,
storedSession,
path,
seed
);
self.postMessage(_session);
};