Skip to content

Commit

Permalink
use improved notification system for all notification types (#2190)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
biosfood authored Oct 24, 2023
1 parent 25b0612 commit 069c2c7
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 16 deletions.
3 changes: 3 additions & 0 deletions src/shared/components/home/login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { HtmlTags } from "../common/html-tags";
import { Spinner } from "../common/icon";
import PasswordInput from "../common/password-input";
import TotpModal from "../common/totp-modal";
import { UnreadCounterService } from "../../services";

interface LoginProps {
prev?: string;
Expand Down Expand Up @@ -55,6 +56,8 @@ async function handleLoginSuccess(i: Login, loginRes: LoginResponse) {
: i.props.history.action === "PUSH"
? i.props.history.back()
: i.props.history.replace("/");

UnreadCounterService.Instance.updateAll();
}

async function handleLoginSubmit(i: Login, event: any) {
Expand Down
2 changes: 1 addition & 1 deletion src/shared/components/person/inbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,7 @@ export class Inbox extends Component<any, InboxState> {
limit,
}),
});
UnreadCounterService.Instance.update();
UnreadCounterService.Instance.updateInboxCounts();
}

async handleSortChange(val: CommentSortType) {
Expand Down
5 changes: 5 additions & 0 deletions src/shared/components/person/registration-applications.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { HtmlTags } from "../common/html-tags";
import { Spinner } from "../common/icon";
import { Paginator } from "../common/paginator";
import { RegistrationApplication } from "../common/registration-application";
import { UnreadCounterService } from "../../services";

enum UnreadOrAll {
Unread,
Expand Down Expand Up @@ -243,6 +244,10 @@ export class RegistrationApplications extends Component<
approveRes.data.registration_application,
s.appsRes.data.registration_applications,
);
if (this.state.unreadOrAll === UnreadOrAll.Unread) {
this.refetch();
UnreadCounterService.Instance.updateApplications();
}
}
return s;
});
Expand Down
13 changes: 13 additions & 0 deletions src/shared/components/person/reports.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import { Spinner } from "../common/icon";
import { Paginator } from "../common/paginator";
import { PostReport } from "../post/post-report";
import { PrivateMessageReport } from "../private_message/private-message-report";
import { UnreadCounterService } from "../../services";

enum UnreadOrAll {
Unread,
Expand Down Expand Up @@ -610,16 +611,28 @@ export class Reports extends Component<any, ReportsState> {
async handleResolveCommentReport(form: ResolveCommentReport) {
const res = await HttpService.client.resolveCommentReport(form);
this.findAndUpdateCommentReport(res);
if (this.state.unreadOrAll === UnreadOrAll.Unread) {
this.refetch();
UnreadCounterService.Instance.updateReports();
}
}

async handleResolvePostReport(form: ResolvePostReport) {
const res = await HttpService.client.resolvePostReport(form);
this.findAndUpdatePostReport(res);
if (this.state.unreadOrAll === UnreadOrAll.Unread) {
this.refetch();
UnreadCounterService.Instance.updateReports();
}
}

async handleResolvePrivateMessageReport(form: ResolvePrivateMessageReport) {
const res = await HttpService.client.resolvePrivateMessageReport(form);
this.findAndUpdatePrivateMessageReport(res);
if (this.state.unreadOrAll === UnreadOrAll.Unread) {
this.refetch();
UnreadCounterService.Instance.updateReports();
}
}

findAndUpdateCommentReport(res: RequestState<CommentReportResponse>) {
Expand Down
48 changes: 33 additions & 15 deletions src/shared/services/UnreadCounterService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,36 @@ export class UnreadCounterService {

constructor() {
if (isBrowser()) {
poll(this.update, updateUnreadCountsInterval);
poll(async () => this.updateAll(), updateUnreadCountsInterval);
}
}

public update = async () => {
private get shouldUpdate() {
if (window.document.visibilityState === "hidden") {
return;
return false;
}
if (!myAuth()) {
return;
return false;
}
const unreadCountRes = await HttpService.client.getUnreadCount();
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,
);
return true;
}

public async updateInboxCounts() {
if (this.shouldUpdate) {
const unreadCountRes = await HttpService.client.getUnreadCount();
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({});
if (reportCountRes.state === "success") {
this.commentReportCount = reportCountRes.data.comment_reports ?? 0;
Expand All @@ -66,7 +75,10 @@ export class UnreadCounterService {
);
}
}
if (amAdmin()) {
}

public async updateApplications() {
if (this.shouldUpdate && amAdmin()) {
const unreadApplicationsRes =
await HttpService.client.getUnreadRegistrationApplicationCount();
if (unreadApplicationsRes.state === "success") {
Expand All @@ -75,7 +87,13 @@ export class UnreadCounterService {
);
}
}
};
}

public async updateAll() {
this.updateInboxCounts();
this.updateReports();
this.updateApplications();
}

static get Instance() {
return this.#instance ?? (this.#instance = new this());
Expand Down

0 comments on commit 069c2c7

Please sign in to comment.