1 import { None, Option, Some } from "@sniptt/monads";
2 import { Component, linkEvent } from "inferno";
4 AddModToCommunityResponse,
5 BanFromCommunityResponse,
6 BlockCommunityResponse,
29 } from "lemmy-js-client";
30 import { Subscription } from "rxjs";
31 import { i18n } from "../../i18next";
36 } from "../../interfaces";
37 import { UserService, WebSocketService } from "../../services";
43 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: Option<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(
109 GetCommunityResponse,
113 private subscription: Subscription;
114 private emptyState: State = {
116 communityName: this.props.match.params.name,
117 communityLoading: true,
119 commentsLoading: true,
122 dataType: getDataTypeFromProps(this.props),
123 sort: getSortTypeFromProps(this.props),
124 page: getPageFromProps(this.props),
125 siteRes: this.isoData.site_res,
126 showSidebarMobile: false,
129 constructor(props: any, context: any) {
130 super(props, context);
132 this.state = this.emptyState;
133 this.handleSortChange = this.handleSortChange.bind(this);
134 this.handleDataTypeChange = this.handleDataTypeChange.bind(this);
135 this.handlePageChange = this.handlePageChange.bind(this);
137 this.parseMessage = this.parseMessage.bind(this);
138 this.subscription = wsSubscribe(this.parseMessage);
140 // Only fetch the data if coming from another route
141 if (this.isoData.path == this.context.router.route.match.url) {
142 this.state.communityRes = Some(
143 this.isoData.routeData[0] as GetCommunityResponse
145 let postsRes = Some(this.isoData.routeData[1] as GetPostsResponse);
146 let commentsRes = Some(this.isoData.routeData[2] as GetCommentsResponse);
149 some: pvs => (this.state.posts = pvs.posts),
153 some: cvs => (this.state.comments = cvs.comments),
157 this.state.communityLoading = false;
158 this.state.postsLoading = false;
159 this.state.commentsLoading = false;
161 this.fetchCommunity();
167 let form = new GetCommunity({
168 name: Some(this.state.communityName),
170 auth: auth(false).ok(),
172 WebSocketService.Instance.send(wsClient.getCommunity(form));
175 componentDidMount() {
179 componentWillUnmount() {
180 saveScrollPosition(this.context);
181 this.subscription.unsubscribe();
184 static getDerivedStateFromProps(props: any): CommunityProps {
186 dataType: getDataTypeFromProps(props),
187 sort: getSortTypeFromProps(props),
188 page: getPageFromProps(props),
192 static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
193 let pathSplit = req.path.split("/");
194 let promises: Promise<any>[] = [];
196 let communityName = pathSplit[2];
197 let communityForm = new GetCommunity({
198 name: Some(communityName),
202 promises.push(req.client.getCommunity(communityForm));
204 let dataType: DataType = pathSplit[4]
205 ? DataType[pathSplit[4]]
208 let sort: Option<SortType> = toOption(
210 ? SortType[pathSplit[6]]
211 : UserService.Instance.myUserInfo.match({
213 Object.values(SortType)[
214 mui.local_user_view.local_user.default_sort_type
216 none: SortType.Active,
220 let page = toOption(pathSplit[8] ? Number(pathSplit[8]) : 1);
222 if (dataType == DataType.Post) {
223 let getPostsForm = new GetPosts({
224 community_name: Some(communityName),
227 limit: Some(fetchLimit),
229 type_: Some(ListingType.All),
230 saved_only: Some(false),
233 promises.push(req.client.getPosts(getPostsForm));
234 promises.push(Promise.resolve());
236 let getCommentsForm = new GetComments({
237 community_name: Some(communityName),
240 limit: Some(fetchLimit),
242 sort: sort.map(postToCommentSortType),
243 type_: Some(ListingType.All),
244 saved_only: Some(false),
249 promises.push(Promise.resolve());
250 promises.push(req.client.getComments(getCommentsForm));
256 componentDidUpdate(_: any, lastState: State) {
258 lastState.dataType !== this.state.dataType ||
259 lastState.sort !== this.state.sort ||
260 lastState.page !== this.state.page
262 this.setState({ postsLoading: true, commentsLoading: true });
267 get documentTitle(): string {
268 return this.state.communityRes.match({
270 this.state.siteRes.site_view.match({
272 `${res.community_view.community.title} - ${siteView.site.name}`,
281 <div class="container">
282 {this.state.communityLoading ? (
287 this.state.communityRes.match({
291 title={this.documentTitle}
292 path={this.context.router.route.match.url}
293 description={res.community_view.community.description}
294 image={res.community_view.community.icon}
298 <div class="col-12 col-md-8">
299 {this.communityInfo()}
300 <div class="d-block d-md-none">
302 class="btn btn-secondary d-inline-block mb-2 mr-3"
303 onClick={linkEvent(this, this.handleShowSidebarMobile)}
305 {i18n.t("sidebar")}{" "}
308 this.state.showSidebarMobile
312 classes="icon-inline"
315 {this.state.showSidebarMobile && (
318 community_view={res.community_view}
319 moderators={res.moderators}
320 admins={this.state.siteRes.admins}
322 enableNsfw={enableNsfw(this.state.siteRes)}
324 {!res.community_view.community.local &&
329 showLocal={showLocal(this.isoData)}
343 page={this.state.page}
344 onChange={this.handlePageChange}
347 <div class="d-none d-md-block col-md-4">
349 community_view={res.community_view}
350 moderators={res.moderators}
351 admins={this.state.siteRes.admins}
353 enableNsfw={enableNsfw(this.state.siteRes)}
355 {!res.community_view.community.local &&
360 showLocal={showLocal(this.isoData)}
380 return this.state.dataType == DataType.Post ? (
381 this.state.postsLoading ? (
387 posts={this.state.posts}
389 enableDownvotes={enableDownvotes(this.state.siteRes)}
390 enableNsfw={enableNsfw(this.state.siteRes)}
393 ) : this.state.commentsLoading ? (
399 nodes={commentsToFlatNodes(this.state.comments)}
400 viewType={CommentViewType.Flat}
403 enableDownvotes={enableDownvotes(this.state.siteRes)}
404 moderators={this.state.communityRes.map(r => r.moderators)}
405 admins={Some(this.state.siteRes.admins)}
406 maxCommentsShown={None}
412 return this.state.communityRes
413 .map(r => r.community_view.community)
417 <BannerIconHeader banner={community.banner} icon={community.icon} />
418 <h5 class="mb-0 overflow-wrap-anywhere">{community.title}</h5>
420 community={community}
433 let communityRss = this.state.communityRes.map(r =>
434 communityRSSUrl(r.community_view.community.actor_id, this.state.sort)
440 type_={this.state.dataType}
441 onChange={this.handleDataTypeChange}
445 <SortSelect sort={this.state.sort} onChange={this.handleSortChange} />
447 {communityRss.match({
450 <a href={rss} title="RSS" rel={relTags}>
451 <Icon icon="rss" classes="text-muted small" />
453 <link rel="alternate" type="application/atom+xml" href={rss} />
462 handlePageChange(page: number) {
463 this.updateUrl({ page });
464 window.scrollTo(0, 0);
467 handleSortChange(val: SortType) {
468 this.updateUrl({ sort: val, page: 1 });
469 window.scrollTo(0, 0);
472 handleDataTypeChange(val: DataType) {
473 this.updateUrl({ dataType: DataType[val], page: 1 });
474 window.scrollTo(0, 0);
477 handleShowSidebarMobile(i: Community) {
478 i.state.showSidebarMobile = !i.state.showSidebarMobile;
482 updateUrl(paramUpdates: UrlParams) {
483 const dataTypeStr = paramUpdates.dataType || DataType[this.state.dataType];
484 const sortStr = paramUpdates.sort || this.state.sort;
485 const page = paramUpdates.page || this.state.page;
487 let typeView = `/c/${this.state.communityName}`;
489 this.props.history.push(
490 `${typeView}/data_type/${dataTypeStr}/sort/${sortStr}/page/${page}`
495 if (this.state.dataType == DataType.Post) {
496 let form = new GetPosts({
497 page: Some(this.state.page),
498 limit: Some(fetchLimit),
499 sort: Some(this.state.sort),
500 type_: Some(ListingType.All),
501 community_name: Some(this.state.communityName),
503 saved_only: Some(false),
504 auth: auth(false).ok(),
506 WebSocketService.Instance.send(wsClient.getPosts(form));
508 let form = new GetComments({
509 page: Some(this.state.page),
510 limit: Some(fetchLimit),
512 sort: Some(postToCommentSortType(this.state.sort)),
513 type_: Some(ListingType.All),
514 community_name: Some(this.state.communityName),
516 saved_only: Some(false),
519 auth: auth(false).ok(),
521 WebSocketService.Instance.send(wsClient.getComments(form));
525 parseMessage(msg: any) {
526 let op = wsUserOp(msg);
529 toast(i18n.t(msg.error), "danger");
530 this.context.router.history.push("/");
532 } else if (msg.reconnect) {
533 this.state.communityRes.match({
535 WebSocketService.Instance.send(
536 wsClient.communityJoin({
537 community_id: res.community_view.community.id,
544 } else if (op == UserOperation.GetCommunity) {
545 let data = wsJsonToRes<GetCommunityResponse>(msg, GetCommunityResponse);
546 this.state.communityRes = Some(data);
547 this.state.communityLoading = false;
548 this.setState(this.state);
549 // TODO why is there no auth in this form?
550 WebSocketService.Instance.send(
551 wsClient.communityJoin({
552 community_id: data.community_view.community.id,
556 op == UserOperation.EditCommunity ||
557 op == UserOperation.DeleteCommunity ||
558 op == UserOperation.RemoveCommunity
560 let data = wsJsonToRes<CommunityResponse>(msg, CommunityResponse);
561 this.state.communityRes.match({
562 some: res => (res.community_view = data.community_view),
565 this.setState(this.state);
566 } else if (op == UserOperation.FollowCommunity) {
567 let data = wsJsonToRes<CommunityResponse>(msg, CommunityResponse);
568 this.state.communityRes.match({
570 res.community_view.subscribed = data.community_view.subscribed;
571 res.community_view.counts.subscribers =
572 data.community_view.counts.subscribers;
576 this.setState(this.state);
577 } else if (op == UserOperation.GetPosts) {
578 let data = wsJsonToRes<GetPostsResponse>(msg, GetPostsResponse);
579 this.state.posts = data.posts;
580 this.state.postsLoading = false;
581 this.setState(this.state);
582 restoreScrollPosition(this.context);
585 op == UserOperation.EditPost ||
586 op == UserOperation.DeletePost ||
587 op == UserOperation.RemovePost ||
588 op == UserOperation.LockPost ||
589 op == UserOperation.StickyPost ||
590 op == UserOperation.SavePost
592 let data = wsJsonToRes<PostResponse>(msg, PostResponse);
593 editPostFindRes(data.post_view, this.state.posts);
594 this.setState(this.state);
595 } else if (op == UserOperation.CreatePost) {
596 let data = wsJsonToRes<PostResponse>(msg, PostResponse);
597 this.state.posts.unshift(data.post_view);
599 UserService.Instance.myUserInfo
600 .map(m => m.local_user_view.local_user.show_new_post_notifs)
603 notifyPost(data.post_view, this.context.router);
605 this.setState(this.state);
606 } else if (op == UserOperation.CreatePostLike) {
607 let data = wsJsonToRes<PostResponse>(msg, PostResponse);
608 createPostLikeFindRes(data.post_view, this.state.posts);
609 this.setState(this.state);
610 } else if (op == UserOperation.AddModToCommunity) {
611 let data = wsJsonToRes<AddModToCommunityResponse>(
613 AddModToCommunityResponse
615 this.state.communityRes.match({
616 some: res => (res.moderators = data.moderators),
619 this.setState(this.state);
620 } else if (op == UserOperation.BanFromCommunity) {
621 let data = wsJsonToRes<BanFromCommunityResponse>(
623 BanFromCommunityResponse
626 // TODO this might be incorrect
628 .filter(p => p.creator.id == data.person_view.person.id)
629 .forEach(p => (p.creator_banned_from_community = data.banned));
631 this.setState(this.state);
632 } else if (op == UserOperation.GetComments) {
633 let data = wsJsonToRes<GetCommentsResponse>(msg, GetCommentsResponse);
634 this.state.comments = data.comments;
635 this.state.commentsLoading = false;
636 this.setState(this.state);
638 op == UserOperation.EditComment ||
639 op == UserOperation.DeleteComment ||
640 op == UserOperation.RemoveComment
642 let data = wsJsonToRes<CommentResponse>(msg, CommentResponse);
643 editCommentRes(data.comment_view, this.state.comments);
644 this.setState(this.state);
645 } else if (op == UserOperation.CreateComment) {
646 let data = wsJsonToRes<CommentResponse>(msg, CommentResponse);
648 // Necessary since it might be a user reply
650 this.state.comments.unshift(data.comment_view);
651 this.setState(this.state);
653 } else if (op == UserOperation.SaveComment) {
654 let data = wsJsonToRes<CommentResponse>(msg, CommentResponse);
655 saveCommentRes(data.comment_view, this.state.comments);
656 this.setState(this.state);
657 } else if (op == UserOperation.CreateCommentLike) {
658 let data = wsJsonToRes<CommentResponse>(msg, CommentResponse);
659 createCommentLikeRes(data.comment_view, this.state.comments);
660 this.setState(this.state);
661 } else if (op == UserOperation.BlockPerson) {
662 let data = wsJsonToRes<BlockPersonResponse>(msg, BlockPersonResponse);
663 updatePersonBlock(data);
664 } else if (op == UserOperation.CreatePostReport) {
665 let data = wsJsonToRes<PostReportResponse>(msg, PostReportResponse);
667 toast(i18n.t("report_created"));
669 } else if (op == UserOperation.CreateCommentReport) {
670 let data = wsJsonToRes<CommentReportResponse>(msg, CommentReportResponse);
672 toast(i18n.t("report_created"));
674 } else if (op == UserOperation.PurgeCommunity) {
675 let data = wsJsonToRes<PurgeItemResponse>(msg, PurgeItemResponse);
677 toast(i18n.t("purge_success"));
678 this.context.router.history.push(`/`);
680 } else if (op == UserOperation.BlockCommunity) {
681 let data = wsJsonToRes<BlockCommunityResponse>(
683 BlockCommunityResponse
685 this.state.communityRes.match({
686 some: res => (res.community_view.blocked = data.blocked),
689 updateCommunityBlock(data);
690 this.setState(this.state);