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';
32 } from '../interfaces';
33 import { WebSocketService, UserService } from '../services';
45 getCommentIdFromProps,
52 import { PostListing } from './post-listing';
53 import { Sidebar } from './sidebar';
54 import { CommentForm } from './comment-form';
55 import { CommentNodes } from './comment-nodes';
56 import autosize from 'autosize';
57 import { i18n } from '../i18next';
60 postRes: GetPostResponse;
63 commentSort: CommentSortType;
64 commentViewType: CommentViewType;
68 siteRes: GetSiteResponse;
69 categories: Category[];
72 export class Post extends Component<any, PostState> {
73 private subscription: Subscription;
74 private isoData = setIsoData(this.context);
75 private emptyState: PostState = {
77 postId: getIdFromProps(this.props),
78 commentId: getCommentIdFromProps(this.props),
79 commentSort: CommentSortType.Hot,
80 commentViewType: CommentViewType.Tree,
84 siteRes: this.isoData.site,
88 constructor(props: any, context: any) {
89 super(props, context);
91 this.state = this.emptyState;
93 this.parseMessage = this.parseMessage.bind(this);
94 this.subscription = wsSubscribe(this.parseMessage);
96 // Only fetch the data if coming from another route
97 if (this.isoData.path == this.context.router.route.match.url) {
98 this.state.postRes = this.isoData.routeData[0];
99 this.state.categories = this.isoData.routeData[1].categories;
100 this.state.loading = false;
102 if (isBrowser() && this.state.commentId) {
103 this.scrollCommentIntoView();
107 WebSocketService.Instance.listCategories();
112 let form: GetPostForm = {
113 id: this.state.postId,
115 WebSocketService.Instance.getPost(form);
118 static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
119 let pathSplit = req.path.split('/');
120 let promises: Promise<any>[] = [];
122 let id = Number(pathSplit[2]);
124 let postForm: GetPostForm = {
127 setAuth(postForm, req.auth);
129 promises.push(req.client.getPost(postForm));
130 promises.push(req.client.listCategories());
135 componentWillUnmount() {
136 this.subscription.unsubscribe();
137 window.isoData.path = undefined;
140 componentDidMount() {
141 WebSocketService.Instance.postJoin({ post_id: this.state.postId });
142 autosize(document.querySelectorAll('textarea'));
145 componentDidUpdate(_lastProps: any, lastState: PostState, _snapshot: any) {
147 this.state.commentId &&
148 !this.state.scrolled &&
150 lastState.postRes.comments.length > 0
152 this.scrollCommentIntoView();
155 // Necessary if you are on a post and you click another post (same route)
156 if (_lastProps.location.pathname !== _lastProps.history.location.pathname) {
157 // TODO Couldnt get a refresh working. This does for now.
160 // let currentId = this.props.match.params.id;
161 // WebSocketService.Instance.getPost(currentId);
162 // this.context.refresh();
163 // this.context.router.history.push(_lastProps.location.pathname);
167 scrollCommentIntoView() {
168 var elmnt = document.getElementById(`comment-${this.state.commentId}`);
169 elmnt.scrollIntoView();
170 elmnt.classList.add('mark');
171 this.state.scrolled = true;
172 this.markScrolledAsRead(this.state.commentId);
175 markScrolledAsRead(commentId: number) {
176 let found = this.state.postRes.comments.find(c => c.id == commentId);
177 let parent = this.state.postRes.comments.find(c => found.parent_id == c.id);
178 let parent_user_id = parent
180 : this.state.postRes.post.creator_id;
183 UserService.Instance.user &&
184 UserService.Instance.user.id == parent_user_id
186 let form: MarkCommentAsReadForm = {
191 WebSocketService.Instance.markCommentAsRead(form);
192 UserService.Instance.unreadCountSub.next(
193 UserService.Instance.unreadCountSub.value - 1
198 get documentTitle(): string {
199 return `${this.state.postRes.post.name} - ${this.state.siteRes.site.name}`;
202 get imageTag(): string {
204 this.state.postRes.post.thumbnail_url ||
205 (this.state.postRes.post.url
206 ? isImage(this.state.postRes.post.url)
207 ? this.state.postRes.post.url
213 get descriptionTag(): string {
214 return this.state.postRes.post.body
215 ? previewLines(this.state.postRes.post.body)
221 <div class="container">
222 {this.state.loading ? (
224 <svg class="icon icon-spinner spin">
225 <use xlinkHref="#icon-spinner"></use>
230 <div class="col-12 col-md-8 mb-3">
232 title={this.documentTitle}
233 path={this.context.router.route.match.url}
234 image={this.imageTag}
235 description={this.descriptionTag}
238 post={this.state.postRes.post}
241 moderators={this.state.postRes.moderators}
242 admins={this.state.siteRes.admins}
243 enableDownvotes={this.state.siteRes.site.enable_downvotes}
244 enableNsfw={this.state.siteRes.site.enable_nsfw}
246 <div className="mb-2" />
248 postId={this.state.postId}
249 disabled={this.state.postRes.post.locked}
251 {this.state.postRes.comments.length > 0 && this.sortRadios()}
252 {this.state.commentViewType == CommentViewType.Tree &&
254 {this.state.commentViewType == CommentViewType.Chat &&
257 <div class="col-12 col-sm-12 col-md-4">{this.sidebar()}</div>
267 <div class="btn-group btn-group-toggle flex-wrap mr-3 mb-2">
269 className={`btn btn-outline-secondary pointer ${
270 this.state.commentSort === CommentSortType.Hot && 'active'
276 value={CommentSortType.Hot}
277 checked={this.state.commentSort === CommentSortType.Hot}
278 onChange={linkEvent(this, this.handleCommentSortChange)}
282 className={`btn btn-outline-secondary pointer ${
283 this.state.commentSort === CommentSortType.Top && 'active'
289 value={CommentSortType.Top}
290 checked={this.state.commentSort === CommentSortType.Top}
291 onChange={linkEvent(this, this.handleCommentSortChange)}
295 className={`btn btn-outline-secondary pointer ${
296 this.state.commentSort === CommentSortType.New && 'active'
302 value={CommentSortType.New}
303 checked={this.state.commentSort === CommentSortType.New}
304 onChange={linkEvent(this, this.handleCommentSortChange)}
308 className={`btn btn-outline-secondary pointer ${
309 this.state.commentSort === CommentSortType.Old && 'active'
315 value={CommentSortType.Old}
316 checked={this.state.commentSort === CommentSortType.Old}
317 onChange={linkEvent(this, this.handleCommentSortChange)}
321 <div class="btn-group btn-group-toggle flex-wrap mb-2">
323 className={`btn btn-outline-secondary pointer ${
324 this.state.commentViewType === CommentViewType.Chat && 'active'
330 value={CommentViewType.Chat}
331 checked={this.state.commentViewType === CommentViewType.Chat}
332 onChange={linkEvent(this, this.handleCommentViewTypeChange)}
344 nodes={commentsToFlatNodes(this.state.postRes.comments)}
346 locked={this.state.postRes.post.locked}
347 moderators={this.state.postRes.moderators}
348 admins={this.state.siteRes.admins}
349 postCreatorId={this.state.postRes.post.creator_id}
351 enableDownvotes={this.state.siteRes.site.enable_downvotes}
352 sort={this.state.commentSort}
362 community={this.state.postRes.community}
363 moderators={this.state.postRes.moderators}
364 admins={this.state.siteRes.admins}
365 online={this.state.postRes.online}
366 enableNsfw={this.state.siteRes.site.enable_nsfw}
368 categories={this.state.categories}
374 handleCommentSortChange(i: Post, event: any) {
375 i.state.commentSort = Number(event.target.value);
376 i.state.commentViewType = CommentViewType.Tree;
380 handleCommentViewTypeChange(i: Post, event: any) {
381 i.state.commentViewType = Number(event.target.value);
382 i.state.commentSort = CommentSortType.New;
386 buildCommentsTree(): CommentNodeI[] {
387 let map = new Map<number, CommentNodeI>();
388 for (let comment of this.state.postRes.comments) {
389 let node: CommentNodeI = {
393 map.set(comment.id, { ...node });
395 let tree: CommentNodeI[] = [];
396 for (let comment of this.state.postRes.comments) {
397 let child = map.get(comment.id);
398 if (comment.parent_id) {
399 let parent_ = map.get(comment.parent_id);
400 parent_.children.push(child);
405 this.setDepth(child);
411 setDepth(node: CommentNodeI, i: number = 0): void {
412 for (let child of node.children) {
413 child.comment.depth = i;
414 this.setDepth(child, i + 1);
419 let nodes = this.buildCommentsTree();
424 locked={this.state.postRes.post.locked}
425 moderators={this.state.postRes.moderators}
426 admins={this.state.siteRes.admins}
427 postCreatorId={this.state.postRes.post.creator_id}
428 sort={this.state.commentSort}
429 enableDownvotes={this.state.siteRes.site.enable_downvotes}
435 parseMessage(msg: WebSocketJsonResponse) {
437 let res = wsJsonToRes(msg);
439 toast(i18n.t(msg.error), 'danger');
441 } else if (msg.reconnect) {
442 let postId = Number(this.props.match.params.id);
443 WebSocketService.Instance.postJoin({ post_id: postId });
444 WebSocketService.Instance.getPost({
447 } else if (res.op == UserOperation.GetPost) {
448 let data = res.data as GetPostResponse;
449 this.state.postRes = data;
450 this.state.loading = false;
453 if (this.state.postRes.post.url) {
454 let form: SearchForm = {
455 q: this.state.postRes.post.url,
456 type_: SearchType.Url,
457 sort: SortType.TopAll,
461 WebSocketService.Instance.search(form);
464 this.setState(this.state);
466 } else if (res.op == UserOperation.CreateComment) {
467 let data = res.data as CommentResponse;
469 // Necessary since it might be a user reply
470 if (data.recipient_ids.length == 0) {
471 this.state.postRes.comments.unshift(data.comment);
472 this.setState(this.state);
475 res.op == UserOperation.EditComment ||
476 res.op == UserOperation.DeleteComment ||
477 res.op == UserOperation.RemoveComment
479 let data = res.data as CommentResponse;
480 editCommentRes(data, this.state.postRes.comments);
481 this.setState(this.state);
482 } else if (res.op == UserOperation.SaveComment) {
483 let data = res.data as CommentResponse;
484 saveCommentRes(data, this.state.postRes.comments);
485 this.setState(this.state);
487 } else if (res.op == UserOperation.CreateCommentLike) {
488 let data = res.data as CommentResponse;
489 createCommentLikeRes(data, this.state.postRes.comments);
490 this.setState(this.state);
491 } else if (res.op == UserOperation.CreatePostLike) {
492 let data = res.data as PostResponse;
493 createPostLikeRes(data, this.state.postRes.post);
494 this.setState(this.state);
496 res.op == UserOperation.EditPost ||
497 res.op == UserOperation.DeletePost ||
498 res.op == UserOperation.RemovePost ||
499 res.op == UserOperation.LockPost ||
500 res.op == UserOperation.StickyPost
502 let data = res.data as PostResponse;
503 this.state.postRes.post = data.post;
504 this.setState(this.state);
506 } else if (res.op == UserOperation.SavePost) {
507 let data = res.data as PostResponse;
508 this.state.postRes.post = data.post;
509 this.setState(this.state);
512 res.op == UserOperation.EditCommunity ||
513 res.op == UserOperation.DeleteCommunity ||
514 res.op == UserOperation.RemoveCommunity
516 let data = res.data as CommunityResponse;
517 this.state.postRes.community = data.community;
518 this.state.postRes.post.community_id = data.community.id;
519 this.state.postRes.post.community_name = data.community.name;
520 this.setState(this.state);
521 } else if (res.op == UserOperation.FollowCommunity) {
522 let data = res.data as CommunityResponse;
523 this.state.postRes.community.subscribed = data.community.subscribed;
524 this.state.postRes.community.number_of_subscribers =
525 data.community.number_of_subscribers;
526 this.setState(this.state);
527 } else if (res.op == UserOperation.BanFromCommunity) {
528 let data = res.data as BanFromCommunityResponse;
529 this.state.postRes.comments
530 .filter(c => c.creator_id == data.user.id)
531 .forEach(c => (c.banned_from_community = data.banned));
532 if (this.state.postRes.post.creator_id == data.user.id) {
533 this.state.postRes.post.banned_from_community = data.banned;
535 this.setState(this.state);
536 } else if (res.op == UserOperation.AddModToCommunity) {
537 let data = res.data as AddModToCommunityResponse;
538 this.state.postRes.moderators = data.moderators;
539 this.setState(this.state);
540 } else if (res.op == UserOperation.BanUser) {
541 let data = res.data as BanUserResponse;
542 this.state.postRes.comments
543 .filter(c => c.creator_id == data.user.id)
544 .forEach(c => (c.banned = data.banned));
545 if (this.state.postRes.post.creator_id == data.user.id) {
546 this.state.postRes.post.banned = data.banned;
548 this.setState(this.state);
549 } else if (res.op == UserOperation.AddAdmin) {
550 let data = res.data as AddAdminResponse;
551 this.state.siteRes.admins = data.admins;
552 this.setState(this.state);
553 } else if (res.op == UserOperation.Search) {
554 let data = res.data as SearchResponse;
555 this.state.crossPosts = data.posts.filter(
556 p => p.id != Number(this.props.match.params.id)
558 if (this.state.crossPosts.length) {
559 this.state.postRes.post.duplicates = this.state.crossPosts;
561 this.setState(this.state);
562 } else if (res.op == UserOperation.TransferSite) {
563 let data = res.data as GetSiteResponse;
564 this.state.siteRes = data;
565 this.setState(this.state);
566 } else if (res.op == UserOperation.TransferCommunity) {
567 let data = res.data as GetCommunityResponse;
568 this.state.postRes.community = data.community;
569 this.state.postRes.moderators = data.moderators;
570 this.setState(this.state);
571 } else if (res.op == UserOperation.ListCategories) {
572 let data = res.data as ListCategoriesResponse;
573 this.state.categories = data.categories;
574 this.setState(this.state);