1 import { Component, linkEvent } from "inferno";
8 GetPersonMentionsResponse,
12 PersonMentionResponse,
15 PrivateMessageResponse,
16 PrivateMessagesResponse,
21 } from "lemmy-js-client";
22 import { Subscription } from "rxjs";
23 import { i18n } from "../../i18next";
24 import { InitialFetchRequest } from "../../interfaces";
25 import { UserService, WebSocketService } from "../../services";
43 import { CommentNodes } from "../comment/comment-nodes";
44 import { HtmlTags } from "../common/html-tags";
45 import { Icon, Spinner } from "../common/icon";
46 import { Paginator } from "../common/paginator";
47 import { SortSelect } from "../common/sort-select";
48 import { PrivateMessage } from "../private_message/private-message";
70 view: CommentView | PrivateMessageView | PersonMentionView;
74 interface InboxState {
75 unreadOrAll: UnreadOrAll;
76 messageType: MessageType;
77 replies: CommentView[];
78 mentions: PersonMentionView[];
79 messages: PrivateMessageView[];
80 combined: ReplyType[];
87 export class Inbox extends Component<any, InboxState> {
88 private isoData = setIsoData(this.context);
89 private subscription: Subscription;
90 private emptyState: InboxState = {
91 unreadOrAll: UnreadOrAll.Unread,
92 messageType: MessageType.All,
99 site_view: this.isoData.site_res.site_view,
103 constructor(props: any, context: any) {
104 super(props, context);
106 this.state = this.emptyState;
107 this.handleSortChange = this.handleSortChange.bind(this);
108 this.handlePageChange = this.handlePageChange.bind(this);
110 if (!UserService.Instance.myUserInfo && isBrowser()) {
111 toast(i18n.t("not_logged_in"), "danger");
112 this.context.router.history.push(`/login`);
115 this.parseMessage = this.parseMessage.bind(this);
116 this.subscription = wsSubscribe(this.parseMessage);
118 // Only fetch the data if coming from another route
119 if (this.isoData.path == this.context.router.route.match.url) {
120 this.state.replies = this.isoData.routeData[0].replies || [];
121 this.state.mentions = this.isoData.routeData[1].mentions || [];
122 this.state.messages = this.isoData.routeData[2].messages || [];
123 this.state.combined = this.buildCombined();
124 this.state.loading = false;
130 componentWillUnmount() {
132 this.subscription.unsubscribe();
136 get documentTitle(): string {
138 UserService.Instance.myUserInfo.local_user_view.person.name
139 } ${i18n.t("inbox")} - ${this.state.site_view.site.name}`;
144 <div class="container">
145 {this.state.loading ? (
153 title={this.documentTitle}
154 path={this.context.router.route.match.url}
160 href={`/feeds/inbox/${UserService.Instance.auth}.xml`}
164 <Icon icon="rss" classes="ml-2 text-muted small" />
168 {this.state.replies.length +
169 this.state.mentions.length +
170 this.state.messages.length >
172 this.state.unreadOrAll == UnreadOrAll.Unread && (
174 class="btn btn-secondary mb-2"
175 onClick={linkEvent(this, this.markAllAsRead)}
177 {i18n.t("mark_all_as_read")}
181 {this.state.messageType == MessageType.All && this.all()}
182 {this.state.messageType == MessageType.Replies && this.replies()}
183 {this.state.messageType == MessageType.Mentions &&
185 {this.state.messageType == MessageType.Messages &&
188 page={this.state.page}
189 onChange={this.handlePageChange}
198 unreadOrAllRadios() {
200 <div class="btn-group btn-group-toggle flex-wrap mb-2">
202 className={`btn btn-outline-secondary pointer
203 ${this.state.unreadOrAll == UnreadOrAll.Unread && "active"}
208 value={UnreadOrAll.Unread}
209 checked={this.state.unreadOrAll == UnreadOrAll.Unread}
210 onChange={linkEvent(this, this.handleUnreadOrAllChange)}
215 className={`btn btn-outline-secondary pointer
216 ${this.state.unreadOrAll == UnreadOrAll.All && "active"}
221 value={UnreadOrAll.All}
222 checked={this.state.unreadOrAll == UnreadOrAll.All}
223 onChange={linkEvent(this, this.handleUnreadOrAllChange)}
231 messageTypeRadios() {
233 <div class="btn-group btn-group-toggle flex-wrap mb-2">
235 className={`btn btn-outline-secondary pointer
236 ${this.state.messageType == MessageType.All && "active"}
241 value={MessageType.All}
242 checked={this.state.messageType == MessageType.All}
243 onChange={linkEvent(this, this.handleMessageTypeChange)}
248 className={`btn btn-outline-secondary pointer
249 ${this.state.messageType == MessageType.Replies && "active"}
254 value={MessageType.Replies}
255 checked={this.state.messageType == MessageType.Replies}
256 onChange={linkEvent(this, this.handleMessageTypeChange)}
261 className={`btn btn-outline-secondary pointer
262 ${this.state.messageType == MessageType.Mentions && "active"}
267 value={MessageType.Mentions}
268 checked={this.state.messageType == MessageType.Mentions}
269 onChange={linkEvent(this, this.handleMessageTypeChange)}
274 className={`btn btn-outline-secondary pointer
275 ${this.state.messageType == MessageType.Messages && "active"}
280 value={MessageType.Messages}
281 checked={this.state.messageType == MessageType.Messages}
282 onChange={linkEvent(this, this.handleMessageTypeChange)}
292 <div className="mb-2">
293 <span class="mr-3">{this.unreadOrAllRadios()}</span>
294 <span class="mr-3">{this.messageTypeRadios()}</span>
296 sort={this.state.sort}
297 onChange={this.handleSortChange}
305 replyToReplyType(r: CommentView): ReplyType {
308 type_: ReplyEnum.Reply,
310 published: r.comment.published,
314 mentionToReplyType(r: PersonMentionView): ReplyType {
316 id: r.person_mention.id,
317 type_: ReplyEnum.Mention,
319 published: r.comment.published,
323 messageToReplyType(r: PrivateMessageView): ReplyType {
325 id: r.private_message.id,
326 type_: ReplyEnum.Message,
328 published: r.private_message.published,
332 buildCombined(): ReplyType[] {
333 let replies: ReplyType[] = this.state.replies.map(r =>
334 this.replyToReplyType(r)
336 let mentions: ReplyType[] = this.state.mentions.map(r =>
337 this.mentionToReplyType(r)
339 let messages: ReplyType[] = this.state.messages.map(r =>
340 this.messageToReplyType(r)
343 return [...replies, ...mentions, ...messages].sort((a, b) =>
344 b.published.localeCompare(a.published)
348 renderReplyType(i: ReplyType) {
350 case ReplyEnum.Reply:
354 nodes={[{ comment_view: i.view as CommentView }]}
359 enableDownvotes={this.state.site_view.site.enable_downvotes}
362 case ReplyEnum.Mention:
366 nodes={[{ comment_view: i.view as PersonMentionView }]}
371 enableDownvotes={this.state.site_view.site.enable_downvotes}
374 case ReplyEnum.Message:
378 private_message_view={i.view as PrivateMessageView}
387 return <div>{this.state.combined.map(i => this.renderReplyType(i))}</div>;
394 nodes={commentsToFlatNodes(this.state.replies)}
399 enableDownvotes={this.state.site_view.site.enable_downvotes}
408 {this.state.mentions.map(umv => (
410 key={umv.person_mention.id}
411 nodes={[{ comment_view: umv }]}
416 enableDownvotes={this.state.site_view.site.enable_downvotes}
426 {this.state.messages.map(pmv => (
428 key={pmv.private_message.id}
429 private_message_view={pmv}
436 handlePageChange(page: number) {
437 this.setState({ page });
441 handleUnreadOrAllChange(i: Inbox, event: any) {
442 i.state.unreadOrAll = Number(event.target.value);
448 handleMessageTypeChange(i: Inbox, event: any) {
449 i.state.messageType = Number(event.target.value);
455 static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
456 let promises: Promise<any>[] = [];
458 // It can be /u/me, or /username/1
459 let repliesForm: GetReplies = {
466 promises.push(req.client.getReplies(repliesForm));
468 let personMentionsForm: GetPersonMentions = {
475 promises.push(req.client.getPersonMentions(personMentionsForm));
477 let privateMessagesForm: GetPrivateMessages = {
483 promises.push(req.client.getPrivateMessages(privateMessagesForm));
489 let repliesForm: GetReplies = {
490 sort: this.state.sort,
491 unread_only: this.state.unreadOrAll == UnreadOrAll.Unread,
492 page: this.state.page,
496 WebSocketService.Instance.send(wsClient.getReplies(repliesForm));
498 let personMentionsForm: GetPersonMentions = {
499 sort: this.state.sort,
500 unread_only: this.state.unreadOrAll == UnreadOrAll.Unread,
501 page: this.state.page,
505 WebSocketService.Instance.send(
506 wsClient.getPersonMentions(personMentionsForm)
509 let privateMessagesForm: GetPrivateMessages = {
510 unread_only: this.state.unreadOrAll == UnreadOrAll.Unread,
511 page: this.state.page,
515 WebSocketService.Instance.send(
516 wsClient.getPrivateMessages(privateMessagesForm)
520 handleSortChange(val: SortType) {
521 this.state.sort = val;
523 this.setState(this.state);
527 markAllAsRead(i: Inbox) {
528 WebSocketService.Instance.send(
529 wsClient.markAllAsRead({
533 i.state.replies = [];
534 i.state.mentions = [];
535 i.state.messages = [];
536 UserService.Instance.unreadInboxCountSub.next(0);
537 window.scrollTo(0, 0);
541 sendUnreadCount(read: boolean) {
542 let urcs = UserService.Instance.unreadInboxCountSub;
544 urcs.next(urcs.getValue() - 1);
546 urcs.next(urcs.getValue() + 1);
550 parseMessage(msg: any) {
551 let op = wsUserOp(msg);
554 toast(i18n.t(msg.error), "danger");
556 } else if (msg.reconnect) {
558 } else if (op == UserOperation.GetReplies) {
559 let data = wsJsonToRes<GetRepliesResponse>(msg).data;
560 this.state.replies = data.replies;
561 this.state.combined = this.buildCombined();
562 this.state.loading = false;
563 window.scrollTo(0, 0);
564 this.setState(this.state);
566 } else if (op == UserOperation.GetPersonMentions) {
567 let data = wsJsonToRes<GetPersonMentionsResponse>(msg).data;
568 this.state.mentions = data.mentions;
569 this.state.combined = this.buildCombined();
570 window.scrollTo(0, 0);
571 this.setState(this.state);
573 } else if (op == UserOperation.GetPrivateMessages) {
574 let data = wsJsonToRes<PrivateMessagesResponse>(msg).data;
575 this.state.messages = data.private_messages;
576 this.state.combined = this.buildCombined();
577 window.scrollTo(0, 0);
578 this.setState(this.state);
580 } else if (op == UserOperation.EditPrivateMessage) {
581 let data = wsJsonToRes<PrivateMessageResponse>(msg).data;
582 let found: PrivateMessageView = this.state.messages.find(
584 m.private_message.id === data.private_message_view.private_message.id
587 let combinedView = this.state.combined.find(
588 i => i.id == data.private_message_view.private_message.id
589 ).view as PrivateMessageView;
590 found.private_message.content = combinedView.private_message.content =
591 data.private_message_view.private_message.content;
592 found.private_message.updated = combinedView.private_message.updated =
593 data.private_message_view.private_message.updated;
595 this.setState(this.state);
596 } else if (op == UserOperation.DeletePrivateMessage) {
597 let data = wsJsonToRes<PrivateMessageResponse>(msg).data;
598 let found: PrivateMessageView = this.state.messages.find(
600 m.private_message.id === data.private_message_view.private_message.id
603 let combinedView = this.state.combined.find(
604 i => i.id == data.private_message_view.private_message.id
605 ).view as PrivateMessageView;
606 found.private_message.deleted = combinedView.private_message.deleted =
607 data.private_message_view.private_message.deleted;
608 found.private_message.updated = combinedView.private_message.updated =
609 data.private_message_view.private_message.updated;
611 this.setState(this.state);
612 } else if (op == UserOperation.MarkPrivateMessageAsRead) {
613 let data = wsJsonToRes<PrivateMessageResponse>(msg).data;
614 let found: PrivateMessageView = this.state.messages.find(
616 m.private_message.id === data.private_message_view.private_message.id
620 let combinedView = this.state.combined.find(
621 i => i.id == data.private_message_view.private_message.id
622 ).view as PrivateMessageView;
623 found.private_message.updated = combinedView.private_message.updated =
624 data.private_message_view.private_message.updated;
626 // If youre in the unread view, just remove it from the list
628 this.state.unreadOrAll == UnreadOrAll.Unread &&
629 data.private_message_view.private_message.read
631 this.state.messages = this.state.messages.filter(
633 r.private_message.id !==
634 data.private_message_view.private_message.id
636 this.state.combined = this.state.combined.filter(
637 r => r.id !== data.private_message_view.private_message.id
640 found.private_message.read = combinedView.private_message.read =
641 data.private_message_view.private_message.read;
644 this.sendUnreadCount(data.private_message_view.private_message.read);
645 this.setState(this.state);
646 } else if (op == UserOperation.MarkAllAsRead) {
647 // Moved to be instant
649 op == UserOperation.EditComment ||
650 op == UserOperation.DeleteComment ||
651 op == UserOperation.RemoveComment
653 let data = wsJsonToRes<CommentResponse>(msg).data;
654 editCommentRes(data.comment_view, this.state.replies);
655 this.setState(this.state);
656 } else if (op == UserOperation.MarkCommentAsRead) {
657 let data = wsJsonToRes<CommentResponse>(msg).data;
659 // If youre in the unread view, just remove it from the list
661 this.state.unreadOrAll == UnreadOrAll.Unread &&
662 data.comment_view.comment.read
664 this.state.replies = this.state.replies.filter(
665 r => r.comment.id !== data.comment_view.comment.id
667 this.state.combined = this.state.combined.filter(
668 r => r.id !== data.comment_view.comment.id
671 let found = this.state.replies.find(
672 c => c.comment.id == data.comment_view.comment.id
674 let combinedView = this.state.combined.find(
675 i => i.id == data.comment_view.comment.id
676 ).view as CommentView;
677 found.comment.read = combinedView.comment.read =
678 data.comment_view.comment.read;
681 this.sendUnreadCount(data.comment_view.comment.read);
682 this.setState(this.state);
684 } else if (op == UserOperation.MarkPersonMentionAsRead) {
685 let data = wsJsonToRes<PersonMentionResponse>(msg).data;
687 // TODO this might not be correct, it might need to use the comment id
688 let found = this.state.mentions.find(
689 c => c.person_mention.id == data.person_mention_view.person_mention.id
693 let combinedView = this.state.combined.find(
694 i => i.id == data.person_mention_view.person_mention.id
695 ).view as PersonMentionView;
696 found.comment.content = combinedView.comment.content =
697 data.person_mention_view.comment.content;
698 found.comment.updated = combinedView.comment.updated =
699 data.person_mention_view.comment.updated;
700 found.comment.removed = combinedView.comment.removed =
701 data.person_mention_view.comment.removed;
702 found.comment.deleted = combinedView.comment.deleted =
703 data.person_mention_view.comment.deleted;
704 found.counts.upvotes = combinedView.counts.upvotes =
705 data.person_mention_view.counts.upvotes;
706 found.counts.downvotes = combinedView.counts.downvotes =
707 data.person_mention_view.counts.downvotes;
708 found.counts.score = combinedView.counts.score =
709 data.person_mention_view.counts.score;
711 // If youre in the unread view, just remove it from the list
713 this.state.unreadOrAll == UnreadOrAll.Unread &&
714 data.person_mention_view.person_mention.read
716 this.state.mentions = this.state.mentions.filter(
718 r.person_mention.id !== data.person_mention_view.person_mention.id
720 this.state.combined = this.state.combined.filter(
721 r => r.id !== data.person_mention_view.person_mention.id
724 // TODO test to make sure these mentions are getting marked as read
725 found.person_mention.read = combinedView.person_mention.read =
726 data.person_mention_view.person_mention.read;
729 this.sendUnreadCount(data.person_mention_view.person_mention.read);
730 this.setState(this.state);
731 } else if (op == UserOperation.CreateComment) {
732 let data = wsJsonToRes<CommentResponse>(msg).data;
735 data.recipient_ids.includes(
736 UserService.Instance.myUserInfo.local_user_view.local_user.id
739 this.state.replies.unshift(data.comment_view);
740 this.state.combined.unshift(this.replyToReplyType(data.comment_view));
741 this.setState(this.state);
743 data.comment_view.creator.id ==
744 UserService.Instance.myUserInfo.local_user_view.person.id
746 // If youre in the unread view, just remove it from the list
747 if (this.state.unreadOrAll == UnreadOrAll.Unread) {
748 this.state.replies = this.state.replies.filter(
749 r => r.comment.id !== data.comment_view.comment.parent_id
751 this.state.mentions = this.state.mentions.filter(
752 m => m.comment.id !== data.comment_view.comment.parent_id
754 this.state.combined = this.state.combined.filter(r => {
755 if (this.isMention(r.view))
756 return r.view.comment.id !== data.comment_view.comment.parent_id;
757 else return r.id !== data.comment_view.comment.parent_id;
760 let mention_found = this.state.mentions.find(
761 i => i.comment.id == data.comment_view.comment.parent_id
764 mention_found.person_mention.read = true;
766 let reply_found = this.state.replies.find(
767 i => i.comment.id == data.comment_view.comment.parent_id
770 reply_found.comment.read = true;
772 this.state.combined = this.buildCombined();
774 this.sendUnreadCount(true);
775 this.setState(this.state);
777 // TODO this seems wrong, you should be using form_id
778 toast(i18n.t("reply_sent"));
780 } else if (op == UserOperation.CreatePrivateMessage) {
781 let data = wsJsonToRes<PrivateMessageResponse>(msg).data;
783 data.private_message_view.recipient.id ==
784 UserService.Instance.myUserInfo.local_user_view.person.id
786 this.state.messages.unshift(data.private_message_view);
787 this.state.combined.unshift(
788 this.messageToReplyType(data.private_message_view)
790 this.setState(this.state);
792 } else if (op == UserOperation.SaveComment) {
793 let data = wsJsonToRes<CommentResponse>(msg).data;
794 saveCommentRes(data.comment_view, this.state.replies);
795 this.setState(this.state);
797 } else if (op == UserOperation.CreateCommentLike) {
798 let data = wsJsonToRes<CommentResponse>(msg).data;
799 createCommentLikeRes(data.comment_view, this.state.replies);
800 this.setState(this.state);
801 } else if (op == UserOperation.BlockPerson) {
802 let data = wsJsonToRes<BlockPersonResponse>(msg).data;
803 updatePersonBlock(data);
804 } else if (op == UserOperation.CreatePostReport) {
805 let data = wsJsonToRes<PostReportResponse>(msg).data;
807 toast(i18n.t("report_created"));
809 } else if (op == UserOperation.CreateCommentReport) {
810 let data = wsJsonToRes<CommentReportResponse>(msg).data;
812 toast(i18n.t("report_created"));
817 isMention(view: any): view is PersonMentionView {
818 return (view as PersonMentionView).person_mention !== undefined;