1 import { Component, linkEvent } from "inferno";
3 AddModToCommunityResponse,
4 BanFromCommunityResponse,
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";
33 createPostLikeFindRes,
42 restoreScrollPosition,
56 import { CommentNodes } from "../comment/comment-nodes";
57 import { BannerIconHeader } from "../common/banner-icon-header";
58 import { DataTypeSelect } from "../common/data-type-select";
59 import { HtmlTags } from "../common/html-tags";
60 import { Icon, Spinner } from "../common/icon";
61 import { Paginator } from "../common/paginator";
62 import { SortSelect } from "../common/sort-select";
63 import { Sidebar } from "../community/sidebar";
64 import { SiteSidebar } from "../home/site-sidebar";
65 import { PostListings } from "../post/post-listings";
66 import { CommunityLink } from "./community-link";
69 communityRes: GetCommunityResponse;
70 siteRes: GetSiteResponse;
71 communityName: string;
72 communityLoading: boolean;
73 postsLoading: boolean;
74 commentsLoading: boolean;
76 comments: CommentView[];
80 showSidebarMobile: boolean;
83 interface CommunityProps {
95 export class Community extends Component<any, State> {
96 private isoData = setIsoData(this.context);
97 private subscription: Subscription;
98 private emptyState: State = {
99 communityRes: undefined,
100 communityName: this.props.match.params.name,
101 communityLoading: true,
103 commentsLoading: true,
106 dataType: getDataTypeFromProps(this.props),
107 sort: getSortTypeFromProps(this.props),
108 page: getPageFromProps(this.props),
109 siteRes: this.isoData.site_res,
110 showSidebarMobile: false,
113 constructor(props: any, context: any) {
114 super(props, context);
116 this.state = this.emptyState;
117 this.handleSortChange = this.handleSortChange.bind(this);
118 this.handleDataTypeChange = this.handleDataTypeChange.bind(this);
119 this.handlePageChange = this.handlePageChange.bind(this);
121 this.parseMessage = this.parseMessage.bind(this);
122 this.subscription = wsSubscribe(this.parseMessage);
124 // Only fetch the data if coming from another route
125 if (this.isoData.path == this.context.router.route.match.url) {
126 this.state.communityRes = this.isoData.routeData[0];
127 if (this.state.dataType == DataType.Post) {
128 this.state.posts = this.isoData.routeData[1].posts;
130 this.state.comments = this.isoData.routeData[1].comments;
132 this.state.communityLoading = false;
133 this.state.postsLoading = false;
134 this.state.commentsLoading = false;
136 this.fetchCommunity();
142 let form: GetCommunity = {
143 name: this.state.communityName ? this.state.communityName : null,
144 auth: authField(false),
146 WebSocketService.Instance.send(wsClient.getCommunity(form));
149 componentDidMount() {
153 componentWillUnmount() {
154 saveScrollPosition(this.context);
155 this.subscription.unsubscribe();
156 window.isoData.path = undefined;
159 static getDerivedStateFromProps(props: any): CommunityProps {
161 dataType: getDataTypeFromProps(props),
162 sort: getSortTypeFromProps(props),
163 page: getPageFromProps(props),
167 static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
168 let pathSplit = req.path.split("/");
169 let promises: Promise<any>[] = [];
171 let communityName = pathSplit[2];
172 let communityForm: GetCommunity = { name: communityName };
173 setOptionalAuth(communityForm, req.auth);
174 promises.push(req.client.getCommunity(communityForm));
176 let dataType: DataType = pathSplit[4]
177 ? DataType[pathSplit[4]]
180 let sort: SortType = pathSplit[6]
181 ? SortType[pathSplit[6]]
182 : UserService.Instance.myUserInfo
183 ? Object.values(SortType)[
184 UserService.Instance.myUserInfo.local_user_view.local_user
189 let page = pathSplit[8] ? Number(pathSplit[8]) : 1;
191 if (dataType == DataType.Post) {
192 let getPostsForm: GetPosts = {
196 type_: ListingType.Community,
199 setOptionalAuth(getPostsForm, req.auth);
200 this.setName(getPostsForm, communityName);
201 promises.push(req.client.getPosts(getPostsForm));
203 let getCommentsForm: GetComments = {
207 type_: ListingType.Community,
210 this.setName(getCommentsForm, communityName);
211 setOptionalAuth(getCommentsForm, req.auth);
212 promises.push(req.client.getComments(getCommentsForm));
218 static setName(obj: any, name_: string) {
219 obj.community_name = name_;
222 componentDidUpdate(_: any, lastState: State) {
224 lastState.dataType !== this.state.dataType ||
225 lastState.sort !== this.state.sort ||
226 lastState.page !== this.state.page
228 this.setState({ postsLoading: true, commentsLoading: true });
233 get documentTitle(): string {
234 return `${this.state.communityRes.community_view.community.title} - ${this.state.siteRes.site_view.site.name}`;
238 let cv = this.state.communityRes?.community_view;
240 <div class="container">
241 {this.state.communityLoading ? (
248 title={this.documentTitle}
249 path={this.context.router.route.match.url}
250 description={cv.community.description}
251 image={cv.community.icon}
255 <div class="col-12 col-md-8">
256 {this.communityInfo()}
257 <div class="d-block d-md-none">
259 class="btn btn-secondary d-inline-block mb-2 mr-3"
260 onClick={linkEvent(this, this.handleShowSidebarMobile)}
262 {i18n.t("sidebar")}{" "}
265 this.state.showSidebarMobile
269 classes="icon-inline"
272 {this.state.showSidebarMobile && (
276 moderators={this.state.communityRes.moderators}
277 admins={this.state.siteRes.admins}
278 online={this.state.communityRes.online}
280 this.state.siteRes.site_view.site.enable_nsfw
283 {!cv.community.local && this.state.communityRes.site && (
285 site={this.state.communityRes.site}
286 showLocal={showLocal(this.isoData)}
295 page={this.state.page}
296 onChange={this.handlePageChange}
299 <div class="d-none d-md-block col-md-4">
302 moderators={this.state.communityRes.moderators}
303 admins={this.state.siteRes.admins}
304 online={this.state.communityRes.online}
305 enableNsfw={this.state.siteRes.site_view.site.enable_nsfw}
307 {!cv.community.local && this.state.communityRes.site && (
309 site={this.state.communityRes.site}
310 showLocal={showLocal(this.isoData)}
322 let site = this.state.siteRes.site_view.site;
323 return this.state.dataType == DataType.Post ? (
324 this.state.postsLoading ? (
330 posts={this.state.posts}
332 enableDownvotes={site.enable_downvotes}
333 enableNsfw={site.enable_nsfw}
336 ) : this.state.commentsLoading ? (
342 nodes={commentsToFlatNodes(this.state.comments)}
345 enableDownvotes={site.enable_downvotes}
351 let community = this.state.communityRes.community_view.community;
354 <BannerIconHeader banner={community.banner} icon={community.icon} />
355 <h5 class="mb-0 overflow-wrap-anywhere">{community.title}</h5>
357 community={community}
368 let communityRss = communityRSSUrl(
369 this.state.communityRes.community_view.community.actor_id,
376 type_={this.state.dataType}
377 onChange={this.handleDataTypeChange}
381 <SortSelect sort={this.state.sort} onChange={this.handleSortChange} />
383 <a href={communityRss} title="RSS" rel={relTags}>
384 <Icon icon="rss" classes="text-muted small" />
386 <link rel="alternate" type="application/atom+xml" href={communityRss} />
391 handlePageChange(page: number) {
392 this.updateUrl({ page });
393 window.scrollTo(0, 0);
396 handleSortChange(val: SortType) {
397 this.updateUrl({ sort: val, page: 1 });
398 window.scrollTo(0, 0);
401 handleDataTypeChange(val: DataType) {
402 this.updateUrl({ dataType: DataType[val], page: 1 });
403 window.scrollTo(0, 0);
406 handleShowSidebarMobile(i: Community) {
407 i.state.showSidebarMobile = !i.state.showSidebarMobile;
411 updateUrl(paramUpdates: UrlParams) {
412 const dataTypeStr = paramUpdates.dataType || DataType[this.state.dataType];
413 const sortStr = paramUpdates.sort || this.state.sort;
414 const page = paramUpdates.page || this.state.page;
416 let typeView = `/c/${this.state.communityName}`;
418 this.props.history.push(
419 `${typeView}/data_type/${dataTypeStr}/sort/${sortStr}/page/${page}`
424 if (this.state.dataType == DataType.Post) {
425 let form: GetPosts = {
426 page: this.state.page,
428 sort: this.state.sort,
429 type_: ListingType.Community,
430 community_name: this.state.communityName,
432 auth: authField(false),
434 WebSocketService.Instance.send(wsClient.getPosts(form));
436 let form: GetComments = {
437 page: this.state.page,
439 sort: this.state.sort,
440 type_: ListingType.Community,
441 community_name: this.state.communityName,
443 auth: authField(false),
445 WebSocketService.Instance.send(wsClient.getComments(form));
449 parseMessage(msg: any) {
450 let op = wsUserOp(msg);
453 toast(i18n.t(msg.error), "danger");
454 this.context.router.history.push("/");
456 } else if (msg.reconnect) {
457 WebSocketService.Instance.send(
458 wsClient.communityJoin({
459 community_id: this.state.communityRes.community_view.community.id,
463 } else if (op == UserOperation.GetCommunity) {
464 let data = wsJsonToRes<GetCommunityResponse>(msg).data;
465 this.state.communityRes = data;
466 this.state.communityLoading = false;
467 this.setState(this.state);
468 // TODO why is there no auth in this form?
469 WebSocketService.Instance.send(
470 wsClient.communityJoin({
471 community_id: data.community_view.community.id,
475 op == UserOperation.EditCommunity ||
476 op == UserOperation.DeleteCommunity ||
477 op == UserOperation.RemoveCommunity
479 let data = wsJsonToRes<CommunityResponse>(msg).data;
480 this.state.communityRes.community_view = data.community_view;
481 this.setState(this.state);
482 } else if (op == UserOperation.FollowCommunity) {
483 let data = wsJsonToRes<CommunityResponse>(msg).data;
484 this.state.communityRes.community_view.subscribed =
485 data.community_view.subscribed;
486 this.state.communityRes.community_view.counts.subscribers =
487 data.community_view.counts.subscribers;
488 this.setState(this.state);
489 } else if (op == UserOperation.GetPosts) {
490 let data = wsJsonToRes<GetPostsResponse>(msg).data;
491 this.state.posts = data.posts;
492 this.state.postsLoading = false;
493 this.setState(this.state);
494 restoreScrollPosition(this.context);
497 op == UserOperation.EditPost ||
498 op == UserOperation.DeletePost ||
499 op == UserOperation.RemovePost ||
500 op == UserOperation.LockPost ||
501 op == UserOperation.StickyPost ||
502 op == UserOperation.SavePost
504 let data = wsJsonToRes<PostResponse>(msg).data;
505 editPostFindRes(data.post_view, this.state.posts);
506 this.setState(this.state);
507 } else if (op == UserOperation.CreatePost) {
508 let data = wsJsonToRes<PostResponse>(msg).data;
509 this.state.posts.unshift(data.post_view);
511 UserService.Instance.myUserInfo?.local_user_view.local_user
512 .show_new_post_notifs
514 notifyPost(data.post_view, this.context.router);
516 this.setState(this.state);
517 } else if (op == UserOperation.CreatePostLike) {
518 let data = wsJsonToRes<PostResponse>(msg).data;
519 createPostLikeFindRes(data.post_view, this.state.posts);
520 this.setState(this.state);
521 } else if (op == UserOperation.AddModToCommunity) {
522 let data = wsJsonToRes<AddModToCommunityResponse>(msg).data;
523 this.state.communityRes.moderators = data.moderators;
524 this.setState(this.state);
525 } else if (op == UserOperation.BanFromCommunity) {
526 let data = wsJsonToRes<BanFromCommunityResponse>(msg).data;
528 // TODO this might be incorrect
530 .filter(p => p.creator.id == data.person_view.person.id)
531 .forEach(p => (p.creator_banned_from_community = data.banned));
533 this.setState(this.state);
534 } else if (op == UserOperation.GetComments) {
535 let data = wsJsonToRes<GetCommentsResponse>(msg).data;
536 this.state.comments = data.comments;
537 this.state.commentsLoading = false;
538 this.setState(this.state);
540 op == UserOperation.EditComment ||
541 op == UserOperation.DeleteComment ||
542 op == UserOperation.RemoveComment
544 let data = wsJsonToRes<CommentResponse>(msg).data;
545 editCommentRes(data.comment_view, this.state.comments);
546 this.setState(this.state);
547 } else if (op == UserOperation.CreateComment) {
548 let data = wsJsonToRes<CommentResponse>(msg).data;
550 // Necessary since it might be a user reply
552 this.state.comments.unshift(data.comment_view);
553 this.setState(this.state);
555 } else if (op == UserOperation.SaveComment) {
556 let data = wsJsonToRes<CommentResponse>(msg).data;
557 saveCommentRes(data.comment_view, this.state.comments);
558 this.setState(this.state);
559 } else if (op == UserOperation.CreateCommentLike) {
560 let data = wsJsonToRes<CommentResponse>(msg).data;
561 createCommentLikeRes(data.comment_view, this.state.comments);
562 this.setState(this.state);
563 } else if (op == UserOperation.BlockPerson) {
564 let data = wsJsonToRes<BlockPersonResponse>(msg).data;
565 updatePersonBlock(data);
566 } else if (op == UserOperation.CreatePostReport) {
567 let data = wsJsonToRes<PostReportResponse>(msg).data;
569 toast(i18n.t("report_created"));
571 } else if (op == UserOperation.CreateCommentReport) {
572 let data = wsJsonToRes<CommentReportResponse>(msg).data;
574 toast(i18n.t("report_created"));