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,
16 CommentNode as CommentNodeI,
17 BanFromCommunityResponse,
19 AddModToCommunityResponse,
28 WebSocketJsonResponse,
29 } from 'lemmy-js-client';
30 import { CommentSortType, CommentViewType } from '../interfaces';
31 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';
53 commentSort: CommentSortType;
54 commentViewType: CommentViewType;
56 moderators: CommunityUser[];
59 scrolled_comment_id?: number;
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,
99 federated_instances: undefined,
103 constructor(props: any, context: any) {
104 super(props, context);
106 this.state = this.emptyState;
108 let postId = Number(this.props.match.params.id);
109 if (this.props.match.params.comment_id) {
110 this.state.scrolled_comment_id = this.props.match.params.comment_id;
113 this.subscription = WebSocketService.Instance.subject
114 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
116 msg => this.parseMessage(msg),
117 err => console.error(err),
118 () => console.log('complete')
121 let form: GetPostForm = {
124 WebSocketService.Instance.getPost(form);
125 WebSocketService.Instance.getSite();
128 componentWillUnmount() {
129 this.subscription.unsubscribe();
132 componentDidMount() {
133 autosize(document.querySelectorAll('textarea'));
136 componentDidUpdate(_lastProps: any, lastState: PostState, _snapshot: any) {
138 this.state.scrolled_comment_id &&
139 !this.state.scrolled &&
140 lastState.comments.length > 0
142 var elmnt = document.getElementById(
143 `comment-${this.state.scrolled_comment_id}`
145 elmnt.scrollIntoView();
146 elmnt.classList.add('mark');
147 this.state.scrolled = true;
148 this.markScrolledAsRead(this.state.scrolled_comment_id);
151 // Necessary if you are on a post and you click another post (same route)
152 if (_lastProps.location.pathname !== _lastProps.history.location.pathname) {
153 // Couldnt get a refresh working. This does for now.
156 // let currentId = this.props.match.params.id;
157 // WebSocketService.Instance.getPost(currentId);
158 // this.context.router.history.push('/sponsors');
159 // this.context.refresh();
160 // this.context.router.history.push(_lastProps.location.pathname);
164 markScrolledAsRead(commentId: number) {
165 let found = this.state.comments.find(c => c.id == commentId);
166 let parent = this.state.comments.find(c => found.parent_id == c.id);
167 let parent_user_id = parent
169 : this.state.post.creator_id;
172 UserService.Instance.user &&
173 UserService.Instance.user.id == parent_user_id
175 let form: MarkCommentAsReadForm = {
180 WebSocketService.Instance.markCommentAsRead(form);
181 UserService.Instance.unreadCountSub.next(
182 UserService.Instance.unreadCountSub.value - 1
187 get documentTitle(): string {
188 if (this.state.post) {
189 return `${this.state.post.name} - ${this.state.siteRes.site.name}`;
195 get favIcon(): string {
196 return this.state.siteRes.site.icon
197 ? this.state.siteRes.site.icon
203 <div class="container">
204 <Helmet title={this.documentTitle}>
212 {this.state.loading ? (
214 <svg class="icon icon-spinner spin">
215 <use xlinkHref="#icon-spinner"></use>
220 <div class="col-12 col-md-8 mb-3">
222 post={this.state.post}
225 moderators={this.state.moderators}
226 admins={this.state.siteRes.admins}
227 enableDownvotes={this.state.siteRes.site.enable_downvotes}
228 enableNsfw={this.state.siteRes.site.enable_nsfw}
230 <div className="mb-2" />
232 postId={this.state.post.id}
233 disabled={this.state.post.locked}
235 {this.state.comments.length > 0 && this.sortRadios()}
236 {this.state.commentViewType == CommentViewType.Tree &&
238 {this.state.commentViewType == CommentViewType.Chat &&
241 <div class="col-12 col-sm-12 col-md-4">{this.sidebar()}</div>
251 <div class="btn-group btn-group-toggle flex-wrap mr-3 mb-2">
253 className={`btn btn-outline-secondary pointer ${
254 this.state.commentSort === CommentSortType.Hot && 'active'
260 value={CommentSortType.Hot}
261 checked={this.state.commentSort === CommentSortType.Hot}
262 onChange={linkEvent(this, this.handleCommentSortChange)}
266 className={`btn btn-outline-secondary pointer ${
267 this.state.commentSort === CommentSortType.Top && 'active'
273 value={CommentSortType.Top}
274 checked={this.state.commentSort === CommentSortType.Top}
275 onChange={linkEvent(this, this.handleCommentSortChange)}
279 className={`btn btn-outline-secondary pointer ${
280 this.state.commentSort === CommentSortType.New && 'active'
286 value={CommentSortType.New}
287 checked={this.state.commentSort === CommentSortType.New}
288 onChange={linkEvent(this, this.handleCommentSortChange)}
292 className={`btn btn-outline-secondary pointer ${
293 this.state.commentSort === CommentSortType.Old && 'active'
299 value={CommentSortType.Old}
300 checked={this.state.commentSort === CommentSortType.Old}
301 onChange={linkEvent(this, this.handleCommentSortChange)}
305 <div class="btn-group btn-group-toggle flex-wrap mb-2">
307 className={`btn btn-outline-secondary pointer ${
308 this.state.commentViewType === CommentViewType.Chat && 'active'
314 value={CommentViewType.Chat}
315 checked={this.state.commentViewType === CommentViewType.Chat}
316 onChange={linkEvent(this, this.handleCommentViewTypeChange)}
328 nodes={commentsToFlatNodes(this.state.comments)}
330 locked={this.state.post.locked}
331 moderators={this.state.moderators}
332 admins={this.state.siteRes.admins}
333 postCreatorId={this.state.post.creator_id}
335 enableDownvotes={this.state.siteRes.site.enable_downvotes}
336 sort={this.state.commentSort}
346 community={this.state.community}
347 moderators={this.state.moderators}
348 admins={this.state.siteRes.admins}
349 online={this.state.online}
350 enableNsfw={this.state.siteRes.site.enable_nsfw}
357 handleCommentSortChange(i: Post, event: any) {
358 i.state.commentSort = Number(event.target.value);
359 i.state.commentViewType = CommentViewType.Tree;
363 handleCommentViewTypeChange(i: Post, event: any) {
364 i.state.commentViewType = Number(event.target.value);
365 i.state.commentSort = CommentSortType.New;
369 buildCommentsTree(): CommentNodeI[] {
370 let map = new Map<number, CommentNodeI>();
371 for (let comment of this.state.comments) {
372 let node: CommentNodeI = {
376 map.set(comment.id, { ...node });
378 let tree: CommentNodeI[] = [];
379 for (let comment of this.state.comments) {
380 let child = map.get(comment.id);
381 if (comment.parent_id) {
382 let parent_ = map.get(comment.parent_id);
383 parent_.children.push(child);
388 this.setDepth(child);
394 setDepth(node: CommentNodeI, i: number = 0): void {
395 for (let child of node.children) {
396 child.comment.depth = i;
397 this.setDepth(child, i + 1);
402 let nodes = this.buildCommentsTree();
407 locked={this.state.post.locked}
408 moderators={this.state.moderators}
409 admins={this.state.siteRes.admins}
410 postCreatorId={this.state.post.creator_id}
411 sort={this.state.commentSort}
412 enableDownvotes={this.state.siteRes.site.enable_downvotes}
418 parseMessage(msg: WebSocketJsonResponse) {
420 let res = wsJsonToRes(msg);
422 toast(i18n.t(msg.error), 'danger');
424 } else if (msg.reconnect) {
425 WebSocketService.Instance.getPost({
426 id: Number(this.props.match.params.id),
428 } else if (res.op == UserOperation.GetPost) {
429 let data = res.data as GetPostResponse;
430 this.state.post = data.post;
431 this.state.comments = data.comments;
432 this.state.community = data.community;
433 this.state.moderators = data.moderators;
434 this.state.online = data.online;
435 this.state.loading = false;
438 if (this.state.post.url) {
439 let form: SearchForm = {
440 q: this.state.post.url,
441 type_: SearchType.Url,
442 sort: SortType.TopAll,
446 WebSocketService.Instance.search(form);
449 this.setState(this.state);
451 } else if (res.op == UserOperation.CreateComment) {
452 let data = res.data as CommentResponse;
454 // Necessary since it might be a user reply
455 if (data.recipient_ids.length == 0) {
456 this.state.comments.unshift(data.comment);
457 this.setState(this.state);
460 res.op == UserOperation.EditComment ||
461 res.op == UserOperation.DeleteComment ||
462 res.op == UserOperation.RemoveComment
464 let data = res.data as CommentResponse;
465 editCommentRes(data, this.state.comments);
466 this.setState(this.state);
467 } else if (res.op == UserOperation.SaveComment) {
468 let data = res.data as CommentResponse;
469 saveCommentRes(data, this.state.comments);
470 this.setState(this.state);
472 } else if (res.op == UserOperation.CreateCommentLike) {
473 let data = res.data as CommentResponse;
474 createCommentLikeRes(data, this.state.comments);
475 this.setState(this.state);
476 } else if (res.op == UserOperation.CreatePostLike) {
477 let data = res.data as PostResponse;
478 createPostLikeRes(data, this.state.post);
479 this.setState(this.state);
481 res.op == UserOperation.EditPost ||
482 res.op == UserOperation.DeletePost ||
483 res.op == UserOperation.RemovePost ||
484 res.op == UserOperation.LockPost ||
485 res.op == UserOperation.StickyPost
487 let data = res.data as PostResponse;
488 this.state.post = data.post;
489 this.setState(this.state);
491 } else if (res.op == UserOperation.SavePost) {
492 let data = res.data as PostResponse;
493 this.state.post = data.post;
494 this.setState(this.state);
497 res.op == UserOperation.EditCommunity ||
498 res.op == UserOperation.DeleteCommunity ||
499 res.op == UserOperation.RemoveCommunity
501 let data = res.data as CommunityResponse;
502 this.state.community = data.community;
503 this.state.post.community_id = data.community.id;
504 this.state.post.community_name = data.community.name;
505 this.setState(this.state);
506 } else if (res.op == UserOperation.FollowCommunity) {
507 let data = res.data as CommunityResponse;
508 this.state.community.subscribed = data.community.subscribed;
509 this.state.community.number_of_subscribers =
510 data.community.number_of_subscribers;
511 this.setState(this.state);
512 } else if (res.op == UserOperation.BanFromCommunity) {
513 let data = res.data as BanFromCommunityResponse;
515 .filter(c => c.creator_id == data.user.id)
516 .forEach(c => (c.banned_from_community = data.banned));
517 if (this.state.post.creator_id == data.user.id) {
518 this.state.post.banned_from_community = data.banned;
520 this.setState(this.state);
521 } else if (res.op == UserOperation.AddModToCommunity) {
522 let data = res.data as AddModToCommunityResponse;
523 this.state.moderators = data.moderators;
524 this.setState(this.state);
525 } else if (res.op == UserOperation.BanUser) {
526 let data = res.data as BanUserResponse;
528 .filter(c => c.creator_id == data.user.id)
529 .forEach(c => (c.banned = data.banned));
530 if (this.state.post.creator_id == data.user.id) {
531 this.state.post.banned = data.banned;
533 this.setState(this.state);
534 } else if (res.op == UserOperation.AddAdmin) {
535 let data = res.data as AddAdminResponse;
536 this.state.siteRes.admins = data.admins;
537 this.setState(this.state);
538 } else if (res.op == UserOperation.Search) {
539 let data = res.data as SearchResponse;
540 this.state.crossPosts = data.posts.filter(
541 p => p.id != Number(this.props.match.params.id)
543 if (this.state.crossPosts.length) {
544 this.state.post.duplicates = this.state.crossPosts;
546 this.setState(this.state);
548 res.op == UserOperation.TransferSite ||
549 res.op == UserOperation.GetSite
551 let data = res.data as GetSiteResponse;
552 this.state.siteRes = data;
553 this.setState(this.state);
554 } else if (res.op == UserOperation.TransferCommunity) {
555 let data = res.data as GetCommunityResponse;
556 this.state.community = data.community;
557 this.state.moderators = data.moderators;
558 this.setState(this.state);