]> Untitled Git - lemmy-ui.git/blob - src/shared/components/community/community.tsx
Fix community comments iso fetch. Fixes #572 (#574)
[lemmy-ui.git] / src / shared / components / community / community.tsx
1 import { Component, linkEvent } from "inferno";
2 import {
3   AddModToCommunityResponse,
4   BanFromCommunityResponse,
5   BlockPersonResponse,
6   CommentReportResponse,
7   CommentResponse,
8   CommentView,
9   CommunityResponse,
10   GetComments,
11   GetCommentsResponse,
12   GetCommunity,
13   GetCommunityResponse,
14   GetPosts,
15   GetPostsResponse,
16   GetSiteResponse,
17   ListingType,
18   PostReportResponse,
19   PostResponse,
20   PostView,
21   SortType,
22   UserOperation,
23 } from "lemmy-js-client";
24 import { Subscription } from "rxjs";
25 import { i18n } from "../../i18next";
26 import { DataType, InitialFetchRequest } from "../../interfaces";
27 import { UserService, WebSocketService } from "../../services";
28 import {
29   authField,
30   commentsToFlatNodes,
31   communityRSSUrl,
32   createCommentLikeRes,
33   createPostLikeFindRes,
34   editCommentRes,
35   editPostFindRes,
36   fetchLimit,
37   getDataTypeFromProps,
38   getPageFromProps,
39   getSortTypeFromProps,
40   notifyPost,
41   restoreScrollPosition,
42   saveCommentRes,
43   saveScrollPosition,
44   setIsoData,
45   setOptionalAuth,
46   setupTippy,
47   toast,
48   updatePersonBlock,
49   wsClient,
50   wsJsonToRes,
51   wsSubscribe,
52   wsUserOp,
53 } from "../../utils";
54 import { CommentNodes } from "../comment/comment-nodes";
55 import { BannerIconHeader } from "../common/banner-icon-header";
56 import { DataTypeSelect } from "../common/data-type-select";
57 import { HtmlTags } from "../common/html-tags";
58 import { Icon, Spinner } from "../common/icon";
59 import { Paginator } from "../common/paginator";
60 import { SortSelect } from "../common/sort-select";
61 import { Sidebar } from "../community/sidebar";
62 import { PostListings } from "../post/post-listings";
63 import { CommunityLink } from "./community-link";
64
65 interface State {
66   communityRes: GetCommunityResponse;
67   siteRes: GetSiteResponse;
68   communityName: string;
69   communityLoading: boolean;
70   postsLoading: boolean;
71   commentsLoading: boolean;
72   posts: PostView[];
73   comments: CommentView[];
74   dataType: DataType;
75   sort: SortType;
76   page: number;
77   showSidebarMobile: boolean;
78 }
79
80 interface CommunityProps {
81   dataType: DataType;
82   sort: SortType;
83   page: number;
84 }
85
86 interface UrlParams {
87   dataType?: string;
88   sort?: SortType;
89   page?: number;
90 }
91
92 export class Community extends Component<any, State> {
93   private isoData = setIsoData(this.context);
94   private subscription: Subscription;
95   private emptyState: State = {
96     communityRes: undefined,
97     communityName: this.props.match.params.name,
98     communityLoading: true,
99     postsLoading: true,
100     commentsLoading: true,
101     posts: [],
102     comments: [],
103     dataType: getDataTypeFromProps(this.props),
104     sort: getSortTypeFromProps(this.props),
105     page: getPageFromProps(this.props),
106     siteRes: this.isoData.site_res,
107     showSidebarMobile: false,
108   };
109
110   constructor(props: any, context: any) {
111     super(props, context);
112
113     this.state = this.emptyState;
114     this.handleSortChange = this.handleSortChange.bind(this);
115     this.handleDataTypeChange = this.handleDataTypeChange.bind(this);
116     this.handlePageChange = this.handlePageChange.bind(this);
117
118     this.parseMessage = this.parseMessage.bind(this);
119     this.subscription = wsSubscribe(this.parseMessage);
120
121     // Only fetch the data if coming from another route
122     if (this.isoData.path == this.context.router.route.match.url) {
123       this.state.communityRes = this.isoData.routeData[0];
124       if (this.state.dataType == DataType.Post) {
125         this.state.posts = this.isoData.routeData[1].posts;
126       } else {
127         this.state.comments = this.isoData.routeData[1].comments;
128       }
129       this.state.communityLoading = false;
130       this.state.postsLoading = false;
131       this.state.commentsLoading = false;
132     } else {
133       this.fetchCommunity();
134       this.fetchData();
135     }
136   }
137
138   fetchCommunity() {
139     let form: GetCommunity = {
140       name: this.state.communityName ? this.state.communityName : null,
141       auth: authField(false),
142     };
143     WebSocketService.Instance.send(wsClient.getCommunity(form));
144   }
145
146   componentDidMount() {
147     setupTippy();
148   }
149
150   componentWillUnmount() {
151     saveScrollPosition(this.context);
152     this.subscription.unsubscribe();
153     window.isoData.path = undefined;
154   }
155
156   static getDerivedStateFromProps(props: any): CommunityProps {
157     return {
158       dataType: getDataTypeFromProps(props),
159       sort: getSortTypeFromProps(props),
160       page: getPageFromProps(props),
161     };
162   }
163
164   static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
165     let pathSplit = req.path.split("/");
166     let promises: Promise<any>[] = [];
167
168     // It can be /c/main, or /c/1
169     let idOrName = pathSplit[2];
170     let id: number;
171     let name_: string;
172     if (isNaN(Number(idOrName))) {
173       name_ = idOrName;
174     } else {
175       id = Number(idOrName);
176     }
177
178     let communityForm: GetCommunity = id ? { id } : { name: name_ };
179     setOptionalAuth(communityForm, req.auth);
180     promises.push(req.client.getCommunity(communityForm));
181
182     let dataType: DataType = pathSplit[4]
183       ? DataType[pathSplit[4]]
184       : DataType.Post;
185
186     let sort: SortType = pathSplit[6]
187       ? SortType[pathSplit[6]]
188       : UserService.Instance.myUserInfo
189       ? Object.values(SortType)[
190           UserService.Instance.myUserInfo.local_user_view.local_user
191             .default_sort_type
192         ]
193       : SortType.Active;
194
195     let page = pathSplit[8] ? Number(pathSplit[8]) : 1;
196
197     if (dataType == DataType.Post) {
198       let getPostsForm: GetPosts = {
199         page,
200         limit: fetchLimit,
201         sort,
202         type_: ListingType.Community,
203         saved_only: false,
204       };
205       setOptionalAuth(getPostsForm, req.auth);
206       this.setName(getPostsForm, name_);
207       promises.push(req.client.getPosts(getPostsForm));
208     } else {
209       let getCommentsForm: GetComments = {
210         page,
211         limit: fetchLimit,
212         sort,
213         type_: ListingType.Community,
214         saved_only: false,
215       };
216       this.setName(getCommentsForm, name_);
217       setOptionalAuth(getCommentsForm, req.auth);
218       promises.push(req.client.getComments(getCommentsForm));
219     }
220
221     return promises;
222   }
223
224   static setName(obj: any, name_: string) {
225     obj.community_name = name_;
226   }
227
228   componentDidUpdate(_: any, lastState: State) {
229     if (
230       lastState.dataType !== this.state.dataType ||
231       lastState.sort !== this.state.sort ||
232       lastState.page !== this.state.page
233     ) {
234       this.setState({ postsLoading: true, commentsLoading: true });
235       this.fetchData();
236     }
237   }
238
239   get documentTitle(): string {
240     return `${this.state.communityRes.community_view.community.title} - ${this.state.siteRes.site_view.site.name}`;
241   }
242
243   render() {
244     let cv = this.state.communityRes?.community_view;
245     return (
246       <div class="container">
247         {this.state.communityLoading ? (
248           <h5>
249             <Spinner large />
250           </h5>
251         ) : (
252           <>
253             <HtmlTags
254               title={this.documentTitle}
255               path={this.context.router.route.match.url}
256               description={cv.community.description}
257               image={cv.community.icon}
258             />
259
260             <div class="row">
261               <div class="col-12 col-md-8">
262                 {this.communityInfo()}
263                 <div class="d-block d-md-none">
264                   <button
265                     class="btn btn-secondary d-inline-block mb-2 mr-3"
266                     onClick={linkEvent(this, this.handleShowSidebarMobile)}
267                   >
268                     {i18n.t("sidebar")}{" "}
269                     <Icon
270                       icon={
271                         this.state.showSidebarMobile
272                           ? `minus-square`
273                           : `plus-square`
274                       }
275                       classes="icon-inline"
276                     />
277                   </button>
278                   {this.state.showSidebarMobile && (
279                     <Sidebar
280                       community_view={cv}
281                       moderators={this.state.communityRes.moderators}
282                       admins={this.state.siteRes.admins}
283                       online={this.state.communityRes.online}
284                       enableNsfw={this.state.siteRes.site_view.site.enable_nsfw}
285                     />
286                   )}
287                 </div>
288                 {this.selects()}
289                 {this.listings()}
290                 <Paginator
291                   page={this.state.page}
292                   onChange={this.handlePageChange}
293                 />
294               </div>
295               <div class="d-none d-md-block col-md-4">
296                 <Sidebar
297                   community_view={cv}
298                   moderators={this.state.communityRes.moderators}
299                   admins={this.state.siteRes.admins}
300                   online={this.state.communityRes.online}
301                   enableNsfw={this.state.siteRes.site_view.site.enable_nsfw}
302                 />
303               </div>
304             </div>
305           </>
306         )}
307       </div>
308     );
309   }
310
311   listings() {
312     let site = this.state.siteRes.site_view.site;
313     return this.state.dataType == DataType.Post ? (
314       this.state.postsLoading ? (
315         <h5>
316           <Spinner large />
317         </h5>
318       ) : (
319         <PostListings
320           posts={this.state.posts}
321           removeDuplicates
322           enableDownvotes={site.enable_downvotes}
323           enableNsfw={site.enable_nsfw}
324         />
325       )
326     ) : this.state.commentsLoading ? (
327       <h5>
328         <Spinner large />
329       </h5>
330     ) : (
331       <CommentNodes
332         nodes={commentsToFlatNodes(this.state.comments)}
333         noIndent
334         showContext
335         enableDownvotes={site.enable_downvotes}
336       />
337     );
338   }
339
340   communityInfo() {
341     let community = this.state.communityRes.community_view.community;
342     return (
343       <div class="mb-2">
344         <BannerIconHeader banner={community.banner} icon={community.icon} />
345         <h5 class="mb-0 overflow-wrap-anywhere">{community.title}</h5>
346         <CommunityLink
347           community={community}
348           realLink
349           useApubName
350           muted
351           hideAvatar
352         />
353       </div>
354     );
355   }
356
357   selects() {
358     let communityRss = communityRSSUrl(
359       this.state.communityRes.community_view.community.actor_id,
360       this.state.sort
361     );
362     return (
363       <div class="mb-3">
364         <span class="mr-3">
365           <DataTypeSelect
366             type_={this.state.dataType}
367             onChange={this.handleDataTypeChange}
368           />
369         </span>
370         <span class="mr-2">
371           <SortSelect sort={this.state.sort} onChange={this.handleSortChange} />
372         </span>
373         <a href={communityRss} title="RSS" rel="noopener">
374           <Icon icon="rss" classes="text-muted small" />
375         </a>
376         <link rel="alternate" type="application/atom+xml" href={communityRss} />
377       </div>
378     );
379   }
380
381   handlePageChange(page: number) {
382     this.updateUrl({ page });
383     window.scrollTo(0, 0);
384   }
385
386   handleSortChange(val: SortType) {
387     this.updateUrl({ sort: val, page: 1 });
388     window.scrollTo(0, 0);
389   }
390
391   handleDataTypeChange(val: DataType) {
392     this.updateUrl({ dataType: DataType[val], page: 1 });
393     window.scrollTo(0, 0);
394   }
395
396   handleShowSidebarMobile(i: Community) {
397     i.state.showSidebarMobile = !i.state.showSidebarMobile;
398     i.setState(i.state);
399   }
400
401   updateUrl(paramUpdates: UrlParams) {
402     const dataTypeStr = paramUpdates.dataType || DataType[this.state.dataType];
403     const sortStr = paramUpdates.sort || this.state.sort;
404     const page = paramUpdates.page || this.state.page;
405
406     let typeView = `/c/${this.state.communityName}`;
407
408     this.props.history.push(
409       `${typeView}/data_type/${dataTypeStr}/sort/${sortStr}/page/${page}`
410     );
411   }
412
413   fetchData() {
414     if (this.state.dataType == DataType.Post) {
415       let form: GetPosts = {
416         page: this.state.page,
417         limit: fetchLimit,
418         sort: this.state.sort,
419         type_: ListingType.Community,
420         community_name: this.state.communityName,
421         saved_only: false,
422         auth: authField(false),
423       };
424       WebSocketService.Instance.send(wsClient.getPosts(form));
425     } else {
426       let form: GetComments = {
427         page: this.state.page,
428         limit: fetchLimit,
429         sort: this.state.sort,
430         type_: ListingType.Community,
431         community_name: this.state.communityName,
432         saved_only: false,
433         auth: authField(false),
434       };
435       WebSocketService.Instance.send(wsClient.getComments(form));
436     }
437   }
438
439   parseMessage(msg: any) {
440     let op = wsUserOp(msg);
441     console.log(msg);
442     if (msg.error) {
443       toast(i18n.t(msg.error), "danger");
444       this.context.router.history.push("/");
445       return;
446     } else if (msg.reconnect) {
447       WebSocketService.Instance.send(
448         wsClient.communityJoin({
449           community_id: this.state.communityRes.community_view.community.id,
450         })
451       );
452       this.fetchData();
453     } else if (op == UserOperation.GetCommunity) {
454       let data = wsJsonToRes<GetCommunityResponse>(msg).data;
455       this.state.communityRes = data;
456       this.state.communityLoading = false;
457       this.setState(this.state);
458       // TODO why is there no auth in this form?
459       WebSocketService.Instance.send(
460         wsClient.communityJoin({
461           community_id: data.community_view.community.id,
462         })
463       );
464     } else if (
465       op == UserOperation.EditCommunity ||
466       op == UserOperation.DeleteCommunity ||
467       op == UserOperation.RemoveCommunity
468     ) {
469       let data = wsJsonToRes<CommunityResponse>(msg).data;
470       this.state.communityRes.community_view = data.community_view;
471       this.setState(this.state);
472     } else if (op == UserOperation.FollowCommunity) {
473       let data = wsJsonToRes<CommunityResponse>(msg).data;
474       this.state.communityRes.community_view.subscribed =
475         data.community_view.subscribed;
476       this.state.communityRes.community_view.counts.subscribers =
477         data.community_view.counts.subscribers;
478       this.setState(this.state);
479     } else if (op == UserOperation.GetPosts) {
480       let data = wsJsonToRes<GetPostsResponse>(msg).data;
481       this.state.posts = data.posts;
482       this.state.postsLoading = false;
483       this.setState(this.state);
484       restoreScrollPosition(this.context);
485       setupTippy();
486     } else if (
487       op == UserOperation.EditPost ||
488       op == UserOperation.DeletePost ||
489       op == UserOperation.RemovePost ||
490       op == UserOperation.LockPost ||
491       op == UserOperation.StickyPost ||
492       op == UserOperation.SavePost
493     ) {
494       let data = wsJsonToRes<PostResponse>(msg).data;
495       editPostFindRes(data.post_view, this.state.posts);
496       this.setState(this.state);
497     } else if (op == UserOperation.CreatePost) {
498       let data = wsJsonToRes<PostResponse>(msg).data;
499       this.state.posts.unshift(data.post_view);
500       if (
501         UserService.Instance.myUserInfo?.local_user_view.local_user
502           .show_new_post_notifs
503       ) {
504         notifyPost(data.post_view, this.context.router);
505       }
506       this.setState(this.state);
507     } else if (op == UserOperation.CreatePostLike) {
508       let data = wsJsonToRes<PostResponse>(msg).data;
509       createPostLikeFindRes(data.post_view, this.state.posts);
510       this.setState(this.state);
511     } else if (op == UserOperation.AddModToCommunity) {
512       let data = wsJsonToRes<AddModToCommunityResponse>(msg).data;
513       this.state.communityRes.moderators = data.moderators;
514       this.setState(this.state);
515     } else if (op == UserOperation.BanFromCommunity) {
516       let data = wsJsonToRes<BanFromCommunityResponse>(msg).data;
517
518       // TODO this might be incorrect
519       this.state.posts
520         .filter(p => p.creator.id == data.person_view.person.id)
521         .forEach(p => (p.creator_banned_from_community = data.banned));
522
523       this.setState(this.state);
524     } else if (op == UserOperation.GetComments) {
525       let data = wsJsonToRes<GetCommentsResponse>(msg).data;
526       this.state.comments = data.comments;
527       this.state.commentsLoading = false;
528       this.setState(this.state);
529     } else if (
530       op == UserOperation.EditComment ||
531       op == UserOperation.DeleteComment ||
532       op == UserOperation.RemoveComment
533     ) {
534       let data = wsJsonToRes<CommentResponse>(msg).data;
535       editCommentRes(data.comment_view, this.state.comments);
536       this.setState(this.state);
537     } else if (op == UserOperation.CreateComment) {
538       let data = wsJsonToRes<CommentResponse>(msg).data;
539
540       // Necessary since it might be a user reply
541       if (data.form_id) {
542         this.state.comments.unshift(data.comment_view);
543         this.setState(this.state);
544       }
545     } else if (op == UserOperation.SaveComment) {
546       let data = wsJsonToRes<CommentResponse>(msg).data;
547       saveCommentRes(data.comment_view, this.state.comments);
548       this.setState(this.state);
549     } else if (op == UserOperation.CreateCommentLike) {
550       let data = wsJsonToRes<CommentResponse>(msg).data;
551       createCommentLikeRes(data.comment_view, this.state.comments);
552       this.setState(this.state);
553     } else if (op == UserOperation.BlockPerson) {
554       let data = wsJsonToRes<BlockPersonResponse>(msg).data;
555       updatePersonBlock(data);
556     } else if (op == UserOperation.CreatePostReport) {
557       let data = wsJsonToRes<PostReportResponse>(msg).data;
558       if (data) {
559         toast(i18n.t("report_created"));
560       }
561     } else if (op == UserOperation.CreateCommentReport) {
562       let data = wsJsonToRes<CommentReportResponse>(msg).data;
563       if (data) {
564         toast(i18n.t("report_created"));
565       }
566     }
567   }
568 }