1 import { Component, linkEvent } from 'inferno';
2 import { HtmlTags } from './html-tags';
3 import { Subscription } from 'rxjs';
12 BanFromCommunityResponse,
14 AddModToCommunityResponse,
23 ListCategoriesResponse,
25 } from 'lemmy-js-client';
30 CommentNode as CommentNodeI,
31 } from '../interfaces';
32 import { WebSocketService, UserService } from '../services';
44 getCommentIdFromProps,
51 import { PostListing } from './post-listing';
52 import { Sidebar } from './sidebar';
53 import { CommentForm } from './comment-form';
54 import { CommentNodes } from './comment-nodes';
55 import autosize from 'autosize';
56 import { i18n } from '../i18next';
59 postRes: GetPostResponse;
62 commentSort: CommentSortType;
63 commentViewType: CommentViewType;
66 crossPosts: PostView[];
67 siteRes: GetSiteResponse;
68 categories: Category[];
71 export class Post extends Component<any, PostState> {
72 private subscription: Subscription;
73 private isoData = setIsoData(this.context);
74 private emptyState: PostState = {
76 postId: getIdFromProps(this.props),
77 commentId: getCommentIdFromProps(this.props),
78 commentSort: CommentSortType.Hot,
79 commentViewType: CommentViewType.Tree,
83 siteRes: this.isoData.site_res,
87 constructor(props: any, context: any) {
88 super(props, context);
90 this.state = this.emptyState;
92 this.parseMessage = this.parseMessage.bind(this);
93 this.subscription = wsSubscribe(this.parseMessage);
95 // Only fetch the data if coming from another route
96 if (this.isoData.path == this.context.router.route.match.url) {
97 this.state.postRes = this.isoData.routeData[0];
98 this.state.categories = this.isoData.routeData[1].categories;
99 this.state.loading = false;
101 if (isBrowser() && this.state.commentId) {
102 this.scrollCommentIntoView();
106 WebSocketService.Instance.client.listCategories();
111 let form: GetPost = {
112 id: this.state.postId,
113 auth: UserService.Instance.authField(false),
115 WebSocketService.Instance.client.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: GetPost = {
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.client.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 // TODO this needs some re-work
176 markScrolledAsRead(commentId: number) {
177 let found = this.state.postRes.comments.find(
178 c => c.comment.id == commentId
180 let parent = this.state.postRes.comments.find(
181 c => found.comment.parent_id == c.comment.id
183 let parent_user_id = parent
185 : this.state.postRes.post_view.creator.id;
188 UserService.Instance.user &&
189 UserService.Instance.user.id == parent_user_id
191 let form: MarkCommentAsRead = {
192 comment_id: found.creator.id,
194 auth: UserService.Instance.authField(),
196 WebSocketService.Instance.client.markCommentAsRead(form);
197 UserService.Instance.unreadCountSub.next(
198 UserService.Instance.unreadCountSub.value - 1
203 get documentTitle(): string {
204 return `${this.state.postRes.post_view.post.name} - ${this.state.siteRes.site_view.site.name}`;
207 get imageTag(): string {
208 let post = this.state.postRes.post_view.post;
210 post.thumbnail_url ||
211 (post.url ? (isImage(post.url) ? post.url : undefined) : undefined)
215 get descriptionTag(): string {
216 let body = this.state.postRes.post_view.post.body;
217 return body ? previewLines(body) : undefined;
221 let pv = this.state.postRes.post_view;
223 <div class="container">
224 {this.state.loading ? (
226 <svg class="icon icon-spinner spin">
227 <use xlinkHref="#icon-spinner"></use>
232 <div class="col-12 col-md-8 mb-3">
234 title={this.documentTitle}
235 path={this.context.router.route.match.url}
236 image={this.imageTag}
237 description={this.descriptionTag}
241 duplicates={this.state.crossPosts}
244 moderators={this.state.postRes.moderators}
245 admins={this.state.siteRes.admins}
247 this.state.siteRes.site_view.site.enable_downvotes
249 enableNsfw={this.state.siteRes.site_view.site.enable_nsfw}
251 <div className="mb-2" />
253 postId={this.state.postId}
254 disabled={pv.post.locked}
256 {this.state.postRes.comments.length > 0 && this.sortRadios()}
257 {this.state.commentViewType == CommentViewType.Tree &&
259 {this.state.commentViewType == CommentViewType.Chat &&
262 <div class="col-12 col-sm-12 col-md-4">{this.sidebar()}</div>
272 <div class="btn-group btn-group-toggle flex-wrap mr-3 mb-2">
274 className={`btn btn-outline-secondary pointer ${
275 this.state.commentSort === CommentSortType.Hot && 'active'
281 value={CommentSortType.Hot}
282 checked={this.state.commentSort === CommentSortType.Hot}
283 onChange={linkEvent(this, this.handleCommentSortChange)}
287 className={`btn btn-outline-secondary pointer ${
288 this.state.commentSort === CommentSortType.Top && 'active'
294 value={CommentSortType.Top}
295 checked={this.state.commentSort === CommentSortType.Top}
296 onChange={linkEvent(this, this.handleCommentSortChange)}
300 className={`btn btn-outline-secondary pointer ${
301 this.state.commentSort === CommentSortType.New && 'active'
307 value={CommentSortType.New}
308 checked={this.state.commentSort === CommentSortType.New}
309 onChange={linkEvent(this, this.handleCommentSortChange)}
313 className={`btn btn-outline-secondary pointer ${
314 this.state.commentSort === CommentSortType.Old && 'active'
320 value={CommentSortType.Old}
321 checked={this.state.commentSort === CommentSortType.Old}
322 onChange={linkEvent(this, this.handleCommentSortChange)}
326 <div class="btn-group btn-group-toggle flex-wrap mb-2">
328 className={`btn btn-outline-secondary pointer ${
329 this.state.commentViewType === CommentViewType.Chat && 'active'
335 value={CommentViewType.Chat}
336 checked={this.state.commentViewType === CommentViewType.Chat}
337 onChange={linkEvent(this, this.handleCommentViewTypeChange)}
349 nodes={commentsToFlatNodes(this.state.postRes.comments)}
351 locked={this.state.postRes.post_view.post.locked}
352 moderators={this.state.postRes.moderators}
353 admins={this.state.siteRes.admins}
354 postCreatorId={this.state.postRes.post_view.creator.id}
356 enableDownvotes={this.state.siteRes.site_view.site.enable_downvotes}
357 sort={this.state.commentSort}
367 community_view={this.state.postRes.community_view}
368 moderators={this.state.postRes.moderators}
369 admins={this.state.siteRes.admins}
370 online={this.state.postRes.online}
371 enableNsfw={this.state.siteRes.site_view.site.enable_nsfw}
373 categories={this.state.categories}
379 handleCommentSortChange(i: Post, event: any) {
380 i.state.commentSort = Number(event.target.value);
381 i.state.commentViewType = CommentViewType.Tree;
385 handleCommentViewTypeChange(i: Post, event: any) {
386 i.state.commentViewType = Number(event.target.value);
387 i.state.commentSort = CommentSortType.New;
391 buildCommentsTree(): CommentNodeI[] {
392 let map = new Map<number, CommentNodeI>();
393 for (let comment_view of this.state.postRes.comments) {
394 let node: CommentNodeI = {
395 comment_view: comment_view,
398 map.set(comment_view.comment.id, { ...node });
400 let tree: CommentNodeI[] = [];
401 for (let comment_view of this.state.postRes.comments) {
402 let child = map.get(comment_view.comment.id);
403 if (comment_view.comment.parent_id) {
404 let parent_ = map.get(comment_view.comment.parent_id);
405 parent_.children.push(child);
410 this.setDepth(child);
416 setDepth(node: CommentNodeI, i: number = 0): void {
417 for (let child of node.children) {
419 this.setDepth(child, i + 1);
424 let nodes = this.buildCommentsTree();
429 locked={this.state.postRes.post_view.post.locked}
430 moderators={this.state.postRes.moderators}
431 admins={this.state.siteRes.admins}
432 postCreatorId={this.state.postRes.post_view.creator.id}
433 sort={this.state.commentSort}
434 enableDownvotes={this.state.siteRes.site_view.site.enable_downvotes}
440 parseMessage(msg: any) {
441 let op = wsUserOp(msg);
443 toast(i18n.t(msg.error), 'danger');
445 } else if (msg.reconnect) {
446 let postId = Number(this.props.match.params.id);
447 WebSocketService.Instance.client.postJoin({ post_id: postId });
448 WebSocketService.Instance.client.getPost({
450 auth: UserService.Instance.authField(false),
452 } else if (op == UserOperation.GetPost) {
453 let data = wsJsonToRes<GetPostResponse>(msg).data;
454 this.state.postRes = data;
455 this.state.loading = false;
458 if (this.state.postRes.post_view.post.url) {
460 q: this.state.postRes.post_view.post.url,
461 type_: SearchType.Url,
462 sort: SortType.TopAll,
465 auth: UserService.Instance.authField(false),
467 WebSocketService.Instance.client.search(form);
470 this.setState(this.state);
472 } else if (op == UserOperation.CreateComment) {
473 let data = wsJsonToRes<CommentResponse>(msg).data;
475 // Necessary since it might be a user reply
476 if (data.recipient_ids.length == 0) {
477 this.state.postRes.comments.unshift(data.comment_view);
478 this.setState(this.state);
481 op == UserOperation.EditComment ||
482 op == UserOperation.DeleteComment ||
483 op == UserOperation.RemoveComment
485 let data = wsJsonToRes<CommentResponse>(msg).data;
486 editCommentRes(data.comment_view, this.state.postRes.comments);
487 this.setState(this.state);
488 } else if (op == UserOperation.SaveComment) {
489 let data = wsJsonToRes<CommentResponse>(msg).data;
490 saveCommentRes(data.comment_view, this.state.postRes.comments);
491 this.setState(this.state);
493 } else if (op == UserOperation.CreateCommentLike) {
494 let data = wsJsonToRes<CommentResponse>(msg).data;
495 createCommentLikeRes(data.comment_view, this.state.postRes.comments);
496 this.setState(this.state);
497 } else if (op == UserOperation.CreatePostLike) {
498 let data = wsJsonToRes<PostResponse>(msg).data;
499 createPostLikeRes(data.post_view, this.state.postRes.post_view);
500 this.setState(this.state);
502 op == UserOperation.EditPost ||
503 op == UserOperation.DeletePost ||
504 op == UserOperation.RemovePost ||
505 op == UserOperation.LockPost ||
506 op == UserOperation.StickyPost ||
507 op == UserOperation.SavePost
509 let data = wsJsonToRes<PostResponse>(msg).data;
510 this.state.postRes.post_view = data.post_view;
511 this.setState(this.state);
514 op == UserOperation.EditCommunity ||
515 op == UserOperation.DeleteCommunity ||
516 op == UserOperation.RemoveCommunity ||
517 op == UserOperation.FollowCommunity
519 let data = wsJsonToRes<CommunityResponse>(msg).data;
520 this.state.postRes.community_view = data.community_view;
521 this.state.postRes.post_view.community = data.community_view.community;
522 this.setState(this.state);
523 this.setState(this.state);
524 } else if (op == UserOperation.BanFromCommunity) {
525 let data = wsJsonToRes<BanFromCommunityResponse>(msg).data;
526 this.state.postRes.comments
527 .filter(c => c.creator.id == data.user_view.user.id)
528 .forEach(c => (c.creator_banned_from_community = data.banned));
529 if (this.state.postRes.post_view.creator.id == data.user_view.user.id) {
530 this.state.postRes.post_view.creator_banned_from_community =
533 this.setState(this.state);
534 } else if (op == UserOperation.AddModToCommunity) {
535 let data = wsJsonToRes<AddModToCommunityResponse>(msg).data;
536 this.state.postRes.moderators = data.moderators;
537 this.setState(this.state);
538 } else if (op == UserOperation.BanUser) {
539 let data = wsJsonToRes<BanUserResponse>(msg).data;
540 this.state.postRes.comments
541 .filter(c => c.creator.id == data.user_view.user.id)
542 .forEach(c => (c.creator.banned = data.banned));
543 if (this.state.postRes.post_view.creator.id == data.user_view.user.id) {
544 this.state.postRes.post_view.creator.banned = data.banned;
546 this.setState(this.state);
547 } else if (op == UserOperation.AddAdmin) {
548 let data = wsJsonToRes<AddAdminResponse>(msg).data;
549 this.state.siteRes.admins = data.admins;
550 this.setState(this.state);
551 } else if (op == UserOperation.Search) {
552 let data = wsJsonToRes<SearchResponse>(msg).data;
553 this.state.crossPosts = data.posts.filter(
554 p => p.post.id != Number(this.props.match.params.id)
556 this.setState(this.state);
557 } else if (op == UserOperation.TransferSite) {
558 let data = wsJsonToRes<GetSiteResponse>(msg).data;
559 this.state.siteRes = data;
560 this.setState(this.state);
561 } else if (op == UserOperation.TransferCommunity) {
562 let data = wsJsonToRes<GetCommunityResponse>(msg).data;
563 this.state.postRes.community_view = data.community_view;
564 this.state.postRes.post_view.community = data.community_view.community;
565 this.state.postRes.moderators = data.moderators;
566 this.setState(this.state);
567 } else if (op == UserOperation.ListCategories) {
568 let data = wsJsonToRes<ListCategoriesResponse>(msg).data;
569 this.state.categories = data.categories;
570 this.setState(this.state);