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";
79 import { i18n } from "../../i18next";
84 } from "../../interfaces";
85 import { UserService } from "../../services";
86 import { FirstLoadService } from "../../services/FirstLoadService";
87 import { HttpService, RequestState } from "../../services/HttpService";
88 import { setupTippy } from "../../tippy";
89 import { toast } from "../../toast";
90 import { CommentForm } from "../comment/comment-form";
91 import { CommentNodes } from "../comment/comment-nodes";
92 import { HtmlTags } from "../common/html-tags";
93 import { Icon, Spinner } from "../common/icon";
94 import { Sidebar } from "../community/sidebar";
95 import { PostListing } from "./post-listing";
97 const commentsShownInterval = 15;
99 type PostData = RouteDataResponse<{
100 postRes: GetPostResponse;
101 commentsRes: GetCommentsResponse;
104 interface PostState {
107 postRes: RequestState<GetPostResponse>;
108 commentsRes: RequestState<GetCommentsResponse>;
109 commentSort: CommentSortType;
110 commentViewType: CommentViewType;
112 siteRes: GetSiteResponse;
113 commentSectionRef?: RefObject<HTMLDivElement>;
114 showSidebarMobile: boolean;
115 maxCommentsShown: number;
116 finished: Map<CommentId, boolean | undefined>;
117 isIsomorphic: boolean;
120 export class Post extends Component<any, PostState> {
121 private isoData = setIsoData<PostData>(this.context);
122 private commentScrollDebounced: () => void;
124 postRes: { state: "empty" },
125 commentsRes: { state: "empty" },
126 postId: getIdFromProps(this.props),
127 commentId: getCommentIdFromProps(this.props),
129 commentViewType: CommentViewType.Tree,
131 siteRes: this.isoData.site_res,
132 showSidebarMobile: false,
133 maxCommentsShown: commentsShownInterval,
138 constructor(props: any, context: any) {
139 super(props, context);
141 this.handleDeleteCommunityClick =
142 this.handleDeleteCommunityClick.bind(this);
143 this.handleEditCommunity = this.handleEditCommunity.bind(this);
144 this.handleFollow = this.handleFollow.bind(this);
145 this.handleModRemoveCommunity = this.handleModRemoveCommunity.bind(this);
146 this.handleCreateComment = this.handleCreateComment.bind(this);
147 this.handleEditComment = this.handleEditComment.bind(this);
148 this.handleSaveComment = this.handleSaveComment.bind(this);
149 this.handleBlockCommunity = this.handleBlockCommunity.bind(this);
150 this.handleBlockPerson = this.handleBlockPerson.bind(this);
151 this.handleDeleteComment = this.handleDeleteComment.bind(this);
152 this.handleRemoveComment = this.handleRemoveComment.bind(this);
153 this.handleCommentVote = this.handleCommentVote.bind(this);
154 this.handleAddModToCommunity = this.handleAddModToCommunity.bind(this);
155 this.handleAddAdmin = this.handleAddAdmin.bind(this);
156 this.handlePurgePerson = this.handlePurgePerson.bind(this);
157 this.handlePurgeComment = this.handlePurgeComment.bind(this);
158 this.handleCommentReport = this.handleCommentReport.bind(this);
159 this.handleDistinguishComment = this.handleDistinguishComment.bind(this);
160 this.handleTransferCommunity = this.handleTransferCommunity.bind(this);
161 this.handleFetchChildren = this.handleFetchChildren.bind(this);
162 this.handleCommentReplyRead = this.handleCommentReplyRead.bind(this);
163 this.handlePersonMentionRead = this.handlePersonMentionRead.bind(this);
164 this.handleBanFromCommunity = this.handleBanFromCommunity.bind(this);
165 this.handleBanPerson = this.handleBanPerson.bind(this);
166 this.handlePostEdit = this.handlePostEdit.bind(this);
167 this.handlePostVote = this.handlePostVote.bind(this);
168 this.handlePostReport = this.handlePostReport.bind(this);
169 this.handleLockPost = this.handleLockPost.bind(this);
170 this.handleDeletePost = this.handleDeletePost.bind(this);
171 this.handleRemovePost = this.handleRemovePost.bind(this);
172 this.handleSavePost = this.handleSavePost.bind(this);
173 this.handlePurgePost = this.handlePurgePost.bind(this);
174 this.handleFeaturePost = this.handleFeaturePost.bind(this);
176 this.state = { ...this.state, commentSectionRef: createRef() };
178 // Only fetch the data if coming from another route
179 if (FirstLoadService.isFirstLoad) {
180 const { commentsRes, postRes } = this.isoData.routeData;
190 if (this.checkScrollIntoCommentsParam) {
191 this.scrollIntoCommentSection();
199 postRes: { state: "loading" },
200 commentsRes: { state: "loading" },
203 const auth = myAuth();
206 postRes: await HttpService.client.getPost({
207 id: this.state.postId,
208 comment_id: this.state.commentId,
211 commentsRes: await HttpService.client.getComments({
212 post_id: this.state.postId,
213 parent_id: this.state.commentId,
214 max_depth: commentTreeMaxDepth,
215 sort: this.state.commentSort,
224 if (!this.state.commentId) restoreScrollPosition(this.context);
226 if (this.checkScrollIntoCommentsParam) {
227 this.scrollIntoCommentSection();
231 static async fetchInitialData({
235 }: InitialFetchRequest): Promise<PostData> {
236 const pathSplit = path.split("/");
238 const pathType = pathSplit.at(1);
239 const id = pathSplit.at(2) ? Number(pathSplit.at(2)) : undefined;
241 const postForm: GetPost = {
245 const commentsForm: GetComments = {
246 max_depth: commentTreeMaxDepth,
253 // Set the correct id based on the path type
254 if (pathType === "post") {
256 commentsForm.post_id = id;
258 postForm.comment_id = id;
259 commentsForm.parent_id = id;
263 postRes: await client.getPost(postForm),
264 commentsRes: await client.getComments(commentsForm),
268 componentWillUnmount() {
269 document.removeEventListener("scroll", this.commentScrollDebounced);
271 saveScrollPosition(this.context);
274 async componentDidMount() {
275 if (!this.state.isIsomorphic) {
276 await this.fetchPost();
279 autosize(document.querySelectorAll("textarea"));
281 this.commentScrollDebounced = debounce(this.trackCommentsBoxScrolling, 100);
282 document.addEventListener("scroll", this.commentScrollDebounced);
285 async componentDidUpdate(_lastProps: any) {
286 // Necessary if you are on a post and you click another post (same route)
287 if (_lastProps.location.pathname !== _lastProps.history.location.pathname) {
288 await this.fetchPost();
292 get checkScrollIntoCommentsParam() {
294 new URLSearchParams(this.props.location.search).get("scrollToComments")
298 scrollIntoCommentSection() {
299 this.state.commentSectionRef?.current?.scrollIntoView();
302 isBottom(el: Element): boolean {
303 return el?.getBoundingClientRect().bottom <= window.innerHeight;
307 * Shows new comments when scrolling to the bottom of the comments div
309 trackCommentsBoxScrolling = () => {
310 const wrappedElement = document.getElementsByClassName("comments")[0];
311 if (wrappedElement && this.isBottom(wrappedElement)) {
313 this.state.commentsRes.state == "success"
314 ? this.state.commentsRes.data.comments.length
317 if (this.state.maxCommentsShown < commentCount) {
319 maxCommentsShown: this.state.maxCommentsShown + commentsShownInterval,
325 get documentTitle(): string {
326 const siteName = this.state.siteRes.site_view.site.name;
327 return this.state.postRes.state == "success"
328 ? `${this.state.postRes.data.post_view.post.name} - ${siteName}`
332 get imageTag(): string | undefined {
333 if (this.state.postRes.state == "success") {
334 const post = this.state.postRes.data.post_view.post;
335 const thumbnail = post.thumbnail_url;
336 const url = post.url;
337 return thumbnail || (url && isImage(url) ? url : undefined);
338 } else return undefined;
342 switch (this.state.postRes.state) {
350 const res = this.state.postRes.data;
352 <div className="row">
353 <div className="col-12 col-md-8 mb-3">
355 title={this.documentTitle}
356 path={this.context.router.route.match.url}
357 image={this.imageTag}
358 description={res.post_view.post.body}
361 post_view={res.post_view}
362 crossPosts={res.cross_posts}
365 moderators={res.moderators}
366 admins={this.state.siteRes.admins}
367 enableDownvotes={enableDownvotes(this.state.siteRes)}
368 enableNsfw={enableNsfw(this.state.siteRes)}
369 allLanguages={this.state.siteRes.all_languages}
370 siteLanguages={this.state.siteRes.discussion_languages}
371 onBlockPerson={this.handleBlockPerson}
372 onPostEdit={this.handlePostEdit}
373 onPostVote={this.handlePostVote}
374 onPostReport={this.handlePostReport}
375 onLockPost={this.handleLockPost}
376 onDeletePost={this.handleDeletePost}
377 onRemovePost={this.handleRemovePost}
378 onSavePost={this.handleSavePost}
379 onPurgePerson={this.handlePurgePerson}
380 onPurgePost={this.handlePurgePost}
381 onBanPerson={this.handleBanPerson}
382 onBanPersonFromCommunity={this.handleBanFromCommunity}
383 onAddModToCommunity={this.handleAddModToCommunity}
384 onAddAdmin={this.handleAddAdmin}
385 onTransferCommunity={this.handleTransferCommunity}
386 onFeaturePost={this.handleFeaturePost}
388 <div ref={this.state.commentSectionRef} className="mb-2" />
390 node={res.post_view.post.id}
391 disabled={res.post_view.post.locked}
392 allLanguages={this.state.siteRes.all_languages}
393 siteLanguages={this.state.siteRes.discussion_languages}
394 containerClass="post-comment-container"
395 onUpsertComment={this.handleCreateComment}
396 finished={this.state.finished.get(0)}
398 <div className="d-block d-md-none">
400 className="btn btn-secondary d-inline-block mb-2 me-3"
401 onClick={linkEvent(this, this.handleShowSidebarMobile)}
403 {i18n.t("sidebar")}{" "}
406 this.state.showSidebarMobile
410 classes="icon-inline"
413 {this.state.showSidebarMobile && this.sidebar()}
416 {this.state.commentViewType == CommentViewType.Tree &&
418 {this.state.commentViewType == CommentViewType.Flat &&
421 <div className="d-none d-md-block col-md-4">{this.sidebar()}</div>
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"
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"
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"
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"
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"
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 {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 {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(i18n.t("report_created"));
843 async handlePostReport(form: CreatePostReport) {
844 const reportRes = await HttpService.client.createPostReport(form);
845 if (reportRes.state == "success") {
846 toast(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(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;