1 import { Component, linkEvent } from 'inferno';
2 import { Helmet } from 'inferno-helmet';
3 import { Subscription } from 'rxjs';
12 CommentNode as CommentNodeI,
13 BanFromCommunityResponse,
15 AddModToCommunityResponse,
24 WebSocketJsonResponse,
25 ListCategoriesResponse,
27 } from 'lemmy-js-client';
28 import { CommentSortType, CommentViewType } from '../interfaces';
29 import { WebSocketService, UserService } from '../services';
42 getCommentIdFromProps,
48 import { PostListing } from './post-listing';
49 import { Sidebar } from './sidebar';
50 import { CommentForm } from './comment-form';
51 import { CommentNodes } from './comment-nodes';
52 import autosize from 'autosize';
53 import { i18n } from '../i18next';
56 postRes: GetPostResponse;
59 commentSort: CommentSortType;
60 commentViewType: CommentViewType;
64 siteRes: GetSiteResponse;
65 categories: Category[];
68 export class Post extends Component<any, PostState> {
69 private subscription: Subscription;
70 private isoData = setIsoData(this.context);
71 private emptyState: PostState = {
73 postId: getIdFromProps(this.props),
74 commentId: getCommentIdFromProps(this.props),
75 commentSort: CommentSortType.Hot,
76 commentViewType: CommentViewType.Tree,
80 siteRes: this.isoData.site,
84 constructor(props: any, context: any) {
85 super(props, context);
87 this.state = this.emptyState;
89 this.parseMessage = this.parseMessage.bind(this);
90 this.subscription = wsSubscribe(this.parseMessage);
92 // Only fetch the data if coming from another route
93 if (this.isoData.path == this.context.router.route.match.url) {
94 this.state.postRes = this.isoData.routeData[0];
95 this.state.categories = this.isoData.routeData[1].categories;
96 this.state.loading = false;
98 if (isBrowser() && this.state.commentId) {
99 this.scrollCommentIntoView();
103 WebSocketService.Instance.listCategories();
108 let form: GetPostForm = {
109 id: this.state.postId,
111 WebSocketService.Instance.getPost(form);
114 static fetchInitialData(auth: string, path: string): Promise<any>[] {
115 let pathSplit = path.split('/');
116 let promises: Promise<any>[] = [];
118 let id = Number(pathSplit[2]);
120 let postForm: GetPostForm = {
123 setAuth(postForm, auth);
125 promises.push(lemmyHttp.getPost(postForm));
126 promises.push(lemmyHttp.listCategories());
131 componentWillUnmount() {
132 this.subscription.unsubscribe();
135 componentDidMount() {
136 autosize(document.querySelectorAll('textarea'));
139 componentDidUpdate(_lastProps: any, lastState: PostState, _snapshot: any) {
141 this.state.commentId &&
142 !this.state.scrolled &&
144 lastState.postRes.comments.length > 0
146 this.scrollCommentIntoView();
149 // Necessary if you are on a post and you click another post (same route)
150 if (_lastProps.location.pathname !== _lastProps.history.location.pathname) {
151 // Couldnt get a refresh working. This does for now.
154 // let currentId = this.props.match.params.id;
155 // WebSocketService.Instance.getPost(currentId);
156 // this.context.router.history.push('/sponsors');
157 // this.context.refresh();
158 // this.context.router.history.push(_lastProps.location.pathname);
162 scrollCommentIntoView() {
163 var elmnt = document.getElementById(`comment-${this.state.commentId}`);
164 elmnt.scrollIntoView();
165 elmnt.classList.add('mark');
166 this.state.scrolled = true;
167 this.markScrolledAsRead(this.state.commentId);
170 markScrolledAsRead(commentId: number) {
171 let found = this.state.postRes.comments.find(c => c.id == commentId);
172 let parent = this.state.postRes.comments.find(c => found.parent_id == c.id);
173 let parent_user_id = parent
175 : this.state.postRes.post.creator_id;
178 UserService.Instance.user &&
179 UserService.Instance.user.id == parent_user_id
181 let form: MarkCommentAsReadForm = {
186 WebSocketService.Instance.markCommentAsRead(form);
187 UserService.Instance.unreadCountSub.next(
188 UserService.Instance.unreadCountSub.value - 1
193 get documentTitle(): string {
194 if (this.state.postRes) {
195 return `${this.state.postRes.post.name} - ${this.state.siteRes.site.name}`;
201 get favIcon(): string {
202 return this.state.siteRes.site.icon
203 ? this.state.siteRes.site.icon
209 <div class="container">
210 <Helmet title={this.documentTitle}>
218 {this.state.loading ? (
220 <svg class="icon icon-spinner spin">
221 <use xlinkHref="#icon-spinner"></use>
226 <div class="col-12 col-md-8 mb-3">
228 post={this.state.postRes.post}
231 moderators={this.state.postRes.moderators}
232 admins={this.state.siteRes.admins}
233 enableDownvotes={this.state.siteRes.site.enable_downvotes}
234 enableNsfw={this.state.siteRes.site.enable_nsfw}
236 <div className="mb-2" />
238 postId={this.state.postId}
239 disabled={this.state.postRes.post.locked}
241 {this.state.postRes.comments.length > 0 && this.sortRadios()}
242 {this.state.commentViewType == CommentViewType.Tree &&
244 {this.state.commentViewType == CommentViewType.Chat &&
247 <div class="col-12 col-sm-12 col-md-4">{this.sidebar()}</div>
257 <div class="btn-group btn-group-toggle flex-wrap mr-3 mb-2">
259 className={`btn btn-outline-secondary pointer ${
260 this.state.commentSort === CommentSortType.Hot && 'active'
266 value={CommentSortType.Hot}
267 checked={this.state.commentSort === CommentSortType.Hot}
268 onChange={linkEvent(this, this.handleCommentSortChange)}
272 className={`btn btn-outline-secondary pointer ${
273 this.state.commentSort === CommentSortType.Top && 'active'
279 value={CommentSortType.Top}
280 checked={this.state.commentSort === CommentSortType.Top}
281 onChange={linkEvent(this, this.handleCommentSortChange)}
285 className={`btn btn-outline-secondary pointer ${
286 this.state.commentSort === CommentSortType.New && 'active'
292 value={CommentSortType.New}
293 checked={this.state.commentSort === CommentSortType.New}
294 onChange={linkEvent(this, this.handleCommentSortChange)}
298 className={`btn btn-outline-secondary pointer ${
299 this.state.commentSort === CommentSortType.Old && 'active'
305 value={CommentSortType.Old}
306 checked={this.state.commentSort === CommentSortType.Old}
307 onChange={linkEvent(this, this.handleCommentSortChange)}
311 <div class="btn-group btn-group-toggle flex-wrap mb-2">
313 className={`btn btn-outline-secondary pointer ${
314 this.state.commentViewType === CommentViewType.Chat && 'active'
320 value={CommentViewType.Chat}
321 checked={this.state.commentViewType === CommentViewType.Chat}
322 onChange={linkEvent(this, this.handleCommentViewTypeChange)}
334 nodes={commentsToFlatNodes(this.state.postRes.comments)}
336 locked={this.state.postRes.post.locked}
337 moderators={this.state.postRes.moderators}
338 admins={this.state.siteRes.admins}
339 postCreatorId={this.state.postRes.post.creator_id}
341 enableDownvotes={this.state.siteRes.site.enable_downvotes}
342 sort={this.state.commentSort}
352 community={this.state.postRes.community}
353 moderators={this.state.postRes.moderators}
354 admins={this.state.siteRes.admins}
355 online={this.state.postRes.online}
356 enableNsfw={this.state.siteRes.site.enable_nsfw}
358 categories={this.state.categories}
364 handleCommentSortChange(i: Post, event: any) {
365 i.state.commentSort = Number(event.target.value);
366 i.state.commentViewType = CommentViewType.Tree;
370 handleCommentViewTypeChange(i: Post, event: any) {
371 i.state.commentViewType = Number(event.target.value);
372 i.state.commentSort = CommentSortType.New;
376 buildCommentsTree(): CommentNodeI[] {
377 let map = new Map<number, CommentNodeI>();
378 for (let comment of this.state.postRes.comments) {
379 let node: CommentNodeI = {
383 map.set(comment.id, { ...node });
385 let tree: CommentNodeI[] = [];
386 for (let comment of this.state.postRes.comments) {
387 let child = map.get(comment.id);
388 if (comment.parent_id) {
389 let parent_ = map.get(comment.parent_id);
390 parent_.children.push(child);
395 this.setDepth(child);
401 setDepth(node: CommentNodeI, i: number = 0): void {
402 for (let child of node.children) {
403 child.comment.depth = i;
404 this.setDepth(child, i + 1);
409 let nodes = this.buildCommentsTree();
414 locked={this.state.postRes.post.locked}
415 moderators={this.state.postRes.moderators}
416 admins={this.state.siteRes.admins}
417 postCreatorId={this.state.postRes.post.creator_id}
418 sort={this.state.commentSort}
419 enableDownvotes={this.state.siteRes.site.enable_downvotes}
425 parseMessage(msg: WebSocketJsonResponse) {
427 let res = wsJsonToRes(msg);
429 toast(i18n.t(msg.error), 'danger');
431 } else if (msg.reconnect) {
432 WebSocketService.Instance.getPost({
433 id: Number(this.props.match.params.id),
435 } else if (res.op == UserOperation.GetPost) {
436 let data = res.data as GetPostResponse;
437 this.state.postRes = data;
438 this.state.loading = false;
441 if (this.state.postRes.post.url) {
442 let form: SearchForm = {
443 q: this.state.postRes.post.url,
444 type_: SearchType.Url,
445 sort: SortType.TopAll,
449 WebSocketService.Instance.search(form);
452 this.setState(this.state);
454 } else if (res.op == UserOperation.CreateComment) {
455 let data = res.data as CommentResponse;
457 // Necessary since it might be a user reply
458 if (data.recipient_ids.length == 0) {
459 this.state.postRes.comments.unshift(data.comment);
460 this.setState(this.state);
463 res.op == UserOperation.EditComment ||
464 res.op == UserOperation.DeleteComment ||
465 res.op == UserOperation.RemoveComment
467 let data = res.data as CommentResponse;
468 editCommentRes(data, this.state.postRes.comments);
469 this.setState(this.state);
470 } else if (res.op == UserOperation.SaveComment) {
471 let data = res.data as CommentResponse;
472 saveCommentRes(data, this.state.postRes.comments);
473 this.setState(this.state);
475 } else if (res.op == UserOperation.CreateCommentLike) {
476 let data = res.data as CommentResponse;
477 createCommentLikeRes(data, this.state.postRes.comments);
478 this.setState(this.state);
479 } else if (res.op == UserOperation.CreatePostLike) {
480 let data = res.data as PostResponse;
481 createPostLikeRes(data, this.state.postRes.post);
482 this.setState(this.state);
484 res.op == UserOperation.EditPost ||
485 res.op == UserOperation.DeletePost ||
486 res.op == UserOperation.RemovePost ||
487 res.op == UserOperation.LockPost ||
488 res.op == UserOperation.StickyPost
490 let data = res.data as PostResponse;
491 this.state.postRes.post = data.post;
492 this.setState(this.state);
494 } else if (res.op == UserOperation.SavePost) {
495 let data = res.data as PostResponse;
496 this.state.postRes.post = data.post;
497 this.setState(this.state);
500 res.op == UserOperation.EditCommunity ||
501 res.op == UserOperation.DeleteCommunity ||
502 res.op == UserOperation.RemoveCommunity
504 let data = res.data as CommunityResponse;
505 this.state.postRes.community = data.community;
506 this.state.postRes.post.community_id = data.community.id;
507 this.state.postRes.post.community_name = data.community.name;
508 this.setState(this.state);
509 } else if (res.op == UserOperation.FollowCommunity) {
510 let data = res.data as CommunityResponse;
511 this.state.postRes.community.subscribed = data.community.subscribed;
512 this.state.postRes.community.number_of_subscribers =
513 data.community.number_of_subscribers;
514 this.setState(this.state);
515 } else if (res.op == UserOperation.BanFromCommunity) {
516 let data = res.data as BanFromCommunityResponse;
517 this.state.postRes.comments
518 .filter(c => c.creator_id == data.user.id)
519 .forEach(c => (c.banned_from_community = data.banned));
520 if (this.state.postRes.post.creator_id == data.user.id) {
521 this.state.postRes.post.banned_from_community = data.banned;
523 this.setState(this.state);
524 } else if (res.op == UserOperation.AddModToCommunity) {
525 let data = res.data as AddModToCommunityResponse;
526 this.state.postRes.moderators = data.moderators;
527 this.setState(this.state);
528 } else if (res.op == UserOperation.BanUser) {
529 let data = res.data as BanUserResponse;
530 this.state.postRes.comments
531 .filter(c => c.creator_id == data.user.id)
532 .forEach(c => (c.banned = data.banned));
533 if (this.state.postRes.post.creator_id == data.user.id) {
534 this.state.postRes.post.banned = data.banned;
536 this.setState(this.state);
537 } else if (res.op == UserOperation.AddAdmin) {
538 let data = res.data as AddAdminResponse;
539 this.state.siteRes.admins = data.admins;
540 this.setState(this.state);
541 } else if (res.op == UserOperation.Search) {
542 let data = res.data as SearchResponse;
543 this.state.crossPosts = data.posts.filter(
544 p => p.id != Number(this.props.match.params.id)
546 if (this.state.crossPosts.length) {
547 this.state.postRes.post.duplicates = this.state.crossPosts;
549 this.setState(this.state);
550 } else if (res.op == UserOperation.TransferSite) {
551 let data = res.data as GetSiteResponse;
552 this.state.siteRes = data;
553 this.setState(this.state);
554 } else if (res.op == UserOperation.TransferCommunity) {
555 let data = res.data as GetCommunityResponse;
556 this.state.postRes.community = data.community;
557 this.state.postRes.moderators = data.moderators;
558 this.setState(this.state);
559 } else if (res.op == UserOperation.ListCategories) {
560 let data = res.data as ListCategoriesResponse;
561 this.state.categories = data.categories;
562 this.setState(this.state);