use improved notification system for all notification types (#2190)

* only refetch PM counts when marking a message as read

* refresh registration applications and the corresponding unread counter when processing an application

* refetch reports when marking one as resolved

* update unread notifications when logging in

* UnreadCounterService: use async functions

* clarify the meaning of UnreadCounterService.updateInboxCounts

* UnreadCounterService: correct updateAll
This commit is contained in:
biosfood 2023-10-24 23:58:00 +02:00 committed by GitHub
parent 25b06124fd
commit 069c2c787f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 55 additions and 16 deletions

View file

@ -16,6 +16,7 @@ import { HtmlTags } from "../common/html-tags";
import { Spinner } from "../common/icon"; import { Spinner } from "../common/icon";
import PasswordInput from "../common/password-input"; import PasswordInput from "../common/password-input";
import TotpModal from "../common/totp-modal"; import TotpModal from "../common/totp-modal";
import { UnreadCounterService } from "../../services";
interface LoginProps { interface LoginProps {
prev?: string; prev?: string;
@ -55,6 +56,8 @@ async function handleLoginSuccess(i: Login, loginRes: LoginResponse) {
: i.props.history.action === "PUSH" : i.props.history.action === "PUSH"
? i.props.history.back() ? i.props.history.back()
: i.props.history.replace("/"); : i.props.history.replace("/");
UnreadCounterService.Instance.updateAll();
} }
async function handleLoginSubmit(i: Login, event: any) { async function handleLoginSubmit(i: Login, event: any) {

View file

@ -795,7 +795,7 @@ export class Inbox extends Component<any, InboxState> {
limit, limit,
}), }),
}); });
UnreadCounterService.Instance.update(); UnreadCounterService.Instance.updateInboxCounts();
} }
async handleSortChange(val: CommentSortType) { async handleSortChange(val: CommentSortType) {

View file

@ -23,6 +23,7 @@ import { HtmlTags } from "../common/html-tags";
import { Spinner } from "../common/icon"; import { Spinner } from "../common/icon";
import { Paginator } from "../common/paginator"; import { Paginator } from "../common/paginator";
import { RegistrationApplication } from "../common/registration-application"; import { RegistrationApplication } from "../common/registration-application";
import { UnreadCounterService } from "../../services";
enum UnreadOrAll { enum UnreadOrAll {
Unread, Unread,
@ -243,6 +244,10 @@ export class RegistrationApplications extends Component<
approveRes.data.registration_application, approveRes.data.registration_application,
s.appsRes.data.registration_applications, s.appsRes.data.registration_applications,
); );
if (this.state.unreadOrAll === UnreadOrAll.Unread) {
this.refetch();
UnreadCounterService.Instance.updateApplications();
}
} }
return s; return s;
}); });

View file

