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";
32 import { DataType, InitialFetchRequest } from "../../interfaces";
33 import { UserService, WebSocketService } from "../../services";
39 createPostLikeFindRes,
50 restoreScrollPosition,
62 import { CommentNodes } from "../comment/comment-nodes";
63 import { BannerIconHeader } from "../common/banner-icon-header";
64 import { DataTypeSelect } from "../common/data-type-select";
65 import { HtmlTags } from "../common/html-tags";
66 import { Icon, Spinner } from "../common/icon";
67 import { Paginator } from "../common/paginator";
68 import { SortSelect } from "../common/sort-select";
69 import { Sidebar } from "../community/sidebar";
70 import { SiteSidebar } from "../home/site-sidebar";
71 import { PostListings } from "../post/post-listings";
72 import { CommunityLink } from "./community-link";
75 communityRes: Option<GetCommunityResponse>;
76 siteRes: GetSiteResponse;
77 communityName: string;
78 communityLoading: boolean;
79 postsLoading: boolean;
80 commentsLoading: boolean;
82 comments: CommentView[];
86 showSidebarMobile: boolean;
89 interface CommunityProps {
101 export class Community extends Component<any, State> {
102 private isoData = setIsoData(
104 GetCommunityResponse,
108 private subscription: Subscription;
109 private emptyState: State = {
111 communityName: this.props.match.params.name,
112 communityLoading: true,
114 commentsLoading: true,
117 dataType: getDataTypeFromProps(this.props),
118 sort: getSortTypeFromProps(this.props),
119 page: getPageFromProps(this.props),
120 siteRes: this.isoData.site_res,
121 showSidebarMobile: false,
124 constructor(props: any, context: any) {
125 super(props, context);
127 this.state = this.emptyState;
128 this.handleSortChange = this.handleSortChange.bind(this);
129 this.handleDataTypeChange = this.handleDataTypeChange.bind(this);
130 this.handlePageChange = this.handlePageChange.bind(this);
132 this.parseMessage = this.parseMessage.bind(this);
133 this.subscription = wsSubscribe(this.parseMessage);
135 // Only fetch the data if coming from another route
136 if (this.isoData.path == this.context.router.route.match.url) {
137 this.state.communityRes = Some(
138 this.isoData.routeData[0] as GetCommunityResponse
140 let postsRes = Some(this.isoData.routeData[1] as GetPostsResponse);
141 let commentsRes = Some(this.isoData.routeData[2] as GetCommentsResponse);
144 some: pvs => (this.state.posts = pvs.posts),
148 some: cvs => (this.state.comments = cvs.comments),
152 this.state.communityLoading = false;
153 this.state.postsLoading = false;
154 this.state.commentsLoading = false;
156 this.fetchCommunity();
162 let form = new GetCommunity({
163 name: Some(this.state.communityName),
165 auth: auth(false).ok(),
167 WebSocketService.Instance.send(wsClient.getCommunity(form));
170 componentDidMount() {
174 componentWillUnmount() {
175 saveScrollPosition(this.context);
176 this.subscription.unsubscribe();
179 static getDerivedStateFromProps(props: any): CommunityProps {
181 dataType: getDataTypeFromProps(props),
182 sort: getSortTypeFromProps(props),
183 page: getPageFromProps(props),
187 static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
188 let pathSplit = req.path.split("/");
189 let promises: Promise<any>[] = [];
191 let communityName = pathSplit[2];
192 let communityForm = new GetCommunity({
193 name: Some(communityName),
197 promises.push(req.client.getCommunity(communityForm));
199 let dataType: DataType = pathSplit[4]
200 ? DataType[pathSplit[4]]
203 let sort: Option<SortType> = toOption(
205 ? SortType[pathSplit[6]]
206 : UserService.Instance.myUserInfo.match({
208 Object.values(SortType)[
209 mui.local_user_view.local_user.default_sort_type
211 none: SortType.Active,
215 let page = toOption(pathSplit[8] ? Number(pathSplit[8]) : 1);
217 if (dataType == DataType.Post) {
218 let getPostsForm = new GetPosts({
219 community_name: Some(communityName),
222 limit: Some(fetchLimit),
224 type_: Some(ListingType.All),
225 saved_only: Some(false),
228 promises.push(req.client.getPosts(getPostsForm));
229 promises.push(Promise.resolve());
231 let getCommentsForm = new GetComments({
232 community_name: Some(communityName),
235 limit: Some(fetchLimit),
237 type_: Some(ListingType.All),
238 saved_only: Some(false),
241 promises.push(Promise.resolve());
242 promises.push(req.client.getComments(getCommentsForm));
248 componentDidUpdate(_: any, lastState: State) {
250 lastState.dataType !== this.state.dataType ||
251 lastState.sort !== this.state.sort ||
252 lastState.page !== this.state.page
254 this.setState({ postsLoading: true, commentsLoading: true });
259 get documentTitle(): string {
260 return this.state.communityRes.match({
262 this.state.siteRes.site_view.match({
264 `${res.community_view.community.title} - ${siteView.site.name}`,
273 <div class="container">
274 {this.state.communityLoading ? (
279 this.state.communityRes.match({
283 title={this.documentTitle}
284 path={this.context.router.route.match.url}
285 description={res.community_view.community.description}
286 image={res.community_view.community.icon}
290 <div class="col-12 col-md-8">
291 {this.communityInfo()}
292 <div class="d-block d-md-none">
294 class="btn btn-secondary d-inline-block mb-2 mr-3"
295 onClick={linkEvent(this, this.handleShowSidebarMobile)}
297 {i18n.t("sidebar")}{" "}
300 this.state.showSidebarMobile
304 classes="icon-inline"
307 {this.state.showSidebarMobile && (
310 community_view={res.community_view}
311 moderators={res.moderators}
312 admins={this.state.siteRes.admins}
314 enableNsfw={enableNsfw(this.state.siteRes)}
316 {!res.community_view.community.local &&
321 showLocal={showLocal(this.isoData)}
335 page={this.state.page}
336 onChange={this.handlePageChange}
339 <div class="d-none d-md-block col-md-4">
341 community_view={res.community_view}
342 moderators={res.moderators}
343 admins={this.state.siteRes.admins}
345 enableNsfw={enableNsfw(this.state.siteRes)}
347 {!res.community_view.community.local &&
352 showLocal={showLocal(this.isoData)}
372 return this.state.dataType == DataType.Post ? (
373 this.state.postsLoading ? (
379 posts={this.state.posts}
381 enableDownvotes={enableDownvotes(this.state.siteRes)}
382 enableNsfw={enableNsfw(this.state.siteRes)}
385 ) : this.state.commentsLoading ? (
391 nodes={commentsToFlatNodes(this.state.comments)}
394 enableDownvotes={enableDownvotes(this.state.siteRes)}
395 moderators={this.state.communityRes.map(r => r.moderators)}
396 admins={Some(this.state.siteRes.admins)}
397 maxCommentsShown={None}
403 return this.state.communityRes
404 .map(r => r.community_view.community)
408 <BannerIconHeader banner={community.banner} icon={community.icon} />
409 <h5 class="mb-0 overflow-wrap-anywhere">{community.title}</h5>
411 community={community}
424 let communityRss = this.state.communityRes.map(r =>
425 communityRSSUrl(r.community_view.community.actor_id, this.state.sort)
431 type_={this.state.dataType}
432 onChange={this.handleDataTypeChange}
436 <SortSelect sort={this.state.sort} onChange={this.handleSortChange} />
438 {communityRss.match({
441 <a href={rss} title="RSS" rel={relTags}>
442 <Icon icon="rss" classes="text-muted small" />
444 <link rel="alternate" type="application/atom+xml" href={rss} />
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.state.showSidebarMobile = !i.state.showSidebarMobile;
473 updateUrl(paramUpdates: UrlParams) {
474 const dataTypeStr = paramUpdates.dataType || DataType[this.state.dataType];
475 const sortStr = paramUpdates.sort || this.state.sort;
476 const page = paramUpdates.page || this.state.page;
478 let typeView = `/c/${this.state.communityName}`;
480 this.props.history.push(
481 `${typeView}/data_type/${dataTypeStr}/sort/${sortStr}/page/${page}`
486 if (this.state.dataType == DataType.Post) {
487 let form = new GetPosts({
488 page: Some(this.state.page),
489 limit: Some(fetchLimit),
490 sort: Some(this.state.sort),
491 type_: Some(ListingType.All),
492 community_name: Some(this.state.communityName),
494 saved_only: Some(false),
495 auth: auth(false).ok(),
497 WebSocketService.Instance.send(wsClient.getPosts(form));
499 let form = new GetComments({
500 page: Some(this.state.page),
501 limit: Some(fetchLimit),
502 sort: Some(this.state.sort),
503 type_: Some(ListingType.All),
504 community_name: Some(this.state.communityName),
506 saved_only: Some(false),
507 auth: auth(false).ok(),
509 WebSocketService.Instance.send(wsClient.getComments(form));
513 parseMessage(msg: any) {
514 let op = wsUserOp(msg);
517 toast(i18n.t(msg.error), "danger");
518 this.context.router.history.push("/");
520 } else if (msg.reconnect) {
521 this.state.communityRes.match({
523 WebSocketService.Instance.send(
524 wsClient.communityJoin({
525 community_id: res.community_view.community.id,
532 } else if (op == UserOperation.GetCommunity) {
533 let data = wsJsonToRes<GetCommunityResponse>(msg, GetCommunityResponse);
534 this.state.communityRes = Some(data);
535 this.state.communityLoading = false;
536 this.setState(this.state);
537 // TODO why is there no auth in this form?
538 WebSocketService.Instance.send(
539 wsClient.communityJoin({
540 community_id: data.community_view.community.id,
544 op == UserOperation.EditCommunity ||
545 op == UserOperation.DeleteCommunity ||
546 op == UserOperation.RemoveCommunity
548 let data = wsJsonToRes<CommunityResponse>(msg, CommunityResponse);
549 this.state.communityRes.match({
550 some: res => (res.community_view = data.community_view),
553 this.setState(this.state);
554 } else if (op == UserOperation.FollowCommunity) {
555 let data = wsJsonToRes<CommunityResponse>(msg, CommunityResponse);
556 this.state.communityRes.match({
558 res.community_view.subscribed = data.community_view.subscribed;
559 res.community_view.counts.subscribers =
560 data.community_view.counts.subscribers;
564 this.setState(this.state);
565 } else if (op == UserOperation.GetPosts) {
566 let data = wsJsonToRes<GetPostsResponse>(msg, GetPostsResponse);
567 this.state.posts = data.posts;
568 this.state.postsLoading = false;
569 this.setState(this.state);
570 restoreScrollPosition(this.context);
573 op == UserOperation.EditPost ||
574 op == UserOperation.DeletePost ||
575 op == UserOperation.RemovePost ||
576 op == UserOperation.LockPost ||
577 op == UserOperation.StickyPost ||
578 op == UserOperation.SavePost
580 let data = wsJsonToRes<PostResponse>(msg, PostResponse);
581 editPostFindRes(data.post_view, this.state.posts);
582 this.setState(this.state);
583 } else if (op == UserOperation.CreatePost) {
584 let data = wsJsonToRes<PostResponse>(msg, PostResponse);
585 this.state.posts.unshift(data.post_view);
587 UserService.Instance.myUserInfo
588 .map(m => m.local_user_view.local_user.show_new_post_notifs)
591 notifyPost(data.post_view, this.context.router);
593 this.setState(this.state);
594 } else if (op == UserOperation.CreatePostLike) {
595 let data = wsJsonToRes<PostResponse>(msg, PostResponse);
596 createPostLikeFindRes(data.post_view, this.state.posts);
597 this.setState(this.state);
598 } else if (op == UserOperation.AddModToCommunity) {
599 let data = wsJsonToRes<AddModToCommunityResponse>(
601 AddModToCommunityResponse
603 this.state.communityRes.match({
604 some: res => (res.moderators = data.moderators),
607 this.setState(this.state);
608 } else if (op == UserOperation.BanFromCommunity) {
609 let data = wsJsonToRes<BanFromCommunityResponse>(
611 BanFromCommunityResponse
614 // TODO this might be incorrect
616 .filter(p => p.creator.id == data.person_view.person.id)
617 .forEach(p => (p.creator_banned_from_community = data.banned));
619 this.setState(this.state);
620 } else if (op == UserOperation.GetComments) {
621 let data = wsJsonToRes<GetCommentsResponse>(msg, GetCommentsResponse);
622 this.state.comments = data.comments;
623 this.state.commentsLoading = false;
624 this.setState(this.state);
626 op == UserOperation.EditComment ||
627 op == UserOperation.DeleteComment ||
628 op == UserOperation.RemoveComment
630 let data = wsJsonToRes<CommentResponse>(msg, CommentResponse);
631 editCommentRes(data.comment_view, this.state.comments);
632 this.setState(this.state);
633 } else if (op == UserOperation.CreateComment) {
634 let data = wsJsonToRes<CommentResponse>(msg, CommentResponse);
636 // Necessary since it might be a user reply
638 this.state.comments.unshift(data.comment_view);
639 this.setState(this.state);
641 } else if (op == UserOperation.SaveComment) {
642 let data = wsJsonToRes<CommentResponse>(msg, CommentResponse);
643 saveCommentRes(data.comment_view, this.state.comments);
644 this.setState(this.state);
645 } else if (op == UserOperation.CreateCommentLike) {
646 let data = wsJsonToRes<CommentResponse>(msg, CommentResponse);
647 createCommentLikeRes(data.comment_view, this.state.comments);
648 this.setState(this.state);
649 } else if (op == UserOperation.BlockPerson) {
650 let data = wsJsonToRes<BlockPersonResponse>(msg, BlockPersonResponse);
651 updatePersonBlock(data);
652 } else if (op == UserOperation.CreatePostReport) {
653 let data = wsJsonToRes<PostReportResponse>(msg, PostReportResponse);
655 toast(i18n.t("report_created"));
657 } else if (op == UserOperation.CreateCommentReport) {
658 let data = wsJsonToRes<CommentReportResponse>(msg, CommentReportResponse);
660 toast(i18n.t("report_created"));
662 } else if (op == UserOperation.PurgeCommunity) {
663 let data = wsJsonToRes<PurgeItemResponse>(msg, PurgeItemResponse);
665 toast(i18n.t("purge_success"));
666 this.context.router.history.push(`/`);
668 } else if (op == UserOperation.BlockCommunity) {
669 let data = wsJsonToRes<BlockCommunityResponse>(
671 BlockCommunityResponse
673 this.state.communityRes.match({
674 some: res => (res.community_view.blocked = data.blocked),
677 updateCommunityBlock(data);
678 this.setState(this.state);