12 postToCommentSortType,
18 import { restoreScrollPosition, saveScrollPosition } 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";
81 import { i18n } from "../../i18next";
86 } from "../../interfaces";
87 import { UserService } from "../../services";
88 import { FirstLoadService } from "../../services/FirstLoadService";
89 import { HttpService, RequestState } from "../../services/HttpService";
90 import { setupTippy } from "../../tippy";
91 import { toast } from "../../toast";
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 me-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">
362 <div className="community container-lg">{this.renderCommunity()}</div>
366 sidebar(res: GetCommunityResponse) {
367 const { site_res } = this.isoData;
368 // For some reason, this returns an empty vec if it matches the site langs
369 const communityLangs =
370 res.discussion_languages.length === 0
371 ? site_res.all_languages.map(({ id }) => id)
372 : res.discussion_languages;
377 community_view={res.community_view}
378 moderators={res.moderators}
379 admins={site_res.admins}
380 enableNsfw={enableNsfw(site_res)}
382 allLanguages={site_res.all_languages}
383 siteLanguages={site_res.discussion_languages}
384 communityLanguages={communityLangs}
385 onDeleteCommunity={this.handleDeleteCommunity}
386 onRemoveCommunity={this.handleRemoveCommunity}
387 onLeaveModTeam={this.handleAddModToCommunity}
388 onFollowCommunity={this.handleFollow}
389 onBlockCommunity={this.handleBlockCommunity}
390 onPurgeCommunity={this.handlePurgeCommunity}
391 onEditCommunity={this.handleEditCommunity}
393 {!res.community_view.community.local && res.site && (
394 <SiteSidebar site={res.site} showLocal={showLocal(this.isoData)} />
400 listings(communityRes: GetCommunityResponse) {
401 const { dataType } = getCommunityQueryParams();
402 const { site_res } = this.isoData;
404 if (dataType === DataType.Post) {
405 switch (this.state.postsRes.state) {
415 posts={this.state.postsRes.data.posts}
417 enableDownvotes={enableDownvotes(site_res)}
418 enableNsfw={enableNsfw(site_res)}
419 allLanguages={site_res.all_languages}
420 siteLanguages={site_res.discussion_languages}
421 onBlockPerson={this.handleBlockPerson}
422 onPostEdit={this.handlePostEdit}
423 onPostVote={this.handlePostVote}
424 onPostReport={this.handlePostReport}
425 onLockPost={this.handleLockPost}
426 onDeletePost={this.handleDeletePost}
427 onRemovePost={this.handleRemovePost}
428 onSavePost={this.handleSavePost}
429 onPurgePerson={this.handlePurgePerson}
430 onPurgePost={this.handlePurgePost}
431 onBanPerson={this.handleBanPerson}
432 onBanPersonFromCommunity={this.handleBanFromCommunity}
433 onAddModToCommunity={this.handleAddModToCommunity}
434 onAddAdmin={this.handleAddAdmin}
435 onTransferCommunity={this.handleTransferCommunity}
436 onFeaturePost={this.handleFeaturePost}
441 switch (this.state.commentsRes.state) {
451 nodes={commentsToFlatNodes(this.state.commentsRes.data.comments)}
452 viewType={CommentViewType.Flat}
453 finished={this.state.finished}
456 enableDownvotes={enableDownvotes(site_res)}
457 moderators={communityRes.moderators}
458 admins={site_res.admins}
459 allLanguages={site_res.all_languages}
460 siteLanguages={site_res.discussion_languages}
461 onSaveComment={this.handleSaveComment}
462 onBlockPerson={this.handleBlockPerson}
463 onDeleteComment={this.handleDeleteComment}
464 onRemoveComment={this.handleRemoveComment}
465 onCommentVote={this.handleCommentVote}
466 onCommentReport={this.handleCommentReport}
467 onDistinguishComment={this.handleDistinguishComment}
468 onAddModToCommunity={this.handleAddModToCommunity}
469 onAddAdmin={this.handleAddAdmin}
470 onTransferCommunity={this.handleTransferCommunity}
471 onPurgeComment={this.handlePurgeComment}
472 onPurgePerson={this.handlePurgePerson}
473 onCommentReplyRead={this.handleCommentReplyRead}
474 onPersonMentionRead={this.handlePersonMentionRead}
475 onBanPersonFromCommunity={this.handleBanFromCommunity}
476 onBanPerson={this.handleBanPerson}
477 onCreateComment={this.handleCreateComment}
478 onEditComment={this.handleEditComment}
485 communityInfo(res: GetCommunityResponse) {
486 const community = res.community_view.community;
490 <div className="mb-2">
491 <BannerIconHeader banner={community.banner} icon={community.icon} />
492 <h5 className="mb-0 overflow-wrap-anywhere">{community.title}</h5>
494 community={community}
505 selects(res: GetCommunityResponse) {
506 // let communityRss = this.state.communityRes.map(r =>
507 // communityRSSUrl(r.community_view.community.actor_id, this.state.sort)
509 const { dataType, sort } = getCommunityQueryParams();
510 const communityRss = res
511 ? communityRSSUrl(res.community_view.community.actor_id, sort)
515 <div className="mb-3">
516 <span className="me-3">
519 onChange={this.handleDataTypeChange}
522 <span className="me-2">
523 <SortSelect sort={sort} onChange={this.handleSortChange} />
527 <a href={communityRss} title="RSS" rel={relTags}>
528 <Icon icon="rss" classes="text-muted small" />
532 type="application/atom+xml"
541 handlePageChange(page: number) {
542 this.updateUrl({ page });
543 window.scrollTo(0, 0);
546 handleSortChange(sort: SortType) {
547 this.updateUrl({ sort, page: 1 });
548 window.scrollTo(0, 0);
551 handleDataTypeChange(dataType: DataType) {
552 this.updateUrl({ dataType, page: 1 });
553 window.scrollTo(0, 0);
556 handleShowSidebarMobile(i: Community) {
557 i.setState(({ showSidebarMobile }) => ({
558 showSidebarMobile: !showSidebarMobile,
562 async updateUrl({ dataType, page, sort }: Partial<CommunityProps>) {
564 dataType: urlDataType,
567 } = getCommunityQueryParams();
569 const queryParams: QueryParams<CommunityProps> = {
570 dataType: getDataTypeString(dataType ?? urlDataType),
571 page: (page ?? urlPage).toString(),
572 sort: sort ?? urlSort,
575 this.props.history.push(
576 `/c/${this.props.match.params.name}${getQueryString(queryParams)}`
579 await this.fetchData();
583 const { dataType, page, sort } = getCommunityQueryParams();
584 const { name } = this.props.match.params;
586 if (dataType === DataType.Post) {
587 this.setState({ postsRes: { state: "loading" } });
589 postsRes: await HttpService.client.getPosts({
594 community_name: name,
600 this.setState({ commentsRes: { state: "loading" } });
602 commentsRes: await HttpService.client.getComments({
605 sort: postToCommentSortType(sort),
607 community_name: name,
614 restoreScrollPosition(this.context);
618 async handleDeleteCommunity(form: DeleteCommunity) {
619 const deleteCommunityRes = await HttpService.client.deleteCommunity(form);
620 this.updateCommunity(deleteCommunityRes);
623 async handleAddModToCommunity(form: AddModToCommunity) {
624 const addModRes = await HttpService.client.addModToCommunity(form);
625 this.updateModerators(addModRes);
628 async handleFollow(form: FollowCommunity) {
629 const followCommunityRes = await HttpService.client.followCommunity(form);
630 this.updateCommunity(followCommunityRes);
633 if (followCommunityRes.state == "success") {
634 const communityId = followCommunityRes.data.community_view.community.id;
635 const mui = UserService.Instance.myUserInfo;
637 mui.follows = mui.follows.filter(i => i.community.id != communityId);
642 async handlePurgeCommunity(form: PurgeCommunity) {
643 const purgeCommunityRes = await HttpService.client.purgeCommunity(form);
644 this.purgeItem(purgeCommunityRes);
647 async handlePurgePerson(form: PurgePerson) {
648 const purgePersonRes = await HttpService.client.purgePerson(form);
649 this.purgeItem(purgePersonRes);
652 async handlePurgeComment(form: PurgeComment) {
653 const purgeCommentRes = await HttpService.client.purgeComment(form);
654 this.purgeItem(purgeCommentRes);
657 async handlePurgePost(form: PurgePost) {
658 const purgeRes = await HttpService.client.purgePost(form);
659 this.purgeItem(purgeRes);
662 async handleBlockCommunity(form: BlockCommunity) {
663 const blockCommunityRes = await HttpService.client.blockCommunity(form);
664 if (blockCommunityRes.state == "success") {
665 updateCommunityBlock(blockCommunityRes.data);
667 if (s.communityRes.state == "success") {
668 s.communityRes.data.community_view.blocked =
669 blockCommunityRes.data.blocked;
675 async handleBlockPerson(form: BlockPerson) {
676 const blockPersonRes = await HttpService.client.blockPerson(form);
677 if (blockPersonRes.state == "success") {
678 updatePersonBlock(blockPersonRes.data);
682 async handleRemoveCommunity(form: RemoveCommunity) {
683 const removeCommunityRes = await HttpService.client.removeCommunity(form);
684 this.updateCommunity(removeCommunityRes);
687 async handleEditCommunity(form: EditCommunity) {
688 const res = await HttpService.client.editCommunity(form);
689 this.updateCommunity(res);
694 async handleCreateComment(form: CreateComment) {
695 const createCommentRes = await HttpService.client.createComment(form);
696 this.createAndUpdateComments(createCommentRes);
698 return createCommentRes;
701 async handleEditComment(form: EditComment) {
702 const editCommentRes = await HttpService.client.editComment(form);
703 this.findAndUpdateComment(editCommentRes);
705 return editCommentRes;
708 async handleDeleteComment(form: DeleteComment) {
709 const deleteCommentRes = await HttpService.client.deleteComment(form);
710 this.findAndUpdateComment(deleteCommentRes);
713 async handleDeletePost(form: DeletePost) {
714 const deleteRes = await HttpService.client.deletePost(form);
715 this.findAndUpdatePost(deleteRes);
718 async handleRemovePost(form: RemovePost) {
719 const removeRes = await HttpService.client.removePost(form);
720 this.findAndUpdatePost(removeRes);
723 async handleRemoveComment(form: RemoveComment) {
724 const removeCommentRes = await HttpService.client.removeComment(form);
725 this.findAndUpdateComment(removeCommentRes);
728 async handleSaveComment(form: SaveComment) {
729 const saveCommentRes = await HttpService.client.saveComment(form);
730 this.findAndUpdateComment(saveCommentRes);
733 async handleSavePost(form: SavePost) {
734 const saveRes = await HttpService.client.savePost(form);
735 this.findAndUpdatePost(saveRes);
738 async handleFeaturePost(form: FeaturePost) {
739 const featureRes = await HttpService.client.featurePost(form);
740 this.findAndUpdatePost(featureRes);
743 async handleCommentVote(form: CreateCommentLike) {
744 const voteRes = await HttpService.client.likeComment(form);
745 this.findAndUpdateComment(voteRes);
748 async handlePostEdit(form: EditPost) {
749 const res = await HttpService.client.editPost(form);
750 this.findAndUpdatePost(res);
753 async handlePostVote(form: CreatePostLike) {
754 const voteRes = await HttpService.client.likePost(form);
755 this.findAndUpdatePost(voteRes);
758 async handleCommentReport(form: CreateCommentReport) {
759 const reportRes = await HttpService.client.createCommentReport(form);
760 if (reportRes.state == "success") {
761 toast(i18n.t("report_created"));
765 async handlePostReport(form: CreatePostReport) {
766 const reportRes = await HttpService.client.createPostReport(form);
767 if (reportRes.state == "success") {
768 toast(i18n.t("report_created"));
772 async handleLockPost(form: LockPost) {
773 const lockRes = await HttpService.client.lockPost(form);
774 this.findAndUpdatePost(lockRes);
777 async handleDistinguishComment(form: DistinguishComment) {
778 const distinguishRes = await HttpService.client.distinguishComment(form);
779 this.findAndUpdateComment(distinguishRes);
782 async handleAddAdmin(form: AddAdmin) {
783 const addAdminRes = await HttpService.client.addAdmin(form);
785 if (addAdminRes.state == "success") {
786 this.setState(s => ((s.siteRes.admins = addAdminRes.data.admins), s));
790 async handleTransferCommunity(form: TransferCommunity) {
791 const transferCommunityRes = await HttpService.client.transferCommunity(
794 toast(i18n.t("transfer_community"));
795 this.updateCommunityFull(transferCommunityRes);
798 async handleCommentReplyRead(form: MarkCommentReplyAsRead) {
799 const readRes = await HttpService.client.markCommentReplyAsRead(form);
800 this.findAndUpdateCommentReply(readRes);
803 async handlePersonMentionRead(form: MarkPersonMentionAsRead) {
804 // TODO not sure what to do here. Maybe it is actually optional, because post doesn't need it.
805 await HttpService.client.markPersonMentionAsRead(form);
808 async handleBanFromCommunity(form: BanFromCommunity) {
809 const banRes = await HttpService.client.banFromCommunity(form);
810 this.updateBanFromCommunity(banRes);
813 async handleBanPerson(form: BanPerson) {
814 const banRes = await HttpService.client.banPerson(form);
815 this.updateBan(banRes);
818 updateBanFromCommunity(banRes: RequestState<BanFromCommunityResponse>) {
819 // Maybe not necessary
820 if (banRes.state == "success") {
822 if (s.postsRes.state == "success") {
823 s.postsRes.data.posts
824 .filter(c => c.creator.id == banRes.data.person_view.person.id)
826 c => (c.creator_banned_from_community = banRes.data.banned)
829 if (s.commentsRes.state == "success") {
830 s.commentsRes.data.comments
831 .filter(c => c.creator.id == banRes.data.person_view.person.id)
833 c => (c.creator_banned_from_community = banRes.data.banned)
841 updateBan(banRes: RequestState<BanPersonResponse>) {
842 // Maybe not necessary
843 if (banRes.state == "success") {
845 if (s.postsRes.state == "success") {
846 s.postsRes.data.posts
847 .filter(c => c.creator.id == banRes.data.person_view.person.id)
848 .forEach(c => (c.creator.banned = banRes.data.banned));
850 if (s.commentsRes.state == "success") {
851 s.commentsRes.data.comments
852 .filter(c => c.creator.id == banRes.data.person_view.person.id)
853 .forEach(c => (c.creator.banned = banRes.data.banned));
860 updateCommunity(res: RequestState<CommunityResponse>) {
862 if (s.communityRes.state == "success" && res.state == "success") {
863 s.communityRes.data.community_view = res.data.community_view;
864 s.communityRes.data.discussion_languages =
865 res.data.discussion_languages;
871 updateCommunityFull(res: RequestState<GetCommunityResponse>) {
873 if (s.communityRes.state == "success" && res.state == "success") {
874 s.communityRes.data.community_view = res.data.community_view;
875 s.communityRes.data.moderators = res.data.moderators;
881 purgeItem(purgeRes: RequestState<PurgeItemResponse>) {
882 if (purgeRes.state == "success") {
883 toast(i18n.t("purge_success"));
884 this.context.router.history.push(`/`);
888 findAndUpdateComment(res: RequestState<CommentResponse>) {
890 if (s.commentsRes.state == "success" && res.state == "success") {
891 s.commentsRes.data.comments = editComment(
892 res.data.comment_view,
893 s.commentsRes.data.comments
895 s.finished.set(res.data.comment_view.comment.id, true);
901 createAndUpdateComments(res: RequestState<CommentResponse>) {
903 if (s.commentsRes.state == "success" && res.state == "success") {
904 s.commentsRes.data.comments.unshift(res.data.comment_view);
906 // Set finished for the parent
908 getCommentParentId(res.data.comment_view.comment) ?? 0,
916 findAndUpdateCommentReply(res: RequestState<CommentReplyResponse>) {
918 if (s.commentsRes.state == "success" && res.state == "success") {
919 s.commentsRes.data.comments = editWith(
920 res.data.comment_reply_view,
921 s.commentsRes.data.comments
928 findAndUpdatePost(res: RequestState<PostResponse>) {
930 if (s.postsRes.state == "success" && res.state == "success") {
931 s.postsRes.data.posts = editPost(
933 s.postsRes.data.posts
940 updateModerators(res: RequestState<AddModToCommunityResponse>) {
941 // Update the moderators
943 if (s.communityRes.state == "success" && res.state == "success") {
944 s.communityRes.data.moderators = res.data.moderators;