@ -46,6 +46,7 @@ import { Spinner } from "../common/icon";
import { Paginator } from "../common/paginator"; import { Paginator } from "../common/paginator";
import { PostReport } from "../post/post-report"; import { PostReport } from "../post/post-report";
import { PrivateMessageReport } from "../private_message/private-message-report"; import { PrivateMessageReport } from "../private_message/private-message-report";
import { UnreadCounterService } from "../../services";
enum UnreadOrAll { enum UnreadOrAll {
Unread, Unread,
@ -610,16 +611,28 @@ export class Reports extends Component<any, ReportsState> {
async handleResolveCommentReport(form: ResolveCommentReport) { async handleResolveCommentReport(form: ResolveCommentReport) {
const res = await HttpService.client.resolveCommentReport(form); const res = await HttpService.client.resolveCommentReport(form);
this.findAndUpdateCommentReport(res); this.findAndUpdateCommentReport(res);
if (this.state.unreadOrAll === UnreadOrAll.Unread) {
this.refetch();
UnreadCounterService.Instance.updateReports();
}
} }
async handleResolvePostReport(form: ResolvePostReport) { async handleResolvePostReport(form: ResolvePostReport) {
const res = await HttpService.client.resolvePostReport(form); const res = await HttpService.client.resolvePostReport(form);
this.findAndUpdatePostReport(res); this.findAndUpdatePostReport(res);
if (this.state.unreadOrAll === UnreadOrAll.Unread) {
this.refetch();
UnreadCounterService.Instance.updateReports();
}
} }
async handleResolvePrivateMessageReport(form: ResolvePrivateMessageReport) { async handleResolvePrivateMessageReport(form: ResolvePrivateMessageReport) {
const res = await HttpService.client.resolvePrivateMessageReport(form); const res = await HttpService.client.resolvePrivateMessageReport(form);
this.findAndUpdatePrivateMessageReport(res); this.findAndUpdatePrivateMessageReport(res);
if (this.state.unreadOrAll === UnreadOrAll.Unread) {
this.refetch();
UnreadCounterService.Instance.updateReports();
}
} }
findAndUpdateCommentReport(res: RequestState<CommentReportResponse>) { findAndUpdateCommentReport(res: RequestState<CommentReportResponse>) {

View file

@ -32,27 +32,36 @@ export class UnreadCounterService {
constructor() { constructor() {
if (isBrowser()) { if (isBrowser()) {
poll(this.update, updateUnreadCountsInterval); poll(async () => this.updateAll(), updateUnreadCountsInterval);
} }
} }
public update = async () => { private get shouldUpdate() {
if (window.document.visibilityState === "hidden") { if (window.document.visibilityState === "hidden") {
return; return false;
} }
if (!myAuth()) { if (!myAuth()) {
return; return false;
} }
const unreadCountRes = await HttpService.client.getUnreadCount(); return true;
if (unreadCountRes.state === "success") { }
this.unreadPrivateMessages = unreadCountRes.data.private_messages;
this.unreadReplies = unreadCountRes.data.replies; public async updateInboxCounts() {
this.unreadMentions = unreadCountRes.data.mentions; if (this.shouldUpdate) {
this.unreadInboxCountSubject.next( const unreadCountRes = await HttpService.client.getUnreadCount();
this.unreadPrivateMessages + this.unreadReplies + this.unreadMentions, if (unreadCountRes.state === "success") {
); this.unreadPrivateMessages = unreadCountRes.data.private_messages;
this.unreadReplies = unreadCountRes.data.replies;
this.unreadMentions = unreadCountRes.data.mentions;
this.unreadInboxCountSubject.next(
this.unreadPrivateMessages + this.unreadReplies + this.unreadMentions,
);
}
} }
if (UserService.Instance.moderatesSomething) { }
public async updateReports() {
if (this.shouldUpdate && UserService.Instance.moderatesSomething) {
const reportCountRes = await HttpService.client.getReportCount({}); const reportCountRes = await HttpService.client.getReportCount({});
if (reportCountRes.state === "success") { if (reportCountRes.state === "success") {
this.commentReportCount = reportCountRes.data.comment_reports ?? 0; this.commentReportCount = reportCountRes.data.comment_reports ?? 0;
@ -66,7 +75,10 @@ export class UnreadCounterService {
); );
} }
} }
if (amAdmin()) { }
public async updateApplications() {
if (this.shouldUpdate && amAdmin()) {
const unreadApplicationsRes = const unreadApplicationsRes =
await HttpService.client.getUnreadRegistrationApplicationCount(); await HttpService.client.getUnreadRegistrationApplicationCount();
if (unreadApplicationsRes.state === "success") { if (unreadApplicationsRes.state === "success") {
@ -75,7 +87,13 @@ export class UnreadCounterService {
); );
} }
} }
}; }
public async updateAll() {
this.updateInboxCounts();
this.updateReports();
this.updateApplications();
}
static get Instance() { static get Instance() {
return this.#instance ?? (this.#instance = new this()); return this.#instance ?? (this.#instance = new this());