1 import { Component, linkEvent } from 'inferno';
2 import { Subscription } from 'rxjs';
3 import { retryWhen, delay, take } from 'rxjs/operators';
11 CommentForm as CommentFormI,
14 CreatePostLikeResponse,
17 CommentNode as CommentNodeI,
18 BanFromCommunityResponse,
20 AddModToCommunityResponse,
30 WebSocketJsonResponse,
31 } from '../interfaces';
32 import { WebSocketService, UserService } from '../services';
33 import { wsJsonToRes, hotRank, toast } from '../utils';
34 import { PostListing } from './post-listing';
35 import { PostListings } from './post-listings';
36 import { Sidebar } from './sidebar';
37 import { CommentForm } from './comment-form';
38 import { CommentNodes } from './comment-nodes';
39 import autosize from 'autosize';
40 import { i18n } from '../i18next';
41 import { T } from 'inferno-i18next';
45 comments: Array<Comment>;
46 commentSort: CommentSortType;
48 moderators: Array<CommunityUser>;
49 admins: Array<UserView>;
51 scrolled_comment_id?: number;
53 crossPosts: Array<PostI>;
56 export class Post extends Component<any, PostState> {
57 private subscription: Subscription;
58 private emptyState: PostState = {
61 commentSort: CommentSortType.Hot,
70 constructor(props: any, context: any) {
71 super(props, context);
73 this.state = this.emptyState;
75 let postId = Number(this.props.match.params.id);
76 if (this.props.match.params.comment_id) {
77 this.state.scrolled_comment_id = this.props.match.params.comment_id;
80 this.subscription = WebSocketService.Instance.subject
81 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
83 msg => this.parseMessage(msg),
84 err => console.error(err),
85 () => console.log('complete')
88 let form: GetPostForm = {
91 WebSocketService.Instance.getPost(form);
94 componentWillUnmount() {
95 this.subscription.unsubscribe();
99 autosize(document.querySelectorAll('textarea'));
102 componentDidUpdate(_lastProps: any, lastState: PostState, _snapshot: any) {
104 this.state.scrolled_comment_id &&
105 !this.state.scrolled &&
106 lastState.comments.length > 0
108 var elmnt = document.getElementById(
109 `comment-${this.state.scrolled_comment_id}`
111 elmnt.scrollIntoView();
112 elmnt.classList.add('mark');
113 this.state.scrolled = true;
114 this.markScrolledAsRead(this.state.scrolled_comment_id);
117 // Necessary if you are on a post and you click another post (same route)
118 if (_lastProps.location.pathname !== _lastProps.history.location.pathname) {
119 // Couldnt get a refresh working. This does for now.
122 // let currentId = this.props.match.params.id;
123 // WebSocketService.Instance.getPost(currentId);
124 // this.context.router.history.push('/sponsors');
125 // this.context.refresh();
126 // this.context.router.history.push(_lastProps.location.pathname);
130 markScrolledAsRead(commentId: number) {
131 let found = this.state.comments.find(c => c.id == commentId);
132 let parent = this.state.comments.find(c => found.parent_id == c.id);
133 let parent_user_id = parent
135 : this.state.post.creator_id;
138 UserService.Instance.user &&
139 UserService.Instance.user.id == parent_user_id
141 let form: CommentFormI = {
142 content: found.content,
144 creator_id: found.creator_id,
145 post_id: found.post_id,
146 parent_id: found.parent_id,
150 WebSocketService.Instance.editComment(form);
156 <div class="container">
157 {this.state.loading ? (
158 <p class="text-center">
159 <svg class="icon icon-spinner spin">
160 <use xlinkHref="#icon-spinner"></use>
165 <div class="col-12 col-md-8 mb-3">
167 post={this.state.post}
170 moderators={this.state.moderators}
171 admins={this.state.admins}
173 {this.state.crossPosts.length > 0 && (
175 <div class="my-1 text-muted small font-weight-bold">
176 { i18n.t('cross_posts') }
178 <PostListings showCommunity posts={this.state.crossPosts} />
181 <div className="mb-2" />
183 postId={this.state.post.id}
184 disabled={this.state.post.locked}
186 {this.state.comments.length > 0 && this.sortRadios()}
187 {this.commentsTree()}
189 <div class="col-12 col-sm-12 col-md-4">
190 {this.state.comments.length > 0 && this.newComments()}
201 <div class="btn-group btn-group-toggle mb-3">
203 className={`btn btn-sm btn-secondary pointer ${this.state
204 .commentSort === CommentSortType.Hot && 'active'}`}
209 value={CommentSortType.Hot}
210 checked={this.state.commentSort === CommentSortType.Hot}
211 onChange={linkEvent(this, this.handleCommentSortChange)}
215 className={`btn btn-sm btn-secondary pointer ${this.state
216 .commentSort === CommentSortType.Top && 'active'}`}
221 value={CommentSortType.Top}
222 checked={this.state.commentSort === CommentSortType.Top}
223 onChange={linkEvent(this, this.handleCommentSortChange)}
227 className={`btn btn-sm btn-secondary pointer ${this.state
228 .commentSort === CommentSortType.New && 'active'}`}
233 value={CommentSortType.New}
234 checked={this.state.commentSort === CommentSortType.New}
235 onChange={linkEvent(this, this.handleCommentSortChange)}
244 <div class="d-none d-md-block new-comments mb-3 card border-secondary">
245 <div class="card-body small">
247 { i18n.t('recent_comments') }
249 {this.state.comments.map(comment => (
251 nodes={[{ comment: comment }]}
253 locked={this.state.post.locked}
254 moderators={this.state.moderators}
255 admins={this.state.admins}
256 postCreatorId={this.state.post.creator_id}
268 community={this.state.community}
269 moderators={this.state.moderators}
270 admins={this.state.admins}
276 handleCommentSortChange(i: Post, event: any) {
277 i.state.commentSort = Number(event.target.value);
281 private buildCommentsTree(): Array<CommentNodeI> {
282 let map = new Map<number, CommentNodeI>();
283 for (let comment of this.state.comments) {
284 let node: CommentNodeI = {
288 map.set(comment.id, { ...node });
290 let tree: Array<CommentNodeI> = [];
291 for (let comment of this.state.comments) {
292 if (comment.parent_id) {
293 map.get(comment.parent_id).children.push(map.get(comment.id));
295 tree.push(map.get(comment.id));
304 sortTree(tree: Array<CommentNodeI>) {
305 // First, put removed and deleted comments at the bottom, then do your other sorts
306 if (this.state.commentSort == CommentSortType.Top) {
309 +a.comment.removed - +b.comment.removed ||
310 +a.comment.deleted - +b.comment.deleted ||
311 b.comment.score - a.comment.score
313 } else if (this.state.commentSort == CommentSortType.New) {
316 +a.comment.removed - +b.comment.removed ||
317 +a.comment.deleted - +b.comment.deleted ||
318 b.comment.published.localeCompare(a.comment.published)
320 } else if (this.state.commentSort == CommentSortType.Hot) {
323 +a.comment.removed - +b.comment.removed ||
324 +a.comment.deleted - +b.comment.deleted ||
325 hotRank(b.comment) - hotRank(a.comment)
329 for (let node of tree) {
330 this.sortTree(node.children);
335 let nodes = this.buildCommentsTree();
340 locked={this.state.post.locked}
341 moderators={this.state.moderators}
342 admins={this.state.admins}
343 postCreatorId={this.state.post.creator_id}
349 parseMessage(msg: WebSocketJsonResponse) {
351 let res = wsJsonToRes(msg);
353 toast(i18n.t(msg.error), 'danger');
355 } else if (res.op == UserOperation.GetPost) {
356 let data = res.data as GetPostResponse;
357 this.state.post = data.post;
358 this.state.comments = data.comments;
359 this.state.community = data.community;
360 this.state.moderators = data.moderators;
361 this.state.admins = data.admins;
362 this.state.loading = false;
363 document.title = `${this.state.post.name} - ${WebSocketService.Instance.site.name}`;
366 if (this.state.post.url) {
367 let form: SearchForm = {
368 q: this.state.post.url,
369 type_: SearchType[SearchType.Url],
370 sort: SortType[SortType.TopAll],
374 WebSocketService.Instance.search(form);
377 this.setState(this.state);
378 } else if (res.op == UserOperation.CreateComment) {
379 let data = res.data as CommentResponse;
380 this.state.comments.unshift(data.comment);
381 this.setState(this.state);
382 } else if (res.op == UserOperation.EditComment) {
383 let data = res.data as CommentResponse;
384 let found = this.state.comments.find(c => c.id == data.comment.id);
385 found.content = data.comment.content;
386 found.updated = data.comment.updated;
387 found.removed = data.comment.removed;
388 found.deleted = data.comment.deleted;
389 found.upvotes = data.comment.upvotes;
390 found.downvotes = data.comment.downvotes;
391 found.score = data.comment.score;
392 found.read = data.comment.read;
394 this.setState(this.state);
395 } else if (res.op == UserOperation.SaveComment) {
396 let data = res.data as CommentResponse;
397 let found = this.state.comments.find(c => c.id == data.comment.id);
398 found.saved = data.comment.saved;
399 this.setState(this.state);
400 } else if (res.op == UserOperation.CreateCommentLike) {
401 let data = res.data as CommentResponse;
402 let found: Comment = this.state.comments.find(
403 c => c.id === data.comment.id
405 found.score = data.comment.score;
406 found.upvotes = data.comment.upvotes;
407 found.downvotes = data.comment.downvotes;
408 if (data.comment.my_vote !== null) {
409 found.my_vote = data.comment.my_vote;
410 found.upvoteLoading = false;
411 found.downvoteLoading = false;
413 this.setState(this.state);
414 } else if (res.op == UserOperation.CreatePostLike) {
415 let data = res.data as CreatePostLikeResponse;
416 this.state.post.my_vote = data.post.my_vote;
417 this.state.post.score = data.post.score;
418 this.state.post.upvotes = data.post.upvotes;
419 this.state.post.downvotes = data.post.downvotes;
420 this.setState(this.state);
421 } else if (res.op == UserOperation.EditPost) {
422 let data = res.data as PostResponse;
423 this.state.post = data.post;
424 this.setState(this.state);
425 } else if (res.op == UserOperation.SavePost) {
426 let data = res.data as PostResponse;
427 this.state.post = data.post;
428 this.setState(this.state);
429 } else if (res.op == UserOperation.EditCommunity) {
430 let data = res.data as CommunityResponse;
431 this.state.community = data.community;
432 this.state.post.community_id = data.community.id;
433 this.state.post.community_name = data.community.name;
434 this.setState(this.state);
435 } else if (res.op == UserOperation.FollowCommunity) {
436 let data = res.data as CommunityResponse;
437 this.state.community.subscribed = data.community.subscribed;
438 this.state.community.number_of_subscribers =
439 data.community.number_of_subscribers;
440 this.setState(this.state);
441 } else if (res.op == UserOperation.BanFromCommunity) {
442 let data = res.data as BanFromCommunityResponse;
444 .filter(c => c.creator_id == data.user.id)
445 .forEach(c => (c.banned_from_community = data.banned));
446 if (this.state.post.creator_id == data.user.id) {
447 this.state.post.banned_from_community = data.banned;
449 this.setState(this.state);
450 } else if (res.op == UserOperation.AddModToCommunity) {
451 let data = res.data as AddModToCommunityResponse;
452 this.state.moderators = data.moderators;
453 this.setState(this.state);
454 } else if (res.op == UserOperation.BanUser) {
455 let data = res.data as BanUserResponse;
457 .filter(c => c.creator_id == data.user.id)
458 .forEach(c => (c.banned = data.banned));
459 if (this.state.post.creator_id == data.user.id) {
460 this.state.post.banned = data.banned;
462 this.setState(this.state);
463 } else if (res.op == UserOperation.AddAdmin) {
464 let data = res.data as AddAdminResponse;
465 this.state.admins = data.admins;
466 this.setState(this.state);
467 } else if (res.op == UserOperation.Search) {
468 let data = res.data as SearchResponse;
469 this.state.crossPosts = data.posts.filter(
470 p => p.id != this.state.post.id
472 this.setState(this.state);
473 } else if (res.op == UserOperation.TransferSite) {
474 let data = res.data as GetSiteResponse;
476 this.state.admins = data.admins;
477 this.setState(this.state);
478 } else if (res.op == UserOperation.TransferCommunity) {
479 let data = res.data as GetCommunityResponse;
480 this.state.community = data.community;
481 this.state.moderators = data.moderators;
482 this.state.admins = data.admins;
483 this.setState(this.state);