2022-05-24 18:35:24 +02:00
|
|
|
import {IPostLoginAction, LoggedInEvent, LoginController} from "../api/main/LoginController.js"
|
|
|
|
|
import {CalendarModel} from "../calendar/model/CalendarModel.js"
|
|
|
|
|
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"
|
2022-05-30 14:16:58 +02:00
|
|
|
import {promiseMap} from "@tutao/tutanota-utils"
|
|
|
|
|
import {NoopProgressMonitor} from "../api/common/utils/ProgressMonitor.js"
|
2022-07-04 14:55:17 +02:00
|
|
|
import {SessionType} from "../api/common/SessionType.js"
|
2022-05-24 18:35:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
export class CachePostLoginAction implements IPostLoginAction {
|
|
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
|
private readonly calendarModel: CalendarModel,
|
|
|
|
|
private readonly entityClient: EntityClient,
|
|
|
|
|
private readonly progressTracker: ProgressTracker,
|
|
|
|
|
private readonly logins: LoginController,
|
|
|
|
|
) {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
// 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
|
|
|
|
|
const monitorHandle = this.progressTracker.registerMonitor(totalWork)
|
|
|
|
|
const progressMonitor = this.progressTracker.getMonitor(monitorHandle) ?? new NoopProgressMonitor()
|
|
|
|
|
const calendarInfos = await this.calendarModel.loadCalendarInfos(progressMonitor)
|
|
|
|
|
|
|
|
|
|
await promiseMap(calendarInfos.values(), async ({groupRoot}) => {
|
|
|
|
|
await Promise.all([
|
|
|
|
|
this.entityClient.loadAll(CalendarEventTypeRef, groupRoot.longEvents, CUSTOM_MIN_ID).then(() => progressMonitor.workDone(1)),
|
|
|
|
|
this.entityClient.loadAll(CalendarEventTypeRef, groupRoot.shortEvents, CUSTOM_MIN_ID).then(() => progressMonitor.workDone(1))
|
|
|
|
|
])
|
|
|
|
|
})
|
|
|
|
|
progressMonitor.completed()
|
2022-05-24 18:35:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async onPartialLoginSuccess(loggedInEvent: LoggedInEvent): Promise<void> {
|
|
|
|
|
return Promise.resolve()
|
|
|
|
|
}
|
|
|
|
|
}
|