]> Untitled Git - lemmy.git/blob - ui/src/components/post.tsx
Merge remote-tracking branch 'weblate/main' into main
[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.user.unreadCount--;
178       UserService.Instance.sub.next({
179         user: UserService.Instance.user,
180       });
181     }
182   }
183
184   render() {
185     return (
186       <div class="container">
187         {this.state.loading ? (
188           <h5>
189             <svg class="icon icon-spinner spin">
190               <use xlinkHref="#icon-spinner"></use>
191             </svg>
192           </h5>
193         ) : (
194           <div class="row">
195             <div class="col-12 col-md-8 mb-3">
196               <PostListing
197                 post={this.state.post}
198                 showBody
199                 showCommunity
200                 moderators={this.state.moderators}
201                 admins={this.state.siteRes.admins}
202                 enableDownvotes={this.state.siteRes.site.enable_downvotes}
203                 enableNsfw={this.state.siteRes.site.enable_nsfw}
204               />
205               <div className="mb-2" />
206               <CommentForm
207                 postId={this.state.post.id}
208                 disabled={this.state.post.locked}
209               />
210               {this.state.comments.length > 0 && this.sortRadios()}
211               {this.state.commentViewType == CommentViewType.Tree &&
212                 this.commentsTree()}
213               {this.state.commentViewType == CommentViewType.Chat &&
214                 this.commentsFlat()}
215             </div>
216             <div class="col-12 col-sm-12 col-md-4">{this.sidebar()}</div>
217           </div>
218         )}
219       </div>
220     );
221   }
222
223   sortRadios() {
224     return (
225       <>
226         <div class="btn-group btn-group-toggle mr-3 mb-2">
227           <label
228             className={`btn btn-outline-secondary pointer ${
229               this.state.commentSort === CommentSortType.Hot && 'active'
230             }`}
231           >
232             {i18n.t('hot')}
233             <input
234               type="radio"
235               value={CommentSortType.Hot}
236               checked={this.state.commentSort === CommentSortType.Hot}
237               onChange={linkEvent(this, this.handleCommentSortChange)}
238             />
239           </label>
240           <label
241             className={`btn btn-outline-secondary pointer ${
242               this.state.commentSort === CommentSortType.Top && 'active'
243             }`}
244           >
245             {i18n.t('top')}
246             <input
247               type="radio"
248               value={CommentSortType.Top}
249               checked={this.state.commentSort === CommentSortType.Top}
250               onChange={linkEvent(this, this.handleCommentSortChange)}
251             />
252           </label>
253           <label
254             className={`btn btn-outline-secondary pointer ${
255               this.state.commentSort === CommentSortType.New && 'active'
256             }`}
257           >
258             {i18n.t('new')}
259             <input
260               type="radio"
261               value={CommentSortType.New}
262               checked={this.state.commentSort === CommentSortType.New}
263               onChange={linkEvent(this, this.handleCommentSortChange)}
264             />
265           </label>
266           <label
267             className={`btn btn-outline-secondary pointer ${
268               this.state.commentSort === CommentSortType.Old && 'active'
269             }`}
270           >
271             {i18n.t('old')}
272             <input
273               type="radio"
274               value={CommentSortType.Old}
275               checked={this.state.commentSort === CommentSortType.Old}
276               onChange={linkEvent(this, this.handleCommentSortChange)}
277             />
278           </label>
279         </div>
280         <div class="btn-group btn-group-toggle mb-2">
281           <label
282             className={`btn btn-outline-secondary pointer ${
283               this.state.commentViewType === CommentViewType.Chat && 'active'
284             }`}
285           >
286             {i18n.t('chat')}
287             <input
288               type="radio"
289               value={CommentViewType.Chat}
290               checked={this.state.commentViewType === CommentViewType.Chat}
291               onChange={linkEvent(this, this.handleCommentViewTypeChange)}
292             />
293           </label>
294         </div>
295       </>
296     );
297   }
298
299   commentsFlat() {
300     return (
301       <div>
302         <CommentNodes
303           nodes={commentsToFlatNodes(this.state.comments)}
304           noIndent
305           locked={this.state.post.locked}
306           moderators={this.state.moderators}
307           admins={this.state.siteRes.admins}
308           postCreatorId={this.state.post.creator_id}
309           showContext
310           enableDownvotes={this.state.siteRes.site.enable_downvotes}
311           sort={this.state.commentSort}
312         />
313       </div>
314     );
315   }
316
317   sidebar() {
318     return (
319       <div class="mb-3">
320         <Sidebar
321           community={this.state.community}
322           moderators={this.state.moderators}
323           admins={this.state.siteRes.admins}
324           online={this.state.online}
325           enableNsfw={this.state.siteRes.site.enable_nsfw}
326         />
327       </div>
328     );
329   }
330
331   handleCommentSortChange(i: Post, event: any) {
332     i.state.commentSort = Number(event.target.value);
333     i.state.commentViewType = CommentViewType.Tree;
334     i.setState(i.state);
335   }
336
337   handleCommentViewTypeChange(i: Post, event: any) {
338     i.state.commentViewType = Number(event.target.value);
339     i.state.commentSort = CommentSortType.New;
340     i.setState(i.state);
341   }
342
343   buildCommentsTree(): Array<CommentNodeI> {
344     let map = new Map<number, CommentNodeI>();
345     for (let comment of this.state.comments) {
346       let node: CommentNodeI = {
347         comment: comment,
348         children: [],
349       };
350       map.set(comment.id, { ...node });
351     }
352     let tree: Array<CommentNodeI> = [];
353     for (let comment of this.state.comments) {
354       let child = map.get(comment.id);
355       if (comment.parent_id) {
356         let parent_ = map.get(comment.parent_id);
357         parent_.children.push(child);
358       } else {
359         tree.push(child);
360       }
361
362       this.setDepth(child);
363     }
364
365     return tree;
366   }
367
368   setDepth(node: CommentNodeI, i: number = 0): void {
369     for (let child of node.children) {
370       child.comment.depth = i;
371       this.setDepth(child, i + 1);
372     }
373   }
374
375   commentsTree() {
376     let nodes = this.buildCommentsTree();
377     return (
378       <div>
379         <CommentNodes
380           nodes={nodes}
381           locked={this.state.post.locked}
382           moderators={this.state.moderators}
383           admins={this.state.siteRes.admins}
384           postCreatorId={this.state.post.creator_id}
385           sort={this.state.commentSort}
386           enableDownvotes={this.state.siteRes.site.enable_downvotes}
387         />
388       </div>
389     );
390   }
391
392   parseMessage(msg: WebSocketJsonResponse) {
393     console.log(msg);
394     let res = wsJsonToRes(msg);
395     if (msg.error) {
396       toast(i18n.t(msg.error), 'danger');
397       return;
398     } else if (msg.reconnect) {
399       WebSocketService.Instance.getPost({
400         id: Number(this.props.match.params.id),
401       });
402     } else if (res.op == UserOperation.GetPost) {
403       let data = res.data as GetPostResponse;
404       this.state.post = data.post;
405       this.state.comments = data.comments;
406       this.state.community = data.community;
407       this.state.moderators = data.moderators;
408       this.state.online = data.online;
409       this.state.loading = false;
410       document.title = `${this.state.post.name} - ${this.state.siteRes.site.name}`;
411
412       // Get cross-posts
413       if (this.state.post.url) {
414         let form: SearchForm = {
415           q: this.state.post.url,
416           type_: SearchType[SearchType.Url],
417           sort: SortType[SortType.TopAll],
418           page: 1,
419           limit: 6,
420         };
421         WebSocketService.Instance.search(form);
422       }
423
424       this.setState(this.state);
425       setupTippy();
426     } else if (res.op == UserOperation.CreateComment) {
427       let data = res.data as CommentResponse;
428
429       // Necessary since it might be a user reply
430       if (data.recipient_ids.length == 0) {
431         this.state.comments.unshift(data.comment);
432         this.setState(this.state);
433       }
434     } else if (
435       res.op == UserOperation.EditComment ||
436       res.op == UserOperation.DeleteComment ||
437       res.op == UserOperation.RemoveComment
438     ) {
439       let data = res.data as CommentResponse;
440       editCommentRes(data, this.state.comments);
441       this.setState(this.state);
442     } else if (res.op == UserOperation.SaveComment) {
443       let data = res.data as CommentResponse;
444       saveCommentRes(data, this.state.comments);
445       this.setState(this.state);
446       setupTippy();
447     } else if (res.op == UserOperation.CreateCommentLike) {
448       let data = res.data as CommentResponse;
449       createCommentLikeRes(data, this.state.comments);
450       this.setState(this.state);
451     } else if (res.op == UserOperation.CreatePostLike) {
452       let data = res.data as PostResponse;
453       createPostLikeRes(data, this.state.post);
454       this.setState(this.state);
455     } else if (
456       res.op == UserOperation.EditPost ||
457       res.op == UserOperation.DeletePost ||
458       res.op == UserOperation.RemovePost ||
459       res.op == UserOperation.LockPost ||
460       res.op == UserOperation.StickyPost
461     ) {
462       let data = res.data as PostResponse;
463       this.state.post = data.post;
464       this.setState(this.state);
465       setupTippy();
466     } else if (res.op == UserOperation.SavePost) {
467       let data = res.data as PostResponse;
468       this.state.post = data.post;
469       this.setState(this.state);
470       setupTippy();
471     } else if (
472       res.op == UserOperation.EditCommunity ||
473       res.op == UserOperation.DeleteCommunity ||
474       res.op == UserOperation.RemoveCommunity
475     ) {
476       let data = res.data as CommunityResponse;
477       this.state.community = data.community;
478       this.state.post.community_id = data.community.id;
479       this.state.post.community_name = data.community.name;
480       this.setState(this.state);
481     } else if (res.op == UserOperation.FollowCommunity) {
482       let data = res.data as CommunityResponse;
483       this.state.community.subscribed = data.community.subscribed;
484       this.state.community.number_of_subscribers =
485         data.community.number_of_subscribers;
486       this.setState(this.state);
487     } else if (res.op == UserOperation.BanFromCommunity) {
488       let data = res.data as BanFromCommunityResponse;
489       this.state.comments
490         .filter(c => c.creator_id == data.user.id)
491         .forEach(c => (c.banned_from_community = data.banned));
492       if (this.state.post.creator_id == data.user.id) {
493         this.state.post.banned_from_community = data.banned;
494       }
495       this.setState(this.state);
496     } else if (res.op == UserOperation.AddModToCommunity) {
497       let data = res.data as AddModToCommunityResponse;
498       this.state.moderators = data.moderators;
499       this.setState(this.state);
500     } else if (res.op == UserOperation.BanUser) {
501       let data = res.data as BanUserResponse;
502       this.state.comments
503         .filter(c => c.creator_id == data.user.id)
504         .forEach(c => (c.banned = data.banned));
505       if (this.state.post.creator_id == data.user.id) {
506         this.state.post.banned = data.banned;
507       }
508       this.setState(this.state);
509     } else if (res.op == UserOperation.AddAdmin) {
510       let data = res.data as AddAdminResponse;
511       this.state.siteRes.admins = data.admins;
512       this.setState(this.state);
513     } else if (res.op == UserOperation.Search) {
514       let data = res.data as SearchResponse;
515       this.state.crossPosts = data.posts.filter(
516         p => p.id != Number(this.props.match.params.id)
517       );
518       if (this.state.crossPosts.length) {
519         this.state.post.duplicates = this.state.crossPosts;
520       }
521       this.setState(this.state);
522     } else if (
523       res.op == UserOperation.TransferSite ||
524       res.op == UserOperation.GetSite
525     ) {
526       let data = res.data as GetSiteResponse;
527       this.state.siteRes = data;
528       this.setState(this.state);
529     } else if (res.op == UserOperation.TransferCommunity) {
530       let data = res.data as GetCommunityResponse;
531       this.state.community = data.community;
532       this.state.moderators = data.moderators;
533       this.setState(this.state);
534     }
535   }
536 }