1 import { Component, linkEvent } from 'inferno';
2 import { Subscription } from 'rxjs';
3 import { retryWhen, delay, take } from 'rxjs/operators';
11 MarkCommentAsReadForm,
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,
99 constructor(props: any, context: any) {
100 super(props, context);
102 this.state = this.emptyState;
104 let postId = Number(this.props.match.params.id);
105 if (this.props.match.params.comment_id) {
106 this.state.scrolled_comment_id = this.props.match.params.comment_id;
109 this.subscription = WebSocketService.Instance.subject
110 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
112 msg => this.parseMessage(msg),
113 err => console.error(err),
114 () => console.log('complete')
117 let form: GetPostForm = {
120 WebSocketService.Instance.getPost(form);
121 WebSocketService.Instance.getSite();
124 componentWillUnmount() {
125 this.subscription.unsubscribe();
128 componentDidMount() {
129 autosize(document.querySelectorAll('textarea'));
132 componentDidUpdate(_lastProps: any, lastState: PostState, _snapshot: any) {
134 this.state.scrolled_comment_id &&
135 !this.state.scrolled &&
136 lastState.comments.length > 0
138 var elmnt = document.getElementById(
139 `comment-${this.state.scrolled_comment_id}`
141 elmnt.scrollIntoView();
142 elmnt.classList.add('mark');
143 this.state.scrolled = true;
144 this.markScrolledAsRead(this.state.scrolled_comment_id);
147 // Necessary if you are on a post and you click another post (same route)
148 if (_lastProps.location.pathname !== _lastProps.history.location.pathname) {
149 // Couldnt get a refresh working. This does for now.
152 // let currentId = this.props.match.params.id;
153 // WebSocketService.Instance.getPost(currentId);
154 // this.context.router.history.push('/sponsors');
155 // this.context.refresh();
156 // this.context.router.history.push(_lastProps.location.pathname);
160 markScrolledAsRead(commentId: number) {
161 let found = this.state.comments.find(c => c.id == commentId);
162 let parent = this.state.comments.find(c => found.parent_id == c.id);
163 let parent_user_id = parent
165 : this.state.post.creator_id;
168 UserService.Instance.user &&
169 UserService.Instance.user.id == parent_user_id
171 let form: MarkCommentAsReadForm = {
176 WebSocketService.Instance.markCommentAsRead(form);
177 UserService.Instance.unreadCountSub.next(
178 UserService.Instance.unreadCountSub.value - 1
185 <div class="container">
186 {this.state.loading ? (
188 <svg class="icon icon-spinner spin">
189 <use xlinkHref="#icon-spinner"></use>
194 <div class="col-12 col-md-8 mb-3">
196 post={this.state.post}
199 moderators={this.state.moderators}
200 admins={this.state.siteRes.admins}
201 enableDownvotes={this.state.siteRes.site.enable_downvotes}
202 enableNsfw={this.state.siteRes.site.enable_nsfw}
204 <div className="mb-2" />
206 postId={this.state.post.id}
207 disabled={this.state.post.locked}
209 {this.state.comments.length > 0 && this.sortRadios()}
210 {this.state.commentViewType == CommentViewType.Tree &&
212 {this.state.commentViewType == CommentViewType.Chat &&
215 <div class="col-12 col-sm-12 col-md-4">{this.sidebar()}</div>
225 <div class="btn-group btn-group-toggle mr-3 mb-2">
227 className={`btn btn-outline-secondary pointer ${
228 this.state.commentSort === CommentSortType.Hot && 'active'
234 value={CommentSortType.Hot}
235 checked={this.state.commentSort === CommentSortType.Hot}
236 onChange={linkEvent(this, this.handleCommentSortChange)}
240 className={`btn btn-outline-secondary pointer ${
241 this.state.commentSort === CommentSortType.Top && 'active'
247 value={CommentSortType.Top}
248 checked={this.state.commentSort === CommentSortType.Top}
249 onChange={linkEvent(this, this.handleCommentSortChange)}
253 className={`btn btn-outline-secondary pointer ${
254 this.state.commentSort === CommentSortType.New && 'active'
260 value={CommentSortType.New}
261 checked={this.state.commentSort === CommentSortType.New}
262 onChange={linkEvent(this, this.handleCommentSortChange)}
266 className={`btn btn-outline-secondary pointer ${
267 this.state.commentSort === CommentSortType.Old && 'active'
273 value={CommentSortType.Old}
274 checked={this.state.commentSort === CommentSortType.Old}
275 onChange={linkEvent(this, this.handleCommentSortChange)}
279 <div class="btn-group btn-group-toggle mb-2">
281 className={`btn btn-outline-secondary pointer ${
282 this.state.commentViewType === CommentViewType.Chat && 'active'
288 value={CommentViewType.Chat}
289 checked={this.state.commentViewType === CommentViewType.Chat}
290 onChange={linkEvent(this, this.handleCommentViewTypeChange)}
302 nodes={commentsToFlatNodes(this.state.comments)}
304 locked={this.state.post.locked}
305 moderators={this.state.moderators}
306 admins={this.state.siteRes.admins}
307 postCreatorId={this.state.post.creator_id}
309 enableDownvotes={this.state.siteRes.site.enable_downvotes}
310 sort={this.state.commentSort}
320 community={this.state.community}
321 moderators={this.state.moderators}
322 admins={this.state.siteRes.admins}
323 online={this.state.online}
324 enableNsfw={this.state.siteRes.site.enable_nsfw}
330 handleCommentSortChange(i: Post, event: any) {
331 i.state.commentSort = Number(event.target.value);
332 i.state.commentViewType = CommentViewType.Tree;
336 handleCommentViewTypeChange(i: Post, event: any) {
337 i.state.commentViewType = Number(event.target.value);
338 i.state.commentSort = CommentSortType.New;
342 buildCommentsTree(): Array<CommentNodeI> {
343 let map = new Map<number, CommentNodeI>();
344 for (let comment of this.state.comments) {
345 let node: CommentNodeI = {
349 map.set(comment.id, { ...node });
351 let tree: Array<CommentNodeI> = [];
352 for (let comment of this.state.comments) {
353 let child = map.get(comment.id);
354 if (comment.parent_id) {
355 let parent_ = map.get(comment.parent_id);
356 parent_.children.push(child);
361 this.setDepth(child);
367 setDepth(node: CommentNodeI, i: number = 0): void {
368 for (let child of node.children) {
369 child.comment.depth = i;
370 this.setDepth(child, i + 1);
375 let nodes = this.buildCommentsTree();
380 locked={this.state.post.locked}
381 moderators={this.state.moderators}
382 admins={this.state.siteRes.admins}
383 postCreatorId={this.state.post.creator_id}
384 sort={this.state.commentSort}
385 enableDownvotes={this.state.siteRes.site.enable_downvotes}
391 parseMessage(msg: WebSocketJsonResponse) {
393 let res = wsJsonToRes(msg);
395 toast(i18n.t(msg.error), 'danger');
397 } else if (msg.reconnect) {
398 WebSocketService.Instance.getPost({
399 id: Number(this.props.match.params.id),
401 } else if (res.op == UserOperation.GetPost) {
402 let data = res.data as GetPostResponse;
403 this.state.post = data.post;
404 this.state.comments = data.comments;
405 this.state.community = data.community;
406 this.state.moderators = data.moderators;
407 this.state.online = data.online;
408 this.state.loading = false;
409 document.title = `${this.state.post.name} - ${this.state.siteRes.site.name}`;
412 if (this.state.post.url) {
413 let form: SearchForm = {
414 q: this.state.post.url,
415 type_: SearchType[SearchType.Url],
416 sort: SortType[SortType.TopAll],
420 WebSocketService.Instance.search(form);
423 this.setState(this.state);
425 } else if (res.op == UserOperation.CreateComment) {
426 let data = res.data as CommentResponse;
428 // Necessary since it might be a user reply
429 if (data.recipient_ids.length == 0) {
430 this.state.comments.unshift(data.comment);
431 this.setState(this.state);
434 res.op == UserOperation.EditComment ||
435 res.op == UserOperation.DeleteComment ||
436 res.op == UserOperation.RemoveComment
438 let data = res.data as CommentResponse;
439 editCommentRes(data, this.state.comments);
440 this.setState(this.state);
441 } else if (res.op == UserOperation.SaveComment) {
442 let data = res.data as CommentResponse;
443 saveCommentRes(data, this.state.comments);
444 this.setState(this.state);
446 } else if (res.op == UserOperation.CreateCommentLike) {
447 let data = res.data as CommentResponse;
448 createCommentLikeRes(data, this.state.comments);
449 this.setState(this.state);
450 } else if (res.op == UserOperation.CreatePostLike) {
451 let data = res.data as PostResponse;
452 createPostLikeRes(data, this.state.post);
453 this.setState(this.state);
455 res.op == UserOperation.EditPost ||
456 res.op == UserOperation.DeletePost ||
457 res.op == UserOperation.RemovePost ||
458 res.op == UserOperation.LockPost ||
459 res.op == UserOperation.StickyPost
461 let data = res.data as PostResponse;
462 this.state.post = data.post;
463 this.setState(this.state);
465 } else if (res.op == UserOperation.SavePost) {
466 let data = res.data as PostResponse;
467 this.state.post = data.post;
468 this.setState(this.state);
471 res.op == UserOperation.EditCommunity ||
472 res.op == UserOperation.DeleteCommunity ||
473 res.op == UserOperation.RemoveCommunity
475 let data = res.data as CommunityResponse;
476 this.state.community = data.community;
477 this.state.post.community_id = data.community.id;
478 this.state.post.community_name = data.community.name;
479 this.setState(this.state);
480 } else if (res.op == UserOperation.FollowCommunity) {
481 let data = res.data as CommunityResponse;
482 this.state.community.subscribed = data.community.subscribed;
483 this.state.community.number_of_subscribers =
484 data.community.number_of_subscribers;
485 this.setState(this.state);
486 } else if (res.op == UserOperation.BanFromCommunity) {
487 let data = res.data as BanFromCommunityResponse;
489 .filter(c => c.creator_id == data.user.id)
490 .forEach(c => (c.banned_from_community = data.banned));
491 if (this.state.post.creator_id == data.user.id) {
492 this.state.post.banned_from_community = data.banned;
494 this.setState(this.state);
495 } else if (res.op == UserOperation.AddModToCommunity) {
496 let data = res.data as AddModToCommunityResponse;
497 this.state.moderators = data.moderators;
498 this.setState(this.state);
499 } else if (res.op == UserOperation.BanUser) {
500 let data = res.data as BanUserResponse;
502 .filter(c => c.creator_id == data.user.id)
503 .forEach(c => (c.banned = data.banned));
504 if (this.state.post.creator_id == data.user.id) {
505 this.state.post.banned = data.banned;
507 this.setState(this.state);
508 } else if (res.op == UserOperation.AddAdmin) {
509 let data = res.data as AddAdminResponse;
510 this.state.siteRes.admins = data.admins;
511 this.setState(this.state);
512 } else if (res.op == UserOperation.Search) {
513 let data = res.data as SearchResponse;
514 this.state.crossPosts = data.posts.filter(
515 p => p.id != Number(this.props.match.params.id)
517 if (this.state.crossPosts.length) {
518 this.state.post.duplicates = this.state.crossPosts;
520 this.setState(this.state);
522 res.op == UserOperation.TransferSite ||
523 res.op == UserOperation.GetSite
525 let data = res.data as GetSiteResponse;
526 this.state.siteRes = data;
527 this.setState(this.state);
528 } else if (res.op == UserOperation.TransferCommunity) {
529 let data = res.data as GetCommunityResponse;
530 this.state.community = data.community;
531 this.state.moderators = data.moderators;
532 this.setState(this.state);