Simplifying getunreadcount. (#455)

This commit is contained in:
Dessalines 2021-10-17 13:42:30 -04:00 committed by GitHub
parent ec23617d55
commit 99c9a608d9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 115 deletions

View file

@ -73,7 +73,7 @@
"husky": "^7.0.2", "husky": "^7.0.2",
"import-sort-style-module": "^6.0.0", "import-sort-style-module": "^6.0.0",
"iso-639-1": "^2.1.9", "iso-639-1": "^2.1.9",
"lemmy-js-client": "0.13.1-rc.1", "lemmy-js-client": "0.13.4-rc.1",
"lint-staged": "^11.0.1", "lint-staged": "^11.0.1",
"mini-css-extract-plugin": "^2.3.0", "mini-css-extract-plugin": "^2.3.0",
"node-fetch": "^2.6.1", "node-fetch": "^2.6.1",

View file

@ -2,19 +2,12 @@ import { Component, createRef, linkEvent, RefObject } from "inferno";
import { Link } from "inferno-router"; import { Link } from "inferno-router";
import { import {
CommentResponse, CommentResponse,
CommentView,
GetPersonMentions,
GetPersonMentionsResponse,
GetPrivateMessages,
GetReplies,
GetRepliesResponse,
GetReportCount, GetReportCount,
GetReportCountResponse, GetReportCountResponse,
GetSiteResponse, GetSiteResponse,
GetUnreadCount,
GetUnreadCountResponse,
PrivateMessageResponse, PrivateMessageResponse,
PrivateMessagesResponse,
PrivateMessageView,
SortType,
UserOperation, UserOperation,
} from "lemmy-js-client"; } from "lemmy-js-client";
import { Subscription } from "rxjs"; import { Subscription } from "rxjs";
@ -23,7 +16,6 @@ import { UserService, WebSocketService } from "../../services";
import { import {
authField, authField,
donateLemmyUrl, donateLemmyUrl,
fetchLimit,
getLanguage, getLanguage,
isBrowser, isBrowser,
notifyComment, notifyComment,
@ -47,9 +39,6 @@ interface NavbarProps {
interface NavbarState { interface NavbarState {
isLoggedIn: boolean; isLoggedIn: boolean;
expanded: boolean; expanded: boolean;
replies: CommentView[];
mentions: CommentView[];
messages: PrivateMessageView[];
unreadInboxCount: number; unreadInboxCount: number;
unreadReportCount: number; unreadReportCount: number;
searchParam: string; searchParam: string;
@ -68,9 +57,6 @@ export class Navbar extends Component<NavbarProps, NavbarState> {
isLoggedIn: !!this.props.site_res.my_user, isLoggedIn: !!this.props.site_res.my_user,
unreadInboxCount: 0, unreadInboxCount: 0,
unreadReportCount: 0, unreadReportCount: 0,
replies: [],
mentions: [],
messages: [],
expanded: false, expanded: false,
searchParam: "", searchParam: "",
toggleSearch: false, toggleSearch: false,
@ -577,30 +563,10 @@ export class Navbar extends Component<NavbarProps, NavbarState> {
}) })
); );
this.fetchUnreads(); this.fetchUnreads();
} else if (op == UserOperation.GetReplies) { } else if (op == UserOperation.GetUnreadCount) {
let data = wsJsonToRes<GetRepliesResponse>(msg).data; let data = wsJsonToRes<GetUnreadCountResponse>(msg).data;
let unreadReplies = data.replies.filter(r => !r.comment.read); this.state.unreadInboxCount =
data.replies + data.mentions + data.private_messages;
this.state.replies = unreadReplies;
this.state.unreadInboxCount = this.calculateUnreadInboxCount();
this.setState(this.state);
this.sendUnreadCount();
} else if (op == UserOperation.GetPersonMentions) {
let data = wsJsonToRes<GetPersonMentionsResponse>(msg).data;
let unreadMentions = data.mentions.filter(r => !r.comment.read);
this.state.mentions = unreadMentions;
this.state.unreadInboxCount = this.calculateUnreadInboxCount();
this.setState(this.state);
this.sendUnreadCount();
} else if (op == UserOperation.GetPrivateMessages) {
let data = wsJsonToRes<PrivateMessagesResponse>(msg).data;
let unreadMessages = data.private_messages.filter(
r => !r.private_message.read
);
this.state.messages = unreadMessages;
this.state.unreadInboxCount = this.calculateUnreadInboxCount();
this.setState(this.state); this.setState(this.state);
this.sendUnreadCount(); this.sendUnreadCount();
} else if (op == UserOperation.GetReportCount) { } else if (op == UserOperation.GetReportCount) {
@ -628,7 +594,6 @@ export class Navbar extends Component<NavbarProps, NavbarState> {
UserService.Instance.myUserInfo.local_user_view.local_user.id UserService.Instance.myUserInfo.local_user_view.local_user.id
) )
) { ) {
this.state.replies.push(data.comment_view);
this.state.unreadInboxCount++; this.state.unreadInboxCount++;
this.setState(this.state); this.setState(this.state);
this.sendUnreadCount(); this.sendUnreadCount();
@ -643,7 +608,6 @@ export class Navbar extends Component<NavbarProps, NavbarState> {
data.private_message_view.recipient.id == data.private_message_view.recipient.id ==
UserService.Instance.myUserInfo.local_user_view.person.id UserService.Instance.myUserInfo.local_user_view.person.id
) { ) {
this.state.messages.push(data.private_message_view);
this.state.unreadInboxCount++; this.state.unreadInboxCount++;
this.setState(this.state); this.setState(this.state);
this.sendUnreadCount(); this.sendUnreadCount();
@ -654,41 +618,13 @@ export class Navbar extends Component<NavbarProps, NavbarState> {
} }
fetchUnreads() { fetchUnreads() {
// TODO we should just add a count call to the API for these, because this is a limited fetch, console.log("Fetching inbox unreads...");
// and it shouldn't have to fetch the actual content
if (this.currentLocation !== "/inbox") {
console.log("Fetching inbox unreads...");
let repliesForm: GetReplies = {
sort: SortType.New,
unread_only: true,
page: 1,
limit: fetchLimit,
auth: authField(),
};
let personMentionsForm: GetPersonMentions = { let unreadForm: GetUnreadCount = {
sort: SortType.New, auth: authField(),
unread_only: true, };
page: 1,
limit: fetchLimit,
auth: authField(),
};
let privateMessagesForm: GetPrivateMessages = { WebSocketService.Instance.send(wsClient.getUnreadCount(unreadForm));
unread_only: true,
page: 1,
limit: fetchLimit,
auth: authField(),
};
WebSocketService.Instance.send(wsClient.getReplies(repliesForm));
WebSocketService.Instance.send(
wsClient.getPersonMentions(personMentionsForm)
);
WebSocketService.Instance.send(
wsClient.getPrivateMessages(privateMessagesForm)
);
}
console.log("Fetching reports..."); console.log("Fetching reports...");
@ -713,14 +649,6 @@ export class Navbar extends Component<NavbarProps, NavbarState> {
); );
} }
calculateUnreadInboxCount(): number {
return (
this.state.replies.filter(r => !r.comment.read).length +
this.state.mentions.filter(r => !r.comment.read).length +
this.state.messages.filter(r => !r.private_message.read).length
);
}
get canAdmin(): boolean { get canAdmin(): boolean {
return ( return (
UserService.Instance.myUserInfo && UserService.Instance.myUserInfo &&

View file

@ -533,11 +533,20 @@ export class Inbox extends Component<any, InboxState> {
i.state.replies = []; i.state.replies = [];
i.state.mentions = []; i.state.mentions = [];
i.state.messages = []; i.state.messages = [];
i.sendUnreadCount(); UserService.Instance.unreadInboxCountSub.next(0);
window.scrollTo(0, 0); window.scrollTo(0, 0);
i.setState(i.state); i.setState(i.state);
} }
sendUnreadCount(read: boolean) {
let urcs = UserService.Instance.unreadInboxCountSub;
if (read) {
urcs.next(urcs.getValue() - 1);
} else {
urcs.next(urcs.getValue() + 1);
}
}
parseMessage(msg: any) { parseMessage(msg: any) {
let op = wsUserOp(msg); let op = wsUserOp(msg);
console.log(msg); console.log(msg);
@ -551,7 +560,6 @@ export class Inbox extends Component<any, InboxState> {
this.state.replies = data.replies; this.state.replies = data.replies;
this.state.combined = this.buildCombined(); this.state.combined = this.buildCombined();
this.state.loading = false; this.state.loading = false;
this.sendUnreadCount();
window.scrollTo(0, 0); window.scrollTo(0, 0);
this.setState(this.state); this.setState(this.state);
setupTippy(); setupTippy();
@ -559,7 +567,6 @@ export class Inbox extends Component<any, InboxState> {
let data = wsJsonToRes<GetPersonMentionsResponse>(msg).data; let data = wsJsonToRes<GetPersonMentionsResponse>(msg).data;
this.state.mentions = data.mentions; this.state.mentions = data.mentions;
this.state.combined = this.buildCombined(); this.state.combined = this.buildCombined();
this.sendUnreadCount();
window.scrollTo(0, 0); window.scrollTo(0, 0);
this.setState(this.state); this.setState(this.state);
setupTippy(); setupTippy();
@ -567,7 +574,6 @@ export class Inbox extends Component<any, InboxState> {
let data = wsJsonToRes<PrivateMessagesResponse>(msg).data; let data = wsJsonToRes<PrivateMessagesResponse>(msg).data;
this.state.messages = data.private_messages; this.state.messages = data.private_messages;
this.state.combined = this.buildCombined(); this.state.combined = this.buildCombined();
this.sendUnreadCount();
window.scrollTo(0, 0); window.scrollTo(0, 0);
this.setState(this.state); this.setState(this.state);
setupTippy(); setupTippy();
@ -635,7 +641,7 @@ export class Inbox extends Component<any, InboxState> {
data.private_message_view.private_message.read; data.private_message_view.private_message.read;
} }
} }
this.sendUnreadCount(); this.sendUnreadCount(data.private_message_view.private_message.read);
this.setState(this.state); this.setState(this.state);
} else if (op == UserOperation.MarkAllAsRead) { } else if (op == UserOperation.MarkAllAsRead) {
// Moved to be instant // Moved to be instant
@ -671,7 +677,8 @@ export class Inbox extends Component<any, InboxState> {
found.comment.read = combinedView.comment.read = found.comment.read = combinedView.comment.read =
data.comment_view.comment.read; data.comment_view.comment.read;
} }
this.sendUnreadCount();
this.sendUnreadCount(data.comment_view.comment.read);
this.setState(this.state); this.setState(this.state);
setupTippy(); setupTippy();
} else if (op == UserOperation.MarkPersonMentionAsRead) { } else if (op == UserOperation.MarkPersonMentionAsRead) {
@ -719,7 +726,7 @@ export class Inbox extends Component<any, InboxState> {
data.person_mention_view.person_mention.read; data.person_mention_view.person_mention.read;
} }
} }
this.sendUnreadCount(); this.sendUnreadCount(data.person_mention_view.person_mention.read);
this.setState(this.state); this.setState(this.state);
} else if (op == UserOperation.CreateComment) { } else if (op == UserOperation.CreateComment) {
let data = wsJsonToRes<CommentResponse>(msg).data; let data = wsJsonToRes<CommentResponse>(msg).data;
@ -764,7 +771,7 @@ export class Inbox extends Component<any, InboxState> {
} }
this.state.combined = this.buildCombined(); this.state.combined = this.buildCombined();
} }
this.sendUnreadCount(); this.sendUnreadCount(true);
this.setState(this.state); this.setState(this.state);
setupTippy(); setupTippy();
// TODO this seems wrong, you should be using form_id // TODO this seems wrong, you should be using form_id
@ -807,25 +814,6 @@ export class Inbox extends Component<any, InboxState> {
} }
} }
sendUnreadCount() {
UserService.Instance.unreadInboxCountSub.next(this.unreadCount());
}
unreadCount(): number {
return (
this.state.replies.filter(r => !r.comment.read).length +
this.state.mentions.filter(r => !r.person_mention.read).length +
this.state.messages.filter(
r =>
UserService.Instance.myUserInfo &&
!r.private_message.read &&
// TODO also seems very strange and wrong
r.creator.id !==
UserService.Instance.myUserInfo.local_user_view.person.id
).length
);
}
isMention(view: any): view is PersonMentionView { isMention(view: any): view is PersonMentionView {
return (view as PersonMentionView).person_mention !== undefined; return (view as PersonMentionView).person_mention !== undefined;
} }

