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();
140 let form: GetCommunity = {
141 name: this.state.communityName ? this.state.communityName : null,
142 auth: authField(false),
144 WebSocketService.Instance.send(wsClient.getCommunity(form));
147 componentWillUnmount() {
148 saveScrollPosition(this.context);
149 this.subscription.unsubscribe();
150 window.isoData.path = undefined;
153 static getDerivedStateFromProps(props: any): CommunityProps {
155 dataType: getDataTypeFromProps(props),
156 sort: getSortTypeFromProps(props),
157 page: getPageFromProps(props),
161 static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
162 let pathSplit = req.path.split("/");
163 let promises: Promise<any>[] = [];
165 // It can be /c/main, or /c/1
166 let idOrName = pathSplit[2];
169 if (isNaN(Number(idOrName))) {
172 id = Number(idOrName);
175 let communityForm: GetCommunity = id ? { id } : { name: name_ };
176 setOptionalAuth(communityForm, req.auth);
177 promises.push(req.client.getCommunity(communityForm));
179 let dataType: DataType = pathSplit[4]
180 ? DataType[pathSplit[4]]
183 let sort: SortType = pathSplit[6]
184 ? SortType[pathSplit[6]]
185 : UserService.Instance.myUserInfo
186 ? Object.values(SortType)[
187 UserService.Instance.myUserInfo.local_user_view.local_user
192 let page = pathSplit[8] ? Number(pathSplit[8]) : 1;
194 if (dataType == DataType.Post) {
195 let getPostsForm: GetPosts = {
199 type_: ListingType.Community,
202 setOptionalAuth(getPostsForm, req.auth);
203 this.setName(getPostsForm, name_);
204 promises.push(req.client.getPosts(getPostsForm));
206 let getCommentsForm: GetComments = {
210 type_: ListingType.Community,
213 setOptionalAuth(getCommentsForm, req.auth);
214 promises.push(req.client.getComments(getCommentsForm));
220 static setName(obj: any, name_: string) {
221 obj.community_name = name_;
224 componentDidUpdate(_: any, lastState: State) {
226 lastState.dataType !== this.state.dataType ||
227 lastState.sort !== this.state.sort ||
228 lastState.page !== this.state.page
230 this.setState({ postsLoading: true, commentsLoading: true });
235 get documentTitle(): string {
236 return `${this.state.communityRes.community_view.community.title} - ${this.state.siteRes.site_view.site.name}`;
240 let cv = this.state.communityRes?.community_view;
242 <div class="container">
243 {this.state.communityLoading ? (
250 title={this.documentTitle}
251 path={this.context.router.route.match.url}
252 description={cv.community.description}
253 image={cv.community.icon}
257 <div class="col-12 col-md-8">
258 {this.communityInfo()}
259 <div class="d-block d-md-none">
261 class="btn btn-secondary d-inline-block mb-2 mr-3"
262 onClick={linkEvent(this, this.handleShowSidebarMobile)}
264 {i18n.t("sidebar")}{" "}
267 this.state.showSidebarMobile
271 classes="icon-inline"
274 {this.state.showSidebarMobile && (
277 moderators={this.state.communityRes.moderators}
278 admins={this.state.siteRes.admins}
279 online={this.state.communityRes.online}
280 enableNsfw={this.state.siteRes.site_view.site.enable_nsfw}
287 page={this.state.page}
288 onChange={this.handlePageChange}
291 <div class="d-none d-md-block col-md-4">
294 moderators={this.state.communityRes.moderators}
295 admins={this.state.siteRes.admins}
296 online={this.state.communityRes.online}
297 enableNsfw={this.state.siteRes.site_view.site.enable_nsfw}
308 let site = this.state.siteRes.site_view.site;
309 return this.state.dataType == DataType.Post ? (
310 this.state.postsLoading ? (
316 posts={this.state.posts}
318 enableDownvotes={site.enable_downvotes}
319 enableNsfw={site.enable_nsfw}
322 ) : this.state.commentsLoading ? (
328 nodes={commentsToFlatNodes(this.state.comments)}
331 enableDownvotes={site.enable_downvotes}
337 let community = this.state.communityRes.community_view.community;
340 <BannerIconHeader banner={community.banner} icon={community.icon} />
341 <h5 class="mb-0 overflow-wrap-anywhere">{community.title}</h5>
343 community={community}
358 type_={this.state.dataType}
359 onChange={this.handleDataTypeChange}
363 <SortSelect sort={this.state.sort} onChange={this.handleSortChange} />
366 href={communityRSSUrl(
367 this.state.communityRes.community_view.community.actor_id,
373 <Icon icon="rss" classes="text-muted small" />
379 handlePageChange(page: number) {
380 this.updateUrl({ page });
381 window.scrollTo(0, 0);
384 handleSortChange(val: SortType) {
385 this.updateUrl({ sort: val, page: 1 });
386 window.scrollTo(0, 0);
389 handleDataTypeChange(val: DataType) {
390 this.updateUrl({ dataType: DataType[val], page: 1 });
391 window.scrollTo(0, 0);
394 handleShowSidebarMobile(i: Community) {
395 i.state.showSidebarMobile = !i.state.showSidebarMobile;
399 updateUrl(paramUpdates: UrlParams) {
400 const dataTypeStr = paramUpdates.dataType || DataType[this.state.dataType];
401 const sortStr = paramUpdates.sort || this.state.sort;
402 const page = paramUpdates.page || this.state.page;
404 let typeView = `/c/${this.state.communityName}`;
406 this.props.history.push(
407 `${typeView}/data_type/${dataTypeStr}/sort/${sortStr}/page/${page}`
412 if (this.state.dataType == DataType.Post) {
413 let form: GetPosts = {
414 page: this.state.page,
416 sort: this.state.sort,
417 type_: ListingType.Community,
418 community_name: this.state.communityName,
420 auth: authField(false),
422 WebSocketService.Instance.send(wsClient.getPosts(form));
424 let form: GetComments = {
425 page: this.state.page,
427 sort: this.state.sort,
428 type_: ListingType.Community,
429 community_name: this.state.communityName,
431 auth: authField(false),
433 WebSocketService.Instance.send(wsClient.getComments(form));
437 parseMessage(msg: any) {
438 let op = wsUserOp(msg);
441 toast(i18n.t(msg.error), "danger");
442 this.context.router.history.push("/");
444 } else if (msg.reconnect) {
445 WebSocketService.Instance.send(
446 wsClient.communityJoin({
447 community_id: this.state.communityRes.community_view.community.id,
451 } else if (op == UserOperation.GetCommunity) {
452 let data = wsJsonToRes<GetCommunityResponse>(msg).data;
453 this.state.communityRes = data;
454 this.state.communityLoading = false;
455 this.setState(this.state);
456 // TODO why is there no auth in this form?
457 WebSocketService.Instance.send(
458 wsClient.communityJoin({
459 community_id: data.community_view.community.id,
463 op == UserOperation.EditCommunity ||
464 op == UserOperation.DeleteCommunity ||
465 op == UserOperation.RemoveCommunity
467 let data = wsJsonToRes<CommunityResponse>(msg).data;
468 this.state.communityRes.community_view = data.community_view;
469 this.setState(this.state);
470 } else if (op == UserOperation.FollowCommunity) {
471 let data = wsJsonToRes<CommunityResponse>(msg).data;
472 this.state.communityRes.community_view.subscribed =
473 data.community_view.subscribed;
474 this.state.communityRes.community_view.counts.subscribers =
475 data.community_view.counts.subscribers;
476 this.setState(this.state);
477 } else if (op == UserOperation.GetPosts) {
478 let data = wsJsonToRes<GetPostsResponse>(msg).data;
479 this.state.posts = data.posts;
480 this.state.postsLoading = false;
481 this.setState(this.state);
482 restoreScrollPosition(this.context);
485 op == UserOperation.EditPost ||
486 op == UserOperation.DeletePost ||
487 op == UserOperation.RemovePost ||
488 op == UserOperation.LockPost ||
489 op == UserOperation.StickyPost ||
490 op == UserOperation.SavePost
492 let data = wsJsonToRes<PostResponse>(msg).data;
493 editPostFindRes(data.post_view, this.state.posts);
494 this.setState(this.state);
495 } else if (op == UserOperation.CreatePost) {
496 let data = wsJsonToRes<PostResponse>(msg).data;
497 this.state.posts.unshift(data.post_view);
499 UserService.Instance.myUserInfo?.local_user_view.local_user
500 .show_new_post_notifs
502 notifyPost(data.post_view, this.context.router);
504 this.setState(this.state);
505 } else if (op == UserOperation.CreatePostLike) {
506 let data = wsJsonToRes<PostResponse>(msg).data;
507 createPostLikeFindRes(data.post_view, this.state.posts);
508 this.setState(this.state);
509 } else if (op == UserOperation.AddModToCommunity) {
510 let data = wsJsonToRes<AddModToCommunityResponse>(msg).data;
511 this.state.communityRes.moderators = data.moderators;
512 this.setState(this.state);
513 } else if (op == UserOperation.BanFromCommunity) {
514 let data = wsJsonToRes<BanFromCommunityResponse>(msg).data;
516 // TODO this might be incorrect
518 .filter(p => p.creator.id == data.person_view.person.id)
519 .forEach(p => (p.creator_banned_from_community = data.banned));
521 this.setState(this.state);
522 } else if (op == UserOperation.GetComments) {
523 let data = wsJsonToRes<GetCommentsResponse>(msg).data;
524 this.state.comments = data.comments;
525 this.state.commentsLoading = false;
526 this.setState(this.state);
528 op == UserOperation.EditComment ||
529 op == UserOperation.DeleteComment ||
530 op == UserOperation.RemoveComment
532 let data = wsJsonToRes<CommentResponse>(msg).data;
533 editCommentRes(data.comment_view, this.state.comments);
534 this.setState(this.state);
535 } else if (op == UserOperation.CreateComment) {
536 let data = wsJsonToRes<CommentResponse>(msg).data;
538 // Necessary since it might be a user reply
540 this.state.comments.unshift(data.comment_view);
541 this.setState(this.state);
543 } else if (op == UserOperation.SaveComment) {
544 let data = wsJsonToRes<CommentResponse>(msg).data;
545 saveCommentRes(data.comment_view, this.state.comments);
546 this.setState(this.state);
547 } else if (op == UserOperation.CreateCommentLike) {
548 let data = wsJsonToRes<CommentResponse>(msg).data;
549 createCommentLikeRes(data.comment_view, this.state.comments);
550 this.setState(this.state);
551 } else if (op == UserOperation.BlockPerson) {
552 let data = wsJsonToRes<BlockPersonResponse>(msg).data;
553 updatePersonBlock(data);
554 } else if (op == UserOperation.CreatePostReport) {
555 let data = wsJsonToRes<PostReportResponse>(msg).data;
557 toast(i18n.t("report_created"));
559 } else if (op == UserOperation.CreateCommentReport) {
560 let data = wsJsonToRes<CommentReportResponse>(msg).data;
562 toast(i18n.t("report_created"));