2017-08-15 13:54:22 +02:00
|
|
|
// @flow
|
|
|
|
|
import {remove} from "../common/utils/ArrayUtils"
|
|
|
|
|
import {assertMainOrNode} from "../Env"
|
2017-12-05 18:41:38 +01:00
|
|
|
import type {LoginController} from "./LoginController"
|
2018-10-19 18:13:58 +02:00
|
|
|
import type {OperationTypeEnum} from "../common/TutanotaConstants"
|
2020-03-16 17:37:50 +01:00
|
|
|
import {PhishingMarkerStatus} from "../common/TutanotaConstants"
|
2018-10-24 16:39:21 +02:00
|
|
|
import {isSameTypeRefByAttr} from "../common/EntityFunctions"
|
2019-01-21 10:48:07 +01:00
|
|
|
import stream from "mithril/stream/stream.js"
|
2019-02-05 17:26:36 +01:00
|
|
|
import {downcast, identity} from "../common/utils/Utils"
|
2017-08-15 13:54:22 +02:00
|
|
|
|
|
|
|
|
assertMainOrNode()
|
|
|
|
|
|
2018-10-19 18:13:58 +02:00
|
|
|
export type EntityUpdateData = {
|
|
|
|
|
application: string,
|
|
|
|
|
type: string,
|
|
|
|
|
instanceListId: string,
|
|
|
|
|
instanceId: string,
|
|
|
|
|
operation: OperationTypeEnum
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-20 18:31:07 +02:00
|
|
|
export type EntityEventsListener = ($ReadOnlyArray<EntityUpdateData>, eventOwnerGroupId: Id) => mixed;
|
2018-10-19 18:13:58 +02:00
|
|
|
|
2018-10-24 16:39:21 +02:00
|
|
|
export const isUpdateForTypeRef = <T>(typeRef: TypeRef<T>, update: EntityUpdateData): boolean => isSameTypeRefByAttr(typeRef, update.application, update.type)
|
2018-10-19 18:13:58 +02:00
|
|
|
|
2019-01-21 10:48:07 +01:00
|
|
|
export class EventController {
|
|
|
|
|
_countersStream: Stream<WebsocketCounterData>;
|
|
|
|
|
_entityListeners: Array<EntityEventsListener>;
|
2018-02-08 13:36:05 +01:00
|
|
|
_logins: LoginController;
|
2020-03-16 17:37:50 +01:00
|
|
|
_phishingMarkers: Set<string>;
|
2017-08-15 13:54:22 +02:00
|
|
|
|
2017-12-05 18:41:38 +01:00
|
|
|
constructor(logins: LoginController) {
|
2020-03-16 17:37:50 +01:00
|
|
|
this._logins = logins
|
2019-01-21 10:48:07 +01:00
|
|
|
this._countersStream = stream()
|
|
|
|
|
this._entityListeners = []
|
2020-03-16 17:37:50 +01:00
|
|
|
this._phishingMarkers = new Set()
|
2017-08-15 13:54:22 +02:00
|
|
|
}
|
|
|
|
|
|
2019-01-21 10:48:07 +01:00
|
|
|
addEntityListener(listener: EntityEventsListener) {
|
|
|
|
|
this._entityListeners.push(listener)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
removeEntityListener(listener: EntityEventsListener) {
|
|
|
|
|
remove(this._entityListeners, listener)
|
2017-08-15 13:54:22 +02:00
|
|
|
}
|
|
|
|
|
|
2019-01-21 10:48:07 +01:00
|
|
|
countersStream(): Stream<WebsocketCounterData> {
|
|
|
|
|
// Create copy so it's never ended
|
|
|
|
|
return this._countersStream.map(identity)
|
2017-08-15 13:54:22 +02:00
|
|
|
}
|
|
|
|
|
|
2020-03-16 17:37:50 +01:00
|
|
|
phishingMarkers(): Set<string> {
|
|
|
|
|
return this._phishingMarkers
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-20 18:31:07 +02:00
|
|
|
notificationReceived(entityUpdates: $ReadOnlyArray<EntityUpdate>, eventOwnerGroupId: Id) {
|
2018-10-22 10:27:26 +02:00
|
|
|
let loginsUpdates = Promise.resolve()
|
2018-02-08 13:36:05 +01:00
|
|
|
if (this._logins.isUserLoggedIn()) {
|
|
|
|
|
// the UserController must be notified first as other event receivers depend on it to be up-to-date
|
2019-06-20 18:31:07 +02:00
|
|
|
loginsUpdates = this._logins.getUserController().entityEventsReceived(entityUpdates, eventOwnerGroupId)
|
2018-02-08 13:36:05 +01:00
|
|
|
}
|
2018-10-19 18:13:58 +02:00
|
|
|
|
2018-10-22 10:27:26 +02:00
|
|
|
loginsUpdates.then(() => {
|
2019-01-21 10:48:07 +01:00
|
|
|
this._entityListeners.forEach(listener => {
|
2019-02-05 17:26:36 +01:00
|
|
|
let entityUpdatesData: Array<EntityUpdateData> = downcast(entityUpdates)
|
2019-06-20 18:31:07 +02:00
|
|
|
listener(entityUpdatesData, eventOwnerGroupId)
|
2018-10-22 10:27:26 +02:00
|
|
|
})
|
2017-08-15 13:54:22 +02:00
|
|
|
})
|
|
|
|
|
}
|
2019-01-21 10:48:07 +01:00
|
|
|
|
|
|
|
|
counterUpdateReceived(update: WebsocketCounterData) {
|
|
|
|
|
this._countersStream(update)
|
|
|
|
|
}
|
2020-03-16 17:37:50 +01:00
|
|
|
|
|
|
|
|
phishingMarkersUpdateReceived(update: PhishingMarkerWebsocketData) {
|
|
|
|
|
update.markers.forEach((marker) => {
|
|
|
|
|
if (marker.status === PhishingMarkerStatus.INACTIVE) {
|
|
|
|
|
this._phishingMarkers.delete(marker.marker)
|
|
|
|
|
} else {
|
|
|
|
|
this._phishingMarkers.add(marker.marker)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
2019-03-21 15:58:26 +01:00
|
|
|
}
|