1 import { getQueryParams, getQueryString } from "@utils/helpers";
2 import type { QueryParams } from "@utils/types";
3 import { Component, linkEvent } from "inferno";
4 import { RouteComponentProps } from "inferno-router/dist/Route";
8 AddModToCommunityResponse,
10 BanFromCommunityResponse,
41 MarkCommentReplyAsRead,
42 MarkPersonMentionAsRead,
56 } from "lemmy-js-client";
57 import { i18n } from "../../i18next";
62 } from "../../interfaces";
63 import { UserService } from "../../services";
64 import { FirstLoadService } from "../../services/FirstLoadService";
65 import { HttpService, RequestState } from "../../services/HttpService";
80 postToCommentSortType,
82 restoreScrollPosition,
91 import { CommentNodes } from "../comment/comment-nodes";
92 import { BannerIconHeader } from "../common/banner-icon-header";
93 import { DataTypeSelect } from "../common/data-type-select";
94 import { HtmlTags } from "../common/html-tags";
95 import { Icon, Spinner } from "../common/icon";
96 import { Paginator } from "../common/paginator";
97 import { SortSelect } from "../common/sort-select";
98 import { Sidebar } from "../community/sidebar";
99 import { SiteSidebar } from "../home/site-sidebar";
100 import { PostListings } from "../post/post-listings";
101 import { CommunityLink } from "./community-link";
103 type CommunityData = RouteDataResponse<{
104 communityRes: GetCommunityResponse;
105 postsRes: GetPostsResponse;
106 commentsRes: GetCommentsResponse;
110 communityRes: RequestState<GetCommunityResponse>;
111 postsRes: RequestState<GetPostsResponse>;
112 commentsRes: RequestState<GetCommentsResponse>;
113 siteRes: GetSiteResponse;
114 showSidebarMobile: boolean;
115 finished: Map<CommentId, boolean | undefined>;
116 isIsomorphic: boolean;
119 interface CommunityProps {
125 function getCommunityQueryParams() {
126 return getQueryParams<CommunityProps>({
127 dataType: getDataTypeFromQuery,
128 page: getPageFromString,
129 sort: getSortTypeFromQuery,
133 function getDataTypeFromQuery(type?: string): DataType {
134 return type ? DataType[type] : DataType.Post;
137 function getSortTypeFromQuery(type?: string): SortType {
139 UserService.Instance.myUserInfo?.local_user_view.local_user
142 return type ? (type as SortType) : mySortType ?? "Active";
145 export class Community extends Component<
146 RouteComponentProps<{ name: string }>,
149 private isoData = setIsoData<CommunityData>(this.context);
151 communityRes: { state: "empty" },
152 postsRes: { state: "empty" },
153 commentsRes: { state: "empty" },
154 siteRes: this.isoData.site_res,
155 showSidebarMobile: false,
160 constructor(props: RouteComponentProps<{ name: string }>, context: any) {
161 super(props, context);
163 this.handleSortChange = this.handleSortChange.bind(this);
164 this.handleDataTypeChange = this.handleDataTypeChange.bind(this);
165 this.handlePageChange = this.handlePageChange.bind(this);
167 // All of the action binds
168 this.handleDeleteCommunity = this.handleDeleteCommunity.bind(this);
169 this.handleEditCommunity = this.handleEditCommunity.bind(this);
170 this.handleFollow = this.handleFollow.bind(this);
171 this.handleRemoveCommunity = this.handleRemoveCommunity.bind(this);
172 this.handleCreateComment = this.handleCreateComment.bind(this);
173 this.handleEditComment = this.handleEditComment.bind(this);
174 this.handleSaveComment = this.handleSaveComment.bind(this);
175 this.handleBlockCommunity = this.handleBlockCommunity.bind(this);
176 this.handleBlockPerson = this.handleBlockPerson.bind(this);
177 this.handleDeleteComment = this.handleDeleteComment.bind(this);
178 this.handleRemoveComment = this.handleRemoveComment.bind(this);
179 this.handleCommentVote = this.handleCommentVote.bind(this);
180 this.handleAddModToCommunity = this.handleAddModToCommunity.bind(this);
181 this.handleAddAdmin = this.handleAddAdmin.bind(this);
182 this.handlePurgePerson = this.handlePurgePerson.bind(this);
183 this.handlePurgeComment = this.handlePurgeComment.bind(this);
184 this.handleCommentReport = this.handleCommentReport.bind(this);
185 this.handleDistinguishComment = this.handleDistinguishComment.bind(this);
186 this.handleTransferCommunity = this.handleTransferCommunity.bind(this);
187 this.handleCommentReplyRead = this.handleCommentReplyRead.bind(this);
188 this.handlePersonMentionRead = this.handlePersonMentionRead.bind(this);
189 this.handleBanFromCommunity = this.handleBanFromCommunity.bind(this);
190 this.handleBanPerson = this.handleBanPerson.bind(this);
191 this.handlePostVote = this.handlePostVote.bind(this);
192 this.handlePostEdit = this.handlePostEdit.bind(this);
193 this.handlePostReport = this.handlePostReport.bind(this);
194 this.handleLockPost = this.handleLockPost.bind(this);
195 this.handleDeletePost = this.handleDeletePost.bind(this);
196 this.handleRemovePost = this.handleRemovePost.bind(this);
197 this.handleSavePost = this.handleSavePost.bind(this);
198 this.handlePurgePost = this.handlePurgePost.bind(this);
199 this.handleFeaturePost = this.handleFeaturePost.bind(this);
201 // Only fetch the data if coming from another route
202 if (FirstLoadService.isFirstLoad) {
203 const { communityRes, commentsRes, postsRes } = this.isoData.routeData;
215 async fetchCommunity() {
216 this.setState({ communityRes: { state: "loading" } });
218 communityRes: await HttpService.client.getCommunity({
219 name: this.props.match.params.name,
225 async componentDidMount() {
226 if (!this.state.isIsomorphic) {
227 await Promise.all([this.fetchCommunity(), this.fetchData()]);
233 componentWillUnmount() {
234 saveScrollPosition(this.context);
237 static async fetchInitialData({
240 query: { dataType: urlDataType, page: urlPage, sort: urlSort },
242 }: InitialFetchRequest<QueryParams<CommunityProps>>): Promise<
243 Promise<CommunityData>
245 const pathSplit = path.split("/");
247 const communityName = pathSplit[2];
248 const communityForm: GetCommunity = {
253 const dataType = getDataTypeFromQuery(urlDataType);
255 const sort = getSortTypeFromQuery(urlSort);
257 const page = getPageFromString(urlPage);
259 let postsResponse: RequestState<GetPostsResponse> = { state: "empty" };
260 let commentsResponse: RequestState<GetCommentsResponse> = {
264 if (dataType === DataType.Post) {
265 const getPostsForm: GetPosts = {
266 community_name: communityName,
275 postsResponse = await client.getPosts(getPostsForm);
277 const getCommentsForm: GetComments = {
278 community_name: communityName,
281 sort: postToCommentSortType(sort),
287 commentsResponse = await client.getComments(getCommentsForm);
291 communityRes: await client.getCommunity(communityForm),
292 commentsRes: commentsResponse,
293 postsRes: postsResponse,
297 get documentTitle(): string {
298 const cRes = this.state.communityRes;
299 return cRes.state == "success"
300 ? `${cRes.data.community_view.community.title} - ${this.isoData.site_res.site_view.site.name}`
305 switch (this.state.communityRes.state) {
313 const res = this.state.communityRes.data;
314 const { page } = getCommunityQueryParams();
319 title={this.documentTitle}
320 path={this.context.router.route.match.url}
321 description={res.community_view.community.description}
322 image={res.community_view.community.icon}
325 <div className="row">
326 <div className="col-12 col-md-8">
327 {this.communityInfo(res)}
328 <div className="d-block d-md-none">
330 className="btn btn-secondary d-inline-block mb-2 me-3"
331 onClick={linkEvent(this, this.handleShowSidebarMobile)}
333 {i18n.t("sidebar")}{" "}
336 this.state.showSidebarMobile
340 classes="icon-inline"
343 {this.state.showSidebarMobile && this.sidebar(res)}
347 <Paginator page={page} onChange={this.handlePageChange} />
349 <div className="d-none d-md-block col-md-4">
361 <div className="community container-lg">{this.renderCommunity()}</div>
365 sidebar(res: GetCommunityResponse) {
366 const { site_res } = this.isoData;
367 // For some reason, this returns an empty vec if it matches the site langs
368 const communityLangs =
369 res.discussion_languages.length === 0
370 ? site_res.all_languages.map(({ id }) => id)
371 : res.discussion_languages;
376 community_view={res.community_view}
377 moderators={res.moderators}
378 admins={site_res.admins}
379 enableNsfw={enableNsfw(site_res)}
381 allLanguages={site_res.all_languages}
382 siteLanguages={site_res.discussion_languages}
383 communityLanguages={communityLangs}
384 onDeleteCommunity={this.handleDeleteCommunity}
385 onRemoveCommunity={this.handleRemoveCommunity}
386 onLeaveModTeam={this.handleAddModToCommunity}
387 onFollowCommunity={this.handleFollow}
388 onBlockCommunity={this.handleBlockCommunity}
389 onPurgeCommunity={this.handlePurgeCommunity}
390 onEditCommunity={this.handleEditCommunity}
392 {!res.community_view.community.local && res.site && (
393 <SiteSidebar site={res.site} showLocal={showLocal(this.isoData)} />
399 listings(communityRes: GetCommunityResponse) {
400 const { dataType } = getCommunityQueryParams();
401 const { site_res } = this.isoData;
403 if (dataType === DataType.Post) {
404 switch (this.state.postsRes.state) {
414 posts={this.state.postsRes.data.posts}
416 enableDownvotes={enableDownvotes(site_res)}
417 enableNsfw={enableNsfw(site_res)}
418 allLanguages={site_res.all_languages}
419 siteLanguages={site_res.discussion_languages}
420 onBlockPerson={this.handleBlockPerson}
421 onPostEdit={this.handlePostEdit}
422 onPostVote={this.handlePostVote}
423 onPostReport={this.handlePostReport}
424 onLockPost={this.handleLockPost}
425 onDeletePost={this.handleDeletePost}
426 onRemovePost={this.handleRemovePost}
427 onSavePost={this.handleSavePost}
428 onPurgePerson={this.handlePurgePerson}
429 onPurgePost={this.handlePurgePost}
430 onBanPerson={this.handleBanPerson}
431 onBanPersonFromCommunity={this.handleBanFromCommunity}
432 onAddModToCommunity={this.handleAddModToCommunity}
433 onAddAdmin={this.handleAddAdmin}
434 onTransferCommunity={this.handleTransferCommunity}
435 onFeaturePost={this.handleFeaturePost}
440 switch (this.state.commentsRes.state) {
450 nodes={commentsToFlatNodes(this.state.commentsRes.data.comments)}
451 viewType={CommentViewType.Flat}
452 finished={this.state.finished}
455 enableDownvotes={enableDownvotes(site_res)}
456 moderators={communityRes.moderators}
457 admins={site_res.admins}
458 allLanguages={site_res.all_languages}
459 siteLanguages={site_res.discussion_languages}
460 onSaveComment={this.handleSaveComment}
461 onBlockPerson={this.handleBlockPerson}
462 onDeleteComment={this.handleDeleteComment}
463 onRemoveComment={this.handleRemoveComment}
464 onCommentVote={this.handleCommentVote}
465 onCommentReport={this.handleCommentReport}
466 onDistinguishComment={this.handleDistinguishComment}
467 onAddModToCommunity={this.handleAddModToCommunity}
468 onAddAdmin={this.handleAddAdmin}
469 onTransferCommunity={this.handleTransferCommunity}
470 onPurgeComment={this.handlePurgeComment}
471 onPurgePerson={this.handlePurgePerson}
472 onCommentReplyRead={this.handleCommentReplyRead}
473 onPersonMentionRead={this.handlePersonMentionRead}
474 onBanPersonFromCommunity={this.handleBanFromCommunity}
475 onBanPerson={this.handleBanPerson}
476 onCreateComment={this.handleCreateComment}
477 onEditComment={this.handleEditComment}
484 communityInfo(res: GetCommunityResponse) {
485 const community = res.community_view.community;
489 <div className="mb-2">
490 <BannerIconHeader banner={community.banner} icon={community.icon} />
491 <h5 className="mb-0 overflow-wrap-anywhere">{community.title}</h5>
493 community={community}
504 selects(res: GetCommunityResponse) {
505 // let communityRss = this.state.communityRes.map(r =>
506 // communityRSSUrl(r.community_view.community.actor_id, this.state.sort)
508 const { dataType, sort } = getCommunityQueryParams();
509 const communityRss = res
510 ? communityRSSUrl(res.community_view.community.actor_id, sort)
514 <div className="mb-3">
515 <span className="me-3">
518 onChange={this.handleDataTypeChange}
521 <span className="me-2">
522 <SortSelect sort={sort} onChange={this.handleSortChange} />
526 <a href={communityRss} title="RSS" rel={relTags}>
527 <Icon icon="rss" classes="text-muted small" />
531 type="application/atom+xml"
540 handlePageChange(page: number) {
541 this.updateUrl({ page });
542 window.scrollTo(0, 0);
545 handleSortChange(sort: SortType) {
546 this.updateUrl({ sort, page: 1 });
547 window.scrollTo(0, 0);
550 handleDataTypeChange(dataType: DataType) {
551 this.updateUrl({ dataType, page: 1 });
552 window.scrollTo(0, 0);
555 handleShowSidebarMobile(i: Community) {
556 i.setState(({ showSidebarMobile }) => ({
557 showSidebarMobile: !showSidebarMobile,
561 async updateUrl({ dataType, page, sort }: Partial<CommunityProps>) {
563 dataType: urlDataType,
566 } = getCommunityQueryParams();
568 const queryParams: QueryParams<CommunityProps> = {
569 dataType: getDataTypeString(dataType ?? urlDataType),
570 page: (page ?? urlPage).toString(),
571 sort: sort ?? urlSort,
574 this.props.history.push(
575 `/c/${this.props.match.params.name}${getQueryString(queryParams)}`
578 await this.fetchData();
582 const { dataType, page, sort } = getCommunityQueryParams();
583 const { name } = this.props.match.params;
585 if (dataType === DataType.Post) {
586 this.setState({ postsRes: { state: "loading" } });
588 postsRes: await HttpService.client.getPosts({
593 community_name: name,
599 this.setState({ commentsRes: { state: "loading" } });
601 commentsRes: await HttpService.client.getComments({
604 sort: postToCommentSortType(sort),
606 community_name: name,
613 restoreScrollPosition(this.context);
617 async handleDeleteCommunity(form: DeleteCommunity) {
618 const deleteCommunityRes = await HttpService.client.deleteCommunity(form);
619 this.updateCommunity(deleteCommunityRes);
622 async handleAddModToCommunity(form: AddModToCommunity) {
623 const addModRes = await HttpService.client.addModToCommunity(form);
624 this.updateModerators(addModRes);
627 async handleFollow(form: FollowCommunity) {
628 const followCommunityRes = await HttpService.client.followCommunity(form);
629 this.updateCommunity(followCommunityRes);
632 if (followCommunityRes.state == "success") {
633 const communityId = followCommunityRes.data.community_view.community.id;
634 const mui = UserService.Instance.myUserInfo;
636 mui.follows = mui.follows.filter(i => i.community.id != communityId);
641 async handlePurgeCommunity(form: PurgeCommunity) {
642 const purgeCommunityRes = await HttpService.client.purgeCommunity(form);
643 this.purgeItem(purgeCommunityRes);
646 async handlePurgePerson(form: PurgePerson) {
647 const purgePersonRes = await HttpService.client.purgePerson(form);
648 this.purgeItem(purgePersonRes);
651 async handlePurgeComment(form: PurgeComment) {
652 const purgeCommentRes = await HttpService.client.purgeComment(form);
653 this.purgeItem(purgeCommentRes);
656 async handlePurgePost(form: PurgePost) {
657 const purgeRes = await HttpService.client.purgePost(form);
658 this.purgeItem(purgeRes);
661 async handleBlockCommunity(form: BlockCommunity) {
662 const blockCommunityRes = await HttpService.client.blockCommunity(form);
663 if (blockCommunityRes.state == "success") {
664 updateCommunityBlock(blockCommunityRes.data);
666 if (s.communityRes.state == "success") {
667 s.communityRes.data.community_view.blocked =
668 blockCommunityRes.data.blocked;
674 async handleBlockPerson(form: BlockPerson) {
675 const blockPersonRes = await HttpService.client.blockPerson(form);
676 if (blockPersonRes.state == "success") {
677 updatePersonBlock(blockPersonRes.data);
681 async handleRemoveCommunity(form: RemoveCommunity) {
682 const removeCommunityRes = await HttpService.client.removeCommunity(form);
683 this.updateCommunity(removeCommunityRes);
686 async handleEditCommunity(form: EditCommunity) {
687 const res = await HttpService.client.editCommunity(form);
688 this.updateCommunity(res);
693 async handleCreateComment(form: CreateComment) {
694 const createCommentRes = await HttpService.client.createComment(form);
695 this.createAndUpdateComments(createCommentRes);
697 return createCommentRes;
700 async handleEditComment(form: EditComment) {
701 const editCommentRes = await HttpService.client.editComment(form);
702 this.findAndUpdateComment(editCommentRes);
704 return editCommentRes;
707 async handleDeleteComment(form: DeleteComment) {
708 const deleteCommentRes = await HttpService.client.deleteComment(form);
709 this.findAndUpdateComment(deleteCommentRes);
712 async handleDeletePost(form: DeletePost) {
713 const deleteRes = await HttpService.client.deletePost(form);
714 this.findAndUpdatePost(deleteRes);
717 async handleRemovePost(form: RemovePost) {
718 const removeRes = await HttpService.client.removePost(form);
719 this.findAndUpdatePost(removeRes);
722 async handleRemoveComment(form: RemoveComment) {
723 const removeCommentRes = await HttpService.client.removeComment(form);
724 this.findAndUpdateComment(removeCommentRes);
727 async handleSaveComment(form: SaveComment) {
728 const saveCommentRes = await HttpService.client.saveComment(form);
729 this.findAndUpdateComment(saveCommentRes);
732 async handleSavePost(form: SavePost) {
733 const saveRes = await HttpService.client.savePost(form);
734 this.findAndUpdatePost(saveRes);
737 async handleFeaturePost(form: FeaturePost) {
738 const featureRes = await HttpService.client.featurePost(form);
739 this.findAndUpdatePost(featureRes);
742 async handleCommentVote(form: CreateCommentLike) {
743 const voteRes = await HttpService.client.likeComment(form);
744 this.findAndUpdateComment(voteRes);
747 async handlePostEdit(form: EditPost) {
748 const res = await HttpService.client.editPost(form);
749 this.findAndUpdatePost(res);
752 async handlePostVote(form: CreatePostLike) {
753 const voteRes = await HttpService.client.likePost(form);
754 this.findAndUpdatePost(voteRes);
757 async handleCommentReport(form: CreateCommentReport) {
758 const reportRes = await HttpService.client.createCommentReport(form);
759 if (reportRes.state == "success") {
760 toast(i18n.t("report_created"));
764 async handlePostReport(form: CreatePostReport) {
765 const reportRes = await HttpService.client.createPostReport(form);
766 if (reportRes.state == "success") {
767 toast(i18n.t("report_created"));
771 async handleLockPost(form: LockPost) {
772 const lockRes = await HttpService.client.lockPost(form);
773 this.findAndUpdatePost(lockRes);
776 async handleDistinguishComment(form: DistinguishComment) {
777 const distinguishRes = await HttpService.client.distinguishComment(form);
778 this.findAndUpdateComment(distinguishRes);
781 async handleAddAdmin(form: AddAdmin) {
782 const addAdminRes = await HttpService.client.addAdmin(form);
784 if (addAdminRes.state == "success") {
785 this.setState(s => ((s.siteRes.admins = addAdminRes.data.admins), s));
789 async handleTransferCommunity(form: TransferCommunity) {
790 const transferCommunityRes = await HttpService.client.transferCommunity(
793 toast(i18n.t("transfer_community"));
794 this.updateCommunityFull(transferCommunityRes);
797 async handleCommentReplyRead(form: MarkCommentReplyAsRead) {
798 const readRes = await HttpService.client.markCommentReplyAsRead(form);
799 this.findAndUpdateCommentReply(readRes);
802 async handlePersonMentionRead(form: MarkPersonMentionAsRead) {
803 // TODO not sure what to do here. Maybe it is actually optional, because post doesn't need it.
804 await HttpService.client.markPersonMentionAsRead(form);
807 async handleBanFromCommunity(form: BanFromCommunity) {
808 const banRes = await HttpService.client.banFromCommunity(form);
809 this.updateBanFromCommunity(banRes);
812 async handleBanPerson(form: BanPerson) {
813 const banRes = await HttpService.client.banPerson(form);
814 this.updateBan(banRes);
817 updateBanFromCommunity(banRes: RequestState<BanFromCommunityResponse>) {
818 // Maybe not necessary
819 if (banRes.state == "success") {
821 if (s.postsRes.state == "success") {
822 s.postsRes.data.posts
823 .filter(c => c.creator.id == banRes.data.person_view.person.id)
825 c => (c.creator_banned_from_community = banRes.data.banned)
828 if (s.commentsRes.state == "success") {
829 s.commentsRes.data.comments
830 .filter(c => c.creator.id == banRes.data.person_view.person.id)
832 c => (c.creator_banned_from_community = banRes.data.banned)
840 updateBan(banRes: RequestState<BanPersonResponse>) {
841 // Maybe not necessary
842 if (banRes.state == "success") {
844 if (s.postsRes.state == "success") {
845 s.postsRes.data.posts
846 .filter(c => c.creator.id == banRes.data.person_view.person.id)
847 .forEach(c => (c.creator.banned = banRes.data.banned));
849 if (s.commentsRes.state == "success") {
850 s.commentsRes.data.comments
851 .filter(c => c.creator.id == banRes.data.person_view.person.id)
852 .forEach(c => (c.creator.banned = banRes.data.banned));
859 updateCommunity(res: RequestState<CommunityResponse>) {
861 if (s.communityRes.state == "success" && res.state == "success") {
862 s.communityRes.data.community_view = res.data.community_view;
863 s.communityRes.data.discussion_languages =
864 res.data.discussion_languages;
870 updateCommunityFull(res: RequestState<GetCommunityResponse>) {
872 if (s.communityRes.state == "success" && res.state == "success") {
873 s.communityRes.data.community_view = res.data.community_view;
874 s.communityRes.data.moderators = res.data.moderators;
880 purgeItem(purgeRes: RequestState<PurgeItemResponse>) {
881 if (purgeRes.state == "success") {
882 toast(i18n.t("purge_success"));
883 this.context.router.history.push(`/`);
887 findAndUpdateComment(res: RequestState<CommentResponse>) {
889 if (s.commentsRes.state == "success" && res.state == "success") {
890 s.commentsRes.data.comments = editComment(
891 res.data.comment_view,
892 s.commentsRes.data.comments
894 s.finished.set(res.data.comment_view.comment.id, true);
900 createAndUpdateComments(res: RequestState<CommentResponse>) {
902 if (s.commentsRes.state == "success" && res.state == "success") {
903 s.commentsRes.data.comments.unshift(res.data.comment_view);
905 // Set finished for the parent
907 getCommentParentId(res.data.comment_view.comment) ?? 0,
915 findAndUpdateCommentReply(res: RequestState<CommentReplyResponse>) {
917 if (s.commentsRes.state == "success" && res.state == "success") {
918 s.commentsRes.data.comments = editWith(
919 res.data.comment_reply_view,
920 s.commentsRes.data.comments
927 findAndUpdatePost(res: RequestState<PostResponse>) {
929 if (s.postsRes.state == "success" && res.state == "success") {
930 s.postsRes.data.posts = editPost(
932 s.postsRes.data.posts
939 updateModerators(res: RequestState<AddModToCommunityResponse>) {
940 // Update the moderators
942 if (s.communityRes.state == "success" && res.state == "success") {
943 s.communityRes.data.moderators = res.data.moderators;