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';
41 import { PostListing } from './post-listing';
42 import { PostListings } from './post-listings';
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 {this.state.crossPosts.length > 0 && (
187 <div class="my-1 text-muted small font-weight-bold">
188 {i18n.t('cross_posts')}
190 <PostListings showCommunity posts={this.state.crossPosts} />
193 <div className="mb-2" />
195 postId={this.state.post.id}
196 disabled={this.state.post.locked}
198 {this.state.comments.length > 0 && this.sortRadios()}
199 {this.commentsTree()}
201 <div class="col-12 col-sm-12 col-md-4">
202 {this.state.comments.length > 0 && this.newComments()}
213 <div class="btn-group btn-group-toggle mb-3">
215 className={`btn btn-sm btn-secondary pointer ${this.state
216 .commentSort === CommentSortType.Hot && 'active'}`}
221 value={CommentSortType.Hot}
222 checked={this.state.commentSort === CommentSortType.Hot}
223 onChange={linkEvent(this, this.handleCommentSortChange)}
227 className={`btn btn-sm btn-secondary pointer ${this.state
228 .commentSort === CommentSortType.Top && 'active'}`}
233 value={CommentSortType.Top}
234 checked={this.state.commentSort === CommentSortType.Top}
235 onChange={linkEvent(this, this.handleCommentSortChange)}
239 className={`btn btn-sm btn-secondary pointer ${this.state
240 .commentSort === CommentSortType.New && 'active'}`}
245 value={CommentSortType.New}
246 checked={this.state.commentSort === CommentSortType.New}
247 onChange={linkEvent(this, this.handleCommentSortChange)}
251 className={`btn btn-sm btn-secondary pointer ${this.state
252 .commentSort === CommentSortType.Old && 'active'}`}
257 value={CommentSortType.Old}
258 checked={this.state.commentSort === CommentSortType.Old}
259 onChange={linkEvent(this, this.handleCommentSortChange)}
268 <div class="d-none d-md-block new-comments mb-3 card border-secondary">
269 <div class="card-body small">
270 <h6>{i18n.t('recent_comments')}</h6>
272 nodes={commentsToFlatNodes(this.state.comments)}
274 locked={this.state.post.locked}
275 moderators={this.state.moderators}
276 admins={this.state.admins}
277 postCreatorId={this.state.post.creator_id}
288 community={this.state.community}
289 moderators={this.state.moderators}
290 admins={this.state.admins}
291 online={this.state.online}
297 handleCommentSortChange(i: Post, event: any) {
298 i.state.commentSort = Number(event.target.value);
302 private buildCommentsTree(): Array<CommentNodeI> {
303 let map = new Map<number, CommentNodeI>();
304 for (let comment of this.state.comments) {
305 let node: CommentNodeI = {
309 map.set(comment.id, { ...node });
311 let tree: Array<CommentNodeI> = [];
312 for (let comment of this.state.comments) {
313 if (comment.parent_id) {
314 map.get(comment.parent_id).children.push(map.get(comment.id));
316 tree.push(map.get(comment.id));
324 let nodes = this.buildCommentsTree();
329 locked={this.state.post.locked}
330 moderators={this.state.moderators}
331 admins={this.state.admins}
332 postCreatorId={this.state.post.creator_id}
333 sort={this.state.commentSort}
339 parseMessage(msg: WebSocketJsonResponse) {
341 let res = wsJsonToRes(msg);
343 toast(i18n.t(msg.error), 'danger');
345 } else if (msg.reconnect) {
346 WebSocketService.Instance.getPost({
347 id: Number(this.props.match.params.id),
349 } else if (res.op == UserOperation.GetPost) {
350 let data = res.data as GetPostResponse;
351 this.state.post = data.post;
352 this.state.comments = data.comments;
353 this.state.community = data.community;
354 this.state.moderators = data.moderators;
355 this.state.admins = data.admins;
356 this.state.online = data.online;
357 this.state.loading = false;
358 document.title = `${this.state.post.name} - ${WebSocketService.Instance.site.name}`;
361 if (this.state.post.url) {
362 let form: SearchForm = {
363 q: this.state.post.url,
364 type_: SearchType[SearchType.Url],
365 sort: SortType[SortType.TopAll],
369 WebSocketService.Instance.search(form);
372 this.setState(this.state);
373 } else if (res.op == UserOperation.CreateComment) {
374 let data = res.data as CommentResponse;
376 // Necessary since it might be a user reply
377 if (data.recipient_ids.length == 0) {
378 this.state.comments.unshift(data.comment);
379 this.setState(this.state);
381 } else if (res.op == UserOperation.EditComment) {
382 let data = res.data as CommentResponse;
383 editCommentRes(data, this.state.comments);
384 this.setState(this.state);
385 } else if (res.op == UserOperation.SaveComment) {
386 let data = res.data as CommentResponse;
387 saveCommentRes(data, this.state.comments);
388 this.setState(this.state);
389 } else if (res.op == UserOperation.CreateCommentLike) {
390 let data = res.data as CommentResponse;
391 createCommentLikeRes(data, this.state.comments);
392 this.setState(this.state);
393 } else if (res.op == UserOperation.CreatePostLike) {
394 let data = res.data as PostResponse;
395 createPostLikeRes(data, this.state.post);
396 this.setState(this.state);
397 } else if (res.op == UserOperation.EditPost) {
398 let data = res.data as PostResponse;
399 this.state.post = data.post;
400 this.setState(this.state);
401 } else if (res.op == UserOperation.SavePost) {
402 let data = res.data as PostResponse;
403 this.state.post = data.post;
404 this.setState(this.state);
405 } else if (res.op == UserOperation.EditCommunity) {
406 let data = res.data as CommunityResponse;
407 this.state.community = data.community;
408 this.state.post.community_id = data.community.id;
409 this.state.post.community_name = data.community.name;
410 this.setState(this.state);
411 } else if (res.op == UserOperation.FollowCommunity) {
412 let data = res.data as CommunityResponse;
413 this.state.community.subscribed = data.community.subscribed;
414 this.state.community.number_of_subscribers =
415 data.community.number_of_subscribers;
416 this.setState(this.state);
417 } else if (res.op == UserOperation.BanFromCommunity) {
418 let data = res.data as BanFromCommunityResponse;
420 .filter(c => c.creator_id == data.user.id)
421 .forEach(c => (c.banned_from_community = data.banned));
422 if (this.state.post.creator_id == data.user.id) {
423 this.state.post.banned_from_community = data.banned;
425 this.setState(this.state);
426 } else if (res.op == UserOperation.AddModToCommunity) {
427 let data = res.data as AddModToCommunityResponse;
428 this.state.moderators = data.moderators;
429 this.setState(this.state);
430 } else if (res.op == UserOperation.BanUser) {
431 let data = res.data as BanUserResponse;
433 .filter(c => c.creator_id == data.user.id)
434 .forEach(c => (c.banned = data.banned));
435 if (this.state.post.creator_id == data.user.id) {
436 this.state.post.banned = data.banned;
438 this.setState(this.state);
439 } else if (res.op == UserOperation.AddAdmin) {
440 let data = res.data as AddAdminResponse;
441 this.state.admins = data.admins;
442 this.setState(this.state);
443 } else if (res.op == UserOperation.Search) {
444 let data = res.data as SearchResponse;
445 this.state.crossPosts = data.posts.filter(
446 p => p.id != this.state.post.id
448 this.setState(this.state);
449 } else if (res.op == UserOperation.TransferSite) {
450 let data = res.data as GetSiteResponse;
451 this.state.admins = data.admins;
452 this.setState(this.state);
453 } else if (res.op == UserOperation.TransferCommunity) {
454 let data = res.data as GetCommunityResponse;
455 this.state.community = data.community;
456 this.state.moderators = data.moderators;
457 this.state.admins = data.admins;
458 this.setState(this.state);