]> Untitled Git - lemmy.git/blob - ui/src/components/comment-node.tsx
Add cake day display in user page & posts/comments #682
[lemmy.git] / ui / src / components / comment-node.tsx
1 import { Component, linkEvent } from 'inferno';
2 import { Link } from 'inferno-router';
3 import {
4   CommentNode as CommentNodeI,
5   CommentLikeForm,
6   CommentForm as CommentFormI,
7   EditUserMentionForm,
8   SaveCommentForm,
9   BanFromCommunityForm,
10   BanUserForm,
11   CommunityUser,
12   UserView,
13   AddModToCommunityForm,
14   AddAdminForm,
15   TransferCommunityForm,
16   TransferSiteForm,
17   BanType,
18   CommentSortType,
19   SortType,
20 } from '../interfaces';
21 import { WebSocketService, UserService } from '../services';
22 import {
23   mdToHtml,
24   getUnixTime,
25   canMod,
26   isMod,
27   setupTippy,
28   colorList,
29 } from '../utils';
30 import moment from 'moment';
31 import { MomentTime } from './moment-time';
32 import { CommentForm } from './comment-form';
33 import { CommentNodes } from './comment-nodes';
34 import { UserListing } from './user-listing';
35 import { i18n } from '../i18next';
36 import { CakeDay } from './cake-day';
37
38 interface CommentNodeState {
39   showReply: boolean;
40   showEdit: boolean;
41   showRemoveDialog: boolean;
42   removeReason: string;
43   showBanDialog: boolean;
44   banReason: string;
45   banExpires: string;
46   banType: BanType;
47   showConfirmTransferSite: boolean;
48   showConfirmTransferCommunity: boolean;
49   showConfirmAppointAsMod: boolean;
50   showConfirmAppointAsAdmin: boolean;
51   collapsed: boolean;
52   viewSource: boolean;
53   showAdvanced: boolean;
54   my_vote: number;
55   score: number;
56   upvotes: number;
57   downvotes: number;
58   borderColor: string;
59   readLoading: boolean;
60   saveLoading: boolean;
61 }
62
63 interface CommentNodeProps {
64   node: CommentNodeI;
65   noIndent?: boolean;
66   viewOnly?: boolean;
67   locked?: boolean;
68   markable?: boolean;
69   showContext?: boolean;
70   moderators: Array<CommunityUser>;
71   admins: Array<UserView>;
72   // TODO is this necessary, can't I get it from the node itself?
73   postCreatorId?: number;
74   showCommunity?: boolean;
75   sort?: CommentSortType;
76   sortType?: SortType;
77 }
78
79 export class CommentNode extends Component<CommentNodeProps, CommentNodeState> {
80   private emptyState: CommentNodeState = {
81     showReply: false,
82     showEdit: false,
83     showRemoveDialog: false,
84     removeReason: null,
85     showBanDialog: false,
86     banReason: null,
87     banExpires: null,
88     banType: BanType.Community,
89     collapsed: false,
90     viewSource: false,
91     showAdvanced: false,
92     showConfirmTransferSite: false,
93     showConfirmTransferCommunity: false,
94     showConfirmAppointAsMod: false,
95     showConfirmAppointAsAdmin: false,
96     my_vote: this.props.node.comment.my_vote,
97     score: this.props.node.comment.score,
98     upvotes: this.props.node.comment.upvotes,
99     downvotes: this.props.node.comment.downvotes,
100     borderColor: this.props.node.comment.depth
101       ? colorList[this.props.node.comment.depth % colorList.length]
102       : colorList[0],
103     readLoading: false,
104     saveLoading: false,
105   };
106
107   constructor(props: any, context: any) {
108     super(props, context);
109
110     this.state = this.emptyState;
111     this.handleReplyCancel = this.handleReplyCancel.bind(this);
112     this.handleCommentUpvote = this.handleCommentUpvote.bind(this);
113     this.handleCommentDownvote = this.handleCommentDownvote.bind(this);
114   }
115
116   componentWillReceiveProps(nextProps: CommentNodeProps) {
117     this.state.my_vote = nextProps.node.comment.my_vote;
118     this.state.upvotes = nextProps.node.comment.upvotes;
119     this.state.downvotes = nextProps.node.comment.downvotes;
120     this.state.score = nextProps.node.comment.score;
121     this.state.readLoading = false;
122     this.state.saveLoading = false;
123     this.setState(this.state);
124   }
125
126   render() {
127     let node = this.props.node;
128     const { creator_name, creator_published } = node.comment;
129     return (
130       <div
131         className={`comment ${
132           node.comment.parent_id && !this.props.noIndent ? 'ml-1' : ''
133         }`}
134       >
135         <div
136           id={`comment-${node.comment.id}`}
137           className={`details comment-node border-top border-light py-2 ${
138             this.isCommentNew ? 'mark' : ''
139           }`}
140           style={
141             !this.props.noIndent &&
142             this.props.node.comment.parent_id &&
143             `border-left: 2px ${this.state.borderColor} solid !important`
144           }
145         >
146           <div
147             class={`${
148               !this.props.noIndent &&
149               this.props.node.comment.parent_id &&
150               'ml-2'
151             }`}
152           >
153             <div class="d-flex flex-wrap align-items-center text-muted small">
154               <span class="mr-2">
155                 <UserListing
156                   user={{
157                     name: node.comment.creator_name,
158                     avatar: node.comment.creator_avatar,
159                     id: node.comment.creator_id,
160                     local: node.comment.creator_local,
161                     actor_id: node.comment.creator_actor_id,
162                   }}
163                 />
164               </span>
165
166               <CakeDay
167                 creator_name={creator_name}
168                 creator_published={creator_published}
169               />
170
171               {this.isMod && (
172                 <div className="badge badge-light d-none d-sm-inline mr-2">
173                   {i18n.t('mod')}
174                 </div>
175               )}
176               {this.isAdmin && (
177                 <div className="badge badge-light d-none d-sm-inline mr-2">
178                   {i18n.t('admin')}
179                 </div>
180               )}
181               {this.isPostCreator && (
182                 <div className="badge badge-light d-none d-sm-inline mr-2">
183                   {i18n.t('creator')}
184                 </div>
185               )}
186               {(node.comment.banned_from_community || node.comment.banned) && (
187                 <div className="badge badge-danger mr-2">
188                   {i18n.t('banned')}
189                 </div>
190               )}
191               {this.props.showCommunity && (
192                 <>
193                   <span class="mx-1">{i18n.t('to')}</span>
194                   <Link class="mr-2" to={`/c/${node.comment.community_name}`}>
195                     {node.comment.community_name}
196                   </Link>
197                 </>
198               )}
199               <div
200                 className="mr-lg-4 flex-grow-1 flex-lg-grow-0 unselectable pointer mx-2"
201                 onClick={linkEvent(this, this.handleCommentCollapse)}
202               >
203                 {this.state.collapsed ? (
204                   <svg class="icon icon-inline">
205                     <use xlinkHref="#icon-plus-square"></use>
206                   </svg>
207                 ) : (
208                   <svg class="icon icon-inline">
209                     <use xlinkHref="#icon-minus-square"></use>
210                   </svg>
211                 )}
212               </div>
213               <span
214                 className={`unselectable pointer ${this.scoreColor}`}
215                 onClick={linkEvent(node, this.handleCommentUpvote)}
216                 data-tippy-content={this.pointsTippy}
217               >
218                 <svg class="icon icon-inline mr-1">
219                   <use xlinkHref="#icon-zap"></use>
220                 </svg>
221                 <span class="mr-1">{this.state.score}</span>
222               </span>
223               <span className="mr-1">•</span>
224               <span>
225                 <MomentTime data={node.comment} />
226               </span>
227             </div>
228             {/* end of user row */}
229             {this.state.showEdit && (
230               <CommentForm
231                 node={node}
232                 edit
233                 onReplyCancel={this.handleReplyCancel}
234                 disabled={this.props.locked}
235               />
236             )}
237             {!this.state.showEdit && !this.state.collapsed && (
238               <div>
239                 {this.state.viewSource ? (
240                   <pre>{this.commentUnlessRemoved}</pre>
241                 ) : (
242                   <div
243                     className="md-div"
244                     dangerouslySetInnerHTML={mdToHtml(
245                       this.commentUnlessRemoved
246                     )}
247                   />
248                 )}
249                 <div class="d-flex justify-content-between justify-content-lg-start flex-wrap text-muted font-weight-bold">
250                   {this.props.showContext && this.linkBtn}
251                   {this.props.markable && (
252                     <button
253                       class="btn btn-link btn-animate text-muted"
254                       onClick={linkEvent(this, this.handleMarkRead)}
255                       data-tippy-content={
256                         node.comment.read
257                           ? i18n.t('mark_as_unread')
258                           : i18n.t('mark_as_read')
259                       }
260                     >
261                       {this.state.readLoading ? (
262                         this.loadingIcon
263                       ) : (
264                         <svg
265                           class={`icon icon-inline ${
266                             node.comment.read && 'text-success'
267                           }`}
268                         >
269                           <use xlinkHref="#icon-check"></use>
270                         </svg>
271                       )}
272                     </button>
273                   )}
274                   {UserService.Instance.user && !this.props.viewOnly && (
275                     <>
276                       <button
277                         className={`btn btn-link btn-animate ${
278                           this.state.my_vote == 1 ? 'text-info' : 'text-muted'
279                         }`}
280                         onClick={linkEvent(node, this.handleCommentUpvote)}
281                         data-tippy-content={i18n.t('upvote')}
282                       >
283                         <svg class="icon icon-inline">
284                           <use xlinkHref="#icon-arrow-up"></use>
285                         </svg>
286                         {this.state.upvotes !== this.state.score && (
287                           <span class="ml-1">{this.state.upvotes}</span>
288                         )}
289                       </button>
290                       {WebSocketService.Instance.site.enable_downvotes && (
291                         <button
292                           className={`btn btn-link btn-animate ${
293                             this.state.my_vote == -1
294                               ? 'text-danger'
295                               : 'text-muted'
296                           }`}
297                           onClick={linkEvent(node, this.handleCommentDownvote)}
298                           data-tippy-content={i18n.t('downvote')}
299                         >
300                           <svg class="icon icon-inline">
301                             <use xlinkHref="#icon-arrow-down"></use>
302                           </svg>
303                           {this.state.upvotes !== this.state.score && (
304                             <span class="ml-1">{this.state.downvotes}</span>
305                           )}
306                         </button>
307                       )}
308                       <button
309                         class="btn btn-link btn-animate text-muted"
310                         onClick={linkEvent(this, this.handleReplyClick)}
311                         data-tippy-content={i18n.t('reply')}
312                       >
313                         <svg class="icon icon-inline">
314                           <use xlinkHref="#icon-reply1"></use>
315                         </svg>
316                       </button>
317                       {!this.state.showAdvanced ? (
318                         <button
319                           className="btn btn-link btn-animate text-muted"
320                           onClick={linkEvent(this, this.handleShowAdvanced)}
321                           data-tippy-content={i18n.t('more')}
322                         >
323                           <svg class="icon icon-inline">
324                             <use xlinkHref="#icon-more-vertical"></use>
325                           </svg>
326                         </button>
327                       ) : (
328                         <>
329                           {!this.myComment && (
330                             <button class="btn btn-link btn-animate">
331                               <Link
332                                 class="text-muted"
333                                 to={`/create_private_message?recipient_id=${node.comment.creator_id}`}
334                                 title={i18n.t('message').toLowerCase()}
335                               >
336                                 <svg class="icon">
337                                   <use xlinkHref="#icon-mail"></use>
338                                 </svg>
339                               </Link>
340                             </button>
341                           )}
342                           {!this.props.showContext && this.linkBtn}
343                           <button
344                             class="btn btn-link btn-animate text-muted"
345                             onClick={linkEvent(
346                               this,
347                               this.handleSaveCommentClick
348                             )}
349                             data-tippy-content={
350                               node.comment.saved
351                                 ? i18n.t('unsave')
352                                 : i18n.t('save')
353                             }
354                           >
355                             {this.state.saveLoading ? (
356                               this.loadingIcon
357                             ) : (
358                               <svg
359                                 class={`icon icon-inline ${
360                                   node.comment.saved && 'text-warning'
361                                 }`}
362                               >
363                                 <use xlinkHref="#icon-star"></use>
364                               </svg>
365                             )}
366                           </button>
367                           <button
368                             className="btn btn-link btn-animate text-muted"
369                             onClick={linkEvent(this, this.handleViewSource)}
370                             data-tippy-content={i18n.t('view_source')}
371                           >
372                             <svg
373                               class={`icon icon-inline ${
374                                 this.state.viewSource && 'text-success'
375                               }`}
376                             >
377                               <use xlinkHref="#icon-file-text"></use>
378                             </svg>
379                           </button>
380                           {this.myComment && (
381                             <>
382                               <button
383                                 class="btn btn-link btn-animate text-muted"
384                                 onClick={linkEvent(this, this.handleEditClick)}
385                                 data-tippy-content={i18n.t('edit')}
386                               >
387                                 <svg class="icon icon-inline">
388                                   <use xlinkHref="#icon-edit"></use>
389                                 </svg>
390                               </button>
391                               <button
392                                 class="btn btn-link btn-animate text-muted"
393                                 onClick={linkEvent(
394                                   this,
395                                   this.handleDeleteClick
396                                 )}
397                                 data-tippy-content={
398                                   !node.comment.deleted
399                                     ? i18n.t('delete')
400                                     : i18n.t('restore')
401                                 }
402                               >
403                                 <svg
404                                   class={`icon icon-inline ${
405                                     node.comment.deleted && 'text-danger'
406                                   }`}
407                                 >
408                                   <use xlinkHref="#icon-trash"></use>
409                                 </svg>
410                               </button>
411                             </>
412                           )}
413                           {/* Admins and mods can remove comments */}
414                           {(this.canMod || this.canAdmin) && (
415                             <>
416                               {!node.comment.removed ? (
417                                 <button
418                                   class="btn btn-link btn-animate text-muted"
419                                   onClick={linkEvent(
420                                     this,
421                                     this.handleModRemoveShow
422                                   )}
423                                 >
424                                   {i18n.t('remove')}
425                                 </button>
426                               ) : (
427                                 <button
428                                   class="btn btn-link btn-animate text-muted"
429                                   onClick={linkEvent(
430                                     this,
431                                     this.handleModRemoveSubmit
432                                   )}
433                                 >
434                                   {i18n.t('restore')}
435                                 </button>
436                               )}
437                             </>
438                           )}
439                           {/* Mods can ban from community, and appoint as mods to community */}
440                           {this.canMod && (
441                             <>
442                               {!this.isMod &&
443                                 (!node.comment.banned_from_community ? (
444                                   <button
445                                     class="btn btn-link btn-animate text-muted"
446                                     onClick={linkEvent(
447                                       this,
448                                       this.handleModBanFromCommunityShow
449                                     )}
450                                   >
451                                     {i18n.t('ban')}
452                                   </button>
453                                 ) : (
454                                   <button
455                                     class="btn btn-link btn-animate text-muted"
456                                     onClick={linkEvent(
457                                       this,
458                                       this.handleModBanFromCommunitySubmit
459                                     )}
460                                   >
461                                     {i18n.t('unban')}
462                                   </button>
463                                 ))}
464                               {!node.comment.banned_from_community &&
465                                 (!this.state.showConfirmAppointAsMod ? (
466                                   <button
467                                     class="btn btn-link btn-animate text-muted"
468                                     onClick={linkEvent(
469                                       this,
470                                       this.handleShowConfirmAppointAsMod
471                                     )}
472                                   >
473                                     {this.isMod
474                                       ? i18n.t('remove_as_mod')
475                                       : i18n.t('appoint_as_mod')}
476                                   </button>
477                                 ) : (
478                                   <>
479                                     <button class="btn btn-link btn-animate text-muted">
480                                       {i18n.t('are_you_sure')}
481                                     </button>
482                                     <button
483                                       class="btn btn-link btn-animate text-muted"
484                                       onClick={linkEvent(
485                                         this,
486                                         this.handleAddModToCommunity
487                                       )}
488                                     >
489                                       {i18n.t('yes')}
490                                     </button>
491                                     <button
492                                       class="btn btn-link btn-animate text-muted"
493                                       onClick={linkEvent(
494                                         this,
495                                         this.handleCancelConfirmAppointAsMod
496                                       )}
497                                     >
498                                       {i18n.t('no')}
499                                     </button>
500                                   </>
501                                 ))}
502                             </>
503                           )}
504                           {/* Community creators and admins can transfer community to another mod */}
505                           {(this.amCommunityCreator || this.canAdmin) &&
506                             this.isMod &&
507                             (!this.state.showConfirmTransferCommunity ? (
508                               <button
509                                 class="btn btn-link btn-animate text-muted"
510                                 onClick={linkEvent(
511                                   this,
512                                   this.handleShowConfirmTransferCommunity
513                                 )}
514                               >
515                                 {i18n.t('transfer_community')}
516                               </button>
517                             ) : (
518                               <>
519                                 <button class="btn btn-link btn-animate text-muted">
520                                   {i18n.t('are_you_sure')}
521                                 </button>
522                                 <button
523                                   class="btn btn-link btn-animate text-muted"
524                                   onClick={linkEvent(
525                                     this,
526                                     this.handleTransferCommunity
527                                   )}
528                                 >
529                                   {i18n.t('yes')}
530                                 </button>
531                                 <button
532                                   class="btn btn-link btn-animate text-muted"
533                                   onClick={linkEvent(
534                                     this,
535                                     this
536                                       .handleCancelShowConfirmTransferCommunity
537                                   )}
538                                 >
539                                   {i18n.t('no')}
540                                 </button>
541                               </>
542                             ))}
543                           {/* Admins can ban from all, and appoint other admins */}
544                           {this.canAdmin && (
545                             <>
546                               {!this.isAdmin &&
547                                 (!node.comment.banned ? (
548                                   <button
549                                     class="btn btn-link btn-animate text-muted"
550                                     onClick={linkEvent(
551                                       this,
552                                       this.handleModBanShow
553                                     )}
554                                   >
555                                     {i18n.t('ban_from_site')}
556                                   </button>
557                                 ) : (
558                                   <button
559                                     class="btn btn-link btn-animate text-muted"
560                                     onClick={linkEvent(
561                                       this,
562                                       this.handleModBanSubmit
563                                     )}
564                                   >
565                                     {i18n.t('unban_from_site')}
566                                   </button>
567                                 ))}
568                               {!node.comment.banned &&
569                                 (!this.state.showConfirmAppointAsAdmin ? (
570                                   <button
571                                     class="btn btn-link btn-animate text-muted"
572                                     onClick={linkEvent(
573                                       this,
574                                       this.handleShowConfirmAppointAsAdmin
575                                     )}
576                                   >
577                                     {this.isAdmin
578                                       ? i18n.t('remove_as_admin')
579                                       : i18n.t('appoint_as_admin')}
580                                   </button>
581                                 ) : (
582                                   <>
583                                     <button class="btn btn-link btn-animate text-muted">
584                                       {i18n.t('are_you_sure')}
585                                     </button>
586                                     <button
587                                       class="btn btn-link btn-animate text-muted"
588                                       onClick={linkEvent(
589                                         this,
590                                         this.handleAddAdmin
591                                       )}
592                                     >
593                                       {i18n.t('yes')}
594                                     </button>
595                                     <button
596                                       class="btn btn-link btn-animate text-muted"
597                                       onClick={linkEvent(
598                                         this,
599                                         this.handleCancelConfirmAppointAsAdmin
600                                       )}
601                                     >
602                                       {i18n.t('no')}
603                                     </button>
604                                   </>
605                                 ))}
606                             </>
607                           )}
608                           {/* Site Creator can transfer to another admin */}
609                           {this.amSiteCreator &&
610                             this.isAdmin &&
611                             (!this.state.showConfirmTransferSite ? (
612                               <button
613                                 class="btn btn-link btn-animate text-muted"
614                                 onClick={linkEvent(
615                                   this,
616                                   this.handleShowConfirmTransferSite
617                                 )}
618                               >
619                                 {i18n.t('transfer_site')}
620                               </button>
621                             ) : (
622                               <>
623                                 <button class="btn btn-link btn-animate text-muted">
624                                   {i18n.t('are_you_sure')}
625                                 </button>
626                                 <button
627                                   class="btn btn-link btn-animate text-muted"
628                                   onClick={linkEvent(
629                                     this,
630                                     this.handleTransferSite
631                                   )}
632                                 >
633                                   {i18n.t('yes')}
634                                 </button>
635                                 <button
636                                   class="btn btn-link btn-animate text-muted"
637                                   onClick={linkEvent(
638                                     this,
639                                     this.handleCancelShowConfirmTransferSite
640                                   )}
641                                 >
642                                   {i18n.t('no')}
643                                 </button>
644                               </>
645                             ))}
646                         </>
647                       )}
648                     </>
649                   )}
650                 </div>
651                 {/* end of button group */}
652               </div>
653             )}
654           </div>
655         </div>
656         {/* end of details */}
657         {this.state.showRemoveDialog && (
658           <form
659             class="form-inline"
660             onSubmit={linkEvent(this, this.handleModRemoveSubmit)}
661           >
662             <input
663               type="text"
664               class="form-control mr-2"
665               placeholder={i18n.t('reason')}
666               value={this.state.removeReason}
667               onInput={linkEvent(this, this.handleModRemoveReasonChange)}
668             />
669             <button type="submit" class="btn btn-secondary">
670               {i18n.t('remove_comment')}
671             </button>
672           </form>
673         )}
674         {this.state.showBanDialog && (
675           <form onSubmit={linkEvent(this, this.handleModBanBothSubmit)}>
676             <div class="form-group row">
677               <label class="col-form-label">{i18n.t('reason')}</label>
678               <input
679                 type="text"
680                 class="form-control mr-2"
681                 placeholder={i18n.t('reason')}
682                 value={this.state.banReason}
683                 onInput={linkEvent(this, this.handleModBanReasonChange)}
684               />
685             </div>
686             {/* TODO hold off on expires until later */}
687             {/* <div class="form-group row"> */}
688             {/*   <label class="col-form-label">Expires</label> */}
689             {/*   <input type="date" class="form-control mr-2" placeholder={i18n.t('expires')} value={this.state.banExpires} onInput={linkEvent(this, this.handleModBanExpiresChange)} /> */}
690             {/* </div> */}
691             <div class="form-group row">
692               <button type="submit" class="btn btn-secondary">
693                 {i18n.t('ban')} {node.comment.creator_name}
694               </button>
695             </div>
696           </form>
697         )}
698         {this.state.showReply && (
699           <CommentForm
700             node={node}
701             onReplyCancel={this.handleReplyCancel}
702             disabled={this.props.locked}
703           />
704         )}
705         {node.children && !this.state.collapsed && (
706           <CommentNodes
707             nodes={node.children}
708             locked={this.props.locked}
709             moderators={this.props.moderators}
710             admins={this.props.admins}
711             postCreatorId={this.props.postCreatorId}
712             sort={this.props.sort}
713             sortType={this.props.sortType}
714           />
715         )}
716         {/* A collapsed clearfix */}
717         {this.state.collapsed && <div class="row col-12"></div>}
718       </div>
719     );
720   }
721
722   get linkBtn() {
723     let node = this.props.node;
724     return (
725       <Link
726         class="btn btn-link btn-animate text-muted"
727         to={`/post/${node.comment.post_id}/comment/${node.comment.id}`}
728         title={this.props.showContext ? i18n.t('show_context') : i18n.t('link')}
729       >
730         <svg class="icon icon-inline">
731           <use xlinkHref="#icon-link"></use>
732         </svg>
733       </Link>
734     );
735   }
736
737   get loadingIcon() {
738     return (
739       <svg class="icon icon-spinner spin">
740         <use xlinkHref="#icon-spinner"></use>
741       </svg>
742     );
743   }
744
745   get myComment(): boolean {
746     return (
747       UserService.Instance.user &&
748       this.props.node.comment.creator_id == UserService.Instance.user.id
749     );
750   }
751
752   get isMod(): boolean {
753     return (
754       this.props.moderators &&
755       isMod(
756         this.props.moderators.map(m => m.user_id),
757         this.props.node.comment.creator_id
758       )
759     );
760   }
761
762   get isAdmin(): boolean {
763     return (
764       this.props.admins &&
765       isMod(
766         this.props.admins.map(a => a.id),
767         this.props.node.comment.creator_id
768       )
769     );
770   }
771
772   get isPostCreator(): boolean {
773     return this.props.node.comment.creator_id == this.props.postCreatorId;
774   }
775
776   get canMod(): boolean {
777     if (this.props.admins && this.props.moderators) {
778       let adminsThenMods = this.props.admins
779         .map(a => a.id)
780         .concat(this.props.moderators.map(m => m.user_id));
781
782       return canMod(
783         UserService.Instance.user,
784         adminsThenMods,
785         this.props.node.comment.creator_id
786       );
787     } else {
788       return false;
789     }
790   }
791
792   get canAdmin(): boolean {
793     return (
794       this.props.admins &&
795       canMod(
796         UserService.Instance.user,
797         this.props.admins.map(a => a.id),
798         this.props.node.comment.creator_id
799       )
800     );
801   }
802
803   get amCommunityCreator(): boolean {
804     return (
805       this.props.moderators &&
806       UserService.Instance.user &&
807       this.props.node.comment.creator_id != UserService.Instance.user.id &&
808       UserService.Instance.user.id == this.props.moderators[0].user_id
809     );
810   }
811
812   get amSiteCreator(): boolean {
813     return (
814       this.props.admins &&
815       UserService.Instance.user &&
816       this.props.node.comment.creator_id != UserService.Instance.user.id &&
817       UserService.Instance.user.id == this.props.admins[0].id
818     );
819   }
820
821   get commentUnlessRemoved(): string {
822     let node = this.props.node;
823     return node.comment.removed
824       ? `*${i18n.t('removed')}*`
825       : node.comment.deleted
826       ? `*${i18n.t('deleted')}*`
827       : node.comment.content;
828   }
829
830   handleReplyClick(i: CommentNode) {
831     i.state.showReply = true;
832     i.setState(i.state);
833   }
834
835   handleEditClick(i: CommentNode) {
836     i.state.showEdit = true;
837     i.setState(i.state);
838   }
839
840   handleDeleteClick(i: CommentNode) {
841     let deleteForm: CommentFormI = {
842       content: i.props.node.comment.content,
843       edit_id: i.props.node.comment.id,
844       creator_id: i.props.node.comment.creator_id,
845       post_id: i.props.node.comment.post_id,
846       parent_id: i.props.node.comment.parent_id,
847       deleted: !i.props.node.comment.deleted,
848       auth: null,
849     };
850     WebSocketService.Instance.editComment(deleteForm);
851   }
852
853   handleSaveCommentClick(i: CommentNode) {
854     let saved =
855       i.props.node.comment.saved == undefined
856         ? true
857         : !i.props.node.comment.saved;
858     let form: SaveCommentForm = {
859       comment_id: i.props.node.comment.id,
860       save: saved,
861     };
862
863     WebSocketService.Instance.saveComment(form);
864
865     i.state.saveLoading = true;
866     i.setState(this.state);
867   }
868
869   handleReplyCancel() {
870     this.state.showReply = false;
871     this.state.showEdit = false;
872     this.setState(this.state);
873   }
874
875   handleCommentUpvote(i: CommentNodeI) {
876     let new_vote = this.state.my_vote == 1 ? 0 : 1;
877
878     if (this.state.my_vote == 1) {
879       this.state.score--;
880       this.state.upvotes--;
881     } else if (this.state.my_vote == -1) {
882       this.state.downvotes--;
883       this.state.upvotes++;
884       this.state.score += 2;
885     } else {
886       this.state.upvotes++;
887       this.state.score++;
888     }
889
890     this.state.my_vote = new_vote;
891
892     let form: CommentLikeForm = {
893       comment_id: i.comment.id,
894       post_id: i.comment.post_id,
895       score: this.state.my_vote,
896     };
897
898     WebSocketService.Instance.likeComment(form);
899     this.setState(this.state);
900     setupTippy();
901   }
902
903   handleCommentDownvote(i: CommentNodeI) {
904     let new_vote = this.state.my_vote == -1 ? 0 : -1;
905
906     if (this.state.my_vote == 1) {
907       this.state.score -= 2;
908       this.state.upvotes--;
909       this.state.downvotes++;
910     } else if (this.state.my_vote == -1) {
911       this.state.downvotes--;
912       this.state.score++;
913     } else {
914       this.state.downvotes++;
915       this.state.score--;
916     }
917
918     this.state.my_vote = new_vote;
919
920     let form: CommentLikeForm = {
921       comment_id: i.comment.id,
922       post_id: i.comment.post_id,
923       score: this.state.my_vote,
924     };
925
926     WebSocketService.Instance.likeComment(form);
927     this.setState(this.state);
928     setupTippy();
929   }
930
931   handleModRemoveShow(i: CommentNode) {
932     i.state.showRemoveDialog = true;
933     i.setState(i.state);
934   }
935
936   handleModRemoveReasonChange(i: CommentNode, event: any) {
937     i.state.removeReason = event.target.value;
938     i.setState(i.state);
939   }
940
941   handleModRemoveSubmit(i: CommentNode) {
942     event.preventDefault();
943     let form: CommentFormI = {
944       content: i.props.node.comment.content,
945       edit_id: i.props.node.comment.id,
946       creator_id: i.props.node.comment.creator_id,
947       post_id: i.props.node.comment.post_id,
948       parent_id: i.props.node.comment.parent_id,
949       removed: !i.props.node.comment.removed,
950       reason: i.state.removeReason,
951       auth: null,
952     };
953     WebSocketService.Instance.editComment(form);
954
955     i.state.showRemoveDialog = false;
956     i.setState(i.state);
957   }
958
959   handleMarkRead(i: CommentNode) {
960     // if it has a user_mention_id field, then its a mention
961     if (i.props.node.comment.user_mention_id) {
962       let form: EditUserMentionForm = {
963         user_mention_id: i.props.node.comment.user_mention_id,
964         read: !i.props.node.comment.read,
965       };
966       WebSocketService.Instance.editUserMention(form);
967     } else {
968       let form: CommentFormI = {
969         content: i.props.node.comment.content,
970         edit_id: i.props.node.comment.id,
971         creator_id: i.props.node.comment.creator_id,
972         post_id: i.props.node.comment.post_id,
973         parent_id: i.props.node.comment.parent_id,
974         read: !i.props.node.comment.read,
975         auth: null,
976       };
977       WebSocketService.Instance.editComment(form);
978     }
979
980     i.state.readLoading = true;
981     i.setState(this.state);
982   }
983
984   handleModBanFromCommunityShow(i: CommentNode) {
985     i.state.showBanDialog = !i.state.showBanDialog;
986     i.state.banType = BanType.Community;
987     i.setState(i.state);
988   }
989
990   handleModBanShow(i: CommentNode) {
991     i.state.showBanDialog = !i.state.showBanDialog;
992     i.state.banType = BanType.Site;
993     i.setState(i.state);
994   }
995
996   handleModBanReasonChange(i: CommentNode, event: any) {
997     i.state.banReason = event.target.value;
998     i.setState(i.state);
999   }
1000
1001   handleModBanExpiresChange(i: CommentNode, event: any) {
1002     i.state.banExpires = event.target.value;
1003     i.setState(i.state);
1004   }
1005
1006   handleModBanFromCommunitySubmit(i: CommentNode) {
1007     i.state.banType = BanType.Community;
1008     i.setState(i.state);
1009     i.handleModBanBothSubmit(i);
1010   }
1011
1012   handleModBanSubmit(i: CommentNode) {
1013     i.state.banType = BanType.Site;
1014     i.setState(i.state);
1015     i.handleModBanBothSubmit(i);
1016   }
1017
1018   handleModBanBothSubmit(i: CommentNode) {
1019     event.preventDefault();
1020
1021     if (i.state.banType == BanType.Community) {
1022       let form: BanFromCommunityForm = {
1023         user_id: i.props.node.comment.creator_id,
1024         community_id: i.props.node.comment.community_id,
1025         ban: !i.props.node.comment.banned_from_community,
1026         reason: i.state.banReason,
1027         expires: getUnixTime(i.state.banExpires),
1028       };
1029       WebSocketService.Instance.banFromCommunity(form);
1030     } else {
1031       let form: BanUserForm = {
1032         user_id: i.props.node.comment.creator_id,
1033         ban: !i.props.node.comment.banned,
1034         reason: i.state.banReason,
1035         expires: getUnixTime(i.state.banExpires),
1036       };
1037       WebSocketService.Instance.banUser(form);
1038     }
1039
1040     i.state.showBanDialog = false;
1041     i.setState(i.state);
1042   }
1043
1044   handleShowConfirmAppointAsMod(i: CommentNode) {
1045     i.state.showConfirmAppointAsMod = true;
1046     i.setState(i.state);
1047   }
1048
1049   handleCancelConfirmAppointAsMod(i: CommentNode) {
1050     i.state.showConfirmAppointAsMod = false;
1051     i.setState(i.state);
1052   }
1053
1054   handleAddModToCommunity(i: CommentNode) {
1055     let form: AddModToCommunityForm = {
1056       user_id: i.props.node.comment.creator_id,
1057       community_id: i.props.node.comment.community_id,
1058       added: !i.isMod,
1059     };
1060     WebSocketService.Instance.addModToCommunity(form);
1061     i.state.showConfirmAppointAsMod = false;
1062     i.setState(i.state);
1063   }
1064
1065   handleShowConfirmAppointAsAdmin(i: CommentNode) {
1066     i.state.showConfirmAppointAsAdmin = true;
1067     i.setState(i.state);
1068   }
1069
1070   handleCancelConfirmAppointAsAdmin(i: CommentNode) {
1071     i.state.showConfirmAppointAsAdmin = false;
1072     i.setState(i.state);
1073   }
1074
1075   handleAddAdmin(i: CommentNode) {
1076     let form: AddAdminForm = {
1077       user_id: i.props.node.comment.creator_id,
1078       added: !i.isAdmin,
1079     };
1080     WebSocketService.Instance.addAdmin(form);
1081     i.state.showConfirmAppointAsAdmin = false;
1082     i.setState(i.state);
1083   }
1084
1085   handleShowConfirmTransferCommunity(i: CommentNode) {
1086     i.state.showConfirmTransferCommunity = true;
1087     i.setState(i.state);
1088   }
1089
1090   handleCancelShowConfirmTransferCommunity(i: CommentNode) {
1091     i.state.showConfirmTransferCommunity = false;
1092     i.setState(i.state);
1093   }
1094
1095   handleTransferCommunity(i: CommentNode) {
1096     let form: TransferCommunityForm = {
1097       community_id: i.props.node.comment.community_id,
1098       user_id: i.props.node.comment.creator_id,
1099     };
1100     WebSocketService.Instance.transferCommunity(form);
1101     i.state.showConfirmTransferCommunity = false;
1102     i.setState(i.state);
1103   }
1104
1105   handleShowConfirmTransferSite(i: CommentNode) {
1106     i.state.showConfirmTransferSite = true;
1107     i.setState(i.state);
1108   }
1109
1110   handleCancelShowConfirmTransferSite(i: CommentNode) {
1111     i.state.showConfirmTransferSite = false;
1112     i.setState(i.state);
1113   }
1114
1115   handleTransferSite(i: CommentNode) {
1116     let form: TransferSiteForm = {
1117       user_id: i.props.node.comment.creator_id,
1118     };
1119     WebSocketService.Instance.transferSite(form);
1120     i.state.showConfirmTransferSite = false;
1121     i.setState(i.state);
1122   }
1123
1124   get isCommentNew(): boolean {
1125     let now = moment.utc().subtract(10, 'minutes');
1126     let then = moment.utc(this.props.node.comment.published);
1127     return now.isBefore(then);
1128   }
1129
1130   handleCommentCollapse(i: CommentNode) {
1131     i.state.collapsed = !i.state.collapsed;
1132     i.setState(i.state);
1133   }
1134
1135   handleViewSource(i: CommentNode) {
1136     i.state.viewSource = !i.state.viewSource;
1137     i.setState(i.state);
1138   }
1139
1140   handleShowAdvanced(i: CommentNode) {
1141     i.state.showAdvanced = !i.state.showAdvanced;
1142     i.setState(i.state);
1143     setupTippy();
1144   }
1145
1146   get scoreColor() {
1147     if (this.state.my_vote == 1) {
1148       return 'text-info';
1149     } else if (this.state.my_vote == -1) {
1150       return 'text-danger';
1151     } else {
1152       return 'text-muted';
1153     }
1154   }
1155
1156   get pointsTippy(): string {
1157     let points = i18n.t('number_of_points', {
1158       count: this.state.score,
1159     });
1160
1161     let upvotes = i18n.t('number_of_upvotes', {
1162       count: this.state.upvotes,
1163     });
1164
1165     let downvotes = i18n.t('number_of_downvotes', {
1166       count: this.state.downvotes,
1167     });
1168
1169     return `${points} • ${upvotes} • ${downvotes}`;
1170   }
1171 }