1 import { Component, linkEvent } from 'inferno';
2 import { Subscription } from 'rxjs';
3 import { retryWhen, delay, take } from 'rxjs/operators';
11 CommentForm as CommentFormI,
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: CommentFormI = {
171 content: found.content,
173 creator_id: found.creator_id,
174 post_id: found.post_id,
175 parent_id: found.parent_id,
179 WebSocketService.Instance.editComment(form);
180 UserService.Instance.user.unreadCount--;
181 UserService.Instance.sub.next({
182 user: UserService.Instance.user,
189 <div class="container">
190 {this.state.loading ? (
192 <svg class="icon icon-spinner spin">
193 <use xlinkHref="#icon-spinner"></use>
198 <div class="col-12 col-md-8 mb-3">
200 post={this.state.post}
203 moderators={this.state.moderators}
204 admins={this.state.siteRes.admins}
205 enableDownvotes={this.state.siteRes.site.enable_downvotes}
206 enableNsfw={this.state.siteRes.site.enable_nsfw}
208 <div className="mb-2" />
210 postId={this.state.post.id}
211 disabled={this.state.post.locked}
213 {this.state.comments.length > 0 && this.sortRadios()}
214 {this.state.commentViewType == CommentViewType.Tree &&
216 {this.state.commentViewType == CommentViewType.Chat &&
219 <div class="col-12 col-sm-12 col-md-4">{this.sidebar()}</div>
229 <div class="btn-group btn-group-toggle mb-2">
231 className={`btn btn-sm btn-secondary pointer ${
232 this.state.commentSort === CommentSortType.Hot && 'active'
238 value={CommentSortType.Hot}
239 checked={this.state.commentSort === CommentSortType.Hot}
240 onChange={linkEvent(this, this.handleCommentSortChange)}
244 className={`btn btn-sm btn-secondary pointer ${
245 this.state.commentSort === CommentSortType.Top && 'active'
251 value={CommentSortType.Top}
252 checked={this.state.commentSort === CommentSortType.Top}
253 onChange={linkEvent(this, this.handleCommentSortChange)}
257 className={`btn btn-sm btn-secondary pointer ${
258 this.state.commentSort === CommentSortType.New && 'active'
264 value={CommentSortType.New}
265 checked={this.state.commentSort === CommentSortType.New}
266 onChange={linkEvent(this, this.handleCommentSortChange)}
270 className={`btn btn-sm btn-secondary pointer ${
271 this.state.commentSort === CommentSortType.Old && 'active'
277 value={CommentSortType.Old}
278 checked={this.state.commentSort === CommentSortType.Old}
279 onChange={linkEvent(this, this.handleCommentSortChange)}
283 <div class="btn-group btn-group-toggle ml-3 mb-2">
285 className={`btn btn-sm btn-secondary pointer ${
286 this.state.commentViewType === CommentViewType.Tree && 'active'
292 value={CommentViewType.Tree}
293 checked={this.state.commentViewType === CommentViewType.Tree}
294 onChange={linkEvent(this, this.handleCommentViewTypeChange)}
298 className={`btn btn-sm btn-secondary pointer ${
299 this.state.commentViewType === CommentViewType.Chat && 'active'
305 value={CommentViewType.Chat}
306 checked={this.state.commentViewType === CommentViewType.Chat}
307 onChange={linkEvent(this, this.handleCommentViewTypeChange)}
319 nodes={commentsToFlatNodes(this.state.comments)}
321 locked={this.state.post.locked}
322 moderators={this.state.moderators}
323 admins={this.state.siteRes.admins}
324 postCreatorId={this.state.post.creator_id}
326 enableDownvotes={this.state.siteRes.site.enable_downvotes}
327 sort={this.state.commentSort}
337 community={this.state.community}
338 moderators={this.state.moderators}
339 admins={this.state.siteRes.admins}
340 online={this.state.online}
341 enableNsfw={this.state.siteRes.site.enable_nsfw}
347 handleCommentSortChange(i: Post, event: any) {
348 i.state.commentSort = Number(event.target.value);
352 handleCommentViewTypeChange(i: Post, event: any) {
353 i.state.commentViewType = Number(event.target.value);
357 buildCommentsTree(): Array<CommentNodeI> {
358 let map = new Map<number, CommentNodeI>();
359 for (let comment of this.state.comments) {
360 let node: CommentNodeI = {
364 map.set(comment.id, { ...node });
366 let tree: Array<CommentNodeI> = [];
367 for (let comment of this.state.comments) {
368 let child = map.get(comment.id);
369 if (comment.parent_id) {
370 let parent_ = map.get(comment.parent_id);
371 parent_.children.push(child);
376 this.setDepth(child);
382 setDepth(node: CommentNodeI, i: number = 0): void {
383 for (let child of node.children) {
384 child.comment.depth = i;
385 this.setDepth(child, i + 1);
390 let nodes = this.buildCommentsTree();
395 locked={this.state.post.locked}
396 moderators={this.state.moderators}
397 admins={this.state.siteRes.admins}
398 postCreatorId={this.state.post.creator_id}
399 sort={this.state.commentSort}
400 enableDownvotes={this.state.siteRes.site.enable_downvotes}
406 parseMessage(msg: WebSocketJsonResponse) {
408 let res = wsJsonToRes(msg);
410 toast(i18n.t(msg.error), 'danger');
412 } else if (msg.reconnect) {
413 WebSocketService.Instance.getPost({
414 id: Number(this.props.match.params.id),
416 } else if (res.op == UserOperation.GetPost) {
417 let data = res.data as GetPostResponse;
418 this.state.post = data.post;
419 this.state.comments = data.comments;
420 this.state.community = data.community;
421 this.state.moderators = data.moderators;
422 this.state.siteRes.admins = data.admins;
423 this.state.online = data.online;
424 this.state.loading = false;
425 document.title = `${this.state.post.name} - ${this.state.siteRes.site.name}`;
428 if (this.state.post.url) {
429 let form: SearchForm = {
430 q: this.state.post.url,
431 type_: SearchType[SearchType.Url],
432 sort: SortType[SortType.TopAll],
436 WebSocketService.Instance.search(form);
439 this.setState(this.state);
441 } else if (res.op == UserOperation.CreateComment) {
442 let data = res.data as CommentResponse;
444 // Necessary since it might be a user reply
445 if (data.recipient_ids.length == 0) {
446 this.state.comments.unshift(data.comment);
447 this.setState(this.state);
449 } else if (res.op == UserOperation.EditComment) {
450 let data = res.data as CommentResponse;
451 editCommentRes(data, this.state.comments);
452 this.setState(this.state);
453 } else if (res.op == UserOperation.SaveComment) {
454 let data = res.data as CommentResponse;
455 saveCommentRes(data, this.state.comments);
456 this.setState(this.state);
458 } else if (res.op == UserOperation.CreateCommentLike) {
459 let data = res.data as CommentResponse;
460 createCommentLikeRes(data, this.state.comments);
461 this.setState(this.state);
462 } else if (res.op == UserOperation.CreatePostLike) {
463 let data = res.data as PostResponse;
464 createPostLikeRes(data, this.state.post);
465 this.setState(this.state);
466 } else if (res.op == UserOperation.EditPost) {
467 let data = res.data as PostResponse;
468 this.state.post = data.post;
469 this.setState(this.state);
471 } else if (res.op == UserOperation.SavePost) {
472 let data = res.data as PostResponse;
473 this.state.post = data.post;
474 this.setState(this.state);
476 } else if (res.op == UserOperation.EditCommunity) {
477 let data = res.data as CommunityResponse;
478 this.state.community = data.community;
479 this.state.post.community_id = data.community.id;
480 this.state.post.community_name = data.community.name;
481 this.setState(this.state);
482 } else if (res.op == UserOperation.FollowCommunity) {
483 let data = res.data as CommunityResponse;
484 this.state.community.subscribed = data.community.subscribed;
485 this.state.community.number_of_subscribers =
486 data.community.number_of_subscribers;
487 this.setState(this.state);
488 } else if (res.op == UserOperation.BanFromCommunity) {
489 let data = res.data as BanFromCommunityResponse;
491 .filter(c => c.creator_id == data.user.id)
492 .forEach(c => (c.banned_from_community = data.banned));
493 if (this.state.post.creator_id == data.user.id) {
494 this.state.post.banned_from_community = data.banned;
496 this.setState(this.state);
497 } else if (res.op == UserOperation.AddModToCommunity) {
498 let data = res.data as AddModToCommunityResponse;
499 this.state.moderators = data.moderators;
500 this.setState(this.state);
501 } else if (res.op == UserOperation.BanUser) {
502 let data = res.data as BanUserResponse;
504 .filter(c => c.creator_id == data.user.id)
505 .forEach(c => (c.banned = data.banned));
506 if (this.state.post.creator_id == data.user.id) {
507 this.state.post.banned = data.banned;
509 this.setState(this.state);
510 } else if (res.op == UserOperation.AddAdmin) {
511 let data = res.data as AddAdminResponse;
512 this.state.siteRes.admins = data.admins;
513 this.setState(this.state);
514 } else if (res.op == UserOperation.Search) {
515 let data = res.data as SearchResponse;
516 this.state.crossPosts = data.posts.filter(
517 p => p.id != Number(this.props.match.params.id)
519 if (this.state.crossPosts.length) {
520 this.state.post.duplicates = this.state.crossPosts;
522 this.setState(this.state);
524 res.op == UserOperation.TransferSite ||
525 res.op == UserOperation.GetSite
527 let data = res.data as GetSiteResponse;
528 this.state.siteRes = data;
529 this.setState(this.state);
530 } else if (res.op == UserOperation.TransferCommunity) {
531 let data = res.data as GetCommunityResponse;
532 this.state.community = data.community;
533 this.state.moderators = data.moderators;
534 this.state.siteRes.admins = data.admins;
535 this.setState(this.state);