2022-09-27 00:26:10 +02:00
|
|
|
import {
|
|
|
|
HttpTransportType,
|
|
|
|
HubConnection,
|
|
|
|
HubConnectionBuilder,
|
|
|
|
IHubProtocol,
|
|
|
|
} from "@microsoft/signalr";
|
|
|
|
import { MessagePackHubProtocol } from "@microsoft/signalr-protocol-msgpack";
|
2024-03-21 17:09:44 +01:00
|
|
|
import { firstValueFrom } from "rxjs";
|
2022-09-27 00:26:10 +02:00
|
|
|
|
2024-02-05 20:26:41 +01:00
|
|
|
import { LoginStrategyServiceAbstraction } from "../../../../auth/src/common/abstractions/login-strategy.service";
|
2022-09-27 00:26:10 +02:00
|
|
|
import {
|
|
|
|
AuthRequestPushNotification,
|
|
|
|
NotificationResponse,
|
2023-11-27 21:59:44 +01:00
|
|
|
} from "../../models/response/notification.response";
|
|
|
|
import { EnvironmentService } from "../../platform/abstractions/environment.service";
|
|
|
|
import { LogService } from "../../platform/abstractions/log.service";
|
|
|
|
import { AnonymousHubService as AnonymousHubServiceAbstraction } from "../abstractions/anonymous-hub.service";
|
2022-09-27 00:26:10 +02:00
|
|
|
|
|
|
|
export class AnonymousHubService implements AnonymousHubServiceAbstraction {
|
|
|
|
private anonHubConnection: HubConnection;
|
|
|
|
private url: string;
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
private environmentService: EnvironmentService,
|
2024-02-05 20:26:41 +01:00
|
|
|
private loginStrategyService: LoginStrategyServiceAbstraction,
|
2022-09-27 00:26:10 +02:00
|
|
|
private logService: LogService,
|
|
|
|
) {}
|
|
|
|
|
|
|
|
async createHubConnection(token: string) {
|
2024-03-21 17:09:44 +01:00
|
|
|
this.url = (await firstValueFrom(this.environmentService.environment$)).getNotificationsUrl();
|
2022-09-27 00:26:10 +02:00
|
|
|
|
|
|
|
this.anonHubConnection = new HubConnectionBuilder()
|
2022-09-29 20:10:35 +02:00
|
|
|
.withUrl(this.url + "/anonymous-hub?Token=" + token, {
|
2022-09-27 00:26:10 +02:00
|
|
|
skipNegotiation: true,
|
|
|
|
transport: HttpTransportType.WebSockets,
|
|
|
|
})
|
|
|
|
.withHubProtocol(new MessagePackHubProtocol() as IHubProtocol)
|
|
|
|
.build();
|
|
|
|
|
|
|
|
this.anonHubConnection.start().catch((error) => this.logService.error(error));
|
|
|
|
|
|
|
|
this.anonHubConnection.on("AuthRequestResponseRecieved", (data: any) => {
|
2024-02-02 21:13:37 +01:00
|
|
|
// FIXME: Verify that this floating promise is intentional. If it is, add an explanatory comment and ensure there is proper error handling.
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
2022-09-27 00:26:10 +02:00
|
|
|
this.ProcessNotification(new NotificationResponse(data));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
stopHubConnection() {
|
|
|
|
if (this.anonHubConnection) {
|
2024-02-02 21:13:37 +01:00
|
|
|
// FIXME: Verify that this floating promise is intentional. If it is, add an explanatory comment and ensure there is proper error handling.
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
2022-09-27 00:26:10 +02:00
|
|
|
this.anonHubConnection.stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async ProcessNotification(notification: NotificationResponse) {
|
2024-03-12 19:19:50 +01:00
|
|
|
await this.loginStrategyService.sendAuthRequestPushNotification(
|
2022-09-27 00:26:10 +02:00
|
|
|
notification.payload as AuthRequestPushNotification,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|