1 import { Component, linkEvent } from "inferno";
2 import { RouteComponentProps } from "inferno-router/dist/Route";
6 AddModToCommunityResponse,
8 BanFromCommunityResponse,
39 MarkCommentReplyAsRead,
40 MarkPersonMentionAsRead,
54 } from "lemmy-js-client";
55 import { i18n } from "../../i18next";
60 } from "../../interfaces";
61 import { UserService } from "../../services";
62 import { FirstLoadService } from "../../services/FirstLoadService";
63 import { HttpService, RequestState } from "../../services/HttpService";
81 postToCommentSortType,
83 restoreScrollPosition,
92 import { CommentNodes } from "../comment/comment-nodes";
93 import { BannerIconHeader } from "../common/banner-icon-header";
94 import { DataTypeSelect } from "../common/data-type-select";
95 import { HtmlTags } from "../common/html-tags";
96 import { Icon, Spinner } from "../common/icon";
97 import { Paginator } from "../common/paginator";
98 import { SortSelect } from "../common/sort-select";
99 import { Sidebar } from "../community/sidebar";
100 import { SiteSidebar } from "../home/site-sidebar";
101 import { PostListings } from "../post/post-listings";
102 import { CommunityLink } from "./community-link";
104 type CommunityData = RouteDataResponse<{
105 communityRes: GetCommunityResponse;
106 postsRes: GetPostsResponse;
107 commentsRes: GetCommentsResponse;
111 communityRes: RequestState<GetCommunityResponse>;
112 postsRes: RequestState<GetPostsResponse>;
113 commentsRes: RequestState<GetCommentsResponse>;
114 siteRes: GetSiteResponse;
115 showSidebarMobile: boolean;
116 finished: Map<CommentId, boolean | undefined>;
117 isIsomorphic: boolean;
120 interface CommunityProps {
126 function getCommunityQueryParams() {
127 return getQueryParams<CommunityProps>({
128 dataType: getDataTypeFromQuery,
129 page: getPageFromString,
130 sort: getSortTypeFromQuery,
134 function getDataTypeFromQuery(type?: string): DataType {
135 return type ? DataType[type] : DataType.Post;
138 function getSortTypeFromQuery(type?: string): SortType {
140 UserService.Instance.myUserInfo?.local_user_view.local_user
143 return type ? (type as SortType) : mySortType ?? "Active";
146 export class Community extends Component<
147 RouteComponentProps<{ name: string }>,
150 private isoData = setIsoData<CommunityData>(this.context);
152 communityRes: { state: "empty" },
153 postsRes: { state: "empty" },
154 commentsRes: { state: "empty" },
155 siteRes: this.isoData.site_res,
156 showSidebarMobile: false,
161 constructor(props: RouteComponentProps<{ name: string }>, context: any) {
162 super(props, context);
164 this.handleSortChange = this.handleSortChange.bind(this);
165 this.handleDataTypeChange = this.handleDataTypeChange.bind(this);
166 this.handlePageChange = this.handlePageChange.bind(this);
168 // All of the action binds
169 this.handleDeleteCommunity = this.handleDeleteCommunity.bind(this);
170 this.handleEditCommunity = this.handleEditCommunity.bind(this);
171 this.handleFollow = this.handleFollow.bind(this);
172 this.handleRemoveCommunity = this.handleRemoveCommunity.bind(this);
173 this.handleCreateComment = this.handleCreateComment.bind(this);
174 this.handleEditComment = this.handleEditComment.bind(this);
175 this.handleSaveComment = this.handleSaveComment.bind(this);
176 this.handleBlockCommunity = this.handleBlockCommunity.bind(this);
177 this.handleBlockPerson = this.handleBlockPerson.bind(this);
178 this.handleDeleteComment = this.handleDeleteComment.bind(this);
179 this.handleRemoveComment = this.handleRemoveComment.bind(this);
180 this.handleCommentVote = this.handleCommentVote.bind(this);
181 this.handleAddModToCommunity = this.handleAddModToCommunity.bind(this);
182 this.handleAddAdmin = this.handleAddAdmin.bind(this);
183 this.handlePurgePerson = this.handlePurgePerson.bind(this);
184 this.handlePurgeComment = this.handlePurgeComment.bind(this);
185 this.handleCommentReport = this.handleCommentReport.bind(this);
186 this.handleDistinguishComment = this.handleDistinguishComment.bind(this);
187 this.handleTransferCommunity = this.handleTransferCommunity.bind(this);
188 this.handleCommentReplyRead = this.handleCommentReplyRead.bind(this);
189 this.handlePersonMentionRead = this.handlePersonMentionRead.bind(this);
190 this.handleBanFromCommunity = this.handleBanFromCommunity.bind(this);
191 this.handleBanPerson = this.handleBanPerson.bind(this);
192 this.handlePostVote = this.handlePostVote.bind(this);
193 this.handlePostEdit = this.handlePostEdit.bind(this);
194 this.handlePostReport = this.handlePostReport.bind(this);
195 this.handleLockPost = this.handleLockPost.bind(this);
196 this.handleDeletePost = this.handleDeletePost.bind(this);
197 this.handleRemovePost = this.handleRemovePost.bind(this);
198 this.handleSavePost = this.handleSavePost.bind(this);
199 this.handlePurgePost = this.handlePurgePost.bind(this);
200 this.handleFeaturePost = this.handleFeaturePost.bind(this);
202 // Only fetch the data if coming from another route
203 if (FirstLoadService.isFirstLoad) {
204 const { communityRes, commentsRes, postsRes } = this.isoData.routeData;
216 async fetchCommunity() {
217 this.setState({ communityRes: { state: "loading" } });
219 communityRes: await HttpService.client.getCommunity({
220 name: this.props.match.params.name,
226 async componentDidMount() {
227 if (!this.state.isIsomorphic) {
228 await Promise.all([this.fetchCommunity(), this.fetchData()]);
234 componentWillUnmount() {
235 saveScrollPosition(this.context);
238 static async fetchInitialData({
241 query: { dataType: urlDataType, page: urlPage, sort: urlSort },
243 }: InitialFetchRequest<QueryParams<CommunityProps>>): Promise<
244 Promise<CommunityData>
246 const pathSplit = path.split("/");
248 const communityName = pathSplit[2];
249 const communityForm: GetCommunity = {
254 const dataType = getDataTypeFromQuery(urlDataType);
256 const sort = getSortTypeFromQuery(urlSort);
258 const page = getPageFromString(urlPage);
260 let postsResponse: RequestState<GetPostsResponse> = { state: "empty" };
261 let commentsResponse: RequestState<GetCommentsResponse> = {
265 if (dataType === DataType.Post) {
266 const getPostsForm: GetPosts = {
267 community_name: communityName,
276 postsResponse = await client.getPosts(getPostsForm);
278 const getCommentsForm: GetComments = {
279 community_name: communityName,
282 sort: postToCommentSortType(sort),
288 commentsResponse = await client.getComments(getCommentsForm);
292 communityRes: await client.getCommunity(communityForm),
293 commentsRes: commentsResponse,
294 postsRes: postsResponse,
298 get documentTitle(): string {
299 const cRes = this.state.communityRes;
300 return cRes.state == "success"
301 ? `${cRes.data.community_view.community.title} - ${this.isoData.site_res.site_view.site.name}`
306 switch (this.state.communityRes.state) {
314 const res = this.state.communityRes.data;
315 const { page } = getCommunityQueryParams();
320 title={this.documentTitle}
321 path={this.context.router.route.match.url}
322 description={res.community_view.community.description}
323 image={res.community_view.community.icon}
326 <div className="row">
327 <div className="col-12 col-md-8">
328 {this.communityInfo(res)}
329 <div className="d-block d-md-none">
331 className="btn btn-secondary d-inline-block mb-2 mr-3"
332 onClick={linkEvent(this, this.handleShowSidebarMobile)}
334 {i18n.t("sidebar")}{" "}
337 this.state.showSidebarMobile
341 classes="icon-inline"
344 {this.state.showSidebarMobile && this.sidebar(res)}
348 <Paginator page={page} onChange={this.handlePageChange} />
350 <div className="d-none d-md-block col-md-4">
361 return <div className="container-lg">{this.renderCommunity()}</div>;
364 sidebar(res: GetCommunityResponse) {
365 const { site_res } = this.isoData;
366 // For some reason, this returns an empty vec if it matches the site langs
367 const communityLangs =
368 res.discussion_languages.length === 0
369 ? site_res.all_languages.map(({ id }) => id)
370 : res.discussion_languages;
375 community_view={res.community_view}
376 moderators={res.moderators}
377 admins={site_res.admins}
379 enableNsfw={enableNsfw(site_res)}
381 allLanguages={site_res.all_languages}
382 siteLanguages={site_res.discussion_languages}
383 communityLanguages={communityLangs}
384 onDeleteCommunity={this.handleDeleteCommunity}
385 onRemoveCommunity={this.handleRemoveCommunity}
386 onLeaveModTeam={this.handleAddModToCommunity}
387 onFollowCommunity={this.handleFollow}
388 onBlockCommunity={this.handleBlockCommunity}
389 onPurgeCommunity={this.handlePurgeCommunity}
390 onEditCommunity={this.handleEditCommunity}
392 {!res.community_view.community.local && res.site && (
393 <SiteSidebar site={res.site} showLocal={showLocal(this.isoData)} />
399 listings(communityRes: GetCommunityResponse) {
400 const { dataType } = getCommunityQueryParams();
401 const { site_res } = this.isoData;
403 if (dataType === DataType.Post) {
404 switch (this.state.postsRes.state) {
414 posts={this.state.postsRes.data.posts}
416 enableDownvotes={enableDownvotes(site_res)}
417 enableNsfw={enableNsfw(site_res)}
418 allLanguages={site_res.all_languages}
419 siteLanguages={site_res.discussion_languages}
420 onBlockPerson={this.handleBlockPerson}
421 onPostEdit={this.handlePostEdit}
422 onPostVote={this.handlePostVote}
423 onPostReport={this.handlePostReport}
424 onLockPost={this.handleLockPost}
425 onDeletePost={this.handleDeletePost}
426 onRemovePost={this.handleRemovePost}
427 onSavePost={this.handleSavePost}
428 onPurgePerson={this.handlePurgePerson}
429 onPurgePost={this.handlePurgePost}
430 onBanPerson={this.handleBanPerson}
431 onBanPersonFromCommunity={this.handleBanFromCommunity}
432 onAddModToCommunity={this.handleAddModToCommunity}
433 onAddAdmin={this.handleAddAdmin}
434 onTransferCommunity={this.handleTransferCommunity}
435 onFeaturePost={this.handleFeaturePost}
440 switch (this.state.commentsRes.state) {
450 nodes={commentsToFlatNodes(this.state.commentsRes.data.comments)}
451 viewType={CommentViewType.Flat}
452 finished={this.state.finished}
455 enableDownvotes={enableDownvotes(site_res)}
456 moderators={communityRes.moderators}
457 admins={site_res.admins}
458 allLanguages={site_res.all_languages}
459 siteLanguages={site_res.discussion_languages}
460 onSaveComment={this.handleSaveComment}
461 onBlockPerson={this.handleBlockPerson}
462 onDeleteComment={this.handleDeleteComment}
463 onRemoveComment={this.handleRemoveComment}
464 onCommentVote={this.handleCommentVote}
465 onCommentReport={this.handleCommentReport}
466 onDistinguishComment={this.handleDistinguishComment}
467 onAddModToCommunity={this.handleAddModToCommunity}
468 onAddAdmin={this.handleAddAdmin}
469 onTransferCommunity={this.handleTransferCommunity}
470 onPurgeComment={this.handlePurgeComment}
471 onPurgePerson={this.handlePurgePerson}
472 onCommentReplyRead={this.handleCommentReplyRead}
473 onPersonMentionRead={this.handlePersonMentionRead}
474 onBanPersonFromCommunity={this.handleBanFromCommunity}
475 onBanPerson={this.handleBanPerson}
476 onCreateComment={this.handleCreateComment}
477 onEditComment={this.handleEditComment}
484 communityInfo(res: GetCommunityResponse) {
485 const community = res.community_view.community;
489 <div className="mb-2">
490 <BannerIconHeader banner={community.banner} icon={community.icon} />
491 <h5 className="mb-0 overflow-wrap-anywhere">{community.title}</h5>
493 community={community}
504 selects(res: GetCommunityResponse) {
505 // let communityRss = this.state.communityRes.map(r =>
506 // communityRSSUrl(r.community_view.community.actor_id, this.state.sort)
508 const { dataType, sort } = getCommunityQueryParams();
509 const communityRss = res
510 ? communityRSSUrl(res.community_view.community.actor_id, sort)
514 <div className="mb-3">
515 <span className="mr-3">
518 onChange={this.handleDataTypeChange}
521 <span className="mr-2">
522 <SortSelect sort={sort} onChange={this.handleSortChange} />
526 <a href={communityRss} title="RSS" rel={relTags}>
527 <Icon icon="rss" classes="text-muted small" />
531 type="application/atom+xml"
540 handlePageChange(page: number) {
541 this.updateUrl({ page });
542 window.scrollTo(0, 0);
545 handleSortChange(sort: SortType) {
546 this.updateUrl({ sort, page: 1 });
547 window.scrollTo(0, 0);
550 handleDataTypeChange(dataType: DataType) {
551 this.updateUrl({ dataType, page: 1 });
552 window.scrollTo(0, 0);
555 handleShowSidebarMobile(i: Community) {
556 i.setState(({ showSidebarMobile }) => ({
557 showSidebarMobile: !showSidebarMobile,
561 async updateUrl({ dataType, page, sort }: Partial<CommunityProps>) {
563 dataType: urlDataType,
566 } = getCommunityQueryParams();
568 const queryParams: QueryParams<CommunityProps> = {
569 dataType: getDataTypeString(dataType ?? urlDataType),
570 page: (page ?? urlPage).toString(),
571 sort: sort ?? urlSort,
574 this.props.history.push(
575 `/c/${this.props.match.params.name}${getQueryString(queryParams)}`
578 await this.fetchData();
582 const { dataType, page, sort } = getCommunityQueryParams();
583 const { name } = this.props.match.params;
585 if (dataType === DataType.Post) {
586 this.setState({ postsRes: { state: "loading" } });
588 postsRes: await HttpService.client.getPosts({
593 community_name: name,
599 this.setState({ commentsRes: { state: "loading" } });
601 commentsRes: await HttpService.client.getComments({
604 sort: postToCommentSortType(sort),
606 community_name: name,
613 restoreScrollPosition(this.context);
617 async handleDeleteCommunity(form: DeleteCommunity) {
618 const deleteCommunityRes = await HttpService.client.deleteCommunity(form);
619 this.updateCommunity(deleteCommunityRes);
622 async handleAddModToCommunity(form: AddModToCommunity) {
623 const addModRes = await HttpService.client.addModToCommunity(form);
624 this.updateModerators(addModRes);
627 async handleFollow(form: FollowCommunity) {
628 const followCommunityRes = await HttpService.client.followCommunity(form);
629 this.updateCommunity(followCommunityRes);
632 if (followCommunityRes.state == "success") {
633 const communityId = followCommunityRes.data.community_view.community.id;
634 const mui = UserService.Instance.myUserInfo;
636 mui.follows = mui.follows.filter(i => i.community.id != communityId);
641 async handlePurgeCommunity(form: PurgeCommunity) {
642 const purgeCommunityRes = await HttpService.client.purgeCommunity(form);
643 this.purgeItem(purgeCommunityRes);
646 async handlePurgePerson(form: PurgePerson) {
647 const purgePersonRes = await HttpService.client.purgePerson(form);
648 this.purgeItem(purgePersonRes);
651 async handlePurgeComment(form: PurgeComment) {
652 const purgeCommentRes = await HttpService.client.purgeComment(form);
653 this.purgeItem(purgeCommentRes);
656 async handlePurgePost(form: PurgePost) {
657 const purgeRes = await HttpService.client.purgePost(form);
658 this.purgeItem(purgeRes);
661 async handleBlockCommunity(form: BlockCommunity) {
662 const blockCommunityRes = await HttpService.client.blockCommunity(form);
663 if (blockCommunityRes.state == "success") {
664 updateCommunityBlock(blockCommunityRes.data);
668 async handleBlockPerson(form: BlockPerson) {
669 const blockPersonRes = await HttpService.client.blockPerson(form);
670 if (blockPersonRes.state == "success") {
671 updatePersonBlock(blockPersonRes.data);
675 async handleRemoveCommunity(form: RemoveCommunity) {
676 const removeCommunityRes = await HttpService.client.removeCommunity(form);
677 this.updateCommunity(removeCommunityRes);
680 async handleEditCommunity(form: EditCommunity) {
681 const res = await HttpService.client.editCommunity(form);
682 this.updateCommunity(res);
687 async handleCreateComment(form: CreateComment) {
688 const createCommentRes = await HttpService.client.createComment(form);
689 this.createAndUpdateComments(createCommentRes);
691 return createCommentRes;
694 async handleEditComment(form: EditComment) {
695 const editCommentRes = await HttpService.client.editComment(form);
696 this.findAndUpdateComment(editCommentRes);
698 return editCommentRes;
701 async handleDeleteComment(form: DeleteComment) {
702 const deleteCommentRes = await HttpService.client.deleteComment(form);
703 this.findAndUpdateComment(deleteCommentRes);
706 async handleDeletePost(form: DeletePost) {
707 const deleteRes = await HttpService.client.deletePost(form);
708 this.findAndUpdatePost(deleteRes);
711 async handleRemovePost(form: RemovePost) {
712 const removeRes = await HttpService.client.removePost(form);
713 this.findAndUpdatePost(removeRes);
716 async handleRemoveComment(form: RemoveComment) {
717 const removeCommentRes = await HttpService.client.removeComment(form);
718 this.findAndUpdateComment(removeCommentRes);
721 async handleSaveComment(form: SaveComment) {
722 const saveCommentRes = await HttpService.client.saveComment(form);
723 this.findAndUpdateComment(saveCommentRes);
726 async handleSavePost(form: SavePost) {
727 const saveRes = await HttpService.client.savePost(form);
728 this.findAndUpdatePost(saveRes);
731 async handleFeaturePost(form: FeaturePost) {
732 const featureRes = await HttpService.client.featurePost(form);
733 this.findAndUpdatePost(featureRes);
736 async handleCommentVote(form: CreateCommentLike) {
737 const voteRes = await HttpService.client.likeComment(form);
738 this.findAndUpdateComment(voteRes);
741 async handlePostEdit(form: EditPost) {
742 const res = await HttpService.client.editPost(form);
743 this.findAndUpdatePost(res);
746 async handlePostVote(form: CreatePostLike) {
747 const voteRes = await HttpService.client.likePost(form);
748 this.findAndUpdatePost(voteRes);
751 async handleCommentReport(form: CreateCommentReport) {
752 const reportRes = await HttpService.client.createCommentReport(form);
753 if (reportRes.state == "success") {
754 toast(i18n.t("report_created"));
758 async handlePostReport(form: CreatePostReport) {
759 const reportRes = await HttpService.client.createPostReport(form);
760 if (reportRes.state == "success") {
761 toast(i18n.t("report_created"));
765 async handleLockPost(form: LockPost) {
766 const lockRes = await HttpService.client.lockPost(form);
767 this.findAndUpdatePost(lockRes);
770 async handleDistinguishComment(form: DistinguishComment) {
771 const distinguishRes = await HttpService.client.distinguishComment(form);
772 this.findAndUpdateComment(distinguishRes);
775 async handleAddAdmin(form: AddAdmin) {
776 const addAdminRes = await HttpService.client.addAdmin(form);
778 if (addAdminRes.state == "success") {
779 this.setState(s => ((s.siteRes.admins = addAdminRes.data.admins), s));
783 async handleTransferCommunity(form: TransferCommunity) {
784 const transferCommunityRes = await HttpService.client.transferCommunity(
787 toast(i18n.t("transfer_community"));
788 this.updateCommunityFull(transferCommunityRes);
791 async handleCommentReplyRead(form: MarkCommentReplyAsRead) {
792 const readRes = await HttpService.client.markCommentReplyAsRead(form);
793 this.findAndUpdateCommentReply(readRes);
796 async handlePersonMentionRead(form: MarkPersonMentionAsRead) {
797 // TODO not sure what to do here. Maybe it is actually optional, because post doesn't need it.
798 await HttpService.client.markPersonMentionAsRead(form);
801 async handleBanFromCommunity(form: BanFromCommunity) {
802 const banRes = await HttpService.client.banFromCommunity(form);
803 this.updateBanFromCommunity(banRes);
806 async handleBanPerson(form: BanPerson) {
807 const banRes = await HttpService.client.banPerson(form);
808 this.updateBan(banRes);
811 updateBanFromCommunity(banRes: RequestState<BanFromCommunityResponse>) {
812 // Maybe not necessary
813 if (banRes.state == "success") {
815 if (s.postsRes.state == "success") {
816 s.postsRes.data.posts
817 .filter(c => c.creator.id == banRes.data.person_view.person.id)
819 c => (c.creator_banned_from_community = banRes.data.banned)
822 if (s.commentsRes.state == "success") {
823 s.commentsRes.data.comments
824 .filter(c => c.creator.id == banRes.data.person_view.person.id)
826 c => (c.creator_banned_from_community = banRes.data.banned)
834 updateBan(banRes: RequestState<BanPersonResponse>) {
835 // Maybe not necessary
836 if (banRes.state == "success") {
838 if (s.postsRes.state == "success") {
839 s.postsRes.data.posts
840 .filter(c => c.creator.id == banRes.data.person_view.person.id)
841 .forEach(c => (c.creator.banned = banRes.data.banned));
843 if (s.commentsRes.state == "success") {
844 s.commentsRes.data.comments
845 .filter(c => c.creator.id == banRes.data.person_view.person.id)
846 .forEach(c => (c.creator.banned = banRes.data.banned));
853 updateCommunity(res: RequestState<CommunityResponse>) {
855 if (s.communityRes.state == "success" && res.state == "success") {
856 s.communityRes.data.community_view = res.data.community_view;
857 s.communityRes.data.discussion_languages =
858 res.data.discussion_languages;
864 updateCommunityFull(res: RequestState<GetCommunityResponse>) {
866 if (s.communityRes.state == "success" && res.state == "success") {
867 s.communityRes.data.community_view = res.data.community_view;
868 s.communityRes.data.moderators = res.data.moderators;
874 purgeItem(purgeRes: RequestState<PurgeItemResponse>) {
875 if (purgeRes.state == "success") {
876 toast(i18n.t("purge_success"));
877 this.context.router.history.push(`/`);
881 findAndUpdateComment(res: RequestState<CommentResponse>) {
883 if (s.commentsRes.state == "success" && res.state == "success") {
884 s.commentsRes.data.comments = editComment(
885 res.data.comment_view,
886 s.commentsRes.data.comments
888 s.finished.set(res.data.comment_view.comment.id, true);
894 createAndUpdateComments(res: RequestState<CommentResponse>) {
896 if (s.commentsRes.state == "success" && res.state == "success") {
897 s.commentsRes.data.comments.unshift(res.data.comment_view);
899 // Set finished for the parent
901 getCommentParentId(res.data.comment_view.comment) ?? 0,
909 findAndUpdateCommentReply(res: RequestState<CommentReplyResponse>) {
911 if (s.commentsRes.state == "success" && res.state == "success") {
912 s.commentsRes.data.comments = editWith(
913 res.data.comment_reply_view,
914 s.commentsRes.data.comments
921 findAndUpdatePost(res: RequestState<PostResponse>) {
923 if (s.postsRes.state == "success" && res.state == "success") {
924 s.postsRes.data.posts = editPost(
926 s.postsRes.data.posts
933 updateModerators(res: RequestState<AddModToCommunityResponse>) {
934 // Update the moderators
936 if (s.communityRes.state == "success" && res.state == "success") {
937 s.communityRes.data.moderators = res.data.moderators;