2020-12-12 19:46:02 +00:00
|
|
|
import { gettext } from "django";
|
2021-03-21 16:36:51 +00:00
|
|
|
import { LitElement, html, customElement, TemplateResult, property, CSSResult, css } from "lit-element";
|
2020-12-12 19:46:02 +00:00
|
|
|
import "./Message";
|
|
|
|
import { APIMessage } from "./Message";
|
2021-03-17 12:23:33 +00:00
|
|
|
import PFAlertGroup from "@patternfly/patternfly/components/AlertGroup/alert-group.css";
|
|
|
|
import PFBase from "@patternfly/patternfly/patternfly-base.css";
|
2020-10-16 13:26:51 +00:00
|
|
|
|
2020-12-12 19:46:02 +00:00
|
|
|
export function showMessage(message: APIMessage): void {
|
|
|
|
const container = document.querySelector<MessageContainer>("ak-message-container");
|
|
|
|
if (!container) {
|
|
|
|
throw new Error("failed to find message container");
|
|
|
|
}
|
2021-03-12 11:41:17 +00:00
|
|
|
container.addMessage(message);
|
2020-12-12 19:46:02 +00:00
|
|
|
container.requestUpdate();
|
2020-11-20 21:08:00 +00:00
|
|
|
}
|
2020-10-16 13:26:51 +00:00
|
|
|
|
2020-12-12 19:46:02 +00:00
|
|
|
@customElement("ak-message-container")
|
|
|
|
export class MessageContainer extends LitElement {
|
2020-11-20 21:08:00 +00:00
|
|
|
|
2020-12-12 19:46:02 +00:00
|
|
|
@property({attribute: false})
|
|
|
|
messages: APIMessage[] = [];
|
|
|
|
|
2020-11-26 16:23:29 +00:00
|
|
|
messageSocket?: WebSocket;
|
2020-12-01 08:15:41 +00:00
|
|
|
retryDelay = 200;
|
2020-11-26 17:04:24 +00:00
|
|
|
|
2021-03-16 16:09:52 +00:00
|
|
|
static get styles(): CSSResult[] {
|
2021-03-21 16:36:51 +00:00
|
|
|
return [PFBase, PFAlertGroup, css`
|
|
|
|
/* Fix spacing between messages */
|
|
|
|
ak-message {
|
|
|
|
display: block;
|
|
|
|
}
|
|
|
|
`];
|
2020-10-16 13:26:51 +00:00
|
|
|
}
|
|
|
|
|
2020-11-26 16:23:29 +00:00
|
|
|
constructor() {
|
|
|
|
super();
|
2020-11-26 17:36:26 +00:00
|
|
|
try {
|
|
|
|
this.connect();
|
|
|
|
} catch (error) {
|
2020-12-05 21:08:42 +00:00
|
|
|
console.warn(`authentik/messages: failed to connect to ws ${error}`);
|
2020-11-26 17:36:26 +00:00
|
|
|
}
|
2020-10-16 18:11:24 +00:00
|
|
|
}
|
|
|
|
|
2021-03-12 11:41:17 +00:00
|
|
|
// add a new message, but only if the message isn't currently shown.
|
|
|
|
addMessage(message: APIMessage): void {
|
|
|
|
const matchingMessages = this.messages.filter(m => m.message == message.message);
|
|
|
|
if (matchingMessages.length < 1) {
|
|
|
|
this.messages.push(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-01 16:27:19 +00:00
|
|
|
connect(): void {
|
2021-03-08 10:14:00 +00:00
|
|
|
if (navigator.webdriver) return;
|
2020-11-26 16:23:29 +00:00
|
|
|
const wsUrl = `${window.location.protocol.replace("http", "ws")}//${
|
|
|
|
window.location.host
|
|
|
|
}/ws/client/`;
|
|
|
|
this.messageSocket = new WebSocket(wsUrl);
|
2020-12-01 16:27:19 +00:00
|
|
|
this.messageSocket.addEventListener("open", () => {
|
2020-12-05 21:08:42 +00:00
|
|
|
console.debug(`authentik/messages: connected to ${wsUrl}`);
|
2021-02-19 16:10:16 +00:00
|
|
|
this.retryDelay = 200;
|
2020-11-26 16:23:29 +00:00
|
|
|
});
|
|
|
|
this.messageSocket.addEventListener("close", (e) => {
|
2020-12-05 21:08:42 +00:00
|
|
|
console.debug(`authentik/messages: closed ws connection: ${e}`);
|
2020-12-12 19:46:02 +00:00
|
|
|
if (this.retryDelay > 3000) {
|
|
|
|
showMessage({
|
|
|
|
level_tag: "error",
|
|
|
|
message: gettext("Connection error, reconnecting...")
|
|
|
|
});
|
|
|
|
}
|
2020-11-26 16:23:29 +00:00
|
|
|
setTimeout(() => {
|
2020-12-05 21:08:42 +00:00
|
|
|
console.debug(`authentik/messages: reconnecting ws in ${this.retryDelay}ms`);
|
2020-11-26 16:23:29 +00:00
|
|
|
this.connect();
|
2020-11-26 17:04:24 +00:00
|
|
|
}, this.retryDelay);
|
|
|
|
this.retryDelay = this.retryDelay * 2;
|
2020-11-26 16:23:29 +00:00
|
|
|
});
|
|
|
|
this.messageSocket.addEventListener("message", (e) => {
|
|
|
|
const data = JSON.parse(e.data);
|
2021-03-12 11:41:17 +00:00
|
|
|
this.addMessage(data);
|
2020-12-12 19:46:02 +00:00
|
|
|
this.requestUpdate();
|
2020-11-26 16:23:29 +00:00
|
|
|
});
|
2020-11-26 17:36:26 +00:00
|
|
|
this.messageSocket.addEventListener("error", (e) => {
|
2020-12-05 21:08:42 +00:00
|
|
|
console.warn(`authentik/messages: error ${e}`);
|
2020-11-26 17:36:26 +00:00
|
|
|
this.retryDelay = this.retryDelay * 2;
|
2020-11-26 21:37:41 +00:00
|
|
|
});
|
2020-10-16 13:26:51 +00:00
|
|
|
}
|
|
|
|
|
2020-12-01 16:27:19 +00:00
|
|
|
render(): TemplateResult {
|
2020-12-12 19:46:02 +00:00
|
|
|
return html`<ul class="pf-c-alert-group pf-m-toast">
|
|
|
|
${this.messages.map((m) => {
|
2021-03-01 19:33:00 +00:00
|
|
|
return html`<ak-message
|
2020-12-12 19:46:02 +00:00
|
|
|
.message=${m}
|
|
|
|
.onRemove=${(m: APIMessage) => {
|
2021-03-01 19:33:00 +00:00
|
|
|
this.messages = this.messages.filter((v) => v !== m);
|
2021-03-12 11:41:17 +00:00
|
|
|
this.requestUpdate();
|
|
|
|
}}>
|
2021-03-01 19:33:00 +00:00
|
|
|
</ak-message>`;
|
|
|
|
})}
|
2020-12-12 19:46:02 +00:00
|
|
|
</ul>`;
|
2020-10-16 13:26:51 +00:00
|
|
|
}
|
|
|
|
}
|