]> Untitled Git - lemmy-ui.git/blob - src/shared/components/post/post-listing.tsx
Merge branch 'main' into fix/fix-post-title-element
[lemmy-ui.git] / src / shared / components / post / post-listing.tsx
1 import classNames from "classnames";
2 import { Component, linkEvent } from "inferno";
3 import { Link } from "inferno-router";
4 import {
5   AddAdmin,
6   AddModToCommunity,
7   BanFromCommunity,
8   BanPerson,
9   BlockPerson,
10   CommunityModeratorView,
11   CreatePostLike,
12   CreatePostReport,
13   DeletePost,
14   EditPost,
15   FeaturePost,
16   Language,
17   LockPost,
18   PersonView,
19   PostView,
20   PurgePerson,
21   PurgePost,
22   RemovePost,
23   SavePost,
24   TransferCommunity,
25 } from "lemmy-js-client";
26 import { getExternalHost, getHttpBase } from "../../env";
27 import { i18n } from "../../i18next";
28 import { BanType, PostFormParams, PurgeType, VoteType } from "../../interfaces";
29 import { UserService } from "../../services";
30 import {
31   amAdmin,
32   amCommunityCreator,
33   amMod,
34   canAdmin,
35   canMod,
36   canShare,
37   futureDaysToUnixTime,
38   hostname,
39   isAdmin,
40   isBanned,
41   isImage,
42   isMod,
43   isVideo,
44   mdNoImages,
45   mdToHtml,
46   mdToHtmlInline,
47   myAuthRequired,
48   newVote,
49   numToSI,
50   relTags,
51   setupTippy,
52   share,
53   showScores,
54 } from "../../utils";
55 import { Icon, PurgeWarning, Spinner } from "../common/icon";
56 import { MomentTime } from "../common/moment-time";
57 import { PictrsImage } from "../common/pictrs-image";
58 import { CommunityLink } from "../community/community-link";
59 import { PersonListing } from "../person/person-listing";
60 import { MetadataCard } from "./metadata-card";
61 import { PostForm } from "./post-form";
62
63 interface PostListingState {
64   showEdit: boolean;
65   showRemoveDialog: boolean;
66   showPurgeDialog: boolean;
67   purgeReason?: string;
68   purgeType?: PurgeType;
69   purgeLoading: boolean;
70   removeReason?: string;
71   showBanDialog: boolean;
72   banReason?: string;
73   banExpireDays?: number;
74   banType?: BanType;
75   removeData?: boolean;
76   showConfirmTransferSite: boolean;
77   showConfirmTransferCommunity: boolean;
78   imageExpanded: boolean;
79   viewSource: boolean;
80   showAdvanced: boolean;
81   showMoreMobile: boolean;
82   showBody: boolean;
83   showReportDialog: boolean;
84   reportReason?: string;
85   upvoteLoading: boolean;
86   downvoteLoading: boolean;
87   reportLoading: boolean;
88   blockLoading: boolean;
89   lockLoading: boolean;
90   deleteLoading: boolean;
91   removeLoading: boolean;
92   saveLoading: boolean;
93   featureCommunityLoading: boolean;
94   featureLocalLoading: boolean;
95   banLoading: boolean;
96   addModLoading: boolean;
97   addAdminLoading: boolean;
98   transferLoading: boolean;
99 }
100
101 interface PostListingProps {
102   post_view: PostView;
103   crossPosts?: PostView[];
104   moderators?: CommunityModeratorView[];
105   admins?: PersonView[];
106   allLanguages: Language[];
107   siteLanguages: number[];
108   showCommunity?: boolean;
109   showBody?: boolean;
110   hideImage?: boolean;
111   enableDownvotes?: boolean;
112   enableNsfw?: boolean;
113   viewOnly?: boolean;
114   onPostEdit(form: EditPost): void;
115   onPostVote(form: CreatePostLike): void;
116   onPostReport(form: CreatePostReport): void;
117   onBlockPerson(form: BlockPerson): void;
118   onLockPost(form: LockPost): void;
119   onDeletePost(form: DeletePost): void;
120   onRemovePost(form: RemovePost): void;
121   onSavePost(form: SavePost): void;
122   onFeaturePost(form: FeaturePost): void;
123   onPurgePerson(form: PurgePerson): void;
124   onPurgePost(form: PurgePost): void;
125   onBanPersonFromCommunity(form: BanFromCommunity): void;
126   onBanPerson(form: BanPerson): void;
127   onAddModToCommunity(form: AddModToCommunity): void;
128   onAddAdmin(form: AddAdmin): void;
129   onTransferCommunity(form: TransferCommunity): void;
130 }
131
132 export class PostListing extends Component<PostListingProps, PostListingState> {
133   state: PostListingState = {
134     showEdit: false,
135     showRemoveDialog: false,
136     showPurgeDialog: false,
137     purgeType: PurgeType.Person,
138     showBanDialog: false,
139     banType: BanType.Community,
140     removeData: false,
141     showConfirmTransferSite: false,
142     showConfirmTransferCommunity: false,
143     imageExpanded: false,
144     viewSource: false,
145     showAdvanced: false,
146     showMoreMobile: false,
147     showBody: false,
148     showReportDialog: false,
149     upvoteLoading: false,
150     downvoteLoading: false,
151     purgeLoading: false,
152     reportLoading: false,
153     blockLoading: false,
154     lockLoading: false,
155     deleteLoading: false,
156     removeLoading: false,
157     saveLoading: false,
158     featureCommunityLoading: false,
159     featureLocalLoading: false,
160     banLoading: false,
161     addModLoading: false,
162     addAdminLoading: false,
163     transferLoading: false,
164   };
165
166   constructor(props: any, context: any) {
167     super(props, context);
168
169     this.handleEditPost = this.handleEditPost.bind(this);
170     this.handleEditCancel = this.handleEditCancel.bind(this);
171   }
172
173   componentWillReceiveProps(nextProps: PostListingProps) {
174     if (this.props !== nextProps) {
175       this.setState({
176         upvoteLoading: false,
177         downvoteLoading: false,
178         purgeLoading: false,
179         reportLoading: false,
180         blockLoading: false,
181         lockLoading: false,
182         deleteLoading: false,
183         removeLoading: false,
184         saveLoading: false,
185         featureCommunityLoading: false,
186         featureLocalLoading: false,
187         banLoading: false,
188         addModLoading: false,
189         addAdminLoading: false,
190         transferLoading: false,
191         imageExpanded: false,
192       });
193     }
194   }
195
196   get postView(): PostView {
197     return this.props.post_view;
198   }
199
200   render() {
201     const post = this.postView.post;
202
203     return (
204       <div className="post-listing">
205         {!this.state.showEdit ? (
206           <>
207             {this.listing()}
208             {this.state.imageExpanded && !this.props.hideImage && this.img}
209             {post.url && this.state.showBody && post.embed_title && (
210               <MetadataCard post={post} />
211             )}
212             {this.showBody && this.body()}
213           </>
214         ) : (
215           <PostForm
216             post_view={this.postView}
217             crossPosts={this.props.crossPosts}
218             onEdit={this.handleEditPost}
219             onCancel={this.handleEditCancel}
220             enableNsfw={this.props.enableNsfw}
221             enableDownvotes={this.props.enableDownvotes}
222             allLanguages={this.props.allLanguages}
223             siteLanguages={this.props.siteLanguages}
224           />
225         )}
226       </div>
227     );
228   }
229
230   body() {
231     const body = this.postView.post.body;
232     return body ? (
233       <article id="postContent" className="col-12 card my-2 p-2">
234         {this.state.viewSource ? (
235           <pre>{body}</pre>
236         ) : (
237           <div className="md-div" dangerouslySetInnerHTML={mdToHtml(body)} />
238         )}
239       </article>
240     ) : (
241       <></>
242     );
243   }
244
245   get img() {
246     return this.imageSrc ? (
247       <>
248         <div className="offset-sm-3 my-2 d-none d-sm-block">
249           <a href={this.imageSrc} className="d-inline-block">
250             <PictrsImage src={this.imageSrc} />
251           </a>
252         </div>
253         <div className="my-2 d-block d-sm-none">
254           <a
255             className="d-inline-block"
256             onClick={linkEvent(this, this.handleImageExpandClick)}
257           >
258             <PictrsImage src={this.imageSrc} />
259           </a>
260         </div>
261       </>
262     ) : (
263       <></>
264     );
265   }
266
267   imgThumb(src: string) {
268     const post_view = this.postView;
269     return (
270       <PictrsImage
271         src={src}
272         thumbnail
273         alt=""
274         nsfw={post_view.post.nsfw || post_view.community.nsfw}
275       />
276     );
277   }
278
279   get imageSrc(): string | undefined {
280     const post = this.postView.post;
281     const url = post.url;
282     const thumbnail = post.thumbnail_url;
283
284     if (url && isImage(url)) {
285       if (url.includes("pictrs")) {
286         return url;
287       } else if (thumbnail) {
288         return thumbnail;
289       } else {
290         return url;
291       }
292     } else if (thumbnail) {
293       return thumbnail;
294     } else {
295       return undefined;
296     }
297   }
298
299   thumbnail() {
300     const post = this.postView.post;
301     const url = post.url;
302     const thumbnail = post.thumbnail_url;
303
304     if (!this.props.hideImage && url && isImage(url) && this.imageSrc) {
305       return (
306         <a
307           href={this.imageSrc}
308           className="text-body d-inline-block position-relative mb-2"
309           data-tippy-content={i18n.t("expand_here")}
310           onClick={linkEvent(this, this.handleImageExpandClick)}
311           aria-label={i18n.t("expand_here")}
312         >
313           {this.imgThumb(this.imageSrc)}
314           <Icon icon="image" classes="mini-overlay" />
315         </a>
316       );
317     } else if (!this.props.hideImage && url && thumbnail && this.imageSrc) {
318       return (
319         <a
320           className="text-body d-inline-block position-relative mb-2"
321           href={url}
322           rel={relTags}
323           title={url}
324         >
325           {this.imgThumb(this.imageSrc)}
326           <Icon icon="external-link" classes="mini-overlay" />
327         </a>
328       );
329     } else if (url) {
330       if (!this.props.hideImage && isVideo(url)) {
331         return (
332           <div className="embed-responsive embed-responsive-16by9">
333             <video
334               playsInline
335               muted
336               loop
337               controls
338               className="embed-responsive-item"
339             >
340               <source src={url} type="video/mp4" />
341             </video>
342           </div>
343         );
344       } else {
345         return (
346           <a className="text-body" href={url} title={url} rel={relTags}>
347             <div className="thumbnail rounded bg-light d-flex justify-content-center">
348               <Icon icon="external-link" classes="d-flex align-items-center" />
349             </div>
350           </a>
351         );
352       }
353     } else {
354       return (
355         <Link
356           className="text-body"
357           to={`/post/${post.id}`}
358           title={i18n.t("comments")}
359         >
360           <div className="thumbnail rounded bg-light d-flex justify-content-center">
361             <Icon icon="message-square" classes="d-flex align-items-center" />
362           </div>
363         </Link>
364       );
365     }
366   }
367
368   createdLine() {
369     const post_view = this.postView;
370     const url = post_view.post.url;
371     const body = post_view.post.body;
372     return (
373       <ul className="list-inline mb-1 text-muted small">
374         <li className="list-inline-item">
375           <PersonListing person={post_view.creator} />
376
377           {this.creatorIsMod_ && (
378             <span className="mx-1 badge">{i18n.t("mod")}</span>
379           )}
380           {this.creatorIsAdmin_ && (
381             <span className="mx-1 badge">{i18n.t("admin")}</span>
382           )}
383           {post_view.creator.bot_account && (
384             <span className="mx-1 badge">
385               {i18n.t("bot_account").toLowerCase()}
386             </span>
387           )}
388           {this.props.showCommunity && (
389             <span>
390               <span className="mx-1"> {i18n.t("to")} </span>
391               <CommunityLink community={post_view.community} />
392             </span>
393           )}
394         </li>
395         {post_view.post.language_id !== 0 && (
396           <span className="mx-1 badge">
397             {
398               this.props.allLanguages.find(
399                 lang => lang.id === post_view.post.language_id
400               )?.name
401             }
402           </span>
403         )}
404         <li className="list-inline-item">•</li>
405         {url && !(hostname(url) === getExternalHost()) && (
406           <>
407             <li className="list-inline-item">
408               <a
409                 className="text-muted font-italic"
410                 href={url}
411                 title={url}
412                 rel={relTags}
413               >
414                 {hostname(url)}
415               </a>
416             </li>
417             <li className="list-inline-item">•</li>
418           </>
419         )}
420         <li className="list-inline-item">
421           <span>
422             <MomentTime
423               published={post_view.post.published}
424               updated={post_view.post.updated}
425             />
426           </span>
427         </li>
428         {body && (
429           <>
430             <li className="list-inline-item">•</li>
431             <li className="list-inline-item">
432               <button
433                 className="text-muted btn btn-sm btn-link p-0"
434                 data-tippy-content={mdNoImages.render(body)}
435                 data-tippy-allowHtml={true}
436                 onClick={linkEvent(this, this.handleShowBody)}
437               >
438                 <Icon icon="book-open" classes="icon-inline mr-1" />
439               </button>
440             </li>
441           </>
442         )}
443       </ul>
444     );
445   }
446
447   voteBar() {
448     return (
449       <div className={`vote-bar col-1 pr-0 small text-center`}>
450         <button
451           className={`btn-animate btn btn-link p-0 ${
452             this.postView.my_vote == 1 ? "text-info" : "text-muted"
453           }`}
454           onClick={linkEvent(this, this.handleUpvote)}
455           data-tippy-content={i18n.t("upvote")}
456           aria-label={i18n.t("upvote")}
457           aria-pressed={this.postView.my_vote === 1}
458         >
459           {this.state.upvoteLoading ? (
460             <Spinner />
461           ) : (
462             <Icon icon="arrow-up1" classes="upvote" />
463           )}
464         </button>
465         {showScores() ? (
466           <div
467             className={`unselectable pointer font-weight-bold text-muted px-1 post-score`}
468             data-tippy-content={this.pointsTippy}
469           >
470             {numToSI(this.postView.counts.score)}
471           </div>
472         ) : (
473           <div className="p-1"></div>
474         )}
475         {this.props.enableDownvotes && (
476           <button
477             className={`btn-animate btn btn-link p-0 ${
478               this.postView.my_vote == -1 ? "text-danger" : "text-muted"
479             }`}
480             onClick={linkEvent(this, this.handleDownvote)}
481             data-tippy-content={i18n.t("downvote")}
482             aria-label={i18n.t("downvote")}
483             aria-pressed={this.postView.my_vote === -1}
484           >
485             {this.state.downvoteLoading ? (
486               <Spinner />
487             ) : (
488               <Icon icon="arrow-down1" classes="downvote" />
489             )}
490           </button>
491         )}
492       </div>
493     );
494   }
495
496   get postLink() {
497     const post = this.postView.post;
498     return (
499       <Link
500         className={`d-inline ${
501           !post.featured_community && !post.featured_local
502             ? "text-body"
503             : "text-primary"
504         }`}
505         to={`/post/${post.id}`}
506         title={i18n.t("comments")}
507       >
508         <span
509           className="d-inline"
510           dangerouslySetInnerHTML={mdToHtmlInline(post.name)}
511         />
512       </Link>
513     );
514   }
515
516   postTitleLine() {
517     const post = this.postView.post;
518     const url = post.url;
519
520     return (
521       <div className="post-title overflow-hidden">
522         <h5 className="d-inline">
523           {url && this.props.showBody ? (
524             <a
525               className={
526                 !post.featured_community && !post.featured_local
527                   ? "text-body"
528                   : "text-primary"
529               }
530               href={url}
531               title={url}
532               rel={relTags}
533               dangerouslySetInnerHTML={mdToHtmlInline(post.name)}
534             ></a>
535           ) : (
536             this.postLink
537           )}
538         </h5>
539         {(url && isImage(url)) ||
540           (post.thumbnail_url && (
541             <button
542               className="btn btn-link text-monospace text-muted small d-inline-block"
543               data-tippy-content={i18n.t("expand_here")}
544               onClick={linkEvent(this, this.handleImageExpandClick)}
545             >
546               <Icon
547                 icon={
548                   !this.state.imageExpanded ? "plus-square" : "minus-square"
549                 }
550                 classes="icon-inline"
551               />
552             </button>
553           ))}
554         {post.removed && (
555           <small className="ml-2 text-muted font-italic">
556             {i18n.t("removed")}
557           </small>
558         )}
559         {post.deleted && (
560           <small
561             className="unselectable pointer ml-2 text-muted font-italic"
562             data-tippy-content={i18n.t("deleted")}
563           >
564             <Icon icon="trash" classes="icon-inline text-danger" />
565           </small>
566         )}
567         {post.locked && (
568           <small
569             className="unselectable pointer ml-2 text-muted font-italic"
570             data-tippy-content={i18n.t("locked")}
571           >
572             <Icon icon="lock" classes="icon-inline text-danger" />
573           </small>
574         )}
575         {post.featured_community && (
576           <small
577             className="unselectable pointer ml-2 text-muted font-italic"
578             data-tippy-content={i18n.t("featured")}
579           >
580             <Icon icon="pin" classes="icon-inline text-primary" />
581           </small>
582         )}
583         {post.featured_local && (
584           <small
585             className="unselectable pointer ml-2 text-muted font-italic"
586             data-tippy-content={i18n.t("featured")}
587           >
588             <Icon icon="pin" classes="icon-inline text-secondary" />
589           </small>
590         )}
591         {post.nsfw && (
592           <small className="ml-2 text-muted font-italic">
593             {i18n.t("nsfw")}
594           </small>
595         )}
596       </div>
597     );
598   }
599
600   duplicatesLine() {
601     const dupes = this.props.crossPosts;
602     return dupes && dupes.length > 0 ? (
603       <ul className="list-inline mb-1 small text-muted">
604         <>
605           <li className="list-inline-item mr-2">{i18n.t("cross_posted_to")}</li>
606           {dupes.map(pv => (
607             <li key={pv.post.id} className="list-inline-item mr-2">
608               <Link to={`/post/${pv.post.id}`}>
609                 {pv.community.local
610                   ? pv.community.name
611                   : `${pv.community.name}@${hostname(pv.community.actor_id)}`}
612               </Link>
613             </li>
614           ))}
615         </>
616       </ul>
617     ) : (
618       <></>
619     );
620   }
621
622   commentsLine(mobile = false) {
623     const post = this.postView.post;
624
625     return (
626       <div className="d-flex align-items-center justify-content-start flex-wrap text-muted font-weight-bold mb-1">
627         {this.commentsButton}
628         {canShare() && (
629           <button
630             className="btn btn-link"
631             onClick={linkEvent(this, this.handleShare)}
632             type="button"
633           >
634             <Icon icon="share" inline />
635           </button>
636         )}
637         {!post.local && (
638           <a
639             className="btn btn-link btn-animate text-muted py-0"
640             title={i18n.t("link")}
641             href={post.ap_id}
642           >
643             <Icon icon="fedilink" inline />
644           </a>
645         )}
646         {mobile && !this.props.viewOnly && this.mobileVotes}
647         {UserService.Instance.myUserInfo &&
648           !this.props.viewOnly &&
649           this.postActions(mobile)}
650       </div>
651     );
652   }
653
654   postActions(mobile = false) {
655     // Possible enhancement: Priority+ pattern instead of just hard coding which get hidden behind the show more button.
656     // Possible enhancement: Make each button a component.
657     const post_view = this.postView;
658     return (
659       <>
660         {this.saveButton}
661         {this.crossPostButton}
662         {mobile && this.showMoreButton}
663         {(!mobile || this.state.showAdvanced) && (
664           <>
665             {!this.myPost && (
666               <>
667                 {this.reportButton}
668                 {this.blockButton}
669               </>
670             )}
671             {this.myPost && (this.showBody || this.state.showAdvanced) && (
672               <>
673                 {this.editButton}
674                 {this.deleteButton}
675               </>
676             )}
677           </>
678         )}
679         {this.state.showAdvanced && (
680           <>
681             {this.showBody && post_view.post.body && this.viewSourceButton}
682             {/* Any mod can do these, not limited to hierarchy*/}
683             {(amMod(this.props.moderators) || amAdmin()) && (
684               <>
685                 {this.lockButton}
686                 {this.featureButton}
687               </>
688             )}
689             {(this.canMod_ || this.canAdmin_) && <>{this.modRemoveButton}</>}
690           </>
691         )}
692         {!mobile && this.showMoreButton}
693       </>
694     );
695   }
696
697   get commentsButton() {
698     const post_view = this.postView;
699     return (
700       <Link
701         className="btn btn-link text-muted py-0 pl-0 text-muted"
702         title={i18n.t("number_of_comments", {
703           count: Number(post_view.counts.comments),
704           formattedCount: Number(post_view.counts.comments),
705         })}
706         to={`/post/${post_view.post.id}?scrollToComments=true`}
707       >
708         <Icon icon="message-square" classes="mr-1" inline />
709         <span className="mr-2">
710           {i18n.t("number_of_comments", {
711             count: Number(post_view.counts.comments),
712             formattedCount: numToSI(post_view.counts.comments),
713           })}
714         </span>
715         {this.unreadCount && (
716           <span className="small text-warning">
717             ({this.unreadCount} {i18n.t("new")})
718           </span>
719         )}
720       </Link>
721     );
722   }
723
724   get unreadCount(): number | undefined {
725     const pv = this.postView;
726     return pv.unread_comments == pv.counts.comments || pv.unread_comments == 0
727       ? undefined
728       : pv.unread_comments;
729   }
730
731   get mobileVotes() {
732     // TODO: make nicer
733     const tippy = showScores()
734       ? { "data-tippy-content": this.pointsTippy }
735       : {};
736     return (
737       <>
738         <div>
739           <button
740             className={`btn-animate btn py-0 px-1 ${
741               this.postView.my_vote === 1 ? "text-info" : "text-muted"
742             }`}
743             {...tippy}
744             onClick={linkEvent(this, this.handleUpvote)}
745             aria-label={i18n.t("upvote")}
746             aria-pressed={this.postView.my_vote === 1}
747           >
748             {this.state.upvoteLoading ? (
749               <Spinner />
750             ) : (
751               <>
752                 <Icon icon="arrow-up1" classes="icon-inline small" />
753                 {showScores() && (
754                   <span className="ml-2">
755                     {numToSI(this.postView.counts.upvotes)}
756                   </span>
757                 )}
758               </>
759             )}
760           </button>
761           {this.props.enableDownvotes && (
762             <button
763               className={`ml-2 btn-animate btn py-0 px-1 ${
764                 this.postView.my_vote === -1 ? "text-danger" : "text-muted"
765               }`}
766               onClick={linkEvent(this, this.handleDownvote)}
767               {...tippy}
768               aria-label={i18n.t("downvote")}
769               aria-pressed={this.postView.my_vote === -1}
770             >
771               {this.state.downvoteLoading ? (
772                 <Spinner />
773               ) : (
774                 <>
775                   <Icon icon="arrow-down1" classes="icon-inline small" />
776                   {showScores() && (
777                     <span
778                       className={classNames("ml-2", {
779                         invisible: this.postView.counts.downvotes === 0,
780                       })}
781                     >
782                       {numToSI(this.postView.counts.downvotes)}
783                     </span>
784                   )}
785                 </>
786               )}
787             </button>
788           )}
789         </div>
790       </>
791     );
792   }
793
794   get saveButton() {
795     const saved = this.postView.saved;
796     const label = saved ? i18n.t("unsave") : i18n.t("save");
797     return (
798       <button
799         className="btn btn-link btn-animate text-muted py-0"
800         onClick={linkEvent(this, this.handleSavePostClick)}
801         data-tippy-content={label}
802         aria-label={label}
803       >
804         {this.state.saveLoading ? (
805           <Spinner />
806         ) : (
807           <Icon
808             icon="star"
809             classes={classNames({ "text-warning": saved })}
810             inline
811           />
812         )}
813       </button>
814     );
815   }
816
817   get crossPostButton() {
818     return (
819       <Link
820         className="btn btn-link btn-animate text-muted py-0"
821         to={{
822           /* Empty string properties are required to satisfy type*/
823           pathname: "/create_post",
824           state: { ...this.crossPostParams },
825           hash: "",
826           key: "",
827           search: "",
828         }}
829         title={i18n.t("cross_post")}
830         data-tippy-content={i18n.t("cross_post")}
831         aria-label={i18n.t("cross_post")}
832       >
833         <Icon icon="copy" inline />
834       </Link>
835     );
836   }
837
838   get reportButton() {
839     return (
840       <button
841         className="btn btn-link btn-animate text-muted py-0"
842         onClick={linkEvent(this, this.handleShowReportDialog)}
843         data-tippy-content={i18n.t("show_report_dialog")}
844         aria-label={i18n.t("show_report_dialog")}
845       >
846         <Icon icon="flag" inline />
847       </button>
848     );
849   }
850
851   get blockButton() {
852     return (
853       <button
854         className="btn btn-link btn-animate text-muted py-0"
855         onClick={linkEvent(this, this.handleBlockPersonClick)}
856         data-tippy-content={i18n.t("block_user")}
857         aria-label={i18n.t("block_user")}
858       >
859         {this.state.blockLoading ? <Spinner /> : <Icon icon="slash" inline />}
860       </button>
861     );
862   }
863
864   get editButton() {
865     return (
866       <button
867         className="btn btn-link btn-animate text-muted py-0"
868         onClick={linkEvent(this, this.handleEditClick)}
869         data-tippy-content={i18n.t("edit")}
870         aria-label={i18n.t("edit")}
871       >
872         <Icon icon="edit" inline />
873       </button>
874     );
875   }
876
877   get deleteButton() {
878     const deleted = this.postView.post.deleted;
879     const label = !deleted ? i18n.t("delete") : i18n.t("restore");
880     return (
881       <button
882         className="btn btn-link btn-animate text-muted py-0"
883         onClick={linkEvent(this, this.handleDeleteClick)}
884         data-tippy-content={label}
885         aria-label={label}
886       >
887         {this.state.deleteLoading ? (
888           <Spinner />
889         ) : (
890           <Icon
891             icon="trash"
892             classes={classNames({ "text-danger": deleted })}
893             inline
894           />
895         )}
896       </button>
897     );
898   }
899
900   get showMoreButton() {
901     return (
902       <button
903         className="btn btn-link btn-animate text-muted py-0"
904         onClick={linkEvent(this, this.handleShowAdvanced)}
905         data-tippy-content={i18n.t("more")}
906         aria-label={i18n.t("more")}
907       >
908         <Icon icon="more-vertical" inline />
909       </button>
910     );
911   }
912
913   get viewSourceButton() {
914     return (
915       <button
916         className="btn btn-link btn-animate text-muted py-0"
917         onClick={linkEvent(this, this.handleViewSource)}
918         data-tippy-content={i18n.t("view_source")}
919         aria-label={i18n.t("view_source")}
920       >
921         <Icon
922           icon="file-text"
923           classes={classNames({ "text-success": this.state.viewSource })}
924           inline
925         />
926       </button>
927     );
928   }
929
930   get lockButton() {
931     const locked = this.postView.post.locked;
932     const label = locked ? i18n.t("unlock") : i18n.t("lock");
933     return (
934       <button
935         className="btn btn-link btn-animate text-muted py-0"
936         onClick={linkEvent(this, this.handleModLock)}
937         data-tippy-content={label}
938         aria-label={label}
939       >
940         {this.state.lockLoading ? (
941           <Spinner />
942         ) : (
943           <Icon
944             icon="lock"
945             classes={classNames({ "text-danger": locked })}
946             inline
947           />
948         )}
949       </button>
950     );
951   }
952
953   get featureButton() {
954     const featuredCommunity = this.postView.post.featured_community;
955     const labelCommunity = featuredCommunity
956       ? i18n.t("unfeature_from_community")
957       : i18n.t("feature_in_community");
958
959     const featuredLocal = this.postView.post.featured_local;
960     const labelLocal = featuredLocal
961       ? i18n.t("unfeature_from_local")
962       : i18n.t("feature_in_local");
963     return (
964       <span>
965         <button
966           className="btn btn-link btn-animate text-muted py-0 pl-0"
967           onClick={linkEvent(this, this.handleModFeaturePostCommunity)}
968           data-tippy-content={labelCommunity}
969           aria-label={labelCommunity}
970         >
971           {this.state.featureCommunityLoading ? (
972             <Spinner />
973           ) : (
974             <span>
975               <Icon
976                 icon="pin"
977                 classes={classNames({ "text-success": featuredCommunity })}
978                 inline
979               />
980               {i18n.t("community")}
981             </span>
982           )}
983         </button>
984         {amAdmin() && (
985           <button
986             className="btn btn-link btn-animate text-muted py-0"
987             onClick={linkEvent(this, this.handleModFeaturePostLocal)}
988             data-tippy-content={labelLocal}
989             aria-label={labelLocal}
990           >
991             {this.state.featureLocalLoading ? (
992               <Spinner />
993             ) : (
994               <span>
995                 <Icon
996                   icon="pin"
997                   classes={classNames({ "text-success": featuredLocal })}
998                   inline
999                 />
1000                 {i18n.t("local")}
1001               </span>
1002             )}
1003           </button>
1004         )}
1005       </span>
1006     );
1007   }
1008
1009   get modRemoveButton() {
1010     const removed = this.postView.post.removed;
1011     return (
1012       <button
1013         className="btn btn-link btn-animate text-muted py-0"
1014         onClick={linkEvent(
1015           this,
1016           !removed ? this.handleModRemoveShow : this.handleModRemoveSubmit
1017         )}
1018       >
1019         {/* TODO: Find an icon for this. */}
1020         {this.state.removeLoading ? (
1021           <Spinner />
1022         ) : !removed ? (
1023           i18n.t("remove")
1024         ) : (
1025           i18n.t("restore")
1026         )}
1027       </button>
1028     );
1029   }
1030
1031   /**
1032    * Mod/Admin actions to be taken against the author.
1033    */
1034   userActionsLine() {
1035     // TODO: make nicer
1036     const post_view = this.postView;
1037     return (
1038       this.state.showAdvanced && (
1039         <>
1040           {this.canMod_ && (
1041             <>
1042               {!this.creatorIsMod_ &&
1043                 (!post_view.creator_banned_from_community ? (
1044                   <button
1045                     className="btn btn-link btn-animate text-muted py-0"
1046                     onClick={linkEvent(
1047                       this,
1048                       this.handleModBanFromCommunityShow
1049                     )}
1050                     aria-label={i18n.t("ban_from_community")}
1051                   >
1052                     {i18n.t("ban_from_community")}
1053                   </button>
1054                 ) : (
1055                   <button
1056                     className="btn btn-link btn-animate text-muted py-0"
1057                     onClick={linkEvent(
1058                       this,
1059                       this.handleModBanFromCommunitySubmit
1060                     )}
1061                     aria-label={i18n.t("unban")}
1062                   >
1063                     {this.state.banLoading ? <Spinner /> : i18n.t("unban")}
1064                   </button>
1065                 ))}
1066               {!post_view.creator_banned_from_community && (
1067                 <button
1068                   className="btn btn-link btn-animate text-muted py-0"
1069                   onClick={linkEvent(this, this.handleAddModToCommunity)}
1070                   aria-label={
1071                     this.creatorIsMod_
1072                       ? i18n.t("remove_as_mod")
1073                       : i18n.t("appoint_as_mod")
1074                   }
1075                 >
1076                   {this.state.addModLoading ? (
1077                     <Spinner />
1078                   ) : this.creatorIsMod_ ? (
1079                     i18n.t("remove_as_mod")
1080                   ) : (
1081                     i18n.t("appoint_as_mod")
1082                   )}
1083                 </button>
1084               )}
1085             </>
1086           )}
1087           {/* Community creators and admins can transfer community to another mod */}
1088           {(amCommunityCreator(post_view.creator.id, this.props.moderators) ||
1089             this.canAdmin_) &&
1090             this.creatorIsMod_ &&
1091             (!this.state.showConfirmTransferCommunity ? (
1092               <button
1093                 className="btn btn-link btn-animate text-muted py-0"
1094                 onClick={linkEvent(
1095                   this,
1096                   this.handleShowConfirmTransferCommunity
1097                 )}
1098                 aria-label={i18n.t("transfer_community")}
1099               >
1100                 {i18n.t("transfer_community")}
1101               </button>
1102             ) : (
1103               <>
1104                 <button
1105                   className="d-inline-block mr-1 btn btn-link btn-animate text-muted py-0"
1106                   aria-label={i18n.t("are_you_sure")}
1107                 >
1108                   {i18n.t("are_you_sure")}
1109                 </button>
1110                 <button
1111                   className="btn btn-link btn-animate text-muted py-0 d-inline-block mr-1"
1112                   aria-label={i18n.t("yes")}
1113                   onClick={linkEvent(this, this.handleTransferCommunity)}
1114                 >
1115                   {this.state.transferLoading ? <Spinner /> : i18n.t("yes")}
1116                 </button>
1117                 <button
1118                   className="btn btn-link btn-animate text-muted py-0 d-inline-block"
1119                   onClick={linkEvent(
1120                     this,
1121                     this.handleCancelShowConfirmTransferCommunity
1122                   )}
1123                   aria-label={i18n.t("no")}
1124                 >
1125                   {i18n.t("no")}
1126                 </button>
1127               </>
1128             ))}
1129           {/* Admins can ban from all, and appoint other admins */}
1130           {this.canAdmin_ && (
1131             <>
1132               {!this.creatorIsAdmin_ && (
1133                 <>
1134                   {!isBanned(post_view.creator) ? (
1135                     <button
1136                       className="btn btn-link btn-animate text-muted py-0"
1137                       onClick={linkEvent(this, this.handleModBanShow)}
1138                       aria-label={i18n.t("ban_from_site")}
1139                     >
1140                       {i18n.t("ban_from_site")}
1141                     </button>
1142                   ) : (
1143                     <button
1144                       className="btn btn-link btn-animate text-muted py-0"
1145                       onClick={linkEvent(this, this.handleModBanSubmit)}
1146                       aria-label={i18n.t("unban_from_site")}
1147                     >
1148                       {this.state.banLoading ? (
1149                         <Spinner />
1150                       ) : (
1151                         i18n.t("unban_from_site")
1152                       )}
1153                     </button>
1154                   )}
1155                   <button
1156                     className="btn btn-link btn-animate text-muted py-0"
1157                     onClick={linkEvent(this, this.handlePurgePersonShow)}
1158                     aria-label={i18n.t("purge_user")}
1159                   >
1160                     {i18n.t("purge_user")}
1161                   </button>
1162                   <button
1163                     className="btn btn-link btn-animate text-muted py-0"
1164                     onClick={linkEvent(this, this.handlePurgePostShow)}
1165                     aria-label={i18n.t("purge_post")}
1166                   >
1167                     {i18n.t("purge_post")}
1168                   </button>
1169                 </>
1170               )}
1171               {!isBanned(post_view.creator) && post_view.creator.local && (
1172                 <button
1173                   className="btn btn-link btn-animate text-muted py-0"
1174                   onClick={linkEvent(this, this.handleAddAdmin)}
1175                   aria-label={
1176                     this.creatorIsAdmin_
1177                       ? i18n.t("remove_as_admin")
1178                       : i18n.t("appoint_as_admin")
1179                   }
1180                 >
1181                   {this.state.addAdminLoading ? (
1182                     <Spinner />
1183                   ) : this.creatorIsAdmin_ ? (
1184                     i18n.t("remove_as_admin")
1185                   ) : (
1186                     i18n.t("appoint_as_admin")
1187                   )}
1188                 </button>
1189               )}
1190             </>
1191           )}
1192         </>
1193       )
1194     );
1195   }
1196
1197   removeAndBanDialogs() {
1198     const post = this.postView;
1199     const purgeTypeText =
1200       this.state.purgeType == PurgeType.Post
1201         ? i18n.t("purge_post")
1202         : `${i18n.t("purge")} ${post.creator.name}`;
1203     return (
1204       <>
1205         {this.state.showRemoveDialog && (
1206           <form
1207             className="form-inline"
1208             onSubmit={linkEvent(this, this.handleModRemoveSubmit)}
1209           >
1210             <label className="sr-only" htmlFor="post-listing-remove-reason">
1211               {i18n.t("reason")}
1212             </label>
1213             <input
1214               type="text"
1215               id="post-listing-remove-reason"
1216               className="form-control mr-2"
1217               placeholder={i18n.t("reason")}
1218               value={this.state.removeReason}
1219               onInput={linkEvent(this, this.handleModRemoveReasonChange)}
1220             />
1221             <button
1222               type="submit"
1223               className="btn btn-secondary"
1224               aria-label={i18n.t("remove_post")}
1225             >
1226               {this.state.removeLoading ? <Spinner /> : i18n.t("remove_post")}
1227             </button>
1228           </form>
1229         )}
1230         {this.state.showBanDialog && (
1231           <form onSubmit={linkEvent(this, this.handleModBanBothSubmit)}>
1232             <div className="form-group row col-12">
1233               <label
1234                 className="col-form-label"
1235                 htmlFor="post-listing-ban-reason"
1236               >
1237                 {i18n.t("reason")}
1238               </label>
1239               <input
1240                 type="text"
1241                 id="post-listing-ban-reason"
1242                 className="form-control mr-2"
1243                 placeholder={i18n.t("reason")}
1244                 value={this.state.banReason}
1245                 onInput={linkEvent(this, this.handleModBanReasonChange)}
1246               />
1247               <label className="col-form-label" htmlFor={`mod-ban-expires`}>
1248                 {i18n.t("expires")}
1249               </label>
1250               <input
1251                 type="number"
1252                 id={`mod-ban-expires`}
1253                 className="form-control mr-2"
1254                 placeholder={i18n.t("number_of_days")}
1255                 value={this.state.banExpireDays}
1256                 onInput={linkEvent(this, this.handleModBanExpireDaysChange)}
1257               />
1258               <div className="form-group">
1259                 <div className="form-check">
1260                   <input
1261                     className="form-check-input"
1262                     id="mod-ban-remove-data"
1263                     type="checkbox"
1264                     checked={this.state.removeData}
1265                     onChange={linkEvent(this, this.handleModRemoveDataChange)}
1266                   />
1267                   <label
1268                     className="form-check-label"
1269                     htmlFor="mod-ban-remove-data"
1270                     title={i18n.t("remove_content_more")}
1271                   >
1272                     {i18n.t("remove_content")}
1273                   </label>
1274                 </div>
1275               </div>
1276             </div>
1277             {/* TODO hold off on expires until later */}
1278             {/* <div class="form-group row"> */}
1279             {/*   <label class="col-form-label">Expires</label> */}
1280             {/*   <input type="date" class="form-control mr-2" placeholder={i18n.t('expires')} value={this.state.banExpires} onInput={linkEvent(this, this.handleModBanExpiresChange)} /> */}
1281             {/* </div> */}
1282             <div className="form-group row">
1283               <button
1284                 type="submit"
1285                 className="btn btn-secondary"
1286                 aria-label={i18n.t("ban")}
1287               >
1288                 {this.state.banLoading ? (
1289                   <Spinner />
1290                 ) : (
1291                   <span>
1292                     {i18n.t("ban")} {post.creator.name}
1293                   </span>
1294                 )}
1295               </button>
1296             </div>
1297           </form>
1298         )}
1299         {this.state.showReportDialog && (
1300           <form
1301             className="form-inline"
1302             onSubmit={linkEvent(this, this.handleReportSubmit)}
1303           >
1304             <label className="sr-only" htmlFor="post-report-reason">
1305               {i18n.t("reason")}
1306             </label>
1307             <input
1308               type="text"
1309               id="post-report-reason"
1310               className="form-control mr-2"
1311               placeholder={i18n.t("reason")}
1312               required
1313               value={this.state.reportReason}
1314               onInput={linkEvent(this, this.handleReportReasonChange)}
1315             />
1316             <button
1317               type="submit"
1318               className="btn btn-secondary"
1319               aria-label={i18n.t("create_report")}
1320             >
1321               {this.state.reportLoading ? <Spinner /> : i18n.t("create_report")}
1322             </button>
1323           </form>
1324         )}
1325         {this.state.showPurgeDialog && (
1326           <form
1327             className="form-inline"
1328             onSubmit={linkEvent(this, this.handlePurgeSubmit)}
1329           >
1330             <PurgeWarning />
1331             <label className="sr-only" htmlFor="purge-reason">
1332               {i18n.t("reason")}
1333             </label>
1334             <input
1335               type="text"
1336               id="purge-reason"
1337               className="form-control mr-2"
1338               placeholder={i18n.t("reason")}
1339               value={this.state.purgeReason}
1340               onInput={linkEvent(this, this.handlePurgeReasonChange)}
1341             />
1342             {this.state.purgeLoading ? (
1343               <Spinner />
1344             ) : (
1345               <button
1346                 type="submit"
1347                 className="btn btn-secondary"
1348                 aria-label={purgeTypeText}
1349               >
1350                 {this.state.purgeLoading ? <Spinner /> : { purgeTypeText }}
1351               </button>
1352             )}
1353           </form>
1354         )}
1355       </>
1356     );
1357   }
1358
1359   mobileThumbnail() {
1360     const post = this.postView.post;
1361     return post.thumbnail_url || (post.url && isImage(post.url)) ? (
1362       <div className="row">
1363         <div className={`${this.state.imageExpanded ? "col-12" : "col-8"}`}>
1364           {this.postTitleLine()}
1365         </div>
1366         <div className="col-4">
1367           {/* Post body prev or thumbnail */}
1368           {!this.state.imageExpanded && this.thumbnail()}
1369         </div>
1370       </div>
1371     ) : (
1372       this.postTitleLine()
1373     );
1374   }
1375
1376   showMobilePreview() {
1377     const { body, id } = this.postView.post;
1378
1379     return !this.showBody && body ? (
1380       <Link className="text-body" to={`/post/${id}`}>
1381         <div className="md-div mb-1 preview-lines">{body}</div>
1382       </Link>
1383     ) : (
1384       <></>
1385     );
1386   }
1387
1388   listing() {
1389     return (
1390       <>
1391         {/* The mobile view*/}
1392         <div className="d-block d-sm-none">
1393           <article className="row post-container">
1394             <div className="col-12">
1395               {this.createdLine()}
1396
1397               {/* If it has a thumbnail, do a right aligned thumbnail */}
1398               {this.mobileThumbnail()}
1399
1400               {/* Show a preview of the post body */}
1401               {this.showMobilePreview()}
1402
1403               {this.commentsLine(true)}
1404               {this.userActionsLine()}
1405               {this.duplicatesLine()}
1406               {this.removeAndBanDialogs()}
1407             </div>
1408           </article>
1409         </div>
1410
1411         {/* The larger view*/}
1412         <div className="d-none d-sm-block">
1413           <article className="row post-container">
1414             {!this.props.viewOnly && this.voteBar()}
1415             <div className="col-sm-2 pr-0 post-media">
1416               <div className="">{this.thumbnail()}</div>
1417             </div>
1418             <div className="col-12 col-sm-9">
1419               <div className="row">
1420                 <div className="col-12">
1421                   {this.postTitleLine()}
1422                   {this.createdLine()}
1423                   {this.commentsLine()}
1424                   {this.duplicatesLine()}
1425                   {this.userActionsLine()}
1426                   {this.removeAndBanDialogs()}
1427                 </div>
1428               </div>
1429             </div>
1430           </article>
1431         </div>
1432       </>
1433     );
1434   }
1435
1436   private get myPost(): boolean {
1437     return (
1438       this.postView.creator.id ==
1439       UserService.Instance.myUserInfo?.local_user_view.person.id
1440     );
1441   }
1442   handleEditClick(i: PostListing) {
1443     i.setState({ showEdit: true });
1444   }
1445
1446   handleEditCancel() {
1447     this.setState({ showEdit: false });
1448   }
1449
1450   // The actual editing is done in the receive for post
1451   handleEditPost(form: EditPost) {
1452     this.setState({ showEdit: false });
1453     this.props.onPostEdit(form);
1454   }
1455
1456   handleShare(i: PostListing) {
1457     const { name, body, id } = i.props.post_view.post;
1458     share({
1459       title: name,
1460       text: body?.slice(0, 50),
1461       url: `${getHttpBase()}/post/${id}`,
1462     });
1463   }
1464
1465   handleShowReportDialog(i: PostListing) {
1466     i.setState({ showReportDialog: !i.state.showReportDialog });
1467   }
1468
1469   handleReportReasonChange(i: PostListing, event: any) {
1470     i.setState({ reportReason: event.target.value });
1471   }
1472
1473   handleReportSubmit(i: PostListing, event: any) {
1474     event.preventDefault();
1475     i.setState({ reportLoading: true });
1476     i.props.onPostReport({
1477       post_id: i.postView.post.id,
1478       reason: i.state.reportReason ?? "",
1479       auth: myAuthRequired(),
1480     });
1481   }
1482
1483   handleBlockPersonClick(i: PostListing) {
1484     i.setState({ blockLoading: true });
1485     i.props.onBlockPerson({
1486       person_id: i.postView.creator.id,
1487       block: true,
1488       auth: myAuthRequired(),
1489     });
1490   }
1491
1492   handleDeleteClick(i: PostListing) {
1493     i.setState({ deleteLoading: true });
1494     i.props.onDeletePost({
1495       post_id: i.postView.post.id,
1496       deleted: !i.postView.post.deleted,
1497       auth: myAuthRequired(),
1498     });
1499   }
1500
1501   handleSavePostClick(i: PostListing) {
1502     i.setState({ saveLoading: true });
1503     i.props.onSavePost({
1504       post_id: i.postView.post.id,
1505       save: !i.postView.saved,
1506       auth: myAuthRequired(),
1507     });
1508   }
1509
1510   get crossPostParams(): PostFormParams {
1511     const queryParams: PostFormParams = {};
1512     const { name, url } = this.postView.post;
1513
1514     queryParams.name = name;
1515
1516     if (url) {
1517       queryParams.url = url;
1518     }
1519
1520     const crossPostBody = this.crossPostBody();
1521     if (crossPostBody) {
1522       queryParams.body = crossPostBody;
1523     }
1524
1525     return queryParams;
1526   }
1527
1528   crossPostBody(): string | undefined {
1529     const post = this.postView.post;
1530     const body = post.body;
1531
1532     return body
1533       ? `${i18n.t("cross_posted_from")} ${post.ap_id}\n\n${body.replace(
1534           /^/gm,
1535           "> "
1536         )}`
1537       : undefined;
1538   }
1539
1540   get showBody(): boolean {
1541     return this.props.showBody || this.state.showBody;
1542   }
1543
1544   handleModRemoveShow(i: PostListing) {
1545     i.setState({
1546       showRemoveDialog: !i.state.showRemoveDialog,
1547       showBanDialog: false,
1548     });
1549   }
1550
1551   handleModRemoveReasonChange(i: PostListing, event: any) {
1552     i.setState({ removeReason: event.target.value });
1553   }
1554
1555   handleModRemoveDataChange(i: PostListing, event: any) {
1556     i.setState({ removeData: event.target.checked });
1557   }
1558
1559   handleModRemoveSubmit(i: PostListing, event: any) {
1560     event.preventDefault();
1561     i.setState({ removeLoading: true });
1562     i.props.onRemovePost({
1563       post_id: i.postView.post.id,
1564       removed: !i.postView.post.removed,
1565       auth: myAuthRequired(),
1566     });
1567   }
1568
1569   handleModLock(i: PostListing) {
1570     i.setState({ lockLoading: true });
1571     i.props.onLockPost({
1572       post_id: i.postView.post.id,
1573       locked: !i.postView.post.locked,
1574       auth: myAuthRequired(),
1575     });
1576   }
1577
1578   handleModFeaturePostLocal(i: PostListing) {
1579     i.setState({ featureLocalLoading: true });
1580     i.props.onFeaturePost({
1581       post_id: i.postView.post.id,
1582       featured: !i.postView.post.featured_local,
1583       feature_type: "Local",
1584       auth: myAuthRequired(),
1585     });
1586   }
1587
1588   handleModFeaturePostCommunity(i: PostListing) {
1589     i.setState({ featureCommunityLoading: true });
1590     i.props.onFeaturePost({
1591       post_id: i.postView.post.id,
1592       featured: !i.postView.post.featured_community,
1593       feature_type: "Community",
1594       auth: myAuthRequired(),
1595     });
1596   }
1597
1598   handleModBanFromCommunityShow(i: PostListing) {
1599     i.setState({
1600       showBanDialog: true,
1601       banType: BanType.Community,
1602       showRemoveDialog: false,
1603     });
1604   }
1605
1606   handleModBanShow(i: PostListing) {
1607     i.setState({
1608       showBanDialog: true,
1609       banType: BanType.Site,
1610       showRemoveDialog: false,
1611     });
1612   }
1613
1614   handlePurgePersonShow(i: PostListing) {
1615     i.setState({
1616       showPurgeDialog: true,
1617       purgeType: PurgeType.Person,
1618       showRemoveDialog: false,
1619     });
1620   }
1621
1622   handlePurgePostShow(i: PostListing) {
1623     i.setState({
1624       showPurgeDialog: true,
1625       purgeType: PurgeType.Post,
1626       showRemoveDialog: false,
1627     });
1628   }
1629
1630   handlePurgeReasonChange(i: PostListing, event: any) {
1631     i.setState({ purgeReason: event.target.value });
1632   }
1633
1634   handlePurgeSubmit(i: PostListing, event: any) {
1635     event.preventDefault();
1636     i.setState({ purgeLoading: true });
1637     if (i.state.purgeType == PurgeType.Person) {
1638       i.props.onPurgePerson({
1639         person_id: i.postView.creator.id,
1640         reason: i.state.purgeReason,
1641         auth: myAuthRequired(),
1642       });
1643     } else if (i.state.purgeType == PurgeType.Post) {
1644       i.props.onPurgePost({
1645         post_id: i.postView.post.id,
1646         reason: i.state.purgeReason,
1647         auth: myAuthRequired(),
1648       });
1649     }
1650   }
1651
1652   handleModBanReasonChange(i: PostListing, event: any) {
1653     i.setState({ banReason: event.target.value });
1654   }
1655
1656   handleModBanExpireDaysChange(i: PostListing, event: any) {
1657     i.setState({ banExpireDays: event.target.value });
1658   }
1659
1660   handleModBanFromCommunitySubmit(i: PostListing, event: any) {
1661     i.setState({ banType: BanType.Community });
1662     i.handleModBanBothSubmit(i, event);
1663   }
1664
1665   handleModBanSubmit(i: PostListing, event: any) {
1666     i.setState({ banType: BanType.Site });
1667     i.handleModBanBothSubmit(i, event);
1668   }
1669
1670   handleModBanBothSubmit(i: PostListing, event: any) {
1671     event.preventDefault();
1672     i.setState({ banLoading: true });
1673
1674     const ban = !i.props.post_view.creator_banned_from_community;
1675     // If its an unban, restore all their data
1676     if (ban == false) {
1677       i.setState({ removeData: false });
1678     }
1679     const person_id = i.props.post_view.creator.id;
1680     const remove_data = i.state.removeData;
1681     const reason = i.state.banReason;
1682     const expires = futureDaysToUnixTime(i.state.banExpireDays);
1683
1684     if (i.state.banType == BanType.Community) {
1685       const community_id = i.postView.community.id;
1686       i.props.onBanPersonFromCommunity({
1687         community_id,
1688         person_id,
1689         ban,
1690         remove_data,
1691         reason,
1692         expires,
1693         auth: myAuthRequired(),
1694       });
1695     } else {
1696       i.props.onBanPerson({
1697         person_id,
1698         ban,
1699         remove_data,
1700         reason,
1701         expires,
1702         auth: myAuthRequired(),
1703       });
1704     }
1705   }
1706
1707   handleAddModToCommunity(i: PostListing) {
1708     i.setState({ addModLoading: true });
1709     i.props.onAddModToCommunity({
1710       community_id: i.postView.community.id,
1711       person_id: i.postView.creator.id,
1712       added: !i.creatorIsMod_,
1713       auth: myAuthRequired(),
1714     });
1715   }
1716
1717   handleAddAdmin(i: PostListing) {
1718     i.setState({ addAdminLoading: true });
1719     i.props.onAddAdmin({
1720       person_id: i.postView.creator.id,
1721       added: !i.creatorIsAdmin_,
1722       auth: myAuthRequired(),
1723     });
1724   }
1725
1726   handleShowConfirmTransferCommunity(i: PostListing) {
1727     i.setState({ showConfirmTransferCommunity: true });
1728   }
1729
1730   handleCancelShowConfirmTransferCommunity(i: PostListing) {
1731     i.setState({ showConfirmTransferCommunity: false });
1732   }
1733
1734   handleTransferCommunity(i: PostListing) {
1735     i.setState({ transferLoading: true });
1736     i.props.onTransferCommunity({
1737       community_id: i.postView.community.id,
1738       person_id: i.postView.creator.id,
1739       auth: myAuthRequired(),
1740     });
1741   }
1742
1743   handleShowConfirmTransferSite(i: PostListing) {
1744     i.setState({ showConfirmTransferSite: true });
1745   }
1746
1747   handleCancelShowConfirmTransferSite(i: PostListing) {
1748     i.setState({ showConfirmTransferSite: false });
1749   }
1750
1751   handleImageExpandClick(i: PostListing, event: any) {
1752     event.preventDefault();
1753     i.setState({ imageExpanded: !i.state.imageExpanded });
1754     setupTippy();
1755   }
1756
1757   handleViewSource(i: PostListing) {
1758     i.setState({ viewSource: !i.state.viewSource });
1759   }
1760
1761   handleShowAdvanced(i: PostListing) {
1762     i.setState({ showAdvanced: !i.state.showAdvanced });
1763     setupTippy();
1764   }
1765
1766   handleShowMoreMobile(i: PostListing) {
1767     i.setState({
1768       showMoreMobile: !i.state.showMoreMobile,
1769       showAdvanced: !i.state.showAdvanced,
1770     });
1771     setupTippy();
1772   }
1773
1774   handleShowBody(i: PostListing) {
1775     i.setState({ showBody: !i.state.showBody });
1776     setupTippy();
1777   }
1778
1779   handleUpvote(i: PostListing) {
1780     i.setState({ upvoteLoading: true });
1781     i.props.onPostVote({
1782       post_id: i.postView.post.id,
1783       score: newVote(VoteType.Upvote, i.props.post_view.my_vote),
1784       auth: myAuthRequired(),
1785     });
1786   }
1787
1788   handleDownvote(i: PostListing) {
1789     i.setState({ downvoteLoading: true });
1790     i.props.onPostVote({
1791       post_id: i.postView.post.id,
1792       score: newVote(VoteType.Downvote, i.props.post_view.my_vote),
1793       auth: myAuthRequired(),
1794     });
1795   }
1796
1797   get pointsTippy(): string {
1798     const points = i18n.t("number_of_points", {
1799       count: Number(this.postView.counts.score),
1800       formattedCount: Number(this.postView.counts.score),
1801     });
1802
1803     const upvotes = i18n.t("number_of_upvotes", {
1804       count: Number(this.postView.counts.upvotes),
1805       formattedCount: Number(this.postView.counts.upvotes),
1806     });
1807
1808     const downvotes = i18n.t("number_of_downvotes", {
1809       count: Number(this.postView.counts.downvotes),
1810       formattedCount: Number(this.postView.counts.downvotes),
1811     });
1812
1813     return `${points} • ${upvotes} • ${downvotes}`;
1814   }
1815
1816   get canModOnSelf_(): boolean {
1817     return canMod(
1818       this.postView.creator.id,
1819       this.props.moderators,
1820       this.props.admins,
1821       undefined,
1822       true
1823     );
1824   }
1825
1826   get canMod_(): boolean {
1827     return canMod(
1828       this.postView.creator.id,
1829       this.props.moderators,
1830       this.props.admins
1831     );
1832   }
1833
1834   get canAdmin_(): boolean {
1835     return canAdmin(this.postView.creator.id, this.props.admins);
1836   }
1837
1838   get creatorIsMod_(): boolean {
1839     return isMod(this.postView.creator.id, this.props.moderators);
1840   }
1841
1842   get creatorIsAdmin_(): boolean {
1843     return isAdmin(this.postView.creator.id, this.props.admins);
1844   }
1845 }