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 description={res.community_view.community.description}
316 image={res.community_view.community.icon}
319 <div className="row">
321 className="col-12 col-md-8 col-lg-9"
322 ref={this.mainContentRef}
324 {this.communityInfo(res)}
325 <div className="d-block d-md-none">
327 className="btn btn-secondary d-inline-block mb-2 me-3"
328 onClick={linkEvent(this, this.handleShowSidebarMobile)}
330 {I18NextService.i18n.t("sidebar")}{" "}
333 this.state.showSidebarMobile
337 classes="icon-inline"
340 {this.state.showSidebarMobile && this.sidebar(res)}
344 <Paginator page={page} onChange={this.handlePageChange} />
346 <aside className="d-none d-md-block col-md-4 col-lg-3">
358 <div className="community container-lg">{this.renderCommunity()}</div>
362 sidebar(res: GetCommunityResponse) {
363 const { site_res } = this.isoData;
364 // For some reason, this returns an empty vec if it matches the site langs
365 const communityLangs =
366 res.discussion_languages.length === 0
367 ? site_res.all_languages.map(({ id }) => id)
368 : res.discussion_languages;
373 community_view={res.community_view}
374 moderators={res.moderators}
375 admins={site_res.admins}
376 enableNsfw={enableNsfw(site_res)}
378 allLanguages={site_res.all_languages}
379 siteLanguages={site_res.discussion_languages}
380 communityLanguages={communityLangs}
381 onDeleteCommunity={this.handleDeleteCommunity}
382 onRemoveCommunity={this.handleRemoveCommunity}
383 onLeaveModTeam={this.handleAddModToCommunity}
384 onFollowCommunity={this.handleFollow}
385 onBlockCommunity={this.handleBlockCommunity}
386 onPurgeCommunity={this.handlePurgeCommunity}
387 onEditCommunity={this.handleEditCommunity}
389 {!res.community_view.community.local && res.site && (
390 <SiteSidebar site={res.site} showLocal={showLocal(this.isoData)} />
396 listings(communityRes: GetCommunityResponse) {
397 const { dataType } = getCommunityQueryParams();
398 const { site_res } = this.isoData;
400 if (dataType === DataType.Post) {
401 switch (this.state.postsRes.state) {
411 posts={this.state.postsRes.data.posts}
413 enableDownvotes={enableDownvotes(site_res)}
414 enableNsfw={enableNsfw(site_res)}
415 allLanguages={site_res.all_languages}
416 siteLanguages={site_res.discussion_languages}
417 onBlockPerson={this.handleBlockPerson}
418 onPostEdit={this.handlePostEdit}
419 onPostVote={this.handlePostVote}
420 onPostReport={this.handlePostReport}
421 onLockPost={this.handleLockPost}
422 onDeletePost={this.handleDeletePost}
423 onRemovePost={this.handleRemovePost}
424 onSavePost={this.handleSavePost}
425 onPurgePerson={this.handlePurgePerson}
426 onPurgePost={this.handlePurgePost}
427 onBanPerson={this.handleBanPerson}
428 onBanPersonFromCommunity={this.handleBanFromCommunity}
429 onAddModToCommunity={this.handleAddModToCommunity}
430 onAddAdmin={this.handleAddAdmin}
431 onTransferCommunity={this.handleTransferCommunity}
432 onFeaturePost={this.handleFeaturePost}
437 switch (this.state.commentsRes.state) {
447 nodes={commentsToFlatNodes(this.state.commentsRes.data.comments)}
448 viewType={CommentViewType.Flat}
449 finished={this.state.finished}
452 enableDownvotes={enableDownvotes(site_res)}
453 moderators={communityRes.moderators}
454 admins={site_res.admins}
455 allLanguages={site_res.all_languages}
456 siteLanguages={site_res.discussion_languages}
457 onSaveComment={this.handleSaveComment}
458 onBlockPerson={this.handleBlockPerson}
459 onDeleteComment={this.handleDeleteComment}
460 onRemoveComment={this.handleRemoveComment}
461 onCommentVote={this.handleCommentVote}
462 onCommentReport={this.handleCommentReport}
463 onDistinguishComment={this.handleDistinguishComment}
464 onAddModToCommunity={this.handleAddModToCommunity}
465 onAddAdmin={this.handleAddAdmin}
466 onTransferCommunity={this.handleTransferCommunity}
467 onPurgeComment={this.handlePurgeComment}
468 onPurgePerson={this.handlePurgePerson}
469 onCommentReplyRead={this.handleCommentReplyRead}
470 onPersonMentionRead={this.handlePersonMentionRead}
471 onBanPersonFromCommunity={this.handleBanFromCommunity}
472 onBanPerson={this.handleBanPerson}
473 onCreateComment={this.handleCreateComment}
474 onEditComment={this.handleEditComment}
481 communityInfo(res: GetCommunityResponse) {
482 const community = res.community_view.community;
486 <div className="mb-2">
487 <BannerIconHeader banner={community.banner} icon={community.icon} />
488 <h1 className="h4 mb-0 overflow-wrap-anywhere">{community.title}</h1>
490 community={community}
501 selects(res: GetCommunityResponse) {
502 // let communityRss = this.state.communityRes.map(r =>
503 // communityRSSUrl(r.community_view.community.actor_id, this.state.sort)
505 const { dataType, sort } = getCommunityQueryParams();
506 const communityRss = res
507 ? communityRSSUrl(res.community_view.community.actor_id, sort)
511 <div className="mb-3">
512 <span className="me-3">
515 onChange={this.handleDataTypeChange}
518 <span className="me-2">
519 <SortSelect sort={sort} onChange={this.handleSortChange} />
523 <a href={communityRss} title="RSS" rel={relTags}>
524 <Icon icon="rss" classes="text-muted small" />
528 type="application/atom+xml"
537 handlePageChange(page: number) {
538 this.updateUrl({ page });
539 window.scrollTo(0, 0);
542 handleSortChange(sort: SortType) {
543 this.updateUrl({ sort, page: 1 });
544 window.scrollTo(0, 0);
547 handleDataTypeChange(dataType: DataType) {
548 this.updateUrl({ dataType, page: 1 });
549 window.scrollTo(0, 0);
552 handleShowSidebarMobile(i: Community) {
553 i.setState(({ showSidebarMobile }) => ({
554 showSidebarMobile: !showSidebarMobile,
558 async updateUrl({ dataType, page, sort }: Partial<CommunityProps>) {
560 dataType: urlDataType,
563 } = getCommunityQueryParams();
565 const queryParams: QueryParams<CommunityProps> = {
566 dataType: getDataTypeString(dataType ?? urlDataType),
567 page: (page ?? urlPage).toString(),
568 sort: sort ?? urlSort,
571 this.props.history.push(
572 `/c/${this.props.match.params.name}${getQueryString(queryParams)}`
575 await this.fetchData();
579 const { dataType, page, sort } = getCommunityQueryParams();
580 const { name } = this.props.match.params;
582 if (dataType === DataType.Post) {
583 this.setState({ postsRes: { state: "loading" } });
585 postsRes: await HttpService.client.getPosts({
590 community_name: name,
596 this.setState({ commentsRes: { state: "loading" } });
598 commentsRes: await HttpService.client.getComments({
601 sort: postToCommentSortType(sort),
603 community_name: name,
613 async handleDeleteCommunity(form: DeleteCommunity) {
614 const deleteCommunityRes = await HttpService.client.deleteCommunity(form);
615 this.updateCommunity(deleteCommunityRes);
618 async handleAddModToCommunity(form: AddModToCommunity) {
619 const addModRes = await HttpService.client.addModToCommunity(form);
620 this.updateModerators(addModRes);
623 async handleFollow(form: FollowCommunity) {
624 const followCommunityRes = await HttpService.client.followCommunity(form);
625 this.updateCommunity(followCommunityRes);
628 if (followCommunityRes.state == "success") {
629 const communityId = followCommunityRes.data.community_view.community.id;
630 const mui = UserService.Instance.myUserInfo;
632 mui.follows = mui.follows.filter(i => i.community.id != communityId);
637 async handlePurgeCommunity(form: PurgeCommunity) {
638 const purgeCommunityRes = await HttpService.client.purgeCommunity(form);
639 this.purgeItem(purgeCommunityRes);
642 async handlePurgePerson(form: PurgePerson) {
643 const purgePersonRes = await HttpService.client.purgePerson(form);
644 this.purgeItem(purgePersonRes);
647 async handlePurgeComment(form: PurgeComment) {
648 const purgeCommentRes = await HttpService.client.purgeComment(form);
649 this.purgeItem(purgeCommentRes);
652 async handlePurgePost(form: PurgePost) {
653 const purgeRes = await HttpService.client.purgePost(form);
654 this.purgeItem(purgeRes);
657 async handleBlockCommunity(form: BlockCommunity) {
658 const blockCommunityRes = await HttpService.client.blockCommunity(form);
659 if (blockCommunityRes.state == "success") {
660 updateCommunityBlock(blockCommunityRes.data);
662 if (s.communityRes.state == "success") {
663 s.communityRes.data.community_view.blocked =
664 blockCommunityRes.data.blocked;
670 async handleBlockPerson(form: BlockPerson) {
671 const blockPersonRes = await HttpService.client.blockPerson(form);
672 if (blockPersonRes.state == "success") {
673 updatePersonBlock(blockPersonRes.data);
677 async handleRemoveCommunity(form: RemoveCommunity) {
678 const removeCommunityRes = await HttpService.client.removeCommunity(form);
679 this.updateCommunity(removeCommunityRes);
682 async handleEditCommunity(form: EditCommunity) {
683 const res = await HttpService.client.editCommunity(form);
684 this.updateCommunity(res);
689 async handleCreateComment(form: CreateComment) {
690 const createCommentRes = await HttpService.client.createComment(form);
691 this.createAndUpdateComments(createCommentRes);
693 return createCommentRes;
696 async handleEditComment(form: EditComment) {
697 const editCommentRes = await HttpService.client.editComment(form);
698 this.findAndUpdateComment(editCommentRes);
700 return editCommentRes;
703 async handleDeleteComment(form: DeleteComment) {
704 const deleteCommentRes = await HttpService.client.deleteComment(form);
705 this.findAndUpdateComment(deleteCommentRes);
708 async handleDeletePost(form: DeletePost) {
709 const deleteRes = await HttpService.client.deletePost(form);
710 this.findAndUpdatePost(deleteRes);
713 async handleRemovePost(form: RemovePost) {
714 const removeRes = await HttpService.client.removePost(form);
715 this.findAndUpdatePost(removeRes);
718 async handleRemoveComment(form: RemoveComment) {
719 const removeCommentRes = await HttpService.client.removeComment(form);
720 this.findAndUpdateComment(removeCommentRes);
723 async handleSaveComment(form: SaveComment) {
724 const saveCommentRes = await HttpService.client.saveComment(form);
725 this.findAndUpdateComment(saveCommentRes);
728 async handleSavePost(form: SavePost) {
729 const saveRes = await HttpService.client.savePost(form);
730 this.findAndUpdatePost(saveRes);
733 async handleFeaturePost(form: FeaturePost) {
734 const featureRes = await HttpService.client.featurePost(form);
735 this.findAndUpdatePost(featureRes);
738 async handleCommentVote(form: CreateCommentLike) {
739 const voteRes = await HttpService.client.likeComment(form);
740 this.findAndUpdateComment(voteRes);
743 async handlePostEdit(form: EditPost) {
744 const res = await HttpService.client.editPost(form);
745 this.findAndUpdatePost(res);
748 async handlePostVote(form: CreatePostLike) {
749 const voteRes = await HttpService.client.likePost(form);
750 this.findAndUpdatePost(voteRes);
753 async handleCommentReport(form: CreateCommentReport) {
754 const reportRes = await HttpService.client.createCommentReport(form);
755 if (reportRes.state == "success") {
756 toast(I18NextService.i18n.t("report_created"));
760 async handlePostReport(form: CreatePostReport) {
761 const reportRes = await HttpService.client.createPostReport(form);
762 if (reportRes.state == "success") {
763 toast(I18NextService.i18n.t("report_created"));
767 async handleLockPost(form: LockPost) {
768 const lockRes = await HttpService.client.lockPost(form);
769 this.findAndUpdatePost(lockRes);
772 async handleDistinguishComment(form: DistinguishComment) {
773 const distinguishRes = await HttpService.client.distinguishComment(form);
774 this.findAndUpdateComment(distinguishRes);
777 async handleAddAdmin(form: AddAdmin) {
778 const addAdminRes = await HttpService.client.addAdmin(form);
780 if (addAdminRes.state == "success") {
781 this.setState(s => ((s.siteRes.admins = addAdminRes.data.admins), s));
785 async handleTransferCommunity(form: TransferCommunity) {
786 const transferCommunityRes = await HttpService.client.transferCommunity(
789 toast(I18NextService.i18n.t("transfer_community"));
790 this.updateCommunityFull(transferCommunityRes);
793 async handleCommentReplyRead(form: MarkCommentReplyAsRead) {
794 const readRes = await HttpService.client.markCommentReplyAsRead(form);
795 this.findAndUpdateCommentReply(readRes);
798 async handlePersonMentionRead(form: MarkPersonMentionAsRead) {
799 // TODO not sure what to do here. Maybe it is actually optional, because post doesn't need it.
800 await HttpService.client.markPersonMentionAsRead(form);
803 async handleBanFromCommunity(form: BanFromCommunity) {
804 const banRes = await HttpService.client.banFromCommunity(form);
805 this.updateBanFromCommunity(banRes);
808 async handleBanPerson(form: BanPerson) {
809 const banRes = await HttpService.client.banPerson(form);
810 this.updateBan(banRes);
813 updateBanFromCommunity(banRes: RequestState<BanFromCommunityResponse>) {
814 // Maybe not necessary
815 if (banRes.state == "success") {
817 if (s.postsRes.state == "success") {
818 s.postsRes.data.posts
819 .filter(c => c.creator.id == banRes.data.person_view.person.id)
821 c => (c.creator_banned_from_community = banRes.data.banned)
824 if (s.commentsRes.state == "success") {
825 s.commentsRes.data.comments
826 .filter(c => c.creator.id == banRes.data.person_view.person.id)
828 c => (c.creator_banned_from_community = banRes.data.banned)
836 updateBan(banRes: RequestState<BanPersonResponse>) {
837 // Maybe not necessary
838 if (banRes.state == "success") {
840 if (s.postsRes.state == "success") {
841 s.postsRes.data.posts
842 .filter(c => c.creator.id == banRes.data.person_view.person.id)
843 .forEach(c => (c.creator.banned = banRes.data.banned));
845 if (s.commentsRes.state == "success") {
846 s.commentsRes.data.comments
847 .filter(c => c.creator.id == banRes.data.person_view.person.id)
848 .forEach(c => (c.creator.banned = banRes.data.banned));
855 updateCommunity(res: RequestState<CommunityResponse>) {
857 if (s.communityRes.state == "success" && res.state == "success") {
858 s.communityRes.data.community_view = res.data.community_view;
859 s.communityRes.data.discussion_languages =
860 res.data.discussion_languages;
866 updateCommunityFull(res: RequestState<GetCommunityResponse>) {
868 if (s.communityRes.state == "success" && res.state == "success") {
869 s.communityRes.data.community_view = res.data.community_view;
870 s.communityRes.data.moderators = res.data.moderators;
876 purgeItem(purgeRes: RequestState<PurgeItemResponse>) {
877 if (purgeRes.state == "success") {
878 toast(I18NextService.i18n.t("purge_success"));
879 this.context.router.history.push(`/`);
883 findAndUpdateComment(res: RequestState<CommentResponse>) {
885 if (s.commentsRes.state == "success" && res.state == "success") {
886 s.commentsRes.data.comments = editComment(
887 res.data.comment_view,
888 s.commentsRes.data.comments
890 s.finished.set(res.data.comment_view.comment.id, true);
896 createAndUpdateComments(res: RequestState<CommentResponse>) {
898 if (s.commentsRes.state == "success" && res.state == "success") {
899 s.commentsRes.data.comments.unshift(res.data.comment_view);
901 // Set finished for the parent
903 getCommentParentId(res.data.comment_view.comment) ?? 0,
911 findAndUpdateCommentReply(res: RequestState<CommentReplyResponse>) {
913 if (s.commentsRes.state == "success" && res.state == "success") {
914 s.commentsRes.data.comments = editWith(
915 res.data.comment_reply_view,
916 s.commentsRes.data.comments
923 findAndUpdatePost(res: RequestState<PostResponse>) {
925 if (s.postsRes.state == "success" && res.state == "success") {
926 s.postsRes.data.posts = editPost(
928 s.postsRes.data.posts
935 updateModerators(res: RequestState<AddModToCommunityResponse>) {
936 // Update the moderators
938 if (s.communityRes.state == "success" && res.state == "success") {
939 s.communityRes.data.moderators = res.data.moderators;