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,
100 federated_instances: undefined,
104 constructor(props: any, context: any) {
105 super(props, context);
107 this.state = this.emptyState;
109 let postId = Number(this.props.match.params.id);
110 if (this.props.match.params.comment_id) {
111 this.state.scrolled_comment_id = this.props.match.params.comment_id;
114 this.subscription = WebSocketService.Instance.subject
115 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
117 msg => this.parseMessage(msg),
118 err => console.error(err),
119 () => console.log('complete')
122 let form: GetPostForm = {
125 WebSocketService.Instance.getPost(form);
126 WebSocketService.Instance.getSite();
129 componentWillUnmount() {
130 this.subscription.unsubscribe();
133 componentDidMount() {
134 autosize(document.querySelectorAll('textarea'));
137 componentDidUpdate(_lastProps: any, lastState: PostState, _snapshot: any) {
139 this.state.scrolled_comment_id &&
140 !this.state.scrolled &&
141 lastState.comments.length > 0
143 var elmnt = document.getElementById(
144 `comment-${this.state.scrolled_comment_id}`
146 elmnt.scrollIntoView();
147 elmnt.classList.add('mark');
148 this.state.scrolled = true;
149 this.markScrolledAsRead(this.state.scrolled_comment_id);
152 // Necessary if you are on a post and you click another post (same route)
153 if (_lastProps.location.pathname !== _lastProps.history.location.pathname) {
154 // Couldnt get a refresh working. This does for now.
157 // let currentId = this.props.match.params.id;
158 // WebSocketService.Instance.getPost(currentId);
159 // this.context.router.history.push('/sponsors');
160 // this.context.refresh();
161 // this.context.router.history.push(_lastProps.location.pathname);
165 markScrolledAsRead(commentId: number) {
166 let found = this.state.comments.find(c => c.id == commentId);
167 let parent = this.state.comments.find(c => found.parent_id == c.id);
168 let parent_user_id = parent
170 : this.state.post.creator_id;
173 UserService.Instance.user &&
174 UserService.Instance.user.id == parent_user_id
176 let form: MarkCommentAsReadForm = {
181 WebSocketService.Instance.markCommentAsRead(form);
182 UserService.Instance.unreadCountSub.next(
183 UserService.Instance.unreadCountSub.value - 1
188 get documentTitle(): string {
189 if (this.state.post) {
190 return `${this.state.post.name} - ${this.state.siteRes.site.name}`;
196 get favIcon(): string {
197 return this.state.siteRes.site.icon
198 ? this.state.siteRes.site.icon
204 <div class="container">
205 <Helmet title={this.documentTitle}>
213 {this.state.loading ? (
215 <svg class="icon icon-spinner spin">
216 <use xlinkHref="#icon-spinner"></use>
221 <div class="col-12 col-md-8 mb-3">
223 post={this.state.post}
226 moderators={this.state.moderators}
227 admins={this.state.siteRes.admins}
228 enableDownvotes={this.state.siteRes.site.enable_downvotes}
229 enableNsfw={this.state.siteRes.site.enable_nsfw}
231 <div className="mb-2" />
233 postId={this.state.post.id}
234 disabled={this.state.post.locked}
236 {this.state.comments.length > 0 && this.sortRadios()}
237 {this.state.commentViewType == CommentViewType.Tree &&
239 {this.state.commentViewType == CommentViewType.Chat &&
242 <div class="col-12 col-sm-12 col-md-4">{this.sidebar()}</div>
252 <div class="btn-group btn-group-toggle flex-wrap mr-3 mb-2">
254 className={`btn btn-outline-secondary pointer ${
255 this.state.commentSort === CommentSortType.Hot && 'active'
261 value={CommentSortType.Hot}
262 checked={this.state.commentSort === CommentSortType.Hot}
263 onChange={linkEvent(this, this.handleCommentSortChange)}
267 className={`btn btn-outline-secondary pointer ${
268 this.state.commentSort === CommentSortType.Top && 'active'
274 value={CommentSortType.Top}
275 checked={this.state.commentSort === CommentSortType.Top}
276 onChange={linkEvent(this, this.handleCommentSortChange)}
280 className={`btn btn-outline-secondary pointer ${
281 this.state.commentSort === CommentSortType.New && 'active'
287 value={CommentSortType.New}
288 checked={this.state.commentSort === CommentSortType.New}
289 onChange={linkEvent(this, this.handleCommentSortChange)}
293 className={`btn btn-outline-secondary pointer ${
294 this.state.commentSort === CommentSortType.Old && 'active'
300 value={CommentSortType.Old}
301 checked={this.state.commentSort === CommentSortType.Old}
302 onChange={linkEvent(this, this.handleCommentSortChange)}
306 <div class="btn-group btn-group-toggle flex-wrap mb-2">
308 className={`btn btn-outline-secondary pointer ${
309 this.state.commentViewType === CommentViewType.Chat && 'active'
315 value={CommentViewType.Chat}
316 checked={this.state.commentViewType === CommentViewType.Chat}
317 onChange={linkEvent(this, this.handleCommentViewTypeChange)}
329 nodes={commentsToFlatNodes(this.state.comments)}
331 locked={this.state.post.locked}
332 moderators={this.state.moderators}
333 admins={this.state.siteRes.admins}
334 postCreatorId={this.state.post.creator_id}
336 enableDownvotes={this.state.siteRes.site.enable_downvotes}
337 sort={this.state.commentSort}
347 community={this.state.community}
348 moderators={this.state.moderators}
349 admins={this.state.siteRes.admins}
350 online={this.state.online}
351 enableNsfw={this.state.siteRes.site.enable_nsfw}
358 handleCommentSortChange(i: Post, event: any) {
359 i.state.commentSort = Number(event.target.value);
360 i.state.commentViewType = CommentViewType.Tree;
364 handleCommentViewTypeChange(i: Post, event: any) {
365 i.state.commentViewType = Number(event.target.value);
366 i.state.commentSort = CommentSortType.New;
370 buildCommentsTree(): Array<CommentNodeI> {
371 let map = new Map<number, CommentNodeI>();
372 for (let comment of this.state.comments) {
373 let node: CommentNodeI = {
377 map.set(comment.id, { ...node });
379 let tree: Array<CommentNodeI> = [];
380 for (let comment of this.state.comments) {
381 let child = map.get(comment.id);
382 if (comment.parent_id) {
383 let parent_ = map.get(comment.parent_id);
384 parent_.children.push(child);
389 this.setDepth(child);
395 setDepth(node: CommentNodeI, i: number = 0): void {
396 for (let child of node.children) {
397 child.comment.depth = i;
398 this.setDepth(child, i + 1);
403 let nodes = this.buildCommentsTree();
408 locked={this.state.post.locked}
409 moderators={this.state.moderators}
410 admins={this.state.siteRes.admins}
411 postCreatorId={this.state.post.creator_id}
412 sort={this.state.commentSort}
413 enableDownvotes={this.state.siteRes.site.enable_downvotes}
419 parseMessage(msg: WebSocketJsonResponse) {
421 let res = wsJsonToRes(msg);
423 toast(i18n.t(msg.error), 'danger');
425 } else if (msg.reconnect) {
426 WebSocketService.Instance.getPost({
427 id: Number(this.props.match.params.id),
429 } else if (res.op == UserOperation.GetPost) {
430 let data = res.data as GetPostResponse;
431 this.state.post = data.post;
432 this.state.comments = data.comments;
433 this.state.community = data.community;
434 this.state.moderators = data.moderators;
435 this.state.online = data.online;
436 this.state.loading = false;
439 if (this.state.post.url) {
440 let form: SearchForm = {
441 q: this.state.post.url,
442 type_: SearchType[SearchType.Url],
443 sort: SortType[SortType.TopAll],
447 WebSocketService.Instance.search(form);
450 this.setState(this.state);
452 } else if (res.op == UserOperation.CreateComment) {
453 let data = res.data as CommentResponse;
455 // Necessary since it might be a user reply
456 if (data.recipient_ids.length == 0) {
457 this.state.comments.unshift(data.comment);
458 this.setState(this.state);
461 res.op == UserOperation.EditComment ||
462 res.op == UserOperation.DeleteComment ||
463 res.op == UserOperation.RemoveComment
465 let data = res.data as CommentResponse;
466 editCommentRes(data, this.state.comments);
467 this.setState(this.state);
468 } else if (res.op == UserOperation.SaveComment) {
469 let data = res.data as CommentResponse;
470 saveCommentRes(data, this.state.comments);
471 this.setState(this.state);
473 } else if (res.op == UserOperation.CreateCommentLike) {
474 let data = res.data as CommentResponse;
475 createCommentLikeRes(data, this.state.comments);
476 this.setState(this.state);
477 } else if (res.op == UserOperation.CreatePostLike) {
478 let data = res.data as PostResponse;
479 createPostLikeRes(data, this.state.post);
480 this.setState(this.state);
482 res.op == UserOperation.EditPost ||
483 res.op == UserOperation.DeletePost ||
484 res.op == UserOperation.RemovePost ||
485 res.op == UserOperation.LockPost ||
486 res.op == UserOperation.StickyPost
488 let data = res.data as PostResponse;
489 this.state.post = data.post;
490 this.setState(this.state);
492 } else if (res.op == UserOperation.SavePost) {
493 let data = res.data as PostResponse;
494 this.state.post = data.post;
495 this.setState(this.state);
498 res.op == UserOperation.EditCommunity ||
499 res.op == UserOperation.DeleteCommunity ||
500 res.op == UserOperation.RemoveCommunity
502 let data = res.data as CommunityResponse;
503 this.state.community = data.community;
504 this.state.post.community_id = data.community.id;
505 this.state.post.community_name = data.community.name;
506 this.setState(this.state);
507 } else if (res.op == UserOperation.FollowCommunity) {
508 let data = res.data as CommunityResponse;
509 this.state.community.subscribed = data.community.subscribed;
510 this.state.community.number_of_subscribers =
511 data.community.number_of_subscribers;
512 this.setState(this.state);
513 } else if (res.op == UserOperation.BanFromCommunity) {
514 let data = res.data as BanFromCommunityResponse;
516 .filter(c => c.creator_id == data.user.id)
517 .forEach(c => (c.banned_from_community = data.banned));
518 if (this.state.post.creator_id == data.user.id) {
519 this.state.post.banned_from_community = data.banned;
521 this.setState(this.state);
522 } else if (res.op == UserOperation.AddModToCommunity) {
523 let data = res.data as AddModToCommunityResponse;
524 this.state.moderators = data.moderators;
525 this.setState(this.state);
526 } else if (res.op == UserOperation.BanUser) {
527 let data = res.data as BanUserResponse;
529 .filter(c => c.creator_id == data.user.id)
530 .forEach(c => (c.banned = data.banned));
531 if (this.state.post.creator_id == data.user.id) {
532 this.state.post.banned = data.banned;
534 this.setState(this.state);
535 } else if (res.op == UserOperation.AddAdmin) {
536 let data = res.data as AddAdminResponse;
537 this.state.siteRes.admins = data.admins;
538 this.setState(this.state);
539 } else if (res.op == UserOperation.Search) {
540 let data = res.data as SearchResponse;
541 this.state.crossPosts = data.posts.filter(
542 p => p.id != Number(this.props.match.params.id)
544 if (this.state.crossPosts.length) {
545 this.state.post.duplicates = this.state.crossPosts;
547 this.setState(this.state);
549 res.op == UserOperation.TransferSite ||
550 res.op == UserOperation.GetSite
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.community = data.community;
558 this.state.moderators = data.moderators;
559 this.setState(this.state);