1 import autosize from "autosize";
2 import { Component, createRef, linkEvent, RefObject } from "inferno";
5 AddModToCommunityResponse,
6 BanFromCommunityResponse,
28 } from "lemmy-js-client";
29 import { Subscription } from "rxjs";
30 import { i18n } from "../../i18next";
35 } from "../../interfaces";
36 import { UserService, WebSocketService } from "../../services";
47 getCommentIdFromProps,
51 insertCommentIntoTree,
55 restoreScrollPosition,
66 import { CommentForm } from "../comment/comment-form";
67 import { CommentNodes } from "../comment/comment-nodes";
68 import { HtmlTags } from "../common/html-tags";
69 import { Icon, Spinner } from "../common/icon";
70 import { Sidebar } from "../community/sidebar";
71 import { PostListing } from "./post-listing";
73 const commentsShownInterval = 15;
78 postRes?: GetPostResponse;
79 commentsRes?: GetCommentsResponse;
80 commentTree: CommentNodeI[];
81 commentSort: CommentSortType;
82 commentViewType: CommentViewType;
85 crossPosts?: PostView[];
86 siteRes: GetSiteResponse;
87 commentSectionRef?: RefObject<HTMLDivElement>;
88 showSidebarMobile: boolean;
89 maxCommentsShown: number;
92 export class Post extends Component<any, PostState> {
93 private subscription?: Subscription;
94 private isoData = setIsoData(this.context);
95 private commentScrollDebounced: () => void;
97 postId: getIdFromProps(this.props),
98 commentId: getCommentIdFromProps(this.props),
101 commentViewType: CommentViewType.Tree,
104 siteRes: this.isoData.site_res,
105 showSidebarMobile: false,
106 maxCommentsShown: commentsShownInterval,
109 constructor(props: any, context: any) {
110 super(props, context);
112 this.parseMessage = this.parseMessage.bind(this);
113 this.subscription = wsSubscribe(this.parseMessage);
115 this.state = { ...this.state, commentSectionRef: createRef() };
117 // Only fetch the data if coming from another route
118 if (this.isoData.path == this.context.router.route.match.url) {
121 postRes: this.isoData.routeData[0] as GetPostResponse,
122 commentsRes: this.isoData.routeData[1] as GetCommentsResponse,
125 if (this.state.commentsRes) {
128 commentTree: buildCommentsTree(
129 this.state.commentsRes.comments,
130 !!this.state.commentId
135 this.state = { ...this.state, loading: false };
138 if (this.state.postRes) {
139 WebSocketService.Instance.send(
140 wsClient.communityJoin({
141 community_id: this.state.postRes.community_view.community.id,
146 if (this.state.postId) {
147 WebSocketService.Instance.send(
148 wsClient.postJoin({ post_id: this.state.postId })
152 this.fetchCrossPosts();
154 if (this.checkScrollIntoCommentsParam) {
155 this.scrollIntoCommentSection();
164 let auth = myAuth(false);
165 let postForm: GetPost = {
166 id: this.state.postId,
167 comment_id: this.state.commentId,
170 WebSocketService.Instance.send(wsClient.getPost(postForm));
172 let commentsForm: GetComments = {
173 post_id: this.state.postId,
174 parent_id: this.state.commentId,
175 max_depth: commentTreeMaxDepth,
176 sort: this.state.commentSort,
181 WebSocketService.Instance.send(wsClient.getComments(commentsForm));
185 let q = this.state.postRes?.post_view.post.url;
193 limit: trendingFetchLimit,
196 WebSocketService.Instance.send(wsClient.search(form));
200 static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
201 let pathSplit = req.path.split("/");
202 let promises: Promise<any>[] = [];
204 let pathType = pathSplit.at(1);
205 let id = pathSplit.at(2) ? Number(pathSplit.at(2)) : undefined;
208 let postForm: GetPost = {
212 let commentsForm: GetComments = {
213 max_depth: commentTreeMaxDepth,
220 // Set the correct id based on the path type
221 if (pathType == "post") {
223 commentsForm.post_id = id;
225 postForm.comment_id = id;
226 commentsForm.parent_id = id;
229 promises.push(req.client.getPost(postForm));
230 promises.push(req.client.getComments(commentsForm));
235 componentWillUnmount() {
236 this.subscription?.unsubscribe();
237 document.removeEventListener("scroll", this.commentScrollDebounced);
239 saveScrollPosition(this.context);
242 componentDidMount() {
243 autosize(document.querySelectorAll("textarea"));
245 this.commentScrollDebounced = debounce(this.trackCommentsBoxScrolling, 100);
246 document.addEventListener("scroll", this.commentScrollDebounced);
249 componentDidUpdate(_lastProps: any) {
250 // Necessary if you are on a post and you click another post (same route)
251 if (_lastProps.location.pathname !== _lastProps.history.location.pathname) {
252 // TODO Couldnt get a refresh working. This does for now.
255 // let currentId = this.props.match.params.id;
256 // WebSocketService.Instance.getPost(currentId);
257 // this.context.refresh();
258 // this.context.router.history.push(_lastProps.location.pathname);
262 get checkScrollIntoCommentsParam() {
264 new URLSearchParams(this.props.location.search).get("scrollToComments")
268 scrollIntoCommentSection() {
269 this.state.commentSectionRef?.current?.scrollIntoView();
272 isBottom(el: Element): boolean {
273 return el?.getBoundingClientRect().bottom <= window.innerHeight;
277 * Shows new comments when scrolling to the bottom of the comments div
279 trackCommentsBoxScrolling = () => {
280 const wrappedElement = document.getElementsByClassName("comments")[0];
281 if (wrappedElement && this.isBottom(wrappedElement)) {
283 maxCommentsShown: this.state.maxCommentsShown + commentsShownInterval,
288 get documentTitle(): string {
289 let name_ = this.state.postRes?.post_view.post.name;
290 let siteName = this.state.siteRes.site_view.site.name;
291 return name_ ? `${name_} - ${siteName}` : "";
294 get imageTag(): string | undefined {
295 let post = this.state.postRes?.post_view.post;
296 let thumbnail = post?.thumbnail_url;
298 return thumbnail || (url && isImage(url) ? url : undefined);
302 let res = this.state.postRes;
303 let description = res?.post_view.post.body;
305 <div className="container-lg">
306 {this.state.loading ? (
312 <div className="row">
313 <div className="col-12 col-md-8 mb-3">
315 title={this.documentTitle}
316 path={this.context.router.route.match.url}
317 image={this.imageTag}
318 description={description}
321 post_view={res.post_view}
322 duplicates={this.state.crossPosts}
325 moderators={res.moderators}
326 admins={this.state.siteRes.admins}
327 enableDownvotes={enableDownvotes(this.state.siteRes)}
328 enableNsfw={enableNsfw(this.state.siteRes)}
329 allLanguages={this.state.siteRes.all_languages}
330 siteLanguages={this.state.siteRes.discussion_languages}
332 <div ref={this.state.commentSectionRef} className="mb-2" />
334 node={res.post_view.post.id}
335 disabled={res.post_view.post.locked}
336 allLanguages={this.state.siteRes.all_languages}
337 siteLanguages={this.state.siteRes.discussion_languages}
339 <div className="d-block d-md-none">
341 className="btn btn-secondary d-inline-block mb-2 mr-3"
342 onClick={linkEvent(this, this.handleShowSidebarMobile)}
344 {i18n.t("sidebar")}{" "}
347 this.state.showSidebarMobile
351 classes="icon-inline"
354 {this.state.showSidebarMobile && this.sidebar()}
357 {this.state.commentViewType == CommentViewType.Tree &&
359 {this.state.commentViewType == CommentViewType.Flat &&
362 <div className="d-none d-md-block col-md-4">{this.sidebar()}</div>
373 <div className="btn-group btn-group-toggle flex-wrap mr-3 mb-2">
375 className={`btn btn-outline-secondary pointer ${
376 this.state.commentSort === "Hot" && "active"
383 checked={this.state.commentSort === "Hot"}
384 onChange={linkEvent(this, this.handleCommentSortChange)}
388 className={`btn btn-outline-secondary pointer ${
389 this.state.commentSort === "Top" && "active"
396 checked={this.state.commentSort === "Top"}
397 onChange={linkEvent(this, this.handleCommentSortChange)}
401 className={`btn btn-outline-secondary pointer ${
402 this.state.commentSort === "New" && "active"
409 checked={this.state.commentSort === "New"}
410 onChange={linkEvent(this, this.handleCommentSortChange)}
414 className={`btn btn-outline-secondary pointer ${
415 this.state.commentSort === "Old" && "active"
422 checked={this.state.commentSort === "Old"}
423 onChange={linkEvent(this, this.handleCommentSortChange)}
427 <div className="btn-group btn-group-toggle flex-wrap mb-2">
429 className={`btn btn-outline-secondary pointer ${
430 this.state.commentViewType === CommentViewType.Flat && "active"
436 value={CommentViewType.Flat}
437 checked={this.state.commentViewType === CommentViewType.Flat}
438 onChange={linkEvent(this, this.handleCommentViewTypeChange)}
447 // These are already sorted by new
448 let commentsRes = this.state.commentsRes;
449 let postRes = this.state.postRes;
455 nodes={commentsToFlatNodes(commentsRes.comments)}
456 viewType={this.state.commentViewType}
457 maxCommentsShown={this.state.maxCommentsShown}
459 locked={postRes.post_view.post.locked}
460 moderators={postRes.moderators}
461 admins={this.state.siteRes.admins}
462 enableDownvotes={enableDownvotes(this.state.siteRes)}
464 allLanguages={this.state.siteRes.all_languages}
465 siteLanguages={this.state.siteRes.discussion_languages}
473 let res = this.state.postRes;
476 <div className="mb-3">
478 community_view={res.community_view}
479 moderators={res.moderators}
480 admins={this.state.siteRes.admins}
482 enableNsfw={enableNsfw(this.state.siteRes)}
484 allLanguages={this.state.siteRes.all_languages}
485 siteLanguages={this.state.siteRes.discussion_languages}
492 handleCommentSortChange(i: Post, event: any) {
494 commentSort: event.target.value as CommentSortType,
495 commentViewType: CommentViewType.Tree,
496 commentsRes: undefined,
502 handleCommentViewTypeChange(i: Post, event: any) {
503 let comments = i.state.commentsRes?.comments;
506 commentViewType: Number(event.target.value),
508 commentTree: buildCommentsTree(comments, !!i.state.commentId),
513 handleShowSidebarMobile(i: Post) {
514 i.setState({ showSidebarMobile: !i.state.showSidebarMobile });
517 handleViewPost(i: Post) {
518 let id = i.state.postRes?.post_view.post.id;
520 i.context.router.history.push(`/post/${id}`);
524 handleViewContext(i: Post) {
525 let parentId = getCommentParentId(
526 i.state.commentsRes?.comments?.at(0)?.comment
529 i.context.router.history.push(`/comment/${parentId}`);
534 let res = this.state.postRes;
535 let firstComment = this.state.commentTree.at(0)?.comment_view.comment;
536 let depth = getDepthFromComment(firstComment);
537 let showContextButton = depth ? depth > 0 : false;
542 {!!this.state.commentId && (
545 className="pl-0 d-block btn btn-link text-muted"
546 onClick={linkEvent(this, this.handleViewPost)}
548 {i18n.t("view_all_comments")} ➔
550 {showContextButton && (
552 className="pl-0 d-block btn btn-link text-muted"
553 onClick={linkEvent(this, this.handleViewContext)}
555 {i18n.t("show_context")} ➔
561 nodes={this.state.commentTree}
562 viewType={this.state.commentViewType}
563 maxCommentsShown={this.state.maxCommentsShown}
564 locked={res.post_view.post.locked}
565 moderators={res.moderators}
566 admins={this.state.siteRes.admins}
567 enableDownvotes={enableDownvotes(this.state.siteRes)}
568 allLanguages={this.state.siteRes.all_languages}
569 siteLanguages={this.state.siteRes.discussion_languages}
576 parseMessage(msg: any) {
577 let op = wsUserOp(msg);
580 toast(i18n.t(msg.error), "danger");
582 } else if (msg.reconnect) {
583 let post_id = this.state.postRes?.post_view.post.id;
585 WebSocketService.Instance.send(wsClient.postJoin({ post_id }));
586 WebSocketService.Instance.send(
593 } else if (op == UserOperation.GetPost) {
594 let data = wsJsonToRes<GetPostResponse>(msg);
595 this.setState({ postRes: data });
598 WebSocketService.Instance.send(
599 wsClient.postJoin({ post_id: data.post_view.post.id })
601 WebSocketService.Instance.send(
602 wsClient.communityJoin({
603 community_id: data.community_view.community.id,
608 // TODO move this into initial fetch and refetch
609 this.fetchCrossPosts();
611 if (!this.state.commentId) restoreScrollPosition(this.context);
613 if (this.checkScrollIntoCommentsParam) {
614 this.scrollIntoCommentSection();
616 } else if (op == UserOperation.GetComments) {
617 let data = wsJsonToRes<GetCommentsResponse>(msg);
618 // This section sets the comments res
619 let comments = this.state.commentsRes?.comments;
621 // You might need to append here, since this could be building more comments from a tree fetch
622 // Remove the first comment, since it is the parent
623 let newComments = data.comments;
625 comments.push(...newComments);
627 this.setState({ commentsRes: data });
630 let cComments = this.state.commentsRes?.comments ?? [];
632 commentTree: buildCommentsTree(cComments, !!this.state.commentId),
635 } else if (op == UserOperation.CreateComment) {
636 let data = wsJsonToRes<CommentResponse>(msg);
638 // Don't get comments from the post room, if the creator is blocked
639 let creatorBlocked = UserService.Instance.myUserInfo?.person_blocks
640 .map(pb => pb.target.id)
641 .includes(data.comment_view.creator.id);
643 // Necessary since it might be a user reply, which has the recipients, to avoid double
644 let postRes = this.state.postRes;
645 let commentsRes = this.state.commentsRes;
647 data.recipient_ids.length == 0 &&
650 data.comment_view.post.id == postRes.post_view.post.id &&
653 commentsRes.comments.unshift(data.comment_view);
654 insertCommentIntoTree(
655 this.state.commentTree,
657 !!this.state.commentId
659 postRes.post_view.counts.comments++;
661 this.setState(this.state);
665 op == UserOperation.EditComment ||
666 op == UserOperation.DeleteComment ||
667 op == UserOperation.RemoveComment
669 let data = wsJsonToRes<CommentResponse>(msg);
670 editCommentRes(data.comment_view, this.state.commentsRes?.comments);
671 this.setState(this.state);
673 } else if (op == UserOperation.SaveComment) {
674 let data = wsJsonToRes<CommentResponse>(msg);
675 saveCommentRes(data.comment_view, this.state.commentsRes?.comments);
676 this.setState(this.state);
678 } else if (op == UserOperation.CreateCommentLike) {
679 let data = wsJsonToRes<CommentResponse>(msg);
680 createCommentLikeRes(data.comment_view, this.state.commentsRes?.comments);
681 this.setState(this.state);
682 } else if (op == UserOperation.CreatePostLike) {
683 let data = wsJsonToRes<PostResponse>(msg);
684 createPostLikeRes(data.post_view, this.state.postRes?.post_view);
685 this.setState(this.state);
687 op == UserOperation.EditPost ||
688 op == UserOperation.DeletePost ||
689 op == UserOperation.RemovePost ||
690 op == UserOperation.LockPost ||
691 op == UserOperation.FeaturePost ||
692 op == UserOperation.SavePost
694 let data = wsJsonToRes<PostResponse>(msg);
695 let res = this.state.postRes;
697 res.post_view = data.post_view;
698 this.setState(this.state);
702 op == UserOperation.EditCommunity ||
703 op == UserOperation.DeleteCommunity ||
704 op == UserOperation.RemoveCommunity ||
705 op == UserOperation.FollowCommunity
707 let data = wsJsonToRes<CommunityResponse>(msg);
708 let res = this.state.postRes;
710 res.community_view = data.community_view;
711 res.post_view.community = data.community_view.community;
712 this.setState(this.state);
714 } else if (op == UserOperation.BanFromCommunity) {
715 let data = wsJsonToRes<BanFromCommunityResponse>(msg);
717 let res = this.state.postRes;
719 if (res.post_view.creator.id == data.person_view.person.id) {
720 res.post_view.creator_banned_from_community = data.banned;
724 this.state.commentsRes?.comments
725 .filter(c => c.creator.id == data.person_view.person.id)
726 .forEach(c => (c.creator_banned_from_community = data.banned));
727 this.setState(this.state);
728 } else if (op == UserOperation.AddModToCommunity) {
729 let data = wsJsonToRes<AddModToCommunityResponse>(msg);
730 let res = this.state.postRes;
732 res.moderators = data.moderators;
733 this.setState(this.state);
735 } else if (op == UserOperation.BanPerson) {
736 let data = wsJsonToRes<BanPersonResponse>(msg);
737 this.state.commentsRes?.comments
738 .filter(c => c.creator.id == data.person_view.person.id)
739 .forEach(c => (c.creator.banned = data.banned));
741 let res = this.state.postRes;
743 if (res.post_view.creator.id == data.person_view.person.id) {
744 res.post_view.creator.banned = data.banned;
747 this.setState(this.state);
748 } else if (op == UserOperation.AddAdmin) {
749 let data = wsJsonToRes<AddAdminResponse>(msg);
750 this.setState(s => ((s.siteRes.admins = data.admins), s));
751 } else if (op == UserOperation.Search) {
752 let data = wsJsonToRes<SearchResponse>(msg);
753 let xPosts = data.posts.filter(
754 p => p.post.ap_id != this.state.postRes?.post_view.post.ap_id
756 this.setState({ crossPosts: xPosts.length > 0 ? xPosts : undefined });
757 } else if (op == UserOperation.LeaveAdmin) {
758 let data = wsJsonToRes<GetSiteResponse>(msg);
759 this.setState({ siteRes: data });
760 } else if (op == UserOperation.TransferCommunity) {
761 let data = wsJsonToRes<GetCommunityResponse>(msg);
762 let res = this.state.postRes;
764 res.community_view = data.community_view;
765 res.post_view.community = data.community_view.community;
766 res.moderators = data.moderators;
767 this.setState(this.state);
769 } else if (op == UserOperation.BlockPerson) {
770 let data = wsJsonToRes<BlockPersonResponse>(msg);
771 updatePersonBlock(data);
772 } else if (op == UserOperation.CreatePostReport) {
773 let data = wsJsonToRes<PostReportResponse>(msg);
775 toast(i18n.t("report_created"));
777 } else if (op == UserOperation.CreateCommentReport) {
778 let data = wsJsonToRes<CommentReportResponse>(msg);
780 toast(i18n.t("report_created"));
783 op == UserOperation.PurgePerson ||
784 op == UserOperation.PurgePost ||
785 op == UserOperation.PurgeComment ||
786 op == UserOperation.PurgeCommunity
788 let data = wsJsonToRes<PurgeItemResponse>(msg);
790 toast(i18n.t("purge_success"));
791 this.context.router.history.push(`/`);