1 import { Component, linkEvent } from 'inferno';
2 import { Subscription } from 'rxjs';
3 import { retryWhen, delay, take } from 'rxjs/operators';
11 MarkCommentAsReadForm,
17 CommentNode as CommentNodeI,
18 BanFromCommunityResponse,
20 AddModToCommunityResponse,
29 WebSocketJsonResponse,
30 } from '../interfaces';
31 import { WebSocketService, UserService } from '../services';
42 import { PostListing } from './post-listing';
43 import { Sidebar } from './sidebar';
44 import { CommentForm } from './comment-form';
45 import { CommentNodes } from './comment-nodes';
46 import autosize from 'autosize';
47 import { i18n } from '../i18next';
51 comments: Array<Comment>;
52 commentSort: CommentSortType;
53 commentViewType: CommentViewType;
55 moderators: Array<CommunityUser>;
58 scrolled_comment_id?: number;
60 crossPosts: Array<PostI>;
61 siteRes: GetSiteResponse;
64 export class Post extends Component<any, PostState> {
65 private subscription: Subscription;
66 private emptyState: PostState = {
69 commentSort: CommentSortType.Hot,
70 commentViewType: CommentViewType.Tree,
83 creator_id: undefined,
85 creator_name: undefined,
86 number_of_users: undefined,
87 number_of_posts: undefined,
88 number_of_comments: undefined,
89 number_of_communities: undefined,
90 enable_downvotes: undefined,
91 open_registration: undefined,
92 enable_nsfw: undefined,
99 constructor(props: any, context: any) {
100 super(props, context);
102 this.state = this.emptyState;
104 let postId = Number(this.props.match.params.id);
105 if (this.props.match.params.comment_id) {
106 this.state.scrolled_comment_id = this.props.match.params.comment_id;
109 this.subscription = WebSocketService.Instance.subject
110 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
112 msg => this.parseMessage(msg),
113 err => console.error(err),
114 () => console.log('complete')
117 let form: GetPostForm = {
120 WebSocketService.Instance.getPost(form);
121 WebSocketService.Instance.getSite();
124 componentWillUnmount() {
125 this.subscription.unsubscribe();
128 componentDidMount() {
129 autosize(document.querySelectorAll('textarea'));
132 componentDidUpdate(_lastProps: any, lastState: PostState, _snapshot: any) {
134 this.state.scrolled_comment_id &&
135 !this.state.scrolled &&
136 lastState.comments.length > 0
138 var elmnt = document.getElementById(
139 `comment-${this.state.scrolled_comment_id}`
141 elmnt.scrollIntoView();
142 elmnt.classList.add('mark');
143 this.state.scrolled = true;
144 this.markScrolledAsRead(this.state.scrolled_comment_id);
147 // Necessary if you are on a post and you click another post (same route)
148 if (_lastProps.location.pathname !== _lastProps.history.location.pathname) {
149 // Couldnt get a refresh working. This does for now.
152 // let currentId = this.props.match.params.id;
153 // WebSocketService.Instance.getPost(currentId);
154 // this.context.router.history.push('/sponsors');
155 // this.context.refresh();
156 // this.context.router.history.push(_lastProps.location.pathname);
160 markScrolledAsRead(commentId: number) {
161 let found = this.state.comments.find(c => c.id == commentId);
162 let parent = this.state.comments.find(c => found.parent_id == c.id);
163 let parent_user_id = parent
165 : this.state.post.creator_id;
168 UserService.Instance.user &&
169 UserService.Instance.user.id == parent_user_id
171 let form: MarkCommentAsReadForm = {
176 WebSocketService.Instance.markCommentAsRead(form);
177 UserService.Instance.user.unreadCount--;
178 UserService.Instance.sub.next({
179 user: UserService.Instance.user,
186 <div class="container">
187 {this.state.loading ? (
189 <svg class="icon icon-spinner spin">
190 <use xlinkHref="#icon-spinner"></use>
195 <div class="col-12 col-md-8 mb-3">
197 post={this.state.post}
200 moderators={this.state.moderators}
201 admins={this.state.siteRes.admins}
202 enableDownvotes={this.state.siteRes.site.enable_downvotes}
203 enableNsfw={this.state.siteRes.site.enable_nsfw}
205 <div className="mb-2" />
207 postId={this.state.post.id}
208 disabled={this.state.post.locked}
210 {this.state.comments.length > 0 && this.sortRadios()}
211 {this.state.commentViewType == CommentViewType.Tree &&
213 {this.state.commentViewType == CommentViewType.Chat &&
216 <div class="col-12 col-sm-12 col-md-4">{this.sidebar()}</div>
226 <div class="btn-group btn-group-toggle mr-3 mb-2">
228 className={`btn btn-sm btn-secondary pointer ${
229 this.state.commentSort === CommentSortType.Hot && 'active'
235 value={CommentSortType.Hot}
236 checked={this.state.commentSort === CommentSortType.Hot}
237 onChange={linkEvent(this, this.handleCommentSortChange)}
241 className={`btn btn-sm btn-secondary pointer ${
242 this.state.commentSort === CommentSortType.Top && 'active'
248 value={CommentSortType.Top}
249 checked={this.state.commentSort === CommentSortType.Top}
250 onChange={linkEvent(this, this.handleCommentSortChange)}
254 className={`btn btn-sm btn-secondary pointer ${
255 this.state.commentSort === CommentSortType.New && 'active'
261 value={CommentSortType.New}
262 checked={this.state.commentSort === CommentSortType.New}
263 onChange={linkEvent(this, this.handleCommentSortChange)}
267 className={`btn btn-sm btn-secondary pointer ${
268 this.state.commentSort === CommentSortType.Old && 'active'
274 value={CommentSortType.Old}
275 checked={this.state.commentSort === CommentSortType.Old}
276 onChange={linkEvent(this, this.handleCommentSortChange)}
280 <div class="btn-group btn-group-toggle mb-2">
282 className={`btn btn-sm btn-secondary pointer ${
283 this.state.commentViewType === CommentViewType.Chat && 'active'
289 value={CommentViewType.Chat}
290 checked={this.state.commentViewType === CommentViewType.Chat}
291 onChange={linkEvent(this, this.handleCommentViewTypeChange)}
303 nodes={commentsToFlatNodes(this.state.comments)}
305 locked={this.state.post.locked}
306 moderators={this.state.moderators}
307 admins={this.state.siteRes.admins}
308 postCreatorId={this.state.post.creator_id}
310 enableDownvotes={this.state.siteRes.site.enable_downvotes}
311 sort={this.state.commentSort}
321 community={this.state.community}
322 moderators={this.state.moderators}
323 admins={this.state.siteRes.admins}
324 online={this.state.online}
325 enableNsfw={this.state.siteRes.site.enable_nsfw}
331 handleCommentSortChange(i: Post, event: any) {
332 i.state.commentSort = Number(event.target.value);
333 i.state.commentViewType = CommentViewType.Tree;
337 handleCommentViewTypeChange(i: Post, event: any) {
338 i.state.commentViewType = Number(event.target.value);
339 i.state.commentSort = CommentSortType.New;
343 buildCommentsTree(): Array<CommentNodeI> {
344 let map = new Map<number, CommentNodeI>();
345 for (let comment of this.state.comments) {
346 let node: CommentNodeI = {
350 map.set(comment.id, { ...node });
352 let tree: Array<CommentNodeI> = [];
353 for (let comment of this.state.comments) {
354 let child = map.get(comment.id);
355 if (comment.parent_id) {
356 let parent_ = map.get(comment.parent_id);
357 parent_.children.push(child);
362 this.setDepth(child);
368 setDepth(node: CommentNodeI, i: number = 0): void {
369 for (let child of node.children) {
370 child.comment.depth = i;
371 this.setDepth(child, i + 1);
376 let nodes = this.buildCommentsTree();
381 locked={this.state.post.locked}
382 moderators={this.state.moderators}
383 admins={this.state.siteRes.admins}
384 postCreatorId={this.state.post.creator_id}
385 sort={this.state.commentSort}
386 enableDownvotes={this.state.siteRes.site.enable_downvotes}
392 parseMessage(msg: WebSocketJsonResponse) {
394 let res = wsJsonToRes(msg);
396 toast(i18n.t(msg.error), 'danger');
398 } else if (msg.reconnect) {
399 WebSocketService.Instance.getPost({
400 id: Number(this.props.match.params.id),
402 } else if (res.op == UserOperation.GetPost) {
403 let data = res.data as GetPostResponse;
404 this.state.post = data.post;
405 this.state.comments = data.comments;
406 this.state.community = data.community;
407 this.state.moderators = data.moderators;
408 this.state.siteRes.admins = data.admins;
409 this.state.online = data.online;
410 this.state.loading = false;
411 document.title = `${this.state.post.name} - ${this.state.siteRes.site.name}`;
414 if (this.state.post.url) {
415 let form: SearchForm = {
416 q: this.state.post.url,
417 type_: SearchType[SearchType.Url],
418 sort: SortType[SortType.TopAll],
422 WebSocketService.Instance.search(form);
425 this.setState(this.state);
427 } else if (res.op == UserOperation.CreateComment) {
428 let data = res.data as CommentResponse;
430 // Necessary since it might be a user reply
431 if (data.recipient_ids.length == 0) {
432 this.state.comments.unshift(data.comment);
433 this.setState(this.state);
436 res.op == UserOperation.EditComment ||
437 res.op == UserOperation.DeleteComment ||
438 res.op == UserOperation.RemoveComment
440 let data = res.data as CommentResponse;
441 editCommentRes(data, this.state.comments);
442 this.setState(this.state);
443 } else if (res.op == UserOperation.SaveComment) {
444 let data = res.data as CommentResponse;
445 saveCommentRes(data, this.state.comments);
446 this.setState(this.state);
448 } else if (res.op == UserOperation.CreateCommentLike) {
449 let data = res.data as CommentResponse;
450 createCommentLikeRes(data, this.state.comments);
451 this.setState(this.state);
452 } else if (res.op == UserOperation.CreatePostLike) {
453 let data = res.data as PostResponse;
454 createPostLikeRes(data, this.state.post);
455 this.setState(this.state);
457 res.op == UserOperation.EditPost ||
458 res.op == UserOperation.DeletePost ||
459 res.op == UserOperation.RemovePost ||
460 res.op == UserOperation.LockPost ||
461 res.op == UserOperation.StickyPost
463 let data = res.data as PostResponse;
464 this.state.post = data.post;
465 this.setState(this.state);
467 } else if (res.op == UserOperation.SavePost) {
468 let data = res.data as PostResponse;
469 this.state.post = data.post;
470 this.setState(this.state);
473 res.op == UserOperation.EditCommunity ||
474 res.op == UserOperation.DeleteCommunity ||
475 res.op == UserOperation.RemoveCommunity
477 let data = res.data as CommunityResponse;
478 this.state.community = data.community;
479 this.state.post.community_id = data.community.id;
480 this.state.post.community_name = data.community.name;
481 this.setState(this.state);
482 } else if (res.op == UserOperation.FollowCommunity) {
483 let data = res.data as CommunityResponse;
484 this.state.community.subscribed = data.community.subscribed;
485 this.state.community.number_of_subscribers =
486 data.community.number_of_subscribers;
487 this.setState(this.state);
488 } else if (res.op == UserOperation.BanFromCommunity) {
489 let data = res.data as BanFromCommunityResponse;
491 .filter(c => c.creator_id == data.user.id)
492 .forEach(c => (c.banned_from_community = data.banned));
493 if (this.state.post.creator_id == data.user.id) {
494 this.state.post.banned_from_community = data.banned;
496 this.setState(this.state);
497 } else if (res.op == UserOperation.AddModToCommunity) {
498 let data = res.data as AddModToCommunityResponse;
499 this.state.moderators = data.moderators;
500 this.setState(this.state);
501 } else if (res.op == UserOperation.BanUser) {
502 let data = res.data as BanUserResponse;
504 .filter(c => c.creator_id == data.user.id)
505 .forEach(c => (c.banned = data.banned));
506 if (this.state.post.creator_id == data.user.id) {
507 this.state.post.banned = data.banned;
509 this.setState(this.state);
510 } else if (res.op == UserOperation.AddAdmin) {
511 let data = res.data as AddAdminResponse;
512 this.state.siteRes.admins = data.admins;
513 this.setState(this.state);
514 } else if (res.op == UserOperation.Search) {
515 let data = res.data as SearchResponse;
516 this.state.crossPosts = data.posts.filter(
517 p => p.id != Number(this.props.match.params.id)
519 if (this.state.crossPosts.length) {
520 this.state.post.duplicates = this.state.crossPosts;
522 this.setState(this.state);
524 res.op == UserOperation.TransferSite ||
525 res.op == UserOperation.GetSite
527 let data = res.data as GetSiteResponse;
528 this.state.siteRes = data;
529 this.setState(this.state);
530 } else if (res.op == UserOperation.TransferCommunity) {
531 let data = res.data as GetCommunityResponse;
532 this.state.community = data.community;
533 this.state.moderators = data.moderators;
534 this.state.siteRes.admins = data.admins;
535 this.setState(this.state);