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 communityResponse: GetCommunityResponse;
106 postsResponse?: GetPostsResponse;
107 commentsResponse?: 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) {
205 communityResponse: communityRes,
206 commentsResponse: commentsRes,
207 postsResponse: postsRes,
208 } = this.isoData.routeData;
215 if (communityRes.state === "success") {
222 if (postsRes?.state === "success") {
229 if (commentsRes?.state === "success") {
238 async fetchCommunity() {
239 this.setState({ communityRes: { state: "loading" } });
241 communityRes: await HttpService.client.getCommunity({
242 name: this.props.match.params.name,
248 async componentDidMount() {
249 if (!this.state.isIsomorphic) {
250 await Promise.all([this.fetchCommunity(), this.fetchData()]);
256 componentWillUnmount() {
257 saveScrollPosition(this.context);
260 static async fetchInitialData({
263 query: { dataType: urlDataType, page: urlPage, sort: urlSort },
265 }: InitialFetchRequest<QueryParams<CommunityProps>>): Promise<
266 Promise<CommunityData>
268 const pathSplit = path.split("/");
270 const communityName = pathSplit[2];
271 const communityForm: GetCommunity = {
276 const dataType = getDataTypeFromQuery(urlDataType);
278 const sort = getSortTypeFromQuery(urlSort);
280 const page = getPageFromString(urlPage);
282 let postsResponse: RequestState<GetPostsResponse> | undefined = undefined;
283 let commentsResponse: RequestState<GetCommentsResponse> | undefined =
286 if (dataType === DataType.Post) {
287 const getPostsForm: GetPosts = {
288 community_name: communityName,
297 postsResponse = await client.getPosts(getPostsForm);
299 const getCommentsForm: GetComments = {
300 community_name: communityName,
303 sort: postToCommentSortType(sort),
309 commentsResponse = await client.getComments(getCommentsForm);
313 communityResponse: await client.getCommunity(communityForm),
319 get documentTitle(): string {
320 const cRes = this.state.communityRes;
321 return cRes.state == "success"
322 ? `${cRes.data.community_view.community.title} - ${this.isoData.site_res.site_view.site.name}`
327 switch (this.state.communityRes.state) {
335 const res = this.state.communityRes.data;
336 const { page } = getCommunityQueryParams();
341 title={this.documentTitle}
342 path={this.context.router.route.match.url}
343 description={res.community_view.community.description}
344 image={res.community_view.community.icon}
347 <div className="row">
348 <div className="col-12 col-md-8">
349 {this.communityInfo(res)}
350 <div className="d-block d-md-none">
352 className="btn btn-secondary d-inline-block mb-2 mr-3"
353 onClick={linkEvent(this, this.handleShowSidebarMobile)}
355 {i18n.t("sidebar")}{" "}
358 this.state.showSidebarMobile
362 classes="icon-inline"
365 {this.state.showSidebarMobile && this.sidebar(res)}
369 <Paginator page={page} onChange={this.handlePageChange} />
371 <div className="d-none d-md-block col-md-4">
382 return <div className="container-lg">{this.renderCommunity()}</div>;
385 sidebar(res: GetCommunityResponse) {
386 const { site_res } = this.isoData;
387 // For some reason, this returns an empty vec if it matches the site langs
388 const communityLangs =
389 res.discussion_languages.length === 0
390 ? site_res.all_languages.map(({ id }) => id)
391 : res.discussion_languages;
396 community_view={res.community_view}
397 moderators={res.moderators}
398 admins={site_res.admins}
400 enableNsfw={enableNsfw(site_res)}
402 allLanguages={site_res.all_languages}
403 siteLanguages={site_res.discussion_languages}
404 communityLanguages={communityLangs}
405 onDeleteCommunity={this.handleDeleteCommunity}
406 onRemoveCommunity={this.handleRemoveCommunity}
407 onLeaveModTeam={this.handleAddModToCommunity}
408 onFollowCommunity={this.handleFollow}
409 onBlockCommunity={this.handleBlockCommunity}
410 onPurgeCommunity={this.handlePurgeCommunity}
411 onEditCommunity={this.handleEditCommunity}
413 {!res.community_view.community.local && res.site && (
414 <SiteSidebar site={res.site} showLocal={showLocal(this.isoData)} />
420 listings(communityRes: GetCommunityResponse) {
421 const { dataType } = getCommunityQueryParams();
422 const { site_res } = this.isoData;
424 if (dataType === DataType.Post) {
425 switch (this.state.postsRes.state) {
435 posts={this.state.postsRes.data.posts}
437 enableDownvotes={enableDownvotes(site_res)}
438 enableNsfw={enableNsfw(site_res)}
439 allLanguages={site_res.all_languages}
440 siteLanguages={site_res.discussion_languages}
441 onBlockPerson={this.handleBlockPerson}
442 onPostEdit={this.handlePostEdit}
443 onPostVote={this.handlePostVote}
444 onPostReport={this.handlePostReport}
445 onLockPost={this.handleLockPost}
446 onDeletePost={this.handleDeletePost}
447 onRemovePost={this.handleRemovePost}
448 onSavePost={this.handleSavePost}
449 onPurgePerson={this.handlePurgePerson}
450 onPurgePost={this.handlePurgePost}
451 onBanPerson={this.handleBanPerson}
452 onBanPersonFromCommunity={this.handleBanFromCommunity}
453 onAddModToCommunity={this.handleAddModToCommunity}
454 onAddAdmin={this.handleAddAdmin}
455 onTransferCommunity={this.handleTransferCommunity}
456 onFeaturePost={this.handleFeaturePost}
461 switch (this.state.commentsRes.state) {
471 nodes={commentsToFlatNodes(this.state.commentsRes.data.comments)}
472 viewType={CommentViewType.Flat}
473 finished={this.state.finished}
476 enableDownvotes={enableDownvotes(site_res)}
477 moderators={communityRes.moderators}
478 admins={site_res.admins}
479 allLanguages={site_res.all_languages}
480 siteLanguages={site_res.discussion_languages}
481 onSaveComment={this.handleSaveComment}
482 onBlockPerson={this.handleBlockPerson}
483 onDeleteComment={this.handleDeleteComment}
484 onRemoveComment={this.handleRemoveComment}
485 onCommentVote={this.handleCommentVote}
486 onCommentReport={this.handleCommentReport}
487 onDistinguishComment={this.handleDistinguishComment}
488 onAddModToCommunity={this.handleAddModToCommunity}
489 onAddAdmin={this.handleAddAdmin}
490 onTransferCommunity={this.handleTransferCommunity}
491 onPurgeComment={this.handlePurgeComment}
492 onPurgePerson={this.handlePurgePerson}
493 onCommentReplyRead={this.handleCommentReplyRead}
494 onPersonMentionRead={this.handlePersonMentionRead}
495 onBanPersonFromCommunity={this.handleBanFromCommunity}
496 onBanPerson={this.handleBanPerson}
497 onCreateComment={this.handleCreateComment}
498 onEditComment={this.handleEditComment}
505 communityInfo(res: GetCommunityResponse) {
506 const community = res.community_view.community;
510 <div className="mb-2">
511 <BannerIconHeader banner={community.banner} icon={community.icon} />
512 <h5 className="mb-0 overflow-wrap-anywhere">{community.title}</h5>
514 community={community}
525 selects(res: GetCommunityResponse) {
526 // let communityRss = this.state.communityRes.map(r =>
527 // communityRSSUrl(r.community_view.community.actor_id, this.state.sort)
529 const { dataType, sort } = getCommunityQueryParams();
530 const communityRss = res
531 ? communityRSSUrl(res.community_view.community.actor_id, sort)
535 <div className="mb-3">
536 <span className="mr-3">
539 onChange={this.handleDataTypeChange}
542 <span className="mr-2">
543 <SortSelect sort={sort} onChange={this.handleSortChange} />
547 <a href={communityRss} title="RSS" rel={relTags}>
548 <Icon icon="rss" classes="text-muted small" />
552 type="application/atom+xml"
561 handlePageChange(page: number) {
562 this.updateUrl({ page });
563 window.scrollTo(0, 0);
566 handleSortChange(sort: SortType) {
567 this.updateUrl({ sort, page: 1 });
568 window.scrollTo(0, 0);
571 handleDataTypeChange(dataType: DataType) {
572 this.updateUrl({ dataType, page: 1 });
573 window.scrollTo(0, 0);
576 handleShowSidebarMobile(i: Community) {
577 i.setState(({ showSidebarMobile }) => ({
578 showSidebarMobile: !showSidebarMobile,
582 async updateUrl({ dataType, page, sort }: Partial<CommunityProps>) {
584 dataType: urlDataType,
587 } = getCommunityQueryParams();
589 const queryParams: QueryParams<CommunityProps> = {
590 dataType: getDataTypeString(dataType ?? urlDataType),
591 page: (page ?? urlPage).toString(),
592 sort: sort ?? urlSort,
595 this.props.history.push(
596 `/c/${this.props.match.params.name}${getQueryString(queryParams)}`
599 await this.fetchData();
603 const { dataType, page, sort } = getCommunityQueryParams();
604 const { name } = this.props.match.params;
606 if (dataType === DataType.Post) {
607 this.setState({ postsRes: { state: "loading" } });
609 postsRes: await HttpService.client.getPosts({
614 community_name: name,
620 this.setState({ commentsRes: { state: "loading" } });
622 commentsRes: await HttpService.client.getComments({
625 sort: postToCommentSortType(sort),
627 community_name: name,
634 restoreScrollPosition(this.context);
638 async handleDeleteCommunity(form: DeleteCommunity) {
639 const deleteCommunityRes = await HttpService.client.deleteCommunity(form);
640 this.updateCommunity(deleteCommunityRes);
643 async handleAddModToCommunity(form: AddModToCommunity) {
644 const addModRes = await HttpService.client.addModToCommunity(form);
645 this.updateModerators(addModRes);
648 async handleFollow(form: FollowCommunity) {
649 const followCommunityRes = await HttpService.client.followCommunity(form);
650 this.updateCommunity(followCommunityRes);
653 if (followCommunityRes.state == "success") {
654 const communityId = followCommunityRes.data.community_view.community.id;
655 const mui = UserService.Instance.myUserInfo;
657 mui.follows = mui.follows.filter(i => i.community.id != communityId);
662 async handlePurgeCommunity(form: PurgeCommunity) {
663 const purgeCommunityRes = await HttpService.client.purgeCommunity(form);
664 this.purgeItem(purgeCommunityRes);
667 async handlePurgePerson(form: PurgePerson) {
668 const purgePersonRes = await HttpService.client.purgePerson(form);
669 this.purgeItem(purgePersonRes);
672 async handlePurgeComment(form: PurgeComment) {
673 const purgeCommentRes = await HttpService.client.purgeComment(form);
674 this.purgeItem(purgeCommentRes);
677 async handlePurgePost(form: PurgePost) {
678 const purgeRes = await HttpService.client.purgePost(form);
679 this.purgeItem(purgeRes);
682 async handleBlockCommunity(form: BlockCommunity) {
683 const blockCommunityRes = await HttpService.client.blockCommunity(form);
684 if (blockCommunityRes.state == "success") {
685 updateCommunityBlock(blockCommunityRes.data);
689 async handleBlockPerson(form: BlockPerson) {
690 const blockPersonRes = await HttpService.client.blockPerson(form);
691 if (blockPersonRes.state == "success") {
692 updatePersonBlock(blockPersonRes.data);
696 async handleRemoveCommunity(form: RemoveCommunity) {
697 const removeCommunityRes = await HttpService.client.removeCommunity(form);
698 this.updateCommunity(removeCommunityRes);
701 async handleEditCommunity(form: EditCommunity) {
702 const res = await HttpService.client.editCommunity(form);
703 this.updateCommunity(res);
708 async handleCreateComment(form: CreateComment) {
709 const createCommentRes = await HttpService.client.createComment(form);
710 this.createAndUpdateComments(createCommentRes);
712 return createCommentRes;
715 async handleEditComment(form: EditComment) {
716 const editCommentRes = await HttpService.client.editComment(form);
717 this.findAndUpdateComment(editCommentRes);
719 return editCommentRes;
722 async handleDeleteComment(form: DeleteComment) {
723 const deleteCommentRes = await HttpService.client.deleteComment(form);
724 this.findAndUpdateComment(deleteCommentRes);
727 async handleDeletePost(form: DeletePost) {
728 const deleteRes = await HttpService.client.deletePost(form);
729 this.findAndUpdatePost(deleteRes);
732 async handleRemovePost(form: RemovePost) {
733 const removeRes = await HttpService.client.removePost(form);
734 this.findAndUpdatePost(removeRes);
737 async handleRemoveComment(form: RemoveComment) {
738 const removeCommentRes = await HttpService.client.removeComment(form);
739 this.findAndUpdateComment(removeCommentRes);
742 async handleSaveComment(form: SaveComment) {
743 const saveCommentRes = await HttpService.client.saveComment(form);
744 this.findAndUpdateComment(saveCommentRes);
747 async handleSavePost(form: SavePost) {
748 const saveRes = await HttpService.client.savePost(form);
749 this.findAndUpdatePost(saveRes);
752 async handleFeaturePost(form: FeaturePost) {
753 const featureRes = await HttpService.client.featurePost(form);
754 this.findAndUpdatePost(featureRes);
757 async handleCommentVote(form: CreateCommentLike) {
758 const voteRes = await HttpService.client.likeComment(form);
759 this.findAndUpdateComment(voteRes);
762 async handlePostEdit(form: EditPost) {
763 const res = await HttpService.client.editPost(form);
764 this.findAndUpdatePost(res);
767 async handlePostVote(form: CreatePostLike) {
768 const voteRes = await HttpService.client.likePost(form);
769 this.findAndUpdatePost(voteRes);
772 async handleCommentReport(form: CreateCommentReport) {
773 const reportRes = await HttpService.client.createCommentReport(form);
774 if (reportRes.state == "success") {
775 toast(i18n.t("report_created"));
779 async handlePostReport(form: CreatePostReport) {
780 const reportRes = await HttpService.client.createPostReport(form);
781 if (reportRes.state == "success") {
782 toast(i18n.t("report_created"));
786 async handleLockPost(form: LockPost) {
787 const lockRes = await HttpService.client.lockPost(form);
788 this.findAndUpdatePost(lockRes);
791 async handleDistinguishComment(form: DistinguishComment) {
792 const distinguishRes = await HttpService.client.distinguishComment(form);
793 this.findAndUpdateComment(distinguishRes);
796 async handleAddAdmin(form: AddAdmin) {
797 const addAdminRes = await HttpService.client.addAdmin(form);
799 if (addAdminRes.state == "success") {
800 this.setState(s => ((s.siteRes.admins = addAdminRes.data.admins), s));
804 async handleTransferCommunity(form: TransferCommunity) {
805 const transferCommunityRes = await HttpService.client.transferCommunity(
808 toast(i18n.t("transfer_community"));
809 this.updateCommunityFull(transferCommunityRes);
812 async handleCommentReplyRead(form: MarkCommentReplyAsRead) {
813 const readRes = await HttpService.client.markCommentReplyAsRead(form);
814 this.findAndUpdateCommentReply(readRes);
817 async handlePersonMentionRead(form: MarkPersonMentionAsRead) {
818 // TODO not sure what to do here. Maybe it is actually optional, because post doesn't need it.
819 await HttpService.client.markPersonMentionAsRead(form);
822 async handleBanFromCommunity(form: BanFromCommunity) {
823 const banRes = await HttpService.client.banFromCommunity(form);
824 this.updateBanFromCommunity(banRes);
827 async handleBanPerson(form: BanPerson) {
828 const banRes = await HttpService.client.banPerson(form);
829 this.updateBan(banRes);
832 updateBanFromCommunity(banRes: RequestState<BanFromCommunityResponse>) {
833 // Maybe not necessary
834 if (banRes.state == "success") {
836 if (s.postsRes.state == "success") {
837 s.postsRes.data.posts
838 .filter(c => c.creator.id == banRes.data.person_view.person.id)
840 c => (c.creator_banned_from_community = 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)
847 c => (c.creator_banned_from_community = banRes.data.banned)
855 updateBan(banRes: RequestState<BanPersonResponse>) {
856 // Maybe not necessary
857 if (banRes.state == "success") {
859 if (s.postsRes.state == "success") {
860 s.postsRes.data.posts
861 .filter(c => c.creator.id == banRes.data.person_view.person.id)
862 .forEach(c => (c.creator.banned = banRes.data.banned));
864 if (s.commentsRes.state == "success") {
865 s.commentsRes.data.comments
866 .filter(c => c.creator.id == banRes.data.person_view.person.id)
867 .forEach(c => (c.creator.banned = banRes.data.banned));
874 updateCommunity(res: RequestState<CommunityResponse>) {
876 if (s.communityRes.state == "success" && res.state == "success") {
877 s.communityRes.data.community_view = res.data.community_view;
878 s.communityRes.data.discussion_languages =
879 res.data.discussion_languages;
885 updateCommunityFull(res: RequestState<GetCommunityResponse>) {
887 if (s.communityRes.state == "success" && res.state == "success") {
888 s.communityRes.data.community_view = res.data.community_view;
889 s.communityRes.data.moderators = res.data.moderators;
895 purgeItem(purgeRes: RequestState<PurgeItemResponse>) {
896 if (purgeRes.state == "success") {
897 toast(i18n.t("purge_success"));
898 this.context.router.history.push(`/`);
902 findAndUpdateComment(res: RequestState<CommentResponse>) {
904 if (s.commentsRes.state == "success" && res.state == "success") {
905 s.commentsRes.data.comments = editComment(
906 res.data.comment_view,
907 s.commentsRes.data.comments
909 s.finished.set(res.data.comment_view.comment.id, true);
915 createAndUpdateComments(res: RequestState<CommentResponse>) {
917 if (s.commentsRes.state == "success" && res.state == "success") {
918 s.commentsRes.data.comments.unshift(res.data.comment_view);
920 // Set finished for the parent
922 getCommentParentId(res.data.comment_view.comment) ?? 0,
930 findAndUpdateCommentReply(res: RequestState<CommentReplyResponse>) {
932 if (s.commentsRes.state == "success" && res.state == "success") {
933 s.commentsRes.data.comments = editWith(
934 res.data.comment_reply_view,
935 s.commentsRes.data.comments
942 findAndUpdatePost(res: RequestState<PostResponse>) {
944 if (s.postsRes.state == "success" && res.state == "success") {
945 s.postsRes.data.posts = editPost(
947 s.postsRes.data.posts
954 updateModerators(res: RequestState<AddModToCommunityResponse>) {
955 // Update the moderators
957 if (s.communityRes.state == "success" && res.state == "success") {
958 s.communityRes.data.moderators = res.data.moderators;