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,
41 restoreScrollPosition,
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";
66 communityRes: GetCommunityResponse;
67 siteRes: GetSiteResponse;
68 communityName: string;
69 communityLoading: boolean;
70 postsLoading: boolean;
71 commentsLoading: boolean;
73 comments: CommentView[];
77 showSidebarMobile: boolean;
80 interface CommunityProps {
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,
100 commentsLoading: true,
103 dataType: getDataTypeFromProps(this.props),
104 sort: getSortTypeFromProps(this.props),
105 page: getPageFromProps(this.props),
106 siteRes: this.isoData.site_res,
107 showSidebarMobile: false,
110 constructor(props: any, context: any) {
111 super(props, context);
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);
118 this.parseMessage = this.parseMessage.bind(this);
119 this.subscription = wsSubscribe(this.parseMessage);
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;
127 this.state.comments = this.isoData.routeData[1].comments;
129 this.state.communityLoading = false;
130 this.state.postsLoading = false;
131 this.state.commentsLoading = false;
133 this.fetchCommunity();
139 let form: GetCommunity = {
140 name: this.state.communityName ? this.state.communityName : null,
141 auth: authField(false),
143 WebSocketService.Instance.send(wsClient.getCommunity(form));
146 componentDidMount() {
150 componentWillUnmount() {
151 saveScrollPosition(this.context);
152 this.subscription.unsubscribe();
153 window.isoData.path = undefined;
156 static getDerivedStateFromProps(props: any): CommunityProps {
158 dataType: getDataTypeFromProps(props),
159 sort: getSortTypeFromProps(props),
160 page: getPageFromProps(props),
164 static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
165 let pathSplit = req.path.split("/");
166 let promises: Promise<any>[] = [];
168 // It can be /c/main, or /c/1
169 let idOrName = pathSplit[2];
172 if (isNaN(Number(idOrName))) {
175 id = Number(idOrName);
178 let communityForm: GetCommunity = id ? { id } : { name: name_ };
179 setOptionalAuth(communityForm, req.auth);
180 promises.push(req.client.getCommunity(communityForm));
182 let dataType: DataType = pathSplit[4]
183 ? DataType[pathSplit[4]]
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
195 let page = pathSplit[8] ? Number(pathSplit[8]) : 1;
197 if (dataType == DataType.Post) {
198 let getPostsForm: GetPosts = {
202 type_: ListingType.Community,
205 setOptionalAuth(getPostsForm, req.auth);
206 this.setName(getPostsForm, name_);
207 promises.push(req.client.getPosts(getPostsForm));
209 let getCommentsForm: GetComments = {
213 type_: ListingType.Community,
216 setOptionalAuth(getCommentsForm, req.auth);
217 promises.push(req.client.getComments(getCommentsForm));
223 static setName(obj: any, name_: string) {
224 obj.community_name = name_;
227 componentDidUpdate(_: any, lastState: State) {
229 lastState.dataType !== this.state.dataType ||
230 lastState.sort !== this.state.sort ||
231 lastState.page !== this.state.page
233 this.setState({ postsLoading: true, commentsLoading: true });
238 get documentTitle(): string {
239 return `${this.state.communityRes.community_view.community.title} - ${this.state.siteRes.site_view.site.name}`;
243 let cv = this.state.communityRes?.community_view;
245 <div class="container">
246 {this.state.communityLoading ? (
253 title={this.documentTitle}
254 path={this.context.router.route.match.url}
255 description={cv.community.description}
256 image={cv.community.icon}
260 <div class="col-12 col-md-8">
261 {this.communityInfo()}
262 <div class="d-block d-md-none">
264 class="btn btn-secondary d-inline-block mb-2 mr-3"
265 onClick={linkEvent(this, this.handleShowSidebarMobile)}
267 {i18n.t("sidebar")}{" "}
270 this.state.showSidebarMobile
274 classes="icon-inline"
277 {this.state.showSidebarMobile && (
280 moderators={this.state.communityRes.moderators}
281 admins={this.state.siteRes.admins}
282 online={this.state.communityRes.online}
283 enableNsfw={this.state.siteRes.site_view.site.enable_nsfw}
290 page={this.state.page}
291 onChange={this.handlePageChange}
294 <div class="d-none d-md-block col-md-4">
297 moderators={this.state.communityRes.moderators}
298 admins={this.state.siteRes.admins}
299 online={this.state.communityRes.online}
300 enableNsfw={this.state.siteRes.site_view.site.enable_nsfw}
311 let site = this.state.siteRes.site_view.site;
312 return this.state.dataType == DataType.Post ? (
313 this.state.postsLoading ? (
319 posts={this.state.posts}
321 enableDownvotes={site.enable_downvotes}
322 enableNsfw={site.enable_nsfw}
325 ) : this.state.commentsLoading ? (
331 nodes={commentsToFlatNodes(this.state.comments)}
334 enableDownvotes={site.enable_downvotes}
340 let community = this.state.communityRes.community_view.community;
343 <BannerIconHeader banner={community.banner} icon={community.icon} />
344 <h5 class="mb-0 overflow-wrap-anywhere">{community.title}</h5>
346 community={community}
361 type_={this.state.dataType}
362 onChange={this.handleDataTypeChange}
366 <SortSelect sort={this.state.sort} onChange={this.handleSortChange} />
369 href={communityRSSUrl(
370 this.state.communityRes.community_view.community.actor_id,
376 <Icon icon="rss" classes="text-muted small" />
382 handlePageChange(page: number) {
383 this.updateUrl({ page });
384 window.scrollTo(0, 0);
387 handleSortChange(val: SortType) {
388 this.updateUrl({ sort: val, page: 1 });
389 window.scrollTo(0, 0);
392 handleDataTypeChange(val: DataType) {
393 this.updateUrl({ dataType: DataType[val], page: 1 });
394 window.scrollTo(0, 0);
397 handleShowSidebarMobile(i: Community) {
398 i.state.showSidebarMobile = !i.state.showSidebarMobile;
402 updateUrl(paramUpdates: UrlParams) {
403 const dataTypeStr = paramUpdates.dataType || DataType[this.state.dataType];
404 const sortStr = paramUpdates.sort || this.state.sort;
405 const page = paramUpdates.page || this.state.page;
407 let typeView = `/c/${this.state.communityName}`;
409 this.props.history.push(
410 `${typeView}/data_type/${dataTypeStr}/sort/${sortStr}/page/${page}`
415 if (this.state.dataType == DataType.Post) {
416 let form: GetPosts = {
417 page: this.state.page,
419 sort: this.state.sort,
420 type_: ListingType.Community,
421 community_name: this.state.communityName,
423 auth: authField(false),
425 WebSocketService.Instance.send(wsClient.getPosts(form));
427 let form: GetComments = {
428 page: this.state.page,
430 sort: this.state.sort,
431 type_: ListingType.Community,
432 community_name: this.state.communityName,
434 auth: authField(false),
436 WebSocketService.Instance.send(wsClient.getComments(form));
440 parseMessage(msg: any) {
441 let op = wsUserOp(msg);
444 toast(i18n.t(msg.error), "danger");
445 this.context.router.history.push("/");
447 } else if (msg.reconnect) {
448 WebSocketService.Instance.send(
449 wsClient.communityJoin({
450 community_id: this.state.communityRes.community_view.community.id,
454 } else if (op == UserOperation.GetCommunity) {
455 let data = wsJsonToRes<GetCommunityResponse>(msg).data;
456 this.state.communityRes = data;
457 this.state.communityLoading = false;
458 this.setState(this.state);
459 // TODO why is there no auth in this form?
460 WebSocketService.Instance.send(
461 wsClient.communityJoin({
462 community_id: data.community_view.community.id,
466 op == UserOperation.EditCommunity ||
467 op == UserOperation.DeleteCommunity ||
468 op == UserOperation.RemoveCommunity
470 let data = wsJsonToRes<CommunityResponse>(msg).data;
471 this.state.communityRes.community_view = data.community_view;
472 this.setState(this.state);
473 } else if (op == UserOperation.FollowCommunity) {
474 let data = wsJsonToRes<CommunityResponse>(msg).data;
475 this.state.communityRes.community_view.subscribed =
476 data.community_view.subscribed;
477 this.state.communityRes.community_view.counts.subscribers =
478 data.community_view.counts.subscribers;
479 this.setState(this.state);
480 } else if (op == UserOperation.GetPosts) {
481 let data = wsJsonToRes<GetPostsResponse>(msg).data;
482 this.state.posts = data.posts;
483 this.state.postsLoading = false;
484 this.setState(this.state);
485 restoreScrollPosition(this.context);
488 op == UserOperation.EditPost ||
489 op == UserOperation.DeletePost ||
490 op == UserOperation.RemovePost ||
491 op == UserOperation.LockPost ||
492 op == UserOperation.StickyPost ||
493 op == UserOperation.SavePost
495 let data = wsJsonToRes<PostResponse>(msg).data;
496 editPostFindRes(data.post_view, this.state.posts);
497 this.setState(this.state);
498 } else if (op == UserOperation.CreatePost) {
499 let data = wsJsonToRes<PostResponse>(msg).data;
500 this.state.posts.unshift(data.post_view);
502 UserService.Instance.myUserInfo?.local_user_view.local_user
503 .show_new_post_notifs
505 notifyPost(data.post_view, this.context.router);
507 this.setState(this.state);
508 } else if (op == UserOperation.CreatePostLike) {
509 let data = wsJsonToRes<PostResponse>(msg).data;
510 createPostLikeFindRes(data.post_view, this.state.posts);
511 this.setState(this.state);
512 } else if (op == UserOperation.AddModToCommunity) {
513 let data = wsJsonToRes<AddModToCommunityResponse>(msg).data;
514 this.state.communityRes.moderators = data.moderators;
515 this.setState(this.state);
516 } else if (op == UserOperation.BanFromCommunity) {
517 let data = wsJsonToRes<BanFromCommunityResponse>(msg).data;
519 // TODO this might be incorrect
521 .filter(p => p.creator.id == data.person_view.person.id)
522 .forEach(p => (p.creator_banned_from_community = data.banned));
524 this.setState(this.state);
525 } else if (op == UserOperation.GetComments) {
526 let data = wsJsonToRes<GetCommentsResponse>(msg).data;
527 this.state.comments = data.comments;
528 this.state.commentsLoading = false;
529 this.setState(this.state);
531 op == UserOperation.EditComment ||
532 op == UserOperation.DeleteComment ||
533 op == UserOperation.RemoveComment
535 let data = wsJsonToRes<CommentResponse>(msg).data;
536 editCommentRes(data.comment_view, this.state.comments);
537 this.setState(this.state);
538 } else if (op == UserOperation.CreateComment) {
539 let data = wsJsonToRes<CommentResponse>(msg).data;
541 // Necessary since it might be a user reply
543 this.state.comments.unshift(data.comment_view);
544 this.setState(this.state);
546 } else if (op == UserOperation.SaveComment) {
547 let data = wsJsonToRes<CommentResponse>(msg).data;
548 saveCommentRes(data.comment_view, this.state.comments);
549 this.setState(this.state);
550 } else if (op == UserOperation.CreateCommentLike) {
551 let data = wsJsonToRes<CommentResponse>(msg).data;
552 createCommentLikeRes(data.comment_view, this.state.comments);
553 this.setState(this.state);
554 } else if (op == UserOperation.BlockPerson) {
555 let data = wsJsonToRes<BlockPersonResponse>(msg).data;
556 updatePersonBlock(data);
557 } else if (op == UserOperation.CreatePostReport) {
558 let data = wsJsonToRes<PostReportResponse>(msg).data;
560 toast(i18n.t("report_created"));
562 } else if (op == UserOperation.CreateCommentReport) {
563 let data = wsJsonToRes<CommentReportResponse>(msg).data;
565 toast(i18n.t("report_created"));