]> Untitled Git - lemmy.git/blob - ui/src/components/post.tsx
Remove extra jwt claims (for user settings) (#1025)
[lemmy.git] / ui / src / components / post.tsx
1 import { Component, linkEvent } from 'inferno';
2 import { Subscription } from 'rxjs';
3 import { retryWhen, delay, take } from 'rxjs/operators';
4 import {
5   UserOperation,
6   Community,
7   Post as PostI,
8   GetPostResponse,
9   PostResponse,
10   Comment,
11   MarkCommentAsReadForm,
12   CommentResponse,
13   CommentSortType,
14   CommentViewType,
15   CommunityUser,
16   CommunityResponse,
17   CommentNode as CommentNodeI,
18   BanFromCommunityResponse,
19   BanUserResponse,
20   AddModToCommunityResponse,
21   AddAdminResponse,
22   SearchType,
23   SortType,
24   SearchForm,
25   GetPostForm,
26   SearchResponse,
27   GetSiteResponse,
28   GetCommunityResponse,
29   WebSocketJsonResponse,
30 } from '../interfaces';
31 import { WebSocketService, UserService } from '../services';
32 import {
33   wsJsonToRes,
34   toast,
35   editCommentRes,
36   saveCommentRes,
37   createCommentLikeRes,
38   createPostLikeRes,
39   commentsToFlatNodes,
40   setupTippy,
41 } from '../utils';
42 import { PostListing } from './post-listing';
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';
48
49 interface PostState {
50   post: PostI;
51   comments: Array<Comment>;
52   commentSort: CommentSortType;
53   commentViewType: CommentViewType;
54   community: Community;
55   moderators: Array<CommunityUser>;
56   online: number;
57   scrolled?: boolean;
58   scrolled_comment_id?: number;
59   loading: boolean;
60   crossPosts: Array<PostI>;
61   siteRes: GetSiteResponse;
62 }
63
64 export class Post extends Component<any, PostState> {
65   private subscription: Subscription;
66   private emptyState: PostState = {
67     post: null,
68     comments: [],
69     commentSort: CommentSortType.Hot,
70     commentViewType: CommentViewType.Tree,
71     community: null,
72     moderators: [],
73     online: null,
74     scrolled: false,
75     loading: true,
76     crossPosts: [],
77     siteRes: {
78       admins: [],
79       banned: [],
80       site: {
81         id: undefined,
82         name: undefined,
83         creator_id: undefined,
84         published: undefined,
85         creator_name: undefined,
86         number_of_users: undefined,
87         number_of_posts: undefined,
88         number_of_comments: undefined,
89         number_of_communities: undefined,
90         enable_downvotes: undefined,
91         open_registration: undefined,
92         enable_nsfw: undefined,
93       },
94       online: null,
95       version: null,
96     },
97   };
98
99   constructor(props: any, context: any) {
100     super(props, context);
101
102     this.state = this.emptyState;
103
104     let postId = Number(this.props.match.params.id);
105     if (this.props.match.params.comment_id) {
106       this.state.scrolled_comment_id = this.props.match.params.comment_id;
107     }
108
109     this.subscription = WebSocketService.Instance.subject
110       .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
111       .subscribe(
112         msg => this.parseMessage(msg),
113         err => console.error(err),
114         () => console.log('complete')
115       );
116
117     let form: GetPostForm = {
118       id: postId,
119     };
120     WebSocketService.Instance.getPost(form);
121     WebSocketService.Instance.getSite();
122   }
123
124   componentWillUnmount() {
125     this.subscription.unsubscribe();
126   }
127
128   componentDidMount() {
129     autosize(document.querySelectorAll('textarea'));
130   }
131
132   componentDidUpdate(_lastProps: any, lastState: PostState, _snapshot: any) {
133     if (
134       this.state.scrolled_comment_id &&
135       !this.state.scrolled &&
136       lastState.comments.length > 0
137     ) {
138       var elmnt = document.getElementById(
139         `comment-${this.state.scrolled_comment_id}`
140       );
141       elmnt.scrollIntoView();
142       elmnt.classList.add('mark');
143       this.state.scrolled = true;
144       this.markScrolledAsRead(this.state.scrolled_comment_id);
145     }
146
147     // Necessary if you are on a post and you click another post (same route)
148     if (_lastProps.location.pathname !== _lastProps.history.location.pathname) {
149       // Couldnt get a refresh working. This does for now.
150       location.reload();
151
152       // let currentId = this.props.match.params.id;
153       // WebSocketService.Instance.getPost(currentId);
154       // this.context.router.history.push('/sponsors');
155       // this.context.refresh();
156       // this.context.router.history.push(_lastProps.location.pathname);
157     }
158   }
159
160   markScrolledAsRead(commentId: number) {
161     let found = this.state.comments.find(c => c.id == commentId);
162     let parent = this.state.comments.find(c => found.parent_id == c.id);
163     let parent_user_id = parent
164       ? parent.creator_id
165       : this.state.post.creator_id;
166
167     if (
168       UserService.Instance.user &&
169       UserService.Instance.user.id == parent_user_id
170     ) {
171       let form: MarkCommentAsReadForm = {
172         edit_id: found.id,
173         read: true,
174         auth: null,
175       };
176       WebSocketService.Instance.markCommentAsRead(form);
177       UserService.Instance.unreadCountSub.next(
178         UserService.Instance.unreadCountSub.value - 1
179       );
180     }
181   }
182
183   render() {
184     return (
185       <div class="container">
186         {this.state.loading ? (
187           <h5>
188             <svg class="icon icon-spinner spin">
189               <use xlinkHref="#icon-spinner"></use>
190             </svg>
191           </h5>
192         ) : (
193           <div class="row">
194             <div class="col-12 col-md-8 mb-3">
195               <PostListing
196                 post={this.state.post}
197                 showBody
198                 showCommunity
199                 moderators={this.state.moderators}
200                 admins={this.state.siteRes.admins}
201                 enableDownvotes={this.state.siteRes.site.enable_downvotes}
202                 enableNsfw={this.state.siteRes.site.enable_nsfw}
203               />
204               <div className="mb-2" />
205               <CommentForm
206                 postId={this.state.post.id}
207                 disabled={this.state.post.locked}
208               />
209               {this.state.comments.length > 0 && this.sortRadios()}
210               {this.state.commentViewType == CommentViewType.Tree &&
211                 this.commentsTree()}
212               {this.state.commentViewType == CommentViewType.Chat &&
213                 this.commentsFlat()}
214             </div>
215             <div class="col-12 col-sm-12 col-md-4">{this.sidebar()}</div>
216           </div>
217         )}
218       </div>
219     );
220   }
221
222   sortRadios() {
223     return (
224       <>
225         <div class="btn-group btn-group-toggle mr-3 mb-2">
226           <label
227             className={`btn btn-outline-secondary pointer ${
228               this.state.commentSort === CommentSortType.Hot && 'active'
229             }`}
230           >
231             {i18n.t('hot')}
232             <input
233               type="radio"
234               value={CommentSortType.Hot}
235               checked={this.state.commentSort === CommentSortType.Hot}
236               onChange={linkEvent(this, this.handleCommentSortChange)}
237             />
238           </label>
239           <label
240             className={`btn btn-outline-secondary pointer ${
241               this.state.commentSort === CommentSortType.Top && 'active'
242             }`}
243           >
244             {i18n.t('top')}
245             <input
246               type="radio"
247               value={CommentSortType.Top}
248               checked={this.state.commentSort === CommentSortType.Top}
249               onChange={linkEvent(this, this.handleCommentSortChange)}
250             />
251           </label>
252           <label
253             className={`btn btn-outline-secondary pointer ${
254               this.state.commentSort === CommentSortType.New && 'active'
255             }`}
256           >
257             {i18n.t('new')}
258             <input
259               type="radio"
260               value={CommentSortType.New}
261               checked={this.state.commentSort === CommentSortType.New}
262               onChange={linkEvent(this, this.handleCommentSortChange)}
263             />
264           </label>
265           <label
266             className={`btn btn-outline-secondary pointer ${
267               this.state.commentSort === CommentSortType.Old && 'active'
268             }`}
269           >
270             {i18n.t('old')}
271             <input
272               type="radio"
273               value={CommentSortType.Old}
274               checked={this.state.commentSort === CommentSortType.Old}
275               onChange={linkEvent(this, this.handleCommentSortChange)}
276             />
277           </label>
278         </div>
279         <div class="btn-group btn-group-toggle mb-2">
280           <label
281             className={`btn btn-outline-secondary pointer ${
282               this.state.commentViewType === CommentViewType.Chat && 'active'
283             }`}
284           >
285             {i18n.t('chat')}
286             <input
287               type="radio"
288               value={CommentViewType.Chat}
289               checked={this.state.commentViewType === CommentViewType.Chat}
290               onChange={linkEvent(this, this.handleCommentViewTypeChange)}
291             />
292           </label>
293         </div>
294       </>
295     );
296   }
297
298   commentsFlat() {
299     return (
300       <div>
301         <CommentNodes
302           nodes={commentsToFlatNodes(this.state.comments)}
303           noIndent
304           locked={this.state.post.locked}
305           moderators={this.state.moderators}
306           admins={this.state.siteRes.admins}
307           postCreatorId={this.state.post.creator_id}
308           showContext
309           enableDownvotes={this.state.siteRes.site.enable_downvotes}
310           sort={this.state.commentSort}
311         />
312       </div>
313     );
314   }
315
316   sidebar() {
317     return (
318       <div class="mb-3">
319         <Sidebar
320           community={this.state.community}
321           moderators={this.state.moderators}
322           admins={this.state.siteRes.admins}
323           online={this.state.online}
324           enableNsfw={this.state.siteRes.site.enable_nsfw}
325         />
326       </div>
327     );
328   }
329
330   handleCommentSortChange(i: Post, event: any) {
331     i.state.commentSort = Number(event.target.value);
332     i.state.commentViewType = CommentViewType.Tree;
333     i.setState(i.state);
334   }
335
336   handleCommentViewTypeChange(i: Post, event: any) {
337     i.state.commentViewType = Number(event.target.value);
338     i.state.commentSort = CommentSortType.New;
339     i.setState(i.state);
340   }
341
342   buildCommentsTree(): Array<CommentNodeI> {
343     let map = new Map<number, CommentNodeI>();
344     for (let comment of this.state.comments) {
345       let node: CommentNodeI = {
346         comment: comment,
347         children: [],
348       };
349       map.set(comment.id, { ...node });
350     }
351     let tree: Array<CommentNodeI> = [];
352     for (let comment of this.state.comments) {
353       let child = map.get(comment.id);
354       if (comment.parent_id) {
355         let parent_ = map.get(comment.parent_id);
356         parent_.children.push(child);
357       } else {
358         tree.push(child);
359       }
360
361       this.setDepth(child);
362     }
363
364     return tree;
365   }
366
367   setDepth(node: CommentNodeI, i: number = 0): void {
368     for (let child of node.children) {
369       child.comment.depth = i;
370       this.setDepth(child, i + 1);
371     }
372   }
373
374   commentsTree() {
375     let nodes = this.buildCommentsTree();
376     return (
377       <div>
378         <CommentNodes
379           nodes={nodes}
380           locked={this.state.post.locked}
381           moderators={this.state.moderators}
382           admins={this.state.siteRes.admins}
383           postCreatorId={this.state.post.creator_id}
384           sort={this.state.commentSort}
385           enableDownvotes={this.state.siteRes.site.enable_downvotes}
386         />
387       </div>
388     );
389   }
390
391   parseMessage(msg: WebSocketJsonResponse) {
392     console.log(msg);
393     let res = wsJsonToRes(msg);
394     if (msg.error) {
395       toast(i18n.t(msg.error), 'danger');
396       return;
397     } else if (msg.reconnect) {
398       WebSocketService.Instance.getPost({
399         id: Number(this.props.match.params.id),
400       });
401     } else if (res.op == UserOperation.GetPost) {
402       let data = res.data as GetPostResponse;
403       this.state.post = data.post;
404       this.state.comments = data.comments;
405       this.state.community = data.community;
406       this.state.moderators = data.moderators;
407       this.state.online = data.online;
408       this.state.loading = false;
409       document.title = `${this.state.post.name} - ${this.state.siteRes.site.name}`;
410
411       // Get cross-posts
412       if (this.state.post.url) {
413         let form: SearchForm = {
414           q: this.state.post.url,
415           type_: SearchType[SearchType.Url],
416           sort: SortType[SortType.TopAll],
417           page: 1,
418           limit: 6,
419         };
420         WebSocketService.Instance.search(form);
421       }
422
423       this.setState(this.state);
424       setupTippy();
425     } else if (res.op == UserOperation.CreateComment) {
426       let data = res.data as CommentResponse;
427
428       // Necessary since it might be a user reply
429       if (data.recipient_ids.length == 0) {
430         this.state.comments.unshift(data.comment);
431         this.setState(this.state);
432       }
433     } else if (
434       res.op == UserOperation.EditComment ||
435       res.op == UserOperation.DeleteComment ||
436       res.op == UserOperation.RemoveComment
437     ) {
438       let data = res.data as CommentResponse;
439       editCommentRes(data, this.state.comments);
440       this.setState(this.state);
441     } else if (res.op == UserOperation.SaveComment) {
442       let data = res.data as CommentResponse;
443       saveCommentRes(data, this.state.comments);
444       this.setState(this.state);
445       setupTippy();
446     } else if (res.op == UserOperation.CreateCommentLike) {
447       let data = res.data as CommentResponse;
448       createCommentLikeRes(data, this.state.comments);
449       this.setState(this.state);
450     } else if (res.op == UserOperation.CreatePostLike) {
451       let data = res.data as PostResponse;
452       createPostLikeRes(data, this.state.post);
453       this.setState(this.state);
454     } else if (
455       res.op == UserOperation.EditPost ||
456       res.op == UserOperation.DeletePost ||
457       res.op == UserOperation.RemovePost ||
458       res.op == UserOperation.LockPost ||
459       res.op == UserOperation.StickyPost
460     ) {
461       let data = res.data as PostResponse;
462       this.state.post = data.post;
463       this.setState(this.state);
464       setupTippy();
465     } else if (res.op == UserOperation.SavePost) {
466       let data = res.data as PostResponse;
467       this.state.post = data.post;
468       this.setState(this.state);
469       setupTippy();
470     } else if (
471       res.op == UserOperation.EditCommunity ||
472       res.op == UserOperation.DeleteCommunity ||
473       res.op == UserOperation.RemoveCommunity
474     ) {
475       let data = res.data as CommunityResponse;
476       this.state.community = data.community;
477       this.state.post.community_id = data.community.id;
478       this.state.post.community_name = data.community.name;
479       this.setState(this.state);
480     } else if (res.op == UserOperation.FollowCommunity) {
481       let data = res.data as CommunityResponse;
482       this.state.community.subscribed = data.community.subscribed;
483       this.state.community.number_of_subscribers =
484         data.community.number_of_subscribers;
485       this.setState(this.state);
486     } else if (res.op == UserOperation.BanFromCommunity) {
487       let data = res.data as BanFromCommunityResponse;
488       this.state.comments
489         .filter(c => c.creator_id == data.user.id)
490         .forEach(c => (c.banned_from_community = data.banned));
491       if (this.state.post.creator_id == data.user.id) {
492         this.state.post.banned_from_community = data.banned;
493       }
494       this.setState(this.state);
495     } else if (res.op == UserOperation.AddModToCommunity) {
496       let data = res.data as AddModToCommunityResponse;
497       this.state.moderators = data.moderators;
498       this.setState(this.state);
499     } else if (res.op == UserOperation.BanUser) {
500       let data = res.data as BanUserResponse;
501       this.state.comments
502         .filter(c => c.creator_id == data.user.id)
503         .forEach(c => (c.banned = data.banned));
504       if (this.state.post.creator_id == data.user.id) {
505         this.state.post.banned = data.banned;
506       }
507       this.setState(this.state);
508     } else if (res.op == UserOperation.AddAdmin) {
509       let data = res.data as AddAdminResponse;
510       this.state.siteRes.admins = data.admins;
511       this.setState(this.state);
512     } else if (res.op == UserOperation.Search) {
513       let data = res.data as SearchResponse;
514       this.state.crossPosts = data.posts.filter(
515         p => p.id != Number(this.props.match.params.id)
516       );
517       if (this.state.crossPosts.length) {
518         this.state.post.duplicates = this.state.crossPosts;
519       }
520       this.setState(this.state);
521     } else if (
522       res.op == UserOperation.TransferSite ||
523       res.op == UserOperation.GetSite
524     ) {
525       let data = res.data as GetSiteResponse;
526       this.state.siteRes = data;
527       this.setState(this.state);
528     } else if (res.op == UserOperation.TransferCommunity) {
529       let data = res.data as GetCommunityResponse;
530       this.state.community = data.community;
531       this.state.moderators = data.moderators;
532       this.setState(this.state);
533     }
534   }
535 }