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,
98 constructor(props: any, context: any) {
99 super(props, context);
101 this.state = this.emptyState;
103 let postId = Number(this.props.match.params.id);
104 if (this.props.match.params.comment_id) {
105 this.state.scrolled_comment_id = this.props.match.params.comment_id;
108 this.subscription = WebSocketService.Instance.subject
109 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
111 msg => this.parseMessage(msg),
112 err => console.error(err),
113 () => console.log('complete')
116 let form: GetPostForm = {
119 WebSocketService.Instance.getPost(form);
120 WebSocketService.Instance.getSite();
123 componentWillUnmount() {
124 this.subscription.unsubscribe();
127 componentDidMount() {
128 autosize(document.querySelectorAll('textarea'));
131 componentDidUpdate(_lastProps: any, lastState: PostState, _snapshot: any) {
133 this.state.scrolled_comment_id &&
134 !this.state.scrolled &&
135 lastState.comments.length > 0
137 var elmnt = document.getElementById(
138 `comment-${this.state.scrolled_comment_id}`
140 elmnt.scrollIntoView();
141 elmnt.classList.add('mark');
142 this.state.scrolled = true;
143 this.markScrolledAsRead(this.state.scrolled_comment_id);
146 // Necessary if you are on a post and you click another post (same route)
147 if (_lastProps.location.pathname !== _lastProps.history.location.pathname) {
148 // Couldnt get a refresh working. This does for now.
151 // let currentId = this.props.match.params.id;
152 // WebSocketService.Instance.getPost(currentId);
153 // this.context.router.history.push('/sponsors');
154 // this.context.refresh();
155 // this.context.router.history.push(_lastProps.location.pathname);
159 markScrolledAsRead(commentId: number) {
160 let found = this.state.comments.find(c => c.id == commentId);
161 let parent = this.state.comments.find(c => found.parent_id == c.id);
162 let parent_user_id = parent
164 : this.state.post.creator_id;
167 UserService.Instance.user &&
168 UserService.Instance.user.id == parent_user_id
170 let form: MarkCommentAsReadForm = {
175 WebSocketService.Instance.markCommentAsRead(form);
176 UserService.Instance.user.unreadCount--;
177 UserService.Instance.sub.next({
178 user: UserService.Instance.user,
185 <div class="container">
186 {this.state.loading ? (
188 <svg class="icon icon-spinner spin">
189 <use xlinkHref="#icon-spinner"></use>
194 <div class="col-12 col-md-8 mb-3">
196 post={this.state.post}
199 moderators={this.state.moderators}
200 admins={this.state.siteRes.admins}
201 enableDownvotes={this.state.siteRes.site.enable_downvotes}
202 enableNsfw={this.state.siteRes.site.enable_nsfw}
204 <div className="mb-2" />
206 postId={this.state.post.id}
207 disabled={this.state.post.locked}
209 {this.state.comments.length > 0 && this.sortRadios()}
210 {this.state.commentViewType == CommentViewType.Tree &&
212 {this.state.commentViewType == CommentViewType.Chat &&
215 <div class="col-12 col-sm-12 col-md-4">{this.sidebar()}</div>
225 <div class="btn-group btn-group-toggle mr-3 mb-2">
227 className={`btn btn-sm btn-secondary pointer ${
228 this.state.commentSort === CommentSortType.Hot && 'active'
234 value={CommentSortType.Hot}
235 checked={this.state.commentSort === CommentSortType.Hot}
236 onChange={linkEvent(this, this.handleCommentSortChange)}
240 className={`btn btn-sm btn-secondary pointer ${
241 this.state.commentSort === CommentSortType.Top && 'active'
247 value={CommentSortType.Top}
248 checked={this.state.commentSort === CommentSortType.Top}
249 onChange={linkEvent(this, this.handleCommentSortChange)}
253 className={`btn btn-sm btn-secondary pointer ${
254 this.state.commentSort === CommentSortType.New && 'active'
260 value={CommentSortType.New}
261 checked={this.state.commentSort === CommentSortType.New}
262 onChange={linkEvent(this, this.handleCommentSortChange)}
266 className={`btn btn-sm btn-secondary pointer ${
267 this.state.commentSort === CommentSortType.Old && 'active'
273 value={CommentSortType.Old}
274 checked={this.state.commentSort === CommentSortType.Old}
275 onChange={linkEvent(this, this.handleCommentSortChange)}
279 <div class="btn-group btn-group-toggle mb-2">
281 className={`btn btn-sm btn-secondary pointer ${
282 this.state.commentViewType === CommentViewType.Chat && 'active'
288 value={CommentViewType.Chat}
289 checked={this.state.commentViewType === CommentViewType.Chat}
290 onChange={linkEvent(this, this.handleCommentViewTypeChange)}
302 nodes={commentsToFlatNodes(this.state.comments)}
304 locked={this.state.post.locked}
305 moderators={this.state.moderators}
306 admins={this.state.siteRes.admins}
307 postCreatorId={this.state.post.creator_id}
309 enableDownvotes={this.state.siteRes.site.enable_downvotes}
310 sort={this.state.commentSort}
320 community={this.state.community}
321 moderators={this.state.moderators}
322 admins={this.state.siteRes.admins}
323 online={this.state.online}
324 enableNsfw={this.state.siteRes.site.enable_nsfw}
330 handleCommentSortChange(i: Post, event: any) {
331 i.state.commentSort = Number(event.target.value);
332 i.state.commentViewType = CommentViewType.Tree;
336 handleCommentViewTypeChange(i: Post, event: any) {
337 i.state.commentViewType = Number(event.target.value);
338 i.state.commentSort = CommentSortType.New;
342 buildCommentsTree(): Array<CommentNodeI> {
343 let map = new Map<number, CommentNodeI>();
344 for (let comment of this.state.comments) {
345 let node: CommentNodeI = {
349 map.set(comment.id, { ...node });
351 let tree: Array<CommentNodeI> = [];
352 for (let comment of this.state.comments) {
353 let child = map.get(comment.id);
354 if (comment.parent_id) {
355 let parent_ = map.get(comment.parent_id);
356 parent_.children.push(child);
361 this.setDepth(child);
367 setDepth(node: CommentNodeI, i: number = 0): void {
368 for (let child of node.children) {
369 child.comment.depth = i;
370 this.setDepth(child, i + 1);
375 let nodes = this.buildCommentsTree();
380 locked={this.state.post.locked}
381 moderators={this.state.moderators}
382 admins={this.state.siteRes.admins}
383 postCreatorId={this.state.post.creator_id}
384 sort={this.state.commentSort}
385 enableDownvotes={this.state.siteRes.site.enable_downvotes}
391 parseMessage(msg: WebSocketJsonResponse) {
393 let res = wsJsonToRes(msg);
395 toast(i18n.t(msg.error), 'danger');
397 } else if (msg.reconnect) {
398 WebSocketService.Instance.getPost({
399 id: Number(this.props.match.params.id),
401 } else if (res.op == UserOperation.GetPost) {
402 let data = res.data as GetPostResponse;
403 this.state.post = data.post;
404 this.state.comments = data.comments;
405 this.state.community = data.community;
406 this.state.moderators = data.moderators;
407 this.state.siteRes.admins = data.admins;
408 this.state.online = data.online;
409 this.state.loading = false;
410 document.title = `${this.state.post.name} - ${this.state.siteRes.site.name}`;
413 if (this.state.post.url) {
414 let form: SearchForm = {
415 q: this.state.post.url,
416 type_: SearchType[SearchType.Url],
417 sort: SortType[SortType.TopAll],
421 WebSocketService.Instance.search(form);
424 this.setState(this.state);
426 } else if (res.op == UserOperation.CreateComment) {
427 let data = res.data as CommentResponse;
429 // Necessary since it might be a user reply
430 if (data.recipient_ids.length == 0) {
431 this.state.comments.unshift(data.comment);
432 this.setState(this.state);
435 res.op == UserOperation.EditComment ||
436 res.op == UserOperation.DeleteComment ||
437 res.op == UserOperation.RemoveComment
439 let data = res.data as CommentResponse;
440 editCommentRes(data, this.state.comments);
441 this.setState(this.state);
442 } else if (res.op == UserOperation.SaveComment) {
443 let data = res.data as CommentResponse;
444 saveCommentRes(data, this.state.comments);
445 this.setState(this.state);
447 } else if (res.op == UserOperation.CreateCommentLike) {
448 let data = res.data as CommentResponse;
449 createCommentLikeRes(data, this.state.comments);
450 this.setState(this.state);
451 } else if (res.op == UserOperation.CreatePostLike) {
452 let data = res.data as PostResponse;
453 createPostLikeRes(data, this.state.post);
454 this.setState(this.state);
456 res.op == UserOperation.EditPost ||
457 res.op == UserOperation.DeletePost ||
458 res.op == UserOperation.RemovePost ||
459 res.op == UserOperation.LockPost ||
460 res.op == UserOperation.StickyPost
462 let data = res.data as PostResponse;
463 this.state.post = data.post;
464 this.setState(this.state);
466 } else if (res.op == UserOperation.SavePost) {
467 let data = res.data as PostResponse;
468 this.state.post = data.post;
469 this.setState(this.state);
472 res.op == UserOperation.EditCommunity ||
473 res.op == UserOperation.DeleteCommunity ||
474 res.op == UserOperation.RemoveCommunity
476 let data = res.data as CommunityResponse;
477 this.state.community = data.community;
478 this.state.post.community_id = data.community.id;
479 this.state.post.community_name = data.community.name;
480 this.setState(this.state);
481 } else if (res.op == UserOperation.FollowCommunity) {
482 let data = res.data as CommunityResponse;
483 this.state.community.subscribed = data.community.subscribed;
484 this.state.community.number_of_subscribers =
485 data.community.number_of_subscribers;
486 this.setState(this.state);
487 } else if (res.op == UserOperation.BanFromCommunity) {
488 let data = res.data as BanFromCommunityResponse;
490 .filter(c => c.creator_id == data.user.id)
491 .forEach(c => (c.banned_from_community = data.banned));
492 if (this.state.post.creator_id == data.user.id) {
493 this.state.post.banned_from_community = data.banned;
495 this.setState(this.state);
496 } else if (res.op == UserOperation.AddModToCommunity) {
497 let data = res.data as AddModToCommunityResponse;
498 this.state.moderators = data.moderators;
499 this.setState(this.state);
500 } else if (res.op == UserOperation.BanUser) {
501 let data = res.data as BanUserResponse;
503 .filter(c => c.creator_id == data.user.id)
504 .forEach(c => (c.banned = data.banned));
505 if (this.state.post.creator_id == data.user.id) {
506 this.state.post.banned = data.banned;
508 this.setState(this.state);
509 } else if (res.op == UserOperation.AddAdmin) {
510 let data = res.data as AddAdminResponse;
511 this.state.siteRes.admins = data.admins;
512 this.setState(this.state);
513 } else if (res.op == UserOperation.Search) {
514 let data = res.data as SearchResponse;
515 this.state.crossPosts = data.posts.filter(
516 p => p.id != Number(this.props.match.params.id)
518 if (this.state.crossPosts.length) {
519 this.state.post.duplicates = this.state.crossPosts;
521 this.setState(this.state);
523 res.op == UserOperation.TransferSite ||
524 res.op == UserOperation.GetSite
526 let data = res.data as GetSiteResponse;
527 this.state.siteRes = data;
528 this.setState(this.state);
529 } else if (res.op == UserOperation.TransferCommunity) {
530 let data = res.data as GetCommunityResponse;
531 this.state.community = data.community;
532 this.state.moderators = data.moderators;
533 this.state.siteRes.admins = data.admins;
534 this.setState(this.state);