1 import { Component, linkEvent } from 'inferno';
2 import { Subscription } from 'rxjs';
3 import { retryWhen, delay, take } from 'rxjs/operators';
11 CommentForm as CommentFormI,
16 CommentNode as CommentNodeI,
17 BanFromCommunityResponse,
19 AddModToCommunityResponse,
29 WebSocketJsonResponse,
30 } from '../interfaces';
31 import { WebSocketService, UserService } from '../services';
32 import { wsJsonToRes, hotRank, toast } from '../utils';
33 import { PostListing } from './post-listing';
34 import { PostListings } from './post-listings';
35 import { Sidebar } from './sidebar';
36 import { CommentForm } from './comment-form';
37 import { CommentNodes } from './comment-nodes';
38 import autosize from 'autosize';
39 import { i18n } from '../i18next';
43 comments: Array<Comment>;
44 commentSort: CommentSortType;
46 moderators: Array<CommunityUser>;
47 admins: Array<UserView>;
49 scrolled_comment_id?: number;
51 crossPosts: Array<PostI>;
54 export class Post extends Component<any, PostState> {
55 private subscription: Subscription;
56 private emptyState: PostState = {
59 commentSort: CommentSortType.Hot,
68 constructor(props: any, context: any) {
69 super(props, context);
71 this.state = this.emptyState;
73 let postId = Number(this.props.match.params.id);
74 if (this.props.match.params.comment_id) {
75 this.state.scrolled_comment_id = this.props.match.params.comment_id;
78 this.subscription = WebSocketService.Instance.subject
79 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
81 msg => this.parseMessage(msg),
82 err => console.error(err),
83 () => console.log('complete')
86 let form: GetPostForm = {
89 WebSocketService.Instance.getPost(form);
92 componentWillUnmount() {
93 this.subscription.unsubscribe();
97 autosize(document.querySelectorAll('textarea'));
100 componentDidUpdate(_lastProps: any, lastState: PostState, _snapshot: any) {
102 this.state.scrolled_comment_id &&
103 !this.state.scrolled &&
104 lastState.comments.length > 0
106 var elmnt = document.getElementById(
107 `comment-${this.state.scrolled_comment_id}`
109 elmnt.scrollIntoView();
110 elmnt.classList.add('mark');
111 this.state.scrolled = true;
112 this.markScrolledAsRead(this.state.scrolled_comment_id);
115 // Necessary if you are on a post and you click another post (same route)
116 if (_lastProps.location.pathname !== _lastProps.history.location.pathname) {
117 // Couldnt get a refresh working. This does for now.
120 // let currentId = this.props.match.params.id;
121 // WebSocketService.Instance.getPost(currentId);
122 // this.context.router.history.push('/sponsors');
123 // this.context.refresh();
124 // this.context.router.history.push(_lastProps.location.pathname);
128 markScrolledAsRead(commentId: number) {
129 let found = this.state.comments.find(c => c.id == commentId);
130 let parent = this.state.comments.find(c => found.parent_id == c.id);
131 let parent_user_id = parent
133 : this.state.post.creator_id;
136 UserService.Instance.user &&
137 UserService.Instance.user.id == parent_user_id
139 let form: CommentFormI = {
140 content: found.content,
142 creator_id: found.creator_id,
143 post_id: found.post_id,
144 parent_id: found.parent_id,
148 WebSocketService.Instance.editComment(form);
154 <div class="container">
155 {this.state.loading ? (
157 <svg class="icon icon-spinner spin">
158 <use xlinkHref="#icon-spinner"></use>
163 <div class="col-12 col-md-8 mb-3">
165 post={this.state.post}
168 moderators={this.state.moderators}
169 admins={this.state.admins}
171 {this.state.crossPosts.length > 0 && (
173 <div class="my-1 text-muted small font-weight-bold">
174 {i18n.t('cross_posts')}
176 <PostListings showCommunity posts={this.state.crossPosts} />
179 <div className="mb-2" />
181 postId={this.state.post.id}
182 disabled={this.state.post.locked}
184 {this.state.comments.length > 0 && this.sortRadios()}
185 {this.commentsTree()}
187 <div class="col-12 col-sm-12 col-md-4">
188 {this.state.comments.length > 0 && this.newComments()}
199 <div class="btn-group btn-group-toggle mb-3">
201 className={`btn btn-sm btn-secondary pointer ${this.state
202 .commentSort === CommentSortType.Hot && 'active'}`}
207 value={CommentSortType.Hot}
208 checked={this.state.commentSort === CommentSortType.Hot}
209 onChange={linkEvent(this, this.handleCommentSortChange)}
213 className={`btn btn-sm btn-secondary pointer ${this.state
214 .commentSort === CommentSortType.Top && 'active'}`}
219 value={CommentSortType.Top}
220 checked={this.state.commentSort === CommentSortType.Top}
221 onChange={linkEvent(this, this.handleCommentSortChange)}
225 className={`btn btn-sm btn-secondary pointer ${this.state
226 .commentSort === CommentSortType.New && 'active'}`}
231 value={CommentSortType.New}
232 checked={this.state.commentSort === CommentSortType.New}
233 onChange={linkEvent(this, this.handleCommentSortChange)}
237 className={`btn btn-sm btn-secondary pointer ${this.state
238 .commentSort === CommentSortType.Old && 'active'}`}
243 value={CommentSortType.Old}
244 checked={this.state.commentSort === CommentSortType.Old}
245 onChange={linkEvent(this, this.handleCommentSortChange)}
254 <div class="d-none d-md-block new-comments mb-3 card border-secondary">
255 <div class="card-body small">
256 <h6>{i18n.t('recent_comments')}</h6>
257 {this.state.comments.map(comment => (
259 nodes={[{ comment: comment }]}
261 locked={this.state.post.locked}
262 moderators={this.state.moderators}
263 admins={this.state.admins}
264 postCreatorId={this.state.post.creator_id}
276 community={this.state.community}
277 moderators={this.state.moderators}
278 admins={this.state.admins}
284 handleCommentSortChange(i: Post, event: any) {
285 i.state.commentSort = Number(event.target.value);
289 private buildCommentsTree(): Array<CommentNodeI> {
290 let map = new Map<number, CommentNodeI>();
291 for (let comment of this.state.comments) {
292 let node: CommentNodeI = {
296 map.set(comment.id, { ...node });
298 let tree: Array<CommentNodeI> = [];
299 for (let comment of this.state.comments) {
300 if (comment.parent_id) {
301 map.get(comment.parent_id).children.push(map.get(comment.id));
303 tree.push(map.get(comment.id));
312 sortTree(tree: Array<CommentNodeI>) {
313 // First, put removed and deleted comments at the bottom, then do your other sorts
314 if (this.state.commentSort == CommentSortType.Top) {
317 +a.comment.removed - +b.comment.removed ||
318 +a.comment.deleted - +b.comment.deleted ||
319 b.comment.score - a.comment.score
321 } else if (this.state.commentSort == CommentSortType.New) {
324 +a.comment.removed - +b.comment.removed ||
325 +a.comment.deleted - +b.comment.deleted ||
326 b.comment.published.localeCompare(a.comment.published)
328 } else if (this.state.commentSort == CommentSortType.Old) {
331 +a.comment.removed - +b.comment.removed ||
332 +a.comment.deleted - +b.comment.deleted ||
333 a.comment.published.localeCompare(b.comment.published)
335 } else if (this.state.commentSort == CommentSortType.Hot) {
338 +a.comment.removed - +b.comment.removed ||
339 +a.comment.deleted - +b.comment.deleted ||
340 hotRank(b.comment) - hotRank(a.comment)
344 for (let node of tree) {
345 this.sortTree(node.children);
350 let nodes = this.buildCommentsTree();
355 locked={this.state.post.locked}
356 moderators={this.state.moderators}
357 admins={this.state.admins}
358 postCreatorId={this.state.post.creator_id}
364 parseMessage(msg: WebSocketJsonResponse) {
366 let res = wsJsonToRes(msg);
368 toast(i18n.t(msg.error), 'danger');
370 } else if (res.op == UserOperation.GetPost) {
371 let data = res.data as GetPostResponse;
372 this.state.post = data.post;
373 this.state.comments = data.comments;
374 this.state.community = data.community;
375 this.state.moderators = data.moderators;
376 this.state.admins = data.admins;
377 this.state.loading = false;
378 document.title = `${this.state.post.name} - ${WebSocketService.Instance.site.name}`;
381 if (this.state.post.url) {
382 let form: SearchForm = {
383 q: this.state.post.url,
384 type_: SearchType[SearchType.Url],
385 sort: SortType[SortType.TopAll],
389 WebSocketService.Instance.search(form);
392 this.setState(this.state);
393 } else if (res.op == UserOperation.CreateComment) {
394 let data = res.data as CommentResponse;
395 this.state.comments.unshift(data.comment);
396 this.setState(this.state);
397 } else if (res.op == UserOperation.EditComment) {
398 let data = res.data as CommentResponse;
399 let found = this.state.comments.find(c => c.id == data.comment.id);
400 found.content = data.comment.content;
401 found.updated = data.comment.updated;
402 found.removed = data.comment.removed;
403 found.deleted = data.comment.deleted;
404 found.upvotes = data.comment.upvotes;
405 found.downvotes = data.comment.downvotes;
406 found.score = data.comment.score;
407 found.read = data.comment.read;
409 this.setState(this.state);
410 } else if (res.op == UserOperation.SaveComment) {
411 let data = res.data as CommentResponse;
412 let found = this.state.comments.find(c => c.id == data.comment.id);
413 found.saved = data.comment.saved;
414 this.setState(this.state);
415 } else if (res.op == UserOperation.CreateCommentLike) {
416 let data = res.data as CommentResponse;
417 let found: Comment = this.state.comments.find(
418 c => c.id === data.comment.id
420 found.score = data.comment.score;
421 found.upvotes = data.comment.upvotes;
422 found.downvotes = data.comment.downvotes;
423 if (data.comment.my_vote !== null) {
424 found.my_vote = data.comment.my_vote;
425 found.upvoteLoading = false;
426 found.downvoteLoading = false;
428 this.setState(this.state);
429 } else if (res.op == UserOperation.CreatePostLike) {
430 let data = res.data as PostResponse;
431 this.state.post.my_vote = data.post.my_vote;
432 this.state.post.score = data.post.score;
433 this.state.post.upvotes = data.post.upvotes;
434 this.state.post.downvotes = data.post.downvotes;
435 this.setState(this.state);
436 } else if (res.op == UserOperation.EditPost) {
437 let data = res.data as PostResponse;
438 this.state.post = data.post;
439 this.setState(this.state);
440 } else if (res.op == UserOperation.SavePost) {
441 let data = res.data as PostResponse;
442 this.state.post = data.post;
443 this.setState(this.state);
444 } else if (res.op == UserOperation.EditCommunity) {
445 let data = res.data as CommunityResponse;
446 this.state.community = data.community;
447 this.state.post.community_id = data.community.id;
448 this.state.post.community_name = data.community.name;
449 this.setState(this.state);
450 } else if (res.op == UserOperation.FollowCommunity) {
451 let data = res.data as CommunityResponse;
452 this.state.community.subscribed = data.community.subscribed;
453 this.state.community.number_of_subscribers =
454 data.community.number_of_subscribers;
455 this.setState(this.state);
456 } else if (res.op == UserOperation.BanFromCommunity) {
457 let data = res.data as BanFromCommunityResponse;
459 .filter(c => c.creator_id == data.user.id)
460 .forEach(c => (c.banned_from_community = data.banned));
461 if (this.state.post.creator_id == data.user.id) {
462 this.state.post.banned_from_community = data.banned;
464 this.setState(this.state);
465 } else if (res.op == UserOperation.AddModToCommunity) {
466 let data = res.data as AddModToCommunityResponse;
467 this.state.moderators = data.moderators;
468 this.setState(this.state);
469 } else if (res.op == UserOperation.BanUser) {
470 let data = res.data as BanUserResponse;
472 .filter(c => c.creator_id == data.user.id)
473 .forEach(c => (c.banned = data.banned));
474 if (this.state.post.creator_id == data.user.id) {
475 this.state.post.banned = data.banned;
477 this.setState(this.state);
478 } else if (res.op == UserOperation.AddAdmin) {
479 let data = res.data as AddAdminResponse;
480 this.state.admins = data.admins;
481 this.setState(this.state);
482 } else if (res.op == UserOperation.Search) {
483 let data = res.data as SearchResponse;
484 this.state.crossPosts = data.posts.filter(
485 p => p.id != this.state.post.id
487 this.setState(this.state);
488 } else if (res.op == UserOperation.TransferSite) {
489 let data = res.data as GetSiteResponse;
491 this.state.admins = data.admins;
492 this.setState(this.state);
493 } else if (res.op == UserOperation.TransferCommunity) {
494 let data = res.data as GetCommunityResponse;
495 this.state.community = data.community;
496 this.state.moderators = data.moderators;
497 this.state.admins = data.admins;
498 this.setState(this.state);