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 communities={[this.state.postRes.community]}
229 post={this.state.postRes.post}
232 moderators={this.state.postRes.moderators}
233 admins={this.state.siteRes.admins}
234 enableDownvotes={this.state.siteRes.site.enable_downvotes}
235 enableNsfw={this.state.siteRes.site.enable_nsfw}
237 <div className="mb-2" />
239 postId={this.state.postId}
240 disabled={this.state.postRes.post.locked}
242 {this.state.postRes.comments.length > 0 && this.sortRadios()}
243 {this.state.commentViewType == CommentViewType.Tree &&
245 {this.state.commentViewType == CommentViewType.Chat &&
248 <div class="col-12 col-sm-12 col-md-4">{this.sidebar()}</div>
258 <div class="btn-group btn-group-toggle flex-wrap mr-3 mb-2">
260 className={`btn btn-outline-secondary pointer ${
261 this.state.commentSort === CommentSortType.Hot && 'active'
267 value={CommentSortType.Hot}
268 checked={this.state.commentSort === CommentSortType.Hot}
269 onChange={linkEvent(this, this.handleCommentSortChange)}
273 className={`btn btn-outline-secondary pointer ${
274 this.state.commentSort === CommentSortType.Top && 'active'
280 value={CommentSortType.Top}
281 checked={this.state.commentSort === CommentSortType.Top}
282 onChange={linkEvent(this, this.handleCommentSortChange)}
286 className={`btn btn-outline-secondary pointer ${
287 this.state.commentSort === CommentSortType.New && 'active'
293 value={CommentSortType.New}
294 checked={this.state.commentSort === CommentSortType.New}
295 onChange={linkEvent(this, this.handleCommentSortChange)}
299 className={`btn btn-outline-secondary pointer ${
300 this.state.commentSort === CommentSortType.Old && 'active'
306 value={CommentSortType.Old}
307 checked={this.state.commentSort === CommentSortType.Old}
308 onChange={linkEvent(this, this.handleCommentSortChange)}
312 <div class="btn-group btn-group-toggle flex-wrap mb-2">
314 className={`btn btn-outline-secondary pointer ${
315 this.state.commentViewType === CommentViewType.Chat && 'active'
321 value={CommentViewType.Chat}
322 checked={this.state.commentViewType === CommentViewType.Chat}
323 onChange={linkEvent(this, this.handleCommentViewTypeChange)}
335 nodes={commentsToFlatNodes(this.state.postRes.comments)}
337 locked={this.state.postRes.post.locked}
338 moderators={this.state.postRes.moderators}
339 admins={this.state.siteRes.admins}
340 postCreatorId={this.state.postRes.post.creator_id}
342 enableDownvotes={this.state.siteRes.site.enable_downvotes}
343 sort={this.state.commentSort}
353 community={this.state.postRes.community}
354 moderators={this.state.postRes.moderators}
355 admins={this.state.siteRes.admins}
356 online={this.state.postRes.online}
357 enableNsfw={this.state.siteRes.site.enable_nsfw}
359 categories={this.state.categories}
365 handleCommentSortChange(i: Post, event: any) {
366 i.state.commentSort = Number(event.target.value);
367 i.state.commentViewType = CommentViewType.Tree;
371 handleCommentViewTypeChange(i: Post, event: any) {
372 i.state.commentViewType = Number(event.target.value);
373 i.state.commentSort = CommentSortType.New;
377 buildCommentsTree(): CommentNodeI[] {
378 let map = new Map<number, CommentNodeI>();
379 for (let comment of this.state.postRes.comments) {
380 let node: CommentNodeI = {
384 map.set(comment.id, { ...node });
386 let tree: CommentNodeI[] = [];
387 for (let comment of this.state.postRes.comments) {
388 let child = map.get(comment.id);
389 if (comment.parent_id) {
390 let parent_ = map.get(comment.parent_id);
391 parent_.children.push(child);
396 this.setDepth(child);
402 setDepth(node: CommentNodeI, i: number = 0): void {
403 for (let child of node.children) {
404 child.comment.depth = i;
405 this.setDepth(child, i + 1);
410 let nodes = this.buildCommentsTree();
415 locked={this.state.postRes.post.locked}
416 moderators={this.state.postRes.moderators}
417 admins={this.state.siteRes.admins}
418 postCreatorId={this.state.postRes.post.creator_id}
419 sort={this.state.commentSort}
420 enableDownvotes={this.state.siteRes.site.enable_downvotes}
426 parseMessage(msg: WebSocketJsonResponse) {
428 let res = wsJsonToRes(msg);
430 toast(i18n.t(msg.error), 'danger');
432 } else if (msg.reconnect) {
433 WebSocketService.Instance.getPost({
434 id: Number(this.props.match.params.id),
436 } else if (res.op == UserOperation.GetPost) {
437 let data = res.data as GetPostResponse;
438 this.state.postRes = data;
439 this.state.loading = false;
442 if (this.state.postRes.post.url) {
443 let form: SearchForm = {
444 q: this.state.postRes.post.url,
445 type_: SearchType.Url,
446 sort: SortType.TopAll,
450 WebSocketService.Instance.search(form);
453 this.setState(this.state);
455 } else if (res.op == UserOperation.CreateComment) {
456 let data = res.data as CommentResponse;
458 // Necessary since it might be a user reply
459 if (data.recipient_ids.length == 0) {
460 this.state.postRes.comments.unshift(data.comment);
461 this.setState(this.state);
464 res.op == UserOperation.EditComment ||
465 res.op == UserOperation.DeleteComment ||
466 res.op == UserOperation.RemoveComment
468 let data = res.data as CommentResponse;
469 editCommentRes(data, this.state.postRes.comments);
470 this.setState(this.state);
471 } else if (res.op == UserOperation.SaveComment) {
472 let data = res.data as CommentResponse;
473 saveCommentRes(data, this.state.postRes.comments);
474 this.setState(this.state);
476 } else if (res.op == UserOperation.CreateCommentLike) {
477 let data = res.data as CommentResponse;
478 createCommentLikeRes(data, this.state.postRes.comments);
479 this.setState(this.state);
480 } else if (res.op == UserOperation.CreatePostLike) {
481 let data = res.data as PostResponse;
482 createPostLikeRes(data, this.state.postRes.post);
483 this.setState(this.state);
485 res.op == UserOperation.EditPost ||
486 res.op == UserOperation.DeletePost ||
487 res.op == UserOperation.RemovePost ||
488 res.op == UserOperation.LockPost ||
489 res.op == UserOperation.StickyPost
491 let data = res.data as PostResponse;
492 this.state.postRes.post = data.post;
493 this.setState(this.state);
495 } else if (res.op == UserOperation.SavePost) {
496 let data = res.data as PostResponse;
497 this.state.postRes.post = data.post;
498 this.setState(this.state);
501 res.op == UserOperation.EditCommunity ||
502 res.op == UserOperation.DeleteCommunity ||
503 res.op == UserOperation.RemoveCommunity
505 let data = res.data as CommunityResponse;
506 this.state.postRes.community = data.community;
507 this.state.postRes.post.community_id = data.community.id;
508 this.state.postRes.post.community_name = data.community.name;
509 this.setState(this.state);
510 } else if (res.op == UserOperation.FollowCommunity) {
511 let data = res.data as CommunityResponse;
512 this.state.postRes.community.subscribed = data.community.subscribed;
513 this.state.postRes.community.number_of_subscribers =
514 data.community.number_of_subscribers;
515 this.setState(this.state);
516 } else if (res.op == UserOperation.BanFromCommunity) {
517 let data = res.data as BanFromCommunityResponse;
518 this.state.postRes.comments
519 .filter(c => c.creator_id == data.user.id)
520 .forEach(c => (c.banned_from_community = data.banned));
521 if (this.state.postRes.post.creator_id == data.user.id) {
522 this.state.postRes.post.banned_from_community = data.banned;
524 this.setState(this.state);
525 } else if (res.op == UserOperation.AddModToCommunity) {
526 let data = res.data as AddModToCommunityResponse;
527 this.state.postRes.moderators = data.moderators;
528 this.setState(this.state);
529 } else if (res.op == UserOperation.BanUser) {
530 let data = res.data as BanUserResponse;
531 this.state.postRes.comments
532 .filter(c => c.creator_id == data.user.id)
533 .forEach(c => (c.banned = data.banned));
534 if (this.state.postRes.post.creator_id == data.user.id) {
535 this.state.postRes.post.banned = data.banned;
537 this.setState(this.state);
538 } else if (res.op == UserOperation.AddAdmin) {
539 let data = res.data as AddAdminResponse;
540 this.state.siteRes.admins = data.admins;
541 this.setState(this.state);
542 } else if (res.op == UserOperation.Search) {
543 let data = res.data as SearchResponse;
544 this.state.crossPosts = data.posts.filter(
545 p => p.id != Number(this.props.match.params.id)
547 if (this.state.crossPosts.length) {
548 this.state.postRes.post.duplicates = this.state.crossPosts;
550 this.setState(this.state);
551 } else if (res.op == UserOperation.TransferSite) {
552 let data = res.data as GetSiteResponse;
553 this.state.siteRes = data;
554 this.setState(this.state);
555 } else if (res.op == UserOperation.TransferCommunity) {
556 let data = res.data as GetCommunityResponse;
557 this.state.postRes.community = data.community;
558 this.state.postRes.moderators = data.moderators;
559 this.setState(this.state);
560 } else if (res.op == UserOperation.ListCategories) {
561 let data = res.data as ListCategoriesResponse;
562 this.state.categories = data.categories;
563 this.setState(this.state);