1 import autosize from "autosize";
2 import { Component, createRef, linkEvent, RefObject } from "inferno";
5 AddModToCommunityResponse,
6 BanFromCommunityResponse,
24 } from "lemmy-js-client";
25 import { Subscription } from "rxjs";
26 import { i18n } from "../../i18next";
28 CommentNode as CommentNodeI,
32 } from "../../interfaces";
33 import { UserService, WebSocketService } from "../../services";
41 getCommentIdFromProps,
43 insertCommentIntoTree,
47 restoreScrollPosition,
60 import { CommentForm } from "../comment/comment-form";
61 import { CommentNodes } from "../comment/comment-nodes";
62 import { HtmlTags } from "../common/html-tags";
63 import { Icon, Spinner } from "../common/icon";
64 import { Sidebar } from "../community/sidebar";
65 import { PostListing } from "./post-listing";
68 postRes: GetPostResponse;
70 commentTree: CommentNodeI[];
72 commentSort: CommentSortType;
73 commentViewType: CommentViewType;
76 crossPosts: PostView[];
77 siteRes: GetSiteResponse;
78 commentSectionRef?: RefObject<HTMLDivElement>;
79 showSidebarMobile: boolean;
82 export class Post extends Component<any, PostState> {
83 private subscription: Subscription;
84 private isoData = setIsoData(this.context);
85 private emptyState: PostState = {
87 postId: getIdFromProps(this.props),
89 commentId: getCommentIdFromProps(this.props),
90 commentSort: CommentSortType.Hot,
91 commentViewType: CommentViewType.Tree,
95 siteRes: this.isoData.site_res,
96 commentSectionRef: null,
97 showSidebarMobile: false,
100 constructor(props: any, context: any) {
101 super(props, context);
103 this.state = this.emptyState;
104 this.state.commentSectionRef = createRef();
106 this.parseMessage = this.parseMessage.bind(this);
107 this.subscription = wsSubscribe(this.parseMessage);
109 // Only fetch the data if coming from another route
110 if (this.isoData.path == this.context.router.route.match.url) {
111 this.state.postRes = this.isoData.routeData[0];
112 this.state.commentTree = buildCommentsTree(
113 this.state.postRes.comments,
114 this.state.commentSort
116 this.state.loading = false;
119 this.fetchCrossPosts();
120 if (this.state.commentId) {
121 this.scrollCommentIntoView();
124 if (this.checkScrollIntoCommentsParam) {
125 this.scrollIntoCommentSection();
134 let form: GetPost = {
135 id: this.state.postId,
136 auth: authField(false),
138 WebSocketService.Instance.send(wsClient.getPost(form));
142 if (this.state.postRes.post_view.post.url) {
144 q: this.state.postRes.post_view.post.url,
145 type_: SearchType.Url,
146 sort: SortType.TopAll,
147 listing_type: ListingType.All,
150 auth: authField(false),
152 WebSocketService.Instance.send(wsClient.search(form));
156 static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
157 let pathSplit = req.path.split("/");
158 let promises: Promise<any>[] = [];
160 let id = Number(pathSplit[2]);
162 let postForm: GetPost = {
165 setOptionalAuth(postForm, req.auth);
167 promises.push(req.client.getPost(postForm));
172 componentWillUnmount() {
173 this.subscription.unsubscribe();
174 window.isoData.path = undefined;
175 saveScrollPosition(this.context);
178 componentDidMount() {
179 WebSocketService.Instance.send(
180 wsClient.postJoin({ post_id: this.state.postId })
182 autosize(document.querySelectorAll("textarea"));
185 componentDidUpdate(_lastProps: any, lastState: PostState) {
187 this.state.commentId &&
188 !this.state.scrolled &&
190 lastState.postRes.comments.length > 0
192 this.scrollCommentIntoView();
195 // Necessary if you are on a post and you click another post (same route)
196 if (_lastProps.location.pathname !== _lastProps.history.location.pathname) {
197 // TODO Couldnt get a refresh working. This does for now.
200 // let currentId = this.props.match.params.id;
201 // WebSocketService.Instance.getPost(currentId);
202 // this.context.refresh();
203 // this.context.router.history.push(_lastProps.location.pathname);
207 scrollCommentIntoView() {
208 var elmnt = document.getElementById(`comment-${this.state.commentId}`);
209 elmnt.scrollIntoView();
210 elmnt.classList.add("mark");
211 this.state.scrolled = true;
212 this.markScrolledAsRead(this.state.commentId);
215 get checkScrollIntoCommentsParam() {
217 new URLSearchParams(this.props.location.search).get("scrollToComments")
221 scrollIntoCommentSection() {
222 this.state.commentSectionRef.current?.scrollIntoView();
225 // TODO this needs some re-work
226 markScrolledAsRead(commentId: number) {
227 let found = this.state.postRes.comments.find(
228 c => c.comment.id == commentId
230 let parent = this.state.postRes.comments.find(
231 c => found.comment.parent_id == c.comment.id
233 let parent_person_id = parent
235 : this.state.postRes.post_view.creator.id;
238 UserService.Instance.myUserInfo &&
239 UserService.Instance.myUserInfo.local_user_view.person.id ==
242 let form: MarkCommentAsRead = {
243 comment_id: found.comment.id,
247 WebSocketService.Instance.send(wsClient.markCommentAsRead(form));
248 UserService.Instance.unreadCountSub.next(
249 UserService.Instance.unreadCountSub.value - 1
254 get documentTitle(): string {
255 return `${this.state.postRes.post_view.post.name} - ${this.state.siteRes.site_view.site.name}`;
258 get imageTag(): string {
259 let post = this.state.postRes.post_view.post;
261 post.thumbnail_url ||
262 (post.url ? (isImage(post.url) ? post.url : undefined) : undefined)
266 get descriptionTag(): string {
267 let body = this.state.postRes.post_view.post.body;
268 return body ? previewLines(body) : undefined;
272 let pv = this.state.postRes?.post_view;
274 <div class="container">
275 {this.state.loading ? (
281 <div class="col-12 col-md-8 mb-3">
283 title={this.documentTitle}
284 path={this.context.router.route.match.url}
285 image={this.imageTag}
286 description={this.descriptionTag}
290 duplicates={this.state.crossPosts}
293 moderators={this.state.postRes.moderators}
294 admins={this.state.siteRes.admins}
296 this.state.siteRes.site_view.site.enable_downvotes
298 enableNsfw={this.state.siteRes.site_view.site.enable_nsfw}
300 <div ref={this.state.commentSectionRef} className="mb-2" />
302 postId={this.state.postId}
303 disabled={pv.post.locked}
305 <div class="d-block d-md-none">
307 class="btn btn-secondary d-inline-block mb-2 mr-3"
308 onClick={linkEvent(this, this.handleShowSidebarMobile)}
310 {i18n.t("sidebar")}{" "}
313 this.state.showSidebarMobile
317 classes="icon-inline"
320 {this.state.showSidebarMobile && this.sidebar()}
322 {this.state.postRes.comments.length > 0 && this.sortRadios()}
323 {this.state.commentViewType == CommentViewType.Tree &&
325 {this.state.commentViewType == CommentViewType.Chat &&
328 <div class="d-none d-md-block col-md-4">{this.sidebar()}</div>
338 <div class="btn-group btn-group-toggle flex-wrap mr-3 mb-2">
340 className={`btn btn-outline-secondary pointer ${
341 this.state.commentSort === CommentSortType.Hot && "active"
347 value={CommentSortType.Hot}
348 checked={this.state.commentSort === CommentSortType.Hot}
349 onChange={linkEvent(this, this.handleCommentSortChange)}
353 className={`btn btn-outline-secondary pointer ${
354 this.state.commentSort === CommentSortType.Top && "active"
360 value={CommentSortType.Top}
361 checked={this.state.commentSort === CommentSortType.Top}
362 onChange={linkEvent(this, this.handleCommentSortChange)}
366 className={`btn btn-outline-secondary pointer ${
367 this.state.commentSort === CommentSortType.New && "active"
373 value={CommentSortType.New}
374 checked={this.state.commentSort === CommentSortType.New}
375 onChange={linkEvent(this, this.handleCommentSortChange)}
379 className={`btn btn-outline-secondary pointer ${
380 this.state.commentSort === CommentSortType.Old && "active"
386 value={CommentSortType.Old}
387 checked={this.state.commentSort === CommentSortType.Old}
388 onChange={linkEvent(this, this.handleCommentSortChange)}
392 <div class="btn-group btn-group-toggle flex-wrap mb-2">
394 className={`btn btn-outline-secondary pointer ${
395 this.state.commentViewType === CommentViewType.Chat && "active"
401 value={CommentViewType.Chat}
402 checked={this.state.commentViewType === CommentViewType.Chat}
403 onChange={linkEvent(this, this.handleCommentViewTypeChange)}
412 // These are already sorted by new
416 nodes={commentsToFlatNodes(this.state.postRes.comments)}
418 locked={this.state.postRes.post_view.post.locked}
419 moderators={this.state.postRes.moderators}
420 admins={this.state.siteRes.admins}
421 postCreatorId={this.state.postRes.post_view.creator.id}
423 enableDownvotes={this.state.siteRes.site_view.site.enable_downvotes}
433 community_view={this.state.postRes.community_view}
434 moderators={this.state.postRes.moderators}
435 admins={this.state.siteRes.admins}
436 online={this.state.postRes.online}
437 enableNsfw={this.state.siteRes.site_view.site.enable_nsfw}
444 handleCommentSortChange(i: Post, event: any) {
445 i.state.commentSort = Number(event.target.value);
446 i.state.commentViewType = CommentViewType.Tree;
447 i.state.commentTree = buildCommentsTree(
448 i.state.postRes.comments,
454 handleCommentViewTypeChange(i: Post, event: any) {
455 i.state.commentViewType = Number(event.target.value);
456 i.state.commentSort = CommentSortType.New;
457 i.state.commentTree = buildCommentsTree(
458 i.state.postRes.comments,
464 handleShowSidebarMobile(i: Post) {
465 i.state.showSidebarMobile = !i.state.showSidebarMobile;
473 nodes={this.state.commentTree}
474 locked={this.state.postRes.post_view.post.locked}
475 moderators={this.state.postRes.moderators}
476 admins={this.state.siteRes.admins}
477 postCreatorId={this.state.postRes.post_view.creator.id}
478 enableDownvotes={this.state.siteRes.site_view.site.enable_downvotes}
484 parseMessage(msg: any) {
485 let op = wsUserOp(msg);
488 toast(i18n.t(msg.error), "danger");
490 } else if (msg.reconnect) {
491 let postId = Number(this.props.match.params.id);
492 WebSocketService.Instance.send(wsClient.postJoin({ post_id: postId }));
493 WebSocketService.Instance.send(
496 auth: authField(false),
499 } else if (op == UserOperation.GetPost) {
500 let data = wsJsonToRes<GetPostResponse>(msg).data;
501 this.state.postRes = data;
502 this.state.commentTree = buildCommentsTree(
503 this.state.postRes.comments,
504 this.state.commentSort
506 this.state.loading = false;
509 this.fetchCrossPosts();
510 this.setState(this.state);
512 if (!this.state.commentId) restoreScrollPosition(this.context);
514 if (this.checkScrollIntoCommentsParam) {
515 this.scrollIntoCommentSection();
517 } else if (op == UserOperation.CreateComment) {
518 let data = wsJsonToRes<CommentResponse>(msg).data;
520 // Necessary since it might be a user reply, which has the recipients, to avoid double
521 if (data.recipient_ids.length == 0) {
522 this.state.postRes.comments.unshift(data.comment_view);
523 insertCommentIntoTree(this.state.commentTree, data.comment_view);
524 this.state.postRes.post_view.counts.comments++;
525 this.setState(this.state);
529 op == UserOperation.EditComment ||
530 op == UserOperation.DeleteComment ||
531 op == UserOperation.RemoveComment
533 let data = wsJsonToRes<CommentResponse>(msg).data;
534 editCommentRes(data.comment_view, this.state.postRes.comments);
535 this.setState(this.state);
536 } else if (op == UserOperation.SaveComment) {
537 let data = wsJsonToRes<CommentResponse>(msg).data;
538 saveCommentRes(data.comment_view, this.state.postRes.comments);
539 this.setState(this.state);
541 } else if (op == UserOperation.CreateCommentLike) {
542 let data = wsJsonToRes<CommentResponse>(msg).data;
543 createCommentLikeRes(data.comment_view, this.state.postRes.comments);
544 this.setState(this.state);
545 } else if (op == UserOperation.CreatePostLike) {
546 let data = wsJsonToRes<PostResponse>(msg).data;
547 createPostLikeRes(data.post_view, this.state.postRes.post_view);
548 this.setState(this.state);
550 op == UserOperation.EditPost ||
551 op == UserOperation.DeletePost ||
552 op == UserOperation.RemovePost ||
553 op == UserOperation.LockPost ||
554 op == UserOperation.StickyPost ||
555 op == UserOperation.SavePost
557 let data = wsJsonToRes<PostResponse>(msg).data;
558 this.state.postRes.post_view = data.post_view;
559 this.setState(this.state);
562 op == UserOperation.EditCommunity ||
563 op == UserOperation.DeleteCommunity ||
564 op == UserOperation.RemoveCommunity ||
565 op == UserOperation.FollowCommunity
567 let data = wsJsonToRes<CommunityResponse>(msg).data;
568 this.state.postRes.community_view = data.community_view;
569 this.state.postRes.post_view.community = data.community_view.community;
570 this.setState(this.state);
571 this.setState(this.state);
572 } else if (op == UserOperation.BanFromCommunity) {
573 let data = wsJsonToRes<BanFromCommunityResponse>(msg).data;
574 this.state.postRes.comments
575 .filter(c => c.creator.id == data.person_view.person.id)
576 .forEach(c => (c.creator_banned_from_community = data.banned));
578 this.state.postRes.post_view.creator.id == data.person_view.person.id
580 this.state.postRes.post_view.creator_banned_from_community =
583 this.setState(this.state);
584 } else if (op == UserOperation.AddModToCommunity) {
585 let data = wsJsonToRes<AddModToCommunityResponse>(msg).data;
586 this.state.postRes.moderators = data.moderators;
587 this.setState(this.state);
588 } else if (op == UserOperation.BanPerson) {
589 let data = wsJsonToRes<BanPersonResponse>(msg).data;
590 this.state.postRes.comments
591 .filter(c => c.creator.id == data.person_view.person.id)
592 .forEach(c => (c.creator.banned = data.banned));
594 this.state.postRes.post_view.creator.id == data.person_view.person.id
596 this.state.postRes.post_view.creator.banned = data.banned;
598 this.setState(this.state);
599 } else if (op == UserOperation.AddAdmin) {
600 let data = wsJsonToRes<AddAdminResponse>(msg).data;
601 this.state.siteRes.admins = data.admins;
602 this.setState(this.state);
603 } else if (op == UserOperation.Search) {
604 let data = wsJsonToRes<SearchResponse>(msg).data;
605 this.state.crossPosts = data.posts.filter(
606 p => p.post.id != Number(this.props.match.params.id)
608 this.setState(this.state);
609 } else if (op == UserOperation.TransferSite) {
610 let data = wsJsonToRes<GetSiteResponse>(msg).data;
611 this.state.siteRes = data;
612 this.setState(this.state);
613 } else if (op == UserOperation.TransferCommunity) {
614 let data = wsJsonToRes<GetCommunityResponse>(msg).data;
615 this.state.postRes.community_view = data.community_view;
616 this.state.postRes.post_view.community = data.community_view.community;
617 this.state.postRes.moderators = data.moderators;
618 this.setState(this.state);
619 } else if (op == UserOperation.BlockPerson) {
620 let data = wsJsonToRes<BlockPersonResponse>(msg).data;
621 updatePersonBlock(data);