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';
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;
54 moderators: Array<CommunityUser>;
55 admins: Array<UserView>;
58 scrolled_comment_id?: number;
60 crossPosts: Array<PostI>;
63 export class Post extends Component<any, PostState> {
64 private subscription: Subscription;
65 private emptyState: PostState = {
68 commentSort: CommentSortType.Hot,
78 constructor(props: any, context: any) {
79 super(props, context);
81 this.state = this.emptyState;
83 let postId = Number(this.props.match.params.id);
84 if (this.props.match.params.comment_id) {
85 this.state.scrolled_comment_id = this.props.match.params.comment_id;
88 this.subscription = WebSocketService.Instance.subject
89 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
91 msg => this.parseMessage(msg),
92 err => console.error(err),
93 () => console.log('complete')
96 let form: GetPostForm = {
99 WebSocketService.Instance.getPost(form);
102 componentWillUnmount() {
103 this.subscription.unsubscribe();
106 componentDidMount() {
107 autosize(document.querySelectorAll('textarea'));
110 componentDidUpdate(_lastProps: any, lastState: PostState, _snapshot: any) {
112 this.state.scrolled_comment_id &&
113 !this.state.scrolled &&
114 lastState.comments.length > 0
116 var elmnt = document.getElementById(
117 `comment-${this.state.scrolled_comment_id}`
119 elmnt.scrollIntoView();
120 elmnt.classList.add('mark');
121 this.state.scrolled = true;
122 this.markScrolledAsRead(this.state.scrolled_comment_id);
125 // Necessary if you are on a post and you click another post (same route)
126 if (_lastProps.location.pathname !== _lastProps.history.location.pathname) {
127 // Couldnt get a refresh working. This does for now.
130 // let currentId = this.props.match.params.id;
131 // WebSocketService.Instance.getPost(currentId);
132 // this.context.router.history.push('/sponsors');
133 // this.context.refresh();
134 // this.context.router.history.push(_lastProps.location.pathname);
138 markScrolledAsRead(commentId: number) {
139 let found = this.state.comments.find(c => c.id == commentId);
140 let parent = this.state.comments.find(c => found.parent_id == c.id);
141 let parent_user_id = parent
143 : this.state.post.creator_id;
146 UserService.Instance.user &&
147 UserService.Instance.user.id == parent_user_id
149 let form: CommentFormI = {
150 content: found.content,
152 creator_id: found.creator_id,
153 post_id: found.post_id,
154 parent_id: found.parent_id,
158 WebSocketService.Instance.editComment(form);
159 UserService.Instance.user.unreadCount--;
160 UserService.Instance.sub.next({
161 user: UserService.Instance.user,
168 <div class="container">
169 {this.state.loading ? (
171 <svg class="icon icon-spinner spin">
172 <use xlinkHref="#icon-spinner"></use>
177 <div class="col-12 col-md-8 mb-3">
179 post={this.state.post}
182 moderators={this.state.moderators}
183 admins={this.state.admins}
185 <div className="mb-2" />
187 postId={this.state.post.id}
188 disabled={this.state.post.locked}
190 {this.state.comments.length > 0 && this.sortRadios()}
191 {this.commentsTree()}
193 <div class="col-12 col-sm-12 col-md-4">
194 {this.state.comments.length > 0 && this.newComments()}
205 <div class="btn-group btn-group-toggle mb-2">
207 className={`btn btn-sm btn-secondary pointer ${
208 this.state.commentSort === CommentSortType.Hot && 'active'
214 value={CommentSortType.Hot}
215 checked={this.state.commentSort === CommentSortType.Hot}
216 onChange={linkEvent(this, this.handleCommentSortChange)}
220 className={`btn btn-sm btn-secondary pointer ${
221 this.state.commentSort === CommentSortType.Top && 'active'
227 value={CommentSortType.Top}
228 checked={this.state.commentSort === CommentSortType.Top}
229 onChange={linkEvent(this, this.handleCommentSortChange)}
233 className={`btn btn-sm btn-secondary pointer ${
234 this.state.commentSort === CommentSortType.New && 'active'
240 value={CommentSortType.New}
241 checked={this.state.commentSort === CommentSortType.New}
242 onChange={linkEvent(this, this.handleCommentSortChange)}
246 className={`btn btn-sm btn-secondary pointer ${
247 this.state.commentSort === CommentSortType.Old && 'active'
253 value={CommentSortType.Old}
254 checked={this.state.commentSort === CommentSortType.Old}
255 onChange={linkEvent(this, this.handleCommentSortChange)}
264 <div class="d-none d-md-block new-comments mb-3 card border-secondary">
265 <div class="card-body small">
266 <h6>{i18n.t('recent_comments')}</h6>
268 nodes={commentsToFlatNodes(this.state.comments)}
270 locked={this.state.post.locked}
271 moderators={this.state.moderators}
272 admins={this.state.admins}
273 postCreatorId={this.state.post.creator_id}
285 community={this.state.community}
286 moderators={this.state.moderators}
287 admins={this.state.admins}
288 online={this.state.online}
294 handleCommentSortChange(i: Post, event: any) {
295 i.state.commentSort = Number(event.target.value);
299 buildCommentsTree(): Array<CommentNodeI> {
300 let map = new Map<number, CommentNodeI>();
301 for (let comment of this.state.comments) {
302 let node: CommentNodeI = {
306 map.set(comment.id, { ...node });
308 let tree: Array<CommentNodeI> = [];
309 for (let comment of this.state.comments) {
310 let child = map.get(comment.id);
311 if (comment.parent_id) {
312 let parent_ = map.get(comment.parent_id);
313 parent_.children.push(child);
318 this.setDepth(child);
324 setDepth(node: CommentNodeI, i: number = 0): void {
325 for (let child of node.children) {
326 child.comment.depth = i;
327 this.setDepth(child, i + 1);
332 let nodes = this.buildCommentsTree();
337 locked={this.state.post.locked}
338 moderators={this.state.moderators}
339 admins={this.state.admins}
340 postCreatorId={this.state.post.creator_id}
341 sort={this.state.commentSort}
347 parseMessage(msg: WebSocketJsonResponse) {
349 let res = wsJsonToRes(msg);
351 toast(i18n.t(msg.error), 'danger');
353 } else if (msg.reconnect) {
354 WebSocketService.Instance.getPost({
355 id: Number(this.props.match.params.id),
357 } else if (res.op == UserOperation.GetPost) {
358 let data = res.data as GetPostResponse;
359 this.state.post = data.post;
360 this.state.comments = data.comments;
361 this.state.community = data.community;
362 this.state.moderators = data.moderators;
363 this.state.admins = data.admins;
364 this.state.online = data.online;
365 this.state.loading = false;
366 document.title = `${this.state.post.name} - ${WebSocketService.Instance.site.name}`;
369 if (this.state.post.url) {
370 let form: SearchForm = {
371 q: this.state.post.url,
372 type_: SearchType[SearchType.Url],
373 sort: SortType[SortType.TopAll],
377 WebSocketService.Instance.search(form);
380 this.setState(this.state);
382 } else if (res.op == UserOperation.CreateComment) {
383 let data = res.data as CommentResponse;
385 // Necessary since it might be a user reply
386 if (data.recipient_ids.length == 0) {
387 this.state.comments.unshift(data.comment);
388 this.setState(this.state);
390 } else if (res.op == UserOperation.EditComment) {
391 let data = res.data as CommentResponse;
392 editCommentRes(data, this.state.comments);
393 this.setState(this.state);
394 } else if (res.op == UserOperation.SaveComment) {
395 let data = res.data as CommentResponse;
396 saveCommentRes(data, this.state.comments);
397 this.setState(this.state);
399 } else if (res.op == UserOperation.CreateCommentLike) {
400 let data = res.data as CommentResponse;
401 createCommentLikeRes(data, this.state.comments);
402 this.setState(this.state);
403 } else if (res.op == UserOperation.CreatePostLike) {
404 let data = res.data as PostResponse;
405 createPostLikeRes(data, this.state.post);
406 this.setState(this.state);
407 } else if (res.op == UserOperation.EditPost) {
408 let data = res.data as PostResponse;
409 this.state.post = data.post;
410 this.setState(this.state);
412 } else if (res.op == UserOperation.SavePost) {
413 let data = res.data as PostResponse;
414 this.state.post = data.post;
415 this.setState(this.state);
417 } else if (res.op == UserOperation.EditCommunity) {
418 let data = res.data as CommunityResponse;
419 this.state.community = data.community;
420 this.state.post.community_id = data.community.id;
421 this.state.post.community_name = data.community.name;
422 this.setState(this.state);
423 } else if (res.op == UserOperation.FollowCommunity) {
424 let data = res.data as CommunityResponse;
425 this.state.community.subscribed = data.community.subscribed;
426 this.state.community.number_of_subscribers =
427 data.community.number_of_subscribers;
428 this.setState(this.state);
429 } else if (res.op == UserOperation.BanFromCommunity) {
430 let data = res.data as BanFromCommunityResponse;
432 .filter(c => c.creator_id == data.user.id)
433 .forEach(c => (c.banned_from_community = data.banned));
434 if (this.state.post.creator_id == data.user.id) {
435 this.state.post.banned_from_community = data.banned;
437 this.setState(this.state);
438 } else if (res.op == UserOperation.AddModToCommunity) {
439 let data = res.data as AddModToCommunityResponse;
440 this.state.moderators = data.moderators;
441 this.setState(this.state);
442 } else if (res.op == UserOperation.BanUser) {
443 let data = res.data as BanUserResponse;
445 .filter(c => c.creator_id == data.user.id)
446 .forEach(c => (c.banned = data.banned));
447 if (this.state.post.creator_id == data.user.id) {
448 this.state.post.banned = data.banned;
450 this.setState(this.state);
451 } else if (res.op == UserOperation.AddAdmin) {
452 let data = res.data as AddAdminResponse;
453 this.state.admins = data.admins;
454 this.setState(this.state);
455 } else if (res.op == UserOperation.Search) {
456 let data = res.data as SearchResponse;
457 this.state.crossPosts = data.posts.filter(
458 p => p.id != Number(this.props.match.params.id)
460 if (this.state.crossPosts.length) {
461 this.state.post.duplicates = this.state.crossPosts;
463 this.setState(this.state);
464 } else if (res.op == UserOperation.TransferSite) {
465 let data = res.data as GetSiteResponse;
466 this.state.admins = data.admins;
467 this.setState(this.state);
468 } else if (res.op == UserOperation.TransferCommunity) {
469 let data = res.data as GetCommunityResponse;
470 this.state.community = data.community;
471 this.state.moderators = data.moderators;
472 this.state.admins = data.admins;
473 this.setState(this.state);