1 import { Component, linkEvent } from "inferno";
3 AddModToCommunityResponse,
4 BanFromCommunityResponse,
21 } from "lemmy-js-client";
22 import { Subscription } from "rxjs";
23 import { i18n } from "../../i18next";
24 import { DataType, InitialFetchRequest } from "../../interfaces";
25 import { UserService, WebSocketService } from "../../services";
31 createPostLikeFindRes,
39 restoreScrollPosition,
52 import { CommentNodes } from "../comment/comment-nodes";
53 import { BannerIconHeader } from "../common/banner-icon-header";
54 import { DataTypeSelect } from "../common/data-type-select";
55 import { HtmlTags } from "../common/html-tags";
56 import { Icon, Spinner } from "../common/icon";
57 import { Paginator } from "../common/paginator";
58 import { SortSelect } from "../common/sort-select";
59 import { Sidebar } from "../community/sidebar";
60 import { PostListings } from "../post/post-listings";
61 import { CommunityLink } from "./community-link";
64 communityRes: GetCommunityResponse;
65 siteRes: GetSiteResponse;
66 communityName: string;
67 communityLoading: boolean;
68 postsLoading: boolean;
69 commentsLoading: boolean;
71 comments: CommentView[];
75 showSidebarMobile: boolean;
78 interface CommunityProps {
90 export class Community extends Component<any, State> {
91 private isoData = setIsoData(this.context);
92 private subscription: Subscription;
93 private emptyState: State = {
94 communityRes: undefined,
95 communityName: this.props.match.params.name,
96 communityLoading: true,
98 commentsLoading: true,
101 dataType: getDataTypeFromProps(this.props),
102 sort: getSortTypeFromProps(this.props),
103 page: getPageFromProps(this.props),
104 siteRes: this.isoData.site_res,
105 showSidebarMobile: false,
108 constructor(props: any, context: any) {
109 super(props, context);
111 this.state = this.emptyState;
112 this.handleSortChange = this.handleSortChange.bind(this);
113 this.handleDataTypeChange = this.handleDataTypeChange.bind(this);
114 this.handlePageChange = this.handlePageChange.bind(this);
116 this.parseMessage = this.parseMessage.bind(this);
117 this.subscription = wsSubscribe(this.parseMessage);
119 // Only fetch the data if coming from another route
120 if (this.isoData.path == this.context.router.route.match.url) {
121 this.state.communityRes = this.isoData.routeData[0];
122 if (this.state.dataType == DataType.Post) {
123 this.state.posts = this.isoData.routeData[1].posts;
125 this.state.comments = this.isoData.routeData[1].comments;
127 this.state.communityLoading = false;
128 this.state.postsLoading = false;
129 this.state.commentsLoading = false;
131 this.fetchCommunity();
138 let form: GetCommunity = {
139 name: this.state.communityName ? this.state.communityName : null,
140 auth: authField(false),
142 WebSocketService.Instance.send(wsClient.getCommunity(form));
145 componentWillUnmount() {
146 saveScrollPosition(this.context);
147 this.subscription.unsubscribe();
148 window.isoData.path = undefined;
151 static getDerivedStateFromProps(props: any): CommunityProps {
153 dataType: getDataTypeFromProps(props),
154 sort: getSortTypeFromProps(props),
155 page: getPageFromProps(props),
159 static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
160 let pathSplit = req.path.split("/");
161 let promises: Promise<any>[] = [];
163 // It can be /c/main, or /c/1
164 let idOrName = pathSplit[2];
167 if (isNaN(Number(idOrName))) {
170 id = Number(idOrName);
173 let communityForm: GetCommunity = id ? { id } : { name: name_ };
174 setOptionalAuth(communityForm, req.auth);
175 promises.push(req.client.getCommunity(communityForm));
177 let dataType: DataType = pathSplit[4]
178 ? DataType[pathSplit[4]]
181 let sort: SortType = pathSplit[6]
182 ? SortType[pathSplit[6]]
183 : UserService.Instance.myUserInfo
184 ? Object.values(SortType)[
185 UserService.Instance.myUserInfo.local_user_view.local_user
190 let page = pathSplit[8] ? Number(pathSplit[8]) : 1;
192 if (dataType == DataType.Post) {
193 let getPostsForm: GetPosts = {
197 type_: ListingType.Community,
200 setOptionalAuth(getPostsForm, req.auth);
201 this.setName(getPostsForm, name_);
202 promises.push(req.client.getPosts(getPostsForm));
204 let getCommentsForm: GetComments = {
208 type_: ListingType.Community,
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 && (
275 moderators={this.state.communityRes.moderators}
276 admins={this.state.siteRes.admins}
277 online={this.state.communityRes.online}
278 enableNsfw={this.state.siteRes.site_view.site.enable_nsfw}
285 page={this.state.page}
286 onChange={this.handlePageChange}
289 <div class="d-none d-md-block col-md-4">
292 moderators={this.state.communityRes.moderators}
293 admins={this.state.siteRes.admins}
294 online={this.state.communityRes.online}
295 enableNsfw={this.state.siteRes.site_view.site.enable_nsfw}
306 let site = this.state.siteRes.site_view.site;
307 return this.state.dataType == DataType.Post ? (
308 this.state.postsLoading ? (
314 posts={this.state.posts}
316 enableDownvotes={site.enable_downvotes}
317 enableNsfw={site.enable_nsfw}
320 ) : this.state.commentsLoading ? (
326 nodes={commentsToFlatNodes(this.state.comments)}
329 enableDownvotes={site.enable_downvotes}
335 let community = this.state.communityRes.community_view.community;
338 <BannerIconHeader banner={community.banner} icon={community.icon} />
339 <h5 class="mb-0">{community.title}</h5>
341 community={community}
356 type_={this.state.dataType}
357 onChange={this.handleDataTypeChange}
361 <SortSelect sort={this.state.sort} onChange={this.handleSortChange} />
364 href={communityRSSUrl(
365 this.state.communityRes.community_view.community.actor_id,
371 <Icon icon="rss" classes="text-muted small" />
377 handlePageChange(page: number) {
378 this.updateUrl({ page });
379 window.scrollTo(0, 0);
382 handleSortChange(val: SortType) {
383 this.updateUrl({ sort: val, page: 1 });
384 window.scrollTo(0, 0);
387 handleDataTypeChange(val: DataType) {
388 this.updateUrl({ dataType: DataType[val], page: 1 });
389 window.scrollTo(0, 0);
392 handleShowSidebarMobile(i: Community) {
393 i.state.showSidebarMobile = !i.state.showSidebarMobile;
397 updateUrl(paramUpdates: UrlParams) {
398 const dataTypeStr = paramUpdates.dataType || DataType[this.state.dataType];
399 const sortStr = paramUpdates.sort || this.state.sort;
400 const page = paramUpdates.page || this.state.page;
402 let typeView = `/c/${this.state.communityName}`;
404 this.props.history.push(
405 `${typeView}/data_type/${dataTypeStr}/sort/${sortStr}/page/${page}`
410 if (this.state.dataType == DataType.Post) {
411 let form: GetPosts = {
412 page: this.state.page,
414 sort: this.state.sort,
415 type_: ListingType.Community,
416 community_name: this.state.communityName,
418 auth: authField(false),
420 WebSocketService.Instance.send(wsClient.getPosts(form));
422 let form: GetComments = {
423 page: this.state.page,
425 sort: this.state.sort,
426 type_: ListingType.Community,
427 community_name: this.state.communityName,
429 auth: authField(false),
431 WebSocketService.Instance.send(wsClient.getComments(form));
435 parseMessage(msg: any) {
436 let op = wsUserOp(msg);
439 toast(i18n.t(msg.error), "danger");
440 this.context.router.history.push("/");
442 } else if (msg.reconnect) {
443 WebSocketService.Instance.send(
444 wsClient.communityJoin({
445 community_id: this.state.communityRes.community_view.community.id,
449 } else if (op == UserOperation.GetCommunity) {
450 let data = wsJsonToRes<GetCommunityResponse>(msg).data;
451 this.state.communityRes = data;
452 this.state.communityLoading = false;
453 this.setState(this.state);
454 // TODO why is there no auth in this form?
455 WebSocketService.Instance.send(
456 wsClient.communityJoin({
457 community_id: data.community_view.community.id,
461 op == UserOperation.EditCommunity ||
462 op == UserOperation.DeleteCommunity ||
463 op == UserOperation.RemoveCommunity
465 let data = wsJsonToRes<CommunityResponse>(msg).data;
466 this.state.communityRes.community_view = data.community_view;
467 this.setState(this.state);
468 } else if (op == UserOperation.FollowCommunity) {
469 let data = wsJsonToRes<CommunityResponse>(msg).data;
470 this.state.communityRes.community_view.subscribed =
471 data.community_view.subscribed;
472 this.state.communityRes.community_view.counts.subscribers =
473 data.community_view.counts.subscribers;
474 this.setState(this.state);
475 } else if (op == UserOperation.GetPosts) {
476 let data = wsJsonToRes<GetPostsResponse>(msg).data;
477 this.state.posts = data.posts;
478 this.state.postsLoading = false;
479 this.setState(this.state);
480 restoreScrollPosition(this.context);
483 op == UserOperation.EditPost ||
484 op == UserOperation.DeletePost ||
485 op == UserOperation.RemovePost ||
486 op == UserOperation.LockPost ||
487 op == UserOperation.StickyPost ||
488 op == UserOperation.SavePost
490 let data = wsJsonToRes<PostResponse>(msg).data;
491 editPostFindRes(data.post_view, this.state.posts);
492 this.setState(this.state);
493 } else if (op == UserOperation.CreatePost) {
494 let data = wsJsonToRes<PostResponse>(msg).data;
495 this.state.posts.unshift(data.post_view);
497 UserService.Instance.myUserInfo?.local_user_view.local_user
498 .show_new_post_notifs
500 notifyPost(data.post_view, this.context.router);
502 this.setState(this.state);
503 } else if (op == UserOperation.CreatePostLike) {
504 let data = wsJsonToRes<PostResponse>(msg).data;
505 createPostLikeFindRes(data.post_view, this.state.posts);
506 this.setState(this.state);
507 } else if (op == UserOperation.AddModToCommunity) {
508 let data = wsJsonToRes<AddModToCommunityResponse>(msg).data;
509 this.state.communityRes.moderators = data.moderators;
510 this.setState(this.state);
511 } else if (op == UserOperation.BanFromCommunity) {
512 let data = wsJsonToRes<BanFromCommunityResponse>(msg).data;
514 // TODO this might be incorrect
516 .filter(p => p.creator.id == data.person_view.person.id)
517 .forEach(p => (p.creator_banned_from_community = data.banned));
519 this.setState(this.state);
520 } else if (op == UserOperation.GetComments) {
521 let data = wsJsonToRes<GetCommentsResponse>(msg).data;
522 this.state.comments = data.comments;
523 this.state.commentsLoading = false;
524 this.setState(this.state);
526 op == UserOperation.EditComment ||
527 op == UserOperation.DeleteComment ||
528 op == UserOperation.RemoveComment
530 let data = wsJsonToRes<CommentResponse>(msg).data;
531 editCommentRes(data.comment_view, this.state.comments);
532 this.setState(this.state);
533 } else if (op == UserOperation.CreateComment) {
534 let data = wsJsonToRes<CommentResponse>(msg).data;
536 // Necessary since it might be a user reply
538 this.state.comments.unshift(data.comment_view);
539 this.setState(this.state);
541 } else if (op == UserOperation.SaveComment) {
542 let data = wsJsonToRes<CommentResponse>(msg).data;
543 saveCommentRes(data.comment_view, this.state.comments);
544 this.setState(this.state);
545 } else if (op == UserOperation.CreateCommentLike) {
546 let data = wsJsonToRes<CommentResponse>(msg).data;
547 createCommentLikeRes(data.comment_view, this.state.comments);
548 this.setState(this.state);
549 } else if (op == UserOperation.BlockPerson) {
550 let data = wsJsonToRes<BlockPersonResponse>(msg).data;
551 updatePersonBlock(data);