1 import { None, Option, Some } from "@sniptt/monads";
2 import { Component, linkEvent } from "inferno";
4 AddModToCommunityResponse,
5 BanFromCommunityResponse,
28 } from "lemmy-js-client";
29 import { Subscription } from "rxjs";
30 import { i18n } from "../../i18next";
31 import { DataType, InitialFetchRequest } from "../../interfaces";
32 import { UserService, WebSocketService } from "../../services";
38 createPostLikeFindRes,
49 restoreScrollPosition,
60 import { CommentNodes } from "../comment/comment-nodes";
61 import { BannerIconHeader } from "../common/banner-icon-header";
62 import { DataTypeSelect } from "../common/data-type-select";
63 import { HtmlTags } from "../common/html-tags";
64 import { Icon, Spinner } from "../common/icon";
65 import { Paginator } from "../common/paginator";
66 import { SortSelect } from "../common/sort-select";
67 import { Sidebar } from "../community/sidebar";
68 import { SiteSidebar } from "../home/site-sidebar";
69 import { PostListings } from "../post/post-listings";
70 import { CommunityLink } from "./community-link";
73 communityRes: Option<GetCommunityResponse>;
74 siteRes: GetSiteResponse;
75 communityName: string;
76 communityLoading: boolean;
77 postsLoading: boolean;
78 commentsLoading: boolean;
80 comments: CommentView[];
84 showSidebarMobile: boolean;
87 interface CommunityProps {
99 export class Community extends Component<any, State> {
100 private isoData = setIsoData(
102 GetCommunityResponse,
106 private subscription: Subscription;
107 private emptyState: State = {
109 communityName: this.props.match.params.name,
110 communityLoading: true,
112 commentsLoading: true,
115 dataType: getDataTypeFromProps(this.props),
116 sort: getSortTypeFromProps(this.props),
117 page: getPageFromProps(this.props),
118 siteRes: this.isoData.site_res,
119 showSidebarMobile: false,
122 constructor(props: any, context: any) {
123 super(props, context);
125 this.state = this.emptyState;
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) {
135 this.state.communityRes = Some(
136 this.isoData.routeData[0] as GetCommunityResponse
138 let postsRes = Some(this.isoData.routeData[1] as GetPostsResponse);
139 let commentsRes = Some(this.isoData.routeData[2] as GetCommentsResponse);
142 some: pvs => (this.state.posts = pvs.posts),
146 some: cvs => (this.state.comments = cvs.comments),
150 this.state.communityLoading = false;
151 this.state.postsLoading = false;
152 this.state.commentsLoading = false;
154 this.fetchCommunity();
160 let form = new GetCommunity({
161 name: Some(this.state.communityName),
163 auth: auth(false).ok(),
165 WebSocketService.Instance.send(wsClient.getCommunity(form));
168 componentDidMount() {
172 componentWillUnmount() {
173 saveScrollPosition(this.context);
174 this.subscription.unsubscribe();
177 static getDerivedStateFromProps(props: any): CommunityProps {
179 dataType: getDataTypeFromProps(props),
180 sort: getSortTypeFromProps(props),
181 page: getPageFromProps(props),
185 static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
186 let pathSplit = req.path.split("/");
187 let promises: Promise<any>[] = [];
189 let communityName = pathSplit[2];
190 let communityForm = new GetCommunity({
191 name: Some(communityName),
195 promises.push(req.client.getCommunity(communityForm));
197 let dataType: DataType = pathSplit[4]
198 ? DataType[pathSplit[4]]
201 let sort: Option<SortType> = toOption(
203 ? SortType[pathSplit[6]]
204 : UserService.Instance.myUserInfo.match({
206 Object.values(SortType)[
207 mui.local_user_view.local_user.default_sort_type
209 none: SortType.Active,
213 let page = toOption(pathSplit[8] ? Number(pathSplit[8]) : 1);
215 if (dataType == DataType.Post) {
216 let getPostsForm = new GetPosts({
217 community_name: Some(communityName),
220 limit: Some(fetchLimit),
222 type_: Some(ListingType.All),
223 saved_only: Some(false),
226 promises.push(req.client.getPosts(getPostsForm));
227 promises.push(Promise.resolve());
229 let getCommentsForm = new GetComments({
230 community_name: Some(communityName),
233 limit: Some(fetchLimit),
235 type_: Some(ListingType.All),
236 saved_only: Some(false),
239 promises.push(Promise.resolve());
240 promises.push(req.client.getComments(getCommentsForm));
246 componentDidUpdate(_: any, lastState: State) {
248 lastState.dataType !== this.state.dataType ||
249 lastState.sort !== this.state.sort ||
250 lastState.page !== this.state.page
252 this.setState({ postsLoading: true, commentsLoading: true });
257 get documentTitle(): string {
258 return this.state.communityRes.match({
260 this.state.siteRes.site_view.match({
262 `${res.community_view.community.title} - ${siteView.site.name}`,
271 <div class="container">
272 {this.state.communityLoading ? (
277 this.state.communityRes.match({
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}
288 <div class="col-12 col-md-8">
289 {this.communityInfo()}
290 <div class="d-block d-md-none">
292 class="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 {!res.community_view.community.local &&
319 showLocal={showLocal(this.isoData)}
333 page={this.state.page}
334 onChange={this.handlePageChange}
337 <div class="d-none d-md-block col-md-4">
339 community_view={res.community_view}
340 moderators={res.moderators}
341 admins={this.state.siteRes.admins}
343 enableNsfw={enableNsfw(this.state.siteRes)}
345 {!res.community_view.community.local &&
350 showLocal={showLocal(this.isoData)}
370 return this.state.dataType == DataType.Post ? (
371 this.state.postsLoading ? (
377 posts={this.state.posts}
379 enableDownvotes={enableDownvotes(this.state.siteRes)}
380 enableNsfw={enableNsfw(this.state.siteRes)}
383 ) : this.state.commentsLoading ? (
389 nodes={commentsToFlatNodes(this.state.comments)}
392 enableDownvotes={enableDownvotes(this.state.siteRes)}
393 moderators={this.state.communityRes.map(r => r.moderators)}
394 admins={Some(this.state.siteRes.admins)}
395 maxCommentsShown={None}
401 return this.state.communityRes
402 .map(r => r.community_view.community)
406 <BannerIconHeader banner={community.banner} icon={community.icon} />
407 <h5 class="mb-0 overflow-wrap-anywhere">{community.title}</h5>
409 community={community}
422 let communityRss = this.state.communityRes.map(r =>
423 communityRSSUrl(r.community_view.community.actor_id, this.state.sort)
429 type_={this.state.dataType}
430 onChange={this.handleDataTypeChange}
434 <SortSelect sort={this.state.sort} onChange={this.handleSortChange} />
436 {communityRss.match({
439 <a href={rss} title="RSS" rel={relTags}>
440 <Icon icon="rss" classes="text-muted small" />
442 <link rel="alternate" type="application/atom+xml" href={rss} />
451 handlePageChange(page: number) {
452 this.updateUrl({ page });
453 window.scrollTo(0, 0);
456 handleSortChange(val: SortType) {
457 this.updateUrl({ sort: val, page: 1 });
458 window.scrollTo(0, 0);
461 handleDataTypeChange(val: DataType) {
462 this.updateUrl({ dataType: DataType[val], page: 1 });
463 window.scrollTo(0, 0);
466 handleShowSidebarMobile(i: Community) {
467 i.state.showSidebarMobile = !i.state.showSidebarMobile;
471 updateUrl(paramUpdates: UrlParams) {
472 const dataTypeStr = paramUpdates.dataType || DataType[this.state.dataType];
473 const sortStr = paramUpdates.sort || this.state.sort;
474 const page = paramUpdates.page || this.state.page;
476 let typeView = `/c/${this.state.communityName}`;
478 this.props.history.push(
479 `${typeView}/data_type/${dataTypeStr}/sort/${sortStr}/page/${page}`
484 if (this.state.dataType == DataType.Post) {
485 let form = new GetPosts({
486 page: Some(this.state.page),
487 limit: Some(fetchLimit),
488 sort: Some(this.state.sort),
489 type_: Some(ListingType.All),
490 community_name: Some(this.state.communityName),
492 saved_only: Some(false),
493 auth: auth(false).ok(),
495 WebSocketService.Instance.send(wsClient.getPosts(form));
497 let form = new GetComments({
498 page: Some(this.state.page),
499 limit: Some(fetchLimit),
500 sort: Some(this.state.sort),
501 type_: Some(ListingType.All),
502 community_name: Some(this.state.communityName),
504 saved_only: Some(false),
505 auth: auth(false).ok(),
507 WebSocketService.Instance.send(wsClient.getComments(form));
511 parseMessage(msg: any) {
512 let op = wsUserOp(msg);
515 toast(i18n.t(msg.error), "danger");
516 this.context.router.history.push("/");
518 } else if (msg.reconnect) {
519 this.state.communityRes.match({
521 WebSocketService.Instance.send(
522 wsClient.communityJoin({
523 community_id: res.community_view.community.id,
530 } else if (op == UserOperation.GetCommunity) {
531 let data = wsJsonToRes<GetCommunityResponse>(msg, GetCommunityResponse);
532 this.state.communityRes = Some(data);
533 this.state.communityLoading = false;
534 this.setState(this.state);
535 // TODO why is there no auth in this form?
536 WebSocketService.Instance.send(
537 wsClient.communityJoin({
538 community_id: data.community_view.community.id,
542 op == UserOperation.EditCommunity ||
543 op == UserOperation.DeleteCommunity ||
544 op == UserOperation.RemoveCommunity
546 let data = wsJsonToRes<CommunityResponse>(msg, CommunityResponse);
547 this.state.communityRes.match({
548 some: res => (res.community_view = data.community_view),
551 this.setState(this.state);
552 } else if (op == UserOperation.FollowCommunity) {
553 let data = wsJsonToRes<CommunityResponse>(msg, CommunityResponse);
554 this.state.communityRes.match({
556 res.community_view.subscribed = data.community_view.subscribed;
557 res.community_view.counts.subscribers =
558 data.community_view.counts.subscribers;
562 this.setState(this.state);
563 } else if (op == UserOperation.GetPosts) {
564 let data = wsJsonToRes<GetPostsResponse>(msg, GetPostsResponse);
565 this.state.posts = data.posts;
566 this.state.postsLoading = false;
567 this.setState(this.state);
568 restoreScrollPosition(this.context);
571 op == UserOperation.EditPost ||
572 op == UserOperation.DeletePost ||
573 op == UserOperation.RemovePost ||
574 op == UserOperation.LockPost ||
575 op == UserOperation.StickyPost ||
576 op == UserOperation.SavePost
578 let data = wsJsonToRes<PostResponse>(msg, PostResponse);
579 editPostFindRes(data.post_view, this.state.posts);
580 this.setState(this.state);
581 } else if (op == UserOperation.CreatePost) {
582 let data = wsJsonToRes<PostResponse>(msg, PostResponse);
583 this.state.posts.unshift(data.post_view);
585 UserService.Instance.myUserInfo
586 .map(m => m.local_user_view.local_user.show_new_post_notifs)
589 notifyPost(data.post_view, this.context.router);
591 this.setState(this.state);
592 } else if (op == UserOperation.CreatePostLike) {
593 let data = wsJsonToRes<PostResponse>(msg, PostResponse);
594 createPostLikeFindRes(data.post_view, this.state.posts);
595 this.setState(this.state);
596 } else if (op == UserOperation.AddModToCommunity) {
597 let data = wsJsonToRes<AddModToCommunityResponse>(
599 AddModToCommunityResponse
601 this.state.communityRes.match({
602 some: res => (res.moderators = data.moderators),
605 this.setState(this.state);
606 } else if (op == UserOperation.BanFromCommunity) {
607 let data = wsJsonToRes<BanFromCommunityResponse>(
609 BanFromCommunityResponse
612 // TODO this might be incorrect
614 .filter(p => p.creator.id == data.person_view.person.id)
615 .forEach(p => (p.creator_banned_from_community = data.banned));
617 this.setState(this.state);
618 } else if (op == UserOperation.GetComments) {
619 let data = wsJsonToRes<GetCommentsResponse>(msg, GetCommentsResponse);
620 this.state.comments = data.comments;
621 this.state.commentsLoading = false;
622 this.setState(this.state);
624 op == UserOperation.EditComment ||
625 op == UserOperation.DeleteComment ||
626 op == UserOperation.RemoveComment
628 let data = wsJsonToRes<CommentResponse>(msg, CommentResponse);
629 editCommentRes(data.comment_view, this.state.comments);
630 this.setState(this.state);
631 } else if (op == UserOperation.CreateComment) {
632 let data = wsJsonToRes<CommentResponse>(msg, CommentResponse);
634 // Necessary since it might be a user reply
636 this.state.comments.unshift(data.comment_view);
637 this.setState(this.state);
639 } else if (op == UserOperation.SaveComment) {
640 let data = wsJsonToRes<CommentResponse>(msg, CommentResponse);
641 saveCommentRes(data.comment_view, this.state.comments);
642 this.setState(this.state);
643 } else if (op == UserOperation.CreateCommentLike) {
644 let data = wsJsonToRes<CommentResponse>(msg, CommentResponse);
645 createCommentLikeRes(data.comment_view, this.state.comments);
646 this.setState(this.state);
647 } else if (op == UserOperation.BlockPerson) {
648 let data = wsJsonToRes<BlockPersonResponse>(msg, BlockPersonResponse);
649 updatePersonBlock(data);
650 } else if (op == UserOperation.CreatePostReport) {
651 let data = wsJsonToRes<PostReportResponse>(msg, PostReportResponse);
653 toast(i18n.t("report_created"));
655 } else if (op == UserOperation.CreateCommentReport) {
656 let data = wsJsonToRes<CommentReportResponse>(msg, CommentReportResponse);
658 toast(i18n.t("report_created"));
660 } else if (op == UserOperation.PurgeCommunity) {
661 let data = wsJsonToRes<PurgeItemResponse>(msg, PurgeItemResponse);
663 toast(i18n.t("purge_success"));
664 this.context.router.history.push(`/`);