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)}
242 <div class="d-none d-md-block new-comments mb-3 card border-secondary">
243 <div class="card-body small">
244 <h6>{i18n.t('recent_comments')}</h6>
245 {this.state.comments.map(comment => (
247 nodes={[{ comment: comment }]}
249 locked={this.state.post.locked}
250 moderators={this.state.moderators}
251 admins={this.state.admins}
252 postCreatorId={this.state.post.creator_id}
264 community={this.state.community}
265 moderators={this.state.moderators}
266 admins={this.state.admins}
272 handleCommentSortChange(i: Post, event: any) {
273 i.state.commentSort = Number(event.target.value);
277 private buildCommentsTree(): Array<CommentNodeI> {
278 let map = new Map<number, CommentNodeI>();
279 for (let comment of this.state.comments) {
280 let node: CommentNodeI = {
284 map.set(comment.id, { ...node });
286 let tree: Array<CommentNodeI> = [];
287 for (let comment of this.state.comments) {
288 if (comment.parent_id) {
289 map.get(comment.parent_id).children.push(map.get(comment.id));
291 tree.push(map.get(comment.id));
300 sortTree(tree: Array<CommentNodeI>) {
301 // First, put removed and deleted comments at the bottom, then do your other sorts
302 if (this.state.commentSort == CommentSortType.Top) {
305 +a.comment.removed - +b.comment.removed ||
306 +a.comment.deleted - +b.comment.deleted ||
307 b.comment.score - a.comment.score
309 } else if (this.state.commentSort == CommentSortType.New) {
312 +a.comment.removed - +b.comment.removed ||
313 +a.comment.deleted - +b.comment.deleted ||
314 b.comment.published.localeCompare(a.comment.published)
316 } else if (this.state.commentSort == CommentSortType.Hot) {
319 +a.comment.removed - +b.comment.removed ||
320 +a.comment.deleted - +b.comment.deleted ||
321 hotRank(b.comment) - hotRank(a.comment)
325 for (let node of tree) {
326 this.sortTree(node.children);
331 let nodes = this.buildCommentsTree();
336 locked={this.state.post.locked}
337 moderators={this.state.moderators}
338 admins={this.state.admins}
339 postCreatorId={this.state.post.creator_id}
345 parseMessage(msg: WebSocketJsonResponse) {
347 let res = wsJsonToRes(msg);
349 toast(i18n.t(msg.error), 'danger');
351 } else if (res.op == UserOperation.GetPost) {
352 let data = res.data as GetPostResponse;
353 this.state.post = data.post;
354 this.state.comments = data.comments;
355 this.state.community = data.community;
356 this.state.moderators = data.moderators;
357 this.state.admins = data.admins;
358 this.state.loading = false;
359 document.title = `${this.state.post.name} - ${WebSocketService.Instance.site.name}`;
362 if (this.state.post.url) {
363 let form: SearchForm = {
364 q: this.state.post.url,
365 type_: SearchType[SearchType.Url],
366 sort: SortType[SortType.TopAll],
370 WebSocketService.Instance.search(form);
373 this.setState(this.state);
374 } else if (res.op == UserOperation.CreateComment) {
375 let data = res.data as CommentResponse;
376 this.state.comments.unshift(data.comment);
377 this.setState(this.state);
378 } else if (res.op == UserOperation.EditComment) {
379 let data = res.data as CommentResponse;
380 let found = this.state.comments.find(c => c.id == data.comment.id);
381 found.content = data.comment.content;
382 found.updated = data.comment.updated;
383 found.removed = data.comment.removed;
384 found.deleted = data.comment.deleted;
385 found.upvotes = data.comment.upvotes;
386 found.downvotes = data.comment.downvotes;
387 found.score = data.comment.score;
388 found.read = data.comment.read;
390 this.setState(this.state);
391 } else if (res.op == UserOperation.SaveComment) {
392 let data = res.data as CommentResponse;
393 let found = this.state.comments.find(c => c.id == data.comment.id);
394 found.saved = data.comment.saved;
395 this.setState(this.state);
396 } else if (res.op == UserOperation.CreateCommentLike) {
397 let data = res.data as CommentResponse;
398 let found: Comment = this.state.comments.find(
399 c => c.id === data.comment.id
401 found.score = data.comment.score;
402 found.upvotes = data.comment.upvotes;
403 found.downvotes = data.comment.downvotes;
404 if (data.comment.my_vote !== null) {
405 found.my_vote = data.comment.my_vote;
406 found.upvoteLoading = false;
407 found.downvoteLoading = false;
409 this.setState(this.state);
410 } else if (res.op == UserOperation.CreatePostLike) {
411 let data = res.data as PostResponse;
412 this.state.post.my_vote = data.post.my_vote;
413 this.state.post.score = data.post.score;
414 this.state.post.upvotes = data.post.upvotes;
415 this.state.post.downvotes = data.post.downvotes;
416 this.setState(this.state);
417 } else if (res.op == UserOperation.EditPost) {
418 let data = res.data as PostResponse;
419 this.state.post = data.post;
420 this.setState(this.state);
421 } else if (res.op == UserOperation.SavePost) {
422 let data = res.data as PostResponse;
423 this.state.post = data.post;
424 this.setState(this.state);
425 } else if (res.op == UserOperation.EditCommunity) {
426 let data = res.data as CommunityResponse;
427 this.state.community = data.community;
428 this.state.post.community_id = data.community.id;
429 this.state.post.community_name = data.community.name;
430 this.setState(this.state);
431 } else if (res.op == UserOperation.FollowCommunity) {
432 let data = res.data as CommunityResponse;
433 this.state.community.subscribed = data.community.subscribed;
434 this.state.community.number_of_subscribers =
435 data.community.number_of_subscribers;
436 this.setState(this.state);
437 } else if (res.op == UserOperation.BanFromCommunity) {
438 let data = res.data as BanFromCommunityResponse;
440 .filter(c => c.creator_id == data.user.id)
441 .forEach(c => (c.banned_from_community = data.banned));
442 if (this.state.post.creator_id == data.user.id) {
443 this.state.post.banned_from_community = data.banned;
445 this.setState(this.state);
446 } else if (res.op == UserOperation.AddModToCommunity) {
447 let data = res.data as AddModToCommunityResponse;
448 this.state.moderators = data.moderators;
449 this.setState(this.state);
450 } else if (res.op == UserOperation.BanUser) {
451 let data = res.data as BanUserResponse;
453 .filter(c => c.creator_id == data.user.id)
454 .forEach(c => (c.banned = data.banned));
455 if (this.state.post.creator_id == data.user.id) {
456 this.state.post.banned = data.banned;
458 this.setState(this.state);
459 } else if (res.op == UserOperation.AddAdmin) {
460 let data = res.data as AddAdminResponse;
461 this.state.admins = data.admins;
462 this.setState(this.state);
463 } else if (res.op == UserOperation.Search) {
464 let data = res.data as SearchResponse;
465 this.state.crossPosts = data.posts.filter(
466 p => p.id != this.state.post.id
468 this.setState(this.state);
469 } else if (res.op == UserOperation.TransferSite) {
470 let data = res.data as GetSiteResponse;
472 this.state.admins = data.admins;
473 this.setState(this.state);
474 } else if (res.op == UserOperation.TransferCommunity) {
475 let data = res.data as GetCommunityResponse;
476 this.state.community = data.community;
477 this.state.moderators = data.moderators;
478 this.state.admins = data.admins;
479 this.setState(this.state);