1 import { Component, linkEvent } from 'inferno';
2 import { HtmlTags } from './html-tags';
3 import { Subscription } from 'rxjs';
12 BanFromCommunityResponse,
14 AddModToCommunityResponse,
23 ListCategoriesResponse,
25 } from 'lemmy-js-client';
30 CommentNode as CommentNodeI,
31 } from '../interfaces';
32 import { WebSocketService, UserService } from '../services';
44 getCommentIdFromProps,
54 import { PostListing } from './post-listing';
55 import { Sidebar } from './sidebar';
56 import { CommentForm } from './comment-form';
57 import { CommentNodes } from './comment-nodes';
58 import autosize from 'autosize';
59 import { i18n } from '../i18next';
62 postRes: GetPostResponse;
65 commentSort: CommentSortType;
66 commentViewType: CommentViewType;
69 crossPosts: PostView[];
70 siteRes: GetSiteResponse;
71 categories: Category[];
74 export class Post extends Component<any, PostState> {
75 private subscription: Subscription;
76 private isoData = setIsoData(this.context);
77 private emptyState: PostState = {
79 postId: getIdFromProps(this.props),
80 commentId: getCommentIdFromProps(this.props),
81 commentSort: CommentSortType.Hot,
82 commentViewType: CommentViewType.Tree,
86 siteRes: this.isoData.site_res,
90 constructor(props: any, context: any) {
91 super(props, context);
93 this.state = this.emptyState;
95 this.parseMessage = this.parseMessage.bind(this);
96 this.subscription = wsSubscribe(this.parseMessage);
98 // Only fetch the data if coming from another route
99 if (this.isoData.path == this.context.router.route.match.url) {
100 this.state.postRes = this.isoData.routeData[0];
101 this.state.categories = this.isoData.routeData[1].categories;
102 this.state.loading = false;
104 if (isBrowser() && this.state.commentId) {
105 this.scrollCommentIntoView();
109 WebSocketService.Instance.send(wsClient.listCategories());
114 let form: GetPost = {
115 id: this.state.postId,
116 auth: authField(false),
118 WebSocketService.Instance.send(wsClient.getPost(form));
121 static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
122 let pathSplit = req.path.split('/');
123 let promises: Promise<any>[] = [];
125 let id = Number(pathSplit[2]);
127 let postForm: GetPost = {
130 setOptionalAuth(postForm, req.auth);
132 promises.push(req.client.getPost(postForm));
133 promises.push(req.client.listCategories());
138 componentWillUnmount() {
139 this.subscription.unsubscribe();
140 window.isoData.path = undefined;
143 componentDidMount() {
144 WebSocketService.Instance.send(
145 wsClient.postJoin({ post_id: this.state.postId })
147 autosize(document.querySelectorAll('textarea'));
150 componentDidUpdate(_lastProps: any, lastState: PostState, _snapshot: any) {
152 this.state.commentId &&
153 !this.state.scrolled &&
155 lastState.postRes.comments.length > 0
157 this.scrollCommentIntoView();
160 // Necessary if you are on a post and you click another post (same route)
161 if (_lastProps.location.pathname !== _lastProps.history.location.pathname) {
162 // TODO Couldnt get a refresh working. This does for now.
165 // let currentId = this.props.match.params.id;
166 // WebSocketService.Instance.getPost(currentId);
167 // this.context.refresh();
168 // this.context.router.history.push(_lastProps.location.pathname);
172 scrollCommentIntoView() {
173 var elmnt = document.getElementById(`comment-${this.state.commentId}`);
174 elmnt.scrollIntoView();
175 elmnt.classList.add('mark');
176 this.state.scrolled = true;
177 this.markScrolledAsRead(this.state.commentId);
180 // TODO this needs some re-work
181 markScrolledAsRead(commentId: number) {
182 let found = this.state.postRes.comments.find(
183 c => c.comment.id == commentId
185 let parent = this.state.postRes.comments.find(
186 c => found.comment.parent_id == c.comment.id
188 let parent_user_id = parent
190 : this.state.postRes.post_view.creator.id;
193 UserService.Instance.user &&
194 UserService.Instance.user.id == parent_user_id
196 let form: MarkCommentAsRead = {
197 comment_id: found.comment.id,
201 WebSocketService.Instance.send(wsClient.markCommentAsRead(form));
202 UserService.Instance.unreadCountSub.next(
203 UserService.Instance.unreadCountSub.value - 1
208 get documentTitle(): string {
209 return `${this.state.postRes.post_view.post.name} - ${this.state.siteRes.site_view.site.name}`;
212 get imageTag(): string {
213 let post = this.state.postRes.post_view.post;
215 post.thumbnail_url ||
216 (post.url ? (isImage(post.url) ? post.url : undefined) : undefined)
220 get descriptionTag(): string {
221 let body = this.state.postRes.post_view.post.body;
222 return body ? previewLines(body) : undefined;
226 let pv = this.state.postRes?.post_view;
228 <div class="container">
229 {this.state.loading ? (
231 <svg class="icon icon-spinner spin">
232 <use xlinkHref="#icon-spinner"></use>
237 <div class="col-12 col-md-8 mb-3">
239 title={this.documentTitle}
240 path={this.context.router.route.match.url}
241 image={this.imageTag}
242 description={this.descriptionTag}
246 duplicates={this.state.crossPosts}
249 moderators={this.state.postRes.moderators}
250 admins={this.state.siteRes.admins}
252 this.state.siteRes.site_view.site.enable_downvotes
254 enableNsfw={this.state.siteRes.site_view.site.enable_nsfw}
256 <div className="mb-2" />
258 postId={this.state.postId}
259 disabled={pv.post.locked}
261 {this.state.postRes.comments.length > 0 && this.sortRadios()}
262 {this.state.commentViewType == CommentViewType.Tree &&
264 {this.state.commentViewType == CommentViewType.Chat &&
267 <div class="col-12 col-sm-12 col-md-4">{this.sidebar()}</div>
277 <div class="btn-group btn-group-toggle flex-wrap mr-3 mb-2">
279 className={`btn btn-outline-secondary pointer ${
280 this.state.commentSort === CommentSortType.Hot && 'active'
286 value={CommentSortType.Hot}
287 checked={this.state.commentSort === CommentSortType.Hot}
288 onChange={linkEvent(this, this.handleCommentSortChange)}
292 className={`btn btn-outline-secondary pointer ${
293 this.state.commentSort === CommentSortType.Top && 'active'
299 value={CommentSortType.Top}
300 checked={this.state.commentSort === CommentSortType.Top}
301 onChange={linkEvent(this, this.handleCommentSortChange)}
305 className={`btn btn-outline-secondary pointer ${
306 this.state.commentSort === CommentSortType.New && 'active'
312 value={CommentSortType.New}
313 checked={this.state.commentSort === CommentSortType.New}
314 onChange={linkEvent(this, this.handleCommentSortChange)}
318 className={`btn btn-outline-secondary pointer ${
319 this.state.commentSort === CommentSortType.Old && 'active'
325 value={CommentSortType.Old}
326 checked={this.state.commentSort === CommentSortType.Old}
327 onChange={linkEvent(this, this.handleCommentSortChange)}
331 <div class="btn-group btn-group-toggle flex-wrap mb-2">
333 className={`btn btn-outline-secondary pointer ${
334 this.state.commentViewType === CommentViewType.Chat && 'active'
340 value={CommentViewType.Chat}
341 checked={this.state.commentViewType === CommentViewType.Chat}
342 onChange={linkEvent(this, this.handleCommentViewTypeChange)}
354 nodes={commentsToFlatNodes(this.state.postRes.comments)}
356 locked={this.state.postRes.post_view.post.locked}
357 moderators={this.state.postRes.moderators}
358 admins={this.state.siteRes.admins}
359 postCreatorId={this.state.postRes.post_view.creator.id}
361 enableDownvotes={this.state.siteRes.site_view.site.enable_downvotes}
362 sort={this.state.commentSort}
372 community_view={this.state.postRes.community_view}
373 moderators={this.state.postRes.moderators}
374 admins={this.state.siteRes.admins}
375 online={this.state.postRes.online}
376 enableNsfw={this.state.siteRes.site_view.site.enable_nsfw}
378 categories={this.state.categories}
384 handleCommentSortChange(i: Post, event: any) {
385 i.state.commentSort = Number(event.target.value);
386 i.state.commentViewType = CommentViewType.Tree;
390 handleCommentViewTypeChange(i: Post, event: any) {
391 i.state.commentViewType = Number(event.target.value);
392 i.state.commentSort = CommentSortType.New;
396 buildCommentsTree(): CommentNodeI[] {
397 let map = new Map<number, CommentNodeI>();
398 for (let comment_view of this.state.postRes.comments) {
399 let node: CommentNodeI = {
400 comment_view: comment_view,
403 map.set(comment_view.comment.id, { ...node });
405 let tree: CommentNodeI[] = [];
406 for (let comment_view of this.state.postRes.comments) {
407 let child = map.get(comment_view.comment.id);
408 if (comment_view.comment.parent_id) {
409 let parent_ = map.get(comment_view.comment.parent_id);
410 parent_.children.push(child);
415 this.setDepth(child);
421 setDepth(node: CommentNodeI, i: number = 0): void {
422 for (let child of node.children) {
424 this.setDepth(child, i + 1);
429 let nodes = this.buildCommentsTree();
434 locked={this.state.postRes.post_view.post.locked}
435 moderators={this.state.postRes.moderators}
436 admins={this.state.siteRes.admins}
437 postCreatorId={this.state.postRes.post_view.creator.id}
438 sort={this.state.commentSort}
439 enableDownvotes={this.state.siteRes.site_view.site.enable_downvotes}
445 parseMessage(msg: any) {
446 let op = wsUserOp(msg);
449 toast(i18n.t(msg.error), 'danger');
451 } else if (msg.reconnect) {
452 let postId = Number(this.props.match.params.id);
453 WebSocketService.Instance.send(wsClient.postJoin({ post_id: postId }));
454 WebSocketService.Instance.send(
457 auth: authField(false),
460 } else if (op == UserOperation.GetPost) {
461 let data = wsJsonToRes<GetPostResponse>(msg).data;
462 this.state.postRes = data;
463 this.state.loading = false;
466 if (this.state.postRes.post_view.post.url) {
468 q: this.state.postRes.post_view.post.url,
469 type_: SearchType.Url,
470 sort: SortType.TopAll,
473 auth: authField(false),
475 WebSocketService.Instance.send(wsClient.search(form));
478 this.setState(this.state);
480 } else if (op == UserOperation.CreateComment) {
481 let data = wsJsonToRes<CommentResponse>(msg).data;
483 // Necessary since it might be a user reply, which has the recipients, to avoid double
484 if (data.recipient_ids.length == 0) {
485 this.state.postRes.comments.unshift(data.comment_view);
486 this.state.postRes.post_view.counts.comments++;
487 this.setState(this.state);
491 op == UserOperation.EditComment ||
492 op == UserOperation.DeleteComment ||
493 op == UserOperation.RemoveComment
495 let data = wsJsonToRes<CommentResponse>(msg).data;
496 editCommentRes(data.comment_view, this.state.postRes.comments);
497 this.setState(this.state);
498 } else if (op == UserOperation.SaveComment) {
499 let data = wsJsonToRes<CommentResponse>(msg).data;
500 saveCommentRes(data.comment_view, this.state.postRes.comments);
501 this.setState(this.state);
503 } else if (op == UserOperation.CreateCommentLike) {
504 let data = wsJsonToRes<CommentResponse>(msg).data;
505 createCommentLikeRes(data.comment_view, this.state.postRes.comments);
506 this.setState(this.state);
507 } else if (op == UserOperation.CreatePostLike) {
508 let data = wsJsonToRes<PostResponse>(msg).data;
509 createPostLikeRes(data.post_view, this.state.postRes.post_view);
510 this.setState(this.state);
512 op == UserOperation.EditPost ||
513 op == UserOperation.DeletePost ||
514 op == UserOperation.RemovePost ||
515 op == UserOperation.LockPost ||
516 op == UserOperation.StickyPost ||
517 op == UserOperation.SavePost
519 let data = wsJsonToRes<PostResponse>(msg).data;
520 this.state.postRes.post_view = data.post_view;
521 this.setState(this.state);
524 op == UserOperation.EditCommunity ||
525 op == UserOperation.DeleteCommunity ||
526 op == UserOperation.RemoveCommunity ||
527 op == UserOperation.FollowCommunity
529 let data = wsJsonToRes<CommunityResponse>(msg).data;
530 this.state.postRes.community_view = data.community_view;
531 this.state.postRes.post_view.community = data.community_view.community;
532 this.setState(this.state);
533 this.setState(this.state);
534 } else if (op == UserOperation.BanFromCommunity) {
535 let data = wsJsonToRes<BanFromCommunityResponse>(msg).data;
536 this.state.postRes.comments
537 .filter(c => c.creator.id == data.user_view.user.id)
538 .forEach(c => (c.creator_banned_from_community = data.banned));
539 if (this.state.postRes.post_view.creator.id == data.user_view.user.id) {
540 this.state.postRes.post_view.creator_banned_from_community =
543 this.setState(this.state);
544 } else if (op == UserOperation.AddModToCommunity) {
545 let data = wsJsonToRes<AddModToCommunityResponse>(msg).data;
546 this.state.postRes.moderators = data.moderators;
547 this.setState(this.state);
548 } else if (op == UserOperation.BanUser) {
549 let data = wsJsonToRes<BanUserResponse>(msg).data;
550 this.state.postRes.comments
551 .filter(c => c.creator.id == data.user_view.user.id)
552 .forEach(c => (c.creator.banned = data.banned));
553 if (this.state.postRes.post_view.creator.id == data.user_view.user.id) {
554 this.state.postRes.post_view.creator.banned = data.banned;
556 this.setState(this.state);
557 } else if (op == UserOperation.AddAdmin) {
558 let data = wsJsonToRes<AddAdminResponse>(msg).data;
559 this.state.siteRes.admins = data.admins;
560 this.setState(this.state);
561 } else if (op == UserOperation.Search) {
562 let data = wsJsonToRes<SearchResponse>(msg).data;
563 this.state.crossPosts = data.posts.filter(
564 p => p.post.id != Number(this.props.match.params.id)
566 this.setState(this.state);
567 } else if (op == UserOperation.TransferSite) {
568 let data = wsJsonToRes<GetSiteResponse>(msg).data;
569 this.state.siteRes = data;
570 this.setState(this.state);
571 } else if (op == UserOperation.TransferCommunity) {
572 let data = wsJsonToRes<GetCommunityResponse>(msg).data;
573 this.state.postRes.community_view = data.community_view;
574 this.state.postRes.post_view.community = data.community_view.community;
575 this.state.postRes.moderators = data.moderators;
576 this.setState(this.state);
577 } else if (op == UserOperation.ListCategories) {
578 let data = wsJsonToRes<ListCategoriesResponse>(msg).data;
579 this.state.categories = data.categories;
580 this.setState(this.state);