]> Untitled Git - lemmy-ui.git/blob - src/shared/components/community/community.tsx
Merge branch 'main' into route-data-refactor
[lemmy-ui.git] / src / shared / components / community / community.tsx
1 import { Component, linkEvent } from "inferno";
2 import { RouteComponentProps } from "inferno-router/dist/Route";
3 import {
4   AddAdmin,
5   AddModToCommunity,
6   AddModToCommunityResponse,
7   BanFromCommunity,
8   BanFromCommunityResponse,
9   BanPerson,
10   BanPersonResponse,
11   BlockCommunity,
12   BlockPerson,
13   CommentId,
14   CommentReplyResponse,
15   CommentResponse,
16   CommunityResponse,
17   CreateComment,
18   CreateCommentLike,
19   CreateCommentReport,
20   CreatePostLike,
21   CreatePostReport,
22   DeleteComment,
23   DeleteCommunity,
24   DeletePost,
25   DistinguishComment,
26   EditComment,
27   EditCommunity,
28   EditPost,
29   FeaturePost,
30   FollowCommunity,
31   GetComments,
32   GetCommentsResponse,
33   GetCommunity,
34   GetCommunityResponse,
35   GetPosts,
36   GetPostsResponse,
37   GetSiteResponse,
38   LockPost,
39   MarkCommentReplyAsRead,
40   MarkPersonMentionAsRead,
41   PostResponse,
42   PurgeComment,
43   PurgeCommunity,
44   PurgeItemResponse,
45   PurgePerson,
46   PurgePost,
47   RemoveComment,
48   RemoveCommunity,
49   RemovePost,
50   SaveComment,
51   SavePost,
52   SortType,
53   TransferCommunity,
54 } from "lemmy-js-client";
55 import { i18n } from "../../i18next";
56 import {
57   CommentViewType,
58   DataType,
59   InitialFetchRequest,
60 } from "../../interfaces";
61 import { UserService } from "../../services";
62 import { FirstLoadService } from "../../services/FirstLoadService";
63 import { HttpService, RequestState } from "../../services/HttpService";
64 import {
65   QueryParams,
66   RouteDataResponse,
67   commentsToFlatNodes,
68   communityRSSUrl,
69   editComment,
70   editPost,
71   editWith,
72   enableDownvotes,
73   enableNsfw,
74   fetchLimit,
75   getCommentParentId,
76   getDataTypeString,
77   getPageFromString,
78   getQueryParams,
79   getQueryString,
80   myAuth,
81   postToCommentSortType,
82   relTags,
83   restoreScrollPosition,
84   saveScrollPosition,
85   setIsoData,
86   setupTippy,
87   showLocal,
88   toast,
89   updateCommunityBlock,
90   updatePersonBlock,
91 } from "../../utils";
92 import { CommentNodes } from "../comment/comment-nodes";
93 import { BannerIconHeader } from "../common/banner-icon-header";
94 import { DataTypeSelect } from "../common/data-type-select";
95 import { HtmlTags } from "../common/html-tags";
96 import { Icon, Spinner } from "../common/icon";
97 import { Paginator } from "../common/paginator";
98 import { SortSelect } from "../common/sort-select";
99 import { Sidebar } from "../community/sidebar";
100 import { SiteSidebar } from "../home/site-sidebar";
101 import { PostListings } from "../post/post-listings";
102 import { CommunityLink } from "./community-link";
103
104 type CommunityData = RouteDataResponse<{
105   communityResponse: GetCommunityResponse;
106   postsResponse?: GetPostsResponse;
107   commentsResponse?: GetCommentsResponse;
108 }>;
109
110 interface State {
111   communityRes: RequestState<GetCommunityResponse>;
112   postsRes: RequestState<GetPostsResponse>;
113   commentsRes: RequestState<GetCommentsResponse>;
114   siteRes: GetSiteResponse;
115   showSidebarMobile: boolean;
116   finished: Map<CommentId, boolean | undefined>;
117   isIsomorphic: boolean;
118 }
119
120 interface CommunityProps {
121   dataType: DataType;
122   sort: SortType;
123   page: number;
124 }
125
126 function getCommunityQueryParams() {
127   return getQueryParams<CommunityProps>({
128     dataType: getDataTypeFromQuery,
129     page: getPageFromString,
130     sort: getSortTypeFromQuery,
131   });
132 }
133
134 function getDataTypeFromQuery(type?: string): DataType {
135   return type ? DataType[type] : DataType.Post;
136 }
137
138 function getSortTypeFromQuery(type?: string): SortType {
139   const mySortType =
140     UserService.Instance.myUserInfo?.local_user_view.local_user
141       .default_sort_type;
142
143   return type ? (type as SortType) : mySortType ?? "Active";
144 }
145
146 export class Community extends Component<
147   RouteComponentProps<{ name: string }>,
148   State
149 > {
150   private isoData = setIsoData<CommunityData>(this.context);
151   state: State = {
152     communityRes: { state: "empty" },
153     postsRes: { state: "empty" },
154     commentsRes: { state: "empty" },
155     siteRes: this.isoData.site_res,
156     showSidebarMobile: false,
157     finished: new Map(),
158     isIsomorphic: false,
159   };
160
161   constructor(props: RouteComponentProps<{ name: string }>, context: any) {
162     super(props, context);
163
164     this.handleSortChange = this.handleSortChange.bind(this);
165     this.handleDataTypeChange = this.handleDataTypeChange.bind(this);
166     this.handlePageChange = this.handlePageChange.bind(this);
167
168     // All of the action binds
169     this.handleDeleteCommunity = this.handleDeleteCommunity.bind(this);
170     this.handleEditCommunity = this.handleEditCommunity.bind(this);
171     this.handleFollow = this.handleFollow.bind(this);
172     this.handleRemoveCommunity = this.handleRemoveCommunity.bind(this);
173     this.handleCreateComment = this.handleCreateComment.bind(this);
174     this.handleEditComment = this.handleEditComment.bind(this);
175     this.handleSaveComment = this.handleSaveComment.bind(this);
176     this.handleBlockCommunity = this.handleBlockCommunity.bind(this);
177     this.handleBlockPerson = this.handleBlockPerson.bind(this);
178     this.handleDeleteComment = this.handleDeleteComment.bind(this);
179     this.handleRemoveComment = this.handleRemoveComment.bind(this);
180     this.handleCommentVote = this.handleCommentVote.bind(this);
181     this.handleAddModToCommunity = this.handleAddModToCommunity.bind(this);
182     this.handleAddAdmin = this.handleAddAdmin.bind(this);
183     this.handlePurgePerson = this.handlePurgePerson.bind(this);
184     this.handlePurgeComment = this.handlePurgeComment.bind(this);
185     this.handleCommentReport = this.handleCommentReport.bind(this);
186     this.handleDistinguishComment = this.handleDistinguishComment.bind(this);
187     this.handleTransferCommunity = this.handleTransferCommunity.bind(this);
188     this.handleCommentReplyRead = this.handleCommentReplyRead.bind(this);
189     this.handlePersonMentionRead = this.handlePersonMentionRead.bind(this);
190     this.handleBanFromCommunity = this.handleBanFromCommunity.bind(this);
191     this.handleBanPerson = this.handleBanPerson.bind(this);
192     this.handlePostVote = this.handlePostVote.bind(this);
193     this.handlePostEdit = this.handlePostEdit.bind(this);
194     this.handlePostReport = this.handlePostReport.bind(this);
195     this.handleLockPost = this.handleLockPost.bind(this);
196     this.handleDeletePost = this.handleDeletePost.bind(this);
197     this.handleRemovePost = this.handleRemovePost.bind(this);
198     this.handleSavePost = this.handleSavePost.bind(this);
199     this.handlePurgePost = this.handlePurgePost.bind(this);
200     this.handleFeaturePost = this.handleFeaturePost.bind(this);
201
202     // Only fetch the data if coming from another route
203     if (FirstLoadService.isFirstLoad) {
204       const {
205         communityResponse: communityRes,
206         commentsResponse: commentsRes,
207         postsResponse: postsRes,
208       } = this.isoData.routeData;
209
210       this.state = {
211         ...this.state,
212         isIsomorphic: true,
213       };
214
215       if (communityRes.state === "success") {
216         this.state = {
217           ...this.state,
218           communityRes,
219         };
220       }
221
222       if (postsRes?.state === "success") {
223         this.state = {
224           ...this.state,
225           postsRes,
226         };
227       }
228
229       if (commentsRes?.state === "success") {
230         this.state = {
231           ...this.state,
232           commentsRes,
233         };
234       }
235     }
236   }
237
238   async fetchCommunity() {
239     this.setState({ communityRes: { state: "loading" } });
240     this.setState({
241       communityRes: await HttpService.client.getCommunity({
242         name: this.props.match.params.name,
243         auth: myAuth(),
244       }),
245     });
246   }
247
248   async componentDidMount() {
249     if (!this.state.isIsomorphic) {
250       await Promise.all([this.fetchCommunity(), this.fetchData()]);
251     }
252
253     setupTippy();
254   }
255
256   componentWillUnmount() {
257     saveScrollPosition(this.context);
258   }
259
260   static async fetchInitialData({
261     client,
262     path,
263     query: { dataType: urlDataType, page: urlPage, sort: urlSort },
264     auth,
265   }: InitialFetchRequest<QueryParams<CommunityProps>>): Promise<
266     Promise<CommunityData>
267   > {
268     const pathSplit = path.split("/");
269
270     const communityName = pathSplit[2];
271     const communityForm: GetCommunity = {
272       name: communityName,
273       auth,
274     };
275
276     const dataType = getDataTypeFromQuery(urlDataType);
277
278     const sort = getSortTypeFromQuery(urlSort);
279
280     const page = getPageFromString(urlPage);
281
282     let postsResponse: RequestState<GetPostsResponse> | undefined = undefined;
283     let commentsResponse: RequestState<GetCommentsResponse> | undefined =
284       undefined;
285
286     if (dataType === DataType.Post) {
287       const getPostsForm: GetPosts = {
288         community_name: communityName,
289         page,
290         limit: fetchLimit,
291         sort,
292         type_: "All",
293         saved_only: false,
294         auth,
295       };
296
297       postsResponse = await client.getPosts(getPostsForm);
298     } else {
299       const getCommentsForm: GetComments = {
300         community_name: communityName,
301         page,
302         limit: fetchLimit,
303         sort: postToCommentSortType(sort),
304         type_: "All",
305         saved_only: false,
306         auth,
307       };
308
309       commentsResponse = await client.getComments(getCommentsForm);
310     }
311
312     return {
313       communityResponse: await client.getCommunity(communityForm),
314       commentsResponse,
315       postsResponse,
316     };
317   }
318
319   get documentTitle(): string {
320     const cRes = this.state.communityRes;
321     return cRes.state == "success"
322       ? `${cRes.data.community_view.community.title} - ${this.isoData.site_res.site_view.site.name}`
323       : "";
324   }
325
326   renderCommunity() {
327     switch (this.state.communityRes.state) {
328       case "loading":
329         return (
330           <h5>
331             <Spinner large />
332           </h5>
333         );
334       case "success": {
335         const res = this.state.communityRes.data;
336         const { page } = getCommunityQueryParams();
337
338         return (
339           <>
340             <HtmlTags
341               title={this.documentTitle}
342               path={this.context.router.route.match.url}
343               description={res.community_view.community.description}
344               image={res.community_view.community.icon}
345             />
346
347             <div className="row">
348               <div className="col-12 col-md-8">
349                 {this.communityInfo(res)}
350                 <div className="d-block d-md-none">
351                   <button
352                     className="btn btn-secondary d-inline-block mb-2 mr-3"
353                     onClick={linkEvent(this, this.handleShowSidebarMobile)}
354                   >
355                     {i18n.t("sidebar")}{" "}
356                     <Icon
357                       icon={
358                         this.state.showSidebarMobile
359                           ? `minus-square`
360                           : `plus-square`
361                       }
362                       classes="icon-inline"
363                     />
364                   </button>
365                   {this.state.showSidebarMobile && this.sidebar(res)}
366                 </div>
367                 {this.selects(res)}
368                 {this.listings(res)}
369                 <Paginator page={page} onChange={this.handlePageChange} />
370               </div>
371               <div className="d-none d-md-block col-md-4">
372                 {this.sidebar(res)}
373               </div>
374             </div>
375           </>
376         );
377       }
378     }
379   }
380
381   render() {
382     return <div className="container-lg">{this.renderCommunity()}</div>;
383   }
384
385   sidebar(res: GetCommunityResponse) {
386     const { site_res } = this.isoData;
387     // For some reason, this returns an empty vec if it matches the site langs
388     const communityLangs =
389       res.discussion_languages.length === 0
390         ? site_res.all_languages.map(({ id }) => id)
391         : res.discussion_languages;
392
393     return (
394       <>
395         <Sidebar
396           community_view={res.community_view}
397           moderators={res.moderators}
398           admins={site_res.admins}
399           online={res.online}
400           enableNsfw={enableNsfw(site_res)}
401           editable
402           allLanguages={site_res.all_languages}
403           siteLanguages={site_res.discussion_languages}
404           communityLanguages={communityLangs}
405           onDeleteCommunity={this.handleDeleteCommunity}
406           onRemoveCommunity={this.handleRemoveCommunity}
407           onLeaveModTeam={this.handleAddModToCommunity}
408           onFollowCommunity={this.handleFollow}
409           onBlockCommunity={this.handleBlockCommunity}
410           onPurgeCommunity={this.handlePurgeCommunity}
411           onEditCommunity={this.handleEditCommunity}
412         />
413         {!res.community_view.community.local && res.site && (
414           <SiteSidebar site={res.site} showLocal={showLocal(this.isoData)} />
415         )}
416       </>
417     );
418   }
419
420   listings(communityRes: GetCommunityResponse) {
421     const { dataType } = getCommunityQueryParams();
422     const { site_res } = this.isoData;
423
424     if (dataType === DataType.Post) {
425       switch (this.state.postsRes.state) {
426         case "loading":
427           return (
428             <h5>
429               <Spinner large />
430             </h5>
431           );
432         case "success":
433           return (
434             <PostListings
435               posts={this.state.postsRes.data.posts}
436               removeDuplicates
437               enableDownvotes={enableDownvotes(site_res)}
438               enableNsfw={enableNsfw(site_res)}
439               allLanguages={site_res.all_languages}
440               siteLanguages={site_res.discussion_languages}
441               onBlockPerson={this.handleBlockPerson}
442               onPostEdit={this.handlePostEdit}
443               onPostVote={this.handlePostVote}
444               onPostReport={this.handlePostReport}
445               onLockPost={this.handleLockPost}
446               onDeletePost={this.handleDeletePost}
447               onRemovePost={this.handleRemovePost}
448               onSavePost={this.handleSavePost}
449               onPurgePerson={this.handlePurgePerson}
450               onPurgePost={this.handlePurgePost}
451               onBanPerson={this.handleBanPerson}
452               onBanPersonFromCommunity={this.handleBanFromCommunity}
453               onAddModToCommunity={this.handleAddModToCommunity}
454               onAddAdmin={this.handleAddAdmin}
455               onTransferCommunity={this.handleTransferCommunity}
456               onFeaturePost={this.handleFeaturePost}
457             />
458           );
459       }
460     } else {
461       switch (this.state.commentsRes.state) {
462         case "loading":
463           return (
464             <h5>
465               <Spinner large />
466             </h5>
467           );
468         case "success":
469           return (
470             <CommentNodes
471               nodes={commentsToFlatNodes(this.state.commentsRes.data.comments)}
472               viewType={CommentViewType.Flat}
473               finished={this.state.finished}
474               noIndent
475               showContext
476               enableDownvotes={enableDownvotes(site_res)}
477               moderators={communityRes.moderators}
478               admins={site_res.admins}
479               allLanguages={site_res.all_languages}
480               siteLanguages={site_res.discussion_languages}
481               onSaveComment={this.handleSaveComment}
482               onBlockPerson={this.handleBlockPerson}
483               onDeleteComment={this.handleDeleteComment}
484               onRemoveComment={this.handleRemoveComment}
485               onCommentVote={this.handleCommentVote}
486               onCommentReport={this.handleCommentReport}
487               onDistinguishComment={this.handleDistinguishComment}
488               onAddModToCommunity={this.handleAddModToCommunity}
489               onAddAdmin={this.handleAddAdmin}
490               onTransferCommunity={this.handleTransferCommunity}
491               onPurgeComment={this.handlePurgeComment}
492               onPurgePerson={this.handlePurgePerson}
493               onCommentReplyRead={this.handleCommentReplyRead}
494               onPersonMentionRead={this.handlePersonMentionRead}
495               onBanPersonFromCommunity={this.handleBanFromCommunity}
496               onBanPerson={this.handleBanPerson}
497               onCreateComment={this.handleCreateComment}
498               onEditComment={this.handleEditComment}
499             />
500           );
501       }
502     }
503   }
504
505   communityInfo(res: GetCommunityResponse) {
506     const community = res.community_view.community;
507
508     return (
509       community && (
510         <div className="mb-2">
511           <BannerIconHeader banner={community.banner} icon={community.icon} />
512           <h5 className="mb-0 overflow-wrap-anywhere">{community.title}</h5>
513           <CommunityLink
514             community={community}
515             realLink
516             useApubName
517             muted
518             hideAvatar
519           />
520         </div>
521       )
522     );
523   }
524
525   selects(res: GetCommunityResponse) {
526     // let communityRss = this.state.communityRes.map(r =>
527     //   communityRSSUrl(r.community_view.community.actor_id, this.state.sort)
528     // );
529     const { dataType, sort } = getCommunityQueryParams();
530     const communityRss = res
531       ? communityRSSUrl(res.community_view.community.actor_id, sort)
532       : undefined;
533
534     return (
535       <div className="mb-3">
536         <span className="mr-3">
537           <DataTypeSelect
538             type_={dataType}
539             onChange={this.handleDataTypeChange}
540           />
541         </span>
542         <span className="mr-2">
543           <SortSelect sort={sort} onChange={this.handleSortChange} />
544         </span>
545         {communityRss && (
546           <>
547             <a href={communityRss} title="RSS" rel={relTags}>
548               <Icon icon="rss" classes="text-muted small" />
549             </a>
550             <link
551               rel="alternate"
552               type="application/atom+xml"
553               href={communityRss}
554             />
555           </>
556         )}
557       </div>
558     );
559   }
560
561   handlePageChange(page: number) {
562     this.updateUrl({ page });
563     window.scrollTo(0, 0);
564   }
565
566   handleSortChange(sort: SortType) {
567     this.updateUrl({ sort, page: 1 });
568     window.scrollTo(0, 0);
569   }
570
571   handleDataTypeChange(dataType: DataType) {
572     this.updateUrl({ dataType, page: 1 });
573     window.scrollTo(0, 0);
574   }
575
576   handleShowSidebarMobile(i: Community) {
577     i.setState(({ showSidebarMobile }) => ({
578       showSidebarMobile: !showSidebarMobile,
579     }));
580   }
581
582   async updateUrl({ dataType, page, sort }: Partial<CommunityProps>) {
583     const {
584       dataType: urlDataType,
585       page: urlPage,
586       sort: urlSort,
587     } = getCommunityQueryParams();
588
589     const queryParams: QueryParams<CommunityProps> = {
590       dataType: getDataTypeString(dataType ?? urlDataType),
591       page: (page ?? urlPage).toString(),
592       sort: sort ?? urlSort,
593     };
594
595     this.props.history.push(
596       `/c/${this.props.match.params.name}${getQueryString(queryParams)}`
597     );
598
599     await this.fetchData();
600   }
601
602   async fetchData() {
603     const { dataType, page, sort } = getCommunityQueryParams();
604     const { name } = this.props.match.params;
605
606     if (dataType === DataType.Post) {
607       this.setState({ postsRes: { state: "loading" } });
608       this.setState({
609         postsRes: await HttpService.client.getPosts({
610           page,
611           limit: fetchLimit,
612           sort,
613           type_: "All",
614           community_name: name,
615           saved_only: false,
616           auth: myAuth(),
617         }),
618       });
619     } else {
620       this.setState({ commentsRes: { state: "loading" } });
621       this.setState({
622         commentsRes: await HttpService.client.getComments({
623           page,
624           limit: fetchLimit,
625           sort: postToCommentSortType(sort),
626           type_: "All",
627           community_name: name,
628           saved_only: false,
629           auth: myAuth(),
630         }),
631       });
632     }
633
634     restoreScrollPosition(this.context);
635     setupTippy();
636   }
637
638   async handleDeleteCommunity(form: DeleteCommunity) {
639     const deleteCommunityRes = await HttpService.client.deleteCommunity(form);
640     this.updateCommunity(deleteCommunityRes);
641   }
642
643   async handleAddModToCommunity(form: AddModToCommunity) {
644     const addModRes = await HttpService.client.addModToCommunity(form);
645     this.updateModerators(addModRes);
646   }
647
648   async handleFollow(form: FollowCommunity) {
649     const followCommunityRes = await HttpService.client.followCommunity(form);
650     this.updateCommunity(followCommunityRes);
651
652     // Update myUserInfo
653     if (followCommunityRes.state == "success") {
654       const communityId = followCommunityRes.data.community_view.community.id;
655       const mui = UserService.Instance.myUserInfo;
656       if (mui) {
657         mui.follows = mui.follows.filter(i => i.community.id != communityId);
658       }
659     }
660   }
661
662   async handlePurgeCommunity(form: PurgeCommunity) {
663     const purgeCommunityRes = await HttpService.client.purgeCommunity(form);
664     this.purgeItem(purgeCommunityRes);
665   }
666
667   async handlePurgePerson(form: PurgePerson) {
668     const purgePersonRes = await HttpService.client.purgePerson(form);
669     this.purgeItem(purgePersonRes);
670   }
671
672   async handlePurgeComment(form: PurgeComment) {
673     const purgeCommentRes = await HttpService.client.purgeComment(form);
674     this.purgeItem(purgeCommentRes);
675   }
676
677   async handlePurgePost(form: PurgePost) {
678     const purgeRes = await HttpService.client.purgePost(form);
679     this.purgeItem(purgeRes);
680   }
681
682   async handleBlockCommunity(form: BlockCommunity) {
683     const blockCommunityRes = await HttpService.client.blockCommunity(form);
684     if (blockCommunityRes.state == "success") {
685       updateCommunityBlock(blockCommunityRes.data);
686     }
687   }
688
689   async handleBlockPerson(form: BlockPerson) {
690     const blockPersonRes = await HttpService.client.blockPerson(form);
691     if (blockPersonRes.state == "success") {
692       updatePersonBlock(blockPersonRes.data);
693     }
694   }
695
696   async handleRemoveCommunity(form: RemoveCommunity) {
697     const removeCommunityRes = await HttpService.client.removeCommunity(form);
698     this.updateCommunity(removeCommunityRes);
699   }
700
701   async handleEditCommunity(form: EditCommunity) {
702     const res = await HttpService.client.editCommunity(form);
703     this.updateCommunity(res);
704
705     return res;
706   }
707
708   async handleCreateComment(form: CreateComment) {
709     const createCommentRes = await HttpService.client.createComment(form);
710     this.createAndUpdateComments(createCommentRes);
711
712     return createCommentRes;
713   }
714
715   async handleEditComment(form: EditComment) {
716     const editCommentRes = await HttpService.client.editComment(form);
717     this.findAndUpdateComment(editCommentRes);
718
719     return editCommentRes;
720   }
721
722   async handleDeleteComment(form: DeleteComment) {
723     const deleteCommentRes = await HttpService.client.deleteComment(form);
724     this.findAndUpdateComment(deleteCommentRes);
725   }
726
727   async handleDeletePost(form: DeletePost) {
728     const deleteRes = await HttpService.client.deletePost(form);
729     this.findAndUpdatePost(deleteRes);
730   }
731
732   async handleRemovePost(form: RemovePost) {
733     const removeRes = await HttpService.client.removePost(form);
734     this.findAndUpdatePost(removeRes);
735   }
736
737   async handleRemoveComment(form: RemoveComment) {
738     const removeCommentRes = await HttpService.client.removeComment(form);
739     this.findAndUpdateComment(removeCommentRes);
740   }
741
742   async handleSaveComment(form: SaveComment) {
743     const saveCommentRes = await HttpService.client.saveComment(form);
744     this.findAndUpdateComment(saveCommentRes);
745   }
746
747   async handleSavePost(form: SavePost) {
748     const saveRes = await HttpService.client.savePost(form);
749     this.findAndUpdatePost(saveRes);
750   }
751
752   async handleFeaturePost(form: FeaturePost) {
753     const featureRes = await HttpService.client.featurePost(form);
754     this.findAndUpdatePost(featureRes);
755   }
756
757   async handleCommentVote(form: CreateCommentLike) {
758     const voteRes = await HttpService.client.likeComment(form);
759     this.findAndUpdateComment(voteRes);
760   }
761
762   async handlePostEdit(form: EditPost) {
763     const res = await HttpService.client.editPost(form);
764     this.findAndUpdatePost(res);
765   }
766
767   async handlePostVote(form: CreatePostLike) {
768     const voteRes = await HttpService.client.likePost(form);
769     this.findAndUpdatePost(voteRes);
770   }
771
772   async handleCommentReport(form: CreateCommentReport) {
773     const reportRes = await HttpService.client.createCommentReport(form);
774     if (reportRes.state == "success") {
775       toast(i18n.t("report_created"));
776     }
777   }
778
779   async handlePostReport(form: CreatePostReport) {
780     const reportRes = await HttpService.client.createPostReport(form);
781     if (reportRes.state == "success") {
782       toast(i18n.t("report_created"));
783     }
784   }
785
786   async handleLockPost(form: LockPost) {
787     const lockRes = await HttpService.client.lockPost(form);
788     this.findAndUpdatePost(lockRes);
789   }
790
791   async handleDistinguishComment(form: DistinguishComment) {
792     const distinguishRes = await HttpService.client.distinguishComment(form);
793     this.findAndUpdateComment(distinguishRes);
794   }
795
796   async handleAddAdmin(form: AddAdmin) {
797     const addAdminRes = await HttpService.client.addAdmin(form);
798
799     if (addAdminRes.state == "success") {
800       this.setState(s => ((s.siteRes.admins = addAdminRes.data.admins), s));
801     }
802   }
803
804   async handleTransferCommunity(form: TransferCommunity) {
805     const transferCommunityRes = await HttpService.client.transferCommunity(
806       form
807     );
808     toast(i18n.t("transfer_community"));
809     this.updateCommunityFull(transferCommunityRes);
810   }
811
812   async handleCommentReplyRead(form: MarkCommentReplyAsRead) {
813     const readRes = await HttpService.client.markCommentReplyAsRead(form);
814     this.findAndUpdateCommentReply(readRes);
815   }
816
817   async handlePersonMentionRead(form: MarkPersonMentionAsRead) {
818     // TODO not sure what to do here. Maybe it is actually optional, because post doesn't need it.
819     await HttpService.client.markPersonMentionAsRead(form);
820   }
821
822   async handleBanFromCommunity(form: BanFromCommunity) {
823     const banRes = await HttpService.client.banFromCommunity(form);
824     this.updateBanFromCommunity(banRes);
825   }
826
827   async handleBanPerson(form: BanPerson) {
828     const banRes = await HttpService.client.banPerson(form);
829     this.updateBan(banRes);
830   }
831
832   updateBanFromCommunity(banRes: RequestState<BanFromCommunityResponse>) {
833     // Maybe not necessary
834     if (banRes.state == "success") {
835       this.setState(s => {
836         if (s.postsRes.state == "success") {
837           s.postsRes.data.posts
838             .filter(c => c.creator.id == banRes.data.person_view.person.id)
839             .forEach(
840               c => (c.creator_banned_from_community = banRes.data.banned)
841             );
842         }
843         if (s.commentsRes.state == "success") {
844           s.commentsRes.data.comments
845             .filter(c => c.creator.id == banRes.data.person_view.person.id)
846             .forEach(
847               c => (c.creator_banned_from_community = banRes.data.banned)
848             );
849         }
850         return s;
851       });
852     }
853   }
854
855   updateBan(banRes: RequestState<BanPersonResponse>) {
856     // Maybe not necessary
857     if (banRes.state == "success") {
858       this.setState(s => {
859         if (s.postsRes.state == "success") {
860           s.postsRes.data.posts
861             .filter(c => c.creator.id == banRes.data.person_view.person.id)
862             .forEach(c => (c.creator.banned = banRes.data.banned));
863         }
864         if (s.commentsRes.state == "success") {
865           s.commentsRes.data.comments
866             .filter(c => c.creator.id == banRes.data.person_view.person.id)
867             .forEach(c => (c.creator.banned = banRes.data.banned));
868         }
869         return s;
870       });
871     }
872   }
873
874   updateCommunity(res: RequestState<CommunityResponse>) {
875     this.setState(s => {
876       if (s.communityRes.state == "success" && res.state == "success") {
877         s.communityRes.data.community_view = res.data.community_view;
878         s.communityRes.data.discussion_languages =
879           res.data.discussion_languages;
880       }
881       return s;
882     });
883   }
884
885   updateCommunityFull(res: RequestState<GetCommunityResponse>) {
886     this.setState(s => {
887       if (s.communityRes.state == "success" && res.state == "success") {
888         s.communityRes.data.community_view = res.data.community_view;
889         s.communityRes.data.moderators = res.data.moderators;
890       }
891       return s;
892     });
893   }
894
895   purgeItem(purgeRes: RequestState<PurgeItemResponse>) {
896     if (purgeRes.state == "success") {
897       toast(i18n.t("purge_success"));
898       this.context.router.history.push(`/`);
899     }
900   }
901
902   findAndUpdateComment(res: RequestState<CommentResponse>) {
903     this.setState(s => {
904       if (s.commentsRes.state == "success" && res.state == "success") {
905         s.commentsRes.data.comments = editComment(
906           res.data.comment_view,
907           s.commentsRes.data.comments
908         );
909         s.finished.set(res.data.comment_view.comment.id, true);
910       }
911       return s;
912     });
913   }
914
915   createAndUpdateComments(res: RequestState<CommentResponse>) {
916     this.setState(s => {
917       if (s.commentsRes.state == "success" && res.state == "success") {
918         s.commentsRes.data.comments.unshift(res.data.comment_view);
919
920         // Set finished for the parent
921         s.finished.set(
922           getCommentParentId(res.data.comment_view.comment) ?? 0,
923           true
924         );
925       }
926       return s;
927     });
928   }
929
930   findAndUpdateCommentReply(res: RequestState<CommentReplyResponse>) {
931     this.setState(s => {
932       if (s.commentsRes.state == "success" && res.state == "success") {
933         s.commentsRes.data.comments = editWith(
934           res.data.comment_reply_view,
935           s.commentsRes.data.comments
936         );
937       }
938       return s;
939     });
940   }
941
942   findAndUpdatePost(res: RequestState<PostResponse>) {
943     this.setState(s => {
944       if (s.postsRes.state == "success" && res.state == "success") {
945         s.postsRes.data.posts = editPost(
946           res.data.post_view,
947           s.postsRes.data.posts
948         );
949       }
950       return s;
951     });
952   }
953
954   updateModerators(res: RequestState<AddModToCommunityResponse>) {
955     // Update the moderators
956     this.setState(s => {
957       if (s.communityRes.state == "success" && res.state == "success") {
958         s.communityRes.data.moderators = res.data.moderators;
959       }
960       return s;
961     });
962   }
963 }