]> Untitled Git - lemmy-ui.git/blob - src/shared/components/home/home.tsx
Merge branch 'main' into route-data-refactor
[lemmy-ui.git] / src / shared / components / home / home.tsx
1 import { NoOptionI18nKeys } from "i18next";
2 import { Component, linkEvent, MouseEventHandler } from "inferno";
3 import { T } from "inferno-i18next-dess";
4 import { Link } from "inferno-router";
5 import {
6   AddAdminResponse,
7   BanPersonResponse,
8   BlockPersonResponse,
9   CommentReportResponse,
10   CommentResponse,
11   CommentView,
12   CommunityView,
13   GetComments,
14   GetCommentsResponse,
15   GetPosts,
16   GetPostsResponse,
17   GetSiteResponse,
18   ListCommunities,
19   ListCommunitiesResponse,
20   ListingType,
21   PostReportResponse,
22   PostResponse,
23   PostView,
24   PurgeItemResponse,
25   SiteResponse,
26   SortType,
27   UserOperation,
28   wsJsonToRes,
29   wsUserOp,
30 } from "lemmy-js-client";
31 import { Subscription } from "rxjs";
32 import { i18n } from "../../i18next";
33 import {
34   CommentViewType,
35   DataType,
36   InitialFetchRequest,
37 } from "../../interfaces";
38 import { UserService, WebSocketService } from "../../services";
39 import {
40   canCreateCommunity,
41   commentsToFlatNodes,
42   createCommentLikeRes,
43   createPostLikeFindRes,
44   editCommentRes,
45   editPostFindRes,
46   enableDownvotes,
47   enableNsfw,
48   fetchLimit,
49   getDataTypeString,
50   getPageFromString,
51   getQueryParams,
52   getQueryString,
53   getRandomFromList,
54   isBrowser,
55   isPostBlocked,
56   mdToHtml,
57   myAuth,
58   notifyPost,
59   nsfwCheck,
60   postToCommentSortType,
61   QueryParams,
62   relTags,
63   restoreScrollPosition,
64   saveCommentRes,
65   saveScrollPosition,
66   setIsoData,
67   setupTippy,
68   showLocal,
69   toast,
70   trendingFetchLimit,
71   updatePersonBlock,
72   WithPromiseKeys,
73   wsClient,
74   wsSubscribe,
75 } from "../../utils";
76 import { CommentNodes } from "../comment/comment-nodes";
77 import { DataTypeSelect } from "../common/data-type-select";
78 import { HtmlTags } from "../common/html-tags";
79 import { Icon, Spinner } from "../common/icon";
80 import { ListingTypeSelect } from "../common/listing-type-select";
81 import { Paginator } from "../common/paginator";
82 import { SortSelect } from "../common/sort-select";
83 import { CommunityLink } from "../community/community-link";
84 import { PostListings } from "../post/post-listings";
85 import { SiteSidebar } from "./site-sidebar";
86
87 interface HomeState {
88   trendingCommunities: CommunityView[];
89   siteRes: GetSiteResponse;
90   posts: PostView[];
91   comments: CommentView[];
92   showSubscribedMobile: boolean;
93   showTrendingMobile: boolean;
94   showSidebarMobile: boolean;
95   subscribedCollapsed: boolean;
96   loading: boolean;
97   tagline?: string;
98 }
99
100 interface HomeProps {
101   listingType: ListingType;
102   dataType: DataType;
103   sort: SortType;
104   page: number;
105 }
106
107 interface HomeData {
108   postsResponse?: GetPostsResponse;
109   commentsResponse?: GetCommentsResponse;
110   trendingResponse: ListCommunitiesResponse;
111 }
112
113 function getDataTypeFromQuery(type?: string): DataType {
114   return type ? DataType[type] : DataType.Post;
115 }
116
117 function getListingTypeFromQuery(type?: string): ListingType {
118   const myListingType =
119     UserService.Instance.myUserInfo?.local_user_view?.local_user
120       ?.default_listing_type;
121
122   return type ? (type as ListingType) : myListingType ?? "Local";
123 }
124
125 function getSortTypeFromQuery(type?: string): SortType {
126   const mySortType =
127     UserService.Instance.myUserInfo?.local_user_view?.local_user
128       ?.default_sort_type;
129
130   return type ? (type as SortType) : mySortType ?? "Active";
131 }
132
133 const getHomeQueryParams = () =>
134   getQueryParams<HomeProps>({
135     sort: getSortTypeFromQuery,
136     listingType: getListingTypeFromQuery,
137     page: getPageFromString,
138     dataType: getDataTypeFromQuery,
139   });
140
141 function fetchTrendingCommunities() {
142   const listCommunitiesForm: ListCommunities = {
143     type_: "Local",
144     sort: "Hot",
145     limit: trendingFetchLimit,
146     auth: myAuth(false),
147   };
148   WebSocketService.Instance.send(wsClient.listCommunities(listCommunitiesForm));
149 }
150
151 function fetchData() {
152   const auth = myAuth(false);
153   const { dataType, page, listingType, sort } = getHomeQueryParams();
154   let req: string;
155
156   if (dataType === DataType.Post) {
157     const getPostsForm: GetPosts = {
158       page,
159       limit: fetchLimit,
160       sort,
161       saved_only: false,
162       type_: listingType,
163       auth,
164     };
165
166     req = wsClient.getPosts(getPostsForm);
167   } else {
168     const getCommentsForm: GetComments = {
169       page,
170       limit: fetchLimit,
171       sort: postToCommentSortType(sort),
172       saved_only: false,
173       type_: listingType,
174       auth,
175     };
176
177     req = wsClient.getComments(getCommentsForm);
178   }
179
180   WebSocketService.Instance.send(req);
181 }
182
183 const MobileButton = ({
184   textKey,
185   show,
186   onClick,
187 }: {
188   textKey: NoOptionI18nKeys;
189   show: boolean;
190   onClick: MouseEventHandler<HTMLButtonElement>;
191 }) => (
192   <button
193     className="btn btn-secondary d-inline-block mb-2 mr-3"
194     onClick={onClick}
195   >
196     {i18n.t(textKey)}{" "}
197     <Icon icon={show ? `minus-square` : `plus-square`} classes="icon-inline" />
198   </button>
199 );
200
201 const LinkButton = ({
202   path,
203   translationKey,
204 }: {
205   path: string;
206   translationKey: NoOptionI18nKeys;
207 }) => (
208   <Link className="btn btn-secondary btn-block" to={path}>
209     {i18n.t(translationKey)}
210   </Link>
211 );
212
213 function getRss(listingType: ListingType) {
214   const { sort } = getHomeQueryParams();
215   const auth = myAuth(false);
216
217   let rss: string | undefined = undefined;
218
219   switch (listingType) {
220     case "All": {
221       rss = `/feeds/all.xml?sort=${sort}`;
222       break;
223     }
224     case "Local": {
225       rss = `/feeds/local.xml?sort=${sort}`;
226       break;
227     }
228     case "Subscribed": {
229       rss = auth ? `/feeds/front/${auth}.xml?sort=${sort}` : undefined;
230       break;
231     }
232   }
233
234   return (
235     rss && (
236       <>
237         <a href={rss} rel={relTags} title="RSS">
238           <Icon icon="rss" classes="text-muted small" />
239         </a>
240         <link rel="alternate" type="application/atom+xml" href={rss} />
241       </>
242     )
243   );
244 }
245
246 export class Home extends Component<any, HomeState> {
247   private isoData = setIsoData<HomeData>(this.context);
248   private subscription?: Subscription;
249   state: HomeState = {
250     trendingCommunities: [],
251     siteRes: this.isoData.site_res,
252     showSubscribedMobile: false,
253     showTrendingMobile: false,
254     showSidebarMobile: false,
255     subscribedCollapsed: false,
256     loading: true,
257     posts: [],
258     comments: [],
259   };
260
261   constructor(props: any, context: any) {
262     super(props, context);
263
264     this.handleSortChange = this.handleSortChange.bind(this);
265     this.handleListingTypeChange = this.handleListingTypeChange.bind(this);
266     this.handleDataTypeChange = this.handleDataTypeChange.bind(this);
267     this.handlePageChange = this.handlePageChange.bind(this);
268
269     this.parseMessage = this.parseMessage.bind(this);
270     this.subscription = wsSubscribe(this.parseMessage);
271
272     // Only fetch the data if coming from another route
273     if (this.isoData.path === this.context.router.route.match.url) {
274       const { trendingResponse, commentsResponse, postsResponse } =
275         this.isoData.routeData;
276
277       if (postsResponse) {
278         this.state = { ...this.state, posts: postsResponse.posts };
279       }
280
281       if (commentsResponse) {
282         this.state = { ...this.state, comments: commentsResponse.comments };
283       }
284
285       if (isBrowser()) {
286         WebSocketService.Instance.send(
287           wsClient.communityJoin({ community_id: 0 })
288         );
289       }
290       const taglines = this.state?.siteRes?.taglines ?? [];
291       this.state = {
292         ...this.state,
293         trendingCommunities: trendingResponse?.communities ?? [],
294         loading: false,
295         tagline: getRandomFromList(taglines)?.content,
296       };
297     } else {
298       fetchTrendingCommunities();
299       fetchData();
300     }
301   }
302
303   componentDidMount() {
304     // This means it hasn't been set up yet
305     if (!this.state.siteRes.site_view.local_site.site_setup) {
306       this.context.router.history.push("/setup");
307     }
308     setupTippy();
309   }
310
311   componentWillUnmount() {
312     saveScrollPosition(this.context);
313     this.subscription?.unsubscribe();
314   }
315
316   static fetchInitialData({
317     client,
318     auth,
319     query: { dataType: urlDataType, listingType, page: urlPage, sort: urlSort },
320   }: InitialFetchRequest<QueryParams<HomeProps>>): WithPromiseKeys<HomeData> {
321     const dataType = getDataTypeFromQuery(urlDataType);
322
323     // TODO figure out auth default_listingType, default_sort_type
324     const type_ = getListingTypeFromQuery(listingType);
325     const sort = getSortTypeFromQuery(urlSort);
326
327     const page = urlPage ? Number(urlPage) : 1;
328
329     const promises: Promise<any>[] = [];
330
331     let postsResponse: Promise<GetPostsResponse> | undefined = undefined;
332     let commentsResponse: Promise<GetCommentsResponse> | undefined = undefined;
333
334     if (dataType === DataType.Post) {
335       const getPostsForm: GetPosts = {
336         type_,
337         page,
338         limit: fetchLimit,
339         sort,
340         saved_only: false,
341         auth,
342       };
343
344       postsResponse = client.getPosts(getPostsForm);
345     } else {
346       const getCommentsForm: GetComments = {
347         page,
348         limit: fetchLimit,
349         sort: postToCommentSortType(sort),
350         type_,
351         saved_only: false,
352         auth,
353       };
354
355       commentsResponse = client.getComments(getCommentsForm);
356     }
357
358     const trendingCommunitiesForm: ListCommunities = {
359       type_: "Local",
360       sort: "Hot",
361       limit: trendingFetchLimit,
362       auth,
363     };
364     promises.push(client.listCommunities(trendingCommunitiesForm));
365
366     return {
367       trendingResponse: client.listCommunities(trendingCommunitiesForm),
368       commentsResponse,
369       postsResponse,
370     };
371   }
372
373   get documentTitle(): string {
374     const { name, description } = this.state.siteRes.site_view.site;
375
376     return description ? `${name} - ${description}` : name;
377   }
378
379   render() {
380     const {
381       tagline,
382       siteRes: {
383         site_view: {
384           local_site: { site_setup },
385         },
386       },
387     } = this.state;
388
389     return (
390       <div className="container-lg">
391         <HtmlTags
392           title={this.documentTitle}
393           path={this.context.router.route.match.url}
394         />
395         {site_setup && (
396           <div className="row">
397             <main role="main" className="col-12 col-md-8">
398               {tagline && (
399                 <div
400                   id="tagline"
401                   dangerouslySetInnerHTML={mdToHtml(tagline)}
402                 ></div>
403               )}
404               <div className="d-block d-md-none">{this.mobileView}</div>
405               {this.posts()}
406             </main>
407             <aside className="d-none d-md-block col-md-4">
408               {this.mySidebar}
409             </aside>
410           </div>
411         )}
412       </div>
413     );
414   }
415
416   get hasFollows(): boolean {
417     const mui = UserService.Instance.myUserInfo;
418     return !!mui && mui.follows.length > 0;
419   }
420
421   get mobileView() {
422     const {
423       siteRes: {
424         site_view: { counts, site },
425         admins,
426         online,
427       },
428       showSubscribedMobile,
429       showTrendingMobile,
430       showSidebarMobile,
431     } = this.state;
432
433     return (
434       <div className="row">
435         <div className="col-12">
436           {this.hasFollows && (
437             <MobileButton
438               textKey="subscribed"
439               show={showSubscribedMobile}
440               onClick={linkEvent(this, this.handleShowSubscribedMobile)}
441             />
442           )}
443           <MobileButton
444             textKey="trending"
445             show={showTrendingMobile}
446             onClick={linkEvent(this, this.handleShowTrendingMobile)}
447           />
448           <MobileButton
449             textKey="sidebar"
450             show={showSidebarMobile}
451             onClick={linkEvent(this, this.handleShowSidebarMobile)}
452           />
453           {showSidebarMobile && (
454             <SiteSidebar
455               site={site}
456               admins={admins}
457               counts={counts}
458               online={online}
459               showLocal={showLocal(this.isoData)}
460             />
461           )}
462           {showTrendingMobile && (
463             <div className="col-12 card border-secondary mb-3">
464               <div className="card-body">{this.trendingCommunities(true)}</div>
465             </div>
466           )}
467           {showSubscribedMobile && (
468             <div className="col-12 card border-secondary mb-3">
469               <div className="card-body">{this.subscribedCommunities}</div>
470             </div>
471           )}
472         </div>
473       </div>
474     );
475   }
476
477   get mySidebar() {
478     const {
479       siteRes: {
480         site_view: { counts, site },
481         admins,
482         online,
483       },
484       loading,
485     } = this.state;
486
487     return (
488       <div>
489         {!loading && (
490           <div>
491             <div className="card border-secondary mb-3">
492               <div className="card-body">
493                 {this.trendingCommunities()}
494                 {canCreateCommunity(this.state.siteRes) && (
495                   <LinkButton
496                     path="/create_community"
497                     translationKey="create_a_community"
498                   />
499                 )}
500                 <LinkButton
501                   path="/communities"
502                   translationKey="explore_communities"
503                 />
504               </div>
505             </div>
506             <SiteSidebar
507               site={site}
508               admins={admins}
509               counts={counts}
510               online={online}
511               showLocal={showLocal(this.isoData)}
512             />
513             {this.hasFollows && (
514               <div className="card border-secondary mb-3">
515                 <div className="card-body">{this.subscribedCommunities}</div>
516               </div>
517             )}
518           </div>
519         )}
520       </div>
521     );
522   }
523
524   trendingCommunities(isMobile = false) {
525     return (
526       <div className={!isMobile ? "mb-2" : ""}>
527         <h5>
528           <T i18nKey="trending_communities">
529             #
530             <Link className="text-body" to="/communities">
531               #
532             </Link>
533           </T>
534         </h5>
535         <ul className="list-inline mb-0">
536           {this.state.trendingCommunities.map(cv => (
537             <li
538               key={cv.community.id}
539               className="list-inline-item d-inline-block"
540             >
541               <CommunityLink community={cv.community} />
542             </li>
543           ))}
544         </ul>
545       </div>
546     );
547   }
548
549   get subscribedCommunities() {
550     const { subscribedCollapsed } = this.state;
551
552     return (
553       <div>
554         <h5>
555           <T class="d-inline" i18nKey="subscribed_to_communities">
556             #
557             <Link className="text-body" to="/communities">
558               #
559             </Link>
560           </T>
561           <button
562             className="btn btn-sm text-muted"
563             onClick={linkEvent(this, this.handleCollapseSubscribe)}
564             aria-label={i18n.t("collapse")}
565             data-tippy-content={i18n.t("collapse")}
566           >
567             <Icon
568               icon={`${subscribedCollapsed ? "plus" : "minus"}-square`}
569               classes="icon-inline"
570             />
571           </button>
572         </h5>
573         {!subscribedCollapsed && (
574           <ul className="list-inline mb-0">
575             {UserService.Instance.myUserInfo?.follows.map(cfv => (
576               <li
577                 key={cfv.community.id}
578                 className="list-inline-item d-inline-block"
579               >
580                 <CommunityLink community={cfv.community} />
581               </li>
582             ))}
583           </ul>
584         )}
585       </div>
586     );
587   }
588
589   updateUrl({ dataType, listingType, page, sort }: Partial<HomeProps>) {
590     const {
591       dataType: urlDataType,
592       listingType: urlListingType,
593       page: urlPage,
594       sort: urlSort,
595     } = getHomeQueryParams();
596
597     const queryParams: QueryParams<HomeProps> = {
598       dataType: getDataTypeString(dataType ?? urlDataType),
599       listingType: listingType ?? urlListingType,
600       page: (page ?? urlPage).toString(),
601       sort: sort ?? urlSort,
602     };
603
604     this.props.history.push({
605       pathname: "/",
606       search: getQueryString(queryParams),
607     });
608
609     this.setState({
610       loading: true,
611       posts: [],
612       comments: [],
613     });
614
615     fetchData();
616   }
617
618   posts() {
619     const { page } = getHomeQueryParams();
620
621     return (
622       <div className="main-content-wrapper">
623         {this.state.loading ? (
624           <h5>
625             <Spinner large />
626           </h5>
627         ) : (
628           <div>
629             {this.selects()}
630             {this.listings}
631             <Paginator page={page} onChange={this.handlePageChange} />
632           </div>
633         )}
634       </div>
635     );
636   }
637
638   get listings() {
639     const { dataType } = getHomeQueryParams();
640     const { siteRes, posts, comments } = this.state;
641
642     return dataType === DataType.Post ? (
643       <PostListings
644         posts={posts}
645         showCommunity
646         removeDuplicates
647         enableDownvotes={enableDownvotes(siteRes)}
648         enableNsfw={enableNsfw(siteRes)}
649         allLanguages={siteRes.all_languages}
650         siteLanguages={siteRes.discussion_languages}
651       />
652     ) : (
653       <CommentNodes
654         nodes={commentsToFlatNodes(comments)}
655         viewType={CommentViewType.Flat}
656         noIndent
657         showCommunity
658         showContext
659         enableDownvotes={enableDownvotes(siteRes)}
660         allLanguages={siteRes.all_languages}
661         siteLanguages={siteRes.discussion_languages}
662       />
663     );
664   }
665
666   selects() {
667     const { listingType, dataType, sort } = getHomeQueryParams();
668
669     return (
670       <div className="mb-3">
671         <span className="mr-3">
672           <DataTypeSelect
673             type_={dataType}
674             onChange={this.handleDataTypeChange}
675           />
676         </span>
677         <span className="mr-3">
678           <ListingTypeSelect
679             type_={listingType}
680             showLocal={showLocal(this.isoData)}
681             showSubscribed
682             onChange={this.handleListingTypeChange}
683           />
684         </span>
685         <span className="mr-2">
686           <SortSelect sort={sort} onChange={this.handleSortChange} />
687         </span>
688         {getRss(listingType)}
689       </div>
690     );
691   }
692
693   handleShowSubscribedMobile(i: Home) {
694     i.setState({ showSubscribedMobile: !i.state.showSubscribedMobile });
695   }
696
697   handleShowTrendingMobile(i: Home) {
698     i.setState({ showTrendingMobile: !i.state.showTrendingMobile });
699   }
700
701   handleShowSidebarMobile(i: Home) {
702     i.setState({ showSidebarMobile: !i.state.showSidebarMobile });
703   }
704
705   handleCollapseSubscribe(i: Home) {
706     i.setState({ subscribedCollapsed: !i.state.subscribedCollapsed });
707   }
708
709   handlePageChange(page: number) {
710     this.updateUrl({ page });
711     window.scrollTo(0, 0);
712   }
713
714   handleSortChange(val: SortType) {
715     this.updateUrl({ sort: val, page: 1 });
716     window.scrollTo(0, 0);
717   }
718
719   handleListingTypeChange(val: ListingType) {
720     this.updateUrl({ listingType: val, page: 1 });
721     window.scrollTo(0, 0);
722   }
723
724   handleDataTypeChange(val: DataType) {
725     this.updateUrl({ dataType: val, page: 1 });
726     window.scrollTo(0, 0);
727   }
728
729   parseMessage(msg: any) {
730     const op = wsUserOp(msg);
731     console.log(msg);
732
733     if (msg.error) {
734       toast(i18n.t(msg.error), "danger");
735     } else if (msg.reconnect) {
736       WebSocketService.Instance.send(
737         wsClient.communityJoin({ community_id: 0 })
738       );
739       fetchData();
740     } else {
741       switch (op) {
742         case UserOperation.ListCommunities: {
743           const { communities } = wsJsonToRes<ListCommunitiesResponse>(msg);
744           this.setState({ trendingCommunities: communities });
745
746           break;
747         }
748
749         case UserOperation.EditSite: {
750           const { site_view } = wsJsonToRes<SiteResponse>(msg);
751           this.setState(s => ((s.siteRes.site_view = site_view), s));
752           toast(i18n.t("site_saved"));
753
754           break;
755         }
756
757         case UserOperation.GetPosts: {
758           const { posts } = wsJsonToRes<GetPostsResponse>(msg);
759           this.setState({ posts, loading: false });
760           WebSocketService.Instance.send(
761             wsClient.communityJoin({ community_id: 0 })
762           );
763           restoreScrollPosition(this.context);
764           setupTippy();
765
766           break;
767         }
768
769         case UserOperation.CreatePost: {
770           const { page, listingType } = getHomeQueryParams();
771           const { post_view } = wsJsonToRes<PostResponse>(msg);
772
773           // Only push these if you're on the first page, you pass the nsfw check, and it isn't blocked
774           if (page === 1 && nsfwCheck(post_view) && !isPostBlocked(post_view)) {
775             const mui = UserService.Instance.myUserInfo;
776             const showPostNotifs =
777               mui?.local_user_view.local_user.show_new_post_notifs;
778             let shouldAddPost: boolean;
779
780             switch (listingType) {
781               case "Subscribed": {
782                 // If you're on subscribed, only push it if you're subscribed.
783                 shouldAddPost = !!mui?.follows.some(
784                   ({ community: { id } }) => id === post_view.community.id
785                 );
786                 break;
787               }
788               case "Local": {
789                 // If you're on the local view, only push it if its local
790                 shouldAddPost = post_view.post.local;
791                 break;
792               }
793               default: {
794                 shouldAddPost = true;
795                 break;
796               }
797             }
798
799             if (shouldAddPost) {
800               this.setState(({ posts }) => ({
801                 posts: [post_view].concat(posts),
802               }));
803               if (showPostNotifs) {
804                 notifyPost(post_view, this.context.router);
805               }
806             }
807           }
808
809           break;
810         }
811
812         case UserOperation.EditPost:
813         case UserOperation.DeletePost:
814         case UserOperation.RemovePost:
815         case UserOperation.LockPost:
816         case UserOperation.FeaturePost:
817         case UserOperation.SavePost: {
818           const { post_view } = wsJsonToRes<PostResponse>(msg);
819           editPostFindRes(post_view, this.state.posts);
820           this.setState(this.state);
821
822           break;
823         }
824
825         case UserOperation.CreatePostLike: {
826           const { post_view } = wsJsonToRes<PostResponse>(msg);
827           createPostLikeFindRes(post_view, this.state.posts);
828           this.setState(this.state);
829
830           break;
831         }
832
833         case UserOperation.AddAdmin: {
834           const { admins } = wsJsonToRes<AddAdminResponse>(msg);
835           this.setState(s => ((s.siteRes.admins = admins), s));
836
837           break;
838         }
839
840         case UserOperation.BanPerson: {
841           const {
842             banned,
843             person_view: {
844               person: { id },
845             },
846           } = wsJsonToRes<BanPersonResponse>(msg);
847
848           this.state.posts
849             .filter(p => p.creator.id == id)
850             .forEach(p => (p.creator.banned = banned));
851           this.setState(this.state);
852
853           break;
854         }
855
856         case UserOperation.GetComments: {
857           const { comments } = wsJsonToRes<GetCommentsResponse>(msg);
858           this.setState({ comments, loading: false });
859
860           break;
861         }
862
863         case UserOperation.EditComment:
864         case UserOperation.DeleteComment:
865         case UserOperation.RemoveComment: {
866           const { comment_view } = wsJsonToRes<CommentResponse>(msg);
867           editCommentRes(comment_view, this.state.comments);
868           this.setState(this.state);
869
870           break;
871         }
872
873         case UserOperation.CreateComment: {
874           const { form_id, comment_view } = wsJsonToRes<CommentResponse>(msg);
875
876           // Necessary since it might be a user reply
877           if (form_id) {
878             const { listingType } = getHomeQueryParams();
879
880             // If you're on subscribed, only push it if you're subscribed.
881             const shouldAddComment =
882               listingType === "Subscribed"
883                 ? UserService.Instance.myUserInfo?.follows.some(
884                     ({ community: { id } }) => id === comment_view.community.id
885                   )
886                 : true;
887
888             if (shouldAddComment) {
889               this.setState(({ comments }) => ({
890                 comments: [comment_view].concat(comments),
891               }));
892             }
893           }
894
895           break;
896         }
897
898         case UserOperation.SaveComment: {
899           const { comment_view } = wsJsonToRes<CommentResponse>(msg);
900           saveCommentRes(comment_view, this.state.comments);
901           this.setState(this.state);
902
903           break;
904         }
905
906         case UserOperation.CreateCommentLike: {
907           const { comment_view } = wsJsonToRes<CommentResponse>(msg);
908           createCommentLikeRes(comment_view, this.state.comments);
909           this.setState(this.state);
910
911           break;
912         }
913
914         case UserOperation.BlockPerson: {
915           const data = wsJsonToRes<BlockPersonResponse>(msg);
916           updatePersonBlock(data);
917
918           break;
919         }
920
921         case UserOperation.CreatePostReport: {
922           const data = wsJsonToRes<PostReportResponse>(msg);
923           if (data) {
924             toast(i18n.t("report_created"));
925           }
926
927           break;
928         }
929
930         case UserOperation.CreateCommentReport: {
931           const data = wsJsonToRes<CommentReportResponse>(msg);
932           if (data) {
933             toast(i18n.t("report_created"));
934           }
935
936           break;
937         }
938
939         case UserOperation.PurgePerson:
940         case UserOperation.PurgePost:
941         case UserOperation.PurgeComment:
942         case UserOperation.PurgeCommunity: {
943           const data = wsJsonToRes<PurgeItemResponse>(msg);
944           if (data.success) {
945             toast(i18n.t("purge_success"));
946             this.context.router.history.push(`/`);
947           }
948
949           break;
950         }
951       }
952     }
953   }
954 }