2025-03-18 17:27:59 +01:00
|
|
|
import { LoggedInEvent, LoginController, PostLoginAction } from "../api/main/LoginController.js"
|
2024-07-01 17:56:41 +02:00
|
|
|
import { CalendarModel } from "../../calendar-app/calendar/model/CalendarModel.js"
|
2022-12-27 15:37:40 +01:00
|
|
|
import { CalendarEventTypeRef } from "../api/entities/tutanota/TypeRefs.js"
|
|
|
|
|
import { CUSTOM_MIN_ID } from "../api/common/utils/EntityUtils.js"
|
|
|
|
|
import { EntityClient } from "../api/common/EntityClient.js"
|
|
|
|
|
import { ProgressTracker } from "../api/main/ProgressTracker.js"
|
|
|
|
|
import { promiseMap } from "@tutao/tutanota-utils"
|
|
|
|
|
import { NoopProgressMonitor } from "../api/common/utils/ProgressMonitor.js"
|
|
|
|
|
import { SessionType } from "../api/common/SessionType.js"
|
|
|
|
|
import { ExposedCacheStorage } from "../api/worker/rest/DefaultEntityRestCache.js"
|
2025-03-18 17:27:59 +01:00
|
|
|
import { OfflineStorageSettingsModel } from "./OfflineStorageSettingsModel"
|
2022-05-24 18:35:24 +02:00
|
|
|
|
2023-07-24 14:44:42 +02:00
|
|
|
export class CachePostLoginAction implements PostLoginAction {
|
2022-05-24 18:35:24 +02:00
|
|
|
constructor(
|
|
|
|
|
private readonly calendarModel: CalendarModel,
|
|
|
|
|
private readonly entityClient: EntityClient,
|
|
|
|
|
private readonly progressTracker: ProgressTracker,
|
2022-11-28 17:38:17 +01:00
|
|
|
private readonly cacheStorage: ExposedCacheStorage,
|
2022-05-24 18:35:24 +02:00
|
|
|
private readonly logins: LoginController,
|
2025-03-18 17:27:59 +01:00
|
|
|
private readonly offlineStorageSettings: OfflineStorageSettingsModel | null, // null if no cleaning e.g in calendar
|
2022-12-27 15:37:40 +01:00
|
|
|
) {}
|
2022-05-24 18:35:24 +02:00
|
|
|
|
|
|
|
|
async onFullLoginSuccess(loggedInEvent: LoggedInEvent): Promise<void> {
|
2022-07-04 14:55:17 +02:00
|
|
|
// we use an ephemeral cache for non-persistent sessions which doesn't
|
|
|
|
|
// support or save calendar events, so it's pointless to preload them.
|
|
|
|
|
if (loggedInEvent.sessionType !== SessionType.Persistent) return
|
2023-12-07 17:19:18 +01:00
|
|
|
//
|
2022-07-04 14:55:17 +02:00
|
|
|
// 3 work to load calendar info, 2 work to load short and long events
|
2022-05-30 14:16:58 +02:00
|
|
|
const workPerCalendar = 3 + 2
|
|
|
|
|
const totalWork = this.logins.getUserController().getCalendarMemberships().length * workPerCalendar
|
2022-12-28 15:28:28 +01:00
|
|
|
const monitorHandle = await this.progressTracker.registerMonitor(totalWork)
|
2022-05-30 14:16:58 +02:00
|
|
|
const progressMonitor = this.progressTracker.getMonitor(monitorHandle) ?? new NoopProgressMonitor()
|
2024-01-04 11:21:05 +01:00
|
|
|
const calendarInfos = await this.calendarModel.getCalendarInfos()
|
2022-05-30 14:16:58 +02:00
|
|
|
|
2022-12-27 15:37:40 +01:00
|
|
|
await promiseMap(calendarInfos.values(), async ({ groupRoot }) => {
|
2022-05-30 14:16:58 +02:00
|
|
|
await Promise.all([
|
|
|
|
|
this.entityClient.loadAll(CalendarEventTypeRef, groupRoot.longEvents, CUSTOM_MIN_ID).then(() => progressMonitor.workDone(1)),
|
2022-12-27 15:37:40 +01:00
|
|
|
this.entityClient.loadAll(CalendarEventTypeRef, groupRoot.shortEvents, CUSTOM_MIN_ID).then(() => progressMonitor.workDone(1)),
|
2022-05-30 14:16:58 +02:00
|
|
|
])
|
|
|
|
|
})
|
|
|
|
|
progressMonitor.completed()
|
2025-06-18 15:33:00 +02:00
|
|
|
}
|
|
|
|
|
|
2025-10-14 12:11:22 +02:00
|
|
|
async onPartialLoginSuccess(event: LoggedInEvent): Promise<{ asyncAction: Promise<void> }> {
|
2025-06-18 15:33:00 +02:00
|
|
|
if (event.sessionType === SessionType.Persistent && this.offlineStorageSettings != null) {
|
2025-03-18 17:27:59 +01:00
|
|
|
await this.offlineStorageSettings.init()
|
2022-11-28 17:38:17 +01:00
|
|
|
|
2025-03-18 17:27:59 +01:00
|
|
|
// Clear the excluded data (i.e. trash and spam lists, old data) in the offline storage.
|
|
|
|
|
await this.cacheStorage.clearExcludedData(this.offlineStorageSettings.getTimeRange())
|
|
|
|
|
}
|
2025-10-14 12:11:22 +02:00
|
|
|
return { asyncAction: Promise.resolve() }
|
2022-05-24 18:35:24 +02:00
|
|
|
}
|
2022-12-27 15:37:40 +01:00
|
|
|
}
|