diff --git a/src/shared/services/InboxService.ts b/src/shared/services/InboxService.ts index c1999c83..589bfc84 100644 --- a/src/shared/services/InboxService.ts +++ b/src/shared/services/InboxService.ts @@ -1,4 +1,5 @@ import { myAuth } from "@utils/app"; +import { isBrowser } from "@utils/browser"; import { poll } from "@utils/helpers"; import { amAdmin } from "@utils/roles"; import { @@ -11,27 +12,27 @@ import { HttpService, UserService } from "../services"; import { RequestState } from "../services/HttpService"; export class InboxService { - #unreadInboxCountRes: RequestState; - #unreadReportCountRes: RequestState; - #unreadApplicationCountRes: RequestState; - - static #instance: InboxService; + static #_instance: InboxService; + unreadInboxCountRes: RequestState; + unreadReportCountRes: RequestState; + unreadApplicationCountRes: RequestState; private constructor() { - this.#unreadInboxCountRes = { state: "empty" }; - this.#unreadReportCountRes = { state: "empty" }; - this.#unreadApplicationCountRes = { state: "empty" }; + this.unreadInboxCountRes = { state: "empty" }; + this.unreadReportCountRes = { state: "empty" }; + this.unreadApplicationCountRes = { state: "empty" }; this.startPoll(); + this.fetchUnreadCounts(); } static get #Instance() { - return this.#instance ?? (this.#instance = new this()); + return this.#_instance ?? (this.#_instance = new this()); } unreadInboxCount(): number { - if (this.#unreadInboxCountRes.state === "success") { - const { data } = this.#unreadInboxCountRes; + if (this.unreadInboxCountRes.state === "success") { + const { data } = this.unreadInboxCountRes; return data.replies + data.mentions + data.private_messages; } else { return 0; @@ -39,12 +40,12 @@ export class InboxService { } public static get unreadInboxCount(): number { - return this.#instance.unreadInboxCount(); + return this.#Instance.unreadInboxCount(); } unreadReportCount(): number { - if (this.#unreadReportCountRes.state === "success") { - const { data } = this.#unreadReportCountRes; + if (this.unreadReportCountRes.state === "success") { + const { data } = this.unreadReportCountRes; return ( data.post_reports + data.comment_reports + @@ -56,19 +57,19 @@ export class InboxService { } public static get unreadReportCount(): number { - return this.#instance.unreadReportCount(); + return this.#Instance.unreadReportCount(); } unreadApplicationCount(): number { - if (this.#unreadApplicationCountRes.state === "success") { - return this.#unreadApplicationCountRes.data.registration_applications; + if (this.unreadApplicationCountRes.state === "success") { + return this.unreadApplicationCountRes.data.registration_applications; } else { return 0; } } public static get unreadApplicationCount(): number { - return this.#instance.unreadApplicationCount(); + return this.#Instance.unreadApplicationCount(); } get isModerator(): boolean { @@ -78,6 +79,10 @@ export class InboxService { } startPoll() { + if (!isBrowser()) { + return; + } + poll(async () => { if (window.document.visibilityState === "hidden") { return; @@ -91,18 +96,18 @@ export class InboxService { const auth = myAuth(); if (auth) { - this.#unreadInboxCountRes = await HttpService.client.getUnreadCount({ + this.unreadInboxCountRes = await HttpService.client.getUnreadCount({ auth, }); if (this.isModerator) { - this.#unreadReportCountRes = await HttpService.client.getReportCount({ + this.unreadReportCountRes = await HttpService.client.getReportCount({ auth, }); } if (amAdmin()) { - this.#unreadApplicationCountRes = + this.unreadApplicationCountRes = await HttpService.client.getUnreadRegistrationApplicationCount({ auth, });