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 `${res.community_view.community.title} - ${this.state.siteRes.site_view.site.name}`,
281 // For some reason, this returns an empty vec if it matches the site langs
282 let communityLangs = this.state.communityRes.map(r => {
283 let langs = r.discussion_languages;
284 if (langs.length == 0) {
285 return this.state.siteRes.all_languages.map(l => l.id);
292 <div className="container-lg">
293 {this.state.communityLoading ? (
298 this.state.communityRes.match({
302 title={this.documentTitle}
303 path={this.context.router.route.match.url}
304 description={res.community_view.community.description}
305 image={res.community_view.community.icon}
308 <div className="row">
309 <div className="col-12 col-md-8">
310 {this.communityInfo()}
311 <div className="d-block d-md-none">
313 className="btn btn-secondary d-inline-block mb-2 mr-3"
314 onClick={linkEvent(this, this.handleShowSidebarMobile)}
316 {i18n.t("sidebar")}{" "}
319 this.state.showSidebarMobile
323 classes="icon-inline"
326 {this.state.showSidebarMobile && (
329 community_view={res.community_view}
330 moderators={res.moderators}
331 admins={this.state.siteRes.admins}
333 enableNsfw={enableNsfw(this.state.siteRes)}
335 allLanguages={this.state.siteRes.all_languages}
337 this.state.siteRes.discussion_languages
339 communityLanguages={communityLangs}
341 {!res.community_view.community.local &&
346 showLocal={showLocal(this.isoData)}
360 page={this.state.page}
361 onChange={this.handlePageChange}
364 <div className="d-none d-md-block col-md-4">
366 community_view={res.community_view}
367 moderators={res.moderators}
368 admins={this.state.siteRes.admins}
370 enableNsfw={enableNsfw(this.state.siteRes)}
372 allLanguages={this.state.siteRes.all_languages}
373 siteLanguages={this.state.siteRes.discussion_languages}
374 communityLanguages={communityLangs}
376 {!res.community_view.community.local &&
381 showLocal={showLocal(this.isoData)}
401 return this.state.dataType == DataType.Post ? (
402 this.state.postsLoading ? (
408 posts={this.state.posts}
410 enableDownvotes={enableDownvotes(this.state.siteRes)}
411 enableNsfw={enableNsfw(this.state.siteRes)}
412 allLanguages={this.state.siteRes.all_languages}
413 siteLanguages={this.state.siteRes.discussion_languages}
416 ) : this.state.commentsLoading ? (
422 nodes={commentsToFlatNodes(this.state.comments)}
423 viewType={CommentViewType.Flat}
426 enableDownvotes={enableDownvotes(this.state.siteRes)}
427 moderators={this.state.communityRes.map(r => r.moderators)}
428 admins={Some(this.state.siteRes.admins)}
429 maxCommentsShown={None}
430 allLanguages={this.state.siteRes.all_languages}
431 siteLanguages={this.state.siteRes.discussion_languages}
437 return this.state.communityRes
438 .map(r => r.community_view.community)
441 <div className="mb-2">
442 <BannerIconHeader banner={community.banner} icon={community.icon} />
443 <h5 className="mb-0 overflow-wrap-anywhere">{community.title}</h5>
445 community={community}
458 let communityRss = this.state.communityRes.map(r =>
459 communityRSSUrl(r.community_view.community.actor_id, this.state.sort)
462 <div className="mb-3">
463 <span className="mr-3">
465 type_={this.state.dataType}
466 onChange={this.handleDataTypeChange}
469 <span className="mr-2">
470 <SortSelect sort={this.state.sort} onChange={this.handleSortChange} />
472 {communityRss.match({
475 <a href={rss} title="RSS" rel={relTags}>
476 <Icon icon="rss" classes="text-muted small" />
478 <link rel="alternate" type="application/atom+xml" href={rss} />
487 handlePageChange(page: number) {
488 this.updateUrl({ page });
489 window.scrollTo(0, 0);
492 handleSortChange(val: SortType) {
493 this.updateUrl({ sort: val, page: 1 });
494 window.scrollTo(0, 0);
497 handleDataTypeChange(val: DataType) {
498 this.updateUrl({ dataType: DataType[val], page: 1 });
499 window.scrollTo(0, 0);
502 handleShowSidebarMobile(i: Community) {
503 i.setState({ showSidebarMobile: !i.state.showSidebarMobile });
506 updateUrl(paramUpdates: UrlParams) {
507 const dataTypeStr = paramUpdates.dataType || DataType[this.state.dataType];
508 const sortStr = paramUpdates.sort || this.state.sort;
509 const page = paramUpdates.page || this.state.page;
511 let typeView = `/c/${this.state.communityName}`;
513 this.props.history.push(
514 `${typeView}/data_type/${dataTypeStr}/sort/${sortStr}/page/${page}`
519 if (this.state.dataType == DataType.Post) {
520 let form = new GetPosts({
521 page: Some(this.state.page),
522 limit: Some(fetchLimit),
523 sort: Some(this.state.sort),
524 type_: Some(ListingType.All),
525 community_name: Some(this.state.communityName),
527 saved_only: Some(false),
528 auth: auth(false).ok(),
530 WebSocketService.Instance.send(wsClient.getPosts(form));
532 let form = new GetComments({
533 page: Some(this.state.page),
534 limit: Some(fetchLimit),
536 sort: Some(postToCommentSortType(this.state.sort)),
537 type_: Some(ListingType.All),
538 community_name: Some(this.state.communityName),
540 saved_only: Some(false),
543 auth: auth(false).ok(),
545 WebSocketService.Instance.send(wsClient.getComments(form));
549 parseMessage(msg: any) {
550 let op = wsUserOp(msg);
553 toast(i18n.t(msg.error), "danger");
554 this.context.router.history.push("/");
556 } else if (msg.reconnect) {
557 this.state.communityRes.match({
559 WebSocketService.Instance.send(
560 wsClient.communityJoin({
561 community_id: res.community_view.community.id,
568 } else if (op == UserOperation.GetCommunity) {
569 let data = wsJsonToRes<GetCommunityResponse>(msg, GetCommunityResponse);
570 this.setState({ communityRes: Some(data), communityLoading: false });
571 // TODO why is there no auth in this form?
572 WebSocketService.Instance.send(
573 wsClient.communityJoin({
574 community_id: data.community_view.community.id,
578 op == UserOperation.EditCommunity ||
579 op == UserOperation.DeleteCommunity ||
580 op == UserOperation.RemoveCommunity
582 let data = wsJsonToRes<CommunityResponse>(msg, CommunityResponse);
583 this.state.communityRes.match({
585 res.community_view = data.community_view;
586 res.discussion_languages = data.discussion_languages;
590 this.setState(this.state);
591 } else if (op == UserOperation.FollowCommunity) {
592 let data = wsJsonToRes<CommunityResponse>(msg, CommunityResponse);
593 this.state.communityRes.match({
595 res.community_view.subscribed = data.community_view.subscribed;
596 res.community_view.counts.subscribers =
597 data.community_view.counts.subscribers;
601 this.setState(this.state);
602 } else if (op == UserOperation.GetPosts) {
603 let data = wsJsonToRes<GetPostsResponse>(msg, GetPostsResponse);
604 this.setState({ posts: data.posts, postsLoading: false });
605 restoreScrollPosition(this.context);
608 op == UserOperation.EditPost ||
609 op == UserOperation.DeletePost ||
610 op == UserOperation.RemovePost ||
611 op == UserOperation.LockPost ||
612 op == UserOperation.FeaturePost ||
613 op == UserOperation.SavePost
615 let data = wsJsonToRes<PostResponse>(msg, PostResponse);
616 editPostFindRes(data.post_view, this.state.posts);
617 this.setState(this.state);
618 } else if (op == UserOperation.CreatePost) {
619 let data = wsJsonToRes<PostResponse>(msg, PostResponse);
621 let showPostNotifs = UserService.Instance.myUserInfo
622 .map(m => m.local_user_view.local_user.show_new_post_notifs)
625 // Only push these if you're on the first page, you pass the nsfw check, and it isn't blocked
628 this.state.page == 1 &&
629 nsfwCheck(data.post_view) &&
630 !isPostBlocked(data.post_view)
632 this.state.posts.unshift(data.post_view);
633 if (showPostNotifs) {
634 notifyPost(data.post_view, this.context.router);
636 this.setState(this.state);
638 } else if (op == UserOperation.CreatePostLike) {
639 let data = wsJsonToRes<PostResponse>(msg, PostResponse);
640 createPostLikeFindRes(data.post_view, this.state.posts);
641 this.setState(this.state);
642 } else if (op == UserOperation.AddModToCommunity) {
643 let data = wsJsonToRes<AddModToCommunityResponse>(
645 AddModToCommunityResponse
647 this.state.communityRes.match({
648 some: res => (res.moderators = data.moderators),
651 this.setState(this.state);
652 } else if (op == UserOperation.BanFromCommunity) {
653 let data = wsJsonToRes<BanFromCommunityResponse>(
655 BanFromCommunityResponse
658 // TODO this might be incorrect
660 .filter(p => p.creator.id == data.person_view.person.id)
661 .forEach(p => (p.creator_banned_from_community = data.banned));
663 this.setState(this.state);
664 } else if (op == UserOperation.GetComments) {
665 let data = wsJsonToRes<GetCommentsResponse>(msg, GetCommentsResponse);
666 this.setState({ comments: data.comments, commentsLoading: false });
668 op == UserOperation.EditComment ||
669 op == UserOperation.DeleteComment ||
670 op == UserOperation.RemoveComment
672 let data = wsJsonToRes<CommentResponse>(msg, CommentResponse);
673 editCommentRes(data.comment_view, this.state.comments);
674 this.setState(this.state);
675 } else if (op == UserOperation.CreateComment) {
676 let data = wsJsonToRes<CommentResponse>(msg, CommentResponse);
678 // Necessary since it might be a user reply
680 this.state.comments.unshift(data.comment_view);
681 this.setState(this.state);
683 } else if (op == UserOperation.SaveComment) {
684 let data = wsJsonToRes<CommentResponse>(msg, CommentResponse);
685 saveCommentRes(data.comment_view, this.state.comments);
686 this.setState(this.state);
687 } else if (op == UserOperation.CreateCommentLike) {
688 let data = wsJsonToRes<CommentResponse>(msg, CommentResponse);
689 createCommentLikeRes(data.comment_view, this.state.comments);
690 this.setState(this.state);
691 } else if (op == UserOperation.BlockPerson) {
692 let data = wsJsonToRes<BlockPersonResponse>(msg, BlockPersonResponse);
693 updatePersonBlock(data);
694 } else if (op == UserOperation.CreatePostReport) {
695 let data = wsJsonToRes<PostReportResponse>(msg, PostReportResponse);
697 toast(i18n.t("report_created"));
699 } else if (op == UserOperation.CreateCommentReport) {
700 let data = wsJsonToRes<CommentReportResponse>(msg, CommentReportResponse);
702 toast(i18n.t("report_created"));
704 } else if (op == UserOperation.PurgeCommunity) {
705 let data = wsJsonToRes<PurgeItemResponse>(msg, PurgeItemResponse);
707 toast(i18n.t("purge_success"));
708 this.context.router.history.push(`/`);
710 } else if (op == UserOperation.BlockCommunity) {
711 let data = wsJsonToRes<BlockCommunityResponse>(
713 BlockCommunityResponse
715 this.state.communityRes.match({
716 some: res => (res.community_view.blocked = data.blocked),
719 updateCommunityBlock(data);
720 this.setState(this.state);