1 import { Component, linkEvent } from "inferno";
3 AddModToCommunityResponse,
4 BanFromCommunityResponse,
20 } from "lemmy-js-client";
21 import { Subscription } from "rxjs";
22 import { i18n } from "../../i18next";
23 import { DataType, InitialFetchRequest } from "../../interfaces";
24 import { UserService, WebSocketService } from "../../services";
30 createPostLikeFindRes,
38 restoreScrollPosition,
50 import { CommentNodes } from "../comment/comment-nodes";
51 import { BannerIconHeader } from "../common/banner-icon-header";
52 import { DataTypeSelect } from "../common/data-type-select";
53 import { HtmlTags } from "../common/html-tags";
54 import { Icon, Spinner } from "../common/icon";
55 import { Paginator } from "../common/paginator";
56 import { SortSelect } from "../common/sort-select";
57 import { Sidebar } from "../community/sidebar";
58 import { PostListings } from "../post/post-listings";
59 import { CommunityLink } from "./community-link";
62 communityRes: GetCommunityResponse;
63 siteRes: GetSiteResponse;
64 communityName: string;
65 communityLoading: boolean;
66 postsLoading: boolean;
67 commentsLoading: boolean;
69 comments: CommentView[];
73 showSidebarMobile: boolean;
76 interface CommunityProps {
88 export class Community extends Component<any, State> {
89 private isoData = setIsoData(this.context);
90 private subscription: Subscription;
91 private emptyState: State = {
92 communityRes: undefined,
93 communityName: this.props.match.params.name,
94 communityLoading: true,
96 commentsLoading: true,
99 dataType: getDataTypeFromProps(this.props),
100 sort: getSortTypeFromProps(this.props),
101 page: getPageFromProps(this.props),
102 siteRes: this.isoData.site_res,
103 showSidebarMobile: false,
106 constructor(props: any, context: any) {
107 super(props, context);
109 this.state = this.emptyState;
110 this.handleSortChange = this.handleSortChange.bind(this);
111 this.handleDataTypeChange = this.handleDataTypeChange.bind(this);
112 this.handlePageChange = this.handlePageChange.bind(this);
114 this.parseMessage = this.parseMessage.bind(this);
115 this.subscription = wsSubscribe(this.parseMessage);
117 // Only fetch the data if coming from another route
118 if (this.isoData.path == this.context.router.route.match.url) {
119 this.state.communityRes = this.isoData.routeData[0];
120 if (this.state.dataType == DataType.Post) {
121 this.state.posts = this.isoData.routeData[1].posts;
123 this.state.comments = this.isoData.routeData[1].comments;
125 this.state.communityLoading = false;
126 this.state.postsLoading = false;
127 this.state.commentsLoading = false;
129 this.fetchCommunity();
136 let form: GetCommunity = {
137 name: this.state.communityName ? this.state.communityName : null,
138 auth: authField(false),
140 WebSocketService.Instance.send(wsClient.getCommunity(form));
143 componentWillUnmount() {
144 saveScrollPosition(this.context);
145 this.subscription.unsubscribe();
146 window.isoData.path = undefined;
149 static getDerivedStateFromProps(props: any): CommunityProps {
151 dataType: getDataTypeFromProps(props),
152 sort: getSortTypeFromProps(props),
153 page: getPageFromProps(props),
157 static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
158 let pathSplit = req.path.split("/");
159 let promises: Promise<any>[] = [];
161 // It can be /c/main, or /c/1
162 let idOrName = pathSplit[2];
165 if (isNaN(Number(idOrName))) {
168 id = Number(idOrName);
171 let communityForm: GetCommunity = id ? { id } : { name: name_ };
172 setOptionalAuth(communityForm, req.auth);
173 promises.push(req.client.getCommunity(communityForm));
175 let dataType: DataType = pathSplit[4]
176 ? DataType[pathSplit[4]]
179 let sort: SortType = pathSplit[6]
180 ? SortType[pathSplit[6]]
181 : UserService.Instance.localUserView
182 ? Object.values(SortType)[
183 UserService.Instance.localUserView.local_user.default_sort_type
187 let page = pathSplit[8] ? Number(pathSplit[8]) : 1;
189 if (dataType == DataType.Post) {
190 let getPostsForm: GetPosts = {
194 type_: ListingType.Community,
197 setOptionalAuth(getPostsForm, req.auth);
198 this.setName(getPostsForm, name_);
199 promises.push(req.client.getPosts(getPostsForm));
201 let getCommentsForm: GetComments = {
205 type_: ListingType.Community,
208 setOptionalAuth(getCommentsForm, req.auth);
209 promises.push(req.client.getComments(getCommentsForm));
215 static setName(obj: any, name_: string) {
216 obj.community_name = name_;
219 componentDidUpdate(_: any, lastState: State) {
221 lastState.dataType !== this.state.dataType ||
222 lastState.sort !== this.state.sort ||
223 lastState.page !== this.state.page
225 this.setState({ postsLoading: true, commentsLoading: true });
230 get documentTitle(): string {
231 return `${this.state.communityRes.community_view.community.title} - ${this.state.siteRes.site_view.site.name}`;
235 let cv = this.state.communityRes?.community_view;
237 <div class="container">
238 {this.state.communityLoading ? (
245 title={this.documentTitle}
246 path={this.context.router.route.match.url}
247 description={cv.community.description}
248 image={cv.community.icon}
252 <div class="col-12 col-md-8">
253 {this.communityInfo()}
254 <div class="d-block d-md-none">
256 class="btn btn-secondary d-inline-block mb-2 mr-3"
257 onClick={linkEvent(this, this.handleShowSidebarMobile)}
259 {i18n.t("sidebar")}{" "}
262 this.state.showSidebarMobile
266 classes="icon-inline"
269 {this.state.showSidebarMobile && (
272 moderators={this.state.communityRes.moderators}
273 admins={this.state.siteRes.admins}
274 online={this.state.communityRes.online}
275 enableNsfw={this.state.siteRes.site_view.site.enable_nsfw}
282 page={this.state.page}
283 onChange={this.handlePageChange}
286 <div class="d-none d-md-block col-md-4">
289 moderators={this.state.communityRes.moderators}
290 admins={this.state.siteRes.admins}
291 online={this.state.communityRes.online}
292 enableNsfw={this.state.siteRes.site_view.site.enable_nsfw}
303 let site = this.state.siteRes.site_view.site;
304 return this.state.dataType == DataType.Post ? (
305 this.state.postsLoading ? (
311 posts={this.state.posts}
313 enableDownvotes={site.enable_downvotes}
314 enableNsfw={site.enable_nsfw}
317 ) : this.state.commentsLoading ? (
323 nodes={commentsToFlatNodes(this.state.comments)}
326 enableDownvotes={site.enable_downvotes}
332 let community = this.state.communityRes.community_view.community;
335 <BannerIconHeader banner={community.banner} icon={community.icon} />
336 <h5 class="mb-0">{community.title}</h5>
338 community={community}
353 type_={this.state.dataType}
354 onChange={this.handleDataTypeChange}
358 <SortSelect sort={this.state.sort} onChange={this.handleSortChange} />
361 href={communityRSSUrl(
362 this.state.communityRes.community_view.community.actor_id,
368 <Icon icon="rss" classes="text-muted small" />
374 handlePageChange(page: number) {
375 this.updateUrl({ page });
376 window.scrollTo(0, 0);
379 handleSortChange(val: SortType) {
380 this.updateUrl({ sort: val, page: 1 });
381 window.scrollTo(0, 0);
384 handleDataTypeChange(val: DataType) {
385 this.updateUrl({ dataType: DataType[val], page: 1 });
386 window.scrollTo(0, 0);
389 handleShowSidebarMobile(i: Community) {
390 i.state.showSidebarMobile = !i.state.showSidebarMobile;
394 updateUrl(paramUpdates: UrlParams) {
395 const dataTypeStr = paramUpdates.dataType || DataType[this.state.dataType];
396 const sortStr = paramUpdates.sort || this.state.sort;
397 const page = paramUpdates.page || this.state.page;
399 let typeView = `/c/${this.state.communityName}`;
401 this.props.history.push(
402 `${typeView}/data_type/${dataTypeStr}/sort/${sortStr}/page/${page}`
407 if (this.state.dataType == DataType.Post) {
408 let form: GetPosts = {
409 page: this.state.page,
411 sort: this.state.sort,
412 type_: ListingType.Community,
413 community_name: this.state.communityName,
415 auth: authField(false),
417 WebSocketService.Instance.send(wsClient.getPosts(form));
419 let form: GetComments = {
420 page: this.state.page,
422 sort: this.state.sort,
423 type_: ListingType.Community,
424 community_name: this.state.communityName,
426 auth: authField(false),
428 WebSocketService.Instance.send(wsClient.getComments(form));
432 parseMessage(msg: any) {
433 let op = wsUserOp(msg);
436 toast(i18n.t(msg.error), "danger");
437 this.context.router.history.push("/");
439 } else if (msg.reconnect) {
440 WebSocketService.Instance.send(
441 wsClient.communityJoin({
442 community_id: this.state.communityRes.community_view.community.id,
446 } else if (op == UserOperation.GetCommunity) {
447 let data = wsJsonToRes<GetCommunityResponse>(msg).data;
448 this.state.communityRes = data;
449 this.state.communityLoading = false;
450 this.setState(this.state);
451 // TODO why is there no auth in this form?
452 WebSocketService.Instance.send(
453 wsClient.communityJoin({
454 community_id: data.community_view.community.id,
458 op == UserOperation.EditCommunity ||
459 op == UserOperation.DeleteCommunity ||
460 op == UserOperation.RemoveCommunity
462 let data = wsJsonToRes<CommunityResponse>(msg).data;
463 this.state.communityRes.community_view = data.community_view;
464 this.setState(this.state);
465 } else if (op == UserOperation.FollowCommunity) {
466 let data = wsJsonToRes<CommunityResponse>(msg).data;
467 this.state.communityRes.community_view.subscribed =
468 data.community_view.subscribed;
469 this.state.communityRes.community_view.counts.subscribers =
470 data.community_view.counts.subscribers;
471 this.setState(this.state);
472 } else if (op == UserOperation.GetPosts) {
473 let data = wsJsonToRes<GetPostsResponse>(msg).data;
474 this.state.posts = data.posts;
475 this.state.postsLoading = false;
476 this.setState(this.state);
477 restoreScrollPosition(this.context);
480 op == UserOperation.EditPost ||
481 op == UserOperation.DeletePost ||
482 op == UserOperation.RemovePost ||
483 op == UserOperation.LockPost ||
484 op == UserOperation.StickyPost ||
485 op == UserOperation.SavePost
487 let data = wsJsonToRes<PostResponse>(msg).data;
488 editPostFindRes(data.post_view, this.state.posts);
489 this.setState(this.state);
490 } else if (op == UserOperation.CreatePost) {
491 let data = wsJsonToRes<PostResponse>(msg).data;
492 this.state.posts.unshift(data.post_view);
493 notifyPost(data.post_view, this.context.router);
494 this.setState(this.state);
495 } else if (op == UserOperation.CreatePostLike) {
496 let data = wsJsonToRes<PostResponse>(msg).data;
497 createPostLikeFindRes(data.post_view, this.state.posts);
498 this.setState(this.state);
499 } else if (op == UserOperation.AddModToCommunity) {
500 let data = wsJsonToRes<AddModToCommunityResponse>(msg).data;
501 this.state.communityRes.moderators = data.moderators;
502 this.setState(this.state);
503 } else if (op == UserOperation.BanFromCommunity) {
504 let data = wsJsonToRes<BanFromCommunityResponse>(msg).data;
506 // TODO this might be incorrect
508 .filter(p => p.creator.id == data.person_view.person.id)
509 .forEach(p => (p.creator_banned_from_community = data.banned));
511 this.setState(this.state);
512 } else if (op == UserOperation.GetComments) {
513 let data = wsJsonToRes<GetCommentsResponse>(msg).data;
514 this.state.comments = data.comments;
515 this.state.commentsLoading = false;
516 this.setState(this.state);
518 op == UserOperation.EditComment ||
519 op == UserOperation.DeleteComment ||
520 op == UserOperation.RemoveComment
522 let data = wsJsonToRes<CommentResponse>(msg).data;
523 editCommentRes(data.comment_view, this.state.comments);
524 this.setState(this.state);
525 } else if (op == UserOperation.CreateComment) {
526 let data = wsJsonToRes<CommentResponse>(msg).data;
528 // Necessary since it might be a user reply
530 this.state.comments.unshift(data.comment_view);
531 this.setState(this.state);
533 } else if (op == UserOperation.SaveComment) {
534 let data = wsJsonToRes<CommentResponse>(msg).data;
535 saveCommentRes(data.comment_view, this.state.comments);
536 this.setState(this.state);
537 } else if (op == UserOperation.CreateCommentLike) {
538 let data = wsJsonToRes<CommentResponse>(msg).data;
539 createCommentLikeRes(data.comment_view, this.state.comments);
540 this.setState(this.state);