19 restoreScrollPosition,
21 } from "@utils/browser";
22 import { debounce, randomStr } from "@utils/helpers";
23 import { isImage } from "@utils/media";
24 import { RouteDataResponse } from "@utils/types";
25 import autosize from "autosize";
26 import classNames from "classnames";
27 import { Component, RefObject, createRef, linkEvent } from "inferno";
31 AddModToCommunityResponse,
33 BanFromCommunityResponse,
64 MarkCommentReplyAsRead,
65 MarkPersonMentionAsRead,
79 } from "lemmy-js-client";
80 import { commentTreeMaxDepth } 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 { CommentForm } from "../comment/comment-form";
91 import { CommentNodes } from "../comment/comment-nodes";
92 import { HtmlTags } from "../common/html-tags";
93 import { Icon, Spinner } from "../common/icon";
94 import { Sidebar } from "../community/sidebar";
95 import { PostListing } from "./post-listing";
97 const commentsShownInterval = 15;
99 type PostData = RouteDataResponse<{
100 postRes: GetPostResponse;
101 commentsRes: GetCommentsResponse;
104 interface PostState {
107 postRes: RequestState<GetPostResponse>;
108 commentsRes: RequestState<GetCommentsResponse>;
109 commentSort: CommentSortType;
110 commentViewType: CommentViewType;
112 siteRes: GetSiteResponse;
113 commentSectionRef?: RefObject<HTMLDivElement>;
114 showSidebarMobile: boolean;
115 maxCommentsShown: number;
116 finished: Map<CommentId, boolean | undefined>;
117 isIsomorphic: boolean;
120 export class Post extends Component<any, PostState> {
121 private isoData = setIsoData<PostData>(this.context);
122 private commentScrollDebounced: () => void;
124 postRes: { state: "empty" },
125 commentsRes: { state: "empty" },
126 postId: getIdFromProps(this.props),
127 commentId: getCommentIdFromProps(this.props),
129 commentViewType: CommentViewType.Tree,
131 siteRes: this.isoData.site_res,
132 showSidebarMobile: false,
133 maxCommentsShown: commentsShownInterval,
138 constructor(props: any, context: any) {
139 super(props, context);
141 this.handleDeleteCommunityClick =
142 this.handleDeleteCommunityClick.bind(this);
143 this.handleEditCommunity = this.handleEditCommunity.bind(this);
144 this.handleFollow = this.handleFollow.bind(this);
145 this.handleModRemoveCommunity = this.handleModRemoveCommunity.bind(this);
146 this.handleCreateComment = this.handleCreateComment.bind(this);
147 this.handleEditComment = this.handleEditComment.bind(this);
148 this.handleSaveComment = this.handleSaveComment.bind(this);
149 this.handleBlockCommunity = this.handleBlockCommunity.bind(this);
150 this.handleBlockPerson = this.handleBlockPerson.bind(this);
151 this.handleDeleteComment = this.handleDeleteComment.bind(this);
152 this.handleRemoveComment = this.handleRemoveComment.bind(this);
153 this.handleCommentVote = this.handleCommentVote.bind(this);
154 this.handleAddModToCommunity = this.handleAddModToCommunity.bind(this);
155 this.handleAddAdmin = this.handleAddAdmin.bind(this);
156 this.handlePurgePerson = this.handlePurgePerson.bind(this);
157 this.handlePurgeComment = this.handlePurgeComment.bind(this);
158 this.handleCommentReport = this.handleCommentReport.bind(this);
159 this.handleDistinguishComment = this.handleDistinguishComment.bind(this);
160 this.handleTransferCommunity = this.handleTransferCommunity.bind(this);
161 this.handleFetchChildren = this.handleFetchChildren.bind(this);
162 this.handleCommentReplyRead = this.handleCommentReplyRead.bind(this);
163 this.handlePersonMentionRead = this.handlePersonMentionRead.bind(this);
164 this.handleBanFromCommunity = this.handleBanFromCommunity.bind(this);
165 this.handleBanPerson = this.handleBanPerson.bind(this);
166 this.handlePostEdit = this.handlePostEdit.bind(this);
167 this.handlePostVote = this.handlePostVote.bind(this);
168 this.handlePostReport = this.handlePostReport.bind(this);
169 this.handleLockPost = this.handleLockPost.bind(this);
170 this.handleDeletePost = this.handleDeletePost.bind(this);
171 this.handleRemovePost = this.handleRemovePost.bind(this);
172 this.handleSavePost = this.handleSavePost.bind(this);
173 this.handlePurgePost = this.handlePurgePost.bind(this);
174 this.handleFeaturePost = this.handleFeaturePost.bind(this);
175 this.handleMarkPostAsRead = this.handleMarkPostAsRead.bind(this);
177 this.state = { ...this.state, commentSectionRef: createRef() };
179 // Only fetch the data if coming from another route
180 if (FirstLoadService.isFirstLoad) {
181 const { commentsRes, postRes } = this.isoData.routeData;
191 if (this.checkScrollIntoCommentsParam) {
192 this.scrollIntoCommentSection();
200 postRes: { state: "loading" },
201 commentsRes: { state: "loading" },
204 const auth = myAuth();
207 postRes: await HttpService.client.getPost({
208 id: this.state.postId,
209 comment_id: this.state.commentId,
212 commentsRes: await HttpService.client.getComments({
213 post_id: this.state.postId,
214 parent_id: this.state.commentId,
215 max_depth: commentTreeMaxDepth,
216 sort: this.state.commentSort,
225 if (!this.state.commentId) restoreScrollPosition(this.context);
227 if (this.checkScrollIntoCommentsParam) {
228 this.scrollIntoCommentSection();
232 static async fetchInitialData({
236 }: InitialFetchRequest): Promise<PostData> {
237 const pathSplit = path.split("/");
239 const pathType = pathSplit.at(1);
240 const id = pathSplit.at(2) ? Number(pathSplit.at(2)) : undefined;
242 const postForm: GetPost = {
246 const commentsForm: GetComments = {
247 max_depth: commentTreeMaxDepth,
254 // Set the correct id based on the path type
255 if (pathType === "post") {
257 commentsForm.post_id = id;
259 postForm.comment_id = id;
260 commentsForm.parent_id = id;
264 postRes: await client.getPost(postForm),
265 commentsRes: await client.getComments(commentsForm),
269 componentWillUnmount() {
270 document.removeEventListener("scroll", this.commentScrollDebounced);
272 saveScrollPosition(this.context);
275 async componentDidMount() {
276 if (!this.state.isIsomorphic) {
277 await this.fetchPost();
280 autosize(document.querySelectorAll("textarea"));
282 this.commentScrollDebounced = debounce(this.trackCommentsBoxScrolling, 100);
283 document.addEventListener("scroll", this.commentScrollDebounced);
286 async componentDidUpdate(_lastProps: any) {
287 // Necessary if you are on a post and you click another post (same route)
288 if (_lastProps.location.pathname !== _lastProps.history.location.pathname) {
289 await this.fetchPost();
293 get checkScrollIntoCommentsParam() {
295 new URLSearchParams(this.props.location.search).get("scrollToComments"),
299 scrollIntoCommentSection() {
300 this.state.commentSectionRef?.current?.scrollIntoView();
303 isBottom(el: Element): boolean {
304 return el?.getBoundingClientRect().bottom <= window.innerHeight;
308 * Shows new comments when scrolling to the bottom of the comments div
310 trackCommentsBoxScrolling = () => {
311 const wrappedElement = document.getElementsByClassName("comments")[0];
312 if (wrappedElement && this.isBottom(wrappedElement)) {
314 this.state.commentsRes.state === "success"
315 ? this.state.commentsRes.data.comments.length
318 if (this.state.maxCommentsShown < commentCount) {
320 maxCommentsShown: this.state.maxCommentsShown + commentsShownInterval,
326 get documentTitle(): string {
327 const siteName = this.state.siteRes.site_view.site.name;
328 return this.state.postRes.state === "success"
329 ? `${this.state.postRes.data.post_view.post.name} - ${siteName}`
333 get imageTag(): string | undefined {
334 if (this.state.postRes.state === "success") {
335 const post = this.state.postRes.data.post_view.post;
336 const thumbnail = post.thumbnail_url;
337 const url = post.url;
338 return thumbnail || (url && isImage(url) ? url : undefined);
339 } else return undefined;
343 switch (this.state.postRes.state) {
351 const res = this.state.postRes.data;
353 <div className="row">
354 <main className="col-12 col-md-8 col-lg-9 mb-3">
356 title={this.documentTitle}
357 path={this.context.router.route.match.url}
358 canonicalPath={res.post_view.post.ap_id}
359 image={this.imageTag}
360 description={res.post_view.post.body}
363 post_view={res.post_view}
364 crossPosts={res.cross_posts}
367 moderators={res.moderators}
368 admins={this.state.siteRes.admins}
369 enableDownvotes={enableDownvotes(this.state.siteRes)}
370 enableNsfw={enableNsfw(this.state.siteRes)}
371 allLanguages={this.state.siteRes.all_languages}
372 siteLanguages={this.state.siteRes.discussion_languages}
373 onBlockPerson={this.handleBlockPerson}
374 onPostEdit={this.handlePostEdit}
375 onPostVote={this.handlePostVote}
376 onPostReport={this.handlePostReport}
377 onLockPost={this.handleLockPost}
378 onDeletePost={this.handleDeletePost}
379 onRemovePost={this.handleRemovePost}
380 onSavePost={this.handleSavePost}
381 onPurgePerson={this.handlePurgePerson}
382 onPurgePost={this.handlePurgePost}
383 onBanPerson={this.handleBanPerson}
384 onBanPersonFromCommunity={this.handleBanFromCommunity}
385 onAddModToCommunity={this.handleAddModToCommunity}
386 onAddAdmin={this.handleAddAdmin}
387 onTransferCommunity={this.handleTransferCommunity}
388 onFeaturePost={this.handleFeaturePost}
389 onMarkPostAsRead={this.handleMarkPostAsRead}
391 <div ref={this.state.commentSectionRef} className="mb-2" />
393 {/* Only show the top level comment form if its not a context view */}
394 {!this.state.commentId && (
396 node={res.post_view.post.id}
397 disabled={res.post_view.post.locked}
398 allLanguages={this.state.siteRes.all_languages}
399 siteLanguages={this.state.siteRes.discussion_languages}
400 containerClass="post-comment-container"
401 onUpsertComment={this.handleCreateComment}
402 finished={this.state.finished.get(0)}
405 <div className="d-block d-md-none">
407 className="btn btn-secondary d-inline-block mb-2 me-3"
408 onClick={linkEvent(this, this.handleShowSidebarMobile)}
410 {I18NextService.i18n.t("sidebar")}{" "}
413 this.state.showSidebarMobile
417 classes="icon-inline"
420 {this.state.showSidebarMobile && this.sidebar()}
423 {this.state.commentViewType === CommentViewType.Tree &&
425 {this.state.commentViewType === CommentViewType.Flat &&
428 <aside className="d-none d-md-block col-md-4 col-lg-3">
438 return <div className="post container-lg">{this.renderPostRes()}</div>;
443 this.state.postRes.state === "success"
444 ? this.state.postRes.data.post_view.post.id
450 className="btn-group btn-group-toggle flex-wrap me-3 mb-2"
454 id={`${radioId}-hot`}
456 className="btn-check"
458 checked={this.state.commentSort === "Hot"}
459 onChange={linkEvent(this, this.handleCommentSortChange)}
462 htmlFor={`${radioId}-hot`}
463 className={classNames("btn btn-outline-secondary pointer", {
464 active: this.state.commentSort === "Hot",
467 {I18NextService.i18n.t("hot")}
470 id={`${radioId}-top`}
472 className="btn-check"
474 checked={this.state.commentSort === "Top"}
475 onChange={linkEvent(this, this.handleCommentSortChange)}
478 htmlFor={`${radioId}-top`}
479 className={classNames("btn btn-outline-secondary pointer", {
480 active: this.state.commentSort === "Top",
483 {I18NextService.i18n.t("top")}
486 id={`${radioId}-controversial`}
488 className="btn-check"
489 value={"Controversial"}
490 checked={this.state.commentSort === "Controversial"}
491 onChange={linkEvent(this, this.handleCommentSortChange)}
494 htmlFor={`${radioId}-controversial`}
495 className={classNames("btn btn-outline-secondary pointer", {
496 active: this.state.commentSort === "Controversial",
499 {I18NextService.i18n.t("controversial")}
502 id={`${radioId}-new`}
504 className="btn-check"
506 checked={this.state.commentSort === "New"}
507 onChange={linkEvent(this, this.handleCommentSortChange)}
510 htmlFor={`${radioId}-new`}
511 className={classNames("btn btn-outline-secondary pointer", {
512 active: this.state.commentSort === "New",
515 {I18NextService.i18n.t("new")}
518 id={`${radioId}-old`}
520 className="btn-check"
522 checked={this.state.commentSort === "Old"}
523 onChange={linkEvent(this, this.handleCommentSortChange)}
526 htmlFor={`${radioId}-old`}
527 className={classNames("btn btn-outline-secondary pointer", {
528 active: this.state.commentSort === "Old",
531 {I18NextService.i18n.t("old")}
534 <div className="btn-group btn-group-toggle flex-wrap mb-2" role="group">
536 id={`${radioId}-chat`}
538 className="btn-check"
539 value={CommentViewType.Flat}
540 checked={this.state.commentViewType === CommentViewType.Flat}
541 onChange={linkEvent(this, this.handleCommentViewTypeChange)}
544 htmlFor={`${radioId}-chat`}
545 className={classNames("btn btn-outline-secondary pointer", {
546 active: this.state.commentViewType === CommentViewType.Flat,
549 {I18NextService.i18n.t("chat")}
557 // These are already sorted by new
558 const commentsRes = this.state.commentsRes;
559 const postRes = this.state.postRes;
561 if (commentsRes.state === "success" && postRes.state === "success") {
565 nodes={commentsToFlatNodes(commentsRes.data.comments)}
566 viewType={this.state.commentViewType}
567 maxCommentsShown={this.state.maxCommentsShown}
569 locked={postRes.data.post_view.post.locked}
570 moderators={postRes.data.moderators}
571 admins={this.state.siteRes.admins}
572 enableDownvotes={enableDownvotes(this.state.siteRes)}
574 finished={this.state.finished}
575 allLanguages={this.state.siteRes.all_languages}
576 siteLanguages={this.state.siteRes.discussion_languages}
577 onSaveComment={this.handleSaveComment}
578 onBlockPerson={this.handleBlockPerson}
579 onDeleteComment={this.handleDeleteComment}
580 onRemoveComment={this.handleRemoveComment}
581 onCommentVote={this.handleCommentVote}
582 onCommentReport={this.handleCommentReport}
583 onDistinguishComment={this.handleDistinguishComment}
584 onAddModToCommunity={this.handleAddModToCommunity}
585 onAddAdmin={this.handleAddAdmin}
586 onTransferCommunity={this.handleTransferCommunity}
587 onFetchChildren={this.handleFetchChildren}
588 onPurgeComment={this.handlePurgeComment}
589 onPurgePerson={this.handlePurgePerson}
590 onCommentReplyRead={this.handleCommentReplyRead}
591 onPersonMentionRead={this.handlePersonMentionRead}
592 onBanPersonFromCommunity={this.handleBanFromCommunity}
593 onBanPerson={this.handleBanPerson}
594 onCreateComment={this.handleCreateComment}
595 onEditComment={this.handleEditComment}
603 const res = this.state.postRes;
604 if (res.state === "success") {
607 community_view={res.data.community_view}
608 moderators={res.data.moderators}
609 admins={this.state.siteRes.admins}
610 enableNsfw={enableNsfw(this.state.siteRes)}
612 allLanguages={this.state.siteRes.all_languages}
613 siteLanguages={this.state.siteRes.discussion_languages}
614 onDeleteCommunity={this.handleDeleteCommunityClick}
615 onLeaveModTeam={this.handleAddModToCommunity}
616 onFollowCommunity={this.handleFollow}
617 onRemoveCommunity={this.handleModRemoveCommunity}
618 onPurgeCommunity={this.handlePurgeCommunity}
619 onBlockCommunity={this.handleBlockCommunity}
620 onEditCommunity={this.handleEditCommunity}
627 const res = this.state.postRes;
628 const firstComment = this.commentTree().at(0)?.comment_view.comment;
629 const depth = getDepthFromComment(firstComment);
630 const showContextButton = depth ? depth > 0 : false;
633 res.state === "success" && (
635 {!!this.state.commentId && (
638 className="ps-0 d-block btn btn-link text-muted"
639 onClick={linkEvent(this, this.handleViewPost)}
641 {I18NextService.i18n.t("view_all_comments")} âž”
643 {showContextButton && (
645 className="ps-0 d-block btn btn-link text-muted"
646 onClick={linkEvent(this, this.handleViewContext)}
648 {I18NextService.i18n.t("show_context")} âž”
654 nodes={this.commentTree()}
655 viewType={this.state.commentViewType}
656 maxCommentsShown={this.state.maxCommentsShown}
657 locked={res.data.post_view.post.locked}
658 moderators={res.data.moderators}
659 admins={this.state.siteRes.admins}
660 enableDownvotes={enableDownvotes(this.state.siteRes)}
661 finished={this.state.finished}
662 allLanguages={this.state.siteRes.all_languages}
663 siteLanguages={this.state.siteRes.discussion_languages}
664 onSaveComment={this.handleSaveComment}
665 onBlockPerson={this.handleBlockPerson}
666 onDeleteComment={this.handleDeleteComment}
667 onRemoveComment={this.handleRemoveComment}
668 onCommentVote={this.handleCommentVote}
669 onCommentReport={this.handleCommentReport}
670 onDistinguishComment={this.handleDistinguishComment}
671 onAddModToCommunity={this.handleAddModToCommunity}
672 onAddAdmin={this.handleAddAdmin}
673 onTransferCommunity={this.handleTransferCommunity}
674 onFetchChildren={this.handleFetchChildren}
675 onPurgeComment={this.handlePurgeComment}
676 onPurgePerson={this.handlePurgePerson}
677 onCommentReplyRead={this.handleCommentReplyRead}
678 onPersonMentionRead={this.handlePersonMentionRead}
679 onBanPersonFromCommunity={this.handleBanFromCommunity}
680 onBanPerson={this.handleBanPerson}
681 onCreateComment={this.handleCreateComment}
682 onEditComment={this.handleEditComment}
689 commentTree(): CommentNodeI[] {
690 if (this.state.commentsRes.state === "success") {
691 return buildCommentsTree(
692 this.state.commentsRes.data.comments,
693 !!this.state.commentId,
700 async handleCommentSortChange(i: Post, event: any) {
702 commentSort: event.target.value as CommentSortType,
703 commentViewType: CommentViewType.Tree,
704 commentsRes: { state: "loading" },
705 postRes: { state: "loading" },
710 handleCommentViewTypeChange(i: Post, event: any) {
712 commentViewType: Number(event.target.value),
717 handleShowSidebarMobile(i: Post) {
718 i.setState({ showSidebarMobile: !i.state.showSidebarMobile });
721 handleViewPost(i: Post) {
722 if (i.state.postRes.state === "success") {
723 const id = i.state.postRes.data.post_view.post.id;
724 i.context.router.history.push(`/post/${id}`);
728 handleViewContext(i: Post) {
729 if (i.state.commentsRes.state === "success") {
730 const parentId = getCommentParentId(
731 i.state.commentsRes.data.comments.at(0)?.comment,
734 i.context.router.history.push(`/comment/${parentId}`);
739 async handleDeleteCommunityClick(form: DeleteCommunity) {
740 const deleteCommunityRes = await HttpService.client.deleteCommunity(form);
741 this.updateCommunity(deleteCommunityRes);
744 async handleAddModToCommunity(form: AddModToCommunity) {
745 const addModRes = await HttpService.client.addModToCommunity(form);
746 this.updateModerators(addModRes);
749 async handleFollow(form: FollowCommunity) {
750 const followCommunityRes = await HttpService.client.followCommunity(form);
751 this.updateCommunity(followCommunityRes);
754 if (followCommunityRes.state === "success") {
755 const communityId = followCommunityRes.data.community_view.community.id;
756 const mui = UserService.Instance.myUserInfo;
758 mui.follows = mui.follows.filter(i => i.community.id !== communityId);
763 async handlePurgeCommunity(form: PurgeCommunity) {
764 const purgeCommunityRes = await HttpService.client.purgeCommunity(form);
765 this.purgeItem(purgeCommunityRes);
768 async handlePurgePerson(form: PurgePerson) {
769 const purgePersonRes = await HttpService.client.purgePerson(form);
770 this.purgeItem(purgePersonRes);
773 async handlePurgeComment(form: PurgeComment) {
774 const purgeCommentRes = await HttpService.client.purgeComment(form);
775 this.purgeItem(purgeCommentRes);
778 async handlePurgePost(form: PurgePost) {
779 const purgeRes = await HttpService.client.purgePost(form);
780 this.purgeItem(purgeRes);
783 async handleBlockCommunity(form: BlockCommunity) {
784 const blockCommunityRes = await HttpService.client.blockCommunity(form);
785 if (blockCommunityRes.state === "success") {
786 updateCommunityBlock(blockCommunityRes.data);
788 if (s.postRes.state === "success") {
789 s.postRes.data.community_view.blocked =
790 blockCommunityRes.data.blocked;
796 async handleBlockPerson(form: BlockPerson) {
797 const blockPersonRes = await HttpService.client.blockPerson(form);
798 if (blockPersonRes.state === "success") {
799 updatePersonBlock(blockPersonRes.data);
803 async handleModRemoveCommunity(form: RemoveCommunity) {
804 const removeCommunityRes = await HttpService.client.removeCommunity(form);
805 this.updateCommunity(removeCommunityRes);
808 async handleEditCommunity(form: EditCommunity) {
809 const res = await HttpService.client.editCommunity(form);
810 this.updateCommunity(res);
815 async handleCreateComment(form: CreateComment) {
816 const createCommentRes = await HttpService.client.createComment(form);
817 this.createAndUpdateComments(createCommentRes);
819 return createCommentRes;
822 async handleEditComment(form: EditComment) {
823 const editCommentRes = await HttpService.client.editComment(form);
824 this.findAndUpdateComment(editCommentRes);
826 return editCommentRes;
829 async handleDeleteComment(form: DeleteComment) {
830 const deleteCommentRes = await HttpService.client.deleteComment(form);
831 this.findAndUpdateComment(deleteCommentRes);
834 async handleDeletePost(form: DeletePost) {
835 const deleteRes = await HttpService.client.deletePost(form);
836 this.updatePost(deleteRes);
839 async handleRemovePost(form: RemovePost) {
840 const removeRes = await HttpService.client.removePost(form);
841 this.updatePost(removeRes);
844 async handleRemoveComment(form: RemoveComment) {
845 const removeCommentRes = await HttpService.client.removeComment(form);
846 this.findAndUpdateComment(removeCommentRes);
849 async handleSaveComment(form: SaveComment) {
850 const saveCommentRes = await HttpService.client.saveComment(form);
851 this.findAndUpdateComment(saveCommentRes);
854 async handleSavePost(form: SavePost) {
855 const saveRes = await HttpService.client.savePost(form);
856 this.updatePost(saveRes);
859 async handleFeaturePost(form: FeaturePost) {
860 const featureRes = await HttpService.client.featurePost(form);
861 this.updatePost(featureRes);
864 async handleCommentVote(form: CreateCommentLike) {
865 const voteRes = await HttpService.client.likeComment(form);
866 this.findAndUpdateComment(voteRes);
869 async handlePostVote(form: CreatePostLike) {
870 const voteRes = await HttpService.client.likePost(form);
871 this.updatePost(voteRes);
874 async handlePostEdit(form: EditPost) {
875 const res = await HttpService.client.editPost(form);
876 this.updatePost(res);
879 async handleCommentReport(form: CreateCommentReport) {
880 const reportRes = await HttpService.client.createCommentReport(form);
881 if (reportRes.state === "success") {
882 toast(I18NextService.i18n.t("report_created"));
886 async handlePostReport(form: CreatePostReport) {
887 const reportRes = await HttpService.client.createPostReport(form);
888 if (reportRes.state === "success") {
889 toast(I18NextService.i18n.t("report_created"));
893 async handleLockPost(form: LockPost) {
894 const lockRes = await HttpService.client.lockPost(form);
895 this.updatePost(lockRes);
898 async handleDistinguishComment(form: DistinguishComment) {
899 const distinguishRes = await HttpService.client.distinguishComment(form);
900 this.findAndUpdateComment(distinguishRes);
903 async handleAddAdmin(form: AddAdmin) {
904 const addAdminRes = await HttpService.client.addAdmin(form);
906 if (addAdminRes.state === "success") {
907 this.setState(s => ((s.siteRes.admins = addAdminRes.data.admins), s));
911 async handleTransferCommunity(form: TransferCommunity) {
912 const transferCommunityRes = await HttpService.client.transferCommunity(
915 this.updateCommunityFull(transferCommunityRes);
918 async handleFetchChildren(form: GetComments) {
919 const moreCommentsRes = await HttpService.client.getComments(form);
921 this.state.commentsRes.state === "success" &&
922 moreCommentsRes.state === "success"
924 const newComments = moreCommentsRes.data.comments;
925 // Remove the first comment, since it is the parent
927 const newRes = this.state.commentsRes;
928 newRes.data.comments.push(...newComments);
929 this.setState({ commentsRes: newRes });
933 async handleCommentReplyRead(form: MarkCommentReplyAsRead) {
934 const readRes = await HttpService.client.markCommentReplyAsRead(form);
935 this.findAndUpdateCommentReply(readRes);
938 async handlePersonMentionRead(form: MarkPersonMentionAsRead) {
939 // TODO not sure what to do here. Maybe it is actually optional, because post doesn't need it.
940 await HttpService.client.markPersonMentionAsRead(form);
943 async handleMarkPostAsRead(form: MarkPostAsRead) {
944 const res = await HttpService.client.markPostAsRead(form);
945 this.updatePost(res);
948 async handleBanFromCommunity(form: BanFromCommunity) {
949 const banRes = await HttpService.client.banFromCommunity(form);
950 this.updateBan(banRes);
953 async handleBanPerson(form: BanPerson) {
954 const banRes = await HttpService.client.banPerson(form);
955 this.updateBan(banRes);
958 updateBanFromCommunity(banRes: RequestState<BanFromCommunityResponse>) {
959 // Maybe not necessary
960 if (banRes.state === "success") {
963 s.postRes.state === "success" &&
964 s.postRes.data.post_view.creator.id ===
965 banRes.data.person_view.person.id
967 s.postRes.data.post_view.creator_banned_from_community =
970 if (s.commentsRes.state === "success") {
971 s.commentsRes.data.comments
972 .filter(c => c.creator.id === banRes.data.person_view.person.id)
974 c => (c.creator_banned_from_community = banRes.data.banned),
982 updateBan(banRes: RequestState<BanPersonResponse>) {
983 // Maybe not necessary
984 if (banRes.state === "success") {
987 s.postRes.state === "success" &&
988 s.postRes.data.post_view.creator.id ===
989 banRes.data.person_view.person.id
991 s.postRes.data.post_view.creator.banned = banRes.data.banned;
993 if (s.commentsRes.state === "success") {
994 s.commentsRes.data.comments
995 .filter(c => c.creator.id === banRes.data.person_view.person.id)
996 .forEach(c => (c.creator.banned = banRes.data.banned));
1003 updateCommunity(communityRes: RequestState<CommunityResponse>) {
1004 this.setState(s => {
1005 if (s.postRes.state === "success" && communityRes.state === "success") {
1006 s.postRes.data.community_view = communityRes.data.community_view;
1012 updateCommunityFull(res: RequestState<GetCommunityResponse>) {
1013 this.setState(s => {
1014 if (s.postRes.state === "success" && res.state === "success") {
1015 s.postRes.data.community_view = res.data.community_view;
1016 s.postRes.data.moderators = res.data.moderators;
1022 updatePost(post: RequestState<PostResponse>) {
1023 this.setState(s => {
1024 if (s.postRes.state === "success" && post.state === "success") {
1025 s.postRes.data.post_view = post.data.post_view;
1031 purgeItem(purgeRes: RequestState<PurgeItemResponse>) {
1032 if (purgeRes.state === "success") {
1033 toast(I18NextService.i18n.t("purge_success"));
1034 this.context.router.history.push(`/`);
1038 createAndUpdateComments(res: RequestState<CommentResponse>) {
1039 this.setState(s => {
1040 if (s.commentsRes.state === "success" && res.state === "success") {
1041 // The comment must be inserted not at the very beginning of the list,
1042 // because the buildCommentsTree needs a correct path ordering.
1043 // It should be inserted right after its parent is found
1044 const comments = s.commentsRes.data.comments;
1045 const newComment = res.data.comment_view;
1046 const newCommentParentId = getCommentParentId(newComment.comment);
1048 const foundCommentParentIndex = comments.findIndex(
1049 c => c.comment.id === newCommentParentId,
1052 comments.splice(foundCommentParentIndex + 1, 0, newComment);
1054 // Set finished for the parent
1055 s.finished.set(newCommentParentId ?? 0, true);
1061 findAndUpdateComment(res: RequestState<CommentResponse>) {
1062 this.setState(s => {
1063 if (s.commentsRes.state === "success" && res.state === "success") {
1064 s.commentsRes.data.comments = editComment(
1065 res.data.comment_view,
1066 s.commentsRes.data.comments,
1068 s.finished.set(res.data.comment_view.comment.id, true);
1074 findAndUpdateCommentReply(res: RequestState<CommentReplyResponse>) {
1075 this.setState(s => {
1076 if (s.commentsRes.state === "success" && res.state === "success") {
1077 s.commentsRes.data.comments = editWith(
1078 res.data.comment_reply_view,
1079 s.commentsRes.data.comments,
1086 updateModerators(res: RequestState<AddModToCommunityResponse>) {
1087 // Update the moderators
1088 this.setState(s => {
1089 if (s.postRes.state === "success" && res.state === "success") {
1090 s.postRes.data.moderators = res.data.moderators;