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";
72 getCommentIdFromProps,
79 restoreScrollPosition,
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 onUpsertComment={this.handleCreateComment}
393 finished={this.state.finished.get(0)}
395 <div className="d-block d-md-none">
397 className="btn btn-secondary d-inline-block mb-2 mr-3"
398 onClick={linkEvent(this, this.handleShowSidebarMobile)}
400 {i18n.t("sidebar")}{" "}
403 this.state.showSidebarMobile
407 classes="icon-inline"
410 {this.state.showSidebarMobile && this.sidebar()}
413 {this.state.commentViewType == CommentViewType.Tree &&
415 {this.state.commentViewType == CommentViewType.Flat &&
418 <div className="d-none d-md-block col-md-4">{this.sidebar()}</div>
426 return <div className="container-lg">{this.renderPostRes()}</div>;
432 <div className="btn-group btn-group-toggle flex-wrap mr-3 mb-2">
434 className={`btn btn-outline-secondary pointer ${
435 this.state.commentSort === "Hot" && "active"
442 checked={this.state.commentSort === "Hot"}
443 onChange={linkEvent(this, this.handleCommentSortChange)}
447 className={`btn btn-outline-secondary pointer ${
448 this.state.commentSort === "Top" && "active"
455 checked={this.state.commentSort === "Top"}
456 onChange={linkEvent(this, this.handleCommentSortChange)}
460 className={`btn btn-outline-secondary pointer ${
461 this.state.commentSort === "New" && "active"
468 checked={this.state.commentSort === "New"}
469 onChange={linkEvent(this, this.handleCommentSortChange)}
473 className={`btn btn-outline-secondary pointer ${
474 this.state.commentSort === "Old" && "active"
481 checked={this.state.commentSort === "Old"}
482 onChange={linkEvent(this, this.handleCommentSortChange)}
486 <div className="btn-group btn-group-toggle flex-wrap mb-2">
488 className={`btn btn-outline-secondary pointer ${
489 this.state.commentViewType === CommentViewType.Flat && "active"
495 value={CommentViewType.Flat}
496 checked={this.state.commentViewType === CommentViewType.Flat}
497 onChange={linkEvent(this, this.handleCommentViewTypeChange)}
506 // These are already sorted by new
507 const commentsRes = this.state.commentsRes;
508 const postRes = this.state.postRes;
510 if (commentsRes.state == "success" && postRes.state == "success") {
514 nodes={commentsToFlatNodes(commentsRes.data.comments)}
515 viewType={this.state.commentViewType}
516 maxCommentsShown={this.state.maxCommentsShown}
518 locked={postRes.data.post_view.post.locked}
519 moderators={postRes.data.moderators}
520 admins={this.state.siteRes.admins}
521 enableDownvotes={enableDownvotes(this.state.siteRes)}
523 finished={this.state.finished}
524 allLanguages={this.state.siteRes.all_languages}
525 siteLanguages={this.state.siteRes.discussion_languages}
526 onSaveComment={this.handleSaveComment}
527 onBlockPerson={this.handleBlockPerson}
528 onDeleteComment={this.handleDeleteComment}
529 onRemoveComment={this.handleRemoveComment}
530 onCommentVote={this.handleCommentVote}
531 onCommentReport={this.handleCommentReport}
532 onDistinguishComment={this.handleDistinguishComment}
533 onAddModToCommunity={this.handleAddModToCommunity}
534 onAddAdmin={this.handleAddAdmin}
535 onTransferCommunity={this.handleTransferCommunity}
536 onFetchChildren={this.handleFetchChildren}
537 onPurgeComment={this.handlePurgeComment}
538 onPurgePerson={this.handlePurgePerson}
539 onCommentReplyRead={this.handleCommentReplyRead}
540 onPersonMentionRead={this.handlePersonMentionRead}
541 onBanPersonFromCommunity={this.handleBanFromCommunity}
542 onBanPerson={this.handleBanPerson}
543 onCreateComment={this.handleCreateComment}
544 onEditComment={this.handleEditComment}
552 const res = this.state.postRes;
553 if (res.state === "success") {
555 <div className="mb-3">
557 community_view={res.data.community_view}
558 moderators={res.data.moderators}
559 admins={this.state.siteRes.admins}
560 online={res.data.online}
561 enableNsfw={enableNsfw(this.state.siteRes)}
563 allLanguages={this.state.siteRes.all_languages}
564 siteLanguages={this.state.siteRes.discussion_languages}
565 onDeleteCommunity={this.handleDeleteCommunityClick}
566 onLeaveModTeam={this.handleAddModToCommunity}
567 onFollowCommunity={this.handleFollow}
568 onRemoveCommunity={this.handleModRemoveCommunity}
569 onPurgeCommunity={this.handlePurgeCommunity}
570 onBlockCommunity={this.handleBlockCommunity}
571 onEditCommunity={this.handleEditCommunity}
579 const res = this.state.postRes;
580 const firstComment = this.commentTree().at(0)?.comment_view.comment;
581 const depth = getDepthFromComment(firstComment);
582 const showContextButton = depth ? depth > 0 : false;
585 res.state == "success" && (
587 {!!this.state.commentId && (
590 className="pl-0 d-block btn btn-link text-muted"
591 onClick={linkEvent(this, this.handleViewPost)}
593 {i18n.t("view_all_comments")} âž”
595 {showContextButton && (
597 className="pl-0 d-block btn btn-link text-muted"
598 onClick={linkEvent(this, this.handleViewContext)}
600 {i18n.t("show_context")} âž”
606 nodes={this.commentTree()}
607 viewType={this.state.commentViewType}
608 maxCommentsShown={this.state.maxCommentsShown}
609 locked={res.data.post_view.post.locked}
610 moderators={res.data.moderators}
611 admins={this.state.siteRes.admins}
612 enableDownvotes={enableDownvotes(this.state.siteRes)}
613 finished={this.state.finished}
614 allLanguages={this.state.siteRes.all_languages}
615 siteLanguages={this.state.siteRes.discussion_languages}
616 onSaveComment={this.handleSaveComment}
617 onBlockPerson={this.handleBlockPerson}
618 onDeleteComment={this.handleDeleteComment}
619 onRemoveComment={this.handleRemoveComment}
620 onCommentVote={this.handleCommentVote}
621 onCommentReport={this.handleCommentReport}
622 onDistinguishComment={this.handleDistinguishComment}
623 onAddModToCommunity={this.handleAddModToCommunity}
624 onAddAdmin={this.handleAddAdmin}
625 onTransferCommunity={this.handleTransferCommunity}
626 onFetchChildren={this.handleFetchChildren}
627 onPurgeComment={this.handlePurgeComment}
628 onPurgePerson={this.handlePurgePerson}
629 onCommentReplyRead={this.handleCommentReplyRead}
630 onPersonMentionRead={this.handlePersonMentionRead}
631 onBanPersonFromCommunity={this.handleBanFromCommunity}
632 onBanPerson={this.handleBanPerson}
633 onCreateComment={this.handleCreateComment}
634 onEditComment={this.handleEditComment}
641 commentTree(): CommentNodeI[] {
642 if (this.state.commentsRes.state == "success") {
643 return buildCommentsTree(
644 this.state.commentsRes.data.comments,
645 !!this.state.commentId
652 async handleCommentSortChange(i: Post, event: any) {
654 commentSort: event.target.value as CommentSortType,
655 commentViewType: CommentViewType.Tree,
656 commentsRes: { state: "loading" },
657 postRes: { state: "loading" },
662 handleCommentViewTypeChange(i: Post, event: any) {
664 commentViewType: Number(event.target.value),
669 handleShowSidebarMobile(i: Post) {
670 i.setState({ showSidebarMobile: !i.state.showSidebarMobile });
673 handleViewPost(i: Post) {
674 if (i.state.postRes.state == "success") {
675 const id = i.state.postRes.data.post_view.post.id;
676 i.context.router.history.push(`/post/${id}`);
680 handleViewContext(i: Post) {
681 if (i.state.commentsRes.state == "success") {
682 const parentId = getCommentParentId(
683 i.state.commentsRes.data.comments.at(0)?.comment
686 i.context.router.history.push(`/comment/${parentId}`);
691 async handleDeleteCommunityClick(form: DeleteCommunity) {
692 const deleteCommunityRes = await HttpService.client.deleteCommunity(form);
693 this.updateCommunity(deleteCommunityRes);
696 async handleAddModToCommunity(form: AddModToCommunity) {
697 const addModRes = await HttpService.client.addModToCommunity(form);
698 this.updateModerators(addModRes);
701 async handleFollow(form: FollowCommunity) {
702 const followCommunityRes = await HttpService.client.followCommunity(form);
703 this.updateCommunity(followCommunityRes);
706 if (followCommunityRes.state === "success") {
707 const communityId = followCommunityRes.data.community_view.community.id;
708 const mui = UserService.Instance.myUserInfo;
710 mui.follows = mui.follows.filter(i => i.community.id != communityId);
715 async handlePurgeCommunity(form: PurgeCommunity) {
716 const purgeCommunityRes = await HttpService.client.purgeCommunity(form);
717 this.purgeItem(purgeCommunityRes);
720 async handlePurgePerson(form: PurgePerson) {
721 const purgePersonRes = await HttpService.client.purgePerson(form);
722 this.purgeItem(purgePersonRes);
725 async handlePurgeComment(form: PurgeComment) {
726 const purgeCommentRes = await HttpService.client.purgeComment(form);
727 this.purgeItem(purgeCommentRes);
730 async handlePurgePost(form: PurgePost) {
731 const purgeRes = await HttpService.client.purgePost(form);
732 this.purgeItem(purgeRes);
735 async handleBlockCommunity(form: BlockCommunity) {
736 const blockCommunityRes = await HttpService.client.blockCommunity(form);
737 // TODO Probably isn't necessary
740 s.postRes.state == "success" &&
741 blockCommunityRes.state == "success"
743 s.postRes.data.community_view = blockCommunityRes.data.community_view;
748 if (blockCommunityRes.state == "success") {
749 updateCommunityBlock(blockCommunityRes.data);
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;