1 import { Component, linkEvent } from 'inferno';
2 import { Helmet } from 'inferno-helmet';
3 import { Subscription } from 'rxjs';
4 import { retryWhen, delay, take } from 'rxjs/operators';
12 MarkCommentAsReadForm,
18 CommentNode as CommentNodeI,
19 BanFromCommunityResponse,
21 AddModToCommunityResponse,
30 WebSocketJsonResponse,
31 } from '../interfaces';
32 import { WebSocketService, UserService } from '../services';
43 import { PostListing } from './post-listing';
44 import { Sidebar } from './sidebar';
45 import { CommentForm } from './comment-form';
46 import { CommentNodes } from './comment-nodes';
47 import autosize from 'autosize';
48 import { i18n } from '../i18next';
52 comments: Array<Comment>;
53 commentSort: CommentSortType;
54 commentViewType: CommentViewType;
56 moderators: Array<CommunityUser>;
59 scrolled_comment_id?: number;
61 crossPosts: Array<PostI>;
62 siteRes: GetSiteResponse;
65 export class Post extends Component<any, PostState> {
66 private subscription: Subscription;
67 private emptyState: PostState = {
70 commentSort: CommentSortType.Hot,
71 commentViewType: CommentViewType.Tree,
84 creator_id: undefined,
86 creator_name: undefined,
87 number_of_users: undefined,
88 number_of_posts: undefined,
89 number_of_comments: undefined,
90 number_of_communities: undefined,
91 enable_downvotes: undefined,
92 open_registration: undefined,
93 enable_nsfw: undefined,
100 constructor(props: any, context: any) {
101 super(props, context);
103 this.state = this.emptyState;
105 let postId = Number(this.props.match.params.id);
106 if (this.props.match.params.comment_id) {
107 this.state.scrolled_comment_id = this.props.match.params.comment_id;
110 this.subscription = WebSocketService.Instance.subject
111 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
113 msg => this.parseMessage(msg),
114 err => console.error(err),
115 () => console.log('complete')
118 let form: GetPostForm = {
121 WebSocketService.Instance.getPost(form);
122 WebSocketService.Instance.getSite();
125 componentWillUnmount() {
126 this.subscription.unsubscribe();
129 componentDidMount() {
130 autosize(document.querySelectorAll('textarea'));
133 componentDidUpdate(_lastProps: any, lastState: PostState, _snapshot: any) {
135 this.state.scrolled_comment_id &&
136 !this.state.scrolled &&
137 lastState.comments.length > 0
139 var elmnt = document.getElementById(
140 `comment-${this.state.scrolled_comment_id}`
142 elmnt.scrollIntoView();
143 elmnt.classList.add('mark');
144 this.state.scrolled = true;
145 this.markScrolledAsRead(this.state.scrolled_comment_id);
148 // Necessary if you are on a post and you click another post (same route)
149 if (_lastProps.location.pathname !== _lastProps.history.location.pathname) {
150 // Couldnt get a refresh working. This does for now.
153 // let currentId = this.props.match.params.id;
154 // WebSocketService.Instance.getPost(currentId);
155 // this.context.router.history.push('/sponsors');
156 // this.context.refresh();
157 // this.context.router.history.push(_lastProps.location.pathname);
161 markScrolledAsRead(commentId: number) {
162 let found = this.state.comments.find(c => c.id == commentId);
163 let parent = this.state.comments.find(c => found.parent_id == c.id);
164 let parent_user_id = parent
166 : this.state.post.creator_id;
169 UserService.Instance.user &&
170 UserService.Instance.user.id == parent_user_id
172 let form: MarkCommentAsReadForm = {
177 WebSocketService.Instance.markCommentAsRead(form);
178 UserService.Instance.unreadCountSub.next(
179 UserService.Instance.unreadCountSub.value - 1
184 get documentTitle(): string {
185 if (this.state.post) {
186 return `${this.state.post.name} - ${this.state.siteRes.site.name}`;
194 <div class="container">
195 <Helmet title={this.documentTitle} />
196 {this.state.loading ? (
198 <svg class="icon icon-spinner spin">
199 <use xlinkHref="#icon-spinner"></use>
204 <div class="col-12 col-md-8 mb-3">
206 post={this.state.post}
209 moderators={this.state.moderators}
210 admins={this.state.siteRes.admins}
211 enableDownvotes={this.state.siteRes.site.enable_downvotes}
212 enableNsfw={this.state.siteRes.site.enable_nsfw}
214 <div className="mb-2" />
216 postId={this.state.post.id}
217 disabled={this.state.post.locked}
219 {this.state.comments.length > 0 && this.sortRadios()}
220 {this.state.commentViewType == CommentViewType.Tree &&
222 {this.state.commentViewType == CommentViewType.Chat &&
225 <div class="col-12 col-sm-12 col-md-4">{this.sidebar()}</div>
235 <div class="btn-group btn-group-toggle mr-3 mb-2">
237 className={`btn btn-outline-secondary pointer ${
238 this.state.commentSort === CommentSortType.Hot && 'active'
244 value={CommentSortType.Hot}
245 checked={this.state.commentSort === CommentSortType.Hot}
246 onChange={linkEvent(this, this.handleCommentSortChange)}
250 className={`btn btn-outline-secondary pointer ${
251 this.state.commentSort === CommentSortType.Top && 'active'
257 value={CommentSortType.Top}
258 checked={this.state.commentSort === CommentSortType.Top}
259 onChange={linkEvent(this, this.handleCommentSortChange)}
263 className={`btn btn-outline-secondary pointer ${
264 this.state.commentSort === CommentSortType.New && 'active'
270 value={CommentSortType.New}
271 checked={this.state.commentSort === CommentSortType.New}
272 onChange={linkEvent(this, this.handleCommentSortChange)}
276 className={`btn btn-outline-secondary pointer ${
277 this.state.commentSort === CommentSortType.Old && 'active'
283 value={CommentSortType.Old}
284 checked={this.state.commentSort === CommentSortType.Old}
285 onChange={linkEvent(this, this.handleCommentSortChange)}
289 <div class="btn-group btn-group-toggle mb-2">
291 className={`btn btn-outline-secondary pointer ${
292 this.state.commentViewType === CommentViewType.Chat && 'active'
298 value={CommentViewType.Chat}
299 checked={this.state.commentViewType === CommentViewType.Chat}
300 onChange={linkEvent(this, this.handleCommentViewTypeChange)}
312 nodes={commentsToFlatNodes(this.state.comments)}
314 locked={this.state.post.locked}
315 moderators={this.state.moderators}
316 admins={this.state.siteRes.admins}
317 postCreatorId={this.state.post.creator_id}
319 enableDownvotes={this.state.siteRes.site.enable_downvotes}
320 sort={this.state.commentSort}
330 community={this.state.community}
331 moderators={this.state.moderators}
332 admins={this.state.siteRes.admins}
333 online={this.state.online}
334 enableNsfw={this.state.siteRes.site.enable_nsfw}
340 handleCommentSortChange(i: Post, event: any) {
341 i.state.commentSort = Number(event.target.value);
342 i.state.commentViewType = CommentViewType.Tree;
346 handleCommentViewTypeChange(i: Post, event: any) {
347 i.state.commentViewType = Number(event.target.value);
348 i.state.commentSort = CommentSortType.New;
352 buildCommentsTree(): Array<CommentNodeI> {
353 let map = new Map<number, CommentNodeI>();
354 for (let comment of this.state.comments) {
355 let node: CommentNodeI = {
359 map.set(comment.id, { ...node });
361 let tree: Array<CommentNodeI> = [];
362 for (let comment of this.state.comments) {
363 let child = map.get(comment.id);
364 if (comment.parent_id) {
365 let parent_ = map.get(comment.parent_id);
366 parent_.children.push(child);
371 this.setDepth(child);
377 setDepth(node: CommentNodeI, i: number = 0): void {
378 for (let child of node.children) {
379 child.comment.depth = i;
380 this.setDepth(child, i + 1);
385 let nodes = this.buildCommentsTree();
390 locked={this.state.post.locked}
391 moderators={this.state.moderators}
392 admins={this.state.siteRes.admins}
393 postCreatorId={this.state.post.creator_id}
394 sort={this.state.commentSort}
395 enableDownvotes={this.state.siteRes.site.enable_downvotes}
401 parseMessage(msg: WebSocketJsonResponse) {
403 let res = wsJsonToRes(msg);
405 toast(i18n.t(msg.error), 'danger');
407 } else if (msg.reconnect) {
408 WebSocketService.Instance.getPost({
409 id: Number(this.props.match.params.id),
411 } else if (res.op == UserOperation.GetPost) {
412 let data = res.data as GetPostResponse;
413 this.state.post = data.post;
414 this.state.comments = data.comments;
415 this.state.community = data.community;
416 this.state.moderators = data.moderators;
417 this.state.online = data.online;
418 this.state.loading = false;
421 if (this.state.post.url) {
422 let form: SearchForm = {
423 q: this.state.post.url,
424 type_: SearchType[SearchType.Url],
425 sort: SortType[SortType.TopAll],
429 WebSocketService.Instance.search(form);
432 this.setState(this.state);
434 } else if (res.op == UserOperation.CreateComment) {
435 let data = res.data as CommentResponse;
437 // Necessary since it might be a user reply
438 if (data.recipient_ids.length == 0) {
439 this.state.comments.unshift(data.comment);
440 this.setState(this.state);
443 res.op == UserOperation.EditComment ||
444 res.op == UserOperation.DeleteComment ||
445 res.op == UserOperation.RemoveComment
447 let data = res.data as CommentResponse;
448 editCommentRes(data, this.state.comments);
449 this.setState(this.state);
450 } else if (res.op == UserOperation.SaveComment) {
451 let data = res.data as CommentResponse;
452 saveCommentRes(data, this.state.comments);
453 this.setState(this.state);
455 } else if (res.op == UserOperation.CreateCommentLike) {
456 let data = res.data as CommentResponse;
457 createCommentLikeRes(data, this.state.comments);
458 this.setState(this.state);
459 } else if (res.op == UserOperation.CreatePostLike) {
460 let data = res.data as PostResponse;
461 createPostLikeRes(data, this.state.post);
462 this.setState(this.state);
464 res.op == UserOperation.EditPost ||
465 res.op == UserOperation.DeletePost ||
466 res.op == UserOperation.RemovePost ||
467 res.op == UserOperation.LockPost ||
468 res.op == UserOperation.StickyPost
470 let data = res.data as PostResponse;
471 this.state.post = data.post;
472 this.setState(this.state);
474 } else if (res.op == UserOperation.SavePost) {
475 let data = res.data as PostResponse;
476 this.state.post = data.post;
477 this.setState(this.state);
480 res.op == UserOperation.EditCommunity ||
481 res.op == UserOperation.DeleteCommunity ||
482 res.op == UserOperation.RemoveCommunity
484 let data = res.data as CommunityResponse;
485 this.state.community = data.community;
486 this.state.post.community_id = data.community.id;
487 this.state.post.community_name = data.community.name;
488 this.setState(this.state);
489 } else if (res.op == UserOperation.FollowCommunity) {
490 let data = res.data as CommunityResponse;
491 this.state.community.subscribed = data.community.subscribed;
492 this.state.community.number_of_subscribers =
493 data.community.number_of_subscribers;
494 this.setState(this.state);
495 } else if (res.op == UserOperation.BanFromCommunity) {
496 let data = res.data as BanFromCommunityResponse;
498 .filter(c => c.creator_id == data.user.id)
499 .forEach(c => (c.banned_from_community = data.banned));
500 if (this.state.post.creator_id == data.user.id) {
501 this.state.post.banned_from_community = data.banned;
503 this.setState(this.state);
504 } else if (res.op == UserOperation.AddModToCommunity) {
505 let data = res.data as AddModToCommunityResponse;
506 this.state.moderators = data.moderators;
507 this.setState(this.state);
508 } else if (res.op == UserOperation.BanUser) {
509 let data = res.data as BanUserResponse;
511 .filter(c => c.creator_id == data.user.id)
512 .forEach(c => (c.banned = data.banned));
513 if (this.state.post.creator_id == data.user.id) {
514 this.state.post.banned = data.banned;
516 this.setState(this.state);
517 } else if (res.op == UserOperation.AddAdmin) {
518 let data = res.data as AddAdminResponse;
519 this.state.siteRes.admins = data.admins;
520 this.setState(this.state);
521 } else if (res.op == UserOperation.Search) {
522 let data = res.data as SearchResponse;
523 this.state.crossPosts = data.posts.filter(
524 p => p.id != Number(this.props.match.params.id)
526 if (this.state.crossPosts.length) {
527 this.state.post.duplicates = this.state.crossPosts;
529 this.setState(this.state);
531 res.op == UserOperation.TransferSite ||
532 res.op == UserOperation.GetSite
534 let data = res.data as GetSiteResponse;
535 this.state.siteRes = data;
536 this.setState(this.state);
537 } else if (res.op == UserOperation.TransferCommunity) {
538 let data = res.data as GetCommunityResponse;
539 this.state.community = data.community;
540 this.state.moderators = data.moderators;
541 this.setState(this.state);