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 col-lg-9 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 col-lg-3">
429 return <div className="post container-lg">{this.renderPostRes()}</div>;
435 <div className="btn-group btn-group-toggle flex-wrap me-3 mb-2">
437 className={`btn btn-outline-secondary pointer ${
438 this.state.commentSort === "Hot" && "active"
441 {I18NextService.i18n.t("hot")}
444 className="btn-check"
446 checked={this.state.commentSort === "Hot"}
447 onChange={linkEvent(this, this.handleCommentSortChange)}
451 className={`btn btn-outline-secondary pointer ${
452 this.state.commentSort === "Top" && "active"
455 {I18NextService.i18n.t("top")}
458 className="btn-check"
460 checked={this.state.commentSort === "Top"}
461 onChange={linkEvent(this, this.handleCommentSortChange)}
465 className={`btn btn-outline-secondary pointer ${
466 this.state.commentSort === "New" && "active"
469 {I18NextService.i18n.t("new")}
472 className="btn-check"
474 checked={this.state.commentSort === "New"}
475 onChange={linkEvent(this, this.handleCommentSortChange)}
479 className={`btn btn-outline-secondary pointer ${
480 this.state.commentSort === "Old" && "active"
483 {I18NextService.i18n.t("old")}
486 className="btn-check"
488 checked={this.state.commentSort === "Old"}
489 onChange={linkEvent(this, this.handleCommentSortChange)}
493 <div className="btn-group btn-group-toggle flex-wrap mb-2">
495 className={`btn btn-outline-secondary pointer ${
496 this.state.commentViewType === CommentViewType.Flat && "active"
499 {I18NextService.i18n.t("chat")}
502 className="btn-check"
503 value={CommentViewType.Flat}
504 checked={this.state.commentViewType === CommentViewType.Flat}
505 onChange={linkEvent(this, this.handleCommentViewTypeChange)}
514 // These are already sorted by new
515 const commentsRes = this.state.commentsRes;
516 const postRes = this.state.postRes;
518 if (commentsRes.state == "success" && postRes.state == "success") {
522 nodes={commentsToFlatNodes(commentsRes.data.comments)}
523 viewType={this.state.commentViewType}
524 maxCommentsShown={this.state.maxCommentsShown}
526 locked={postRes.data.post_view.post.locked}
527 moderators={postRes.data.moderators}
528 admins={this.state.siteRes.admins}
529 enableDownvotes={enableDownvotes(this.state.siteRes)}
531 finished={this.state.finished}
532 allLanguages={this.state.siteRes.all_languages}
533 siteLanguages={this.state.siteRes.discussion_languages}
534 onSaveComment={this.handleSaveComment}
535 onBlockPerson={this.handleBlockPerson}
536 onDeleteComment={this.handleDeleteComment}
537 onRemoveComment={this.handleRemoveComment}
538 onCommentVote={this.handleCommentVote}
539 onCommentReport={this.handleCommentReport}
540 onDistinguishComment={this.handleDistinguishComment}
541 onAddModToCommunity={this.handleAddModToCommunity}
542 onAddAdmin={this.handleAddAdmin}
543 onTransferCommunity={this.handleTransferCommunity}
544 onFetchChildren={this.handleFetchChildren}
545 onPurgeComment={this.handlePurgeComment}
546 onPurgePerson={this.handlePurgePerson}
547 onCommentReplyRead={this.handleCommentReplyRead}
548 onPersonMentionRead={this.handlePersonMentionRead}
549 onBanPersonFromCommunity={this.handleBanFromCommunity}
550 onBanPerson={this.handleBanPerson}
551 onCreateComment={this.handleCreateComment}
552 onEditComment={this.handleEditComment}
560 const res = this.state.postRes;
561 if (res.state === "success") {
564 community_view={res.data.community_view}
565 moderators={res.data.moderators}
566 admins={this.state.siteRes.admins}
567 enableNsfw={enableNsfw(this.state.siteRes)}
569 allLanguages={this.state.siteRes.all_languages}
570 siteLanguages={this.state.siteRes.discussion_languages}
571 onDeleteCommunity={this.handleDeleteCommunityClick}
572 onLeaveModTeam={this.handleAddModToCommunity}
573 onFollowCommunity={this.handleFollow}
574 onRemoveCommunity={this.handleModRemoveCommunity}
575 onPurgeCommunity={this.handlePurgeCommunity}
576 onBlockCommunity={this.handleBlockCommunity}
577 onEditCommunity={this.handleEditCommunity}
584 const res = this.state.postRes;
585 const firstComment = this.commentTree().at(0)?.comment_view.comment;
586 const depth = getDepthFromComment(firstComment);
587 const showContextButton = depth ? depth > 0 : false;
590 res.state == "success" && (
592 {!!this.state.commentId && (
595 className="ps-0 d-block btn btn-link text-muted"
596 onClick={linkEvent(this, this.handleViewPost)}
598 {I18NextService.i18n.t("view_all_comments")} âž”
600 {showContextButton && (
602 className="ps-0 d-block btn btn-link text-muted"
603 onClick={linkEvent(this, this.handleViewContext)}
605 {I18NextService.i18n.t("show_context")} âž”
611 nodes={this.commentTree()}
612 viewType={this.state.commentViewType}
613 maxCommentsShown={this.state.maxCommentsShown}
614 locked={res.data.post_view.post.locked}
615 moderators={res.data.moderators}
616 admins={this.state.siteRes.admins}
617 enableDownvotes={enableDownvotes(this.state.siteRes)}
618 finished={this.state.finished}
619 allLanguages={this.state.siteRes.all_languages}
620 siteLanguages={this.state.siteRes.discussion_languages}
621 onSaveComment={this.handleSaveComment}
622 onBlockPerson={this.handleBlockPerson}
623 onDeleteComment={this.handleDeleteComment}
624 onRemoveComment={this.handleRemoveComment}
625 onCommentVote={this.handleCommentVote}
626 onCommentReport={this.handleCommentReport}
627 onDistinguishComment={this.handleDistinguishComment}
628 onAddModToCommunity={this.handleAddModToCommunity}
629 onAddAdmin={this.handleAddAdmin}
630 onTransferCommunity={this.handleTransferCommunity}
631 onFetchChildren={this.handleFetchChildren}
632 onPurgeComment={this.handlePurgeComment}
633 onPurgePerson={this.handlePurgePerson}
634 onCommentReplyRead={this.handleCommentReplyRead}
635 onPersonMentionRead={this.handlePersonMentionRead}
636 onBanPersonFromCommunity={this.handleBanFromCommunity}
637 onBanPerson={this.handleBanPerson}
638 onCreateComment={this.handleCreateComment}
639 onEditComment={this.handleEditComment}
646 commentTree(): CommentNodeI[] {
647 if (this.state.commentsRes.state == "success") {
648 return buildCommentsTree(
649 this.state.commentsRes.data.comments,
650 !!this.state.commentId
657 async handleCommentSortChange(i: Post, event: any) {
659 commentSort: event.target.value as CommentSortType,
660 commentViewType: CommentViewType.Tree,
661 commentsRes: { state: "loading" },
662 postRes: { state: "loading" },
667 handleCommentViewTypeChange(i: Post, event: any) {
669 commentViewType: Number(event.target.value),
674 handleShowSidebarMobile(i: Post) {
675 i.setState({ showSidebarMobile: !i.state.showSidebarMobile });
678 handleViewPost(i: Post) {
679 if (i.state.postRes.state == "success") {
680 const id = i.state.postRes.data.post_view.post.id;
681 i.context.router.history.push(`/post/${id}`);
685 handleViewContext(i: Post) {
686 if (i.state.commentsRes.state == "success") {
687 const parentId = getCommentParentId(
688 i.state.commentsRes.data.comments.at(0)?.comment
691 i.context.router.history.push(`/comment/${parentId}`);
696 async handleDeleteCommunityClick(form: DeleteCommunity) {
697 const deleteCommunityRes = await HttpService.client.deleteCommunity(form);
698 this.updateCommunity(deleteCommunityRes);
701 async handleAddModToCommunity(form: AddModToCommunity) {
702 const addModRes = await HttpService.client.addModToCommunity(form);
703 this.updateModerators(addModRes);
706 async handleFollow(form: FollowCommunity) {
707 const followCommunityRes = await HttpService.client.followCommunity(form);
708 this.updateCommunity(followCommunityRes);
711 if (followCommunityRes.state === "success") {
712 const communityId = followCommunityRes.data.community_view.community.id;
713 const mui = UserService.Instance.myUserInfo;
715 mui.follows = mui.follows.filter(i => i.community.id != communityId);
720 async handlePurgeCommunity(form: PurgeCommunity) {
721 const purgeCommunityRes = await HttpService.client.purgeCommunity(form);
722 this.purgeItem(purgeCommunityRes);
725 async handlePurgePerson(form: PurgePerson) {
726 const purgePersonRes = await HttpService.client.purgePerson(form);
727 this.purgeItem(purgePersonRes);
730 async handlePurgeComment(form: PurgeComment) {
731 const purgeCommentRes = await HttpService.client.purgeComment(form);
732 this.purgeItem(purgeCommentRes);
735 async handlePurgePost(form: PurgePost) {
736 const purgeRes = await HttpService.client.purgePost(form);
737 this.purgeItem(purgeRes);
740 async handleBlockCommunity(form: BlockCommunity) {
741 const blockCommunityRes = await HttpService.client.blockCommunity(form);
742 if (blockCommunityRes.state == "success") {
743 updateCommunityBlock(blockCommunityRes.data);
745 if (s.postRes.state == "success") {
746 s.postRes.data.community_view.blocked =
747 blockCommunityRes.data.blocked;
753 async handleBlockPerson(form: BlockPerson) {
754 const blockPersonRes = await HttpService.client.blockPerson(form);
755 if (blockPersonRes.state == "success") {
756 updatePersonBlock(blockPersonRes.data);
760 async handleModRemoveCommunity(form: RemoveCommunity) {
761 const removeCommunityRes = await HttpService.client.removeCommunity(form);
762 this.updateCommunity(removeCommunityRes);
765 async handleEditCommunity(form: EditCommunity) {
766 const res = await HttpService.client.editCommunity(form);
767 this.updateCommunity(res);
772 async handleCreateComment(form: CreateComment) {
773 const createCommentRes = await HttpService.client.createComment(form);
774 this.createAndUpdateComments(createCommentRes);
776 return createCommentRes;
779 async handleEditComment(form: EditComment) {
780 const editCommentRes = await HttpService.client.editComment(form);
781 this.findAndUpdateComment(editCommentRes);
783 return editCommentRes;
786 async handleDeleteComment(form: DeleteComment) {
787 const deleteCommentRes = await HttpService.client.deleteComment(form);
788 this.findAndUpdateComment(deleteCommentRes);
791 async handleDeletePost(form: DeletePost) {
792 const deleteRes = await HttpService.client.deletePost(form);
793 this.updatePost(deleteRes);
796 async handleRemovePost(form: RemovePost) {
797 const removeRes = await HttpService.client.removePost(form);
798 this.updatePost(removeRes);
801 async handleRemoveComment(form: RemoveComment) {
802 const removeCommentRes = await HttpService.client.removeComment(form);
803 this.findAndUpdateComment(removeCommentRes);
806 async handleSaveComment(form: SaveComment) {
807 const saveCommentRes = await HttpService.client.saveComment(form);
808 this.findAndUpdateComment(saveCommentRes);
811 async handleSavePost(form: SavePost) {
812 const saveRes = await HttpService.client.savePost(form);
813 this.updatePost(saveRes);
816 async handleFeaturePost(form: FeaturePost) {
817 const featureRes = await HttpService.client.featurePost(form);
818 this.updatePost(featureRes);
821 async handleCommentVote(form: CreateCommentLike) {
822 const voteRes = await HttpService.client.likeComment(form);
823 this.findAndUpdateComment(voteRes);
826 async handlePostVote(form: CreatePostLike) {
827 const voteRes = await HttpService.client.likePost(form);
828 this.updatePost(voteRes);
831 async handlePostEdit(form: EditPost) {
832 const res = await HttpService.client.editPost(form);
833 this.updatePost(res);
836 async handleCommentReport(form: CreateCommentReport) {
837 const reportRes = await HttpService.client.createCommentReport(form);
838 if (reportRes.state == "success") {
839 toast(I18NextService.i18n.t("report_created"));
843 async handlePostReport(form: CreatePostReport) {
844 const reportRes = await HttpService.client.createPostReport(form);
845 if (reportRes.state == "success") {
846 toast(I18NextService.i18n.t("report_created"));
850 async handleLockPost(form: LockPost) {
851 const lockRes = await HttpService.client.lockPost(form);
852 this.updatePost(lockRes);
855 async handleDistinguishComment(form: DistinguishComment) {
856 const distinguishRes = await HttpService.client.distinguishComment(form);
857 this.findAndUpdateComment(distinguishRes);
860 async handleAddAdmin(form: AddAdmin) {
861 const addAdminRes = await HttpService.client.addAdmin(form);
863 if (addAdminRes.state === "success") {
864 this.setState(s => ((s.siteRes.admins = addAdminRes.data.admins), s));
868 async handleTransferCommunity(form: TransferCommunity) {
869 const transferCommunityRes = await HttpService.client.transferCommunity(
872 this.updateCommunityFull(transferCommunityRes);
875 async handleFetchChildren(form: GetComments) {
876 const moreCommentsRes = await HttpService.client.getComments(form);
878 this.state.commentsRes.state == "success" &&
879 moreCommentsRes.state == "success"
881 const newComments = moreCommentsRes.data.comments;
882 // Remove the first comment, since it is the parent
884 const newRes = this.state.commentsRes;
885 newRes.data.comments.push(...newComments);
886 this.setState({ commentsRes: newRes });
890 async handleCommentReplyRead(form: MarkCommentReplyAsRead) {
891 const readRes = await HttpService.client.markCommentReplyAsRead(form);
892 this.findAndUpdateCommentReply(readRes);
895 async handlePersonMentionRead(form: MarkPersonMentionAsRead) {
896 // TODO not sure what to do here. Maybe it is actually optional, because post doesn't need it.
897 await HttpService.client.markPersonMentionAsRead(form);
900 async handleBanFromCommunity(form: BanFromCommunity) {
901 const banRes = await HttpService.client.banFromCommunity(form);
902 this.updateBan(banRes);
905 async handleBanPerson(form: BanPerson) {
906 const banRes = await HttpService.client.banPerson(form);
907 this.updateBan(banRes);
910 updateBanFromCommunity(banRes: RequestState<BanFromCommunityResponse>) {
911 // Maybe not necessary
912 if (banRes.state == "success") {
915 s.postRes.state == "success" &&
916 s.postRes.data.post_view.creator.id ==
917 banRes.data.person_view.person.id
919 s.postRes.data.post_view.creator_banned_from_community =
922 if (s.commentsRes.state == "success") {
923 s.commentsRes.data.comments
924 .filter(c => c.creator.id == banRes.data.person_view.person.id)
926 c => (c.creator_banned_from_community = banRes.data.banned)
934 updateBan(banRes: RequestState<BanPersonResponse>) {
935 // Maybe not necessary
936 if (banRes.state == "success") {
939 s.postRes.state == "success" &&
940 s.postRes.data.post_view.creator.id ==
941 banRes.data.person_view.person.id
943 s.postRes.data.post_view.creator.banned = banRes.data.banned;
945 if (s.commentsRes.state == "success") {
946 s.commentsRes.data.comments
947 .filter(c => c.creator.id == banRes.data.person_view.person.id)
948 .forEach(c => (c.creator.banned = banRes.data.banned));
955 updateCommunity(communityRes: RequestState<CommunityResponse>) {
957 if (s.postRes.state == "success" && communityRes.state == "success") {
958 s.postRes.data.community_view = communityRes.data.community_view;
964 updateCommunityFull(res: RequestState<GetCommunityResponse>) {
966 if (s.postRes.state == "success" && res.state == "success") {
967 s.postRes.data.community_view = res.data.community_view;
968 s.postRes.data.moderators = res.data.moderators;
974 updatePost(post: RequestState<PostResponse>) {
976 if (s.postRes.state == "success" && post.state == "success") {
977 s.postRes.data.post_view = post.data.post_view;
983 purgeItem(purgeRes: RequestState<PurgeItemResponse>) {
984 if (purgeRes.state == "success") {
985 toast(I18NextService.i18n.t("purge_success"));
986 this.context.router.history.push(`/`);
990 createAndUpdateComments(res: RequestState<CommentResponse>) {
992 if (s.commentsRes.state === "success" && res.state === "success") {
993 s.commentsRes.data.comments.unshift(res.data.comment_view);
995 // Set finished for the parent
997 getCommentParentId(res.data.comment_view.comment) ?? 0,
1005 findAndUpdateComment(res: RequestState<CommentResponse>) {
1006 this.setState(s => {
1007 if (s.commentsRes.state == "success" && res.state == "success") {
1008 s.commentsRes.data.comments = editComment(
1009 res.data.comment_view,
1010 s.commentsRes.data.comments
1012 s.finished.set(res.data.comment_view.comment.id, true);
1018 findAndUpdateCommentReply(res: RequestState<CommentReplyResponse>) {
1019 this.setState(s => {
1020 if (s.commentsRes.state == "success" && res.state == "success") {
1021 s.commentsRes.data.comments = editWith(
1022 res.data.comment_reply_view,
1023 s.commentsRes.data.comments
1030 updateModerators(res: RequestState<AddModToCommunityResponse>) {
1031 // Update the moderators
1032 this.setState(s => {
1033 if (s.postRes.state == "success" && res.state == "success") {
1034 s.postRes.data.moderators = res.data.moderators;