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,
55 postToCommentSortType,
57 restoreScrollPosition,
69 import { CommentNodes } from "../comment/comment-nodes";
70 import { BannerIconHeader } from "../common/banner-icon-header";
71 import { DataTypeSelect } from "../common/data-type-select";
72 import { HtmlTags } from "../common/html-tags";
73 import { Icon, Spinner } from "../common/icon";
74 import { Paginator } from "../common/paginator";
75 import { SortSelect } from "../common/sort-select";
76 import { Sidebar } from "../community/sidebar";
77 import { SiteSidebar } from "../home/site-sidebar";
78 import { PostListings } from "../post/post-listings";
79 import { CommunityLink } from "./community-link";
82 communityRes: Option<GetCommunityResponse>;
83 siteRes: GetSiteResponse;
84 communityName: string;
85 communityLoading: boolean;
86 postsLoading: boolean;
87 commentsLoading: boolean;
89 comments: CommentView[];
93 showSidebarMobile: boolean;
96 interface CommunityProps {
102 interface UrlParams {
108 export class Community extends Component<any, State> {
109 private isoData = setIsoData(
111 GetCommunityResponse,
115 private subscription: Subscription;
116 private emptyState: State = {
118 communityName: this.props.match.params.name,
119 communityLoading: true,
121 commentsLoading: true,
124 dataType: getDataTypeFromProps(this.props),
125 sort: getSortTypeFromProps(this.props),
126 page: getPageFromProps(this.props),
127 siteRes: this.isoData.site_res,
128 showSidebarMobile: false,
131 constructor(props: any, context: any) {
132 super(props, context);
134 this.state = this.emptyState;
135 this.handleSortChange = this.handleSortChange.bind(this);
136 this.handleDataTypeChange = this.handleDataTypeChange.bind(this);
137 this.handlePageChange = this.handlePageChange.bind(this);
139 this.parseMessage = this.parseMessage.bind(this);
140 this.subscription = wsSubscribe(this.parseMessage);
142 // Only fetch the data if coming from another route
143 if (this.isoData.path == this.context.router.route.match.url) {
146 communityRes: Some(this.isoData.routeData[0] as GetCommunityResponse),
148 let postsRes = Some(this.isoData.routeData[1] as GetPostsResponse);
149 let commentsRes = Some(this.isoData.routeData[2] as GetCommentsResponse);
151 if (postsRes.isSome()) {
152 this.state = { ...this.state, posts: postsRes.unwrap().posts };
155 if (commentsRes.isSome()) {
156 this.state = { ...this.state, comments: commentsRes.unwrap().comments };
161 communityLoading: false,
163 commentsLoading: false,
166 this.fetchCommunity();
172 let form = new GetCommunity({
173 name: Some(this.state.communityName),
175 auth: auth(false).ok(),
177 WebSocketService.Instance.send(wsClient.getCommunity(form));
180 componentDidMount() {
184 componentWillUnmount() {
185 saveScrollPosition(this.context);
186 this.subscription.unsubscribe();
189 static getDerivedStateFromProps(props: any): CommunityProps {
191 dataType: getDataTypeFromProps(props),
192 sort: getSortTypeFromProps(props),
193 page: getPageFromProps(props),
197 static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
198 let pathSplit = req.path.split("/");
199 let promises: Promise<any>[] = [];
201 let communityName = pathSplit[2];
202 let communityForm = new GetCommunity({
203 name: Some(communityName),
207 promises.push(req.client.getCommunity(communityForm));
209 let dataType: DataType = pathSplit[4]
210 ? DataType[pathSplit[4]]
213 let sort: Option<SortType> = toOption(
215 ? SortType[pathSplit[6]]
216 : UserService.Instance.myUserInfo.match({
218 Object.values(SortType)[
219 mui.local_user_view.local_user.default_sort_type
221 none: SortType.Active,
225 let page = toOption(pathSplit[8] ? Number(pathSplit[8]) : 1);
227 if (dataType == DataType.Post) {
228 let getPostsForm = new GetPosts({
229 community_name: Some(communityName),
232 limit: Some(fetchLimit),
234 type_: Some(ListingType.All),
235 saved_only: Some(false),
238 promises.push(req.client.getPosts(getPostsForm));
239 promises.push(Promise.resolve());
241 let getCommentsForm = new GetComments({
242 community_name: Some(communityName),
245 limit: Some(fetchLimit),
247 sort: sort.map(postToCommentSortType),
248 type_: Some(ListingType.All),
249 saved_only: Some(false),
254 promises.push(Promise.resolve());
255 promises.push(req.client.getComments(getCommentsForm));
261 componentDidUpdate(_: any, lastState: State) {
263 lastState.dataType !== this.state.dataType ||
264 lastState.sort !== this.state.sort ||
265 lastState.page !== this.state.page
267 this.setState({ postsLoading: true, commentsLoading: true });
272 get documentTitle(): string {
273 return this.state.communityRes.match({
275 this.state.siteRes.site_view.match({
277 `${res.community_view.community.title} - ${siteView.site.name}`,
286 <div className="container">
287 {this.state.communityLoading ? (
292 this.state.communityRes.match({
296 title={this.documentTitle}
297 path={this.context.router.route.match.url}
298 description={res.community_view.community.description}
299 image={res.community_view.community.icon}
302 <div className="row">
303 <div className="col-12 col-md-8">
304 {this.communityInfo()}
305 <div className="d-block d-md-none">
307 className="btn btn-secondary d-inline-block mb-2 mr-3"
308 onClick={linkEvent(this, this.handleShowSidebarMobile)}
310 {i18n.t("sidebar")}{" "}
313 this.state.showSidebarMobile
317 classes="icon-inline"
320 {this.state.showSidebarMobile && (
323 community_view={res.community_view}
324 moderators={res.moderators}
325 admins={this.state.siteRes.admins}
327 enableNsfw={enableNsfw(this.state.siteRes)}
329 {!res.community_view.community.local &&
334 showLocal={showLocal(this.isoData)}
348 page={this.state.page}
349 onChange={this.handlePageChange}
352 <div className="d-none d-md-block col-md-4">
354 community_view={res.community_view}
355 moderators={res.moderators}
356 admins={this.state.siteRes.admins}
358 enableNsfw={enableNsfw(this.state.siteRes)}
360 {!res.community_view.community.local &&
365 showLocal={showLocal(this.isoData)}
385 return this.state.dataType == DataType.Post ? (
386 this.state.postsLoading ? (
392 posts={this.state.posts}
394 enableDownvotes={enableDownvotes(this.state.siteRes)}
395 enableNsfw={enableNsfw(this.state.siteRes)}
396 allLanguages={this.state.siteRes.all_languages}
399 ) : this.state.commentsLoading ? (
405 nodes={commentsToFlatNodes(this.state.comments)}
406 viewType={CommentViewType.Flat}
409 enableDownvotes={enableDownvotes(this.state.siteRes)}
410 moderators={this.state.communityRes.map(r => r.moderators)}
411 admins={Some(this.state.siteRes.admins)}
412 maxCommentsShown={None}
413 allLanguages={this.state.siteRes.all_languages}
419 return this.state.communityRes
420 .map(r => r.community_view.community)
423 <div className="mb-2">
424 <BannerIconHeader banner={community.banner} icon={community.icon} />
425 <h5 className="mb-0 overflow-wrap-anywhere">{community.title}</h5>
427 community={community}
440 let communityRss = this.state.communityRes.map(r =>
441 communityRSSUrl(r.community_view.community.actor_id, this.state.sort)
444 <div className="mb-3">
445 <span className="mr-3">
447 type_={this.state.dataType}
448 onChange={this.handleDataTypeChange}
451 <span className="mr-2">
452 <SortSelect sort={this.state.sort} onChange={this.handleSortChange} />
454 {communityRss.match({
457 <a href={rss} title="RSS" rel={relTags}>
458 <Icon icon="rss" classes="text-muted small" />
460 <link rel="alternate" type="application/atom+xml" href={rss} />
469 handlePageChange(page: number) {
470 this.updateUrl({ page });
471 window.scrollTo(0, 0);
474 handleSortChange(val: SortType) {
475 this.updateUrl({ sort: val, page: 1 });
476 window.scrollTo(0, 0);
479 handleDataTypeChange(val: DataType) {
480 this.updateUrl({ dataType: DataType[val], page: 1 });
481 window.scrollTo(0, 0);
484 handleShowSidebarMobile(i: Community) {
485 i.setState({ showSidebarMobile: !i.state.showSidebarMobile });
488 updateUrl(paramUpdates: UrlParams) {
489 const dataTypeStr = paramUpdates.dataType || DataType[this.state.dataType];
490 const sortStr = paramUpdates.sort || this.state.sort;
491 const page = paramUpdates.page || this.state.page;
493 let typeView = `/c/${this.state.communityName}`;
495 this.props.history.push(
496 `${typeView}/data_type/${dataTypeStr}/sort/${sortStr}/page/${page}`
501 if (this.state.dataType == DataType.Post) {
502 let form = new GetPosts({
503 page: Some(this.state.page),
504 limit: Some(fetchLimit),
505 sort: Some(this.state.sort),
506 type_: Some(ListingType.All),
507 community_name: Some(this.state.communityName),
509 saved_only: Some(false),
510 auth: auth(false).ok(),
512 WebSocketService.Instance.send(wsClient.getPosts(form));
514 let form = new GetComments({
515 page: Some(this.state.page),
516 limit: Some(fetchLimit),
518 sort: Some(postToCommentSortType(this.state.sort)),
519 type_: Some(ListingType.All),
520 community_name: Some(this.state.communityName),
522 saved_only: Some(false),
525 auth: auth(false).ok(),
527 WebSocketService.Instance.send(wsClient.getComments(form));
531 parseMessage(msg: any) {
532 let op = wsUserOp(msg);
535 toast(i18n.t(msg.error), "danger");
536 this.context.router.history.push("/");
538 } else if (msg.reconnect) {
539 this.state.communityRes.match({
541 WebSocketService.Instance.send(
542 wsClient.communityJoin({
543 community_id: res.community_view.community.id,
550 } else if (op == UserOperation.GetCommunity) {
551 let data = wsJsonToRes<GetCommunityResponse>(msg, GetCommunityResponse);
552 this.setState({ communityRes: Some(data), communityLoading: false });
553 // TODO why is there no auth in this form?
554 WebSocketService.Instance.send(
555 wsClient.communityJoin({
556 community_id: data.community_view.community.id,
560 op == UserOperation.EditCommunity ||
561 op == UserOperation.DeleteCommunity ||
562 op == UserOperation.RemoveCommunity
564 let data = wsJsonToRes<CommunityResponse>(msg, CommunityResponse);
565 this.state.communityRes.match({
566 some: res => (res.community_view = data.community_view),
569 this.setState(this.state);
570 } else if (op == UserOperation.FollowCommunity) {
571 let data = wsJsonToRes<CommunityResponse>(msg, CommunityResponse);
572 this.state.communityRes.match({
574 res.community_view.subscribed = data.community_view.subscribed;
575 res.community_view.counts.subscribers =
576 data.community_view.counts.subscribers;
580 this.setState(this.state);
581 } else if (op == UserOperation.GetPosts) {
582 let data = wsJsonToRes<GetPostsResponse>(msg, GetPostsResponse);
583 this.setState({ posts: data.posts, postsLoading: false });
584 restoreScrollPosition(this.context);
587 op == UserOperation.EditPost ||
588 op == UserOperation.DeletePost ||
589 op == UserOperation.RemovePost ||
590 op == UserOperation.LockPost ||
591 op == UserOperation.StickyPost ||
592 op == UserOperation.SavePost
594 let data = wsJsonToRes<PostResponse>(msg, PostResponse);
595 editPostFindRes(data.post_view, this.state.posts);
596 this.setState(this.state);
597 } else if (op == UserOperation.CreatePost) {
598 let data = wsJsonToRes<PostResponse>(msg, PostResponse);
600 let showPostNotifs = UserService.Instance.myUserInfo
601 .map(m => m.local_user_view.local_user.show_new_post_notifs)
604 // Only push these if you're on the first page, you pass the nsfw check, and it isn't blocked
607 this.state.page == 1 &&
608 nsfwCheck(data.post_view) &&
609 !isPostBlocked(data.post_view)
611 this.state.posts.unshift(data.post_view);
612 if (showPostNotifs) {
613 notifyPost(data.post_view, this.context.router);
615 this.setState(this.state);
617 } else if (op == UserOperation.CreatePostLike) {
618 let data = wsJsonToRes<PostResponse>(msg, PostResponse);
619 createPostLikeFindRes(data.post_view, this.state.posts);
620 this.setState(this.state);
621 } else if (op == UserOperation.AddModToCommunity) {
622 let data = wsJsonToRes<AddModToCommunityResponse>(
624 AddModToCommunityResponse
626 this.state.communityRes.match({
627 some: res => (res.moderators = data.moderators),
630 this.setState(this.state);
631 } else if (op == UserOperation.BanFromCommunity) {
632 let data = wsJsonToRes<BanFromCommunityResponse>(
634 BanFromCommunityResponse
637 // TODO this might be incorrect
639 .filter(p => p.creator.id == data.person_view.person.id)
640 .forEach(p => (p.creator_banned_from_community = data.banned));
642 this.setState(this.state);
643 } else if (op == UserOperation.GetComments) {
644 let data = wsJsonToRes<GetCommentsResponse>(msg, GetCommentsResponse);
645 this.setState({ comments: data.comments, commentsLoading: false });
647 op == UserOperation.EditComment ||
648 op == UserOperation.DeleteComment ||
649 op == UserOperation.RemoveComment
651 let data = wsJsonToRes<CommentResponse>(msg, CommentResponse);
652 editCommentRes(data.comment_view, this.state.comments);
653 this.setState(this.state);
654 } else if (op == UserOperation.CreateComment) {
655 let data = wsJsonToRes<CommentResponse>(msg, CommentResponse);
657 // Necessary since it might be a user reply
659 this.state.comments.unshift(data.comment_view);
660 this.setState(this.state);
662 } else if (op == UserOperation.SaveComment) {
663 let data = wsJsonToRes<CommentResponse>(msg, CommentResponse);
664 saveCommentRes(data.comment_view, this.state.comments);
665 this.setState(this.state);
666 } else if (op == UserOperation.CreateCommentLike) {
667 let data = wsJsonToRes<CommentResponse>(msg, CommentResponse);
668 createCommentLikeRes(data.comment_view, this.state.comments);
669 this.setState(this.state);
670 } else if (op == UserOperation.BlockPerson) {
671 let data = wsJsonToRes<BlockPersonResponse>(msg, BlockPersonResponse);
672 updatePersonBlock(data);
673 } else if (op == UserOperation.CreatePostReport) {
674 let data = wsJsonToRes<PostReportResponse>(msg, PostReportResponse);
676 toast(i18n.t("report_created"));
678 } else if (op == UserOperation.CreateCommentReport) {
679 let data = wsJsonToRes<CommentReportResponse>(msg, CommentReportResponse);
681 toast(i18n.t("report_created"));
683 } else if (op == UserOperation.PurgeCommunity) {
684 let data = wsJsonToRes<PurgeItemResponse>(msg, PurgeItemResponse);
686 toast(i18n.t("purge_success"));
687 this.context.router.history.push(`/`);
689 } else if (op == UserOperation.BlockCommunity) {
690 let data = wsJsonToRes<BlockCommunityResponse>(
692 BlockCommunityResponse
694 this.state.communityRes.match({
695 some: res => (res.community_view.blocked = data.blocked),
698 updateCommunityBlock(data);
699 this.setState(this.state);