1 import { Component, linkEvent } from "inferno";
3 AddModToCommunityResponse,
4 BanFromCommunityResponse,
5 BlockCommunityResponse,
27 } from "lemmy-js-client";
28 import { Subscription } from "rxjs";
29 import { i18n } from "../../i18next";
34 } from "../../interfaces";
35 import { UserService, WebSocketService } from "../../services";
40 createPostLikeFindRes,
53 postToCommentSortType,
55 restoreScrollPosition,
67 import { CommentNodes } from "../comment/comment-nodes";
68 import { BannerIconHeader } from "../common/banner-icon-header";
69 import { DataTypeSelect } from "../common/data-type-select";
70 import { HtmlTags } from "../common/html-tags";
71 import { Icon, Spinner } from "../common/icon";
72 import { Paginator } from "../common/paginator";
73 import { SortSelect } from "../common/sort-select";
74 import { Sidebar } from "../community/sidebar";
75 import { SiteSidebar } from "../home/site-sidebar";
76 import { PostListings } from "../post/post-listings";
77 import { CommunityLink } from "./community-link";
80 communityRes?: GetCommunityResponse;
81 siteRes: GetSiteResponse;
82 communityName: string;
83 communityLoading: boolean;
84 postsLoading: boolean;
85 commentsLoading: boolean;
87 comments: CommentView[];
91 showSidebarMobile: boolean;
94 interface CommunityProps {
100 interface UrlParams {
106 export class Community extends Component<any, State> {
107 private isoData = setIsoData(this.context);
108 private subscription?: Subscription;
110 communityName: this.props.match.params.name,
111 communityLoading: true,
113 commentsLoading: true,
116 dataType: getDataTypeFromProps(this.props),
117 sort: getSortTypeFromProps(this.props),
118 page: getPageFromProps(this.props),
119 siteRes: this.isoData.site_res,
120 showSidebarMobile: false,
123 constructor(props: any, context: any) {
124 super(props, context);
126 this.handleSortChange = this.handleSortChange.bind(this);
127 this.handleDataTypeChange = this.handleDataTypeChange.bind(this);
128 this.handlePageChange = this.handlePageChange.bind(this);
130 this.parseMessage = this.parseMessage.bind(this);
131 this.subscription = wsSubscribe(this.parseMessage);
133 // Only fetch the data if coming from another route
134 if (this.isoData.path == this.context.router.route.match.url) {
137 communityRes: this.isoData.routeData[0] as GetCommunityResponse,
139 let postsRes = this.isoData.routeData[1] as GetPostsResponse | undefined;
140 let commentsRes = this.isoData.routeData[2] as
141 | GetCommentsResponse
145 this.state = { ...this.state, posts: postsRes.posts };
149 this.state = { ...this.state, comments: commentsRes.comments };
154 communityLoading: false,
156 commentsLoading: false,
159 this.fetchCommunity();
165 let form: GetCommunity = {
166 name: this.state.communityName,
169 WebSocketService.Instance.send(wsClient.getCommunity(form));
172 componentDidMount() {
176 componentWillUnmount() {
177 saveScrollPosition(this.context);
178 this.subscription?.unsubscribe();
181 static getDerivedStateFromProps(props: any): CommunityProps {
183 dataType: getDataTypeFromProps(props),
184 sort: getSortTypeFromProps(props),
185 page: getPageFromProps(props),
189 static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
190 let pathSplit = req.path.split("/");
191 let promises: Promise<any>[] = [];
193 let communityName = pathSplit[2];
194 let communityForm: GetCommunity = {
198 promises.push(req.client.getCommunity(communityForm));
200 let dataType: DataType = pathSplit[4]
201 ? DataType[pathSplit[4]]
204 let mui = UserService.Instance.myUserInfo;
206 let sort: SortType = pathSplit[6]
207 ? SortType[pathSplit[6]]
209 ? Object.values(SortType)[
210 mui.local_user_view.local_user.default_sort_type
214 let page = pathSplit[8] ? Number(pathSplit[8]) : 1;
216 if (dataType == DataType.Post) {
217 let getPostsForm: GetPosts = {
218 community_name: communityName,
222 type_: ListingType.All,
226 promises.push(req.client.getPosts(getPostsForm));
227 promises.push(Promise.resolve());
229 let getCommentsForm: GetComments = {
230 community_name: communityName,
233 sort: postToCommentSortType(sort),
234 type_: ListingType.All,
238 promises.push(Promise.resolve());
239 promises.push(req.client.getComments(getCommentsForm));
245 componentDidUpdate(_: any, lastState: State) {
247 lastState.dataType !== this.state.dataType ||
248 lastState.sort !== this.state.sort ||
249 lastState.page !== this.state.page
251 this.setState({ postsLoading: true, commentsLoading: true });
256 get documentTitle(): string {
257 let cRes = this.state.communityRes;
259 ? `${cRes.community_view.community.title} - ${this.state.siteRes.site_view.site.name}`
264 // For some reason, this returns an empty vec if it matches the site langs
265 let res = this.state.communityRes;
267 res?.discussion_languages.length == 0
268 ? this.state.siteRes.all_languages.map(l => l.id)
269 : res?.discussion_languages;
272 <div className="container-lg">
273 {this.state.communityLoading ? (
281 title={this.documentTitle}
282 path={this.context.router.route.match.url}
283 description={res.community_view.community.description}
284 image={res.community_view.community.icon}
287 <div className="row">
288 <div className="col-12 col-md-8">
289 {this.communityInfo()}
290 <div className="d-block d-md-none">
292 className="btn btn-secondary d-inline-block mb-2 mr-3"
293 onClick={linkEvent(this, this.handleShowSidebarMobile)}
295 {i18n.t("sidebar")}{" "}
298 this.state.showSidebarMobile
302 classes="icon-inline"
305 {this.state.showSidebarMobile && (
308 community_view={res.community_view}
309 moderators={res.moderators}
310 admins={this.state.siteRes.admins}
312 enableNsfw={enableNsfw(this.state.siteRes)}
314 allLanguages={this.state.siteRes.all_languages}
316 this.state.siteRes.discussion_languages
318 communityLanguages={communityLangs}
320 {!res.community_view.community.local && res.site && (
323 showLocal={showLocal(this.isoData)}
332 page={this.state.page}
333 onChange={this.handlePageChange}
336 <div className="d-none d-md-block col-md-4">
338 community_view={res.community_view}
339 moderators={res.moderators}
340 admins={this.state.siteRes.admins}
342 enableNsfw={enableNsfw(this.state.siteRes)}
344 allLanguages={this.state.siteRes.all_languages}
345 siteLanguages={this.state.siteRes.discussion_languages}
346 communityLanguages={communityLangs}
348 {!res.community_view.community.local && res.site && (
351 showLocal={showLocal(this.isoData)}
364 return this.state.dataType == DataType.Post ? (
365 this.state.postsLoading ? (
371 posts={this.state.posts}
373 enableDownvotes={enableDownvotes(this.state.siteRes)}
374 enableNsfw={enableNsfw(this.state.siteRes)}
375 allLanguages={this.state.siteRes.all_languages}
376 siteLanguages={this.state.siteRes.discussion_languages}
379 ) : this.state.commentsLoading ? (
385 nodes={commentsToFlatNodes(this.state.comments)}
386 viewType={CommentViewType.Flat}
389 enableDownvotes={enableDownvotes(this.state.siteRes)}
390 moderators={this.state.communityRes?.moderators}
391 admins={this.state.siteRes.admins}
392 allLanguages={this.state.siteRes.all_languages}
393 siteLanguages={this.state.siteRes.discussion_languages}
399 let community = this.state.communityRes?.community_view.community;
402 <div className="mb-2">
403 <BannerIconHeader banner={community.banner} icon={community.icon} />
404 <h5 className="mb-0 overflow-wrap-anywhere">{community.title}</h5>
406 community={community}
418 // let communityRss = this.state.communityRes.map(r =>
419 // communityRSSUrl(r.community_view.community.actor_id, this.state.sort)
421 let res = this.state.communityRes;
422 let communityRss = res
423 ? communityRSSUrl(res.community_view.community.actor_id, this.state.sort)
427 <div className="mb-3">
428 <span className="mr-3">
430 type_={this.state.dataType}
431 onChange={this.handleDataTypeChange}
434 <span className="mr-2">
435 <SortSelect sort={this.state.sort} onChange={this.handleSortChange} />
439 <a href={communityRss} title="RSS" rel={relTags}>
440 <Icon icon="rss" classes="text-muted small" />
444 type="application/atom+xml"
453 handlePageChange(page: number) {
454 this.updateUrl({ page });
455 window.scrollTo(0, 0);
458 handleSortChange(val: SortType) {
459 this.updateUrl({ sort: val, page: 1 });
460 window.scrollTo(0, 0);
463 handleDataTypeChange(val: DataType) {
464 this.updateUrl({ dataType: DataType[val], page: 1 });
465 window.scrollTo(0, 0);
468 handleShowSidebarMobile(i: Community) {
469 i.setState({ showSidebarMobile: !i.state.showSidebarMobile });
472 updateUrl(paramUpdates: UrlParams) {
473 const dataTypeStr = paramUpdates.dataType || DataType[this.state.dataType];
474 const sortStr = paramUpdates.sort || this.state.sort;
475 const page = paramUpdates.page || this.state.page;
477 let typeView = `/c/${this.state.communityName}`;
479 this.props.history.push(
480 `${typeView}/data_type/${dataTypeStr}/sort/${sortStr}/page/${page}`
485 if (this.state.dataType == DataType.Post) {
486 let form: GetPosts = {
487 page: this.state.page,
489 sort: this.state.sort,
490 type_: ListingType.All,
491 community_name: this.state.communityName,
495 WebSocketService.Instance.send(wsClient.getPosts(form));
497 let form: GetComments = {
498 page: this.state.page,
500 sort: postToCommentSortType(this.state.sort),
501 type_: ListingType.All,
502 community_name: this.state.communityName,
506 WebSocketService.Instance.send(wsClient.getComments(form));
510 parseMessage(msg: any) {
511 let op = wsUserOp(msg);
513 let res = this.state.communityRes;
515 toast(i18n.t(msg.error), "danger");
516 this.context.router.history.push("/");
518 } else if (msg.reconnect) {
520 WebSocketService.Instance.send(
521 wsClient.communityJoin({
522 community_id: res.community_view.community.id,
527 } else if (op == UserOperation.GetCommunity) {
528 let data = wsJsonToRes<GetCommunityResponse>(msg);
529 this.setState({ communityRes: data, communityLoading: false });
530 // TODO why is there no auth in this form?
531 WebSocketService.Instance.send(
532 wsClient.communityJoin({
533 community_id: data.community_view.community.id,
537 op == UserOperation.EditCommunity ||
538 op == UserOperation.DeleteCommunity ||
539 op == UserOperation.RemoveCommunity
541 let data = wsJsonToRes<CommunityResponse>(msg);
543 res.community_view = data.community_view;
544 res.discussion_languages = data.discussion_languages;
546 this.setState(this.state);
547 } else if (op == UserOperation.FollowCommunity) {
548 let data = wsJsonToRes<CommunityResponse>(msg);
550 res.community_view.subscribed = data.community_view.subscribed;
551 res.community_view.counts.subscribers =
552 data.community_view.counts.subscribers;
554 this.setState(this.state);
555 } else if (op == UserOperation.GetPosts) {
556 let data = wsJsonToRes<GetPostsResponse>(msg);
557 this.setState({ posts: data.posts, postsLoading: false });
558 restoreScrollPosition(this.context);
561 op == UserOperation.EditPost ||
562 op == UserOperation.DeletePost ||
563 op == UserOperation.RemovePost ||
564 op == UserOperation.LockPost ||
565 op == UserOperation.FeaturePost ||
566 op == UserOperation.SavePost
568 let data = wsJsonToRes<PostResponse>(msg);
569 editPostFindRes(data.post_view, this.state.posts);
570 this.setState(this.state);
571 } else if (op == UserOperation.CreatePost) {
572 let data = wsJsonToRes<PostResponse>(msg);
575 UserService.Instance.myUserInfo?.local_user_view.local_user
576 .show_new_post_notifs;
578 // Only push these if you're on the first page, you pass the nsfw check, and it isn't blocked
581 this.state.page == 1 &&
582 nsfwCheck(data.post_view) &&
583 !isPostBlocked(data.post_view)
585 this.state.posts.unshift(data.post_view);
586 if (showPostNotifs) {
587 notifyPost(data.post_view, this.context.router);
589 this.setState(this.state);
591 } else if (op == UserOperation.CreatePostLike) {
592 let data = wsJsonToRes<PostResponse>(msg);
593 createPostLikeFindRes(data.post_view, this.state.posts);
594 this.setState(this.state);
595 } else if (op == UserOperation.AddModToCommunity) {
596 let data = wsJsonToRes<AddModToCommunityResponse>(msg);
598 res.moderators = data.moderators;
600 this.setState(this.state);
601 } else if (op == UserOperation.BanFromCommunity) {
602 let data = wsJsonToRes<BanFromCommunityResponse>(msg);
604 // TODO this might be incorrect
606 .filter(p => p.creator.id == data.person_view.person.id)
607 .forEach(p => (p.creator_banned_from_community = data.banned));
609 this.setState(this.state);
610 } else if (op == UserOperation.GetComments) {
611 let data = wsJsonToRes<GetCommentsResponse>(msg);
612 this.setState({ comments: data.comments, commentsLoading: false });
614 op == UserOperation.EditComment ||
615 op == UserOperation.DeleteComment ||
616 op == UserOperation.RemoveComment
618 let data = wsJsonToRes<CommentResponse>(msg);
619 editCommentRes(data.comment_view, this.state.comments);
620 this.setState(this.state);
621 } else if (op == UserOperation.CreateComment) {
622 let data = wsJsonToRes<CommentResponse>(msg);
624 // Necessary since it might be a user reply
626 this.state.comments.unshift(data.comment_view);
627 this.setState(this.state);
629 } else if (op == UserOperation.SaveComment) {
630 let data = wsJsonToRes<CommentResponse>(msg);
631 saveCommentRes(data.comment_view, this.state.comments);
632 this.setState(this.state);
633 } else if (op == UserOperation.CreateCommentLike) {
634 let data = wsJsonToRes<CommentResponse>(msg);
635 createCommentLikeRes(data.comment_view, this.state.comments);
636 this.setState(this.state);
637 } else if (op == UserOperation.BlockPerson) {
638 let data = wsJsonToRes<BlockPersonResponse>(msg);
639 updatePersonBlock(data);
640 } else if (op == UserOperation.CreatePostReport) {
641 let data = wsJsonToRes<PostReportResponse>(msg);
643 toast(i18n.t("report_created"));
645 } else if (op == UserOperation.CreateCommentReport) {
646 let data = wsJsonToRes<CommentReportResponse>(msg);
648 toast(i18n.t("report_created"));
650 } else if (op == UserOperation.PurgeCommunity) {
651 let data = wsJsonToRes<PurgeItemResponse>(msg);
653 toast(i18n.t("purge_success"));
654 this.context.router.history.push(`/`);
656 } else if (op == UserOperation.BlockCommunity) {
657 let data = wsJsonToRes<BlockCommunityResponse>(msg);
659 res.community_view.blocked = data.blocked;
661 updateCommunityBlock(data);
662 this.setState(this.state);