View file

@ -4915,10 +4915,10 @@ lcid@^1.0.0:
dependencies: dependencies:
invert-kv "^1.0.0" invert-kv "^1.0.0"
lemmy-js-client@0.13.1-rc.1: lemmy-js-client@0.13.4-rc.1:
version "0.13.1-rc.1" version "0.13.4-rc.1"
resolved "https://registry.yarnpkg.com/lemmy-js-client/-/lemmy-js-client-0.13.1-rc.1.tgz#e1af4749a5493954a17f87b7b20dcccb8c585f22" resolved "https://registry.yarnpkg.com/lemmy-js-client/-/lemmy-js-client-0.13.4-rc.1.tgz#ddc7fd7c02c17f46c311a6c2656171188a90549e"
integrity sha512-fncCq6Zu8s6GpeCrkmJS8/rqXcyrJ8p8EyWfXiiuZlWkgzOIi+qZjTRnO63wI6DomYwVOjwk7sry4RbOJSdK5Q== integrity sha512-3Tvxa2Xl9jE+9zvwkR6BFDCcdb1RJNPowfyspY4bB4bqC1dmrP/MCLIxmlwGhd/E1IEEE6TA7XX1wldweYn6gw==
levn@^0.4.1: levn@^0.4.1:
version "0.4.1" version "0.4.1"