1 import { Component, linkEvent } from 'inferno';
2 import { HtmlTags } from './html-tags';
3 import { Subscription } from 'rxjs';
12 CommentNode as CommentNodeI,
13 BanFromCommunityResponse,
15 AddModToCommunityResponse,
24 WebSocketJsonResponse,
25 ListCategoriesResponse,
27 } from 'lemmy-js-client';
28 import { CommentSortType, CommentViewType } from '../interfaces';
29 import { WebSocketService, UserService } from '../services';
41 getCommentIdFromProps,
49 import { PostListing } from './post-listing';
50 import { Sidebar } from './sidebar';
51 import { CommentForm } from './comment-form';
52 import { CommentNodes } from './comment-nodes';
53 import autosize from 'autosize';
54 import { i18n } from '../i18next';
57 postRes: GetPostResponse;
60 commentSort: CommentSortType;
61 commentViewType: CommentViewType;
65 siteRes: GetSiteResponse;
66 categories: Category[];
69 export class Post extends Component<any, PostState> {
70 private subscription: Subscription;
71 private isoData = setIsoData(this.context);
72 private emptyState: PostState = {
74 postId: getIdFromProps(this.props),
75 commentId: getCommentIdFromProps(this.props),
76 commentSort: CommentSortType.Hot,
77 commentViewType: CommentViewType.Tree,
81 siteRes: this.isoData.site,
85 constructor(props: any, context: any) {
86 super(props, context);
88 this.state = this.emptyState;
90 this.parseMessage = this.parseMessage.bind(this);
91 this.subscription = wsSubscribe(this.parseMessage);
93 // Only fetch the data if coming from another route
94 if (this.isoData.path == this.context.router.route.match.url) {
95 this.state.postRes = this.isoData.routeData[0];
96 this.state.categories = this.isoData.routeData[1].categories;
97 this.state.loading = false;
99 if (isBrowser() && this.state.commentId) {
100 this.scrollCommentIntoView();
104 WebSocketService.Instance.listCategories();
109 let form: GetPostForm = {
110 id: this.state.postId,
112 WebSocketService.Instance.getPost(form);
115 static fetchInitialData(auth: string, path: string): Promise<any>[] {
116 let pathSplit = path.split('/');
117 let promises: Promise<any>[] = [];
119 let id = Number(pathSplit[2]);
121 let postForm: GetPostForm = {
124 setAuth(postForm, auth);
126 promises.push(lemmyHttp.getPost(postForm));
127 promises.push(lemmyHttp.listCategories());
132 componentWillUnmount() {
133 this.subscription.unsubscribe();
136 componentDidMount() {
137 WebSocketService.Instance.postJoin({ post_id: this.state.postId });
138 autosize(document.querySelectorAll('textarea'));
141 componentDidUpdate(_lastProps: any, lastState: PostState, _snapshot: any) {
143 this.state.commentId &&
144 !this.state.scrolled &&
146 lastState.postRes.comments.length > 0
148 this.scrollCommentIntoView();
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 // TODO 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.refresh();
159 // this.context.router.history.push(_lastProps.location.pathname);
163 scrollCommentIntoView() {
164 var elmnt = document.getElementById(`comment-${this.state.commentId}`);
165 elmnt.scrollIntoView();
166 elmnt.classList.add('mark');
167 this.state.scrolled = true;
168 this.markScrolledAsRead(this.state.commentId);
171 markScrolledAsRead(commentId: number) {
172 let found = this.state.postRes.comments.find(c => c.id == commentId);
173 let parent = this.state.postRes.comments.find(c => found.parent_id == c.id);
174 let parent_user_id = parent
176 : this.state.postRes.post.creator_id;
179 UserService.Instance.user &&
180 UserService.Instance.user.id == parent_user_id
182 let form: MarkCommentAsReadForm = {
187 WebSocketService.Instance.markCommentAsRead(form);
188 UserService.Instance.unreadCountSub.next(
189 UserService.Instance.unreadCountSub.value - 1
194 get documentTitle(): string {
195 return `${this.state.postRes.post.name} - ${this.state.siteRes.site.name}`;
198 get imageTag(): string {
200 this.state.postRes.post.thumbnail_url ||
201 (this.state.postRes.post.url
202 ? isImage(this.state.postRes.post.url)
203 ? this.state.postRes.post.url
209 get descriptionTag(): string {
210 return this.state.postRes.post.body
211 ? previewLines(this.state.postRes.post.body)
217 <div class="container">
218 {this.state.loading ? (
220 <svg class="icon icon-spinner spin">
221 <use xlinkHref="#icon-spinner"></use>
226 <div class="col-12 col-md-8 mb-3">
228 title={this.documentTitle}
229 path={this.context.router.route.match.url}
230 image={this.imageTag}
231 description={this.descriptionTag}
234 post={this.state.postRes.post}
237 moderators={this.state.postRes.moderators}
238 admins={this.state.siteRes.admins}
239 enableDownvotes={this.state.siteRes.site.enable_downvotes}
240 enableNsfw={this.state.siteRes.site.enable_nsfw}
242 <div className="mb-2" />
244 postId={this.state.postId}
245 disabled={this.state.postRes.post.locked}
247 {this.state.postRes.comments.length > 0 && this.sortRadios()}
248 {this.state.commentViewType == CommentViewType.Tree &&
250 {this.state.commentViewType == CommentViewType.Chat &&
253 <div class="col-12 col-sm-12 col-md-4">{this.sidebar()}</div>
263 <div class="btn-group btn-group-toggle flex-wrap mr-3 mb-2">
265 className={`btn btn-outline-secondary pointer ${
266 this.state.commentSort === CommentSortType.Hot && 'active'
272 value={CommentSortType.Hot}
273 checked={this.state.commentSort === CommentSortType.Hot}
274 onChange={linkEvent(this, this.handleCommentSortChange)}
278 className={`btn btn-outline-secondary pointer ${
279 this.state.commentSort === CommentSortType.Top && 'active'
285 value={CommentSortType.Top}
286 checked={this.state.commentSort === CommentSortType.Top}
287 onChange={linkEvent(this, this.handleCommentSortChange)}
291 className={`btn btn-outline-secondary pointer ${
292 this.state.commentSort === CommentSortType.New && 'active'
298 value={CommentSortType.New}
299 checked={this.state.commentSort === CommentSortType.New}
300 onChange={linkEvent(this, this.handleCommentSortChange)}
304 className={`btn btn-outline-secondary pointer ${
305 this.state.commentSort === CommentSortType.Old && 'active'
311 value={CommentSortType.Old}
312 checked={this.state.commentSort === CommentSortType.Old}
313 onChange={linkEvent(this, this.handleCommentSortChange)}
317 <div class="btn-group btn-group-toggle flex-wrap mb-2">
319 className={`btn btn-outline-secondary pointer ${
320 this.state.commentViewType === CommentViewType.Chat && 'active'
326 value={CommentViewType.Chat}
327 checked={this.state.commentViewType === CommentViewType.Chat}
328 onChange={linkEvent(this, this.handleCommentViewTypeChange)}
340 nodes={commentsToFlatNodes(this.state.postRes.comments)}
342 locked={this.state.postRes.post.locked}
343 moderators={this.state.postRes.moderators}
344 admins={this.state.siteRes.admins}
345 postCreatorId={this.state.postRes.post.creator_id}
347 enableDownvotes={this.state.siteRes.site.enable_downvotes}
348 sort={this.state.commentSort}
358 community={this.state.postRes.community}
359 moderators={this.state.postRes.moderators}
360 admins={this.state.siteRes.admins}
361 online={this.state.postRes.online}
362 enableNsfw={this.state.siteRes.site.enable_nsfw}
364 categories={this.state.categories}
370 handleCommentSortChange(i: Post, event: any) {
371 i.state.commentSort = Number(event.target.value);
372 i.state.commentViewType = CommentViewType.Tree;
376 handleCommentViewTypeChange(i: Post, event: any) {
377 i.state.commentViewType = Number(event.target.value);
378 i.state.commentSort = CommentSortType.New;
382 buildCommentsTree(): CommentNodeI[] {
383 let map = new Map<number, CommentNodeI>();
384 for (let comment of this.state.postRes.comments) {
385 let node: CommentNodeI = {
389 map.set(comment.id, { ...node });
391 let tree: CommentNodeI[] = [];
392 for (let comment of this.state.postRes.comments) {
393 let child = map.get(comment.id);
394 if (comment.parent_id) {
395 let parent_ = map.get(comment.parent_id);
396 parent_.children.push(child);
401 this.setDepth(child);
407 setDepth(node: CommentNodeI, i: number = 0): void {
408 for (let child of node.children) {
409 child.comment.depth = i;
410 this.setDepth(child, i + 1);
415 let nodes = this.buildCommentsTree();
420 locked={this.state.postRes.post.locked}
421 moderators={this.state.postRes.moderators}
422 admins={this.state.siteRes.admins}
423 postCreatorId={this.state.postRes.post.creator_id}
424 sort={this.state.commentSort}
425 enableDownvotes={this.state.siteRes.site.enable_downvotes}
431 parseMessage(msg: WebSocketJsonResponse) {
433 let res = wsJsonToRes(msg);
435 toast(i18n.t(msg.error), 'danger');
437 } else if (msg.reconnect) {
438 let postId = Number(this.props.match.params.id);
439 WebSocketService.Instance.postJoin({ post_id: postId });
440 WebSocketService.Instance.getPost({
443 } else if (res.op == UserOperation.GetPost) {
444 let data = res.data as GetPostResponse;
445 this.state.postRes = data;
446 this.state.loading = false;
449 if (this.state.postRes.post.url) {
450 let form: SearchForm = {
451 q: this.state.postRes.post.url,
452 type_: SearchType.Url,
453 sort: SortType.TopAll,
457 WebSocketService.Instance.search(form);
460 this.setState(this.state);
462 } else if (res.op == UserOperation.CreateComment) {
463 let data = res.data as CommentResponse;
465 // Necessary since it might be a user reply
466 if (data.recipient_ids.length == 0) {
467 this.state.postRes.comments.unshift(data.comment);
468 this.setState(this.state);
471 res.op == UserOperation.EditComment ||
472 res.op == UserOperation.DeleteComment ||
473 res.op == UserOperation.RemoveComment
475 let data = res.data as CommentResponse;
476 editCommentRes(data, this.state.postRes.comments);
477 this.setState(this.state);
478 } else if (res.op == UserOperation.SaveComment) {
479 let data = res.data as CommentResponse;
480 saveCommentRes(data, this.state.postRes.comments);
481 this.setState(this.state);
483 } else if (res.op == UserOperation.CreateCommentLike) {
484 let data = res.data as CommentResponse;
485 createCommentLikeRes(data, this.state.postRes.comments);
486 this.setState(this.state);
487 } else if (res.op == UserOperation.CreatePostLike) {
488 let data = res.data as PostResponse;
489 createPostLikeRes(data, this.state.postRes.post);
490 this.setState(this.state);
492 res.op == UserOperation.EditPost ||
493 res.op == UserOperation.DeletePost ||
494 res.op == UserOperation.RemovePost ||
495 res.op == UserOperation.LockPost ||
496 res.op == UserOperation.StickyPost
498 let data = res.data as PostResponse;
499 this.state.postRes.post = data.post;
500 this.setState(this.state);
502 } else if (res.op == UserOperation.SavePost) {
503 let data = res.data as PostResponse;
504 this.state.postRes.post = data.post;
505 this.setState(this.state);
508 res.op == UserOperation.EditCommunity ||
509 res.op == UserOperation.DeleteCommunity ||
510 res.op == UserOperation.RemoveCommunity
512 let data = res.data as CommunityResponse;
513 this.state.postRes.community = data.community;
514 this.state.postRes.post.community_id = data.community.id;
515 this.state.postRes.post.community_name = data.community.name;
516 this.setState(this.state);
517 } else if (res.op == UserOperation.FollowCommunity) {
518 let data = res.data as CommunityResponse;
519 this.state.postRes.community.subscribed = data.community.subscribed;
520 this.state.postRes.community.number_of_subscribers =
521 data.community.number_of_subscribers;
522 this.setState(this.state);
523 } else if (res.op == UserOperation.BanFromCommunity) {
524 let data = res.data as BanFromCommunityResponse;
525 this.state.postRes.comments
526 .filter(c => c.creator_id == data.user.id)
527 .forEach(c => (c.banned_from_community = data.banned));
528 if (this.state.postRes.post.creator_id == data.user.id) {
529 this.state.postRes.post.banned_from_community = data.banned;
531 this.setState(this.state);
532 } else if (res.op == UserOperation.AddModToCommunity) {
533 let data = res.data as AddModToCommunityResponse;
534 this.state.postRes.moderators = data.moderators;
535 this.setState(this.state);
536 } else if (res.op == UserOperation.BanUser) {
537 let data = res.data as BanUserResponse;
538 this.state.postRes.comments
539 .filter(c => c.creator_id == data.user.id)
540 .forEach(c => (c.banned = data.banned));
541 if (this.state.postRes.post.creator_id == data.user.id) {
542 this.state.postRes.post.banned = data.banned;
544 this.setState(this.state);
545 } else if (res.op == UserOperation.AddAdmin) {
546 let data = res.data as AddAdminResponse;
547 this.state.siteRes.admins = data.admins;
548 this.setState(this.state);
549 } else if (res.op == UserOperation.Search) {
550 let data = res.data as SearchResponse;
551 this.state.crossPosts = data.posts.filter(
552 p => p.id != Number(this.props.match.params.id)
554 if (this.state.crossPosts.length) {
555 this.state.postRes.post.duplicates = this.state.crossPosts;
557 this.setState(this.state);
558 } else if (res.op == UserOperation.TransferSite) {
559 let data = res.data as GetSiteResponse;
560 this.state.siteRes = data;
561 this.setState(this.state);
562 } else if (res.op == UserOperation.TransferCommunity) {
563 let data = res.data as GetCommunityResponse;
564 this.state.postRes.community = data.community;
565 this.state.postRes.moderators = data.moderators;
566 this.setState(this.state);
567 } else if (res.op == UserOperation.ListCategories) {
568 let data = res.data as ListCategoriesResponse;
569 this.state.categories = data.categories;
570 this.setState(this.state);