19 restoreScrollPosition,
21 } from "@utils/browser";
22 import { debounce } from "@utils/helpers";
23 import { isImage } from "@utils/media";
24 import { RouteDataResponse } from "@utils/types";
25 import autosize from "autosize";
26 import { Component, RefObject, createRef, linkEvent } from "inferno";
30 AddModToCommunityResponse,
32 BanFromCommunityResponse,
63 MarkCommentReplyAsRead,
64 MarkPersonMentionAsRead,
77 } from "lemmy-js-client";
78 import { commentTreeMaxDepth } from "../../config";
83 } from "../../interfaces";
84 import { FirstLoadService, I18NextService, UserService } from "../../services";
85 import { HttpService, RequestState } from "../../services/HttpService";
86 import { setupTippy } from "../../tippy";
87 import { toast } from "../../toast";
88 import { CommentForm } from "../comment/comment-form";
89 import { CommentNodes } from "../comment/comment-nodes";
90 import { HtmlTags } from "../common/html-tags";
91 import { Icon, Spinner } from "../common/icon";
92 import { Sidebar } from "../community/sidebar";
93 import { PostListing } from "./post-listing";
95 const commentsShownInterval = 15;
97 type PostData = RouteDataResponse<{
98 postRes: GetPostResponse;
99 commentsRes: GetCommentsResponse;
102 interface PostState {
105 postRes: RequestState<GetPostResponse>;
106 commentsRes: RequestState<GetCommentsResponse>;
107 commentSort: CommentSortType;
108 commentViewType: CommentViewType;
110 siteRes: GetSiteResponse;
111 commentSectionRef?: RefObject<HTMLDivElement>;
112 showSidebarMobile: boolean;
113 maxCommentsShown: number;
114 finished: Map<CommentId, boolean | undefined>;
115 isIsomorphic: boolean;
118 export class Post extends Component<any, PostState> {
119 private isoData = setIsoData<PostData>(this.context);
120 private commentScrollDebounced: () => void;
122 postRes: { state: "empty" },
123 commentsRes: { state: "empty" },
124 postId: getIdFromProps(this.props),
125 commentId: getCommentIdFromProps(this.props),
127 commentViewType: CommentViewType.Tree,
129 siteRes: this.isoData.site_res,
130 showSidebarMobile: false,
131 maxCommentsShown: commentsShownInterval,
136 constructor(props: any, context: any) {
137 super(props, context);
139 this.handleDeleteCommunityClick =
140 this.handleDeleteCommunityClick.bind(this);
141 this.handleEditCommunity = this.handleEditCommunity.bind(this);
142 this.handleFollow = this.handleFollow.bind(this);
143 this.handleModRemoveCommunity = this.handleModRemoveCommunity.bind(this);
144 this.handleCreateComment = this.handleCreateComment.bind(this);
145 this.handleEditComment = this.handleEditComment.bind(this);
146 this.handleSaveComment = this.handleSaveComment.bind(this);
147 this.handleBlockCommunity = this.handleBlockCommunity.bind(this);
148 this.handleBlockPerson = this.handleBlockPerson.bind(this);
149 this.handleDeleteComment = this.handleDeleteComment.bind(this);
150 this.handleRemoveComment = this.handleRemoveComment.bind(this);
151 this.handleCommentVote = this.handleCommentVote.bind(this);
152 this.handleAddModToCommunity = this.handleAddModToCommunity.bind(this);
153 this.handleAddAdmin = this.handleAddAdmin.bind(this);
154 this.handlePurgePerson = this.handlePurgePerson.bind(this);
155 this.handlePurgeComment = this.handlePurgeComment.bind(this);
156 this.handleCommentReport = this.handleCommentReport.bind(this);
157 this.handleDistinguishComment = this.handleDistinguishComment.bind(this);
158 this.handleTransferCommunity = this.handleTransferCommunity.bind(this);
159 this.handleFetchChildren = this.handleFetchChildren.bind(this);
160 this.handleCommentReplyRead = this.handleCommentReplyRead.bind(this);
161 this.handlePersonMentionRead = this.handlePersonMentionRead.bind(this);
162 this.handleBanFromCommunity = this.handleBanFromCommunity.bind(this);
163 this.handleBanPerson = this.handleBanPerson.bind(this);
164 this.handlePostEdit = this.handlePostEdit.bind(this);
165 this.handlePostVote = this.handlePostVote.bind(this);
166 this.handlePostReport = this.handlePostReport.bind(this);
167 this.handleLockPost = this.handleLockPost.bind(this);
168 this.handleDeletePost = this.handleDeletePost.bind(this);
169 this.handleRemovePost = this.handleRemovePost.bind(this);
170 this.handleSavePost = this.handleSavePost.bind(this);
171 this.handlePurgePost = this.handlePurgePost.bind(this);
172 this.handleFeaturePost = this.handleFeaturePost.bind(this);
174 this.state = { ...this.state, commentSectionRef: createRef() };
176 // Only fetch the data if coming from another route
177 if (FirstLoadService.isFirstLoad) {
178 const { commentsRes, postRes } = this.isoData.routeData;
188 if (this.checkScrollIntoCommentsParam) {
189 this.scrollIntoCommentSection();
197 postRes: { state: "loading" },
198 commentsRes: { state: "loading" },
201 const auth = myAuth();
204 postRes: await HttpService.client.getPost({
205 id: this.state.postId,
206 comment_id: this.state.commentId,
209 commentsRes: await HttpService.client.getComments({
210 post_id: this.state.postId,
211 parent_id: this.state.commentId,
212 max_depth: commentTreeMaxDepth,
213 sort: this.state.commentSort,
222 if (!this.state.commentId) restoreScrollPosition(this.context);
224 if (this.checkScrollIntoCommentsParam) {
225 this.scrollIntoCommentSection();
229 static async fetchInitialData({
233 }: InitialFetchRequest): Promise<PostData> {
234 const pathSplit = path.split("/");
236 const pathType = pathSplit.at(1);
237 const id = pathSplit.at(2) ? Number(pathSplit.at(2)) : undefined;
239 const postForm: GetPost = {
243 const commentsForm: GetComments = {
244 max_depth: commentTreeMaxDepth,
251 // Set the correct id based on the path type
252 if (pathType === "post") {
254 commentsForm.post_id = id;
256 postForm.comment_id = id;
257 commentsForm.parent_id = id;
261 postRes: await client.getPost(postForm),
262 commentsRes: await client.getComments(commentsForm),
266 componentWillUnmount() {
267 document.removeEventListener("scroll", this.commentScrollDebounced);
269 saveScrollPosition(this.context);
272 async componentDidMount() {
273 if (!this.state.isIsomorphic) {
274 await this.fetchPost();
277 autosize(document.querySelectorAll("textarea"));
279 this.commentScrollDebounced = debounce(this.trackCommentsBoxScrolling, 100);
280 document.addEventListener("scroll", this.commentScrollDebounced);
283 async componentDidUpdate(_lastProps: any) {
284 // Necessary if you are on a post and you click another post (same route)
285 if (_lastProps.location.pathname !== _lastProps.history.location.pathname) {
286 await this.fetchPost();
290 get checkScrollIntoCommentsParam() {
292 new URLSearchParams(this.props.location.search).get("scrollToComments")
296 scrollIntoCommentSection() {
297 this.state.commentSectionRef?.current?.scrollIntoView();
300 isBottom(el: Element): boolean {
301 return el?.getBoundingClientRect().bottom <= window.innerHeight;
305 * Shows new comments when scrolling to the bottom of the comments div
307 trackCommentsBoxScrolling = () => {
308 const wrappedElement = document.getElementsByClassName("comments")[0];
309 if (wrappedElement && this.isBottom(wrappedElement)) {
311 this.state.commentsRes.state == "success"
312 ? this.state.commentsRes.data.comments.length
315 if (this.state.maxCommentsShown < commentCount) {
317 maxCommentsShown: this.state.maxCommentsShown + commentsShownInterval,
323 get documentTitle(): string {
324 const siteName = this.state.siteRes.site_view.site.name;
325 return this.state.postRes.state == "success"
326 ? `${this.state.postRes.data.post_view.post.name} - ${siteName}`
330 get imageTag(): string | undefined {
331 if (this.state.postRes.state == "success") {
332 const post = this.state.postRes.data.post_view.post;
333 const thumbnail = post.thumbnail_url;
334 const url = post.url;
335 return thumbnail || (url && isImage(url) ? url : undefined);
336 } else return undefined;
340 switch (this.state.postRes.state) {
348 const res = this.state.postRes.data;
350 <div className="row">
351 <div className="col-12 col-md-8 mb-3">
353 title={this.documentTitle}
354 path={this.context.router.route.match.url}
355 image={this.imageTag}
356 description={res.post_view.post.body}
359 post_view={res.post_view}
360 crossPosts={res.cross_posts}
363 moderators={res.moderators}
364 admins={this.state.siteRes.admins}
365 enableDownvotes={enableDownvotes(this.state.siteRes)}
366 enableNsfw={enableNsfw(this.state.siteRes)}
367 allLanguages={this.state.siteRes.all_languages}
368 siteLanguages={this.state.siteRes.discussion_languages}
369 onBlockPerson={this.handleBlockPerson}
370 onPostEdit={this.handlePostEdit}
371 onPostVote={this.handlePostVote}
372 onPostReport={this.handlePostReport}
373 onLockPost={this.handleLockPost}
374 onDeletePost={this.handleDeletePost}
375 onRemovePost={this.handleRemovePost}
376 onSavePost={this.handleSavePost}
377 onPurgePerson={this.handlePurgePerson}
378 onPurgePost={this.handlePurgePost}
379 onBanPerson={this.handleBanPerson}
380 onBanPersonFromCommunity={this.handleBanFromCommunity}
381 onAddModToCommunity={this.handleAddModToCommunity}
382 onAddAdmin={this.handleAddAdmin}
383 onTransferCommunity={this.handleTransferCommunity}
384 onFeaturePost={this.handleFeaturePost}
386 <div ref={this.state.commentSectionRef} className="mb-2" />
388 node={res.post_view.post.id}
389 disabled={res.post_view.post.locked}
390 allLanguages={this.state.siteRes.all_languages}
391 siteLanguages={this.state.siteRes.discussion_languages}
392 containerClass="post-comment-container"
393 onUpsertComment={this.handleCreateComment}
394 finished={this.state.finished.get(0)}
396 <div className="d-block d-md-none">
398 className="btn btn-secondary d-inline-block mb-2 me-3"
399 onClick={linkEvent(this, this.handleShowSidebarMobile)}
401 {I18NextService.i18n.t("sidebar")}{" "}
404 this.state.showSidebarMobile
408 classes="icon-inline"
411 {this.state.showSidebarMobile && this.sidebar()}
414 {this.state.commentViewType == CommentViewType.Tree &&
416 {this.state.commentViewType == CommentViewType.Flat &&
419 <div className="d-none d-md-block col-md-4">{this.sidebar()}</div>
427 return <div className="post container-lg">{this.renderPostRes()}</div>;
433 <div className="btn-group btn-group-toggle flex-wrap me-3 mb-2">
435 className={`btn btn-outline-secondary pointer ${
436 this.state.commentSort === "Hot" && "active"
439 {I18NextService.i18n.t("hot")}
442 className="btn-check"
444 checked={this.state.commentSort === "Hot"}
445 onChange={linkEvent(this, this.handleCommentSortChange)}
449 className={`btn btn-outline-secondary pointer ${
450 this.state.commentSort === "Top" && "active"
453 {I18NextService.i18n.t("top")}
456 className="btn-check"
458 checked={this.state.commentSort === "Top"}
459 onChange={linkEvent(this, this.handleCommentSortChange)}
463 className={`btn btn-outline-secondary pointer ${
464 this.state.commentSort === "New" && "active"
467 {I18NextService.i18n.t("new")}
470 className="btn-check"
472 checked={this.state.commentSort === "New"}
473 onChange={linkEvent(this, this.handleCommentSortChange)}
477 className={`btn btn-outline-secondary pointer ${
478 this.state.commentSort === "Old" && "active"
481 {I18NextService.i18n.t("old")}
484 className="btn-check"
486 checked={this.state.commentSort === "Old"}
487 onChange={linkEvent(this, this.handleCommentSortChange)}
491 <div className="btn-group btn-group-toggle flex-wrap mb-2">
493 className={`btn btn-outline-secondary pointer ${
494 this.state.commentViewType === CommentViewType.Flat && "active"
497 {I18NextService.i18n.t("chat")}
500 className="btn-check"
501 value={CommentViewType.Flat}
502 checked={this.state.commentViewType === CommentViewType.Flat}
503 onChange={linkEvent(this, this.handleCommentViewTypeChange)}
512 // These are already sorted by new
513 const commentsRes = this.state.commentsRes;
514 const postRes = this.state.postRes;
516 if (commentsRes.state == "success" && postRes.state == "success") {
520 nodes={commentsToFlatNodes(commentsRes.data.comments)}
521 viewType={this.state.commentViewType}
522 maxCommentsShown={this.state.maxCommentsShown}
524 locked={postRes.data.post_view.post.locked}
525 moderators={postRes.data.moderators}
526 admins={this.state.siteRes.admins}
527 enableDownvotes={enableDownvotes(this.state.siteRes)}
529 finished={this.state.finished}
530 allLanguages={this.state.siteRes.all_languages}
531 siteLanguages={this.state.siteRes.discussion_languages}
532 onSaveComment={this.handleSaveComment}
533 onBlockPerson={this.handleBlockPerson}
534 onDeleteComment={this.handleDeleteComment}
535 onRemoveComment={this.handleRemoveComment}
536 onCommentVote={this.handleCommentVote}
537 onCommentReport={this.handleCommentReport}
538 onDistinguishComment={this.handleDistinguishComment}
539 onAddModToCommunity={this.handleAddModToCommunity}
540 onAddAdmin={this.handleAddAdmin}
541 onTransferCommunity={this.handleTransferCommunity}
542 onFetchChildren={this.handleFetchChildren}
543 onPurgeComment={this.handlePurgeComment}
544 onPurgePerson={this.handlePurgePerson}
545 onCommentReplyRead={this.handleCommentReplyRead}
546 onPersonMentionRead={this.handlePersonMentionRead}
547 onBanPersonFromCommunity={this.handleBanFromCommunity}
548 onBanPerson={this.handleBanPerson}
549 onCreateComment={this.handleCreateComment}
550 onEditComment={this.handleEditComment}
558 const res = this.state.postRes;
559 if (res.state === "success") {
562 community_view={res.data.community_view}
563 moderators={res.data.moderators}
564 admins={this.state.siteRes.admins}
565 enableNsfw={enableNsfw(this.state.siteRes)}
567 allLanguages={this.state.siteRes.all_languages}
568 siteLanguages={this.state.siteRes.discussion_languages}
569 onDeleteCommunity={this.handleDeleteCommunityClick}
570 onLeaveModTeam={this.handleAddModToCommunity}
571 onFollowCommunity={this.handleFollow}
572 onRemoveCommunity={this.handleModRemoveCommunity}
573 onPurgeCommunity={this.handlePurgeCommunity}
574 onBlockCommunity={this.handleBlockCommunity}
575 onEditCommunity={this.handleEditCommunity}
582 const res = this.state.postRes;
583 const firstComment = this.commentTree().at(0)?.comment_view.comment;
584 const depth = getDepthFromComment(firstComment);
585 const showContextButton = depth ? depth > 0 : false;
588 res.state == "success" && (
590 {!!this.state.commentId && (
593 className="ps-0 d-block btn btn-link text-muted"
594 onClick={linkEvent(this, this.handleViewPost)}
596 {I18NextService.i18n.t("view_all_comments")} âž”
598 {showContextButton && (
600 className="ps-0 d-block btn btn-link text-muted"
601 onClick={linkEvent(this, this.handleViewContext)}
603 {I18NextService.i18n.t("show_context")} âž”
609 nodes={this.commentTree()}
610 viewType={this.state.commentViewType}
611 maxCommentsShown={this.state.maxCommentsShown}
612 locked={res.data.post_view.post.locked}
613 moderators={res.data.moderators}
614 admins={this.state.siteRes.admins}
615 enableDownvotes={enableDownvotes(this.state.siteRes)}
616 finished={this.state.finished}
617 allLanguages={this.state.siteRes.all_languages}
618 siteLanguages={this.state.siteRes.discussion_languages}
619 onSaveComment={this.handleSaveComment}
620 onBlockPerson={this.handleBlockPerson}
621 onDeleteComment={this.handleDeleteComment}
622 onRemoveComment={this.handleRemoveComment}
623 onCommentVote={this.handleCommentVote}
624 onCommentReport={this.handleCommentReport}
625 onDistinguishComment={this.handleDistinguishComment}
626 onAddModToCommunity={this.handleAddModToCommunity}
627 onAddAdmin={this.handleAddAdmin}
628 onTransferCommunity={this.handleTransferCommunity}
629 onFetchChildren={this.handleFetchChildren}
630 onPurgeComment={this.handlePurgeComment}
631 onPurgePerson={this.handlePurgePerson}
632 onCommentReplyRead={this.handleCommentReplyRead}
633 onPersonMentionRead={this.handlePersonMentionRead}
634 onBanPersonFromCommunity={this.handleBanFromCommunity}
635 onBanPerson={this.handleBanPerson}
636 onCreateComment={this.handleCreateComment}
637 onEditComment={this.handleEditComment}
644 commentTree(): CommentNodeI[] {
645 if (this.state.commentsRes.state == "success") {
646 return buildCommentsTree(
647 this.state.commentsRes.data.comments,
648 !!this.state.commentId
655 async handleCommentSortChange(i: Post, event: any) {
657 commentSort: event.target.value as CommentSortType,
658 commentViewType: CommentViewType.Tree,
659 commentsRes: { state: "loading" },
660 postRes: { state: "loading" },
665 handleCommentViewTypeChange(i: Post, event: any) {
667 commentViewType: Number(event.target.value),
672 handleShowSidebarMobile(i: Post) {
673 i.setState({ showSidebarMobile: !i.state.showSidebarMobile });
676 handleViewPost(i: Post) {
677 if (i.state.postRes.state == "success") {
678 const id = i.state.postRes.data.post_view.post.id;
679 i.context.router.history.push(`/post/${id}`);
683 handleViewContext(i: Post) {
684 if (i.state.commentsRes.state == "success") {
685 const parentId = getCommentParentId(
686 i.state.commentsRes.data.comments.at(0)?.comment
689 i.context.router.history.push(`/comment/${parentId}`);
694 async handleDeleteCommunityClick(form: DeleteCommunity) {
695 const deleteCommunityRes = await HttpService.client.deleteCommunity(form);
696 this.updateCommunity(deleteCommunityRes);
699 async handleAddModToCommunity(form: AddModToCommunity) {
700 const addModRes = await HttpService.client.addModToCommunity(form);
701 this.updateModerators(addModRes);
704 async handleFollow(form: FollowCommunity) {
705 const followCommunityRes = await HttpService.client.followCommunity(form);
706 this.updateCommunity(followCommunityRes);
709 if (followCommunityRes.state === "success") {
710 const communityId = followCommunityRes.data.community_view.community.id;
711 const mui = UserService.Instance.myUserInfo;
713 mui.follows = mui.follows.filter(i => i.community.id != communityId);
718 async handlePurgeCommunity(form: PurgeCommunity) {
719 const purgeCommunityRes = await HttpService.client.purgeCommunity(form);
720 this.purgeItem(purgeCommunityRes);
723 async handlePurgePerson(form: PurgePerson) {
724 const purgePersonRes = await HttpService.client.purgePerson(form);
725 this.purgeItem(purgePersonRes);
728 async handlePurgeComment(form: PurgeComment) {
729 const purgeCommentRes = await HttpService.client.purgeComment(form);
730 this.purgeItem(purgeCommentRes);
733 async handlePurgePost(form: PurgePost) {
734 const purgeRes = await HttpService.client.purgePost(form);
735 this.purgeItem(purgeRes);
738 async handleBlockCommunity(form: BlockCommunity) {
739 const blockCommunityRes = await HttpService.client.blockCommunity(form);
740 if (blockCommunityRes.state == "success") {
741 updateCommunityBlock(blockCommunityRes.data);
743 if (s.postRes.state == "success") {
744 s.postRes.data.community_view.blocked =
745 blockCommunityRes.data.blocked;
751 async handleBlockPerson(form: BlockPerson) {
752 const blockPersonRes = await HttpService.client.blockPerson(form);
753 if (blockPersonRes.state == "success") {
754 updatePersonBlock(blockPersonRes.data);
758 async handleModRemoveCommunity(form: RemoveCommunity) {
759 const removeCommunityRes = await HttpService.client.removeCommunity(form);
760 this.updateCommunity(removeCommunityRes);
763 async handleEditCommunity(form: EditCommunity) {
764 const res = await HttpService.client.editCommunity(form);
765 this.updateCommunity(res);
770 async handleCreateComment(form: CreateComment) {
771 const createCommentRes = await HttpService.client.createComment(form);
772 this.createAndUpdateComments(createCommentRes);
774 return createCommentRes;
777 async handleEditComment(form: EditComment) {
778 const editCommentRes = await HttpService.client.editComment(form);
779 this.findAndUpdateComment(editCommentRes);
781 return editCommentRes;
784 async handleDeleteComment(form: DeleteComment) {
785 const deleteCommentRes = await HttpService.client.deleteComment(form);
786 this.findAndUpdateComment(deleteCommentRes);
789 async handleDeletePost(form: DeletePost) {
790 const deleteRes = await HttpService.client.deletePost(form);
791 this.updatePost(deleteRes);
794 async handleRemovePost(form: RemovePost) {
795 const removeRes = await HttpService.client.removePost(form);
796 this.updatePost(removeRes);
799 async handleRemoveComment(form: RemoveComment) {
800 const removeCommentRes = await HttpService.client.removeComment(form);
801 this.findAndUpdateComment(removeCommentRes);
804 async handleSaveComment(form: SaveComment) {
805 const saveCommentRes = await HttpService.client.saveComment(form);
806 this.findAndUpdateComment(saveCommentRes);
809 async handleSavePost(form: SavePost) {
810 const saveRes = await HttpService.client.savePost(form);
811 this.updatePost(saveRes);
814 async handleFeaturePost(form: FeaturePost) {
815 const featureRes = await HttpService.client.featurePost(form);
816 this.updatePost(featureRes);
819 async handleCommentVote(form: CreateCommentLike) {
820 const voteRes = await HttpService.client.likeComment(form);
821 this.findAndUpdateComment(voteRes);
824 async handlePostVote(form: CreatePostLike) {
825 const voteRes = await HttpService.client.likePost(form);
826 this.updatePost(voteRes);
829 async handlePostEdit(form: EditPost) {
830 const res = await HttpService.client.editPost(form);
831 this.updatePost(res);
834 async handleCommentReport(form: CreateCommentReport) {
835 const reportRes = await HttpService.client.createCommentReport(form);
836 if (reportRes.state == "success") {
837 toast(I18NextService.i18n.t("report_created"));
841 async handlePostReport(form: CreatePostReport) {
842 const reportRes = await HttpService.client.createPostReport(form);
843 if (reportRes.state == "success") {
844 toast(I18NextService.i18n.t("report_created"));
848 async handleLockPost(form: LockPost) {
849 const lockRes = await HttpService.client.lockPost(form);
850 this.updatePost(lockRes);
853 async handleDistinguishComment(form: DistinguishComment) {
854 const distinguishRes = await HttpService.client.distinguishComment(form);
855 this.findAndUpdateComment(distinguishRes);
858 async handleAddAdmin(form: AddAdmin) {
859 const addAdminRes = await HttpService.client.addAdmin(form);
861 if (addAdminRes.state === "success") {
862 this.setState(s => ((s.siteRes.admins = addAdminRes.data.admins), s));
866 async handleTransferCommunity(form: TransferCommunity) {
867 const transferCommunityRes = await HttpService.client.transferCommunity(
870 this.updateCommunityFull(transferCommunityRes);
873 async handleFetchChildren(form: GetComments) {
874 const moreCommentsRes = await HttpService.client.getComments(form);
876 this.state.commentsRes.state == "success" &&
877 moreCommentsRes.state == "success"
879 const newComments = moreCommentsRes.data.comments;
880 // Remove the first comment, since it is the parent
882 const newRes = this.state.commentsRes;
883 newRes.data.comments.push(...newComments);
884 this.setState({ commentsRes: newRes });
888 async handleCommentReplyRead(form: MarkCommentReplyAsRead) {
889 const readRes = await HttpService.client.markCommentReplyAsRead(form);
890 this.findAndUpdateCommentReply(readRes);
893 async handlePersonMentionRead(form: MarkPersonMentionAsRead) {
894 // TODO not sure what to do here. Maybe it is actually optional, because post doesn't need it.
895 await HttpService.client.markPersonMentionAsRead(form);
898 async handleBanFromCommunity(form: BanFromCommunity) {
899 const banRes = await HttpService.client.banFromCommunity(form);
900 this.updateBan(banRes);
903 async handleBanPerson(form: BanPerson) {
904 const banRes = await HttpService.client.banPerson(form);
905 this.updateBan(banRes);
908 updateBanFromCommunity(banRes: RequestState<BanFromCommunityResponse>) {
909 // Maybe not necessary
910 if (banRes.state == "success") {
913 s.postRes.state == "success" &&
914 s.postRes.data.post_view.creator.id ==
915 banRes.data.person_view.person.id
917 s.postRes.data.post_view.creator_banned_from_community =
920 if (s.commentsRes.state == "success") {
921 s.commentsRes.data.comments
922 .filter(c => c.creator.id == banRes.data.person_view.person.id)
924 c => (c.creator_banned_from_community = banRes.data.banned)
932 updateBan(banRes: RequestState<BanPersonResponse>) {
933 // Maybe not necessary
934 if (banRes.state == "success") {
937 s.postRes.state == "success" &&
938 s.postRes.data.post_view.creator.id ==
939 banRes.data.person_view.person.id
941 s.postRes.data.post_view.creator.banned = banRes.data.banned;
943 if (s.commentsRes.state == "success") {
944 s.commentsRes.data.comments
945 .filter(c => c.creator.id == banRes.data.person_view.person.id)
946 .forEach(c => (c.creator.banned = banRes.data.banned));
953 updateCommunity(communityRes: RequestState<CommunityResponse>) {
955 if (s.postRes.state == "success" && communityRes.state == "success") {
956 s.postRes.data.community_view = communityRes.data.community_view;
962 updateCommunityFull(res: RequestState<GetCommunityResponse>) {
964 if (s.postRes.state == "success" && res.state == "success") {
965 s.postRes.data.community_view = res.data.community_view;
966 s.postRes.data.moderators = res.data.moderators;
972 updatePost(post: RequestState<PostResponse>) {
974 if (s.postRes.state == "success" && post.state == "success") {
975 s.postRes.data.post_view = post.data.post_view;
981 purgeItem(purgeRes: RequestState<PurgeItemResponse>) {
982 if (purgeRes.state == "success") {
983 toast(I18NextService.i18n.t("purge_success"));
984 this.context.router.history.push(`/`);
988 createAndUpdateComments(res: RequestState<CommentResponse>) {
990 if (s.commentsRes.state === "success" && res.state === "success") {
991 s.commentsRes.data.comments.unshift(res.data.comment_view);
993 // Set finished for the parent
995 getCommentParentId(res.data.comment_view.comment) ?? 0,
1003 findAndUpdateComment(res: RequestState<CommentResponse>) {
1004 this.setState(s => {
1005 if (s.commentsRes.state == "success" && res.state == "success") {
1006 s.commentsRes.data.comments = editComment(
1007 res.data.comment_view,
1008 s.commentsRes.data.comments
1010 s.finished.set(res.data.comment_view.comment.id, true);
1016 findAndUpdateCommentReply(res: RequestState<CommentReplyResponse>) {
1017 this.setState(s => {
1018 if (s.commentsRes.state == "success" && res.state == "success") {
1019 s.commentsRes.data.comments = editWith(
1020 res.data.comment_reply_view,
1021 s.commentsRes.data.comments
1028 updateModerators(res: RequestState<AddModToCommunityResponse>) {
1029 // Update the moderators
1030 this.setState(s => {
1031 if (s.postRes.state == "success" && res.state == "success") {
1032 s.postRes.data.moderators = res.data.moderators;