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);
164 <div class="container">
165 {this.state.loading ? (
167 <svg class="icon icon-spinner spin">
168 <use xlinkHref="#icon-spinner"></use>
173 <div class="col-12 col-md-8 mb-3">
175 post={this.state.post}
178 moderators={this.state.moderators}
179 admins={this.state.admins}
181 {this.state.crossPosts.length > 0 && (
183 <div class="my-1 text-muted small font-weight-bold">
184 {i18n.t('cross_posts')}
186 <PostListings showCommunity posts={this.state.crossPosts} />
189 <div className="mb-2" />
191 postId={this.state.post.id}
192 disabled={this.state.post.locked}
194 {this.state.comments.length > 0 && this.sortRadios()}
195 {this.commentsTree()}
197 <div class="col-12 col-sm-12 col-md-4">
198 {this.state.comments.length > 0 && this.newComments()}
209 <div class="btn-group btn-group-toggle mb-3">
211 className={`btn btn-sm btn-secondary pointer ${this.state
212 .commentSort === CommentSortType.Hot && 'active'}`}
217 value={CommentSortType.Hot}
218 checked={this.state.commentSort === CommentSortType.Hot}
219 onChange={linkEvent(this, this.handleCommentSortChange)}
223 className={`btn btn-sm btn-secondary pointer ${this.state
224 .commentSort === CommentSortType.Top && 'active'}`}
229 value={CommentSortType.Top}
230 checked={this.state.commentSort === CommentSortType.Top}
231 onChange={linkEvent(this, this.handleCommentSortChange)}
235 className={`btn btn-sm btn-secondary pointer ${this.state
236 .commentSort === CommentSortType.New && 'active'}`}
241 value={CommentSortType.New}
242 checked={this.state.commentSort === CommentSortType.New}
243 onChange={linkEvent(this, this.handleCommentSortChange)}
247 className={`btn btn-sm btn-secondary pointer ${this.state
248 .commentSort === CommentSortType.Old && 'active'}`}
253 value={CommentSortType.Old}
254 checked={this.state.commentSort === CommentSortType.Old}
255 onChange={linkEvent(this, this.handleCommentSortChange)}
264 <div class="d-none d-md-block new-comments mb-3 card border-secondary">
265 <div class="card-body small">
266 <h6>{i18n.t('recent_comments')}</h6>
268 nodes={commentsToFlatNodes(this.state.comments)}
270 locked={this.state.post.locked}
271 moderators={this.state.moderators}
272 admins={this.state.admins}
273 postCreatorId={this.state.post.creator_id}
284 community={this.state.community}
285 moderators={this.state.moderators}
286 admins={this.state.admins}
287 online={this.state.online}
293 handleCommentSortChange(i: Post, event: any) {
294 i.state.commentSort = Number(event.target.value);
298 private buildCommentsTree(): Array<CommentNodeI> {
299 let map = new Map<number, CommentNodeI>();
300 for (let comment of this.state.comments) {
301 let node: CommentNodeI = {
305 map.set(comment.id, { ...node });
307 let tree: Array<CommentNodeI> = [];
308 for (let comment of this.state.comments) {
309 if (comment.parent_id) {
310 map.get(comment.parent_id).children.push(map.get(comment.id));
312 tree.push(map.get(comment.id));
320 let nodes = this.buildCommentsTree();
325 locked={this.state.post.locked}
326 moderators={this.state.moderators}
327 admins={this.state.admins}
328 postCreatorId={this.state.post.creator_id}
329 sort={this.state.commentSort}
335 parseMessage(msg: WebSocketJsonResponse) {
337 let res = wsJsonToRes(msg);
339 toast(i18n.t(msg.error), 'danger');
341 } else if (msg.reconnect) {
342 WebSocketService.Instance.getPost({
343 id: Number(this.props.match.params.id),
345 } else if (res.op == UserOperation.GetPost) {
346 let data = res.data as GetPostResponse;
347 this.state.post = data.post;
348 this.state.comments = data.comments;
349 this.state.community = data.community;
350 this.state.moderators = data.moderators;
351 this.state.admins = data.admins;
352 this.state.online = data.online;
353 this.state.loading = false;
354 document.title = `${this.state.post.name} - ${WebSocketService.Instance.site.name}`;
357 if (this.state.post.url) {
358 let form: SearchForm = {
359 q: this.state.post.url,
360 type_: SearchType[SearchType.Url],
361 sort: SortType[SortType.TopAll],
365 WebSocketService.Instance.search(form);
368 this.setState(this.state);
369 } else if (res.op == UserOperation.CreateComment) {
370 let data = res.data as CommentResponse;
372 // Necessary since it might be a user reply
373 if (data.recipient_ids.length == 0) {
374 this.state.comments.unshift(data.comment);
375 this.setState(this.state);
377 } else if (res.op == UserOperation.EditComment) {
378 let data = res.data as CommentResponse;
379 editCommentRes(data, this.state.comments);
380 this.setState(this.state);
381 } else if (res.op == UserOperation.SaveComment) {
382 let data = res.data as CommentResponse;
383 saveCommentRes(data, this.state.comments);
384 this.setState(this.state);
385 } else if (res.op == UserOperation.CreateCommentLike) {
386 let data = res.data as CommentResponse;
387 createCommentLikeRes(data, this.state.comments);
388 this.setState(this.state);
389 } else if (res.op == UserOperation.CreatePostLike) {
390 let data = res.data as PostResponse;
391 createPostLikeRes(data, this.state.post);
392 this.setState(this.state);
393 } else if (res.op == UserOperation.EditPost) {
394 let data = res.data as PostResponse;
395 this.state.post = data.post;
396 this.setState(this.state);
397 } else if (res.op == UserOperation.SavePost) {
398 let data = res.data as PostResponse;
399 this.state.post = data.post;
400 this.setState(this.state);
401 } else if (res.op == UserOperation.EditCommunity) {
402 let data = res.data as CommunityResponse;
403 this.state.community = data.community;
404 this.state.post.community_id = data.community.id;
405 this.state.post.community_name = data.community.name;
406 this.setState(this.state);
407 } else if (res.op == UserOperation.FollowCommunity) {
408 let data = res.data as CommunityResponse;
409 this.state.community.subscribed = data.community.subscribed;
410 this.state.community.number_of_subscribers =
411 data.community.number_of_subscribers;
412 this.setState(this.state);
413 } else if (res.op == UserOperation.BanFromCommunity) {
414 let data = res.data as BanFromCommunityResponse;
416 .filter(c => c.creator_id == data.user.id)
417 .forEach(c => (c.banned_from_community = data.banned));
418 if (this.state.post.creator_id == data.user.id) {
419 this.state.post.banned_from_community = data.banned;
421 this.setState(this.state);
422 } else if (res.op == UserOperation.AddModToCommunity) {
423 let data = res.data as AddModToCommunityResponse;
424 this.state.moderators = data.moderators;
425 this.setState(this.state);
426 } else if (res.op == UserOperation.BanUser) {
427 let data = res.data as BanUserResponse;
429 .filter(c => c.creator_id == data.user.id)
430 .forEach(c => (c.banned = data.banned));
431 if (this.state.post.creator_id == data.user.id) {
432 this.state.post.banned = data.banned;
434 this.setState(this.state);
435 } else if (res.op == UserOperation.AddAdmin) {
436 let data = res.data as AddAdminResponse;
437 this.state.admins = data.admins;
438 this.setState(this.state);
439 } else if (res.op == UserOperation.Search) {
440 let data = res.data as SearchResponse;
441 this.state.crossPosts = data.posts.filter(
442 p => p.id != this.state.post.id
444 this.setState(this.state);
445 } else if (res.op == UserOperation.TransferSite) {
446 let data = res.data as GetSiteResponse;
447 this.state.admins = data.admins;
448 this.setState(this.state);
449 } else if (res.op == UserOperation.TransferCommunity) {
450 let data = res.data as GetCommunityResponse;
451 this.state.community = data.community;
452 this.state.moderators = data.moderators;
453 this.state.admins = data.admins;
454 this.setState(this.state);