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 mr-3 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 mb-2">
285 className={`btn btn-sm btn-secondary pointer ${
286 this.state.commentViewType === CommentViewType.Chat && 'active'
292 value={CommentViewType.Chat}
293 checked={this.state.commentViewType === CommentViewType.Chat}
294 onChange={linkEvent(this, this.handleCommentViewTypeChange)}
306 nodes={commentsToFlatNodes(this.state.comments)}
308 locked={this.state.post.locked}
309 moderators={this.state.moderators}
310 admins={this.state.siteRes.admins}
311 postCreatorId={this.state.post.creator_id}
313 enableDownvotes={this.state.siteRes.site.enable_downvotes}
314 sort={this.state.commentSort}
324 community={this.state.community}
325 moderators={this.state.moderators}
326 admins={this.state.siteRes.admins}
327 online={this.state.online}
328 enableNsfw={this.state.siteRes.site.enable_nsfw}
334 handleCommentSortChange(i: Post, event: any) {
335 i.state.commentSort = Number(event.target.value);
336 i.state.commentViewType = CommentViewType.Tree;
340 handleCommentViewTypeChange(i: Post, event: any) {
341 i.state.commentViewType = Number(event.target.value);
342 i.state.commentSort = CommentSortType.New;
346 buildCommentsTree(): Array<CommentNodeI> {
347 let map = new Map<number, CommentNodeI>();
348 for (let comment of this.state.comments) {
349 let node: CommentNodeI = {
353 map.set(comment.id, { ...node });
355 let tree: Array<CommentNodeI> = [];
356 for (let comment of this.state.comments) {
357 let child = map.get(comment.id);
358 if (comment.parent_id) {
359 let parent_ = map.get(comment.parent_id);
360 parent_.children.push(child);
365 this.setDepth(child);
371 setDepth(node: CommentNodeI, i: number = 0): void {
372 for (let child of node.children) {
373 child.comment.depth = i;
374 this.setDepth(child, i + 1);
379 let nodes = this.buildCommentsTree();
384 locked={this.state.post.locked}
385 moderators={this.state.moderators}
386 admins={this.state.siteRes.admins}
387 postCreatorId={this.state.post.creator_id}
388 sort={this.state.commentSort}
389 enableDownvotes={this.state.siteRes.site.enable_downvotes}
395 parseMessage(msg: WebSocketJsonResponse) {
397 let res = wsJsonToRes(msg);
399 toast(i18n.t(msg.error), 'danger');
401 } else if (msg.reconnect) {
402 WebSocketService.Instance.getPost({
403 id: Number(this.props.match.params.id),
405 } else if (res.op == UserOperation.GetPost) {
406 let data = res.data as GetPostResponse;
407 this.state.post = data.post;
408 this.state.comments = data.comments;
409 this.state.community = data.community;
410 this.state.moderators = data.moderators;
411 this.state.siteRes.admins = data.admins;
412 this.state.online = data.online;
413 this.state.loading = false;
414 document.title = `${this.state.post.name} - ${this.state.siteRes.site.name}`;
417 if (this.state.post.url) {
418 let form: SearchForm = {
419 q: this.state.post.url,
420 type_: SearchType[SearchType.Url],
421 sort: SortType[SortType.TopAll],
425 WebSocketService.Instance.search(form);
428 this.setState(this.state);
430 } else if (res.op == UserOperation.CreateComment) {
431 let data = res.data as CommentResponse;
433 // Necessary since it might be a user reply
434 if (data.recipient_ids.length == 0) {
435 this.state.comments.unshift(data.comment);
436 this.setState(this.state);
438 } else if (res.op == UserOperation.EditComment) {
439 let data = res.data as CommentResponse;
440 editCommentRes(data, this.state.comments);
441 this.setState(this.state);
442 } else if (res.op == UserOperation.SaveComment) {
443 let data = res.data as CommentResponse;
444 saveCommentRes(data, this.state.comments);
445 this.setState(this.state);
447 } else if (res.op == UserOperation.CreateCommentLike) {
448 let data = res.data as CommentResponse;
449 createCommentLikeRes(data, this.state.comments);
450 this.setState(this.state);
451 } else if (res.op == UserOperation.CreatePostLike) {
452 let data = res.data as PostResponse;
453 createPostLikeRes(data, this.state.post);
454 this.setState(this.state);
455 } else if (res.op == UserOperation.EditPost) {
456 let data = res.data as PostResponse;
457 this.state.post = data.post;
458 this.setState(this.state);
460 } else if (res.op == UserOperation.SavePost) {
461 let data = res.data as PostResponse;
462 this.state.post = data.post;
463 this.setState(this.state);
465 } else if (res.op == UserOperation.EditCommunity) {
466 let data = res.data as CommunityResponse;
467 this.state.community = data.community;
468 this.state.post.community_id = data.community.id;
469 this.state.post.community_name = data.community.name;
470 this.setState(this.state);
471 } else if (res.op == UserOperation.FollowCommunity) {
472 let data = res.data as CommunityResponse;
473 this.state.community.subscribed = data.community.subscribed;
474 this.state.community.number_of_subscribers =
475 data.community.number_of_subscribers;
476 this.setState(this.state);
477 } else if (res.op == UserOperation.BanFromCommunity) {
478 let data = res.data as BanFromCommunityResponse;
480 .filter(c => c.creator_id == data.user.id)
481 .forEach(c => (c.banned_from_community = data.banned));
482 if (this.state.post.creator_id == data.user.id) {
483 this.state.post.banned_from_community = data.banned;
485 this.setState(this.state);
486 } else if (res.op == UserOperation.AddModToCommunity) {
487 let data = res.data as AddModToCommunityResponse;
488 this.state.moderators = data.moderators;
489 this.setState(this.state);
490 } else if (res.op == UserOperation.BanUser) {
491 let data = res.data as BanUserResponse;
493 .filter(c => c.creator_id == data.user.id)
494 .forEach(c => (c.banned = data.banned));
495 if (this.state.post.creator_id == data.user.id) {
496 this.state.post.banned = data.banned;
498 this.setState(this.state);
499 } else if (res.op == UserOperation.AddAdmin) {
500 let data = res.data as AddAdminResponse;
501 this.state.siteRes.admins = data.admins;
502 this.setState(this.state);
503 } else if (res.op == UserOperation.Search) {
504 let data = res.data as SearchResponse;
505 this.state.crossPosts = data.posts.filter(
506 p => p.id != Number(this.props.match.params.id)
508 if (this.state.crossPosts.length) {
509 this.state.post.duplicates = this.state.crossPosts;
511 this.setState(this.state);
513 res.op == UserOperation.TransferSite ||
514 res.op == UserOperation.GetSite
516 let data = res.data as GetSiteResponse;
517 this.state.siteRes = data;
518 this.setState(this.state);
519 } else if (res.op == UserOperation.TransferCommunity) {
520 let data = res.data as GetCommunityResponse;
521 this.state.community = data.community;
522 this.state.moderators = data.moderators;
523 this.state.siteRes.admins = data.admins;
524 this.setState(this.state);