1 import autosize from "autosize";
2 import { Component, createRef, linkEvent, RefObject } from "inferno";
6 AddModToCommunityResponse,
8 BanFromCommunityResponse,
39 MarkCommentReplyAsRead,
40 MarkPersonMentionAsRead,
53 } from "lemmy-js-client";
54 import { i18n } from "../../i18next";
59 } from "../../interfaces";
60 import { UserService } from "../../services";
61 import { FirstLoadService } from "../../services/FirstLoadService";
62 import { HttpService, RequestState } from "../../services/HttpService";
71 getCommentIdFromProps,
77 restoreScrollPosition,
85 import { isBrowser } from "../../utils/browser/is-browser";
86 import { debounce } from "../../utils/helpers/debounce";
87 import { CommentForm } from "../comment/comment-form";
88 import { CommentNodes } from "../comment/comment-nodes";
89 import { HtmlTags } from "../common/html-tags";
90 import { Icon, Spinner } from "../common/icon";
91 import { Sidebar } from "../community/sidebar";
92 import { PostListing } from "./post-listing";
94 const commentsShownInterval = 15;
99 postRes: RequestState<GetPostResponse>;
100 commentsRes: RequestState<GetCommentsResponse>;
101 commentSort: CommentSortType;
102 commentViewType: CommentViewType;
104 siteRes: GetSiteResponse;
105 commentSectionRef?: RefObject<HTMLDivElement>;
106 showSidebarMobile: boolean;
107 maxCommentsShown: number;
108 finished: Map<CommentId, boolean | undefined>;
109 isIsomorphic: boolean;
112 export class Post extends Component<any, PostState> {
113 private isoData = setIsoData(this.context);
114 private commentScrollDebounced: () => void;
116 postRes: { state: "empty" },
117 commentsRes: { state: "empty" },
118 postId: getIdFromProps(this.props),
119 commentId: getCommentIdFromProps(this.props),
121 commentViewType: CommentViewType.Tree,
123 siteRes: this.isoData.site_res,
124 showSidebarMobile: false,
125 maxCommentsShown: commentsShownInterval,
130 constructor(props: any, context: any) {
131 super(props, context);
133 this.handleDeleteCommunityClick =
134 this.handleDeleteCommunityClick.bind(this);
135 this.handleEditCommunity = this.handleEditCommunity.bind(this);
136 this.handleFollow = this.handleFollow.bind(this);
137 this.handleModRemoveCommunity = this.handleModRemoveCommunity.bind(this);
138 this.handleCreateComment = this.handleCreateComment.bind(this);
139 this.handleEditComment = this.handleEditComment.bind(this);
140 this.handleSaveComment = this.handleSaveComment.bind(this);
141 this.handleBlockCommunity = this.handleBlockCommunity.bind(this);
142 this.handleBlockPerson = this.handleBlockPerson.bind(this);
143 this.handleDeleteComment = this.handleDeleteComment.bind(this);
144 this.handleRemoveComment = this.handleRemoveComment.bind(this);
145 this.handleCommentVote = this.handleCommentVote.bind(this);
146 this.handleAddModToCommunity = this.handleAddModToCommunity.bind(this);
147 this.handleAddAdmin = this.handleAddAdmin.bind(this);
148 this.handlePurgePerson = this.handlePurgePerson.bind(this);
149 this.handlePurgeComment = this.handlePurgeComment.bind(this);
150 this.handleCommentReport = this.handleCommentReport.bind(this);
151 this.handleDistinguishComment = this.handleDistinguishComment.bind(this);
152 this.handleTransferCommunity = this.handleTransferCommunity.bind(this);
153 this.handleFetchChildren = this.handleFetchChildren.bind(this);
154 this.handleCommentReplyRead = this.handleCommentReplyRead.bind(this);
155 this.handlePersonMentionRead = this.handlePersonMentionRead.bind(this);
156 this.handleBanFromCommunity = this.handleBanFromCommunity.bind(this);
157 this.handleBanPerson = this.handleBanPerson.bind(this);
158 this.handlePostEdit = this.handlePostEdit.bind(this);
159 this.handlePostVote = this.handlePostVote.bind(this);
160 this.handlePostReport = this.handlePostReport.bind(this);
161 this.handleLockPost = this.handleLockPost.bind(this);
162 this.handleDeletePost = this.handleDeletePost.bind(this);
163 this.handleRemovePost = this.handleRemovePost.bind(this);
164 this.handleSavePost = this.handleSavePost.bind(this);
165 this.handlePurgePost = this.handlePurgePost.bind(this);
166 this.handleFeaturePost = this.handleFeaturePost.bind(this);
168 this.state = { ...this.state, commentSectionRef: createRef() };
170 // Only fetch the data if coming from another route
171 if (FirstLoadService.isFirstLoad) {
172 const [postRes, commentsRes] = this.isoData.routeData;
182 if (this.checkScrollIntoCommentsParam) {
183 this.scrollIntoCommentSection();
191 postRes: { state: "loading" },
192 commentsRes: { state: "loading" },
195 const auth = myAuth();
198 postRes: await HttpService.client.getPost({
199 id: this.state.postId,
200 comment_id: this.state.commentId,
203 commentsRes: await HttpService.client.getComments({
204 post_id: this.state.postId,
205 parent_id: this.state.commentId,
206 max_depth: commentTreeMaxDepth,
207 sort: this.state.commentSort,
216 if (!this.state.commentId) restoreScrollPosition(this.context);
218 if (this.checkScrollIntoCommentsParam) {
219 this.scrollIntoCommentSection();
223 static fetchInitialData({
227 }: InitialFetchRequest): Promise<any>[] {
228 const pathSplit = path.split("/");
229 const promises: Promise<RequestState<any>>[] = [];
231 const pathType = pathSplit.at(1);
232 const id = pathSplit.at(2) ? Number(pathSplit.at(2)) : undefined;
234 const postForm: GetPost = {
238 const commentsForm: GetComments = {
239 max_depth: commentTreeMaxDepth,
246 // Set the correct id based on the path type
247 if (pathType === "post") {
249 commentsForm.post_id = id;
251 postForm.comment_id = id;
252 commentsForm.parent_id = id;
255 promises.push(client.getPost(postForm));
256 promises.push(client.getComments(commentsForm));
261 componentWillUnmount() {
262 document.removeEventListener("scroll", this.commentScrollDebounced);
264 saveScrollPosition(this.context);
267 async componentDidMount() {
268 if (!this.state.isIsomorphic) {
269 await this.fetchPost();
272 autosize(document.querySelectorAll("textarea"));
274 this.commentScrollDebounced = debounce(this.trackCommentsBoxScrolling, 100);
275 document.addEventListener("scroll", this.commentScrollDebounced);
278 async componentDidUpdate(_lastProps: any) {
279 // Necessary if you are on a post and you click another post (same route)
280 if (_lastProps.location.pathname !== _lastProps.history.location.pathname) {
281 await this.fetchPost();
285 get checkScrollIntoCommentsParam() {
287 new URLSearchParams(this.props.location.search).get("scrollToComments")
291 scrollIntoCommentSection() {
292 this.state.commentSectionRef?.current?.scrollIntoView();
295 isBottom(el: Element): boolean {
296 return el?.getBoundingClientRect().bottom <= window.innerHeight;
300 * Shows new comments when scrolling to the bottom of the comments div
302 trackCommentsBoxScrolling = () => {
303 const wrappedElement = document.getElementsByClassName("comments")[0];
304 if (wrappedElement && this.isBottom(wrappedElement)) {
306 this.state.commentsRes.state == "success"
307 ? this.state.commentsRes.data.comments.length
310 if (this.state.maxCommentsShown < commentCount) {
312 maxCommentsShown: this.state.maxCommentsShown + commentsShownInterval,
318 get documentTitle(): string {
319 const siteName = this.state.siteRes.site_view.site.name;
320 return this.state.postRes.state == "success"
321 ? `${this.state.postRes.data.post_view.post.name} - ${siteName}`
325 get imageTag(): string | undefined {
326 if (this.state.postRes.state == "success") {
327 const post = this.state.postRes.data.post_view.post;
328 const thumbnail = post.thumbnail_url;
329 const url = post.url;
330 return thumbnail || (url && isImage(url) ? url : undefined);
331 } else return undefined;
335 switch (this.state.postRes.state) {
343 const res = this.state.postRes.data;
345 <div className="row">
346 <div className="col-12 col-md-8 mb-3">
348 title={this.documentTitle}
349 path={this.context.router.route.match.url}
350 image={this.imageTag}
351 description={res.post_view.post.body}
354 post_view={res.post_view}
355 crossPosts={res.cross_posts}
358 moderators={res.moderators}
359 admins={this.state.siteRes.admins}
360 enableDownvotes={enableDownvotes(this.state.siteRes)}
361 enableNsfw={enableNsfw(this.state.siteRes)}
362 allLanguages={this.state.siteRes.all_languages}
363 siteLanguages={this.state.siteRes.discussion_languages}
364 onBlockPerson={this.handleBlockPerson}
365 onPostEdit={this.handlePostEdit}
366 onPostVote={this.handlePostVote}
367 onPostReport={this.handlePostReport}
368 onLockPost={this.handleLockPost}
369 onDeletePost={this.handleDeletePost}
370 onRemovePost={this.handleRemovePost}
371 onSavePost={this.handleSavePost}
372 onPurgePerson={this.handlePurgePerson}
373 onPurgePost={this.handlePurgePost}
374 onBanPerson={this.handleBanPerson}
375 onBanPersonFromCommunity={this.handleBanFromCommunity}
376 onAddModToCommunity={this.handleAddModToCommunity}
377 onAddAdmin={this.handleAddAdmin}
378 onTransferCommunity={this.handleTransferCommunity}
379 onFeaturePost={this.handleFeaturePost}
381 <div ref={this.state.commentSectionRef} className="mb-2" />
383 node={res.post_view.post.id}
384 disabled={res.post_view.post.locked}
385 allLanguages={this.state.siteRes.all_languages}
386 siteLanguages={this.state.siteRes.discussion_languages}
387 onUpsertComment={this.handleCreateComment}
388 finished={this.state.finished.get(0)}
390 <div className="d-block d-md-none">
392 className="btn btn-secondary d-inline-block mb-2 mr-3"
393 onClick={linkEvent(this, this.handleShowSidebarMobile)}
395 {i18n.t("sidebar")}{" "}
398 this.state.showSidebarMobile
402 classes="icon-inline"
405 {this.state.showSidebarMobile && this.sidebar()}
408 {this.state.commentViewType == CommentViewType.Tree &&
410 {this.state.commentViewType == CommentViewType.Flat &&
413 <div className="d-none d-md-block col-md-4">{this.sidebar()}</div>
421 return <div className="container-lg">{this.renderPostRes()}</div>;
427 <div className="btn-group btn-group-toggle flex-wrap mr-3 mb-2">
429 className={`btn btn-outline-secondary pointer ${
430 this.state.commentSort === "Hot" && "active"
437 checked={this.state.commentSort === "Hot"}
438 onChange={linkEvent(this, this.handleCommentSortChange)}
442 className={`btn btn-outline-secondary pointer ${
443 this.state.commentSort === "Top" && "active"
450 checked={this.state.commentSort === "Top"}
451 onChange={linkEvent(this, this.handleCommentSortChange)}
455 className={`btn btn-outline-secondary pointer ${
456 this.state.commentSort === "New" && "active"
463 checked={this.state.commentSort === "New"}
464 onChange={linkEvent(this, this.handleCommentSortChange)}
468 className={`btn btn-outline-secondary pointer ${
469 this.state.commentSort === "Old" && "active"
476 checked={this.state.commentSort === "Old"}
477 onChange={linkEvent(this, this.handleCommentSortChange)}
481 <div className="btn-group btn-group-toggle flex-wrap mb-2">
483 className={`btn btn-outline-secondary pointer ${
484 this.state.commentViewType === CommentViewType.Flat && "active"
490 value={CommentViewType.Flat}
491 checked={this.state.commentViewType === CommentViewType.Flat}
492 onChange={linkEvent(this, this.handleCommentViewTypeChange)}
501 // These are already sorted by new
502 const commentsRes = this.state.commentsRes;
503 const postRes = this.state.postRes;
505 if (commentsRes.state == "success" && postRes.state == "success") {
509 nodes={commentsToFlatNodes(commentsRes.data.comments)}
510 viewType={this.state.commentViewType}
511 maxCommentsShown={this.state.maxCommentsShown}
513 locked={postRes.data.post_view.post.locked}
514 moderators={postRes.data.moderators}
515 admins={this.state.siteRes.admins}
516 enableDownvotes={enableDownvotes(this.state.siteRes)}
518 finished={this.state.finished}
519 allLanguages={this.state.siteRes.all_languages}
520 siteLanguages={this.state.siteRes.discussion_languages}
521 onSaveComment={this.handleSaveComment}
522 onBlockPerson={this.handleBlockPerson}
523 onDeleteComment={this.handleDeleteComment}
524 onRemoveComment={this.handleRemoveComment}
525 onCommentVote={this.handleCommentVote}
526 onCommentReport={this.handleCommentReport}
527 onDistinguishComment={this.handleDistinguishComment}
528 onAddModToCommunity={this.handleAddModToCommunity}
529 onAddAdmin={this.handleAddAdmin}
530 onTransferCommunity={this.handleTransferCommunity}
531 onFetchChildren={this.handleFetchChildren}
532 onPurgeComment={this.handlePurgeComment}
533 onPurgePerson={this.handlePurgePerson}
534 onCommentReplyRead={this.handleCommentReplyRead}
535 onPersonMentionRead={this.handlePersonMentionRead}
536 onBanPersonFromCommunity={this.handleBanFromCommunity}
537 onBanPerson={this.handleBanPerson}
538 onCreateComment={this.handleCreateComment}
539 onEditComment={this.handleEditComment}
547 const res = this.state.postRes;
548 if (res.state === "success") {
550 <div className="mb-3">
552 community_view={res.data.community_view}
553 moderators={res.data.moderators}
554 admins={this.state.siteRes.admins}
555 online={res.data.online}
556 enableNsfw={enableNsfw(this.state.siteRes)}
558 allLanguages={this.state.siteRes.all_languages}
559 siteLanguages={this.state.siteRes.discussion_languages}
560 onDeleteCommunity={this.handleDeleteCommunityClick}
561 onLeaveModTeam={this.handleAddModToCommunity}
562 onFollowCommunity={this.handleFollow}
563 onRemoveCommunity={this.handleModRemoveCommunity}
564 onPurgeCommunity={this.handlePurgeCommunity}
565 onBlockCommunity={this.handleBlockCommunity}
566 onEditCommunity={this.handleEditCommunity}
574 const res = this.state.postRes;
575 const firstComment = this.commentTree().at(0)?.comment_view.comment;
576 const depth = getDepthFromComment(firstComment);
577 const showContextButton = depth ? depth > 0 : false;
580 res.state == "success" && (
582 {!!this.state.commentId && (
585 className="pl-0 d-block btn btn-link text-muted"
586 onClick={linkEvent(this, this.handleViewPost)}
588 {i18n.t("view_all_comments")} âž”
590 {showContextButton && (
592 className="pl-0 d-block btn btn-link text-muted"
593 onClick={linkEvent(this, this.handleViewContext)}
595 {i18n.t("show_context")} âž”
601 nodes={this.commentTree()}
602 viewType={this.state.commentViewType}
603 maxCommentsShown={this.state.maxCommentsShown}
604 locked={res.data.post_view.post.locked}
605 moderators={res.data.moderators}
606 admins={this.state.siteRes.admins}
607 enableDownvotes={enableDownvotes(this.state.siteRes)}
608 finished={this.state.finished}
609 allLanguages={this.state.siteRes.all_languages}
610 siteLanguages={this.state.siteRes.discussion_languages}
611 onSaveComment={this.handleSaveComment}
612 onBlockPerson={this.handleBlockPerson}
613 onDeleteComment={this.handleDeleteComment}
614 onRemoveComment={this.handleRemoveComment}
615 onCommentVote={this.handleCommentVote}
616 onCommentReport={this.handleCommentReport}
617 onDistinguishComment={this.handleDistinguishComment}
618 onAddModToCommunity={this.handleAddModToCommunity}
619 onAddAdmin={this.handleAddAdmin}
620 onTransferCommunity={this.handleTransferCommunity}
621 onFetchChildren={this.handleFetchChildren}
622 onPurgeComment={this.handlePurgeComment}
623 onPurgePerson={this.handlePurgePerson}
624 onCommentReplyRead={this.handleCommentReplyRead}
625 onPersonMentionRead={this.handlePersonMentionRead}
626 onBanPersonFromCommunity={this.handleBanFromCommunity}
627 onBanPerson={this.handleBanPerson}
628 onCreateComment={this.handleCreateComment}
629 onEditComment={this.handleEditComment}
636 commentTree(): CommentNodeI[] {
637 if (this.state.commentsRes.state == "success") {
638 return buildCommentsTree(
639 this.state.commentsRes.data.comments,
640 !!this.state.commentId
647 async handleCommentSortChange(i: Post, event: any) {
649 commentSort: event.target.value as CommentSortType,
650 commentViewType: CommentViewType.Tree,
651 commentsRes: { state: "loading" },
652 postRes: { state: "loading" },
657 handleCommentViewTypeChange(i: Post, event: any) {
659 commentViewType: Number(event.target.value),
664 handleShowSidebarMobile(i: Post) {
665 i.setState({ showSidebarMobile: !i.state.showSidebarMobile });
668 handleViewPost(i: Post) {
669 if (i.state.postRes.state == "success") {
670 const id = i.state.postRes.data.post_view.post.id;
671 i.context.router.history.push(`/post/${id}`);
675 handleViewContext(i: Post) {
676 if (i.state.commentsRes.state == "success") {
677 const parentId = getCommentParentId(
678 i.state.commentsRes.data.comments.at(0)?.comment
681 i.context.router.history.push(`/comment/${parentId}`);
686 async handleDeleteCommunityClick(form: DeleteCommunity) {
687 const deleteCommunityRes = await HttpService.client.deleteCommunity(form);
688 this.updateCommunity(deleteCommunityRes);
691 async handleAddModToCommunity(form: AddModToCommunity) {
692 const addModRes = await HttpService.client.addModToCommunity(form);
693 this.updateModerators(addModRes);
696 async handleFollow(form: FollowCommunity) {
697 const followCommunityRes = await HttpService.client.followCommunity(form);
698 this.updateCommunity(followCommunityRes);
701 if (followCommunityRes.state === "success") {
702 const communityId = followCommunityRes.data.community_view.community.id;
703 const mui = UserService.Instance.myUserInfo;
705 mui.follows = mui.follows.filter(i => i.community.id != communityId);
710 async handlePurgeCommunity(form: PurgeCommunity) {
711 const purgeCommunityRes = await HttpService.client.purgeCommunity(form);
712 this.purgeItem(purgeCommunityRes);
715 async handlePurgePerson(form: PurgePerson) {
716 const purgePersonRes = await HttpService.client.purgePerson(form);
717 this.purgeItem(purgePersonRes);
720 async handlePurgeComment(form: PurgeComment) {
721 const purgeCommentRes = await HttpService.client.purgeComment(form);
722 this.purgeItem(purgeCommentRes);
725 async handlePurgePost(form: PurgePost) {
726 const purgeRes = await HttpService.client.purgePost(form);
727 this.purgeItem(purgeRes);
730 async handleBlockCommunity(form: BlockCommunity) {
731 const blockCommunityRes = await HttpService.client.blockCommunity(form);
732 // TODO Probably isn't necessary
735 s.postRes.state == "success" &&
736 blockCommunityRes.state == "success"
738 s.postRes.data.community_view = blockCommunityRes.data.community_view;
743 if (blockCommunityRes.state == "success") {
744 updateCommunityBlock(blockCommunityRes.data);
748 async handleBlockPerson(form: BlockPerson) {
749 const blockPersonRes = await HttpService.client.blockPerson(form);
750 if (blockPersonRes.state == "success") {
751 updatePersonBlock(blockPersonRes.data);
755 async handleModRemoveCommunity(form: RemoveCommunity) {
756 const removeCommunityRes = await HttpService.client.removeCommunity(form);
757 this.updateCommunity(removeCommunityRes);
760 async handleEditCommunity(form: EditCommunity) {
761 const res = await HttpService.client.editCommunity(form);
762 this.updateCommunity(res);
767 async handleCreateComment(form: CreateComment) {
768 const createCommentRes = await HttpService.client.createComment(form);
769 this.createAndUpdateComments(createCommentRes);
771 return createCommentRes;
774 async handleEditComment(form: EditComment) {
775 const editCommentRes = await HttpService.client.editComment(form);
776 this.findAndUpdateComment(editCommentRes);
778 return editCommentRes;
781 async handleDeleteComment(form: DeleteComment) {
782 const deleteCommentRes = await HttpService.client.deleteComment(form);
783 this.findAndUpdateComment(deleteCommentRes);
786 async handleDeletePost(form: DeletePost) {
787 const deleteRes = await HttpService.client.deletePost(form);
788 this.updatePost(deleteRes);
791 async handleRemovePost(form: RemovePost) {
792 const removeRes = await HttpService.client.removePost(form);
793 this.updatePost(removeRes);
796 async handleRemoveComment(form: RemoveComment) {
797 const removeCommentRes = await HttpService.client.removeComment(form);
798 this.findAndUpdateComment(removeCommentRes);
801 async handleSaveComment(form: SaveComment) {
802 const saveCommentRes = await HttpService.client.saveComment(form);
803 this.findAndUpdateComment(saveCommentRes);
806 async handleSavePost(form: SavePost) {
807 const saveRes = await HttpService.client.savePost(form);
808 this.updatePost(saveRes);
811 async handleFeaturePost(form: FeaturePost) {
812 const featureRes = await HttpService.client.featurePost(form);
813 this.updatePost(featureRes);
816 async handleCommentVote(form: CreateCommentLike) {
817 const voteRes = await HttpService.client.likeComment(form);
818 this.findAndUpdateComment(voteRes);
821 async handlePostVote(form: CreatePostLike) {
822 const voteRes = await HttpService.client.likePost(form);
823 this.updatePost(voteRes);
826 async handlePostEdit(form: EditPost) {
827 const res = await HttpService.client.editPost(form);
828 this.updatePost(res);
831 async handleCommentReport(form: CreateCommentReport) {
832 const reportRes = await HttpService.client.createCommentReport(form);
833 if (reportRes.state == "success") {
834 toast(i18n.t("report_created"));
838 async handlePostReport(form: CreatePostReport) {
839 const reportRes = await HttpService.client.createPostReport(form);
840 if (reportRes.state == "success") {
841 toast(i18n.t("report_created"));
845 async handleLockPost(form: LockPost) {
846 const lockRes = await HttpService.client.lockPost(form);
847 this.updatePost(lockRes);
850 async handleDistinguishComment(form: DistinguishComment) {
851 const distinguishRes = await HttpService.client.distinguishComment(form);
852 this.findAndUpdateComment(distinguishRes);
855 async handleAddAdmin(form: AddAdmin) {
856 const addAdminRes = await HttpService.client.addAdmin(form);
858 if (addAdminRes.state === "success") {
859 this.setState(s => ((s.siteRes.admins = addAdminRes.data.admins), s));
863 async handleTransferCommunity(form: TransferCommunity) {
864 const transferCommunityRes = await HttpService.client.transferCommunity(
867 this.updateCommunityFull(transferCommunityRes);
870 async handleFetchChildren(form: GetComments) {
871 const moreCommentsRes = await HttpService.client.getComments(form);
873 this.state.commentsRes.state == "success" &&
874 moreCommentsRes.state == "success"
876 const newComments = moreCommentsRes.data.comments;
877 // Remove the first comment, since it is the parent
879 const newRes = this.state.commentsRes;
880 newRes.data.comments.push(...newComments);
881 this.setState({ commentsRes: newRes });
885 async handleCommentReplyRead(form: MarkCommentReplyAsRead) {
886 const readRes = await HttpService.client.markCommentReplyAsRead(form);
887 this.findAndUpdateCommentReply(readRes);
890 async handlePersonMentionRead(form: MarkPersonMentionAsRead) {
891 // TODO not sure what to do here. Maybe it is actually optional, because post doesn't need it.
892 await HttpService.client.markPersonMentionAsRead(form);
895 async handleBanFromCommunity(form: BanFromCommunity) {
896 const banRes = await HttpService.client.banFromCommunity(form);
897 this.updateBan(banRes);
900 async handleBanPerson(form: BanPerson) {
901 const banRes = await HttpService.client.banPerson(form);
902 this.updateBan(banRes);
905 updateBanFromCommunity(banRes: RequestState<BanFromCommunityResponse>) {
906 // Maybe not necessary
907 if (banRes.state == "success") {
910 s.postRes.state == "success" &&
911 s.postRes.data.post_view.creator.id ==
912 banRes.data.person_view.person.id
914 s.postRes.data.post_view.creator_banned_from_community =
917 if (s.commentsRes.state == "success") {
918 s.commentsRes.data.comments
919 .filter(c => c.creator.id == banRes.data.person_view.person.id)
921 c => (c.creator_banned_from_community = banRes.data.banned)
929 updateBan(banRes: RequestState<BanPersonResponse>) {
930 // Maybe not necessary
931 if (banRes.state == "success") {
934 s.postRes.state == "success" &&
935 s.postRes.data.post_view.creator.id ==
936 banRes.data.person_view.person.id
938 s.postRes.data.post_view.creator.banned = banRes.data.banned;
940 if (s.commentsRes.state == "success") {
941 s.commentsRes.data.comments
942 .filter(c => c.creator.id == banRes.data.person_view.person.id)
943 .forEach(c => (c.creator.banned = banRes.data.banned));
950 updateCommunity(communityRes: RequestState<CommunityResponse>) {
952 if (s.postRes.state == "success" && communityRes.state == "success") {
953 s.postRes.data.community_view = communityRes.data.community_view;
959 updateCommunityFull(res: RequestState<GetCommunityResponse>) {
961 if (s.postRes.state == "success" && res.state == "success") {
962 s.postRes.data.community_view = res.data.community_view;
963 s.postRes.data.moderators = res.data.moderators;
969 updatePost(post: RequestState<PostResponse>) {
971 if (s.postRes.state == "success" && post.state == "success") {
972 s.postRes.data.post_view = post.data.post_view;
978 purgeItem(purgeRes: RequestState<PurgeItemResponse>) {
979 if (purgeRes.state == "success") {
980 toast(i18n.t("purge_success"));
981 this.context.router.history.push(`/`);
985 createAndUpdateComments(res: RequestState<CommentResponse>) {
987 if (s.commentsRes.state === "success" && res.state === "success") {
988 s.commentsRes.data.comments.unshift(res.data.comment_view);
990 // Set finished for the parent
992 getCommentParentId(res.data.comment_view.comment) ?? 0,
1000 findAndUpdateComment(res: RequestState<CommentResponse>) {
1001 this.setState(s => {
1002 if (s.commentsRes.state == "success" && res.state == "success") {
1003 s.commentsRes.data.comments = editComment(
1004 res.data.comment_view,
1005 s.commentsRes.data.comments
1007 s.finished.set(res.data.comment_view.comment.id, true);
1013 findAndUpdateCommentReply(res: RequestState<CommentReplyResponse>) {
1014 this.setState(s => {
1015 if (s.commentsRes.state == "success" && res.state == "success") {
1016 s.commentsRes.data.comments = editWith(
1017 res.data.comment_reply_view,
1018 s.commentsRes.data.comments
1025 updateModerators(res: RequestState<AddModToCommunityResponse>) {
1026 // Update the moderators
1027 this.setState(s => {
1028 if (s.postRes.state == "success" && res.state == "success") {
1029 s.postRes.data.moderators = res.data.moderators;