1 import { None, Option, Some } from "@sniptt/monads";
2 import { Component, linkEvent } from "inferno";
4 AddModToCommunityResponse,
5 BanFromCommunityResponse,
27 } from "lemmy-js-client";
28 import { Subscription } from "rxjs";
29 import { i18n } from "../../i18next";
30 import { DataType, InitialFetchRequest } from "../../interfaces";
31 import { UserService, WebSocketService } from "../../services";
37 createPostLikeFindRes,
48 restoreScrollPosition,
59 import { CommentNodes } from "../comment/comment-nodes";
60 import { BannerIconHeader } from "../common/banner-icon-header";
61 import { DataTypeSelect } from "../common/data-type-select";
62 import { HtmlTags } from "../common/html-tags";
63 import { Icon, Spinner } from "../common/icon";
64 import { Paginator } from "../common/paginator";
65 import { SortSelect } from "../common/sort-select";
66 import { Sidebar } from "../community/sidebar";
67 import { SiteSidebar } from "../home/site-sidebar";
68 import { PostListings } from "../post/post-listings";
69 import { CommunityLink } from "./community-link";
72 communityRes: Option<GetCommunityResponse>;
73 siteRes: GetSiteResponse;
74 communityName: string;
75 communityLoading: boolean;
76 postsLoading: boolean;
77 commentsLoading: boolean;
79 comments: CommentView[];
83 showSidebarMobile: boolean;
86 interface CommunityProps {
98 export class Community extends Component<any, State> {
99 private isoData = setIsoData(
101 GetCommunityResponse,
105 private subscription: Subscription;
106 private emptyState: State = {
108 communityName: this.props.match.params.name,
109 communityLoading: true,
111 commentsLoading: true,
114 dataType: getDataTypeFromProps(this.props),
115 sort: getSortTypeFromProps(this.props),
116 page: getPageFromProps(this.props),
117 siteRes: this.isoData.site_res,
118 showSidebarMobile: false,
121 constructor(props: any, context: any) {
122 super(props, context);
124 this.state = this.emptyState;
125 this.handleSortChange = this.handleSortChange.bind(this);
126 this.handleDataTypeChange = this.handleDataTypeChange.bind(this);
127 this.handlePageChange = this.handlePageChange.bind(this);
129 this.parseMessage = this.parseMessage.bind(this);
130 this.subscription = wsSubscribe(this.parseMessage);
132 // Only fetch the data if coming from another route
133 if (this.isoData.path == this.context.router.route.match.url) {
134 this.state.communityRes = Some(
135 this.isoData.routeData[0] as GetCommunityResponse
137 let postsRes = Some(this.isoData.routeData[1] as GetPostsResponse);
138 let commentsRes = Some(this.isoData.routeData[2] as GetCommentsResponse);
141 some: pvs => (this.state.posts = pvs.posts),
145 some: cvs => (this.state.comments = cvs.comments),
149 this.state.communityLoading = false;
150 this.state.postsLoading = false;
151 this.state.commentsLoading = false;
153 this.fetchCommunity();
159 let form = new GetCommunity({
160 name: Some(this.state.communityName),
162 auth: auth(false).ok(),
164 WebSocketService.Instance.send(wsClient.getCommunity(form));
167 componentDidMount() {
171 componentWillUnmount() {
172 saveScrollPosition(this.context);
173 this.subscription.unsubscribe();
176 static getDerivedStateFromProps(props: any): CommunityProps {
178 dataType: getDataTypeFromProps(props),
179 sort: getSortTypeFromProps(props),
180 page: getPageFromProps(props),
184 static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
185 let pathSplit = req.path.split("/");
186 let promises: Promise<any>[] = [];
188 let communityName = pathSplit[2];
189 let communityForm = new GetCommunity({
190 name: Some(communityName),
194 promises.push(req.client.getCommunity(communityForm));
196 let dataType: DataType = pathSplit[4]
197 ? DataType[pathSplit[4]]
200 let sort: Option<SortType> = toOption(
202 ? SortType[pathSplit[6]]
203 : UserService.Instance.myUserInfo.match({
205 Object.values(SortType)[
206 mui.local_user_view.local_user.default_sort_type
208 none: SortType.Active,
212 let page = toOption(pathSplit[8] ? Number(pathSplit[8]) : 1);
214 if (dataType == DataType.Post) {
215 let getPostsForm = new GetPosts({
216 community_name: Some(communityName),
219 limit: Some(fetchLimit),
221 type_: Some(ListingType.Community),
222 saved_only: Some(false),
225 promises.push(req.client.getPosts(getPostsForm));
226 promises.push(Promise.resolve());
228 let getCommentsForm = new GetComments({
229 community_name: Some(communityName),
232 limit: Some(fetchLimit),
234 type_: Some(ListingType.Community),
235 saved_only: Some(false),
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 return this.state.communityRes.match({
259 this.state.siteRes.site_view.match({
261 `${res.community_view.community.title} - ${siteView.site.name}`,
270 <div class="container">
271 {this.state.communityLoading ? (
276 this.state.communityRes.match({
280 title={this.documentTitle}
281 path={this.context.router.route.match.url}
282 description={res.community_view.community.description}
283 image={res.community_view.community.icon}
287 <div class="col-12 col-md-8">
288 {this.communityInfo()}
289 <div class="d-block d-md-none">
291 class="btn btn-secondary d-inline-block mb-2 mr-3"
292 onClick={linkEvent(this, this.handleShowSidebarMobile)}
294 {i18n.t("sidebar")}{" "}
297 this.state.showSidebarMobile
301 classes="icon-inline"
304 {this.state.showSidebarMobile && (
307 community_view={res.community_view}
308 moderators={res.moderators}
309 admins={this.state.siteRes.admins}
311 enableNsfw={enableNsfw(this.state.siteRes)}
313 {!res.community_view.community.local &&
318 showLocal={showLocal(this.isoData)}
332 page={this.state.page}
333 onChange={this.handlePageChange}
336 <div class="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 {!res.community_view.community.local &&
349 showLocal={showLocal(this.isoData)}
369 return this.state.dataType == DataType.Post ? (
370 this.state.postsLoading ? (
376 posts={this.state.posts}
378 enableDownvotes={enableDownvotes(this.state.siteRes)}
379 enableNsfw={enableNsfw(this.state.siteRes)}
382 ) : this.state.commentsLoading ? (
388 nodes={commentsToFlatNodes(this.state.comments)}
391 enableDownvotes={enableDownvotes(this.state.siteRes)}
392 moderators={this.state.communityRes.map(r => r.moderators)}
393 admins={Some(this.state.siteRes.admins)}
394 maxCommentsShown={None}
400 return this.state.communityRes
401 .map(r => r.community_view.community)
405 <BannerIconHeader banner={community.banner} icon={community.icon} />
406 <h5 class="mb-0 overflow-wrap-anywhere">{community.title}</h5>
408 community={community}
421 let communityRss = this.state.communityRes.map(r =>
422 communityRSSUrl(r.community_view.community.actor_id, this.state.sort)
428 type_={this.state.dataType}
429 onChange={this.handleDataTypeChange}
433 <SortSelect sort={this.state.sort} onChange={this.handleSortChange} />
435 {communityRss.match({
438 <a href={rss} title="RSS" rel={relTags}>
439 <Icon icon="rss" classes="text-muted small" />
441 <link rel="alternate" type="application/atom+xml" href={rss} />
450 handlePageChange(page: number) {
451 this.updateUrl({ page });
452 window.scrollTo(0, 0);
455 handleSortChange(val: SortType) {
456 this.updateUrl({ sort: val, page: 1 });
457 window.scrollTo(0, 0);
460 handleDataTypeChange(val: DataType) {
461 this.updateUrl({ dataType: DataType[val], page: 1 });
462 window.scrollTo(0, 0);
465 handleShowSidebarMobile(i: Community) {
466 i.state.showSidebarMobile = !i.state.showSidebarMobile;
470 updateUrl(paramUpdates: UrlParams) {
471 const dataTypeStr = paramUpdates.dataType || DataType[this.state.dataType];
472 const sortStr = paramUpdates.sort || this.state.sort;
473 const page = paramUpdates.page || this.state.page;
475 let typeView = `/c/${this.state.communityName}`;
477 this.props.history.push(
478 `${typeView}/data_type/${dataTypeStr}/sort/${sortStr}/page/${page}`
483 if (this.state.dataType == DataType.Post) {
484 let form = new GetPosts({
485 page: Some(this.state.page),
486 limit: Some(fetchLimit),
487 sort: Some(this.state.sort),
488 type_: Some(ListingType.Community),
489 community_name: Some(this.state.communityName),
491 saved_only: Some(false),
492 auth: auth(false).ok(),
494 WebSocketService.Instance.send(wsClient.getPosts(form));
496 let form = new GetComments({
497 page: Some(this.state.page),
498 limit: Some(fetchLimit),
499 sort: Some(this.state.sort),
500 type_: Some(ListingType.Community),
501 community_name: Some(this.state.communityName),
503 saved_only: Some(false),
504 auth: auth(false).ok(),
506 WebSocketService.Instance.send(wsClient.getComments(form));
510 parseMessage(msg: any) {
511 let op = wsUserOp(msg);
514 toast(i18n.t(msg.error), "danger");
515 this.context.router.history.push("/");
517 } else if (msg.reconnect) {
518 this.state.communityRes.match({
520 WebSocketService.Instance.send(
521 wsClient.communityJoin({
522 community_id: res.community_view.community.id,
529 } else if (op == UserOperation.GetCommunity) {
530 let data = wsJsonToRes<GetCommunityResponse>(msg, GetCommunityResponse);
531 this.state.communityRes = Some(data);
532 this.state.communityLoading = false;
533 this.setState(this.state);
534 // TODO why is there no auth in this form?
535 WebSocketService.Instance.send(
536 wsClient.communityJoin({
537 community_id: data.community_view.community.id,
541 op == UserOperation.EditCommunity ||
542 op == UserOperation.DeleteCommunity ||
543 op == UserOperation.RemoveCommunity
545 let data = wsJsonToRes<CommunityResponse>(msg, CommunityResponse);
546 this.state.communityRes.match({
547 some: res => (res.community_view = data.community_view),
550 this.setState(this.state);
551 } else if (op == UserOperation.FollowCommunity) {
552 let data = wsJsonToRes<CommunityResponse>(msg, CommunityResponse);
553 this.state.communityRes.match({
555 res.community_view.subscribed = data.community_view.subscribed;
556 res.community_view.counts.subscribers =
557 data.community_view.counts.subscribers;
561 this.setState(this.state);
562 } else if (op == UserOperation.GetPosts) {
563 let data = wsJsonToRes<GetPostsResponse>(msg, GetPostsResponse);
564 this.state.posts = data.posts;
565 this.state.postsLoading = false;
566 this.setState(this.state);
567 restoreScrollPosition(this.context);
570 op == UserOperation.EditPost ||
571 op == UserOperation.DeletePost ||
572 op == UserOperation.RemovePost ||
573 op == UserOperation.LockPost ||
574 op == UserOperation.StickyPost ||
575 op == UserOperation.SavePost
577 let data = wsJsonToRes<PostResponse>(msg, PostResponse);
578 editPostFindRes(data.post_view, this.state.posts);
579 this.setState(this.state);
580 } else if (op == UserOperation.CreatePost) {
581 let data = wsJsonToRes<PostResponse>(msg, PostResponse);
582 this.state.posts.unshift(data.post_view);
584 UserService.Instance.myUserInfo
585 .map(m => m.local_user_view.local_user.show_new_post_notifs)
588 notifyPost(data.post_view, this.context.router);
590 this.setState(this.state);
591 } else if (op == UserOperation.CreatePostLike) {
592 let data = wsJsonToRes<PostResponse>(msg, PostResponse);
593 createPostLikeFindRes(data.post_view, this.state.posts);
594 this.setState(this.state);
595 } else if (op == UserOperation.AddModToCommunity) {
596 let data = wsJsonToRes<AddModToCommunityResponse>(
598 AddModToCommunityResponse
600 this.state.communityRes.match({
601 some: res => (res.moderators = data.moderators),
604 this.setState(this.state);
605 } else if (op == UserOperation.BanFromCommunity) {
606 let data = wsJsonToRes<BanFromCommunityResponse>(
608 BanFromCommunityResponse
611 // TODO this might be incorrect
613 .filter(p => p.creator.id == data.person_view.person.id)
614 .forEach(p => (p.creator_banned_from_community = data.banned));
616 this.setState(this.state);
617 } else if (op == UserOperation.GetComments) {
618 let data = wsJsonToRes<GetCommentsResponse>(msg, GetCommentsResponse);
619 this.state.comments = data.comments;
620 this.state.commentsLoading = false;
621 this.setState(this.state);
623 op == UserOperation.EditComment ||
624 op == UserOperation.DeleteComment ||
625 op == UserOperation.RemoveComment
627 let data = wsJsonToRes<CommentResponse>(msg, CommentResponse);
628 editCommentRes(data.comment_view, this.state.comments);
629 this.setState(this.state);
630 } else if (op == UserOperation.CreateComment) {
631 let data = wsJsonToRes<CommentResponse>(msg, CommentResponse);
633 // Necessary since it might be a user reply
635 this.state.comments.unshift(data.comment_view);
636 this.setState(this.state);
638 } else if (op == UserOperation.SaveComment) {
639 let data = wsJsonToRes<CommentResponse>(msg, CommentResponse);
640 saveCommentRes(data.comment_view, this.state.comments);
641 this.setState(this.state);
642 } else if (op == UserOperation.CreateCommentLike) {
643 let data = wsJsonToRes<CommentResponse>(msg, CommentResponse);
644 createCommentLikeRes(data.comment_view, this.state.comments);
645 this.setState(this.state);
646 } else if (op == UserOperation.BlockPerson) {
647 let data = wsJsonToRes<BlockPersonResponse>(msg, BlockPersonResponse);
648 updatePersonBlock(data);
649 } else if (op == UserOperation.CreatePostReport) {
650 let data = wsJsonToRes<PostReportResponse>(msg, PostReportResponse);
652 toast(i18n.t("report_created"));
654 } else if (op == UserOperation.CreateCommentReport) {
655 let data = wsJsonToRes<CommentReportResponse>(msg, CommentReportResponse);
657 toast(i18n.t("report_created"));