1 import { Component, linkEvent } from 'inferno';
2 import { Helmet } from 'inferno-helmet';
3 import { Subscription } from 'rxjs';
4 import { retryWhen, delay, take } from 'rxjs/operators';
12 MarkCommentAsReadForm,
18 CommentNode as CommentNodeI,
19 BanFromCommunityResponse,
21 AddModToCommunityResponse,
30 WebSocketJsonResponse,
31 } from '../interfaces';
32 import { WebSocketService, UserService } from '../services';
44 import { PostListing } from './post-listing';
45 import { Sidebar } from './sidebar';
46 import { CommentForm } from './comment-form';
47 import { CommentNodes } from './comment-nodes';
48 import autosize from 'autosize';
49 import { i18n } from '../i18next';
53 comments: Array<Comment>;
54 commentSort: CommentSortType;
55 commentViewType: CommentViewType;
57 moderators: Array<CommunityUser>;
60 scrolled_comment_id?: number;
62 crossPosts: Array<PostI>;
63 siteRes: GetSiteResponse;
66 export class Post extends Component<any, PostState> {
67 private subscription: Subscription;
68 private emptyState: PostState = {
71 commentSort: CommentSortType.Hot,
72 commentViewType: CommentViewType.Tree,
85 creator_id: undefined,
87 creator_name: undefined,
88 number_of_users: undefined,
89 number_of_posts: undefined,
90 number_of_comments: undefined,
91 number_of_communities: undefined,
92 enable_downvotes: undefined,
93 open_registration: undefined,
94 enable_nsfw: undefined,
101 constructor(props: any, context: any) {
102 super(props, context);
104 this.state = this.emptyState;
106 let postId = Number(this.props.match.params.id);
107 if (this.props.match.params.comment_id) {
108 this.state.scrolled_comment_id = this.props.match.params.comment_id;
111 this.subscription = WebSocketService.Instance.subject
112 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
114 msg => this.parseMessage(msg),
115 err => console.error(err),
116 () => console.log('complete')
119 let form: GetPostForm = {
122 WebSocketService.Instance.getPost(form);
123 WebSocketService.Instance.getSite();
126 componentWillUnmount() {
127 this.subscription.unsubscribe();
130 componentDidMount() {
131 autosize(document.querySelectorAll('textarea'));
134 componentDidUpdate(_lastProps: any, lastState: PostState, _snapshot: any) {
136 this.state.scrolled_comment_id &&
137 !this.state.scrolled &&
138 lastState.comments.length > 0
140 var elmnt = document.getElementById(
141 `comment-${this.state.scrolled_comment_id}`
143 elmnt.scrollIntoView();
144 elmnt.classList.add('mark');
145 this.state.scrolled = true;
146 this.markScrolledAsRead(this.state.scrolled_comment_id);
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 markScrolledAsRead(commentId: number) {
163 let found = this.state.comments.find(c => c.id == commentId);
164 let parent = this.state.comments.find(c => found.parent_id == c.id);
165 let parent_user_id = parent
167 : this.state.post.creator_id;
170 UserService.Instance.user &&
171 UserService.Instance.user.id == parent_user_id
173 let form: MarkCommentAsReadForm = {
178 WebSocketService.Instance.markCommentAsRead(form);
179 UserService.Instance.unreadCountSub.next(
180 UserService.Instance.unreadCountSub.value - 1
185 get documentTitle(): string {
186 if (this.state.post) {
187 return `${this.state.post.name} - ${this.state.siteRes.site.name}`;
193 get favIcon(): string {
194 return this.state.post ? this.state.post.community_icon : favIconUrl;
199 <div class="container">
200 <Helmet title={this.documentTitle}>
208 {this.state.loading ? (
210 <svg class="icon icon-spinner spin">
211 <use xlinkHref="#icon-spinner"></use>
216 <div class="col-12 col-md-8 mb-3">
218 post={this.state.post}
221 moderators={this.state.moderators}
222 admins={this.state.siteRes.admins}
223 enableDownvotes={this.state.siteRes.site.enable_downvotes}
224 enableNsfw={this.state.siteRes.site.enable_nsfw}
226 <div className="mb-2" />
228 postId={this.state.post.id}
229 disabled={this.state.post.locked}
231 {this.state.comments.length > 0 && this.sortRadios()}
232 {this.state.commentViewType == CommentViewType.Tree &&
234 {this.state.commentViewType == CommentViewType.Chat &&
237 <div class="col-12 col-sm-12 col-md-4">{this.sidebar()}</div>
247 <div class="btn-group btn-group-toggle flex-wrap mr-3 mb-2">
249 className={`btn btn-outline-secondary pointer ${
250 this.state.commentSort === CommentSortType.Hot && 'active'
256 value={CommentSortType.Hot}
257 checked={this.state.commentSort === CommentSortType.Hot}
258 onChange={linkEvent(this, this.handleCommentSortChange)}
262 className={`btn btn-outline-secondary pointer ${
263 this.state.commentSort === CommentSortType.Top && 'active'
269 value={CommentSortType.Top}
270 checked={this.state.commentSort === CommentSortType.Top}
271 onChange={linkEvent(this, this.handleCommentSortChange)}
275 className={`btn btn-outline-secondary pointer ${
276 this.state.commentSort === CommentSortType.New && 'active'
282 value={CommentSortType.New}
283 checked={this.state.commentSort === CommentSortType.New}
284 onChange={linkEvent(this, this.handleCommentSortChange)}
288 className={`btn btn-outline-secondary pointer ${
289 this.state.commentSort === CommentSortType.Old && 'active'
295 value={CommentSortType.Old}
296 checked={this.state.commentSort === CommentSortType.Old}
297 onChange={linkEvent(this, this.handleCommentSortChange)}
301 <div class="btn-group btn-group-toggle flex-wrap mb-2">
303 className={`btn btn-outline-secondary pointer ${
304 this.state.commentViewType === CommentViewType.Chat && 'active'
310 value={CommentViewType.Chat}
311 checked={this.state.commentViewType === CommentViewType.Chat}
312 onChange={linkEvent(this, this.handleCommentViewTypeChange)}
324 nodes={commentsToFlatNodes(this.state.comments)}
326 locked={this.state.post.locked}
327 moderators={this.state.moderators}
328 admins={this.state.siteRes.admins}
329 postCreatorId={this.state.post.creator_id}
331 enableDownvotes={this.state.siteRes.site.enable_downvotes}
332 sort={this.state.commentSort}
342 community={this.state.community}
343 moderators={this.state.moderators}
344 admins={this.state.siteRes.admins}
345 online={this.state.online}
346 enableNsfw={this.state.siteRes.site.enable_nsfw}
353 handleCommentSortChange(i: Post, event: any) {
354 i.state.commentSort = Number(event.target.value);
355 i.state.commentViewType = CommentViewType.Tree;
359 handleCommentViewTypeChange(i: Post, event: any) {
360 i.state.commentViewType = Number(event.target.value);
361 i.state.commentSort = CommentSortType.New;
365 buildCommentsTree(): Array<CommentNodeI> {
366 let map = new Map<number, CommentNodeI>();
367 for (let comment of this.state.comments) {
368 let node: CommentNodeI = {
372 map.set(comment.id, { ...node });
374 let tree: Array<CommentNodeI> = [];
375 for (let comment of this.state.comments) {
376 let child = map.get(comment.id);
377 if (comment.parent_id) {
378 let parent_ = map.get(comment.parent_id);
379 parent_.children.push(child);
384 this.setDepth(child);
390 setDepth(node: CommentNodeI, i: number = 0): void {
391 for (let child of node.children) {
392 child.comment.depth = i;
393 this.setDepth(child, i + 1);
398 let nodes = this.buildCommentsTree();
403 locked={this.state.post.locked}
404 moderators={this.state.moderators}
405 admins={this.state.siteRes.admins}
406 postCreatorId={this.state.post.creator_id}
407 sort={this.state.commentSort}
408 enableDownvotes={this.state.siteRes.site.enable_downvotes}
414 parseMessage(msg: WebSocketJsonResponse) {
416 let res = wsJsonToRes(msg);
418 toast(i18n.t(msg.error), 'danger');
420 } else if (msg.reconnect) {
421 WebSocketService.Instance.getPost({
422 id: Number(this.props.match.params.id),
424 } else if (res.op == UserOperation.GetPost) {
425 let data = res.data as GetPostResponse;
426 this.state.post = data.post;
427 this.state.comments = data.comments;
428 this.state.community = data.community;
429 this.state.moderators = data.moderators;
430 this.state.online = data.online;
431 this.state.loading = false;
434 if (this.state.post.url) {
435 let form: SearchForm = {
436 q: this.state.post.url,
437 type_: SearchType[SearchType.Url],
438 sort: SortType[SortType.TopAll],
442 WebSocketService.Instance.search(form);
445 this.setState(this.state);
447 } else if (res.op == UserOperation.CreateComment) {
448 let data = res.data as CommentResponse;
450 // Necessary since it might be a user reply
451 if (data.recipient_ids.length == 0) {
452 this.state.comments.unshift(data.comment);
453 this.setState(this.state);
456 res.op == UserOperation.EditComment ||
457 res.op == UserOperation.DeleteComment ||
458 res.op == UserOperation.RemoveComment
460 let data = res.data as CommentResponse;
461 editCommentRes(data, this.state.comments);
462 this.setState(this.state);
463 } else if (res.op == UserOperation.SaveComment) {
464 let data = res.data as CommentResponse;
465 saveCommentRes(data, this.state.comments);
466 this.setState(this.state);
468 } else if (res.op == UserOperation.CreateCommentLike) {
469 let data = res.data as CommentResponse;
470 createCommentLikeRes(data, this.state.comments);
471 this.setState(this.state);
472 } else if (res.op == UserOperation.CreatePostLike) {
473 let data = res.data as PostResponse;
474 createPostLikeRes(data, this.state.post);
475 this.setState(this.state);
477 res.op == UserOperation.EditPost ||
478 res.op == UserOperation.DeletePost ||
479 res.op == UserOperation.RemovePost ||
480 res.op == UserOperation.LockPost ||
481 res.op == UserOperation.StickyPost
483 let data = res.data as PostResponse;
484 this.state.post = data.post;
485 this.setState(this.state);
487 } else if (res.op == UserOperation.SavePost) {
488 let data = res.data as PostResponse;
489 this.state.post = data.post;
490 this.setState(this.state);
493 res.op == UserOperation.EditCommunity ||
494 res.op == UserOperation.DeleteCommunity ||
495 res.op == UserOperation.RemoveCommunity
497 let data = res.data as CommunityResponse;
498 this.state.community = data.community;
499 this.state.post.community_id = data.community.id;
500 this.state.post.community_name = data.community.name;
501 this.setState(this.state);
502 } else if (res.op == UserOperation.FollowCommunity) {
503 let data = res.data as CommunityResponse;
504 this.state.community.subscribed = data.community.subscribed;
505 this.state.community.number_of_subscribers =
506 data.community.number_of_subscribers;
507 this.setState(this.state);
508 } else if (res.op == UserOperation.BanFromCommunity) {
509 let data = res.data as BanFromCommunityResponse;
511 .filter(c => c.creator_id == data.user.id)
512 .forEach(c => (c.banned_from_community = data.banned));
513 if (this.state.post.creator_id == data.user.id) {
514 this.state.post.banned_from_community = data.banned;
516 this.setState(this.state);
517 } else if (res.op == UserOperation.AddModToCommunity) {
518 let data = res.data as AddModToCommunityResponse;
519 this.state.moderators = data.moderators;
520 this.setState(this.state);
521 } else if (res.op == UserOperation.BanUser) {
522 let data = res.data as BanUserResponse;
524 .filter(c => c.creator_id == data.user.id)
525 .forEach(c => (c.banned = data.banned));
526 if (this.state.post.creator_id == data.user.id) {
527 this.state.post.banned = data.banned;
529 this.setState(this.state);
530 } else if (res.op == UserOperation.AddAdmin) {
531 let data = res.data as AddAdminResponse;
532 this.state.siteRes.admins = data.admins;
533 this.setState(this.state);
534 } else if (res.op == UserOperation.Search) {
535 let data = res.data as SearchResponse;
536 this.state.crossPosts = data.posts.filter(
537 p => p.id != Number(this.props.match.params.id)
539 if (this.state.crossPosts.length) {
540 this.state.post.duplicates = this.state.crossPosts;
542 this.setState(this.state);
544 res.op == UserOperation.TransferSite ||
545 res.op == UserOperation.GetSite
547 let data = res.data as GetSiteResponse;
548 this.state.siteRes = data;
549 this.setState(this.state);
550 } else if (res.op == UserOperation.TransferCommunity) {
551 let data = res.data as GetCommunityResponse;
552 this.state.community = data.community;
553 this.state.moderators = data.moderators;
554 this.setState(this.state);