forked from tutao/tutanota
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.ts
441 lines (401 loc) · 15.4 KB
/
app.ts
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
import {client} from "./misc/ClientDetector"
import m, {Component, RouteDefs, RouteResolver} from "mithril"
import {lang, languageCodeToTag, languages} from "./misc/LanguageViewModel"
import {root} from "./RootView"
import {disableErrorHandlingDuringLogout, handleUncaughtError} from "./misc/ErrorHandler"
import "./gui/main-styles"
import {assertMainOrNodeBoot, bootFinished, isApp, isDesktop, isTutanotaDomain} from "./api/common/Env"
import {logins} from "./api/main/LoginController"
import type {lazy} from "@tutao/tutanota-utils"
import {downcast, neverNull} from "@tutao/tutanota-utils"
import {routeChange} from "./misc/RouteChange"
import {windowFacade} from "./misc/WindowFacade"
import {styles} from "./gui/styles"
import {deviceConfig} from "./misc/DeviceConfig"
import {Logger, replaceNativeLogger} from "./api/common/Logger"
import {init as initSW} from "./serviceworker/ServiceWorkerClient"
import {applicationPaths} from "./ApplicationPaths"
import {ProgrammingError} from "./api/common/error/ProgrammingError"
import {CurrentView} from "./gui/base/Header"
import {NativeWebauthnView} from "./login/NativeWebauthnView"
import {WebauthnNativeBridge} from "./native/main/WebauthnNativeBridge"
assertMainOrNodeBoot()
bootFinished()
const urlQueryParams = m.parseQueryString(location.search)
const platformId = urlQueryParams["platformId"]
if (isApp() || isDesktop()) {
if (
(isApp() && (platformId === "android" || platformId === "ios")) ||
(isDesktop() && (platformId === "linux" || platformId === "win32" || platformId === "darwin"))
) {
env.platformId = platformId
} else {
throw new ProgrammingError(`Invalid platform id: ${String(platformId)}`)
}
}
replaceNativeLogger(window, new Logger())
type View = Record<string, any>
let currentView: Component<unknown> | null = null
window.tutao = {
client,
m,
lang,
root,
logins,
currentView,
locator: window.tutao?.locator, // locator is not restored on hot reload otherwise
}
client.init(navigator.userAgent, navigator.platform)
if (!client.isSupported()) {
throw new Error("Unsupported")
}
// Setup exception handling after checking for client support, because in android the Error is caught by the unhandled rejection handler
// and then the "Update WebView" message will never be show
// we still want to do this ASAP so we can handle other errors
setupExceptionHandling()
// this needs to stay after client.init
windowFacade.init()
export const state: {
prefix: string | null
prefixWithoutFile: string | null
} =
// @ts-ignore
typeof module != "undefined" && module.hot && module.hot.data
// @ts-ignore
? downcast(module.hot.data.state)
: {
prefix: null,
prefixWithoutFile: null,
}
let startRoute = "/"
if (state.prefix == null) {
const prefix = (state.prefix =
location.pathname[location.pathname.length - 1] !== "/" ? location.pathname : location.pathname.substring(0, location.pathname.length - 1))
state.prefixWithoutFile = prefix.includes(".") ? prefix.substring(0, prefix.lastIndexOf("/")) : prefix
let redirectTo = urlQueryParams["r"] // redirection triggered by the server (e.g. the user reloads /mail/id by pressing F5)
if (redirectTo) {
delete urlQueryParams["r"]
if (typeof redirectTo !== "string") {
redirectTo = ""
}
} else {
redirectTo = ""
}
let newQueryString = m.buildQueryString(urlQueryParams)
if (newQueryString.length > 0) {
newQueryString = "?" + newQueryString
}
let target = redirectTo + newQueryString + location.hash
if (target === "" || target[0] !== "/") target = "/" + target
history.replaceState(null, "", neverNull(state.prefix) + target)
startRoute = target
}
// Write it here for the WorkerClient so that it can load relative worker easily. Should do it here so that it doesn't break after HMR.
window.tutao.appState = state
let origin = location.origin
if (location.origin.indexOf("localhost") !== -1) {
origin += "/client/build/index"
}
if (!isDesktop() && typeof navigator.registerProtocolHandler === "function") {
try {
// @ts-ignore third argument removed from spec, but use is still recommended
navigator.registerProtocolHandler("mailto", origin + "/mailto#url=%s", "Tutanota")
} catch (e) {
// Catch SecurityError's and some other cases when we are not allowed to register a handler
console.log("Failed to register a mailto: protocol handler ", e)
}
}
import("./translations/en")
.then(en => lang.init(en.default))
.then(async () => {
// do this after lang initialized
const {locator} = await import("./api/main/MainLocator")
await locator.init()
if (client.isIE()) {
import("./gui/base/NotificationOverlay.js").then(module =>
module.show(
{
view: () => m("", lang.get("unsupportedBrowserOverlay_msg")),
},
{
label: "close_alt",
},
[],
),
)
} else if (isDesktop()) {
import("./native/main/UpdatePrompt.js").then(({registerForUpdates}) => registerForUpdates(locator.native))
}
const userLanguage = deviceConfig.getLanguage() && languages.find(l => l.code === deviceConfig.getLanguage())
if (userLanguage) {
const language = {
code: userLanguage.code,
languageTag: languageCodeToTag(userLanguage.code),
}
lang.setLanguage(language).catch(e => {
console.error("Failed to fetch translation: " + userLanguage.code, e)
})
if (isApp() || isDesktop()) {
locator.systemApp.changeSystemLanguage(language)
}
}
function createViewResolver(getView: lazy<Promise<CurrentView>>, requireLogin: boolean = true, doNotCache: boolean = false): RouteResolver {
let cache: {view: CurrentView | null} = {view: null}
return {
onmatch: async (args, requestedPath) => {
if (requireLogin && !logins.isUserLoggedIn()) {
forceLogin(args, requestedPath)
} else if (!requireLogin && logins.isUserLoggedIn()) {
await disableErrorHandlingDuringLogout()
return logins.logout(false).then(() => {
windowFacade.reload(args)
})
} else {
let promise: Promise<CurrentView>
if (cache.view == null) {
promise = getView().then(view => {
if (!doNotCache) {
cache.view = view
}
return view
})
} else {
promise = Promise.resolve(cache.view)
}
Promise.all([promise, import("./gui/base/Header")]).then(([view, {header}]) => {
view.updateUrl(args, requestedPath)
const currentPath = m.route.get()
routeChange({
args,
requestedPath,
currentPath,
})
header.updateCurrentView(view)
window.tutao.currentView = view
})
return promise
}
},
render: vnode => {
return m(root, vnode)
},
}
}
const loginListener = await import("./login/LoginListener")
await loginListener.registerLoginListener(
locator.credentialsProvider,
locator.secondFactorHandler,
)
styles.init()
const {usingKeychainAuthentication} = await import("./misc/credentials/CredentialsProviderFactory")
/**
* Migrate credentials on supported devices to be encrypted using an intermediate key secured by the device keychain (biometrics).
* This code can (and will) be removed once all users have migrated.
*/
if (usingKeychainAuthentication()) {
// We can only determine platform after we establish native bridge
const hasAlreadyMigrated = deviceConfig.getCredentialsEncryptionKey() != null
const hasCredentials = deviceConfig.loadAll().length > 0
if (!hasAlreadyMigrated && hasCredentials) {
const migrationModule = await import("./misc/credentials/CredentialsMigration")
await locator.native.init()
const migration = new migrationModule.CredentialsMigration(deviceConfig, locator.deviceEncryptionFacade, locator.native)
await migration.migrateCredentials()
// Reload the app just to make sure we are in the right state and don't init nativeApp twice
windowFacade.reload({})
return
}
}
const paths = applicationPaths({
login: createViewResolver(
async () => {
const {LoginView} = await import("./login/LoginView.js")
const {LoginViewModel} = await import("./login/LoginViewModel.js")
const {locator} = await import("./api/main/MainLocator")
const {DatabaseKeyFactory} = await import("./misc/credentials/DatabaseKeyFactory")
const loginViewModel = new LoginViewModel(logins, locator.credentialsProvider, locator.secondFactorHandler, new DatabaseKeyFactory(locator.deviceEncryptionFacade))
await loginViewModel.init()
return new LoginView(loginViewModel, "/mail")
},
false,
true,
),
contact: createViewResolver(() => import("./contacts/view/ContactView.js").then(module => new module.ContactView())),
externalLogin: createViewResolver(() => import("./login/ExternalLoginView.js").then(module => new module.ExternalLoginView()), false),
mail: createViewResolver(() => import("./mail/view/MailView.js").then(module => new module.MailView())),
settings: createViewResolver(() => import("./settings/SettingsView.js").then(module => new module.SettingsView())),
search: createViewResolver(() => import("./search/view/SearchView.js").then(module => new module.SearchView())),
contactForm: createViewResolver(() => import("./login/contactform/ContactFormView.js").then(module => module.contactFormView), false),
calendar: createViewResolver(() => import("./calendar/view/CalendarView.js").then(module => new module.CalendarView()), true),
/**
* The following resolvers are programmed by hand instead of using createViewResolver() in order to be able to properly redirect
* to the login page without having to deal with a ton of conditional logic in the LoginViewModel and to avoid some of the default
* behaviour of resolvers created with createViewResolver(), e.g. caching.
*/
signup: {
async onmatch() {
const {showSignupDialog} = await import("./misc/LoginUtils")
// We have to manually parse it because mithril does not put hash into args of onmatch
const hashParams = m.parseQueryString(location.hash.substring(1))
showSignupDialog(hashParams)
m.route.set("/login", {
noAutoLogin: true,
})
return null
},
},
giftcard: {
async onmatch() {
const {showGiftCardDialog} = await import("./misc/LoginUtils")
showGiftCardDialog(location.hash)
m.route.set("/login", {
noAutoLogin: true,
})
return null
},
},
recover: {
async onmatch(args: any) {
const {showRecoverDialog} = await import("./misc/LoginUtils")
const resetAction = args.resetAction === "password" || args.resetAction === "secondFactor" ? args.resetAction : "password"
const mailAddress = typeof args.mailAddress === "string" ? args.mailAddress : ""
showRecoverDialog(mailAddress, resetAction)
m.route.set("/login", {
noAutoLogin: true,
})
return null
},
},
webauthn: createViewResolver(
async () => {
const {BrowserWebauthn} = await import("./misc/2fa/webauthn/BrowserWebauthn.js")
const {NativeWebauthnView} = await import("./login/NativeWebauthnView.js")
const {WebauthnNativeBridge} = await import("./native/main/WebauthnNativeBridge.js")
const creds = navigator.credentials
return new NativeWebauthnView(new BrowserWebauthn(creds, window.location.hostname), new WebauthnNativeBridge())
},
false,
false
)
})
// see https://github.com/MithrilJS/mithril.js/issues/2659
m.route.prefix = neverNull(state.prefix).replace(/(?:%[a-f89][a-f0-9])+/gim, decodeURIComponent)
// keep in sync with RewriteAppResourceUrlHandler.java
const resolvers: RouteDefs = {
"/": {
onmatch: (args, requestedPath) => forceLogin(args, requestedPath),
},
}
for (let path in paths) {
resolvers[path] = paths[path]
}
// append catch all at the end because mithril will stop at the first match
resolvers["/:path"] = {
onmatch: (): Promise<Component> => {
return Promise.all([import("./gui/base/InfoView"), import("./gui/base/ButtonN")]).then(([{InfoView}, {
ButtonType,
ButtonN
}]) => {
return {
view() {
return m(
root,
m(
new InfoView(
() => "404",
() => [
m("p", lang.get("notFound404_msg")),
m(ButtonN, {
label: "back_action",
click: () => window.history.back(),
type: ButtonType.Primary,
}),
],
),
),
)
},
}
})
},
}
// keep in sync with RewriteAppResourceUrlHandler.java
m.route(document.body, startRoute, resolvers)
// We need to initialize native once we start the mithril routing, specifically for the case of mailto handling in android
// If native starts telling the web side to navigate too early, mithril won't be ready and the requests will be lost
if (isApp() || isDesktop()) {
await locator.native.init()
}
import("./gui/InfoMessageHandler.js").then(module => module.registerInfoMessageHandler())
// after we set up prefixWithoutFile
initSW()
})
function forceLogin(args: Record<string, Dict>, requestedPath: string) {
if (requestedPath.indexOf("#mail") !== -1) {
m.route.set(`/ext${location.hash}`)
} else if (requestedPath.startsWith("/#")) {
// we do not allow any other hashes except "#mail". this prevents login loops.
m.route.set("/login")
} else {
let pathWithoutParameter = requestedPath.indexOf("?") > 0 ? requestedPath.substring(0, requestedPath.indexOf("?")) : requestedPath
if (pathWithoutParameter.trim() === "/") {
let newQueryString = m.buildQueryString(args)
m.route.set(`/login` + (newQueryString.length > 0 ? "?" + newQueryString : ""))
} else {
m.route.set(`/login?requestedPath=${encodeURIComponent(requestedPath)}`)
}
}
}
function setupExceptionHandling() {
window.addEventListener("error", function (evt) {
// evt.error is not always set, e.g. not for "content.js:1963 Uncaught DOMException: Failed to read the 'selectionStart' property from 'HTMLInputElement': The input element's type ('email') does not support selection."
if (evt.error) {
handleUncaughtError(evt.error)
evt.preventDefault()
}
})
// Handle unhandled native JS Promise rejections
window.addEventListener("unhandledrejection", function (evt) {
handleUncaughtError(evt.reason)
evt.preventDefault()
})
}
env.dist &&
isTutanotaDomain() &&
setTimeout(() => {
console.log(`
''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''',:,''''''''''''
''''''''''''';:llllcccccccc,'''''''''''' Do you care about privacy?
'''''''''''':kXWXkoc::;,,'''''''''''''''
'''''''''''',cdk0KKK00kxdolc;,'''''''''' Work at Tutanota! Fight for our rights!
'''''''''''''''';coxOKNMMWWNK0kdl:,'''''
'''''''''''''''''''',;oKMMMMMMMMWX0dc,'' https://tutanota.com/jobs
'''''''''''''''''''''';kWMMMMMMMMMMWXk:'
'''''''''''''''''''',:xXMMMMMMMMMMMMMWKl
''''''''''''''''';lk0KWMMMMMMMMMMMMMMMWK
''''''''''''';cdOKWMMMMMMMMMMMMMMMMMMMMM
'''''''',:ldOKNWMMMMMMMMMMMMMMMMMMMMMMMM
''',:ldk0XWMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
ldk0XWMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
WWMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
`)
}, 5000)
// @ts-ignore see above
const hot = typeof module !== "undefined" && module.hot
if (hot) {
// Save the state (mostly prefix) before the reload
hot.dispose((data: any) => {
data.state = state
})
// Import ourselves again to actually replace ourselves and all the dependencies
hot.accept(() => {
console.log("Requiring new app.js")
require(module.id)
})
}