12 postToCommentSortType,
22 } from "@utils/helpers";
23 import type { QueryParams } from "@utils/types";
24 import { RouteDataResponse } from "@utils/types";
25 import { Component, RefObject, createRef, linkEvent } from "inferno";
26 import { RouteComponentProps } from "inferno-router/dist/Route";
30 AddModToCommunityResponse,
32 BanFromCommunityResponse,
63 MarkCommentReplyAsRead,
64 MarkPersonMentionAsRead,
78 } from "lemmy-js-client";
79 import { fetchLimit, relTags } from "../../config";
84 } from "../../interfaces";
85 import { FirstLoadService, I18NextService, UserService } from "../../services";
86 import { HttpService, RequestState } from "../../services/HttpService";
87 import { setupTippy } from "../../tippy";
88 import { toast } from "../../toast";
89 import { CommentNodes } from "../comment/comment-nodes";
90 import { BannerIconHeader } from "../common/banner-icon-header";
91 import { DataTypeSelect } from "../common/data-type-select";
92 import { HtmlTags } from "../common/html-tags";
93 import { Icon, Spinner } from "../common/icon";
94 import { Paginator } from "../common/paginator";
95 import { SortSelect } from "../common/sort-select";
96 import { Sidebar } from "../community/sidebar";
97 import { SiteSidebar } from "../home/site-sidebar";
98 import { PostListings } from "../post/post-listings";
99 import { CommunityLink } from "./community-link";
101 type CommunityData = RouteDataResponse<{
102 communityRes: GetCommunityResponse;
103 postsRes: GetPostsResponse;
104 commentsRes: GetCommentsResponse;
108 communityRes: RequestState<GetCommunityResponse>;
109 postsRes: RequestState<GetPostsResponse>;
110 commentsRes: RequestState<GetCommentsResponse>;
111 siteRes: GetSiteResponse;
112 showSidebarMobile: boolean;
113 finished: Map<CommentId, boolean | undefined>;
114 isIsomorphic: boolean;
117 interface CommunityProps {
123 function getCommunityQueryParams() {
124 return getQueryParams<CommunityProps>({
125 dataType: getDataTypeFromQuery,
126 page: getPageFromString,
127 sort: getSortTypeFromQuery,
131 function getDataTypeFromQuery(type?: string): DataType {
132 return type ? DataType[type] : DataType.Post;
135 function getSortTypeFromQuery(type?: string): SortType {
137 UserService.Instance.myUserInfo?.local_user_view.local_user
140 return type ? (type as SortType) : mySortType ?? "Active";
143 export class Community extends Component<
144 RouteComponentProps<{ name: string }>,
147 private isoData = setIsoData<CommunityData>(this.context);
149 communityRes: { state: "empty" },
150 postsRes: { state: "empty" },
151 commentsRes: { state: "empty" },
152 siteRes: this.isoData.site_res,
153 showSidebarMobile: false,
157 private readonly mainContentRef: RefObject<HTMLElement>;
158 constructor(props: RouteComponentProps<{ name: string }>, context: any) {
159 super(props, context);
161 this.handleSortChange = this.handleSortChange.bind(this);
162 this.handleDataTypeChange = this.handleDataTypeChange.bind(this);
163 this.handlePageChange = this.handlePageChange.bind(this);
165 // All of the action binds
166 this.handleDeleteCommunity = this.handleDeleteCommunity.bind(this);
167 this.handleEditCommunity = this.handleEditCommunity.bind(this);
168 this.handleFollow = this.handleFollow.bind(this);
169 this.handleRemoveCommunity = this.handleRemoveCommunity.bind(this);
170 this.handleCreateComment = this.handleCreateComment.bind(this);
171 this.handleEditComment = this.handleEditComment.bind(this);
172 this.handleSaveComment = this.handleSaveComment.bind(this);
173 this.handleBlockCommunity = this.handleBlockCommunity.bind(this);
174 this.handleBlockPerson = this.handleBlockPerson.bind(this);
175 this.handleDeleteComment = this.handleDeleteComment.bind(this);
176 this.handleRemoveComment = this.handleRemoveComment.bind(this);
177 this.handleCommentVote = this.handleCommentVote.bind(this);
178 this.handleAddModToCommunity = this.handleAddModToCommunity.bind(this);
179 this.handleAddAdmin = this.handleAddAdmin.bind(this);
180 this.handlePurgePerson = this.handlePurgePerson.bind(this);
181 this.handlePurgeComment = this.handlePurgeComment.bind(this);
182 this.handleCommentReport = this.handleCommentReport.bind(this);
183 this.handleDistinguishComment = this.handleDistinguishComment.bind(this);
184 this.handleTransferCommunity = this.handleTransferCommunity.bind(this);
185 this.handleCommentReplyRead = this.handleCommentReplyRead.bind(this);
186 this.handlePersonMentionRead = this.handlePersonMentionRead.bind(this);
187 this.handleBanFromCommunity = this.handleBanFromCommunity.bind(this);
188 this.handleBanPerson = this.handleBanPerson.bind(this);
189 this.handlePostVote = this.handlePostVote.bind(this);
190 this.handlePostEdit = this.handlePostEdit.bind(this);
191 this.handlePostReport = this.handlePostReport.bind(this);
192 this.handleLockPost = this.handleLockPost.bind(this);
193 this.handleDeletePost = this.handleDeletePost.bind(this);
194 this.handleRemovePost = this.handleRemovePost.bind(this);
195 this.handleSavePost = this.handleSavePost.bind(this);
196 this.handlePurgePost = this.handlePurgePost.bind(this);
197 this.handleFeaturePost = this.handleFeaturePost.bind(this);
198 this.mainContentRef = createRef();
199 // Only fetch the data if coming from another route
200 if (FirstLoadService.isFirstLoad) {
201 const { communityRes, commentsRes, postsRes } = this.isoData.routeData;
213 async fetchCommunity() {
214 this.setState({ communityRes: { state: "loading" } });
216 communityRes: await HttpService.client.getCommunity({
217 name: this.props.match.params.name,
223 async componentDidMount() {
224 if (!this.state.isIsomorphic) {
225 await Promise.all([this.fetchCommunity(), this.fetchData()]);
231 static async fetchInitialData({
234 query: { dataType: urlDataType, page: urlPage, sort: urlSort },
236 }: InitialFetchRequest<QueryParams<CommunityProps>>): Promise<
237 Promise<CommunityData>
239 const pathSplit = path.split("/");
241 const communityName = pathSplit[2];
242 const communityForm: GetCommunity = {
247 const dataType = getDataTypeFromQuery(urlDataType);
249 const sort = getSortTypeFromQuery(urlSort);
251 const page = getPageFromString(urlPage);
253 let postsResponse: RequestState<GetPostsResponse> = { state: "empty" };
254 let commentsResponse: RequestState<GetCommentsResponse> = {
258 if (dataType === DataType.Post) {
259 const getPostsForm: GetPosts = {
260 community_name: communityName,
269 postsResponse = await client.getPosts(getPostsForm);
271 const getCommentsForm: GetComments = {
272 community_name: communityName,
275 sort: postToCommentSortType(sort),
281 commentsResponse = await client.getComments(getCommentsForm);
285 communityRes: await client.getCommunity(communityForm),
286 commentsRes: commentsResponse,
287 postsRes: postsResponse,
291 get documentTitle(): string {
292 const cRes = this.state.communityRes;
293 return cRes.state === "success"
294 ? `${cRes.data.community_view.community.title} - ${this.isoData.site_res.site_view.site.name}`
299 switch (this.state.communityRes.state) {
307 const res = this.state.communityRes.data;
308 const { page } = getCommunityQueryParams();
313 title={this.documentTitle}
314 path={this.context.router.route.match.url}
315 canonicalPath={res.community_view.community.actor_id}
316 description={res.community_view.community.description}
317 image={res.community_view.community.icon}
320 <div className="row">
322 className="col-12 col-md-8 col-lg-9"
323 ref={this.mainContentRef}
325 {this.communityInfo(res)}
326 <div className="d-block d-md-none">
328 className="btn btn-secondary d-inline-block mb-2 me-3"
329 onClick={linkEvent(this, this.handleShowSidebarMobile)}
331 {I18NextService.i18n.t("sidebar")}{" "}
334 this.state.showSidebarMobile
338 classes="icon-inline"
341 {this.state.showSidebarMobile && this.sidebar(res)}
345 <Paginator page={page} onChange={this.handlePageChange} />
347 <aside className="d-none d-md-block col-md-4 col-lg-3">
359 <div className="community container-lg">{this.renderCommunity()}</div>
363 sidebar(res: GetCommunityResponse) {
364 const { site_res } = this.isoData;
365 // For some reason, this returns an empty vec if it matches the site langs
366 const communityLangs =
367 res.discussion_languages.length === 0
368 ? site_res.all_languages.map(({ id }) => id)
369 : res.discussion_languages;
374 community_view={res.community_view}
375 moderators={res.moderators}
376 admins={site_res.admins}
377 enableNsfw={enableNsfw(site_res)}
379 allLanguages={site_res.all_languages}
380 siteLanguages={site_res.discussion_languages}
381 communityLanguages={communityLangs}
382 onDeleteCommunity={this.handleDeleteCommunity}
383 onRemoveCommunity={this.handleRemoveCommunity}
384 onLeaveModTeam={this.handleAddModToCommunity}
385 onFollowCommunity={this.handleFollow}
386 onBlockCommunity={this.handleBlockCommunity}
387 onPurgeCommunity={this.handlePurgeCommunity}
388 onEditCommunity={this.handleEditCommunity}
390 {!res.community_view.community.local && res.site && (
391 <SiteSidebar site={res.site} showLocal={showLocal(this.isoData)} />
397 listings(communityRes: GetCommunityResponse) {
398 const { dataType } = getCommunityQueryParams();
399 const { site_res } = this.isoData;
401 if (dataType === DataType.Post) {
402 switch (this.state.postsRes.state) {
412 posts={this.state.postsRes.data.posts}
414 enableDownvotes={enableDownvotes(site_res)}
415 enableNsfw={enableNsfw(site_res)}
416 allLanguages={site_res.all_languages}
417 siteLanguages={site_res.discussion_languages}
418 onBlockPerson={this.handleBlockPerson}
419 onPostEdit={this.handlePostEdit}
420 onPostVote={this.handlePostVote}
421 onPostReport={this.handlePostReport}
422 onLockPost={this.handleLockPost}
423 onDeletePost={this.handleDeletePost}
424 onRemovePost={this.handleRemovePost}
425 onSavePost={this.handleSavePost}
426 onPurgePerson={this.handlePurgePerson}
427 onPurgePost={this.handlePurgePost}
428 onBanPerson={this.handleBanPerson}
429 onBanPersonFromCommunity={this.handleBanFromCommunity}
430 onAddModToCommunity={this.handleAddModToCommunity}
431 onAddAdmin={this.handleAddAdmin}
432 onTransferCommunity={this.handleTransferCommunity}
433 onFeaturePost={this.handleFeaturePost}
438 switch (this.state.commentsRes.state) {
448 nodes={commentsToFlatNodes(this.state.commentsRes.data.comments)}
449 viewType={CommentViewType.Flat}
450 finished={this.state.finished}
453 enableDownvotes={enableDownvotes(site_res)}
454 moderators={communityRes.moderators}
455 admins={site_res.admins}
456 allLanguages={site_res.all_languages}
457 siteLanguages={site_res.discussion_languages}
458 onSaveComment={this.handleSaveComment}
459 onBlockPerson={this.handleBlockPerson}
460 onDeleteComment={this.handleDeleteComment}
461 onRemoveComment={this.handleRemoveComment}
462 onCommentVote={this.handleCommentVote}
463 onCommentReport={this.handleCommentReport}
464 onDistinguishComment={this.handleDistinguishComment}
465 onAddModToCommunity={this.handleAddModToCommunity}
466 onAddAdmin={this.handleAddAdmin}
467 onTransferCommunity={this.handleTransferCommunity}
468 onPurgeComment={this.handlePurgeComment}
469 onPurgePerson={this.handlePurgePerson}
470 onCommentReplyRead={this.handleCommentReplyRead}
471 onPersonMentionRead={this.handlePersonMentionRead}
472 onBanPersonFromCommunity={this.handleBanFromCommunity}
473 onBanPerson={this.handleBanPerson}
474 onCreateComment={this.handleCreateComment}
475 onEditComment={this.handleEditComment}
482 communityInfo(res: GetCommunityResponse) {
483 const community = res.community_view.community;
487 <div className="mb-2">
488 <BannerIconHeader banner={community.banner} icon={community.icon} />
489 <h1 className="h4 mb-0 overflow-wrap-anywhere">{community.title}</h1>
491 community={community}
502 selects(res: GetCommunityResponse) {
503 // let communityRss = this.state.communityRes.map(r =>
504 // communityRSSUrl(r.community_view.community.actor_id, this.state.sort)
506 const { dataType, sort } = getCommunityQueryParams();
507 const communityRss = res
508 ? communityRSSUrl(res.community_view.community.actor_id, sort)
512 <div className="mb-3">
513 <span className="me-3">
516 onChange={this.handleDataTypeChange}
519 <span className="me-2">
520 <SortSelect sort={sort} onChange={this.handleSortChange} />
524 <a href={communityRss} title="RSS" rel={relTags}>
525 <Icon icon="rss" classes="text-muted small" />
529 type="application/atom+xml"
538 handlePageChange(page: number) {
539 this.updateUrl({ page });
540 window.scrollTo(0, 0);
543 handleSortChange(sort: SortType) {
544 this.updateUrl({ sort, page: 1 });
545 window.scrollTo(0, 0);
548 handleDataTypeChange(dataType: DataType) {
549 this.updateUrl({ dataType, page: 1 });
550 window.scrollTo(0, 0);
553 handleShowSidebarMobile(i: Community) {
554 i.setState(({ showSidebarMobile }) => ({
555 showSidebarMobile: !showSidebarMobile,
559 async updateUrl({ dataType, page, sort }: Partial<CommunityProps>) {
561 dataType: urlDataType,
564 } = getCommunityQueryParams();
566 const queryParams: QueryParams<CommunityProps> = {
567 dataType: getDataTypeString(dataType ?? urlDataType),
568 page: (page ?? urlPage).toString(),
569 sort: sort ?? urlSort,
572 this.props.history.push(
573 `/c/${this.props.match.params.name}${getQueryString(queryParams)}`,
576 await this.fetchData();
580 const { dataType, page, sort } = getCommunityQueryParams();
581 const { name } = this.props.match.params;
583 if (dataType === DataType.Post) {
584 this.setState({ postsRes: { state: "loading" } });
586 postsRes: await HttpService.client.getPosts({
591 community_name: name,
597 this.setState({ commentsRes: { state: "loading" } });
599 commentsRes: await HttpService.client.getComments({
602 sort: postToCommentSortType(sort),
604 community_name: name,
614 async handleDeleteCommunity(form: DeleteCommunity) {
615 const deleteCommunityRes = await HttpService.client.deleteCommunity(form);
616 this.updateCommunity(deleteCommunityRes);
619 async handleAddModToCommunity(form: AddModToCommunity) {
620 const addModRes = await HttpService.client.addModToCommunity(form);
621 this.updateModerators(addModRes);
624 async handleFollow(form: FollowCommunity) {
625 const followCommunityRes = await HttpService.client.followCommunity(form);
626 this.updateCommunity(followCommunityRes);
629 if (followCommunityRes.state === "success") {
630 const communityId = followCommunityRes.data.community_view.community.id;
631 const mui = UserService.Instance.myUserInfo;
633 mui.follows = mui.follows.filter(i => i.community.id !== communityId);
638 async handlePurgeCommunity(form: PurgeCommunity) {
639 const purgeCommunityRes = await HttpService.client.purgeCommunity(form);
640 this.purgeItem(purgeCommunityRes);
643 async handlePurgePerson(form: PurgePerson) {
644 const purgePersonRes = await HttpService.client.purgePerson(form);
645 this.purgeItem(purgePersonRes);
648 async handlePurgeComment(form: PurgeComment) {
649 const purgeCommentRes = await HttpService.client.purgeComment(form);
650 this.purgeItem(purgeCommentRes);
653 async handlePurgePost(form: PurgePost) {
654 const purgeRes = await HttpService.client.purgePost(form);
655 this.purgeItem(purgeRes);
658 async handleBlockCommunity(form: BlockCommunity) {
659 const blockCommunityRes = await HttpService.client.blockCommunity(form);
660 if (blockCommunityRes.state === "success") {
661 updateCommunityBlock(blockCommunityRes.data);
663 if (s.communityRes.state === "success") {
664 s.communityRes.data.community_view.blocked =
665 blockCommunityRes.data.blocked;
671 async handleBlockPerson(form: BlockPerson) {
672 const blockPersonRes = await HttpService.client.blockPerson(form);
673 if (blockPersonRes.state === "success") {
674 updatePersonBlock(blockPersonRes.data);
678 async handleRemoveCommunity(form: RemoveCommunity) {
679 const removeCommunityRes = await HttpService.client.removeCommunity(form);
680 this.updateCommunity(removeCommunityRes);
683 async handleEditCommunity(form: EditCommunity) {
684 const res = await HttpService.client.editCommunity(form);
685 this.updateCommunity(res);
690 async handleCreateComment(form: CreateComment) {
691 const createCommentRes = await HttpService.client.createComment(form);
692 this.createAndUpdateComments(createCommentRes);
694 return createCommentRes;
697 async handleEditComment(form: EditComment) {
698 const editCommentRes = await HttpService.client.editComment(form);
699 this.findAndUpdateComment(editCommentRes);
701 return editCommentRes;
704 async handleDeleteComment(form: DeleteComment) {
705 const deleteCommentRes = await HttpService.client.deleteComment(form);
706 this.findAndUpdateComment(deleteCommentRes);
709 async handleDeletePost(form: DeletePost) {
710 const deleteRes = await HttpService.client.deletePost(form);
711 this.findAndUpdatePost(deleteRes);
714 async handleRemovePost(form: RemovePost) {
715 const removeRes = await HttpService.client.removePost(form);
716 this.findAndUpdatePost(removeRes);
719 async handleRemoveComment(form: RemoveComment) {
720 const removeCommentRes = await HttpService.client.removeComment(form);
721 this.findAndUpdateComment(removeCommentRes);
724 async handleSaveComment(form: SaveComment) {
725 const saveCommentRes = await HttpService.client.saveComment(form);
726 this.findAndUpdateComment(saveCommentRes);
729 async handleSavePost(form: SavePost) {
730 const saveRes = await HttpService.client.savePost(form);
731 this.findAndUpdatePost(saveRes);
734 async handleFeaturePost(form: FeaturePost) {
735 const featureRes = await HttpService.client.featurePost(form);
736 this.findAndUpdatePost(featureRes);
739 async handleCommentVote(form: CreateCommentLike) {
740 const voteRes = await HttpService.client.likeComment(form);
741 this.findAndUpdateComment(voteRes);
744 async handlePostEdit(form: EditPost) {
745 const res = await HttpService.client.editPost(form);
746 this.findAndUpdatePost(res);
749 async handlePostVote(form: CreatePostLike) {
750 const voteRes = await HttpService.client.likePost(form);
751 this.findAndUpdatePost(voteRes);
754 async handleCommentReport(form: CreateCommentReport) {
755 const reportRes = await HttpService.client.createCommentReport(form);
756 if (reportRes.state === "success") {
757 toast(I18NextService.i18n.t("report_created"));
761 async handlePostReport(form: CreatePostReport) {
762 const reportRes = await HttpService.client.createPostReport(form);
763 if (reportRes.state === "success") {
764 toast(I18NextService.i18n.t("report_created"));
768 async handleLockPost(form: LockPost) {
769 const lockRes = await HttpService.client.lockPost(form);
770 this.findAndUpdatePost(lockRes);
773 async handleDistinguishComment(form: DistinguishComment) {
774 const distinguishRes = await HttpService.client.distinguishComment(form);
775 this.findAndUpdateComment(distinguishRes);
778 async handleAddAdmin(form: AddAdmin) {
779 const addAdminRes = await HttpService.client.addAdmin(form);
781 if (addAdminRes.state === "success") {
782 this.setState(s => ((s.siteRes.admins = addAdminRes.data.admins), s));
786 async handleTransferCommunity(form: TransferCommunity) {
787 const transferCommunityRes = await HttpService.client.transferCommunity(
790 toast(I18NextService.i18n.t("transfer_community"));
791 this.updateCommunityFull(transferCommunityRes);
794 async handleCommentReplyRead(form: MarkCommentReplyAsRead) {
795 const readRes = await HttpService.client.markCommentReplyAsRead(form);
796 this.findAndUpdateCommentReply(readRes);
799 async handlePersonMentionRead(form: MarkPersonMentionAsRead) {
800 // TODO not sure what to do here. Maybe it is actually optional, because post doesn't need it.
801 await HttpService.client.markPersonMentionAsRead(form);
804 async handleBanFromCommunity(form: BanFromCommunity) {
805 const banRes = await HttpService.client.banFromCommunity(form);
806 this.updateBanFromCommunity(banRes);
809 async handleBanPerson(form: BanPerson) {
810 const banRes = await HttpService.client.banPerson(form);
811 this.updateBan(banRes);
814 updateBanFromCommunity(banRes: RequestState<BanFromCommunityResponse>) {
815 // Maybe not necessary
816 if (banRes.state === "success") {
818 if (s.postsRes.state === "success") {
819 s.postsRes.data.posts
820 .filter(c => c.creator.id === banRes.data.person_view.person.id)
822 c => (c.creator_banned_from_community = banRes.data.banned),
825 if (s.commentsRes.state === "success") {
826 s.commentsRes.data.comments
827 .filter(c => c.creator.id === banRes.data.person_view.person.id)
829 c => (c.creator_banned_from_community = banRes.data.banned),
837 updateBan(banRes: RequestState<BanPersonResponse>) {
838 // Maybe not necessary
839 if (banRes.state === "success") {
841 if (s.postsRes.state === "success") {
842 s.postsRes.data.posts
843 .filter(c => c.creator.id === banRes.data.person_view.person.id)
844 .forEach(c => (c.creator.banned = banRes.data.banned));
846 if (s.commentsRes.state === "success") {
847 s.commentsRes.data.comments
848 .filter(c => c.creator.id === banRes.data.person_view.person.id)
849 .forEach(c => (c.creator.banned = banRes.data.banned));
856 updateCommunity(res: RequestState<CommunityResponse>) {
858 if (s.communityRes.state === "success" && res.state === "success") {
859 s.communityRes.data.community_view = res.data.community_view;
860 s.communityRes.data.discussion_languages =
861 res.data.discussion_languages;
867 updateCommunityFull(res: RequestState<GetCommunityResponse>) {
869 if (s.communityRes.state === "success" && res.state === "success") {
870 s.communityRes.data.community_view = res.data.community_view;
871 s.communityRes.data.moderators = res.data.moderators;
877 purgeItem(purgeRes: RequestState<PurgeItemResponse>) {
878 if (purgeRes.state === "success") {
879 toast(I18NextService.i18n.t("purge_success"));
880 this.context.router.history.push(`/`);
884 findAndUpdateComment(res: RequestState<CommentResponse>) {
886 if (s.commentsRes.state === "success" && res.state === "success") {
887 s.commentsRes.data.comments = editComment(
888 res.data.comment_view,
889 s.commentsRes.data.comments,
891 s.finished.set(res.data.comment_view.comment.id, true);
897 createAndUpdateComments(res: RequestState<CommentResponse>) {
899 if (s.commentsRes.state === "success" && res.state === "success") {
900 s.commentsRes.data.comments.unshift(res.data.comment_view);
902 // Set finished for the parent
904 getCommentParentId(res.data.comment_view.comment) ?? 0,
912 findAndUpdateCommentReply(res: RequestState<CommentReplyResponse>) {
914 if (s.commentsRes.state === "success" && res.state === "success") {
915 s.commentsRes.data.comments = editWith(
916 res.data.comment_reply_view,
917 s.commentsRes.data.comments,
924 findAndUpdatePost(res: RequestState<PostResponse>) {
926 if (s.postsRes.state === "success" && res.state === "success") {
927 s.postsRes.data.posts = editPost(
929 s.postsRes.data.posts,
936 updateModerators(res: RequestState<AddModToCommunityResponse>) {
937 // Update the moderators
939 if (s.communityRes.state === "success" && res.state === "success") {
940 s.communityRes.data.moderators = res.data.moderators;