12 postToCommentSortType,
18 import { isBrowser } from "@utils/browser";
23 } from "@utils/helpers";
24 import type { QueryParams } from "@utils/types";
25 import { RouteDataResponse } from "@utils/types";
26 import { Component, linkEvent } from "inferno";
27 import { RouteComponentProps } from "inferno-router/dist/Route";
31 AddModToCommunityResponse,
33 BanFromCommunityResponse,
64 MarkCommentReplyAsRead,
65 MarkPersonMentionAsRead,
79 } from "lemmy-js-client";
80 import { fetchLimit, relTags } from "../../config";
85 } from "../../interfaces";
86 import { FirstLoadService, I18NextService, UserService } from "../../services";
87 import { HttpService, RequestState } from "../../services/HttpService";
88 import { setupTippy } from "../../tippy";
89 import { toast } from "../../toast";
90 import { CommentNodes } from "../comment/comment-nodes";
91 import { BannerIconHeader } from "../common/banner-icon-header";
92 import { DataTypeSelect } from "../common/data-type-select";
93 import { HtmlTags } from "../common/html-tags";
94 import { Icon, Spinner } from "../common/icon";
95 import { Paginator } from "../common/paginator";
96 import { SortSelect } from "../common/sort-select";
97 import { Sidebar } from "../community/sidebar";
98 import { SiteSidebar } from "../home/site-sidebar";
99 import { PostListings } from "../post/post-listings";
100 import { CommunityLink } from "./community-link";
102 type CommunityData = RouteDataResponse<{
103 communityRes: GetCommunityResponse;
104 postsRes: GetPostsResponse;
105 commentsRes: GetCommentsResponse;
109 communityRes: RequestState<GetCommunityResponse>;
110 postsRes: RequestState<GetPostsResponse>;
111 commentsRes: RequestState<GetCommentsResponse>;
112 siteRes: GetSiteResponse;
113 showSidebarMobile: boolean;
114 finished: Map<CommentId, boolean | undefined>;
115 isIsomorphic: boolean;
118 interface CommunityProps {
124 function getCommunityQueryParams() {
125 return getQueryParams<CommunityProps>({
126 dataType: getDataTypeFromQuery,
127 page: getPageFromString,
128 sort: getSortTypeFromQuery,
132 function getDataTypeFromQuery(type?: string): DataType {
133 return type ? DataType[type] : DataType.Post;
136 function getSortTypeFromQuery(type?: string): SortType {
138 UserService.Instance.myUserInfo?.local_user_view.local_user
141 return type ? (type as SortType) : mySortType ?? "Active";
144 export class Community extends Component<
145 RouteComponentProps<{ name: string }>,
148 private isoData = setIsoData<CommunityData>(this.context);
150 communityRes: { state: "empty" },
151 postsRes: { state: "empty" },
152 commentsRes: { state: "empty" },
153 siteRes: this.isoData.site_res,
154 showSidebarMobile: false,
159 constructor(props: RouteComponentProps<{ name: string }>, context: any) {
160 super(props, context);
162 this.handleSortChange = this.handleSortChange.bind(this);
163 this.handleDataTypeChange = this.handleDataTypeChange.bind(this);
164 this.handlePageChange = this.handlePageChange.bind(this);
166 // All of the action binds
167 this.handleDeleteCommunity = this.handleDeleteCommunity.bind(this);
168 this.handleEditCommunity = this.handleEditCommunity.bind(this);
169 this.handleFollow = this.handleFollow.bind(this);
170 this.handleRemoveCommunity = this.handleRemoveCommunity.bind(this);
171 this.handleCreateComment = this.handleCreateComment.bind(this);
172 this.handleEditComment = this.handleEditComment.bind(this);
173 this.handleSaveComment = this.handleSaveComment.bind(this);
174 this.handleBlockCommunity = this.handleBlockCommunity.bind(this);
175 this.handleBlockPerson = this.handleBlockPerson.bind(this);
176 this.handleDeleteComment = this.handleDeleteComment.bind(this);
177 this.handleRemoveComment = this.handleRemoveComment.bind(this);
178 this.handleCommentVote = this.handleCommentVote.bind(this);
179 this.handleAddModToCommunity = this.handleAddModToCommunity.bind(this);
180 this.handleAddAdmin = this.handleAddAdmin.bind(this);
181 this.handlePurgePerson = this.handlePurgePerson.bind(this);
182 this.handlePurgeComment = this.handlePurgeComment.bind(this);
183 this.handleCommentReport = this.handleCommentReport.bind(this);
184 this.handleDistinguishComment = this.handleDistinguishComment.bind(this);
185 this.handleTransferCommunity = this.handleTransferCommunity.bind(this);
186 this.handleCommentReplyRead = this.handleCommentReplyRead.bind(this);
187 this.handlePersonMentionRead = this.handlePersonMentionRead.bind(this);
188 this.handleBanFromCommunity = this.handleBanFromCommunity.bind(this);
189 this.handleBanPerson = this.handleBanPerson.bind(this);
190 this.handlePostVote = this.handlePostVote.bind(this);
191 this.handlePostEdit = this.handlePostEdit.bind(this);
192 this.handlePostReport = this.handlePostReport.bind(this);
193 this.handleLockPost = this.handleLockPost.bind(this);
194 this.handleDeletePost = this.handleDeletePost.bind(this);
195 this.handleRemovePost = this.handleRemovePost.bind(this);
196 this.handleSavePost = this.handleSavePost.bind(this);
197 this.handlePurgePost = this.handlePurgePost.bind(this);
198 this.handleFeaturePost = this.handleFeaturePost.bind(this);
200 // Only fetch the data if coming from another route
201 if (!isBrowser() || FirstLoadService.isFirstLoad) {
202 const { communityRes, commentsRes, postsRes } = this.isoData.routeData;
214 async fetchCommunity() {
215 this.setState({ communityRes: { state: "loading" } });
217 communityRes: await HttpService.client.getCommunity({
218 name: this.props.match.params.name,
224 async componentDidMount() {
225 if (!this.state.isIsomorphic) {
226 await Promise.all([this.fetchCommunity(), this.fetchData()]);
232 static async fetchInitialData({
235 query: { dataType: urlDataType, page: urlPage, sort: urlSort },
237 }: InitialFetchRequest<QueryParams<CommunityProps>>): Promise<
238 Promise<CommunityData>
240 const pathSplit = path.split("/");
242 const communityName = pathSplit[2];
243 const communityForm: GetCommunity = {
248 const dataType = getDataTypeFromQuery(urlDataType);
250 const sort = getSortTypeFromQuery(urlSort);
252 const page = getPageFromString(urlPage);
254 let postsResponse: RequestState<GetPostsResponse> = { state: "empty" };
255 let commentsResponse: RequestState<GetCommentsResponse> = {
259 if (dataType === DataType.Post) {
260 const getPostsForm: GetPosts = {
261 community_name: communityName,
270 postsResponse = await client.getPosts(getPostsForm);
272 const getCommentsForm: GetComments = {
273 community_name: communityName,
276 sort: postToCommentSortType(sort),
282 commentsResponse = await client.getComments(getCommentsForm);
286 communityRes: await client.getCommunity(communityForm),
287 commentsRes: commentsResponse,
288 postsRes: postsResponse,
292 get documentTitle(): string {
293 const cRes = this.state.communityRes;
294 return cRes.state == "success"
295 ? `${cRes.data.community_view.community.title} - ${this.isoData.site_res.site_view.site.name}`
300 switch (this.state.communityRes.state) {
308 const res = this.state.communityRes.data;
309 const { page } = getCommunityQueryParams();
314 title={this.documentTitle}
315 path={this.context.router.route.match.url}
316 description={res.community_view.community.description}
317 image={res.community_view.community.icon}
320 <div className="row">
321 <div className="col-12 col-md-8">
322 {this.communityInfo(res)}
323 <div className="d-block d-md-none">
325 className="btn btn-secondary d-inline-block mb-2 me-3"
326 onClick={linkEvent(this, this.handleShowSidebarMobile)}
328 {I18NextService.i18n.t("sidebar")}{" "}
331 this.state.showSidebarMobile
335 classes="icon-inline"
338 {this.state.showSidebarMobile && this.sidebar(res)}
342 <Paginator page={page} onChange={this.handlePageChange} />
344 <div className="d-none d-md-block col-md-4">
356 <div className="community container-lg">{this.renderCommunity()}</div>
360 sidebar(res: GetCommunityResponse) {
361 const { site_res } = this.isoData;
362 // For some reason, this returns an empty vec if it matches the site langs
363 const communityLangs =
364 res.discussion_languages.length === 0
365 ? site_res.all_languages.map(({ id }) => id)
366 : res.discussion_languages;
371 community_view={res.community_view}
372 moderators={res.moderators}
373 admins={site_res.admins}
374 enableNsfw={enableNsfw(site_res)}
376 allLanguages={site_res.all_languages}
377 siteLanguages={site_res.discussion_languages}
378 communityLanguages={communityLangs}
379 onDeleteCommunity={this.handleDeleteCommunity}
380 onRemoveCommunity={this.handleRemoveCommunity}
381 onLeaveModTeam={this.handleAddModToCommunity}
382 onFollowCommunity={this.handleFollow}
383 onBlockCommunity={this.handleBlockCommunity}
384 onPurgeCommunity={this.handlePurgeCommunity}
385 onEditCommunity={this.handleEditCommunity}
387 {!res.community_view.community.local && res.site && (
388 <SiteSidebar site={res.site} showLocal={showLocal(this.isoData)} />
394 listings(communityRes: GetCommunityResponse) {
395 const { dataType } = getCommunityQueryParams();
396 const { site_res } = this.isoData;
398 if (dataType === DataType.Post) {
399 switch (this.state.postsRes.state) {
409 posts={this.state.postsRes.data.posts}
411 enableDownvotes={enableDownvotes(site_res)}
412 enableNsfw={enableNsfw(site_res)}
413 allLanguages={site_res.all_languages}
414 siteLanguages={site_res.discussion_languages}
415 onBlockPerson={this.handleBlockPerson}
416 onPostEdit={this.handlePostEdit}
417 onPostVote={this.handlePostVote}
418 onPostReport={this.handlePostReport}
419 onLockPost={this.handleLockPost}
420 onDeletePost={this.handleDeletePost}
421 onRemovePost={this.handleRemovePost}
422 onSavePost={this.handleSavePost}
423 onPurgePerson={this.handlePurgePerson}
424 onPurgePost={this.handlePurgePost}
425 onBanPerson={this.handleBanPerson}
426 onBanPersonFromCommunity={this.handleBanFromCommunity}
427 onAddModToCommunity={this.handleAddModToCommunity}
428 onAddAdmin={this.handleAddAdmin}
429 onTransferCommunity={this.handleTransferCommunity}
430 onFeaturePost={this.handleFeaturePost}
435 switch (this.state.commentsRes.state) {
445 nodes={commentsToFlatNodes(this.state.commentsRes.data.comments)}
446 viewType={CommentViewType.Flat}
447 finished={this.state.finished}
450 enableDownvotes={enableDownvotes(site_res)}
451 moderators={communityRes.moderators}
452 admins={site_res.admins}
453 allLanguages={site_res.all_languages}
454 siteLanguages={site_res.discussion_languages}
455 onSaveComment={this.handleSaveComment}
456 onBlockPerson={this.handleBlockPerson}
457 onDeleteComment={this.handleDeleteComment}
458 onRemoveComment={this.handleRemoveComment}
459 onCommentVote={this.handleCommentVote}
460 onCommentReport={this.handleCommentReport}
461 onDistinguishComment={this.handleDistinguishComment}
462 onAddModToCommunity={this.handleAddModToCommunity}
463 onAddAdmin={this.handleAddAdmin}
464 onTransferCommunity={this.handleTransferCommunity}
465 onPurgeComment={this.handlePurgeComment}
466 onPurgePerson={this.handlePurgePerson}
467 onCommentReplyRead={this.handleCommentReplyRead}
468 onPersonMentionRead={this.handlePersonMentionRead}
469 onBanPersonFromCommunity={this.handleBanFromCommunity}
470 onBanPerson={this.handleBanPerson}
471 onCreateComment={this.handleCreateComment}
472 onEditComment={this.handleEditComment}
479 communityInfo(res: GetCommunityResponse) {
480 const community = res.community_view.community;
484 <div className="mb-2">
485 <BannerIconHeader banner={community.banner} icon={community.icon} />
486 <h5 className="mb-0 overflow-wrap-anywhere">{community.title}</h5>
488 community={community}
499 selects(res: GetCommunityResponse) {
500 // let communityRss = this.state.communityRes.map(r =>
501 // communityRSSUrl(r.community_view.community.actor_id, this.state.sort)
503 const { dataType, sort } = getCommunityQueryParams();
504 const communityRss = res
505 ? communityRSSUrl(res.community_view.community.actor_id, sort)
509 <div className="mb-3">
510 <span className="me-3">
513 onChange={this.handleDataTypeChange}
516 <span className="me-2">
517 <SortSelect sort={sort} onChange={this.handleSortChange} />
521 <a href={communityRss} title="RSS" rel={relTags}>
522 <Icon icon="rss" classes="text-muted small" />
526 type="application/atom+xml"
535 handlePageChange(page: number) {
536 this.updateUrl({ page });
537 window.scrollTo(0, 0);
540 handleSortChange(sort: SortType) {
541 this.updateUrl({ sort, page: 1 });
542 window.scrollTo(0, 0);
545 handleDataTypeChange(dataType: DataType) {
546 this.updateUrl({ dataType, page: 1 });
547 window.scrollTo(0, 0);
550 handleShowSidebarMobile(i: Community) {
551 i.setState(({ showSidebarMobile }) => ({
552 showSidebarMobile: !showSidebarMobile,
556 async updateUrl({ dataType, page, sort }: Partial<CommunityProps>) {
558 dataType: urlDataType,
561 } = getCommunityQueryParams();
563 const queryParams: QueryParams<CommunityProps> = {
564 dataType: getDataTypeString(dataType ?? urlDataType),
565 page: (page ?? urlPage).toString(),
566 sort: sort ?? urlSort,
569 this.props.history.push(
570 `/c/${this.props.match.params.name}${getQueryString(queryParams)}`
573 await this.fetchData();
577 const { dataType, page, sort } = getCommunityQueryParams();
578 const { name } = this.props.match.params;
580 if (dataType === DataType.Post) {
581 this.setState({ postsRes: { state: "loading" } });
583 postsRes: await HttpService.client.getPosts({
588 community_name: name,
594 this.setState({ commentsRes: { state: "loading" } });
596 commentsRes: await HttpService.client.getComments({
599 sort: postToCommentSortType(sort),
601 community_name: name,
611 async handleDeleteCommunity(form: DeleteCommunity) {
612 const deleteCommunityRes = await HttpService.client.deleteCommunity(form);
613 this.updateCommunity(deleteCommunityRes);
616 async handleAddModToCommunity(form: AddModToCommunity) {
617 const addModRes = await HttpService.client.addModToCommunity(form);
618 this.updateModerators(addModRes);
621 async handleFollow(form: FollowCommunity) {
622 const followCommunityRes = await HttpService.client.followCommunity(form);
623 this.updateCommunity(followCommunityRes);
626 if (followCommunityRes.state == "success") {
627 const communityId = followCommunityRes.data.community_view.community.id;
628 const mui = UserService.Instance.myUserInfo;
630 mui.follows = mui.follows.filter(i => i.community.id != communityId);
635 async handlePurgeCommunity(form: PurgeCommunity) {
636 const purgeCommunityRes = await HttpService.client.purgeCommunity(form);
637 this.purgeItem(purgeCommunityRes);
640 async handlePurgePerson(form: PurgePerson) {
641 const purgePersonRes = await HttpService.client.purgePerson(form);
642 this.purgeItem(purgePersonRes);
645 async handlePurgeComment(form: PurgeComment) {
646 const purgeCommentRes = await HttpService.client.purgeComment(form);
647 this.purgeItem(purgeCommentRes);
650 async handlePurgePost(form: PurgePost) {
651 const purgeRes = await HttpService.client.purgePost(form);
652 this.purgeItem(purgeRes);
655 async handleBlockCommunity(form: BlockCommunity) {
656 const blockCommunityRes = await HttpService.client.blockCommunity(form);
657 if (blockCommunityRes.state == "success") {
658 updateCommunityBlock(blockCommunityRes.data);
660 if (s.communityRes.state == "success") {
661 s.communityRes.data.community_view.blocked =
662 blockCommunityRes.data.blocked;
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(I18NextService.i18n.t("report_created"));
758 async handlePostReport(form: CreatePostReport) {
759 const reportRes = await HttpService.client.createPostReport(form);
760 if (reportRes.state == "success") {
761 toast(I18NextService.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(I18NextService.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(I18NextService.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;