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