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,
86 import isBrowser from "../../utils/browser/is-browser";
87 import debounce from "../../utils/helpers/debounce";
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 mr-3"
399 onClick={linkEvent(this, this.handleShowSidebarMobile)}
401 {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="container-lg">{this.renderPostRes()}</div>;
433 <div className="btn-group btn-group-toggle flex-wrap mr-3 mb-2">
435 className={`btn btn-outline-secondary pointer ${
436 this.state.commentSort === "Hot" && "active"
443 checked={this.state.commentSort === "Hot"}
444 onChange={linkEvent(this, this.handleCommentSortChange)}
448 className={`btn btn-outline-secondary pointer ${
449 this.state.commentSort === "Top" && "active"
456 checked={this.state.commentSort === "Top"}
457 onChange={linkEvent(this, this.handleCommentSortChange)}
461 className={`btn btn-outline-secondary pointer ${
462 this.state.commentSort === "New" && "active"
469 checked={this.state.commentSort === "New"}
470 onChange={linkEvent(this, this.handleCommentSortChange)}
474 className={`btn btn-outline-secondary pointer ${
475 this.state.commentSort === "Old" && "active"
482 checked={this.state.commentSort === "Old"}
483 onChange={linkEvent(this, this.handleCommentSortChange)}
487 <div className="btn-group btn-group-toggle flex-wrap mb-2">
489 className={`btn btn-outline-secondary pointer ${
490 this.state.commentViewType === CommentViewType.Flat && "active"
496 value={CommentViewType.Flat}
497 checked={this.state.commentViewType === CommentViewType.Flat}
498 onChange={linkEvent(this, this.handleCommentViewTypeChange)}
507 // These are already sorted by new
508 const commentsRes = this.state.commentsRes;
509 const postRes = this.state.postRes;
511 if (commentsRes.state == "success" && postRes.state == "success") {
515 nodes={commentsToFlatNodes(commentsRes.data.comments)}
516 viewType={this.state.commentViewType}
517 maxCommentsShown={this.state.maxCommentsShown}
519 locked={postRes.data.post_view.post.locked}
520 moderators={postRes.data.moderators}
521 admins={this.state.siteRes.admins}
522 enableDownvotes={enableDownvotes(this.state.siteRes)}
524 finished={this.state.finished}
525 allLanguages={this.state.siteRes.all_languages}
526 siteLanguages={this.state.siteRes.discussion_languages}
527 onSaveComment={this.handleSaveComment}
528 onBlockPerson={this.handleBlockPerson}
529 onDeleteComment={this.handleDeleteComment}
530 onRemoveComment={this.handleRemoveComment}
531 onCommentVote={this.handleCommentVote}
532 onCommentReport={this.handleCommentReport}
533 onDistinguishComment={this.handleDistinguishComment}
534 onAddModToCommunity={this.handleAddModToCommunity}
535 onAddAdmin={this.handleAddAdmin}
536 onTransferCommunity={this.handleTransferCommunity}
537 onFetchChildren={this.handleFetchChildren}
538 onPurgeComment={this.handlePurgeComment}
539 onPurgePerson={this.handlePurgePerson}
540 onCommentReplyRead={this.handleCommentReplyRead}
541 onPersonMentionRead={this.handlePersonMentionRead}
542 onBanPersonFromCommunity={this.handleBanFromCommunity}
543 onBanPerson={this.handleBanPerson}
544 onCreateComment={this.handleCreateComment}
545 onEditComment={this.handleEditComment}
553 const res = this.state.postRes;
554 if (res.state === "success") {
557 community_view={res.data.community_view}
558 moderators={res.data.moderators}
559 admins={this.state.siteRes.admins}
560 enableNsfw={enableNsfw(this.state.siteRes)}
562 allLanguages={this.state.siteRes.all_languages}
563 siteLanguages={this.state.siteRes.discussion_languages}
564 onDeleteCommunity={this.handleDeleteCommunityClick}
565 onLeaveModTeam={this.handleAddModToCommunity}
566 onFollowCommunity={this.handleFollow}
567 onRemoveCommunity={this.handleModRemoveCommunity}
568 onPurgeCommunity={this.handlePurgeCommunity}
569 onBlockCommunity={this.handleBlockCommunity}
570 onEditCommunity={this.handleEditCommunity}
577 const res = this.state.postRes;
578 const firstComment = this.commentTree().at(0)?.comment_view.comment;
579 const depth = getDepthFromComment(firstComment);
580 const showContextButton = depth ? depth > 0 : false;
583 res.state == "success" && (
585 {!!this.state.commentId && (
588 className="pl-0 d-block btn btn-link text-muted"
589 onClick={linkEvent(this, this.handleViewPost)}
591 {i18n.t("view_all_comments")} âž”
593 {showContextButton && (
595 className="pl-0 d-block btn btn-link text-muted"
596 onClick={linkEvent(this, this.handleViewContext)}
598 {i18n.t("show_context")} âž”
604 nodes={this.commentTree()}
605 viewType={this.state.commentViewType}
606 maxCommentsShown={this.state.maxCommentsShown}
607 locked={res.data.post_view.post.locked}
608 moderators={res.data.moderators}
609 admins={this.state.siteRes.admins}
610 enableDownvotes={enableDownvotes(this.state.siteRes)}
611 finished={this.state.finished}
612 allLanguages={this.state.siteRes.all_languages}
613 siteLanguages={this.state.siteRes.discussion_languages}
614 onSaveComment={this.handleSaveComment}
615 onBlockPerson={this.handleBlockPerson}
616 onDeleteComment={this.handleDeleteComment}
617 onRemoveComment={this.handleRemoveComment}
618 onCommentVote={this.handleCommentVote}
619 onCommentReport={this.handleCommentReport}
620 onDistinguishComment={this.handleDistinguishComment}
621 onAddModToCommunity={this.handleAddModToCommunity}
622 onAddAdmin={this.handleAddAdmin}
623 onTransferCommunity={this.handleTransferCommunity}
624 onFetchChildren={this.handleFetchChildren}
625 onPurgeComment={this.handlePurgeComment}
626 onPurgePerson={this.handlePurgePerson}
627 onCommentReplyRead={this.handleCommentReplyRead}
628 onPersonMentionRead={this.handlePersonMentionRead}
629 onBanPersonFromCommunity={this.handleBanFromCommunity}
630 onBanPerson={this.handleBanPerson}
631 onCreateComment={this.handleCreateComment}
632 onEditComment={this.handleEditComment}
639 commentTree(): CommentNodeI[] {
640 if (this.state.commentsRes.state == "success") {
641 return buildCommentsTree(
642 this.state.commentsRes.data.comments,
643 !!this.state.commentId
650 async handleCommentSortChange(i: Post, event: any) {
652 commentSort: event.target.value as CommentSortType,
653 commentViewType: CommentViewType.Tree,
654 commentsRes: { state: "loading" },
655 postRes: { state: "loading" },
660 handleCommentViewTypeChange(i: Post, event: any) {
662 commentViewType: Number(event.target.value),
667 handleShowSidebarMobile(i: Post) {
668 i.setState({ showSidebarMobile: !i.state.showSidebarMobile });
671 handleViewPost(i: Post) {
672 if (i.state.postRes.state == "success") {
673 const id = i.state.postRes.data.post_view.post.id;
674 i.context.router.history.push(`/post/${id}`);
678 handleViewContext(i: Post) {
679 if (i.state.commentsRes.state == "success") {
680 const parentId = getCommentParentId(
681 i.state.commentsRes.data.comments.at(0)?.comment
684 i.context.router.history.push(`/comment/${parentId}`);
689 async handleDeleteCommunityClick(form: DeleteCommunity) {
690 const deleteCommunityRes = await HttpService.client.deleteCommunity(form);
691 this.updateCommunity(deleteCommunityRes);
694 async handleAddModToCommunity(form: AddModToCommunity) {
695 const addModRes = await HttpService.client.addModToCommunity(form);
696 this.updateModerators(addModRes);
699 async handleFollow(form: FollowCommunity) {
700 const followCommunityRes = await HttpService.client.followCommunity(form);
701 this.updateCommunity(followCommunityRes);
704 if (followCommunityRes.state === "success") {
705 const communityId = followCommunityRes.data.community_view.community.id;
706 const mui = UserService.Instance.myUserInfo;
708 mui.follows = mui.follows.filter(i => i.community.id != communityId);
713 async handlePurgeCommunity(form: PurgeCommunity) {
714 const purgeCommunityRes = await HttpService.client.purgeCommunity(form);
715 this.purgeItem(purgeCommunityRes);
718 async handlePurgePerson(form: PurgePerson) {
719 const purgePersonRes = await HttpService.client.purgePerson(form);
720 this.purgeItem(purgePersonRes);
723 async handlePurgeComment(form: PurgeComment) {
724 const purgeCommentRes = await HttpService.client.purgeComment(form);
725 this.purgeItem(purgeCommentRes);
728 async handlePurgePost(form: PurgePost) {
729 const purgeRes = await HttpService.client.purgePost(form);
730 this.purgeItem(purgeRes);
733 async handleBlockCommunity(form: BlockCommunity) {
734 const blockCommunityRes = await HttpService.client.blockCommunity(form);
735 if (blockCommunityRes.state == "success") {
736 updateCommunityBlock(blockCommunityRes.data);
738 if (s.postRes.state == "success") {
739 s.postRes.data.community_view.blocked =
740 blockCommunityRes.data.blocked;
746 async handleBlockPerson(form: BlockPerson) {
747 const blockPersonRes = await HttpService.client.blockPerson(form);
748 if (blockPersonRes.state == "success") {
749 updatePersonBlock(blockPersonRes.data);
753 async handleModRemoveCommunity(form: RemoveCommunity) {
754 const removeCommunityRes = await HttpService.client.removeCommunity(form);
755 this.updateCommunity(removeCommunityRes);
758 async handleEditCommunity(form: EditCommunity) {
759 const res = await HttpService.client.editCommunity(form);
760 this.updateCommunity(res);
765 async handleCreateComment(form: CreateComment) {
766 const createCommentRes = await HttpService.client.createComment(form);
767 this.createAndUpdateComments(createCommentRes);
769 return createCommentRes;
772 async handleEditComment(form: EditComment) {
773 const editCommentRes = await HttpService.client.editComment(form);
774 this.findAndUpdateComment(editCommentRes);
776 return editCommentRes;
779 async handleDeleteComment(form: DeleteComment) {
780 const deleteCommentRes = await HttpService.client.deleteComment(form);
781 this.findAndUpdateComment(deleteCommentRes);
784 async handleDeletePost(form: DeletePost) {
785 const deleteRes = await HttpService.client.deletePost(form);
786 this.updatePost(deleteRes);
789 async handleRemovePost(form: RemovePost) {
790 const removeRes = await HttpService.client.removePost(form);
791 this.updatePost(removeRes);
794 async handleRemoveComment(form: RemoveComment) {
795 const removeCommentRes = await HttpService.client.removeComment(form);
796 this.findAndUpdateComment(removeCommentRes);
799 async handleSaveComment(form: SaveComment) {
800 const saveCommentRes = await HttpService.client.saveComment(form);
801 this.findAndUpdateComment(saveCommentRes);
804 async handleSavePost(form: SavePost) {
805 const saveRes = await HttpService.client.savePost(form);
806 this.updatePost(saveRes);
809 async handleFeaturePost(form: FeaturePost) {
810 const featureRes = await HttpService.client.featurePost(form);
811 this.updatePost(featureRes);
814 async handleCommentVote(form: CreateCommentLike) {
815 const voteRes = await HttpService.client.likeComment(form);
816 this.findAndUpdateComment(voteRes);
819 async handlePostVote(form: CreatePostLike) {
820 const voteRes = await HttpService.client.likePost(form);
821 this.updatePost(voteRes);
824 async handlePostEdit(form: EditPost) {
825 const res = await HttpService.client.editPost(form);
826 this.updatePost(res);
829 async handleCommentReport(form: CreateCommentReport) {
830 const reportRes = await HttpService.client.createCommentReport(form);
831 if (reportRes.state == "success") {
832 toast(i18n.t("report_created"));
836 async handlePostReport(form: CreatePostReport) {
837 const reportRes = await HttpService.client.createPostReport(form);
838 if (reportRes.state == "success") {
839 toast(i18n.t("report_created"));
843 async handleLockPost(form: LockPost) {
844 const lockRes = await HttpService.client.lockPost(form);
845 this.updatePost(lockRes);
848 async handleDistinguishComment(form: DistinguishComment) {
849 const distinguishRes = await HttpService.client.distinguishComment(form);
850 this.findAndUpdateComment(distinguishRes);
853 async handleAddAdmin(form: AddAdmin) {
854 const addAdminRes = await HttpService.client.addAdmin(form);
856 if (addAdminRes.state === "success") {
857 this.setState(s => ((s.siteRes.admins = addAdminRes.data.admins), s));
861 async handleTransferCommunity(form: TransferCommunity) {
862 const transferCommunityRes = await HttpService.client.transferCommunity(
865 this.updateCommunityFull(transferCommunityRes);
868 async handleFetchChildren(form: GetComments) {
869 const moreCommentsRes = await HttpService.client.getComments(form);
871 this.state.commentsRes.state == "success" &&
872 moreCommentsRes.state == "success"
874 const newComments = moreCommentsRes.data.comments;
875 // Remove the first comment, since it is the parent
877 const newRes = this.state.commentsRes;
878 newRes.data.comments.push(...newComments);
879 this.setState({ commentsRes: newRes });
883 async handleCommentReplyRead(form: MarkCommentReplyAsRead) {
884 const readRes = await HttpService.client.markCommentReplyAsRead(form);
885 this.findAndUpdateCommentReply(readRes);
888 async handlePersonMentionRead(form: MarkPersonMentionAsRead) {
889 // TODO not sure what to do here. Maybe it is actually optional, because post doesn't need it.
890 await HttpService.client.markPersonMentionAsRead(form);
893 async handleBanFromCommunity(form: BanFromCommunity) {
894 const banRes = await HttpService.client.banFromCommunity(form);
895 this.updateBan(banRes);
898 async handleBanPerson(form: BanPerson) {
899 const banRes = await HttpService.client.banPerson(form);
900 this.updateBan(banRes);
903 updateBanFromCommunity(banRes: RequestState<BanFromCommunityResponse>) {
904 // Maybe not necessary
905 if (banRes.state == "success") {
908 s.postRes.state == "success" &&
909 s.postRes.data.post_view.creator.id ==
910 banRes.data.person_view.person.id
912 s.postRes.data.post_view.creator_banned_from_community =
915 if (s.commentsRes.state == "success") {
916 s.commentsRes.data.comments
917 .filter(c => c.creator.id == banRes.data.person_view.person.id)
919 c => (c.creator_banned_from_community = banRes.data.banned)
927 updateBan(banRes: RequestState<BanPersonResponse>) {
928 // Maybe not necessary
929 if (banRes.state == "success") {
932 s.postRes.state == "success" &&
933 s.postRes.data.post_view.creator.id ==
934 banRes.data.person_view.person.id
936 s.postRes.data.post_view.creator.banned = banRes.data.banned;
938 if (s.commentsRes.state == "success") {
939 s.commentsRes.data.comments
940 .filter(c => c.creator.id == banRes.data.person_view.person.id)
941 .forEach(c => (c.creator.banned = banRes.data.banned));
948 updateCommunity(communityRes: RequestState<CommunityResponse>) {
950 if (s.postRes.state == "success" && communityRes.state == "success") {
951 s.postRes.data.community_view = communityRes.data.community_view;
957 updateCommunityFull(res: RequestState<GetCommunityResponse>) {
959 if (s.postRes.state == "success" && res.state == "success") {
960 s.postRes.data.community_view = res.data.community_view;
961 s.postRes.data.moderators = res.data.moderators;
967 updatePost(post: RequestState<PostResponse>) {
969 if (s.postRes.state == "success" && post.state == "success") {
970 s.postRes.data.post_view = post.data.post_view;
976 purgeItem(purgeRes: RequestState<PurgeItemResponse>) {
977 if (purgeRes.state == "success") {
978 toast(i18n.t("purge_success"));
979 this.context.router.history.push(`/`);
983 createAndUpdateComments(res: RequestState<CommentResponse>) {
985 if (s.commentsRes.state === "success" && res.state === "success") {
986 s.commentsRes.data.comments.unshift(res.data.comment_view);
988 // Set finished for the parent
990 getCommentParentId(res.data.comment_view.comment) ?? 0,
998 findAndUpdateComment(res: RequestState<CommentResponse>) {
1000 if (s.commentsRes.state == "success" && res.state == "success") {
1001 s.commentsRes.data.comments = editComment(
1002 res.data.comment_view,
1003 s.commentsRes.data.comments
1005 s.finished.set(res.data.comment_view.comment.id, true);
1011 findAndUpdateCommentReply(res: RequestState<CommentReplyResponse>) {
1012 this.setState(s => {
1013 if (s.commentsRes.state == "success" && res.state == "success") {
1014 s.commentsRes.data.comments = editWith(
1015 res.data.comment_reply_view,
1016 s.commentsRes.data.comments
1023 updateModerators(res: RequestState<AddModToCommunityResponse>) {
1024 // Update the moderators
1025 this.setState(s => {
1026 if (s.postRes.state == "success" && res.state == "success") {
1027 s.postRes.data.moderators = res.data.moderators;