import { Link } from "inferno-router";
import {
CommentResponse,
- CommentView,
- GetPersonMentions,
- GetPersonMentionsResponse,
- GetPrivateMessages,
- GetReplies,
- GetRepliesResponse,
GetReportCount,
GetReportCountResponse,
GetSiteResponse,
+ GetUnreadCount,
+ GetUnreadCountResponse,
PrivateMessageResponse,
- PrivateMessagesResponse,
- PrivateMessageView,
- SortType,
UserOperation,
} from "lemmy-js-client";
import { Subscription } from "rxjs";
import {
authField,
donateLemmyUrl,
- fetchLimit,
getLanguage,
isBrowser,
notifyComment,
interface NavbarState {
isLoggedIn: boolean;
expanded: boolean;
- replies: CommentView[];
- mentions: CommentView[];
- messages: PrivateMessageView[];
unreadInboxCount: number;
unreadReportCount: number;
searchParam: string;
isLoggedIn: !!this.props.site_res.my_user,
unreadInboxCount: 0,
unreadReportCount: 0,
- replies: [],
- mentions: [],
- messages: [],
expanded: false,
searchParam: "",
toggleSearch: false,
})
);
this.fetchUnreads();
- } else if (op == UserOperation.GetReplies) {
- let data = wsJsonToRes<GetRepliesResponse>(msg).data;
- let unreadReplies = data.replies.filter(r => !r.comment.read);
-
- 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();
+ } else if (op == UserOperation.GetUnreadCount) {
+ let data = wsJsonToRes<GetUnreadCountResponse>(msg).data;
+ this.state.unreadInboxCount =
+ data.replies + data.mentions + data.private_messages;
this.setState(this.state);
this.sendUnreadCount();
} else if (op == UserOperation.GetReportCount) {
UserService.Instance.myUserInfo.local_user_view.local_user.id
)
) {
- this.state.replies.push(data.comment_view);
this.state.unreadInboxCount++;
this.setState(this.state);
this.sendUnreadCount();
data.private_message_view.recipient.id ==
UserService.Instance.myUserInfo.local_user_view.person.id
) {
- this.state.messages.push(data.private_message_view);
this.state.unreadInboxCount++;
this.setState(this.state);
this.sendUnreadCount();
}
fetchUnreads() {
- // TODO we should just add a count call to the API for these, because this is a limited fetch,
- // 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 = {
- sort: SortType.New,
- unread_only: true,
- page: 1,
- limit: fetchLimit,
- auth: authField(),
- };
-
- let privateMessagesForm: GetPrivateMessages = {
- 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 inbox unreads...");
+
+ let unreadForm: GetUnreadCount = {
+ auth: authField(),
+ };
+
+ WebSocketService.Instance.send(wsClient.getUnreadCount(unreadForm));
console.log("Fetching reports...");
);
}
- 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 {
return (
UserService.Instance.myUserInfo &&
i.state.replies = [];
i.state.mentions = [];
i.state.messages = [];
- i.sendUnreadCount();
+ UserService.Instance.unreadInboxCountSub.next(0);
window.scrollTo(0, 0);
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) {
let op = wsUserOp(msg);
console.log(msg);
this.state.replies = data.replies;
this.state.combined = this.buildCombined();
this.state.loading = false;
- this.sendUnreadCount();
window.scrollTo(0, 0);
this.setState(this.state);
setupTippy();
let data = wsJsonToRes<GetPersonMentionsResponse>(msg).data;
this.state.mentions = data.mentions;
this.state.combined = this.buildCombined();
- this.sendUnreadCount();
window.scrollTo(0, 0);
this.setState(this.state);
setupTippy();
let data = wsJsonToRes<PrivateMessagesResponse>(msg).data;
this.state.messages = data.private_messages;
this.state.combined = this.buildCombined();
- this.sendUnreadCount();
window.scrollTo(0, 0);
this.setState(this.state);
setupTippy();
data.private_message_view.private_message.read;
}
}
- this.sendUnreadCount();
+ this.sendUnreadCount(data.private_message_view.private_message.read);
this.setState(this.state);
} else if (op == UserOperation.MarkAllAsRead) {
// Moved to be instant
found.comment.read = combinedView.comment.read =
data.comment_view.comment.read;
}
- this.sendUnreadCount();
+
+ this.sendUnreadCount(data.comment_view.comment.read);
this.setState(this.state);
setupTippy();
} else if (op == UserOperation.MarkPersonMentionAsRead) {
data.person_mention_view.person_mention.read;
}
}
- this.sendUnreadCount();
+ this.sendUnreadCount(data.person_mention_view.person_mention.read);
this.setState(this.state);
} else if (op == UserOperation.CreateComment) {
let data = wsJsonToRes<CommentResponse>(msg).data;
}
this.state.combined = this.buildCombined();
}
- this.sendUnreadCount();
+ this.sendUnreadCount(true);
this.setState(this.state);
setupTippy();
// TODO this seems wrong, you should be using form_id
}
}
- 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 {
return (view as PersonMentionView).person_mention !== undefined;
}