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 this.setName(getCommentsForm, name_);
217 setOptionalAuth(getCommentsForm, req.auth);
218 promises.push(req.client.getComments(getCommentsForm));
224 static setName(obj: any, name_: string) {
225 obj.community_name = name_;
228 componentDidUpdate(_: any, lastState: State) {
230 lastState.dataType !== this.state.dataType ||
231 lastState.sort !== this.state.sort ||
232 lastState.page !== this.state.page
234 this.setState({ postsLoading: true, commentsLoading: true });
239 get documentTitle(): string {
240 return `${this.state.communityRes.community_view.community.title} - ${this.state.siteRes.site_view.site.name}`;
244 let cv = this.state.communityRes?.community_view;
246 <div class="container">
247 {this.state.communityLoading ? (
254 title={this.documentTitle}
255 path={this.context.router.route.match.url}
256 description={cv.community.description}
257 image={cv.community.icon}
261 <div class="col-12 col-md-8">
262 {this.communityInfo()}
263 <div class="d-block d-md-none">
265 class="btn btn-secondary d-inline-block mb-2 mr-3"
266 onClick={linkEvent(this, this.handleShowSidebarMobile)}
268 {i18n.t("sidebar")}{" "}
271 this.state.showSidebarMobile
275 classes="icon-inline"
278 {this.state.showSidebarMobile && (
281 moderators={this.state.communityRes.moderators}
282 admins={this.state.siteRes.admins}
283 online={this.state.communityRes.online}
284 enableNsfw={this.state.siteRes.site_view.site.enable_nsfw}
291 page={this.state.page}
292 onChange={this.handlePageChange}
295 <div class="d-none d-md-block col-md-4">
298 moderators={this.state.communityRes.moderators}
299 admins={this.state.siteRes.admins}
300 online={this.state.communityRes.online}
301 enableNsfw={this.state.siteRes.site_view.site.enable_nsfw}
312 let site = this.state.siteRes.site_view.site;
313 return this.state.dataType == DataType.Post ? (
314 this.state.postsLoading ? (
320 posts={this.state.posts}
322 enableDownvotes={site.enable_downvotes}
323 enableNsfw={site.enable_nsfw}
326 ) : this.state.commentsLoading ? (
332 nodes={commentsToFlatNodes(this.state.comments)}
335 enableDownvotes={site.enable_downvotes}
341 let community = this.state.communityRes.community_view.community;
344 <BannerIconHeader banner={community.banner} icon={community.icon} />
345 <h5 class="mb-0 overflow-wrap-anywhere">{community.title}</h5>
347 community={community}
358 let communityRss = communityRSSUrl(
359 this.state.communityRes.community_view.community.actor_id,
366 type_={this.state.dataType}
367 onChange={this.handleDataTypeChange}
371 <SortSelect sort={this.state.sort} onChange={this.handleSortChange} />
373 <a href={communityRss} title="RSS" rel="noopener">
374 <Icon icon="rss" classes="text-muted small" />
376 <link rel="alternate" type="application/atom+xml" href={communityRss} />
381 handlePageChange(page: number) {
382 this.updateUrl({ page });
383 window.scrollTo(0, 0);
386 handleSortChange(val: SortType) {
387 this.updateUrl({ sort: val, page: 1 });
388 window.scrollTo(0, 0);
391 handleDataTypeChange(val: DataType) {
392 this.updateUrl({ dataType: DataType[val], page: 1 });
393 window.scrollTo(0, 0);
396 handleShowSidebarMobile(i: Community) {
397 i.state.showSidebarMobile = !i.state.showSidebarMobile;
401 updateUrl(paramUpdates: UrlParams) {
402 const dataTypeStr = paramUpdates.dataType || DataType[this.state.dataType];
403 const sortStr = paramUpdates.sort || this.state.sort;
404 const page = paramUpdates.page || this.state.page;
406 let typeView = `/c/${this.state.communityName}`;
408 this.props.history.push(
409 `${typeView}/data_type/${dataTypeStr}/sort/${sortStr}/page/${page}`
414 if (this.state.dataType == DataType.Post) {
415 let form: GetPosts = {
416 page: this.state.page,
418 sort: this.state.sort,
419 type_: ListingType.Community,
420 community_name: this.state.communityName,
422 auth: authField(false),
424 WebSocketService.Instance.send(wsClient.getPosts(form));
426 let form: GetComments = {
427 page: this.state.page,
429 sort: this.state.sort,
430 type_: ListingType.Community,
431 community_name: this.state.communityName,
433 auth: authField(false),
435 WebSocketService.Instance.send(wsClient.getComments(form));
439 parseMessage(msg: any) {
440 let op = wsUserOp(msg);
443 toast(i18n.t(msg.error), "danger");
444 this.context.router.history.push("/");
446 } else if (msg.reconnect) {
447 WebSocketService.Instance.send(
448 wsClient.communityJoin({
449 community_id: this.state.communityRes.community_view.community.id,
453 } else if (op == UserOperation.GetCommunity) {
454 let data = wsJsonToRes<GetCommunityResponse>(msg).data;
455 this.state.communityRes = data;
456 this.state.communityLoading = false;
457 this.setState(this.state);
458 // TODO why is there no auth in this form?
459 WebSocketService.Instance.send(
460 wsClient.communityJoin({
461 community_id: data.community_view.community.id,
465 op == UserOperation.EditCommunity ||
466 op == UserOperation.DeleteCommunity ||
467 op == UserOperation.RemoveCommunity
469 let data = wsJsonToRes<CommunityResponse>(msg).data;
470 this.state.communityRes.community_view = data.community_view;
471 this.setState(this.state);
472 } else if (op == UserOperation.FollowCommunity) {
473 let data = wsJsonToRes<CommunityResponse>(msg).data;
474 this.state.communityRes.community_view.subscribed =
475 data.community_view.subscribed;
476 this.state.communityRes.community_view.counts.subscribers =
477 data.community_view.counts.subscribers;
478 this.setState(this.state);
479 } else if (op == UserOperation.GetPosts) {
480 let data = wsJsonToRes<GetPostsResponse>(msg).data;
481 this.state.posts = data.posts;
482 this.state.postsLoading = false;
483 this.setState(this.state);
484 restoreScrollPosition(this.context);
487 op == UserOperation.EditPost ||
488 op == UserOperation.DeletePost ||
489 op == UserOperation.RemovePost ||
490 op == UserOperation.LockPost ||
491 op == UserOperation.StickyPost ||
492 op == UserOperation.SavePost
494 let data = wsJsonToRes<PostResponse>(msg).data;
495 editPostFindRes(data.post_view, this.state.posts);
496 this.setState(this.state);
497 } else if (op == UserOperation.CreatePost) {
498 let data = wsJsonToRes<PostResponse>(msg).data;
499 this.state.posts.unshift(data.post_view);
501 UserService.Instance.myUserInfo?.local_user_view.local_user
502 .show_new_post_notifs
504 notifyPost(data.post_view, this.context.router);
506 this.setState(this.state);
507 } else if (op == UserOperation.CreatePostLike) {
508 let data = wsJsonToRes<PostResponse>(msg).data;
509 createPostLikeFindRes(data.post_view, this.state.posts);
510 this.setState(this.state);
511 } else if (op == UserOperation.AddModToCommunity) {
512 let data = wsJsonToRes<AddModToCommunityResponse>(msg).data;
513 this.state.communityRes.moderators = data.moderators;
514 this.setState(this.state);
515 } else if (op == UserOperation.BanFromCommunity) {
516 let data = wsJsonToRes<BanFromCommunityResponse>(msg).data;
518 // TODO this might be incorrect
520 .filter(p => p.creator.id == data.person_view.person.id)
521 .forEach(p => (p.creator_banned_from_community = data.banned));
523 this.setState(this.state);
524 } else if (op == UserOperation.GetComments) {
525 let data = wsJsonToRes<GetCommentsResponse>(msg).data;
526 this.state.comments = data.comments;
527 this.state.commentsLoading = false;
528 this.setState(this.state);
530 op == UserOperation.EditComment ||
531 op == UserOperation.DeleteComment ||
532 op == UserOperation.RemoveComment
534 let data = wsJsonToRes<CommentResponse>(msg).data;
535 editCommentRes(data.comment_view, this.state.comments);
536 this.setState(this.state);
537 } else if (op == UserOperation.CreateComment) {
538 let data = wsJsonToRes<CommentResponse>(msg).data;
540 // Necessary since it might be a user reply
542 this.state.comments.unshift(data.comment_view);
543 this.setState(this.state);
545 } else if (op == UserOperation.SaveComment) {
546 let data = wsJsonToRes<CommentResponse>(msg).data;
547 saveCommentRes(data.comment_view, this.state.comments);
548 this.setState(this.state);
549 } else if (op == UserOperation.CreateCommentLike) {
550 let data = wsJsonToRes<CommentResponse>(msg).data;
551 createCommentLikeRes(data.comment_view, this.state.comments);
552 this.setState(this.state);
553 } else if (op == UserOperation.BlockPerson) {
554 let data = wsJsonToRes<BlockPersonResponse>(msg).data;
555 updatePersonBlock(data);
556 } else if (op == UserOperation.CreatePostReport) {
557 let data = wsJsonToRes<PostReportResponse>(msg).data;
559 toast(i18n.t("report_created"));
561 } else if (op == UserOperation.CreateCommentReport) {
562 let data = wsJsonToRes<CommentReportResponse>(msg).data;
564 toast(i18n.t("report_created"));